2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* 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
|
2002-11-10 07:03:55 +05:30
|
|
|
*/
|
|
|
|
|
2002-12-16 13:07:21 +05:30
|
|
|
#include <netinet/ip.h>
|
2003-01-14 14:24:08 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <asm/types.h>
|
2008-12-04 01:46:11 +05:30
|
|
|
|
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
|
2008-12-04 01:46:11 +05:30
|
|
|
|
|
|
|
// FYI: #define SIOCDEVPRIVATE 0x89F0
|
|
|
|
|
|
|
|
/* From linux/if_tunnel.h. #including it proved troublesome
|
|
|
|
* (redefiniton errors due to name collisions in linux/ and net[inet]/) */
|
|
|
|
#define SIOCGETTUNNEL (SIOCDEVPRIVATE + 0)
|
|
|
|
#define SIOCADDTUNNEL (SIOCDEVPRIVATE + 1)
|
|
|
|
#define SIOCDELTUNNEL (SIOCDEVPRIVATE + 2)
|
|
|
|
#define SIOCCHGTUNNEL (SIOCDEVPRIVATE + 3)
|
|
|
|
//#define SIOCGETPRL (SIOCDEVPRIVATE + 4)
|
|
|
|
//#define SIOCADDPRL (SIOCDEVPRIVATE + 5)
|
|
|
|
//#define SIOCDELPRL (SIOCDEVPRIVATE + 6)
|
|
|
|
//#define SIOCCHGPRL (SIOCDEVPRIVATE + 7)
|
|
|
|
#define GRE_CSUM __constant_htons(0x8000)
|
|
|
|
//#define GRE_ROUTING __constant_htons(0x4000)
|
|
|
|
#define GRE_KEY __constant_htons(0x2000)
|
|
|
|
#define GRE_SEQ __constant_htons(0x1000)
|
|
|
|
//#define GRE_STRICT __constant_htons(0x0800)
|
|
|
|
//#define GRE_REC __constant_htons(0x0700)
|
|
|
|
//#define GRE_FLAGS __constant_htons(0x00F8)
|
|
|
|
//#define GRE_VERSION __constant_htons(0x0007)
|
|
|
|
struct ip_tunnel_parm {
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
int link;
|
|
|
|
uint16_t i_flags;
|
|
|
|
uint16_t o_flags;
|
|
|
|
uint32_t i_key;
|
|
|
|
uint32_t o_key;
|
|
|
|
struct iphdr iph;
|
|
|
|
};
|
|
|
|
/* SIT-mode i_flags */
|
|
|
|
//#define SIT_ISATAP 0x0001
|
|
|
|
//struct ip_tunnel_prl {
|
|
|
|
// uint32_t addr;
|
|
|
|
// uint16_t flags;
|
|
|
|
// uint16_t __reserved;
|
|
|
|
// uint32_t datalen;
|
|
|
|
// uint32_t __reserved2;
|
|
|
|
// /* data follows */
|
|
|
|
//};
|
|
|
|
///* PRL flags */
|
|
|
|
//#define PRL_DEFAULT 0x0001
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-06-01 04:12:12 +05:30
|
|
|
#include "ip_common.h" /* #include "libbb.h" is inside */
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Dies on error */
|
2002-11-10 07:03:55 +05:30
|
|
|
static int do_ioctl_get_ifindex(char *dev)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2007-07-15 03:37:14 +05:30
|
|
|
xioctl(fd, SIOCGIFINDEX, &ifr);
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
|
|
|
return ifr.ifr_ifindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_ioctl_get_iftype(char *dev)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
2007-07-15 03:37:14 +05:30
|
|
|
int err;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2007-07-15 03:37:14 +05:30
|
|
|
err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
2007-07-15 03:37:14 +05:30
|
|
|
return err ? -1 : ifr.ifr_addr.sa_family;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
static char *do_ioctl_get_ifname(int idx)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-04-07 06:44:45 +05:30
|
|
|
struct ifreq ifr;
|
2002-11-10 07:03:55 +05:30
|
|
|
int fd;
|
2007-07-15 03:37:14 +05:30
|
|
|
int err;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
ifr.ifr_ifindex = idx;
|
2006-09-03 17:51:59 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
|
2007-07-15 03:37:14 +05:30
|
|
|
err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
2007-07-15 03:37:14 +05:30
|
|
|
return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-01-30 04:21:58 +05:30
|
|
|
static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
|
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);
|
2007-07-15 03:37:14 +05:30
|
|
|
err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Dies on error, otherwise returns 0 */
|
2007-01-30 04:21:58 +05:30
|
|
|
static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
if (cmd == SIOCCHGTUNNEL && p->name[0]) {
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
|
2002-11-28 17:49:51 +05:30
|
|
|
} else {
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
|
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);
|
2007-07-15 03:37:14 +05:30
|
|
|
#if ENABLE_IOCTL_HEX2STR_ERROR
|
|
|
|
/* #define magic will turn ioctl# into string */
|
|
|
|
if (cmd == SIOCCHGTUNNEL)
|
|
|
|
xioctl(fd, SIOCCHGTUNNEL, &ifr);
|
|
|
|
else
|
|
|
|
xioctl(fd, SIOCADDTUNNEL, &ifr);
|
|
|
|
#else
|
|
|
|
xioctl(fd, cmd, &ifr);
|
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
2007-04-07 06:44:45 +05:30
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Dies on error, otherwise returns 0 */
|
2007-01-30 04:21:58 +05:30
|
|
|
static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2002-11-28 17:49:51 +05:30
|
|
|
if (p->name[0]) {
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
|
2002-11-28 17:49:51 +05:30
|
|
|
} else {
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
|
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);
|
2007-07-15 03:37:14 +05:30
|
|
|
xioctl(fd, SIOCDELTUNNEL, &ifr);
|
2002-11-10 07:03:55 +05:30
|
|
|
close(fd);
|
2007-04-07 06:44:45 +05:30
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Dies on error */
|
2007-11-19 04:26:25 +05:30
|
|
|
static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2007-07-24 21:24:42 +05:30
|
|
|
"mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
|
|
|
|
"key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
|
|
|
|
"csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
|
|
|
|
"remote\0""any\0""local\0""dev\0"
|
|
|
|
"ttl\0""inherit\0""tos\0""dsfield\0"
|
|
|
|
"name\0";
|
2007-06-21 15:50:13 +05:30
|
|
|
enum {
|
|
|
|
ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
|
|
|
|
ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
|
|
|
|
ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
|
|
|
|
ARG_remote, ARG_any, ARG_local, ARG_dev,
|
|
|
|
ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
|
|
|
|
ARG_name
|
|
|
|
};
|
2007-07-24 21:24:42 +05:30
|
|
|
int count = 0;
|
|
|
|
char medium[IFNAMSIZ];
|
2007-06-21 15:50:13 +05:30
|
|
|
int key;
|
2007-07-24 21:24:42 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
memset(p, 0, sizeof(*p));
|
2008-12-02 23:48:50 +05:30
|
|
|
medium[0] = '\0';
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
p->iph.version = 4;
|
|
|
|
p->iph.ihl = 5;
|
|
|
|
#ifndef IP_DF
|
2007-07-24 21:24:42 +05:30
|
|
|
#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
|
2002-11-10 07:03:55 +05:30
|
|
|
#endif
|
|
|
|
p->iph.frag_off = htons(IP_DF);
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
while (*argv) {
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
if (key == ARG_mode) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
if (key == ARG_ipip ||
|
2010-01-28 06:54:24 +05:30
|
|
|
key == ARG_ip_ip
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
|
2008-10-30 23:12:49 +05:30
|
|
|
bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_IPIP;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_gre ||
|
2010-01-28 06:54:24 +05:30
|
|
|
key == ARG_gre_ip
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
|
2008-10-30 23:12:49 +05:30
|
|
|
bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_GRE;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_sit ||
|
2010-01-28 06:54:24 +05:30
|
|
|
key == ARG_ip6_ip
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
|
2008-10-30 23:12:49 +05:30
|
|
|
bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
p->iph.protocol = IPPROTO_IPV6;
|
|
|
|
} else {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_error_msg_and_die("%s tunnel mode", "can't guess");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_key) {
|
2002-11-10 07:03:55 +05:30
|
|
|
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 {
|
2009-03-05 14:51:57 +05:30
|
|
|
uval = get_unsigned(*argv, "key");
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_key = p->o_key = htonl(uval);
|
|
|
|
}
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_ikey) {
|
2002-11-10 07:03:55 +05:30
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
p->i_flags |= GRE_KEY;
|
|
|
|
if (strchr(*argv, '.'))
|
|
|
|
p->o_key = get_addr32(*argv);
|
|
|
|
else {
|
2009-03-05 14:51:57 +05:30
|
|
|
uval = get_unsigned(*argv, "ikey");
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_key = htonl(uval);
|
|
|
|
}
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_okey) {
|
2002-11-10 07:03:55 +05:30
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
|
|
|
p->o_flags |= GRE_KEY;
|
|
|
|
if (strchr(*argv, '.'))
|
|
|
|
p->o_key = get_addr32(*argv);
|
|
|
|
else {
|
2009-03-05 14:51:57 +05:30
|
|
|
uval = get_unsigned(*argv, "okey");
|
2002-11-10 07:03:55 +05:30
|
|
|
p->o_key = htonl(uval);
|
|
|
|
}
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_seq) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_flags |= GRE_SEQ;
|
|
|
|
p->o_flags |= GRE_SEQ;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_iseq) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_flags |= GRE_SEQ;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_oseq) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->o_flags |= GRE_SEQ;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_csum) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_flags |= GRE_CSUM;
|
|
|
|
p->o_flags |= GRE_CSUM;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_icsum) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->i_flags |= GRE_CSUM;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_ocsum) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->o_flags |= GRE_CSUM;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_nopmtudisc) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.frag_off = 0;
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_pmtudisc) {
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.frag_off = htons(IP_DF);
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_remote) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-10-10 20:13:03 +05:30
|
|
|
if (key != ARG_any)
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.daddr = get_addr32(*argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_local) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-10-10 20:13:03 +05:30
|
|
|
if (key != ARG_any)
|
2002-11-10 07:03:55 +05:30
|
|
|
p->iph.saddr = get_addr32(*argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_dev) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(medium, *argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_ttl) {
|
2002-11-10 07:03:55 +05:30
|
|
|
unsigned uval;
|
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
if (key != ARG_inherit) {
|
2009-03-05 14:51:57 +05:30
|
|
|
uval = get_unsigned(*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;
|
|
|
|
}
|
2007-06-21 15:50:13 +05:30
|
|
|
} else if (key == ARG_tos ||
|
2010-01-28 06:54:24 +05:30
|
|
|
key == ARG_dsfield
|
|
|
|
) {
|
2007-01-01 00:27:37 +05:30
|
|
|
uint32_t uval;
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
key = index_in_strings(keywords, *argv);
|
2007-06-21 15:50:13 +05:30
|
|
|
if (key != ARG_inherit) {
|
2002-11-10 07:03:55 +05:30
|
|
|
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 {
|
2007-06-21 15:50:13 +05:30
|
|
|
if (key == ARG_name) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (p->name[0])
|
|
|
|
duparg2("name", *argv);
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(p->name, *argv);
|
2002-11-10 07:03:55 +05:30
|
|
|
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))
|
2008-05-19 14:59:47 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2002-11-10 07:03:55 +05:30
|
|
|
*p = old_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
2007-04-07 06:44:45 +05:30
|
|
|
argv++;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("keys are not allowed with ipip and sit");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (medium[0]) {
|
|
|
|
p->link = do_ioctl_get_ifindex(medium);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("broadcast tunnel requires a source address");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int do_add(int cmd, char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
parse_args(argv, cmd, &p);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (p.iph.ttl && p.iph.frag_off == 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int do_del(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
parse_args(argv, SIOCDELTUNNEL, &p);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2007-04-07 06:44:45 +05:30
|
|
|
if (n) {
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" dev %s ", n);
|
2007-04-07 06:44:45 +05:30
|
|
|
free(n);
|
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (p->iph.ttl)
|
|
|
|
printf(" ttl %d ", p->iph.ttl);
|
|
|
|
else
|
|
|
|
printf(" ttl inherit ");
|
|
|
|
if (p->iph.tos) {
|
|
|
|
SPRINT_BUF(b1);
|
|
|
|
printf(" tos");
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->iph.tos & 1)
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" inherit");
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->iph.tos & ~1)
|
|
|
|
printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
|
2013-01-14 20:27:44 +05:30
|
|
|
rtnl_dsfield_n2a(p->iph.tos & ~1, b1));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-01-02 22:02:16 +05:30
|
|
|
if (!(p->iph.frag_off & htons(IP_DF)))
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" nopmtudisc");
|
|
|
|
|
2007-01-02 22:02:16 +05:30
|
|
|
if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" key %s", s3);
|
2007-01-02 22:02:16 +05:30
|
|
|
else if ((p->i_flags | p->o_flags) & GRE_KEY) {
|
|
|
|
if (p->i_flags & GRE_KEY)
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" ikey %s ", s3);
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->o_flags & GRE_KEY)
|
2002-11-10 07:03:55 +05:30
|
|
|
printf(" okey %s ", s4);
|
|
|
|
}
|
|
|
|
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->i_flags & GRE_SEQ)
|
2007-04-07 06:44:45 +05:30
|
|
|
printf("%c Drop packets out of sequence.\n", _SL_);
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->i_flags & GRE_CSUM)
|
2007-04-07 06:44:45 +05:30
|
|
|
printf("%c Checksum in received packet is required.", _SL_);
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->o_flags & GRE_SEQ)
|
2007-04-07 06:44:45 +05:30
|
|
|
printf("%c Sequence packets on output.", _SL_);
|
2007-01-02 22:02:16 +05:30
|
|
|
if (p->o_flags & GRE_CSUM)
|
2007-04-07 06:44:45 +05:30
|
|
|
printf("%c Checksum output packets.", _SL_);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
static void do_tunnels_list(struct ip_tunnel_parm *p)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
char name[IFNAMSIZ];
|
2007-01-02 22:02:16 +05:30
|
|
|
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;
|
2002-11-10 07:03:55 +05:30
|
|
|
int type;
|
|
|
|
struct ip_tunnel_parm p1;
|
|
|
|
char buf[512];
|
2007-04-12 04:50:53 +05:30
|
|
|
FILE *fp = fopen_or_warn("/proc/net/dev", "r");
|
2007-01-02 22:02:16 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (fp == NULL) {
|
2007-04-07 06:44:45 +05:30
|
|
|
return;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-10-30 23:12:49 +05:30
|
|
|
/* skip headers */
|
2002-11-10 07:03:55 +05:30
|
|
|
fgets(buf, sizeof(buf), fp);
|
|
|
|
fgets(buf, sizeof(buf), fp);
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
char *ptr;
|
2007-01-02 22:02:16 +05:30
|
|
|
|
|
|
|
/*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
|
|
|
|
ptr = strchr(buf, ':');
|
|
|
|
if (ptr == NULL ||
|
2010-01-28 06:54:24 +05:30
|
|
|
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)
|
|
|
|
) {
|
2007-06-21 15:50:13 +05:30
|
|
|
bb_error_msg("wrong format of /proc/net/dev");
|
2007-04-07 06:44:45 +05:30
|
|
|
return;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
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) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_error_msg("can't 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) ||
|
2010-01-28 06:54:24 +05:30
|
|
|
(p->i_key && p1.i_key != p->i_key)
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
continue;
|
2010-01-28 06:54:24 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
print_tunnel(&p1);
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int do_show(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ip_tunnel_parm p;
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
parse_args(argv, SIOCGETTUNNEL, &p);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
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);
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2010-07-25 02:57:38 +05:30
|
|
|
int FAST_FUNC do_iptunnel(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2007-07-24 21:24:42 +05:30
|
|
|
"add\0""change\0""delete\0""show\0""list\0""lst\0";
|
2007-07-15 03:37:14 +05:30
|
|
|
enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
if (*argv) {
|
2010-05-25 20:49:35 +05:30
|
|
|
smalluint key = index_in_substrings(keywords, *argv);
|
|
|
|
if (key > 5)
|
2007-07-15 03:37:14 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
|
2007-11-19 04:26:25 +05:30
|
|
|
argv++;
|
2007-07-15 03:37:14 +05:30
|
|
|
if (key == ARG_add)
|
2007-11-19 04:26:25 +05:30
|
|
|
return do_add(SIOCADDTUNNEL, argv);
|
2007-07-15 03:37:14 +05:30
|
|
|
if (key == ARG_change)
|
2007-11-19 04:26:25 +05:30
|
|
|
return do_add(SIOCCHGTUNNEL, argv);
|
2007-07-15 03:37:14 +05:30
|
|
|
if (key == ARG_del)
|
2007-11-19 04:26:25 +05:30
|
|
|
return do_del(argv);
|
2007-07-15 03:37:14 +05:30
|
|
|
}
|
2007-11-19 04:26:25 +05:30
|
|
|
return do_show(argv);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|