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>
30 #include "user-internals.h"
32 #include "signal-common.h"
36 QEMU_IFLA_BR_FORWARD_DELAY,
37 QEMU_IFLA_BR_HELLO_TIME,
39 QEMU_IFLA_BR_AGEING_TIME,
40 QEMU_IFLA_BR_STP_STATE,
41 QEMU_IFLA_BR_PRIORITY,
42 QEMU_IFLA_BR_VLAN_FILTERING,
43 QEMU_IFLA_BR_VLAN_PROTOCOL,
44 QEMU_IFLA_BR_GROUP_FWD_MASK,
46 QEMU_IFLA_BR_BRIDGE_ID,
47 QEMU_IFLA_BR_ROOT_PORT,
48 QEMU_IFLA_BR_ROOT_PATH_COST,
49 QEMU_IFLA_BR_TOPOLOGY_CHANGE,
50 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
51 QEMU_IFLA_BR_HELLO_TIMER,
52 QEMU_IFLA_BR_TCN_TIMER,
53 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
54 QEMU_IFLA_BR_GC_TIMER,
55 QEMU_IFLA_BR_GROUP_ADDR,
56 QEMU_IFLA_BR_FDB_FLUSH,
57 QEMU_IFLA_BR_MCAST_ROUTER,
58 QEMU_IFLA_BR_MCAST_SNOOPING,
59 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
60 QEMU_IFLA_BR_MCAST_QUERIER,
61 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
62 QEMU_IFLA_BR_MCAST_HASH_MAX,
63 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
64 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
65 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
66 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
67 QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
68 QEMU_IFLA_BR_MCAST_QUERY_INTVL,
69 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
70 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
71 QEMU_IFLA_BR_NF_CALL_IPTABLES,
72 QEMU_IFLA_BR_NF_CALL_IP6TABLES,
73 QEMU_IFLA_BR_NF_CALL_ARPTABLES,
74 QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
76 QEMU_IFLA_BR_VLAN_STATS_ENABLED,
77 QEMU_IFLA_BR_MCAST_STATS_ENABLED,
78 QEMU_IFLA_BR_MCAST_IGMP_VERSION,
79 QEMU_IFLA_BR_MCAST_MLD_VERSION,
80 QEMU_IFLA_BR_VLAN_STATS_PER_PORT,
81 QEMU_IFLA_BR_MULTI_BOOLOPT,
105 QEMU_IFLA_NET_NS_PID,
108 QEMU_IFLA_VFINFO_LIST,
116 QEMU_IFLA_PROMISCUITY,
117 QEMU_IFLA_NUM_TX_QUEUES,
118 QEMU_IFLA_NUM_RX_QUEUES,
120 QEMU_IFLA_PHYS_PORT_ID,
121 QEMU_IFLA_CARRIER_CHANGES,
122 QEMU_IFLA_PHYS_SWITCH_ID,
123 QEMU_IFLA_LINK_NETNSID,
124 QEMU_IFLA_PHYS_PORT_NAME,
125 QEMU_IFLA_PROTO_DOWN,
126 QEMU_IFLA_GSO_MAX_SEGS,
127 QEMU_IFLA_GSO_MAX_SIZE,
131 QEMU_IFLA_NEW_NETNSID,
132 QEMU_IFLA_IF_NETNSID,
133 QEMU_IFLA_CARRIER_UP_COUNT,
134 QEMU_IFLA_CARRIER_DOWN_COUNT,
135 QEMU_IFLA_NEW_IFINDEX,
139 QEMU_IFLA_ALT_IFNAME,
140 QEMU_IFLA_PERM_ADDRESS,
145 QEMU_IFLA_BRPORT_UNSPEC,
146 QEMU_IFLA_BRPORT_STATE,
147 QEMU_IFLA_BRPORT_PRIORITY,
148 QEMU_IFLA_BRPORT_COST,
149 QEMU_IFLA_BRPORT_MODE,
150 QEMU_IFLA_BRPORT_GUARD,
151 QEMU_IFLA_BRPORT_PROTECT,
152 QEMU_IFLA_BRPORT_FAST_LEAVE,
153 QEMU_IFLA_BRPORT_LEARNING,
154 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
155 QEMU_IFLA_BRPORT_PROXYARP,
156 QEMU_IFLA_BRPORT_LEARNING_SYNC,
157 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
158 QEMU_IFLA_BRPORT_ROOT_ID,
159 QEMU_IFLA_BRPORT_BRIDGE_ID,
160 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
161 QEMU_IFLA_BRPORT_DESIGNATED_COST,
164 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
165 QEMU_IFLA_BRPORT_CONFIG_PENDING,
166 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
167 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
168 QEMU_IFLA_BRPORT_HOLD_TIMER,
169 QEMU_IFLA_BRPORT_FLUSH,
170 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
171 QEMU_IFLA_BRPORT_PAD,
172 QEMU_IFLA_BRPORT_MCAST_FLOOD,
173 QEMU_IFLA_BRPORT_MCAST_TO_UCAST,
174 QEMU_IFLA_BRPORT_VLAN_TUNNEL,
175 QEMU_IFLA_BRPORT_BCAST_FLOOD,
176 QEMU_IFLA_BRPORT_GROUP_FWD_MASK,
177 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS,
178 QEMU_IFLA_BRPORT_ISOLATED,
179 QEMU_IFLA_BRPORT_BACKUP_PORT,
180 QEMU_IFLA_BRPORT_MRP_RING_OPEN,
181 QEMU_IFLA_BRPORT_MRP_IN_OPEN,
182 QEMU___IFLA_BRPORT_MAX
186 QEMU_IFLA_TUN_UNSPEC,
191 QEMU_IFLA_TUN_VNET_HDR,
192 QEMU_IFLA_TUN_PERSIST,
193 QEMU_IFLA_TUN_MULTI_QUEUE,
194 QEMU_IFLA_TUN_NUM_QUEUES,
195 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES,
200 QEMU_IFLA_INFO_UNSPEC,
203 QEMU_IFLA_INFO_XSTATS,
204 QEMU_IFLA_INFO_SLAVE_KIND,
205 QEMU_IFLA_INFO_SLAVE_DATA,
206 QEMU___IFLA_INFO_MAX,
210 QEMU_IFLA_INET_UNSPEC,
212 QEMU___IFLA_INET_MAX,
216 QEMU_IFLA_INET6_UNSPEC,
217 QEMU_IFLA_INET6_FLAGS,
218 QEMU_IFLA_INET6_CONF,
219 QEMU_IFLA_INET6_STATS,
220 QEMU_IFLA_INET6_MCAST,
221 QEMU_IFLA_INET6_CACHEINFO,
222 QEMU_IFLA_INET6_ICMP6STATS,
223 QEMU_IFLA_INET6_TOKEN,
224 QEMU_IFLA_INET6_ADDR_GEN_MODE,
225 QEMU___IFLA_INET6_MAX
229 QEMU_IFLA_XDP_UNSPEC,
231 QEMU_IFLA_XDP_ATTACHED,
233 QEMU_IFLA_XDP_PROG_ID,
248 QEMU_RTA_PROTOINFO, /* no longer used */
251 QEMU_RTA_SESSION, /* no longer used */
252 QEMU_RTA_MP_ALGO, /* no longer used */
264 QEMU_RTA_TTL_PROPAGATE,
271 TargetFdTrans **target_fd_trans;
272 QemuMutex target_fd_trans_lock;
273 unsigned int target_fd_max;
275 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
277 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
278 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
279 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
280 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
281 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
284 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
286 abi_long (*host_to_target_nlmsg)
290 uint32_t aligned_nlmsg_len;
293 while (len > sizeof(struct nlmsghdr)) {
295 nlmsg_len = nlh->nlmsg_len;
296 if (nlmsg_len < sizeof(struct nlmsghdr) ||
301 switch (nlh->nlmsg_type) {
309 struct nlmsgerr *e = NLMSG_DATA(nlh);
310 e->error = tswap32(e->error);
311 tswap_nlmsghdr(&e->msg);
316 ret = host_to_target_nlmsg(nlh);
325 aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len);
326 if (aligned_nlmsg_len >= len) {
329 len -= aligned_nlmsg_len;
330 nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len);
335 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
337 abi_long (*target_to_host_nlmsg)
340 uint32_t aligned_nlmsg_len;
343 while (len > sizeof(struct nlmsghdr)) {
344 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
345 tswap32(nlh->nlmsg_len) > len) {
349 switch (nlh->nlmsg_type) {
356 struct nlmsgerr *e = NLMSG_DATA(nlh);
357 e->error = tswap32(e->error);
358 tswap_nlmsghdr(&e->msg);
362 ret = target_to_host_nlmsg(nlh);
368 aligned_nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len);
369 if (aligned_nlmsg_len >= len) {
372 len -= aligned_nlmsg_len;
373 nlh = (struct nlmsghdr *)(((char *)nlh) + aligned_nlmsg_len);
378 #ifdef CONFIG_RTNETLINK
379 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
380 size_t len, void *context,
381 abi_long (*host_to_target_nlattr)
385 unsigned short nla_len;
386 unsigned short aligned_nla_len;
389 while (len > sizeof(struct nlattr)) {
390 nla_len = nlattr->nla_len;
391 if (nla_len < sizeof(struct nlattr) ||
395 ret = host_to_target_nlattr(nlattr, context);
396 nlattr->nla_len = tswap16(nlattr->nla_len);
397 nlattr->nla_type = tswap16(nlattr->nla_type);
402 aligned_nla_len = NLA_ALIGN(nla_len);
403 if (aligned_nla_len >= len) {
406 len -= aligned_nla_len;
407 nlattr = (struct nlattr *)(((char *)nlattr) + aligned_nla_len);
412 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
414 abi_long (*host_to_target_rtattr)
417 unsigned short rta_len;
418 unsigned short aligned_rta_len;
421 while (len > sizeof(struct rtattr)) {
422 rta_len = rtattr->rta_len;
423 if (rta_len < sizeof(struct rtattr) ||
427 ret = host_to_target_rtattr(rtattr);
428 rtattr->rta_len = tswap16(rtattr->rta_len);
429 rtattr->rta_type = tswap16(rtattr->rta_type);
434 aligned_rta_len = RTA_ALIGN(rta_len);
435 if (aligned_rta_len >= len) {
438 len -= aligned_rta_len;
439 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
444 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
446 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
453 switch (nlattr->nla_type) {
455 case QEMU_IFLA_BR_FDB_FLUSH:
458 case QEMU_IFLA_BR_GROUP_ADDR:
461 case QEMU_IFLA_BR_VLAN_FILTERING:
462 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
463 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
464 case QEMU_IFLA_BR_MCAST_ROUTER:
465 case QEMU_IFLA_BR_MCAST_SNOOPING:
466 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
467 case QEMU_IFLA_BR_MCAST_QUERIER:
468 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
469 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
470 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
471 case QEMU_IFLA_BR_VLAN_STATS_ENABLED:
472 case QEMU_IFLA_BR_MCAST_STATS_ENABLED:
473 case QEMU_IFLA_BR_MCAST_IGMP_VERSION:
474 case QEMU_IFLA_BR_MCAST_MLD_VERSION:
475 case QEMU_IFLA_BR_VLAN_STATS_PER_PORT:
478 case QEMU_IFLA_BR_PRIORITY:
479 case QEMU_IFLA_BR_VLAN_PROTOCOL:
480 case QEMU_IFLA_BR_GROUP_FWD_MASK:
481 case QEMU_IFLA_BR_ROOT_PORT:
482 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
483 u16 = NLA_DATA(nlattr);
484 *u16 = tswap16(*u16);
487 case QEMU_IFLA_BR_FORWARD_DELAY:
488 case QEMU_IFLA_BR_HELLO_TIME:
489 case QEMU_IFLA_BR_MAX_AGE:
490 case QEMU_IFLA_BR_AGEING_TIME:
491 case QEMU_IFLA_BR_STP_STATE:
492 case QEMU_IFLA_BR_ROOT_PATH_COST:
493 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
494 case QEMU_IFLA_BR_MCAST_HASH_MAX:
495 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
496 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
497 u32 = NLA_DATA(nlattr);
498 *u32 = tswap32(*u32);
501 case QEMU_IFLA_BR_HELLO_TIMER:
502 case QEMU_IFLA_BR_TCN_TIMER:
503 case QEMU_IFLA_BR_GC_TIMER:
504 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
505 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
506 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
507 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
508 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
509 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
510 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
511 u64 = NLA_DATA(nlattr);
512 *u64 = tswap64(*u64);
514 /* ifla_bridge_id: uin8_t[] */
515 case QEMU_IFLA_BR_ROOT_ID:
516 case QEMU_IFLA_BR_BRIDGE_ID:
518 /* br_boolopt_multi { uint32_t, uint32_t } */
519 case QEMU_IFLA_BR_MULTI_BOOLOPT:
520 u32 = NLA_DATA(nlattr);
521 u32[0] = tswap32(u32[0]); /* optval */
522 u32[1] = tswap32(u32[1]); /* optmask */
525 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BR type %d\n",
532 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
539 switch (nlattr->nla_type) {
541 case QEMU_IFLA_BRPORT_STATE:
542 case QEMU_IFLA_BRPORT_MODE:
543 case QEMU_IFLA_BRPORT_GUARD:
544 case QEMU_IFLA_BRPORT_PROTECT:
545 case QEMU_IFLA_BRPORT_FAST_LEAVE:
546 case QEMU_IFLA_BRPORT_LEARNING:
547 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
548 case QEMU_IFLA_BRPORT_PROXYARP:
549 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
550 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
551 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
552 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
553 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
554 case QEMU_IFLA_BRPORT_MCAST_FLOOD:
555 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST:
556 case QEMU_IFLA_BRPORT_VLAN_TUNNEL:
557 case QEMU_IFLA_BRPORT_BCAST_FLOOD:
558 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS:
559 case QEMU_IFLA_BRPORT_ISOLATED:
560 case QEMU_IFLA_BRPORT_MRP_RING_OPEN:
561 case QEMU_IFLA_BRPORT_MRP_IN_OPEN:
564 case QEMU_IFLA_BRPORT_PRIORITY:
565 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
566 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
567 case QEMU_IFLA_BRPORT_ID:
568 case QEMU_IFLA_BRPORT_NO:
569 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK:
570 u16 = NLA_DATA(nlattr);
571 *u16 = tswap16(*u16);
574 case QEMU_IFLA_BRPORT_COST:
575 case QEMU_IFLA_BRPORT_BACKUP_PORT:
576 u32 = NLA_DATA(nlattr);
577 *u32 = tswap32(*u32);
580 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
581 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
582 case QEMU_IFLA_BRPORT_HOLD_TIMER:
583 u64 = NLA_DATA(nlattr);
584 *u64 = tswap64(*u64);
586 /* ifla_bridge_id: uint8_t[] */
587 case QEMU_IFLA_BRPORT_ROOT_ID:
588 case QEMU_IFLA_BRPORT_BRIDGE_ID:
591 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BRPORT type %d\n",
598 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr,
603 switch (nlattr->nla_type) {
605 case QEMU_IFLA_TUN_TYPE:
606 case QEMU_IFLA_TUN_PI:
607 case QEMU_IFLA_TUN_VNET_HDR:
608 case QEMU_IFLA_TUN_PERSIST:
609 case QEMU_IFLA_TUN_MULTI_QUEUE:
612 case QEMU_IFLA_TUN_NUM_QUEUES:
613 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES:
614 case QEMU_IFLA_TUN_OWNER:
615 case QEMU_IFLA_TUN_GROUP:
616 u32 = NLA_DATA(nlattr);
617 *u32 = tswap32(*u32);
620 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_TUN type %d\n",
627 struct linkinfo_context {
634 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
637 struct linkinfo_context *li_context = context;
639 switch (nlattr->nla_type) {
641 case QEMU_IFLA_INFO_KIND:
642 li_context->name = NLA_DATA(nlattr);
643 li_context->len = nlattr->nla_len - NLA_HDRLEN;
645 case QEMU_IFLA_INFO_SLAVE_KIND:
646 li_context->slave_name = NLA_DATA(nlattr);
647 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
650 case QEMU_IFLA_INFO_XSTATS:
651 /* FIXME: only used by CAN */
654 case QEMU_IFLA_INFO_DATA:
655 if (strncmp(li_context->name, "bridge",
656 li_context->len) == 0) {
657 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
660 host_to_target_data_bridge_nlattr);
661 } else if (strncmp(li_context->name, "tun",
662 li_context->len) == 0) {
663 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
666 host_to_target_data_tun_nlattr);
668 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_KIND %s\n",
672 case QEMU_IFLA_INFO_SLAVE_DATA:
673 if (strncmp(li_context->slave_name, "bridge",
674 li_context->slave_len) == 0) {
675 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
678 host_to_target_slave_data_bridge_nlattr);
680 qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
681 li_context->slave_name);
685 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA_INFO type: %d\n",
693 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
699 switch (nlattr->nla_type) {
700 case QEMU_IFLA_INET_CONF:
701 u32 = NLA_DATA(nlattr);
702 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
704 u32[i] = tswap32(u32[i]);
708 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET type: %d\n",
714 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
719 struct ifla_cacheinfo *ci;
722 switch (nlattr->nla_type) {
724 case QEMU_IFLA_INET6_TOKEN:
727 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
730 case QEMU_IFLA_INET6_FLAGS:
731 u32 = NLA_DATA(nlattr);
732 *u32 = tswap32(*u32);
735 case QEMU_IFLA_INET6_CONF:
736 u32 = NLA_DATA(nlattr);
737 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
739 u32[i] = tswap32(u32[i]);
743 case QEMU_IFLA_INET6_CACHEINFO:
744 ci = NLA_DATA(nlattr);
745 ci->max_reasm_len = tswap32(ci->max_reasm_len);
746 ci->tstamp = tswap32(ci->tstamp);
747 ci->reachable_time = tswap32(ci->reachable_time);
748 ci->retrans_time = tswap32(ci->retrans_time);
751 case QEMU_IFLA_INET6_STATS:
752 case QEMU_IFLA_INET6_ICMP6STATS:
753 u64 = NLA_DATA(nlattr);
754 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
756 u64[i] = tswap64(u64[i]);
760 qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET6 type: %d\n",
766 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
769 switch (nlattr->nla_type) {
771 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
773 host_to_target_data_inet_nlattr);
775 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
777 host_to_target_data_inet6_nlattr);
779 qemu_log_mask(LOG_UNIMP, "Unknown host AF_SPEC type: %d\n",
786 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr,
791 switch (nlattr->nla_type) {
793 case QEMU_IFLA_XDP_ATTACHED:
796 case QEMU_IFLA_XDP_PROG_ID:
797 u32 = NLA_DATA(nlattr);
798 *u32 = tswap32(*u32);
802 LOG_UNIMP, "Unknown host XDP type: %d\n", nlattr->nla_type);
808 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
811 struct rtnl_link_stats *st;
812 struct rtnl_link_stats64 *st64;
813 struct rtnl_link_ifmap *map;
814 struct linkinfo_context li_context;
816 switch (rtattr->rta_type) {
818 case QEMU_IFLA_ADDRESS:
819 case QEMU_IFLA_BROADCAST:
820 case QEMU_IFLA_PERM_ADDRESS:
822 case QEMU_IFLA_IFNAME:
823 case QEMU_IFLA_QDISC:
826 case QEMU_IFLA_OPERSTATE:
827 case QEMU_IFLA_LINKMODE:
828 case QEMU_IFLA_CARRIER:
829 case QEMU_IFLA_PROTO_DOWN:
834 case QEMU_IFLA_WEIGHT:
835 case QEMU_IFLA_TXQLEN:
836 case QEMU_IFLA_CARRIER_CHANGES:
837 case QEMU_IFLA_NUM_RX_QUEUES:
838 case QEMU_IFLA_NUM_TX_QUEUES:
839 case QEMU_IFLA_PROMISCUITY:
840 case QEMU_IFLA_EXT_MASK:
841 case QEMU_IFLA_LINK_NETNSID:
842 case QEMU_IFLA_GROUP:
843 case QEMU_IFLA_MASTER:
844 case QEMU_IFLA_NUM_VF:
845 case QEMU_IFLA_GSO_MAX_SEGS:
846 case QEMU_IFLA_GSO_MAX_SIZE:
847 case QEMU_IFLA_CARRIER_UP_COUNT:
848 case QEMU_IFLA_CARRIER_DOWN_COUNT:
849 case QEMU_IFLA_MIN_MTU:
850 case QEMU_IFLA_MAX_MTU:
851 u32 = RTA_DATA(rtattr);
852 *u32 = tswap32(*u32);
854 /* struct rtnl_link_stats */
855 case QEMU_IFLA_STATS:
856 st = RTA_DATA(rtattr);
857 st->rx_packets = tswap32(st->rx_packets);
858 st->tx_packets = tswap32(st->tx_packets);
859 st->rx_bytes = tswap32(st->rx_bytes);
860 st->tx_bytes = tswap32(st->tx_bytes);
861 st->rx_errors = tswap32(st->rx_errors);
862 st->tx_errors = tswap32(st->tx_errors);
863 st->rx_dropped = tswap32(st->rx_dropped);
864 st->tx_dropped = tswap32(st->tx_dropped);
865 st->multicast = tswap32(st->multicast);
866 st->collisions = tswap32(st->collisions);
868 /* detailed rx_errors: */
869 st->rx_length_errors = tswap32(st->rx_length_errors);
870 st->rx_over_errors = tswap32(st->rx_over_errors);
871 st->rx_crc_errors = tswap32(st->rx_crc_errors);
872 st->rx_frame_errors = tswap32(st->rx_frame_errors);
873 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
874 st->rx_missed_errors = tswap32(st->rx_missed_errors);
876 /* detailed tx_errors */
877 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
878 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
879 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
880 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
881 st->tx_window_errors = tswap32(st->tx_window_errors);
884 st->rx_compressed = tswap32(st->rx_compressed);
885 st->tx_compressed = tswap32(st->tx_compressed);
887 /* struct rtnl_link_stats64 */
888 case QEMU_IFLA_STATS64:
889 st64 = RTA_DATA(rtattr);
890 st64->rx_packets = tswap64(st64->rx_packets);
891 st64->tx_packets = tswap64(st64->tx_packets);
892 st64->rx_bytes = tswap64(st64->rx_bytes);
893 st64->tx_bytes = tswap64(st64->tx_bytes);
894 st64->rx_errors = tswap64(st64->rx_errors);
895 st64->tx_errors = tswap64(st64->tx_errors);
896 st64->rx_dropped = tswap64(st64->rx_dropped);
897 st64->tx_dropped = tswap64(st64->tx_dropped);
898 st64->multicast = tswap64(st64->multicast);
899 st64->collisions = tswap64(st64->collisions);
901 /* detailed rx_errors: */
902 st64->rx_length_errors = tswap64(st64->rx_length_errors);
903 st64->rx_over_errors = tswap64(st64->rx_over_errors);
904 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
905 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
906 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
907 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
909 /* detailed tx_errors */
910 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
911 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
912 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
913 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
914 st64->tx_window_errors = tswap64(st64->tx_window_errors);
917 st64->rx_compressed = tswap64(st64->rx_compressed);
918 st64->tx_compressed = tswap64(st64->tx_compressed);
920 /* struct rtnl_link_ifmap */
922 map = RTA_DATA(rtattr);
923 map->mem_start = tswap64(map->mem_start);
924 map->mem_end = tswap64(map->mem_end);
925 map->base_addr = tswap64(map->base_addr);
926 map->irq = tswap16(map->irq);
929 case QEMU_IFLA_LINKINFO:
930 memset(&li_context, 0, sizeof(li_context));
931 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
933 host_to_target_data_linkinfo_nlattr);
934 case QEMU_IFLA_AF_SPEC:
935 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
937 host_to_target_data_spec_nlattr);
939 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
941 host_to_target_data_xdp_nlattr);
943 qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA type: %d\n",
950 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
953 struct ifa_cacheinfo *ci;
955 switch (rtattr->rta_type) {
956 /* binary: depends on family type */
966 u32 = RTA_DATA(rtattr);
967 *u32 = tswap32(*u32);
969 /* struct ifa_cacheinfo */
971 ci = RTA_DATA(rtattr);
972 ci->ifa_prefered = tswap32(ci->ifa_prefered);
973 ci->ifa_valid = tswap32(ci->ifa_valid);
974 ci->cstamp = tswap32(ci->cstamp);
975 ci->tstamp = tswap32(ci->tstamp);
979 LOG_UNIMP, "Unknown host IFA type: %d\n", rtattr->rta_type);
985 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
988 struct rta_cacheinfo *ci;
990 switch (rtattr->rta_type) {
991 /* binary: depends on family type */
992 case QEMU_RTA_GATEWAY:
994 case QEMU_RTA_PREFSRC:
1000 case QEMU_RTA_PRIORITY:
1001 case QEMU_RTA_TABLE:
1003 u32 = RTA_DATA(rtattr);
1004 *u32 = tswap32(*u32);
1006 /* struct rta_cacheinfo */
1007 case QEMU_RTA_CACHEINFO:
1008 ci = RTA_DATA(rtattr);
1009 ci->rta_clntref = tswap32(ci->rta_clntref);
1010 ci->rta_lastuse = tswap32(ci->rta_lastuse);
1011 ci->rta_expires = tswap32(ci->rta_expires);
1012 ci->rta_error = tswap32(ci->rta_error);
1013 ci->rta_used = tswap32(ci->rta_used);
1014 #if defined(RTNETLINK_HAVE_PEERINFO)
1015 ci->rta_id = tswap32(ci->rta_id);
1016 ci->rta_ts = tswap32(ci->rta_ts);
1017 ci->rta_tsage = tswap32(ci->rta_tsage);
1022 LOG_UNIMP, "Unknown host RTA type: %d\n", rtattr->rta_type);
1028 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1029 uint32_t rtattr_len)
1031 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1032 host_to_target_data_link_rtattr);
1035 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1036 uint32_t rtattr_len)
1038 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1039 host_to_target_data_addr_rtattr);
1042 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1043 uint32_t rtattr_len)
1045 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1046 host_to_target_data_route_rtattr);
1049 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1052 struct ifinfomsg *ifi;
1053 struct ifaddrmsg *ifa;
1056 nlmsg_len = nlh->nlmsg_len;
1057 switch (nlh->nlmsg_type) {
1061 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1062 ifi = NLMSG_DATA(nlh);
1063 ifi->ifi_type = tswap16(ifi->ifi_type);
1064 ifi->ifi_index = tswap32(ifi->ifi_index);
1065 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1066 ifi->ifi_change = tswap32(ifi->ifi_change);
1067 host_to_target_link_rtattr(IFLA_RTA(ifi),
1068 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1074 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1075 ifa = NLMSG_DATA(nlh);
1076 ifa->ifa_index = tswap32(ifa->ifa_index);
1077 host_to_target_addr_rtattr(IFA_RTA(ifa),
1078 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1084 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1085 rtm = NLMSG_DATA(nlh);
1086 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1087 host_to_target_route_rtattr(RTM_RTA(rtm),
1088 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1092 return -TARGET_EINVAL;
1097 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1100 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1103 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1105 abi_long (*target_to_host_rtattr)
1108 unsigned short aligned_rta_len;
1111 while (len >= sizeof(struct rtattr)) {
1112 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1113 tswap16(rtattr->rta_len) > len) {
1116 rtattr->rta_len = tswap16(rtattr->rta_len);
1117 rtattr->rta_type = tswap16(rtattr->rta_type);
1118 ret = target_to_host_rtattr(rtattr);
1123 aligned_rta_len = RTA_ALIGN(rtattr->rta_len);
1124 if (aligned_rta_len >= len) {
1127 len -= aligned_rta_len;
1128 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
1133 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1137 switch (rtattr->rta_type) {
1139 case QEMU_IFLA_EXT_MASK:
1140 u32 = RTA_DATA(rtattr);
1141 *u32 = tswap32(*u32);
1144 qemu_log_mask(LOG_UNIMP, "Unknown target QEMU_IFLA type: %d\n",
1151 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1153 switch (rtattr->rta_type) {
1154 /* binary: depends on family type */
1159 qemu_log_mask(LOG_UNIMP, "Unknown target IFA type: %d\n",
1166 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1169 switch (rtattr->rta_type) {
1170 /* binary: depends on family type */
1173 case QEMU_RTA_GATEWAY:
1176 case QEMU_RTA_PRIORITY:
1177 case QEMU_RTA_TABLE:
1179 u32 = RTA_DATA(rtattr);
1180 *u32 = tswap32(*u32);
1183 qemu_log_mask(LOG_UNIMP, "Unknown target RTA type: %d\n",
1190 static void target_to_host_link_rtattr(struct rtattr *rtattr,
1191 uint32_t rtattr_len)
1193 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1194 target_to_host_data_link_rtattr);
1197 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1198 uint32_t rtattr_len)
1200 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1201 target_to_host_data_addr_rtattr);
1204 static void target_to_host_route_rtattr(struct rtattr *rtattr,
1205 uint32_t rtattr_len)
1207 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1208 target_to_host_data_route_rtattr);
1211 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
1213 struct ifinfomsg *ifi;
1214 struct ifaddrmsg *ifa;
1217 switch (nlh->nlmsg_type) {
1222 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1223 ifi = NLMSG_DATA(nlh);
1224 ifi->ifi_type = tswap16(ifi->ifi_type);
1225 ifi->ifi_index = tswap32(ifi->ifi_index);
1226 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1227 ifi->ifi_change = tswap32(ifi->ifi_change);
1228 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
1229 NLMSG_LENGTH(sizeof(*ifi)));
1235 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1236 ifa = NLMSG_DATA(nlh);
1237 ifa->ifa_index = tswap32(ifa->ifa_index);
1238 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
1239 NLMSG_LENGTH(sizeof(*ifa)));
1245 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1246 rtm = NLMSG_DATA(nlh);
1247 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1248 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
1249 NLMSG_LENGTH(sizeof(*rtm)));
1253 return -TARGET_EOPNOTSUPP;
1258 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
1260 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
1262 #endif /* CONFIG_RTNETLINK */
1264 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
1266 switch (nlh->nlmsg_type) {
1268 qemu_log_mask(LOG_UNIMP, "Unknown host audit message type %d\n",
1270 return -TARGET_EINVAL;
1275 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
1278 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
1281 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
1283 switch (nlh->nlmsg_type) {
1285 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1286 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
1289 qemu_log_mask(LOG_UNIMP, "Unknown target audit message type %d\n",
1291 return -TARGET_EINVAL;
1297 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
1299 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
1302 static abi_long packet_target_to_host_sockaddr(void *host_addr,
1303 abi_ulong target_addr,
1306 struct sockaddr *addr = host_addr;
1307 struct target_sockaddr *target_saddr;
1309 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1310 if (!target_saddr) {
1311 return -TARGET_EFAULT;
1314 memcpy(addr, target_saddr, len);
1315 addr->sa_family = tswap16(target_saddr->sa_family);
1316 /* spkt_protocol is big-endian */
1318 unlock_user(target_saddr, target_addr, 0);
1322 TargetFdTrans target_packet_trans = {
1323 .target_to_host_addr = packet_target_to_host_sockaddr,
1326 #ifdef CONFIG_RTNETLINK
1327 static abi_long netlink_route_target_to_host(void *buf, size_t len)
1331 ret = target_to_host_nlmsg_route(buf, len);
1339 static abi_long netlink_route_host_to_target(void *buf, size_t len)
1343 ret = host_to_target_nlmsg_route(buf, len);
1351 TargetFdTrans target_netlink_route_trans = {
1352 .target_to_host_data = netlink_route_target_to_host,
1353 .host_to_target_data = netlink_route_host_to_target,
1355 #endif /* CONFIG_RTNETLINK */
1357 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
1361 ret = target_to_host_nlmsg_audit(buf, len);
1369 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
1373 ret = host_to_target_nlmsg_audit(buf, len);
1381 TargetFdTrans target_netlink_audit_trans = {
1382 .target_to_host_data = netlink_audit_target_to_host,
1383 .host_to_target_data = netlink_audit_host_to_target,
1386 /* signalfd siginfo conversion */
1389 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
1390 const struct signalfd_siginfo *info)
1392 int sig = host_to_target_signal(info->ssi_signo);
1394 /* linux/signalfd.h defines a ssi_addr_lsb
1395 * not defined in sys/signalfd.h but used by some kernels
1398 #ifdef BUS_MCEERR_AO
1399 if (tinfo->ssi_signo == SIGBUS &&
1400 (tinfo->ssi_code == BUS_MCEERR_AR ||
1401 tinfo->ssi_code == BUS_MCEERR_AO)) {
1402 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
1403 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
1404 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
1408 tinfo->ssi_signo = tswap32(sig);
1409 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
1410 tinfo->ssi_code = tswap32(info->ssi_code);
1411 tinfo->ssi_pid = tswap32(info->ssi_pid);
1412 tinfo->ssi_uid = tswap32(info->ssi_uid);
1413 tinfo->ssi_fd = tswap32(info->ssi_fd);
1414 tinfo->ssi_tid = tswap32(info->ssi_tid);
1415 tinfo->ssi_band = tswap32(info->ssi_band);
1416 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
1417 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
1418 tinfo->ssi_status = tswap32(info->ssi_status);
1419 tinfo->ssi_int = tswap32(info->ssi_int);
1420 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
1421 tinfo->ssi_utime = tswap64(info->ssi_utime);
1422 tinfo->ssi_stime = tswap64(info->ssi_stime);
1423 tinfo->ssi_addr = tswap64(info->ssi_addr);
1426 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
1430 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
1431 host_to_target_signalfd_siginfo(buf + i, buf + i);
1437 TargetFdTrans target_signalfd_trans = {
1438 .host_to_target_data = host_to_target_data_signalfd,
1441 static abi_long swap_data_eventfd(void *buf, size_t len)
1443 uint64_t *counter = buf;
1446 if (len < sizeof(uint64_t)) {
1450 for (i = 0; i < len; i += sizeof(uint64_t)) {
1451 *counter = tswap64(*counter);
1458 TargetFdTrans target_eventfd_trans = {
1459 .host_to_target_data = swap_data_eventfd,
1460 .target_to_host_data = swap_data_eventfd,
1463 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
1464 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
1465 defined(__NR_inotify_init1))
1466 static abi_long host_to_target_data_inotify(void *buf, size_t len)
1468 struct inotify_event *ev;
1472 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
1473 ev = (struct inotify_event *)((char *)buf + i);
1476 ev->wd = tswap32(ev->wd);
1477 ev->mask = tswap32(ev->mask);
1478 ev->cookie = tswap32(ev->cookie);
1479 ev->len = tswap32(name_len);
1485 TargetFdTrans target_inotify_trans = {
1486 .host_to_target_data = host_to_target_data_inotify,