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>
29 #include <linux/if_arp.h>
30 #include <linux/if_tunnel.h>
37 static int do_ioctl_get_ifindex(char *dev)
42 strcpy(ifr.ifr_name, dev);
43 fd = socket(AF_INET, SOCK_DGRAM, 0);
44 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
49 return ifr.ifr_ifindex;
52 static int do_ioctl_get_iftype(char *dev)
57 strcpy(ifr.ifr_name, dev);
58 fd = socket(AF_INET, SOCK_DGRAM, 0);
59 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
64 return ifr.ifr_addr.sa_family;
68 static char *do_ioctl_get_ifname(int idx)
70 static struct ifreq ifr;
73 ifr.ifr_ifindex = idx;
74 fd = socket(AF_INET, SOCK_DGRAM, 0);
75 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
85 static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
91 strcpy(ifr.ifr_name, basedev);
92 ifr.ifr_ifru.ifru_data = (void*)p;
93 fd = socket(AF_INET, SOCK_DGRAM, 0);
94 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
102 static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
108 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
109 strcpy(ifr.ifr_name, p->name);
111 strcpy(ifr.ifr_name, basedev);
113 ifr.ifr_ifru.ifru_data = (void*)p;
114 fd = socket(AF_INET, SOCK_DGRAM, 0);
115 err = ioctl(fd, cmd, &ifr);
123 static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
130 strcpy(ifr.ifr_name, p->name);
132 strcpy(ifr.ifr_name, basedev);
134 ifr.ifr_ifru.ifru_data = (void*)p;
135 fd = socket(AF_INET, SOCK_DGRAM, 0);
136 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
144 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
147 char medium[IFNAMSIZ];
148 memset(p, 0, sizeof(*p));
149 memset(&medium, 0, sizeof(medium));
154 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
156 p->iph.frag_off = htons(IP_DF);
159 if (strcmp(*argv, "mode") == 0) {
161 if (strcmp(*argv, "ipip") == 0 ||
162 strcmp(*argv, "ip/ip") == 0) {
163 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
164 error_msg("You managed to ask for more than one tunnel mode.");
167 p->iph.protocol = IPPROTO_IPIP;
168 } else if (strcmp(*argv, "gre") == 0 ||
169 strcmp(*argv, "gre/ip") == 0) {
170 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
171 error_msg("You managed to ask for more than one tunnel mode.");
174 p->iph.protocol = IPPROTO_GRE;
175 } else if (strcmp(*argv, "sit") == 0 ||
176 strcmp(*argv, "ipv6/ip") == 0) {
177 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
178 error_msg("You managed to ask for more than one tunnel mode.");
181 p->iph.protocol = IPPROTO_IPV6;
183 error_msg("Cannot guess tunnel mode.");
186 } else if (strcmp(*argv, "key") == 0) {
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 error_msg("invalid value of \"key\"");
198 p->i_key = p->o_key = htonl(uval);
200 } else if (strcmp(*argv, "ikey") == 0) {
203 p->i_flags |= GRE_KEY;
204 if (strchr(*argv, '.'))
205 p->o_key = get_addr32(*argv);
207 if (get_unsigned(&uval, *argv, 0)<0) {
208 error_msg("invalid value of \"ikey\"");
211 p->i_key = htonl(uval);
213 } else if (strcmp(*argv, "okey") == 0) {
216 p->o_flags |= GRE_KEY;
217 if (strchr(*argv, '.'))
218 p->o_key = get_addr32(*argv);
220 if (get_unsigned(&uval, *argv, 0)<0) {
221 error_msg("invalid value of \"okey\"");
224 p->o_key = htonl(uval);
226 } else if (strcmp(*argv, "seq") == 0) {
227 p->i_flags |= GRE_SEQ;
228 p->o_flags |= GRE_SEQ;
229 } else if (strcmp(*argv, "iseq") == 0) {
230 p->i_flags |= GRE_SEQ;
231 } else if (strcmp(*argv, "oseq") == 0) {
232 p->o_flags |= GRE_SEQ;
233 } else if (strcmp(*argv, "csum") == 0) {
234 p->i_flags |= GRE_CSUM;
235 p->o_flags |= GRE_CSUM;
236 } else if (strcmp(*argv, "icsum") == 0) {
237 p->i_flags |= GRE_CSUM;
238 } else if (strcmp(*argv, "ocsum") == 0) {
239 p->o_flags |= GRE_CSUM;
240 } else if (strcmp(*argv, "nopmtudisc") == 0) {
242 } else if (strcmp(*argv, "pmtudisc") == 0) {
243 p->iph.frag_off = htons(IP_DF);
244 } else if (strcmp(*argv, "remote") == 0) {
246 if (strcmp(*argv, "any"))
247 p->iph.daddr = get_addr32(*argv);
248 } else if (strcmp(*argv, "local") == 0) {
250 if (strcmp(*argv, "any"))
251 p->iph.saddr = get_addr32(*argv);
252 } else if (strcmp(*argv, "dev") == 0) {
254 strncpy(medium, *argv, IFNAMSIZ-1);
255 } else if (strcmp(*argv, "ttl") == 0) {
258 if (strcmp(*argv, "inherit") != 0) {
259 if (get_unsigned(&uval, *argv, 0))
260 invarg("invalid TTL\n", *argv);
262 invarg("TTL must be <=255\n", *argv);
265 } else if (strcmp(*argv, "tos") == 0 ||
266 matches(*argv, "dsfield") == 0) {
269 if (strcmp(*argv, "inherit") != 0) {
270 if (rtnl_dsfield_a2n(&uval, *argv))
271 invarg("bad TOS value", *argv);
276 if (strcmp(*argv, "name") == 0) {
280 duparg2("name", *argv);
281 strncpy(p->name, *argv, IFNAMSIZ);
282 if (cmd == SIOCCHGTUNNEL && count == 0) {
283 struct ip_tunnel_parm old_p;
284 memset(&old_p, 0, sizeof(old_p));
285 if (do_get_ioctl(*argv, &old_p))
295 if (p->iph.protocol == 0) {
296 if (memcmp(p->name, "gre", 3) == 0)
297 p->iph.protocol = IPPROTO_GRE;
298 else if (memcmp(p->name, "ipip", 4) == 0)
299 p->iph.protocol = IPPROTO_IPIP;
300 else if (memcmp(p->name, "sit", 3) == 0)
301 p->iph.protocol = IPPROTO_IPV6;
304 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
305 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
306 error_msg("Keys are not allowed with ipip and sit.");
312 p->link = do_ioctl_get_ifindex(medium);
317 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
318 p->i_key = p->iph.daddr;
319 p->i_flags |= GRE_KEY;
321 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
322 p->o_key = p->iph.daddr;
323 p->o_flags |= GRE_KEY;
325 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
326 error_msg("Broadcast tunnel requires a source address.");
333 static int do_add(int cmd, int argc, char **argv)
335 struct ip_tunnel_parm p;
337 if (parse_args(argc, argv, cmd, &p) < 0)
340 if (p.iph.ttl && p.iph.frag_off == 0) {
341 error_msg("ttl != 0 and noptmudisc are incompatible");
345 switch (p.iph.protocol) {
347 return do_add_ioctl(cmd, "tunl0", &p);
349 return do_add_ioctl(cmd, "gre0", &p);
351 return do_add_ioctl(cmd, "sit0", &p);
353 error_msg("cannot determine tunnel mode (ipip, gre or sit)");
359 int do_del(int argc, char **argv)
361 struct ip_tunnel_parm p;
363 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
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);
379 void print_tunnel(struct ip_tunnel_parm *p)
386 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
387 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
388 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
389 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
391 printf("%s: %s/ip remote %s local %s ",
393 p->iph.protocol == IPPROTO_IPIP ? "ip" :
394 (p->iph.protocol == IPPROTO_GRE ? "gre" :
395 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
396 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
398 char *n = do_ioctl_get_ifname(p->link);
400 printf(" dev %s ", n);
403 printf(" ttl %d ", p->iph.ttl);
405 printf(" ttl inherit ");
412 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
413 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
415 if (!(p->iph.frag_off&htons(IP_DF)))
416 printf(" nopmtudisc");
418 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
419 printf(" key %s", s3);
420 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
421 if (p->i_flags&GRE_KEY)
422 printf(" ikey %s ", s3);
423 if (p->o_flags&GRE_KEY)
424 printf(" okey %s ", s4);
427 if (p->i_flags&GRE_SEQ)
428 printf("%s Drop packets out of sequence.\n", _SL_);
429 if (p->i_flags&GRE_CSUM)
430 printf("%s Checksum in received packet is required.", _SL_);
431 if (p->o_flags&GRE_SEQ)
432 printf("%s Sequence packets on output.", _SL_);
433 if (p->o_flags&GRE_CSUM)
434 printf("%s Checksum output packets.", _SL_);
437 static int do_tunnels_list(struct ip_tunnel_parm *p)
440 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
442 tx_bytes, tx_packets, tx_errs, tx_drops,
443 tx_fifo, tx_colls, tx_carrier, rx_multi;
445 struct ip_tunnel_parm p1;
448 FILE *fp = fopen("/proc/net/dev", "r");
454 fgets(buf, sizeof(buf), fp);
455 fgets(buf, sizeof(buf), fp);
457 while (fgets(buf, sizeof(buf), fp) != NULL) {
459 buf[sizeof(buf) - 1] = 0;
460 if ((ptr = strchr(buf, ':')) == NULL ||
461 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
462 error_msg("Wrong format of /proc/net/dev. Sorry.");
465 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
466 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
467 &rx_fifo, &rx_frame, &rx_multi,
468 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
469 &tx_fifo, &tx_colls, &tx_carrier) != 14)
471 if (p->name[0] && strcmp(p->name, name))
473 type = do_ioctl_get_iftype(name);
475 error_msg("Failed to get type of [%s]", name);
478 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
480 memset(&p1, 0, sizeof(p1));
481 if (do_get_ioctl(name, &p1))
483 if ((p->link && p1.link != p->link) ||
484 (p->name[0] && strcmp(p1.name, p->name)) ||
485 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
486 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
487 (p->i_key && p1.i_key != p->i_key))
495 static int do_show(int argc, char **argv)
498 struct ip_tunnel_parm p;
500 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
503 switch (p.iph.protocol) {
505 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
508 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
511 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
525 int do_iptunnel(int argc, char **argv)
528 if (matches(*argv, "add") == 0)
529 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
530 if (matches(*argv, "change") == 0)
531 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
532 if (matches(*argv, "del") == 0)
533 return do_del(argc-1, argv+1);
534 if (matches(*argv, "show") == 0 ||
535 matches(*argv, "lst") == 0 ||
536 matches(*argv, "list") == 0)
537 return do_show(argc-1, argv+1);
539 return do_show(0, NULL);
541 error_msg("Command \"%s\" is unknown, try \"ip tunnel help\".", *argv);