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