Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <stdarg.h>
9#include <errno.h>
10#include <stddef.h>
11#include <string.h>
12#include <sys/ioctl.h>
13#include <net/if.h>
14#include <linux/if_tun.h>
15#include <arpa/inet.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.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>
27#include <sys/wait.h>
28#include <linux/virtio_net.h>
29#include <netdb.h>
30#include <stdlib.h>
31#include <os.h>
32#include <um_malloc.h>
33#include "vector_user.h"
34
35#define ID_GRE 0
36#define ID_L2TPV3 1
37#define ID_MAX 1
38
39#define TOKEN_IFNAME "ifname"
40
41#define TRANS_RAW "raw"
42#define TRANS_RAW_LEN strlen(TRANS_RAW)
43
44#define VNET_HDR_FAIL "could not enable vnet headers on fd %d"
45#define TUN_GET_F_FAIL "tapraw: TUNGETFEATURES failed: %s"
46#define L2TPV3_BIND_FAIL "l2tpv3_open : could not bind socket err=%i"
47#define BPF_ATTACH_FAIL "Failed to attach filter size %d to %d, err %d\n"
48
49/* This is very ugly and brute force lookup, but it is done
50 * only once at initialization so not worth doing hashes or
51 * anything more intelligent
52 */
53
54char *uml_vector_fetch_arg(struct arglist *ifspec, char *token)
55{
56 int i;
57
58 for (i = 0; i < ifspec->numargs; i++) {
59 if (strcmp(ifspec->tokens[i], token) == 0)
60 return ifspec->values[i];
61 }
62 return NULL;
63
64}
65
66struct arglist *uml_parse_vector_ifspec(char *arg)
67{
68 struct arglist *result;
69 int pos, len;
70 bool parsing_token = true, next_starts = true;
71
72 if (arg == NULL)
73 return NULL;
74 result = uml_kmalloc(sizeof(struct arglist), UM_GFP_KERNEL);
75 if (result == NULL)
76 return NULL;
77 result->numargs = 0;
78 len = strlen(arg);
79 for (pos = 0; pos < len; pos++) {
80 if (next_starts) {
81 if (parsing_token) {
82 result->tokens[result->numargs] = arg + pos;
83 } else {
84 result->values[result->numargs] = arg + pos;
85 result->numargs++;
86 }
87 next_starts = false;
88 }
89 if (*(arg + pos) == '=') {
90 if (parsing_token)
91 parsing_token = false;
92 else
93 goto cleanup;
94 next_starts = true;
95 (*(arg + pos)) = '\0';
96 }
97 if (*(arg + pos) == ',') {
98 parsing_token = true;
99 next_starts = true;
100 (*(arg + pos)) = '\0';
101 }
102 }
103 return result;
104cleanup:
105 printk(UM_KERN_ERR "vector_setup - Couldn't parse '%s'\n", arg);
106 kfree(result);
107 return NULL;
108}
109
110/*
111 * Socket/FD configuration functions. These return an structure
112 * of rx and tx descriptors to cover cases where these are not
113 * the same (f.e. read via raw socket and write via tap).
114 */
115
116#define PATH_NET_TUN "/dev/net/tun"
117
118static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
119{
120 struct ifreq ifr;
121 int fd = -1;
122 struct sockaddr_ll sock;
123 int err = -ENOMEM, offload;
124 char *iface;
125 struct vector_fds *result = NULL;
126
127 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
128 if (iface == NULL) {
129 printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n");
130 goto tap_cleanup;
131 }
132
133 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
134 if (result == NULL) {
135 printk(UM_KERN_ERR "uml_tap: failed to allocate file descriptors\n");
136 goto tap_cleanup;
137 }
138 result->rx_fd = -1;
139 result->tx_fd = -1;
140 result->remote_addr = NULL;
141 result->remote_addr_size = 0;
142
143 /* TAP */
144
145 fd = open(PATH_NET_TUN, O_RDWR);
146 if (fd < 0) {
147 printk(UM_KERN_ERR "uml_tap: failed to open tun device\n");
148 goto tap_cleanup;
149 }
150 result->tx_fd = fd;
151 memset(&ifr, 0, sizeof(ifr));
152 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
153 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
154
155 err = ioctl(fd, TUNSETIFF, (void *) &ifr);
156 if (err != 0) {
157 printk(UM_KERN_ERR "uml_tap: failed to select tap interface\n");
158 goto tap_cleanup;
159 }
160
161 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
162 ioctl(fd, TUNSETOFFLOAD, offload);
163
164 /* RAW */
165
166 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
167 if (fd == -1) {
168 printk(UM_KERN_ERR
169 "uml_tap: failed to create socket: %i\n", -errno);
170 goto tap_cleanup;
171 }
172 result->rx_fd = fd;
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) {
176 printk(UM_KERN_ERR
177 "uml_tap: failed to set interface: %i\n", -errno);
178 goto tap_cleanup;
179 }
180
181 sock.sll_family = AF_PACKET;
182 sock.sll_protocol = htons(ETH_P_ALL);
183 sock.sll_ifindex = ifr.ifr_ifindex;
184
185 if (bind(fd,
186 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
187 printk(UM_KERN_ERR
188 "user_init_tap: failed to bind raw pair, err %d\n",
189 -errno);
190 goto tap_cleanup;
191 }
192 return result;
193tap_cleanup:
194 printk(UM_KERN_ERR "user_init_tap: init failed, error %d", err);
195 if (result != NULL) {
196 if (result->rx_fd >= 0)
197 os_close_file(result->rx_fd);
198 if (result->tx_fd >= 0)
199 os_close_file(result->tx_fd);
200 kfree(result);
201 }
202 return NULL;
203}
204
205
206static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
207{
208 struct ifreq ifr;
209 int rxfd = -1, txfd = -1;
210 struct sockaddr_ll sock;
211 int err = -ENOMEM;
212 char *iface;
213 struct vector_fds *result = NULL;
214
215 iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
216 if (iface == NULL)
217 goto cleanup;
218
219 rxfd = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
220 if (rxfd == -1) {
221 err = -errno;
222 goto cleanup;
223 }
224 txfd = socket(AF_PACKET, SOCK_RAW, 0); /* Turn off RX on this fd */
225 if (txfd == -1) {
226 err = -errno;
227 goto cleanup;
228 }
229 memset(&ifr, 0, sizeof(ifr));
230 strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
231 if (ioctl(rxfd, SIOCGIFINDEX, (void *) &ifr) < 0) {
232 err = -errno;
233 goto cleanup;
234 }
235
236 sock.sll_family = AF_PACKET;
237 sock.sll_protocol = htons(ETH_P_ALL);
238 sock.sll_ifindex = ifr.ifr_ifindex;
239
240 if (bind(rxfd,
241 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
242 err = -errno;
243 goto cleanup;
244 }
245
246 sock.sll_family = AF_PACKET;
247 sock.sll_protocol = htons(ETH_P_IP);
248 sock.sll_ifindex = ifr.ifr_ifindex;
249
250 if (bind(txfd,
251 (struct sockaddr *) &sock, sizeof(struct sockaddr_ll)) < 0) {
252 err = -errno;
253 goto cleanup;
254 }
255
256 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
257 if (result != NULL) {
258 result->rx_fd = rxfd;
259 result->tx_fd = txfd;
260 result->remote_addr = NULL;
261 result->remote_addr_size = 0;
262 }
263 return result;
264cleanup:
265 printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
266 if (rxfd >= 0)
267 os_close_file(rxfd);
268 if (txfd >= 0)
269 os_close_file(txfd);
270 kfree(result);
271 return NULL;
272}
273
274
275bool uml_raw_enable_qdisc_bypass(int fd)
276{
277 int optval = 1;
278
279 if (setsockopt(fd,
280 SOL_PACKET, PACKET_QDISC_BYPASS,
281 &optval, sizeof(optval)) != 0) {
282 return false;
283 }
284 return true;
285}
286
287bool uml_raw_enable_vnet_headers(int fd)
288{
289 int optval = 1;
290
291 if (setsockopt(fd,
292 SOL_PACKET, PACKET_VNET_HDR,
293 &optval, sizeof(optval)) != 0) {
294 printk(UM_KERN_INFO VNET_HDR_FAIL, fd);
295 return false;
296 }
297 return true;
298}
299bool uml_tap_enable_vnet_headers(int fd)
300{
301 unsigned int features;
302 int len = sizeof(struct virtio_net_hdr);
303
304 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
305 printk(UM_KERN_INFO TUN_GET_F_FAIL, strerror(errno));
306 return false;
307 }
308 if ((features & IFF_VNET_HDR) == 0) {
309 printk(UM_KERN_INFO "tapraw: No VNET HEADER support");
310 return false;
311 }
312 ioctl(fd, TUNSETVNETHDRSZ, &len);
313 return true;
314}
315
316static struct vector_fds *user_init_socket_fds(struct arglist *ifspec, int id)
317{
318 int err = -ENOMEM;
319 int fd = -1, gairet;
320 struct addrinfo srchints;
321 struct addrinfo dsthints;
322 bool v6, udp;
323 char *value;
324 char *src, *dst, *srcport, *dstport;
325 struct addrinfo *gairesult = NULL;
326 struct vector_fds *result = NULL;
327
328
329 value = uml_vector_fetch_arg(ifspec, "v6");
330 v6 = false;
331 udp = false;
332 if (value != NULL) {
333 if (strtol((const char *) value, NULL, 10) > 0)
334 v6 = true;
335 }
336
337 value = uml_vector_fetch_arg(ifspec, "udp");
338 if (value != NULL) {
339 if (strtol((const char *) value, NULL, 10) > 0)
340 udp = true;
341 }
342 src = uml_vector_fetch_arg(ifspec, "src");
343 dst = uml_vector_fetch_arg(ifspec, "dst");
344 srcport = uml_vector_fetch_arg(ifspec, "srcport");
345 dstport = uml_vector_fetch_arg(ifspec, "dstport");
346
347 memset(&dsthints, 0, sizeof(dsthints));
348
349 if (v6)
350 dsthints.ai_family = AF_INET6;
351 else
352 dsthints.ai_family = AF_INET;
353
354 switch (id) {
355 case ID_GRE:
356 dsthints.ai_socktype = SOCK_RAW;
357 dsthints.ai_protocol = IPPROTO_GRE;
358 break;
359 case ID_L2TPV3:
360 if (udp) {
361 dsthints.ai_socktype = SOCK_DGRAM;
362 dsthints.ai_protocol = 0;
363 } else {
364 dsthints.ai_socktype = SOCK_RAW;
365 dsthints.ai_protocol = IPPROTO_L2TP;
366 }
367 break;
368 default:
369 printk(KERN_ERR "Unsupported socket type\n");
370 return NULL;
371 }
372 memcpy(&srchints, &dsthints, sizeof(struct addrinfo));
373
374 gairet = getaddrinfo(src, srcport, &dsthints, &gairesult);
375 if ((gairet != 0) || (gairesult == NULL)) {
376 printk(UM_KERN_ERR
377 "socket_open : could not resolve src, error = %s",
378 gai_strerror(gairet)
379 );
380 return NULL;
381 }
382 fd = socket(gairesult->ai_family,
383 gairesult->ai_socktype, gairesult->ai_protocol);
384 if (fd == -1) {
385 printk(UM_KERN_ERR
386 "socket_open : could not open socket, error = %d",
387 -errno
388 );
389 goto cleanup;
390 }
391 if (bind(fd,
392 (struct sockaddr *) gairesult->ai_addr,
393 gairesult->ai_addrlen)) {
394 printk(UM_KERN_ERR L2TPV3_BIND_FAIL, errno);
395 goto cleanup;
396 }
397
398 if (gairesult != NULL)
399 freeaddrinfo(gairesult);
400
401 gairesult = NULL;
402
403 gairet = getaddrinfo(dst, dstport, &dsthints, &gairesult);
404 if ((gairet != 0) || (gairesult == NULL)) {
405 printk(UM_KERN_ERR
406 "socket_open : could not resolve dst, error = %s",
407 gai_strerror(gairet)
408 );
409 return NULL;
410 }
411
412 result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
413 if (result != NULL) {
414 result->rx_fd = fd;
415 result->tx_fd = fd;
416 result->remote_addr = uml_kmalloc(
417 gairesult->ai_addrlen, UM_GFP_KERNEL);
418 if (result->remote_addr == NULL)
419 goto cleanup;
420 result->remote_addr_size = gairesult->ai_addrlen;
421 memcpy(
422 result->remote_addr,
423 gairesult->ai_addr,
424 gairesult->ai_addrlen
425 );
426 }
427 freeaddrinfo(gairesult);
428 return result;
429cleanup:
430 if (gairesult != NULL)
431 freeaddrinfo(gairesult);
432 printk(UM_KERN_ERR "user_init_socket: init failed, error %d", err);
433 if (fd >= 0)
434 os_close_file(fd);
435 if (result != NULL) {
436 kfree(result->remote_addr);
437 kfree(result);
438 }
439 return NULL;
440}
441
442struct vector_fds *uml_vector_user_open(
443 int unit,
444 struct arglist *parsed
445)
446{
447 char *transport;
448
449 if (parsed == NULL) {
450 printk(UM_KERN_ERR "no parsed config for unit %d\n", unit);
451 return NULL;
452 }
453 transport = uml_vector_fetch_arg(parsed, "transport");
454 if (transport == NULL) {
455 printk(UM_KERN_ERR "missing transport for unit %d\n", unit);
456 return NULL;
457 }
458 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
459 return user_init_raw_fds(parsed);
460 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
461 return user_init_tap_fds(parsed);
462 if (strncmp(transport, TRANS_GRE, TRANS_GRE_LEN) == 0)
463 return user_init_socket_fds(parsed, ID_GRE);
464 if (strncmp(transport, TRANS_L2TPV3, TRANS_L2TPV3_LEN) == 0)
465 return user_init_socket_fds(parsed, ID_L2TPV3);
466 return NULL;
467}
468
469
470int uml_vector_sendmsg(int fd, void *hdr, int flags)
471{
472 int n;
473
474 CATCH_EINTR(n = sendmsg(fd, (struct msghdr *) hdr, flags));
475 if ((n < 0) && (errno == EAGAIN))
476 return 0;
477 if (n >= 0)
478 return n;
479 else
480 return -errno;
481}
482
483int uml_vector_recvmsg(int fd, void *hdr, int flags)
484{
485 int n;
486
487 CATCH_EINTR(n = recvmsg(fd, (struct msghdr *) hdr, flags));
488 if ((n < 0) && (errno == EAGAIN))
489 return 0;
490 if (n >= 0)
491 return n;
492 else
493 return -errno;
494}
495
496int uml_vector_writev(int fd, void *hdr, int iovcount)
497{
498 int n;
499
500 CATCH_EINTR(n = writev(fd, (struct iovec *) hdr, iovcount));
501 if ((n < 0) && (errno == EAGAIN))
502 return 0;
503 if (n >= 0)
504 return n;
505 else
506 return -errno;
507}
508
509int uml_vector_sendmmsg(
510 int fd,
511 void *msgvec,
512 unsigned int vlen,
513 unsigned int flags)
514{
515 int n;
516
517 CATCH_EINTR(n = sendmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags));
518 if ((n < 0) && (errno == EAGAIN))
519 return 0;
520 if (n >= 0)
521 return n;
522 else
523 return -errno;
524}
525
526int uml_vector_recvmmsg(
527 int fd,
528 void *msgvec,
529 unsigned int vlen,
530 unsigned int flags)
531{
532 int n;
533
534 CATCH_EINTR(
535 n = recvmmsg(fd, (struct mmsghdr *) msgvec, vlen, flags, 0));
536 if ((n < 0) && (errno == EAGAIN))
537 return 0;
538 if (n >= 0)
539 return n;
540 else
541 return -errno;
542}
543int uml_vector_attach_bpf(int fd, void *bpf, int bpf_len)
544{
545 int err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, bpf, bpf_len);
546
547 if (err < 0)
548 printk(KERN_ERR BPF_ATTACH_FAIL, bpf_len, fd, -errno);
549 return err;
550}
551
552#define DEFAULT_BPF_LEN 6
553
554void *uml_vector_default_bpf(int fd, void *mac)
555{
556 struct sock_filter *bpf;
557 uint32_t *mac1 = (uint32_t *)(mac + 2);
558 uint16_t *mac2 = (uint16_t *) mac;
559 struct sock_fprog bpf_prog = {
560 .len = 6,
561 .filter = NULL,
562 };
563
564 bpf = uml_kmalloc(
565 sizeof(struct sock_filter) * DEFAULT_BPF_LEN, UM_GFP_KERNEL);
566 if (bpf != NULL) {
567 bpf_prog.filter = bpf;
568 /* ld [8] */
569 bpf[0] = (struct sock_filter){ 0x20, 0, 0, 0x00000008 };
570 /* jeq #0xMAC[2-6] jt 2 jf 5*/
571 bpf[1] = (struct sock_filter){ 0x15, 0, 3, ntohl(*mac1)};
572 /* ldh [6] */
573 bpf[2] = (struct sock_filter){ 0x28, 0, 0, 0x00000006 };
574 /* jeq #0xMAC[0-1] jt 4 jf 5 */
575 bpf[3] = (struct sock_filter){ 0x15, 0, 1, ntohs(*mac2)};
576 /* ret #0 */
577 bpf[4] = (struct sock_filter){ 0x6, 0, 0, 0x00000000 };
578 /* ret #0x40000 */
579 bpf[5] = (struct sock_filter){ 0x6, 0, 0, 0x00040000 };
580 if (uml_vector_attach_bpf(
581 fd, &bpf_prog, sizeof(struct sock_fprog)) < 0) {
582 kfree(bpf);
583 bpf = NULL;
584 }
585 }
586 return bpf;
587}
588