2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
|
|
|
* iptunnel.c "ip tunnel"
|
|
|
|
*
|
2006-01-12 18:45:49 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
|
|
|
|
* Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
|
|
|
|
*/
|
|
|
|
|
2006-06-03 02:23:38 +05:30
|
|
|
#include "libbb.h"
|
2002-12-16 13:07:21 +05:30
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2002-12-16 13:07:21 +05:30
|
|
|
|
|
|
|
#include <netinet/ip.h>
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2003-01-14 14:24:08 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
|
|
|
#include <asm/types.h>
|
2006-01-12 18:45:49 +05:30
|
|
|
#ifndef __constant_htons
|
2003-01-14 14:24:08 +05:30
|
|
|
#define __constant_htons htons
|
2006-01-12 18:45:49 +05:30
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
#include <linux/if_tunnel.h>
|
|
|
|
|
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
2005-10-09 02:17:15 +05:30
|
|
|
#include "ip_common.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
|
|
|
|
static int do_ioctl_get_ifindex(char *dev)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return ifr.ifr_ifindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_ioctl_get_iftype(char *dev)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return ifr.ifr_addr.sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
static char *do_ioctl_get_ifname(int idx)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
static struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ifr.ifr_ifindex = idx;
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (ioctl(fd, SIOCGIFNAME, &ifr)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-10 07:03:55 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return ifr.ifr_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
|
2002-11-10 07:03:55 +05:30
|
|
|
ifr.ifr_ifru.ifru_data = (void*)p;
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
err = ioctl(fd, SIOCGETTUNNEL, &ifr);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (err) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-28 17:49:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
if (cmd == SIOCCHGTUNNEL && p->name[0]) {
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
|
2002-11-28 17:49:51 +05:30
|
|
|
} else {
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
|
2002-11-28 17:49:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
ifr.ifr_ifru.ifru_data = (void*)p;
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
err = ioctl(fd, cmd, &ifr);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (err) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-28 17:49:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
if (p->name[0]) {
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
|
2002-11-28 17:49:51 +05:30
|
|
|
} else {
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
|
2002-11-28 17:49:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
ifr.ifr_ifru.ifru_data = (void*)p;
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
err = ioctl(fd, SIOCDELTUNNEL, &ifr);
|
2002-11-28 17:49:51 +05:30
|
|
|
if (err) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("ioctl");
|
2002-11-28 17:49:51 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
char medium[IFNAMSIZ];
|
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
memset(&medium, 0, sizeof(medium));
|
|
|
|
|
|
|
|
p->iph.version = 4;
|
|
|
|
p->iph.ihl = 5;
|
|
|
|
#ifndef IP_DF
|
|
|
|
#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
|
|
|
|
#endif
|
|
|
|
p->iph.frag_off = htons(IP_DF);
|
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
if (strcmp(*argv, "mode") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "ipip") == 0 ||
|
|
|
|
strcmp(*argv, "ip/ip") == 0) {
|
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("you managed to ask for more than one tunnel mode");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_IPIP;
|
|
|
|
} else if (strcmp(*argv, "gre") == 0 ||
|
|
|
|
strcmp(*argv, "gre/ip") == 0) {
|
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("you managed to ask for more than one tunnel mode");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_GRE;
|
|
|
|
} else if (strcmp(*argv, "sit") == 0 ||
|
|
|
|
strcmp(*argv, "ipv6/ip") == 0) {
|
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("you managed to ask for more than one tunnel mode");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_IPV6;
|
|
|
|
} else {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot guess tunnel mode");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "key") == 0) {
|
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
p->i_flags |= GRE_KEY;
|
|
|
|
p->o_flags |= GRE_KEY;
|
|
|
|
if (strchr(*argv, '.'))
|
|
|
|
p->i_key = p->o_key = get_addr32(*argv);
|
|
|
|
else {
|
|
|
|
if (get_unsigned(&uval, *argv, 0)<0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("invalid value of \"key\"");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->i_key = p->o_key = htonl(uval);
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "ikey") == 0) {
|
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
p->i_flags |= GRE_KEY;
|
|
|
|
if (strchr(*argv, '.'))
|
|
|
|
p->o_key = get_addr32(*argv);
|
|
|
|
else {
|
|
|
|
if (get_unsigned(&uval, *argv, 0)<0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("invalid value of \"ikey\"");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->i_key = htonl(uval);
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "okey") == 0) {
|
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
p->o_flags |= GRE_KEY;
|
|
|
|
if (strchr(*argv, '.'))
|
|
|
|
p->o_key = get_addr32(*argv);
|
|
|
|
else {
|
|
|
|
if (get_unsigned(&uval, *argv, 0)<0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("invalid value of \"okey\"");
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
p->o_key = htonl(uval);
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "seq") == 0) {
|
|
|
|
p->i_flags |= GRE_SEQ;
|
|
|
|
p->o_flags |= GRE_SEQ;
|
|
|
|
} else if (strcmp(*argv, "iseq") == 0) {
|
|
|
|
p->i_flags |= GRE_SEQ;
|
|
|
|
} else if (strcmp(*argv, "oseq") == 0) {
|
|
|
|
p->o_flags |= GRE_SEQ;
|
|
|
|
} else if (strcmp(*argv, "csum") == 0) {
|
|
|
|
p->i_flags |= GRE_CSUM;
|
|
|
|
p->o_flags |= GRE_CSUM;
|
|
|
|
} else if (strcmp(*argv, "icsum") == 0) {
|
|
|
|
p->i_flags |= GRE_CSUM;
|
|
|
|
} else if (strcmp(*argv, "ocsum") == 0) {
|
|
|
|
p->o_flags |= GRE_CSUM;
|
|
|
|
} else if (strcmp(*argv, "nopmtudisc") == 0) {
|
|
|
|
p->iph.frag_off = 0;
|
|
|
|
} else if (strcmp(*argv, "pmtudisc") == 0) {
|
|
|
|
p->iph.frag_off = htons(IP_DF);
|
|
|
|
} else if (strcmp(*argv, "remote") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "any"))
|
|
|
|
p->iph.daddr = get_addr32(*argv);
|
|
|
|
} else if (strcmp(*argv, "local") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "any"))
|
|
|
|
p->iph.saddr = get_addr32(*argv);
|
|
|
|
} else if (strcmp(*argv, "dev") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
strncpy(medium, *argv, IFNAMSIZ-1);
|
|
|
|
} else if (strcmp(*argv, "ttl") == 0) {
|
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "inherit") != 0) {
|
|
|
|
if (get_unsigned(&uval, *argv, 0))
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "TTL");
|
2002-11-10 07:03:55 +05:30
|
|
|
if (uval > 255)
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "TTL must be <=255");
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.ttl = uval;
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "tos") == 0 ||
|
|
|
|
matches(*argv, "dsfield") == 0) {
|
|
|
|
__u32 uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "inherit") != 0) {
|
|
|
|
if (rtnl_dsfield_a2n(&uval, *argv))
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "TOS");
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.tos = uval;
|
|
|
|
} else
|
|
|
|
p->iph.tos = 1;
|
|
|
|
} else {
|
|
|
|
if (strcmp(*argv, "name") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (p->name[0])
|
|
|
|
duparg2("name", *argv);
|
|
|
|
strncpy(p->name, *argv, IFNAMSIZ);
|
|
|
|
if (cmd == SIOCCHGTUNNEL && count == 0) {
|
|
|
|
struct ip_tunnel_parm old_p;
|
|
|
|
memset(&old_p, 0, sizeof(old_p));
|
|
|
|
if (do_get_ioctl(*argv, &old_p))
|
|
|
|
return -1;
|
|
|
|
*p = old_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (p->iph.protocol == 0) {
|
|
|
|
if (memcmp(p->name, "gre", 3) == 0)
|
|
|
|
p->iph.protocol = IPPROTO_GRE;
|
|
|
|
else if (memcmp(p->name, "ipip", 4) == 0)
|
|
|
|
p->iph.protocol = IPPROTO_IPIP;
|
|
|
|
else if (memcmp(p->name, "sit", 3) == 0)
|
|
|
|
p->iph.protocol = IPPROTO_IPV6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
|
|
|
|
if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("keys are not allowed with ipip and sit");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (medium[0]) {
|
|
|
|
p->link = do_ioctl_get_ifindex(medium);
|
|
|
|
if (p->link == 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
|
|
|
|
p->i_key = p->iph.daddr;
|
|
|
|
p->i_flags |= GRE_KEY;
|
|
|
|
}
|
|
|
|
if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
|
|
|
|
p->o_key = p->iph.daddr;
|
|
|
|
p->o_flags |= GRE_KEY;
|
|
|
|
}
|
|
|
|
if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("broadcast tunnel requires a source address");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int do_add(int cmd, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
|
|
|
if (parse_args(argc, argv, cmd, &p) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (p.iph.ttl && p.iph.frag_off == 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("ttl != 0 and noptmudisc are incompatible");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (p.iph.protocol) {
|
|
|
|
case IPPROTO_IPIP:
|
|
|
|
return do_add_ioctl(cmd, "tunl0", &p);
|
|
|
|
case IPPROTO_GRE:
|
|
|
|
return do_add_ioctl(cmd, "gre0", &p);
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
return do_add_ioctl(cmd, "sit0", &p);
|
2004-03-15 13:59:22 +05:30
|
|
|
default:
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 01:09:00 +05:30
|
|
|
static int do_del(int argc, char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
|
|
|
if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (p.iph.protocol) {
|
|
|
|
case IPPROTO_IPIP:
|
|
|
|
return do_del_ioctl("tunl0", &p);
|
|
|
|
case IPPROTO_GRE:
|
|
|
|
return do_del_ioctl("gre0", &p);
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
return do_del_ioctl("sit0", &p);
|
2004-03-15 13:59:22 +05:30
|
|
|
default:
|
2002-11-10 07:03:55 +05:30
|
|
|
return do_del_ioctl(p.name, &p);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 01:09:00 +05:30
|
|
|
static void print_tunnel(struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
char s1[256];
|
|
|
|
char s2[256];
|
|
|
|
char s3[64];
|
|
|
|
char s4[64];
|
|
|
|
|
|
|
|
format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
|
|
|
|
format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
|
|
|
|
inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
|
|
|
|
inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
|
|
|
|
|
|
|
|
printf("%s: %s/ip remote %s local %s ",
|
|
|
|
p->name,
|
|
|
|
p->iph.protocol == IPPROTO_IPIP ? "ip" :
|
|
|
|
(p->iph.protocol == IPPROTO_GRE ? "gre" :
|
|
|
|
(p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
|
|
|
|
p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
|
|
|
|
if (p->link) {
|
|
|
|
char *n = do_ioctl_get_ifname(p->link);
|
|
|
|
if (n)
|
|
|
|
printf(" dev %s ", n);
|
|
|
|
}
|
|
|
|
if (p->iph.ttl)
|
|
|
|
printf(" ttl %d ", p->iph.ttl);
|
|
|
|
else
|
|
|
|
printf(" ttl inherit ");
|
|
|
|
if (p->iph.tos) {
|
|
|
|
SPRINT_BUF(b1);
|
|
|
|
printf(" tos");
|
|
|
|
if (p->iph.tos&1)
|
|
|
|
printf(" inherit");
|
|
|
|
if (p->iph.tos&~1)
|
|
|
|
printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
|
|
|
|
rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
|
|
|
|
}
|
|
|
|
if (!(p->iph.frag_off&htons(IP_DF)))
|
|
|
|
printf(" nopmtudisc");
|
|
|
|
|
|
|
|
if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
|
|
|
|
printf(" key %s", s3);
|
|
|
|
else if ((p->i_flags|p->o_flags)&GRE_KEY) {
|
|
|
|
if (p->i_flags&GRE_KEY)
|
|
|
|
printf(" ikey %s ", s3);
|
|
|
|
if (p->o_flags&GRE_KEY)
|
|
|
|
printf(" okey %s ", s4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->i_flags&GRE_SEQ)
|
|
|
|
printf("%s Drop packets out of sequence.\n", _SL_);
|
|
|
|
if (p->i_flags&GRE_CSUM)
|
|
|
|
printf("%s Checksum in received packet is required.", _SL_);
|
|
|
|
if (p->o_flags&GRE_SEQ)
|
|
|
|
printf("%s Sequence packets on output.", _SL_);
|
|
|
|
if (p->o_flags&GRE_CSUM)
|
|
|
|
printf("%s Checksum output packets.", _SL_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_tunnels_list(struct ip_tunnel_parm *p)
|
|
|
|
{
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
|
|
|
|
rx_fifo, rx_frame,
|
|
|
|
tx_bytes, tx_packets, tx_errs, tx_drops,
|
|
|
|
tx_fifo, tx_colls, tx_carrier, rx_multi;
|
|
|
|
int type;
|
|
|
|
struct ip_tunnel_parm p1;
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
FILE *fp = fopen("/proc/net/dev", "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror("fopen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fgets(buf, sizeof(buf), fp);
|
|
|
|
fgets(buf, sizeof(buf), fp);
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
char *ptr;
|
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
if ((ptr = strchr(buf, ':')) == NULL ||
|
|
|
|
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("wrong format of /proc/net/dev. Sorry");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2006-01-12 18:45:49 +05:30
|
|
|
if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
|
2002-11-10 07:03:55 +05:30
|
|
|
&rx_bytes, &rx_packets, &rx_errs, &rx_drops,
|
|
|
|
&rx_fifo, &rx_frame, &rx_multi,
|
|
|
|
&tx_bytes, &tx_packets, &tx_errs, &tx_drops,
|
|
|
|
&tx_fifo, &tx_colls, &tx_carrier) != 14)
|
|
|
|
continue;
|
|
|
|
if (p->name[0] && strcmp(p->name, name))
|
|
|
|
continue;
|
|
|
|
type = do_ioctl_get_iftype(name);
|
|
|
|
if (type == -1) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("failed to get type of [%s]", name);
|
2002-11-10 07:03:55 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
|
|
|
|
continue;
|
|
|
|
memset(&p1, 0, sizeof(p1));
|
|
|
|
if (do_get_ioctl(name, &p1))
|
|
|
|
continue;
|
|
|
|
if ((p->link && p1.link != p->link) ||
|
|
|
|
(p->name[0] && strcmp(p1.name, p->name)) ||
|
|
|
|
(p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
|
|
|
|
(p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
|
|
|
|
(p->i_key && p1.i_key != p->i_key))
|
|
|
|
continue;
|
|
|
|
print_tunnel(&p1);
|
2006-10-26 06:07:00 +05:30
|
|
|
puts("");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_show(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
|
|
|
if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (p.iph.protocol) {
|
2004-03-15 13:59:22 +05:30
|
|
|
case IPPROTO_IPIP:
|
2002-11-10 07:03:55 +05:30
|
|
|
err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
|
|
|
|
break;
|
|
|
|
case IPPROTO_GRE:
|
|
|
|
err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
|
|
|
|
break;
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_tunnels_list(&p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
print_tunnel(&p);
|
2006-10-26 06:07:00 +05:30
|
|
|
puts("");
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_iptunnel(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc > 0) {
|
|
|
|
if (matches(*argv, "add") == 0)
|
|
|
|
return do_add(SIOCADDTUNNEL, argc-1, argv+1);
|
|
|
|
if (matches(*argv, "change") == 0)
|
|
|
|
return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
|
|
|
|
if (matches(*argv, "del") == 0)
|
|
|
|
return do_del(argc-1, argv+1);
|
|
|
|
if (matches(*argv, "show") == 0 ||
|
|
|
|
matches(*argv, "lst") == 0 ||
|
|
|
|
matches(*argv, "list") == 0)
|
|
|
|
return do_show(argc-1, argv+1);
|
|
|
|
} else
|
|
|
|
return do_show(0, NULL);
|
|
|
|
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("command \"%s\" is unknown", *argv);
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(-1);
|
|
|
|
}
|