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