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
,
136 QEMU_IFLA_BRPORT_UNSPEC
,
137 QEMU_IFLA_BRPORT_STATE
,
138 QEMU_IFLA_BRPORT_PRIORITY
,
139 QEMU_IFLA_BRPORT_COST
,
140 QEMU_IFLA_BRPORT_MODE
,
141 QEMU_IFLA_BRPORT_GUARD
,
142 QEMU_IFLA_BRPORT_PROTECT
,
143 QEMU_IFLA_BRPORT_FAST_LEAVE
,
144 QEMU_IFLA_BRPORT_LEARNING
,
145 QEMU_IFLA_BRPORT_UNICAST_FLOOD
,
146 QEMU_IFLA_BRPORT_PROXYARP
,
147 QEMU_IFLA_BRPORT_LEARNING_SYNC
,
148 QEMU_IFLA_BRPORT_PROXYARP_WIFI
,
149 QEMU_IFLA_BRPORT_ROOT_ID
,
150 QEMU_IFLA_BRPORT_BRIDGE_ID
,
151 QEMU_IFLA_BRPORT_DESIGNATED_PORT
,
152 QEMU_IFLA_BRPORT_DESIGNATED_COST
,
155 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
,
156 QEMU_IFLA_BRPORT_CONFIG_PENDING
,
157 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
,
158 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
,
159 QEMU_IFLA_BRPORT_HOLD_TIMER
,
160 QEMU_IFLA_BRPORT_FLUSH
,
161 QEMU_IFLA_BRPORT_MULTICAST_ROUTER
,
162 QEMU_IFLA_BRPORT_PAD
,
163 QEMU_IFLA_BRPORT_MCAST_FLOOD
,
164 QEMU_IFLA_BRPORT_MCAST_TO_UCAST
,
165 QEMU_IFLA_BRPORT_VLAN_TUNNEL
,
166 QEMU_IFLA_BRPORT_BCAST_FLOOD
,
167 QEMU_IFLA_BRPORT_GROUP_FWD_MASK
,
168 QEMU_IFLA_BRPORT_NEIGH_SUPPRESS
,
169 QEMU___IFLA_BRPORT_MAX
173 QEMU_IFLA_TUN_UNSPEC
,
178 QEMU_IFLA_TUN_VNET_HDR
,
179 QEMU_IFLA_TUN_PERSIST
,
180 QEMU_IFLA_TUN_MULTI_QUEUE
,
181 QEMU_IFLA_TUN_NUM_QUEUES
,
182 QEMU_IFLA_TUN_NUM_DISABLED_QUEUES
,
187 QEMU_IFLA_INFO_UNSPEC
,
190 QEMU_IFLA_INFO_XSTATS
,
191 QEMU_IFLA_INFO_SLAVE_KIND
,
192 QEMU_IFLA_INFO_SLAVE_DATA
,
193 QEMU___IFLA_INFO_MAX
,
197 QEMU_IFLA_INET_UNSPEC
,
199 QEMU___IFLA_INET_MAX
,
203 QEMU_IFLA_INET6_UNSPEC
,
204 QEMU_IFLA_INET6_FLAGS
,
205 QEMU_IFLA_INET6_CONF
,
206 QEMU_IFLA_INET6_STATS
,
207 QEMU_IFLA_INET6_MCAST
,
208 QEMU_IFLA_INET6_CACHEINFO
,
209 QEMU_IFLA_INET6_ICMP6STATS
,
210 QEMU_IFLA_INET6_TOKEN
,
211 QEMU_IFLA_INET6_ADDR_GEN_MODE
,
212 QEMU___IFLA_INET6_MAX
216 QEMU_IFLA_XDP_UNSPEC
,
218 QEMU_IFLA_XDP_ATTACHED
,
220 QEMU_IFLA_XDP_PROG_ID
,
235 QEMU_RTA_PROTOINFO
, /* no longer used */
238 QEMU_RTA_SESSION
, /* no longer used */
239 QEMU_RTA_MP_ALGO
, /* no longer used */
251 QEMU_RTA_TTL_PROPAGATE
,
258 TargetFdTrans
**target_fd_trans
;
259 unsigned int target_fd_max
;
261 static void tswap_nlmsghdr(struct nlmsghdr
*nlh
)
263 nlh
->nlmsg_len
= tswap32(nlh
->nlmsg_len
);
264 nlh
->nlmsg_type
= tswap16(nlh
->nlmsg_type
);
265 nlh
->nlmsg_flags
= tswap16(nlh
->nlmsg_flags
);
266 nlh
->nlmsg_seq
= tswap32(nlh
->nlmsg_seq
);
267 nlh
->nlmsg_pid
= tswap32(nlh
->nlmsg_pid
);
270 static abi_long
host_to_target_for_each_nlmsg(struct nlmsghdr
*nlh
,
272 abi_long (*host_to_target_nlmsg
)
278 while (len
> sizeof(struct nlmsghdr
)) {
280 nlmsg_len
= nlh
->nlmsg_len
;
281 if (nlmsg_len
< sizeof(struct nlmsghdr
) ||
286 switch (nlh
->nlmsg_type
) {
294 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
295 e
->error
= tswap32(e
->error
);
296 tswap_nlmsghdr(&e
->msg
);
301 ret
= host_to_target_nlmsg(nlh
);
309 len
-= NLMSG_ALIGN(nlmsg_len
);
310 nlh
= (struct nlmsghdr
*)(((char*)nlh
) + NLMSG_ALIGN(nlmsg_len
));
315 static abi_long
target_to_host_for_each_nlmsg(struct nlmsghdr
*nlh
,
317 abi_long (*target_to_host_nlmsg
)
322 while (len
> sizeof(struct nlmsghdr
)) {
323 if (tswap32(nlh
->nlmsg_len
) < sizeof(struct nlmsghdr
) ||
324 tswap32(nlh
->nlmsg_len
) > len
) {
328 switch (nlh
->nlmsg_type
) {
335 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
336 e
->error
= tswap32(e
->error
);
337 tswap_nlmsghdr(&e
->msg
);
341 ret
= target_to_host_nlmsg(nlh
);
346 len
-= NLMSG_ALIGN(nlh
->nlmsg_len
);
347 nlh
= (struct nlmsghdr
*)(((char *)nlh
) + NLMSG_ALIGN(nlh
->nlmsg_len
));
352 #ifdef CONFIG_RTNETLINK
353 static abi_long
host_to_target_for_each_nlattr(struct nlattr
*nlattr
,
354 size_t len
, void *context
,
355 abi_long (*host_to_target_nlattr
)
359 unsigned short nla_len
;
362 while (len
> sizeof(struct nlattr
)) {
363 nla_len
= nlattr
->nla_len
;
364 if (nla_len
< sizeof(struct nlattr
) ||
368 ret
= host_to_target_nlattr(nlattr
, context
);
369 nlattr
->nla_len
= tswap16(nlattr
->nla_len
);
370 nlattr
->nla_type
= tswap16(nlattr
->nla_type
);
374 len
-= NLA_ALIGN(nla_len
);
375 nlattr
= (struct nlattr
*)(((char *)nlattr
) + NLA_ALIGN(nla_len
));
380 static abi_long
host_to_target_for_each_rtattr(struct rtattr
*rtattr
,
382 abi_long (*host_to_target_rtattr
)
385 unsigned short rta_len
;
388 while (len
> sizeof(struct rtattr
)) {
389 rta_len
= rtattr
->rta_len
;
390 if (rta_len
< sizeof(struct rtattr
) ||
394 ret
= host_to_target_rtattr(rtattr
);
395 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
396 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
400 len
-= RTA_ALIGN(rta_len
);
401 rtattr
= (struct rtattr
*)(((char *)rtattr
) + RTA_ALIGN(rta_len
));
406 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
408 static abi_long
host_to_target_data_bridge_nlattr(struct nlattr
*nlattr
,
415 switch (nlattr
->nla_type
) {
417 case QEMU_IFLA_BR_FDB_FLUSH
:
420 case QEMU_IFLA_BR_GROUP_ADDR
:
423 case QEMU_IFLA_BR_VLAN_FILTERING
:
424 case QEMU_IFLA_BR_TOPOLOGY_CHANGE
:
425 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED
:
426 case QEMU_IFLA_BR_MCAST_ROUTER
:
427 case QEMU_IFLA_BR_MCAST_SNOOPING
:
428 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR
:
429 case QEMU_IFLA_BR_MCAST_QUERIER
:
430 case QEMU_IFLA_BR_NF_CALL_IPTABLES
:
431 case QEMU_IFLA_BR_NF_CALL_IP6TABLES
:
432 case QEMU_IFLA_BR_NF_CALL_ARPTABLES
:
433 case QEMU_IFLA_BR_VLAN_STATS_ENABLED
:
434 case QEMU_IFLA_BR_MCAST_STATS_ENABLED
:
435 case QEMU_IFLA_BR_MCAST_IGMP_VERSION
:
436 case QEMU_IFLA_BR_MCAST_MLD_VERSION
:
439 case QEMU_IFLA_BR_PRIORITY
:
440 case QEMU_IFLA_BR_VLAN_PROTOCOL
:
441 case QEMU_IFLA_BR_GROUP_FWD_MASK
:
442 case QEMU_IFLA_BR_ROOT_PORT
:
443 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID
:
444 u16
= NLA_DATA(nlattr
);
445 *u16
= tswap16(*u16
);
448 case QEMU_IFLA_BR_FORWARD_DELAY
:
449 case QEMU_IFLA_BR_HELLO_TIME
:
450 case QEMU_IFLA_BR_MAX_AGE
:
451 case QEMU_IFLA_BR_AGEING_TIME
:
452 case QEMU_IFLA_BR_STP_STATE
:
453 case QEMU_IFLA_BR_ROOT_PATH_COST
:
454 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY
:
455 case QEMU_IFLA_BR_MCAST_HASH_MAX
:
456 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT
:
457 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT
:
458 u32
= NLA_DATA(nlattr
);
459 *u32
= tswap32(*u32
);
462 case QEMU_IFLA_BR_HELLO_TIMER
:
463 case QEMU_IFLA_BR_TCN_TIMER
:
464 case QEMU_IFLA_BR_GC_TIMER
:
465 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER
:
466 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL
:
467 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL
:
468 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL
:
469 case QEMU_IFLA_BR_MCAST_QUERY_INTVL
:
470 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
:
471 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL
:
472 u64
= NLA_DATA(nlattr
);
473 *u64
= tswap64(*u64
);
475 /* ifla_bridge_id: uin8_t[] */
476 case QEMU_IFLA_BR_ROOT_ID
:
477 case QEMU_IFLA_BR_BRIDGE_ID
:
480 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr
->nla_type
);
486 static abi_long
host_to_target_slave_data_bridge_nlattr(struct nlattr
*nlattr
,
493 switch (nlattr
->nla_type
) {
495 case QEMU_IFLA_BRPORT_STATE
:
496 case QEMU_IFLA_BRPORT_MODE
:
497 case QEMU_IFLA_BRPORT_GUARD
:
498 case QEMU_IFLA_BRPORT_PROTECT
:
499 case QEMU_IFLA_BRPORT_FAST_LEAVE
:
500 case QEMU_IFLA_BRPORT_LEARNING
:
501 case QEMU_IFLA_BRPORT_UNICAST_FLOOD
:
502 case QEMU_IFLA_BRPORT_PROXYARP
:
503 case QEMU_IFLA_BRPORT_LEARNING_SYNC
:
504 case QEMU_IFLA_BRPORT_PROXYARP_WIFI
:
505 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
:
506 case QEMU_IFLA_BRPORT_CONFIG_PENDING
:
507 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER
:
508 case QEMU_IFLA_BRPORT_MCAST_FLOOD
:
509 case QEMU_IFLA_BRPORT_MCAST_TO_UCAST
:
510 case QEMU_IFLA_BRPORT_VLAN_TUNNEL
:
511 case QEMU_IFLA_BRPORT_BCAST_FLOOD
:
512 case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS
:
515 case QEMU_IFLA_BRPORT_PRIORITY
:
516 case QEMU_IFLA_BRPORT_DESIGNATED_PORT
:
517 case QEMU_IFLA_BRPORT_DESIGNATED_COST
:
518 case QEMU_IFLA_BRPORT_ID
:
519 case QEMU_IFLA_BRPORT_NO
:
520 case QEMU_IFLA_BRPORT_GROUP_FWD_MASK
:
521 u16
= NLA_DATA(nlattr
);
522 *u16
= tswap16(*u16
);
525 case QEMU_IFLA_BRPORT_COST
:
526 u32
= NLA_DATA(nlattr
);
527 *u32
= tswap32(*u32
);
530 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
:
531 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
:
532 case QEMU_IFLA_BRPORT_HOLD_TIMER
:
533 u64
= NLA_DATA(nlattr
);
534 *u64
= tswap64(*u64
);
536 /* ifla_bridge_id: uint8_t[] */
537 case QEMU_IFLA_BRPORT_ROOT_ID
:
538 case QEMU_IFLA_BRPORT_BRIDGE_ID
:
541 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr
->nla_type
);
547 static abi_long
host_to_target_data_tun_nlattr(struct nlattr
*nlattr
,
552 switch (nlattr
->nla_type
) {
554 case QEMU_IFLA_TUN_TYPE
:
555 case QEMU_IFLA_TUN_PI
:
556 case QEMU_IFLA_TUN_VNET_HDR
:
557 case QEMU_IFLA_TUN_PERSIST
:
558 case QEMU_IFLA_TUN_MULTI_QUEUE
:
561 case QEMU_IFLA_TUN_NUM_QUEUES
:
562 case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES
:
563 case QEMU_IFLA_TUN_OWNER
:
564 case QEMU_IFLA_TUN_GROUP
:
565 u32
= NLA_DATA(nlattr
);
566 *u32
= tswap32(*u32
);
569 gemu_log("Unknown QEMU_IFLA_TUN type %d\n", nlattr
->nla_type
);
575 struct linkinfo_context
{
582 static abi_long
host_to_target_data_linkinfo_nlattr(struct nlattr
*nlattr
,
585 struct linkinfo_context
*li_context
= context
;
587 switch (nlattr
->nla_type
) {
589 case QEMU_IFLA_INFO_KIND
:
590 li_context
->name
= NLA_DATA(nlattr
);
591 li_context
->len
= nlattr
->nla_len
- NLA_HDRLEN
;
593 case QEMU_IFLA_INFO_SLAVE_KIND
:
594 li_context
->slave_name
= NLA_DATA(nlattr
);
595 li_context
->slave_len
= nlattr
->nla_len
- NLA_HDRLEN
;
598 case QEMU_IFLA_INFO_XSTATS
:
599 /* FIXME: only used by CAN */
602 case QEMU_IFLA_INFO_DATA
:
603 if (strncmp(li_context
->name
, "bridge",
604 li_context
->len
) == 0) {
605 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
608 host_to_target_data_bridge_nlattr
);
609 } else if (strncmp(li_context
->name
, "tun",
610 li_context
->len
) == 0) {
611 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
614 host_to_target_data_tun_nlattr
);
616 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context
->name
);
619 case QEMU_IFLA_INFO_SLAVE_DATA
:
620 if (strncmp(li_context
->slave_name
, "bridge",
621 li_context
->slave_len
) == 0) {
622 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
625 host_to_target_slave_data_bridge_nlattr
);
627 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
628 li_context
->slave_name
);
632 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr
->nla_type
);
639 static abi_long
host_to_target_data_inet_nlattr(struct nlattr
*nlattr
,
645 switch (nlattr
->nla_type
) {
646 case QEMU_IFLA_INET_CONF
:
647 u32
= NLA_DATA(nlattr
);
648 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
650 u32
[i
] = tswap32(u32
[i
]);
654 gemu_log("Unknown host AF_INET type: %d\n", nlattr
->nla_type
);
659 static abi_long
host_to_target_data_inet6_nlattr(struct nlattr
*nlattr
,
664 struct ifla_cacheinfo
*ci
;
667 switch (nlattr
->nla_type
) {
669 case QEMU_IFLA_INET6_TOKEN
:
672 case QEMU_IFLA_INET6_ADDR_GEN_MODE
:
675 case QEMU_IFLA_INET6_FLAGS
:
676 u32
= NLA_DATA(nlattr
);
677 *u32
= tswap32(*u32
);
680 case QEMU_IFLA_INET6_CONF
:
681 u32
= NLA_DATA(nlattr
);
682 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
684 u32
[i
] = tswap32(u32
[i
]);
688 case QEMU_IFLA_INET6_CACHEINFO
:
689 ci
= NLA_DATA(nlattr
);
690 ci
->max_reasm_len
= tswap32(ci
->max_reasm_len
);
691 ci
->tstamp
= tswap32(ci
->tstamp
);
692 ci
->reachable_time
= tswap32(ci
->reachable_time
);
693 ci
->retrans_time
= tswap32(ci
->retrans_time
);
696 case QEMU_IFLA_INET6_STATS
:
697 case QEMU_IFLA_INET6_ICMP6STATS
:
698 u64
= NLA_DATA(nlattr
);
699 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u64
);
701 u64
[i
] = tswap64(u64
[i
]);
705 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr
->nla_type
);
710 static abi_long
host_to_target_data_spec_nlattr(struct nlattr
*nlattr
,
713 switch (nlattr
->nla_type
) {
715 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
717 host_to_target_data_inet_nlattr
);
719 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
721 host_to_target_data_inet6_nlattr
);
723 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr
->nla_type
);
729 static abi_long
host_to_target_data_xdp_nlattr(struct nlattr
*nlattr
,
734 switch (nlattr
->nla_type
) {
736 case QEMU_IFLA_XDP_ATTACHED
:
739 case QEMU_IFLA_XDP_PROG_ID
:
740 u32
= NLA_DATA(nlattr
);
741 *u32
= tswap32(*u32
);
744 gemu_log("Unknown host XDP type: %d\n", nlattr
->nla_type
);
750 static abi_long
host_to_target_data_link_rtattr(struct rtattr
*rtattr
)
753 struct rtnl_link_stats
*st
;
754 struct rtnl_link_stats64
*st64
;
755 struct rtnl_link_ifmap
*map
;
756 struct linkinfo_context li_context
;
758 switch (rtattr
->rta_type
) {
760 case QEMU_IFLA_ADDRESS
:
761 case QEMU_IFLA_BROADCAST
:
763 case QEMU_IFLA_IFNAME
:
764 case QEMU_IFLA_QDISC
:
767 case QEMU_IFLA_OPERSTATE
:
768 case QEMU_IFLA_LINKMODE
:
769 case QEMU_IFLA_CARRIER
:
770 case QEMU_IFLA_PROTO_DOWN
:
775 case QEMU_IFLA_WEIGHT
:
776 case QEMU_IFLA_TXQLEN
:
777 case QEMU_IFLA_CARRIER_CHANGES
:
778 case QEMU_IFLA_NUM_RX_QUEUES
:
779 case QEMU_IFLA_NUM_TX_QUEUES
:
780 case QEMU_IFLA_PROMISCUITY
:
781 case QEMU_IFLA_EXT_MASK
:
782 case QEMU_IFLA_LINK_NETNSID
:
783 case QEMU_IFLA_GROUP
:
784 case QEMU_IFLA_MASTER
:
785 case QEMU_IFLA_NUM_VF
:
786 case QEMU_IFLA_GSO_MAX_SEGS
:
787 case QEMU_IFLA_GSO_MAX_SIZE
:
788 case QEMU_IFLA_CARRIER_UP_COUNT
:
789 case QEMU_IFLA_CARRIER_DOWN_COUNT
:
790 u32
= RTA_DATA(rtattr
);
791 *u32
= tswap32(*u32
);
793 /* struct rtnl_link_stats */
794 case QEMU_IFLA_STATS
:
795 st
= RTA_DATA(rtattr
);
796 st
->rx_packets
= tswap32(st
->rx_packets
);
797 st
->tx_packets
= tswap32(st
->tx_packets
);
798 st
->rx_bytes
= tswap32(st
->rx_bytes
);
799 st
->tx_bytes
= tswap32(st
->tx_bytes
);
800 st
->rx_errors
= tswap32(st
->rx_errors
);
801 st
->tx_errors
= tswap32(st
->tx_errors
);
802 st
->rx_dropped
= tswap32(st
->rx_dropped
);
803 st
->tx_dropped
= tswap32(st
->tx_dropped
);
804 st
->multicast
= tswap32(st
->multicast
);
805 st
->collisions
= tswap32(st
->collisions
);
807 /* detailed rx_errors: */
808 st
->rx_length_errors
= tswap32(st
->rx_length_errors
);
809 st
->rx_over_errors
= tswap32(st
->rx_over_errors
);
810 st
->rx_crc_errors
= tswap32(st
->rx_crc_errors
);
811 st
->rx_frame_errors
= tswap32(st
->rx_frame_errors
);
812 st
->rx_fifo_errors
= tswap32(st
->rx_fifo_errors
);
813 st
->rx_missed_errors
= tswap32(st
->rx_missed_errors
);
815 /* detailed tx_errors */
816 st
->tx_aborted_errors
= tswap32(st
->tx_aborted_errors
);
817 st
->tx_carrier_errors
= tswap32(st
->tx_carrier_errors
);
818 st
->tx_fifo_errors
= tswap32(st
->tx_fifo_errors
);
819 st
->tx_heartbeat_errors
= tswap32(st
->tx_heartbeat_errors
);
820 st
->tx_window_errors
= tswap32(st
->tx_window_errors
);
823 st
->rx_compressed
= tswap32(st
->rx_compressed
);
824 st
->tx_compressed
= tswap32(st
->tx_compressed
);
826 /* struct rtnl_link_stats64 */
827 case QEMU_IFLA_STATS64
:
828 st64
= RTA_DATA(rtattr
);
829 st64
->rx_packets
= tswap64(st64
->rx_packets
);
830 st64
->tx_packets
= tswap64(st64
->tx_packets
);
831 st64
->rx_bytes
= tswap64(st64
->rx_bytes
);
832 st64
->tx_bytes
= tswap64(st64
->tx_bytes
);
833 st64
->rx_errors
= tswap64(st64
->rx_errors
);
834 st64
->tx_errors
= tswap64(st64
->tx_errors
);
835 st64
->rx_dropped
= tswap64(st64
->rx_dropped
);
836 st64
->tx_dropped
= tswap64(st64
->tx_dropped
);
837 st64
->multicast
= tswap64(st64
->multicast
);
838 st64
->collisions
= tswap64(st64
->collisions
);
840 /* detailed rx_errors: */
841 st64
->rx_length_errors
= tswap64(st64
->rx_length_errors
);
842 st64
->rx_over_errors
= tswap64(st64
->rx_over_errors
);
843 st64
->rx_crc_errors
= tswap64(st64
->rx_crc_errors
);
844 st64
->rx_frame_errors
= tswap64(st64
->rx_frame_errors
);
845 st64
->rx_fifo_errors
= tswap64(st64
->rx_fifo_errors
);
846 st64
->rx_missed_errors
= tswap64(st64
->rx_missed_errors
);
848 /* detailed tx_errors */
849 st64
->tx_aborted_errors
= tswap64(st64
->tx_aborted_errors
);
850 st64
->tx_carrier_errors
= tswap64(st64
->tx_carrier_errors
);
851 st64
->tx_fifo_errors
= tswap64(st64
->tx_fifo_errors
);
852 st64
->tx_heartbeat_errors
= tswap64(st64
->tx_heartbeat_errors
);
853 st64
->tx_window_errors
= tswap64(st64
->tx_window_errors
);
856 st64
->rx_compressed
= tswap64(st64
->rx_compressed
);
857 st64
->tx_compressed
= tswap64(st64
->tx_compressed
);
859 /* struct rtnl_link_ifmap */
861 map
= RTA_DATA(rtattr
);
862 map
->mem_start
= tswap64(map
->mem_start
);
863 map
->mem_end
= tswap64(map
->mem_end
);
864 map
->base_addr
= tswap64(map
->base_addr
);
865 map
->irq
= tswap16(map
->irq
);
868 case QEMU_IFLA_LINKINFO
:
869 memset(&li_context
, 0, sizeof(li_context
));
870 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
872 host_to_target_data_linkinfo_nlattr
);
873 case QEMU_IFLA_AF_SPEC
:
874 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
876 host_to_target_data_spec_nlattr
);
878 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
880 host_to_target_data_xdp_nlattr
);
882 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr
->rta_type
);
888 static abi_long
host_to_target_data_addr_rtattr(struct rtattr
*rtattr
)
891 struct ifa_cacheinfo
*ci
;
893 switch (rtattr
->rta_type
) {
894 /* binary: depends on family type */
904 u32
= RTA_DATA(rtattr
);
905 *u32
= tswap32(*u32
);
907 /* struct ifa_cacheinfo */
909 ci
= RTA_DATA(rtattr
);
910 ci
->ifa_prefered
= tswap32(ci
->ifa_prefered
);
911 ci
->ifa_valid
= tswap32(ci
->ifa_valid
);
912 ci
->cstamp
= tswap32(ci
->cstamp
);
913 ci
->tstamp
= tswap32(ci
->tstamp
);
916 gemu_log("Unknown host IFA type: %d\n", rtattr
->rta_type
);
922 static abi_long
host_to_target_data_route_rtattr(struct rtattr
*rtattr
)
925 struct rta_cacheinfo
*ci
;
927 switch (rtattr
->rta_type
) {
928 /* binary: depends on family type */
929 case QEMU_RTA_GATEWAY
:
931 case QEMU_RTA_PREFSRC
:
937 case QEMU_RTA_PRIORITY
:
940 u32
= RTA_DATA(rtattr
);
941 *u32
= tswap32(*u32
);
943 /* struct rta_cacheinfo */
944 case QEMU_RTA_CACHEINFO
:
945 ci
= RTA_DATA(rtattr
);
946 ci
->rta_clntref
= tswap32(ci
->rta_clntref
);
947 ci
->rta_lastuse
= tswap32(ci
->rta_lastuse
);
948 ci
->rta_expires
= tswap32(ci
->rta_expires
);
949 ci
->rta_error
= tswap32(ci
->rta_error
);
950 ci
->rta_used
= tswap32(ci
->rta_used
);
951 #if defined(RTNETLINK_HAVE_PEERINFO)
952 ci
->rta_id
= tswap32(ci
->rta_id
);
953 ci
->rta_ts
= tswap32(ci
->rta_ts
);
954 ci
->rta_tsage
= tswap32(ci
->rta_tsage
);
958 gemu_log("Unknown host RTA type: %d\n", rtattr
->rta_type
);
964 static abi_long
host_to_target_link_rtattr(struct rtattr
*rtattr
,
967 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
968 host_to_target_data_link_rtattr
);
971 static abi_long
host_to_target_addr_rtattr(struct rtattr
*rtattr
,
974 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
975 host_to_target_data_addr_rtattr
);
978 static abi_long
host_to_target_route_rtattr(struct rtattr
*rtattr
,
981 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
982 host_to_target_data_route_rtattr
);
985 static abi_long
host_to_target_data_route(struct nlmsghdr
*nlh
)
988 struct ifinfomsg
*ifi
;
989 struct ifaddrmsg
*ifa
;
992 nlmsg_len
= nlh
->nlmsg_len
;
993 switch (nlh
->nlmsg_type
) {
997 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
998 ifi
= NLMSG_DATA(nlh
);
999 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
1000 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
1001 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
1002 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
1003 host_to_target_link_rtattr(IFLA_RTA(ifi
),
1004 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
1010 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
1011 ifa
= NLMSG_DATA(nlh
);
1012 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
1013 host_to_target_addr_rtattr(IFA_RTA(ifa
),
1014 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1020 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
1021 rtm
= NLMSG_DATA(nlh
);
1022 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
1023 host_to_target_route_rtattr(RTM_RTA(rtm
),
1024 nlmsg_len
- NLMSG_LENGTH(sizeof(*rtm
)));
1028 return -TARGET_EINVAL
;
1033 static inline abi_long
host_to_target_nlmsg_route(struct nlmsghdr
*nlh
,
1036 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_route
);
1039 static abi_long
target_to_host_for_each_rtattr(struct rtattr
*rtattr
,
1041 abi_long (*target_to_host_rtattr
)
1046 while (len
>= sizeof(struct rtattr
)) {
1047 if (tswap16(rtattr
->rta_len
) < sizeof(struct rtattr
) ||
1048 tswap16(rtattr
->rta_len
) > len
) {
1051 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
1052 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
1053 ret
= target_to_host_rtattr(rtattr
);
1057 len
-= RTA_ALIGN(rtattr
->rta_len
);
1058 rtattr
= (struct rtattr
*)(((char *)rtattr
) +
1059 RTA_ALIGN(rtattr
->rta_len
));
1064 static abi_long
target_to_host_data_link_rtattr(struct rtattr
*rtattr
)
1066 switch (rtattr
->rta_type
) {
1068 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr
->rta_type
);
1074 static abi_long
target_to_host_data_addr_rtattr(struct rtattr
*rtattr
)
1076 switch (rtattr
->rta_type
) {
1077 /* binary: depends on family type */
1082 gemu_log("Unknown target IFA type: %d\n", rtattr
->rta_type
);
1088 static abi_long
target_to_host_data_route_rtattr(struct rtattr
*rtattr
)
1091 switch (rtattr
->rta_type
) {
1092 /* binary: depends on family type */
1095 case QEMU_RTA_GATEWAY
:
1098 case QEMU_RTA_PRIORITY
:
1100 u32
= RTA_DATA(rtattr
);
1101 *u32
= tswap32(*u32
);
1104 gemu_log("Unknown target RTA type: %d\n", rtattr
->rta_type
);
1110 static void target_to_host_link_rtattr(struct rtattr
*rtattr
,
1111 uint32_t rtattr_len
)
1113 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
1114 target_to_host_data_link_rtattr
);
1117 static void target_to_host_addr_rtattr(struct rtattr
*rtattr
,
1118 uint32_t rtattr_len
)
1120 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
1121 target_to_host_data_addr_rtattr
);
1124 static void target_to_host_route_rtattr(struct rtattr
*rtattr
,
1125 uint32_t rtattr_len
)
1127 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
1128 target_to_host_data_route_rtattr
);
1131 static abi_long
target_to_host_data_route(struct nlmsghdr
*nlh
)
1133 struct ifinfomsg
*ifi
;
1134 struct ifaddrmsg
*ifa
;
1137 switch (nlh
->nlmsg_type
) {
1142 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
1143 ifi
= NLMSG_DATA(nlh
);
1144 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
1145 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
1146 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
1147 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
1148 target_to_host_link_rtattr(IFLA_RTA(ifi
), nlh
->nlmsg_len
-
1149 NLMSG_LENGTH(sizeof(*ifi
)));
1155 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
1156 ifa
= NLMSG_DATA(nlh
);
1157 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
1158 target_to_host_addr_rtattr(IFA_RTA(ifa
), nlh
->nlmsg_len
-
1159 NLMSG_LENGTH(sizeof(*ifa
)));
1166 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
1167 rtm
= NLMSG_DATA(nlh
);
1168 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
1169 target_to_host_route_rtattr(RTM_RTA(rtm
), nlh
->nlmsg_len
-
1170 NLMSG_LENGTH(sizeof(*rtm
)));
1174 return -TARGET_EOPNOTSUPP
;
1179 static abi_long
target_to_host_nlmsg_route(struct nlmsghdr
*nlh
, size_t len
)
1181 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_route
);
1183 #endif /* CONFIG_RTNETLINK */
1185 static abi_long
host_to_target_data_audit(struct nlmsghdr
*nlh
)
1187 switch (nlh
->nlmsg_type
) {
1189 gemu_log("Unknown host audit message type %d\n",
1191 return -TARGET_EINVAL
;
1196 static inline abi_long
host_to_target_nlmsg_audit(struct nlmsghdr
*nlh
,
1199 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_audit
);
1202 static abi_long
target_to_host_data_audit(struct nlmsghdr
*nlh
)
1204 switch (nlh
->nlmsg_type
) {
1206 case AUDIT_FIRST_USER_MSG
... AUDIT_LAST_USER_MSG
:
1207 case AUDIT_FIRST_USER_MSG2
... AUDIT_LAST_USER_MSG2
:
1210 gemu_log("Unknown target audit message type %d\n",
1212 return -TARGET_EINVAL
;
1218 static abi_long
target_to_host_nlmsg_audit(struct nlmsghdr
*nlh
, size_t len
)
1220 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_audit
);
1223 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
1224 abi_ulong target_addr
,
1227 struct sockaddr
*addr
= host_addr
;
1228 struct target_sockaddr
*target_saddr
;
1230 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1231 if (!target_saddr
) {
1232 return -TARGET_EFAULT
;
1235 memcpy(addr
, target_saddr
, len
);
1236 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
1237 /* spkt_protocol is big-endian */
1239 unlock_user(target_saddr
, target_addr
, 0);
1243 TargetFdTrans target_packet_trans
= {
1244 .target_to_host_addr
= packet_target_to_host_sockaddr
,
1247 #ifdef CONFIG_RTNETLINK
1248 static abi_long
netlink_route_target_to_host(void *buf
, size_t len
)
1252 ret
= target_to_host_nlmsg_route(buf
, len
);
1260 static abi_long
netlink_route_host_to_target(void *buf
, size_t len
)
1264 ret
= host_to_target_nlmsg_route(buf
, len
);
1272 TargetFdTrans target_netlink_route_trans
= {
1273 .target_to_host_data
= netlink_route_target_to_host
,
1274 .host_to_target_data
= netlink_route_host_to_target
,
1276 #endif /* CONFIG_RTNETLINK */
1278 static abi_long
netlink_audit_target_to_host(void *buf
, size_t len
)
1282 ret
= target_to_host_nlmsg_audit(buf
, len
);
1290 static abi_long
netlink_audit_host_to_target(void *buf
, size_t len
)
1294 ret
= host_to_target_nlmsg_audit(buf
, len
);
1302 TargetFdTrans target_netlink_audit_trans
= {
1303 .target_to_host_data
= netlink_audit_target_to_host
,
1304 .host_to_target_data
= netlink_audit_host_to_target
,
1307 /* signalfd siginfo conversion */
1310 host_to_target_signalfd_siginfo(struct signalfd_siginfo
*tinfo
,
1311 const struct signalfd_siginfo
*info
)
1313 int sig
= host_to_target_signal(info
->ssi_signo
);
1315 /* linux/signalfd.h defines a ssi_addr_lsb
1316 * not defined in sys/signalfd.h but used by some kernels
1319 #ifdef BUS_MCEERR_AO
1320 if (tinfo
->ssi_signo
== SIGBUS
&&
1321 (tinfo
->ssi_code
== BUS_MCEERR_AR
||
1322 tinfo
->ssi_code
== BUS_MCEERR_AO
)) {
1323 uint16_t *ssi_addr_lsb
= (uint16_t *)(&info
->ssi_addr
+ 1);
1324 uint16_t *tssi_addr_lsb
= (uint16_t *)(&tinfo
->ssi_addr
+ 1);
1325 *tssi_addr_lsb
= tswap16(*ssi_addr_lsb
);
1329 tinfo
->ssi_signo
= tswap32(sig
);
1330 tinfo
->ssi_errno
= tswap32(tinfo
->ssi_errno
);
1331 tinfo
->ssi_code
= tswap32(info
->ssi_code
);
1332 tinfo
->ssi_pid
= tswap32(info
->ssi_pid
);
1333 tinfo
->ssi_uid
= tswap32(info
->ssi_uid
);
1334 tinfo
->ssi_fd
= tswap32(info
->ssi_fd
);
1335 tinfo
->ssi_tid
= tswap32(info
->ssi_tid
);
1336 tinfo
->ssi_band
= tswap32(info
->ssi_band
);
1337 tinfo
->ssi_overrun
= tswap32(info
->ssi_overrun
);
1338 tinfo
->ssi_trapno
= tswap32(info
->ssi_trapno
);
1339 tinfo
->ssi_status
= tswap32(info
->ssi_status
);
1340 tinfo
->ssi_int
= tswap32(info
->ssi_int
);
1341 tinfo
->ssi_ptr
= tswap64(info
->ssi_ptr
);
1342 tinfo
->ssi_utime
= tswap64(info
->ssi_utime
);
1343 tinfo
->ssi_stime
= tswap64(info
->ssi_stime
);
1344 tinfo
->ssi_addr
= tswap64(info
->ssi_addr
);
1347 static abi_long
host_to_target_data_signalfd(void *buf
, size_t len
)
1351 for (i
= 0; i
< len
; i
+= sizeof(struct signalfd_siginfo
)) {
1352 host_to_target_signalfd_siginfo(buf
+ i
, buf
+ i
);
1358 TargetFdTrans target_signalfd_trans
= {
1359 .host_to_target_data
= host_to_target_data_signalfd
,
1362 static abi_long
swap_data_eventfd(void *buf
, size_t len
)
1364 uint64_t *counter
= buf
;
1367 if (len
< sizeof(uint64_t)) {
1371 for (i
= 0; i
< len
; i
+= sizeof(uint64_t)) {
1372 *counter
= tswap64(*counter
);
1379 TargetFdTrans target_eventfd_trans
= {
1380 .host_to_target_data
= swap_data_eventfd
,
1381 .target_to_host_data
= swap_data_eventfd
,
1384 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
1385 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
1386 defined(__NR_inotify_init1))
1387 static abi_long
host_to_target_data_inotify(void *buf
, size_t len
)
1389 struct inotify_event
*ev
;
1393 for (i
= 0; i
< len
; i
+= sizeof(struct inotify_event
) + name_len
) {
1394 ev
= (struct inotify_event
*)((char *)buf
+ i
);
1397 ev
->wd
= tswap32(ev
->wd
);
1398 ev
->mask
= tswap32(ev
->mask
);
1399 ev
->cookie
= tswap32(ev
->cookie
);
1400 ev
->len
= tswap32(name_len
);
1406 TargetFdTrans target_inotify_trans
= {
1407 .host_to_target_data
= host_to_target_data_inotify
,