2
0

fd-trans.c 43 KB

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