2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
12 #include <sys/ioctl.h>
14 #include <linux/if_tun.h>
15 #include <arpa/inet.h>
16 #include <sys/types.h>
19 #include <sys/types.h>
20 #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>
26 #include <sys/socket.h>
28 #include <linux/virtio_net.h>
32 #include <um_malloc.h>
34 #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 BPF_ATTACH_FAIL "Failed to attach filter size %d to %d, err %d\n"
50 /* This is very ugly and brute force lookup, but it is done
51 * only once at initialization so not worth doing hashes or
52 * anything more intelligent
55 char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
59 for (i = 0; i < ifspec->numargs; i++) {
60 if (strcmp(ifspec->tokens[i], token) == 0)
61 return ifspec->values[i];
67 struct arglist *uml_parse_vector_ifspec(char *arg)
69 struct arglist *result;
71 bool parsing_token = true, next_starts = true;
75 result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
80 for (pos = 0; pos < len; pos++) {
83 result->tokens[result->numargs] = arg + pos;
85 result->values[result->numargs] = arg + pos;
90 if (*(arg + pos) == '=') {
92 parsing_token = false;
96 (*(arg + pos)) = '\0';
98 if (*(arg + pos) == ',') {
101 (*(arg + pos)) = '\0';
106 printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
112 * Socket/FD configuration functions. These return an structure
113 * of rx and tx descriptors to cover cases where these are not
114 * the same (f.e. read via raw socket and write via tap).
117 #define PATH_NET_TUN "/dev/net/tun"
119 static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
123 struct sockaddr_ll sock;
124 int err = -ENOMEM, offload;
126 struct vector_fds *result = NULL;
128 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
130 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
134 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
135 if (result == NULL) {
136 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
141 result->remote_addr = NULL;
142 result->remote_addr_size = 0;
146 fd = open(PATH_NET_TUN, O_RDWR);
148 printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
152 memset(&ifr, 0, sizeof(ifr));
153 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
154 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
156 err = ioctl(fd, TUNSETIFF, (void *) &ifr);
158 printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
162 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
163 ioctl(fd, TUNSETOFFLOAD, offload);
167 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
170 "uml_tap: failed to create socket: %i\n", -errno);
174 memset(&ifr, 0, sizeof(ifr));
175 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
176 if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
178 "uml_tap: failed to set interface: %i\n", -errno);
182 sock.sll_family = AF_PACKET;
183 sock.sll_protocol = htons(ETH_P_ALL);
184 sock.sll_ifindex = ifr.ifr_ifindex;
187 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
189 "user_init_tap: failed to bind raw pair, err %d\n",
195 printk(UM_KERN_ERR "user_init_tap: init failed, error %d", err);
196 if (result != NULL) {
197 if (result->rx_fd >= 0)
198 os_close_file(result->rx_fd);
199 if (result->tx_fd >= 0)
200 os_close_file(result->tx_fd);
207 static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
210 int rxfd = -1, txfd = -1;
211 struct sockaddr_ll sock;
214 struct vector_fds *result = NULL;
216 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
220 rxfd = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
225 txfd = socket(AF_PACKET, SOCK_RAW, 0); /* Turn off RX on this fd */
230 memset(&ifr, 0, sizeof(ifr));
231 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
232 if (ioctl(rxfd, SIOCGIFINDEX, (void *) &ifr) < 0) {
237 sock.sll_family = AF_PACKET;
238 sock.sll_protocol = htons(ETH_P_ALL);
239 sock.sll_ifindex = ifr.ifr_ifindex;
242 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
247 sock.sll_family = AF_PACKET;
248 sock.sll_protocol = htons(ETH_P_IP);
249 sock.sll_ifindex = ifr.ifr_ifindex;
252 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
257 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
258 if (result != NULL) {
259 result->rx_fd = rxfd;
260 result->tx_fd = txfd;
261 result->remote_addr = NULL;
262 result->remote_addr_size = 0;
266 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
277 bool uml_raw_enable_qdisc_bypass(int fd)
282 SOL_PACKET, PACKET_QDISC_BYPASS,
283 &optval, sizeof(optval)) != 0) {
289 bool uml_raw_enable_vnet_headers(int fd)
294 SOL_PACKET, PACKET_VNET_HDR,
295 &optval, sizeof(optval)) != 0) {
296 printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
301 bool uml_tap_enable_vnet_headers(int fd)
303 unsigned int features;
304 int len = sizeof(struct virtio_net_hdr);
306 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
307 printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
310 if ((features & IFF_VNET_HDR) == 0) {
311 printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
314 ioctl(fd, TUNSETVNETHDRSZ, &len);
318 static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
322 struct addrinfo srchints;
323 struct addrinfo dsthints;
326 char *src, *dst, *srcport, *dstport;
327 struct addrinfo *gairesult = NULL;
328 struct vector_fds *result = NULL;
331 value = uml_vector_fetch_arg(ifspec, "v6");
335 if (strtol((const char *) value, NULL, 10) > 0)
339 value = uml_vector_fetch_arg(ifspec, "udp");
341 if (strtol((const char *) value, NULL, 10) > 0)
344 src = uml_vector_fetch_arg(ifspec, "src");
345 dst = uml_vector_fetch_arg(ifspec, "dst");
346 srcport = uml_vector_fetch_arg(ifspec, "srcport");
347 dstport = uml_vector_fetch_arg(ifspec, "dstport");
349 memset(&dsthints, 0, sizeof(dsthints));
352 dsthints.ai_family = AF_INET6;
354 dsthints.ai_family = AF_INET;
358 dsthints.ai_socktype = SOCK_RAW;
359 dsthints.ai_protocol = IPPROTO_GRE;
363 dsthints.ai_socktype = SOCK_DGRAM;
364 dsthints.ai_protocol = 0;
366 dsthints.ai_socktype = SOCK_RAW;
367 dsthints.ai_protocol = IPPROTO_L2TP;
371 printk(KERN_ERR "Unsupported socket type\n");
374 memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
376 gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
377 if ((gairet != 0) || (gairesult == NULL)) {
379 "socket_open : could not resolve src, error = %s",
384 fd = socket(gairesult->ai_family,
385 gairesult->ai_socktype, gairesult->ai_protocol);
388 "socket_open : could not open socket, error = %d",
394 (struct sockaddr *) gairesult->ai_addr,
395 gairesult->ai_addrlen)) {
396 printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
400 if (gairesult != NULL)
401 freeaddrinfo(gairesult);
405 gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
406 if ((gairet != 0) || (gairesult == NULL)) {
408 "socket_open : could not resolve dst, error = %s",
414 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
415 if (result != NULL) {
418 result->remote_addr = uml_kmalloc(
419 gairesult->ai_addrlen, UM_GFP_KERNEL);
420 if (result->remote_addr == NULL)
422 result->remote_addr_size = gairesult->ai_addrlen;
426 gairesult->ai_addrlen
429 freeaddrinfo(gairesult);
432 if (gairesult != NULL)
433 freeaddrinfo(gairesult);
434 printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
437 if (result != NULL) {
438 if (result->remote_addr != NULL)
439 kfree(result->remote_addr);
445 struct vector_fds *uml_vector_user_open(
447 struct arglist *parsed
452 if (parsed == NULL) {
453 printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
456 transport = uml_vector_fetch_arg(parsed, "transport");
457 if (transport == NULL) {
458 printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
461 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
462 return user_init_raw_fds(parsed);
463 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
464 return user_init_tap_fds(parsed);
465 if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
466 return user_init_socket_fds(parsed, ID_GRE);
467 if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
468 return user_init_socket_fds(parsed, ID_L2TPV3);
473 int uml_vector_sendmsg(int fd, void *hdr, int flags)
477 CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
478 if ((n < 0) && (errno == EAGAIN))
486 int uml_vector_recvmsg(int fd, void *hdr, int flags)
490 CATCH_EINTR(n = recvmsg(fd, (struct msghdr *) hdr, flags));
491 if ((n < 0) && (errno == EAGAIN))
499 int uml_vector_writev(int fd, void *hdr, int iovcount)
503 CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
504 if ((n < 0) && (errno == EAGAIN))
512 int uml_vector_sendmmsg(
520 CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
521 if ((n < 0) && (errno == EAGAIN))
529 int uml_vector_recvmmsg(
538 n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
539 if ((n < 0) && (errno == EAGAIN))
546 int uml_vector_attach_bpf(int fd, void *bpf, int bpf_len)
548 int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, bpf_len);
551 printk(KERN_ERR BPF_ATTACH_FAIL, bpf_len, fd, -errno);
555 #define DEFAULT_BPF_LEN 6
557 void *uml_vector_default_bpf(int fd, void *mac)
559 struct sock_filter *bpf;
560 uint32_t *mac1 = (uint32_t *)(mac + 2);
561 uint16_t *mac2 = (uint16_t *) mac;
562 struct sock_fprog bpf_prog = {
568 sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
570 bpf_prog.filter = bpf;
572 bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
573 /* jeq #0xMAC[2-6] jt 2 jf 5*/
574 bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
576 bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
577 /* jeq #0xMAC[0-1] jt 4 jf 5 */
578 bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
580 bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
582 bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
583 if (uml_vector_attach_bpf(
584 fd, &bpf_prog, sizeof(struct sock_fprog)) < 0) {