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 <netinet/ip.h>
19 #include <net/if_arp.h>
20 #include <asm/types.h>
21 #ifndef __constant_htons
22 #define __constant_htons htons
24 #include <linux/if_tunnel.h>
26 #include "ip_common.h" /* #include "libbb.h" is inside */
32 static int do_ioctl_get_ifindex(char *dev)
37 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
38 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
39 xioctl(fd, SIOCGIFINDEX, &ifr);
41 return ifr.ifr_ifindex;
44 static int do_ioctl_get_iftype(char *dev)
50 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
51 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
52 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
54 return err ? -1 : ifr.ifr_addr.sa_family;
57 static char *do_ioctl_get_ifname(int idx)
63 ifr.ifr_ifindex = idx;
64 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
65 err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
67 return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
70 static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
76 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
77 ifr.ifr_ifru.ifru_data = (void*)p;
78 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
79 err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
84 /* Dies on error, otherwise returns 0 */
85 static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
90 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
91 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
93 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 #if ENABLE_IOCTL_HEX2STR_ERROR
98 /* #define magic will turn ioctl# into string */
99 if (cmd == SIOCCHGTUNNEL)
100 xioctl(fd, SIOCCHGTUNNEL, &ifr);
102 xioctl(fd, SIOCADDTUNNEL, &ifr);
104 xioctl(fd, cmd, &ifr);
110 /* Dies on error, otherwise returns 0 */
111 static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
117 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
119 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
121 ifr.ifr_ifru.ifru_data = (void*)p;
122 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
123 xioctl(fd, SIOCDELTUNNEL, &ifr);
129 static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
132 char medium[IFNAMSIZ];
133 static const char * const keywords[] = {
134 "mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip",
135 "key", "ikey", "okey", "seq", "iseq", "oseq",
136 "csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc",
137 "remote", "any", "local", "dev",
138 "ttl", "inherit", "tos", "dsfield",
142 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
143 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
144 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
145 ARG_remote, ARG_any, ARG_local, ARG_dev,
146 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
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 key = index_in_str_array(keywords, *argv);
162 if (key == ARG_mode) {
164 key = index_in_str_array(keywords, *argv);
165 if (key == ARG_ipip ||
167 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
168 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
170 p->iph.protocol = IPPROTO_IPIP;
171 } else if (key == ARG_gre ||
173 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
174 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
176 p->iph.protocol = IPPROTO_GRE;
177 } else if (key == ARG_sit ||
179 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
180 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
182 p->iph.protocol = IPPROTO_IPV6;
184 bb_error_msg_and_die("cannot guess tunnel mode");
186 } else if (key == ARG_key) {
189 p->i_flags |= GRE_KEY;
190 p->o_flags |= GRE_KEY;
191 if (strchr(*argv, '.'))
192 p->i_key = p->o_key = get_addr32(*argv);
194 if (get_unsigned(&uval, *argv, 0)<0) {
195 bb_error_msg_and_die("invalid value of \"key\"");
197 p->i_key = p->o_key = htonl(uval);
199 } else if (key == ARG_ikey) {
202 p->i_flags |= GRE_KEY;
203 if (strchr(*argv, '.'))
204 p->o_key = get_addr32(*argv);
206 if (get_unsigned(&uval, *argv, 0)<0) {
207 bb_error_msg_and_die("invalid value of \"ikey\"");
209 p->i_key = htonl(uval);
211 } else if (key == ARG_okey) {
214 p->o_flags |= GRE_KEY;
215 if (strchr(*argv, '.'))
216 p->o_key = get_addr32(*argv);
218 if (get_unsigned(&uval, *argv, 0)<0) {
219 bb_error_msg_and_die("invalid value of \"okey\"");
221 p->o_key = htonl(uval);
223 } else if (key == ARG_seq) {
224 p->i_flags |= GRE_SEQ;
225 p->o_flags |= GRE_SEQ;
226 } else if (key == ARG_iseq) {
227 p->i_flags |= GRE_SEQ;
228 } else if (key == ARG_oseq) {
229 p->o_flags |= GRE_SEQ;
230 } else if (key == ARG_csum) {
231 p->i_flags |= GRE_CSUM;
232 p->o_flags |= GRE_CSUM;
233 } else if (key == ARG_icsum) {
234 p->i_flags |= GRE_CSUM;
235 } else if (key == ARG_ocsum) {
236 p->o_flags |= GRE_CSUM;
237 } else if (key == ARG_nopmtudisc) {
239 } else if (key == ARG_pmtudisc) {
240 p->iph.frag_off = htons(IP_DF);
241 } else if (key == ARG_remote) {
243 key = index_in_str_array(keywords, *argv);
245 p->iph.daddr = get_addr32(*argv);
246 } else if (key == ARG_local) {
248 key = index_in_str_array(keywords, *argv);
250 p->iph.saddr = get_addr32(*argv);
251 } else if (key == ARG_dev) {
253 strncpy(medium, *argv, IFNAMSIZ-1);
254 } else if (key == ARG_ttl) {
257 key = index_in_str_array(keywords, *argv);
258 if (key != ARG_inherit) {
259 if (get_unsigned(&uval, *argv, 0))
260 invarg(*argv, "TTL");
262 invarg(*argv, "TTL must be <=255");
265 } else if (key == ARG_tos ||
266 key == ARG_dsfield) {
269 key = index_in_str_array(keywords, *argv);
270 if (key != ARG_inherit) {
271 if (rtnl_dsfield_a2n(&uval, *argv))
272 invarg(*argv, "TOS");
277 if (key == ARG_name) {
281 duparg2("name", *argv);
282 strncpy(p->name, *argv, IFNAMSIZ);
283 if (cmd == SIOCCHGTUNNEL && count == 0) {
284 struct ip_tunnel_parm old_p;
285 memset(&old_p, 0, sizeof(old_p));
286 if (do_get_ioctl(*argv, &old_p))
296 if (p->iph.protocol == 0) {
297 if (memcmp(p->name, "gre", 3) == 0)
298 p->iph.protocol = IPPROTO_GRE;
299 else if (memcmp(p->name, "ipip", 4) == 0)
300 p->iph.protocol = IPPROTO_IPIP;
301 else if (memcmp(p->name, "sit", 3) == 0)
302 p->iph.protocol = IPPROTO_IPV6;
305 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
306 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
307 bb_error_msg_and_die("keys are not allowed with ipip and sit");
312 p->link = do_ioctl_get_ifindex(medium);
315 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
316 p->i_key = p->iph.daddr;
317 p->i_flags |= GRE_KEY;
319 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
320 p->o_key = p->iph.daddr;
321 p->o_flags |= GRE_KEY;
323 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
324 bb_error_msg_and_die("broadcast tunnel requires a source address");
329 /* Return value becomes exitcode. It's okay to not return at all */
330 static int do_add(int cmd, int argc, char **argv)
332 struct ip_tunnel_parm p;
334 parse_args(argc, argv, cmd, &p);
336 if (p.iph.ttl && p.iph.frag_off == 0) {
337 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
340 switch (p.iph.protocol) {
342 return do_add_ioctl(cmd, "tunl0", &p);
344 return do_add_ioctl(cmd, "gre0", &p);
346 return do_add_ioctl(cmd, "sit0", &p);
348 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
352 /* Return value becomes exitcode. It's okay to not return at all */
353 static int do_del(int argc, char **argv)
355 struct ip_tunnel_parm p;
357 parse_args(argc, argv, SIOCDELTUNNEL, &p);
359 switch (p.iph.protocol) {
361 return do_del_ioctl("tunl0", &p);
363 return do_del_ioctl("gre0", &p);
365 return do_del_ioctl("sit0", &p);
367 return do_del_ioctl(p.name, &p);
371 static void print_tunnel(struct ip_tunnel_parm *p)
378 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
379 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
380 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
381 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
383 printf("%s: %s/ip remote %s local %s ",
385 p->iph.protocol == IPPROTO_IPIP ? "ip" :
386 (p->iph.protocol == IPPROTO_GRE ? "gre" :
387 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
388 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
390 char *n = do_ioctl_get_ifname(p->link);
392 printf(" dev %s ", n);
397 printf(" ttl %d ", p->iph.ttl);
399 printf(" ttl inherit ");
406 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
407 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
409 if (!(p->iph.frag_off & htons(IP_DF)))
410 printf(" nopmtudisc");
412 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
413 printf(" key %s", s3);
414 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
415 if (p->i_flags & GRE_KEY)
416 printf(" ikey %s ", s3);
417 if (p->o_flags & GRE_KEY)
418 printf(" okey %s ", s4);
421 if (p->i_flags & GRE_SEQ)
422 printf("%c Drop packets out of sequence.\n", _SL_);
423 if (p->i_flags & GRE_CSUM)
424 printf("%c Checksum in received packet is required.", _SL_);
425 if (p->o_flags & GRE_SEQ)
426 printf("%c Sequence packets on output.", _SL_);
427 if (p->o_flags & GRE_CSUM)
428 printf("%c Checksum output packets.", _SL_);
431 static void do_tunnels_list(struct ip_tunnel_parm *p)
434 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
436 tx_bytes, tx_packets, tx_errs, tx_drops,
437 tx_fifo, tx_colls, tx_carrier, rx_multi;
439 struct ip_tunnel_parm p1;
441 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
447 fgets(buf, sizeof(buf), fp);
448 fgets(buf, sizeof(buf), fp);
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
454 ptr = strchr(buf, ':');
456 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
457 bb_error_msg("wrong format of /proc/net/dev");
460 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
461 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
462 &rx_fifo, &rx_frame, &rx_multi,
463 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
464 &tx_fifo, &tx_colls, &tx_carrier) != 14)
466 if (p->name[0] && strcmp(p->name, name))
468 type = do_ioctl_get_iftype(name);
470 bb_error_msg("cannot get type of [%s]", name);
473 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
475 memset(&p1, 0, sizeof(p1));
476 if (do_get_ioctl(name, &p1))
478 if ((p->link && p1.link != p->link) ||
479 (p->name[0] && strcmp(p1.name, p->name)) ||
480 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
481 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
482 (p->i_key && p1.i_key != p->i_key))
489 /* Return value becomes exitcode. It's okay to not return at all */
490 static int do_show(int argc, char **argv)
493 struct ip_tunnel_parm p;
495 parse_args(argc, argv, SIOCGETTUNNEL, &p);
497 switch (p.iph.protocol) {
499 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
502 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
505 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
519 /* Return value becomes exitcode. It's okay to not return at all */
520 int do_iptunnel(int argc, char **argv)
522 static const char *const keywords[] = {
523 "add", "change", "delete", "show", "list", "lst", NULL
525 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
529 key = index_in_substr_array(keywords, *argv);
531 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
535 return do_add(SIOCADDTUNNEL, argc, argv);
536 if (key == ARG_change)
537 return do_add(SIOCCHGTUNNEL, argc, argv);
539 return do_del(argc, argv);
541 return do_show(argc, argv);