1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License as published by 4 * the Free Software Foundation; either version 2 of the License, or 5 * (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, see <http://www.gnu.org/licenses/>. 14 */ 15 16 #include "qemu/osdep.h" 17 18 #include <sys/signalfd.h> 19 #include <linux/unistd.h> 20 #include <linux/audit.h> 21 #ifdef CONFIG_INOTIFY 22 #include <sys/inotify.h> 23 #endif 24 #include <linux/netlink.h> 25 #ifdef CONFIG_RTNETLINK 26 #include <linux/rtnetlink.h> 27 #include <linux/if_bridge.h> 28 #endif 29 #include "qemu.h" 30 #include "fd-trans.h" 31 32 enum { 33 QEMU_IFLA_BR_UNSPEC, 34 QEMU_IFLA_BR_FORWARD_DELAY, 35 QEMU_IFLA_BR_HELLO_TIME, 36 QEMU_IFLA_BR_MAX_AGE, 37 QEMU_IFLA_BR_AGEING_TIME, 38 QEMU_IFLA_BR_STP_STATE, 39 QEMU_IFLA_BR_PRIORITY, 40 QEMU_IFLA_BR_VLAN_FILTERING, 41 QEMU_IFLA_BR_VLAN_PROTOCOL, 42 QEMU_IFLA_BR_GROUP_FWD_MASK, 43 QEMU_IFLA_BR_ROOT_ID, 44 QEMU_IFLA_BR_BRIDGE_ID, 45 QEMU_IFLA_BR_ROOT_PORT, 46 QEMU_IFLA_BR_ROOT_PATH_COST, 47 QEMU_IFLA_BR_TOPOLOGY_CHANGE, 48 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED, 49 QEMU_IFLA_BR_HELLO_TIMER, 50 QEMU_IFLA_BR_TCN_TIMER, 51 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER, 52 QEMU_IFLA_BR_GC_TIMER, 53 QEMU_IFLA_BR_GROUP_ADDR, 54 QEMU_IFLA_BR_FDB_FLUSH, 55 QEMU_IFLA_BR_MCAST_ROUTER, 56 QEMU_IFLA_BR_MCAST_SNOOPING, 57 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR, 58 QEMU_IFLA_BR_MCAST_QUERIER, 59 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY, 60 QEMU_IFLA_BR_MCAST_HASH_MAX, 61 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT, 62 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT, 63 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL, 64 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL, 65 QEMU_IFLA_BR_MCAST_QUERIER_INTVL, 66 QEMU_IFLA_BR_MCAST_QUERY_INTVL, 67 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, 68 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL, 69 QEMU_IFLA_BR_NF_CALL_IPTABLES, 70 QEMU_IFLA_BR_NF_CALL_IP6TABLES, 71 QEMU_IFLA_BR_NF_CALL_ARPTABLES, 72 QEMU_IFLA_BR_VLAN_DEFAULT_PVID, 73 QEMU_IFLA_BR_PAD, 74 QEMU_IFLA_BR_VLAN_STATS_ENABLED, 75 QEMU_IFLA_BR_MCAST_STATS_ENABLED, 76 QEMU_IFLA_BR_MCAST_IGMP_VERSION, 77 QEMU_IFLA_BR_MCAST_MLD_VERSION, 78 QEMU_IFLA_BR_VLAN_STATS_PER_PORT, 79 QEMU_IFLA_BR_MULTI_BOOLOPT, 80 QEMU___IFLA_BR_MAX, 81 }; 82 83 enum { 84 QEMU_IFLA_UNSPEC, 85 QEMU_IFLA_ADDRESS, 86 QEMU_IFLA_BROADCAST, 87 QEMU_IFLA_IFNAME, 88 QEMU_IFLA_MTU, 89 QEMU_IFLA_LINK, 90 QEMU_IFLA_QDISC, 91 QEMU_IFLA_STATS, 92 QEMU_IFLA_COST, 93 QEMU_IFLA_PRIORITY, 94 QEMU_IFLA_MASTER, 95 QEMU_IFLA_WIRELESS, 96 QEMU_IFLA_PROTINFO, 97 QEMU_IFLA_TXQLEN, 98 QEMU_IFLA_MAP, 99 QEMU_IFLA_WEIGHT, 100 QEMU_IFLA_OPERSTATE, 101 QEMU_IFLA_LINKMODE, 102 QEMU_IFLA_LINKINFO, 103 QEMU_IFLA_NET_NS_PID, 104 QEMU_IFLA_IFALIAS, 105 QEMU_IFLA_NUM_VF, 106 QEMU_IFLA_VFINFO_LIST, 107 QEMU_IFLA_STATS64, 108 QEMU_IFLA_VF_PORTS, 109 QEMU_IFLA_PORT_SELF, 110 QEMU_IFLA_AF_SPEC, 111 QEMU_IFLA_GROUP, 112 QEMU_IFLA_NET_NS_FD, 113 QEMU_IFLA_EXT_MASK, 114 QEMU_IFLA_PROMISCUITY, 115 QEMU_IFLA_NUM_TX_QUEUES, 116 QEMU_IFLA_NUM_RX_QUEUES, 117 QEMU_IFLA_CARRIER, 118 QEMU_IFLA_PHYS_PORT_ID, 119 QEMU_IFLA_CARRIER_CHANGES, 120 QEMU_IFLA_PHYS_SWITCH_ID, 121 QEMU_IFLA_LINK_NETNSID, 122 QEMU_IFLA_PHYS_PORT_NAME, 123 QEMU_IFLA_PROTO_DOWN, 124 QEMU_IFLA_GSO_MAX_SEGS, 125 QEMU_IFLA_GSO_MAX_SIZE, 126 QEMU_IFLA_PAD, 127 QEMU_IFLA_XDP, 128 QEMU_IFLA_EVENT, 129 QEMU_IFLA_NEW_NETNSID, 130 QEMU_IFLA_IF_NETNSID, 131 QEMU_IFLA_CARRIER_UP_COUNT, 132 QEMU_IFLA_CARRIER_DOWN_COUNT, 133 QEMU_IFLA_NEW_IFINDEX, 134 QEMU_IFLA_MIN_MTU, 135 QEMU_IFLA_MAX_MTU, 136 QEMU___IFLA_MAX 137 }; 138 139 enum { 140 QEMU_IFLA_BRPORT_UNSPEC, 141 QEMU_IFLA_BRPORT_STATE, 142 QEMU_IFLA_BRPORT_PRIORITY, 143 QEMU_IFLA_BRPORT_COST, 144 QEMU_IFLA_BRPORT_MODE, 145 QEMU_IFLA_BRPORT_GUARD, 146 QEMU_IFLA_BRPORT_PROTECT, 147 QEMU_IFLA_BRPORT_FAST_LEAVE, 148 QEMU_IFLA_BRPORT_LEARNING, 149 QEMU_IFLA_BRPORT_UNICAST_FLOOD, 150 QEMU_IFLA_BRPORT_PROXYARP, 151 QEMU_IFLA_BRPORT_LEARNING_SYNC, 152 QEMU_IFLA_BRPORT_PROXYARP_WIFI, 153 QEMU_IFLA_BRPORT_ROOT_ID, 154 QEMU_IFLA_BRPORT_BRIDGE_ID, 155 QEMU_IFLA_BRPORT_DESIGNATED_PORT, 156 QEMU_IFLA_BRPORT_DESIGNATED_COST, 157 QEMU_IFLA_BRPORT_ID, 158 QEMU_IFLA_BRPORT_NO, 159 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK, 160 QEMU_IFLA_BRPORT_CONFIG_PENDING, 161 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER, 162 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER, 163 QEMU_IFLA_BRPORT_HOLD_TIMER, 164 QEMU_IFLA_BRPORT_FLUSH, 165 QEMU_IFLA_BRPORT_MULTICAST_ROUTER, 166 QEMU_IFLA_BRPORT_PAD, 167 QEMU_IFLA_BRPORT_MCAST_FLOOD, 168 QEMU_IFLA_BRPORT_MCAST_TO_UCAST, 169 QEMU_IFLA_BRPORT_VLAN_TUNNEL, 170 QEMU_IFLA_BRPORT_BCAST_FLOOD, 171 QEMU_IFLA_BRPORT_GROUP_FWD_MASK, 172 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS, 173 QEMU_IFLA_BRPORT_ISOLATED, 174 QEMU_IFLA_BRPORT_BACKUP_PORT, 175 QEMU___IFLA_BRPORT_MAX 176 }; 177 178 enum { 179 QEMU_IFLA_TUN_UNSPEC, 180 QEMU_IFLA_TUN_OWNER, 181 QEMU_IFLA_TUN_GROUP, 182 QEMU_IFLA_TUN_TYPE, 183 QEMU_IFLA_TUN_PI, 184 QEMU_IFLA_TUN_VNET_HDR, 185 QEMU_IFLA_TUN_PERSIST, 186 QEMU_IFLA_TUN_MULTI_QUEUE, 187 QEMU_IFLA_TUN_NUM_QUEUES, 188 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES, 189 QEMU___IFLA_TUN_MAX, 190 }; 191 192 enum { 193 QEMU_IFLA_INFO_UNSPEC, 194 QEMU_IFLA_INFO_KIND, 195 QEMU_IFLA_INFO_DATA, 196 QEMU_IFLA_INFO_XSTATS, 197 QEMU_IFLA_INFO_SLAVE_KIND, 198 QEMU_IFLA_INFO_SLAVE_DATA, 199 QEMU___IFLA_INFO_MAX, 200 }; 201 202 enum { 203 QEMU_IFLA_INET_UNSPEC, 204 QEMU_IFLA_INET_CONF, 205 QEMU___IFLA_INET_MAX, 206 }; 207 208 enum { 209 QEMU_IFLA_INET6_UNSPEC, 210 QEMU_IFLA_INET6_FLAGS, 211 QEMU_IFLA_INET6_CONF, 212 QEMU_IFLA_INET6_STATS, 213 QEMU_IFLA_INET6_MCAST, 214 QEMU_IFLA_INET6_CACHEINFO, 215 QEMU_IFLA_INET6_ICMP6STATS, 216 QEMU_IFLA_INET6_TOKEN, 217 QEMU_IFLA_INET6_ADDR_GEN_MODE, 218 QEMU___IFLA_INET6_MAX 219 }; 220 221 enum { 222 QEMU_IFLA_XDP_UNSPEC, 223 QEMU_IFLA_XDP_FD, 224 QEMU_IFLA_XDP_ATTACHED, 225 QEMU_IFLA_XDP_FLAGS, 226 QEMU_IFLA_XDP_PROG_ID, 227 QEMU___IFLA_XDP_MAX, 228 }; 229 230 enum { 231 QEMU_RTA_UNSPEC, 232 QEMU_RTA_DST, 233 QEMU_RTA_SRC, 234 QEMU_RTA_IIF, 235 QEMU_RTA_OIF, 236 QEMU_RTA_GATEWAY, 237 QEMU_RTA_PRIORITY, 238 QEMU_RTA_PREFSRC, 239 QEMU_RTA_METRICS, 240 QEMU_RTA_MULTIPATH, 241 QEMU_RTA_PROTOINFO, /* no longer used */ 242 QEMU_RTA_FLOW, 243 QEMU_RTA_CACHEINFO, 244 QEMU_RTA_SESSION, /* no longer used */ 245 QEMU_RTA_MP_ALGO, /* no longer used */ 246 QEMU_RTA_TABLE, 247 QEMU_RTA_MARK, 248 QEMU_RTA_MFC_STATS, 249 QEMU_RTA_VIA, 250 QEMU_RTA_NEWDST, 251 QEMU_RTA_PREF, 252 QEMU_RTA_ENCAP_TYPE, 253 QEMU_RTA_ENCAP, 254 QEMU_RTA_EXPIRES, 255 QEMU_RTA_PAD, 256 QEMU_RTA_UID, 257 QEMU_RTA_TTL_PROPAGATE, 258 QEMU_RTA_IP_PROTO, 259 QEMU_RTA_SPORT, 260 QEMU_RTA_DPORT, 261 QEMU___RTA_MAX 262 }; 263 264 TargetFdTrans **target_fd_trans; 265 unsigned int target_fd_max; 266 267 static void tswap_nlmsghdr(struct nlmsghdr *nlh) 268 { 269 nlh->nlmsg_len = tswap32(nlh->nlmsg_len); 270 nlh->nlmsg_type = tswap16(nlh->nlmsg_type); 271 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags); 272 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq); 273 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid); 274 } 275 276 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh, 277 size_t len, 278 abi_long (*host_to_target_nlmsg) 279 (struct nlmsghdr *)) 280 { 281 uint32_t nlmsg_len; 282 uint32_t aligned_nlmsg_len; 283 abi_long ret; 284 285 while (len > sizeof(struct nlmsghdr)) { 286 287 nlmsg_len = nlh->nlmsg_len; 288 if (nlmsg_len < sizeof(struct nlmsghdr) || 289 nlmsg_len > len) { 290 break; 291 } 292 293 switch (nlh->nlmsg_type) { 294 case NLMSG_DONE: 295 tswap_nlmsghdr(nlh); 296 return 0; 297 case NLMSG_NOOP: 298 break; 299 case NLMSG_ERROR: 300 { 301 struct nlmsgerr *e = NLMSG_DATA(nlh); 302 e->error = tswap32(e->error); 303 tswap_nlmsghdr(&e->msg); 304 tswap_nlmsghdr(nlh); 305 return 0; 306 } 307 default: 308 ret = host_to_target_nlmsg(nlh); 309 if (ret < 0) { 310 tswap_nlmsghdr(nlh); 311 return ret; 312 } 313 break; 314 } 315 tswap_nlmsghdr(nlh); 316 317 aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len); 318 if (aligned_nlmsg_len >= len) { 319 break; 320 } 321 len -= aligned_nlmsg_len; 322 nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len); 323 } 324 return 0; 325 } 326 327 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh, 328 size_t len, 329 abi_long (*target_to_host_nlmsg) 330 (struct nlmsghdr *)) 331 { 332 uint32_t aligned_nlmsg_len; 333 int ret; 334 335 while (len > sizeof(struct nlmsghdr)) { 336 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) || 337 tswap32(nlh->nlmsg_len) > len) { 338 break; 339 } 340 tswap_nlmsghdr(nlh); 341 switch (nlh->nlmsg_type) { 342 case NLMSG_DONE: 343 return 0; 344 case NLMSG_NOOP: 345 break; 346 case NLMSG_ERROR: 347 { 348 struct nlmsgerr *e = NLMSG_DATA(nlh); 349 e->error = tswap32(e->error); 350 tswap_nlmsghdr(&e->msg); 351 return 0; 352 } 353 default: 354 ret = target_to_host_nlmsg(nlh); 355 if (ret < 0) { 356 return ret; 357 } 358 } 359 360 aligned_nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len); 361 if (aligned_nlmsg_len >= len) { 362 break; 363 } 364 len -= aligned_nlmsg_len; 365 nlh = (struct nlmsghdr *)(((char *)nlh) + aligned_nlmsg_len); 366 } 367 return 0; 368 } 369 370 #ifdef CONFIG_RTNETLINK 371 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr, 372 size_t len, void *context, 373 abi_long (*host_to_target_nlattr) 374 (struct nlattr *, 375 void *context)) 376 { 377 unsigned short nla_len; 378 unsigned short aligned_nla_len; 379 abi_long ret; 380 381 while (len > sizeof(struct nlattr)) { 382 nla_len = nlattr->nla_len; 383 if (nla_len < sizeof(struct nlattr) || 384 nla_len > len) { 385 break; 386 } 387 ret = host_to_target_nlattr(nlattr, context); 388 nlattr->nla_len = tswap16(nlattr->nla_len); 389 nlattr->nla_type = tswap16(nlattr->nla_type); 390 if (ret < 0) { 391 return ret; 392 } 393 394 aligned_nla_len = NLA_ALIGN(nla_len); 395 if (aligned_nla_len >= len) { 396 break; 397 } 398 len -= aligned_nla_len; 399 nlattr = (struct nlattr *)(((char *)nlattr) + aligned_nla_len); 400 } 401 return 0; 402 } 403 404 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr, 405 size_t len, 406 abi_long (*host_to_target_rtattr) 407 (struct rtattr *)) 408 { 409 unsigned short rta_len; 410 unsigned short aligned_rta_len; 411 abi_long ret; 412 413 while (len > sizeof(struct rtattr)) { 414 rta_len = rtattr->rta_len; 415 if (rta_len < sizeof(struct rtattr) || 416 rta_len > len) { 417 break; 418 } 419 ret = host_to_target_rtattr(rtattr); 420 rtattr->rta_len = tswap16(rtattr->rta_len); 421 rtattr->rta_type = tswap16(rtattr->rta_type); 422 if (ret < 0) { 423 return ret; 424 } 425 426 aligned_rta_len = RTA_ALIGN(rta_len); 427 if (aligned_rta_len >= len) { 428 break; 429 } 430 len -= aligned_rta_len; 431 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len); 432 } 433 return 0; 434 } 435 436 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN) 437 438 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr, 439 void *context) 440 { 441 uint16_t *u16; 442 uint32_t *u32; 443 uint64_t *u64; 444 445 switch (nlattr->nla_type) { 446 /* no data */ 447 case QEMU_IFLA_BR_FDB_FLUSH: 448 break; 449 /* binary */ 450 case QEMU_IFLA_BR_GROUP_ADDR: 451 break; 452 /* uint8_t */ 453 case QEMU_IFLA_BR_VLAN_FILTERING: 454 case QEMU_IFLA_BR_TOPOLOGY_CHANGE: 455 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED: 456 case QEMU_IFLA_BR_MCAST_ROUTER: 457 case QEMU_IFLA_BR_MCAST_SNOOPING: 458 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR: 459 case QEMU_IFLA_BR_MCAST_QUERIER: 460 case QEMU_IFLA_BR_NF_CALL_IPTABLES: 461 case QEMU_IFLA_BR_NF_CALL_IP6TABLES: 462 case QEMU_IFLA_BR_NF_CALL_ARPTABLES: 463 case QEMU_IFLA_BR_VLAN_STATS_ENABLED: 464 case QEMU_IFLA_BR_MCAST_STATS_ENABLED: 465 case QEMU_IFLA_BR_MCAST_IGMP_VERSION: 466 case QEMU_IFLA_BR_MCAST_MLD_VERSION: 467 case QEMU_IFLA_BR_VLAN_STATS_PER_PORT: 468 break; 469 /* uint16_t */ 470 case QEMU_IFLA_BR_PRIORITY: 471 case QEMU_IFLA_BR_VLAN_PROTOCOL: 472 case QEMU_IFLA_BR_GROUP_FWD_MASK: 473 case QEMU_IFLA_BR_ROOT_PORT: 474 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID: 475 u16 = NLA_DATA(nlattr); 476 *u16 = tswap16(*u16); 477 break; 478 /* uint32_t */ 479 case QEMU_IFLA_BR_FORWARD_DELAY: 480 case QEMU_IFLA_BR_HELLO_TIME: 481 case QEMU_IFLA_BR_MAX_AGE: 482 case QEMU_IFLA_BR_AGEING_TIME: 483 case QEMU_IFLA_BR_STP_STATE: 484 case QEMU_IFLA_BR_ROOT_PATH_COST: 485 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY: 486 case QEMU_IFLA_BR_MCAST_HASH_MAX: 487 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT: 488 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT: 489 u32 = NLA_DATA(nlattr); 490 *u32 = tswap32(*u32); 491 break; 492 /* uint64_t */ 493 case QEMU_IFLA_BR_HELLO_TIMER: 494 case QEMU_IFLA_BR_TCN_TIMER: 495 case QEMU_IFLA_BR_GC_TIMER: 496 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER: 497 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL: 498 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL: 499 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL: 500 case QEMU_IFLA_BR_MCAST_QUERY_INTVL: 501 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL: 502 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL: 503 u64 = NLA_DATA(nlattr); 504 *u64 = tswap64(*u64); 505 break; 506 /* ifla_bridge_id: uin8_t[] */ 507 case QEMU_IFLA_BR_ROOT_ID: 508 case QEMU_IFLA_BR_BRIDGE_ID: 509 break; 510 /* br_boolopt_multi { uint32_t, uint32_t } */ 511 case QEMU_IFLA_BR_MULTI_BOOLOPT: 512 u32 = NLA_DATA(nlattr); 513 u32[0] = tswap32(u32[0]); /* optval */ 514 u32[1] = tswap32(u32[1]); /* optmask */ 515 break; 516 default: 517 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BR type %d\n", 518 nlattr->nla_type); 519 break; 520 } 521 return 0; 522 } 523 524 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr, 525 void *context) 526 { 527 uint16_t *u16; 528 uint32_t *u32; 529 uint64_t *u64; 530 531 switch (nlattr->nla_type) { 532 /* uint8_t */ 533 case QEMU_IFLA_BRPORT_STATE: 534 case QEMU_IFLA_BRPORT_MODE: 535 case QEMU_IFLA_BRPORT_GUARD: 536 case QEMU_IFLA_BRPORT_PROTECT: 537 case QEMU_IFLA_BRPORT_FAST_LEAVE: 538 case QEMU_IFLA_BRPORT_LEARNING: 539 case QEMU_IFLA_BRPORT_UNICAST_FLOOD: 540 case QEMU_IFLA_BRPORT_PROXYARP: 541 case QEMU_IFLA_BRPORT_LEARNING_SYNC: 542 case QEMU_IFLA_BRPORT_PROXYARP_WIFI: 543 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK: 544 case QEMU_IFLA_BRPORT_CONFIG_PENDING: 545 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER: 546 case QEMU_IFLA_BRPORT_MCAST_FLOOD: 547 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST: 548 case QEMU_IFLA_BRPORT_VLAN_TUNNEL: 549 case QEMU_IFLA_BRPORT_BCAST_FLOOD: 550 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS: 551 case QEMU_IFLA_BRPORT_ISOLATED: 552 break; 553 /* uint16_t */ 554 case QEMU_IFLA_BRPORT_PRIORITY: 555 case QEMU_IFLA_BRPORT_DESIGNATED_PORT: 556 case QEMU_IFLA_BRPORT_DESIGNATED_COST: 557 case QEMU_IFLA_BRPORT_ID: 558 case QEMU_IFLA_BRPORT_NO: 559 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK: 560 u16 = NLA_DATA(nlattr); 561 *u16 = tswap16(*u16); 562 break; 563 /* uin32_t */ 564 case QEMU_IFLA_BRPORT_COST: 565 case QEMU_IFLA_BRPORT_BACKUP_PORT: 566 u32 = NLA_DATA(nlattr); 567 *u32 = tswap32(*u32); 568 break; 569 /* uint64_t */ 570 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER: 571 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER: 572 case QEMU_IFLA_BRPORT_HOLD_TIMER: 573 u64 = NLA_DATA(nlattr); 574 *u64 = tswap64(*u64); 575 break; 576 /* ifla_bridge_id: uint8_t[] */ 577 case QEMU_IFLA_BRPORT_ROOT_ID: 578 case QEMU_IFLA_BRPORT_BRIDGE_ID: 579 break; 580 default: 581 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BRPORT type %d\n", 582 nlattr->nla_type); 583 break; 584 } 585 return 0; 586 } 587 588 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr, 589 void *context) 590 { 591 uint32_t *u32; 592 593 switch (nlattr->nla_type) { 594 /* uint8_t */ 595 case QEMU_IFLA_TUN_TYPE: 596 case QEMU_IFLA_TUN_PI: 597 case QEMU_IFLA_TUN_VNET_HDR: 598 case QEMU_IFLA_TUN_PERSIST: 599 case QEMU_IFLA_TUN_MULTI_QUEUE: 600 break; 601 /* uint32_t */ 602 case QEMU_IFLA_TUN_NUM_QUEUES: 603 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES: 604 case QEMU_IFLA_TUN_OWNER: 605 case QEMU_IFLA_TUN_GROUP: 606 u32 = NLA_DATA(nlattr); 607 *u32 = tswap32(*u32); 608 break; 609 default: 610 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_TUN type %d\n", 611 nlattr->nla_type); 612 break; 613 } 614 return 0; 615 } 616 617 struct linkinfo_context { 618 int len; 619 char *name; 620 int slave_len; 621 char *slave_name; 622 }; 623 624 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr, 625 void *context) 626 { 627 struct linkinfo_context *li_context = context; 628 629 switch (nlattr->nla_type) { 630 /* string */ 631 case QEMU_IFLA_INFO_KIND: 632 li_context->name = NLA_DATA(nlattr); 633 li_context->len = nlattr->nla_len - NLA_HDRLEN; 634 break; 635 case QEMU_IFLA_INFO_SLAVE_KIND: 636 li_context->slave_name = NLA_DATA(nlattr); 637 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN; 638 break; 639 /* stats */ 640 case QEMU_IFLA_INFO_XSTATS: 641 /* FIXME: only used by CAN */ 642 break; 643 /* nested */ 644 case QEMU_IFLA_INFO_DATA: 645 if (strncmp(li_context->name, "bridge", 646 li_context->len) == 0) { 647 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), 648 nlattr->nla_len, 649 NULL, 650 host_to_target_data_bridge_nlattr); 651 } else if (strncmp(li_context->name, "tun", 652 li_context->len) == 0) { 653 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), 654 nlattr->nla_len, 655 NULL, 656 host_to_target_data_tun_nlattr); 657 } else { 658 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_KIND %s\n", 659 li_context->name); 660 } 661 break; 662 case QEMU_IFLA_INFO_SLAVE_DATA: 663 if (strncmp(li_context->slave_name, "bridge", 664 li_context->slave_len) == 0) { 665 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), 666 nlattr->nla_len, 667 NULL, 668 host_to_target_slave_data_bridge_nlattr); 669 } else { 670 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n", 671 li_context->slave_name); 672 } 673 break; 674 default: 675 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA_INFO type: %d\n", 676 nlattr->nla_type); 677 break; 678 } 679 680 return 0; 681 } 682 683 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr, 684 void *context) 685 { 686 uint32_t *u32; 687 int i; 688 689 switch (nlattr->nla_type) { 690 case QEMU_IFLA_INET_CONF: 691 u32 = NLA_DATA(nlattr); 692 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32); 693 i++) { 694 u32[i] = tswap32(u32[i]); 695 } 696 break; 697 default: 698 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET type: %d\n", 699 nlattr->nla_type); 700 } 701 return 0; 702 } 703 704 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr, 705 void *context) 706 { 707 uint32_t *u32; 708 uint64_t *u64; 709 struct ifla_cacheinfo *ci; 710 int i; 711 712 switch (nlattr->nla_type) { 713 /* binaries */ 714 case QEMU_IFLA_INET6_TOKEN: 715 break; 716 /* uint8_t */ 717 case QEMU_IFLA_INET6_ADDR_GEN_MODE: 718 break; 719 /* uint32_t */ 720 case QEMU_IFLA_INET6_FLAGS: 721 u32 = NLA_DATA(nlattr); 722 *u32 = tswap32(*u32); 723 break; 724 /* uint32_t[] */ 725 case QEMU_IFLA_INET6_CONF: 726 u32 = NLA_DATA(nlattr); 727 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32); 728 i++) { 729 u32[i] = tswap32(u32[i]); 730 } 731 break; 732 /* ifla_cacheinfo */ 733 case QEMU_IFLA_INET6_CACHEINFO: 734 ci = NLA_DATA(nlattr); 735 ci->max_reasm_len = tswap32(ci->max_reasm_len); 736 ci->tstamp = tswap32(ci->tstamp); 737 ci->reachable_time = tswap32(ci->reachable_time); 738 ci->retrans_time = tswap32(ci->retrans_time); 739 break; 740 /* uint64_t[] */ 741 case QEMU_IFLA_INET6_STATS: 742 case QEMU_IFLA_INET6_ICMP6STATS: 743 u64 = NLA_DATA(nlattr); 744 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64); 745 i++) { 746 u64[i] = tswap64(u64[i]); 747 } 748 break; 749 default: 750 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET6 type: %d\n", 751 nlattr->nla_type); 752 } 753 return 0; 754 } 755 756 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr, 757 void *context) 758 { 759 switch (nlattr->nla_type) { 760 case AF_INET: 761 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len, 762 NULL, 763 host_to_target_data_inet_nlattr); 764 case AF_INET6: 765 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len, 766 NULL, 767 host_to_target_data_inet6_nlattr); 768 default: 769 qemu_log_mask(LOG_UNIMP, "Unknown host AF_SPEC type: %d\n", 770 nlattr->nla_type); 771 break; 772 } 773 return 0; 774 } 775 776 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr, 777 void *context) 778 { 779 uint32_t *u32; 780 781 switch (nlattr->nla_type) { 782 /* uint8_t */ 783 case QEMU_IFLA_XDP_ATTACHED: 784 break; 785 /* uint32_t */ 786 case QEMU_IFLA_XDP_PROG_ID: 787 u32 = NLA_DATA(nlattr); 788 *u32 = tswap32(*u32); 789 break; 790 default: 791 qemu_log_mask( 792 LOG_UNIMP, "Unknown host XDP type: %d\n", nlattr->nla_type); 793 break; 794 } 795 return 0; 796 } 797 798 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr) 799 { 800 uint32_t *u32; 801 struct rtnl_link_stats *st; 802 struct rtnl_link_stats64 *st64; 803 struct rtnl_link_ifmap *map; 804 struct linkinfo_context li_context; 805 806 switch (rtattr->rta_type) { 807 /* binary stream */ 808 case QEMU_IFLA_ADDRESS: 809 case QEMU_IFLA_BROADCAST: 810 /* string */ 811 case QEMU_IFLA_IFNAME: 812 case QEMU_IFLA_QDISC: 813 break; 814 /* uin8_t */ 815 case QEMU_IFLA_OPERSTATE: 816 case QEMU_IFLA_LINKMODE: 817 case QEMU_IFLA_CARRIER: 818 case QEMU_IFLA_PROTO_DOWN: 819 break; 820 /* uint32_t */ 821 case QEMU_IFLA_MTU: 822 case QEMU_IFLA_LINK: 823 case QEMU_IFLA_WEIGHT: 824 case QEMU_IFLA_TXQLEN: 825 case QEMU_IFLA_CARRIER_CHANGES: 826 case QEMU_IFLA_NUM_RX_QUEUES: 827 case QEMU_IFLA_NUM_TX_QUEUES: 828 case QEMU_IFLA_PROMISCUITY: 829 case QEMU_IFLA_EXT_MASK: 830 case QEMU_IFLA_LINK_NETNSID: 831 case QEMU_IFLA_GROUP: 832 case QEMU_IFLA_MASTER: 833 case QEMU_IFLA_NUM_VF: 834 case QEMU_IFLA_GSO_MAX_SEGS: 835 case QEMU_IFLA_GSO_MAX_SIZE: 836 case QEMU_IFLA_CARRIER_UP_COUNT: 837 case QEMU_IFLA_CARRIER_DOWN_COUNT: 838 case QEMU_IFLA_MIN_MTU: 839 case QEMU_IFLA_MAX_MTU: 840 u32 = RTA_DATA(rtattr); 841 *u32 = tswap32(*u32); 842 break; 843 /* struct rtnl_link_stats */ 844 case QEMU_IFLA_STATS: 845 st = RTA_DATA(rtattr); 846 st->rx_packets = tswap32(st->rx_packets); 847 st->tx_packets = tswap32(st->tx_packets); 848 st->rx_bytes = tswap32(st->rx_bytes); 849 st->tx_bytes = tswap32(st->tx_bytes); 850 st->rx_errors = tswap32(st->rx_errors); 851 st->tx_errors = tswap32(st->tx_errors); 852 st->rx_dropped = tswap32(st->rx_dropped); 853 st->tx_dropped = tswap32(st->tx_dropped); 854 st->multicast = tswap32(st->multicast); 855 st->collisions = tswap32(st->collisions); 856 857 /* detailed rx_errors: */ 858 st->rx_length_errors = tswap32(st->rx_length_errors); 859 st->rx_over_errors = tswap32(st->rx_over_errors); 860 st->rx_crc_errors = tswap32(st->rx_crc_errors); 861 st->rx_frame_errors = tswap32(st->rx_frame_errors); 862 st->rx_fifo_errors = tswap32(st->rx_fifo_errors); 863 st->rx_missed_errors = tswap32(st->rx_missed_errors); 864 865 /* detailed tx_errors */ 866 st->tx_aborted_errors = tswap32(st->tx_aborted_errors); 867 st->tx_carrier_errors = tswap32(st->tx_carrier_errors); 868 st->tx_fifo_errors = tswap32(st->tx_fifo_errors); 869 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors); 870 st->tx_window_errors = tswap32(st->tx_window_errors); 871 872 /* for cslip etc */ 873 st->rx_compressed = tswap32(st->rx_compressed); 874 st->tx_compressed = tswap32(st->tx_compressed); 875 break; 876 /* struct rtnl_link_stats64 */ 877 case QEMU_IFLA_STATS64: 878 st64 = RTA_DATA(rtattr); 879 st64->rx_packets = tswap64(st64->rx_packets); 880 st64->tx_packets = tswap64(st64->tx_packets); 881 st64->rx_bytes = tswap64(st64->rx_bytes); 882 st64->tx_bytes = tswap64(st64->tx_bytes); 883 st64->rx_errors = tswap64(st64->rx_errors); 884 st64->tx_errors = tswap64(st64->tx_errors); 885 st64->rx_dropped = tswap64(st64->rx_dropped); 886 st64->tx_dropped = tswap64(st64->tx_dropped); 887 st64->multicast = tswap64(st64->multicast); 888 st64->collisions = tswap64(st64->collisions); 889 890 /* detailed rx_errors: */ 891 st64->rx_length_errors = tswap64(st64->rx_length_errors); 892 st64->rx_over_errors = tswap64(st64->rx_over_errors); 893 st64->rx_crc_errors = tswap64(st64->rx_crc_errors); 894 st64->rx_frame_errors = tswap64(st64->rx_frame_errors); 895 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors); 896 st64->rx_missed_errors = tswap64(st64->rx_missed_errors); 897 898 /* detailed tx_errors */ 899 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors); 900 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors); 901 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors); 902 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors); 903 st64->tx_window_errors = tswap64(st64->tx_window_errors); 904 905 /* for cslip etc */ 906 st64->rx_compressed = tswap64(st64->rx_compressed); 907 st64->tx_compressed = tswap64(st64->tx_compressed); 908 break; 909 /* struct rtnl_link_ifmap */ 910 case QEMU_IFLA_MAP: 911 map = RTA_DATA(rtattr); 912 map->mem_start = tswap64(map->mem_start); 913 map->mem_end = tswap64(map->mem_end); 914 map->base_addr = tswap64(map->base_addr); 915 map->irq = tswap16(map->irq); 916 break; 917 /* nested */ 918 case QEMU_IFLA_LINKINFO: 919 memset(&li_context, 0, sizeof(li_context)); 920 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len, 921 &li_context, 922 host_to_target_data_linkinfo_nlattr); 923 case QEMU_IFLA_AF_SPEC: 924 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len, 925 NULL, 926 host_to_target_data_spec_nlattr); 927 case QEMU_IFLA_XDP: 928 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len, 929 NULL, 930 host_to_target_data_xdp_nlattr); 931 default: 932 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA type: %d\n", 933 rtattr->rta_type); 934 break; 935 } 936 return 0; 937 } 938 939 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr) 940 { 941 uint32_t *u32; 942 struct ifa_cacheinfo *ci; 943 944 switch (rtattr->rta_type) { 945 /* binary: depends on family type */ 946 case IFA_ADDRESS: 947 case IFA_LOCAL: 948 break; 949 /* string */ 950 case IFA_LABEL: 951 break; 952 /* u32 */ 953 case IFA_FLAGS: 954 case IFA_BROADCAST: 955 u32 = RTA_DATA(rtattr); 956 *u32 = tswap32(*u32); 957 break; 958 /* struct ifa_cacheinfo */ 959 case IFA_CACHEINFO: 960 ci = RTA_DATA(rtattr); 961 ci->ifa_prefered = tswap32(ci->ifa_prefered); 962 ci->ifa_valid = tswap32(ci->ifa_valid); 963 ci->cstamp = tswap32(ci->cstamp); 964 ci->tstamp = tswap32(ci->tstamp); 965 break; 966 default: 967 qemu_log_mask( 968 LOG_UNIMP, "Unknown host IFA type: %d\n", rtattr->rta_type); 969 break; 970 } 971 return 0; 972 } 973 974 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr) 975 { 976 uint32_t *u32; 977 struct rta_cacheinfo *ci; 978 979 switch (rtattr->rta_type) { 980 /* binary: depends on family type */ 981 case QEMU_RTA_GATEWAY: 982 case QEMU_RTA_DST: 983 case QEMU_RTA_PREFSRC: 984 break; 985 /* u8 */ 986 case QEMU_RTA_PREF: 987 break; 988 /* u32 */ 989 case QEMU_RTA_PRIORITY: 990 case QEMU_RTA_TABLE: 991 case QEMU_RTA_OIF: 992 u32 = RTA_DATA(rtattr); 993 *u32 = tswap32(*u32); 994 break; 995 /* struct rta_cacheinfo */ 996 case QEMU_RTA_CACHEINFO: 997 ci = RTA_DATA(rtattr); 998 ci->rta_clntref = tswap32(ci->rta_clntref); 999 ci->rta_lastuse = tswap32(ci->rta_lastuse); 1000 ci->rta_expires = tswap32(ci->rta_expires); 1001 ci->rta_error = tswap32(ci->rta_error); 1002 ci->rta_used = tswap32(ci->rta_used); 1003 #if defined(RTNETLINK_HAVE_PEERINFO) 1004 ci->rta_id = tswap32(ci->rta_id); 1005 ci->rta_ts = tswap32(ci->rta_ts); 1006 ci->rta_tsage = tswap32(ci->rta_tsage); 1007 #endif 1008 break; 1009 default: 1010 qemu_log_mask( 1011 LOG_UNIMP, "Unknown host RTA type: %d\n", rtattr->rta_type); 1012 break; 1013 } 1014 return 0; 1015 } 1016 1017 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr, 1018 uint32_t rtattr_len) 1019 { 1020 return host_to_target_for_each_rtattr(rtattr, rtattr_len, 1021 host_to_target_data_link_rtattr); 1022 } 1023 1024 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr, 1025 uint32_t rtattr_len) 1026 { 1027 return host_to_target_for_each_rtattr(rtattr, rtattr_len, 1028 host_to_target_data_addr_rtattr); 1029 } 1030 1031 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr, 1032 uint32_t rtattr_len) 1033 { 1034 return host_to_target_for_each_rtattr(rtattr, rtattr_len, 1035 host_to_target_data_route_rtattr); 1036 } 1037 1038 static abi_long host_to_target_data_route(struct nlmsghdr *nlh) 1039 { 1040 uint32_t nlmsg_len; 1041 struct ifinfomsg *ifi; 1042 struct ifaddrmsg *ifa; 1043 struct rtmsg *rtm; 1044 1045 nlmsg_len = nlh->nlmsg_len; 1046 switch (nlh->nlmsg_type) { 1047 case RTM_NEWLINK: 1048 case RTM_DELLINK: 1049 case RTM_GETLINK: 1050 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) { 1051 ifi = NLMSG_DATA(nlh); 1052 ifi->ifi_type = tswap16(ifi->ifi_type); 1053 ifi->ifi_index = tswap32(ifi->ifi_index); 1054 ifi->ifi_flags = tswap32(ifi->ifi_flags); 1055 ifi->ifi_change = tswap32(ifi->ifi_change); 1056 host_to_target_link_rtattr(IFLA_RTA(ifi), 1057 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi))); 1058 } 1059 break; 1060 case RTM_NEWADDR: 1061 case RTM_DELADDR: 1062 case RTM_GETADDR: 1063 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) { 1064 ifa = NLMSG_DATA(nlh); 1065 ifa->ifa_index = tswap32(ifa->ifa_index); 1066 host_to_target_addr_rtattr(IFA_RTA(ifa), 1067 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); 1068 } 1069 break; 1070 case RTM_NEWROUTE: 1071 case RTM_DELROUTE: 1072 case RTM_GETROUTE: 1073 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) { 1074 rtm = NLMSG_DATA(nlh); 1075 rtm->rtm_flags = tswap32(rtm->rtm_flags); 1076 host_to_target_route_rtattr(RTM_RTA(rtm), 1077 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm))); 1078 } 1079 break; 1080 default: 1081 return -TARGET_EINVAL; 1082 } 1083 return 0; 1084 } 1085 1086 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh, 1087 size_t len) 1088 { 1089 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route); 1090 } 1091 1092 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr, 1093 size_t len, 1094 abi_long (*target_to_host_rtattr) 1095 (struct rtattr *)) 1096 { 1097 unsigned short aligned_rta_len; 1098 abi_long ret; 1099 1100 while (len >= sizeof(struct rtattr)) { 1101 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) || 1102 tswap16(rtattr->rta_len) > len) { 1103 break; 1104 } 1105 rtattr->rta_len = tswap16(rtattr->rta_len); 1106 rtattr->rta_type = tswap16(rtattr->rta_type); 1107 ret = target_to_host_rtattr(rtattr); 1108 if (ret < 0) { 1109 return ret; 1110 } 1111 1112 aligned_rta_len = RTA_ALIGN(rtattr->rta_len); 1113 if (aligned_rta_len >= len) { 1114 break; 1115 } 1116 len -= aligned_rta_len; 1117 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len); 1118 } 1119 return 0; 1120 } 1121 1122 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) 1123 { 1124 switch (rtattr->rta_type) { 1125 default: 1126 qemu_log_mask(LOG_UNIMP, "Unknown target QEMU_IFLA type: %d\n", 1127 rtattr->rta_type); 1128 break; 1129 } 1130 return 0; 1131 } 1132 1133 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr) 1134 { 1135 switch (rtattr->rta_type) { 1136 /* binary: depends on family type */ 1137 case IFA_LOCAL: 1138 case IFA_ADDRESS: 1139 break; 1140 default: 1141 qemu_log_mask(LOG_UNIMP, "Unknown target IFA type: %d\n", 1142 rtattr->rta_type); 1143 break; 1144 } 1145 return 0; 1146 } 1147 1148 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr) 1149 { 1150 uint32_t *u32; 1151 switch (rtattr->rta_type) { 1152 /* binary: depends on family type */ 1153 case QEMU_RTA_DST: 1154 case QEMU_RTA_SRC: 1155 case QEMU_RTA_GATEWAY: 1156 break; 1157 /* u32 */ 1158 case QEMU_RTA_PRIORITY: 1159 case QEMU_RTA_OIF: 1160 u32 = RTA_DATA(rtattr); 1161 *u32 = tswap32(*u32); 1162 break; 1163 default: 1164 qemu_log_mask(LOG_UNIMP, "Unknown target RTA type: %d\n", 1165 rtattr->rta_type); 1166 break; 1167 } 1168 return 0; 1169 } 1170 1171 static void target_to_host_link_rtattr(struct rtattr *rtattr, 1172 uint32_t rtattr_len) 1173 { 1174 target_to_host_for_each_rtattr(rtattr, rtattr_len, 1175 target_to_host_data_link_rtattr); 1176 } 1177 1178 static void target_to_host_addr_rtattr(struct rtattr *rtattr, 1179 uint32_t rtattr_len) 1180 { 1181 target_to_host_for_each_rtattr(rtattr, rtattr_len, 1182 target_to_host_data_addr_rtattr); 1183 } 1184 1185 static void target_to_host_route_rtattr(struct rtattr *rtattr, 1186 uint32_t rtattr_len) 1187 { 1188 target_to_host_for_each_rtattr(rtattr, rtattr_len, 1189 target_to_host_data_route_rtattr); 1190 } 1191 1192 static abi_long target_to_host_data_route(struct nlmsghdr *nlh) 1193 { 1194 struct ifinfomsg *ifi; 1195 struct ifaddrmsg *ifa; 1196 struct rtmsg *rtm; 1197 1198 switch (nlh->nlmsg_type) { 1199 case RTM_GETLINK: 1200 break; 1201 case RTM_NEWLINK: 1202 case RTM_DELLINK: 1203 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) { 1204 ifi = NLMSG_DATA(nlh); 1205 ifi->ifi_type = tswap16(ifi->ifi_type); 1206 ifi->ifi_index = tswap32(ifi->ifi_index); 1207 ifi->ifi_flags = tswap32(ifi->ifi_flags); 1208 ifi->ifi_change = tswap32(ifi->ifi_change); 1209 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len - 1210 NLMSG_LENGTH(sizeof(*ifi))); 1211 } 1212 break; 1213 case RTM_GETADDR: 1214 case RTM_NEWADDR: 1215 case RTM_DELADDR: 1216 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) { 1217 ifa = NLMSG_DATA(nlh); 1218 ifa->ifa_index = tswap32(ifa->ifa_index); 1219 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len - 1220 NLMSG_LENGTH(sizeof(*ifa))); 1221 } 1222 break; 1223 case RTM_GETROUTE: 1224 break; 1225 case RTM_NEWROUTE: 1226 case RTM_DELROUTE: 1227 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) { 1228 rtm = NLMSG_DATA(nlh); 1229 rtm->rtm_flags = tswap32(rtm->rtm_flags); 1230 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len - 1231 NLMSG_LENGTH(sizeof(*rtm))); 1232 } 1233 break; 1234 default: 1235 return -TARGET_EOPNOTSUPP; 1236 } 1237 return 0; 1238 } 1239 1240 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len) 1241 { 1242 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route); 1243 } 1244 #endif /* CONFIG_RTNETLINK */ 1245 1246 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh) 1247 { 1248 switch (nlh->nlmsg_type) { 1249 default: 1250 qemu_log_mask(LOG_UNIMP, "Unknown host audit message type %d\n", 1251 nlh->nlmsg_type); 1252 return -TARGET_EINVAL; 1253 } 1254 return 0; 1255 } 1256 1257 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh, 1258 size_t len) 1259 { 1260 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit); 1261 } 1262 1263 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh) 1264 { 1265 switch (nlh->nlmsg_type) { 1266 case AUDIT_USER: 1267 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: 1268 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: 1269 break; 1270 default: 1271 qemu_log_mask(LOG_UNIMP, "Unknown target audit message type %d\n", 1272 nlh->nlmsg_type); 1273 return -TARGET_EINVAL; 1274 } 1275 1276 return 0; 1277 } 1278 1279 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len) 1280 { 1281 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit); 1282 } 1283 1284 static abi_long packet_target_to_host_sockaddr(void *host_addr, 1285 abi_ulong target_addr, 1286 socklen_t len) 1287 { 1288 struct sockaddr *addr = host_addr; 1289 struct target_sockaddr *target_saddr; 1290 1291 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); 1292 if (!target_saddr) { 1293 return -TARGET_EFAULT; 1294 } 1295 1296 memcpy(addr, target_saddr, len); 1297 addr->sa_family = tswap16(target_saddr->sa_family); 1298 /* spkt_protocol is big-endian */ 1299 1300 unlock_user(target_saddr, target_addr, 0); 1301 return 0; 1302 } 1303 1304 TargetFdTrans target_packet_trans = { 1305 .target_to_host_addr = packet_target_to_host_sockaddr, 1306 }; 1307 1308 #ifdef CONFIG_RTNETLINK 1309 static abi_long netlink_route_target_to_host(void *buf, size_t len) 1310 { 1311 abi_long ret; 1312 1313 ret = target_to_host_nlmsg_route(buf, len); 1314 if (ret < 0) { 1315 return ret; 1316 } 1317 1318 return len; 1319 } 1320 1321 static abi_long netlink_route_host_to_target(void *buf, size_t len) 1322 { 1323 abi_long ret; 1324 1325 ret = host_to_target_nlmsg_route(buf, len); 1326 if (ret < 0) { 1327 return ret; 1328 } 1329 1330 return len; 1331 } 1332 1333 TargetFdTrans target_netlink_route_trans = { 1334 .target_to_host_data = netlink_route_target_to_host, 1335 .host_to_target_data = netlink_route_host_to_target, 1336 }; 1337 #endif /* CONFIG_RTNETLINK */ 1338 1339 static abi_long netlink_audit_target_to_host(void *buf, size_t len) 1340 { 1341 abi_long ret; 1342 1343 ret = target_to_host_nlmsg_audit(buf, len); 1344 if (ret < 0) { 1345 return ret; 1346 } 1347 1348 return len; 1349 } 1350 1351 static abi_long netlink_audit_host_to_target(void *buf, size_t len) 1352 { 1353 abi_long ret; 1354 1355 ret = host_to_target_nlmsg_audit(buf, len); 1356 if (ret < 0) { 1357 return ret; 1358 } 1359 1360 return len; 1361 } 1362 1363 TargetFdTrans target_netlink_audit_trans = { 1364 .target_to_host_data = netlink_audit_target_to_host, 1365 .host_to_target_data = netlink_audit_host_to_target, 1366 }; 1367 1368 /* signalfd siginfo conversion */ 1369 1370 static void 1371 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo, 1372 const struct signalfd_siginfo *info) 1373 { 1374 int sig = host_to_target_signal(info->ssi_signo); 1375 1376 /* linux/signalfd.h defines a ssi_addr_lsb 1377 * not defined in sys/signalfd.h but used by some kernels 1378 */ 1379 1380 #ifdef BUS_MCEERR_AO 1381 if (tinfo->ssi_signo == SIGBUS && 1382 (tinfo->ssi_code == BUS_MCEERR_AR || 1383 tinfo->ssi_code == BUS_MCEERR_AO)) { 1384 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1); 1385 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1); 1386 *tssi_addr_lsb = tswap16(*ssi_addr_lsb); 1387 } 1388 #endif 1389 1390 tinfo->ssi_signo = tswap32(sig); 1391 tinfo->ssi_errno = tswap32(tinfo->ssi_errno); 1392 tinfo->ssi_code = tswap32(info->ssi_code); 1393 tinfo->ssi_pid = tswap32(info->ssi_pid); 1394 tinfo->ssi_uid = tswap32(info->ssi_uid); 1395 tinfo->ssi_fd = tswap32(info->ssi_fd); 1396 tinfo->ssi_tid = tswap32(info->ssi_tid); 1397 tinfo->ssi_band = tswap32(info->ssi_band); 1398 tinfo->ssi_overrun = tswap32(info->ssi_overrun); 1399 tinfo->ssi_trapno = tswap32(info->ssi_trapno); 1400 tinfo->ssi_status = tswap32(info->ssi_status); 1401 tinfo->ssi_int = tswap32(info->ssi_int); 1402 tinfo->ssi_ptr = tswap64(info->ssi_ptr); 1403 tinfo->ssi_utime = tswap64(info->ssi_utime); 1404 tinfo->ssi_stime = tswap64(info->ssi_stime); 1405 tinfo->ssi_addr = tswap64(info->ssi_addr); 1406 } 1407 1408 static abi_long host_to_target_data_signalfd(void *buf, size_t len) 1409 { 1410 int i; 1411 1412 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) { 1413 host_to_target_signalfd_siginfo(buf + i, buf + i); 1414 } 1415 1416 return len; 1417 } 1418 1419 TargetFdTrans target_signalfd_trans = { 1420 .host_to_target_data = host_to_target_data_signalfd, 1421 }; 1422 1423 static abi_long swap_data_eventfd(void *buf, size_t len) 1424 { 1425 uint64_t *counter = buf; 1426 int i; 1427 1428 if (len < sizeof(uint64_t)) { 1429 return -EINVAL; 1430 } 1431 1432 for (i = 0; i < len; i += sizeof(uint64_t)) { 1433 *counter = tswap64(*counter); 1434 counter++; 1435 } 1436 1437 return len; 1438 } 1439 1440 TargetFdTrans target_eventfd_trans = { 1441 .host_to_target_data = swap_data_eventfd, 1442 .target_to_host_data = swap_data_eventfd, 1443 }; 1444 1445 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \ 1446 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \ 1447 defined(__NR_inotify_init1)) 1448 static abi_long host_to_target_data_inotify(void *buf, size_t len) 1449 { 1450 struct inotify_event *ev; 1451 int i; 1452 uint32_t name_len; 1453 1454 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) { 1455 ev = (struct inotify_event *)((char *)buf + i); 1456 name_len = ev->len; 1457 1458 ev->wd = tswap32(ev->wd); 1459 ev->mask = tswap32(ev->mask); 1460 ev->cookie = tswap32(ev->cookie); 1461 ev->len = tswap32(name_len); 1462 } 1463 1464 return len; 1465 } 1466 1467 TargetFdTrans target_inotify_trans = { 1468 .host_to_target_data = host_to_target_data_inotify, 1469 }; 1470 #endif 1471