2 * iptunnel.c "ip tunnel"
4 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
13 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
23 #include <netinet/ip.h>
26 #include <net/if_arp.h>
28 #include <asm/types.h>
29 #ifndef __constant_htons
30 #define __constant_htons htons
32 #include <linux/if_tunnel.h>
36 #include "ip_common.h"
39 static int do_ioctl_get_ifindex(char *dev)
44 strcpy(ifr.ifr_name, dev);
45 fd = socket(AF_INET, SOCK_DGRAM, 0);
46 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
47 bb_perror_msg("ioctl");
51 return ifr.ifr_ifindex;
54 static int do_ioctl_get_iftype(char *dev)
59 strcpy(ifr.ifr_name, dev);
60 fd = socket(AF_INET, SOCK_DGRAM, 0);
61 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
62 bb_perror_msg("ioctl");
66 return ifr.ifr_addr.sa_family;
70 static char *do_ioctl_get_ifname(int idx)
72 static struct ifreq ifr;
75 ifr.ifr_ifindex = idx;
76 fd = socket(AF_INET, SOCK_DGRAM, 0);
77 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
78 bb_perror_msg("ioctl");
87 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
93 strcpy(ifr.ifr_name, basedev);
94 ifr.ifr_ifru.ifru_data = (void*)p;
95 fd = socket(AF_INET, SOCK_DGRAM, 0);
96 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
98 bb_perror_msg("ioctl");
104 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
110 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
111 strcpy(ifr.ifr_name, p->name);
113 strcpy(ifr.ifr_name, basedev);
115 ifr.ifr_ifru.ifru_data = (void*)p;
116 fd = socket(AF_INET, SOCK_DGRAM, 0);
117 err = ioctl(fd, cmd, &ifr);
119 bb_perror_msg("ioctl");
125 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
132 strcpy(ifr.ifr_name, p->name);
134 strcpy(ifr.ifr_name, basedev);
136 ifr.ifr_ifru.ifru_data = (void*)p;
137 fd = socket(AF_INET, SOCK_DGRAM, 0);
138 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
140 bb_perror_msg("ioctl");
146 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
149 char medium[IFNAMSIZ];
150 memset(p, 0, sizeof(*p));
151 memset(&medium, 0, sizeof(medium));
156 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
158 p->iph.frag_off = htons(IP_DF);
161 if (strcmp(*argv, "mode") == 0) {
163 if (strcmp(*argv, "ipip") == 0 ||
164 strcmp(*argv, "ip/ip") == 0) {
165 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
166 bb_error_msg("You managed to ask for more than one tunnel mode.");
169 p->iph.protocol = IPPROTO_IPIP;
170 } else if (strcmp(*argv, "gre") == 0 ||
171 strcmp(*argv, "gre/ip") == 0) {
172 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
173 bb_error_msg("You managed to ask for more than one tunnel mode.");
176 p->iph.protocol = IPPROTO_GRE;
177 } else if (strcmp(*argv, "sit") == 0 ||
178 strcmp(*argv, "ipv6/ip") == 0) {
179 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
180 bb_error_msg("You managed to ask for more than one tunnel mode.");
183 p->iph.protocol = IPPROTO_IPV6;
185 bb_error_msg("Cannot guess tunnel mode.");
188 } else if (strcmp(*argv, "key") == 0) {
191 p->i_flags |= GRE_KEY;
192 p->o_flags |= GRE_KEY;
193 if (strchr(*argv, '.'))
194 p->i_key = p->o_key = get_addr32(*argv);
196 if (get_unsigned(&uval, *argv, 0)<0) {
197 bb_error_msg("invalid value of \"key\"");
200 p->i_key = p->o_key = htonl(uval);
202 } else if (strcmp(*argv, "ikey") == 0) {
205 p->i_flags |= GRE_KEY;
206 if (strchr(*argv, '.'))
207 p->o_key = get_addr32(*argv);
209 if (get_unsigned(&uval, *argv, 0)<0) {
210 bb_error_msg("invalid value of \"ikey\"");
213 p->i_key = htonl(uval);
215 } else if (strcmp(*argv, "okey") == 0) {
218 p->o_flags |= GRE_KEY;
219 if (strchr(*argv, '.'))
220 p->o_key = get_addr32(*argv);
222 if (get_unsigned(&uval, *argv, 0)<0) {
223 bb_error_msg("invalid value of \"okey\"");
226 p->o_key = htonl(uval);
228 } else if (strcmp(*argv, "seq") == 0) {
229 p->i_flags |= GRE_SEQ;
230 p->o_flags |= GRE_SEQ;
231 } else if (strcmp(*argv, "iseq") == 0) {
232 p->i_flags |= GRE_SEQ;
233 } else if (strcmp(*argv, "oseq") == 0) {
234 p->o_flags |= GRE_SEQ;
235 } else if (strcmp(*argv, "csum") == 0) {
236 p->i_flags |= GRE_CSUM;
237 p->o_flags |= GRE_CSUM;
238 } else if (strcmp(*argv, "icsum") == 0) {
239 p->i_flags |= GRE_CSUM;
240 } else if (strcmp(*argv, "ocsum") == 0) {
241 p->o_flags |= GRE_CSUM;
242 } else if (strcmp(*argv, "nopmtudisc") == 0) {
244 } else if (strcmp(*argv, "pmtudisc") == 0) {
245 p->iph.frag_off = htons(IP_DF);
246 } else if (strcmp(*argv, "remote") == 0) {
248 if (strcmp(*argv, "any"))
249 p->iph.daddr = get_addr32(*argv);
250 } else if (strcmp(*argv, "local") == 0) {
252 if (strcmp(*argv, "any"))
253 p->iph.saddr = get_addr32(*argv);
254 } else if (strcmp(*argv, "dev") == 0) {
256 strncpy(medium, *argv, IFNAMSIZ-1);
257 } else if (strcmp(*argv, "ttl") == 0) {
260 if (strcmp(*argv, "inherit") != 0) {
261 if (get_unsigned(&uval, *argv, 0))
262 invarg("invalid TTL\n", *argv);
264 invarg("TTL must be <=255\n", *argv);
267 } else if (strcmp(*argv, "tos") == 0 ||
268 matches(*argv, "dsfield") == 0) {
271 if (strcmp(*argv, "inherit") != 0) {
272 if (rtnl_dsfield_a2n(&uval, *argv))
273 invarg("bad TOS value", *argv);
278 if (strcmp(*argv, "name") == 0) {
282 duparg2("name", *argv);
283 strncpy(p->name, *argv, IFNAMSIZ);
284 if (cmd == SIOCCHGTUNNEL && count == 0) {
285 struct ip_tunnel_parm old_p;
286 memset(&old_p, 0, sizeof(old_p));
287 if (do_get_ioctl(*argv, &old_p))
297 if (p->iph.protocol == 0) {
298 if (memcmp(p->name, "gre", 3) == 0)
299 p->iph.protocol = IPPROTO_GRE;
300 else if (memcmp(p->name, "ipip", 4) == 0)
301 p->iph.protocol = IPPROTO_IPIP;
302 else if (memcmp(p->name, "sit", 3) == 0)
303 p->iph.protocol = IPPROTO_IPV6;
306 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
307 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
308 bb_error_msg("Keys are not allowed with ipip and sit.");
314 p->link = do_ioctl_get_ifindex(medium);
319 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
320 p->i_key = p->iph.daddr;
321 p->i_flags |= GRE_KEY;
323 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
324 p->o_key = p->iph.daddr;
325 p->o_flags |= GRE_KEY;
327 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
328 bb_error_msg("Broadcast tunnel requires a source address.");
335 static int do_add(int cmd, int argc, char **argv)
337 struct ip_tunnel_parm p;
339 if (parse_args(argc, argv, cmd, &p) < 0)
342 if (p.iph.ttl && p.iph.frag_off == 0) {
343 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
347 switch (p.iph.protocol) {
349 return do_add_ioctl(cmd, "tunl0", &p);
351 return do_add_ioctl(cmd, "gre0", &p);
353 return do_add_ioctl(cmd, "sit0", &p);
355 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
361 static int do_del(int argc, char **argv)
363 struct ip_tunnel_parm p;
365 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
368 switch (p.iph.protocol) {
370 return do_del_ioctl("tunl0", &p);
372 return do_del_ioctl("gre0", &p);
374 return do_del_ioctl("sit0", &p);
376 return do_del_ioctl(p.name, &p);
381 static void print_tunnel(struct ip_tunnel_parm *p)
388 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
389 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
390 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
391 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
393 printf("%s: %s/ip remote %s local %s ",
395 p->iph.protocol == IPPROTO_IPIP ? "ip" :
396 (p->iph.protocol == IPPROTO_GRE ? "gre" :
397 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
398 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
400 char *n = do_ioctl_get_ifname(p->link);
402 printf(" dev %s ", n);
405 printf(" ttl %d ", p->iph.ttl);
407 printf(" ttl inherit ");
414 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
415 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
417 if (!(p->iph.frag_off&htons(IP_DF)))
418 printf(" nopmtudisc");
420 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
421 printf(" key %s", s3);
422 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
423 if (p->i_flags&GRE_KEY)
424 printf(" ikey %s ", s3);
425 if (p->o_flags&GRE_KEY)
426 printf(" okey %s ", s4);
429 if (p->i_flags&GRE_SEQ)
430 printf("%s Drop packets out of sequence.\n", _SL_);
431 if (p->i_flags&GRE_CSUM)
432 printf("%s Checksum in received packet is required.", _SL_);
433 if (p->o_flags&GRE_SEQ)
434 printf("%s Sequence packets on output.", _SL_);
435 if (p->o_flags&GRE_CSUM)
436 printf("%s Checksum output packets.", _SL_);
439 static int do_tunnels_list(struct ip_tunnel_parm *p)
442 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
444 tx_bytes, tx_packets, tx_errs, tx_drops,
445 tx_fifo, tx_colls, tx_carrier, rx_multi;
447 struct ip_tunnel_parm p1;
450 FILE *fp = fopen("/proc/net/dev", "r");
456 fgets(buf, sizeof(buf), fp);
457 fgets(buf, sizeof(buf), fp);
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 buf[sizeof(buf) - 1] = 0;
462 if ((ptr = strchr(buf, ':')) == NULL ||
463 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
464 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
467 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
468 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
469 &rx_fifo, &rx_frame, &rx_multi,
470 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
471 &tx_fifo, &tx_colls, &tx_carrier) != 14)
473 if (p->name[0] && strcmp(p->name, name))
475 type = do_ioctl_get_iftype(name);
477 bb_error_msg("Failed to get type of [%s]", name);
480 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
482 memset(&p1, 0, sizeof(p1));
483 if (do_get_ioctl(name, &p1))
485 if ((p->link && p1.link != p->link) ||
486 (p->name[0] && strcmp(p1.name, p->name)) ||
487 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
488 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
489 (p->i_key && p1.i_key != p->i_key))
497 static int do_show(int argc, char **argv)
500 struct ip_tunnel_parm p;
502 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
505 switch (p.iph.protocol) {
507 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
510 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
513 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
527 int do_iptunnel(int argc, char **argv)
530 if (matches(*argv, "add") == 0)
531 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
532 if (matches(*argv, "change") == 0)
533 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
534 if (matches(*argv, "del") == 0)
535 return do_del(argc-1, argv+1);
536 if (matches(*argv, "show") == 0 ||
537 matches(*argv, "lst") == 0 ||
538 matches(*argv, "list") == 0)
539 return do_show(argc-1, argv+1);
541 return do_show(0, NULL);
543 bb_error_msg("Command \"%s\" is unknown.", *argv);