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