1/*
2 * iptunnel.c	       "ip tunnel"
3 *
4 *		This program is free software; you can redistribute it and/or
5 *		modify it under the terms of the GNU General Public License
6 *		as published by the Free Software Foundation; either version
7 *		2 of the License, or (at your option) any later version.
8 *
9 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <arpa/inet.h>
20#include <sys/ioctl.h>
21#include <net/if.h>
22#include <net/if_arp.h>
23#include <linux/ip.h>
24#include <linux/if_tunnel.h>
25
26#include "rt_names.h"
27#include "utils.h"
28#include "ip_common.h"
29#include "tunnel.h"
30
31static void usage(void) __attribute__((noreturn));
32
33static void usage(void)
34{
35	fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n");
36	fprintf(stderr, "          [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n");
37	fprintf(stderr, "          [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
38	fprintf(stderr, "          [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
39	fprintf(stderr, "          [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
40	fprintf(stderr, "          [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
41	fprintf(stderr, "\n");
42	fprintf(stderr, "Where: NAME := STRING\n");
43	fprintf(stderr, "       ADDR := { IP_ADDRESS | any }\n");
44	fprintf(stderr, "       TOS  := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n");
45	fprintf(stderr, "       TTL  := { 1..255 | inherit }\n");
46	fprintf(stderr, "       KEY  := { DOTTED_QUAD | NUMBER }\n");
47	exit(-1);
48}
49
50static void set_tunnel_proto(struct ip_tunnel_parm *p, int proto)
51{
52	if (p->iph.protocol && p->iph.protocol != proto) {
53		fprintf(stderr,
54			"You managed to ask for more than one tunnel mode.\n");
55		exit(-1);
56	}
57	p->iph.protocol = proto;
58}
59
60static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
61{
62	int count = 0;
63	char medium[IFNAMSIZ];
64	int isatap = 0;
65
66	memset(p, 0, sizeof(*p));
67	memset(&medium, 0, sizeof(medium));
68
69	p->iph.version = 4;
70	p->iph.ihl = 5;
71#ifndef IP_DF
72#define IP_DF		0x4000		/* Flag: "Don't Fragment"	*/
73#endif
74	p->iph.frag_off = htons(IP_DF);
75
76	while (argc > 0) {
77		if (strcmp(*argv, "mode") == 0) {
78			NEXT_ARG();
79			if (strcmp(*argv, "ipip") == 0 ||
80			    strcmp(*argv, "ip/ip") == 0) {
81				set_tunnel_proto(p, IPPROTO_IPIP);
82			} else if (strcmp(*argv, "gre") == 0 ||
83				   strcmp(*argv, "gre/ip") == 0) {
84				set_tunnel_proto(p, IPPROTO_GRE);
85			} else if (strcmp(*argv, "sit") == 0 ||
86				   strcmp(*argv, "ipv6/ip") == 0) {
87				set_tunnel_proto(p, IPPROTO_IPV6);
88			} else if (strcmp(*argv, "isatap") == 0) {
89				set_tunnel_proto(p, IPPROTO_IPV6);
90				isatap++;
91			} else if (strcmp(*argv, "vti") == 0) {
92				set_tunnel_proto(p, IPPROTO_IPIP);
93				p->i_flags |= VTI_ISVTI;
94			} else {
95				fprintf(stderr,
96					"Unknown tunnel mode \"%s\"\n", *argv);
97				exit(-1);
98			}
99		} else if (strcmp(*argv, "key") == 0) {
100			NEXT_ARG();
101			p->i_flags |= GRE_KEY;
102			p->o_flags |= GRE_KEY;
103			p->i_key = p->o_key = tnl_parse_key("key", *argv);
104		} else if (strcmp(*argv, "ikey") == 0) {
105			NEXT_ARG();
106			p->i_flags |= GRE_KEY;
107			p->i_key = tnl_parse_key("ikey", *argv);
108		} else if (strcmp(*argv, "okey") == 0) {
109			NEXT_ARG();
110			p->o_flags |= GRE_KEY;
111			p->o_key = tnl_parse_key("okey", *argv);
112		} else if (strcmp(*argv, "seq") == 0) {
113			p->i_flags |= GRE_SEQ;
114			p->o_flags |= GRE_SEQ;
115		} else if (strcmp(*argv, "iseq") == 0) {
116			p->i_flags |= GRE_SEQ;
117		} else if (strcmp(*argv, "oseq") == 0) {
118			p->o_flags |= GRE_SEQ;
119		} else if (strcmp(*argv, "csum") == 0) {
120			p->i_flags |= GRE_CSUM;
121			p->o_flags |= GRE_CSUM;
122		} else if (strcmp(*argv, "icsum") == 0) {
123			p->i_flags |= GRE_CSUM;
124		} else if (strcmp(*argv, "ocsum") == 0) {
125			p->o_flags |= GRE_CSUM;
126		} else if (strcmp(*argv, "nopmtudisc") == 0) {
127			p->iph.frag_off = 0;
128		} else if (strcmp(*argv, "pmtudisc") == 0) {
129			p->iph.frag_off = htons(IP_DF);
130		} else if (strcmp(*argv, "remote") == 0) {
131			NEXT_ARG();
132			if (strcmp(*argv, "any"))
133				p->iph.daddr = get_addr32(*argv);
134			else
135				p->iph.daddr = htonl(INADDR_ANY);
136		} else if (strcmp(*argv, "local") == 0) {
137			NEXT_ARG();
138			if (strcmp(*argv, "any"))
139				p->iph.saddr = get_addr32(*argv);
140			else
141				p->iph.saddr = htonl(INADDR_ANY);
142		} else if (strcmp(*argv, "dev") == 0) {
143			NEXT_ARG();
144			strncpy(medium, *argv, IFNAMSIZ - 1);
145		} else if (strcmp(*argv, "ttl") == 0 ||
146			   strcmp(*argv, "hoplimit") == 0 ||
147			   strcmp(*argv, "hlim") == 0) {
148			__u8 uval;
149
150			NEXT_ARG();
151			if (strcmp(*argv, "inherit") != 0) {
152				if (get_u8(&uval, *argv, 0))
153					invarg("invalid TTL\n", *argv);
154				p->iph.ttl = uval;
155			}
156		} else if (strcmp(*argv, "tos") == 0 ||
157			   strcmp(*argv, "tclass") == 0 ||
158			   matches(*argv, "dsfield") == 0) {
159			char *dsfield;
160			__u32 uval;
161
162			NEXT_ARG();
163			dsfield = *argv;
164			strsep(&dsfield, "/");
165			if (strcmp(*argv, "inherit") != 0) {
166				dsfield = *argv;
167				p->iph.tos = 0;
168			} else
169				p->iph.tos = 1;
170			if (dsfield) {
171				if (rtnl_dsfield_a2n(&uval, dsfield))
172					invarg("bad TOS value", *argv);
173				p->iph.tos |= uval;
174			}
175		} else {
176			if (strcmp(*argv, "name") == 0)
177				NEXT_ARG();
178			else if (matches(*argv, "help") == 0)
179				usage();
180
181			if (p->name[0])
182				duparg2("name", *argv);
183			strncpy(p->name, *argv, IFNAMSIZ - 1);
184			if (cmd == SIOCCHGTUNNEL && count == 0) {
185				struct ip_tunnel_parm old_p;
186
187				memset(&old_p, 0, sizeof(old_p));
188				if (tnl_get_ioctl(*argv, &old_p))
189					return -1;
190				*p = old_p;
191			}
192		}
193		count++;
194		argc--; argv++;
195	}
196
197
198	if (p->iph.protocol == 0) {
199		if (memcmp(p->name, "gre", 3) == 0)
200			p->iph.protocol = IPPROTO_GRE;
201		else if (memcmp(p->name, "ipip", 4) == 0)
202			p->iph.protocol = IPPROTO_IPIP;
203		else if (memcmp(p->name, "sit", 3) == 0)
204			p->iph.protocol = IPPROTO_IPV6;
205		else if (memcmp(p->name, "isatap", 6) == 0) {
206			p->iph.protocol = IPPROTO_IPV6;
207			isatap++;
208		} else if (memcmp(p->name, "vti", 3) == 0) {
209			p->iph.protocol = IPPROTO_IPIP;
210			p->i_flags |= VTI_ISVTI;
211		}
212	}
213
214	if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
215		if (!(p->i_flags & VTI_ISVTI) &&
216		    (p->iph.protocol != IPPROTO_GRE)) {
217			fprintf(stderr, "Keys are not allowed with ipip and sit tunnels\n");
218			return -1;
219		}
220	}
221
222	if (medium[0]) {
223		p->link = ll_name_to_index(medium);
224		if (p->link == 0) {
225			fprintf(stderr, "Cannot find device \"%s\"\n", medium);
226			return -1;
227		}
228	}
229
230	if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
231		p->i_key = p->iph.daddr;
232		p->i_flags |= GRE_KEY;
233	}
234	if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
235		p->o_key = p->iph.daddr;
236		p->o_flags |= GRE_KEY;
237	}
238	if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
239		fprintf(stderr, "A broadcast tunnel requires a source address\n");
240		return -1;
241	}
242	if (isatap)
243		p->i_flags |= SIT_ISATAP;
244
245	return 0;
246}
247
248static const char *tnl_defname(const struct ip_tunnel_parm *p)
249{
250	switch (p->iph.protocol) {
251	case IPPROTO_IPIP:
252		if (p->i_flags & VTI_ISVTI)
253			return "ip_vti0";
254		else
255			return "tunl0";
256	case IPPROTO_GRE:
257		return "gre0";
258	case IPPROTO_IPV6:
259		return "sit0";
260	}
261	return NULL;
262}
263
264static int do_add(int cmd, int argc, char **argv)
265{
266	struct ip_tunnel_parm p;
267	const char *basedev;
268
269	if (parse_args(argc, argv, cmd, &p) < 0)
270		return -1;
271
272	if (p.iph.ttl && p.iph.frag_off == 0) {
273		fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
274		return -1;
275	}
276
277	basedev = tnl_defname(&p);
278	if (!basedev) {
279		fprintf(stderr,
280			"cannot determine tunnel mode (ipip, gre, vti or sit)\n");
281		return -1;
282	}
283
284	return tnl_add_ioctl(cmd, basedev, p.name, &p);
285}
286
287static int do_del(int argc, char **argv)
288{
289	struct ip_tunnel_parm p;
290
291	if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
292		return -1;
293
294	return tnl_del_ioctl(tnl_defname(&p) ? : p.name, p.name, &p);
295}
296
297static void print_tunnel(struct ip_tunnel_parm *p)
298{
299	struct ip_tunnel_6rd ip6rd;
300	char s1[1024];
301	char s2[1024];
302
303	memset(&ip6rd, 0, sizeof(ip6rd));
304
305	/* Do not use format_host() for local addr,
306	 * symbolic name will not be useful.
307	 */
308	printf("%s: %s/ip remote %s local %s",
309	       p->name,
310	       tnl_strproto(p->iph.protocol),
311	       p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
312	       p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
313
314	if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
315		struct ip_tunnel_prl prl[16];
316		int i;
317
318		memset(prl, 0, sizeof(prl));
319		prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
320		prl[0].addr = htonl(INADDR_ANY);
321
322		if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
323			for (i = 1; i < ARRAY_SIZE(prl); i++) {
324				if (prl[i].addr != htonl(INADDR_ANY)) {
325					printf(" %s %s ",
326					       (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
327					       format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1)));
328				}
329			}
330	}
331
332	if (p->link) {
333		const char *n = ll_index_to_name(p->link);
334
335		if (n)
336			printf(" dev %s", n);
337	}
338
339	if (p->iph.ttl)
340		printf(" ttl %d", p->iph.ttl);
341	else
342		printf(" ttl inherit");
343
344	if (p->iph.tos) {
345		SPRINT_BUF(b1);
346		printf(" tos");
347		if (p->iph.tos & 1)
348			printf(" inherit");
349		if (p->iph.tos & ~1)
350			printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
351			       rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
352	}
353
354	if (!(p->iph.frag_off & htons(IP_DF)))
355		printf(" nopmtudisc");
356
357	if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
358		printf(" 6rd-prefix %s/%u",
359		       inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
360		       ip6rd.prefixlen);
361		if (ip6rd.relay_prefix) {
362			printf(" 6rd-relay_prefix %s/%u",
363			       format_host(AF_INET, 4, &ip6rd.relay_prefix, s1, sizeof(s1)),
364			       ip6rd.relay_prefixlen);
365		}
366	}
367
368	if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
369		printf(" key %u", ntohl(p->i_key));
370	else if ((p->i_flags | p->o_flags) & GRE_KEY) {
371		if (p->i_flags & GRE_KEY)
372			printf(" ikey %u", ntohl(p->i_key));
373		if (p->o_flags & GRE_KEY)
374			printf(" okey %u", ntohl(p->o_key));
375	}
376
377	if (p->i_flags & GRE_SEQ)
378		printf("%s  Drop packets out of sequence.", _SL_);
379	if (p->i_flags & GRE_CSUM)
380		printf("%s  Checksum in received packet is required.", _SL_);
381	if (p->o_flags & GRE_SEQ)
382		printf("%s  Sequence packets on output.", _SL_);
383	if (p->o_flags & GRE_CSUM)
384		printf("%s  Checksum output packets.", _SL_);
385}
386
387static int do_tunnels_list(struct ip_tunnel_parm *p)
388{
389	char buf[512];
390	int err = -1;
391	FILE *fp = fopen("/proc/net/dev", "r");
392
393	if (fp == NULL) {
394		perror("fopen");
395		return -1;
396	}
397
398	/* skip header lines */
399	if (!fgets(buf, sizeof(buf), fp) ||
400	    !fgets(buf, sizeof(buf), fp)) {
401		fprintf(stderr, "/proc/net/dev read error\n");
402		goto end;
403	}
404
405	while (fgets(buf, sizeof(buf), fp) != NULL) {
406		char name[IFNAMSIZ];
407		int index, type;
408		struct ip_tunnel_parm p1;
409		char *ptr;
410
411		buf[sizeof(buf) - 1] = 0;
412		ptr = strchr(buf, ':');
413		if (ptr == NULL ||
414		    (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
415			fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
416			goto end;
417		}
418		if (p->name[0] && strcmp(p->name, name))
419			continue;
420		index = ll_name_to_index(name);
421		if (index == 0)
422			continue;
423		type = ll_index_to_type(index);
424		if (type == -1) {
425			fprintf(stderr, "Failed to get type of \"%s\"\n", name);
426			continue;
427		}
428		if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
429			continue;
430		memset(&p1, 0, sizeof(p1));
431		if (tnl_get_ioctl(name, &p1))
432			continue;
433		if ((p->link && p1.link != p->link) ||
434		    (p->name[0] && strcmp(p1.name, p->name)) ||
435		    (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
436		    (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
437		    (p->i_key && p1.i_key != p->i_key))
438			continue;
439		print_tunnel(&p1);
440		if (show_stats)
441			tnl_print_stats(ptr);
442		printf("\n");
443	}
444	err = 0;
445 end:
446	fclose(fp);
447	return err;
448}
449
450static int do_show(int argc, char **argv)
451{
452	struct ip_tunnel_parm p;
453	const char *basedev;
454
455	ll_init_map(&rth);
456	if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
457		return -1;
458
459	basedev = tnl_defname(&p);
460	if (!basedev)
461		return do_tunnels_list(&p);
462
463	if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
464		return -1;
465
466	print_tunnel(&p);
467	printf("\n");
468	return 0;
469}
470
471static int do_prl(int argc, char **argv)
472{
473	struct ip_tunnel_prl p;
474	int count = 0;
475	int devname = 0;
476	int cmd = 0;
477	char medium[IFNAMSIZ];
478
479	memset(&p, 0, sizeof(p));
480	memset(&medium, 0, sizeof(medium));
481
482	while (argc > 0) {
483		if (strcmp(*argv, "prl-default") == 0) {
484			NEXT_ARG();
485			cmd = SIOCADDPRL;
486			p.addr = get_addr32(*argv);
487			p.flags |= PRL_DEFAULT;
488			count++;
489		} else if (strcmp(*argv, "prl-nodefault") == 0) {
490			NEXT_ARG();
491			cmd = SIOCADDPRL;
492			p.addr = get_addr32(*argv);
493			count++;
494		} else if (strcmp(*argv, "prl-delete") == 0) {
495			NEXT_ARG();
496			cmd = SIOCDELPRL;
497			p.addr = get_addr32(*argv);
498			count++;
499		} else if (strcmp(*argv, "dev") == 0) {
500			NEXT_ARG();
501			strncpy(medium, *argv, IFNAMSIZ-1);
502			devname++;
503		} else {
504			fprintf(stderr,
505				"Invalid PRL parameter \"%s\"\n", *argv);
506			exit(-1);
507		}
508		if (count > 1) {
509			fprintf(stderr,
510				"One PRL entry at a time\n");
511			exit(-1);
512		}
513		argc--; argv++;
514	}
515	if (devname == 0) {
516		fprintf(stderr, "Must specify device\n");
517		exit(-1);
518	}
519
520	return tnl_prl_ioctl(cmd, medium, &p);
521}
522
523static int do_6rd(int argc, char **argv)
524{
525	struct ip_tunnel_6rd ip6rd;
526	int devname = 0;
527	int cmd = 0;
528	char medium[IFNAMSIZ];
529	inet_prefix prefix;
530
531	memset(&ip6rd, 0, sizeof(ip6rd));
532	memset(&medium, 0, sizeof(medium));
533
534	while (argc > 0) {
535		if (strcmp(*argv, "6rd-prefix") == 0) {
536			NEXT_ARG();
537			if (get_prefix(&prefix, *argv, AF_INET6))
538				invarg("invalid 6rd_prefix\n", *argv);
539			cmd = SIOCADD6RD;
540			memcpy(&ip6rd.prefix, prefix.data, 16);
541			ip6rd.prefixlen = prefix.bitlen;
542		} else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
543			NEXT_ARG();
544			if (get_prefix(&prefix, *argv, AF_INET))
545				invarg("invalid 6rd-relay_prefix\n", *argv);
546			cmd = SIOCADD6RD;
547			memcpy(&ip6rd.relay_prefix, prefix.data, 4);
548			ip6rd.relay_prefixlen = prefix.bitlen;
549		} else if (strcmp(*argv, "6rd-reset") == 0) {
550			cmd = SIOCDEL6RD;
551		} else if (strcmp(*argv, "dev") == 0) {
552			NEXT_ARG();
553			strncpy(medium, *argv, IFNAMSIZ-1);
554			devname++;
555		} else {
556			fprintf(stderr,
557				"Invalid 6RD parameter \"%s\"\n", *argv);
558			exit(-1);
559		}
560		argc--; argv++;
561	}
562	if (devname == 0) {
563		fprintf(stderr, "Must specify device\n");
564		exit(-1);
565	}
566
567	return tnl_6rd_ioctl(cmd, medium, &ip6rd);
568}
569
570static int tunnel_mode_is_ipv6(char *tunnel_mode)
571{
572	static const char * const ipv6_modes[] = {
573		"ipv6/ipv6", "ip6ip6",
574		"vti6",
575		"ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
576		"ip6gre", "gre/ipv6",
577		"any/ipv6", "any"
578	};
579	int i;
580
581	for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
582		if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
583			return 1;
584	}
585	return 0;
586}
587
588int do_iptunnel(int argc, char **argv)
589{
590	int i;
591
592	for (i = 0; i < argc - 1; i++) {
593		if (strcmp(argv[i], "mode") == 0) {
594			if (tunnel_mode_is_ipv6(argv[i + 1]))
595				preferred_family = AF_INET6;
596			break;
597		}
598	}
599	switch (preferred_family) {
600	case AF_UNSPEC:
601		preferred_family = AF_INET;
602		break;
603	case AF_INET:
604		break;
605	/*
606	 * This is silly enough but we have no easy way to make it
607	 * protocol-independent because of unarranged structure between
608	 * IPv4 and IPv6.
609	 */
610	case AF_INET6:
611		return do_ip6tunnel(argc, argv);
612	default:
613		fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
614		exit(-1);
615	}
616
617	if (argc > 0) {
618		if (matches(*argv, "add") == 0)
619			return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
620		if (matches(*argv, "change") == 0)
621			return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
622		if (matches(*argv, "delete") == 0)
623			return do_del(argc - 1, argv + 1);
624		if (matches(*argv, "show") == 0 ||
625		    matches(*argv, "lst") == 0 ||
626		    matches(*argv, "list") == 0)
627			return do_show(argc - 1, argv + 1);
628		if (matches(*argv, "prl") == 0)
629			return do_prl(argc - 1, argv + 1);
630		if (matches(*argv, "6rd") == 0)
631			return do_6rd(argc - 1, argv + 1);
632		if (matches(*argv, "help") == 0)
633			usage();
634	} else
635		return do_show(0, NULL);
636
637	fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
638	exit(-1);
639}
640