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
17 //#include <sys/socket.h>
18 //#include <sys/ioctl.h>
19 #include <netinet/ip.h>
21 #include <net/if_arp.h>
22 #include <asm/types.h>
23 #ifndef __constant_htons
24 #define __constant_htons htons
26 #include <linux/if_tunnel.h>
28 #include "ip_common.h" /* #include "libbb.h" is inside */
34 static int do_ioctl_get_ifindex(char *dev)
39 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
40 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
41 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
42 bb_perror_msg_and_die("SIOCGIFINDEX");
45 return ifr.ifr_ifindex;
48 static int do_ioctl_get_iftype(char *dev)
53 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
54 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
55 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
56 bb_perror_msg("SIOCGIFHWADDR");
60 return ifr.ifr_addr.sa_family;
63 static char *do_ioctl_get_ifname(int idx)
68 ifr.ifr_ifindex = idx;
69 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
70 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
71 bb_perror_msg("SIOCGIFNAME");
75 return xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
78 static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
84 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
85 ifr.ifr_ifru.ifru_data = (void*)p;
86 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
87 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
89 bb_perror_msg("SIOCGETTUNNEL");
95 /* Dies on error, otherwise returns 0 */
96 static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
101 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
102 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
104 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
106 ifr.ifr_ifru.ifru_data = (void*)p;
107 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
108 if (ioctl(fd, cmd, &ifr)) {
109 bb_perror_msg_and_die("ioctl");
115 /* Dies on error, otherwise returns 0 */
116 static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
122 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
124 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
126 ifr.ifr_ifru.ifru_data = (void*)p;
127 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
128 if (ioctl(fd, SIOCDELTUNNEL, &ifr)) {
129 bb_perror_msg_and_die("SIOCDELTUNNEL");
136 static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
139 char medium[IFNAMSIZ];
140 static const char * const keywords[] = {
141 "mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip",
142 "key", "ikey", "okey", "seq", "iseq", "oseq",
143 "csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc",
144 "remote", "any", "local", "dev",
145 "ttl", "inherit", "tos", "dsfield",
149 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
150 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
151 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
152 ARG_remote, ARG_any, ARG_local, ARG_dev,
153 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
157 memset(p, 0, sizeof(*p));
158 memset(&medium, 0, sizeof(medium));
163 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
165 p->iph.frag_off = htons(IP_DF);
168 key = index_in_str_array(keywords, *argv);
169 if (key == ARG_mode) {
171 key = index_in_str_array(keywords, *argv);
172 if (key == ARG_ipip ||
174 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
175 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
177 p->iph.protocol = IPPROTO_IPIP;
178 } else if (key == ARG_gre ||
180 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
181 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
183 p->iph.protocol = IPPROTO_GRE;
184 } else if (key == ARG_sit ||
186 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
187 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
189 p->iph.protocol = IPPROTO_IPV6;
191 bb_error_msg_and_die("cannot guess tunnel mode");
193 } else if (key == ARG_key) {
196 p->i_flags |= GRE_KEY;
197 p->o_flags |= GRE_KEY;
198 if (strchr(*argv, '.'))
199 p->i_key = p->o_key = get_addr32(*argv);
201 if (get_unsigned(&uval, *argv, 0)<0) {
202 bb_error_msg_and_die("invalid value of \"key\"");
204 p->i_key = p->o_key = htonl(uval);
206 } else if (key == ARG_ikey) {
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_and_die("invalid value of \"ikey\"");
216 p->i_key = htonl(uval);
218 } else if (key == ARG_okey) {
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_and_die("invalid value of \"okey\"");
228 p->o_key = htonl(uval);
230 } else if (key == ARG_seq) {
231 p->i_flags |= GRE_SEQ;
232 p->o_flags |= GRE_SEQ;
233 } else if (key == ARG_iseq) {
234 p->i_flags |= GRE_SEQ;
235 } else if (key == ARG_oseq) {
236 p->o_flags |= GRE_SEQ;
237 } else if (key == ARG_csum) {
238 p->i_flags |= GRE_CSUM;
239 p->o_flags |= GRE_CSUM;
240 } else if (key == ARG_icsum) {
241 p->i_flags |= GRE_CSUM;
242 } else if (key == ARG_ocsum) {
243 p->o_flags |= GRE_CSUM;
244 } else if (key == ARG_nopmtudisc) {
246 } else if (key == ARG_pmtudisc) {
247 p->iph.frag_off = htons(IP_DF);
248 } else if (key == ARG_remote) {
250 key = index_in_str_array(keywords, *argv);
252 p->iph.daddr = get_addr32(*argv);
253 } else if (key == ARG_local) {
255 key = index_in_str_array(keywords, *argv);
257 p->iph.saddr = get_addr32(*argv);
258 } else if (key == ARG_dev) {
260 strncpy(medium, *argv, IFNAMSIZ-1);
261 } else if (key == ARG_ttl) {
264 key = index_in_str_array(keywords, *argv);
265 if (key != ARG_inherit) {
266 if (get_unsigned(&uval, *argv, 0))
267 invarg(*argv, "TTL");
269 invarg(*argv, "TTL must be <=255");
272 } else if (key == ARG_tos ||
273 key == ARG_dsfield) {
276 key = index_in_str_array(keywords, *argv);
277 if (key != ARG_inherit) {
278 if (rtnl_dsfield_a2n(&uval, *argv))
279 invarg(*argv, "TOS");
284 if (key == ARG_name) {
288 duparg2("name", *argv);
289 strncpy(p->name, *argv, IFNAMSIZ);
290 if (cmd == SIOCCHGTUNNEL && count == 0) {
291 struct ip_tunnel_parm old_p;
292 memset(&old_p, 0, sizeof(old_p));
293 if (do_get_ioctl(*argv, &old_p))
303 if (p->iph.protocol == 0) {
304 if (memcmp(p->name, "gre", 3) == 0)
305 p->iph.protocol = IPPROTO_GRE;
306 else if (memcmp(p->name, "ipip", 4) == 0)
307 p->iph.protocol = IPPROTO_IPIP;
308 else if (memcmp(p->name, "sit", 3) == 0)
309 p->iph.protocol = IPPROTO_IPV6;
312 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
313 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
314 bb_error_msg_and_die("keys are not allowed with ipip and sit");
319 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_and_die("broadcast tunnel requires a source address");
336 /* Return value becomes exitcode. It's okay to not return at all */
337 static int do_add(int cmd, int argc, char **argv)
339 struct ip_tunnel_parm p;
341 parse_args(argc, argv, cmd, &p);
343 if (p.iph.ttl && p.iph.frag_off == 0) {
344 bb_error_msg_and_die("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_and_die("cannot determine tunnel mode (ipip, gre or sit)");
359 /* Return value becomes exitcode. It's okay to not return at all */
360 static int do_del(int argc, char **argv)
362 struct ip_tunnel_parm p;
364 parse_args(argc, argv, SIOCDELTUNNEL, &p);
366 switch (p.iph.protocol) {
368 return do_del_ioctl("tunl0", &p);
370 return do_del_ioctl("gre0", &p);
372 return do_del_ioctl("sit0", &p);
374 return do_del_ioctl(p.name, &p);
378 static void print_tunnel(struct ip_tunnel_parm *p)
385 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
386 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
387 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
388 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
390 printf("%s: %s/ip remote %s local %s ",
392 p->iph.protocol == IPPROTO_IPIP ? "ip" :
393 (p->iph.protocol == IPPROTO_GRE ? "gre" :
394 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
395 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
397 char *n = do_ioctl_get_ifname(p->link);
399 printf(" dev %s ", n);
404 printf(" ttl %d ", p->iph.ttl);
406 printf(" ttl inherit ");
413 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
414 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
416 if (!(p->iph.frag_off & htons(IP_DF)))
417 printf(" nopmtudisc");
419 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
420 printf(" key %s", s3);
421 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
422 if (p->i_flags & GRE_KEY)
423 printf(" ikey %s ", s3);
424 if (p->o_flags & GRE_KEY)
425 printf(" okey %s ", s4);
428 if (p->i_flags & GRE_SEQ)
429 printf("%c Drop packets out of sequence.\n", _SL_);
430 if (p->i_flags & GRE_CSUM)
431 printf("%c Checksum in received packet is required.", _SL_);
432 if (p->o_flags & GRE_SEQ)
433 printf("%c Sequence packets on output.", _SL_);
434 if (p->o_flags & GRE_CSUM)
435 printf("%c Checksum output packets.", _SL_);
438 static void do_tunnels_list(struct ip_tunnel_parm *p)
441 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
443 tx_bytes, tx_packets, tx_errs, tx_drops,
444 tx_fifo, tx_colls, tx_carrier, rx_multi;
446 struct ip_tunnel_parm p1;
448 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
454 fgets(buf, sizeof(buf), fp);
455 fgets(buf, sizeof(buf), fp);
457 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
461 ptr = strchr(buf, ':');
463 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
464 bb_error_msg("wrong format of /proc/net/dev");
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("cannot 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))
496 /* Return value becomes exitcode. It's okay to not return at all */
497 static int do_show(int argc, char **argv)
500 struct ip_tunnel_parm p;
502 parse_args(argc, argv, SIOCGETTUNNEL, &p);
504 switch (p.iph.protocol) {
506 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
509 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
512 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
526 /* Return value becomes exitcode. It's okay to not return at all */
527 int do_iptunnel(int argc, char **argv)
529 static const char * const keywords[] = {
530 "add", "change", "delete", "show", "list", "lst", NULL
532 enum {ARG_add = 1, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst};
533 smalluint key = 4; /* show */
535 key = index_in_substr_array(keywords, *argv) +1;
539 return do_show(0, NULL);
542 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
545 return do_add(SIOCADDTUNNEL, argc, argv);
546 if (key == ARG_change)
547 return do_add(SIOCCHGTUNNEL, argc, argv);
549 return do_del(argc, argv);
550 if (key == ARG_show || key == ARG_list || key == ARG_lst)
551 return do_show(argc, argv);
552 /* be gentle to gcc; avoid warning about non returning */
553 goto bail; /* never reached */