1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
12 #include <sys/ioctl.h>
14 #include <linux/if_tun.h>
15 #include <arpa/inet.h>
16 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <net/ethernet.h>
22 #include <netinet/ip.h>
23 #include <netinet/ether.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_packet.h>
28 #include <linux/virtio_net.h>
33 #include <um_malloc.h>
34 #include "vector_user.h"
41 #define TOKEN_IFNAME "ifname"
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 /* This is very ugly and brute force lookup, but it is done
59 * only once at initialization so not worth doing hashes or
60 * anything more intelligent
63 char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
67 for (i = 0; i < ifspec->numargs; i++) {
68 if (strcmp(ifspec->tokens[i], token) == 0)
69 return ifspec->values[i];
75 struct arglist *uml_parse_vector_ifspec(char *arg)
77 struct arglist *result;
79 bool parsing_token = true, next_starts = true;
83 result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
88 for (pos = 0; pos < len; pos++) {
91 result->tokens[result->numargs] = arg + pos;
93 result->values[result->numargs] = arg + pos;
98 if (*(arg + pos) == '=') {
100 parsing_token = false;
104 (*(arg + pos)) = '\0';
106 if (*(arg + pos) == ',') {
107 parsing_token = true;
109 (*(arg + pos)) = '\0';
114 printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
120 * Socket/FD configuration functions. These return an structure
121 * of rx and tx descriptors to cover cases where these are not
122 * the same (f.e. read via raw socket and write via tap).
125 #define PATH_NET_TUN "/dev/net/tun"
128 static int create_tap_fd(char *iface)
132 int err = -ENOMEM, offload;
134 fd = open(PATH_NET_TUN, O_RDWR);
136 printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
139 memset(&ifr, 0, sizeof(ifr));
140 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
141 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
143 err = ioctl(fd, TUNSETIFF, (void *) &ifr);
145 printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
149 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
150 ioctl(fd, TUNSETOFFLOAD, offload);
158 static int create_raw_fd(char *iface, int flags, int proto)
162 struct sockaddr_ll sock;
165 fd = socket(AF_PACKET, SOCK_RAW, flags);
170 memset(&ifr, 0, sizeof(ifr));
171 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
172 if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
177 sock.sll_family = AF_PACKET;
178 sock.sll_protocol = htons(proto);
179 sock.sll_ifindex = ifr.ifr_ifindex;
182 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
188 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
194 static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
198 struct vector_fds *result = NULL;
200 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
202 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
206 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
207 if (result == NULL) {
208 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
213 result->remote_addr = NULL;
214 result->remote_addr_size = 0;
218 fd = create_tap_fd(iface);
220 printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n");
227 printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd);
232 static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
235 struct vector_fds *result = NULL;
237 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
239 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
243 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
244 if (result == NULL) {
245 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
250 result->remote_addr = NULL;
251 result->remote_addr_size = 0;
255 result->tx_fd = create_tap_fd(iface);
256 if (result->tx_fd < 0) {
257 printk(UM_KERN_ERR "uml_tap: failed to create tun interface: %i\n", result->tx_fd);
263 result->rx_fd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
264 if (result->rx_fd == -1) {
266 "uml_tap: failed to create paired raw socket: %i\n", result->rx_fd);
271 printk(UM_KERN_ERR "user_init_hybrid: init failed");
276 static struct vector_fds *user_init_unix_fds(struct arglist *ifspec, int id)
281 struct vector_fds *result = NULL;
282 struct sockaddr_un *local_addr = NULL, *remote_addr = NULL;
284 src = uml_vector_fetch_arg(ifspec, "src");
285 dst = uml_vector_fetch_arg(ifspec, "dst");
286 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
287 if (result == NULL) {
288 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
291 remote_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
292 if (remote_addr == NULL) {
293 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
299 socktype = SOCK_SEQPACKET;
300 if ((src != NULL) && (strlen(src) <= MAX_UN_LEN)) {
301 local_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
302 if (local_addr == NULL) {
303 printk(UM_KERN_ERR "bess open:cannot allocate local addr");
306 local_addr->sun_family = AF_UNIX;
307 memcpy(local_addr->sun_path, src, strlen(src) + 1);
309 if ((dst == NULL) || (strlen(dst) > MAX_UN_LEN))
311 remote_addr->sun_family = AF_UNIX;
312 memcpy(remote_addr->sun_path, dst, strlen(dst) + 1);
315 printk(KERN_ERR "Unsupported unix socket type\n");
319 fd = socket(AF_UNIX, socktype, 0);
322 "unix open: could not open socket, error = %d",
327 if (local_addr != NULL) {
328 if (bind(fd, (struct sockaddr *) local_addr, sizeof(struct sockaddr_un))) {
329 printk(UM_KERN_ERR UNIX_BIND_FAIL, errno);
335 if (connect(fd, remote_addr, sizeof(struct sockaddr_un)) < 0) {
336 printk(UM_KERN_ERR "bess open:cannot connect to %s %i", remote_addr->sun_path, -errno);
343 result->remote_addr_size = sizeof(struct sockaddr_un);
344 result->remote_addr = remote_addr;
354 static int strtofd(const char *nptr)
363 fd = strtol(nptr, &endptr, 10);
364 if (nptr == endptr ||
374 static struct vector_fds *user_init_fd_fds(struct arglist *ifspec)
378 struct vector_fds *result = NULL;
380 fdarg = uml_vector_fetch_arg(ifspec, "fd");
383 printk(UM_KERN_ERR "fd open: bad or missing fd argument");
387 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
388 if (result == NULL) {
389 printk(UM_KERN_ERR "fd open: allocation failed");
395 result->remote_addr_size = 0;
396 result->remote_addr = NULL;
407 static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
409 int rxfd = -1, txfd = -1;
412 struct vector_fds *result = NULL;
414 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
418 rxfd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
423 txfd = create_raw_fd(iface, 0, ETH_P_IP); /* Turn off RX on this fd */
428 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
429 if (result != NULL) {
430 result->rx_fd = rxfd;
431 result->tx_fd = txfd;
432 result->remote_addr = NULL;
433 result->remote_addr_size = 0;
437 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
443 bool uml_raw_enable_qdisc_bypass(int fd)
448 SOL_PACKET, PACKET_QDISC_BYPASS,
449 &optval, sizeof(optval)) != 0) {
455 bool uml_raw_enable_vnet_headers(int fd)
460 SOL_PACKET, PACKET_VNET_HDR,
461 &optval, sizeof(optval)) != 0) {
462 printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
467 bool uml_tap_enable_vnet_headers(int fd)
469 unsigned int features;
470 int len = sizeof(struct virtio_net_hdr);
472 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
473 printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
476 if ((features & IFF_VNET_HDR) == 0) {
477 printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
480 ioctl(fd, TUNSETVNETHDRSZ, &len);
484 static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
488 struct addrinfo srchints;
489 struct addrinfo dsthints;
492 char *src, *dst, *srcport, *dstport;
493 struct addrinfo *gairesult = NULL;
494 struct vector_fds *result = NULL;
497 value = uml_vector_fetch_arg(ifspec, "v6");
501 if (strtol((const char *) value, NULL, 10) > 0)
505 value = uml_vector_fetch_arg(ifspec, "udp");
507 if (strtol((const char *) value, NULL, 10) > 0)
510 src = uml_vector_fetch_arg(ifspec, "src");
511 dst = uml_vector_fetch_arg(ifspec, "dst");
512 srcport = uml_vector_fetch_arg(ifspec, "srcport");
513 dstport = uml_vector_fetch_arg(ifspec, "dstport");
515 memset(&dsthints, 0, sizeof(dsthints));
518 dsthints.ai_family = AF_INET6;
520 dsthints.ai_family = AF_INET;
524 dsthints.ai_socktype = SOCK_RAW;
525 dsthints.ai_protocol = IPPROTO_GRE;
529 dsthints.ai_socktype = SOCK_DGRAM;
530 dsthints.ai_protocol = 0;
532 dsthints.ai_socktype = SOCK_RAW;
533 dsthints.ai_protocol = IPPROTO_L2TP;
537 printk(KERN_ERR "Unsupported socket type\n");
540 memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
542 gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
543 if ((gairet != 0) || (gairesult == NULL)) {
545 "socket_open : could not resolve src, error = %s",
550 fd = socket(gairesult->ai_family,
551 gairesult->ai_socktype, gairesult->ai_protocol);
554 "socket_open : could not open socket, error = %d",
560 (struct sockaddr *) gairesult->ai_addr,
561 gairesult->ai_addrlen)) {
562 printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
566 if (gairesult != NULL)
567 freeaddrinfo(gairesult);
571 gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
572 if ((gairet != 0) || (gairesult == NULL)) {
574 "socket_open : could not resolve dst, error = %s",
580 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
581 if (result != NULL) {
584 result->remote_addr = uml_kmalloc(
585 gairesult->ai_addrlen, UM_GFP_KERNEL);
586 if (result->remote_addr == NULL)
588 result->remote_addr_size = gairesult->ai_addrlen;
592 gairesult->ai_addrlen
595 freeaddrinfo(gairesult);
598 if (gairesult != NULL)
599 freeaddrinfo(gairesult);
600 printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
603 if (result != NULL) {
604 kfree(result->remote_addr);
610 struct vector_fds *uml_vector_user_open(
612 struct arglist *parsed
617 if (parsed == NULL) {
618 printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
621 transport = uml_vector_fetch_arg(parsed, "transport");
622 if (transport == NULL) {
623 printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
626 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
627 return user_init_raw_fds(parsed);
628 if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
629 return user_init_hybrid_fds(parsed);
630 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
631 return user_init_tap_fds(parsed);
632 if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
633 return user_init_socket_fds(parsed, ID_GRE);
634 if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
635 return user_init_socket_fds(parsed, ID_L2TPV3);
636 if (strncmp(transport, TRANS_BESS, TRANS_BESS_LEN) == 0)
637 return user_init_unix_fds(parsed, ID_BESS);
638 if (strncmp(transport, TRANS_FD, TRANS_FD_LEN) == 0)
639 return user_init_fd_fds(parsed);
644 int uml_vector_sendmsg(int fd, void *hdr, int flags)
648 CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
649 if ((n < 0) && (errno == EAGAIN))
657 int uml_vector_recvmsg(int fd, void *hdr, int flags)
660 struct msghdr *msg = (struct msghdr *) hdr;
662 CATCH_EINTR(n = readv(fd, msg->msg_iov, msg->msg_iovlen));
663 if ((n < 0) && (errno == EAGAIN))
671 int uml_vector_writev(int fd, void *hdr, int iovcount)
675 CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
676 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
684 int uml_vector_sendmmsg(
692 CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
693 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
701 int uml_vector_recvmmsg(
710 n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
711 if ((n < 0) && (errno == EAGAIN))
718 int uml_vector_attach_bpf(int fd, void *bpf)
720 struct sock_fprog *prog = bpf;
722 int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, sizeof(struct sock_fprog));
725 printk(KERN_ERR BPF_ATTACH_FAIL, prog->len, prog->filter, fd, -errno);
729 int uml_vector_detach_bpf(int fd, void *bpf)
731 struct sock_fprog *prog = bpf;
733 int err = setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, bpf, sizeof(struct sock_fprog));
735 printk(KERN_ERR BPF_DETACH_FAIL, prog->len, prog->filter, fd, -errno);
738 void *uml_vector_default_bpf(void *mac)
740 struct sock_filter *bpf;
741 uint32_t *mac1 = (uint32_t *)(mac + 2);
742 uint16_t *mac2 = (uint16_t *) mac;
743 struct sock_fprog *bpf_prog;
745 bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
747 bpf_prog->len = DEFAULT_BPF_LEN;
748 bpf_prog->filter = NULL;
753 sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
755 bpf_prog->filter = bpf;
757 bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
758 /* jeq #0xMAC[2-6] jt 2 jf 5*/
759 bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
761 bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
762 /* jeq #0xMAC[0-1] jt 4 jf 5 */
763 bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
765 bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
767 bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
775 /* Note - this function requires a valid mac being passed as an arg */
777 void *uml_vector_user_bpf(char *filename)
779 struct sock_filter *bpf;
780 struct sock_fprog *bpf_prog;
784 if (filename == NULL)
787 if (stat(filename, &statbuf) < 0) {
788 printk(KERN_ERR "Error %d reading bpf file", -errno);
791 bpf_prog = uml_kmalloc(sizeof(struct sock_fprog), UM_GFP_KERNEL);
792 if (bpf_prog != NULL) {
793 bpf_prog->len = statbuf.st_size / sizeof(struct sock_filter);
794 bpf_prog->filter = NULL;
796 ffd = os_open_file(filename, of_read(OPENFLAGS()), 0);
798 printk(KERN_ERR "Error %d opening bpf file", -errno);
801 bpf = uml_kmalloc(statbuf.st_size, UM_GFP_KERNEL);
803 printk(KERN_ERR "Failed to allocate bpf buffer");
806 bpf_prog->filter = bpf;
807 res = os_read_file(ffd, bpf, statbuf.st_size);
808 if (res < statbuf.st_size) {
809 printk(KERN_ERR "Failed to read bpf program %s, error %d", filename, res);