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>
32 #include <um_malloc.h>
33 #include "vector_user.h"
40 #define TOKEN_IFNAME "ifname"
42 #define TRANS_RAW "raw"
43 #define TRANS_RAW_LEN strlen(TRANS_RAW)
45 #define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
46 #define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
47 #define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
48 #define UNIX_BIND_FAIL "unix_open : could not bind socket err=%i"
49 #define BPF_ATTACH_FAIL "Failed to attach filter size %d to %d, err %d\n"
51 #define MAX_UN_LEN 107
53 /* This is very ugly and brute force lookup, but it is done
54 * only once at initialization so not worth doing hashes or
55 * anything more intelligent
58 char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
62 for (i = 0; i < ifspec->numargs; i++) {
63 if (strcmp(ifspec->tokens[i], token) == 0)
64 return ifspec->values[i];
70 struct arglist *uml_parse_vector_ifspec(char *arg)
72 struct arglist *result;
74 bool parsing_token = true, next_starts = true;
78 result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
83 for (pos = 0; pos < len; pos++) {
86 result->tokens[result->numargs] = arg + pos;
88 result->values[result->numargs] = arg + pos;
93 if (*(arg + pos) == '=') {
95 parsing_token = false;
99 (*(arg + pos)) = '\0';
101 if (*(arg + pos) == ',') {
102 parsing_token = true;
104 (*(arg + pos)) = '\0';
109 printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
115 * Socket/FD configuration functions. These return an structure
116 * of rx and tx descriptors to cover cases where these are not
117 * the same (f.e. read via raw socket and write via tap).
120 #define PATH_NET_TUN "/dev/net/tun"
123 static int create_tap_fd(char *iface)
127 int err = -ENOMEM, offload;
129 fd = open(PATH_NET_TUN, O_RDWR);
131 printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
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);
138 err = ioctl(fd, TUNSETIFF, (void *) &ifr);
140 printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
144 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
145 ioctl(fd, TUNSETOFFLOAD, offload);
153 static int create_raw_fd(char *iface, int flags, int proto)
157 struct sockaddr_ll sock;
160 fd = socket(AF_PACKET, SOCK_RAW, flags);
165 memset(&ifr, 0, sizeof(ifr));
166 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
167 if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
172 sock.sll_family = AF_PACKET;
173 sock.sll_protocol = htons(proto);
174 sock.sll_ifindex = ifr.ifr_ifindex;
177 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
183 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
189 static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
193 struct vector_fds *result = NULL;
195 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
197 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
201 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
202 if (result == NULL) {
203 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
208 result->remote_addr = NULL;
209 result->remote_addr_size = 0;
213 fd = create_tap_fd(iface);
215 printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n");
222 printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd);
228 static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
231 struct vector_fds *result = NULL;
233 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
235 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
239 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
240 if (result == NULL) {
241 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
246 result->remote_addr = NULL;
247 result->remote_addr_size = 0;
251 result->tx_fd = create_tap_fd(iface);
252 if (result->tx_fd < 0) {
253 printk(UM_KERN_ERR "uml_tap: failed to create tun interface: %i\n", result->tx_fd);
259 result->rx_fd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
260 if (result->rx_fd == -1) {
262 "uml_tap: failed to create paired raw socket: %i\n", result->rx_fd);
267 printk(UM_KERN_ERR "user_init_hybrid: init failed");
273 static struct vector_fds *user_init_unix_fds(struct arglist *ifspec, int id)
278 struct vector_fds *result = NULL;
279 struct sockaddr_un *local_addr = NULL, *remote_addr = NULL;
281 src = uml_vector_fetch_arg(ifspec, "src");
282 dst = uml_vector_fetch_arg(ifspec, "dst");
283 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
284 if (result == NULL) {
285 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
288 remote_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
289 if (remote_addr == NULL) {
290 printk(UM_KERN_ERR "unix open:cannot allocate remote addr");
296 socktype = SOCK_SEQPACKET;
297 if ((src != NULL) && (strlen(src) <= MAX_UN_LEN)) {
298 local_addr = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL);
299 if (local_addr == NULL) {
300 printk(UM_KERN_ERR "bess open:cannot allocate local addr");
303 local_addr->sun_family = AF_UNIX;
304 memcpy(local_addr->sun_path, src, strlen(src) + 1);
306 if ((dst == NULL) || (strlen(dst) > MAX_UN_LEN))
308 remote_addr->sun_family = AF_UNIX;
309 memcpy(remote_addr->sun_path, dst, strlen(dst) + 1);
312 printk(KERN_ERR "Unsupported unix socket type\n");
316 fd = socket(AF_UNIX, socktype, 0);
319 "unix open: could not open socket, error = %d",
324 if (local_addr != NULL) {
325 if (bind(fd, (struct sockaddr *) local_addr, sizeof(struct sockaddr_un))) {
326 printk(UM_KERN_ERR UNIX_BIND_FAIL, errno);
332 if (connect(fd, remote_addr, sizeof(struct sockaddr_un)) < 0) {
333 printk(UM_KERN_ERR "bess open:cannot connect to %s %i", remote_addr->sun_path, -errno);
340 result->remote_addr_size = sizeof(struct sockaddr_un);
341 result->remote_addr = remote_addr;
346 if (remote_addr != NULL)
353 static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
355 int rxfd = -1, txfd = -1;
358 struct vector_fds *result = NULL;
360 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
364 rxfd = create_raw_fd(iface, ETH_P_ALL, ETH_P_ALL);
369 txfd = create_raw_fd(iface, 0, ETH_P_IP); /* Turn off RX on this fd */
374 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
375 if (result != NULL) {
376 result->rx_fd = rxfd;
377 result->tx_fd = txfd;
378 result->remote_addr = NULL;
379 result->remote_addr_size = 0;
383 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
390 bool uml_raw_enable_qdisc_bypass(int fd)
395 SOL_PACKET, PACKET_QDISC_BYPASS,
396 &optval, sizeof(optval)) != 0) {
402 bool uml_raw_enable_vnet_headers(int fd)
407 SOL_PACKET, PACKET_VNET_HDR,
408 &optval, sizeof(optval)) != 0) {
409 printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
414 bool uml_tap_enable_vnet_headers(int fd)
416 unsigned int features;
417 int len = sizeof(struct virtio_net_hdr);
419 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
420 printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
423 if ((features & IFF_VNET_HDR) == 0) {
424 printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
427 ioctl(fd, TUNSETVNETHDRSZ, &len);
431 static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
435 struct addrinfo srchints;
436 struct addrinfo dsthints;
439 char *src, *dst, *srcport, *dstport;
440 struct addrinfo *gairesult = NULL;
441 struct vector_fds *result = NULL;
444 value = uml_vector_fetch_arg(ifspec, "v6");
448 if (strtol((const char *) value, NULL, 10) > 0)
452 value = uml_vector_fetch_arg(ifspec, "udp");
454 if (strtol((const char *) value, NULL, 10) > 0)
457 src = uml_vector_fetch_arg(ifspec, "src");
458 dst = uml_vector_fetch_arg(ifspec, "dst");
459 srcport = uml_vector_fetch_arg(ifspec, "srcport");
460 dstport = uml_vector_fetch_arg(ifspec, "dstport");
462 memset(&dsthints, 0, sizeof(dsthints));
465 dsthints.ai_family = AF_INET6;
467 dsthints.ai_family = AF_INET;
471 dsthints.ai_socktype = SOCK_RAW;
472 dsthints.ai_protocol = IPPROTO_GRE;
476 dsthints.ai_socktype = SOCK_DGRAM;
477 dsthints.ai_protocol = 0;
479 dsthints.ai_socktype = SOCK_RAW;
480 dsthints.ai_protocol = IPPROTO_L2TP;
484 printk(KERN_ERR "Unsupported socket type\n");
487 memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
489 gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
490 if ((gairet != 0) || (gairesult == NULL)) {
492 "socket_open : could not resolve src, error = %s",
497 fd = socket(gairesult->ai_family,
498 gairesult->ai_socktype, gairesult->ai_protocol);
501 "socket_open : could not open socket, error = %d",
507 (struct sockaddr *) gairesult->ai_addr,
508 gairesult->ai_addrlen)) {
509 printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
513 if (gairesult != NULL)
514 freeaddrinfo(gairesult);
518 gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
519 if ((gairet != 0) || (gairesult == NULL)) {
521 "socket_open : could not resolve dst, error = %s",
527 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
528 if (result != NULL) {
531 result->remote_addr = uml_kmalloc(
532 gairesult->ai_addrlen, UM_GFP_KERNEL);
533 if (result->remote_addr == NULL)
535 result->remote_addr_size = gairesult->ai_addrlen;
539 gairesult->ai_addrlen
542 freeaddrinfo(gairesult);
545 if (gairesult != NULL)
546 freeaddrinfo(gairesult);
547 printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
550 if (result != NULL) {
551 kfree(result->remote_addr);
557 struct vector_fds *uml_vector_user_open(
559 struct arglist *parsed
564 if (parsed == NULL) {
565 printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
568 transport = uml_vector_fetch_arg(parsed, "transport");
569 if (transport == NULL) {
570 printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
573 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
574 return user_init_raw_fds(parsed);
575 if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
576 return user_init_hybrid_fds(parsed);
577 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
578 return user_init_tap_fds(parsed);
579 if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
580 return user_init_socket_fds(parsed, ID_GRE);
581 if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
582 return user_init_socket_fds(parsed, ID_L2TPV3);
583 if (strncmp(transport, TRANS_BESS, TRANS_BESS_LEN) == 0)
584 return user_init_unix_fds(parsed, ID_BESS);
589 int uml_vector_sendmsg(int fd, void *hdr, int flags)
593 CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
594 if ((n < 0) && (errno == EAGAIN))
602 int uml_vector_recvmsg(int fd, void *hdr, int flags)
605 struct msghdr *msg = (struct msghdr *) hdr;
607 CATCH_EINTR(n = readv(fd, msg->msg_iov, msg->msg_iovlen));
608 if ((n < 0) && (errno == EAGAIN))
616 int uml_vector_writev(int fd, void *hdr, int iovcount)
620 CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
621 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
629 int uml_vector_sendmmsg(
637 CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
638 if ((n < 0) && ((errno == EAGAIN) || (errno == ENOBUFS)))
646 int uml_vector_recvmmsg(
655 n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
656 if ((n < 0) && (errno == EAGAIN))
663 int uml_vector_attach_bpf(int fd, void *bpf, int bpf_len)
665 int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, bpf_len);
668 printk(KERN_ERR BPF_ATTACH_FAIL, bpf_len, fd, -errno);
672 #define DEFAULT_BPF_LEN 6
674 void *uml_vector_default_bpf(int fd, void *mac)
676 struct sock_filter *bpf;
677 uint32_t *mac1 = (uint32_t *)(mac + 2);
678 uint16_t *mac2 = (uint16_t *) mac;
679 struct sock_fprog bpf_prog = {
685 sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
687 bpf_prog.filter = bpf;
689 bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
690 /* jeq #0xMAC[2-6] jt 2 jf 5*/
691 bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
693 bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
694 /* jeq #0xMAC[0-1] jt 4 jf 5 */
695 bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
697 bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
699 bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
700 if (uml_vector_attach_bpf(
701 fd, &bpf_prog, sizeof(struct sock_fprog)) < 0) {