vector_user.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdarg.h>
  9. #include <errno.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include <sys/ioctl.h>
  13. #include <net/if.h>
  14. #include <linux/if_tun.h>
  15. #include <arpa/inet.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <sys/socket.h>
  20. #include <sys/un.h>
  21. #include <netinet/ip.h>
  22. #include <linux/if_ether.h>
  23. #include <linux/if_packet.h>
  24. #include <sys/wait.h>
  25. #include <sys/uio.h>
  26. #include <linux/virtio_net.h>
  27. #include <netdb.h>
  28. #include <stdlib.h>
  29. #include <os.h>
  30. #include <limits.h>
  31. #include <um_malloc.h>
  32. #include "vector_user.h"
  33. #define ID_GRE 0
  34. #define ID_L2TPV3 1
  35. #define ID_BESS 2
  36. #define ID_MAX 2
  37. #define TOKEN_IFNAME "ifname"
  38. #define TOKEN_SCRIPT "ifup"
  39. #define TRANS_RAW "raw"
  40. #define TRANS_RAW_LEN strlen(TRANS_RAW)
  41. #define TRANS_FD "fd"
  42. #define TRANS_FD_LEN strlen(TRANS_FD)
  43. #define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
  44. #define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
  45. #define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
  46. #define UNIX_BIND_FAIL "unix_open : could not bind socket err=%i"
  47. #define BPF_ATTACH_FAIL "Failed to attach filter size %d prog %px to %d, err %d\n"
  48. #define BPF_DETACH_FAIL "Failed to detach filter size %d prog %px to %d, err %d\n"
  49. #define MAX_UN_LEN 107
  50. static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  51. static const char *template = "tapXXXXXX";
  52. /* This is very ugly and brute force lookup, but it is done
  53. * only once at initialization so not worth doing hashes or
  54. * anything more intelligent
  55. */
  56. char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
  57. {
  58. int i;
  59. for (i = 0; i < ifspec->numargs; i++) {
  60. if (strcmp(ifspec->tokens[i], token) == 0)
  61. return ifspec->values[i];
  62. }
  63. return NULL;
  64. }
  65. struct arglist *uml_parse_vector_ifspec(char *arg)
  66. {
  67. struct arglist *result;
  68. int pos, len;
  69. bool parsing_token = true, next_starts = true;
  70. if (arg == NULL)
  71. return NULL;
  72. result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
  73. if (result == NULL)
  74. return NULL;
  75. result->numargs = 0;
  76. len = strlen(arg);
  77. for (pos = 0; pos < len; pos++) {
  78. if (next_starts) {
  79. if (parsing_token) {
  80. result->tokens[result->numargs] = arg + pos;
  81. } else {
  82. result->values[result->numargs] = arg + pos;
  83. result->numargs++;
  84. }
  85. next_starts = false;
  86. }
  87. if (*(arg + pos) == '=') {
  88. if (parsing_token)
  89. parsing_token = false;
  90. else
  91. goto cleanup;
  92. next_starts = true;
  93. (*(arg + pos)) = '\0';
  94. }
  95. if (*(arg + pos) == ',') {
  96. parsing_token = true;
  97. next_starts = true;
  98. (*(arg + pos)) = '\0';
  99. }
  100. }
  101. return result;
  102. cleanup:
  103. printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
  104. kfree(result);
  105. return NULL;
  106. }
  107. /*
  108. * Socket/FD configuration functions. These return an structure
  109. * of rx and tx descriptors to cover cases where these are not
  110. * the same (f.e. read via raw socket and write via tap).
  111. */
  112. #define PATH_NET_TUN "/dev/net/tun"
  113. static int create_tap_fd(char *iface)
  114. {
  115. struct ifreq ifr;
  116. int fd = -1;
  117. int err = -ENOMEM, offload;
  118. fd = open(PATH_NET_TUN, O_RDWR);
  119. if (fd < 0) {
  120. printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
  121. goto tap_fd_cleanup;
  122. }
  123. memset(&ifr, 0, sizeof(ifr));
  124. ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
  125. strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  126. err = ioctl(fd, TUNSETIFF, (void *) &ifr);
  127. if (err != 0) {
  128. printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
  129. goto tap_fd_cleanup;
  130. }
  131. offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
  132. ioctl(fd, TUNSETOFFLOAD, offload);
  133. return fd;
  134. tap_fd_cleanup:
  135. if (fd >= 0)
  136. os_close_file(fd);
  137. return err;
  138. }
  139. static int create_raw_fd(char *iface, int flags, int proto)
  140. {
  141. struct ifreq ifr;
  142. int fd = -1;
  143. struct sockaddr_ll sock;
  144. int err = -ENOMEM;
  145. fd = socket(AF_PACKET, SOCK_RAW, flags);
  146. if (fd == -1) {
  147. err = -errno;
  148. goto raw_fd_cleanup;
  149. }
  150. memset(&ifr, 0, sizeof(ifr));
  151. strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  152. if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
  153. err = -errno;
  154. goto raw_fd_cleanup;
  155. }
  156. sock.sll_family = AF_PACKET;
  157. sock.sll_protocol = htons(proto);
  158. sock.sll_ifindex = ifr.ifr_ifindex;
  159. if (bind(fd,
  160. (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
  161. err = -errno;
  162. goto raw_fd_cleanup;
  163. }
  164. return fd;
  165. raw_fd_cleanup:
  166. printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
  167. if (fd >= 0)
  168. os_close_file(fd);
  169. return err;
  170. }
  171. static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
  172. {
  173. int fd = -1, i;
  174. char *iface;
  175. struct vector_fds *result = NULL;
  176. bool dynamic = false;
  177. char dynamic_ifname[IFNAMSIZ];
  178. char *argv[] = {NULL, NULL, NULL, NULL};
  179. iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
  180. if (iface == NULL) {
  181. dynamic = true;
  182. iface = dynamic_ifname;
  183. srand(getpid());
  184. }
  185. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  186. if (result == NULL) {
  187. printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
  188. goto tap_cleanup;
  189. }
  190. result->rx_fd = -1;
  191. result->tx_fd = -1;
  192. result->remote_addr = NULL;
  193. result->remote_addr_size = 0;
  194. /* TAP */
  195. do {
  196. if (dynamic) {
  197. strcpy(iface, template);
  198. for (i = 0; i < strlen(iface); i++) {
  199. if (iface[i] == 'X') {
  200. iface[i] = padchar[rand() % strlen(padchar)];
  201. }
  202. }
  203. }
  204. fd = create_tap_fd(iface);
  205. if ((fd < 0) && (!dynamic)) {
  206. printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n");
  207. goto tap_cleanup;
  208. }
  209. result->tx_fd = fd;
  210. result->rx_fd = fd;
  211. } while (fd < 0);
  212. argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
  213. if (argv[0]) {
  214. argv[1] = iface;
  215. run_helper(NULL, NULL, argv);
  216. }
  217. return result;
  218. tap_cleanup:
  219. printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd);
  220. kfree(result);
  221. return NULL;
  222. }
  223. static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
  224. {
  225. char *iface;
  226. struct vector_fds *result = NULL;
  227. char *argv[] = {NULL, NULL, NULL, NULL};
  228. iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
  229. if (iface == NULL) {
  230. printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
  231. goto hybrid_cleanup;
  232. }
  233. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  234. if (result == NULL) {
  235. printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
  236. goto hybrid_cleanup;
  237. }
  238. result->rx_fd = -1;
  239. result->tx_fd = -1;
  240. result->remote_addr = NULL;
  241. result->remote_addr_size = 0;
  242. /* TAP */
  243. result->tx_fd = create_tap_fd(iface);
  244. if (result->tx_fd < 0) {
  245. printk(UM_KERN_ERR "uml_tap: failed to create tun interface: %i\n", result->tx_fd);
  246. goto hybrid_cleanup;
  247. }
  248. /* RAW */
  249. result->rx_fd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
  250. if (result->rx_fd == -1) {
  251. printk(UM_KERN_ERR
  252. "uml_tap: failed to create paired raw socket: %i\n", result->rx_fd);
  253. goto hybrid_cleanup;
  254. }
  255. argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
  256. if (argv[0]) {
  257. argv[1] = iface;
  258. run_helper(NULL, NULL, argv);
  259. }
  260. return result;
  261. hybrid_cleanup:
  262. printk(UM_KERN_ERR "user_init_hybrid: init failed");
  263. kfree(result);
  264. return NULL;
  265. }
  266. static struct vector_fds *user_init_unix_fds(struct arglist *ifspec, int id)
  267. {
  268. int fd = -1;
  269. int socktype;
  270. char *src, *dst;
  271. struct vector_fds *result = NULL;
  272. struct sockaddr_un *local_addr = NULL, *remote_addr = NULL;
  273. src = uml_vector_fetch_arg(ifspec, "src");
  274. dst = uml_vector_fetch_arg(ifspec, "dst");
  275. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  276. if (result == NULL) {
  277. printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
  278. goto unix_cleanup;
  279. }
  280. remote_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
  281. if (remote_addr == NULL) {
  282. printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
  283. goto unix_cleanup;
  284. }
  285. switch (id) {
  286. case ID_BESS:
  287. socktype = SOCK_SEQPACKET;
  288. if ((src != NULL) && (strlen(src) <= MAX_UN_LEN)) {
  289. local_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
  290. if (local_addr == NULL) {
  291. printk(UM_KERN_ERR "bess open:cannot allocate local addr");
  292. goto unix_cleanup;
  293. }
  294. local_addr->sun_family = AF_UNIX;
  295. memcpy(local_addr->sun_path, src, strlen(src) + 1);
  296. }
  297. if ((dst == NULL) || (strlen(dst) > MAX_UN_LEN))
  298. goto unix_cleanup;
  299. remote_addr->sun_family = AF_UNIX;
  300. memcpy(remote_addr->sun_path, dst, strlen(dst) + 1);
  301. break;
  302. default:
  303. printk(KERN_ERR "Unsupported unix socket type\n");
  304. return NULL;
  305. }
  306. fd = socket(AF_UNIX, socktype, 0);
  307. if (fd == -1) {
  308. printk(UM_KERN_ERR
  309. "unix open: could not open socket, error = %d",
  310. -errno
  311. );
  312. goto unix_cleanup;
  313. }
  314. if (local_addr != NULL) {
  315. if (bind(fd, (struct sockaddr *) local_addr, sizeof(struct sockaddr_un))) {
  316. printk(UM_KERN_ERR UNIX_BIND_FAIL, errno);
  317. goto unix_cleanup;
  318. }
  319. }
  320. switch (id) {
  321. case ID_BESS:
  322. if (connect(fd, (const struct sockaddr *) remote_addr, sizeof(struct sockaddr_un)) < 0) {
  323. printk(UM_KERN_ERR "bess open:cannot connect to %s %i", remote_addr->sun_path, -errno);
  324. goto unix_cleanup;
  325. }
  326. break;
  327. }
  328. result->rx_fd = fd;
  329. result->tx_fd = fd;
  330. result->remote_addr_size = sizeof(struct sockaddr_un);
  331. result->remote_addr = remote_addr;
  332. return result;
  333. unix_cleanup:
  334. if (fd >= 0)
  335. os_close_file(fd);
  336. kfree(remote_addr);
  337. kfree(result);
  338. return NULL;
  339. }
  340. static int strtofd(const char *nptr)
  341. {
  342. long fd;
  343. char *endptr;
  344. if (nptr == NULL)
  345. return -1;
  346. errno = 0;
  347. fd = strtol(nptr, &endptr, 10);
  348. if (nptr == endptr ||
  349. errno != 0 ||
  350. *endptr != '\0' ||
  351. fd < 0 ||
  352. fd > INT_MAX) {
  353. return -1;
  354. }
  355. return fd;
  356. }
  357. static struct vector_fds *user_init_fd_fds(struct arglist *ifspec)
  358. {
  359. int fd = -1;
  360. char *fdarg = NULL;
  361. struct vector_fds *result = NULL;
  362. fdarg = uml_vector_fetch_arg(ifspec, "fd");
  363. fd = strtofd(fdarg);
  364. if (fd == -1) {
  365. printk(UM_KERN_ERR "fd open: bad or missing fd argument");
  366. goto fd_cleanup;
  367. }
  368. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  369. if (result == NULL) {
  370. printk(UM_KERN_ERR "fd open: allocation failed");
  371. goto fd_cleanup;
  372. }
  373. result->rx_fd = fd;
  374. result->tx_fd = fd;
  375. result->remote_addr_size = 0;
  376. result->remote_addr = NULL;
  377. return result;
  378. fd_cleanup:
  379. if (fd >= 0)
  380. os_close_file(fd);
  381. kfree(result);
  382. return NULL;
  383. }
  384. static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
  385. {
  386. int rxfd = -1, txfd = -1;
  387. int err = -ENOMEM;
  388. char *iface;
  389. struct vector_fds *result = NULL;
  390. char *argv[] = {NULL, NULL, NULL, NULL};
  391. iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
  392. if (iface == NULL)
  393. goto raw_cleanup;
  394. rxfd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
  395. if (rxfd == -1) {
  396. err = -errno;
  397. goto raw_cleanup;
  398. }
  399. txfd = create_raw_fd(iface, 0, ETH_P_IP); /* Turn off RX on this fd */
  400. if (txfd == -1) {
  401. err = -errno;
  402. goto raw_cleanup;
  403. }
  404. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  405. if (result != NULL) {
  406. result->rx_fd = rxfd;
  407. result->tx_fd = txfd;
  408. result->remote_addr = NULL;
  409. result->remote_addr_size = 0;
  410. }
  411. argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
  412. if (argv[0]) {
  413. argv[1] = iface;
  414. run_helper(NULL, NULL, argv);
  415. }
  416. return result;
  417. raw_cleanup:
  418. printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
  419. kfree(result);
  420. return NULL;
  421. }
  422. bool uml_raw_enable_qdisc_bypass(int fd)
  423. {
  424. int optval = 1;
  425. if (setsockopt(fd,
  426. SOL_PACKET, PACKET_QDISC_BYPASS,
  427. &optval, sizeof(optval)) != 0) {
  428. return false;
  429. }
  430. return true;
  431. }
  432. bool uml_raw_enable_vnet_headers(int fd)
  433. {
  434. int optval = 1;
  435. if (setsockopt(fd,
  436. SOL_PACKET, PACKET_VNET_HDR,
  437. &optval, sizeof(optval)) != 0) {
  438. printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
  439. return false;
  440. }
  441. return true;
  442. }
  443. bool uml_tap_enable_vnet_headers(int fd)
  444. {
  445. unsigned int features;
  446. int len = sizeof(struct virtio_net_hdr);
  447. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  448. printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
  449. return false;
  450. }
  451. if ((features & IFF_VNET_HDR) == 0) {
  452. printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
  453. return false;
  454. }
  455. ioctl(fd, TUNSETVNETHDRSZ, &len);
  456. return true;
  457. }
  458. static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
  459. {
  460. int err = -ENOMEM;
  461. int fd = -1, gairet;
  462. struct addrinfo srchints;
  463. struct addrinfo dsthints;
  464. bool v6, udp;
  465. char *value;
  466. char *src, *dst, *srcport, *dstport;
  467. struct addrinfo *gairesult = NULL;
  468. struct vector_fds *result = NULL;
  469. value = uml_vector_fetch_arg(ifspec, "v6");
  470. v6 = false;
  471. udp = false;
  472. if (value != NULL) {
  473. if (strtol((const char *) value, NULL, 10) > 0)
  474. v6 = true;
  475. }
  476. value = uml_vector_fetch_arg(ifspec, "udp");
  477. if (value != NULL) {
  478. if (strtol((const char *) value, NULL, 10) > 0)
  479. udp = true;
  480. }
  481. src = uml_vector_fetch_arg(ifspec, "src");
  482. dst = uml_vector_fetch_arg(ifspec, "dst");
  483. srcport = uml_vector_fetch_arg(ifspec, "srcport");
  484. dstport = uml_vector_fetch_arg(ifspec, "dstport");
  485. memset(&dsthints, 0, sizeof(dsthints));
  486. if (v6)
  487. dsthints.ai_family = AF_INET6;
  488. else
  489. dsthints.ai_family = AF_INET;
  490. switch (id) {
  491. case ID_GRE:
  492. dsthints.ai_socktype = SOCK_RAW;
  493. dsthints.ai_protocol = IPPROTO_GRE;
  494. break;
  495. case ID_L2TPV3:
  496. if (udp) {
  497. dsthints.ai_socktype = SOCK_DGRAM;
  498. dsthints.ai_protocol = 0;
  499. } else {
  500. dsthints.ai_socktype = SOCK_RAW;
  501. dsthints.ai_protocol = IPPROTO_L2TP;
  502. }
  503. break;
  504. default:
  505. printk(KERN_ERR "Unsupported socket type\n");
  506. return NULL;
  507. }
  508. memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
  509. gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
  510. if ((gairet != 0) || (gairesult == NULL)) {
  511. printk(UM_KERN_ERR
  512. "socket_open : could not resolve src, error = %s",
  513. gai_strerror(gairet)
  514. );
  515. return NULL;
  516. }
  517. fd = socket(gairesult->ai_family,
  518. gairesult->ai_socktype, gairesult->ai_protocol);
  519. if (fd == -1) {
  520. printk(UM_KERN_ERR
  521. "socket_open : could not open socket, error = %d",
  522. -errno
  523. );
  524. goto cleanup;
  525. }
  526. if (bind(fd,
  527. (struct sockaddr *) gairesult->ai_addr,
  528. gairesult->ai_addrlen)) {
  529. printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
  530. goto cleanup;
  531. }
  532. if (gairesult != NULL)
  533. freeaddrinfo(gairesult);
  534. gairesult = NULL;
  535. gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
  536. if ((gairet != 0) || (gairesult == NULL)) {
  537. printk(UM_KERN_ERR
  538. "socket_open : could not resolve dst, error = %s",
  539. gai_strerror(gairet)
  540. );
  541. return NULL;
  542. }
  543. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  544. if (result != NULL) {
  545. result->rx_fd = fd;
  546. result->tx_fd = fd;
  547. result->remote_addr = uml_kmalloc(
  548. gairesult->ai_addrlen, UM_GFP_KERNEL);
  549. if (result->remote_addr == NULL)
  550. goto cleanup;
  551. result->remote_addr_size = gairesult->ai_addrlen;
  552. memcpy(
  553. result->remote_addr,
  554. gairesult->ai_addr,
  555. gairesult->ai_addrlen
  556. );
  557. }
  558. freeaddrinfo(gairesult);
  559. return result;
  560. cleanup:
  561. if (gairesult != NULL)
  562. freeaddrinfo(gairesult);
  563. printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
  564. if (fd >= 0)
  565. os_close_file(fd);
  566. if (result != NULL) {
  567. kfree(result->remote_addr);
  568. kfree(result);
  569. }
  570. return NULL;
  571. }
  572. struct vector_fds *uml_vector_user_open(
  573. int unit,
  574. struct arglist *parsed
  575. )
  576. {
  577. char *transport;
  578. if (parsed == NULL) {
  579. printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
  580. return NULL;
  581. }
  582. transport = uml_vector_fetch_arg(parsed, "transport");
  583. if (transport == NULL) {
  584. printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
  585. return NULL;
  586. }
  587. if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
  588. return user_init_raw_fds(parsed);
  589. if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
  590. return user_init_hybrid_fds(parsed);
  591. if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
  592. return user_init_tap_fds(parsed);
  593. if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
  594. return user_init_socket_fds(parsed, ID_GRE);
  595. if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
  596. return user_init_socket_fds(parsed, ID_L2TPV3);
  597. if (strncmp(transport, TRANS_BESS, TRANS_BESS_LEN) == 0)
  598. return user_init_unix_fds(parsed, ID_BESS);
  599. if (strncmp(transport, TRANS_FD, TRANS_FD_LEN) == 0)
  600. return user_init_fd_fds(parsed);
  601. return NULL;
  602. }
  603. int uml_vector_sendmsg(int fd, void *hdr, int flags)
  604. {
  605. int n;
  606. CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
  607. if ((n < 0) && (errno == EAGAIN))
  608. return 0;
  609. if (n >= 0)
  610. return n;
  611. else
  612. return -errno;
  613. }
  614. int uml_vector_recvmsg(int fd, void *hdr, int flags)
  615. {
  616. int n;
  617. struct msghdr *msg = (struct msghdr *) hdr;
  618. CATCH_EINTR(n = readv(fd, msg->msg_iov, msg->msg_iovlen));
  619. if ((n < 0) && (errno == EAGAIN))
  620. return 0;
  621. if (n >= 0)
  622. return n;
  623. else
  624. return -errno;
  625. }
  626. int uml_vector_writev(int fd, void *hdr, int iovcount)
  627. {
  628. int n;
  629. CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
  630. if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
  631. return 0;
  632. if (n >= 0)
  633. return n;
  634. else
  635. return -errno;
  636. }
  637. int uml_vector_sendmmsg(
  638. int fd,
  639. void *msgvec,
  640. unsigned int vlen,
  641. unsigned int flags)
  642. {
  643. int n;
  644. CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
  645. if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
  646. return 0;
  647. if (n >= 0)
  648. return n;
  649. else
  650. return -errno;
  651. }
  652. int uml_vector_recvmmsg(
  653. int fd,
  654. void *msgvec,
  655. unsigned int vlen,
  656. unsigned int flags)
  657. {
  658. int n;
  659. CATCH_EINTR(
  660. n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
  661. if ((n < 0) && (errno == EAGAIN))
  662. return 0;
  663. if (n >= 0)
  664. return n;
  665. else
  666. return -errno;
  667. }
  668. int uml_vector_attach_bpf(int fd, void *bpf)
  669. {
  670. struct sock_fprog *prog = bpf;
  671. int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, sizeof(struct sock_fprog));
  672. if (err < 0)
  673. printk(KERN_ERR BPF_ATTACH_FAIL, prog->len, prog->filter, fd, -errno);
  674. return err;
  675. }
  676. int uml_vector_detach_bpf(int fd, void *bpf)
  677. {
  678. struct sock_fprog *prog = bpf;
  679. int err = setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, bpf, sizeof(struct sock_fprog));
  680. if (err < 0)
  681. printk(KERN_ERR BPF_DETACH_FAIL, prog->len, prog->filter, fd, -errno);
  682. return err;
  683. }
  684. void *uml_vector_default_bpf(const void *mac)
  685. {
  686. struct sock_filter *bpf;
  687. uint32_t *mac1 = (uint32_t *)(mac + 2);
  688. uint16_t *mac2 = (uint16_t *) mac;
  689. struct sock_fprog *bpf_prog;
  690. bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
  691. if (bpf_prog) {
  692. bpf_prog->len = DEFAULT_BPF_LEN;
  693. bpf_prog->filter = NULL;
  694. } else {
  695. return NULL;
  696. }
  697. bpf = uml_kmalloc(
  698. sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
  699. if (bpf) {
  700. bpf_prog->filter = bpf;
  701. /* ld [8] */
  702. bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
  703. /* jeq #0xMAC[2-6] jt 2 jf 5*/
  704. bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
  705. /* ldh [6] */
  706. bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
  707. /* jeq #0xMAC[0-1] jt 4 jf 5 */
  708. bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
  709. /* ret #0 */
  710. bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
  711. /* ret #0x40000 */
  712. bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
  713. } else {
  714. kfree(bpf_prog);
  715. bpf_prog = NULL;
  716. }
  717. return bpf_prog;
  718. }
  719. /* Note - this function requires a valid mac being passed as an arg */
  720. void *uml_vector_user_bpf(char *filename)
  721. {
  722. struct sock_filter *bpf;
  723. struct sock_fprog *bpf_prog;
  724. struct stat statbuf;
  725. int res, ffd = -1;
  726. if (filename == NULL)
  727. return NULL;
  728. if (stat(filename, &statbuf) < 0) {
  729. printk(KERN_ERR "Error %d reading bpf file", -errno);
  730. return false;
  731. }
  732. bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
  733. if (bpf_prog == NULL) {
  734. printk(KERN_ERR "Failed to allocate bpf prog buffer");
  735. return NULL;
  736. }
  737. bpf_prog->len = statbuf.st_size / sizeof(struct sock_filter);
  738. bpf_prog->filter = NULL;
  739. ffd = os_open_file(filename, of_read(OPENFLAGS()), 0);
  740. if (ffd < 0) {
  741. printk(KERN_ERR "Error %d opening bpf file", -errno);
  742. goto bpf_failed;
  743. }
  744. bpf = uml_kmalloc(statbuf.st_size, UM_GFP_KERNEL);
  745. if (bpf == NULL) {
  746. printk(KERN_ERR "Failed to allocate bpf buffer");
  747. goto bpf_failed;
  748. }
  749. bpf_prog->filter = bpf;
  750. res = os_read_file(ffd, bpf, statbuf.st_size);
  751. if (res < statbuf.st_size) {
  752. printk(KERN_ERR "Failed to read bpf program %s, error %d", filename, res);
  753. kfree(bpf);
  754. goto bpf_failed;
  755. }
  756. os_close_file(ffd);
  757. return bpf_prog;
  758. bpf_failed:
  759. if (ffd > 0)
  760. os_close_file(ffd);
  761. kfree(bpf_prog);
  762. return NULL;
  763. }