blob: b12bceb4517971323cd3a2762bc57b7ddeb91d33 [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
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000017//#include <sys/socket.h>
18//#include <sys/ioctl.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000019#include <netinet/ip.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000020#include <net/if.h>
21#include <net/if_arp.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000022#include <asm/types.h>
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000023#ifndef __constant_htons
Eric Andersenab4e19a2003-01-14 08:54:08 +000024#define __constant_htons htons
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +000025#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026#include <linux/if_tunnel.h>
27
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000028#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029#include "rt_names.h"
30#include "utils.h"
31
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032
Denis Vlasenko540a2a12007-04-07 01:14:45 +000033/* Dies on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034static int do_ioctl_get_ifindex(char *dev)
35{
36 struct ifreq ifr;
37 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038
Denis Vlasenko229b3d22006-11-27 23:44:57 +000039 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000040 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000041 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000042 bb_perror_msg_and_die("SIOCGIFINDEX");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043 }
44 close(fd);
45 return ifr.ifr_ifindex;
46}
47
48static int do_ioctl_get_iftype(char *dev)
49{
50 struct ifreq ifr;
51 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000052
Denis Vlasenko229b3d22006-11-27 23:44:57 +000053 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenko27af5a02006-09-03 12:21:59 +000054 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000055 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000056 bb_perror_msg("SIOCGIFHWADDR");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057 return -1;
58 }
59 close(fd);
60 return ifr.ifr_addr.sa_family;
61}
62
Glenn L McGrath8b602442002-11-28 12:19:51 +000063static char *do_ioctl_get_ifname(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000064{
Denis Vlasenko540a2a12007-04-07 01:14:45 +000065 struct ifreq ifr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067
68 ifr.ifr_ifindex = idx;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000069 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath8b602442002-11-28 12:19:51 +000070 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000071 bb_perror_msg("SIOCGIFNAME");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072 return NULL;
73 }
74 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000075 return xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076}
77
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000078static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079{
80 struct ifreq ifr;
81 int fd;
82 int err;
83
Denis Vlasenko229b3d22006-11-27 23:44:57 +000084 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +000086 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
Glenn L McGrath8b602442002-11-28 12:19:51 +000088 if (err) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000089 bb_perror_msg("SIOCGETTUNNEL");
Glenn L McGrath8b602442002-11-28 12:19:51 +000090 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 close(fd);
92 return err;
93}
94
Denis Vlasenko540a2a12007-04-07 01:14:45 +000095/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000096static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097{
98 struct ifreq ifr;
99 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100
Glenn L McGrath8b602442002-11-28 12:19:51 +0000101 if (cmd == SIOCCHGTUNNEL && p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000102 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000103 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000104 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000105 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000107 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000108 if (ioctl(fd, cmd, &ifr)) {
109 bb_perror_msg_and_die("ioctl");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000110 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000112 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113}
114
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000115/* Dies on error, otherwise returns 0 */
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000116static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117{
118 struct ifreq ifr;
119 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120
Glenn L McGrath8b602442002-11-28 12:19:51 +0000121 if (p->name[0]) {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000122 strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000123 } else {
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000124 strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
Glenn L McGrath8b602442002-11-28 12:19:51 +0000125 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126 ifr.ifr_ifru.ifru_data = (void*)p;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000127 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000128 if (ioctl(fd, SIOCDELTUNNEL, &ifr)) {
129 bb_perror_msg_and_die("SIOCDELTUNNEL");
Glenn L McGrath8b602442002-11-28 12:19:51 +0000130 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131 close(fd);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000132 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133}
134
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000135/* Dies on error */
136static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137{
138 int count = 0;
139 char medium[IFNAMSIZ];
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000140 static const char * const keywords[] = {
141 "mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip",
142 "key", "ikey", "okey", "seq", "iseq", "oseq",
143 "csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc",
144 "remote", "any", "local", "dev",
145 "ttl", "inherit", "tos", "dsfield",
146 "name", NULL
147 };
148 enum {
149 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
150 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
151 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
152 ARG_remote, ARG_any, ARG_local, ARG_dev,
153 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
154 ARG_name
155 };
156 int key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 memset(p, 0, sizeof(*p));
158 memset(&medium, 0, sizeof(medium));
159
160 p->iph.version = 4;
161 p->iph.ihl = 5;
162#ifndef IP_DF
163#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
164#endif
165 p->iph.frag_off = htons(IP_DF);
166
167 while (argc > 0) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000168 key = index_in_str_array(keywords, *argv);
169 if (key == ARG_mode) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 NEXT_ARG();
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000171 key = index_in_str_array(keywords, *argv);
172 if (key == ARG_ipip ||
173 key == ARG_ip_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000175 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000176 }
177 p->iph.protocol = IPPROTO_IPIP;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000178 } else if (key == ARG_gre ||
179 key == ARG_gre_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000181 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 }
183 p->iph.protocol = IPPROTO_GRE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000184 } else if (key == ARG_sit ||
185 key == ARG_ip6_ip) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000187 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188 }
189 p->iph.protocol = IPPROTO_IPV6;
190 } else {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000191 bb_error_msg_and_die("cannot guess tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000193 } else if (key == ARG_key) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 unsigned uval;
195 NEXT_ARG();
196 p->i_flags |= GRE_KEY;
197 p->o_flags |= GRE_KEY;
198 if (strchr(*argv, '.'))
199 p->i_key = p->o_key = get_addr32(*argv);
200 else {
201 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000202 bb_error_msg_and_die("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203 }
204 p->i_key = p->o_key = htonl(uval);
205 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000206 } else if (key == ARG_ikey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207 unsigned uval;
208 NEXT_ARG();
209 p->i_flags |= GRE_KEY;
210 if (strchr(*argv, '.'))
211 p->o_key = get_addr32(*argv);
212 else {
213 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000214 bb_error_msg_and_die("invalid value of \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 }
216 p->i_key = htonl(uval);
217 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000218 } else if (key == ARG_okey) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219 unsigned uval;
220 NEXT_ARG();
221 p->o_flags |= GRE_KEY;
222 if (strchr(*argv, '.'))
223 p->o_key = get_addr32(*argv);
224 else {
225 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000226 bb_error_msg_and_die("invalid value of \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 }
228 p->o_key = htonl(uval);
229 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000230 } else if (key == ARG_seq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231 p->i_flags |= GRE_SEQ;
232 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000233 } else if (key == ARG_iseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 p->i_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000235 } else if (key == ARG_oseq) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236 p->o_flags |= GRE_SEQ;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000237 } else if (key == ARG_csum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238 p->i_flags |= GRE_CSUM;
239 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000240 } else if (key == ARG_icsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 p->i_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000242 } else if (key == ARG_ocsum) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 p->o_flags |= GRE_CSUM;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000244 } else if (key == ARG_nopmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 p->iph.frag_off = 0;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000246 } else if (key == ARG_pmtudisc) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 p->iph.frag_off = htons(IP_DF);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000248 } else if (key == ARG_remote) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 NEXT_ARG();
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000250 key = index_in_str_array(keywords, *argv);
251 if (key == ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 p->iph.daddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000253 } else if (key == ARG_local) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 NEXT_ARG();
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000255 key = index_in_str_array(keywords, *argv);
256 if (key == ARG_any)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 p->iph.saddr = get_addr32(*argv);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000258 } else if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 NEXT_ARG();
260 strncpy(medium, *argv, IFNAMSIZ-1);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000261 } else if (key == ARG_ttl) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 unsigned uval;
263 NEXT_ARG();
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000264 key = index_in_str_array(keywords, *argv);
265 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 if (get_unsigned(&uval, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000267 invarg(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 if (uval > 255)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000269 invarg(*argv, "TTL must be <=255");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 p->iph.ttl = uval;
271 }
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000272 } else if (key == ARG_tos ||
273 key == ARG_dsfield) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000274 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275 NEXT_ARG();
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000276 key = index_in_str_array(keywords, *argv);
277 if (key != ARG_inherit) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 if (rtnl_dsfield_a2n(&uval, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000279 invarg(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 p->iph.tos = uval;
281 } else
282 p->iph.tos = 1;
283 } else {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000284 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000285 NEXT_ARG();
286 }
287 if (p->name[0])
288 duparg2("name", *argv);
289 strncpy(p->name, *argv, IFNAMSIZ);
290 if (cmd == SIOCCHGTUNNEL && count == 0) {
291 struct ip_tunnel_parm old_p;
292 memset(&old_p, 0, sizeof(old_p));
293 if (do_get_ioctl(*argv, &old_p))
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000294 exit(1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 *p = old_p;
296 }
297 }
298 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000299 argc--;
300 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000301 }
302
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 if (p->iph.protocol == 0) {
304 if (memcmp(p->name, "gre", 3) == 0)
305 p->iph.protocol = IPPROTO_GRE;
306 else if (memcmp(p->name, "ipip", 4) == 0)
307 p->iph.protocol = IPPROTO_IPIP;
308 else if (memcmp(p->name, "sit", 3) == 0)
309 p->iph.protocol = IPPROTO_IPV6;
310 }
311
312 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
313 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000314 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000315 }
316 }
317
318 if (medium[0]) {
319 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000320 }
321
322 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
323 p->i_key = p->iph.daddr;
324 p->i_flags |= GRE_KEY;
325 }
326 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
327 p->o_key = p->iph.daddr;
328 p->o_flags |= GRE_KEY;
329 }
330 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000331 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333}
334
335
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000336/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000337static int do_add(int cmd, int argc, char **argv)
338{
339 struct ip_tunnel_parm p;
340
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000341 parse_args(argc, argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342
343 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000344 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000345 }
346
347 switch (p.iph.protocol) {
348 case IPPROTO_IPIP:
349 return do_add_ioctl(cmd, "tunl0", &p);
350 case IPPROTO_GRE:
351 return do_add_ioctl(cmd, "gre0", &p);
352 case IPPROTO_IPV6:
353 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000354 default:
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000355 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000356 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000357}
358
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000359/* Return value becomes exitcode. It's okay to not return at all */
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000360static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000361{
362 struct ip_tunnel_parm p;
363
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000364 parse_args(argc, argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365
366 switch (p.iph.protocol) {
367 case IPPROTO_IPIP:
368 return do_del_ioctl("tunl0", &p);
369 case IPPROTO_GRE:
370 return do_del_ioctl("gre0", &p);
371 case IPPROTO_IPV6:
372 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000373 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 return do_del_ioctl(p.name, &p);
375 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376}
377
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000378static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379{
380 char s1[256];
381 char s2[256];
382 char s3[64];
383 char s4[64];
384
385 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
386 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
387 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
388 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
389
390 printf("%s: %s/ip remote %s local %s ",
391 p->name,
392 p->iph.protocol == IPPROTO_IPIP ? "ip" :
393 (p->iph.protocol == IPPROTO_GRE ? "gre" :
394 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
395 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
396 if (p->link) {
397 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000398 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000400 free(n);
401 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402 }
403 if (p->iph.ttl)
404 printf(" ttl %d ", p->iph.ttl);
405 else
406 printf(" ttl inherit ");
407 if (p->iph.tos) {
408 SPRINT_BUF(b1);
409 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000410 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000411 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000412 if (p->iph.tos & ~1)
413 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
414 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000415 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000416 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000417 printf(" nopmtudisc");
418
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000419 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 +0000420 printf(" key %s", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000421 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
422 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000423 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000424 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000425 printf(" okey %s ", s4);
426 }
427
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000428 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000429 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000430 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000431 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000432 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000433 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000434 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000435 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000436}
437
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000438static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439{
440 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000441 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
442 rx_fifo, rx_frame,
443 tx_bytes, tx_packets, tx_errs, tx_drops,
444 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000445 int type;
446 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000447 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000448 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000449
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000450 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000451 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000452 }
453
454 fgets(buf, sizeof(buf), fp);
455 fgets(buf, sizeof(buf), fp);
456
457 while (fgets(buf, sizeof(buf), fp) != NULL) {
458 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000459
460 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
461 ptr = strchr(buf, ':');
462 if (ptr == NULL ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000463 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000464 bb_error_msg("wrong format of /proc/net/dev");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000465 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000466 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000467 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 +0000468 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
469 &rx_fifo, &rx_frame, &rx_multi,
470 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
471 &tx_fifo, &tx_colls, &tx_carrier) != 14)
472 continue;
473 if (p->name[0] && strcmp(p->name, name))
474 continue;
475 type = do_ioctl_get_iftype(name);
476 if (type == -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000477 bb_error_msg("cannot get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000478 continue;
479 }
480 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
481 continue;
482 memset(&p1, 0, sizeof(p1));
483 if (do_get_ioctl(name, &p1))
484 continue;
485 if ((p->link && p1.link != p->link) ||
486 (p->name[0] && strcmp(p1.name, p->name)) ||
487 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
488 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
489 (p->i_key && p1.i_key != p->i_key))
490 continue;
491 print_tunnel(&p1);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000492 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000493 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000494}
495
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000496/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000497static int do_show(int argc, char **argv)
498{
499 int err;
500 struct ip_tunnel_parm p;
501
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000502 parse_args(argc, argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503
504 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000505 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000506 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
507 break;
508 case IPPROTO_GRE:
509 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
510 break;
511 case IPPROTO_IPV6:
512 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
513 break;
514 default:
515 do_tunnels_list(&p);
516 return 0;
517 }
518 if (err)
519 return -1;
520
521 print_tunnel(&p);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000522 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000523 return 0;
524}
525
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000526/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000527int do_iptunnel(int argc, char **argv)
528{
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000529 static const char * const keywords[] = {
530 "add", "change", "delete", "show", "list", "lst", NULL
531 };
532 enum {ARG_add = 1, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst};
533 smalluint key = 4; /* show */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000534 if (argc > 0) {
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000535 key = index_in_substr_array(keywords, *argv) +1;
536 --argc;
537 ++argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000538 } else
539 return do_show(0, NULL);
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000540 if (key < ARG_add)
541 bail:
542 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000543
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +0000544 if (key == ARG_add)
545 return do_add(SIOCADDTUNNEL, argc, argv);
546 if (key == ARG_change)
547 return do_add(SIOCCHGTUNNEL, argc, argv);
548 if (key == ARG_del)
549 return do_del(argc, argv);
550 if (key == ARG_show || key == ARG_list || key == ARG_lst)
551 return do_show(argc, argv);
552 /* be gentle to gcc; avoid warning about non returning */
553 goto bail; /* never reached */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000554}