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