]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip6tunnel.c
Merge branch 'master' into next
[mirror_iproute2.git] / ip / ip6tunnel.c
1 /*
2 * Copyright (C)2006 USAGI/WIDE Project
3 *
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.
8 *
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.
13 *
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>.
16 */
17 /*
18 * Author:
19 * Masahide NAKAMURA @USAGI
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
30 #include <linux/ip.h>
31 #include <linux/if.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_tunnel.h>
34 #include <linux/ip6_tunnel.h>
35
36 #include "utils.h"
37 #include "tunnel.h"
38 #include "ip_common.h"
39
40 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
41 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
42
43 #define DEFAULT_TNL_HOP_LIMIT (64)
44
45 static void usage(void) __attribute__((noreturn));
46
47 static void usage(void)
48 {
49 fprintf(stderr,
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"
55 " [ dscp inherit ]\n"
56 " [ [no]allow-localremote ]\n"
57 " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"
58 "\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);
68 exit(-1);
69 }
70
71 static void print_tunnel(const void *t)
72 {
73 const struct ip6_tnl_parm2 *p = t;
74 char s1[1024];
75 char s2[1024];
76
77 /* Do not use format_host() for local addr,
78 * symbolic name will not be useful.
79 */
80 printf("%s: %s/ipv6 remote %s local %s",
81 p->name,
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)));
85 if (p->link) {
86 const char *n = ll_index_to_name(p->link);
87
88 if (n)
89 printf(" dev %s", n);
90 }
91
92 if (p->flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
93 printf(" encaplimit none");
94 else
95 printf(" encaplimit %u", p->encap_limit);
96
97 if (p->hop_limit)
98 printf(" hoplimit %u", p->hop_limit);
99 else
100 printf(" hoplimit inherit");
101
102 if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS)
103 printf(" tclass inherit");
104 else {
105 __u32 val = ntohl(p->flowinfo & IP6_FLOWINFO_TCLASS);
106
107 printf(" tclass 0x%02x", (__u8)(val >> 20));
108 }
109
110 if (p->flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
111 printf(" flowlabel inherit");
112 else
113 printf(" flowlabel 0x%05x", ntohl(p->flowinfo & IP6_FLOWINFO_FLOWLABEL));
114
115 printf(" (flowinfo 0x%08x)", ntohl(p->flowinfo));
116
117 if (p->flags & IP6_TNL_F_RCV_DSCP_COPY)
118 printf(" dscp inherit");
119
120 if (p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE)
121 printf(" allow-localremote");
122
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));
126 else {
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));
131 }
132
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_);
142 }
143 }
144
145 static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
146 {
147 int count = 0;
148 const char *medium = NULL;
149
150 while (argc > 0) {
151 if (strcmp(*argv, "mode") == 0) {
152 NEXT_ARG();
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)
169 p->proto = 0;
170 else {
171 fprintf(stderr, "Unknown tunnel mode \"%s\"\n", *argv);
172 exit(-1);
173 }
174 } else if (strcmp(*argv, "remote") == 0) {
175 inet_prefix raddr;
176
177 NEXT_ARG();
178 get_addr(&raddr, *argv, AF_INET6);
179 memcpy(&p->raddr, &raddr.data, sizeof(p->raddr));
180 } else if (strcmp(*argv, "local") == 0) {
181 inet_prefix laddr;
182
183 NEXT_ARG();
184 get_addr(&laddr, *argv, AF_INET6);
185 memcpy(&p->laddr, &laddr.data, sizeof(p->laddr));
186 } else if (strcmp(*argv, "dev") == 0) {
187 NEXT_ARG();
188 medium = *argv;
189 } else if (strcmp(*argv, "encaplimit") == 0) {
190 NEXT_ARG();
191 if (strcmp(*argv, "none") == 0) {
192 p->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
193 } else {
194 __u8 uval;
195
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;
200 }
201 } else if (strcmp(*argv, "hoplimit") == 0 ||
202 strcmp(*argv, "ttl") == 0 ||
203 strcmp(*argv, "hlim") == 0) {
204 __u8 uval;
205
206 NEXT_ARG();
207 if (get_u8(&uval, *argv, 0))
208 invarg("invalid TTL", *argv);
209 p->hop_limit = uval;
210 } else if (strcmp(*argv, "tclass") == 0 ||
211 strcmp(*argv, "tc") == 0 ||
212 strcmp(*argv, "tos") == 0 ||
213 matches(*argv, "dsfield") == 0) {
214 __u8 uval;
215
216 NEXT_ARG();
217 p->flowinfo &= ~IP6_FLOWINFO_TCLASS;
218 if (strcmp(*argv, "inherit") == 0)
219 p->flags |= IP6_TNL_F_USE_ORIG_TCLASS;
220 else {
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;
225 }
226 } else if (strcmp(*argv, "flowlabel") == 0 ||
227 strcmp(*argv, "fl") == 0) {
228 __u32 uval;
229
230 NEXT_ARG();
231 p->flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
232 if (strcmp(*argv, "inherit") == 0)
233 p->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
234 else {
235 if (get_u32(&uval, *argv, 16))
236 invarg("invalid Flowlabel", *argv);
237 if (uval > 0xFFFFF)
238 invarg("invalid Flowlabel", *argv);
239 p->flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
240 p->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
241 }
242 } else if (strcmp(*argv, "dscp") == 0) {
243 NEXT_ARG();
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) {
252 NEXT_ARG();
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) {
257 NEXT_ARG();
258 p->i_flags |= GRE_KEY;
259 p->i_key = tnl_parse_key("ikey", *argv);
260 } else if (strcmp(*argv, "okey") == 0) {
261 NEXT_ARG();
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;
278 } else {
279 if (strcmp(*argv, "name") == 0) {
280 NEXT_ARG();
281 } else if (matches(*argv, "help") == 0)
282 usage();
283 if (p->name[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 = {};
289
290 if (tnl_get_ioctl(*argv, &old_p))
291 return -1;
292 *p = old_p;
293 }
294 }
295 count++;
296 argc--; argv++;
297 }
298 if (medium) {
299 p->link = ll_name_to_index(medium);
300 if (!p->link)
301 return nodev(medium);
302 else
303 strlcpy(p->name, medium, sizeof(p->name));
304 }
305 return 0;
306 }
307
308 static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default)
309 {
310 memset(p, 0, sizeof(*p));
311 p->proto = IPPROTO_IPV6;
312 if (apply_default) {
313 p->hop_limit = DEFAULT_TNL_HOP_LIMIT;
314 p->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
315 }
316 }
317
318 static void ip6_tnl_parm_initialize(const struct tnl_print_nlmsg_info *info)
319 {
320 const struct ifinfomsg *ifi = info->ifi;
321 const struct ip6_tnl_parm2 *p1 = info->p1;
322 struct ip6_tnl_parm2 *p2 = info->p2;
323
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);
329 }
330
331 static bool ip6_tnl_parm_match(const struct tnl_print_nlmsg_info *info)
332 {
333 const struct ip6_tnl_parm2 *p1 = info->p1;
334 const struct ip6_tnl_parm2 *p2 = info->p2;
335
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)));
350 }
351
352 static int do_show(int argc, char **argv)
353 {
354 struct ip6_tnl_parm2 p, p1;
355
356 ip6_tnl_parm_init(&p, 0);
357 p.proto = 0; /* default to any */
358
359 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
360 return -1;
361
362 if (!p.name[0] || show_stats) {
363 struct tnl_print_nlmsg_info info = {
364 .p1 = &p,
365 .p2 = &p1,
366 .init = ip6_tnl_parm_initialize,
367 .match = ip6_tnl_parm_match,
368 .print = print_tunnel,
369 };
370
371 return do_tunnels_list(&info);
372 }
373
374 if (tnl_get_ioctl(p.name, &p))
375 return -1;
376
377 print_tunnel(&p);
378 fputc('\n', stdout);
379 return 0;
380 }
381
382 static int do_add(int cmd, int argc, char **argv)
383 {
384 struct ip6_tnl_parm2 p;
385 const char *basedev = "ip6tnl0";
386
387 ip6_tnl_parm_init(&p, 1);
388
389 if (parse_args(argc, argv, cmd, &p) < 0)
390 return -1;
391
392 if (p.proto == IPPROTO_GRE)
393 basedev = "ip6gre0";
394 else if (p.i_flags & VTI_ISVTI)
395 basedev = "ip6_vti0";
396
397 return tnl_add_ioctl(cmd, basedev, p.name, &p);
398 }
399
400 static int do_del(int argc, char **argv)
401 {
402 struct ip6_tnl_parm2 p;
403 const char *basedev = "ip6tnl0";
404
405 ip6_tnl_parm_init(&p, 1);
406
407 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
408 return -1;
409
410 if (p.proto == IPPROTO_GRE)
411 basedev = "ip6gre0";
412 else if (p.i_flags & VTI_ISVTI)
413 basedev = "ip6_vti0";
414
415 return tnl_del_ioctl(basedev, p.name, &p);
416 }
417
418 int do_ip6tunnel(int argc, char **argv)
419 {
420 switch (preferred_family) {
421 case AF_UNSPEC:
422 preferred_family = AF_INET6;
423 break;
424 case AF_INET6:
425 break;
426 default:
427 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
428 exit(-1);
429 }
430
431 if (argc > 0) {
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)
443 usage();
444 } else
445 return do_show(0, NULL);
446
447 fprintf(stderr, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv);
448 exit(-1);
449 }