vector_user.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  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/types.h>
  19. #include <sys/socket.h>
  20. #include <net/ethernet.h>
  21. #include <netinet/ip.h>
  22. #include <netinet/ether.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if_packet.h>
  25. #include <sys/socket.h>
  26. #include <sys/wait.h>
  27. #include <linux/virtio_net.h>
  28. #include <netdb.h>
  29. #include <stdlib.h>
  30. #include <os.h>
  31. #include <um_malloc.h>
  32. #include "vector_user.h"
  33. #define ID_GRE 0
  34. #define ID_L2TPV3 1
  35. #define ID_MAX 1
  36. #define TOKEN_IFNAME "ifname"
  37. #define TRANS_RAW "raw"
  38. #define TRANS_RAW_LEN strlen(TRANS_RAW)
  39. #define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
  40. #define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
  41. #define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
  42. #define BPF_ATTACH_FAIL "Failed to attach filter size %d to %d, err %d\n"
  43. /* This is very ugly and brute force lookup, but it is done
  44. * only once at initialization so not worth doing hashes or
  45. * anything more intelligent
  46. */
  47. char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
  48. {
  49. int i;
  50. for (i = 0; i < ifspec->numargs; i++) {
  51. if (strcmp(ifspec->tokens[i], token) == 0)
  52. return ifspec->values[i];
  53. }
  54. return NULL;
  55. }
  56. struct arglist *uml_parse_vector_ifspec(char *arg)
  57. {
  58. struct arglist *result;
  59. int pos, len;
  60. bool parsing_token = true, next_starts = true;
  61. if (arg == NULL)
  62. return NULL;
  63. result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
  64. if (result == NULL)
  65. return NULL;
  66. result->numargs = 0;
  67. len = strlen(arg);
  68. for (pos = 0; pos < len; pos++) {
  69. if (next_starts) {
  70. if (parsing_token) {
  71. result->tokens[result->numargs] = arg + pos;
  72. } else {
  73. result->values[result->numargs] = arg + pos;
  74. result->numargs++;
  75. }
  76. next_starts = false;
  77. }
  78. if (*(arg + pos) == '=') {
  79. if (parsing_token)
  80. parsing_token = false;
  81. else
  82. goto cleanup;
  83. next_starts = true;
  84. (*(arg + pos)) = '\0';
  85. }
  86. if (*(arg + pos) == ',') {
  87. parsing_token = true;
  88. next_starts = true;
  89. (*(arg + pos)) = '\0';
  90. }
  91. }
  92. return result;
  93. cleanup:
  94. printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
  95. kfree(result);
  96. return NULL;
  97. }
  98. /*
  99. * Socket/FD configuration functions. These return an structure
  100. * of rx and tx descriptors to cover cases where these are not
  101. * the same (f.e. read via raw socket and write via tap).
  102. */
  103. #define PATH_NET_TUN "/dev/net/tun"
  104. static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
  105. {
  106. struct ifreq ifr;
  107. int fd = -1;
  108. struct sockaddr_ll sock;
  109. int err = -ENOMEM, offload;
  110. char *iface;
  111. struct vector_fds *result = NULL;
  112. iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
  113. if (iface == NULL) {
  114. printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
  115. goto tap_cleanup;
  116. }
  117. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  118. if (result == NULL) {
  119. printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
  120. goto tap_cleanup;
  121. }
  122. result->rx_fd = -1;
  123. result->tx_fd = -1;
  124. result->remote_addr = NULL;
  125. result->remote_addr_size = 0;
  126. /* TAP */
  127. fd = open(PATH_NET_TUN, O_RDWR);
  128. if (fd < 0) {
  129. printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
  130. goto tap_cleanup;
  131. }
  132. result->tx_fd = fd;
  133. memset(&ifr, 0, sizeof(ifr));
  134. ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
  135. strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  136. err = ioctl(fd, TUNSETIFF, (void *) &ifr);
  137. if (err != 0) {
  138. printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
  139. goto tap_cleanup;
  140. }
  141. offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
  142. ioctl(fd, TUNSETOFFLOAD, offload);
  143. /* RAW */
  144. fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  145. if (fd == -1) {
  146. printk(UM_KERN_ERR
  147. "uml_tap: failed to create socket: %i\n", -errno);
  148. goto tap_cleanup;
  149. }
  150. result->rx_fd = fd;
  151. memset(&ifr, 0, sizeof(ifr));
  152. strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  153. if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
  154. printk(UM_KERN_ERR
  155. "uml_tap: failed to set interface: %i\n", -errno);
  156. goto tap_cleanup;
  157. }
  158. sock.sll_family = AF_PACKET;
  159. sock.sll_protocol = htons(ETH_P_ALL);
  160. sock.sll_ifindex = ifr.ifr_ifindex;
  161. if (bind(fd,
  162. (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
  163. printk(UM_KERN_ERR
  164. "user_init_tap: failed to bind raw pair, err %d\n",
  165. -errno);
  166. goto tap_cleanup;
  167. }
  168. return result;
  169. tap_cleanup:
  170. printk(UM_KERN_ERR "user_init_tap: init failed, error %d", err);
  171. if (result != NULL) {
  172. if (result->rx_fd >= 0)
  173. os_close_file(result->rx_fd);
  174. if (result->tx_fd >= 0)
  175. os_close_file(result->tx_fd);
  176. kfree(result);
  177. }
  178. return NULL;
  179. }
  180. static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
  181. {
  182. struct ifreq ifr;
  183. int rxfd = -1, txfd = -1;
  184. struct sockaddr_ll sock;
  185. int err = -ENOMEM;
  186. char *iface;
  187. struct vector_fds *result = NULL;
  188. iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
  189. if (iface == NULL)
  190. goto cleanup;
  191. rxfd = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
  192. if (rxfd == -1) {
  193. err = -errno;
  194. goto cleanup;
  195. }
  196. txfd = socket(AF_PACKET, SOCK_RAW, 0); /* Turn off RX on this fd */
  197. if (txfd == -1) {
  198. err = -errno;
  199. goto cleanup;
  200. }
  201. memset(&ifr, 0, sizeof(ifr));
  202. strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  203. if (ioctl(rxfd, SIOCGIFINDEX, (void *) &ifr) < 0) {
  204. err = -errno;
  205. goto cleanup;
  206. }
  207. sock.sll_family = AF_PACKET;
  208. sock.sll_protocol = htons(ETH_P_ALL);
  209. sock.sll_ifindex = ifr.ifr_ifindex;
  210. if (bind(rxfd,
  211. (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
  212. err = -errno;
  213. goto cleanup;
  214. }
  215. sock.sll_family = AF_PACKET;
  216. sock.sll_protocol = htons(ETH_P_IP);
  217. sock.sll_ifindex = ifr.ifr_ifindex;
  218. if (bind(txfd,
  219. (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
  220. err = -errno;
  221. goto cleanup;
  222. }
  223. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  224. if (result != NULL) {
  225. result->rx_fd = rxfd;
  226. result->tx_fd = txfd;
  227. result->remote_addr = NULL;
  228. result->remote_addr_size = 0;
  229. }
  230. return result;
  231. cleanup:
  232. printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
  233. if (rxfd >= 0)
  234. os_close_file(rxfd);
  235. if (txfd >= 0)
  236. os_close_file(txfd);
  237. kfree(result);
  238. return NULL;
  239. }
  240. bool uml_raw_enable_qdisc_bypass(int fd)
  241. {
  242. int optval = 1;
  243. if (setsockopt(fd,
  244. SOL_PACKET, PACKET_QDISC_BYPASS,
  245. &optval, sizeof(optval)) != 0) {
  246. return false;
  247. }
  248. return true;
  249. }
  250. bool uml_raw_enable_vnet_headers(int fd)
  251. {
  252. int optval = 1;
  253. if (setsockopt(fd,
  254. SOL_PACKET, PACKET_VNET_HDR,
  255. &optval, sizeof(optval)) != 0) {
  256. printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
  257. return false;
  258. }
  259. return true;
  260. }
  261. bool uml_tap_enable_vnet_headers(int fd)
  262. {
  263. unsigned int features;
  264. int len = sizeof(struct virtio_net_hdr);
  265. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  266. printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
  267. return false;
  268. }
  269. if ((features & IFF_VNET_HDR) == 0) {
  270. printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
  271. return false;
  272. }
  273. ioctl(fd, TUNSETVNETHDRSZ, &len);
  274. return true;
  275. }
  276. static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
  277. {
  278. int err = -ENOMEM;
  279. int fd = -1, gairet;
  280. struct addrinfo srchints;
  281. struct addrinfo dsthints;
  282. bool v6, udp;
  283. char *value;
  284. char *src, *dst, *srcport, *dstport;
  285. struct addrinfo *gairesult = NULL;
  286. struct vector_fds *result = NULL;
  287. value = uml_vector_fetch_arg(ifspec, "v6");
  288. v6 = false;
  289. udp = false;
  290. if (value != NULL) {
  291. if (strtol((const char *) value, NULL, 10) > 0)
  292. v6 = true;
  293. }
  294. value = uml_vector_fetch_arg(ifspec, "udp");
  295. if (value != NULL) {
  296. if (strtol((const char *) value, NULL, 10) > 0)
  297. udp = true;
  298. }
  299. src = uml_vector_fetch_arg(ifspec, "src");
  300. dst = uml_vector_fetch_arg(ifspec, "dst");
  301. srcport = uml_vector_fetch_arg(ifspec, "srcport");
  302. dstport = uml_vector_fetch_arg(ifspec, "dstport");
  303. memset(&dsthints, 0, sizeof(dsthints));
  304. if (v6)
  305. dsthints.ai_family = AF_INET6;
  306. else
  307. dsthints.ai_family = AF_INET;
  308. switch (id) {
  309. case ID_GRE:
  310. dsthints.ai_socktype = SOCK_RAW;
  311. dsthints.ai_protocol = IPPROTO_GRE;
  312. break;
  313. case ID_L2TPV3:
  314. if (udp) {
  315. dsthints.ai_socktype = SOCK_DGRAM;
  316. dsthints.ai_protocol = 0;
  317. } else {
  318. dsthints.ai_socktype = SOCK_RAW;
  319. dsthints.ai_protocol = IPPROTO_L2TP;
  320. }
  321. break;
  322. default:
  323. printk(KERN_ERR "Unsupported socket type\n");
  324. return NULL;
  325. }
  326. memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
  327. gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
  328. if ((gairet != 0) || (gairesult == NULL)) {
  329. printk(UM_KERN_ERR
  330. "socket_open : could not resolve src, error = %s",
  331. gai_strerror(gairet)
  332. );
  333. return NULL;
  334. }
  335. fd = socket(gairesult->ai_family,
  336. gairesult->ai_socktype, gairesult->ai_protocol);
  337. if (fd == -1) {
  338. printk(UM_KERN_ERR
  339. "socket_open : could not open socket, error = %d",
  340. -errno
  341. );
  342. goto cleanup;
  343. }
  344. if (bind(fd,
  345. (struct sockaddr *) gairesult->ai_addr,
  346. gairesult->ai_addrlen)) {
  347. printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
  348. goto cleanup;
  349. }
  350. if (gairesult != NULL)
  351. freeaddrinfo(gairesult);
  352. gairesult = NULL;
  353. gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
  354. if ((gairet != 0) || (gairesult == NULL)) {
  355. printk(UM_KERN_ERR
  356. "socket_open : could not resolve dst, error = %s",
  357. gai_strerror(gairet)
  358. );
  359. return NULL;
  360. }
  361. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  362. if (result != NULL) {
  363. result->rx_fd = fd;
  364. result->tx_fd = fd;
  365. result->remote_addr = uml_kmalloc(
  366. gairesult->ai_addrlen, UM_GFP_KERNEL);
  367. if (result->remote_addr == NULL)
  368. goto cleanup;
  369. result->remote_addr_size = gairesult->ai_addrlen;
  370. memcpy(
  371. result->remote_addr,
  372. gairesult->ai_addr,
  373. gairesult->ai_addrlen
  374. );
  375. }
  376. freeaddrinfo(gairesult);
  377. return result;
  378. cleanup:
  379. if (gairesult != NULL)
  380. freeaddrinfo(gairesult);
  381. printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
  382. if (fd >= 0)
  383. os_close_file(fd);
  384. if (result != NULL) {
  385. kfree(result->remote_addr);
  386. kfree(result);
  387. }
  388. return NULL;
  389. }
  390. struct vector_fds *uml_vector_user_open(
  391. int unit,
  392. struct arglist *parsed
  393. )
  394. {
  395. char *transport;
  396. if (parsed == NULL) {
  397. printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
  398. return NULL;
  399. }
  400. transport = uml_vector_fetch_arg(parsed, "transport");
  401. if (transport == NULL) {
  402. printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
  403. return NULL;
  404. }
  405. if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
  406. return user_init_raw_fds(parsed);
  407. if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
  408. return user_init_tap_fds(parsed);
  409. if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
  410. return user_init_socket_fds(parsed, ID_GRE);
  411. if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
  412. return user_init_socket_fds(parsed, ID_L2TPV3);
  413. return NULL;
  414. }
  415. int uml_vector_sendmsg(int fd, void *hdr, int flags)
  416. {
  417. int n;
  418. CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
  419. if ((n < 0) && (errno == EAGAIN))
  420. return 0;
  421. if (n >= 0)
  422. return n;
  423. else
  424. return -errno;
  425. }
  426. int uml_vector_recvmsg(int fd, void *hdr, int flags)
  427. {
  428. int n;
  429. CATCH_EINTR(n = recvmsg(fd, (struct msghdr *) hdr, flags));
  430. if ((n < 0) && (errno == EAGAIN))
  431. return 0;
  432. if (n >= 0)
  433. return n;
  434. else
  435. return -errno;
  436. }
  437. int uml_vector_writev(int fd, void *hdr, int iovcount)
  438. {
  439. int n;
  440. CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
  441. if ((n < 0) && (errno == EAGAIN))
  442. return 0;
  443. if (n >= 0)
  444. return n;
  445. else
  446. return -errno;
  447. }
  448. int uml_vector_sendmmsg(
  449. int fd,
  450. void *msgvec,
  451. unsigned int vlen,
  452. unsigned int flags)
  453. {
  454. int n;
  455. CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
  456. if ((n < 0) && (errno == EAGAIN))
  457. return 0;
  458. if (n >= 0)
  459. return n;
  460. else
  461. return -errno;
  462. }
  463. int uml_vector_recvmmsg(
  464. int fd,
  465. void *msgvec,
  466. unsigned int vlen,
  467. unsigned int flags)
  468. {
  469. int n;
  470. CATCH_EINTR(
  471. n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
  472. if ((n < 0) && (errno == EAGAIN))
  473. return 0;
  474. if (n >= 0)
  475. return n;
  476. else
  477. return -errno;
  478. }
  479. int uml_vector_attach_bpf(int fd, void *bpf, int bpf_len)
  480. {
  481. int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, bpf_len);
  482. if (err < 0)
  483. printk(KERN_ERR BPF_ATTACH_FAIL, bpf_len, fd, -errno);
  484. return err;
  485. }
  486. #define DEFAULT_BPF_LEN 6
  487. void *uml_vector_default_bpf(int fd, void *mac)
  488. {
  489. struct sock_filter *bpf;
  490. uint32_t *mac1 = (uint32_t *)(mac + 2);
  491. uint16_t *mac2 = (uint16_t *) mac;
  492. struct sock_fprog bpf_prog = {
  493. .len = 6,
  494. .filter = NULL,
  495. };
  496. bpf = uml_kmalloc(
  497. sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
  498. if (bpf != NULL) {
  499. bpf_prog.filter = bpf;
  500. /* ld [8] */
  501. bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
  502. /* jeq #0xMAC[2-6] jt 2 jf 5*/
  503. bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
  504. /* ldh [6] */
  505. bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
  506. /* jeq #0xMAC[0-1] jt 4 jf 5 */
  507. bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
  508. /* ret #0 */
  509. bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
  510. /* ret #0x40000 */
  511. bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
  512. if (uml_vector_attach_bpf(
  513. fd, &bpf_prog, sizeof(struct sock_fprog)) < 0) {
  514. kfree(bpf);
  515. bpf = NULL;
  516. }
  517. }
  518. return bpf;
  519. }