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