]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip6tunnel.c
bpf: avoid compiler warnings about strncpy
[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, "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");
67 exit(-1);
68 }
69
70 static void print_tunnel(struct ip6_tnl_parm2 *p)
71 {
72 char s1[1024];
73 char s2[1024];
74
75 /* Do not use format_host() for local addr,
76 * symbolic name will not be useful.
77 */
78 printf("%s: %s/ipv6 remote %s local %s",
79 p->name,
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)));
83 if (p->link) {
84 const char *n = ll_index_to_name(p->link);
85
86 if (n)
87 printf(" dev %s", n);
88 }
89
90 if (p->flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
91 printf(" encaplimit none");
92 else
93 printf(" encaplimit %u", p->encap_limit);
94
95 if (p->hop_limit)
96 printf(" hoplimit %u", p->hop_limit);
97 else
98 printf(" hoplimit inherit");
99
100 if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS)
101 printf(" tclass inherit");
102 else {
103 __u32 val = ntohl(p->flowinfo & IP6_FLOWINFO_TCLASS);
104
105 printf(" tclass 0x%02x", (__u8)(val >> 20));
106 }
107
108 if (p->flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
109 printf(" flowlabel inherit");
110 else
111 printf(" flowlabel 0x%05x", ntohl(p->flowinfo & IP6_FLOWINFO_FLOWLABEL));
112
113 printf(" (flowinfo 0x%08x)", ntohl(p->flowinfo));
114
115 if (p->flags & IP6_TNL_F_RCV_DSCP_COPY)
116 printf(" dscp inherit");
117
118 if (p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE)
119 printf(" allow-localremote");
120
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));
124 else {
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));
129 }
130
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_);
140 }
141 }
142
143 static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
144 {
145 int count = 0;
146 const char *medium = NULL;
147
148 while (argc > 0) {
149 if (strcmp(*argv, "mode") == 0) {
150 NEXT_ARG();
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)
167 p->proto = 0;
168 else {
169 fprintf(stderr, "Unknown tunnel mode \"%s\"\n", *argv);
170 exit(-1);
171 }
172 } else if (strcmp(*argv, "remote") == 0) {
173 inet_prefix raddr;
174
175 NEXT_ARG();
176 get_addr(&raddr, *argv, AF_INET6);
177 memcpy(&p->raddr, &raddr.data, sizeof(p->raddr));
178 } else if (strcmp(*argv, "local") == 0) {
179 inet_prefix laddr;
180
181 NEXT_ARG();
182 get_addr(&laddr, *argv, AF_INET6);
183 memcpy(&p->laddr, &laddr.data, sizeof(p->laddr));
184 } else if (strcmp(*argv, "dev") == 0) {
185 NEXT_ARG();
186 medium = *argv;
187 } else if (strcmp(*argv, "encaplimit") == 0) {
188 NEXT_ARG();
189 if (strcmp(*argv, "none") == 0) {
190 p->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
191 } else {
192 __u8 uval;
193
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;
198 }
199 } else if (strcmp(*argv, "hoplimit") == 0 ||
200 strcmp(*argv, "ttl") == 0 ||
201 strcmp(*argv, "hlim") == 0) {
202 __u8 uval;
203
204 NEXT_ARG();
205 if (get_u8(&uval, *argv, 0))
206 invarg("invalid TTL", *argv);
207 p->hop_limit = uval;
208 } else if (strcmp(*argv, "tclass") == 0 ||
209 strcmp(*argv, "tc") == 0 ||
210 strcmp(*argv, "tos") == 0 ||
211 matches(*argv, "dsfield") == 0) {
212 __u8 uval;
213
214 NEXT_ARG();
215 p->flowinfo &= ~IP6_FLOWINFO_TCLASS;
216 if (strcmp(*argv, "inherit") == 0)
217 p->flags |= IP6_TNL_F_USE_ORIG_TCLASS;
218 else {
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;
223 }
224 } else if (strcmp(*argv, "flowlabel") == 0 ||
225 strcmp(*argv, "fl") == 0) {
226 __u32 uval;
227
228 NEXT_ARG();
229 p->flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
230 if (strcmp(*argv, "inherit") == 0)
231 p->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
232 else {
233 if (get_u32(&uval, *argv, 16))
234 invarg("invalid Flowlabel", *argv);
235 if (uval > 0xFFFFF)
236 invarg("invalid Flowlabel", *argv);
237 p->flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
238 p->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
239 }
240 } else if (strcmp(*argv, "dscp") == 0) {
241 NEXT_ARG();
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) {
250 NEXT_ARG();
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) {
255 NEXT_ARG();
256 p->i_flags |= GRE_KEY;
257 p->i_key = tnl_parse_key("ikey", *argv);
258 } else if (strcmp(*argv, "okey") == 0) {
259 NEXT_ARG();
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;
276 } else {
277 if (strcmp(*argv, "name") == 0) {
278 NEXT_ARG();
279 } else if (matches(*argv, "help") == 0)
280 usage();
281 if (p->name[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 = {};
287
288 if (tnl_get_ioctl(*argv, &old_p))
289 return -1;
290 *p = old_p;
291 }
292 }
293 count++;
294 argc--; argv++;
295 }
296 if (medium) {
297 p->link = ll_name_to_index(medium);
298 if (p->link == 0) {
299 fprintf(stderr, "Cannot find device \"%s\"\n", medium);
300 return -1;
301 }
302 }
303 return 0;
304 }
305
306 static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default)
307 {
308 memset(p, 0, sizeof(*p));
309 p->proto = IPPROTO_IPV6;
310 if (apply_default) {
311 p->hop_limit = DEFAULT_TNL_HOP_LIMIT;
312 p->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
313 }
314 }
315
316 /*
317 * @p1: user specified parameter
318 * @p2: database entry
319 */
320 static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 *p1,
321 const struct ip6_tnl_parm2 *p2)
322 {
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)));
337 }
338
339 static int do_tunnels_list(struct ip6_tnl_parm2 *p)
340 {
341 char buf[512];
342 int err = -1;
343 FILE *fp = fopen("/proc/net/dev", "r");
344
345 if (fp == NULL) {
346 perror("fopen");
347 return -1;
348 }
349
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");
354 goto end;
355 }
356
357 while (fgets(buf, sizeof(buf), fp) != NULL) {
358 char name[IFNAMSIZ];
359 int index, type;
360 struct ip6_tnl_parm2 p1 = {};
361 char *ptr;
362
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");
367 goto end;
368 }
369 if (p->name[0] && strcmp(p->name, name))
370 continue;
371 index = ll_name_to_index(name);
372 if (index == 0)
373 continue;
374 type = ll_index_to_type(index);
375 if (type == -1) {
376 fprintf(stderr, "Failed to get type of \"%s\"\n", name);
377 continue;
378 }
379 if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE)
380 continue;
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);
386 if (p1.link == 0)
387 continue;
388 if (tnl_get_ioctl(p1.name, &p1))
389 continue;
390 if (!ip6_tnl_parm_match(p, &p1))
391 continue;
392 print_tunnel(&p1);
393 if (show_stats)
394 tnl_print_stats(ptr);
395 printf("\n");
396 }
397 err = 0;
398 end:
399 fclose(fp);
400 return err;
401 }
402
403 static int do_show(int argc, char **argv)
404 {
405 struct ip6_tnl_parm2 p;
406
407 ll_init_map(&rth);
408 ip6_tnl_parm_init(&p, 0);
409 p.proto = 0; /* default to any */
410
411 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
412 return -1;
413
414 if (!p.name[0] || show_stats)
415 do_tunnels_list(&p);
416 else {
417 if (tnl_get_ioctl(p.name, &p))
418 return -1;
419 print_tunnel(&p);
420 printf("\n");
421 }
422
423 return 0;
424 }
425
426 static int do_add(int cmd, int argc, char **argv)
427 {
428 struct ip6_tnl_parm2 p;
429 const char *basedev = "ip6tnl0";
430
431 ip6_tnl_parm_init(&p, 1);
432
433 if (parse_args(argc, argv, cmd, &p) < 0)
434 return -1;
435
436 if (p.proto == IPPROTO_GRE)
437 basedev = "ip6gre0";
438 else if (p.i_flags & VTI_ISVTI)
439 basedev = "ip6_vti0";
440
441 return tnl_add_ioctl(cmd, basedev, p.name, &p);
442 }
443
444 static int do_del(int argc, char **argv)
445 {
446 struct ip6_tnl_parm2 p;
447 const char *basedev = "ip6tnl0";
448
449 ip6_tnl_parm_init(&p, 1);
450
451 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
452 return -1;
453
454 if (p.proto == IPPROTO_GRE)
455 basedev = "ip6gre0";
456 else if (p.i_flags & VTI_ISVTI)
457 basedev = "ip6_vti0";
458
459 return tnl_del_ioctl(basedev, p.name, &p);
460 }
461
462 int do_ip6tunnel(int argc, char **argv)
463 {
464 switch (preferred_family) {
465 case AF_UNSPEC:
466 preferred_family = AF_INET6;
467 break;
468 case AF_INET6:
469 break;
470 default:
471 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
472 exit(-1);
473 }
474
475 if (argc > 0) {
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)
487 usage();
488 } else
489 return do_show(0, NULL);
490
491 fprintf(stderr, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv);
492 exit(-1);
493 }