blob: a45e64d604cb5e8f4159b347d83330c2ab4082f9 [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 *
9 *
10 * Changes:
11 *
12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
14 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
15 */
16
Glenn L McGrath275be872002-12-16 07:37:21 +000017#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000018#include <net/if.h>
19#include <net/if_arp.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000020#include <asm/types.h>
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
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024#include <linux/if_tunnel.h>
25
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000026#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027#include "rt_names.h"
28#include "utils.h"
29
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030
Denis Vlasenko540a2a12007-04-07 01:14:45 +000031/* Dies on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032static int do_ioctl_get_ifindex(char *dev)
33{
34 struct ifreq ifr;
35 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000036
Denis Vlasenko229b3d22006-11-27 23:44:57 +000037 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000038 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000039 xioctl(fd, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000040 close(fd);
41 return ifr.ifr_ifindex;
42}
43
44static int do_ioctl_get_iftype(char *dev)
45{
46 struct ifreq ifr;
47 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000048 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049
Denis Vlasenko229b3d22006-11-27 23:44:57 +000050 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000051 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000052 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000054 return err ? -1 : ifr.ifr_addr.sa_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055}
56
Glenn L McGrath8b602442002-11-28 12:19:51 +000057static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058{
Denis Vlasenko540a2a12007-04-07 01:14:45 +000059 struct ifreq ifr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060 int fd;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000061 int err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062
63 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000064 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000065 err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 close(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000067 return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068}
69
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000070static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071{
72 struct ifreq ifr;
73 int fd;
74 int err;
75
Denis Vlasenko229b3d22006-11-27 23:44:57 +000076 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000078 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000079 err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080 close(fd);
81 return err;
82}
83
Denis Vlasenko540a2a12007-04-07 01:14:45 +000084/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000085static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086{
87 struct ifreq ifr;
88 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000089
Glenn L McGrath8b602442002-11-28 12:19:51 +000090 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +000091 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +000092 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +000093 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +000094 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000096 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000097#if ENABLE_IOCTL_HEX2STR_ERROR
98 /* #define magic will turn ioctl# into string */
99 if (cmd == SIOCCHGTUNNEL)
100 xioctl(fd, SIOCCHGTUNNEL, &ifr);
101 else
102 xioctl(fd, SIOCADDTUNNEL, &ifr);
103#else
104 xioctl(fd, cmd, &ifr);
105#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000107 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108}
109
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000110/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000111static int do_del_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;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115
Glenn L McGrath8b602442002-11-28 12:19:51 +0000116 if (p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000117 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000118 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000119 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000120 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000122 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000123 xioctl(fd, SIOCDELTUNNEL, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000125 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126}
127
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000128/* Dies on error */
129static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000131 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000132 "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
133 "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
134 "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
135 "remote\0""any\0""local\0""dev\0"
136 "ttl\0""inherit\0""tos\0""dsfield\0"
137 "name\0";
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000138 enum {
139 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
140 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
141 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
142 ARG_remote, ARG_any, ARG_local, ARG_dev,
143 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
144 ARG_name
145 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000146 int count = 0;
147 char medium[IFNAMSIZ];
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000148 int key;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000149
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 memset(p, 0, sizeof(*p));
151 memset(&medium, 0, sizeof(medium));
152
153 p->iph.version = 4;
154 p->iph.ihl = 5;
155#ifndef IP_DF
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000156#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157#endif
158 p->iph.frag_off = htons(IP_DF);
159
160 while (argc > 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000161 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000162 if (key == ARG_mode) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000164 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000165 if (key == ARG_ipip ||
166 key == ARG_ip_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000168 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 }
170 p->iph.protocol = IPPROTO_IPIP;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000171 } else if (key == ARG_gre ||
172 key == ARG_gre_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000174 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 }
176 p->iph.protocol = IPPROTO_GRE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000177 } else if (key == ARG_sit ||
178 key == ARG_ip6_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000180 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000181 }
182 p->iph.protocol = IPPROTO_IPV6;
183 } else {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000184 bb_error_msg_and_die("cannot guess tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000186 } else if (key == ARG_key) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 unsigned uval;
188 NEXT_ARG();
189 p->i_flags |= GRE_KEY;
190 p->o_flags |= GRE_KEY;
191 if (strchr(*argv, '.'))
192 p->i_key = p->o_key = get_addr32(*argv);
193 else {
194 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000195 bb_error_msg_and_die("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196 }
197 p->i_key = p->o_key = htonl(uval);
198 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000199 } else if (key == ARG_ikey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200 unsigned uval;
201 NEXT_ARG();
202 p->i_flags |= GRE_KEY;
203 if (strchr(*argv, '.'))
204 p->o_key = get_addr32(*argv);
205 else {
206 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000207 bb_error_msg_and_die("invalid value of \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 }
209 p->i_key = htonl(uval);
210 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000211 } else if (key == ARG_okey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 unsigned uval;
213 NEXT_ARG();
214 p->o_flags |= GRE_KEY;
215 if (strchr(*argv, '.'))
216 p->o_key = get_addr32(*argv);
217 else {
218 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000219 bb_error_msg_and_die("invalid value of \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220 }
221 p->o_key = htonl(uval);
222 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000223 } else if (key == ARG_seq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 p->i_flags |= GRE_SEQ;
225 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000226 } else if (key == ARG_iseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 p->i_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000228 } else if (key == ARG_oseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000230 } else if (key == ARG_csum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231 p->i_flags |= GRE_CSUM;
232 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000233 } else if (key == ARG_icsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 p->i_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000235 } else if (key == ARG_ocsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000237 } else if (key == ARG_nopmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238 p->iph.frag_off = 0;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000239 } else if (key == ARG_pmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240 p->iph.frag_off = htons(IP_DF);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000241 } else if (key == ARG_remote) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000243 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000244 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 p->iph.daddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000246 } else if (key == ARG_local) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000248 key = index_in_strings(keywords, *argv);
Denis Vlasenkodb60bcf2007-10-10 14:43:03 +0000249 if (key != ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250 p->iph.saddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000251 } else if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 NEXT_ARG();
253 strncpy(medium, *argv, IFNAMSIZ-1);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000254 } else if (key == ARG_ttl) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 unsigned uval;
256 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000257 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000258 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 if (get_unsigned(&uval, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000260 invarg(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261 if (uval > 255)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000262 invarg(*argv, "TTL must be <=255");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 p->iph.ttl = uval;
264 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000265 } else if (key == ARG_tos ||
266 key == ARG_dsfield) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000267 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000269 key = index_in_strings(keywords, *argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000270 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 if (rtnl_dsfield_a2n(&uval, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000272 invarg(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000273 p->iph.tos = uval;
274 } else
275 p->iph.tos = 1;
276 } else {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000277 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 NEXT_ARG();
279 }
280 if (p->name[0])
281 duparg2("name", *argv);
282 strncpy(p->name, *argv, IFNAMSIZ);
283 if (cmd == SIOCCHGTUNNEL && count == 0) {
284 struct ip_tunnel_parm old_p;
285 memset(&old_p, 0, sizeof(old_p));
286 if (do_get_ioctl(*argv, &old_p))
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000287 exit(1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 *p = old_p;
289 }
290 }
291 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000292 argc--;
293 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294 }
295
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 if (p->iph.protocol == 0) {
297 if (memcmp(p->name, "gre", 3) == 0)
298 p->iph.protocol = IPPROTO_GRE;
299 else if (memcmp(p->name, "ipip", 4) == 0)
300 p->iph.protocol = IPPROTO_IPIP;
301 else if (memcmp(p->name, "sit", 3) == 0)
302 p->iph.protocol = IPPROTO_IPV6;
303 }
304
305 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
306 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000307 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308 }
309 }
310
311 if (medium[0]) {
312 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 }
314
315 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
316 p->i_key = p->iph.daddr;
317 p->i_flags |= GRE_KEY;
318 }
319 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
320 p->o_key = p->iph.daddr;
321 p->o_flags |= GRE_KEY;
322 }
323 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000324 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000325 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326}
327
328
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000329/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000330static int do_add(int cmd, int argc, char **argv)
331{
332 struct ip_tunnel_parm p;
333
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000334 parse_args(argc, argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000335
336 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000337 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338 }
339
340 switch (p.iph.protocol) {
341 case IPPROTO_IPIP:
342 return do_add_ioctl(cmd, "tunl0", &p);
343 case IPPROTO_GRE:
344 return do_add_ioctl(cmd, "gre0", &p);
345 case IPPROTO_IPV6:
346 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000347 default:
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000348 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000349 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350}
351
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000352/* Return value becomes exitcode. It's okay to not return at all */
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000353static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000354{
355 struct ip_tunnel_parm p;
356
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000357 parse_args(argc, argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000358
359 switch (p.iph.protocol) {
360 case IPPROTO_IPIP:
361 return do_del_ioctl("tunl0", &p);
362 case IPPROTO_GRE:
363 return do_del_ioctl("gre0", &p);
364 case IPPROTO_IPV6:
365 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000366 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000367 return do_del_ioctl(p.name, &p);
368 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369}
370
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000371static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000372{
373 char s1[256];
374 char s2[256];
375 char s3[64];
376 char s4[64];
377
378 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
379 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
380 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
381 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
382
383 printf("%s: %s/ip remote %s local %s ",
384 p->name,
385 p->iph.protocol == IPPROTO_IPIP ? "ip" :
386 (p->iph.protocol == IPPROTO_GRE ? "gre" :
387 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
388 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
389 if (p->link) {
390 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000391 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000393 free(n);
394 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000395 }
396 if (p->iph.ttl)
397 printf(" ttl %d ", p->iph.ttl);
398 else
399 printf(" ttl inherit ");
400 if (p->iph.tos) {
401 SPRINT_BUF(b1);
402 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000403 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000405 if (p->iph.tos & ~1)
406 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
407 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000409 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000410 printf(" nopmtudisc");
411
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000412 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 +0000413 printf(" key %s", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000414 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
415 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000416 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000417 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000418 printf(" okey %s ", s4);
419 }
420
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000421 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000422 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000423 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000424 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000425 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000426 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000427 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000428 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000429}
430
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000431static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000432{
433 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000434 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
435 rx_fifo, rx_frame,
436 tx_bytes, tx_packets, tx_errs, tx_drops,
437 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000438 int type;
439 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000440 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000441 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000442
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000444 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000445 }
446
447 fgets(buf, sizeof(buf), fp);
448 fgets(buf, sizeof(buf), fp);
449
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000452
453 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
454 ptr = strchr(buf, ':');
455 if (ptr == NULL ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000456 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000457 bb_error_msg("wrong format of /proc/net/dev");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000458 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000459 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000460 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 +0000461 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
462 &rx_fifo, &rx_frame, &rx_multi,
463 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
464 &tx_fifo, &tx_colls, &tx_carrier) != 14)
465 continue;
466 if (p->name[0] && strcmp(p->name, name))
467 continue;
468 type = do_ioctl_get_iftype(name);
469 if (type == -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000470 bb_error_msg("cannot get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000471 continue;
472 }
473 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
474 continue;
475 memset(&p1, 0, sizeof(p1));
476 if (do_get_ioctl(name, &p1))
477 continue;
478 if ((p->link && p1.link != p->link) ||
479 (p->name[0] && strcmp(p1.name, p->name)) ||
480 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
481 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
482 (p->i_key && p1.i_key != p->i_key))
483 continue;
484 print_tunnel(&p1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000485 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000486 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000487}
488
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000489/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490static int do_show(int argc, char **argv)
491{
492 int err;
493 struct ip_tunnel_parm p;
494
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000495 parse_args(argc, argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000496
497 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000498 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000499 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
500 break;
501 case IPPROTO_GRE:
502 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
503 break;
504 case IPPROTO_IPV6:
505 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
506 break;
507 default:
508 do_tunnels_list(&p);
509 return 0;
510 }
511 if (err)
512 return -1;
513
514 print_tunnel(&p);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000515 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000516 return 0;
517}
518
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000519/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000520int do_iptunnel(int argc, char **argv)
521{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000522 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000523 "add\0""change\0""delete\0""show\0""list\0""lst\0";
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000524 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
525 int key;
526
527 if (argc) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000528 key = index_in_substrings(keywords, *argv);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000529 if (key < 0)
530 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000531 --argc;
532 ++argv;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000533 if (key == ARG_add)
534 return do_add(SIOCADDTUNNEL, argc, argv);
535 if (key == ARG_change)
536 return do_add(SIOCCHGTUNNEL, argc, argv);
537 if (key == ARG_del)
538 return do_del(argc, argv);
539 }
540 return do_show(argc, argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000541}