vector_user.c 20 KB

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