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