blob: 4002feb78b20437fe519ba937afffda1b33a4afa [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 */
Glenn L McGrath275be872002-12-16 07:37:21 +000013#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000014#include <net/if.h>
15#include <net/if_arp.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000016#include <asm/types.h>
Denis Vlasenko196a9532008-12-03 20:16:11 +000017
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000018#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000019#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000020#endif
Denis Vlasenko196a9532008-12-03 20:16:11 +000021
22// FYI: #define SIOCDEVPRIVATE 0x89F0
23
24/* From linux/if_tunnel.h. #including it proved troublesome
25 * (redefiniton errors due to name collisions in linux/ and net[inet]/) */
26#define SIOCGETTUNNEL (SIOCDEVPRIVATE + 0)
27#define SIOCADDTUNNEL (SIOCDEVPRIVATE + 1)
28#define SIOCDELTUNNEL (SIOCDEVPRIVATE + 2)
29#define SIOCCHGTUNNEL (SIOCDEVPRIVATE + 3)
30//#define SIOCGETPRL (SIOCDEVPRIVATE + 4)
31//#define SIOCADDPRL (SIOCDEVPRIVATE + 5)
32//#define SIOCDELPRL (SIOCDEVPRIVATE + 6)
33//#define SIOCCHGPRL (SIOCDEVPRIVATE + 7)
34#define GRE_CSUM __constant_htons(0x8000)
35//#define GRE_ROUTING __constant_htons(0x4000)
36#define GRE_KEY __constant_htons(0x2000)
37#define GRE_SEQ __constant_htons(0x1000)
38//#define GRE_STRICT __constant_htons(0x0800)
39//#define GRE_REC __constant_htons(0x0700)
40//#define GRE_FLAGS __constant_htons(0x00F8)
41//#define GRE_VERSION __constant_htons(0x0007)
42struct ip_tunnel_parm {
43 char name[IFNAMSIZ];
44 int link;
45 uint16_t i_flags;
46 uint16_t o_flags;
47 uint32_t i_key;
48 uint32_t o_key;
49 struct iphdr iph;
50};
51/* SIT-mode i_flags */
52//#define SIT_ISATAP 0x0001
53//struct ip_tunnel_prl {
54// uint32_t addr;
55// uint16_t flags;
56// uint16_t __reserved;
57// uint32_t datalen;
58// uint32_t __reserved2;
59// /* data follows */
60//};
61///* PRL flags */
62//#define PRL_DEFAULT 0x0001
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000064#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065#include "rt_names.h"
66#include "utils.h"
67
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068
Denis Vlasenko540a2a12007-04-07 01:14:45 +000069/* Dies on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070static int do_ioctl_get_ifindex(char *dev)
71{
72 struct ifreq ifr;
73 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074
Denis Vlasenko360d9662008-12-02 18:18:50 +000075 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000076 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000077 xioctl(fd, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078 close(fd);
79 return ifr.ifr_ifindex;
80}
81
82static int do_ioctl_get_iftype(char *dev)
83{
84 struct ifreq ifr;
85 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000086 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087
Denis Vlasenko360d9662008-12-02 18:18:50 +000088 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000089 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000090 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000092 return err ? -1 : ifr.ifr_addr.sa_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093}
94
Glenn L McGrath8b602442002-11-28 12:19:51 +000095static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096{
Denis Vlasenko540a2a12007-04-07 01:14:45 +000097 struct ifreq ifr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000098 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000099 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100
101 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000102 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000103 err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000105 return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106}
107
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000108static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109{
110 struct ifreq ifr;
111 int fd;
112 int err;
113
Denis Vlasenko360d9662008-12-02 18:18:50 +0000114 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000116 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000117 err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 close(fd);
119 return err;
120}
121
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000122/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000123static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124{
125 struct ifreq ifr;
126 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127
Glenn L McGrath8b602442002-11-28 12:19:51 +0000128 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000129 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000130 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000131 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000132 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000134 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000135#if ENABLE_IOCTL_HEX2STR_ERROR
136 /* #define magic will turn ioctl# into string */
137 if (cmd == SIOCCHGTUNNEL)
138 xioctl(fd, SIOCCHGTUNNEL, &ifr);
139 else
140 xioctl(fd, SIOCADDTUNNEL, &ifr);
141#else
142 xioctl(fd, cmd, &ifr);
143#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000145 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146}
147
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000148/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000149static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150{
151 struct ifreq ifr;
152 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153
Glenn L McGrath8b602442002-11-28 12:19:51 +0000154 if (p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000155 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000156 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000157 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000158 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000160 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000161 xioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000163 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164}
165
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000166/* Dies on error */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000167static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000169 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000170 "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
171 "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
172 "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
173 "remote\0""any\0""local\0""dev\0"
174 "ttl\0""inherit\0""tos\0""dsfield\0"
175 "name\0";
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000176 enum {
177 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
178 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
179 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
180 ARG_remote, ARG_any, ARG_local, ARG_dev,
181 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
182 ARG_name
183 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000184 int count = 0;
185 char medium[IFNAMSIZ];
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000186 int key;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000187
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188 memset(p, 0, sizeof(*p));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000189 medium[0] = '\0';
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190
191 p->iph.version = 4;
192 p->iph.ihl = 5;
193#ifndef IP_DF
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000194#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195#endif
196 p->iph.frag_off = htons(IP_DF);
197
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000198 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000199 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000200 if (key == ARG_mode) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000202 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000203 if (key == ARG_ipip ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100204 key == ARG_ip_ip
205 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000206 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000207 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 }
209 p->iph.protocol = IPPROTO_IPIP;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000210 } else if (key == ARG_gre ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100211 key == ARG_gre_ip
212 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000213 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000214 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 }
216 p->iph.protocol = IPPROTO_GRE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000217 } else if (key == ARG_sit ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100218 key == ARG_ip6_ip
219 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000221 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 }
223 p->iph.protocol = IPPROTO_IPV6;
224 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100225 bb_error_msg_and_die("%s tunnel mode", "can't guess");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000227 } else if (key == ARG_key) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 unsigned uval;
229 NEXT_ARG();
230 p->i_flags |= GRE_KEY;
231 p->o_flags |= GRE_KEY;
232 if (strchr(*argv, '.'))
233 p->i_key = p->o_key = get_addr32(*argv);
234 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000235 uval = get_unsigned(*argv, "key");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236 p->i_key = p->o_key = htonl(uval);
237 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000238 } else if (key == ARG_ikey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239 unsigned uval;
240 NEXT_ARG();
241 p->i_flags |= GRE_KEY;
242 if (strchr(*argv, '.'))
243 p->o_key = get_addr32(*argv);
244 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000245 uval = get_unsigned(*argv, "ikey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 p->i_key = htonl(uval);
247 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000248 } else if (key == ARG_okey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 unsigned uval;
250 NEXT_ARG();
251 p->o_flags |= GRE_KEY;
252 if (strchr(*argv, '.'))
253 p->o_key = get_addr32(*argv);
254 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000255 uval = get_unsigned(*argv, "okey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256 p->o_key = htonl(uval);
257 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000258 } else if (key == ARG_seq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 p->i_flags |= GRE_SEQ;
260 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000261 } else if (key == ARG_iseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 p->i_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000263 } else if (key == ARG_oseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000265 } else if (key == ARG_csum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 p->i_flags |= GRE_CSUM;
267 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000268 } else if (key == ARG_icsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 p->i_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000270 } else if (key == ARG_ocsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000272 } else if (key == ARG_nopmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000273 p->iph.frag_off = 0;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000274 } else if (key == ARG_pmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275 p->iph.frag_off = htons(IP_DF);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000276 } else if (key == ARG_remote) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000278 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000279 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 p->iph.daddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000281 } else if (key == ARG_local) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000282 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000283 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000284 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000285 p->iph.saddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000286 } else if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000287 NEXT_ARG();
Denis Vlasenko360d9662008-12-02 18:18:50 +0000288 strncpy_IFNAMSIZ(medium, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000289 } else if (key == ARG_ttl) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 unsigned uval;
291 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000292 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000293 if (key != ARG_inherit) {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000294 uval = get_unsigned(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 if (uval > 255)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200296 invarg_1_to_2(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000297 p->iph.ttl = uval;
298 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000299 } else if (key == ARG_tos ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100300 key == ARG_dsfield
301 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000302 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000304 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000305 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000306 if (rtnl_dsfield_a2n(&uval, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200307 invarg_1_to_2(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308 p->iph.tos = uval;
309 } else
310 p->iph.tos = 1;
311 } else {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000312 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 NEXT_ARG();
314 }
315 if (p->name[0])
316 duparg2("name", *argv);
Denis Vlasenko360d9662008-12-02 18:18:50 +0000317 strncpy_IFNAMSIZ(p->name, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318 if (cmd == SIOCCHGTUNNEL && count == 0) {
319 struct ip_tunnel_parm old_p;
320 memset(&old_p, 0, sizeof(old_p));
321 if (do_get_ioctl(*argv, &old_p))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000322 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000323 *p = old_p;
324 }
325 }
326 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000327 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000328 }
329
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000330 if (p->iph.protocol == 0) {
331 if (memcmp(p->name, "gre", 3) == 0)
332 p->iph.protocol = IPPROTO_GRE;
333 else if (memcmp(p->name, "ipip", 4) == 0)
334 p->iph.protocol = IPPROTO_IPIP;
335 else if (memcmp(p->name, "sit", 3) == 0)
336 p->iph.protocol = IPPROTO_IPV6;
337 }
338
339 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
340 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000341 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342 }
343 }
344
345 if (medium[0]) {
346 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000347 }
348
349 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
350 p->i_key = p->iph.daddr;
351 p->i_flags |= GRE_KEY;
352 }
353 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
354 p->o_key = p->iph.daddr;
355 p->o_flags |= GRE_KEY;
356 }
357 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000358 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360}
361
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000362/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000363static int do_add(int cmd, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000364{
365 struct ip_tunnel_parm p;
366
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000367 parse_args(argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000368
369 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000370 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 }
372
373 switch (p.iph.protocol) {
374 case IPPROTO_IPIP:
375 return do_add_ioctl(cmd, "tunl0", &p);
376 case IPPROTO_GRE:
377 return do_add_ioctl(cmd, "gre0", &p);
378 case IPPROTO_IPV6:
379 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000380 default:
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100381 bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383}
384
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000385/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000386static int do_del(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387{
388 struct ip_tunnel_parm p;
389
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000390 parse_args(argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000391
392 switch (p.iph.protocol) {
393 case IPPROTO_IPIP:
394 return do_del_ioctl("tunl0", &p);
395 case IPPROTO_GRE:
396 return do_del_ioctl("gre0", &p);
397 case IPPROTO_IPV6:
398 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000399 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000400 return do_del_ioctl(p.name, &p);
401 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402}
403
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000404static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000405{
Denys Vlasenko926d8012015-10-14 13:56:42 +0200406 char s3[INET_ADDRSTRLEN];
407 char s4[INET_ADDRSTRLEN];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408
409 printf("%s: %s/ip remote %s local %s ",
Denys Vlasenko926d8012015-10-14 13:56:42 +0200410 p->name,
411 p->iph.protocol == IPPROTO_IPIP ? "ip" :
412 p->iph.protocol == IPPROTO_GRE ? "gre" :
413 p->iph.protocol == IPPROTO_IPV6 ? "ipv6" :
414 "unknown",
415 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr) : "any",
416 p->iph.saddr ? format_host(AF_INET, 4, &p->iph.saddr) : "any"
417 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000418 if (p->link) {
419 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000420 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000421 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000422 free(n);
423 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000424 }
425 if (p->iph.ttl)
426 printf(" ttl %d ", p->iph.ttl);
427 else
428 printf(" ttl inherit ");
429 if (p->iph.tos) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000430 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000431 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000432 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000433 if (p->iph.tos & ~1)
434 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200435 rtnl_dsfield_n2a(p->iph.tos & ~1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000436 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000437 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000438 printf(" nopmtudisc");
439
Denys Vlasenko926d8012015-10-14 13:56:42 +0200440 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
441 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000442 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 +0000443 printf(" key %s", s3);
Denys Vlasenko926d8012015-10-14 13:56:42 +0200444 else {
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000445 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000446 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000447 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000448 printf(" okey %s ", s4);
449 }
450
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000451 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000452 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000453 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000454 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000455 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000456 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000457 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000458 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000459}
460
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000461static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000462{
463 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000464 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
465 rx_fifo, rx_frame,
466 tx_bytes, tx_packets, tx_errs, tx_drops,
467 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468 int type;
469 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000470 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000471 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000472
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000473 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000474 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000475 }
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000476 /* skip headers */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000477 fgets(buf, sizeof(buf), fp);
478 fgets(buf, sizeof(buf), fp);
479
480 while (fgets(buf, sizeof(buf), fp) != NULL) {
481 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000482
483 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
484 ptr = strchr(buf, ':');
485 if (ptr == NULL ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100486 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)
487 ) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000488 bb_error_msg("wrong format of /proc/net/dev");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000489 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000491 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 +0000492 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
493 &rx_fifo, &rx_frame, &rx_multi,
494 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
495 &tx_fifo, &tx_colls, &tx_carrier) != 14)
496 continue;
497 if (p->name[0] && strcmp(p->name, name))
498 continue;
499 type = do_ioctl_get_iftype(name);
500 if (type == -1) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100501 bb_error_msg("can't get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000502 continue;
503 }
504 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
505 continue;
506 memset(&p1, 0, sizeof(p1));
507 if (do_get_ioctl(name, &p1))
508 continue;
509 if ((p->link && p1.link != p->link) ||
510 (p->name[0] && strcmp(p1.name, p->name)) ||
511 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
512 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100513 (p->i_key && p1.i_key != p->i_key)
514 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000515 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100516 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000517 print_tunnel(&p1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000518 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000519 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000520}
521
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000522/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000523static int do_show(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000524{
525 int err;
526 struct ip_tunnel_parm p;
527
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000528 parse_args(argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000529
530 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000531 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000532 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
533 break;
534 case IPPROTO_GRE:
535 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
536 break;
537 case IPPROTO_IPV6:
538 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
539 break;
540 default:
541 do_tunnels_list(&p);
542 return 0;
543 }
544 if (err)
545 return -1;
546
547 print_tunnel(&p);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000548 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000549 return 0;
550}
551
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000552/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200553int FAST_FUNC do_iptunnel(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000554{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000555 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000556 "add\0""change\0""delete\0""show\0""list\0""lst\0";
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000557 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000558
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000559 if (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200560 int key = index_in_substrings(keywords, *argv);
561 if (key < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200562 invarg_1_to_2(*argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000563 argv++;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000564 if (key == ARG_add)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000565 return do_add(SIOCADDTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000566 if (key == ARG_change)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000567 return do_add(SIOCCHGTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000568 if (key == ARG_del)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000569 return do_del(argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000570 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000571 return do_show(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000572}