"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * Changes: |
| 8 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 9 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 10 | * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit |
| 11 | * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 14 | #include <netinet/ip.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 15 | #include <net/if.h> |
| 16 | #include <net/if_arp.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 17 | #include <asm/types.h> |
Denis Vlasenko | 196a953 | 2008-12-03 20:16:11 +0000 | [diff] [blame] | 18 | |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 19 | #ifndef __constant_htons |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 20 | #define __constant_htons htons |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 21 | #endif |
Denis Vlasenko | 196a953 | 2008-12-03 20:16:11 +0000 | [diff] [blame] | 22 | |
| 23 | // FYI: #define SIOCDEVPRIVATE 0x89F0 |
| 24 | |
| 25 | /* From linux/if_tunnel.h. #including it proved troublesome |
| 26 | * (redefiniton errors due to name collisions in linux/ and net[inet]/) */ |
| 27 | #define SIOCGETTUNNEL (SIOCDEVPRIVATE + 0) |
| 28 | #define SIOCADDTUNNEL (SIOCDEVPRIVATE + 1) |
| 29 | #define SIOCDELTUNNEL (SIOCDEVPRIVATE + 2) |
| 30 | #define SIOCCHGTUNNEL (SIOCDEVPRIVATE + 3) |
| 31 | //#define SIOCGETPRL (SIOCDEVPRIVATE + 4) |
| 32 | //#define SIOCADDPRL (SIOCDEVPRIVATE + 5) |
| 33 | //#define SIOCDELPRL (SIOCDEVPRIVATE + 6) |
| 34 | //#define SIOCCHGPRL (SIOCDEVPRIVATE + 7) |
| 35 | #define GRE_CSUM __constant_htons(0x8000) |
| 36 | //#define GRE_ROUTING __constant_htons(0x4000) |
| 37 | #define GRE_KEY __constant_htons(0x2000) |
| 38 | #define GRE_SEQ __constant_htons(0x1000) |
| 39 | //#define GRE_STRICT __constant_htons(0x0800) |
| 40 | //#define GRE_REC __constant_htons(0x0700) |
| 41 | //#define GRE_FLAGS __constant_htons(0x00F8) |
| 42 | //#define GRE_VERSION __constant_htons(0x0007) |
| 43 | struct ip_tunnel_parm { |
| 44 | char name[IFNAMSIZ]; |
| 45 | int link; |
| 46 | uint16_t i_flags; |
| 47 | uint16_t o_flags; |
| 48 | uint32_t i_key; |
| 49 | uint32_t o_key; |
| 50 | struct iphdr iph; |
| 51 | }; |
| 52 | /* SIT-mode i_flags */ |
| 53 | //#define SIT_ISATAP 0x0001 |
| 54 | //struct ip_tunnel_prl { |
| 55 | // uint32_t addr; |
| 56 | // uint16_t flags; |
| 57 | // uint16_t __reserved; |
| 58 | // uint32_t datalen; |
| 59 | // uint32_t __reserved2; |
| 60 | // /* data follows */ |
| 61 | //}; |
| 62 | ///* PRL flags */ |
| 63 | //#define PRL_DEFAULT 0x0001 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 65 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | #include "rt_names.h" |
| 67 | #include "utils.h" |
| 68 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 70 | /* Dies on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 71 | static int do_ioctl_get_ifindex(char *dev) |
| 72 | { |
| 73 | struct ifreq ifr; |
| 74 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 76 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 77 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 78 | xioctl(fd, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | close(fd); |
| 80 | return ifr.ifr_ifindex; |
| 81 | } |
| 82 | |
| 83 | static int do_ioctl_get_iftype(char *dev) |
| 84 | { |
| 85 | struct ifreq ifr; |
| 86 | int fd; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 87 | int err; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 89 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 90 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 91 | err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 92 | close(fd); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 93 | return err ? -1 : ifr.ifr_addr.sa_family; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 96 | static char *do_ioctl_get_ifname(int idx) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 98 | struct ifreq ifr; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | int fd; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 100 | int err; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 101 | |
| 102 | ifr.ifr_ifindex = idx; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 103 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 104 | err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 105 | close(fd); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 106 | return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 109 | static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | { |
| 111 | struct ifreq ifr; |
| 112 | int fd; |
| 113 | int err; |
| 114 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 115 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 116 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 117 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 118 | err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | close(fd); |
| 120 | return err; |
| 121 | } |
| 122 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 123 | /* Dies on error, otherwise returns 0 */ |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 124 | static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 125 | { |
| 126 | struct ifreq ifr; |
| 127 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 128 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 129 | if (cmd == SIOCCHGTUNNEL && p->name[0]) { |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 130 | strncpy_IFNAMSIZ(ifr.ifr_name, p->name); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 131 | } else { |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 132 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 133 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 134 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 135 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 136 | #if ENABLE_IOCTL_HEX2STR_ERROR |
| 137 | /* #define magic will turn ioctl# into string */ |
| 138 | if (cmd == SIOCCHGTUNNEL) |
| 139 | xioctl(fd, SIOCCHGTUNNEL, &ifr); |
| 140 | else |
| 141 | xioctl(fd, SIOCADDTUNNEL, &ifr); |
| 142 | #else |
| 143 | xioctl(fd, cmd, &ifr); |
| 144 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | close(fd); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 146 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 149 | /* Dies on error, otherwise returns 0 */ |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 150 | static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | { |
| 152 | struct ifreq ifr; |
| 153 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 154 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 155 | if (p->name[0]) { |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 156 | strncpy_IFNAMSIZ(ifr.ifr_name, p->name); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 157 | } else { |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 158 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 159 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 160 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 161 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 162 | xioctl(fd, SIOCDELTUNNEL, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 163 | close(fd); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 164 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 167 | /* Dies on error */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 168 | static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 169 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 170 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 171 | "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0" |
| 172 | "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0" |
| 173 | "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0" |
| 174 | "remote\0""any\0""local\0""dev\0" |
| 175 | "ttl\0""inherit\0""tos\0""dsfield\0" |
| 176 | "name\0"; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 177 | enum { |
| 178 | ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip, |
| 179 | ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq, |
| 180 | ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc, |
| 181 | ARG_remote, ARG_any, ARG_local, ARG_dev, |
| 182 | ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield, |
| 183 | ARG_name |
| 184 | }; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 185 | int count = 0; |
| 186 | char medium[IFNAMSIZ]; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 187 | int key; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 188 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 189 | memset(p, 0, sizeof(*p)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 190 | medium[0] = '\0'; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 191 | |
| 192 | p->iph.version = 4; |
| 193 | p->iph.ihl = 5; |
| 194 | #ifndef IP_DF |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 195 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 196 | #endif |
| 197 | p->iph.frag_off = htons(IP_DF); |
| 198 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 199 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 200 | key = index_in_strings(keywords, *argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 201 | if (key == ARG_mode) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 203 | key = index_in_strings(keywords, *argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 204 | if (key == ARG_ipip || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 205 | key == ARG_ip_ip |
| 206 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 207 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
Bernhard Reutner-Fischer | 470dc1d | 2008-10-30 17:42:49 +0000 | [diff] [blame] | 208 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | } |
| 210 | p->iph.protocol = IPPROTO_IPIP; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 211 | } else if (key == ARG_gre || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 212 | key == ARG_gre_ip |
| 213 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 214 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
Bernhard Reutner-Fischer | 470dc1d | 2008-10-30 17:42:49 +0000 | [diff] [blame] | 215 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 216 | } |
| 217 | p->iph.protocol = IPPROTO_GRE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 218 | } else if (key == ARG_sit || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 219 | key == ARG_ip6_ip |
| 220 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
Bernhard Reutner-Fischer | 470dc1d | 2008-10-30 17:42:49 +0000 | [diff] [blame] | 222 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 223 | } |
| 224 | p->iph.protocol = IPPROTO_IPV6; |
| 225 | } else { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 226 | bb_error_msg_and_die("%s tunnel mode", "can't guess"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 227 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 228 | } else if (key == ARG_key) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 229 | unsigned uval; |
| 230 | NEXT_ARG(); |
| 231 | p->i_flags |= GRE_KEY; |
| 232 | p->o_flags |= GRE_KEY; |
| 233 | if (strchr(*argv, '.')) |
| 234 | p->i_key = p->o_key = get_addr32(*argv); |
| 235 | else { |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 236 | uval = get_unsigned(*argv, "key"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 237 | p->i_key = p->o_key = htonl(uval); |
| 238 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 239 | } else if (key == ARG_ikey) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 240 | unsigned uval; |
| 241 | NEXT_ARG(); |
| 242 | p->i_flags |= GRE_KEY; |
| 243 | if (strchr(*argv, '.')) |
| 244 | p->o_key = get_addr32(*argv); |
| 245 | else { |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 246 | uval = get_unsigned(*argv, "ikey"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | p->i_key = htonl(uval); |
| 248 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 249 | } else if (key == ARG_okey) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 250 | unsigned uval; |
| 251 | NEXT_ARG(); |
| 252 | p->o_flags |= GRE_KEY; |
| 253 | if (strchr(*argv, '.')) |
| 254 | p->o_key = get_addr32(*argv); |
| 255 | else { |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 256 | uval = get_unsigned(*argv, "okey"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | p->o_key = htonl(uval); |
| 258 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 259 | } else if (key == ARG_seq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 260 | p->i_flags |= GRE_SEQ; |
| 261 | p->o_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 262 | } else if (key == ARG_iseq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 263 | p->i_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 264 | } else if (key == ARG_oseq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 265 | p->o_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 266 | } else if (key == ARG_csum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 267 | p->i_flags |= GRE_CSUM; |
| 268 | p->o_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 269 | } else if (key == ARG_icsum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | p->i_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 271 | } else if (key == ARG_ocsum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | p->o_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 273 | } else if (key == ARG_nopmtudisc) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 274 | p->iph.frag_off = 0; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 275 | } else if (key == ARG_pmtudisc) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 276 | p->iph.frag_off = htons(IP_DF); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 277 | } else if (key == ARG_remote) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 279 | key = index_in_strings(keywords, *argv); |
Denis Vlasenko | db60bcf | 2007-10-10 14:43:03 +0000 | [diff] [blame] | 280 | if (key != ARG_any) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 281 | p->iph.daddr = get_addr32(*argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 282 | } else if (key == ARG_local) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 283 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 284 | key = index_in_strings(keywords, *argv); |
Denis Vlasenko | db60bcf | 2007-10-10 14:43:03 +0000 | [diff] [blame] | 285 | if (key != ARG_any) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 286 | p->iph.saddr = get_addr32(*argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 287 | } else if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 288 | NEXT_ARG(); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 289 | strncpy_IFNAMSIZ(medium, *argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 290 | } else if (key == ARG_ttl) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 291 | unsigned uval; |
| 292 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 293 | key = index_in_strings(keywords, *argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 294 | if (key != ARG_inherit) { |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 295 | uval = get_unsigned(*argv, "TTL"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 296 | if (uval > 255) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 297 | invarg(*argv, "TTL must be <=255"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 298 | p->iph.ttl = uval; |
| 299 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 300 | } else if (key == ARG_tos || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 301 | key == ARG_dsfield |
| 302 | ) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 303 | uint32_t uval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 304 | NEXT_ARG(); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 305 | key = index_in_strings(keywords, *argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 306 | if (key != ARG_inherit) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 307 | if (rtnl_dsfield_a2n(&uval, *argv)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 308 | invarg(*argv, "TOS"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 309 | p->iph.tos = uval; |
| 310 | } else |
| 311 | p->iph.tos = 1; |
| 312 | } else { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 313 | if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 314 | NEXT_ARG(); |
| 315 | } |
| 316 | if (p->name[0]) |
| 317 | duparg2("name", *argv); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 318 | strncpy_IFNAMSIZ(p->name, *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 319 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
| 320 | struct ip_tunnel_parm old_p; |
| 321 | memset(&old_p, 0, sizeof(old_p)); |
| 322 | if (do_get_ioctl(*argv, &old_p)) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 323 | exit(EXIT_FAILURE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 324 | *p = old_p; |
| 325 | } |
| 326 | } |
| 327 | count++; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 328 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 331 | if (p->iph.protocol == 0) { |
| 332 | if (memcmp(p->name, "gre", 3) == 0) |
| 333 | p->iph.protocol = IPPROTO_GRE; |
| 334 | else if (memcmp(p->name, "ipip", 4) == 0) |
| 335 | p->iph.protocol = IPPROTO_IPIP; |
| 336 | else if (memcmp(p->name, "sit", 3) == 0) |
| 337 | p->iph.protocol = IPPROTO_IPV6; |
| 338 | } |
| 339 | |
| 340 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
| 341 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 342 | bb_error_msg_and_die("keys are not allowed with ipip and sit"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | if (medium[0]) { |
| 347 | p->link = do_ioctl_get_ifindex(medium); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 351 | p->i_key = p->iph.daddr; |
| 352 | p->i_flags |= GRE_KEY; |
| 353 | } |
| 354 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 355 | p->o_key = p->iph.daddr; |
| 356 | p->o_flags |= GRE_KEY; |
| 357 | } |
| 358 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 359 | bb_error_msg_and_die("broadcast tunnel requires a source address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 360 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 363 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 364 | static int do_add(int cmd, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 365 | { |
| 366 | struct ip_tunnel_parm p; |
| 367 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 368 | parse_args(argv, cmd, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 369 | |
| 370 | if (p.iph.ttl && p.iph.frag_off == 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 371 | bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | switch (p.iph.protocol) { |
| 375 | case IPPROTO_IPIP: |
| 376 | return do_add_ioctl(cmd, "tunl0", &p); |
| 377 | case IPPROTO_GRE: |
| 378 | return do_add_ioctl(cmd, "gre0", &p); |
| 379 | case IPPROTO_IPV6: |
| 380 | return do_add_ioctl(cmd, "sit0", &p); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 381 | default: |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 382 | bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 383 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 386 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 387 | static int do_del(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 388 | { |
| 389 | struct ip_tunnel_parm p; |
| 390 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 391 | parse_args(argv, SIOCDELTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 392 | |
| 393 | switch (p.iph.protocol) { |
| 394 | case IPPROTO_IPIP: |
| 395 | return do_del_ioctl("tunl0", &p); |
| 396 | case IPPROTO_GRE: |
| 397 | return do_del_ioctl("gre0", &p); |
| 398 | case IPPROTO_IPV6: |
| 399 | return do_del_ioctl("sit0", &p); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 400 | default: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 401 | return do_del_ioctl(p.name, &p); |
| 402 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 405 | static void print_tunnel(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 406 | { |
| 407 | char s1[256]; |
| 408 | char s2[256]; |
| 409 | char s3[64]; |
| 410 | char s4[64]; |
| 411 | |
| 412 | format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)); |
| 413 | format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)); |
| 414 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); |
| 415 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); |
| 416 | |
| 417 | printf("%s: %s/ip remote %s local %s ", |
| 418 | p->name, |
| 419 | p->iph.protocol == IPPROTO_IPIP ? "ip" : |
| 420 | (p->iph.protocol == IPPROTO_GRE ? "gre" : |
| 421 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), |
| 422 | p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any"); |
| 423 | if (p->link) { |
| 424 | char *n = do_ioctl_get_ifname(p->link); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 425 | if (n) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 426 | printf(" dev %s ", n); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 427 | free(n); |
| 428 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 429 | } |
| 430 | if (p->iph.ttl) |
| 431 | printf(" ttl %d ", p->iph.ttl); |
| 432 | else |
| 433 | printf(" ttl inherit "); |
| 434 | if (p->iph.tos) { |
| 435 | SPRINT_BUF(b1); |
| 436 | printf(" tos"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 437 | if (p->iph.tos & 1) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 438 | printf(" inherit"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 439 | if (p->iph.tos & ~1) |
| 440 | printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 441 | rtnl_dsfield_n2a(p->iph.tos & ~1, b1)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 442 | } |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 443 | if (!(p->iph.frag_off & htons(IP_DF))) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 444 | printf(" nopmtudisc"); |
| 445 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 446 | if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 447 | printf(" key %s", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 448 | else if ((p->i_flags | p->o_flags) & GRE_KEY) { |
| 449 | if (p->i_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 450 | printf(" ikey %s ", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 451 | if (p->o_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 452 | printf(" okey %s ", s4); |
| 453 | } |
| 454 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 455 | if (p->i_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 456 | printf("%c Drop packets out of sequence.\n", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 457 | if (p->i_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 458 | printf("%c Checksum in received packet is required.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 459 | if (p->o_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 460 | printf("%c Sequence packets on output.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 461 | if (p->o_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 462 | printf("%c Checksum output packets.", _SL_); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 465 | static void do_tunnels_list(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 466 | { |
| 467 | char name[IFNAMSIZ]; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 468 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 469 | rx_fifo, rx_frame, |
| 470 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 471 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 472 | int type; |
| 473 | struct ip_tunnel_parm p1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 474 | char buf[512]; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 475 | FILE *fp = fopen_or_warn("/proc/net/dev", "r"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 476 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 477 | if (fp == NULL) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 478 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 479 | } |
Bernhard Reutner-Fischer | 470dc1d | 2008-10-30 17:42:49 +0000 | [diff] [blame] | 480 | /* skip headers */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 481 | fgets(buf, sizeof(buf), fp); |
| 482 | fgets(buf, sizeof(buf), fp); |
| 483 | |
| 484 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 485 | char *ptr; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 486 | |
| 487 | /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ |
| 488 | ptr = strchr(buf, ':'); |
| 489 | if (ptr == NULL || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 490 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1) |
| 491 | ) { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 492 | bb_error_msg("wrong format of /proc/net/dev"); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 493 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 494 | } |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 495 | if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 496 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 497 | &rx_fifo, &rx_frame, &rx_multi, |
| 498 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 499 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 500 | continue; |
| 501 | if (p->name[0] && strcmp(p->name, name)) |
| 502 | continue; |
| 503 | type = do_ioctl_get_iftype(name); |
| 504 | if (type == -1) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 505 | bb_error_msg("can't get type of [%s]", name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 506 | continue; |
| 507 | } |
| 508 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) |
| 509 | continue; |
| 510 | memset(&p1, 0, sizeof(p1)); |
| 511 | if (do_get_ioctl(name, &p1)) |
| 512 | continue; |
| 513 | if ((p->link && p1.link != p->link) || |
| 514 | (p->name[0] && strcmp(p1.name, p->name)) || |
| 515 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || |
| 516 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 517 | (p->i_key && p1.i_key != p->i_key) |
| 518 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 519 | continue; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 520 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 521 | print_tunnel(&p1); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 522 | bb_putchar('\n'); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 523 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 526 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 527 | static int do_show(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 528 | { |
| 529 | int err; |
| 530 | struct ip_tunnel_parm p; |
| 531 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 532 | parse_args(argv, SIOCGETTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 533 | |
| 534 | switch (p.iph.protocol) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 535 | case IPPROTO_IPIP: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 536 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
| 537 | break; |
| 538 | case IPPROTO_GRE: |
| 539 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
| 540 | break; |
| 541 | case IPPROTO_IPV6: |
| 542 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
| 543 | break; |
| 544 | default: |
| 545 | do_tunnels_list(&p); |
| 546 | return 0; |
| 547 | } |
| 548 | if (err) |
| 549 | return -1; |
| 550 | |
| 551 | print_tunnel(&p); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 552 | bb_putchar('\n'); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 556 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 557 | int FAST_FUNC do_iptunnel(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 558 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 559 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 560 | "add\0""change\0""delete\0""show\0""list\0""lst\0"; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 561 | enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst }; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 562 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 563 | if (*argv) { |
Denys Vlasenko | 9b58fe9 | 2013-07-15 05:15:46 +0200 | [diff] [blame] | 564 | int key = index_in_substrings(keywords, *argv); |
| 565 | if (key < 0) |
| 566 | invarg(*argv, applet_name); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 567 | argv++; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 568 | if (key == ARG_add) |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 569 | return do_add(SIOCADDTUNNEL, argv); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 570 | if (key == ARG_change) |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 571 | return do_add(SIOCCHGTUNNEL, argv); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 572 | if (key == ARG_del) |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 573 | return do_del(argv); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 574 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 575 | return do_show(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 576 | } |