2
0

fd-trans.c 43 KB

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