]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptunnel.c
iptunnel/ip6tunnel: Code cleanups
[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 %u", 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 /*
377 * @p1: user specified parameter
378 * @p2: database entry
379 */
380 static int ip_tunnel_parm_match(const struct ip_tunnel_parm *p1,
381 const struct ip_tunnel_parm *p2)
382 {
383 return ((!p1->link || p1->link == p2->link) &&
384 (!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
385 (!p1->iph.daddr || p1->iph.daddr == p2->iph.daddr) &&
386 (!p1->iph.saddr || p1->iph.saddr == p2->iph.saddr) &&
387 (!p1->i_key || p1->i_key == p2->i_key));
388 }
389
390 static int do_tunnels_list(struct ip_tunnel_parm *p)
391 {
392 char buf[512];
393 int err = -1;
394 FILE *fp = fopen("/proc/net/dev", "r");
395
396 if (fp == NULL) {
397 perror("fopen");
398 return -1;
399 }
400
401 /* skip two lines at the begenning of the file */
402 if (!fgets(buf, sizeof(buf), fp) ||
403 !fgets(buf, sizeof(buf), fp)) {
404 fprintf(stderr, "/proc/net/dev read error\n");
405 goto end;
406 }
407
408 while (fgets(buf, sizeof(buf), fp) != NULL) {
409 char name[IFNAMSIZ];
410 int index, type;
411 struct ip_tunnel_parm p1;
412 char *ptr;
413
414 buf[sizeof(buf) - 1] = '\0';
415 ptr = strchr(buf, ':');
416 if (ptr == NULL ||
417 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
418 fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
419 goto end;
420 }
421 if (p->name[0] && strcmp(p->name, name))
422 continue;
423 index = ll_name_to_index(name);
424 if (index == 0)
425 continue;
426 type = ll_index_to_type(index);
427 if (type == -1) {
428 fprintf(stderr, "Failed to get type of \"%s\"\n", name);
429 continue;
430 }
431 switch (type) {
432 case ARPHRD_TUNNEL:
433 case ARPHRD_IPGRE:
434 case ARPHRD_SIT:
435 break;
436 default:
437 continue;
438 }
439 memset(&p1, 0, sizeof(p1));
440 if (tnl_get_ioctl(name, &p1))
441 continue;
442 if (!ip_tunnel_parm_match(p, &p1))
443 continue;
444 print_tunnel(&p1);
445 if (show_stats) {
446 struct rtnl_link_stats64 s;
447
448 if (!tnl_get_stats(ptr, &s))
449 tnl_print_stats(&s);
450 }
451 fputc('\n', stdout);
452 }
453 err = 0;
454 end:
455 fclose(fp);
456 return err;
457 }
458
459 static int do_show(int argc, char **argv)
460 {
461 struct ip_tunnel_parm p;
462 const char *basedev;
463
464 ll_init_map(&rth);
465 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
466 return -1;
467
468 basedev = tnl_defname(&p);
469 if (!basedev)
470 return do_tunnels_list(&p);
471
472 if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
473 return -1;
474
475 print_tunnel(&p);
476 fputc('\n', stdout);
477 return 0;
478 }
479
480 static int do_prl(int argc, char **argv)
481 {
482 struct ip_tunnel_prl p = {};
483 int count = 0;
484 int cmd = 0;
485 const char *medium = NULL;
486
487 while (argc > 0) {
488 if (strcmp(*argv, "prl-default") == 0) {
489 NEXT_ARG();
490 cmd = SIOCADDPRL;
491 p.addr = get_addr32(*argv);
492 p.flags |= PRL_DEFAULT;
493 count++;
494 } else if (strcmp(*argv, "prl-nodefault") == 0) {
495 NEXT_ARG();
496 cmd = SIOCADDPRL;
497 p.addr = get_addr32(*argv);
498 count++;
499 } else if (strcmp(*argv, "prl-delete") == 0) {
500 NEXT_ARG();
501 cmd = SIOCDELPRL;
502 p.addr = get_addr32(*argv);
503 count++;
504 } else if (strcmp(*argv, "dev") == 0) {
505 NEXT_ARG();
506 if (check_ifname(*argv))
507 invarg("\"dev\" not a valid ifname", *argv);
508 medium = *argv;
509 } else {
510 fprintf(stderr,
511 "Invalid PRL parameter \"%s\"\n", *argv);
512 exit(-1);
513 }
514 if (count > 1) {
515 fprintf(stderr,
516 "One PRL entry at a time\n");
517 exit(-1);
518 }
519 argc--; argv++;
520 }
521 if (!medium) {
522 fprintf(stderr, "Must specify device\n");
523 exit(-1);
524 }
525
526 return tnl_prl_ioctl(cmd, medium, &p);
527 }
528
529 static int do_6rd(int argc, char **argv)
530 {
531 struct ip_tunnel_6rd ip6rd = {};
532 int cmd = 0;
533 const char *medium = NULL;
534 inet_prefix prefix;
535
536 while (argc > 0) {
537 if (strcmp(*argv, "6rd-prefix") == 0) {
538 NEXT_ARG();
539 if (get_prefix(&prefix, *argv, AF_INET6))
540 invarg("invalid 6rd_prefix\n", *argv);
541 cmd = SIOCADD6RD;
542 memcpy(&ip6rd.prefix, prefix.data, 16);
543 ip6rd.prefixlen = prefix.bitlen;
544 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
545 NEXT_ARG();
546 if (get_prefix(&prefix, *argv, AF_INET))
547 invarg("invalid 6rd-relay_prefix\n", *argv);
548 cmd = SIOCADD6RD;
549 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
550 ip6rd.relay_prefixlen = prefix.bitlen;
551 } else if (strcmp(*argv, "6rd-reset") == 0) {
552 cmd = SIOCDEL6RD;
553 } else if (strcmp(*argv, "dev") == 0) {
554 NEXT_ARG();
555 if (check_ifname(*argv))
556 invarg("\"dev\" not a valid ifname", *argv);
557 medium = *argv;
558 } else {
559 fprintf(stderr,
560 "Invalid 6RD parameter \"%s\"\n", *argv);
561 exit(-1);
562 }
563 argc--; argv++;
564 }
565 if (!medium) {
566 fprintf(stderr, "Must specify device\n");
567 exit(-1);
568 }
569
570 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
571 }
572
573 static int tunnel_mode_is_ipv6(char *tunnel_mode)
574 {
575 static const char * const ipv6_modes[] = {
576 "ipv6/ipv6", "ip6ip6",
577 "vti6",
578 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
579 "ip6gre", "gre/ipv6",
580 "any/ipv6", "any"
581 };
582 int i;
583
584 for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
585 if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
586 return 1;
587 }
588 return 0;
589 }
590
591 int do_iptunnel(int argc, char **argv)
592 {
593 int i;
594
595 for (i = 0; i < argc - 1; i++) {
596 if (strcmp(argv[i], "mode") == 0) {
597 if (tunnel_mode_is_ipv6(argv[i + 1]))
598 preferred_family = AF_INET6;
599 break;
600 }
601 }
602 switch (preferred_family) {
603 case AF_UNSPEC:
604 preferred_family = AF_INET;
605 break;
606 case AF_INET:
607 break;
608 /*
609 * This is silly enough but we have no easy way to make it
610 * protocol-independent because of unarranged structure between
611 * IPv4 and IPv6.
612 */
613 case AF_INET6:
614 return do_ip6tunnel(argc, argv);
615 default:
616 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
617 exit(-1);
618 }
619
620 if (argc > 0) {
621 if (matches(*argv, "add") == 0)
622 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
623 if (matches(*argv, "change") == 0)
624 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
625 if (matches(*argv, "delete") == 0)
626 return do_del(argc - 1, argv + 1);
627 if (matches(*argv, "show") == 0 ||
628 matches(*argv, "lst") == 0 ||
629 matches(*argv, "list") == 0)
630 return do_show(argc - 1, argv + 1);
631 if (matches(*argv, "prl") == 0)
632 return do_prl(argc - 1, argv + 1);
633 if (matches(*argv, "6rd") == 0)
634 return do_6rd(argc - 1, argv + 1);
635 if (matches(*argv, "help") == 0)
636 usage();
637 } else
638 return do_show(0, NULL);
639
640 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
641 exit(-1);
642 }