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 memset(p, 0, sizeof(*p));
141 memset(&medium, 0, sizeof(medium));
146 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
148 p->iph.frag_off = htons(IP_DF);
151 if (strcmp(*argv, "mode") == 0) {
153 if (strcmp(*argv, "ipip") == 0 ||
154 strcmp(*argv, "ip/ip") == 0) {
155 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
156 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
158 p->iph.protocol = IPPROTO_IPIP;
159 } else if (strcmp(*argv, "gre") == 0 ||
160 strcmp(*argv, "gre/ip") == 0) {
161 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
162 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
164 p->iph.protocol = IPPROTO_GRE;
165 } else if (strcmp(*argv, "sit") == 0 ||
166 strcmp(*argv, "ipv6/ip") == 0) {
167 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
168 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
170 p->iph.protocol = IPPROTO_IPV6;
172 bb_error_msg_and_die("cannot guess tunnel mode");
174 } else if (strcmp(*argv, "key") == 0) {
177 p->i_flags |= GRE_KEY;
178 p->o_flags |= GRE_KEY;
179 if (strchr(*argv, '.'))
180 p->i_key = p->o_key = get_addr32(*argv);
182 if (get_unsigned(&uval, *argv, 0)<0) {
183 bb_error_msg_and_die("invalid value of \"key\"");
185 p->i_key = p->o_key = htonl(uval);
187 } else if (strcmp(*argv, "ikey") == 0) {
190 p->i_flags |= GRE_KEY;
191 if (strchr(*argv, '.'))
192 p->o_key = get_addr32(*argv);
194 if (get_unsigned(&uval, *argv, 0)<0) {
195 bb_error_msg_and_die("invalid value of \"ikey\"");
197 p->i_key = htonl(uval);
199 } else if (strcmp(*argv, "okey") == 0) {
202 p->o_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 \"okey\"");
209 p->o_key = htonl(uval);
211 } else if (strcmp(*argv, "seq") == 0) {
212 p->i_flags |= GRE_SEQ;
213 p->o_flags |= GRE_SEQ;
214 } else if (strcmp(*argv, "iseq") == 0) {
215 p->i_flags |= GRE_SEQ;
216 } else if (strcmp(*argv, "oseq") == 0) {
217 p->o_flags |= GRE_SEQ;
218 } else if (strcmp(*argv, "csum") == 0) {
219 p->i_flags |= GRE_CSUM;
220 p->o_flags |= GRE_CSUM;
221 } else if (strcmp(*argv, "icsum") == 0) {
222 p->i_flags |= GRE_CSUM;
223 } else if (strcmp(*argv, "ocsum") == 0) {
224 p->o_flags |= GRE_CSUM;
225 } else if (strcmp(*argv, "nopmtudisc") == 0) {
227 } else if (strcmp(*argv, "pmtudisc") == 0) {
228 p->iph.frag_off = htons(IP_DF);
229 } else if (strcmp(*argv, "remote") == 0) {
231 if (strcmp(*argv, "any"))
232 p->iph.daddr = get_addr32(*argv);
233 } else if (strcmp(*argv, "local") == 0) {
235 if (strcmp(*argv, "any"))
236 p->iph.saddr = get_addr32(*argv);
237 } else if (strcmp(*argv, "dev") == 0) {
239 strncpy(medium, *argv, IFNAMSIZ-1);
240 } else if (strcmp(*argv, "ttl") == 0) {
243 if (strcmp(*argv, "inherit") != 0) {
244 if (get_unsigned(&uval, *argv, 0))
245 invarg(*argv, "TTL");
247 invarg(*argv, "TTL must be <=255");
250 } else if (strcmp(*argv, "tos") == 0 ||
251 matches(*argv, "dsfield") == 0) {
254 if (strcmp(*argv, "inherit") != 0) {
255 if (rtnl_dsfield_a2n(&uval, *argv))
256 invarg(*argv, "TOS");
261 if (strcmp(*argv, "name") == 0) {
265 duparg2("name", *argv);
266 strncpy(p->name, *argv, IFNAMSIZ);
267 if (cmd == SIOCCHGTUNNEL && count == 0) {
268 struct ip_tunnel_parm old_p;
269 memset(&old_p, 0, sizeof(old_p));
270 if (do_get_ioctl(*argv, &old_p))
280 if (p->iph.protocol == 0) {
281 if (memcmp(p->name, "gre", 3) == 0)
282 p->iph.protocol = IPPROTO_GRE;
283 else if (memcmp(p->name, "ipip", 4) == 0)
284 p->iph.protocol = IPPROTO_IPIP;
285 else if (memcmp(p->name, "sit", 3) == 0)
286 p->iph.protocol = IPPROTO_IPV6;
289 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
290 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
291 bb_error_msg_and_die("keys are not allowed with ipip and sit");
296 p->link = do_ioctl_get_ifindex(medium);
299 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
300 p->i_key = p->iph.daddr;
301 p->i_flags |= GRE_KEY;
303 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
304 p->o_key = p->iph.daddr;
305 p->o_flags |= GRE_KEY;
307 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
308 bb_error_msg_and_die("broadcast tunnel requires a source address");
313 /* Return value becomes exitcode. It's okay to not return at all */
314 static int do_add(int cmd, int argc, char **argv)
316 struct ip_tunnel_parm p;
318 parse_args(argc, argv, cmd, &p);
320 if (p.iph.ttl && p.iph.frag_off == 0) {
321 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
324 switch (p.iph.protocol) {
326 return do_add_ioctl(cmd, "tunl0", &p);
328 return do_add_ioctl(cmd, "gre0", &p);
330 return do_add_ioctl(cmd, "sit0", &p);
332 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
336 /* Return value becomes exitcode. It's okay to not return at all */
337 static int do_del(int argc, char **argv)
339 struct ip_tunnel_parm p;
341 parse_args(argc, argv, SIOCDELTUNNEL, &p);
343 switch (p.iph.protocol) {
345 return do_del_ioctl("tunl0", &p);
347 return do_del_ioctl("gre0", &p);
349 return do_del_ioctl("sit0", &p);
351 return do_del_ioctl(p.name, &p);
355 static void print_tunnel(struct ip_tunnel_parm *p)
362 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
363 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
364 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
365 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
367 printf("%s: %s/ip remote %s local %s ",
369 p->iph.protocol == IPPROTO_IPIP ? "ip" :
370 (p->iph.protocol == IPPROTO_GRE ? "gre" :
371 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
372 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
374 char *n = do_ioctl_get_ifname(p->link);
376 printf(" dev %s ", n);
381 printf(" ttl %d ", p->iph.ttl);
383 printf(" ttl inherit ");
390 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
391 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
393 if (!(p->iph.frag_off & htons(IP_DF)))
394 printf(" nopmtudisc");
396 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
397 printf(" key %s", s3);
398 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
399 if (p->i_flags & GRE_KEY)
400 printf(" ikey %s ", s3);
401 if (p->o_flags & GRE_KEY)
402 printf(" okey %s ", s4);
405 if (p->i_flags & GRE_SEQ)
406 printf("%c Drop packets out of sequence.\n", _SL_);
407 if (p->i_flags & GRE_CSUM)
408 printf("%c Checksum in received packet is required.", _SL_);
409 if (p->o_flags & GRE_SEQ)
410 printf("%c Sequence packets on output.", _SL_);
411 if (p->o_flags & GRE_CSUM)
412 printf("%c Checksum output packets.", _SL_);
415 static void do_tunnels_list(struct ip_tunnel_parm *p)
418 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
420 tx_bytes, tx_packets, tx_errs, tx_drops,
421 tx_fifo, tx_colls, tx_carrier, rx_multi;
423 struct ip_tunnel_parm p1;
425 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
431 fgets(buf, sizeof(buf), fp);
432 fgets(buf, sizeof(buf), fp);
434 while (fgets(buf, sizeof(buf), fp) != NULL) {
437 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
438 ptr = strchr(buf, ':');
440 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
441 bb_error_msg("wrong format of /proc/net/dev. Sorry");
444 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
445 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
446 &rx_fifo, &rx_frame, &rx_multi,
447 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
448 &tx_fifo, &tx_colls, &tx_carrier) != 14)
450 if (p->name[0] && strcmp(p->name, name))
452 type = do_ioctl_get_iftype(name);
454 bb_error_msg("cannot get type of [%s]", name);
457 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
459 memset(&p1, 0, sizeof(p1));
460 if (do_get_ioctl(name, &p1))
462 if ((p->link && p1.link != p->link) ||
463 (p->name[0] && strcmp(p1.name, p->name)) ||
464 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
465 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
466 (p->i_key && p1.i_key != p->i_key))
473 /* Return value becomes exitcode. It's okay to not return at all */
474 static int do_show(int argc, char **argv)
477 struct ip_tunnel_parm p;
479 parse_args(argc, argv, SIOCGETTUNNEL, &p);
481 switch (p.iph.protocol) {
483 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
486 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
489 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
503 /* Return value becomes exitcode. It's okay to not return at all */
504 int do_iptunnel(int argc, char **argv)
507 if (matches(*argv, "add") == 0)
508 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
509 if (matches(*argv, "change") == 0)
510 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
511 if (matches(*argv, "del") == 0)
512 return do_del(argc-1, argv+1);
513 if (matches(*argv, "show") == 0 ||
514 matches(*argv, "lst") == 0 ||
515 matches(*argv, "list") == 0)
516 return do_show(argc-1, argv+1);
518 return do_show(0, NULL);
520 bb_error_msg_and_die("command \"%s\" is unknown", *argv);