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,
101 QEMU_IFLA_NET_NS_PID,
104 QEMU_IFLA_VFINFO_LIST,
112 QEMU_IFLA_PROMISCUITY,
113 QEMU_IFLA_NUM_TX_QUEUES,
114 QEMU_IFLA_NUM_RX_QUEUES,
116 QEMU_IFLA_PHYS_PORT_ID,
117 QEMU_IFLA_CARRIER_CHANGES,
118 QEMU_IFLA_PHYS_SWITCH_ID,
119 QEMU_IFLA_LINK_NETNSID,
120 QEMU_IFLA_PHYS_PORT_NAME,
121 QEMU_IFLA_PROTO_DOWN,
122 QEMU_IFLA_GSO_MAX_SEGS,
123 QEMU_IFLA_GSO_MAX_SIZE,
127 QEMU_IFLA_NEW_NETNSID,
128 QEMU_IFLA_IF_NETNSID,
129 QEMU_IFLA_CARRIER_UP_COUNT,
130 QEMU_IFLA_CARRIER_DOWN_COUNT,
131 QEMU_IFLA_NEW_IFINDEX,
138 QEMU_IFLA_BRPORT_UNSPEC,
139 QEMU_IFLA_BRPORT_STATE,
140 QEMU_IFLA_BRPORT_PRIORITY,
141 QEMU_IFLA_BRPORT_COST,
142 QEMU_IFLA_BRPORT_MODE,
143 QEMU_IFLA_BRPORT_GUARD,
144 QEMU_IFLA_BRPORT_PROTECT,
145 QEMU_IFLA_BRPORT_FAST_LEAVE,
146 QEMU_IFLA_BRPORT_LEARNING,
147 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
148 QEMU_IFLA_BRPORT_PROXYARP,
149 QEMU_IFLA_BRPORT_LEARNING_SYNC,
150 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
151 QEMU_IFLA_BRPORT_ROOT_ID,
152 QEMU_IFLA_BRPORT_BRIDGE_ID,
153 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
154 QEMU_IFLA_BRPORT_DESIGNATED_COST,
157 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
158 QEMU_IFLA_BRPORT_CONFIG_PENDING,
159 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
160 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
161 QEMU_IFLA_BRPORT_HOLD_TIMER,
162 QEMU_IFLA_BRPORT_FLUSH,
163 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
164 QEMU_IFLA_BRPORT_PAD,
165 QEMU_IFLA_BRPORT_MCAST_FLOOD,
166 QEMU_IFLA_BRPORT_MCAST_TO_UCAST,
167 QEMU_IFLA_BRPORT_VLAN_TUNNEL,
168 QEMU_IFLA_BRPORT_BCAST_FLOOD,
169 QEMU_IFLA_BRPORT_GROUP_FWD_MASK,
170 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS,
171 QEMU_IFLA_BRPORT_ISOLATED,
172 QEMU_IFLA_BRPORT_BACKUP_PORT,
173 QEMU___IFLA_BRPORT_MAX
177 QEMU_IFLA_TUN_UNSPEC,
182 QEMU_IFLA_TUN_VNET_HDR,
183 QEMU_IFLA_TUN_PERSIST,
184 QEMU_IFLA_TUN_MULTI_QUEUE,
185 QEMU_IFLA_TUN_NUM_QUEUES,
186 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES,
191 QEMU_IFLA_INFO_UNSPEC,
194 QEMU_IFLA_INFO_XSTATS,
195 QEMU_IFLA_INFO_SLAVE_KIND,
196 QEMU_IFLA_INFO_SLAVE_DATA,
197 QEMU___IFLA_INFO_MAX,
201 QEMU_IFLA_INET_UNSPEC,
203 QEMU___IFLA_INET_MAX,
207 QEMU_IFLA_INET6_UNSPEC,
208 QEMU_IFLA_INET6_FLAGS,
209 QEMU_IFLA_INET6_CONF,
210 QEMU_IFLA_INET6_STATS,
211 QEMU_IFLA_INET6_MCAST,
212 QEMU_IFLA_INET6_CACHEINFO,
213 QEMU_IFLA_INET6_ICMP6STATS,
214 QEMU_IFLA_INET6_TOKEN,
215 QEMU_IFLA_INET6_ADDR_GEN_MODE,
216 QEMU___IFLA_INET6_MAX
220 QEMU_IFLA_XDP_UNSPEC,
222 QEMU_IFLA_XDP_ATTACHED,
224 QEMU_IFLA_XDP_PROG_ID,
239 QEMU_RTA_PROTOINFO, /* no longer used */
242 QEMU_RTA_SESSION, /* no longer used */
243 QEMU_RTA_MP_ALGO, /* no longer used */
255 QEMU_RTA_TTL_PROPAGATE,
262 TargetFdTrans **target_fd_trans;
263 unsigned int target_fd_max;
265 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
267 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
268 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
269 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
270 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
271 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
274 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
276 abi_long (*host_to_target_nlmsg)
282 while (len > sizeof(struct nlmsghdr)) {
284 nlmsg_len = nlh->nlmsg_len;
285 if (nlmsg_len < sizeof(struct nlmsghdr) ||
290 switch (nlh->nlmsg_type) {
298 struct nlmsgerr *e = NLMSG_DATA(nlh);
299 e->error = tswap32(e->error);
300 tswap_nlmsghdr(&e->msg);
305 ret = host_to_target_nlmsg(nlh);
313 len -= NLMSG_ALIGN(nlmsg_len);
314 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
319 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
321 abi_long (*target_to_host_nlmsg)
326 while (len > sizeof(struct nlmsghdr)) {
327 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
328 tswap32(nlh->nlmsg_len) > len) {
332 switch (nlh->nlmsg_type) {
339 struct nlmsgerr *e = NLMSG_DATA(nlh);
340 e->error = tswap32(e->error);
341 tswap_nlmsghdr(&e->msg);
345 ret = target_to_host_nlmsg(nlh);
350 len -= NLMSG_ALIGN(nlh->nlmsg_len);
351 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
356 #ifdef CONFIG_RTNETLINK
357 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
358 size_t len, void *context,
359 abi_long (*host_to_target_nlattr)
363 unsigned short nla_len;
366 while (len > sizeof(struct nlattr)) {
367 nla_len = nlattr->nla_len;
368 if (nla_len < sizeof(struct nlattr) ||
372 ret = host_to_target_nlattr(nlattr, context);
373 nlattr->nla_len = tswap16(nlattr->nla_len);
374 nlattr->nla_type = tswap16(nlattr->nla_type);
378 len -= NLA_ALIGN(nla_len);
379 nlattr = (struct nlattr *)(((char *)nlattr) + NLA_ALIGN(nla_len));
384 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
386 abi_long (*host_to_target_rtattr)
389 unsigned short rta_len;
392 while (len > sizeof(struct rtattr)) {
393 rta_len = rtattr->rta_len;
394 if (rta_len < sizeof(struct rtattr) ||
398 ret = host_to_target_rtattr(rtattr);
399 rtattr->rta_len = tswap16(rtattr->rta_len);
400 rtattr->rta_type = tswap16(rtattr->rta_type);
404 len -= RTA_ALIGN(rta_len);
405 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
410 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
412 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
419 switch (nlattr->nla_type) {
421 case QEMU_IFLA_BR_FDB_FLUSH:
424 case QEMU_IFLA_BR_GROUP_ADDR:
427 case QEMU_IFLA_BR_VLAN_FILTERING:
428 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
429 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
430 case QEMU_IFLA_BR_MCAST_ROUTER:
431 case QEMU_IFLA_BR_MCAST_SNOOPING:
432 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
433 case QEMU_IFLA_BR_MCAST_QUERIER:
434 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
435 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
436 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
437 case QEMU_IFLA_BR_VLAN_STATS_ENABLED:
438 case QEMU_IFLA_BR_MCAST_STATS_ENABLED:
439 case QEMU_IFLA_BR_MCAST_IGMP_VERSION:
440 case QEMU_IFLA_BR_MCAST_MLD_VERSION:
443 case QEMU_IFLA_BR_PRIORITY:
444 case QEMU_IFLA_BR_VLAN_PROTOCOL:
445 case QEMU_IFLA_BR_GROUP_FWD_MASK:
446 case QEMU_IFLA_BR_ROOT_PORT:
447 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
448 u16 = NLA_DATA(nlattr);
449 *u16 = tswap16(*u16);
452 case QEMU_IFLA_BR_FORWARD_DELAY:
453 case QEMU_IFLA_BR_HELLO_TIME:
454 case QEMU_IFLA_BR_MAX_AGE:
455 case QEMU_IFLA_BR_AGEING_TIME:
456 case QEMU_IFLA_BR_STP_STATE:
457 case QEMU_IFLA_BR_ROOT_PATH_COST:
458 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
459 case QEMU_IFLA_BR_MCAST_HASH_MAX:
460 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
461 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
462 u32 = NLA_DATA(nlattr);
463 *u32 = tswap32(*u32);
466 case QEMU_IFLA_BR_HELLO_TIMER:
467 case QEMU_IFLA_BR_TCN_TIMER:
468 case QEMU_IFLA_BR_GC_TIMER:
469 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
470 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
471 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
472 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
473 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
474 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
475 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
476 u64 = NLA_DATA(nlattr);
477 *u64 = tswap64(*u64);
479 /* ifla_bridge_id: uin8_t[] */
480 case QEMU_IFLA_BR_ROOT_ID:
481 case QEMU_IFLA_BR_BRIDGE_ID:
484 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr->nla_type);
490 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
497 switch (nlattr->nla_type) {
499 case QEMU_IFLA_BRPORT_STATE:
500 case QEMU_IFLA_BRPORT_MODE:
501 case QEMU_IFLA_BRPORT_GUARD:
502 case QEMU_IFLA_BRPORT_PROTECT:
503 case QEMU_IFLA_BRPORT_FAST_LEAVE:
504 case QEMU_IFLA_BRPORT_LEARNING:
505 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
506 case QEMU_IFLA_BRPORT_PROXYARP:
507 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
508 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
509 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
510 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
511 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
512 case QEMU_IFLA_BRPORT_MCAST_FLOOD:
513 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST:
514 case QEMU_IFLA_BRPORT_VLAN_TUNNEL:
515 case QEMU_IFLA_BRPORT_BCAST_FLOOD:
516 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS:
517 case QEMU_IFLA_BRPORT_ISOLATED:
520 case QEMU_IFLA_BRPORT_PRIORITY:
521 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
522 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
523 case QEMU_IFLA_BRPORT_ID:
524 case QEMU_IFLA_BRPORT_NO:
525 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK:
526 u16 = NLA_DATA(nlattr);
527 *u16 = tswap16(*u16);
530 case QEMU_IFLA_BRPORT_COST:
531 case QEMU_IFLA_BRPORT_BACKUP_PORT:
532 u32 = NLA_DATA(nlattr);
533 *u32 = tswap32(*u32);
536 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
537 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
538 case QEMU_IFLA_BRPORT_HOLD_TIMER:
539 u64 = NLA_DATA(nlattr);
540 *u64 = tswap64(*u64);
542 /* ifla_bridge_id: uint8_t[] */
543 case QEMU_IFLA_BRPORT_ROOT_ID:
544 case QEMU_IFLA_BRPORT_BRIDGE_ID:
547 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr->nla_type);
553 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr,
558 switch (nlattr->nla_type) {
560 case QEMU_IFLA_TUN_TYPE:
561 case QEMU_IFLA_TUN_PI:
562 case QEMU_IFLA_TUN_VNET_HDR:
563 case QEMU_IFLA_TUN_PERSIST:
564 case QEMU_IFLA_TUN_MULTI_QUEUE:
567 case QEMU_IFLA_TUN_NUM_QUEUES:
568 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES:
569 case QEMU_IFLA_TUN_OWNER:
570 case QEMU_IFLA_TUN_GROUP:
571 u32 = NLA_DATA(nlattr);
572 *u32 = tswap32(*u32);
575 gemu_log("Unknown QEMU_IFLA_TUN type %d\n", nlattr->nla_type);
581 struct linkinfo_context {
588 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
591 struct linkinfo_context *li_context = context;
593 switch (nlattr->nla_type) {
595 case QEMU_IFLA_INFO_KIND:
596 li_context->name = NLA_DATA(nlattr);
597 li_context->len = nlattr->nla_len - NLA_HDRLEN;
599 case QEMU_IFLA_INFO_SLAVE_KIND:
600 li_context->slave_name = NLA_DATA(nlattr);
601 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
604 case QEMU_IFLA_INFO_XSTATS:
605 /* FIXME: only used by CAN */
608 case QEMU_IFLA_INFO_DATA:
609 if (strncmp(li_context->name, "bridge",
610 li_context->len) == 0) {
611 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
614 host_to_target_data_bridge_nlattr);
615 } else if (strncmp(li_context->name, "tun",
616 li_context->len) == 0) {
617 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
620 host_to_target_data_tun_nlattr);
622 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context->name);
625 case QEMU_IFLA_INFO_SLAVE_DATA:
626 if (strncmp(li_context->slave_name, "bridge",
627 li_context->slave_len) == 0) {
628 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
631 host_to_target_slave_data_bridge_nlattr);
633 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
634 li_context->slave_name);
638 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr->nla_type);
645 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
651 switch (nlattr->nla_type) {
652 case QEMU_IFLA_INET_CONF:
653 u32 = NLA_DATA(nlattr);
654 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
656 u32[i] = tswap32(u32[i]);
660 gemu_log("Unknown host AF_INET type: %d\n", nlattr->nla_type);
665 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
670 struct ifla_cacheinfo *ci;
673 switch (nlattr->nla_type) {
675 case QEMU_IFLA_INET6_TOKEN:
678 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
681 case QEMU_IFLA_INET6_FLAGS:
682 u32 = NLA_DATA(nlattr);
683 *u32 = tswap32(*u32);
686 case QEMU_IFLA_INET6_CONF:
687 u32 = NLA_DATA(nlattr);
688 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
690 u32[i] = tswap32(u32[i]);
694 case QEMU_IFLA_INET6_CACHEINFO:
695 ci = NLA_DATA(nlattr);
696 ci->max_reasm_len = tswap32(ci->max_reasm_len);
697 ci->tstamp = tswap32(ci->tstamp);
698 ci->reachable_time = tswap32(ci->reachable_time);
699 ci->retrans_time = tswap32(ci->retrans_time);
702 case QEMU_IFLA_INET6_STATS:
703 case QEMU_IFLA_INET6_ICMP6STATS:
704 u64 = NLA_DATA(nlattr);
705 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
707 u64[i] = tswap64(u64[i]);
711 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr->nla_type);
716 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
719 switch (nlattr->nla_type) {
721 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
723 host_to_target_data_inet_nlattr);
725 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
727 host_to_target_data_inet6_nlattr);
729 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr->nla_type);
735 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr,
740 switch (nlattr->nla_type) {
742 case QEMU_IFLA_XDP_ATTACHED:
745 case QEMU_IFLA_XDP_PROG_ID:
746 u32 = NLA_DATA(nlattr);
747 *u32 = tswap32(*u32);
750 gemu_log("Unknown host XDP type: %d\n", nlattr->nla_type);
756 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
759 struct rtnl_link_stats *st;
760 struct rtnl_link_stats64 *st64;
761 struct rtnl_link_ifmap *map;
762 struct linkinfo_context li_context;
764 switch (rtattr->rta_type) {
766 case QEMU_IFLA_ADDRESS:
767 case QEMU_IFLA_BROADCAST:
769 case QEMU_IFLA_IFNAME:
770 case QEMU_IFLA_QDISC:
773 case QEMU_IFLA_OPERSTATE:
774 case QEMU_IFLA_LINKMODE:
775 case QEMU_IFLA_CARRIER:
776 case QEMU_IFLA_PROTO_DOWN:
781 case QEMU_IFLA_WEIGHT:
782 case QEMU_IFLA_TXQLEN:
783 case QEMU_IFLA_CARRIER_CHANGES:
784 case QEMU_IFLA_NUM_RX_QUEUES:
785 case QEMU_IFLA_NUM_TX_QUEUES:
786 case QEMU_IFLA_PROMISCUITY:
787 case QEMU_IFLA_EXT_MASK:
788 case QEMU_IFLA_LINK_NETNSID:
789 case QEMU_IFLA_GROUP:
790 case QEMU_IFLA_MASTER:
791 case QEMU_IFLA_NUM_VF:
792 case QEMU_IFLA_GSO_MAX_SEGS:
793 case QEMU_IFLA_GSO_MAX_SIZE:
794 case QEMU_IFLA_CARRIER_UP_COUNT:
795 case QEMU_IFLA_CARRIER_DOWN_COUNT:
796 case QEMU_IFLA_MIN_MTU:
797 case QEMU_IFLA_MAX_MTU:
798 u32 = RTA_DATA(rtattr);
799 *u32 = tswap32(*u32);
801 /* struct rtnl_link_stats */
802 case QEMU_IFLA_STATS:
803 st = RTA_DATA(rtattr);
804 st->rx_packets = tswap32(st->rx_packets);
805 st->tx_packets = tswap32(st->tx_packets);
806 st->rx_bytes = tswap32(st->rx_bytes);
807 st->tx_bytes = tswap32(st->tx_bytes);
808 st->rx_errors = tswap32(st->rx_errors);
809 st->tx_errors = tswap32(st->tx_errors);
810 st->rx_dropped = tswap32(st->rx_dropped);
811 st->tx_dropped = tswap32(st->tx_dropped);
812 st->multicast = tswap32(st->multicast);
813 st->collisions = tswap32(st->collisions);
815 /* detailed rx_errors: */
816 st->rx_length_errors = tswap32(st->rx_length_errors);
817 st->rx_over_errors = tswap32(st->rx_over_errors);
818 st->rx_crc_errors = tswap32(st->rx_crc_errors);
819 st->rx_frame_errors = tswap32(st->rx_frame_errors);
820 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
821 st->rx_missed_errors = tswap32(st->rx_missed_errors);
823 /* detailed tx_errors */
824 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
825 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
826 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
827 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
828 st->tx_window_errors = tswap32(st->tx_window_errors);
831 st->rx_compressed = tswap32(st->rx_compressed);
832 st->tx_compressed = tswap32(st->tx_compressed);
834 /* struct rtnl_link_stats64 */
835 case QEMU_IFLA_STATS64:
836 st64 = RTA_DATA(rtattr);
837 st64->rx_packets = tswap64(st64->rx_packets);
838 st64->tx_packets = tswap64(st64->tx_packets);
839 st64->rx_bytes = tswap64(st64->rx_bytes);
840 st64->tx_bytes = tswap64(st64->tx_bytes);
841 st64->rx_errors = tswap64(st64->rx_errors);
842 st64->tx_errors = tswap64(st64->tx_errors);
843 st64->rx_dropped = tswap64(st64->rx_dropped);
844 st64->tx_dropped = tswap64(st64->tx_dropped);
845 st64->multicast = tswap64(st64->multicast);
846 st64->collisions = tswap64(st64->collisions);
848 /* detailed rx_errors: */
849 st64->rx_length_errors = tswap64(st64->rx_length_errors);
850 st64->rx_over_errors = tswap64(st64->rx_over_errors);
851 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
852 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
853 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
854 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
856 /* detailed tx_errors */
857 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
858 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
859 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
860 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
861 st64->tx_window_errors = tswap64(st64->tx_window_errors);
864 st64->rx_compressed = tswap64(st64->rx_compressed);
865 st64->tx_compressed = tswap64(st64->tx_compressed);
867 /* struct rtnl_link_ifmap */
869 map = RTA_DATA(rtattr);
870 map->mem_start = tswap64(map->mem_start);
871 map->mem_end = tswap64(map->mem_end);
872 map->base_addr = tswap64(map->base_addr);
873 map->irq = tswap16(map->irq);
876 case QEMU_IFLA_LINKINFO:
877 memset(&li_context, 0, sizeof(li_context));
878 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
880 host_to_target_data_linkinfo_nlattr);
881 case QEMU_IFLA_AF_SPEC:
882 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
884 host_to_target_data_spec_nlattr);
886 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
888 host_to_target_data_xdp_nlattr);
890 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr->rta_type);
896 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
899 struct ifa_cacheinfo *ci;
901 switch (rtattr->rta_type) {
902 /* binary: depends on family type */
912 u32 = RTA_DATA(rtattr);
913 *u32 = tswap32(*u32);
915 /* struct ifa_cacheinfo */
917 ci = RTA_DATA(rtattr);
918 ci->ifa_prefered = tswap32(ci->ifa_prefered);
919 ci->ifa_valid = tswap32(ci->ifa_valid);
920 ci->cstamp = tswap32(ci->cstamp);
921 ci->tstamp = tswap32(ci->tstamp);
924 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
930 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
933 struct rta_cacheinfo *ci;
935 switch (rtattr->rta_type) {
936 /* binary: depends on family type */
937 case QEMU_RTA_GATEWAY:
939 case QEMU_RTA_PREFSRC:
945 case QEMU_RTA_PRIORITY:
948 u32 = RTA_DATA(rtattr);
949 *u32 = tswap32(*u32);
951 /* struct rta_cacheinfo */
952 case QEMU_RTA_CACHEINFO:
953 ci = RTA_DATA(rtattr);
954 ci->rta_clntref = tswap32(ci->rta_clntref);
955 ci->rta_lastuse = tswap32(ci->rta_lastuse);
956 ci->rta_expires = tswap32(ci->rta_expires);
957 ci->rta_error = tswap32(ci->rta_error);
958 ci->rta_used = tswap32(ci->rta_used);
959 #if defined(RTNETLINK_HAVE_PEERINFO)
960 ci->rta_id = tswap32(ci->rta_id);
961 ci->rta_ts = tswap32(ci->rta_ts);
962 ci->rta_tsage = tswap32(ci->rta_tsage);
966 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
972 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
975 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
976 host_to_target_data_link_rtattr);
979 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
982 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
983 host_to_target_data_addr_rtattr);
986 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
989 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
990 host_to_target_data_route_rtattr);
993 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
996 struct ifinfomsg *ifi;
997 struct ifaddrmsg *ifa;
1000 nlmsg_len = nlh->nlmsg_len;
1001 switch (nlh->nlmsg_type) {
1005 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1006 ifi = NLMSG_DATA(nlh);
1007 ifi->ifi_type = tswap16(ifi->ifi_type);
1008 ifi->ifi_index = tswap32(ifi->ifi_index);
1009 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1010 ifi->ifi_change = tswap32(ifi->ifi_change);
1011 host_to_target_link_rtattr(IFLA_RTA(ifi),
1012 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1018 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1019 ifa = NLMSG_DATA(nlh);
1020 ifa->ifa_index = tswap32(ifa->ifa_index);
1021 host_to_target_addr_rtattr(IFA_RTA(ifa),
1022 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1028 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1029 rtm = NLMSG_DATA(nlh);
1030 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1031 host_to_target_route_rtattr(RTM_RTA(rtm),
1032 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1036 return -TARGET_EINVAL;
1041 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1044 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1047 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1049 abi_long (*target_to_host_rtattr)
1054 while (len >= sizeof(struct rtattr)) {
1055 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1056 tswap16(rtattr->rta_len) > len) {
1059 rtattr->rta_len = tswap16(rtattr->rta_len);
1060 rtattr->rta_type = tswap16(rtattr->rta_type);
1061 ret = target_to_host_rtattr(rtattr);
1065 len -= RTA_ALIGN(rtattr->rta_len);
1066 rtattr = (struct rtattr *)(((char *)rtattr) +
1067 RTA_ALIGN(rtattr->rta_len));
1072 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1074 switch (rtattr->rta_type) {
1076 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr->rta_type);
1082 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1084 switch (rtattr->rta_type) {
1085 /* binary: depends on family type */
1090 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
1096 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1099 switch (rtattr->rta_type) {
1100 /* binary: depends on family type */
1103 case QEMU_RTA_GATEWAY:
1106 case QEMU_RTA_PRIORITY:
1108 u32 = RTA_DATA(rtattr);
1109 *u32 = tswap32(*u32);
1112 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
1118 static void target_to_host_link_rtattr(struct rtattr *rtattr,
1119 uint32_t rtattr_len)
1121 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1122 target_to_host_data_link_rtattr);
1125 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1126 uint32_t rtattr_len)
1128 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1129 target_to_host_data_addr_rtattr);
1132 static void target_to_host_route_rtattr(struct rtattr *rtattr,
1133 uint32_t rtattr_len)
1135 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1136 target_to_host_data_route_rtattr);
1139 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
1141 struct ifinfomsg *ifi;
1142 struct ifaddrmsg *ifa;
1145 switch (nlh->nlmsg_type) {
1150 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1151 ifi = NLMSG_DATA(nlh);
1152 ifi->ifi_type = tswap16(ifi->ifi_type);
1153 ifi->ifi_index = tswap32(ifi->ifi_index);
1154 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1155 ifi->ifi_change = tswap32(ifi->ifi_change);
1156 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
1157 NLMSG_LENGTH(sizeof(*ifi)));
1163 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1164 ifa = NLMSG_DATA(nlh);
1165 ifa->ifa_index = tswap32(ifa->ifa_index);
1166 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
1167 NLMSG_LENGTH(sizeof(*ifa)));
1174 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1175 rtm = NLMSG_DATA(nlh);
1176 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1177 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
1178 NLMSG_LENGTH(sizeof(*rtm)));
1182 return -TARGET_EOPNOTSUPP;
1187 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
1189 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
1191 #endif /* CONFIG_RTNETLINK */
1193 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
1195 switch (nlh->nlmsg_type) {
1197 gemu_log("Unknown host audit message type %d\n",
1199 return -TARGET_EINVAL;
1204 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
1207 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
1210 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
1212 switch (nlh->nlmsg_type) {
1214 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1215 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
1218 gemu_log("Unknown target audit message type %d\n",
1220 return -TARGET_EINVAL;
1226 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
1228 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
1231 static abi_long packet_target_to_host_sockaddr(void *host_addr,
1232 abi_ulong target_addr,
1235 struct sockaddr *addr = host_addr;
1236 struct target_sockaddr *target_saddr;
1238 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1239 if (!target_saddr) {
1240 return -TARGET_EFAULT;
1243 memcpy(addr, target_saddr, len);
1244 addr->sa_family = tswap16(target_saddr->sa_family);
1245 /* spkt_protocol is big-endian */
1247 unlock_user(target_saddr, target_addr, 0);
1251 TargetFdTrans target_packet_trans = {
1252 .target_to_host_addr = packet_target_to_host_sockaddr,
1255 #ifdef CONFIG_RTNETLINK
1256 static abi_long netlink_route_target_to_host(void *buf, size_t len)
1260 ret = target_to_host_nlmsg_route(buf, len);
1268 static abi_long netlink_route_host_to_target(void *buf, size_t len)
1272 ret = host_to_target_nlmsg_route(buf, len);
1280 TargetFdTrans target_netlink_route_trans = {
1281 .target_to_host_data = netlink_route_target_to_host,
1282 .host_to_target_data = netlink_route_host_to_target,
1284 #endif /* CONFIG_RTNETLINK */
1286 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
1290 ret = target_to_host_nlmsg_audit(buf, len);
1298 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
1302 ret = host_to_target_nlmsg_audit(buf, len);
1310 TargetFdTrans target_netlink_audit_trans = {
1311 .target_to_host_data = netlink_audit_target_to_host,
1312 .host_to_target_data = netlink_audit_host_to_target,
1315 /* signalfd siginfo conversion */
1318 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
1319 const struct signalfd_siginfo *info)
1321 int sig = host_to_target_signal(info->ssi_signo);
1323 /* linux/signalfd.h defines a ssi_addr_lsb
1324 * not defined in sys/signalfd.h but used by some kernels
1327 #ifdef BUS_MCEERR_AO
1328 if (tinfo->ssi_signo == SIGBUS &&
1329 (tinfo->ssi_code == BUS_MCEERR_AR ||
1330 tinfo->ssi_code == BUS_MCEERR_AO)) {
1331 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
1332 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
1333 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
1337 tinfo->ssi_signo = tswap32(sig);
1338 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
1339 tinfo->ssi_code = tswap32(info->ssi_code);
1340 tinfo->ssi_pid = tswap32(info->ssi_pid);
1341 tinfo->ssi_uid = tswap32(info->ssi_uid);
1342 tinfo->ssi_fd = tswap32(info->ssi_fd);
1343 tinfo->ssi_tid = tswap32(info->ssi_tid);
1344 tinfo->ssi_band = tswap32(info->ssi_band);
1345 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
1346 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
1347 tinfo->ssi_status = tswap32(info->ssi_status);
1348 tinfo->ssi_int = tswap32(info->ssi_int);
1349 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
1350 tinfo->ssi_utime = tswap64(info->ssi_utime);
1351 tinfo->ssi_stime = tswap64(info->ssi_stime);
1352 tinfo->ssi_addr = tswap64(info->ssi_addr);
1355 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
1359 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
1360 host_to_target_signalfd_siginfo(buf + i, buf + i);
1366 TargetFdTrans target_signalfd_trans = {
1367 .host_to_target_data = host_to_target_data_signalfd,
1370 static abi_long swap_data_eventfd(void *buf, size_t len)
1372 uint64_t *counter = buf;
1375 if (len < sizeof(uint64_t)) {
1379 for (i = 0; i < len; i += sizeof(uint64_t)) {
1380 *counter = tswap64(*counter);
1387 TargetFdTrans target_eventfd_trans = {
1388 .host_to_target_data = swap_data_eventfd,
1389 .target_to_host_data = swap_data_eventfd,
1392 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
1393 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
1394 defined(__NR_inotify_init1))
1395 static abi_long host_to_target_data_inotify(void *buf, size_t len)
1397 struct inotify_event *ev;
1401 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
1402 ev = (struct inotify_event *)((char *)buf + i);
1405 ev->wd = tswap32(ev->wd);
1406 ev->mask = tswap32(ev->mask);
1407 ev->cookie = tswap32(ev->cookie);
1408 ev->len = tswap32(name_len);
1414 TargetFdTrans target_inotify_trans = {
1415 .host_to_target_data = host_to_target_data_inotify,