blob: 257343826fd8b66a7352c6c34fc25bf660ce89f6 [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/*
3 * iptunnel.c "ip tunnel"
4 *
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +00005 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 * Changes:
10 *
11 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
12 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
13 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
14 */
15
Glenn L McGrath275be872002-12-16 07:37:21 +000016#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000017#include <net/if.h>
18#include <net/if_arp.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000019#include <asm/types.h>
Denis Vlasenko196a9532008-12-03 20:16:11 +000020
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000021#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000022#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000023#endif
Denis Vlasenko196a9532008-12-03 20:16:11 +000024
25// FYI: #define SIOCDEVPRIVATE 0x89F0
26
27/* From linux/if_tunnel.h. #including it proved troublesome
28 * (redefiniton errors due to name collisions in linux/ and net[inet]/) */
29#define SIOCGETTUNNEL (SIOCDEVPRIVATE + 0)
30#define SIOCADDTUNNEL (SIOCDEVPRIVATE + 1)
31#define SIOCDELTUNNEL (SIOCDEVPRIVATE + 2)
32#define SIOCCHGTUNNEL (SIOCDEVPRIVATE + 3)
33//#define SIOCGETPRL (SIOCDEVPRIVATE + 4)
34//#define SIOCADDPRL (SIOCDEVPRIVATE + 5)
35//#define SIOCDELPRL (SIOCDEVPRIVATE + 6)
36//#define SIOCCHGPRL (SIOCDEVPRIVATE + 7)
37#define GRE_CSUM __constant_htons(0x8000)
38//#define GRE_ROUTING __constant_htons(0x4000)
39#define GRE_KEY __constant_htons(0x2000)
40#define GRE_SEQ __constant_htons(0x1000)
41//#define GRE_STRICT __constant_htons(0x0800)
42//#define GRE_REC __constant_htons(0x0700)
43//#define GRE_FLAGS __constant_htons(0x00F8)
44//#define GRE_VERSION __constant_htons(0x0007)
45struct ip_tunnel_parm {
46 char name[IFNAMSIZ];
47 int link;
48 uint16_t i_flags;
49 uint16_t o_flags;
50 uint32_t i_key;
51 uint32_t o_key;
52 struct iphdr iph;
53};
54/* SIT-mode i_flags */
55//#define SIT_ISATAP 0x0001
56//struct ip_tunnel_prl {
57// uint32_t addr;
58// uint16_t flags;
59// uint16_t __reserved;
60// uint32_t datalen;
61// uint32_t __reserved2;
62// /* data follows */
63//};
64///* PRL flags */
65//#define PRL_DEFAULT 0x0001
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000067#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068#include "rt_names.h"
69#include "utils.h"
70
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071
Denis Vlasenko540a2a12007-04-07 01:14:45 +000072/* Dies on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073static int do_ioctl_get_ifindex(char *dev)
74{
75 struct ifreq ifr;
76 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077
Denis Vlasenko360d9662008-12-02 18:18:50 +000078 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000079 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000080 xioctl(fd, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 close(fd);
82 return ifr.ifr_ifindex;
83}
84
85static int do_ioctl_get_iftype(char *dev)
86{
87 struct ifreq ifr;
88 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000089 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090
Denis Vlasenko360d9662008-12-02 18:18:50 +000091 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenko27af5a02006-09-03 12:21:59 +000092 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000093 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000095 return err ? -1 : ifr.ifr_addr.sa_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096}
97
Glenn L McGrath8b602442002-11-28 12:19:51 +000098static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099{
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000100 struct ifreq ifr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000102 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103
104 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000105 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000106 err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000108 return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109}
110
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000111static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112{
113 struct ifreq ifr;
114 int fd;
115 int err;
116
Denis Vlasenko360d9662008-12-02 18:18:50 +0000117 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000119 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000120 err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 close(fd);
122 return err;
123}
124
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000125/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000126static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127{
128 struct ifreq ifr;
129 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130
Glenn L McGrath8b602442002-11-28 12:19:51 +0000131 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000132 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000133 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000134 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000135 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000137 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000138#if ENABLE_IOCTL_HEX2STR_ERROR
139 /* #define magic will turn ioctl# into string */
140 if (cmd == SIOCCHGTUNNEL)
141 xioctl(fd, SIOCCHGTUNNEL, &ifr);
142 else
143 xioctl(fd, SIOCADDTUNNEL, &ifr);
144#else
145 xioctl(fd, cmd, &ifr);
146#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000148 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149}
150
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000151/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000152static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153{
154 struct ifreq ifr;
155 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156
Glenn L McGrath8b602442002-11-28 12:19:51 +0000157 if (p->name[0]) {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000158 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000159 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000160 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
Glenn L McGrath8b602442002-11-28 12:19:51 +0000161 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000163 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000164 xioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000166 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167}
168
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000169/* Dies on error */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000170static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000172 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000173 "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
174 "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
175 "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
176 "remote\0""any\0""local\0""dev\0"
177 "ttl\0""inherit\0""tos\0""dsfield\0"
178 "name\0";
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000179 enum {
180 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
181 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
182 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
183 ARG_remote, ARG_any, ARG_local, ARG_dev,
184 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
185 ARG_name
186 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000187 int count = 0;
188 char medium[IFNAMSIZ];
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000189 int key;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000190
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 memset(p, 0, sizeof(*p));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000192 medium[0] = '\0';
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193
194 p->iph.version = 4;
195 p->iph.ihl = 5;
196#ifndef IP_DF
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000197#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198#endif
199 p->iph.frag_off = htons(IP_DF);
200
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000201 while (*argv) {
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_mode) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000205 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000206 if (key == ARG_ipip ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100207 key == ARG_ip_ip
208 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000210 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211 }
212 p->iph.protocol = IPPROTO_IPIP;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000213 } else if (key == ARG_gre ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100214 key == ARG_gre_ip
215 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000217 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218 }
219 p->iph.protocol = IPPROTO_GRE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000220 } else if (key == ARG_sit ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100221 key == ARG_ip6_ip
222 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000224 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000225 }
226 p->iph.protocol = IPPROTO_IPV6;
227 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100228 bb_error_msg_and_die("%s tunnel mode", "can't guess");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000230 } else if (key == ARG_key) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231 unsigned uval;
232 NEXT_ARG();
233 p->i_flags |= GRE_KEY;
234 p->o_flags |= GRE_KEY;
235 if (strchr(*argv, '.'))
236 p->i_key = p->o_key = get_addr32(*argv);
237 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000238 uval = get_unsigned(*argv, "key");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239 p->i_key = p->o_key = htonl(uval);
240 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000241 } else if (key == ARG_ikey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 unsigned uval;
243 NEXT_ARG();
244 p->i_flags |= GRE_KEY;
245 if (strchr(*argv, '.'))
246 p->o_key = get_addr32(*argv);
247 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000248 uval = get_unsigned(*argv, "ikey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 p->i_key = htonl(uval);
250 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000251 } else if (key == ARG_okey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 unsigned uval;
253 NEXT_ARG();
254 p->o_flags |= GRE_KEY;
255 if (strchr(*argv, '.'))
256 p->o_key = get_addr32(*argv);
257 else {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000258 uval = get_unsigned(*argv, "okey");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 p->o_key = htonl(uval);
260 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000261 } else if (key == ARG_seq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 p->i_flags |= GRE_SEQ;
263 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000264 } else if (key == ARG_iseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000265 p->i_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000266 } else if (key == ARG_oseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000268 } else if (key == ARG_csum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 p->i_flags |= GRE_CSUM;
270 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000271 } else if (key == ARG_icsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 p->i_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000273 } else if (key == ARG_ocsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000275 } else if (key == ARG_nopmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 p->iph.frag_off = 0;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000277 } else if (key == ARG_pmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 p->iph.frag_off = htons(IP_DF);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000279 } else if (key == ARG_remote) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000281 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000282 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 p->iph.daddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000284 } else if (key == ARG_local) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000285 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000286 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000287 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 p->iph.saddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000289 } else if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 NEXT_ARG();
Denis Vlasenko360d9662008-12-02 18:18:50 +0000291 strncpy_IFNAMSIZ(medium, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000292 } else if (key == ARG_ttl) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 unsigned uval;
294 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000295 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000296 if (key != ARG_inherit) {
Denis Vlasenko76140a72009-03-05 09:21:57 +0000297 uval = get_unsigned(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 if (uval > 255)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000299 invarg(*argv, "TTL must be <=255");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 p->iph.ttl = uval;
301 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000302 } else if (key == ARG_tos ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100303 key == ARG_dsfield
304 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000305 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000306 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000307 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000308 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 if (rtnl_dsfield_a2n(&uval, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000310 invarg(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000311 p->iph.tos = uval;
312 } else
313 p->iph.tos = 1;
314 } else {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000315 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316 NEXT_ARG();
317 }
318 if (p->name[0])
319 duparg2("name", *argv);
Denis Vlasenko360d9662008-12-02 18:18:50 +0000320 strncpy_IFNAMSIZ(p->name, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000321 if (cmd == SIOCCHGTUNNEL && count == 0) {
322 struct ip_tunnel_parm old_p;
323 memset(&old_p, 0, sizeof(old_p));
324 if (do_get_ioctl(*argv, &old_p))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000325 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326 *p = old_p;
327 }
328 }
329 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000330 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000331 }
332
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333 if (p->iph.protocol == 0) {
334 if (memcmp(p->name, "gre", 3) == 0)
335 p->iph.protocol = IPPROTO_GRE;
336 else if (memcmp(p->name, "ipip", 4) == 0)
337 p->iph.protocol = IPPROTO_IPIP;
338 else if (memcmp(p->name, "sit", 3) == 0)
339 p->iph.protocol = IPPROTO_IPV6;
340 }
341
342 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
343 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000344 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000345 }
346 }
347
348 if (medium[0]) {
349 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350 }
351
352 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
353 p->i_key = p->iph.daddr;
354 p->i_flags |= GRE_KEY;
355 }
356 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
357 p->o_key = p->iph.daddr;
358 p->o_flags |= GRE_KEY;
359 }
360 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000361 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000363}
364
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000365/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000366static int do_add(int cmd, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000367{
368 struct ip_tunnel_parm p;
369
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000370 parse_args(argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371
372 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000373 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 }
375
376 switch (p.iph.protocol) {
377 case IPPROTO_IPIP:
378 return do_add_ioctl(cmd, "tunl0", &p);
379 case IPPROTO_GRE:
380 return do_add_ioctl(cmd, "gre0", &p);
381 case IPPROTO_IPV6:
382 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000383 default:
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100384 bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000386}
387
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000388/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000389static int do_del(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000390{
391 struct ip_tunnel_parm p;
392
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000393 parse_args(argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000394
395 switch (p.iph.protocol) {
396 case IPPROTO_IPIP:
397 return do_del_ioctl("tunl0", &p);
398 case IPPROTO_GRE:
399 return do_del_ioctl("gre0", &p);
400 case IPPROTO_IPV6:
401 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000402 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000403 return do_del_ioctl(p.name, &p);
404 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000405}
406
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000407static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408{
409 char s1[256];
410 char s2[256];
411 char s3[64];
412 char s4[64];
413
414 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
415 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
416 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
417 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
418
419 printf("%s: %s/ip remote %s local %s ",
420 p->name,
421 p->iph.protocol == IPPROTO_IPIP ? "ip" :
422 (p->iph.protocol == IPPROTO_GRE ? "gre" :
423 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
424 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
425 if (p->link) {
426 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000427 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000428 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000429 free(n);
430 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000431 }
432 if (p->iph.ttl)
433 printf(" ttl %d ", p->iph.ttl);
434 else
435 printf(" ttl inherit ");
436 if (p->iph.tos) {
437 SPRINT_BUF(b1);
438 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000439 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000440 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000441 if (p->iph.tos & ~1)
442 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200443 rtnl_dsfield_n2a(p->iph.tos & ~1, b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000444 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000445 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000446 printf(" nopmtudisc");
447
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000448 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 +0000449 printf(" key %s", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000450 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
451 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000452 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000453 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000454 printf(" okey %s ", s4);
455 }
456
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000457 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000458 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000459 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000460 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000461 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000462 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000463 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000464 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000465}
466
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000467static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468{
469 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000470 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
471 rx_fifo, rx_frame,
472 tx_bytes, tx_packets, tx_errs, tx_drops,
473 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000474 int type;
475 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000476 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000477 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000478
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000480 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000481 }
Bernhard Reutner-Fischer470dc1d2008-10-30 17:42:49 +0000482 /* skip headers */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000483 fgets(buf, sizeof(buf), fp);
484 fgets(buf, sizeof(buf), fp);
485
486 while (fgets(buf, sizeof(buf), fp) != NULL) {
487 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000488
489 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
490 ptr = strchr(buf, ':');
491 if (ptr == NULL ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100492 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)
493 ) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000494 bb_error_msg("wrong format of /proc/net/dev");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000495 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000496 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000497 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 +0000498 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
499 &rx_fifo, &rx_frame, &rx_multi,
500 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
501 &tx_fifo, &tx_colls, &tx_carrier) != 14)
502 continue;
503 if (p->name[0] && strcmp(p->name, name))
504 continue;
505 type = do_ioctl_get_iftype(name);
506 if (type == -1) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100507 bb_error_msg("can't get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000508 continue;
509 }
510 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
511 continue;
512 memset(&p1, 0, sizeof(p1));
513 if (do_get_ioctl(name, &p1))
514 continue;
515 if ((p->link && p1.link != p->link) ||
516 (p->name[0] && strcmp(p1.name, p->name)) ||
517 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
518 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100519 (p->i_key && p1.i_key != p->i_key)
520 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000521 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100522 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000523 print_tunnel(&p1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000524 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000525 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000526}
527
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000528/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000529static int do_show(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000530{
531 int err;
532 struct ip_tunnel_parm p;
533
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000534 parse_args(argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000535
536 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000537 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000538 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
539 break;
540 case IPPROTO_GRE:
541 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
542 break;
543 case IPPROTO_IPV6:
544 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
545 break;
546 default:
547 do_tunnels_list(&p);
548 return 0;
549 }
550 if (err)
551 return -1;
552
553 print_tunnel(&p);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000554 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000555 return 0;
556}
557
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000558/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000559int do_iptunnel(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000560{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000561 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000562 "add\0""change\0""delete\0""show\0""list\0""lst\0";
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000563 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000564
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000565 if (*argv) {
Bernhard Reutner-Fischer96913f92010-05-25 17:19:35 +0200566 smalluint key = index_in_substrings(keywords, *argv);
567 if (key > 5)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000568 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000569 argv++;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000570 if (key == ARG_add)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000571 return do_add(SIOCADDTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000572 if (key == ARG_change)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000573 return do_add(SIOCCHGTUNNEL, argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000574 if (key == ARG_del)
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000575 return do_del(argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000576 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000577 return do_show(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000578}