blob: 3327b27a323d15d7458fccbc030cdc39ee2c344c [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];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 memset(p, 0, sizeof(*p));
141 memset(&medium, 0, sizeof(medium));
142
143 p->iph.version = 4;
144 p->iph.ihl = 5;
145#ifndef IP_DF
146#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
147#endif
148 p->iph.frag_off = htons(IP_DF);
149
150 while (argc > 0) {
151 if (strcmp(*argv, "mode") == 0) {
152 NEXT_ARG();
153 if (strcmp(*argv, "ipip") == 0 ||
154 strcmp(*argv, "ip/ip") == 0) {
155 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000156 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 }
158 p->iph.protocol = IPPROTO_IPIP;
159 } else if (strcmp(*argv, "gre") == 0 ||
160 strcmp(*argv, "gre/ip") == 0) {
161 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000162 bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 }
164 p->iph.protocol = IPPROTO_GRE;
165 } else if (strcmp(*argv, "sit") == 0 ||
166 strcmp(*argv, "ipv6/ip") == 0) {
167 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
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_IPV6;
171 } else {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000172 bb_error_msg_and_die("cannot guess tunnel mode");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 }
174 } else if (strcmp(*argv, "key") == 0) {
175 unsigned uval;
176 NEXT_ARG();
177 p->i_flags |= GRE_KEY;
178 p->o_flags |= GRE_KEY;
179 if (strchr(*argv, '.'))
180 p->i_key = p->o_key = get_addr32(*argv);
181 else {
182 if (get_unsigned(&uval, *argv, 0)<0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000183 bb_error_msg_and_die("invalid value of \"key\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 }
185 p->i_key = p->o_key = htonl(uval);
186 }
187 } else if (strcmp(*argv, "ikey") == 0) {
188 unsigned uval;
189 NEXT_ARG();
190 p->i_flags |= GRE_KEY;
191 if (strchr(*argv, '.'))
192 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 \"ikey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196 }
197 p->i_key = htonl(uval);
198 }
199 } else if (strcmp(*argv, "okey") == 0) {
200 unsigned uval;
201 NEXT_ARG();
202 p->o_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 \"okey\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 }
209 p->o_key = htonl(uval);
210 }
211 } else if (strcmp(*argv, "seq") == 0) {
212 p->i_flags |= GRE_SEQ;
213 p->o_flags |= GRE_SEQ;
214 } else if (strcmp(*argv, "iseq") == 0) {
215 p->i_flags |= GRE_SEQ;
216 } else if (strcmp(*argv, "oseq") == 0) {
217 p->o_flags |= GRE_SEQ;
218 } else if (strcmp(*argv, "csum") == 0) {
219 p->i_flags |= GRE_CSUM;
220 p->o_flags |= GRE_CSUM;
221 } else if (strcmp(*argv, "icsum") == 0) {
222 p->i_flags |= GRE_CSUM;
223 } else if (strcmp(*argv, "ocsum") == 0) {
224 p->o_flags |= GRE_CSUM;
225 } else if (strcmp(*argv, "nopmtudisc") == 0) {
226 p->iph.frag_off = 0;
227 } else if (strcmp(*argv, "pmtudisc") == 0) {
228 p->iph.frag_off = htons(IP_DF);
229 } else if (strcmp(*argv, "remote") == 0) {
230 NEXT_ARG();
231 if (strcmp(*argv, "any"))
232 p->iph.daddr = get_addr32(*argv);
233 } else if (strcmp(*argv, "local") == 0) {
234 NEXT_ARG();
235 if (strcmp(*argv, "any"))
236 p->iph.saddr = get_addr32(*argv);
237 } else if (strcmp(*argv, "dev") == 0) {
238 NEXT_ARG();
239 strncpy(medium, *argv, IFNAMSIZ-1);
240 } else if (strcmp(*argv, "ttl") == 0) {
241 unsigned uval;
242 NEXT_ARG();
243 if (strcmp(*argv, "inherit") != 0) {
244 if (get_unsigned(&uval, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000245 invarg(*argv, "TTL");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 if (uval > 255)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000247 invarg(*argv, "TTL must be <=255");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 p->iph.ttl = uval;
249 }
250 } else if (strcmp(*argv, "tos") == 0 ||
251 matches(*argv, "dsfield") == 0) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000252 uint32_t uval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 NEXT_ARG();
254 if (strcmp(*argv, "inherit") != 0) {
255 if (rtnl_dsfield_a2n(&uval, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000256 invarg(*argv, "TOS");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 p->iph.tos = uval;
258 } else
259 p->iph.tos = 1;
260 } else {
261 if (strcmp(*argv, "name") == 0) {
262 NEXT_ARG();
263 }
264 if (p->name[0])
265 duparg2("name", *argv);
266 strncpy(p->name, *argv, IFNAMSIZ);
267 if (cmd == SIOCCHGTUNNEL && count == 0) {
268 struct ip_tunnel_parm old_p;
269 memset(&old_p, 0, sizeof(old_p));
270 if (do_get_ioctl(*argv, &old_p))
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000271 exit(1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 *p = old_p;
273 }
274 }
275 count++;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000276 argc--;
277 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 }
279
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 if (p->iph.protocol == 0) {
281 if (memcmp(p->name, "gre", 3) == 0)
282 p->iph.protocol = IPPROTO_GRE;
283 else if (memcmp(p->name, "ipip", 4) == 0)
284 p->iph.protocol = IPPROTO_IPIP;
285 else if (memcmp(p->name, "sit", 3) == 0)
286 p->iph.protocol = IPPROTO_IPV6;
287 }
288
289 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
290 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000291 bb_error_msg_and_die("keys are not allowed with ipip and sit");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 }
293 }
294
295 if (medium[0]) {
296 p->link = do_ioctl_get_ifindex(medium);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000297 }
298
299 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
300 p->i_key = p->iph.daddr;
301 p->i_flags |= GRE_KEY;
302 }
303 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
304 p->o_key = p->iph.daddr;
305 p->o_flags |= GRE_KEY;
306 }
307 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000308 bb_error_msg_and_die("broadcast tunnel requires a source address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310}
311
312
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000313/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314static int do_add(int cmd, int argc, char **argv)
315{
316 struct ip_tunnel_parm p;
317
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000318 parse_args(argc, argv, cmd, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000319
320 if (p.iph.ttl && p.iph.frag_off == 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000321 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000322 }
323
324 switch (p.iph.protocol) {
325 case IPPROTO_IPIP:
326 return do_add_ioctl(cmd, "tunl0", &p);
327 case IPPROTO_GRE:
328 return do_add_ioctl(cmd, "gre0", &p);
329 case IPPROTO_IPV6:
330 return do_add_ioctl(cmd, "sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000331 default:
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000332 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000334}
335
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000336/* Return value becomes exitcode. It's okay to not return at all */
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000337static int do_del(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338{
339 struct ip_tunnel_parm p;
340
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000341 parse_args(argc, argv, SIOCDELTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342
343 switch (p.iph.protocol) {
344 case IPPROTO_IPIP:
345 return do_del_ioctl("tunl0", &p);
346 case IPPROTO_GRE:
347 return do_del_ioctl("gre0", &p);
348 case IPPROTO_IPV6:
349 return do_del_ioctl("sit0", &p);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000350 default:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000351 return do_del_ioctl(p.name, &p);
352 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000353}
354
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000355static void print_tunnel(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000356{
357 char s1[256];
358 char s2[256];
359 char s3[64];
360 char s4[64];
361
362 format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
363 format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
364 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
365 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
366
367 printf("%s: %s/ip remote %s local %s ",
368 p->name,
369 p->iph.protocol == IPPROTO_IPIP ? "ip" :
370 (p->iph.protocol == IPPROTO_GRE ? "gre" :
371 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
372 p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
373 if (p->link) {
374 char *n = do_ioctl_get_ifname(p->link);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000375 if (n) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 printf(" dev %s ", n);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000377 free(n);
378 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379 }
380 if (p->iph.ttl)
381 printf(" ttl %d ", p->iph.ttl);
382 else
383 printf(" ttl inherit ");
384 if (p->iph.tos) {
385 SPRINT_BUF(b1);
386 printf(" tos");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000387 if (p->iph.tos & 1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000388 printf(" inherit");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000389 if (p->iph.tos & ~1)
390 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
391 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392 }
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000393 if (!(p->iph.frag_off & htons(IP_DF)))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000394 printf(" nopmtudisc");
395
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000396 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 +0000397 printf(" key %s", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000398 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
399 if (p->i_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000400 printf(" ikey %s ", s3);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000401 if (p->o_flags & GRE_KEY)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402 printf(" okey %s ", s4);
403 }
404
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000405 if (p->i_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000406 printf("%c Drop packets out of sequence.\n", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000407 if (p->i_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000408 printf("%c Checksum in received packet is required.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000409 if (p->o_flags & GRE_SEQ)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000410 printf("%c Sequence packets on output.", _SL_);
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000411 if (p->o_flags & GRE_CSUM)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000412 printf("%c Checksum output packets.", _SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413}
414
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000415static void do_tunnels_list(struct ip_tunnel_parm *p)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000416{
417 char name[IFNAMSIZ];
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000418 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
419 rx_fifo, rx_frame,
420 tx_bytes, tx_packets, tx_errs, tx_drops,
421 tx_fifo, tx_colls, tx_carrier, rx_multi;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000422 int type;
423 struct ip_tunnel_parm p1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000424 char buf[512];
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000425 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000426
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000427 if (fp == NULL) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000428 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000429 }
430
431 fgets(buf, sizeof(buf), fp);
432 fgets(buf, sizeof(buf), fp);
433
434 while (fgets(buf, sizeof(buf), fp) != NULL) {
435 char *ptr;
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000436
437 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
438 ptr = strchr(buf, ':');
439 if (ptr == NULL ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000440 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000441 bb_error_msg("wrong format of /proc/net/dev. Sorry");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000442 return;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443 }
Bernhard Reutner-Fischereedd1be2006-01-12 13:15:49 +0000444 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 +0000445 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
446 &rx_fifo, &rx_frame, &rx_multi,
447 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
448 &tx_fifo, &tx_colls, &tx_carrier) != 14)
449 continue;
450 if (p->name[0] && strcmp(p->name, name))
451 continue;
452 type = do_ioctl_get_iftype(name);
453 if (type == -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000454 bb_error_msg("cannot get type of [%s]", name);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000455 continue;
456 }
457 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
458 continue;
459 memset(&p1, 0, sizeof(p1));
460 if (do_get_ioctl(name, &p1))
461 continue;
462 if ((p->link && p1.link != p->link) ||
463 (p->name[0] && strcmp(p1.name, p->name)) ||
464 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
465 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
466 (p->i_key && p1.i_key != p->i_key))
467 continue;
468 print_tunnel(&p1);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000469 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000470 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000471}
472
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000473/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000474static int do_show(int argc, char **argv)
475{
476 int err;
477 struct ip_tunnel_parm p;
478
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000479 parse_args(argc, argv, SIOCGETTUNNEL, &p);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000480
481 switch (p.iph.protocol) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000482 case IPPROTO_IPIP:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000483 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
484 break;
485 case IPPROTO_GRE:
486 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
487 break;
488 case IPPROTO_IPV6:
489 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
490 break;
491 default:
492 do_tunnels_list(&p);
493 return 0;
494 }
495 if (err)
496 return -1;
497
498 print_tunnel(&p);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000499 puts("");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000500 return 0;
501}
502
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000503/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000504int do_iptunnel(int argc, char **argv)
505{
506 if (argc > 0) {
507 if (matches(*argv, "add") == 0)
508 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
509 if (matches(*argv, "change") == 0)
510 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
511 if (matches(*argv, "del") == 0)
512 return do_del(argc-1, argv+1);
513 if (matches(*argv, "show") == 0 ||
514 matches(*argv, "lst") == 0 ||
515 matches(*argv, "list") == 0)
516 return do_show(argc-1, argv+1);
517 } else
518 return do_show(0, NULL);
519
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000520 bb_error_msg_and_die("command \"%s\" is unknown", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000521}