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>
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 <sys/socket.h>
20 #include <sys/ioctl.h>
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #include <netinet/in.h>
31 #include <net/if_arp.h>
33 #include <asm/types.h>
34 #define __constant_htons htons
35 #include <linux/if_tunnel.h>
42 static int do_ioctl_get_ifindex(char *dev)
47 strcpy(ifr.ifr_name, dev);
48 fd = socket(AF_INET, SOCK_DGRAM, 0);
49 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
50 bb_perror_msg("ioctl");
54 return ifr.ifr_ifindex;
57 static int do_ioctl_get_iftype(char *dev)
62 strcpy(ifr.ifr_name, dev);
63 fd = socket(AF_INET, SOCK_DGRAM, 0);
64 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
65 bb_perror_msg("ioctl");
69 return ifr.ifr_addr.sa_family;
73 static char *do_ioctl_get_ifname(int idx)
75 static struct ifreq ifr;
78 ifr.ifr_ifindex = idx;
79 fd = socket(AF_INET, SOCK_DGRAM, 0);
80 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
81 bb_perror_msg("ioctl");
90 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
96 strcpy(ifr.ifr_name, basedev);
97 ifr.ifr_ifru.ifru_data = (void*)p;
98 fd = socket(AF_INET, SOCK_DGRAM, 0);
99 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
101 bb_perror_msg("ioctl");
107 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
113 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
114 strcpy(ifr.ifr_name, p->name);
116 strcpy(ifr.ifr_name, basedev);
118 ifr.ifr_ifru.ifru_data = (void*)p;
119 fd = socket(AF_INET, SOCK_DGRAM, 0);
120 err = ioctl(fd, cmd, &ifr);
122 bb_perror_msg("ioctl");
128 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
135 strcpy(ifr.ifr_name, p->name);
137 strcpy(ifr.ifr_name, basedev);
139 ifr.ifr_ifru.ifru_data = (void*)p;
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
143 bb_perror_msg("ioctl");
149 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
152 char medium[IFNAMSIZ];
153 memset(p, 0, sizeof(*p));
154 memset(&medium, 0, sizeof(medium));
159 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
161 p->iph.frag_off = htons(IP_DF);
164 if (strcmp(*argv, "mode") == 0) {
166 if (strcmp(*argv, "ipip") == 0 ||
167 strcmp(*argv, "ip/ip") == 0) {
168 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
169 bb_error_msg("You managed to ask for more than one tunnel mode.");
172 p->iph.protocol = IPPROTO_IPIP;
173 } else if (strcmp(*argv, "gre") == 0 ||
174 strcmp(*argv, "gre/ip") == 0) {
175 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
176 bb_error_msg("You managed to ask for more than one tunnel mode.");
179 p->iph.protocol = IPPROTO_GRE;
180 } else if (strcmp(*argv, "sit") == 0 ||
181 strcmp(*argv, "ipv6/ip") == 0) {
182 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
183 bb_error_msg("You managed to ask for more than one tunnel mode.");
186 p->iph.protocol = IPPROTO_IPV6;
188 bb_error_msg("Cannot guess tunnel mode.");
191 } else if (strcmp(*argv, "key") == 0) {
194 p->i_flags |= GRE_KEY;
195 p->o_flags |= GRE_KEY;
196 if (strchr(*argv, '.'))
197 p->i_key = p->o_key = get_addr32(*argv);
199 if (get_unsigned(&uval, *argv, 0)<0) {
200 bb_error_msg("invalid value of \"key\"");
203 p->i_key = p->o_key = htonl(uval);
205 } else if (strcmp(*argv, "ikey") == 0) {
208 p->i_flags |= GRE_KEY;
209 if (strchr(*argv, '.'))
210 p->o_key = get_addr32(*argv);
212 if (get_unsigned(&uval, *argv, 0)<0) {
213 bb_error_msg("invalid value of \"ikey\"");
216 p->i_key = htonl(uval);
218 } else if (strcmp(*argv, "okey") == 0) {
221 p->o_flags |= GRE_KEY;
222 if (strchr(*argv, '.'))
223 p->o_key = get_addr32(*argv);
225 if (get_unsigned(&uval, *argv, 0)<0) {
226 bb_error_msg("invalid value of \"okey\"");
229 p->o_key = htonl(uval);
231 } else if (strcmp(*argv, "seq") == 0) {
232 p->i_flags |= GRE_SEQ;
233 p->o_flags |= GRE_SEQ;
234 } else if (strcmp(*argv, "iseq") == 0) {
235 p->i_flags |= GRE_SEQ;
236 } else if (strcmp(*argv, "oseq") == 0) {
237 p->o_flags |= GRE_SEQ;
238 } else if (strcmp(*argv, "csum") == 0) {
239 p->i_flags |= GRE_CSUM;
240 p->o_flags |= GRE_CSUM;
241 } else if (strcmp(*argv, "icsum") == 0) {
242 p->i_flags |= GRE_CSUM;
243 } else if (strcmp(*argv, "ocsum") == 0) {
244 p->o_flags |= GRE_CSUM;
245 } else if (strcmp(*argv, "nopmtudisc") == 0) {
247 } else if (strcmp(*argv, "pmtudisc") == 0) {
248 p->iph.frag_off = htons(IP_DF);
249 } else if (strcmp(*argv, "remote") == 0) {
251 if (strcmp(*argv, "any"))
252 p->iph.daddr = get_addr32(*argv);
253 } else if (strcmp(*argv, "local") == 0) {
255 if (strcmp(*argv, "any"))
256 p->iph.saddr = get_addr32(*argv);
257 } else if (strcmp(*argv, "dev") == 0) {
259 strncpy(medium, *argv, IFNAMSIZ-1);
260 } else if (strcmp(*argv, "ttl") == 0) {
263 if (strcmp(*argv, "inherit") != 0) {
264 if (get_unsigned(&uval, *argv, 0))
265 invarg("invalid TTL\n", *argv);
267 invarg("TTL must be <=255\n", *argv);
270 } else if (strcmp(*argv, "tos") == 0 ||
271 matches(*argv, "dsfield") == 0) {
274 if (strcmp(*argv, "inherit") != 0) {
275 if (rtnl_dsfield_a2n(&uval, *argv))
276 invarg("bad TOS value", *argv);
281 if (strcmp(*argv, "name") == 0) {
285 duparg2("name", *argv);
286 strncpy(p->name, *argv, IFNAMSIZ);
287 if (cmd == SIOCCHGTUNNEL && count == 0) {
288 struct ip_tunnel_parm old_p;
289 memset(&old_p, 0, sizeof(old_p));
290 if (do_get_ioctl(*argv, &old_p))
300 if (p->iph.protocol == 0) {
301 if (memcmp(p->name, "gre", 3) == 0)
302 p->iph.protocol = IPPROTO_GRE;
303 else if (memcmp(p->name, "ipip", 4) == 0)
304 p->iph.protocol = IPPROTO_IPIP;
305 else if (memcmp(p->name, "sit", 3) == 0)
306 p->iph.protocol = IPPROTO_IPV6;
309 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
310 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
311 bb_error_msg("Keys are not allowed with ipip and sit.");
317 p->link = do_ioctl_get_ifindex(medium);
322 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
323 p->i_key = p->iph.daddr;
324 p->i_flags |= GRE_KEY;
326 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
327 p->o_key = p->iph.daddr;
328 p->o_flags |= GRE_KEY;
330 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
331 bb_error_msg("Broadcast tunnel requires a source address.");
338 static int do_add(int cmd, int argc, char **argv)
340 struct ip_tunnel_parm p;
342 if (parse_args(argc, argv, cmd, &p) < 0)
345 if (p.iph.ttl && p.iph.frag_off == 0) {
346 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
350 switch (p.iph.protocol) {
352 return do_add_ioctl(cmd, "tunl0", &p);
354 return do_add_ioctl(cmd, "gre0", &p);
356 return do_add_ioctl(cmd, "sit0", &p);
358 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
364 int do_del(int argc, char **argv)
366 struct ip_tunnel_parm p;
368 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
371 switch (p.iph.protocol) {
373 return do_del_ioctl("tunl0", &p);
375 return do_del_ioctl("gre0", &p);
377 return do_del_ioctl("sit0", &p);
379 return do_del_ioctl(p.name, &p);
384 void print_tunnel(struct ip_tunnel_parm *p)
391 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
392 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
393 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
394 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
396 printf("%s: %s/ip remote %s local %s ",
398 p->iph.protocol == IPPROTO_IPIP ? "ip" :
399 (p->iph.protocol == IPPROTO_GRE ? "gre" :
400 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
401 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
403 char *n = do_ioctl_get_ifname(p->link);
405 printf(" dev %s ", n);
408 printf(" ttl %d ", p->iph.ttl);
410 printf(" ttl inherit ");
417 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
418 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
420 if (!(p->iph.frag_off&htons(IP_DF)))
421 printf(" nopmtudisc");
423 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
424 printf(" key %s", s3);
425 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
426 if (p->i_flags&GRE_KEY)
427 printf(" ikey %s ", s3);
428 if (p->o_flags&GRE_KEY)
429 printf(" okey %s ", s4);
432 if (p->i_flags&GRE_SEQ)
433 printf("%s Drop packets out of sequence.\n", _SL_);
434 if (p->i_flags&GRE_CSUM)
435 printf("%s Checksum in received packet is required.", _SL_);
436 if (p->o_flags&GRE_SEQ)
437 printf("%s Sequence packets on output.", _SL_);
438 if (p->o_flags&GRE_CSUM)
439 printf("%s Checksum output packets.", _SL_);
442 static int do_tunnels_list(struct ip_tunnel_parm *p)
445 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
447 tx_bytes, tx_packets, tx_errs, tx_drops,
448 tx_fifo, tx_colls, tx_carrier, rx_multi;
450 struct ip_tunnel_parm p1;
453 FILE *fp = fopen("/proc/net/dev", "r");
459 fgets(buf, sizeof(buf), fp);
460 fgets(buf, sizeof(buf), fp);
462 while (fgets(buf, sizeof(buf), fp) != NULL) {
464 buf[sizeof(buf) - 1] = 0;
465 if ((ptr = strchr(buf, ':')) == NULL ||
466 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
467 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
470 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
471 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
472 &rx_fifo, &rx_frame, &rx_multi,
473 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
474 &tx_fifo, &tx_colls, &tx_carrier) != 14)
476 if (p->name[0] && strcmp(p->name, name))
478 type = do_ioctl_get_iftype(name);
480 bb_error_msg("Failed to get type of [%s]", name);
483 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
485 memset(&p1, 0, sizeof(p1));
486 if (do_get_ioctl(name, &p1))
488 if ((p->link && p1.link != p->link) ||
489 (p->name[0] && strcmp(p1.name, p->name)) ||
490 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
491 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
492 (p->i_key && p1.i_key != p->i_key))
500 static int do_show(int argc, char **argv)
503 struct ip_tunnel_parm p;
505 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
508 switch (p.iph.protocol) {
510 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
513 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
516 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
530 int do_iptunnel(int argc, char **argv)
533 if (matches(*argv, "add") == 0)
534 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
535 if (matches(*argv, "change") == 0)
536 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
537 if (matches(*argv, "del") == 0)
538 return do_del(argc-1, argv+1);
539 if (matches(*argv, "show") == 0 ||
540 matches(*argv, "lst") == 0 ||
541 matches(*argv, "list") == 0)
542 return do_show(argc-1, argv+1);
544 return do_show(0, NULL);
546 bb_error_msg("Command \"%s\" is unknown, try \"ip tunnel help\".", *argv);