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