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>
39 #include "ip_common.h"
43 static int do_ioctl_get_ifindex(char *dev)
48 strcpy(ifr.ifr_name, dev);
49 fd = socket(AF_INET, SOCK_DGRAM, 0);
50 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
51 bb_perror_msg("ioctl");
55 return ifr.ifr_ifindex;
58 static int do_ioctl_get_iftype(char *dev)
63 strcpy(ifr.ifr_name, dev);
64 fd = socket(AF_INET, SOCK_DGRAM, 0);
65 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
66 bb_perror_msg("ioctl");
70 return ifr.ifr_addr.sa_family;
74 static char *do_ioctl_get_ifname(int idx)
76 static struct ifreq ifr;
79 ifr.ifr_ifindex = idx;
80 fd = socket(AF_INET, SOCK_DGRAM, 0);
81 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
82 bb_perror_msg("ioctl");
91 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
97 strcpy(ifr.ifr_name, basedev);
98 ifr.ifr_ifru.ifru_data = (void*)p;
99 fd = socket(AF_INET, SOCK_DGRAM, 0);
100 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
102 bb_perror_msg("ioctl");
108 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
114 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
115 strcpy(ifr.ifr_name, p->name);
117 strcpy(ifr.ifr_name, basedev);
119 ifr.ifr_ifru.ifru_data = (void*)p;
120 fd = socket(AF_INET, SOCK_DGRAM, 0);
121 err = ioctl(fd, cmd, &ifr);
123 bb_perror_msg("ioctl");
129 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
136 strcpy(ifr.ifr_name, p->name);
138 strcpy(ifr.ifr_name, basedev);
140 ifr.ifr_ifru.ifru_data = (void*)p;
141 fd = socket(AF_INET, SOCK_DGRAM, 0);
142 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
144 bb_perror_msg("ioctl");
150 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
153 char medium[IFNAMSIZ];
154 memset(p, 0, sizeof(*p));
155 memset(&medium, 0, sizeof(medium));
160 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
162 p->iph.frag_off = htons(IP_DF);
165 if (strcmp(*argv, "mode") == 0) {
167 if (strcmp(*argv, "ipip") == 0 ||
168 strcmp(*argv, "ip/ip") == 0) {
169 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
170 bb_error_msg("You managed to ask for more than one tunnel mode.");
173 p->iph.protocol = IPPROTO_IPIP;
174 } else if (strcmp(*argv, "gre") == 0 ||
175 strcmp(*argv, "gre/ip") == 0) {
176 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
177 bb_error_msg("You managed to ask for more than one tunnel mode.");
180 p->iph.protocol = IPPROTO_GRE;
181 } else if (strcmp(*argv, "sit") == 0 ||
182 strcmp(*argv, "ipv6/ip") == 0) {
183 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
184 bb_error_msg("You managed to ask for more than one tunnel mode.");
187 p->iph.protocol = IPPROTO_IPV6;
189 bb_error_msg("Cannot guess tunnel mode.");
192 } else if (strcmp(*argv, "key") == 0) {
195 p->i_flags |= GRE_KEY;
196 p->o_flags |= GRE_KEY;
197 if (strchr(*argv, '.'))
198 p->i_key = p->o_key = get_addr32(*argv);
200 if (get_unsigned(&uval, *argv, 0)<0) {
201 bb_error_msg("invalid value of \"key\"");
204 p->i_key = p->o_key = htonl(uval);
206 } else if (strcmp(*argv, "ikey") == 0) {
209 p->i_flags |= GRE_KEY;
210 if (strchr(*argv, '.'))
211 p->o_key = get_addr32(*argv);
213 if (get_unsigned(&uval, *argv, 0)<0) {
214 bb_error_msg("invalid value of \"ikey\"");
217 p->i_key = htonl(uval);
219 } else if (strcmp(*argv, "okey") == 0) {
222 p->o_flags |= GRE_KEY;
223 if (strchr(*argv, '.'))
224 p->o_key = get_addr32(*argv);
226 if (get_unsigned(&uval, *argv, 0)<0) {
227 bb_error_msg("invalid value of \"okey\"");
230 p->o_key = htonl(uval);
232 } else if (strcmp(*argv, "seq") == 0) {
233 p->i_flags |= GRE_SEQ;
234 p->o_flags |= GRE_SEQ;
235 } else if (strcmp(*argv, "iseq") == 0) {
236 p->i_flags |= GRE_SEQ;
237 } else if (strcmp(*argv, "oseq") == 0) {
238 p->o_flags |= GRE_SEQ;
239 } else if (strcmp(*argv, "csum") == 0) {
240 p->i_flags |= GRE_CSUM;
241 p->o_flags |= GRE_CSUM;
242 } else if (strcmp(*argv, "icsum") == 0) {
243 p->i_flags |= GRE_CSUM;
244 } else if (strcmp(*argv, "ocsum") == 0) {
245 p->o_flags |= GRE_CSUM;
246 } else if (strcmp(*argv, "nopmtudisc") == 0) {
248 } else if (strcmp(*argv, "pmtudisc") == 0) {
249 p->iph.frag_off = htons(IP_DF);
250 } else if (strcmp(*argv, "remote") == 0) {
252 if (strcmp(*argv, "any"))
253 p->iph.daddr = get_addr32(*argv);
254 } else if (strcmp(*argv, "local") == 0) {
256 if (strcmp(*argv, "any"))
257 p->iph.saddr = get_addr32(*argv);
258 } else if (strcmp(*argv, "dev") == 0) {
260 strncpy(medium, *argv, IFNAMSIZ-1);
261 } else if (strcmp(*argv, "ttl") == 0) {
264 if (strcmp(*argv, "inherit") != 0) {
265 if (get_unsigned(&uval, *argv, 0))
266 invarg("invalid TTL\n", *argv);
268 invarg("TTL must be <=255\n", *argv);
271 } else if (strcmp(*argv, "tos") == 0 ||
272 matches(*argv, "dsfield") == 0) {
275 if (strcmp(*argv, "inherit") != 0) {
276 if (rtnl_dsfield_a2n(&uval, *argv))
277 invarg("bad TOS value", *argv);
282 if (strcmp(*argv, "name") == 0) {
286 duparg2("name", *argv);
287 strncpy(p->name, *argv, IFNAMSIZ);
288 if (cmd == SIOCCHGTUNNEL && count == 0) {
289 struct ip_tunnel_parm old_p;
290 memset(&old_p, 0, sizeof(old_p));
291 if (do_get_ioctl(*argv, &old_p))
301 if (p->iph.protocol == 0) {
302 if (memcmp(p->name, "gre", 3) == 0)
303 p->iph.protocol = IPPROTO_GRE;
304 else if (memcmp(p->name, "ipip", 4) == 0)
305 p->iph.protocol = IPPROTO_IPIP;
306 else if (memcmp(p->name, "sit", 3) == 0)
307 p->iph.protocol = IPPROTO_IPV6;
310 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
311 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
312 bb_error_msg("Keys are not allowed with ipip and sit.");
318 p->link = do_ioctl_get_ifindex(medium);
323 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
324 p->i_key = p->iph.daddr;
325 p->i_flags |= GRE_KEY;
327 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
328 p->o_key = p->iph.daddr;
329 p->o_flags |= GRE_KEY;
331 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
332 bb_error_msg("Broadcast tunnel requires a source address.");
339 static int do_add(int cmd, int argc, char **argv)
341 struct ip_tunnel_parm p;
343 if (parse_args(argc, argv, cmd, &p) < 0)
346 if (p.iph.ttl && p.iph.frag_off == 0) {
347 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
351 switch (p.iph.protocol) {
353 return do_add_ioctl(cmd, "tunl0", &p);
355 return do_add_ioctl(cmd, "gre0", &p);
357 return do_add_ioctl(cmd, "sit0", &p);
359 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
365 static int do_del(int argc, char **argv)
367 struct ip_tunnel_parm p;
369 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
372 switch (p.iph.protocol) {
374 return do_del_ioctl("tunl0", &p);
376 return do_del_ioctl("gre0", &p);
378 return do_del_ioctl("sit0", &p);
380 return do_del_ioctl(p.name, &p);
385 static void print_tunnel(struct ip_tunnel_parm *p)
392 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
393 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
394 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
395 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
397 printf("%s: %s/ip remote %s local %s ",
399 p->iph.protocol == IPPROTO_IPIP ? "ip" :
400 (p->iph.protocol == IPPROTO_GRE ? "gre" :
401 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
402 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
404 char *n = do_ioctl_get_ifname(p->link);
406 printf(" dev %s ", n);
409 printf(" ttl %d ", p->iph.ttl);
411 printf(" ttl inherit ");
418 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
419 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
421 if (!(p->iph.frag_off&htons(IP_DF)))
422 printf(" nopmtudisc");
424 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
425 printf(" key %s", s3);
426 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
427 if (p->i_flags&GRE_KEY)
428 printf(" ikey %s ", s3);
429 if (p->o_flags&GRE_KEY)
430 printf(" okey %s ", s4);
433 if (p->i_flags&GRE_SEQ)
434 printf("%s Drop packets out of sequence.\n", _SL_);
435 if (p->i_flags&GRE_CSUM)
436 printf("%s Checksum in received packet is required.", _SL_);
437 if (p->o_flags&GRE_SEQ)
438 printf("%s Sequence packets on output.", _SL_);
439 if (p->o_flags&GRE_CSUM)
440 printf("%s Checksum output packets.", _SL_);
443 static int do_tunnels_list(struct ip_tunnel_parm *p)
446 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
448 tx_bytes, tx_packets, tx_errs, tx_drops,
449 tx_fifo, tx_colls, tx_carrier, rx_multi;
451 struct ip_tunnel_parm p1;
454 FILE *fp = fopen("/proc/net/dev", "r");
460 fgets(buf, sizeof(buf), fp);
461 fgets(buf, sizeof(buf), fp);
463 while (fgets(buf, sizeof(buf), fp) != NULL) {
465 buf[sizeof(buf) - 1] = 0;
466 if ((ptr = strchr(buf, ':')) == NULL ||
467 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
468 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
471 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
472 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
473 &rx_fifo, &rx_frame, &rx_multi,
474 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
475 &tx_fifo, &tx_colls, &tx_carrier) != 14)
477 if (p->name[0] && strcmp(p->name, name))
479 type = do_ioctl_get_iftype(name);
481 bb_error_msg("Failed to get type of [%s]", name);
484 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
486 memset(&p1, 0, sizeof(p1));
487 if (do_get_ioctl(name, &p1))
489 if ((p->link && p1.link != p->link) ||
490 (p->name[0] && strcmp(p1.name, p->name)) ||
491 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
492 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
493 (p->i_key && p1.i_key != p->i_key))
501 static int do_show(int argc, char **argv)
504 struct ip_tunnel_parm p;
506 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
509 switch (p.iph.protocol) {
511 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
514 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
517 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
531 int do_iptunnel(int argc, char **argv)
534 if (matches(*argv, "add") == 0)
535 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
536 if (matches(*argv, "change") == 0)
537 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
538 if (matches(*argv, "del") == 0)
539 return do_del(argc-1, argv+1);
540 if (matches(*argv, "show") == 0 ||
541 matches(*argv, "lst") == 0 ||
542 matches(*argv, "list") == 0)
543 return do_show(argc-1, argv+1);
545 return do_show(0, NULL);
547 bb_error_msg("Command \"%s\" is unknown.", *argv);