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