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