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