vector_user.c 13 KB

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