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