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