1 /* vi: set sw=4 ts=4: */
3 * iptunnel.c "ip tunnel"
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
14 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
24 #include <netinet/ip.h>
27 #include <net/if_arp.h>
29 #include <asm/types.h>
30 #ifndef __constant_htons
31 #define __constant_htons htons
33 #include <linux/if_tunnel.h>
37 #include "ip_common.h"
40 static int do_ioctl_get_ifindex(char *dev)
45 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
46 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
47 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
48 bb_perror_msg("ioctl");
52 return ifr.ifr_ifindex;
55 static int do_ioctl_get_iftype(char *dev)
60 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
61 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
62 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
63 bb_perror_msg("ioctl");
67 return ifr.ifr_addr.sa_family;
71 static char *do_ioctl_get_ifname(int idx)
73 static struct ifreq ifr;
76 ifr.ifr_ifindex = idx;
77 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
78 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
79 bb_perror_msg("ioctl");
88 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
94 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
95 ifr.ifr_ifru.ifru_data = (void*)p;
96 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
97 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
99 bb_perror_msg("ioctl");
105 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
111 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
112 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
114 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
116 ifr.ifr_ifru.ifru_data = (void*)p;
117 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
118 err = ioctl(fd, cmd, &ifr);
120 bb_perror_msg("ioctl");
126 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
133 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
135 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
137 ifr.ifr_ifru.ifru_data = (void*)p;
138 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
139 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
141 bb_perror_msg("ioctl");
147 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
150 char medium[IFNAMSIZ];
151 memset(p, 0, sizeof(*p));
152 memset(&medium, 0, sizeof(medium));
157 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
159 p->iph.frag_off = htons(IP_DF);
162 if (strcmp(*argv, "mode") == 0) {
164 if (strcmp(*argv, "ipip") == 0 ||
165 strcmp(*argv, "ip/ip") == 0) {
166 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
167 bb_error_msg("you managed to ask for more than one tunnel mode");
170 p->iph.protocol = IPPROTO_IPIP;
171 } else if (strcmp(*argv, "gre") == 0 ||
172 strcmp(*argv, "gre/ip") == 0) {
173 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
174 bb_error_msg("you managed to ask for more than one tunnel mode");
177 p->iph.protocol = IPPROTO_GRE;
178 } else if (strcmp(*argv, "sit") == 0 ||
179 strcmp(*argv, "ipv6/ip") == 0) {
180 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
181 bb_error_msg("you managed to ask for more than one tunnel mode");
184 p->iph.protocol = IPPROTO_IPV6;
186 bb_error_msg("cannot guess tunnel mode");
189 } else if (strcmp(*argv, "key") == 0) {
192 p->i_flags |= GRE_KEY;
193 p->o_flags |= GRE_KEY;
194 if (strchr(*argv, '.'))
195 p->i_key = p->o_key = get_addr32(*argv);
197 if (get_unsigned(&uval, *argv, 0)<0) {
198 bb_error_msg("invalid value of \"key\"");
201 p->i_key = p->o_key = htonl(uval);
203 } else if (strcmp(*argv, "ikey") == 0) {
206 p->i_flags |= GRE_KEY;
207 if (strchr(*argv, '.'))
208 p->o_key = get_addr32(*argv);
210 if (get_unsigned(&uval, *argv, 0)<0) {
211 bb_error_msg("invalid value of \"ikey\"");
214 p->i_key = htonl(uval);
216 } else if (strcmp(*argv, "okey") == 0) {
219 p->o_flags |= GRE_KEY;
220 if (strchr(*argv, '.'))
221 p->o_key = get_addr32(*argv);
223 if (get_unsigned(&uval, *argv, 0)<0) {
224 bb_error_msg("invalid value of \"okey\"");
227 p->o_key = htonl(uval);
229 } else if (strcmp(*argv, "seq") == 0) {
230 p->i_flags |= GRE_SEQ;
231 p->o_flags |= GRE_SEQ;
232 } else if (strcmp(*argv, "iseq") == 0) {
233 p->i_flags |= GRE_SEQ;
234 } else if (strcmp(*argv, "oseq") == 0) {
235 p->o_flags |= GRE_SEQ;
236 } else if (strcmp(*argv, "csum") == 0) {
237 p->i_flags |= GRE_CSUM;
238 p->o_flags |= GRE_CSUM;
239 } else if (strcmp(*argv, "icsum") == 0) {
240 p->i_flags |= GRE_CSUM;
241 } else if (strcmp(*argv, "ocsum") == 0) {
242 p->o_flags |= GRE_CSUM;
243 } else if (strcmp(*argv, "nopmtudisc") == 0) {
245 } else if (strcmp(*argv, "pmtudisc") == 0) {
246 p->iph.frag_off = htons(IP_DF);
247 } else if (strcmp(*argv, "remote") == 0) {
249 if (strcmp(*argv, "any"))
250 p->iph.daddr = get_addr32(*argv);
251 } else if (strcmp(*argv, "local") == 0) {
253 if (strcmp(*argv, "any"))
254 p->iph.saddr = get_addr32(*argv);
255 } else if (strcmp(*argv, "dev") == 0) {
257 strncpy(medium, *argv, IFNAMSIZ-1);
258 } else if (strcmp(*argv, "ttl") == 0) {
261 if (strcmp(*argv, "inherit") != 0) {
262 if (get_unsigned(&uval, *argv, 0))
263 invarg(*argv, "TTL");
265 invarg(*argv, "TTL must be <=255");
268 } else if (strcmp(*argv, "tos") == 0 ||
269 matches(*argv, "dsfield") == 0) {
272 if (strcmp(*argv, "inherit") != 0) {
273 if (rtnl_dsfield_a2n(&uval, *argv))
274 invarg(*argv, "TOS");
279 if (strcmp(*argv, "name") == 0) {
283 duparg2("name", *argv);
284 strncpy(p->name, *argv, IFNAMSIZ);
285 if (cmd == SIOCCHGTUNNEL && count == 0) {
286 struct ip_tunnel_parm old_p;
287 memset(&old_p, 0, sizeof(old_p));
288 if (do_get_ioctl(*argv, &old_p))
298 if (p->iph.protocol == 0) {
299 if (memcmp(p->name, "gre", 3) == 0)
300 p->iph.protocol = IPPROTO_GRE;
301 else if (memcmp(p->name, "ipip", 4) == 0)
302 p->iph.protocol = IPPROTO_IPIP;
303 else if (memcmp(p->name, "sit", 3) == 0)
304 p->iph.protocol = IPPROTO_IPV6;
307 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
308 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
309 bb_error_msg("keys are not allowed with ipip and sit");
315 p->link = do_ioctl_get_ifindex(medium);
320 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
321 p->i_key = p->iph.daddr;
322 p->i_flags |= GRE_KEY;
324 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
325 p->o_key = p->iph.daddr;
326 p->o_flags |= GRE_KEY;
328 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
329 bb_error_msg("broadcast tunnel requires a source address");
336 static int do_add(int cmd, int argc, char **argv)
338 struct ip_tunnel_parm p;
340 if (parse_args(argc, argv, cmd, &p) < 0)
343 if (p.iph.ttl && p.iph.frag_off == 0) {
344 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
348 switch (p.iph.protocol) {
350 return do_add_ioctl(cmd, "tunl0", &p);
352 return do_add_ioctl(cmd, "gre0", &p);
354 return do_add_ioctl(cmd, "sit0", &p);
356 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
362 static int do_del(int argc, char **argv)
364 struct ip_tunnel_parm p;
366 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
369 switch (p.iph.protocol) {
371 return do_del_ioctl("tunl0", &p);
373 return do_del_ioctl("gre0", &p);
375 return do_del_ioctl("sit0", &p);
377 return do_del_ioctl(p.name, &p);
382 static void print_tunnel(struct ip_tunnel_parm *p)
389 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
390 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
391 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
392 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
394 printf("%s: %s/ip remote %s local %s ",
396 p->iph.protocol == IPPROTO_IPIP ? "ip" :
397 (p->iph.protocol == IPPROTO_GRE ? "gre" :
398 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
399 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
401 char *n = do_ioctl_get_ifname(p->link);
403 printf(" dev %s ", n);
406 printf(" ttl %d ", p->iph.ttl);
408 printf(" ttl inherit ");
415 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
416 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
418 if (!(p->iph.frag_off & htons(IP_DF)))
419 printf(" nopmtudisc");
421 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
422 printf(" key %s", s3);
423 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
424 if (p->i_flags & GRE_KEY)
425 printf(" ikey %s ", s3);
426 if (p->o_flags & GRE_KEY)
427 printf(" okey %s ", s4);
430 if (p->i_flags & GRE_SEQ)
431 printf("%s Drop packets out of sequence.\n", _SL_);
432 if (p->i_flags & GRE_CSUM)
433 printf("%s Checksum in received packet is required.", _SL_);
434 if (p->o_flags & GRE_SEQ)
435 printf("%s Sequence packets on output.", _SL_);
436 if (p->o_flags & GRE_CSUM)
437 printf("%s Checksum output packets.", _SL_);
440 static int do_tunnels_list(struct ip_tunnel_parm *p)
443 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
445 tx_bytes, tx_packets, tx_errs, tx_drops,
446 tx_fifo, tx_colls, tx_carrier, rx_multi;
448 struct ip_tunnel_parm p1;
450 FILE *fp = fopen("/proc/net/dev", "r");
457 fgets(buf, sizeof(buf), fp);
458 fgets(buf, sizeof(buf), fp);
460 while (fgets(buf, sizeof(buf), fp) != NULL) {
463 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
464 ptr = strchr(buf, ':');
466 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
467 bb_error_msg("wrong format of /proc/net/dev. Sorry");
470 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
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_and_die("command \"%s\" is unknown", *argv);