xref: /openbmc/qemu/linux-user/fd-trans.c (revision 6c3a9247)
1 /*
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.
6  *
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.
11  *
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/>.
14  */
15 
16 #include "qemu/osdep.h"
17 
18 #include <sys/signalfd.h>
19 #include <linux/unistd.h>
20 #include <linux/audit.h>
21 #ifdef CONFIG_INOTIFY
22 #include <sys/inotify.h>
23 #endif
24 #include <linux/netlink.h>
25 #ifdef CONFIG_RTNETLINK
26 #include <linux/rtnetlink.h>
27 #include <linux/if_bridge.h>
28 #endif
29 #include "qemu.h"
30 #include "user-internals.h"
31 #include "fd-trans.h"
32 #include "signal-common.h"
33 
34 enum {
35     QEMU_IFLA_BR_UNSPEC,
36     QEMU_IFLA_BR_FORWARD_DELAY,
37     QEMU_IFLA_BR_HELLO_TIME,
38     QEMU_IFLA_BR_MAX_AGE,
39     QEMU_IFLA_BR_AGEING_TIME,
40     QEMU_IFLA_BR_STP_STATE,
41     QEMU_IFLA_BR_PRIORITY,
42     QEMU_IFLA_BR_VLAN_FILTERING,
43     QEMU_IFLA_BR_VLAN_PROTOCOL,
44     QEMU_IFLA_BR_GROUP_FWD_MASK,
45     QEMU_IFLA_BR_ROOT_ID,
46     QEMU_IFLA_BR_BRIDGE_ID,
47     QEMU_IFLA_BR_ROOT_PORT,
48     QEMU_IFLA_BR_ROOT_PATH_COST,
49     QEMU_IFLA_BR_TOPOLOGY_CHANGE,
50     QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
51     QEMU_IFLA_BR_HELLO_TIMER,
52     QEMU_IFLA_BR_TCN_TIMER,
53     QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
54     QEMU_IFLA_BR_GC_TIMER,
55     QEMU_IFLA_BR_GROUP_ADDR,
56     QEMU_IFLA_BR_FDB_FLUSH,
57     QEMU_IFLA_BR_MCAST_ROUTER,
58     QEMU_IFLA_BR_MCAST_SNOOPING,
59     QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
60     QEMU_IFLA_BR_MCAST_QUERIER,
61     QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
62     QEMU_IFLA_BR_MCAST_HASH_MAX,
63     QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
64     QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
65     QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
66     QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
67     QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
68     QEMU_IFLA_BR_MCAST_QUERY_INTVL,
69     QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
70     QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
71     QEMU_IFLA_BR_NF_CALL_IPTABLES,
72     QEMU_IFLA_BR_NF_CALL_IP6TABLES,
73     QEMU_IFLA_BR_NF_CALL_ARPTABLES,
74     QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
75     QEMU_IFLA_BR_PAD,
76     QEMU_IFLA_BR_VLAN_STATS_ENABLED,
77     QEMU_IFLA_BR_MCAST_STATS_ENABLED,
78     QEMU_IFLA_BR_MCAST_IGMP_VERSION,
79     QEMU_IFLA_BR_MCAST_MLD_VERSION,
80     QEMU_IFLA_BR_VLAN_STATS_PER_PORT,
81     QEMU_IFLA_BR_MULTI_BOOLOPT,
82     QEMU___IFLA_BR_MAX,
83 };
84 
85 enum {
86     QEMU_IFLA_UNSPEC,
87     QEMU_IFLA_ADDRESS,
88     QEMU_IFLA_BROADCAST,
89     QEMU_IFLA_IFNAME,
90     QEMU_IFLA_MTU,
91     QEMU_IFLA_LINK,
92     QEMU_IFLA_QDISC,
93     QEMU_IFLA_STATS,
94     QEMU_IFLA_COST,
95     QEMU_IFLA_PRIORITY,
96     QEMU_IFLA_MASTER,
97     QEMU_IFLA_WIRELESS,
98     QEMU_IFLA_PROTINFO,
99     QEMU_IFLA_TXQLEN,
100     QEMU_IFLA_MAP,
101     QEMU_IFLA_WEIGHT,
102     QEMU_IFLA_OPERSTATE,
103     QEMU_IFLA_LINKMODE,
104     QEMU_IFLA_LINKINFO,
105     QEMU_IFLA_NET_NS_PID,
106     QEMU_IFLA_IFALIAS,
107     QEMU_IFLA_NUM_VF,
108     QEMU_IFLA_VFINFO_LIST,
109     QEMU_IFLA_STATS64,
110     QEMU_IFLA_VF_PORTS,
111     QEMU_IFLA_PORT_SELF,
112     QEMU_IFLA_AF_SPEC,
113     QEMU_IFLA_GROUP,
114     QEMU_IFLA_NET_NS_FD,
115     QEMU_IFLA_EXT_MASK,
116     QEMU_IFLA_PROMISCUITY,
117     QEMU_IFLA_NUM_TX_QUEUES,
118     QEMU_IFLA_NUM_RX_QUEUES,
119     QEMU_IFLA_CARRIER,
120     QEMU_IFLA_PHYS_PORT_ID,
121     QEMU_IFLA_CARRIER_CHANGES,
122     QEMU_IFLA_PHYS_SWITCH_ID,
123     QEMU_IFLA_LINK_NETNSID,
124     QEMU_IFLA_PHYS_PORT_NAME,
125     QEMU_IFLA_PROTO_DOWN,
126     QEMU_IFLA_GSO_MAX_SEGS,
127     QEMU_IFLA_GSO_MAX_SIZE,
128     QEMU_IFLA_PAD,
129     QEMU_IFLA_XDP,
130     QEMU_IFLA_EVENT,
131     QEMU_IFLA_NEW_NETNSID,
132     QEMU_IFLA_IF_NETNSID,
133     QEMU_IFLA_CARRIER_UP_COUNT,
134     QEMU_IFLA_CARRIER_DOWN_COUNT,
135     QEMU_IFLA_NEW_IFINDEX,
136     QEMU_IFLA_MIN_MTU,
137     QEMU_IFLA_MAX_MTU,
138     QEMU_IFLA_PROP_LIST,
139     QEMU_IFLA_ALT_IFNAME,
140     QEMU_IFLA_PERM_ADDRESS,
141     QEMU_IFLA_PROTO_DOWN_REASON,
142     QEMU_IFLA_PARENT_DEV_NAME,
143     QEMU_IFLA_PARENT_DEV_BUS_NAME,
144     QEMU___IFLA_MAX
145 };
146 
147 enum {
148     QEMU_IFLA_BRPORT_UNSPEC,
149     QEMU_IFLA_BRPORT_STATE,
150     QEMU_IFLA_BRPORT_PRIORITY,
151     QEMU_IFLA_BRPORT_COST,
152     QEMU_IFLA_BRPORT_MODE,
153     QEMU_IFLA_BRPORT_GUARD,
154     QEMU_IFLA_BRPORT_PROTECT,
155     QEMU_IFLA_BRPORT_FAST_LEAVE,
156     QEMU_IFLA_BRPORT_LEARNING,
157     QEMU_IFLA_BRPORT_UNICAST_FLOOD,
158     QEMU_IFLA_BRPORT_PROXYARP,
159     QEMU_IFLA_BRPORT_LEARNING_SYNC,
160     QEMU_IFLA_BRPORT_PROXYARP_WIFI,
161     QEMU_IFLA_BRPORT_ROOT_ID,
162     QEMU_IFLA_BRPORT_BRIDGE_ID,
163     QEMU_IFLA_BRPORT_DESIGNATED_PORT,
164     QEMU_IFLA_BRPORT_DESIGNATED_COST,
165     QEMU_IFLA_BRPORT_ID,
166     QEMU_IFLA_BRPORT_NO,
167     QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
168     QEMU_IFLA_BRPORT_CONFIG_PENDING,
169     QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
170     QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
171     QEMU_IFLA_BRPORT_HOLD_TIMER,
172     QEMU_IFLA_BRPORT_FLUSH,
173     QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
174     QEMU_IFLA_BRPORT_PAD,
175     QEMU_IFLA_BRPORT_MCAST_FLOOD,
176     QEMU_IFLA_BRPORT_MCAST_TO_UCAST,
177     QEMU_IFLA_BRPORT_VLAN_TUNNEL,
178     QEMU_IFLA_BRPORT_BCAST_FLOOD,
179     QEMU_IFLA_BRPORT_GROUP_FWD_MASK,
180     QEMU_IFLA_BRPORT_NEIGH_SUPPRESS,
181     QEMU_IFLA_BRPORT_ISOLATED,
182     QEMU_IFLA_BRPORT_BACKUP_PORT,
183     QEMU_IFLA_BRPORT_MRP_RING_OPEN,
184     QEMU_IFLA_BRPORT_MRP_IN_OPEN,
185     QEMU_IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT,
186     QEMU_IFLA_BRPORT_MCAST_EHT_HOSTS_CNT,
187     QEMU___IFLA_BRPORT_MAX
188 };
189 
190 enum {
191     QEMU_IFLA_TUN_UNSPEC,
192     QEMU_IFLA_TUN_OWNER,
193     QEMU_IFLA_TUN_GROUP,
194     QEMU_IFLA_TUN_TYPE,
195     QEMU_IFLA_TUN_PI,
196     QEMU_IFLA_TUN_VNET_HDR,
197     QEMU_IFLA_TUN_PERSIST,
198     QEMU_IFLA_TUN_MULTI_QUEUE,
199     QEMU_IFLA_TUN_NUM_QUEUES,
200     QEMU_IFLA_TUN_NUM_DISABLED_QUEUES,
201     QEMU___IFLA_TUN_MAX,
202 };
203 
204 enum {
205     QEMU_IFLA_INFO_UNSPEC,
206     QEMU_IFLA_INFO_KIND,
207     QEMU_IFLA_INFO_DATA,
208     QEMU_IFLA_INFO_XSTATS,
209     QEMU_IFLA_INFO_SLAVE_KIND,
210     QEMU_IFLA_INFO_SLAVE_DATA,
211     QEMU___IFLA_INFO_MAX,
212 };
213 
214 enum {
215     QEMU_IFLA_INET_UNSPEC,
216     QEMU_IFLA_INET_CONF,
217     QEMU___IFLA_INET_MAX,
218 };
219 
220 enum {
221     QEMU_IFLA_INET6_UNSPEC,
222     QEMU_IFLA_INET6_FLAGS,
223     QEMU_IFLA_INET6_CONF,
224     QEMU_IFLA_INET6_STATS,
225     QEMU_IFLA_INET6_MCAST,
226     QEMU_IFLA_INET6_CACHEINFO,
227     QEMU_IFLA_INET6_ICMP6STATS,
228     QEMU_IFLA_INET6_TOKEN,
229     QEMU_IFLA_INET6_ADDR_GEN_MODE,
230     QEMU___IFLA_INET6_MAX
231 };
232 
233 enum {
234     QEMU_IFLA_XDP_UNSPEC,
235     QEMU_IFLA_XDP_FD,
236     QEMU_IFLA_XDP_ATTACHED,
237     QEMU_IFLA_XDP_FLAGS,
238     QEMU_IFLA_XDP_PROG_ID,
239     QEMU___IFLA_XDP_MAX,
240 };
241 
242 enum {
243     QEMU_RTA_UNSPEC,
244     QEMU_RTA_DST,
245     QEMU_RTA_SRC,
246     QEMU_RTA_IIF,
247     QEMU_RTA_OIF,
248     QEMU_RTA_GATEWAY,
249     QEMU_RTA_PRIORITY,
250     QEMU_RTA_PREFSRC,
251     QEMU_RTA_METRICS,
252     QEMU_RTA_MULTIPATH,
253     QEMU_RTA_PROTOINFO, /* no longer used */
254     QEMU_RTA_FLOW,
255     QEMU_RTA_CACHEINFO,
256     QEMU_RTA_SESSION, /* no longer used */
257     QEMU_RTA_MP_ALGO, /* no longer used */
258     QEMU_RTA_TABLE,
259     QEMU_RTA_MARK,
260     QEMU_RTA_MFC_STATS,
261     QEMU_RTA_VIA,
262     QEMU_RTA_NEWDST,
263     QEMU_RTA_PREF,
264     QEMU_RTA_ENCAP_TYPE,
265     QEMU_RTA_ENCAP,
266     QEMU_RTA_EXPIRES,
267     QEMU_RTA_PAD,
268     QEMU_RTA_UID,
269     QEMU_RTA_TTL_PROPAGATE,
270     QEMU_RTA_IP_PROTO,
271     QEMU_RTA_SPORT,
272     QEMU_RTA_DPORT,
273     QEMU___RTA_MAX
274 };
275 
276 enum {
277     QEMU_IFLA_VF_STATS_RX_PACKETS,
278     QEMU_IFLA_VF_STATS_TX_PACKETS,
279     QEMU_IFLA_VF_STATS_RX_BYTES,
280     QEMU_IFLA_VF_STATS_TX_BYTES,
281     QEMU_IFLA_VF_STATS_BROADCAST,
282     QEMU_IFLA_VF_STATS_MULTICAST,
283     QEMU_IFLA_VF_STATS_PAD,
284     QEMU_IFLA_VF_STATS_RX_DROPPED,
285     QEMU_IFLA_VF_STATS_TX_DROPPED,
286     QEMU__IFLA_VF_STATS_MAX,
287 };
288 
289 enum {
290     QEMU_IFLA_VF_UNSPEC,
291     QEMU_IFLA_VF_MAC,
292     QEMU_IFLA_VF_VLAN,
293     QEMU_IFLA_VF_TX_RATE,
294     QEMU_IFLA_VF_SPOOFCHK,
295     QEMU_IFLA_VF_LINK_STATE,
296     QEMU_IFLA_VF_RATE,
297     QEMU_IFLA_VF_RSS_QUERY_EN,
298     QEMU_IFLA_VF_STATS,
299     QEMU_IFLA_VF_TRUST,
300     QEMU_IFLA_VF_IB_NODE_GUID,
301     QEMU_IFLA_VF_IB_PORT_GUID,
302     QEMU_IFLA_VF_VLAN_LIST,
303     QEMU_IFLA_VF_BROADCAST,
304     QEMU__IFLA_VF_MAX,
305 };
306 
307 TargetFdTrans **target_fd_trans;
308 QemuMutex target_fd_trans_lock;
309 unsigned int target_fd_max;
310 
311 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
312 {
313     nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
314     nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
315     nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
316     nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
317     nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
318 }
319 
320 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
321                                               size_t len,
322                                               abi_long (*host_to_target_nlmsg)
323                                                        (struct nlmsghdr *))
324 {
325     uint32_t nlmsg_len;
326     uint32_t aligned_nlmsg_len;
327     abi_long ret;
328 
329     while (len > sizeof(struct nlmsghdr)) {
330 
331         nlmsg_len = nlh->nlmsg_len;
332         if (nlmsg_len < sizeof(struct nlmsghdr) ||
333             nlmsg_len > len) {
334             break;
335         }
336 
337         switch (nlh->nlmsg_type) {
338         case NLMSG_DONE:
339             tswap_nlmsghdr(nlh);
340             return 0;
341         case NLMSG_NOOP:
342             break;
343         case NLMSG_ERROR:
344         {
345             struct nlmsgerr *e = NLMSG_DATA(nlh);
346             e->error = tswap32(e->error);
347             tswap_nlmsghdr(&e->msg);
348             tswap_nlmsghdr(nlh);
349             return 0;
350         }
351         default:
352             ret = host_to_target_nlmsg(nlh);
353             if (ret < 0) {
354                 tswap_nlmsghdr(nlh);
355                 return ret;
356             }
357             break;
358         }
359         tswap_nlmsghdr(nlh);
360 
361         aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len);
362         if (aligned_nlmsg_len >= len) {
363             break;
364         }
365         len -= aligned_nlmsg_len;
366         nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len);
367     }
368     return 0;
369 }
370 
371 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
372                                               size_t len,
373                                               abi_long (*target_to_host_nlmsg)
374                                                        (struct nlmsghdr *))
375 {
376     uint32_t aligned_nlmsg_len;
377     int ret;
378 
379     while (len > sizeof(struct nlmsghdr)) {
380         if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
381             tswap32(nlh->nlmsg_len) > len) {
382             break;
383         }
384         tswap_nlmsghdr(nlh);
385         switch (nlh->nlmsg_type) {
386         case NLMSG_DONE:
387             return 0;
388         case NLMSG_NOOP:
389             break;
390         case NLMSG_ERROR:
391         {
392             struct nlmsgerr *e = NLMSG_DATA(nlh);
393             e->error = tswap32(e->error);
394             tswap_nlmsghdr(&e->msg);
395             return 0;
396         }
397         default:
398             ret = target_to_host_nlmsg(nlh);
399             if (ret < 0) {
400                 return ret;
401             }
402         }
403 
404         aligned_nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len);
405         if (aligned_nlmsg_len >= len) {
406             break;
407         }
408         len -= aligned_nlmsg_len;
409         nlh = (struct nlmsghdr *)(((char *)nlh) + aligned_nlmsg_len);
410     }
411     return 0;
412 }
413 
414 #ifdef CONFIG_RTNETLINK
415 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
416                                                size_t len, void *context,
417                                                abi_long (*host_to_target_nlattr)
418                                                         (struct nlattr *,
419                                                          void *context))
420 {
421     unsigned short nla_len;
422     unsigned short aligned_nla_len;
423     abi_long ret;
424 
425     while (len > sizeof(struct nlattr)) {
426         nla_len = nlattr->nla_len;
427         if (nla_len < sizeof(struct nlattr) ||
428             nla_len > len) {
429             break;
430         }
431         ret = host_to_target_nlattr(nlattr, context);
432         nlattr->nla_len = tswap16(nlattr->nla_len);
433         nlattr->nla_type = tswap16(nlattr->nla_type);
434         if (ret < 0) {
435             return ret;
436         }
437 
438         aligned_nla_len = NLA_ALIGN(nla_len);
439         if (aligned_nla_len >= len) {
440             break;
441         }
442         len -= aligned_nla_len;
443         nlattr = (struct nlattr *)(((char *)nlattr) + aligned_nla_len);
444     }
445     return 0;
446 }
447 
448 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
449                                                size_t len,
450                                                abi_long (*host_to_target_rtattr)
451                                                         (struct rtattr *))
452 {
453     unsigned short rta_len;
454     unsigned short aligned_rta_len;
455     abi_long ret;
456 
457     while (len > sizeof(struct rtattr)) {
458         rta_len = rtattr->rta_len;
459         if (rta_len < sizeof(struct rtattr) ||
460             rta_len > len) {
461             break;
462         }
463         ret = host_to_target_rtattr(rtattr);
464         rtattr->rta_len = tswap16(rtattr->rta_len);
465         rtattr->rta_type = tswap16(rtattr->rta_type);
466         if (ret < 0) {
467             return ret;
468         }
469 
470         aligned_rta_len = RTA_ALIGN(rta_len);
471         if (aligned_rta_len >= len) {
472             break;
473         }
474         len -= aligned_rta_len;
475         rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
476     }
477     return 0;
478 }
479 
480 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
481 
482 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
483                                                   void *context)
484 {
485     uint16_t *u16;
486     uint32_t *u32;
487     uint64_t *u64;
488 
489     switch (nlattr->nla_type) {
490     /* no data */
491     case QEMU_IFLA_BR_FDB_FLUSH:
492         break;
493     /* binary */
494     case QEMU_IFLA_BR_GROUP_ADDR:
495         break;
496     /* uint8_t */
497     case QEMU_IFLA_BR_VLAN_FILTERING:
498     case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
499     case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
500     case QEMU_IFLA_BR_MCAST_ROUTER:
501     case QEMU_IFLA_BR_MCAST_SNOOPING:
502     case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
503     case QEMU_IFLA_BR_MCAST_QUERIER:
504     case QEMU_IFLA_BR_NF_CALL_IPTABLES:
505     case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
506     case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
507     case QEMU_IFLA_BR_VLAN_STATS_ENABLED:
508     case QEMU_IFLA_BR_MCAST_STATS_ENABLED:
509     case QEMU_IFLA_BR_MCAST_IGMP_VERSION:
510     case QEMU_IFLA_BR_MCAST_MLD_VERSION:
511     case QEMU_IFLA_BR_VLAN_STATS_PER_PORT:
512         break;
513     /* uint16_t */
514     case QEMU_IFLA_BR_PRIORITY:
515     case QEMU_IFLA_BR_VLAN_PROTOCOL:
516     case QEMU_IFLA_BR_GROUP_FWD_MASK:
517     case QEMU_IFLA_BR_ROOT_PORT:
518     case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
519         u16 = NLA_DATA(nlattr);
520         *u16 = tswap16(*u16);
521         break;
522     /* uint32_t */
523     case QEMU_IFLA_BR_FORWARD_DELAY:
524     case QEMU_IFLA_BR_HELLO_TIME:
525     case QEMU_IFLA_BR_MAX_AGE:
526     case QEMU_IFLA_BR_AGEING_TIME:
527     case QEMU_IFLA_BR_STP_STATE:
528     case QEMU_IFLA_BR_ROOT_PATH_COST:
529     case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
530     case QEMU_IFLA_BR_MCAST_HASH_MAX:
531     case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
532     case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
533         u32 = NLA_DATA(nlattr);
534         *u32 = tswap32(*u32);
535         break;
536     /* uint64_t */
537     case QEMU_IFLA_BR_HELLO_TIMER:
538     case QEMU_IFLA_BR_TCN_TIMER:
539     case QEMU_IFLA_BR_GC_TIMER:
540     case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
541     case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
542     case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
543     case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
544     case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
545     case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
546     case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
547         u64 = NLA_DATA(nlattr);
548         *u64 = tswap64(*u64);
549         break;
550     /* ifla_bridge_id: uin8_t[] */
551     case QEMU_IFLA_BR_ROOT_ID:
552     case QEMU_IFLA_BR_BRIDGE_ID:
553         break;
554     /* br_boolopt_multi { uint32_t, uint32_t } */
555     case QEMU_IFLA_BR_MULTI_BOOLOPT:
556         u32 = NLA_DATA(nlattr);
557         u32[0] = tswap32(u32[0]); /* optval */
558         u32[1] = tswap32(u32[1]); /* optmask */
559         break;
560     default:
561         qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BR type %d\n",
562                       nlattr->nla_type);
563         break;
564     }
565     return 0;
566 }
567 
568 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
569                                                         void *context)
570 {
571     uint16_t *u16;
572     uint32_t *u32;
573     uint64_t *u64;
574 
575     switch (nlattr->nla_type) {
576     /* uint8_t */
577     case QEMU_IFLA_BRPORT_STATE:
578     case QEMU_IFLA_BRPORT_MODE:
579     case QEMU_IFLA_BRPORT_GUARD:
580     case QEMU_IFLA_BRPORT_PROTECT:
581     case QEMU_IFLA_BRPORT_FAST_LEAVE:
582     case QEMU_IFLA_BRPORT_LEARNING:
583     case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
584     case QEMU_IFLA_BRPORT_PROXYARP:
585     case QEMU_IFLA_BRPORT_LEARNING_SYNC:
586     case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
587     case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
588     case QEMU_IFLA_BRPORT_CONFIG_PENDING:
589     case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
590     case QEMU_IFLA_BRPORT_MCAST_FLOOD:
591     case QEMU_IFLA_BRPORT_MCAST_TO_UCAST:
592     case QEMU_IFLA_BRPORT_VLAN_TUNNEL:
593     case QEMU_IFLA_BRPORT_BCAST_FLOOD:
594     case QEMU_IFLA_BRPORT_NEIGH_SUPPRESS:
595     case QEMU_IFLA_BRPORT_ISOLATED:
596     case QEMU_IFLA_BRPORT_MRP_RING_OPEN:
597     case QEMU_IFLA_BRPORT_MRP_IN_OPEN:
598         break;
599     /* uint16_t */
600     case QEMU_IFLA_BRPORT_PRIORITY:
601     case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
602     case QEMU_IFLA_BRPORT_DESIGNATED_COST:
603     case QEMU_IFLA_BRPORT_ID:
604     case QEMU_IFLA_BRPORT_NO:
605     case QEMU_IFLA_BRPORT_GROUP_FWD_MASK:
606         u16 = NLA_DATA(nlattr);
607         *u16 = tswap16(*u16);
608         break;
609     /* uin32_t */
610     case QEMU_IFLA_BRPORT_COST:
611     case QEMU_IFLA_BRPORT_BACKUP_PORT:
612     case QEMU_IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT:
613     case QEMU_IFLA_BRPORT_MCAST_EHT_HOSTS_CNT:
614         u32 = NLA_DATA(nlattr);
615         *u32 = tswap32(*u32);
616         break;
617     /* uint64_t */
618     case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
619     case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
620     case QEMU_IFLA_BRPORT_HOLD_TIMER:
621         u64 = NLA_DATA(nlattr);
622         *u64 = tswap64(*u64);
623         break;
624     /* ifla_bridge_id: uint8_t[] */
625     case QEMU_IFLA_BRPORT_ROOT_ID:
626     case QEMU_IFLA_BRPORT_BRIDGE_ID:
627         break;
628     default:
629         qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_BRPORT type %d\n",
630                       nlattr->nla_type);
631         break;
632     }
633     return 0;
634 }
635 
636 static abi_long host_to_target_data_tun_nlattr(struct nlattr *nlattr,
637                                                   void *context)
638 {
639     uint32_t *u32;
640 
641     switch (nlattr->nla_type) {
642     /* uint8_t */
643     case QEMU_IFLA_TUN_TYPE:
644     case QEMU_IFLA_TUN_PI:
645     case QEMU_IFLA_TUN_VNET_HDR:
646     case QEMU_IFLA_TUN_PERSIST:
647     case QEMU_IFLA_TUN_MULTI_QUEUE:
648         break;
649     /* uint32_t */
650     case QEMU_IFLA_TUN_NUM_QUEUES:
651     case QEMU_IFLA_TUN_NUM_DISABLED_QUEUES:
652     case QEMU_IFLA_TUN_OWNER:
653     case QEMU_IFLA_TUN_GROUP:
654         u32 = NLA_DATA(nlattr);
655         *u32 = tswap32(*u32);
656         break;
657     default:
658         qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_TUN type %d\n",
659                       nlattr->nla_type);
660         break;
661     }
662     return 0;
663 }
664 
665 struct linkinfo_context {
666     int len;
667     char *name;
668     int slave_len;
669     char *slave_name;
670 };
671 
672 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
673                                                     void *context)
674 {
675     struct linkinfo_context *li_context = context;
676 
677     switch (nlattr->nla_type) {
678     /* string */
679     case QEMU_IFLA_INFO_KIND:
680         li_context->name = NLA_DATA(nlattr);
681         li_context->len = nlattr->nla_len - NLA_HDRLEN;
682         break;
683     case QEMU_IFLA_INFO_SLAVE_KIND:
684         li_context->slave_name = NLA_DATA(nlattr);
685         li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
686         break;
687     /* stats */
688     case QEMU_IFLA_INFO_XSTATS:
689         /* FIXME: only used by CAN */
690         break;
691     /* nested */
692     case QEMU_IFLA_INFO_DATA:
693         if (strncmp(li_context->name, "bridge",
694                     li_context->len) == 0) {
695             return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
696                                                   nlattr->nla_len,
697                                                   NULL,
698                                              host_to_target_data_bridge_nlattr);
699         } else if (strncmp(li_context->name, "tun",
700                     li_context->len) == 0) {
701             return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
702                                                   nlattr->nla_len,
703                                                   NULL,
704                                                 host_to_target_data_tun_nlattr);
705         } else {
706             qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_KIND %s\n",
707                           li_context->name);
708         }
709         break;
710     case QEMU_IFLA_INFO_SLAVE_DATA:
711         if (strncmp(li_context->slave_name, "bridge",
712                     li_context->slave_len) == 0) {
713             return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
714                                                   nlattr->nla_len,
715                                                   NULL,
716                                        host_to_target_slave_data_bridge_nlattr);
717         } else {
718             qemu_log_mask(LOG_UNIMP, "Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
719                      li_context->slave_name);
720         }
721         break;
722     default:
723         qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA_INFO type: %d\n",
724                       nlattr->nla_type);
725         break;
726     }
727 
728     return 0;
729 }
730 
731 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
732                                                 void *context)
733 {
734     uint32_t *u32;
735     int i;
736 
737     switch (nlattr->nla_type) {
738     case QEMU_IFLA_INET_CONF:
739         u32 = NLA_DATA(nlattr);
740         for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
741              i++) {
742             u32[i] = tswap32(u32[i]);
743         }
744         break;
745     default:
746         qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET type: %d\n",
747                       nlattr->nla_type);
748     }
749     return 0;
750 }
751 
752 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
753                                                 void *context)
754 {
755     uint32_t *u32;
756     uint64_t *u64;
757     struct ifla_cacheinfo *ci;
758     int i;
759 
760     switch (nlattr->nla_type) {
761     /* binaries */
762     case QEMU_IFLA_INET6_TOKEN:
763         break;
764     /* uint8_t */
765     case QEMU_IFLA_INET6_ADDR_GEN_MODE:
766         break;
767     /* uint32_t */
768     case QEMU_IFLA_INET6_FLAGS:
769         u32 = NLA_DATA(nlattr);
770         *u32 = tswap32(*u32);
771         break;
772     /* uint32_t[] */
773     case QEMU_IFLA_INET6_CONF:
774         u32 = NLA_DATA(nlattr);
775         for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
776              i++) {
777             u32[i] = tswap32(u32[i]);
778         }
779         break;
780     /* ifla_cacheinfo */
781     case QEMU_IFLA_INET6_CACHEINFO:
782         ci = NLA_DATA(nlattr);
783         ci->max_reasm_len = tswap32(ci->max_reasm_len);
784         ci->tstamp = tswap32(ci->tstamp);
785         ci->reachable_time = tswap32(ci->reachable_time);
786         ci->retrans_time = tswap32(ci->retrans_time);
787         break;
788     /* uint64_t[] */
789     case QEMU_IFLA_INET6_STATS:
790     case QEMU_IFLA_INET6_ICMP6STATS:
791         u64 = NLA_DATA(nlattr);
792         for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
793              i++) {
794             u64[i] = tswap64(u64[i]);
795         }
796         break;
797     default:
798         qemu_log_mask(LOG_UNIMP, "Unknown host AF_INET6 type: %d\n",
799                       nlattr->nla_type);
800     }
801     return 0;
802 }
803 
804 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
805                                                     void *context)
806 {
807     switch (nlattr->nla_type) {
808     case AF_INET:
809         return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
810                                               NULL,
811                                              host_to_target_data_inet_nlattr);
812     case AF_INET6:
813         return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
814                                               NULL,
815                                              host_to_target_data_inet6_nlattr);
816     default:
817         qemu_log_mask(LOG_UNIMP, "Unknown host AF_SPEC type: %d\n",
818                       nlattr->nla_type);
819         break;
820     }
821     return 0;
822 }
823 
824 static abi_long host_to_target_data_xdp_nlattr(struct nlattr *nlattr,
825                                                void *context)
826 {
827     uint32_t *u32;
828 
829     switch (nlattr->nla_type) {
830     /* uint8_t */
831     case QEMU_IFLA_XDP_ATTACHED:
832         break;
833     /* uint32_t */
834     case QEMU_IFLA_XDP_PROG_ID:
835         u32 = NLA_DATA(nlattr);
836         *u32 = tswap32(*u32);
837         break;
838     default:
839         qemu_log_mask(
840             LOG_UNIMP, "Unknown host XDP type: %d\n", nlattr->nla_type);
841         break;
842     }
843     return 0;
844 }
845 
846 static abi_long host_to_target_data_vlan_list_nlattr(struct nlattr *nlattr,
847                                                      void *context)
848 {
849     struct ifla_vf_vlan_info *vlan_info;
850 
851     switch (nlattr->nla_type) {
852     /* struct ifla_vf_vlan_info */
853     case IFLA_VF_VLAN_INFO:
854         vlan_info = NLA_DATA(nlattr);
855         vlan_info->vf = tswap32(vlan_info->vf);
856         vlan_info->vlan = tswap32(vlan_info->vlan);
857         vlan_info->qos = tswap32(vlan_info->qos);
858         break;
859     default:
860         qemu_log_mask(LOG_UNIMP, "Unknown host VLAN LIST type: %d\n",
861                       nlattr->nla_type);
862         break;
863     }
864     return 0;
865 }
866 
867 static abi_long host_to_target_data_vf_stats_nlattr(struct nlattr *nlattr,
868                                                     void *context)
869 {
870     uint64_t *u64;
871 
872     switch (nlattr->nla_type) {
873     /* uint64_t */
874     case QEMU_IFLA_VF_STATS_RX_PACKETS:
875     case QEMU_IFLA_VF_STATS_TX_PACKETS:
876     case QEMU_IFLA_VF_STATS_RX_BYTES:
877     case QEMU_IFLA_VF_STATS_TX_BYTES:
878     case QEMU_IFLA_VF_STATS_BROADCAST:
879     case QEMU_IFLA_VF_STATS_MULTICAST:
880     case QEMU_IFLA_VF_STATS_PAD:
881     case QEMU_IFLA_VF_STATS_RX_DROPPED:
882     case QEMU_IFLA_VF_STATS_TX_DROPPED:
883         u64 = NLA_DATA(nlattr);
884         *u64 = tswap64(*u64);
885         break;
886     default:
887         qemu_log_mask(LOG_UNIMP, "Unknown host VF STATS type: %d\n",
888                       nlattr->nla_type);
889         break;
890     }
891     return 0;
892 }
893 
894 static abi_long host_to_target_data_vfinfo_nlattr(struct nlattr *nlattr,
895                                                   void *context)
896 {
897     struct ifla_vf_mac *mac;
898     struct ifla_vf_vlan *vlan;
899     struct ifla_vf_vlan_info *vlan_info;
900     struct ifla_vf_spoofchk *spoofchk;
901     struct ifla_vf_rate *rate;
902     struct ifla_vf_link_state *link_state;
903     struct ifla_vf_rss_query_en *rss_query_en;
904     struct ifla_vf_trust *trust;
905     struct ifla_vf_guid *guid;
906 
907     switch (nlattr->nla_type) {
908     /* struct ifla_vf_mac */
909     case QEMU_IFLA_VF_MAC:
910         mac = NLA_DATA(nlattr);
911         mac->vf = tswap32(mac->vf);
912         break;
913     /* struct ifla_vf_broadcast */
914     case QEMU_IFLA_VF_BROADCAST:
915         break;
916     /* struct struct ifla_vf_vlan */
917     case QEMU_IFLA_VF_VLAN:
918         vlan = NLA_DATA(nlattr);
919         vlan->vf = tswap32(vlan->vf);
920         vlan->vlan = tswap32(vlan->vlan);
921         vlan->qos = tswap32(vlan->qos);
922         break;
923     /* struct ifla_vf_vlan_info */
924     case QEMU_IFLA_VF_TX_RATE:
925         vlan_info = NLA_DATA(nlattr);
926         vlan_info->vf = tswap32(vlan_info->vf);
927         vlan_info->vlan = tswap32(vlan_info->vlan);
928         vlan_info->qos = tswap32(vlan_info->qos);
929         break;
930     /* struct ifla_vf_spoofchk */
931     case QEMU_IFLA_VF_SPOOFCHK:
932         spoofchk = NLA_DATA(nlattr);
933         spoofchk->vf = tswap32(spoofchk->vf);
934         spoofchk->setting = tswap32(spoofchk->setting);
935         break;
936     /* struct ifla_vf_rate */
937     case QEMU_IFLA_VF_RATE:
938         rate = NLA_DATA(nlattr);
939         rate->vf = tswap32(rate->vf);
940         rate->min_tx_rate = tswap32(rate->min_tx_rate);
941         rate->max_tx_rate = tswap32(rate->max_tx_rate);
942         break;
943     /* struct ifla_vf_link_state */
944     case QEMU_IFLA_VF_LINK_STATE:
945         link_state = NLA_DATA(nlattr);
946         link_state->vf = tswap32(link_state->vf);
947         link_state->link_state = tswap32(link_state->link_state);
948         break;
949     /* struct ifla_vf_rss_query_en */
950     case QEMU_IFLA_VF_RSS_QUERY_EN:
951         rss_query_en = NLA_DATA(nlattr);
952         rss_query_en->vf = tswap32(rss_query_en->vf);
953         rss_query_en->setting = tswap32(rss_query_en->setting);
954         break;
955     /* struct ifla_vf_trust */
956     case QEMU_IFLA_VF_TRUST:
957         trust = NLA_DATA(nlattr);
958         trust->vf = tswap32(trust->vf);
959         trust->setting = tswap32(trust->setting);
960         break;
961     /* struct ifla_vf_guid  */
962     case QEMU_IFLA_VF_IB_NODE_GUID:
963     case QEMU_IFLA_VF_IB_PORT_GUID:
964         guid = NLA_DATA(nlattr);
965         guid->vf = tswap32(guid->vf);
966         guid->guid = tswap32(guid->guid);
967         break;
968     /* nested */
969     case QEMU_IFLA_VF_VLAN_LIST:
970         return host_to_target_for_each_nlattr(RTA_DATA(nlattr), nlattr->nla_len,
971                                               NULL,
972                                           host_to_target_data_vlan_list_nlattr);
973     case QEMU_IFLA_VF_STATS:
974         return host_to_target_for_each_nlattr(RTA_DATA(nlattr), nlattr->nla_len,
975                                               NULL,
976                                            host_to_target_data_vf_stats_nlattr);
977     default:
978         qemu_log_mask(LOG_UNIMP, "Unknown host VFINFO type: %d\n",
979                       nlattr->nla_type);
980         break;
981     }
982     return 0;
983 }
984 
985 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
986 {
987     uint32_t *u32;
988     struct rtnl_link_stats *st;
989     struct rtnl_link_stats64 *st64;
990     struct rtnl_link_ifmap *map;
991     struct linkinfo_context li_context;
992 
993     switch (rtattr->rta_type) {
994     /* binary stream */
995     case QEMU_IFLA_ADDRESS:
996     case QEMU_IFLA_BROADCAST:
997     case QEMU_IFLA_PERM_ADDRESS:
998     case QEMU_IFLA_PHYS_PORT_ID:
999     /* string */
1000     case QEMU_IFLA_IFNAME:
1001     case QEMU_IFLA_QDISC:
1002     case QEMU_IFLA_PARENT_DEV_NAME:
1003     case QEMU_IFLA_PARENT_DEV_BUS_NAME:
1004         break;
1005     /* uin8_t */
1006     case QEMU_IFLA_OPERSTATE:
1007     case QEMU_IFLA_LINKMODE:
1008     case QEMU_IFLA_CARRIER:
1009     case QEMU_IFLA_PROTO_DOWN:
1010         break;
1011     /* uint32_t */
1012     case QEMU_IFLA_MTU:
1013     case QEMU_IFLA_LINK:
1014     case QEMU_IFLA_WEIGHT:
1015     case QEMU_IFLA_TXQLEN:
1016     case QEMU_IFLA_CARRIER_CHANGES:
1017     case QEMU_IFLA_NUM_RX_QUEUES:
1018     case QEMU_IFLA_NUM_TX_QUEUES:
1019     case QEMU_IFLA_PROMISCUITY:
1020     case QEMU_IFLA_EXT_MASK:
1021     case QEMU_IFLA_LINK_NETNSID:
1022     case QEMU_IFLA_GROUP:
1023     case QEMU_IFLA_MASTER:
1024     case QEMU_IFLA_NUM_VF:
1025     case QEMU_IFLA_GSO_MAX_SEGS:
1026     case QEMU_IFLA_GSO_MAX_SIZE:
1027     case QEMU_IFLA_CARRIER_UP_COUNT:
1028     case QEMU_IFLA_CARRIER_DOWN_COUNT:
1029     case QEMU_IFLA_MIN_MTU:
1030     case QEMU_IFLA_MAX_MTU:
1031         u32 = RTA_DATA(rtattr);
1032         *u32 = tswap32(*u32);
1033         break;
1034     /* struct rtnl_link_stats */
1035     case QEMU_IFLA_STATS:
1036         st = RTA_DATA(rtattr);
1037         st->rx_packets = tswap32(st->rx_packets);
1038         st->tx_packets = tswap32(st->tx_packets);
1039         st->rx_bytes = tswap32(st->rx_bytes);
1040         st->tx_bytes = tswap32(st->tx_bytes);
1041         st->rx_errors = tswap32(st->rx_errors);
1042         st->tx_errors = tswap32(st->tx_errors);
1043         st->rx_dropped = tswap32(st->rx_dropped);
1044         st->tx_dropped = tswap32(st->tx_dropped);
1045         st->multicast = tswap32(st->multicast);
1046         st->collisions = tswap32(st->collisions);
1047 
1048         /* detailed rx_errors: */
1049         st->rx_length_errors = tswap32(st->rx_length_errors);
1050         st->rx_over_errors = tswap32(st->rx_over_errors);
1051         st->rx_crc_errors = tswap32(st->rx_crc_errors);
1052         st->rx_frame_errors = tswap32(st->rx_frame_errors);
1053         st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
1054         st->rx_missed_errors = tswap32(st->rx_missed_errors);
1055 
1056         /* detailed tx_errors */
1057         st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
1058         st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
1059         st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
1060         st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
1061         st->tx_window_errors = tswap32(st->tx_window_errors);
1062 
1063         /* for cslip etc */
1064         st->rx_compressed = tswap32(st->rx_compressed);
1065         st->tx_compressed = tswap32(st->tx_compressed);
1066         break;
1067     /* struct rtnl_link_stats64 */
1068     case QEMU_IFLA_STATS64:
1069         st64 = RTA_DATA(rtattr);
1070         st64->rx_packets = tswap64(st64->rx_packets);
1071         st64->tx_packets = tswap64(st64->tx_packets);
1072         st64->rx_bytes = tswap64(st64->rx_bytes);
1073         st64->tx_bytes = tswap64(st64->tx_bytes);
1074         st64->rx_errors = tswap64(st64->rx_errors);
1075         st64->tx_errors = tswap64(st64->tx_errors);
1076         st64->rx_dropped = tswap64(st64->rx_dropped);
1077         st64->tx_dropped = tswap64(st64->tx_dropped);
1078         st64->multicast = tswap64(st64->multicast);
1079         st64->collisions = tswap64(st64->collisions);
1080 
1081         /* detailed rx_errors: */
1082         st64->rx_length_errors = tswap64(st64->rx_length_errors);
1083         st64->rx_over_errors = tswap64(st64->rx_over_errors);
1084         st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
1085         st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
1086         st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
1087         st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
1088 
1089         /* detailed tx_errors */
1090         st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
1091         st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
1092         st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
1093         st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
1094         st64->tx_window_errors = tswap64(st64->tx_window_errors);
1095 
1096         /* for cslip etc */
1097         st64->rx_compressed = tswap64(st64->rx_compressed);
1098         st64->tx_compressed = tswap64(st64->tx_compressed);
1099         break;
1100     /* struct rtnl_link_ifmap */
1101     case QEMU_IFLA_MAP:
1102         map = RTA_DATA(rtattr);
1103         map->mem_start = tswap64(map->mem_start);
1104         map->mem_end = tswap64(map->mem_end);
1105         map->base_addr = tswap64(map->base_addr);
1106         map->irq = tswap16(map->irq);
1107         break;
1108     /* nested */
1109     case QEMU_IFLA_LINKINFO:
1110         memset(&li_context, 0, sizeof(li_context));
1111         return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
1112                                               &li_context,
1113                                            host_to_target_data_linkinfo_nlattr);
1114     case QEMU_IFLA_AF_SPEC:
1115         return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
1116                                               NULL,
1117                                              host_to_target_data_spec_nlattr);
1118     case QEMU_IFLA_XDP:
1119         return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
1120                                               NULL,
1121                                                 host_to_target_data_xdp_nlattr);
1122     case QEMU_IFLA_VFINFO_LIST:
1123         return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
1124                                               NULL,
1125                                              host_to_target_data_vfinfo_nlattr);
1126     default:
1127         qemu_log_mask(LOG_UNIMP, "Unknown host QEMU_IFLA type: %d\n",
1128                       rtattr->rta_type);
1129         break;
1130     }
1131     return 0;
1132 }
1133 
1134 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
1135 {
1136     uint32_t *u32;
1137     struct ifa_cacheinfo *ci;
1138 
1139     switch (rtattr->rta_type) {
1140     /* binary: depends on family type */
1141     case IFA_ADDRESS:
1142     case IFA_LOCAL:
1143         break;
1144     /* string */
1145     case IFA_LABEL:
1146         break;
1147     /* u32 */
1148     case IFA_FLAGS:
1149     case IFA_BROADCAST:
1150         u32 = RTA_DATA(rtattr);
1151         *u32 = tswap32(*u32);
1152         break;
1153     /* struct ifa_cacheinfo */
1154     case IFA_CACHEINFO:
1155         ci = RTA_DATA(rtattr);
1156         ci->ifa_prefered = tswap32(ci->ifa_prefered);
1157         ci->ifa_valid = tswap32(ci->ifa_valid);
1158         ci->cstamp = tswap32(ci->cstamp);
1159         ci->tstamp = tswap32(ci->tstamp);
1160         break;
1161     default:
1162         qemu_log_mask(
1163             LOG_UNIMP, "Unknown host IFA type: %d\n", rtattr->rta_type);
1164         break;
1165     }
1166     return 0;
1167 }
1168 
1169 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
1170 {
1171     uint32_t *u32;
1172     struct rta_cacheinfo *ci;
1173 
1174     switch (rtattr->rta_type) {
1175     /* binary: depends on family type */
1176     case QEMU_RTA_GATEWAY:
1177     case QEMU_RTA_DST:
1178     case QEMU_RTA_PREFSRC:
1179         break;
1180     /* u8 */
1181     case QEMU_RTA_PREF:
1182         break;
1183     /* u32 */
1184     case QEMU_RTA_PRIORITY:
1185     case QEMU_RTA_TABLE:
1186     case QEMU_RTA_OIF:
1187         u32 = RTA_DATA(rtattr);
1188         *u32 = tswap32(*u32);
1189         break;
1190     /* struct rta_cacheinfo */
1191     case QEMU_RTA_CACHEINFO:
1192         ci = RTA_DATA(rtattr);
1193         ci->rta_clntref = tswap32(ci->rta_clntref);
1194         ci->rta_lastuse = tswap32(ci->rta_lastuse);
1195         ci->rta_expires = tswap32(ci->rta_expires);
1196         ci->rta_error = tswap32(ci->rta_error);
1197         ci->rta_used = tswap32(ci->rta_used);
1198 #if defined(RTNETLINK_HAVE_PEERINFO)
1199         ci->rta_id = tswap32(ci->rta_id);
1200         ci->rta_ts = tswap32(ci->rta_ts);
1201         ci->rta_tsage = tswap32(ci->rta_tsage);
1202 #endif
1203         break;
1204     default:
1205         qemu_log_mask(
1206             LOG_UNIMP, "Unknown host RTA type: %d\n", rtattr->rta_type);
1207         break;
1208     }
1209     return 0;
1210 }
1211 
1212 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1213                                          uint32_t rtattr_len)
1214 {
1215     return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1216                                           host_to_target_data_link_rtattr);
1217 }
1218 
1219 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1220                                          uint32_t rtattr_len)
1221 {
1222     return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1223                                           host_to_target_data_addr_rtattr);
1224 }
1225 
1226 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1227                                          uint32_t rtattr_len)
1228 {
1229     return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1230                                           host_to_target_data_route_rtattr);
1231 }
1232 
1233 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1234 {
1235     uint32_t nlmsg_len;
1236     struct ifinfomsg *ifi;
1237     struct ifaddrmsg *ifa;
1238     struct rtmsg *rtm;
1239 
1240     nlmsg_len = nlh->nlmsg_len;
1241     switch (nlh->nlmsg_type) {
1242     case RTM_NEWLINK:
1243     case RTM_DELLINK:
1244     case RTM_GETLINK:
1245         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1246             ifi = NLMSG_DATA(nlh);
1247             ifi->ifi_type = tswap16(ifi->ifi_type);
1248             ifi->ifi_index = tswap32(ifi->ifi_index);
1249             ifi->ifi_flags = tswap32(ifi->ifi_flags);
1250             ifi->ifi_change = tswap32(ifi->ifi_change);
1251             host_to_target_link_rtattr(IFLA_RTA(ifi),
1252                                        nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1253         }
1254         break;
1255     case RTM_NEWADDR:
1256     case RTM_DELADDR:
1257     case RTM_GETADDR:
1258         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1259             ifa = NLMSG_DATA(nlh);
1260             ifa->ifa_index = tswap32(ifa->ifa_index);
1261             host_to_target_addr_rtattr(IFA_RTA(ifa),
1262                                        nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1263         }
1264         break;
1265     case RTM_NEWROUTE:
1266     case RTM_DELROUTE:
1267     case RTM_GETROUTE:
1268         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1269             rtm = NLMSG_DATA(nlh);
1270             rtm->rtm_flags = tswap32(rtm->rtm_flags);
1271             host_to_target_route_rtattr(RTM_RTA(rtm),
1272                                         nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1273         }
1274         break;
1275     default:
1276         return -TARGET_EINVAL;
1277     }
1278     return 0;
1279 }
1280 
1281 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1282                                                   size_t len)
1283 {
1284     return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1285 }
1286 
1287 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1288                                                size_t len,
1289                                                abi_long (*target_to_host_rtattr)
1290                                                         (struct rtattr *))
1291 {
1292     unsigned short aligned_rta_len;
1293     abi_long ret;
1294 
1295     while (len >= sizeof(struct rtattr)) {
1296         if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1297             tswap16(rtattr->rta_len) > len) {
1298             break;
1299         }
1300         rtattr->rta_len = tswap16(rtattr->rta_len);
1301         rtattr->rta_type = tswap16(rtattr->rta_type);
1302         ret = target_to_host_rtattr(rtattr);
1303         if (ret < 0) {
1304             return ret;
1305         }
1306 
1307         aligned_rta_len = RTA_ALIGN(rtattr->rta_len);
1308         if (aligned_rta_len >= len) {
1309             break;
1310         }
1311         len -= aligned_rta_len;
1312         rtattr = (struct rtattr *)(((char *)rtattr) + aligned_rta_len);
1313     }
1314     return 0;
1315 }
1316 
1317 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1318 {
1319     uint32_t *u32;
1320 
1321     switch (rtattr->rta_type) {
1322     /* uint32_t */
1323     case QEMU_IFLA_EXT_MASK:
1324         u32 = RTA_DATA(rtattr);
1325         *u32 = tswap32(*u32);
1326         break;
1327     default:
1328         qemu_log_mask(LOG_UNIMP, "Unknown target QEMU_IFLA type: %d\n",
1329                       rtattr->rta_type);
1330         break;
1331     }
1332     return 0;
1333 }
1334 
1335 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1336 {
1337     switch (rtattr->rta_type) {
1338     /* binary: depends on family type */
1339     case IFA_LOCAL:
1340     case IFA_ADDRESS:
1341         break;
1342     default:
1343         qemu_log_mask(LOG_UNIMP, "Unknown target IFA type: %d\n",
1344                       rtattr->rta_type);
1345         break;
1346     }
1347     return 0;
1348 }
1349 
1350 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1351 {
1352     uint32_t *u32;
1353     switch (rtattr->rta_type) {
1354     /* binary: depends on family type */
1355     case QEMU_RTA_DST:
1356     case QEMU_RTA_SRC:
1357     case QEMU_RTA_GATEWAY:
1358         break;
1359     /* u32 */
1360     case QEMU_RTA_PRIORITY:
1361     case QEMU_RTA_TABLE:
1362     case QEMU_RTA_OIF:
1363         u32 = RTA_DATA(rtattr);
1364         *u32 = tswap32(*u32);
1365         break;
1366     default:
1367         qemu_log_mask(LOG_UNIMP, "Unknown target RTA type: %d\n",
1368                       rtattr->rta_type);
1369         break;
1370     }
1371     return 0;
1372 }
1373 
1374 static void target_to_host_link_rtattr(struct rtattr *rtattr,
1375                                        uint32_t rtattr_len)
1376 {
1377     target_to_host_for_each_rtattr(rtattr, rtattr_len,
1378                                    target_to_host_data_link_rtattr);
1379 }
1380 
1381 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1382                                      uint32_t rtattr_len)
1383 {
1384     target_to_host_for_each_rtattr(rtattr, rtattr_len,
1385                                    target_to_host_data_addr_rtattr);
1386 }
1387 
1388 static void target_to_host_route_rtattr(struct rtattr *rtattr,
1389                                      uint32_t rtattr_len)
1390 {
1391     target_to_host_for_each_rtattr(rtattr, rtattr_len,
1392                                    target_to_host_data_route_rtattr);
1393 }
1394 
1395 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
1396 {
1397     struct ifinfomsg *ifi;
1398     struct ifaddrmsg *ifa;
1399     struct rtmsg *rtm;
1400 
1401     switch (nlh->nlmsg_type) {
1402     case RTM_NEWLINK:
1403     case RTM_DELLINK:
1404     case RTM_SETLINK:
1405     case RTM_GETLINK:
1406         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1407             ifi = NLMSG_DATA(nlh);
1408             ifi->ifi_type = tswap16(ifi->ifi_type);
1409             ifi->ifi_index = tswap32(ifi->ifi_index);
1410             ifi->ifi_flags = tswap32(ifi->ifi_flags);
1411             ifi->ifi_change = tswap32(ifi->ifi_change);
1412             target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
1413                                        NLMSG_LENGTH(sizeof(*ifi)));
1414         }
1415         break;
1416     case RTM_GETADDR:
1417     case RTM_NEWADDR:
1418     case RTM_DELADDR:
1419         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1420             ifa = NLMSG_DATA(nlh);
1421             ifa->ifa_index = tswap32(ifa->ifa_index);
1422             target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
1423                                        NLMSG_LENGTH(sizeof(*ifa)));
1424         }
1425         break;
1426     case RTM_NEWROUTE:
1427     case RTM_DELROUTE:
1428     case RTM_GETROUTE:
1429         if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1430             rtm = NLMSG_DATA(nlh);
1431             rtm->rtm_flags = tswap32(rtm->rtm_flags);
1432             target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
1433                                         NLMSG_LENGTH(sizeof(*rtm)));
1434         }
1435         break;
1436     default:
1437         return -TARGET_EOPNOTSUPP;
1438     }
1439     return 0;
1440 }
1441 
1442 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
1443 {
1444     return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
1445 }
1446 #endif /* CONFIG_RTNETLINK */
1447 
1448 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
1449 {
1450     switch (nlh->nlmsg_type) {
1451     default:
1452         qemu_log_mask(LOG_UNIMP, "Unknown host audit message type %d\n",
1453                       nlh->nlmsg_type);
1454         return -TARGET_EINVAL;
1455     }
1456     return 0;
1457 }
1458 
1459 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
1460                                                   size_t len)
1461 {
1462     return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
1463 }
1464 
1465 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
1466 {
1467     switch (nlh->nlmsg_type) {
1468     case AUDIT_USER:
1469     case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
1470     case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
1471         break;
1472     default:
1473         qemu_log_mask(LOG_UNIMP, "Unknown target audit message type %d\n",
1474                       nlh->nlmsg_type);
1475         return -TARGET_EINVAL;
1476     }
1477 
1478     return 0;
1479 }
1480 
1481 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
1482 {
1483     return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
1484 }
1485 
1486 static abi_long packet_target_to_host_sockaddr(void *host_addr,
1487                                                abi_ulong target_addr,
1488                                                socklen_t len)
1489 {
1490     struct sockaddr *addr = host_addr;
1491     struct target_sockaddr *target_saddr;
1492 
1493     target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1494     if (!target_saddr) {
1495         return -TARGET_EFAULT;
1496     }
1497 
1498     memcpy(addr, target_saddr, len);
1499     addr->sa_family = tswap16(target_saddr->sa_family);
1500     /* spkt_protocol is big-endian */
1501 
1502     unlock_user(target_saddr, target_addr, 0);
1503     return 0;
1504 }
1505 
1506 TargetFdTrans target_packet_trans = {
1507     .target_to_host_addr = packet_target_to_host_sockaddr,
1508 };
1509 
1510 #ifdef CONFIG_RTNETLINK
1511 static abi_long netlink_route_target_to_host(void *buf, size_t len)
1512 {
1513     abi_long ret;
1514 
1515     ret = target_to_host_nlmsg_route(buf, len);
1516     if (ret < 0) {
1517         return ret;
1518     }
1519 
1520     return len;
1521 }
1522 
1523 static abi_long netlink_route_host_to_target(void *buf, size_t len)
1524 {
1525     abi_long ret;
1526 
1527     ret = host_to_target_nlmsg_route(buf, len);
1528     if (ret < 0) {
1529         return ret;
1530     }
1531 
1532     return len;
1533 }
1534 
1535 TargetFdTrans target_netlink_route_trans = {
1536     .target_to_host_data = netlink_route_target_to_host,
1537     .host_to_target_data = netlink_route_host_to_target,
1538 };
1539 #endif /* CONFIG_RTNETLINK */
1540 
1541 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
1542 {
1543     abi_long ret;
1544 
1545     ret = target_to_host_nlmsg_audit(buf, len);
1546     if (ret < 0) {
1547         return ret;
1548     }
1549 
1550     return len;
1551 }
1552 
1553 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
1554 {
1555     abi_long ret;
1556 
1557     ret = host_to_target_nlmsg_audit(buf, len);
1558     if (ret < 0) {
1559         return ret;
1560     }
1561 
1562     return len;
1563 }
1564 
1565 TargetFdTrans target_netlink_audit_trans = {
1566     .target_to_host_data = netlink_audit_target_to_host,
1567     .host_to_target_data = netlink_audit_host_to_target,
1568 };
1569 
1570 /* signalfd siginfo conversion */
1571 
1572 static void
1573 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
1574                                 const struct signalfd_siginfo *info)
1575 {
1576     int sig = host_to_target_signal(info->ssi_signo);
1577 
1578     /* linux/signalfd.h defines a ssi_addr_lsb
1579      * not defined in sys/signalfd.h but used by some kernels
1580      */
1581 
1582 #ifdef BUS_MCEERR_AO
1583     if (tinfo->ssi_signo == SIGBUS &&
1584         (tinfo->ssi_code == BUS_MCEERR_AR ||
1585          tinfo->ssi_code == BUS_MCEERR_AO)) {
1586         uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
1587         uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
1588         *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
1589     }
1590 #endif
1591 
1592     tinfo->ssi_signo = tswap32(sig);
1593     tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
1594     tinfo->ssi_code = tswap32(info->ssi_code);
1595     tinfo->ssi_pid = tswap32(info->ssi_pid);
1596     tinfo->ssi_uid = tswap32(info->ssi_uid);
1597     tinfo->ssi_fd = tswap32(info->ssi_fd);
1598     tinfo->ssi_tid = tswap32(info->ssi_tid);
1599     tinfo->ssi_band = tswap32(info->ssi_band);
1600     tinfo->ssi_overrun = tswap32(info->ssi_overrun);
1601     tinfo->ssi_trapno = tswap32(info->ssi_trapno);
1602     tinfo->ssi_status = tswap32(info->ssi_status);
1603     tinfo->ssi_int = tswap32(info->ssi_int);
1604     tinfo->ssi_ptr = tswap64(info->ssi_ptr);
1605     tinfo->ssi_utime = tswap64(info->ssi_utime);
1606     tinfo->ssi_stime = tswap64(info->ssi_stime);
1607     tinfo->ssi_addr = tswap64(info->ssi_addr);
1608 }
1609 
1610 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
1611 {
1612     int i;
1613 
1614     for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
1615         host_to_target_signalfd_siginfo(buf + i, buf + i);
1616     }
1617 
1618     return len;
1619 }
1620 
1621 TargetFdTrans target_signalfd_trans = {
1622     .host_to_target_data = host_to_target_data_signalfd,
1623 };
1624 
1625 static abi_long swap_data_eventfd(void *buf, size_t len)
1626 {
1627     uint64_t *counter = buf;
1628     int i;
1629 
1630     if (len < sizeof(uint64_t)) {
1631         return -EINVAL;
1632     }
1633 
1634     for (i = 0; i < len; i += sizeof(uint64_t)) {
1635         *counter = tswap64(*counter);
1636         counter++;
1637     }
1638 
1639     return len;
1640 }
1641 
1642 TargetFdTrans target_eventfd_trans = {
1643     .host_to_target_data = swap_data_eventfd,
1644     .target_to_host_data = swap_data_eventfd,
1645 };
1646 
1647 #if defined(CONFIG_INOTIFY) && (defined(TARGET_NR_inotify_init) || \
1648         defined(TARGET_NR_inotify_init1))
1649 static abi_long host_to_target_data_inotify(void *buf, size_t len)
1650 {
1651     struct inotify_event *ev;
1652     int i;
1653     uint32_t name_len;
1654 
1655     for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
1656         ev = (struct inotify_event *)((char *)buf + i);
1657         name_len = ev->len;
1658 
1659         ev->wd = tswap32(ev->wd);
1660         ev->mask = tswap32(ev->mask);
1661         ev->cookie = tswap32(ev->cookie);
1662         ev->len = tswap32(name_len);
1663     }
1664 
1665     return len;
1666 }
1667 
1668 TargetFdTrans target_inotify_trans = {
1669     .host_to_target_data = host_to_target_data_inotify,
1670 };
1671 #endif
1672