OSDN Git Service

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