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