blob: 338d8bdf34aa82db786080c0ce7d990c829a0039 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * iptunnel.c "ip tunnel"
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 *
12 * Changes:
13 *
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
16 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090023#include <sys/types.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024#include <sys/socket.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000025#include <arpa/inet.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000026#include <sys/ioctl.h>
27#include <linux/if.h>
28#include <linux/if_arp.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000029#include <linux/ip.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030#include <linux/if_tunnel.h>
31
32#include "rt_names.h"
33#include "utils.h"
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090034#include "ip_common.h"
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090035#include "tunnel.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036
37static void usage(void) __attribute__((noreturn));
38
39static void usage(void)
40{
Sascha Hlusiaka07e9912009-05-04 01:44:47 +020041 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl } [ NAME ]\n");
Templin, Fred L0bd17922007-11-26 09:46:06 -080042 fprintf(stderr, " [ mode { ipip | gre | sit | isatap } ] [ remote ADDR ] [ local ADDR ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
Sascha Hlusiaka07e9912009-05-04 01:44:47 +020044 fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
46 fprintf(stderr, "\n");
47 fprintf(stderr, "Where: NAME := STRING\n");
48 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
49 fprintf(stderr, " TOS := { NUMBER | inherit }\n");
50 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
51 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
52 exit(-1);
53}
54
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000055static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
56{
57 int count = 0;
58 char medium[IFNAMSIZ];
Templin, Fred L0bd17922007-11-26 09:46:06 -080059 int isatap = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000060
61 memset(p, 0, sizeof(*p));
62 memset(&medium, 0, sizeof(medium));
63
64 p->iph.version = 4;
65 p->iph.ihl = 5;
66#ifndef IP_DF
67#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
68#endif
69 p->iph.frag_off = htons(IP_DF);
70
71 while (argc > 0) {
72 if (strcmp(*argv, "mode") == 0) {
73 NEXT_ARG();
74 if (strcmp(*argv, "ipip") == 0 ||
75 strcmp(*argv, "ip/ip") == 0) {
76 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
77 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
78 exit(-1);
79 }
80 p->iph.protocol = IPPROTO_IPIP;
81 } else if (strcmp(*argv, "gre") == 0 ||
82 strcmp(*argv, "gre/ip") == 0) {
83 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
84 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
85 exit(-1);
86 }
87 p->iph.protocol = IPPROTO_GRE;
88 } else if (strcmp(*argv, "sit") == 0 ||
89 strcmp(*argv, "ipv6/ip") == 0) {
90 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
91 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
92 exit(-1);
93 }
94 p->iph.protocol = IPPROTO_IPV6;
Templin, Fred L0bd17922007-11-26 09:46:06 -080095 } else if (strcmp(*argv, "isatap") == 0) {
96 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
97 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
98 exit(-1);
99 }
100 p->iph.protocol = IPPROTO_IPV6;
101 isatap++;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000102 } else {
103 fprintf(stderr,"Cannot guess tunnel mode.\n");
104 exit(-1);
105 }
106 } else if (strcmp(*argv, "key") == 0) {
107 unsigned uval;
108 NEXT_ARG();
109 p->i_flags |= GRE_KEY;
110 p->o_flags |= GRE_KEY;
111 if (strchr(*argv, '.'))
112 p->i_key = p->o_key = get_addr32(*argv);
113 else {
114 if (get_unsigned(&uval, *argv, 0)<0) {
115 fprintf(stderr, "invalid value of \"key\"\n");
116 exit(-1);
117 }
118 p->i_key = p->o_key = htonl(uval);
119 }
120 } else if (strcmp(*argv, "ikey") == 0) {
121 unsigned uval;
122 NEXT_ARG();
123 p->i_flags |= GRE_KEY;
124 if (strchr(*argv, '.'))
Herbert Xu4282c6c2007-10-12 10:56:40 +0200125 p->i_key = get_addr32(*argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000126 else {
127 if (get_unsigned(&uval, *argv, 0)<0) {
128 fprintf(stderr, "invalid value of \"ikey\"\n");
129 exit(-1);
130 }
131 p->i_key = htonl(uval);
132 }
133 } else if (strcmp(*argv, "okey") == 0) {
134 unsigned uval;
135 NEXT_ARG();
136 p->o_flags |= GRE_KEY;
137 if (strchr(*argv, '.'))
138 p->o_key = get_addr32(*argv);
139 else {
140 if (get_unsigned(&uval, *argv, 0)<0) {
141 fprintf(stderr, "invalid value of \"okey\"\n");
142 exit(-1);
143 }
144 p->o_key = htonl(uval);
145 }
146 } else if (strcmp(*argv, "seq") == 0) {
147 p->i_flags |= GRE_SEQ;
148 p->o_flags |= GRE_SEQ;
149 } else if (strcmp(*argv, "iseq") == 0) {
150 p->i_flags |= GRE_SEQ;
151 } else if (strcmp(*argv, "oseq") == 0) {
152 p->o_flags |= GRE_SEQ;
153 } else if (strcmp(*argv, "csum") == 0) {
154 p->i_flags |= GRE_CSUM;
155 p->o_flags |= GRE_CSUM;
156 } else if (strcmp(*argv, "icsum") == 0) {
157 p->i_flags |= GRE_CSUM;
158 } else if (strcmp(*argv, "ocsum") == 0) {
159 p->o_flags |= GRE_CSUM;
160 } else if (strcmp(*argv, "nopmtudisc") == 0) {
161 p->iph.frag_off = 0;
162 } else if (strcmp(*argv, "pmtudisc") == 0) {
163 p->iph.frag_off = htons(IP_DF);
164 } else if (strcmp(*argv, "remote") == 0) {
165 NEXT_ARG();
166 if (strcmp(*argv, "any"))
167 p->iph.daddr = get_addr32(*argv);
168 } else if (strcmp(*argv, "local") == 0) {
169 NEXT_ARG();
170 if (strcmp(*argv, "any"))
171 p->iph.saddr = get_addr32(*argv);
172 } else if (strcmp(*argv, "dev") == 0) {
173 NEXT_ARG();
174 strncpy(medium, *argv, IFNAMSIZ-1);
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400175 } else if (strcmp(*argv, "ttl") == 0 ||
176 strcmp(*argv, "hoplimit") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000177 unsigned uval;
178 NEXT_ARG();
179 if (strcmp(*argv, "inherit") != 0) {
180 if (get_unsigned(&uval, *argv, 0))
181 invarg("invalid TTL\n", *argv);
182 if (uval > 255)
183 invarg("TTL must be <=255\n", *argv);
184 p->iph.ttl = uval;
185 }
186 } else if (strcmp(*argv, "tos") == 0 ||
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400187 strcmp(*argv, "tclass") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188 matches(*argv, "dsfield") == 0) {
189 __u32 uval;
190 NEXT_ARG();
191 if (strcmp(*argv, "inherit") != 0) {
192 if (rtnl_dsfield_a2n(&uval, *argv))
193 invarg("bad TOS value", *argv);
194 p->iph.tos = uval;
195 } else
196 p->iph.tos = 1;
197 } else {
198 if (strcmp(*argv, "name") == 0) {
199 NEXT_ARG();
Andreas Henriksson1f1ae522008-09-18 10:55:03 -0700200 } else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 usage();
202 if (p->name[0])
203 duparg2("name", *argv);
204 strncpy(p->name, *argv, IFNAMSIZ);
205 if (cmd == SIOCCHGTUNNEL && count == 0) {
206 struct ip_tunnel_parm old_p;
207 memset(&old_p, 0, sizeof(old_p));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900208 if (tnl_get_ioctl(*argv, &old_p))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000209 return -1;
210 *p = old_p;
211 }
212 }
213 count++;
214 argc--; argv++;
215 }
216
217
218 if (p->iph.protocol == 0) {
219 if (memcmp(p->name, "gre", 3) == 0)
220 p->iph.protocol = IPPROTO_GRE;
221 else if (memcmp(p->name, "ipip", 4) == 0)
222 p->iph.protocol = IPPROTO_IPIP;
223 else if (memcmp(p->name, "sit", 3) == 0)
224 p->iph.protocol = IPPROTO_IPV6;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800225 else if (memcmp(p->name, "isatap", 6) == 0) {
226 p->iph.protocol = IPPROTO_IPV6;
227 isatap++;
228 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229 }
230
231 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
232 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
233 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
234 return -1;
235 }
236 }
237
238 if (medium[0]) {
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900239 p->link = tnl_ioctl_get_ifindex(medium);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000240 if (p->link == 0)
241 return -1;
242 }
243
244 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
245 p->i_key = p->iph.daddr;
246 p->i_flags |= GRE_KEY;
247 }
248 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
249 p->o_key = p->iph.daddr;
250 p->o_flags |= GRE_KEY;
251 }
252 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
253 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
254 return -1;
255 }
Sascha Hlusiakeeef12c2009-03-27 11:14:00 -0700256 if (isatap)
Templin, Fred L0bd17922007-11-26 09:46:06 -0800257 p->i_flags |= SIT_ISATAP;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800258
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000259 return 0;
260}
261
262
263static int do_add(int cmd, int argc, char **argv)
264{
265 struct ip_tunnel_parm p;
266
267 if (parse_args(argc, argv, cmd, &p) < 0)
268 return -1;
269
270 if (p.iph.ttl && p.iph.frag_off == 0) {
271 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
272 return -1;
273 }
274
275 switch (p.iph.protocol) {
276 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900277 return tnl_add_ioctl(cmd, "tunl0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900279 return tnl_add_ioctl(cmd, "gre0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000280 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900281 return tnl_add_ioctl(cmd, "sit0", p.name, &p);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800282 default:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000283 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
284 return -1;
285 }
286 return -1;
287}
288
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900289static int do_del(int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290{
291 struct ip_tunnel_parm p;
292
293 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
294 return -1;
295
296 switch (p.iph.protocol) {
297 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900298 return tnl_del_ioctl("tunl0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000299 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900300 return tnl_del_ioctl("gre0", p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000301 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900302 return tnl_del_ioctl("sit0", p.name, &p);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800303 default:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900304 return tnl_del_ioctl(p.name, p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305 }
306 return -1;
307}
308
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900309static void print_tunnel(struct ip_tunnel_parm *p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000310{
311 char s1[1024];
312 char s2[1024];
313 char s3[64];
314 char s4[64];
315
316 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
317 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
318
319 /* Do not use format_host() for local addr,
320 * symbolic name will not be useful.
321 */
322 printf("%s: %s/ip remote %s local %s ",
323 p->name,
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900324 tnl_strproto(p->iph.protocol),
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000325 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
326 p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
327
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200328 if (p->i_flags & SIT_ISATAP) {
329 struct ip_tunnel_prl prl[16];
330 int i;
331
332 memset(prl, 0, sizeof(prl));
333 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
334 prl[0].addr = htonl(INADDR_ANY);
335
336 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
337 for (i = 1; i < sizeof(prl) / sizeof(prl[0]); i++)
338 {
339 if (prl[i].addr != htonl(INADDR_ANY)) {
340 printf(" %s %s ",
341 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
342 format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1)));
343 }
344 }
345 }
346
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000347 if (p->link) {
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900348 char *n = tnl_ioctl_get_ifname(p->link);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000349 if (n)
350 printf(" dev %s ", n);
351 }
352
353 if (p->iph.ttl)
354 printf(" ttl %d ", p->iph.ttl);
355 else
356 printf(" ttl inherit ");
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800357
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000358 if (p->iph.tos) {
359 SPRINT_BUF(b1);
360 printf(" tos");
361 if (p->iph.tos&1)
362 printf(" inherit");
363 if (p->iph.tos&~1)
364 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
365 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
366 }
367
368 if (!(p->iph.frag_off&htons(IP_DF)))
369 printf(" nopmtudisc");
370
371 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
372 printf(" key %s", s3);
373 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
374 if (p->i_flags&GRE_KEY)
375 printf(" ikey %s ", s3);
376 if (p->o_flags&GRE_KEY)
377 printf(" okey %s ", s4);
378 }
379
380 if (p->i_flags&GRE_SEQ)
381 printf("%s Drop packets out of sequence.\n", _SL_);
382 if (p->i_flags&GRE_CSUM)
383 printf("%s Checksum in received packet is required.", _SL_);
384 if (p->o_flags&GRE_SEQ)
385 printf("%s Sequence packets on output.", _SL_);
386 if (p->o_flags&GRE_CSUM)
387 printf("%s Checksum output packets.", _SL_);
388}
389
390static int do_tunnels_list(struct ip_tunnel_parm *p)
391{
392 char name[IFNAMSIZ];
393 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
394 rx_fifo, rx_frame,
395 tx_bytes, tx_packets, tx_errs, tx_drops,
396 tx_fifo, tx_colls, tx_carrier, rx_multi;
397 int type;
398 struct ip_tunnel_parm p1;
399
400 char buf[512];
401 FILE *fp = fopen("/proc/net/dev", "r");
402 if (fp == NULL) {
403 perror("fopen");
404 return -1;
405 }
406
407 fgets(buf, sizeof(buf), fp);
408 fgets(buf, sizeof(buf), fp);
409
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 char *ptr;
412 buf[sizeof(buf) - 1] = 0;
413 if ((ptr = strchr(buf, ':')) == NULL ||
414 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
415 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
416 return -1;
417 }
418 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
419 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
420 &rx_fifo, &rx_frame, &rx_multi,
421 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
422 &tx_fifo, &tx_colls, &tx_carrier) != 14)
423 continue;
424 if (p->name[0] && strcmp(p->name, name))
425 continue;
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900426 type = tnl_ioctl_get_iftype(name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000427 if (type == -1) {
428 fprintf(stderr, "Failed to get type of [%s]\n", name);
429 continue;
430 }
431 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
432 continue;
433 memset(&p1, 0, sizeof(p1));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900434 if (tnl_get_ioctl(name, &p1))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000435 continue;
436 if ((p->link && p1.link != p->link) ||
437 (p->name[0] && strcmp(p1.name, p->name)) ||
438 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
439 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
440 (p->i_key && p1.i_key != p->i_key))
441 continue;
442 print_tunnel(&p1);
443 if (show_stats) {
444 printf("%s", _SL_);
445 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
446 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
447 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
448 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
449 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
450 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
451 }
452 printf("\n");
453 }
454 return 0;
455}
456
457static int do_show(int argc, char **argv)
458{
459 int err;
460 struct ip_tunnel_parm p;
461
462 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
463 return -1;
464
465 switch (p.iph.protocol) {
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800466 case IPPROTO_IPIP:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900467 err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000468 break;
469 case IPPROTO_GRE:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900470 err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000471 break;
472 case IPPROTO_IPV6:
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900473 err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000474 break;
475 default:
476 do_tunnels_list(&p);
477 return 0;
478 }
479 if (err)
480 return -1;
481
482 print_tunnel(&p);
483 printf("\n");
484 return 0;
485}
486
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200487static int do_prl(int argc, char **argv)
488{
489 struct ip_tunnel_prl p;
490 int count = 0;
491 int devname = 0;
492 int cmd = 0;
493 char medium[IFNAMSIZ];
494
495 memset(&p, 0, sizeof(p));
496 memset(&medium, 0, sizeof(medium));
497
498 while (argc > 0) {
499 if (strcmp(*argv, "prl-default") == 0) {
500 NEXT_ARG();
501 cmd = SIOCADDPRL;
502 p.addr = get_addr32(*argv);
503 p.flags |= PRL_DEFAULT;
504 count++;
505 } else if (strcmp(*argv, "prl-nodefault") == 0) {
506 NEXT_ARG();
507 cmd = SIOCADDPRL;
508 p.addr = get_addr32(*argv);
509 count++;
510 } else if (strcmp(*argv, "prl-delete") == 0) {
511 NEXT_ARG();
512 cmd = SIOCDELPRL;
513 p.addr = get_addr32(*argv);
514 count++;
515 } else if (strcmp(*argv, "dev") == 0) {
516 NEXT_ARG();
517 strncpy(medium, *argv, IFNAMSIZ-1);
518 devname++;
519 } else {
520 fprintf(stderr,"%s: Invalid PRL parameter.\n", *argv);
521 exit(-1);
522 }
523 if (count > 1) {
524 fprintf(stderr,"One PRL entry at a time.\n");
525 exit(-1);
526 }
527 argc--; argv++;
528 }
529 if (devname == 0) {
530 fprintf(stderr, "Must specify dev.\n");
531 exit(-1);
532 }
533
534 return tnl_prl_ioctl(cmd, medium, &p);
535}
536
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000537int do_iptunnel(int argc, char **argv)
538{
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900539 switch (preferred_family) {
540 case AF_UNSPEC:
541 preferred_family = AF_INET;
542 break;
543 case AF_INET:
544 break;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900545 /*
546 * This is silly enough but we have no easy way to make it
547 * protocol-independent because of unarranged structure between
548 * IPv4 and IPv6.
549 */
550 case AF_INET6:
551 return do_ip6tunnel(argc, argv);
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900552 default:
553 fprintf(stderr, "Unsupported family:%d\n", preferred_family);
554 exit(-1);
555 }
556
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000557 if (argc > 0) {
558 if (matches(*argv, "add") == 0)
559 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
560 if (matches(*argv, "change") == 0)
561 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
562 if (matches(*argv, "del") == 0)
563 return do_del(argc-1, argv+1);
564 if (matches(*argv, "show") == 0 ||
565 matches(*argv, "lst") == 0 ||
566 matches(*argv, "list") == 0)
567 return do_show(argc-1, argv+1);
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200568 if (matches(*argv, "prl") == 0)
569 return do_prl(argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000570 if (matches(*argv, "help") == 0)
571 usage();
572 } else
573 return do_show(0, NULL);
574
575 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);
576 exit(-1);
577}