blob: eb136e435d837d9b46ccae1d608aeb887b80a977 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 * Changes:
8 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02009 * 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 McGrath9a2d2722002-11-10 01:33:55 +000012 */
13
Glenn L McGrath275be872002-12-16 07:37:21 +000014#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000015#include <net/if.h>
16#include <net/if_arp.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000017#include <asm/types.h>
Denis Vlasenko196a9532008-12-03 20:16:11 +000018
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000019#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000020#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000021#endif
Denis Vlasenko196a9532008-12-03 20:16:11 +000022
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)
43struct 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 McGrath9a2d2722002-11-10 01:33:55 +000064
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000065#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066#include "rt_names.h"
67#include "utils.h"
68
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069
Denis Vlasenko540a2a12007-04-07 01:14:45 +000070/* Dies on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071static int do_ioctl_get_ifindex(char *dev)
72{
73 struct ifreq ifr;
74 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075
Denis Vlasenko360d9662008-12-02 18:18:50 +000076 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000077 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000078 xioctl(fd, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 close(fd);
80 return ifr.ifr_ifindex;
81}
82
83static int do_ioctl_get_iftype(char *dev)
84{
85 struct ifreq ifr;
86 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000087 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000088
Denis Vlasenko360d9662008-12-02 18:18:50 +000089 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000090 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000091 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000093 return err ? -1 : ifr.ifr_addr.sa_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094}
95
Glenn L McGrath8b602442002-11-28 12:19:51 +000096static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097{
Denis Vlasenko540a2a12007-04-07 01:14:45 +000098 struct ifreq ifr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000100 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101
102 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000103 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000104 err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000106 return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107}
108
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000109static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110{
111 struct ifreq ifr;
112 int fd;
113 int err;
114
Denis Vlasenko360d9662008-12-02 18:18:50 +0000115 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000117 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000118 err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 close(fd);
120 return err;
121}
122
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000123/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000124static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125{
126 struct ifreq ifr;
127 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128
Glenn L McGrath8b602442002-11-28 12:19:51 +0000129 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000130 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000131 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000132 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000133 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000135 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000136#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 McGrath9a2d2722002-11-10 01:33:55 +0000145 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000146 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147}
148
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000149/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000150static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151{
152 struct ifreq ifr;
153 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154
Glenn L McGrath8b602442002-11-28 12:19:51 +0000155 if (p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000156 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000157 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000158 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000159 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000161 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000162 xioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000164 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165}
166
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000167/* Dies on error */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000168static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000170 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000171 "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-Fischer789b87e2007-06-21 10:20:13 +0000177 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 Vlasenko990d0f62007-07-24 15:54:42 +0000185 int count = 0;
186 char medium[IFNAMSIZ];
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000187 int key;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000188
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 memset(p, 0, sizeof(*p));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000190 medium[0] = '\0';
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191
192 p->iph.version = 4;
193 p->iph.ihl = 5;
194#ifndef IP_DF
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000195#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196#endif
197 p->iph.frag_off = htons(IP_DF);
198
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000199 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000200 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000201 if (key == ARG_mode) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000203 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000204 if (key == ARG_ipip ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100205 key == ARG_ip_ip
206 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000208 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 }
210 p->iph.protocol = IPPROTO_IPIP;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000211 } else if (key == ARG_gre ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100212 key == ARG_gre_ip
213 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000215 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216 }
217 p->iph.protocol = IPPROTO_GRE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000218 } else if (key == ARG_sit ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100219 key == ARG_ip6_ip
220 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000222 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223 }
224 p->iph.protocol = IPPROTO_IPV6;
225 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100226 bb_error_msg_and_die("%s tunnel mode", "can't guess");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000228 } else if (key == ARG_key) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 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 Vlasenko76140a72009-03-05 09:21:57 +0000236 uval = get_unsigned(*argv, "key");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237 p->i_key = p->o_key = htonl(uval);
238 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000239 } else if (key == ARG_ikey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240 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 Vlasenko76140a72009-03-05 09:21:57 +0000246 uval = get_unsigned(*argv, "ikey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 p->i_key = htonl(uval);
248 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000249 } else if (key == ARG_okey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250 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 Vlasenko76140a72009-03-05 09:21:57 +0000256 uval = get_unsigned(*argv, "okey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 p->o_key = htonl(uval);
258 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000259 } else if (key == ARG_seq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000260 p->i_flags |= GRE_SEQ;
261 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000262 } else if (key == ARG_iseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 p->i_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000264 } else if (key == ARG_oseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000265 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000266 } else if (key == ARG_csum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 p->i_flags |= GRE_CSUM;
268 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000269 } else if (key == ARG_icsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 p->i_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000271 } else if (key == ARG_ocsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000273 } else if (key == ARG_nopmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 p->iph.frag_off = 0;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000275 } else if (key == ARG_pmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 p->iph.frag_off = htons(IP_DF);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000277 } else if (key == ARG_remote) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000279 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000280 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 p->iph.daddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000282 } else if (key == ARG_local) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000284 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000285 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286 p->iph.saddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000287 } else if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 NEXT_ARG();
Denis Vlasenko360d9662008-12-02 18:18:50 +0000289 strncpy_IFNAMSIZ(medium, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000290 } else if (key == ARG_ttl) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000291 unsigned uval;
292 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000293 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000294 if (key != ARG_inherit) {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000295 uval = get_unsigned(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 if (uval > 255)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200297 invarg_1_to_2(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 p->iph.ttl = uval;
299 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000300 } else if (key == ARG_tos ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100301 key == ARG_dsfield
302 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000303 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000305 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000306 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307 if (rtnl_dsfield_a2n(&uval, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200308 invarg_1_to_2(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 p->iph.tos = uval;
310 } else
311 p->iph.tos = 1;
312 } else {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000313 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314 NEXT_ARG();
315 }
316 if (p->name[0])
317 duparg2("name", *argv);
Denis Vlasenko360d9662008-12-02 18:18:50 +0000318 strncpy_IFNAMSIZ(p->name, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000319 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-Fischer636a1f82008-05-19 09:29:47 +0000323 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000324 *p = old_p;
325 }
326 }
327 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000328 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 }
330
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000331 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 Vlasenko540a2a12007-04-07 01:14:45 +0000342 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000343 }
344 }
345
346 if (medium[0]) {
347 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 }
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 Vlasenko540a2a12007-04-07 01:14:45 +0000359 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000361}
362
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000363/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000364static int do_add(int cmd, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365{
366 struct ip_tunnel_parm p;
367
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000368 parse_args(argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369
370 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000371 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000372 }
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 Andersenc7bda1c2004-03-15 08:29:22 +0000381 default:
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100382 bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384}
385
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000386/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000387static int do_del(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000388{
389 struct ip_tunnel_parm p;
390
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000391 parse_args(argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392
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 Andersenc7bda1c2004-03-15 08:29:22 +0000400 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 return do_del_ioctl(p.name, &p);
402 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000403}
404
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000405static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000406{
Denys Vlasenko926d8012015-10-14 13:56:42 +0200407 char s3[INET_ADDRSTRLEN];
408 char s4[INET_ADDRSTRLEN];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000409
410 printf("%s: %s/ip remote %s local %s ",
Denys Vlasenko926d8012015-10-14 13:56:42 +0200411 p->name,
412 p->iph.protocol == IPPROTO_IPIP ? "ip" :
413 p->iph.protocol == IPPROTO_GRE ? "gre" :
414 p->iph.protocol == IPPROTO_IPV6 ? "ipv6" :
415 "unknown",
416 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr) : "any",
417 p->iph.saddr ? format_host(AF_INET, 4, &p->iph.saddr) : "any"
418 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000419 if (p->link) {
420 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000421 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000422 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000423 free(n);
424 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000425 }
426 if (p->iph.ttl)
427 printf(" ttl %d ", p->iph.ttl);
428 else
429 printf(" ttl inherit ");
430 if (p->iph.tos) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000431 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000432 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000433 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000434 if (p->iph.tos & ~1)
435 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200436 rtnl_dsfield_n2a(p->iph.tos & ~1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000437 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000438 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439 printf(" nopmtudisc");
440
Denys Vlasenko926d8012015-10-14 13:56:42 +0200441 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
442 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000443 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000444 printf(" key %s", s3);
Denys Vlasenko926d8012015-10-14 13:56:42 +0200445 else {
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000446 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000447 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000448 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000449 printf(" okey %s ", s4);
450 }
451
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000452 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000453 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000454 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000455 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000456 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000457 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000458 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000459 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000460}
461
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000462static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000463{
464 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000465 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
466 rx_fifo, rx_frame,
467 tx_bytes, tx_packets, tx_errs, tx_drops,
468 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000469 int type;
470 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000471 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000472 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000473
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000474 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000475 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000476 }
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000477 /* skip headers */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000478 fgets(buf, sizeof(buf), fp);
479 fgets(buf, sizeof(buf), fp);
480
481 while (fgets(buf, sizeof(buf), fp) != NULL) {
482 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000483
484 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
485 ptr = strchr(buf, ':');
486 if (ptr == NULL ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100487 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)
488 ) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000489 bb_error_msg("wrong format of /proc/net/dev");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000490 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000491 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000492 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000493 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
494 &rx_fifo, &rx_frame, &rx_multi,
495 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
496 &tx_fifo, &tx_colls, &tx_carrier) != 14)
497 continue;
498 if (p->name[0] && strcmp(p->name, name))
499 continue;
500 type = do_ioctl_get_iftype(name);
501 if (type == -1) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100502 bb_error_msg("can't get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503 continue;
504 }
505 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
506 continue;
507 memset(&p1, 0, sizeof(p1));
508 if (do_get_ioctl(name, &p1))
509 continue;
510 if ((p->link && p1.link != p->link) ||
511 (p->name[0] && strcmp(p1.name, p->name)) ||
512 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
513 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100514 (p->i_key && p1.i_key != p->i_key)
515 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000516 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100517 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000518 print_tunnel(&p1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000519 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000520 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000521}
522
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000523/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000524static int do_show(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000525{
526 int err;
527 struct ip_tunnel_parm p;
528
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000529 parse_args(argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000530
531 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000532 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000533 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
534 break;
535 case IPPROTO_GRE:
536 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
537 break;
538 case IPPROTO_IPV6:
539 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
540 break;
541 default:
542 do_tunnels_list(&p);
543 return 0;
544 }
545 if (err)
546 return -1;
547
548 print_tunnel(&p);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000549 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000550 return 0;
551}
552
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000553/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200554int FAST_FUNC do_iptunnel(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000555{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000556 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000557 "add\0""change\0""delete\0""show\0""list\0""lst\0";
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000558 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000559
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000560 if (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200561 int key = index_in_substrings(keywords, *argv);
562 if (key < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200563 invarg_1_to_2(*argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000564 argv++;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000565 if (key == ARG_add)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000566 return do_add(SIOCADDTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000567 if (key == ARG_change)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000568 return do_add(SIOCCHGTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000569 if (key == ARG_del)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000570 return do_del(argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000571 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000572 return do_show(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000573}