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 <netinet/ip.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_packet.h>
26 #include <linux/virtio_net.h>
31 #include <um_malloc.h>
32 #include "vector_user.h"
39 #define TOKEN_IFNAME "ifname"
40 #define TOKEN_SCRIPT "ifup"
42 #define TRANS_RAW "raw"
43 #define TRANS_RAW_LEN strlen(TRANS_RAW)
46 #define TRANS_FD_LEN strlen(TRANS_FD)
48 #define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
49 #define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
50 #define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
51 #define UNIX_BIND_FAIL "unix_open : could not bind socket err=%i"
52 #define BPF_ATTACH_FAIL "Failed to attach filter size %d prog %px to %d, err %d\n"
53 #define BPF_DETACH_FAIL "Failed to detach filter size %d prog %px to %d, err %d\n"
55 #define MAX_UN_LEN 107
57 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
58 static const char *template = "tapXXXXXX";
60 /* This is very ugly and brute force lookup, but it is done
61 * only once at initialization so not worth doing hashes or
62 * anything more intelligent
65 char *uml_vector_fetch_arg(struct arglist
*ifspec
, char *token
)
69 for (i
= 0; i
< ifspec
->numargs
; i
++) {
70 if (strcmp(ifspec
->tokens
[i
], token
) == 0)
71 return ifspec
->values
[i
];
77 struct arglist
*uml_parse_vector_ifspec(char *arg
)
79 struct arglist
*result
;
81 bool parsing_token
= true, next_starts
= true;
85 result
= uml_kmalloc(sizeof(struct arglist
), UM_GFP_KERNEL
);
90 for (pos
= 0; pos
< len
; pos
++) {
93 result
->tokens
[result
->numargs
] = arg
+ pos
;
95 result
->values
[result
->numargs
] = arg
+ pos
;
100 if (*(arg
+ pos
) == '=') {
102 parsing_token
= false;
106 (*(arg
+ pos
)) = '\0';
108 if (*(arg
+ pos
) == ',') {
109 parsing_token
= true;
111 (*(arg
+ pos
)) = '\0';
116 printk(UM_KERN_ERR
"vector_setup - Couldn't parse '%s'\n", arg
);
122 * Socket/FD configuration functions. These return an structure
123 * of rx and tx descriptors to cover cases where these are not
124 * the same (f.e. read via raw socket and write via tap).
127 #define PATH_NET_TUN "/dev/net/tun"
130 static int create_tap_fd(char *iface
)
134 int err
= -ENOMEM
, offload
;
136 fd
= open(PATH_NET_TUN
, O_RDWR
);
138 printk(UM_KERN_ERR
"uml_tap: failed to open tun device\n");
141 memset(&ifr
, 0, sizeof(ifr
));
142 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
| IFF_VNET_HDR
;
143 strncpy((char *)&ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
) - 1);
145 err
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
147 printk(UM_KERN_ERR
"uml_tap: failed to select tap interface\n");
151 offload
= TUN_F_CSUM
| TUN_F_TSO4
| TUN_F_TSO6
;
152 ioctl(fd
, TUNSETOFFLOAD
, offload
);
160 static int create_raw_fd(char *iface
, int flags
, int proto
)
164 struct sockaddr_ll sock
;
167 fd
= socket(AF_PACKET
, SOCK_RAW
, flags
);
172 memset(&ifr
, 0, sizeof(ifr
));
173 strncpy((char *)&ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
) - 1);
174 if (ioctl(fd
, SIOCGIFINDEX
, (void *) &ifr
) < 0) {
179 sock
.sll_family
= AF_PACKET
;
180 sock
.sll_protocol
= htons(proto
);
181 sock
.sll_ifindex
= ifr
.ifr_ifindex
;
184 (struct sockaddr
*) &sock
, sizeof(struct sockaddr_ll
)) < 0) {
190 printk(UM_KERN_ERR
"user_init_raw: init failed, error %d", err
);
197 static struct vector_fds
*user_init_tap_fds(struct arglist
*ifspec
)
201 struct vector_fds
*result
= NULL
;
202 bool dynamic
= false;
203 char dynamic_ifname
[IFNAMSIZ
];
204 char *argv
[] = {NULL
, NULL
, NULL
, NULL
};
206 iface
= uml_vector_fetch_arg(ifspec
, TOKEN_IFNAME
);
209 iface
= dynamic_ifname
;
213 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
214 if (result
== NULL
) {
215 printk(UM_KERN_ERR
"uml_tap: failed to allocate file descriptors\n");
220 result
->remote_addr
= NULL
;
221 result
->remote_addr_size
= 0;
226 strcpy(iface
, template);
227 for (i
= 0; i
< strlen(iface
); i
++) {
228 if (iface
[i
] == 'X') {
229 iface
[i
] = padchar
[rand() % strlen(padchar
)];
233 fd
= create_tap_fd(iface
);
234 if ((fd
< 0) && (!dynamic
)) {
235 printk(UM_KERN_ERR
"uml_tap: failed to create tun interface\n");
242 argv
[0] = uml_vector_fetch_arg(ifspec
, TOKEN_SCRIPT
);
245 run_helper(NULL
, NULL
, argv
);
250 printk(UM_KERN_ERR
"user_init_tap: init failed, error %d", fd
);
255 static struct vector_fds
*user_init_hybrid_fds(struct arglist
*ifspec
)
258 struct vector_fds
*result
= NULL
;
259 char *argv
[] = {NULL
, NULL
, NULL
, NULL
};
261 iface
= uml_vector_fetch_arg(ifspec
, TOKEN_IFNAME
);
263 printk(UM_KERN_ERR
"uml_tap: failed to parse interface spec\n");
267 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
268 if (result
== NULL
) {
269 printk(UM_KERN_ERR
"uml_tap: failed to allocate file descriptors\n");
274 result
->remote_addr
= NULL
;
275 result
->remote_addr_size
= 0;
279 result
->tx_fd
= create_tap_fd(iface
);
280 if (result
->tx_fd
< 0) {
281 printk(UM_KERN_ERR
"uml_tap: failed to create tun interface: %i\n", result
->tx_fd
);
287 result
->rx_fd
= create_raw_fd(iface
, ETH_P_ALL
, ETH_P_ALL
);
288 if (result
->rx_fd
== -1) {
290 "uml_tap: failed to create paired raw socket: %i\n", result
->rx_fd
);
294 argv
[0] = uml_vector_fetch_arg(ifspec
, TOKEN_SCRIPT
);
297 run_helper(NULL
, NULL
, argv
);
301 printk(UM_KERN_ERR
"user_init_hybrid: init failed");
306 static struct vector_fds
*user_init_unix_fds(struct arglist
*ifspec
, int id
)
311 struct vector_fds
*result
= NULL
;
312 struct sockaddr_un
*local_addr
= NULL
, *remote_addr
= NULL
;
314 src
= uml_vector_fetch_arg(ifspec
, "src");
315 dst
= uml_vector_fetch_arg(ifspec
, "dst");
316 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
317 if (result
== NULL
) {
318 printk(UM_KERN_ERR
"unix open:cannot allocate remote addr");
321 remote_addr
= uml_kmalloc(sizeof(struct sockaddr_un
), UM_GFP_KERNEL
);
322 if (remote_addr
== NULL
) {
323 printk(UM_KERN_ERR
"unix open:cannot allocate remote addr");
329 socktype
= SOCK_SEQPACKET
;
330 if ((src
!= NULL
) && (strlen(src
) <= MAX_UN_LEN
)) {
331 local_addr
= uml_kmalloc(sizeof(struct sockaddr_un
), UM_GFP_KERNEL
);
332 if (local_addr
== NULL
) {
333 printk(UM_KERN_ERR
"bess open:cannot allocate local addr");
336 local_addr
->sun_family
= AF_UNIX
;
337 memcpy(local_addr
->sun_path
, src
, strlen(src
) + 1);
339 if ((dst
== NULL
) || (strlen(dst
) > MAX_UN_LEN
))
341 remote_addr
->sun_family
= AF_UNIX
;
342 memcpy(remote_addr
->sun_path
, dst
, strlen(dst
) + 1);
345 printk(KERN_ERR
"Unsupported unix socket type\n");
349 fd
= socket(AF_UNIX
, socktype
, 0);
352 "unix open: could not open socket, error = %d",
357 if (local_addr
!= NULL
) {
358 if (bind(fd
, (struct sockaddr
*) local_addr
, sizeof(struct sockaddr_un
))) {
359 printk(UM_KERN_ERR UNIX_BIND_FAIL
, errno
);
365 if (connect(fd
, (const struct sockaddr
*) remote_addr
, sizeof(struct sockaddr_un
)) < 0) {
366 printk(UM_KERN_ERR
"bess open:cannot connect to %s %i", remote_addr
->sun_path
, -errno
);
373 result
->remote_addr_size
= sizeof(struct sockaddr_un
);
374 result
->remote_addr
= remote_addr
;
384 static int strtofd(const char *nptr
)
393 fd
= strtol(nptr
, &endptr
, 10);
394 if (nptr
== endptr
||
404 static struct vector_fds
*user_init_fd_fds(struct arglist
*ifspec
)
408 struct vector_fds
*result
= NULL
;
410 fdarg
= uml_vector_fetch_arg(ifspec
, "fd");
413 printk(UM_KERN_ERR
"fd open: bad or missing fd argument");
417 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
418 if (result
== NULL
) {
419 printk(UM_KERN_ERR
"fd open: allocation failed");
425 result
->remote_addr_size
= 0;
426 result
->remote_addr
= NULL
;
436 static struct vector_fds
*user_init_raw_fds(struct arglist
*ifspec
)
438 int rxfd
= -1, txfd
= -1;
441 struct vector_fds
*result
= NULL
;
442 char *argv
[] = {NULL
, NULL
, NULL
, NULL
};
444 iface
= uml_vector_fetch_arg(ifspec
, TOKEN_IFNAME
);
448 rxfd
= create_raw_fd(iface
, ETH_P_ALL
, ETH_P_ALL
);
453 txfd
= create_raw_fd(iface
, 0, ETH_P_IP
); /* Turn off RX on this fd */
458 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
459 if (result
!= NULL
) {
460 result
->rx_fd
= rxfd
;
461 result
->tx_fd
= txfd
;
462 result
->remote_addr
= NULL
;
463 result
->remote_addr_size
= 0;
465 argv
[0] = uml_vector_fetch_arg(ifspec
, TOKEN_SCRIPT
);
468 run_helper(NULL
, NULL
, argv
);
472 printk(UM_KERN_ERR
"user_init_raw: init failed, error %d", err
);
478 bool uml_raw_enable_qdisc_bypass(int fd
)
483 SOL_PACKET
, PACKET_QDISC_BYPASS
,
484 &optval
, sizeof(optval
)) != 0) {
490 bool uml_raw_enable_vnet_headers(int fd
)
495 SOL_PACKET
, PACKET_VNET_HDR
,
496 &optval
, sizeof(optval
)) != 0) {
497 printk(UM_KERN_INFO VNET_HDR_FAIL
, fd
);
502 bool uml_tap_enable_vnet_headers(int fd
)
504 unsigned int features
;
505 int len
= sizeof(struct virtio_net_hdr
);
507 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
508 printk(UM_KERN_INFO TUN_GET_F_FAIL
, strerror(errno
));
511 if ((features
& IFF_VNET_HDR
) == 0) {
512 printk(UM_KERN_INFO
"tapraw: No VNET HEADER support");
515 ioctl(fd
, TUNSETVNETHDRSZ
, &len
);
519 static struct vector_fds
*user_init_socket_fds(struct arglist
*ifspec
, int id
)
523 struct addrinfo srchints
;
524 struct addrinfo dsthints
;
527 char *src
, *dst
, *srcport
, *dstport
;
528 struct addrinfo
*gairesult
= NULL
;
529 struct vector_fds
*result
= NULL
;
532 value
= uml_vector_fetch_arg(ifspec
, "v6");
536 if (strtol((const char *) value
, NULL
, 10) > 0)
540 value
= uml_vector_fetch_arg(ifspec
, "udp");
542 if (strtol((const char *) value
, NULL
, 10) > 0)
545 src
= uml_vector_fetch_arg(ifspec
, "src");
546 dst
= uml_vector_fetch_arg(ifspec
, "dst");
547 srcport
= uml_vector_fetch_arg(ifspec
, "srcport");
548 dstport
= uml_vector_fetch_arg(ifspec
, "dstport");
550 memset(&dsthints
, 0, sizeof(dsthints
));
553 dsthints
.ai_family
= AF_INET6
;
555 dsthints
.ai_family
= AF_INET
;
559 dsthints
.ai_socktype
= SOCK_RAW
;
560 dsthints
.ai_protocol
= IPPROTO_GRE
;
564 dsthints
.ai_socktype
= SOCK_DGRAM
;
565 dsthints
.ai_protocol
= 0;
567 dsthints
.ai_socktype
= SOCK_RAW
;
568 dsthints
.ai_protocol
= IPPROTO_L2TP
;
572 printk(KERN_ERR
"Unsupported socket type\n");
575 memcpy(&srchints
, &dsthints
, sizeof(struct addrinfo
));
577 gairet
= getaddrinfo(src
, srcport
, &dsthints
, &gairesult
);
578 if ((gairet
!= 0) || (gairesult
== NULL
)) {
580 "socket_open : could not resolve src, error = %s",
585 fd
= socket(gairesult
->ai_family
,
586 gairesult
->ai_socktype
, gairesult
->ai_protocol
);
589 "socket_open : could not open socket, error = %d",
595 (struct sockaddr
*) gairesult
->ai_addr
,
596 gairesult
->ai_addrlen
)) {
597 printk(UM_KERN_ERR L2TPV3_BIND_FAIL
, errno
);
601 if (gairesult
!= NULL
)
602 freeaddrinfo(gairesult
);
606 gairet
= getaddrinfo(dst
, dstport
, &dsthints
, &gairesult
);
607 if ((gairet
!= 0) || (gairesult
== NULL
)) {
609 "socket_open : could not resolve dst, error = %s",
615 result
= uml_kmalloc(sizeof(struct vector_fds
), UM_GFP_KERNEL
);
616 if (result
!= NULL
) {
619 result
->remote_addr
= uml_kmalloc(
620 gairesult
->ai_addrlen
, UM_GFP_KERNEL
);
621 if (result
->remote_addr
== NULL
)
623 result
->remote_addr_size
= gairesult
->ai_addrlen
;
627 gairesult
->ai_addrlen
630 freeaddrinfo(gairesult
);
633 if (gairesult
!= NULL
)
634 freeaddrinfo(gairesult
);
635 printk(UM_KERN_ERR
"user_init_socket: init failed, error %d", err
);
638 if (result
!= NULL
) {
639 kfree(result
->remote_addr
);
645 struct vector_fds
*uml_vector_user_open(
647 struct arglist
*parsed
652 if (parsed
== NULL
) {
653 printk(UM_KERN_ERR
"no parsed config for unit %d\n", unit
);
656 transport
= uml_vector_fetch_arg(parsed
, "transport");
657 if (transport
== NULL
) {
658 printk(UM_KERN_ERR
"missing transport for unit %d\n", unit
);
661 if (strncmp(transport
, TRANS_RAW
, TRANS_RAW_LEN
) == 0)
662 return user_init_raw_fds(parsed
);
663 if (strncmp(transport
, TRANS_HYBRID
, TRANS_HYBRID_LEN
) == 0)
664 return user_init_hybrid_fds(parsed
);
665 if (strncmp(transport
, TRANS_TAP
, TRANS_TAP_LEN
) == 0)
666 return user_init_tap_fds(parsed
);
667 if (strncmp(transport
, TRANS_GRE
, TRANS_GRE_LEN
) == 0)
668 return user_init_socket_fds(parsed
, ID_GRE
);
669 if (strncmp(transport
, TRANS_L2TPV3
, TRANS_L2TPV3_LEN
) == 0)
670 return user_init_socket_fds(parsed
, ID_L2TPV3
);
671 if (strncmp(transport
, TRANS_BESS
, TRANS_BESS_LEN
) == 0)
672 return user_init_unix_fds(parsed
, ID_BESS
);
673 if (strncmp(transport
, TRANS_FD
, TRANS_FD_LEN
) == 0)
674 return user_init_fd_fds(parsed
);
679 int uml_vector_sendmsg(int fd
, void *hdr
, int flags
)
683 CATCH_EINTR(n
= sendmsg(fd
, (struct msghdr
*) hdr
, flags
));
684 if ((n
< 0) && (errno
== EAGAIN
))
692 int uml_vector_recvmsg(int fd
, void *hdr
, int flags
)
695 struct msghdr
*msg
= (struct msghdr
*) hdr
;
697 CATCH_EINTR(n
= readv(fd
, msg
->msg_iov
, msg
->msg_iovlen
));
698 if ((n
< 0) && (errno
== EAGAIN
))
706 int uml_vector_writev(int fd
, void *hdr
, int iovcount
)
710 CATCH_EINTR(n
= writev(fd
, (struct iovec
*) hdr
, iovcount
));
711 if ((n
< 0) && ((errno
== EAGAIN
) || (errno
== ENOBUFS
)))
719 int uml_vector_sendmmsg(
727 CATCH_EINTR(n
= sendmmsg(fd
, (struct mmsghdr
*) msgvec
, vlen
, flags
));
728 if ((n
< 0) && ((errno
== EAGAIN
) || (errno
== ENOBUFS
)))
736 int uml_vector_recvmmsg(
745 n
= recvmmsg(fd
, (struct mmsghdr
*) msgvec
, vlen
, flags
, 0));
746 if ((n
< 0) && (errno
== EAGAIN
))
753 int uml_vector_attach_bpf(int fd
, void *bpf
)
755 struct sock_fprog
*prog
= bpf
;
757 int err
= setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, bpf
, sizeof(struct sock_fprog
));
760 printk(KERN_ERR BPF_ATTACH_FAIL
, prog
->len
, prog
->filter
, fd
, -errno
);
764 int uml_vector_detach_bpf(int fd
, void *bpf
)
766 struct sock_fprog
*prog
= bpf
;
768 int err
= setsockopt(fd
, SOL_SOCKET
, SO_DETACH_FILTER
, bpf
, sizeof(struct sock_fprog
));
770 printk(KERN_ERR BPF_DETACH_FAIL
, prog
->len
, prog
->filter
, fd
, -errno
);
773 void *uml_vector_default_bpf(void *mac
)
775 struct sock_filter
*bpf
;
776 uint32_t *mac1
= (uint32_t *)(mac
+ 2);
777 uint16_t *mac2
= (uint16_t *) mac
;
778 struct sock_fprog
*bpf_prog
;
780 bpf_prog
= uml_kmalloc(sizeof(struct sock_fprog
), UM_GFP_KERNEL
);
782 bpf_prog
->len
= DEFAULT_BPF_LEN
;
783 bpf_prog
->filter
= NULL
;
788 sizeof(struct sock_filter
) * DEFAULT_BPF_LEN
, UM_GFP_KERNEL
);
790 bpf_prog
->filter
= bpf
;
792 bpf
[0] = (struct sock_filter
){ 0x20, 0, 0, 0x00000008 };
793 /* jeq #0xMAC[2-6] jt 2 jf 5*/
794 bpf
[1] = (struct sock_filter
){ 0x15, 0, 3, ntohl(*mac1
)};
796 bpf
[2] = (struct sock_filter
){ 0x28, 0, 0, 0x00000006 };
797 /* jeq #0xMAC[0-1] jt 4 jf 5 */
798 bpf
[3] = (struct sock_filter
){ 0x15, 0, 1, ntohs(*mac2
)};
800 bpf
[4] = (struct sock_filter
){ 0x6, 0, 0, 0x00000000 };
802 bpf
[5] = (struct sock_filter
){ 0x6, 0, 0, 0x00040000 };
810 /* Note - this function requires a valid mac being passed as an arg */
812 void *uml_vector_user_bpf(char *filename
)
814 struct sock_filter
*bpf
;
815 struct sock_fprog
*bpf_prog
;
819 if (filename
== NULL
)
822 if (stat(filename
, &statbuf
) < 0) {
823 printk(KERN_ERR
"Error %d reading bpf file", -errno
);
826 bpf_prog
= uml_kmalloc(sizeof(struct sock_fprog
), UM_GFP_KERNEL
);
827 if (bpf_prog
== NULL
) {
828 printk(KERN_ERR
"Failed to allocate bpf prog buffer");
831 bpf_prog
->len
= statbuf
.st_size
/ sizeof(struct sock_filter
);
832 bpf_prog
->filter
= NULL
;
833 ffd
= os_open_file(filename
, of_read(OPENFLAGS()), 0);
835 printk(KERN_ERR
"Error %d opening bpf file", -errno
);
838 bpf
= uml_kmalloc(statbuf
.st_size
, UM_GFP_KERNEL
);
840 printk(KERN_ERR
"Failed to allocate bpf buffer");
843 bpf_prog
->filter
= bpf
;
844 res
= os_read_file(ffd
, bpf
, statbuf
.st_size
);
845 if (res
< statbuf
.st_size
) {
846 printk(KERN_ERR
"Failed to read bpf program %s, error %d", filename
, res
);