]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptunnel.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / iptunnel.c
1 /*
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
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
21 #include <net/if.h>
22 #include <net/if_arp.h>
23 #include <linux/ip.h>
24 #include <linux/if_tunnel.h>
25
26 #include "rt_names.h"
27 #include "utils.h"
28 #include "ip_common.h"
29 #include "tunnel.h"
30
31 static void usage(void) __attribute__((noreturn));
32
33 static void usage(void)
34 {
35 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n");
36 fprintf(stderr, " [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
38 fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
39 fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
40 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
41 fprintf(stderr, "\n");
42 fprintf(stderr, "Where: NAME := STRING\n");
43 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
44 fprintf(stderr, " TOS := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n");
45 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
46 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
47 exit(-1);
48 }
49
50 static void set_tunnel_proto(struct ip_tunnel_parm *p, int proto)
51 {
52 if (p->iph.protocol && p->iph.protocol != proto) {
53 fprintf(stderr,
54 "You managed to ask for more than one tunnel mode.\n");
55 exit(-1);
56 }
57 p->iph.protocol = proto;
58 }
59
60 static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
61 {
62 int count = 0;
63 char medium[IFNAMSIZ] = {};
64 int isatap = 0;
65
66 memset(p, 0, sizeof(*p));
67 p->iph.version = 4;
68 p->iph.ihl = 5;
69 #ifndef IP_DF
70 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
71 #endif
72 p->iph.frag_off = htons(IP_DF);
73
74 while (argc > 0) {
75 if (strcmp(*argv, "mode") == 0) {
76 NEXT_ARG();
77 if (strcmp(*argv, "ipip") == 0 ||
78 strcmp(*argv, "ip/ip") == 0) {
79 set_tunnel_proto(p, IPPROTO_IPIP);
80 } else if (strcmp(*argv, "gre") == 0 ||
81 strcmp(*argv, "gre/ip") == 0) {
82 set_tunnel_proto(p, IPPROTO_GRE);
83 } else if (strcmp(*argv, "sit") == 0 ||
84 strcmp(*argv, "ipv6/ip") == 0) {
85 set_tunnel_proto(p, IPPROTO_IPV6);
86 } else if (strcmp(*argv, "isatap") == 0) {
87 set_tunnel_proto(p, IPPROTO_IPV6);
88 isatap++;
89 } else if (strcmp(*argv, "vti") == 0) {
90 set_tunnel_proto(p, IPPROTO_IPIP);
91 p->i_flags |= VTI_ISVTI;
92 } else {
93 fprintf(stderr,
94 "Unknown tunnel mode \"%s\"\n", *argv);
95 exit(-1);
96 }
97 } else if (strcmp(*argv, "key") == 0) {
98 NEXT_ARG();
99 p->i_flags |= GRE_KEY;
100 p->o_flags |= GRE_KEY;
101 p->i_key = p->o_key = tnl_parse_key("key", *argv);
102 } else if (strcmp(*argv, "ikey") == 0) {
103 NEXT_ARG();
104 p->i_flags |= GRE_KEY;
105 p->i_key = tnl_parse_key("ikey", *argv);
106 } else if (strcmp(*argv, "okey") == 0) {
107 NEXT_ARG();
108 p->o_flags |= GRE_KEY;
109 p->o_key = tnl_parse_key("okey", *argv);
110 } else if (strcmp(*argv, "seq") == 0) {
111 p->i_flags |= GRE_SEQ;
112 p->o_flags |= GRE_SEQ;
113 } else if (strcmp(*argv, "iseq") == 0) {
114 p->i_flags |= GRE_SEQ;
115 } else if (strcmp(*argv, "oseq") == 0) {
116 p->o_flags |= GRE_SEQ;
117 } else if (strcmp(*argv, "csum") == 0) {
118 p->i_flags |= GRE_CSUM;
119 p->o_flags |= GRE_CSUM;
120 } else if (strcmp(*argv, "icsum") == 0) {
121 p->i_flags |= GRE_CSUM;
122 } else if (strcmp(*argv, "ocsum") == 0) {
123 p->o_flags |= GRE_CSUM;
124 } else if (strcmp(*argv, "nopmtudisc") == 0) {
125 p->iph.frag_off = 0;
126 } else if (strcmp(*argv, "pmtudisc") == 0) {
127 p->iph.frag_off = htons(IP_DF);
128 } else if (strcmp(*argv, "remote") == 0) {
129 NEXT_ARG();
130 if (strcmp(*argv, "any"))
131 p->iph.daddr = get_addr32(*argv);
132 else
133 p->iph.daddr = htonl(INADDR_ANY);
134 } else if (strcmp(*argv, "local") == 0) {
135 NEXT_ARG();
136 if (strcmp(*argv, "any"))
137 p->iph.saddr = get_addr32(*argv);
138 else
139 p->iph.saddr = htonl(INADDR_ANY);
140 } else if (strcmp(*argv, "dev") == 0) {
141 NEXT_ARG();
142 strncpy(medium, *argv, IFNAMSIZ - 1);
143 } else if (strcmp(*argv, "ttl") == 0 ||
144 strcmp(*argv, "hoplimit") == 0 ||
145 strcmp(*argv, "hlim") == 0) {
146 __u8 uval;
147
148 NEXT_ARG();
149 if (strcmp(*argv, "inherit") != 0) {
150 if (get_u8(&uval, *argv, 0))
151 invarg("invalid TTL\n", *argv);
152 p->iph.ttl = uval;
153 }
154 } else if (strcmp(*argv, "tos") == 0 ||
155 strcmp(*argv, "tclass") == 0 ||
156 matches(*argv, "dsfield") == 0) {
157 char *dsfield;
158 __u32 uval;
159
160 NEXT_ARG();
161 dsfield = *argv;
162 strsep(&dsfield, "/");
163 if (strcmp(*argv, "inherit") != 0) {
164 dsfield = *argv;
165 p->iph.tos = 0;
166 } else
167 p->iph.tos = 1;
168 if (dsfield) {
169 if (rtnl_dsfield_a2n(&uval, dsfield))
170 invarg("bad TOS value", *argv);
171 p->iph.tos |= uval;
172 }
173 } else {
174 if (strcmp(*argv, "name") == 0)
175 NEXT_ARG();
176 else if (matches(*argv, "help") == 0)
177 usage();
178
179 if (p->name[0])
180 duparg2("name", *argv);
181 strncpy(p->name, *argv, IFNAMSIZ - 1);
182 if (cmd == SIOCCHGTUNNEL && count == 0) {
183 struct ip_tunnel_parm old_p = {};
184
185 if (tnl_get_ioctl(*argv, &old_p))
186 return -1;
187 *p = old_p;
188 }
189 }
190 count++;
191 argc--; argv++;
192 }
193
194
195 if (p->iph.protocol == 0) {
196 if (memcmp(p->name, "gre", 3) == 0)
197 p->iph.protocol = IPPROTO_GRE;
198 else if (memcmp(p->name, "ipip", 4) == 0)
199 p->iph.protocol = IPPROTO_IPIP;
200 else if (memcmp(p->name, "sit", 3) == 0)
201 p->iph.protocol = IPPROTO_IPV6;
202 else if (memcmp(p->name, "isatap", 6) == 0) {
203 p->iph.protocol = IPPROTO_IPV6;
204 isatap++;
205 } else if (memcmp(p->name, "vti", 3) == 0) {
206 p->iph.protocol = IPPROTO_IPIP;
207 p->i_flags |= VTI_ISVTI;
208 }
209 }
210
211 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
212 if (!(p->i_flags & VTI_ISVTI) &&
213 (p->iph.protocol != IPPROTO_GRE)) {
214 fprintf(stderr, "Keys are not allowed with ipip and sit tunnels\n");
215 return -1;
216 }
217 }
218
219 if (medium[0]) {
220 p->link = ll_name_to_index(medium);
221 if (p->link == 0) {
222 fprintf(stderr, "Cannot find device \"%s\"\n", medium);
223 return -1;
224 }
225 }
226
227 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
228 p->i_key = p->iph.daddr;
229 p->i_flags |= GRE_KEY;
230 }
231 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
232 p->o_key = p->iph.daddr;
233 p->o_flags |= GRE_KEY;
234 }
235 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
236 fprintf(stderr, "A broadcast tunnel requires a source address\n");
237 return -1;
238 }
239 if (isatap)
240 p->i_flags |= SIT_ISATAP;
241
242 return 0;
243 }
244
245 static const char *tnl_defname(const struct ip_tunnel_parm *p)
246 {
247 switch (p->iph.protocol) {
248 case IPPROTO_IPIP:
249 if (p->i_flags & VTI_ISVTI)
250 return "ip_vti0";
251 else
252 return "tunl0";
253 case IPPROTO_GRE:
254 return "gre0";
255 case IPPROTO_IPV6:
256 return "sit0";
257 }
258 return NULL;
259 }
260
261 static int do_add(int cmd, int argc, char **argv)
262 {
263 struct ip_tunnel_parm p;
264 const char *basedev;
265
266 if (parse_args(argc, argv, cmd, &p) < 0)
267 return -1;
268
269 if (p.iph.ttl && p.iph.frag_off == 0) {
270 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
271 return -1;
272 }
273
274 basedev = tnl_defname(&p);
275 if (!basedev) {
276 fprintf(stderr,
277 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
278 return -1;
279 }
280
281 return tnl_add_ioctl(cmd, basedev, p.name, &p);
282 }
283
284 static int do_del(int argc, char **argv)
285 {
286 struct ip_tunnel_parm p;
287
288 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
289 return -1;
290
291 return tnl_del_ioctl(tnl_defname(&p) ? : p.name, p.name, &p);
292 }
293
294 static void print_tunnel(struct ip_tunnel_parm *p)
295 {
296 struct ip_tunnel_6rd ip6rd = {};
297 char s1[1024];
298 char s2[1024];
299
300 /* Do not use format_host() for local addr,
301 * symbolic name will not be useful.
302 */
303 printf("%s: %s/ip remote %s local %s",
304 p->name,
305 tnl_strproto(p->iph.protocol),
306 p->iph.daddr ? format_host_r(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
307 p->iph.saddr ? rt_addr_n2a_r(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
308
309 if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
310 struct ip_tunnel_prl prl[16] = {};
311 int i;
312
313 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
314 prl[0].addr = htonl(INADDR_ANY);
315
316 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
317 for (i = 1; i < ARRAY_SIZE(prl); i++) {
318 if (prl[i].addr != htonl(INADDR_ANY)) {
319 printf(" %s %s ",
320 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
321 format_host(AF_INET, 4, &prl[i].addr));
322 }
323 }
324 }
325
326 if (p->link) {
327 const char *n = ll_index_to_name(p->link);
328
329 if (n)
330 printf(" dev %s", n);
331 }
332
333 if (p->iph.ttl)
334 printf(" ttl %d", p->iph.ttl);
335 else
336 printf(" ttl inherit");
337
338 if (p->iph.tos) {
339 SPRINT_BUF(b1);
340 printf(" tos");
341 if (p->iph.tos & 1)
342 printf(" inherit");
343 if (p->iph.tos & ~1)
344 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
345 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
346 }
347
348 if (!(p->iph.frag_off & htons(IP_DF)))
349 printf(" nopmtudisc");
350
351 if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
352 printf(" 6rd-prefix %s/%u",
353 inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
354 ip6rd.prefixlen);
355 if (ip6rd.relay_prefix) {
356 printf(" 6rd-relay_prefix %s/%u",
357 format_host(AF_INET, 4, &ip6rd.relay_prefix),
358 ip6rd.relay_prefixlen);
359 }
360 }
361
362 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
363 printf(" key %u", ntohl(p->i_key));
364 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
365 if (p->i_flags & GRE_KEY)
366 printf(" ikey %u", ntohl(p->i_key));
367 if (p->o_flags & GRE_KEY)
368 printf(" okey %u", ntohl(p->o_key));
369 }
370
371 if (p->i_flags & GRE_SEQ)
372 printf("%s Drop packets out of sequence.", _SL_);
373 if (p->i_flags & GRE_CSUM)
374 printf("%s Checksum in received packet is required.", _SL_);
375 if (p->o_flags & GRE_SEQ)
376 printf("%s Sequence packets on output.", _SL_);
377 if (p->o_flags & GRE_CSUM)
378 printf("%s Checksum output packets.", _SL_);
379 }
380
381 static int do_tunnels_list(struct ip_tunnel_parm *p)
382 {
383 char buf[512];
384 int err = -1;
385 FILE *fp = fopen("/proc/net/dev", "r");
386
387 if (fp == NULL) {
388 perror("fopen");
389 return -1;
390 }
391
392 /* skip header lines */
393 if (!fgets(buf, sizeof(buf), fp) ||
394 !fgets(buf, sizeof(buf), fp)) {
395 fprintf(stderr, "/proc/net/dev read error\n");
396 goto end;
397 }
398
399 while (fgets(buf, sizeof(buf), fp) != NULL) {
400 char name[IFNAMSIZ];
401 int index, type;
402 struct ip_tunnel_parm p1 = {};
403 char *ptr;
404
405 buf[sizeof(buf) - 1] = 0;
406 ptr = strchr(buf, ':');
407 if (ptr == NULL ||
408 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
409 fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
410 goto end;
411 }
412 if (p->name[0] && strcmp(p->name, name))
413 continue;
414 index = ll_name_to_index(name);
415 if (index == 0)
416 continue;
417 type = ll_index_to_type(index);
418 if (type == -1) {
419 fprintf(stderr, "Failed to get type of \"%s\"\n", name);
420 continue;
421 }
422 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
423 continue;
424 if (tnl_get_ioctl(name, &p1))
425 continue;
426 if ((p->link && p1.link != p->link) ||
427 (p->name[0] && strcmp(p1.name, p->name)) ||
428 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
429 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
430 (p->i_key && p1.i_key != p->i_key))
431 continue;
432 print_tunnel(&p1);
433 if (show_stats)
434 tnl_print_stats(ptr);
435 printf("\n");
436 }
437 err = 0;
438 end:
439 fclose(fp);
440 return err;
441 }
442
443 static int do_show(int argc, char **argv)
444 {
445 struct ip_tunnel_parm p;
446 const char *basedev;
447
448 ll_init_map(&rth);
449 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
450 return -1;
451
452 basedev = tnl_defname(&p);
453 if (!basedev)
454 return do_tunnels_list(&p);
455
456 if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
457 return -1;
458
459 print_tunnel(&p);
460 printf("\n");
461 return 0;
462 }
463
464 static int do_prl(int argc, char **argv)
465 {
466 struct ip_tunnel_prl p = {};
467 int count = 0;
468 int devname = 0;
469 int cmd = 0;
470 char medium[IFNAMSIZ] = {};
471
472 while (argc > 0) {
473 if (strcmp(*argv, "prl-default") == 0) {
474 NEXT_ARG();
475 cmd = SIOCADDPRL;
476 p.addr = get_addr32(*argv);
477 p.flags |= PRL_DEFAULT;
478 count++;
479 } else if (strcmp(*argv, "prl-nodefault") == 0) {
480 NEXT_ARG();
481 cmd = SIOCADDPRL;
482 p.addr = get_addr32(*argv);
483 count++;
484 } else if (strcmp(*argv, "prl-delete") == 0) {
485 NEXT_ARG();
486 cmd = SIOCDELPRL;
487 p.addr = get_addr32(*argv);
488 count++;
489 } else if (strcmp(*argv, "dev") == 0) {
490 NEXT_ARG();
491 strncpy(medium, *argv, IFNAMSIZ-1);
492 devname++;
493 } else {
494 fprintf(stderr,
495 "Invalid PRL parameter \"%s\"\n", *argv);
496 exit(-1);
497 }
498 if (count > 1) {
499 fprintf(stderr,
500 "One PRL entry at a time\n");
501 exit(-1);
502 }
503 argc--; argv++;
504 }
505 if (devname == 0) {
506 fprintf(stderr, "Must specify device\n");
507 exit(-1);
508 }
509
510 return tnl_prl_ioctl(cmd, medium, &p);
511 }
512
513 static int do_6rd(int argc, char **argv)
514 {
515 struct ip_tunnel_6rd ip6rd = {};
516 int devname = 0;
517 int cmd = 0;
518 char medium[IFNAMSIZ] = {};
519 inet_prefix prefix;
520
521 while (argc > 0) {
522 if (strcmp(*argv, "6rd-prefix") == 0) {
523 NEXT_ARG();
524 if (get_prefix(&prefix, *argv, AF_INET6))
525 invarg("invalid 6rd_prefix\n", *argv);
526 cmd = SIOCADD6RD;
527 memcpy(&ip6rd.prefix, prefix.data, 16);
528 ip6rd.prefixlen = prefix.bitlen;
529 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
530 NEXT_ARG();
531 if (get_prefix(&prefix, *argv, AF_INET))
532 invarg("invalid 6rd-relay_prefix\n", *argv);
533 cmd = SIOCADD6RD;
534 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
535 ip6rd.relay_prefixlen = prefix.bitlen;
536 } else if (strcmp(*argv, "6rd-reset") == 0) {
537 cmd = SIOCDEL6RD;
538 } else if (strcmp(*argv, "dev") == 0) {
539 NEXT_ARG();
540 strncpy(medium, *argv, IFNAMSIZ-1);
541 devname++;
542 } else {
543 fprintf(stderr,
544 "Invalid 6RD parameter \"%s\"\n", *argv);
545 exit(-1);
546 }
547 argc--; argv++;
548 }
549 if (devname == 0) {
550 fprintf(stderr, "Must specify device\n");
551 exit(-1);
552 }
553
554 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
555 }
556
557 static int tunnel_mode_is_ipv6(char *tunnel_mode)
558 {
559 static const char * const ipv6_modes[] = {
560 "ipv6/ipv6", "ip6ip6",
561 "vti6",
562 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
563 "ip6gre", "gre/ipv6",
564 "any/ipv6", "any"
565 };
566 int i;
567
568 for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
569 if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
570 return 1;
571 }
572 return 0;
573 }
574
575 int do_iptunnel(int argc, char **argv)
576 {
577 int i;
578
579 for (i = 0; i < argc - 1; i++) {
580 if (strcmp(argv[i], "mode") == 0) {
581 if (tunnel_mode_is_ipv6(argv[i + 1]))
582 preferred_family = AF_INET6;
583 break;
584 }
585 }
586 switch (preferred_family) {
587 case AF_UNSPEC:
588 preferred_family = AF_INET;
589 break;
590 case AF_INET:
591 break;
592 /*
593 * This is silly enough but we have no easy way to make it
594 * protocol-independent because of unarranged structure between
595 * IPv4 and IPv6.
596 */
597 case AF_INET6:
598 return do_ip6tunnel(argc, argv);
599 default:
600 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
601 exit(-1);
602 }
603
604 if (argc > 0) {
605 if (matches(*argv, "add") == 0)
606 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
607 if (matches(*argv, "change") == 0)
608 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
609 if (matches(*argv, "delete") == 0)
610 return do_del(argc - 1, argv + 1);
611 if (matches(*argv, "show") == 0 ||
612 matches(*argv, "lst") == 0 ||
613 matches(*argv, "list") == 0)
614 return do_show(argc - 1, argv + 1);
615 if (matches(*argv, "prl") == 0)
616 return do_prl(argc - 1, argv + 1);
617 if (matches(*argv, "6rd") == 0)
618 return do_6rd(argc - 1, argv + 1);
619 if (matches(*argv, "help") == 0)
620 usage();
621 } else
622 return do_show(0, NULL);
623
624 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
625 exit(-1);
626 }