vector_user.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. if (result != NULL)
  238. kfree(result);
  239. return NULL;
  240. }
  241. bool uml_raw_enable_qdisc_bypass(int fd)
  242. {
  243. int optval = 1;
  244. if (setsockopt(fd,
  245. SOL_PACKET, PACKET_QDISC_BYPASS,
  246. &optval, sizeof(optval)) != 0) {
  247. return false;
  248. }
  249. return true;
  250. }
  251. bool uml_raw_enable_vnet_headers(int fd)
  252. {
  253. int optval = 1;
  254. if (setsockopt(fd,
  255. SOL_PACKET, PACKET_VNET_HDR,
  256. &optval, sizeof(optval)) != 0) {
  257. printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
  258. return false;
  259. }
  260. return true;
  261. }
  262. bool uml_tap_enable_vnet_headers(int fd)
  263. {
  264. unsigned int features;
  265. int len = sizeof(struct virtio_net_hdr);
  266. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  267. printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
  268. return false;
  269. }
  270. if ((features & IFF_VNET_HDR) == 0) {
  271. printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
  272. return false;
  273. }
  274. ioctl(fd, TUNSETVNETHDRSZ, &len);
  275. return true;
  276. }
  277. static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
  278. {
  279. int err = -ENOMEM;
  280. int fd = -1, gairet;
  281. struct addrinfo srchints;
  282. struct addrinfo dsthints;
  283. bool v6, udp;
  284. char *value;
  285. char *src, *dst, *srcport, *dstport;
  286. struct addrinfo *gairesult = NULL;
  287. struct vector_fds *result = NULL;
  288. value = uml_vector_fetch_arg(ifspec, "v6");
  289. v6 = false;
  290. udp = false;
  291. if (value != NULL) {
  292. if (strtol((const char *) value, NULL, 10) > 0)
  293. v6 = true;
  294. }
  295. value = uml_vector_fetch_arg(ifspec, "udp");
  296. if (value != NULL) {
  297. if (strtol((const char *) value, NULL, 10) > 0)
  298. udp = true;
  299. }
  300. src = uml_vector_fetch_arg(ifspec, "src");
  301. dst = uml_vector_fetch_arg(ifspec, "dst");
  302. srcport = uml_vector_fetch_arg(ifspec, "srcport");
  303. dstport = uml_vector_fetch_arg(ifspec, "dstport");
  304. memset(&dsthints, 0, sizeof(dsthints));
  305. if (v6)
  306. dsthints.ai_family = AF_INET6;
  307. else
  308. dsthints.ai_family = AF_INET;
  309. switch (id) {
  310. case ID_GRE:
  311. dsthints.ai_socktype = SOCK_RAW;
  312. dsthints.ai_protocol = IPPROTO_GRE;
  313. break;
  314. case ID_L2TPV3:
  315. if (udp) {
  316. dsthints.ai_socktype = SOCK_DGRAM;
  317. dsthints.ai_protocol = 0;
  318. } else {
  319. dsthints.ai_socktype = SOCK_RAW;
  320. dsthints.ai_protocol = IPPROTO_L2TP;
  321. }
  322. break;
  323. default:
  324. printk(KERN_ERR "Unsupported socket type\n");
  325. return NULL;
  326. }
  327. memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
  328. gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
  329. if ((gairet != 0) || (gairesult == NULL)) {
  330. printk(UM_KERN_ERR
  331. "socket_open : could not resolve src, error = %s",
  332. gai_strerror(gairet)
  333. );
  334. return NULL;
  335. }
  336. fd = socket(gairesult->ai_family,
  337. gairesult->ai_socktype, gairesult->ai_protocol);
  338. if (fd == -1) {
  339. printk(UM_KERN_ERR
  340. "socket_open : could not open socket, error = %d",
  341. -errno
  342. );
  343. goto cleanup;
  344. }
  345. if (bind(fd,
  346. (struct sockaddr *) gairesult->ai_addr,
  347. gairesult->ai_addrlen)) {
  348. printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
  349. goto cleanup;
  350. }
  351. if (gairesult != NULL)
  352. freeaddrinfo(gairesult);
  353. gairesult = NULL;
  354. gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
  355. if ((gairet != 0) || (gairesult == NULL)) {
  356. printk(UM_KERN_ERR
  357. "socket_open : could not resolve dst, error = %s",
  358. gai_strerror(gairet)
  359. );
  360. return NULL;
  361. }
  362. result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
  363. if (result != NULL) {
  364. result->rx_fd = fd;
  365. result->tx_fd = fd;
  366. result->remote_addr = uml_kmalloc(
  367. gairesult->ai_addrlen, UM_GFP_KERNEL);
  368. if (result->remote_addr == NULL)
  369. goto cleanup;
  370. result->remote_addr_size = gairesult->ai_addrlen;
  371. memcpy(
  372. result->remote_addr,
  373. gairesult->ai_addr,
  374. gairesult->ai_addrlen
  375. );
  376. }
  377. freeaddrinfo(gairesult);
  378. return result;
  379. cleanup:
  380. if (gairesult != NULL)
  381. freeaddrinfo(gairesult);
  382. printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
  383. if (fd >= 0)
  384. os_close_file(fd);
  385. if (result != NULL) {
  386. if (result->remote_addr != NULL)
  387. kfree(result->remote_addr);
  388. kfree(result);
  389. }
  390. return NULL;
  391. }
  392. struct vector_fds *uml_vector_user_open(
  393. int unit,
  394. struct arglist *parsed
  395. )
  396. {
  397. char *transport;
  398. if (parsed == NULL) {
  399. printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
  400. return NULL;
  401. }
  402. transport = uml_vector_fetch_arg(parsed, "transport");
  403. if (transport == NULL) {
  404. printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
  405. return NULL;
  406. }
  407. if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
  408. return user_init_raw_fds(parsed);
  409. if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
  410. return user_init_tap_fds(parsed);
  411. if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
  412. return user_init_socket_fds(parsed, ID_GRE);
  413. if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
  414. return user_init_socket_fds(parsed, ID_L2TPV3);
  415. return NULL;
  416. }
  417. int uml_vector_sendmsg(int fd, void *hdr, int flags)
  418. {
  419. int n;
  420. CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
  421. if ((n < 0) && (errno == EAGAIN))
  422. return 0;
  423. if (n >= 0)
  424. return n;
  425. else
  426. return -errno;
  427. }
  428. int uml_vector_recvmsg(int fd, void *hdr, int flags)
  429. {
  430. int n;
  431. CATCH_EINTR(n = recvmsg(fd, (struct msghdr *) hdr, flags));
  432. if ((n < 0) && (errno == EAGAIN))
  433. return 0;
  434. if (n >= 0)
  435. return n;
  436. else
  437. return -errno;
  438. }
  439. int uml_vector_writev(int fd, void *hdr, int iovcount)
  440. {
  441. int n;
  442. CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
  443. if ((n < 0) && (errno == EAGAIN))
  444. return 0;
  445. if (n >= 0)
  446. return n;
  447. else
  448. return -errno;
  449. }
  450. int uml_vector_sendmmsg(
  451. int fd,
  452. void *msgvec,
  453. unsigned int vlen,
  454. unsigned int flags)
  455. {
  456. int n;
  457. CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
  458. if ((n < 0) && (errno == EAGAIN))
  459. return 0;
  460. if (n >= 0)
  461. return n;
  462. else
  463. return -errno;
  464. }
  465. int uml_vector_recvmmsg(
  466. int fd,
  467. void *msgvec,
  468. unsigned int vlen,
  469. unsigned int flags)
  470. {
  471. int n;
  472. CATCH_EINTR(
  473. n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
  474. if ((n < 0) && (errno == EAGAIN))
  475. return 0;
  476. if (n >= 0)
  477. return n;
  478. else
  479. return -errno;
  480. }
  481. int uml_vector_attach_bpf(int fd, void *bpf, int bpf_len)
  482. {
  483. int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, bpf_len);
  484. if (err < 0)
  485. printk(KERN_ERR BPF_ATTACH_FAIL, bpf_len, fd, -errno);
  486. return err;
  487. }
  488. #define DEFAULT_BPF_LEN 6
  489. void *uml_vector_default_bpf(int fd, void *mac)
  490. {
  491. struct sock_filter *bpf;
  492. uint32_t *mac1 = (uint32_t *)(mac + 2);
  493. uint16_t *mac2 = (uint16_t *) mac;
  494. struct sock_fprog bpf_prog = {
  495. .len = 6,
  496. .filter = NULL,
  497. };
  498. bpf = uml_kmalloc(
  499. sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
  500. if (bpf != NULL) {
  501. bpf_prog.filter = bpf;
  502. /* ld [8] */
  503. bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
  504. /* jeq #0xMAC[2-6] jt 2 jf 5*/
  505. bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
  506. /* ldh [6] */
  507. bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
  508. /* jeq #0xMAC[0-1] jt 4 jf 5 */
  509. bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
  510. /* ret #0 */
  511. bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
  512. /* ret #0x40000 */
  513. bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
  514. if (uml_vector_attach_bpf(
  515. fd, &bpf_prog, sizeof(struct sock_fprog)) < 0) {
  516. kfree(bpf);
  517. bpf = NULL;
  518. }
  519. }
  520. return bpf;
  521. }