1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
13 #include <sys/ioctl.h>
15 #include <linux/if_tun.h>
16 #include <arpa/inet.h>
17 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <netinet/ip.h>
23 #include <linux/if_ether.h>
24 #include <linux/if_packet.h>
27 #include <linux/virtio_net.h>
32 #include <um_malloc.h>
33 #include "vector_user.h"
40 #define TOKEN_IFNAME "ifname"
41 #define TOKEN_SCRIPT "ifup"
43 #define TRANS_RAW "raw"
44 #define TRANS_RAW_LEN strlen(TRANS_RAW)
47 #define TRANS_FD_LEN strlen(TRANS_FD)
49 #define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
50 #define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
51 #define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
52 #define UNIX_BIND_FAIL "unix_open : could not bind socket err=%i"
53 #define BPF_ATTACH_FAIL "Failed to attach filter size %d prog %px to %d, err %d\n"
54 #define BPF_DETACH_FAIL "Failed to detach filter size %d prog %px to %d, err %d\n"
56 #define MAX_UN_LEN 107
58 static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
59 static const char *template = "tapXXXXXX";
61 /* This is very ugly and brute force lookup, but it is done
62 * only once at initialization so not worth doing hashes or
63 * anything more intelligent
66 char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
70 for (i = 0; i < ifspec->numargs; i++) {
71 if (strcmp(ifspec->tokens[i], token) == 0)
72 return ifspec->values[i];
78 struct arglist *uml_parse_vector_ifspec(char *arg)
80 struct arglist *result;
82 bool parsing_token = true, next_starts = true;
86 result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
91 for (pos = 0; pos < len; pos++) {
94 result->tokens[result->numargs] = arg + pos;
96 result->values[result->numargs] = arg + pos;
101 if (*(arg + pos) == '=') {
103 parsing_token = false;
107 (*(arg + pos)) = '\0';
109 if (*(arg + pos) == ',') {
110 parsing_token = true;
112 (*(arg + pos)) = '\0';
117 printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
123 * Socket/FD configuration functions. These return an structure
124 * of rx and tx descriptors to cover cases where these are not
125 * the same (f.e. read via raw socket and write via tap).
128 #define PATH_NET_TUN "/dev/net/tun"
131 static int create_tap_fd(char *iface)
135 int err = -ENOMEM, offload;
137 fd = open(PATH_NET_TUN, O_RDWR);
139 printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
142 memset(&ifr, 0, sizeof(ifr));
143 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
144 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
146 err = ioctl(fd, TUNSETIFF, (void *) &ifr);
148 printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
152 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
153 ioctl(fd, TUNSETOFFLOAD, offload);
161 static int create_raw_fd(char *iface, int flags, int proto)
165 struct sockaddr_ll sock;
168 fd = socket(AF_PACKET, SOCK_RAW, flags);
173 memset(&ifr, 0, sizeof(ifr));
174 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
175 if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
180 sock.sll_family = AF_PACKET;
181 sock.sll_protocol = htons(proto);
182 sock.sll_ifindex = ifr.ifr_ifindex;
185 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
191 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
198 static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
202 struct vector_fds *result = NULL;
203 bool dynamic = false;
204 char dynamic_ifname[IFNAMSIZ];
205 char *argv[] = {NULL, NULL, NULL, NULL};
207 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
210 iface = dynamic_ifname;
214 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
215 if (result == NULL) {
216 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
221 result->remote_addr = NULL;
222 result->remote_addr_size = 0;
227 strcpy(iface, template);
228 for (i = 0; i < strlen(iface); i++) {
229 if (iface[i] == 'X') {
230 iface[i] = padchar[rand() % strlen(padchar)];
234 fd = create_tap_fd(iface);
235 if ((fd < 0) && (!dynamic)) {
236 printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n");
243 argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
246 run_helper(NULL, NULL, argv);
251 printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd);
256 static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
259 struct vector_fds *result = NULL;
260 char *argv[] = {NULL, NULL, NULL, NULL};
262 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
264 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
268 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
269 if (result == NULL) {
270 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
275 result->remote_addr = NULL;
276 result->remote_addr_size = 0;
280 result->tx_fd = create_tap_fd(iface);
281 if (result->tx_fd < 0) {
282 printk(UM_KERN_ERR "uml_tap: failed to create tun interface: %i\n", result->tx_fd);
288 result->rx_fd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
289 if (result->rx_fd == -1) {
291 "uml_tap: failed to create paired raw socket: %i\n", result->rx_fd);
295 argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
298 run_helper(NULL, NULL, argv);
302 printk(UM_KERN_ERR "user_init_hybrid: init failed");
307 static struct vector_fds *user_init_unix_fds(struct arglist *ifspec, int id)
312 struct vector_fds *result = NULL;
313 struct sockaddr_un *local_addr = NULL, *remote_addr = NULL;
315 src = uml_vector_fetch_arg(ifspec, "src");
316 dst = uml_vector_fetch_arg(ifspec, "dst");
317 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
318 if (result == NULL) {
319 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
322 remote_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
323 if (remote_addr == NULL) {
324 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
330 socktype = SOCK_SEQPACKET;
331 if ((src != NULL) && (strlen(src) <= MAX_UN_LEN)) {
332 local_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
333 if (local_addr == NULL) {
334 printk(UM_KERN_ERR "bess open:cannot allocate local addr");
337 local_addr->sun_family = AF_UNIX;
338 memcpy(local_addr->sun_path, src, strlen(src) + 1);
340 if ((dst == NULL) || (strlen(dst) > MAX_UN_LEN))
342 remote_addr->sun_family = AF_UNIX;
343 memcpy(remote_addr->sun_path, dst, strlen(dst) + 1);
346 printk(KERN_ERR "Unsupported unix socket type\n");
350 fd = socket(AF_UNIX, socktype, 0);
353 "unix open: could not open socket, error = %d",
358 if (local_addr != NULL) {
359 if (bind(fd, (struct sockaddr *) local_addr, sizeof(struct sockaddr_un))) {
360 printk(UM_KERN_ERR UNIX_BIND_FAIL, errno);
366 if (connect(fd, (const struct sockaddr *) remote_addr, sizeof(struct sockaddr_un)) < 0) {
367 printk(UM_KERN_ERR "bess open:cannot connect to %s %i", remote_addr->sun_path, -errno);
374 result->remote_addr_size = sizeof(struct sockaddr_un);
375 result->remote_addr = remote_addr;
385 static int strtofd(const char *nptr)
394 fd = strtol(nptr, &endptr, 10);
395 if (nptr == endptr ||
405 static struct vector_fds *user_init_fd_fds(struct arglist *ifspec)
409 struct vector_fds *result = NULL;
411 fdarg = uml_vector_fetch_arg(ifspec, "fd");
414 printk(UM_KERN_ERR "fd open: bad or missing fd argument");
418 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
419 if (result == NULL) {
420 printk(UM_KERN_ERR "fd open: allocation failed");
426 result->remote_addr_size = 0;
427 result->remote_addr = NULL;
437 static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
439 int rxfd = -1, txfd = -1;
442 struct vector_fds *result = NULL;
443 char *argv[] = {NULL, NULL, NULL, NULL};
445 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
449 rxfd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
454 txfd = create_raw_fd(iface, 0, ETH_P_IP); /* Turn off RX on this fd */
459 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
460 if (result != NULL) {
461 result->rx_fd = rxfd;
462 result->tx_fd = txfd;
463 result->remote_addr = NULL;
464 result->remote_addr_size = 0;
466 argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
469 run_helper(NULL, NULL, argv);
473 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
479 bool uml_raw_enable_qdisc_bypass(int fd)
484 SOL_PACKET, PACKET_QDISC_BYPASS,
485 &optval, sizeof(optval)) != 0) {
491 bool uml_raw_enable_vnet_headers(int fd)
496 SOL_PACKET, PACKET_VNET_HDR,
497 &optval, sizeof(optval)) != 0) {
498 printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
503 bool uml_tap_enable_vnet_headers(int fd)
505 unsigned int features;
506 int len = sizeof(struct virtio_net_hdr);
508 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
509 printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
512 if ((features & IFF_VNET_HDR) == 0) {
513 printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
516 ioctl(fd, TUNSETVNETHDRSZ, &len);
520 static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
524 struct addrinfo srchints;
525 struct addrinfo dsthints;
528 char *src, *dst, *srcport, *dstport;
529 struct addrinfo *gairesult = NULL;
530 struct vector_fds *result = NULL;
533 value = uml_vector_fetch_arg(ifspec, "v6");
537 if (strtol((const char *) value, NULL, 10) > 0)
541 value = uml_vector_fetch_arg(ifspec, "udp");
543 if (strtol((const char *) value, NULL, 10) > 0)
546 src = uml_vector_fetch_arg(ifspec, "src");
547 dst = uml_vector_fetch_arg(ifspec, "dst");
548 srcport = uml_vector_fetch_arg(ifspec, "srcport");
549 dstport = uml_vector_fetch_arg(ifspec, "dstport");
551 memset(&dsthints, 0, sizeof(dsthints));
554 dsthints.ai_family = AF_INET6;
556 dsthints.ai_family = AF_INET;
560 dsthints.ai_socktype = SOCK_RAW;
561 dsthints.ai_protocol = IPPROTO_GRE;
565 dsthints.ai_socktype = SOCK_DGRAM;
566 dsthints.ai_protocol = 0;
568 dsthints.ai_socktype = SOCK_RAW;
569 dsthints.ai_protocol = IPPROTO_L2TP;
573 printk(KERN_ERR "Unsupported socket type\n");
576 memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
578 gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
579 if ((gairet != 0) || (gairesult == NULL)) {
581 "socket_open : could not resolve src, error = %s",
586 fd = socket(gairesult->ai_family,
587 gairesult->ai_socktype, gairesult->ai_protocol);
590 "socket_open : could not open socket, error = %d",
596 (struct sockaddr *) gairesult->ai_addr,
597 gairesult->ai_addrlen)) {
598 printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
602 if (gairesult != NULL)
603 freeaddrinfo(gairesult);
607 gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
608 if ((gairet != 0) || (gairesult == NULL)) {
610 "socket_open : could not resolve dst, error = %s",
616 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
617 if (result != NULL) {
620 result->remote_addr = uml_kmalloc(
621 gairesult->ai_addrlen, UM_GFP_KERNEL);
622 if (result->remote_addr == NULL)
624 result->remote_addr_size = gairesult->ai_addrlen;
628 gairesult->ai_addrlen
631 freeaddrinfo(gairesult);
634 if (gairesult != NULL)
635 freeaddrinfo(gairesult);
636 printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
639 if (result != NULL) {
640 kfree(result->remote_addr);
646 struct vector_fds *uml_vector_user_open(
648 struct arglist *parsed
653 if (parsed == NULL) {
654 printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
657 transport = uml_vector_fetch_arg(parsed, "transport");
658 if (transport == NULL) {
659 printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
662 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
663 return user_init_raw_fds(parsed);
664 if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
665 return user_init_hybrid_fds(parsed);
666 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
667 return user_init_tap_fds(parsed);
668 if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
669 return user_init_socket_fds(parsed, ID_GRE);
670 if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
671 return user_init_socket_fds(parsed, ID_L2TPV3);
672 if (strncmp(transport, TRANS_BESS, TRANS_BESS_LEN) == 0)
673 return user_init_unix_fds(parsed, ID_BESS);
674 if (strncmp(transport, TRANS_FD, TRANS_FD_LEN) == 0)
675 return user_init_fd_fds(parsed);
680 int uml_vector_sendmsg(int fd, void *hdr, int flags)
684 CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
685 if ((n < 0) && (errno == EAGAIN))
693 int uml_vector_recvmsg(int fd, void *hdr, int flags)
696 struct msghdr *msg = (struct msghdr *) hdr;
698 CATCH_EINTR(n = readv(fd, msg->msg_iov, msg->msg_iovlen));
699 if ((n < 0) && (errno == EAGAIN))
707 int uml_vector_writev(int fd, void *hdr, int iovcount)
711 CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
712 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
720 int uml_vector_sendmmsg(
728 CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
729 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
737 int uml_vector_recvmmsg(
746 n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
747 if ((n < 0) && (errno == EAGAIN))
754 int uml_vector_attach_bpf(int fd, void *bpf)
756 struct sock_fprog *prog = bpf;
758 int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, sizeof(struct sock_fprog));
761 printk(KERN_ERR BPF_ATTACH_FAIL, prog->len, prog->filter, fd, -errno);
765 int uml_vector_detach_bpf(int fd, void *bpf)
767 struct sock_fprog *prog = bpf;
769 int err = setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, bpf, sizeof(struct sock_fprog));
771 printk(KERN_ERR BPF_DETACH_FAIL, prog->len, prog->filter, fd, -errno);
774 void *uml_vector_default_bpf(void *mac)
776 struct sock_filter *bpf;
777 uint32_t *mac1 = (uint32_t *)(mac + 2);
778 uint16_t *mac2 = (uint16_t *) mac;
779 struct sock_fprog *bpf_prog;
781 bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
783 bpf_prog->len = DEFAULT_BPF_LEN;
784 bpf_prog->filter = NULL;
789 sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
791 bpf_prog->filter = bpf;
793 bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
794 /* jeq #0xMAC[2-6] jt 2 jf 5*/
795 bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
797 bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
798 /* jeq #0xMAC[0-1] jt 4 jf 5 */
799 bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
801 bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
803 bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
811 /* Note - this function requires a valid mac being passed as an arg */
813 void *uml_vector_user_bpf(char *filename)
815 struct sock_filter *bpf;
816 struct sock_fprog *bpf_prog;
820 if (filename == NULL)
823 if (stat(filename, &statbuf) < 0) {
824 printk(KERN_ERR "Error %d reading bpf file", -errno);
827 bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
828 if (bpf_prog == NULL) {
829 printk(KERN_ERR "Failed to allocate bpf prog buffer");
832 bpf_prog->len = statbuf.st_size / sizeof(struct sock_filter);
833 bpf_prog->filter = NULL;
834 ffd = os_open_file(filename, of_read(OPENFLAGS()), 0);
836 printk(KERN_ERR "Error %d opening bpf file", -errno);
839 bpf = uml_kmalloc(statbuf.st_size, UM_GFP_KERNEL);
841 printk(KERN_ERR "Failed to allocate bpf buffer");
844 bpf_prog->filter = bpf;
845 res = os_read_file(ffd, bpf, statbuf.st_size);
846 if (res < statbuf.st_size) {
847 printk(KERN_ERR "Failed to read bpf program %s, error %d", filename, res);