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