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>
34 QEMU_IFLA_BR_FORWARD_DELAY,
35 QEMU_IFLA_BR_HELLO_TIME,
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,
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,
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,
103 QEMU_IFLA_NET_NS_PID,
106 QEMU_IFLA_VFINFO_LIST,
114 QEMU_IFLA_PROMISCUITY,
115 QEMU_IFLA_NUM_TX_QUEUES,
116 QEMU_IFLA_NUM_RX_QUEUES,
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,
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,
140 QEMU_IFLA_BRPORT_UNSPEC,
141 QEMU_IFLA_BRPORT_STATE,
142 QEMU_IFLA_BRPORT_PRIORITY,
143 QEMU_IFLA_BRPORT_COST,
144 QEMU_IFLA_BRPORT_MODE,
145 QEMU_IFLA_BRPORT_GUARD,
146 QEMU_IFLA_BRPORT_PROTECT,
147 QEMU_IFLA_BRPORT_FAST_LEAVE,
148 QEMU_IFLA_BRPORT_LEARNING,
149 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
150 QEMU_IFLA_BRPORT_PROXYARP,
151 QEMU_IFLA_BRPORT_LEARNING_SYNC,
152 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
153 QEMU_IFLA_BRPORT_ROOT_ID,
154 QEMU_IFLA_BRPORT_BRIDGE_ID,
155 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
156 QEMU_IFLA_BRPORT_DESIGNATED_COST,
159 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
160 QEMU_IFLA_BRPORT_CONFIG_PENDING,
161 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
162 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
163 QEMU_IFLA_BRPORT_HOLD_TIMER,
164 QEMU_IFLA_BRPORT_FLUSH,
165 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
166 QEMU_IFLA_BRPORT_PAD,
167 QEMU_IFLA_BRPORT_MCAST_FLOOD,
168 QEMU_IFLA_BRPORT_MCAST_TO_UCAST,
169 QEMU_IFLA_BRPORT_VLAN_TUNNEL,
170 QEMU_IFLA_BRPORT_BCAST_FLOOD,
171 QEMU_IFLA_BRPORT_GROUP_FWD_MASK,
172 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS,
173 QEMU_IFLA_BRPORT_ISOLATED,
174 QEMU_IFLA_BRPORT_BACKUP_PORT,
175 QEMU___IFLA_BRPORT_MAX
179 QEMU_IFLA_TUN_UNSPEC,
184 QEMU_IFLA_TUN_VNET_HDR,
185 QEMU_IFLA_TUN_PERSIST,
186 QEMU_IFLA_TUN_MULTI_QUEUE,
187 QEMU_IFLA_TUN_NUM_QUEUES,
188 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES,
193 QEMU_IFLA_INFO_UNSPEC,
196 QEMU_IFLA_INFO_XSTATS,
197 QEMU_IFLA_INFO_SLAVE_KIND,
198 QEMU_IFLA_INFO_SLAVE_DATA,
199 QEMU___IFLA_INFO_MAX,
203 QEMU_IFLA_INET_UNSPEC,
205 QEMU___IFLA_INET_MAX,
209 QEMU_IFLA_INET6_UNSPEC,
210 QEMU_IFLA_INET6_FLAGS,
211 QEMU_IFLA_INET6_CONF,
212 QEMU_IFLA_INET6_STATS,
213 QEMU_IFLA_INET6_MCAST,
214 QEMU_IFLA_INET6_CACHEINFO,
215 QEMU_IFLA_INET6_ICMP6STATS,
216 QEMU_IFLA_INET6_TOKEN,
217 QEMU_IFLA_INET6_ADDR_GEN_MODE,
218 QEMU___IFLA_INET6_MAX
222 QEMU_IFLA_XDP_UNSPEC,
224 QEMU_IFLA_XDP_ATTACHED,
226 QEMU_IFLA_XDP_PROG_ID,
241 QEMU_RTA_PROTOINFO, /* no longer used */
244 QEMU_RTA_SESSION, /* no longer used */
245 QEMU_RTA_MP_ALGO, /* no longer used */
257 QEMU_RTA_TTL_PROPAGATE,
264 TargetFdTrans **target_fd_trans;
265 unsigned int target_fd_max;
267 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
269 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
270 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
271 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
272 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
273 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
276 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
278 abi_long (*host_to_target_nlmsg)
282 uint32_t aligned_nlmsg_len;
285 while (len > sizeof(struct nlmsghdr)) {
287 nlmsg_len = nlh->nlmsg_len;
288 if (nlmsg_len < sizeof(struct nlmsghdr) ||
293 switch (nlh->nlmsg_type) {
301 struct nlmsgerr *e = NLMSG_DATA(nlh);
302 e->error = tswap32(e->error);
303 tswap_nlmsghdr(&e->msg);
308 ret = host_to_target_nlmsg(nlh);
317 aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len);
318 if (aligned_nlmsg_len >= len) {
321 len -= aligned_nlmsg_len;
322 nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len);
327 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
329 abi_long (*target_to_host_nlmsg)
332 uint32_t aligned_nlmsg_len;
335 while (len > sizeof(struct nlmsghdr)) {
336 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
337 tswap32(nlh->nlmsg_len) > len) {
341 switch (nlh->nlmsg_type) {
348 struct nlmsgerr *e = NLMSG_DATA(nlh);
349 e->error = tswap32(e->error);
350 tswap_nlmsghdr(&e->msg);
354 ret = target_to_host_nlmsg(nlh);
360 aligned_nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len);
361 if (aligned_nlmsg_len >= len) {
364 len -= aligned_nlmsg_len;
365 nlh = (struct nlmsghdr *)(((char *)nlh) + aligned_nlmsg_len);
370 #ifdef CONFIG_RTNETLINK
371 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
372 size_t len, void *context,
373 abi_long (*host_to_target_nlattr)
377 unsigned short nla_len;
378 unsigned short aligned_nla_len;
381 while (len > sizeof(struct nlattr)) {
382 nla_len = nlattr->nla_len;
383 if (nla_len < sizeof(struct nlattr) ||
387 ret = host_to_target_nlattr(nlattr, context);
388 nlattr->nla_len = tswap16(nlattr->nla_len);
389 nlattr->nla_type = tswap16(nlattr->nla_type);
394 aligned_nla_len = NLA_ALIGN(nla_len);
395 if (aligned_nla_len >= len) {
398 len -= aligned_nla_len;
399 nlattr = (struct nlattr *)(((char *)nlattr) + aligned_nla_len);
404 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
406 abi_long (*host_to_target_rtattr)
409 unsigned short rta_len;
410 unsigned short aligned_rta_len;
413 while (len > sizeof(struct rtattr)) {
414 rta_len = rtattr->rta_len;
415 if (rta_len < sizeof(struct rtattr) ||
419 ret = host_to_target_rtattr(rtattr);
420 rtattr->rta_len = tswap16(rtattr->rta_len);
421 rtattr->rta_type = tswap16(rtattr->rta_type);
426 aligned_rta_len = RTA_ALIGN(rta_len);
427 if (aligned_rta_len >= len) {
430 len -= aligned_rta_len;
431 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
436 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
438 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
445 switch (nlattr->nla_type) {
447 case QEMU_IFLA_BR_FDB_FLUSH:
450 case QEMU_IFLA_BR_GROUP_ADDR:
453 case QEMU_IFLA_BR_VLAN_FILTERING:
454 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
455 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
456 case QEMU_IFLA_BR_MCAST_ROUTER:
457 case QEMU_IFLA_BR_MCAST_SNOOPING:
458 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
459 case QEMU_IFLA_BR_MCAST_QUERIER:
460 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
461 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
462 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
463 case QEMU_IFLA_BR_VLAN_STATS_ENABLED:
464 case QEMU_IFLA_BR_MCAST_STATS_ENABLED:
465 case QEMU_IFLA_BR_MCAST_IGMP_VERSION:
466 case QEMU_IFLA_BR_MCAST_MLD_VERSION:
467 case QEMU_IFLA_BR_VLAN_STATS_PER_PORT:
470 case QEMU_IFLA_BR_PRIORITY:
471 case QEMU_IFLA_BR_VLAN_PROTOCOL:
472 case QEMU_IFLA_BR_GROUP_FWD_MASK:
473 case QEMU_IFLA_BR_ROOT_PORT:
474 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
475 u16 = NLA_DATA(nlattr);
476 *u16 = tswap16(*u16);
479 case QEMU_IFLA_BR_FORWARD_DELAY:
480 case QEMU_IFLA_BR_HELLO_TIME:
481 case QEMU_IFLA_BR_MAX_AGE:
482 case QEMU_IFLA_BR_AGEING_TIME:
483 case QEMU_IFLA_BR_STP_STATE:
484 case QEMU_IFLA_BR_ROOT_PATH_COST:
485 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
486 case QEMU_IFLA_BR_MCAST_HASH_MAX:
487 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
488 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
489 u32 = NLA_DATA(nlattr);
490 *u32 = tswap32(*u32);
493 case QEMU_IFLA_BR_HELLO_TIMER:
494 case QEMU_IFLA_BR_TCN_TIMER:
495 case QEMU_IFLA_BR_GC_TIMER:
496 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
497 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
498 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
499 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
500 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
501 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
502 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
503 u64 = NLA_DATA(nlattr);
504 *u64 = tswap64(*u64);
506 /* ifla_bridge_id: uin8_t[] */
507 case QEMU_IFLA_BR_ROOT_ID:
508 case QEMU_IFLA_BR_BRIDGE_ID:
510 /* br_boolopt_multi { uint32_t, uint32_t } */
511 case QEMU_IFLA_BR_MULTI_BOOLOPT:
512 u32 = NLA_DATA(nlattr);
513 u32[0] = tswap32(u32[0]); /* optval */
514 u32[1] = tswap32(u32[1]); /* optmask */
517 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr->nla_type);
523 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
530 switch (nlattr->nla_type) {
532 case QEMU_IFLA_BRPORT_STATE:
533 case QEMU_IFLA_BRPORT_MODE:
534 case QEMU_IFLA_BRPORT_GUARD:
535 case QEMU_IFLA_BRPORT_PROTECT:
536 case QEMU_IFLA_BRPORT_FAST_LEAVE:
537 case QEMU_IFLA_BRPORT_LEARNING:
538 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
539 case QEMU_IFLA_BRPORT_PROXYARP:
540 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
541 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
542 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
543 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
544 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
545 case QEMU_IFLA_BRPORT_MCAST_FLOOD:
546 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST:
547 case QEMU_IFLA_BRPORT_VLAN_TUNNEL:
548 case QEMU_IFLA_BRPORT_BCAST_FLOOD:
549 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS:
550 case QEMU_IFLA_BRPORT_ISOLATED:
553 case QEMU_IFLA_BRPORT_PRIORITY:
554 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
555 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
556 case QEMU_IFLA_BRPORT_ID:
557 case QEMU_IFLA_BRPORT_NO:
558 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK:
559 u16 = NLA_DATA(nlattr);
560 *u16 = tswap16(*u16);
563 case QEMU_IFLA_BRPORT_COST:
564 case QEMU_IFLA_BRPORT_BACKUP_PORT:
565 u32 = NLA_DATA(nlattr);
566 *u32 = tswap32(*u32);
569 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
570 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
571 case QEMU_IFLA_BRPORT_HOLD_TIMER:
572 u64 = NLA_DATA(nlattr);
573 *u64 = tswap64(*u64);
575 /* ifla_bridge_id: uint8_t[] */
576 case QEMU_IFLA_BRPORT_ROOT_ID:
577 case QEMU_IFLA_BRPORT_BRIDGE_ID:
580 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr->nla_type);
586 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr,
591 switch (nlattr->nla_type) {
593 case QEMU_IFLA_TUN_TYPE:
594 case QEMU_IFLA_TUN_PI:
595 case QEMU_IFLA_TUN_VNET_HDR:
596 case QEMU_IFLA_TUN_PERSIST:
597 case QEMU_IFLA_TUN_MULTI_QUEUE:
600 case QEMU_IFLA_TUN_NUM_QUEUES:
601 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES:
602 case QEMU_IFLA_TUN_OWNER:
603 case QEMU_IFLA_TUN_GROUP:
604 u32 = NLA_DATA(nlattr);
605 *u32 = tswap32(*u32);
608 gemu_log("Unknown QEMU_IFLA_TUN type %d\n", nlattr->nla_type);
614 struct linkinfo_context {
621 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
624 struct linkinfo_context *li_context = context;
626 switch (nlattr->nla_type) {
628 case QEMU_IFLA_INFO_KIND:
629 li_context->name = NLA_DATA(nlattr);
630 li_context->len = nlattr->nla_len - NLA_HDRLEN;
632 case QEMU_IFLA_INFO_SLAVE_KIND:
633 li_context->slave_name = NLA_DATA(nlattr);
634 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
637 case QEMU_IFLA_INFO_XSTATS:
638 /* FIXME: only used by CAN */
641 case QEMU_IFLA_INFO_DATA:
642 if (strncmp(li_context->name, "bridge",
643 li_context->len) == 0) {
644 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
647 host_to_target_data_bridge_nlattr);
648 } else if (strncmp(li_context->name, "tun",
649 li_context->len) == 0) {
650 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
653 host_to_target_data_tun_nlattr);
655 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context->name);
658 case QEMU_IFLA_INFO_SLAVE_DATA:
659 if (strncmp(li_context->slave_name, "bridge",
660 li_context->slave_len) == 0) {
661 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
664 host_to_target_slave_data_bridge_nlattr);
666 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
667 li_context->slave_name);
671 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr->nla_type);
678 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
684 switch (nlattr->nla_type) {
685 case QEMU_IFLA_INET_CONF:
686 u32 = NLA_DATA(nlattr);
687 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
689 u32[i] = tswap32(u32[i]);
693 gemu_log("Unknown host AF_INET type: %d\n", nlattr->nla_type);
698 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
703 struct ifla_cacheinfo *ci;
706 switch (nlattr->nla_type) {
708 case QEMU_IFLA_INET6_TOKEN:
711 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
714 case QEMU_IFLA_INET6_FLAGS:
715 u32 = NLA_DATA(nlattr);
716 *u32 = tswap32(*u32);
719 case QEMU_IFLA_INET6_CONF:
720 u32 = NLA_DATA(nlattr);
721 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
723 u32[i] = tswap32(u32[i]);
727 case QEMU_IFLA_INET6_CACHEINFO:
728 ci = NLA_DATA(nlattr);
729 ci->max_reasm_len = tswap32(ci->max_reasm_len);
730 ci->tstamp = tswap32(ci->tstamp);
731 ci->reachable_time = tswap32(ci->reachable_time);
732 ci->retrans_time = tswap32(ci->retrans_time);
735 case QEMU_IFLA_INET6_STATS:
736 case QEMU_IFLA_INET6_ICMP6STATS:
737 u64 = NLA_DATA(nlattr);
738 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
740 u64[i] = tswap64(u64[i]);
744 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr->nla_type);
749 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
752 switch (nlattr->nla_type) {
754 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
756 host_to_target_data_inet_nlattr);
758 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
760 host_to_target_data_inet6_nlattr);
762 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr->nla_type);
768 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr,
773 switch (nlattr->nla_type) {
775 case QEMU_IFLA_XDP_ATTACHED:
778 case QEMU_IFLA_XDP_PROG_ID:
779 u32 = NLA_DATA(nlattr);
780 *u32 = tswap32(*u32);
783 gemu_log("Unknown host XDP type: %d\n", nlattr->nla_type);
789 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
792 struct rtnl_link_stats *st;
793 struct rtnl_link_stats64 *st64;
794 struct rtnl_link_ifmap *map;
795 struct linkinfo_context li_context;
797 switch (rtattr->rta_type) {
799 case QEMU_IFLA_ADDRESS:
800 case QEMU_IFLA_BROADCAST:
802 case QEMU_IFLA_IFNAME:
803 case QEMU_IFLA_QDISC:
806 case QEMU_IFLA_OPERSTATE:
807 case QEMU_IFLA_LINKMODE:
808 case QEMU_IFLA_CARRIER:
809 case QEMU_IFLA_PROTO_DOWN:
814 case QEMU_IFLA_WEIGHT:
815 case QEMU_IFLA_TXQLEN:
816 case QEMU_IFLA_CARRIER_CHANGES:
817 case QEMU_IFLA_NUM_RX_QUEUES:
818 case QEMU_IFLA_NUM_TX_QUEUES:
819 case QEMU_IFLA_PROMISCUITY:
820 case QEMU_IFLA_EXT_MASK:
821 case QEMU_IFLA_LINK_NETNSID:
822 case QEMU_IFLA_GROUP:
823 case QEMU_IFLA_MASTER:
824 case QEMU_IFLA_NUM_VF:
825 case QEMU_IFLA_GSO_MAX_SEGS:
826 case QEMU_IFLA_GSO_MAX_SIZE:
827 case QEMU_IFLA_CARRIER_UP_COUNT:
828 case QEMU_IFLA_CARRIER_DOWN_COUNT:
829 case QEMU_IFLA_MIN_MTU:
830 case QEMU_IFLA_MAX_MTU:
831 u32 = RTA_DATA(rtattr);
832 *u32 = tswap32(*u32);
834 /* struct rtnl_link_stats */
835 case QEMU_IFLA_STATS:
836 st = RTA_DATA(rtattr);
837 st->rx_packets = tswap32(st->rx_packets);
838 st->tx_packets = tswap32(st->tx_packets);
839 st->rx_bytes = tswap32(st->rx_bytes);
840 st->tx_bytes = tswap32(st->tx_bytes);
841 st->rx_errors = tswap32(st->rx_errors);
842 st->tx_errors = tswap32(st->tx_errors);
843 st->rx_dropped = tswap32(st->rx_dropped);
844 st->tx_dropped = tswap32(st->tx_dropped);
845 st->multicast = tswap32(st->multicast);
846 st->collisions = tswap32(st->collisions);
848 /* detailed rx_errors: */
849 st->rx_length_errors = tswap32(st->rx_length_errors);
850 st->rx_over_errors = tswap32(st->rx_over_errors);
851 st->rx_crc_errors = tswap32(st->rx_crc_errors);
852 st->rx_frame_errors = tswap32(st->rx_frame_errors);
853 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
854 st->rx_missed_errors = tswap32(st->rx_missed_errors);
856 /* detailed tx_errors */
857 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
858 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
859 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
860 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
861 st->tx_window_errors = tswap32(st->tx_window_errors);
864 st->rx_compressed = tswap32(st->rx_compressed);
865 st->tx_compressed = tswap32(st->tx_compressed);
867 /* struct rtnl_link_stats64 */
868 case QEMU_IFLA_STATS64:
869 st64 = RTA_DATA(rtattr);
870 st64->rx_packets = tswap64(st64->rx_packets);
871 st64->tx_packets = tswap64(st64->tx_packets);
872 st64->rx_bytes = tswap64(st64->rx_bytes);
873 st64->tx_bytes = tswap64(st64->tx_bytes);
874 st64->rx_errors = tswap64(st64->rx_errors);
875 st64->tx_errors = tswap64(st64->tx_errors);
876 st64->rx_dropped = tswap64(st64->rx_dropped);
877 st64->tx_dropped = tswap64(st64->tx_dropped);
878 st64->multicast = tswap64(st64->multicast);
879 st64->collisions = tswap64(st64->collisions);
881 /* detailed rx_errors: */
882 st64->rx_length_errors = tswap64(st64->rx_length_errors);
883 st64->rx_over_errors = tswap64(st64->rx_over_errors);
884 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
885 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
886 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
887 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
889 /* detailed tx_errors */
890 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
891 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
892 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
893 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
894 st64->tx_window_errors = tswap64(st64->tx_window_errors);
897 st64->rx_compressed = tswap64(st64->rx_compressed);
898 st64->tx_compressed = tswap64(st64->tx_compressed);
900 /* struct rtnl_link_ifmap */
902 map = RTA_DATA(rtattr);
903 map->mem_start = tswap64(map->mem_start);
904 map->mem_end = tswap64(map->mem_end);
905 map->base_addr = tswap64(map->base_addr);
906 map->irq = tswap16(map->irq);
909 case QEMU_IFLA_LINKINFO:
910 memset(&li_context, 0, sizeof(li_context));
911 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
913 host_to_target_data_linkinfo_nlattr);
914 case QEMU_IFLA_AF_SPEC:
915 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
917 host_to_target_data_spec_nlattr);
919 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
921 host_to_target_data_xdp_nlattr);
923 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr->rta_type);
929 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
932 struct ifa_cacheinfo *ci;
934 switch (rtattr->rta_type) {
935 /* binary: depends on family type */
945 u32 = RTA_DATA(rtattr);
946 *u32 = tswap32(*u32);
948 /* struct ifa_cacheinfo */
950 ci = RTA_DATA(rtattr);
951 ci->ifa_prefered = tswap32(ci->ifa_prefered);
952 ci->ifa_valid = tswap32(ci->ifa_valid);
953 ci->cstamp = tswap32(ci->cstamp);
954 ci->tstamp = tswap32(ci->tstamp);
957 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
963 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
966 struct rta_cacheinfo *ci;
968 switch (rtattr->rta_type) {
969 /* binary: depends on family type */
970 case QEMU_RTA_GATEWAY:
972 case QEMU_RTA_PREFSRC:
978 case QEMU_RTA_PRIORITY:
981 u32 = RTA_DATA(rtattr);
982 *u32 = tswap32(*u32);
984 /* struct rta_cacheinfo */
985 case QEMU_RTA_CACHEINFO:
986 ci = RTA_DATA(rtattr);
987 ci->rta_clntref = tswap32(ci->rta_clntref);
988 ci->rta_lastuse = tswap32(ci->rta_lastuse);
989 ci->rta_expires = tswap32(ci->rta_expires);
990 ci->rta_error = tswap32(ci->rta_error);
991 ci->rta_used = tswap32(ci->rta_used);
992 #if defined(RTNETLINK_HAVE_PEERINFO)
993 ci->rta_id = tswap32(ci->rta_id);
994 ci->rta_ts = tswap32(ci->rta_ts);
995 ci->rta_tsage = tswap32(ci->rta_tsage);
999 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
1005 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1006 uint32_t rtattr_len)
1008 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1009 host_to_target_data_link_rtattr);
1012 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1013 uint32_t rtattr_len)
1015 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1016 host_to_target_data_addr_rtattr);
1019 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1020 uint32_t rtattr_len)
1022 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1023 host_to_target_data_route_rtattr);
1026 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1029 struct ifinfomsg *ifi;
1030 struct ifaddrmsg *ifa;
1033 nlmsg_len = nlh->nlmsg_len;
1034 switch (nlh->nlmsg_type) {
1038 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1039 ifi = NLMSG_DATA(nlh);
1040 ifi->ifi_type = tswap16(ifi->ifi_type);
1041 ifi->ifi_index = tswap32(ifi->ifi_index);
1042 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1043 ifi->ifi_change = tswap32(ifi->ifi_change);
1044 host_to_target_link_rtattr(IFLA_RTA(ifi),
1045 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1051 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1052 ifa = NLMSG_DATA(nlh);
1053 ifa->ifa_index = tswap32(ifa->ifa_index);
1054 host_to_target_addr_rtattr(IFA_RTA(ifa),
1055 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1061 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1062 rtm = NLMSG_DATA(nlh);
1063 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1064 host_to_target_route_rtattr(RTM_RTA(rtm),
1065 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1069 return -TARGET_EINVAL;
1074 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1077 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1080 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1082 abi_long (*target_to_host_rtattr)
1085 unsigned short aligned_rta_len;
1088 while (len >= sizeof(struct rtattr)) {
1089 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1090 tswap16(rtattr->rta_len) > len) {
1093 rtattr->rta_len = tswap16(rtattr->rta_len);
1094 rtattr->rta_type = tswap16(rtattr->rta_type);
1095 ret = target_to_host_rtattr(rtattr);
1100 aligned_rta_len = RTA_ALIGN(rtattr->rta_len);
1101 if (aligned_rta_len >= len) {
1104 len -= aligned_rta_len;
1105 rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
1110 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1112 switch (rtattr->rta_type) {
1114 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr->rta_type);
1120 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1122 switch (rtattr->rta_type) {
1123 /* binary: depends on family type */
1128 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
1134 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1137 switch (rtattr->rta_type) {
1138 /* binary: depends on family type */
1141 case QEMU_RTA_GATEWAY:
1144 case QEMU_RTA_PRIORITY:
1146 u32 = RTA_DATA(rtattr);
1147 *u32 = tswap32(*u32);
1150 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
1156 static void target_to_host_link_rtattr(struct rtattr *rtattr,
1157 uint32_t rtattr_len)
1159 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1160 target_to_host_data_link_rtattr);
1163 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1164 uint32_t rtattr_len)
1166 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1167 target_to_host_data_addr_rtattr);
1170 static void target_to_host_route_rtattr(struct rtattr *rtattr,
1171 uint32_t rtattr_len)
1173 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1174 target_to_host_data_route_rtattr);
1177 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
1179 struct ifinfomsg *ifi;
1180 struct ifaddrmsg *ifa;
1183 switch (nlh->nlmsg_type) {
1188 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1189 ifi = NLMSG_DATA(nlh);
1190 ifi->ifi_type = tswap16(ifi->ifi_type);
1191 ifi->ifi_index = tswap32(ifi->ifi_index);
1192 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1193 ifi->ifi_change = tswap32(ifi->ifi_change);
1194 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
1195 NLMSG_LENGTH(sizeof(*ifi)));
1201 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1202 ifa = NLMSG_DATA(nlh);
1203 ifa->ifa_index = tswap32(ifa->ifa_index);
1204 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
1205 NLMSG_LENGTH(sizeof(*ifa)));
1212 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1213 rtm = NLMSG_DATA(nlh);
1214 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1215 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
1216 NLMSG_LENGTH(sizeof(*rtm)));
1220 return -TARGET_EOPNOTSUPP;
1225 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
1227 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
1229 #endif /* CONFIG_RTNETLINK */
1231 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
1233 switch (nlh->nlmsg_type) {
1235 gemu_log("Unknown host audit message type %d\n",
1237 return -TARGET_EINVAL;
1242 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
1245 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
1248 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
1250 switch (nlh->nlmsg_type) {
1252 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1253 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
1256 gemu_log("Unknown target audit message type %d\n",
1258 return -TARGET_EINVAL;
1264 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
1266 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
1269 static abi_long packet_target_to_host_sockaddr(void *host_addr,
1270 abi_ulong target_addr,
1273 struct sockaddr *addr = host_addr;
1274 struct target_sockaddr *target_saddr;
1276 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1277 if (!target_saddr) {
1278 return -TARGET_EFAULT;
1281 memcpy(addr, target_saddr, len);
1282 addr->sa_family = tswap16(target_saddr->sa_family);
1283 /* spkt_protocol is big-endian */
1285 unlock_user(target_saddr, target_addr, 0);
1289 TargetFdTrans target_packet_trans = {
1290 .target_to_host_addr = packet_target_to_host_sockaddr,
1293 #ifdef CONFIG_RTNETLINK
1294 static abi_long netlink_route_target_to_host(void *buf, size_t len)
1298 ret = target_to_host_nlmsg_route(buf, len);
1306 static abi_long netlink_route_host_to_target(void *buf, size_t len)
1310 ret = host_to_target_nlmsg_route(buf, len);
1318 TargetFdTrans target_netlink_route_trans = {
1319 .target_to_host_data = netlink_route_target_to_host,
1320 .host_to_target_data = netlink_route_host_to_target,
1322 #endif /* CONFIG_RTNETLINK */
1324 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
1328 ret = target_to_host_nlmsg_audit(buf, len);
1336 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
1340 ret = host_to_target_nlmsg_audit(buf, len);
1348 TargetFdTrans target_netlink_audit_trans = {
1349 .target_to_host_data = netlink_audit_target_to_host,
1350 .host_to_target_data = netlink_audit_host_to_target,
1353 /* signalfd siginfo conversion */
1356 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
1357 const struct signalfd_siginfo *info)
1359 int sig = host_to_target_signal(info->ssi_signo);
1361 /* linux/signalfd.h defines a ssi_addr_lsb
1362 * not defined in sys/signalfd.h but used by some kernels
1365 #ifdef BUS_MCEERR_AO
1366 if (tinfo->ssi_signo == SIGBUS &&
1367 (tinfo->ssi_code == BUS_MCEERR_AR ||
1368 tinfo->ssi_code == BUS_MCEERR_AO)) {
1369 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
1370 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
1371 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
1375 tinfo->ssi_signo = tswap32(sig);
1376 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
1377 tinfo->ssi_code = tswap32(info->ssi_code);
1378 tinfo->ssi_pid = tswap32(info->ssi_pid);
1379 tinfo->ssi_uid = tswap32(info->ssi_uid);
1380 tinfo->ssi_fd = tswap32(info->ssi_fd);
1381 tinfo->ssi_tid = tswap32(info->ssi_tid);
1382 tinfo->ssi_band = tswap32(info->ssi_band);
1383 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
1384 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
1385 tinfo->ssi_status = tswap32(info->ssi_status);
1386 tinfo->ssi_int = tswap32(info->ssi_int);
1387 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
1388 tinfo->ssi_utime = tswap64(info->ssi_utime);
1389 tinfo->ssi_stime = tswap64(info->ssi_stime);
1390 tinfo->ssi_addr = tswap64(info->ssi_addr);
1393 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
1397 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
1398 host_to_target_signalfd_siginfo(buf + i, buf + i);
1404 TargetFdTrans target_signalfd_trans = {
1405 .host_to_target_data = host_to_target_data_signalfd,
1408 static abi_long swap_data_eventfd(void *buf, size_t len)
1410 uint64_t *counter = buf;
1413 if (len < sizeof(uint64_t)) {
1417 for (i = 0; i < len; i += sizeof(uint64_t)) {
1418 *counter = tswap64(*counter);
1425 TargetFdTrans target_eventfd_trans = {
1426 .host_to_target_data = swap_data_eventfd,
1427 .target_to_host_data = swap_data_eventfd,
1430 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
1431 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
1432 defined(__NR_inotify_init1))
1433 static abi_long host_to_target_data_inotify(void *buf, size_t len)
1435 struct inotify_event *ev;
1439 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
1440 ev = (struct inotify_event *)((char *)buf + i);
1443 ev->wd = tswap32(ev->wd);
1444 ev->mask = tswap32(ev->mask);
1445 ev->cookie = tswap32(ev->cookie);
1446 ev->len = tswap32(name_len);
1452 TargetFdTrans target_inotify_trans = {
1453 .host_to_target_data = host_to_target_data_inotify,