2 * Copyright (C)2006 USAGI/WIDE Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses>.
19 * Masahide NAKAMURA @USAGI
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_tunnel.h>
34 #include <linux/ip6_tunnel.h>
38 #include "ip_common.h"
40 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
41 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
43 #define DEFAULT_TNL_HOP_LIMIT (64)
45 static void usage(void) __attribute__((noreturn
));
47 static void usage(void)
49 fprintf(stderr
, "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n");
50 fprintf(stderr
, " [ mode { ip6ip6 | ipip6 | ip6gre | vti6 | any } ]\n");
51 fprintf(stderr
, " [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n");
52 fprintf(stderr
, " [ encaplimit ELIM ]\n");
53 fprintf(stderr
, " [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
54 fprintf(stderr
, " [ dscp inherit ]\n");
55 fprintf(stderr
, " [ [no]allow-localremote ]\n");
56 fprintf(stderr
, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
57 fprintf(stderr
, "\n");
58 fprintf(stderr
, "Where: NAME := STRING\n");
59 fprintf(stderr
, " ADDR := IPV6_ADDRESS\n");
60 fprintf(stderr
, " ELIM := { none | 0..255 }(default=%d)\n",
61 IPV6_DEFAULT_TNL_ENCAP_LIMIT
);
62 fprintf(stderr
, " TTL := 0..255 (default=%d)\n",
63 DEFAULT_TNL_HOP_LIMIT
);
64 fprintf(stderr
, " TCLASS := { 0x0..0xff | inherit }\n");
65 fprintf(stderr
, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
66 fprintf(stderr
, " KEY := { DOTTED_QUAD | NUMBER }\n");
70 static void print_tunnel(struct ip6_tnl_parm2
*p
)
75 /* Do not use format_host() for local addr,
76 * symbolic name will not be useful.
78 printf("%s: %s/ipv6 remote %s local %s",
80 tnl_strproto(p
->proto
),
81 format_host_r(AF_INET6
, 16, &p
->raddr
, s1
, sizeof(s1
)),
82 rt_addr_n2a_r(AF_INET6
, 16, &p
->laddr
, s2
, sizeof(s2
)));
84 const char *n
= ll_index_to_name(p
->link
);
90 if (p
->flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
91 printf(" encaplimit none");
93 printf(" encaplimit %u", p
->encap_limit
);
96 printf(" hoplimit %u", p
->hop_limit
);
98 printf(" hoplimit inherit");
100 if (p
->flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
101 printf(" tclass inherit");
103 __u32 val
= ntohl(p
->flowinfo
& IP6_FLOWINFO_TCLASS
);
105 printf(" tclass 0x%02x", (__u8
)(val
>> 20));
108 if (p
->flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
109 printf(" flowlabel inherit");
111 printf(" flowlabel 0x%05x", ntohl(p
->flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
113 printf(" (flowinfo 0x%08x)", ntohl(p
->flowinfo
));
115 if (p
->flags
& IP6_TNL_F_RCV_DSCP_COPY
)
116 printf(" dscp inherit");
118 if (p
->flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
)
119 printf(" allow-localremote");
121 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) &&
122 p
->o_key
== p
->i_key
)
123 printf(" key %u", ntohl(p
->i_key
));
125 if (p
->i_flags
& GRE_KEY
)
126 printf(" ikey %u", ntohl(p
->i_key
));
127 if (p
->o_flags
& GRE_KEY
)
128 printf(" okey %u", ntohl(p
->o_key
));
131 if (p
->proto
== IPPROTO_GRE
) {
132 if (p
->i_flags
& GRE_SEQ
)
133 printf("%s Drop packets out of sequence.", _SL_
);
134 if (p
->i_flags
& GRE_CSUM
)
135 printf("%s Checksum in received packet is required.", _SL_
);
136 if (p
->o_flags
& GRE_SEQ
)
137 printf("%s Sequence packets on output.", _SL_
);
138 if (p
->o_flags
& GRE_CSUM
)
139 printf("%s Checksum output packets.", _SL_
);
143 static int parse_args(int argc
, char **argv
, int cmd
, struct ip6_tnl_parm2
*p
)
146 const char *medium
= NULL
;
149 if (strcmp(*argv
, "mode") == 0) {
151 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
152 strcmp(*argv
, "ip6ip6") == 0)
153 p
->proto
= IPPROTO_IPV6
;
154 else if (strcmp(*argv
, "vti6") == 0) {
155 p
->proto
= IPPROTO_IPV6
;
156 p
->i_flags
|= VTI_ISVTI
;
157 } else if (strcmp(*argv
, "ip/ipv6") == 0 ||
158 strcmp(*argv
, "ipv4/ipv6") == 0 ||
159 strcmp(*argv
, "ipip6") == 0 ||
160 strcmp(*argv
, "ip4ip6") == 0)
161 p
->proto
= IPPROTO_IPIP
;
162 else if (strcmp(*argv
, "ip6gre") == 0 ||
163 strcmp(*argv
, "gre/ipv6") == 0)
164 p
->proto
= IPPROTO_GRE
;
165 else if (strcmp(*argv
, "any/ipv6") == 0 ||
166 strcmp(*argv
, "any") == 0)
169 fprintf(stderr
, "Unknown tunnel mode \"%s\"\n", *argv
);
172 } else if (strcmp(*argv
, "remote") == 0) {
176 get_addr(&raddr
, *argv
, AF_INET6
);
177 memcpy(&p
->raddr
, &raddr
.data
, sizeof(p
->raddr
));
178 } else if (strcmp(*argv
, "local") == 0) {
182 get_addr(&laddr
, *argv
, AF_INET6
);
183 memcpy(&p
->laddr
, &laddr
.data
, sizeof(p
->laddr
));
184 } else if (strcmp(*argv
, "dev") == 0) {
187 } else if (strcmp(*argv
, "encaplimit") == 0) {
189 if (strcmp(*argv
, "none") == 0) {
190 p
->flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
194 if (get_u8(&uval
, *argv
, 0) < -1)
195 invarg("invalid ELIM", *argv
);
196 p
->encap_limit
= uval
;
197 p
->flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
199 } else if (strcmp(*argv
, "hoplimit") == 0 ||
200 strcmp(*argv
, "ttl") == 0 ||
201 strcmp(*argv
, "hlim") == 0) {
205 if (get_u8(&uval
, *argv
, 0))
206 invarg("invalid TTL", *argv
);
208 } else if (strcmp(*argv
, "tclass") == 0 ||
209 strcmp(*argv
, "tc") == 0 ||
210 strcmp(*argv
, "tos") == 0 ||
211 matches(*argv
, "dsfield") == 0) {
215 p
->flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
216 if (strcmp(*argv
, "inherit") == 0)
217 p
->flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
219 if (get_u8(&uval
, *argv
, 16))
220 invarg("invalid TClass", *argv
);
221 p
->flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
222 p
->flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
224 } else if (strcmp(*argv
, "flowlabel") == 0 ||
225 strcmp(*argv
, "fl") == 0) {
229 p
->flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
230 if (strcmp(*argv
, "inherit") == 0)
231 p
->flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
233 if (get_u32(&uval
, *argv
, 16))
234 invarg("invalid Flowlabel", *argv
);
236 invarg("invalid Flowlabel", *argv
);
237 p
->flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
238 p
->flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
240 } else if (strcmp(*argv
, "dscp") == 0) {
242 if (strcmp(*argv
, "inherit") != 0)
243 invarg("not inherit", *argv
);
244 p
->flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
245 } else if (strcmp(*argv
, "allow-localremote") == 0) {
246 p
->flags
|= IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
247 } else if (strcmp(*argv
, "noallow-localremote") == 0) {
248 p
->flags
&= ~IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
249 } else if (strcmp(*argv
, "key") == 0) {
251 p
->i_flags
|= GRE_KEY
;
252 p
->o_flags
|= GRE_KEY
;
253 p
->i_key
= p
->o_key
= tnl_parse_key("key", *argv
);
254 } else if (strcmp(*argv
, "ikey") == 0) {
256 p
->i_flags
|= GRE_KEY
;
257 p
->i_key
= tnl_parse_key("ikey", *argv
);
258 } else if (strcmp(*argv
, "okey") == 0) {
260 p
->o_flags
|= GRE_KEY
;
261 p
->o_key
= tnl_parse_key("okey", *argv
);
262 } else if (strcmp(*argv
, "seq") == 0) {
263 p
->i_flags
|= GRE_SEQ
;
264 p
->o_flags
|= GRE_SEQ
;
265 } else if (strcmp(*argv
, "iseq") == 0) {
266 p
->i_flags
|= GRE_SEQ
;
267 } else if (strcmp(*argv
, "oseq") == 0) {
268 p
->o_flags
|= GRE_SEQ
;
269 } else if (strcmp(*argv
, "csum") == 0) {
270 p
->i_flags
|= GRE_CSUM
;
271 p
->o_flags
|= GRE_CSUM
;
272 } else if (strcmp(*argv
, "icsum") == 0) {
273 p
->i_flags
|= GRE_CSUM
;
274 } else if (strcmp(*argv
, "ocsum") == 0) {
275 p
->o_flags
|= GRE_CSUM
;
277 if (strcmp(*argv
, "name") == 0) {
279 } else if (matches(*argv
, "help") == 0)
282 duparg2("name", *argv
);
283 if (get_ifname(p
->name
, *argv
))
284 invarg("\"name\" not a valid ifname", *argv
);
285 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
286 struct ip6_tnl_parm2 old_p
= {};
288 if (tnl_get_ioctl(*argv
, &old_p
))
297 p
->link
= ll_name_to_index(medium
);
299 fprintf(stderr
, "Cannot find device \"%s\"\n", medium
);
306 static void ip6_tnl_parm_init(struct ip6_tnl_parm2
*p
, int apply_default
)
308 memset(p
, 0, sizeof(*p
));
309 p
->proto
= IPPROTO_IPV6
;
311 p
->hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
312 p
->encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
317 * @p1: user specified parameter
318 * @p2: database entry
320 static int ip6_tnl_parm_match(const struct ip6_tnl_parm2
*p1
,
321 const struct ip6_tnl_parm2
*p2
)
323 return ((!p1
->link
|| p1
->link
== p2
->link
) &&
324 (!p1
->name
[0] || strcmp(p1
->name
, p2
->name
) == 0) &&
325 (IN6_IS_ADDR_UNSPECIFIED(&p1
->laddr
) ||
326 IN6_ARE_ADDR_EQUAL(&p1
->laddr
, &p2
->laddr
)) &&
327 (IN6_IS_ADDR_UNSPECIFIED(&p1
->raddr
) ||
328 IN6_ARE_ADDR_EQUAL(&p1
->raddr
, &p2
->raddr
)) &&
329 (!p1
->proto
|| !p2
->proto
|| p1
->proto
== p2
->proto
) &&
330 (!p1
->encap_limit
|| p1
->encap_limit
== p2
->encap_limit
) &&
331 (!p1
->hop_limit
|| p1
->hop_limit
== p2
->hop_limit
) &&
332 (!(p1
->flowinfo
& IP6_FLOWINFO_TCLASS
) ||
333 !((p1
->flowinfo
^ p2
->flowinfo
) & IP6_FLOWINFO_TCLASS
)) &&
334 (!(p1
->flowinfo
& IP6_FLOWINFO_FLOWLABEL
) ||
335 !((p1
->flowinfo
^ p2
->flowinfo
) & IP6_FLOWINFO_FLOWLABEL
)) &&
336 (!p1
->flags
|| (p1
->flags
& p2
->flags
)));
339 static int do_tunnels_list(struct ip6_tnl_parm2
*p
)
343 FILE *fp
= fopen("/proc/net/dev", "r");
350 /* skip two lines at the begenning of the file */
351 if (!fgets(buf
, sizeof(buf
), fp
) ||
352 !fgets(buf
, sizeof(buf
), fp
)) {
353 fprintf(stderr
, "/proc/net/dev read error\n");
357 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
360 struct ip6_tnl_parm2 p1
= {};
363 buf
[sizeof(buf
) - 1] = '\0';
364 if ((ptr
= strchr(buf
, ':')) == NULL
||
365 (*ptr
++ = 0, sscanf(buf
, "%s", name
) != 1)) {
366 fprintf(stderr
, "Wrong format for /proc/net/dev. Giving up.\n");
369 if (p
->name
[0] && strcmp(p
->name
, name
))
371 index
= ll_name_to_index(name
);
374 type
= ll_index_to_type(index
);
376 fprintf(stderr
, "Failed to get type of \"%s\"\n", name
);
379 if (type
!= ARPHRD_TUNNEL6
&& type
!= ARPHRD_IP6GRE
)
381 ip6_tnl_parm_init(&p1
, 0);
382 if (type
== ARPHRD_IP6GRE
)
383 p1
.proto
= IPPROTO_GRE
;
384 strcpy(p1
.name
, name
);
385 p1
.link
= ll_name_to_index(p1
.name
);
388 if (tnl_get_ioctl(p1
.name
, &p1
))
390 if (!ip6_tnl_parm_match(p
, &p1
))
394 tnl_print_stats(ptr
);
403 static int do_show(int argc
, char **argv
)
405 struct ip6_tnl_parm2 p
;
408 ip6_tnl_parm_init(&p
, 0);
409 p
.proto
= 0; /* default to any */
411 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
414 if (!p
.name
[0] || show_stats
)
417 if (tnl_get_ioctl(p
.name
, &p
))
426 static int do_add(int cmd
, int argc
, char **argv
)
428 struct ip6_tnl_parm2 p
;
429 const char *basedev
= "ip6tnl0";
431 ip6_tnl_parm_init(&p
, 1);
433 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
436 if (p
.proto
== IPPROTO_GRE
)
438 else if (p
.i_flags
& VTI_ISVTI
)
439 basedev
= "ip6_vti0";
441 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
444 static int do_del(int argc
, char **argv
)
446 struct ip6_tnl_parm2 p
;
447 const char *basedev
= "ip6tnl0";
449 ip6_tnl_parm_init(&p
, 1);
451 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
454 if (p
.proto
== IPPROTO_GRE
)
456 else if (p
.i_flags
& VTI_ISVTI
)
457 basedev
= "ip6_vti0";
459 return tnl_del_ioctl(basedev
, p
.name
, &p
);
462 int do_ip6tunnel(int argc
, char **argv
)
464 switch (preferred_family
) {
466 preferred_family
= AF_INET6
;
471 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
476 if (matches(*argv
, "add") == 0)
477 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
478 if (matches(*argv
, "change") == 0)
479 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
480 if (matches(*argv
, "delete") == 0)
481 return do_del(argc
- 1, argv
+ 1);
482 if (matches(*argv
, "show") == 0 ||
483 matches(*argv
, "lst") == 0 ||
484 matches(*argv
, "list") == 0)
485 return do_show(argc
- 1, argv
+ 1);
486 if (matches(*argv
, "help") == 0)
489 return do_show(0, NULL
);
491 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv
);