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