dnscrypto-proxy: Support files updated.
[tomato.git] / release / src / router / iproute2 / ip / iptunnel.c
blob10e83d51427a159535f9af2f89398b2e1220c813
1 /*
2 * iptunnel.c "ip tunnel"
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Changes:
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
16 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <sys/ioctl.h>
27 #include <linux/if.h>
28 #include <linux/if_arp.h>
29 #include <linux/ip.h>
31 #include <linux/if_tunnel.h>
33 #include "rt_names.h"
34 #include "utils.h"
35 #include "ip_common.h"
36 #include "tunnel.h"
38 static void usage(void) __attribute__((noreturn));
40 static void usage(void)
42 #ifdef NO_IPV6
43 fprintf(stderr, "Usage: ip tunnel { add | change | del | show } [ NAME ]\n");
44 #else
45 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | 6rd } [ NAME ]\n");
46 #endif
47 fprintf(stderr, " [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n");
48 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
49 #ifndef NO_IPV6
50 fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
51 #endif
52 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
53 fprintf(stderr, "\n");
54 fprintf(stderr, "Where: NAME := STRING\n");
55 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
56 fprintf(stderr, " TOS := { NUMBER | inherit }\n");
57 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
58 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
59 exit(-1);
62 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
64 int count = 0;
65 char medium[IFNAMSIZ];
67 memset(p, 0, sizeof(*p));
68 memset(&medium, 0, sizeof(medium));
70 p->iph.version = 4;
71 p->iph.ihl = 5;
72 #ifndef IP_DF
73 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
74 #endif
75 p->iph.frag_off = htons(IP_DF);
77 while (argc > 0) {
78 if (strcmp(*argv, "mode") == 0) {
79 NEXT_ARG();
80 if (strcmp(*argv, "ipip") == 0 ||
81 strcmp(*argv, "ip/ip") == 0) {
82 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
83 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
84 exit(-1);
86 p->iph.protocol = IPPROTO_IPIP;
87 } else if (strcmp(*argv, "gre") == 0 ||
88 strcmp(*argv, "gre/ip") == 0) {
89 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
90 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
91 exit(-1);
93 p->iph.protocol = IPPROTO_GRE;
94 } else if (strcmp(*argv, "sit") == 0 ||
95 strcmp(*argv, "ipv6/ip") == 0) {
96 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
97 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
98 exit(-1);
100 p->iph.protocol = IPPROTO_IPV6;
101 } else {
102 fprintf(stderr,"Cannot guess tunnel mode.\n");
103 exit(-1);
105 } else if (strcmp(*argv, "key") == 0) {
106 unsigned uval;
107 NEXT_ARG();
108 p->i_flags |= GRE_KEY;
109 p->o_flags |= GRE_KEY;
110 if (strchr(*argv, '.'))
111 p->i_key = p->o_key = get_addr32(*argv);
112 else {
113 if (get_unsigned(&uval, *argv, 0)<0) {
114 fprintf(stderr, "invalid value of \"key\"\n");
115 exit(-1);
117 p->i_key = p->o_key = htonl(uval);
119 } else if (strcmp(*argv, "ikey") == 0) {
120 unsigned uval;
121 NEXT_ARG();
122 p->i_flags |= GRE_KEY;
123 if (strchr(*argv, '.'))
124 p->i_key = get_addr32(*argv);
125 else {
126 if (get_unsigned(&uval, *argv, 0)<0) {
127 fprintf(stderr, "invalid value of \"ikey\"\n");
128 exit(-1);
130 p->i_key = htonl(uval);
132 } else if (strcmp(*argv, "okey") == 0) {
133 unsigned uval;
134 NEXT_ARG();
135 p->o_flags |= GRE_KEY;
136 if (strchr(*argv, '.'))
137 p->o_key = get_addr32(*argv);
138 else {
139 if (get_unsigned(&uval, *argv, 0)<0) {
140 fprintf(stderr, "invalid value of \"okey\"\n");
141 exit(-1);
143 p->o_key = htonl(uval);
145 } else if (strcmp(*argv, "seq") == 0) {
146 p->i_flags |= GRE_SEQ;
147 p->o_flags |= GRE_SEQ;
148 } else if (strcmp(*argv, "iseq") == 0) {
149 p->i_flags |= GRE_SEQ;
150 } else if (strcmp(*argv, "oseq") == 0) {
151 p->o_flags |= GRE_SEQ;
152 } else if (strcmp(*argv, "csum") == 0) {
153 p->i_flags |= GRE_CSUM;
154 p->o_flags |= GRE_CSUM;
155 } else if (strcmp(*argv, "icsum") == 0) {
156 p->i_flags |= GRE_CSUM;
157 } else if (strcmp(*argv, "ocsum") == 0) {
158 p->o_flags |= GRE_CSUM;
159 } else if (strcmp(*argv, "nopmtudisc") == 0) {
160 p->iph.frag_off = 0;
161 } else if (strcmp(*argv, "pmtudisc") == 0) {
162 p->iph.frag_off = htons(IP_DF);
163 } else if (strcmp(*argv, "remote") == 0) {
164 NEXT_ARG();
165 if (strcmp(*argv, "any"))
166 p->iph.daddr = get_addr32(*argv);
167 } else if (strcmp(*argv, "local") == 0) {
168 NEXT_ARG();
169 if (strcmp(*argv, "any"))
170 p->iph.saddr = get_addr32(*argv);
171 } else if (strcmp(*argv, "dev") == 0) {
172 NEXT_ARG();
173 strncpy(medium, *argv, IFNAMSIZ-1);
174 } else if (strcmp(*argv, "ttl") == 0) {
175 unsigned uval;
176 NEXT_ARG();
177 if (strcmp(*argv, "inherit") != 0) {
178 if (get_unsigned(&uval, *argv, 0))
179 invarg("invalid TTL\n", *argv);
180 if (uval > 255)
181 invarg("TTL must be <=255\n", *argv);
182 p->iph.ttl = uval;
184 } else if (strcmp(*argv, "tos") == 0 ||
185 matches(*argv, "dsfield") == 0) {
186 __u32 uval;
187 NEXT_ARG();
188 if (strcmp(*argv, "inherit") != 0) {
189 if (rtnl_dsfield_a2n(&uval, *argv))
190 invarg("bad TOS value", *argv);
191 p->iph.tos = uval;
192 } else
193 p->iph.tos = 1;
194 } else {
195 if (strcmp(*argv, "name") == 0) {
196 NEXT_ARG();
198 if (matches(*argv, "help") == 0)
199 usage();
200 if (p->name[0])
201 duparg2("name", *argv);
202 strncpy(p->name, *argv, IFNAMSIZ);
203 if (cmd == SIOCCHGTUNNEL && count == 0) {
204 struct ip_tunnel_parm old_p;
205 memset(&old_p, 0, sizeof(old_p));
206 if (tnl_get_ioctl(*argv, &old_p))
207 return -1;
208 *p = old_p;
211 count++;
212 argc--; argv++;
216 if (p->iph.protocol == 0) {
217 if (memcmp(p->name, "gre", 3) == 0)
218 p->iph.protocol = IPPROTO_GRE;
219 else if (memcmp(p->name, "ipip", 4) == 0)
220 p->iph.protocol = IPPROTO_IPIP;
221 else if (memcmp(p->name, "sit", 3) == 0)
222 p->iph.protocol = IPPROTO_IPV6;
225 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
226 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
227 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
228 return -1;
232 if (medium[0]) {
233 p->link = tnl_ioctl_get_ifindex(medium);
234 if (p->link == 0)
235 return -1;
238 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
239 p->i_key = p->iph.daddr;
240 p->i_flags |= GRE_KEY;
242 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
243 p->o_key = p->iph.daddr;
244 p->o_flags |= GRE_KEY;
246 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
247 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
248 return -1;
250 return 0;
254 static int do_add(int cmd, int argc, char **argv)
256 struct ip_tunnel_parm p;
258 if (parse_args(argc, argv, cmd, &p) < 0)
259 return -1;
261 if (p.iph.ttl && p.iph.frag_off == 0) {
262 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
263 return -1;
266 switch (p.iph.protocol) {
267 case IPPROTO_IPIP:
268 return tnl_add_ioctl(cmd, "tunl0", p.name, &p);
269 case IPPROTO_GRE:
270 return tnl_add_ioctl(cmd, "gre0", p.name, &p);
271 case IPPROTO_IPV6:
272 return tnl_add_ioctl(cmd, "sit0", p.name, &p);
273 default:
274 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
275 return -1;
277 return -1;
280 static int do_del(int argc, char **argv)
282 struct ip_tunnel_parm p;
284 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
285 return -1;
287 switch (p.iph.protocol) {
288 case IPPROTO_IPIP:
289 return tnl_del_ioctl("tunl0", p.name, &p);
290 case IPPROTO_GRE:
291 return tnl_del_ioctl("gre0", p.name, &p);
292 case IPPROTO_IPV6:
293 return tnl_del_ioctl("sit0", p.name, &p);
294 default:
295 return tnl_del_ioctl(p.name, p.name, &p);
297 return -1;
300 static void print_tunnel(struct ip_tunnel_parm *p)
302 #ifndef NO_IPV6
303 struct ip_tunnel_6rd ip6rd;
304 #endif
305 char s1[1024];
306 char s2[1024];
307 char s3[64];
308 char s4[64];
310 #ifndef NO_IPV6
311 memset(&ip6rd, 0, sizeof(ip6rd));
312 #endif
313 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
314 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
316 /* Do not use format_host() for local addr,
317 * symbolic name will not be useful.
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");
325 if (p->link) {
326 char *n = tnl_ioctl_get_ifname(p->link);
327 if (n)
328 printf(" dev %s ", n);
331 if (p->iph.ttl)
332 printf(" ttl %d ", p->iph.ttl);
333 else
334 printf(" ttl inherit ");
336 if (p->iph.tos) {
337 SPRINT_BUF(b1);
338 printf(" tos");
339 if (p->iph.tos&1)
340 printf(" inherit");
341 if (p->iph.tos&~1)
342 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
343 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
346 if (!(p->iph.frag_off&htons(IP_DF)))
347 printf(" nopmtudisc");
349 #ifndef NO_IPV6
350 if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
351 printf(" 6rd-prefix %s/%u ",
352 inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
353 ip6rd.prefixlen);
354 if (ip6rd.relay_prefix) {
355 printf("6rd-relay_prefix %s/%u ",
356 format_host(AF_INET, 4, &ip6rd.relay_prefix, s1, sizeof(s1)),
357 ip6rd.relay_prefixlen);
360 #endif
362 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
363 printf(" key %s", s3);
364 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
365 if (p->i_flags&GRE_KEY)
366 printf(" ikey %s ", s3);
367 if (p->o_flags&GRE_KEY)
368 printf(" okey %s ", s4);
371 if (p->i_flags&GRE_SEQ)
372 printf("%s Drop packets out of sequence.\n", _SL_);
373 if (p->i_flags&GRE_CSUM)
374 printf("%s Checksum in received packet is required.", _SL_);
375 if (p->o_flags&GRE_SEQ)
376 printf("%s Sequence packets on output.", _SL_);
377 if (p->o_flags&GRE_CSUM)
378 printf("%s Checksum output packets.", _SL_);
381 static int do_tunnels_list(struct ip_tunnel_parm *p)
383 char name[IFNAMSIZ];
384 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
385 rx_fifo, rx_frame,
386 tx_bytes, tx_packets, tx_errs, tx_drops,
387 tx_fifo, tx_colls, tx_carrier, rx_multi;
388 int type;
389 struct ip_tunnel_parm p1;
391 char buf[512];
392 FILE *fp = fopen("/proc/net/dev", "r");
393 if (fp == NULL) {
394 perror("fopen");
395 return -1;
398 fgets(buf, sizeof(buf), fp);
399 fgets(buf, sizeof(buf), fp);
401 while (fgets(buf, sizeof(buf), fp) != NULL) {
402 char *ptr;
403 buf[sizeof(buf) - 1] = 0;
404 if ((ptr = strchr(buf, ':')) == NULL ||
405 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
406 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
407 return -1;
409 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
410 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
411 &rx_fifo, &rx_frame, &rx_multi,
412 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
413 &tx_fifo, &tx_colls, &tx_carrier) != 14)
414 continue;
415 if (p->name[0] && strcmp(p->name, name))
416 continue;
417 type = tnl_ioctl_get_iftype(name);
418 if (type == -1) {
419 fprintf(stderr, "Failed to get type of [%s]\n", name);
420 continue;
422 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
423 continue;
424 memset(&p1, 0, sizeof(p1));
425 if (tnl_get_ioctl(name, &p1))
426 continue;
427 if ((p->link && p1.link != p->link) ||
428 (p->name[0] && strcmp(p1.name, p->name)) ||
429 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
430 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
431 (p->i_key && p1.i_key != p->i_key))
432 continue;
433 print_tunnel(&p1);
434 if (show_stats) {
435 printf("%s", _SL_);
436 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
437 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
438 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
439 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
440 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
441 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
443 printf("\n");
445 return 0;
448 static int do_show(int argc, char **argv)
450 int err;
451 struct ip_tunnel_parm p;
453 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
454 return -1;
456 switch (p.iph.protocol) {
457 case IPPROTO_IPIP:
458 err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
459 break;
460 case IPPROTO_GRE:
461 err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p);
462 break;
463 case IPPROTO_IPV6:
464 err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p);
465 break;
466 default:
467 do_tunnels_list(&p);
468 return 0;
470 if (err)
471 return -1;
473 print_tunnel(&p);
474 printf("\n");
475 return 0;
478 #ifndef NO_IPV6
479 static int do_6rd(int argc, char **argv)
481 struct ip_tunnel_6rd ip6rd;
482 int devname = 0;
483 int cmd = 0;
484 char medium[IFNAMSIZ];
485 inet_prefix prefix;
487 memset(&ip6rd, 0, sizeof(ip6rd));
488 memset(&medium, 0, sizeof(medium));
490 while (argc > 0) {
491 if (strcmp(*argv, "6rd-prefix") == 0) {
492 NEXT_ARG();
493 if (get_prefix(&prefix, *argv, AF_INET6))
494 invarg("invalid 6rd_prefix\n", *argv);
495 cmd = SIOCADD6RD;
496 memcpy(&ip6rd.prefix, prefix.data, 16);
497 ip6rd.prefixlen = prefix.bitlen;
498 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
499 NEXT_ARG();
500 if (get_prefix(&prefix, *argv, AF_INET))
501 invarg("invalid 6rd-relay_prefix\n", *argv);
502 cmd = SIOCADD6RD;
503 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
504 ip6rd.relay_prefixlen = prefix.bitlen;
505 } else if (strcmp(*argv, "6rd-reset") == 0) {
506 cmd = SIOCDEL6RD;
507 } else if (strcmp(*argv, "dev") == 0) {
508 NEXT_ARG();
509 strncpy(medium, *argv, IFNAMSIZ-1);
510 devname++;
511 } else {
512 fprintf(stderr,"%s: Invalid 6RD parameter.\n", *argv);
513 exit(-1);
515 argc--; argv++;
517 if (devname == 0) {
518 fprintf(stderr, "Must specify dev.\n");
519 exit(-1);
522 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
524 #endif
526 int do_iptunnel(int argc, char **argv)
528 switch (preferred_family) {
529 case AF_UNSPEC:
530 preferred_family = AF_INET;
531 break;
532 case AF_INET:
533 break;
534 #ifndef NO_IPV6
536 * This is silly enough but we have no easy way to make it
537 * protocol-independent because of unarranged structure between
538 * IPv4 and IPv6.
540 case AF_INET6:
541 return do_ip6tunnel(argc, argv);
542 #endif
543 default:
544 fprintf(stderr, "Unsupported family:%d\n", preferred_family);
545 exit(-1);
548 if (argc > 0) {
549 if (matches(*argv, "add") == 0)
550 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
551 if (matches(*argv, "change") == 0)
552 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
553 if (matches(*argv, "del") == 0)
554 return do_del(argc-1, argv+1);
555 if (matches(*argv, "show") == 0 ||
556 matches(*argv, "lst") == 0 ||
557 matches(*argv, "list") == 0)
558 return do_show(argc-1, argv+1);
559 #ifndef NO_IPV6
560 if (matches(*argv, "6rd") == 0)
561 return do_6rd(argc-1, argv+1);
562 #endif
563 if (matches(*argv, "help") == 0)
564 usage();
565 } else
566 return do_show(0, NULL);
568 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);
569 exit(-1);