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.
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.
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/>.
16 #include "qemu/osdep.h"
18 #include <sys/signalfd.h>
19 #include <linux/unistd.h>
20 #include <linux/audit.h>
22 #include <sys/inotify.h>
24 #include <linux/netlink.h>
25 #ifdef CONFIG_RTNETLINK
26 #include <linux/rtnetlink.h>
27 #include <linux/if_bridge.h>
31 #include "signal-common.h"
35 QEMU_IFLA_BR_FORWARD_DELAY,
36 QEMU_IFLA_BR_HELLO_TIME,
38 QEMU_IFLA_BR_AGEING_TIME,
39 QEMU_IFLA_BR_STP_STATE,
40 QEMU_IFLA_BR_PRIORITY,
41 QEMU_IFLA_BR_VLAN_FILTERING,
42 QEMU_IFLA_BR_VLAN_PROTOCOL,
43 QEMU_IFLA_BR_GROUP_FWD_MASK,
45 QEMU_IFLA_BR_BRIDGE_ID,
46 QEMU_IFLA_BR_ROOT_PORT,
47 QEMU_IFLA_BR_ROOT_PATH_COST,
48 QEMU_IFLA_BR_TOPOLOGY_CHANGE,
49 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
50 QEMU_IFLA_BR_HELLO_TIMER,
51 QEMU_IFLA_BR_TCN_TIMER,
52 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
53 QEMU_IFLA_BR_GC_TIMER,
54 QEMU_IFLA_BR_GROUP_ADDR,
55 QEMU_IFLA_BR_FDB_FLUSH,
56 QEMU_IFLA_BR_MCAST_ROUTER,
57 QEMU_IFLA_BR_MCAST_SNOOPING,
58 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
59 QEMU_IFLA_BR_MCAST_QUERIER,
60 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
61 QEMU_IFLA_BR_MCAST_HASH_MAX,
62 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
63 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
64 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
65 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
66 QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
67 QEMU_IFLA_BR_MCAST_QUERY_INTVL,
68 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
69 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
70 QEMU_IFLA_BR_NF_CALL_IPTABLES,
71 QEMU_IFLA_BR_NF_CALL_IP6TABLES,
72 QEMU_IFLA_BR_NF_CALL_ARPTABLES,
73 QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
75 QEMU_IFLA_BR_VLAN_STATS_ENABLED,
76 QEMU_IFLA_BR_MCAST_STATS_ENABLED,
77 QEMU_IFLA_BR_MCAST_IGMP_VERSION,
78 QEMU_IFLA_BR_MCAST_MLD_VERSION,
79 QEMU_IFLA_BR_VLAN_STATS_PER_PORT,
80 QEMU_IFLA_BR_MULTI_BOOLOPT,
104 QEMU_IFLA_NET_NS_PID,
107 QEMU_IFLA_VFINFO_LIST,
115 QEMU_IFLA_PROMISCUITY,
116 QEMU_IFLA_NUM_TX_QUEUES,
117 QEMU_IFLA_NUM_RX_QUEUES,
119 QEMU_IFLA_PHYS_PORT_ID,
120 QEMU_IFLA_CARRIER_CHANGES,
121 QEMU_IFLA_PHYS_SWITCH_ID,
122 QEMU_IFLA_LINK_NETNSID,
123 QEMU_IFLA_PHYS_PORT_NAME,
124 QEMU_IFLA_PROTO_DOWN,
125 QEMU_IFLA_GSO_MAX_SEGS,
126 QEMU_IFLA_GSO_MAX_SIZE,
130 QEMU_IFLA_NEW_NETNSID,
131 QEMU_IFLA_IF_NETNSID,
132 QEMU_IFLA_CARRIER_UP_COUNT,
133 QEMU_IFLA_CARRIER_DOWN_COUNT,
134 QEMU_IFLA_NEW_IFINDEX,
138 QEMU_IFLA_ALT_IFNAME,
139 QEMU_IFLA_PERM_ADDRESS,
144 QEMU_IFLA_BRPORT_UNSPEC,
145 QEMU_IFLA_BRPORT_STATE,
146 QEMU_IFLA_BRPORT_PRIORITY,
147 QEMU_IFLA_BRPORT_COST,
148 QEMU_IFLA_BRPORT_MODE,
149 QEMU_IFLA_BRPORT_GUARD,
150 QEMU_IFLA_BRPORT_PROTECT,
151 QEMU_IFLA_BRPORT_FAST_LEAVE,
152 QEMU_IFLA_BRPORT_LEARNING,
153 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
154 QEMU_IFLA_BRPORT_PROXYARP,
155 QEMU_IFLA_BRPORT_LEARNING_SYNC,
156 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
157 QEMU_IFLA_BRPORT_ROOT_ID,
158 QEMU_IFLA_BRPORT_BRIDGE_ID,
159 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
160 QEMU_IFLA_BRPORT_DESIGNATED_COST,
163 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
164 QEMU_IFLA_BRPORT_CONFIG_PENDING,
165 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
166 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
167 QEMU_IFLA_BRPORT_HOLD_TIMER,
168 QEMU_IFLA_BRPORT_FLUSH,
169 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
170 QEMU_IFLA_BRPORT_PAD,
171 QEMU_IFLA_BRPORT_MCAST_FLOOD,
172 QEMU_IFLA_BRPORT_MCAST_TO_UCAST,
173 QEMU_IFLA_BRPORT_VLAN_TUNNEL,
174 QEMU_IFLA_BRPORT_BCAST_FLOOD,
175 QEMU_IFLA_BRPORT_GROUP_FWD_MASK,
176 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS,
177 QEMU_IFLA_BRPORT_ISOLATED,
178 QEMU_IFLA_BRPORT_BACKUP_PORT,
179 QEMU_IFLA_BRPORT_MRP_RING_OPEN,
180 QEMU_IFLA_BRPORT_MRP_IN_OPEN,
181 QEMU___IFLA_BRPORT_MAX
185 QEMU_IFLA_TUN_UNSPEC,
190 QEMU_IFLA_TUN_VNET_HDR,
191 QEMU_IFLA_TUN_PERSIST,
192 QEMU_IFLA_TUN_MULTI_QUEUE,
193 QEMU_IFLA_TUN_NUM_QUEUES,
194 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES,
199 QEMU_IFLA_INFO_UNSPEC,
202 QEMU_IFLA_INFO_XSTATS,
203 QEMU_IFLA_INFO_SLAVE_KIND,
204 QEMU_IFLA_INFO_SLAVE_DATA,
205 QEMU___IFLA_INFO_MAX,
209 QEMU_IFLA_INET_UNSPEC,
211 QEMU___IFLA_INET_MAX,
215 QEMU_IFLA_INET6_UNSPEC,
216 QEMU_IFLA_INET6_FLAGS,
217 QEMU_IFLA_INET6_CONF,
218 QEMU_IFLA_INET6_STATS,
219 QEMU_IFLA_INET6_MCAST,
220 QEMU_IFLA_INET6_CACHEINFO,
221 QEMU_IFLA_INET6_ICMP6STATS,
222 QEMU_IFLA_INET6_TOKEN,
223 QEMU_IFLA_INET6_ADDR_GEN_MODE,
224 QEMU___IFLA_INET6_MAX
228 QEMU_IFLA_XDP_UNSPEC,
230 QEMU_IFLA_XDP_ATTACHED,
232 QEMU_IFLA_XDP_PROG_ID,
247 QEMU_RTA_PROTOINFO, /* no longer used */
250 QEMU_RTA_SESSION, /* no longer used */
251 QEMU_RTA_MP_ALGO, /* no longer used */
263 QEMU_RTA_TTL_PROPAGATE,
270 TargetFdTrans **target_fd_trans;
271 QemuMutex target_fd_trans_lock;
272 unsigned int target_fd_max;
274 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
276 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
277 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
278 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
279 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
280 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
283 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
285 abi_long (*host_to_target_nlmsg)
289 uint32_t aligned_nlmsg_len;
292 while (len > sizeof(struct nlmsghdr)) {
294 nlmsg_len = nlh->nlmsg_len;
295 if (nlmsg_len < sizeof(struct nlmsghdr) ||
300 switch (nlh->nlmsg_type) {
308 struct nlmsgerr *e = NLMSG_DATA(nlh);
309 e->error = tswap32(e->error);
310 tswap_nlmsghdr(&e->msg);
315 ret = host_to_target_nlmsg(nlh);
324 aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len);
325 if (aligned_nlmsg_len >= len) {
328 len -= aligned_nlmsg_len;
329 nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len);
334 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
336 abi_long (*target_to_host_nlmsg)
339 uint32_t aligned_nlmsg_len;
342 while (len > sizeof(struct nlmsghdr)) {
343 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
344 tswap32(nlh->nlmsg_len) > len) {
348 switch (nlh->nlmsg_type) {
355 struct nlmsgerr *e = NLMSG_DATA(nlh);
356 e->error = tswap32(e->error);
357 tswap_nlmsghdr(&e->msg);
361 ret = target_to_host_nlmsg(nlh);
367 aligned_nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len);
368 if (aligned_nlmsg_len >= len) {
371 len -= aligned_nlmsg_len;
372 nlh = (struct nlmsghdr *)(((char *)nlh) + aligned_nlmsg_len);
377 #ifdef CONFIG_RTNETLINK
378 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
379 size_t len, void *context,
380 abi_long (*host_to_target_nlattr)
384 unsigned short nla_len;
385 unsigned short aligned_nla_len;
388 while (len > sizeof(struct nlattr)) {
389 nla_len = nlattr->nla_len;
390 if (nla_len < sizeof(struct nlattr) ||
394 ret = host_to_target_nlattr(nlattr, context);
395 nlattr->nla_len = tswap16(nlattr->nla_len);
396 nlattr->nla_type = tswap16(nlattr->nla_type);
401 aligned_nla_len = NLA_ALIGN(nla_len);
402 if (aligned_nla_len >= len) {
405 len -= aligned_nla_len;
406 nlattr = (struct nlattr *)(((char *)nlattr) + aligned_nla_len);
411 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
413 abi_long (*host_to_target_rtattr)
416 unsigned short rta_len;
417 unsigned short aligned_rta_len;
420 while (len > sizeof(struct rtattr)) {
421 rta_len = rtattr->rta_len;
422 if (rta_len < sizeof(struct rtattr) ||
426 ret = host_to_target_rtattr(rtattr);
427 rtattr->rta_len = tswap16(rtattr->rta_len);
428 rtattr->rta_type = tswap16(rtattr->rta_type);
433 aligned_rta_len = RTA_ALIGN(rta_len);
434 if (aligned_rta_len >= len) {
437 len -= aligned_rta_len;
438 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
443 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
445 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
452 switch (nlattr->nla_type) {
454 case QEMU_IFLA_BR_FDB_FLUSH:
457 case QEMU_IFLA_BR_GROUP_ADDR:
460 case QEMU_IFLA_BR_VLAN_FILTERING:
461 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
462 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
463 case QEMU_IFLA_BR_MCAST_ROUTER:
464 case QEMU_IFLA_BR_MCAST_SNOOPING:
465 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
466 case QEMU_IFLA_BR_MCAST_QUERIER:
467 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
468 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
469 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
470 case QEMU_IFLA_BR_VLAN_STATS_ENABLED:
471 case QEMU_IFLA_BR_MCAST_STATS_ENABLED:
472 case QEMU_IFLA_BR_MCAST_IGMP_VERSION:
473 case QEMU_IFLA_BR_MCAST_MLD_VERSION:
474 case QEMU_IFLA_BR_VLAN_STATS_PER_PORT:
477 case QEMU_IFLA_BR_PRIORITY:
478 case QEMU_IFLA_BR_VLAN_PROTOCOL:
479 case QEMU_IFLA_BR_GROUP_FWD_MASK:
480 case QEMU_IFLA_BR_ROOT_PORT:
481 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
482 u16 = NLA_DATA(nlattr);
483 *u16 = tswap16(*u16);
486 case QEMU_IFLA_BR_FORWARD_DELAY:
487 case QEMU_IFLA_BR_HELLO_TIME:
488 case QEMU_IFLA_BR_MAX_AGE:
489 case QEMU_IFLA_BR_AGEING_TIME:
490 case QEMU_IFLA_BR_STP_STATE:
491 case QEMU_IFLA_BR_ROOT_PATH_COST:
492 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
493 case QEMU_IFLA_BR_MCAST_HASH_MAX:
494 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
495 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
496 u32 = NLA_DATA(nlattr);
497 *u32 = tswap32(*u32);
500 case QEMU_IFLA_BR_HELLO_TIMER:
501 case QEMU_IFLA_BR_TCN_TIMER:
502 case QEMU_IFLA_BR_GC_TIMER:
503 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
504 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
505 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
506 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
507 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
508 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
509 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
510 u64 = NLA_DATA(nlattr);
511 *u64 = tswap64(*u64);
513 /* ifla_bridge_id: uin8_t[] */
514 case QEMU_IFLA_BR_ROOT_ID:
515 case QEMU_IFLA_BR_BRIDGE_ID:
517 /* br_boolopt_multi { uint32_t, uint32_t } */
518 case QEMU_IFLA_BR_MULTI_BOOLOPT:
519 u32 = NLA_DATA(nlattr);
520 u32[0] = tswap32(u32[0]); /* optval */
521 u32[1] = tswap32(u32[1]); /* optmask */
524 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BR type %d\n",
531 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
538 switch (nlattr->nla_type) {
540 case QEMU_IFLA_BRPORT_STATE:
541 case QEMU_IFLA_BRPORT_MODE:
542 case QEMU_IFLA_BRPORT_GUARD:
543 case QEMU_IFLA_BRPORT_PROTECT:
544 case QEMU_IFLA_BRPORT_FAST_LEAVE:
545 case QEMU_IFLA_BRPORT_LEARNING:
546 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
547 case QEMU_IFLA_BRPORT_PROXYARP:
548 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
549 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
550 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
551 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
552 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
553 case QEMU_IFLA_BRPORT_MCAST_FLOOD:
554 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST:
555 case QEMU_IFLA_BRPORT_VLAN_TUNNEL:
556 case QEMU_IFLA_BRPORT_BCAST_FLOOD:
557 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS:
558 case QEMU_IFLA_BRPORT_ISOLATED:
559 case QEMU_IFLA_BRPORT_MRP_RING_OPEN:
560 case QEMU_IFLA_BRPORT_MRP_IN_OPEN:
563 case QEMU_IFLA_BRPORT_PRIORITY:
564 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
565 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
566 case QEMU_IFLA_BRPORT_ID:
567 case QEMU_IFLA_BRPORT_NO:
568 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK:
569 u16 = NLA_DATA(nlattr);
570 *u16 = tswap16(*u16);
573 case QEMU_IFLA_BRPORT_COST:
574 case QEMU_IFLA_BRPORT_BACKUP_PORT:
575 u32 = NLA_DATA(nlattr);
576 *u32 = tswap32(*u32);
579 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
580 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
581 case QEMU_IFLA_BRPORT_HOLD_TIMER:
582 u64 = NLA_DATA(nlattr);
583 *u64 = tswap64(*u64);
585 /* ifla_bridge_id: uint8_t[] */
586 case QEMU_IFLA_BRPORT_ROOT_ID:
587 case QEMU_IFLA_BRPORT_BRIDGE_ID:
590 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BRPORT type %d\n",
597 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr,
602 switch (nlattr->nla_type) {
604 case QEMU_IFLA_TUN_TYPE:
605 case QEMU_IFLA_TUN_PI:
606 case QEMU_IFLA_TUN_VNET_HDR:
607 case QEMU_IFLA_TUN_PERSIST:
608 case QEMU_IFLA_TUN_MULTI_QUEUE:
611 case QEMU_IFLA_TUN_NUM_QUEUES:
612 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES:
613 case QEMU_IFLA_TUN_OWNER:
614 case QEMU_IFLA_TUN_GROUP:
615 u32 = NLA_DATA(nlattr);
616 *u32 = tswap32(*u32);
619 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_TUN type %d\n",
626 struct linkinfo_context {
633 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
636 struct linkinfo_context *li_context = context;
638 switch (nlattr->nla_type) {
640 case QEMU_IFLA_INFO_KIND:
641 li_context->name = NLA_DATA(nlattr);
642 li_context->len = nlattr->nla_len - NLA_HDRLEN;
644 case QEMU_IFLA_INFO_SLAVE_KIND:
645 li_context->slave_name = NLA_DATA(nlattr);
646 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
649 case QEMU_IFLA_INFO_XSTATS:
650 /* FIXME: only used by CAN */
653 case QEMU_IFLA_INFO_DATA:
654 if (strncmp(li_context->name, "bridge",
655 li_context->len) == 0) {
656 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
659 host_to_target_data_bridge_nlattr);
660 } else if (strncmp(li_context->name, "tun",
661 li_context->len) == 0) {
662 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
665 host_to_target_data_tun_nlattr);
667 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_KIND %s\n",
671 case QEMU_IFLA_INFO_SLAVE_DATA:
672 if (strncmp(li_context->slave_name, "bridge",
673 li_context->slave_len) == 0) {
674 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
677 host_to_target_slave_data_bridge_nlattr);
679 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
680 li_context->slave_name);
684 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA_INFO type: %d\n",
692 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
698 switch (nlattr->nla_type) {
699 case QEMU_IFLA_INET_CONF:
700 u32 = NLA_DATA(nlattr);
701 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
703 u32[i] = tswap32(u32[i]);
707 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET type: %d\n",
713 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
718 struct ifla_cacheinfo *ci;
721 switch (nlattr->nla_type) {
723 case QEMU_IFLA_INET6_TOKEN:
726 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
729 case QEMU_IFLA_INET6_FLAGS:
730 u32 = NLA_DATA(nlattr);
731 *u32 = tswap32(*u32);
734 case QEMU_IFLA_INET6_CONF:
735 u32 = NLA_DATA(nlattr);
736 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
738 u32[i] = tswap32(u32[i]);
742 case QEMU_IFLA_INET6_CACHEINFO:
743 ci = NLA_DATA(nlattr);
744 ci->max_reasm_len = tswap32(ci->max_reasm_len);
745 ci->tstamp = tswap32(ci->tstamp);
746 ci->reachable_time = tswap32(ci->reachable_time);
747 ci->retrans_time = tswap32(ci->retrans_time);
750 case QEMU_IFLA_INET6_STATS:
751 case QEMU_IFLA_INET6_ICMP6STATS:
752 u64 = NLA_DATA(nlattr);
753 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
755 u64[i] = tswap64(u64[i]);
759 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET6 type: %d\n",
765 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
768 switch (nlattr->nla_type) {
770 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
772 host_to_target_data_inet_nlattr);
774 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
776 host_to_target_data_inet6_nlattr);
778 qemu_log_mask(LOG_UNIMP, "Unknown host AF_SPEC type: %d\n",
785 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr,
790 switch (nlattr->nla_type) {
792 case QEMU_IFLA_XDP_ATTACHED:
795 case QEMU_IFLA_XDP_PROG_ID:
796 u32 = NLA_DATA(nlattr);
797 *u32 = tswap32(*u32);
801 LOG_UNIMP, "Unknown host XDP type: %d\n", nlattr->nla_type);
807 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
810 struct rtnl_link_stats *st;
811 struct rtnl_link_stats64 *st64;
812 struct rtnl_link_ifmap *map;
813 struct linkinfo_context li_context;
815 switch (rtattr->rta_type) {
817 case QEMU_IFLA_ADDRESS:
818 case QEMU_IFLA_BROADCAST:
819 case QEMU_IFLA_PERM_ADDRESS:
821 case QEMU_IFLA_IFNAME:
822 case QEMU_IFLA_QDISC:
825 case QEMU_IFLA_OPERSTATE:
826 case QEMU_IFLA_LINKMODE:
827 case QEMU_IFLA_CARRIER:
828 case QEMU_IFLA_PROTO_DOWN:
833 case QEMU_IFLA_WEIGHT:
834 case QEMU_IFLA_TXQLEN:
835 case QEMU_IFLA_CARRIER_CHANGES:
836 case QEMU_IFLA_NUM_RX_QUEUES:
837 case QEMU_IFLA_NUM_TX_QUEUES:
838 case QEMU_IFLA_PROMISCUITY:
839 case QEMU_IFLA_EXT_MASK:
840 case QEMU_IFLA_LINK_NETNSID:
841 case QEMU_IFLA_GROUP:
842 case QEMU_IFLA_MASTER:
843 case QEMU_IFLA_NUM_VF:
844 case QEMU_IFLA_GSO_MAX_SEGS:
845 case QEMU_IFLA_GSO_MAX_SIZE:
846 case QEMU_IFLA_CARRIER_UP_COUNT:
847 case QEMU_IFLA_CARRIER_DOWN_COUNT:
848 case QEMU_IFLA_MIN_MTU:
849 case QEMU_IFLA_MAX_MTU:
850 u32 = RTA_DATA(rtattr);
851 *u32 = tswap32(*u32);
853 /* struct rtnl_link_stats */
854 case QEMU_IFLA_STATS:
855 st = RTA_DATA(rtattr);
856 st->rx_packets = tswap32(st->rx_packets);
857 st->tx_packets = tswap32(st->tx_packets);
858 st->rx_bytes = tswap32(st->rx_bytes);
859 st->tx_bytes = tswap32(st->tx_bytes);
860 st->rx_errors = tswap32(st->rx_errors);
861 st->tx_errors = tswap32(st->tx_errors);
862 st->rx_dropped = tswap32(st->rx_dropped);
863 st->tx_dropped = tswap32(st->tx_dropped);
864 st->multicast = tswap32(st->multicast);
865 st->collisions = tswap32(st->collisions);
867 /* detailed rx_errors: */
868 st->rx_length_errors = tswap32(st->rx_length_errors);
869 st->rx_over_errors = tswap32(st->rx_over_errors);
870 st->rx_crc_errors = tswap32(st->rx_crc_errors);
871 st->rx_frame_errors = tswap32(st->rx_frame_errors);
872 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
873 st->rx_missed_errors = tswap32(st->rx_missed_errors);
875 /* detailed tx_errors */
876 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
877 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
878 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
879 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
880 st->tx_window_errors = tswap32(st->tx_window_errors);
883 st->rx_compressed = tswap32(st->rx_compressed);
884 st->tx_compressed = tswap32(st->tx_compressed);
886 /* struct rtnl_link_stats64 */
887 case QEMU_IFLA_STATS64:
888 st64 = RTA_DATA(rtattr);
889 st64->rx_packets = tswap64(st64->rx_packets);
890 st64->tx_packets = tswap64(st64->tx_packets);
891 st64->rx_bytes = tswap64(st64->rx_bytes);
892 st64->tx_bytes = tswap64(st64->tx_bytes);
893 st64->rx_errors = tswap64(st64->rx_errors);
894 st64->tx_errors = tswap64(st64->tx_errors);
895 st64->rx_dropped = tswap64(st64->rx_dropped);
896 st64->tx_dropped = tswap64(st64->tx_dropped);
897 st64->multicast = tswap64(st64->multicast);
898 st64->collisions = tswap64(st64->collisions);
900 /* detailed rx_errors: */
901 st64->rx_length_errors = tswap64(st64->rx_length_errors);
902 st64->rx_over_errors = tswap64(st64->rx_over_errors);
903 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
904 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
905 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
906 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
908 /* detailed tx_errors */
909 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
910 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
911 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
912 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
913 st64->tx_window_errors = tswap64(st64->tx_window_errors);
916 st64->rx_compressed = tswap64(st64->rx_compressed);
917 st64->tx_compressed = tswap64(st64->tx_compressed);
919 /* struct rtnl_link_ifmap */
921 map = RTA_DATA(rtattr);
922 map->mem_start = tswap64(map->mem_start);
923 map->mem_end = tswap64(map->mem_end);
924 map->base_addr = tswap64(map->base_addr);
925 map->irq = tswap16(map->irq);
928 case QEMU_IFLA_LINKINFO:
929 memset(&li_context, 0, sizeof(li_context));
930 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
932 host_to_target_data_linkinfo_nlattr);
933 case QEMU_IFLA_AF_SPEC:
934 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
936 host_to_target_data_spec_nlattr);
938 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
940 host_to_target_data_xdp_nlattr);
942 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA type: %d\n",
949 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
952 struct ifa_cacheinfo *ci;
954 switch (rtattr->rta_type) {
955 /* binary: depends on family type */
965 u32 = RTA_DATA(rtattr);
966 *u32 = tswap32(*u32);
968 /* struct ifa_cacheinfo */
970 ci = RTA_DATA(rtattr);
971 ci->ifa_prefered = tswap32(ci->ifa_prefered);
972 ci->ifa_valid = tswap32(ci->ifa_valid);
973 ci->cstamp = tswap32(ci->cstamp);
974 ci->tstamp = tswap32(ci->tstamp);
978 LOG_UNIMP, "Unknown host IFA type: %d\n", rtattr->rta_type);
984 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
987 struct rta_cacheinfo *ci;
989 switch (rtattr->rta_type) {
990 /* binary: depends on family type */
991 case QEMU_RTA_GATEWAY:
993 case QEMU_RTA_PREFSRC:
999 case QEMU_RTA_PRIORITY:
1000 case QEMU_RTA_TABLE:
1002 u32 = RTA_DATA(rtattr);
1003 *u32 = tswap32(*u32);
1005 /* struct rta_cacheinfo */
1006 case QEMU_RTA_CACHEINFO:
1007 ci = RTA_DATA(rtattr);
1008 ci->rta_clntref = tswap32(ci->rta_clntref);
1009 ci->rta_lastuse = tswap32(ci->rta_lastuse);
1010 ci->rta_expires = tswap32(ci->rta_expires);
1011 ci->rta_error = tswap32(ci->rta_error);
1012 ci->rta_used = tswap32(ci->rta_used);
1013 #if defined(RTNETLINK_HAVE_PEERINFO)
1014 ci->rta_id = tswap32(ci->rta_id);
1015 ci->rta_ts = tswap32(ci->rta_ts);
1016 ci->rta_tsage = tswap32(ci->rta_tsage);
1021 LOG_UNIMP, "Unknown host RTA type: %d\n", rtattr->rta_type);
1027 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1028 uint32_t rtattr_len)
1030 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1031 host_to_target_data_link_rtattr);
1034 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1035 uint32_t rtattr_len)
1037 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1038 host_to_target_data_addr_rtattr);
1041 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1042 uint32_t rtattr_len)
1044 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1045 host_to_target_data_route_rtattr);
1048 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1051 struct ifinfomsg *ifi;
1052 struct ifaddrmsg *ifa;
1055 nlmsg_len = nlh->nlmsg_len;
1056 switch (nlh->nlmsg_type) {
1060 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1061 ifi = NLMSG_DATA(nlh);
1062 ifi->ifi_type = tswap16(ifi->ifi_type);
1063 ifi->ifi_index = tswap32(ifi->ifi_index);
1064 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1065 ifi->ifi_change = tswap32(ifi->ifi_change);
1066 host_to_target_link_rtattr(IFLA_RTA(ifi),
1067 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1073 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1074 ifa = NLMSG_DATA(nlh);
1075 ifa->ifa_index = tswap32(ifa->ifa_index);
1076 host_to_target_addr_rtattr(IFA_RTA(ifa),
1077 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1083 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1084 rtm = NLMSG_DATA(nlh);
1085 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1086 host_to_target_route_rtattr(RTM_RTA(rtm),
1087 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1091 return -TARGET_EINVAL;
1096 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1099 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1102 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1104 abi_long (*target_to_host_rtattr)
1107 unsigned short aligned_rta_len;
1110 while (len >= sizeof(struct rtattr)) {
1111 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1112 tswap16(rtattr->rta_len) > len) {
1115 rtattr->rta_len = tswap16(rtattr->rta_len);
1116 rtattr->rta_type = tswap16(rtattr->rta_type);
1117 ret = target_to_host_rtattr(rtattr);
1122 aligned_rta_len = RTA_ALIGN(rtattr->rta_len);
1123 if (aligned_rta_len >= len) {
1126 len -= aligned_rta_len;
1127 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
1132 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1136 switch (rtattr->rta_type) {
1138 case QEMU_IFLA_EXT_MASK:
1139 u32 = RTA_DATA(rtattr);
1140 *u32 = tswap32(*u32);
1143 qemu_log_mask(LOG_UNIMP, "Unknown target QEMU_IFLA type: %d\n",
1150 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1152 switch (rtattr->rta_type) {
1153 /* binary: depends on family type */
1158 qemu_log_mask(LOG_UNIMP, "Unknown target IFA type: %d\n",
1165 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1168 switch (rtattr->rta_type) {
1169 /* binary: depends on family type */
1172 case QEMU_RTA_GATEWAY:
1175 case QEMU_RTA_PRIORITY:
1176 case QEMU_RTA_TABLE:
1178 u32 = RTA_DATA(rtattr);
1179 *u32 = tswap32(*u32);
1182 qemu_log_mask(LOG_UNIMP, "Unknown target RTA type: %d\n",
1189 static void target_to_host_link_rtattr(struct rtattr *rtattr,
1190 uint32_t rtattr_len)
1192 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1193 target_to_host_data_link_rtattr);
1196 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1197 uint32_t rtattr_len)
1199 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1200 target_to_host_data_addr_rtattr);
1203 static void target_to_host_route_rtattr(struct rtattr *rtattr,
1204 uint32_t rtattr_len)
1206 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1207 target_to_host_data_route_rtattr);
1210 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
1212 struct ifinfomsg *ifi;
1213 struct ifaddrmsg *ifa;
1216 switch (nlh->nlmsg_type) {
1221 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1222 ifi = NLMSG_DATA(nlh);
1223 ifi->ifi_type = tswap16(ifi->ifi_type);
1224 ifi->ifi_index = tswap32(ifi->ifi_index);
1225 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1226 ifi->ifi_change = tswap32(ifi->ifi_change);
1227 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
1228 NLMSG_LENGTH(sizeof(*ifi)));
1234 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1235 ifa = NLMSG_DATA(nlh);
1236 ifa->ifa_index = tswap32(ifa->ifa_index);
1237 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
1238 NLMSG_LENGTH(sizeof(*ifa)));
1244 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1245 rtm = NLMSG_DATA(nlh);
1246 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1247 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
1248 NLMSG_LENGTH(sizeof(*rtm)));
1252 return -TARGET_EOPNOTSUPP;
1257 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
1259 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
1261 #endif /* CONFIG_RTNETLINK */
1263 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
1265 switch (nlh->nlmsg_type) {
1267 qemu_log_mask(LOG_UNIMP, "Unknown host audit message type %d\n",
1269 return -TARGET_EINVAL;
1274 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
1277 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
1280 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
1282 switch (nlh->nlmsg_type) {
1284 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1285 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
1288 qemu_log_mask(LOG_UNIMP, "Unknown target audit message type %d\n",
1290 return -TARGET_EINVAL;
1296 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
1298 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
1301 static abi_long packet_target_to_host_sockaddr(void *host_addr,
1302 abi_ulong target_addr,
1305 struct sockaddr *addr = host_addr;
1306 struct target_sockaddr *target_saddr;
1308 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1309 if (!target_saddr) {
1310 return -TARGET_EFAULT;
1313 memcpy(addr, target_saddr, len);
1314 addr->sa_family = tswap16(target_saddr->sa_family);
1315 /* spkt_protocol is big-endian */
1317 unlock_user(target_saddr, target_addr, 0);
1321 TargetFdTrans target_packet_trans = {
1322 .target_to_host_addr = packet_target_to_host_sockaddr,
1325 #ifdef CONFIG_RTNETLINK
1326 static abi_long netlink_route_target_to_host(void *buf, size_t len)
1330 ret = target_to_host_nlmsg_route(buf, len);
1338 static abi_long netlink_route_host_to_target(void *buf, size_t len)
1342 ret = host_to_target_nlmsg_route(buf, len);
1350 TargetFdTrans target_netlink_route_trans = {
1351 .target_to_host_data = netlink_route_target_to_host,
1352 .host_to_target_data = netlink_route_host_to_target,
1354 #endif /* CONFIG_RTNETLINK */
1356 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
1360 ret = target_to_host_nlmsg_audit(buf, len);
1368 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
1372 ret = host_to_target_nlmsg_audit(buf, len);
1380 TargetFdTrans target_netlink_audit_trans = {
1381 .target_to_host_data = netlink_audit_target_to_host,
1382 .host_to_target_data = netlink_audit_host_to_target,
1385 /* signalfd siginfo conversion */
1388 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
1389 const struct signalfd_siginfo *info)
1391 int sig = host_to_target_signal(info->ssi_signo);
1393 /* linux/signalfd.h defines a ssi_addr_lsb
1394 * not defined in sys/signalfd.h but used by some kernels
1397 #ifdef BUS_MCEERR_AO
1398 if (tinfo->ssi_signo == SIGBUS &&
1399 (tinfo->ssi_code == BUS_MCEERR_AR ||
1400 tinfo->ssi_code == BUS_MCEERR_AO)) {
1401 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
1402 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
1403 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
1407 tinfo->ssi_signo = tswap32(sig);
1408 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
1409 tinfo->ssi_code = tswap32(info->ssi_code);
1410 tinfo->ssi_pid = tswap32(info->ssi_pid);
1411 tinfo->ssi_uid = tswap32(info->ssi_uid);
1412 tinfo->ssi_fd = tswap32(info->ssi_fd);
1413 tinfo->ssi_tid = tswap32(info->ssi_tid);
1414 tinfo->ssi_band = tswap32(info->ssi_band);
1415 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
1416 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
1417 tinfo->ssi_status = tswap32(info->ssi_status);
1418 tinfo->ssi_int = tswap32(info->ssi_int);
1419 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
1420 tinfo->ssi_utime = tswap64(info->ssi_utime);
1421 tinfo->ssi_stime = tswap64(info->ssi_stime);
1422 tinfo->ssi_addr = tswap64(info->ssi_addr);
1425 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
1429 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
1430 host_to_target_signalfd_siginfo(buf + i, buf + i);
1436 TargetFdTrans target_signalfd_trans = {
1437 .host_to_target_data = host_to_target_data_signalfd,
1440 static abi_long swap_data_eventfd(void *buf, size_t len)
1442 uint64_t *counter = buf;
1445 if (len < sizeof(uint64_t)) {
1449 for (i = 0; i < len; i += sizeof(uint64_t)) {
1450 *counter = tswap64(*counter);
1457 TargetFdTrans target_eventfd_trans = {
1458 .host_to_target_data = swap_data_eventfd,
1459 .target_to_host_data = swap_data_eventfd,
1462 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
1463 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
1464 defined(__NR_inotify_init1))
1465 static abi_long host_to_target_data_inotify(void *buf, size_t len)
1467 struct inotify_event *ev;
1471 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
1472 ev = (struct inotify_event *)((char *)buf + i);
1475 ev->wd = tswap32(ev->wd);
1476 ev->mask = tswap32(ev->mask);
1477 ev->cookie = tswap32(ev->cookie);
1478 ev->len = tswap32(name_len);
1484 TargetFdTrans target_inotify_trans = {
1485 .host_to_target_data = host_to_target_data_inotify,