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)
50 "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n"
51 " [ mode { ip6ip6 | ipip6 | ip6gre | vti6 | any } ]\n"
52 " [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n"
53 " [ encaplimit ELIM ]\n"
54 " [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"
56 " [ [no]allow-localremote ]\n"
57 " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"
59 "Where: NAME := STRING\n"
60 " ADDR := IPV6_ADDRESS\n"
61 " ELIM := { none | 0..255 }(default=%d)\n"
62 " TTL := 0..255 (default=%d)\n"
63 " TCLASS := { 0x0..0xff | inherit }\n"
64 " FLOWLABEL := { 0x0..0xfffff | inherit }\n"
65 " KEY := { DOTTED_QUAD | NUMBER }\n",
66 IPV6_DEFAULT_TNL_ENCAP_LIMIT
,
67 DEFAULT_TNL_HOP_LIMIT
);
71 static void print_tunnel(const void *t
)
73 const struct ip6_tnl_parm2
*p
= t
;
77 /* Do not use format_host() for local addr,
78 * symbolic name will not be useful.
80 printf("%s: %s/ipv6 remote %s local %s",
82 tnl_strproto(p
->proto
),
83 format_host_r(AF_INET6
, 16, &p
->raddr
, s1
, sizeof(s1
)),
84 rt_addr_n2a_r(AF_INET6
, 16, &p
->laddr
, s2
, sizeof(s2
)));
86 const char *n
= ll_index_to_name(p
->link
);
92 if (p
->flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
93 printf(" encaplimit none");
95 printf(" encaplimit %u", p
->encap_limit
);
98 printf(" hoplimit %u", p
->hop_limit
);
100 printf(" hoplimit inherit");
102 if (p
->flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
103 printf(" tclass inherit");
105 __u32 val
= ntohl(p
->flowinfo
& IP6_FLOWINFO_TCLASS
);
107 printf(" tclass 0x%02x", (__u8
)(val
>> 20));
110 if (p
->flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
111 printf(" flowlabel inherit");
113 printf(" flowlabel 0x%05x", ntohl(p
->flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
115 printf(" (flowinfo 0x%08x)", ntohl(p
->flowinfo
));
117 if (p
->flags
& IP6_TNL_F_RCV_DSCP_COPY
)
118 printf(" dscp inherit");
120 if (p
->flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
)
121 printf(" allow-localremote");
123 if ((p
->i_flags
& GRE_KEY
) && (p
->o_flags
& GRE_KEY
) &&
124 p
->o_key
== p
->i_key
)
125 printf(" key %u", ntohl(p
->i_key
));
127 if (p
->i_flags
& GRE_KEY
)
128 printf(" ikey %u", ntohl(p
->i_key
));
129 if (p
->o_flags
& GRE_KEY
)
130 printf(" okey %u", ntohl(p
->o_key
));
133 if (p
->proto
== IPPROTO_GRE
) {
134 if (p
->i_flags
& GRE_SEQ
)
135 printf("%s Drop packets out of sequence.", _SL_
);
136 if (p
->i_flags
& GRE_CSUM
)
137 printf("%s Checksum in received packet is required.", _SL_
);
138 if (p
->o_flags
& GRE_SEQ
)
139 printf("%s Sequence packets on output.", _SL_
);
140 if (p
->o_flags
& GRE_CSUM
)
141 printf("%s Checksum output packets.", _SL_
);
145 static int parse_args(int argc
, char **argv
, int cmd
, struct ip6_tnl_parm2
*p
)
148 const char *medium
= NULL
;
151 if (strcmp(*argv
, "mode") == 0) {
153 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
154 strcmp(*argv
, "ip6ip6") == 0)
155 p
->proto
= IPPROTO_IPV6
;
156 else if (strcmp(*argv
, "vti6") == 0) {
157 p
->proto
= IPPROTO_IPV6
;
158 p
->i_flags
|= VTI_ISVTI
;
159 } else if (strcmp(*argv
, "ip/ipv6") == 0 ||
160 strcmp(*argv
, "ipv4/ipv6") == 0 ||
161 strcmp(*argv
, "ipip6") == 0 ||
162 strcmp(*argv
, "ip4ip6") == 0)
163 p
->proto
= IPPROTO_IPIP
;
164 else if (strcmp(*argv
, "ip6gre") == 0 ||
165 strcmp(*argv
, "gre/ipv6") == 0)
166 p
->proto
= IPPROTO_GRE
;
167 else if (strcmp(*argv
, "any/ipv6") == 0 ||
168 strcmp(*argv
, "any") == 0)
171 fprintf(stderr
, "Unknown tunnel mode \"%s\"\n", *argv
);
174 } else if (strcmp(*argv
, "remote") == 0) {
178 get_addr(&raddr
, *argv
, AF_INET6
);
179 memcpy(&p
->raddr
, &raddr
.data
, sizeof(p
->raddr
));
180 } else if (strcmp(*argv
, "local") == 0) {
184 get_addr(&laddr
, *argv
, AF_INET6
);
185 memcpy(&p
->laddr
, &laddr
.data
, sizeof(p
->laddr
));
186 } else if (strcmp(*argv
, "dev") == 0) {
189 } else if (strcmp(*argv
, "encaplimit") == 0) {
191 if (strcmp(*argv
, "none") == 0) {
192 p
->flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
196 if (get_u8(&uval
, *argv
, 0) < -1)
197 invarg("invalid ELIM", *argv
);
198 p
->encap_limit
= uval
;
199 p
->flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
201 } else if (strcmp(*argv
, "hoplimit") == 0 ||
202 strcmp(*argv
, "ttl") == 0 ||
203 strcmp(*argv
, "hlim") == 0) {
207 if (get_u8(&uval
, *argv
, 0))
208 invarg("invalid TTL", *argv
);
210 } else if (strcmp(*argv
, "tclass") == 0 ||
211 strcmp(*argv
, "tc") == 0 ||
212 strcmp(*argv
, "tos") == 0 ||
213 matches(*argv
, "dsfield") == 0) {
217 p
->flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
218 if (strcmp(*argv
, "inherit") == 0)
219 p
->flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
221 if (get_u8(&uval
, *argv
, 16))
222 invarg("invalid TClass", *argv
);
223 p
->flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
224 p
->flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
226 } else if (strcmp(*argv
, "flowlabel") == 0 ||
227 strcmp(*argv
, "fl") == 0) {
231 p
->flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
232 if (strcmp(*argv
, "inherit") == 0)
233 p
->flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
235 if (get_u32(&uval
, *argv
, 16))
236 invarg("invalid Flowlabel", *argv
);
238 invarg("invalid Flowlabel", *argv
);
239 p
->flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
240 p
->flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
242 } else if (strcmp(*argv
, "dscp") == 0) {
244 if (strcmp(*argv
, "inherit") != 0)
245 invarg("not inherit", *argv
);
246 p
->flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
247 } else if (strcmp(*argv
, "allow-localremote") == 0) {
248 p
->flags
|= IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
249 } else if (strcmp(*argv
, "noallow-localremote") == 0) {
250 p
->flags
&= ~IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
251 } else if (strcmp(*argv
, "key") == 0) {
253 p
->i_flags
|= GRE_KEY
;
254 p
->o_flags
|= GRE_KEY
;
255 p
->i_key
= p
->o_key
= tnl_parse_key("key", *argv
);
256 } else if (strcmp(*argv
, "ikey") == 0) {
258 p
->i_flags
|= GRE_KEY
;
259 p
->i_key
= tnl_parse_key("ikey", *argv
);
260 } else if (strcmp(*argv
, "okey") == 0) {
262 p
->o_flags
|= GRE_KEY
;
263 p
->o_key
= tnl_parse_key("okey", *argv
);
264 } else if (strcmp(*argv
, "seq") == 0) {
265 p
->i_flags
|= GRE_SEQ
;
266 p
->o_flags
|= GRE_SEQ
;
267 } else if (strcmp(*argv
, "iseq") == 0) {
268 p
->i_flags
|= GRE_SEQ
;
269 } else if (strcmp(*argv
, "oseq") == 0) {
270 p
->o_flags
|= GRE_SEQ
;
271 } else if (strcmp(*argv
, "csum") == 0) {
272 p
->i_flags
|= GRE_CSUM
;
273 p
->o_flags
|= GRE_CSUM
;
274 } else if (strcmp(*argv
, "icsum") == 0) {
275 p
->i_flags
|= GRE_CSUM
;
276 } else if (strcmp(*argv
, "ocsum") == 0) {
277 p
->o_flags
|= GRE_CSUM
;
279 if (strcmp(*argv
, "name") == 0) {
281 } else if (matches(*argv
, "help") == 0)
284 duparg2("name", *argv
);
285 if (get_ifname(p
->name
, *argv
))
286 invarg("\"name\" not a valid ifname", *argv
);
287 if (cmd
== SIOCCHGTUNNEL
&& count
== 0) {
288 struct ip6_tnl_parm2 old_p
= {};
290 if (tnl_get_ioctl(*argv
, &old_p
))
299 p
->link
= ll_name_to_index(medium
);
301 return nodev(medium
);
303 strlcpy(p
->name
, medium
, sizeof(p
->name
));
308 static void ip6_tnl_parm_init(struct ip6_tnl_parm2
*p
, int apply_default
)
310 memset(p
, 0, sizeof(*p
));
311 p
->proto
= IPPROTO_IPV6
;
313 p
->hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
314 p
->encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
318 static void ip6_tnl_parm_initialize(const struct tnl_print_nlmsg_info
*info
)
320 const struct ifinfomsg
*ifi
= info
->ifi
;
321 const struct ip6_tnl_parm2
*p1
= info
->p1
;
322 struct ip6_tnl_parm2
*p2
= info
->p2
;
324 ip6_tnl_parm_init(p2
, 0);
325 if (ifi
->ifi_type
== ARPHRD_IP6GRE
)
326 p2
->proto
= IPPROTO_GRE
;
327 p2
->link
= ifi
->ifi_index
;
328 strcpy(p2
->name
, p1
->name
);
331 static bool ip6_tnl_parm_match(const struct tnl_print_nlmsg_info
*info
)
333 const struct ip6_tnl_parm2
*p1
= info
->p1
;
334 const struct ip6_tnl_parm2
*p2
= info
->p2
;
336 return ((!p1
->link
|| p1
->link
== p2
->link
) &&
337 (!p1
->name
[0] || strcmp(p1
->name
, p2
->name
) == 0) &&
338 (IN6_IS_ADDR_UNSPECIFIED(&p1
->laddr
) ||
339 IN6_ARE_ADDR_EQUAL(&p1
->laddr
, &p2
->laddr
)) &&
340 (IN6_IS_ADDR_UNSPECIFIED(&p1
->raddr
) ||
341 IN6_ARE_ADDR_EQUAL(&p1
->raddr
, &p2
->raddr
)) &&
342 (!p1
->proto
|| !p2
->proto
|| p1
->proto
== p2
->proto
) &&
343 (!p1
->encap_limit
|| p1
->encap_limit
== p2
->encap_limit
) &&
344 (!p1
->hop_limit
|| p1
->hop_limit
== p2
->hop_limit
) &&
345 (!(p1
->flowinfo
& IP6_FLOWINFO_TCLASS
) ||
346 !((p1
->flowinfo
^ p2
->flowinfo
) & IP6_FLOWINFO_TCLASS
)) &&
347 (!(p1
->flowinfo
& IP6_FLOWINFO_FLOWLABEL
) ||
348 !((p1
->flowinfo
^ p2
->flowinfo
) & IP6_FLOWINFO_FLOWLABEL
)) &&
349 (!p1
->flags
|| (p1
->flags
& p2
->flags
)));
352 static int do_show(int argc
, char **argv
)
354 struct ip6_tnl_parm2 p
, p1
;
356 ip6_tnl_parm_init(&p
, 0);
357 p
.proto
= 0; /* default to any */
359 if (parse_args(argc
, argv
, SIOCGETTUNNEL
, &p
) < 0)
362 if (!p
.name
[0] || show_stats
) {
363 struct tnl_print_nlmsg_info info
= {
366 .init
= ip6_tnl_parm_initialize
,
367 .match
= ip6_tnl_parm_match
,
368 .print
= print_tunnel
,
371 return do_tunnels_list(&info
);
374 if (tnl_get_ioctl(p
.name
, &p
))
382 static int do_add(int cmd
, int argc
, char **argv
)
384 struct ip6_tnl_parm2 p
;
385 const char *basedev
= "ip6tnl0";
387 ip6_tnl_parm_init(&p
, 1);
389 if (parse_args(argc
, argv
, cmd
, &p
) < 0)
392 if (p
.proto
== IPPROTO_GRE
)
394 else if (p
.i_flags
& VTI_ISVTI
)
395 basedev
= "ip6_vti0";
397 return tnl_add_ioctl(cmd
, basedev
, p
.name
, &p
);
400 static int do_del(int argc
, char **argv
)
402 struct ip6_tnl_parm2 p
;
403 const char *basedev
= "ip6tnl0";
405 ip6_tnl_parm_init(&p
, 1);
407 if (parse_args(argc
, argv
, SIOCDELTUNNEL
, &p
) < 0)
410 if (p
.proto
== IPPROTO_GRE
)
412 else if (p
.i_flags
& VTI_ISVTI
)
413 basedev
= "ip6_vti0";
415 return tnl_del_ioctl(basedev
, p
.name
, &p
);
418 int do_ip6tunnel(int argc
, char **argv
)
420 switch (preferred_family
) {
422 preferred_family
= AF_INET6
;
427 fprintf(stderr
, "Unsupported protocol family: %d\n", preferred_family
);
432 if (matches(*argv
, "add") == 0)
433 return do_add(SIOCADDTUNNEL
, argc
- 1, argv
+ 1);
434 if (matches(*argv
, "change") == 0)
435 return do_add(SIOCCHGTUNNEL
, argc
- 1, argv
+ 1);
436 if (matches(*argv
, "delete") == 0)
437 return do_del(argc
- 1, argv
+ 1);
438 if (matches(*argv
, "show") == 0 ||
439 matches(*argv
, "lst") == 0 ||
440 matches(*argv
, "list") == 0)
441 return do_show(argc
- 1, argv
+ 1);
442 if (matches(*argv
, "help") == 0)
445 return do_show(0, NULL
);
447 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv
);