]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptunnel.c
Merge branch 'master' into iproute2-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)
217 return nodev(medium);
218 }
219
220 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
221 p->i_key = p->iph.daddr;
222 p->i_flags |= GRE_KEY;
223 }
224 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
225 p->o_key = p->iph.daddr;
226 p->o_flags |= GRE_KEY;
227 }
228 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
229 fprintf(stderr, "A broadcast tunnel requires a source address\n");
230 return -1;
231 }
232 if (isatap)
233 p->i_flags |= SIT_ISATAP;
234
235 return 0;
236 }
237
238 static const char *tnl_defname(const struct ip_tunnel_parm *p)
239 {
240 switch (p->iph.protocol) {
241 case IPPROTO_IPIP:
242 if (p->i_flags & VTI_ISVTI)
243 return "ip_vti0";
244 else
245 return "tunl0";
246 case IPPROTO_GRE:
247 return "gre0";
248 case IPPROTO_IPV6:
249 return "sit0";
250 }
251 return NULL;
252 }
253
254 static int do_add(int cmd, int argc, char **argv)
255 {
256 struct ip_tunnel_parm p;
257 const char *basedev;
258
259 if (parse_args(argc, argv, cmd, &p) < 0)
260 return -1;
261
262 if (p.iph.ttl && p.iph.frag_off == 0) {
263 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
264 return -1;
265 }
266
267 basedev = tnl_defname(&p);
268 if (!basedev) {
269 fprintf(stderr,
270 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
271 return -1;
272 }
273
274 return tnl_add_ioctl(cmd, basedev, p.name, &p);
275 }
276
277 static int do_del(int argc, char **argv)
278 {
279 struct ip_tunnel_parm p;
280
281 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
282 return -1;
283
284 return tnl_del_ioctl(tnl_defname(&p) ? : p.name, p.name, &p);
285 }
286
287 static void print_tunnel(const void *t)
288 {
289 const struct ip_tunnel_parm *p = t;
290 struct ip_tunnel_6rd ip6rd = {};
291 char s1[1024];
292 char s2[1024];
293
294 /* Do not use format_host() for local addr,
295 * symbolic name will not be useful.
296 */
297 printf("%s: %s/ip remote %s local %s",
298 p->name,
299 tnl_strproto(p->iph.protocol),
300 p->iph.daddr ? format_host_r(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
301 p->iph.saddr ? rt_addr_n2a_r(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
302
303 if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
304 struct ip_tunnel_prl prl[16] = {};
305 int i;
306
307 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
308 prl[0].addr = htonl(INADDR_ANY);
309
310 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
311 for (i = 1; i < ARRAY_SIZE(prl); i++) {
312 if (prl[i].addr != htonl(INADDR_ANY)) {
313 printf(" %s %s ",
314 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
315 format_host(AF_INET, 4, &prl[i].addr));
316 }
317 }
318 }
319
320 if (p->link) {
321 const char *n = ll_index_to_name(p->link);
322
323 if (n)
324 printf(" dev %s", n);
325 }
326
327 if (p->iph.ttl)
328 printf(" ttl %u", p->iph.ttl);
329 else
330 printf(" ttl inherit");
331
332 if (p->iph.tos) {
333 SPRINT_BUF(b1);
334 printf(" tos");
335 if (p->iph.tos & 1)
336 printf(" inherit");
337 if (p->iph.tos & ~1)
338 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
339 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
340 }
341
342 if (!(p->iph.frag_off & htons(IP_DF)))
343 printf(" nopmtudisc");
344
345 if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
346 printf(" 6rd-prefix %s/%u",
347 inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
348 ip6rd.prefixlen);
349 if (ip6rd.relay_prefix) {
350 printf(" 6rd-relay_prefix %s/%u",
351 format_host(AF_INET, 4, &ip6rd.relay_prefix),
352 ip6rd.relay_prefixlen);
353 }
354 }
355
356 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
357 printf(" key %u", ntohl(p->i_key));
358 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
359 if (p->i_flags & GRE_KEY)
360 printf(" ikey %u", ntohl(p->i_key));
361 if (p->o_flags & GRE_KEY)
362 printf(" okey %u", ntohl(p->o_key));
363 }
364
365 if (p->i_flags & GRE_SEQ)
366 printf("%s Drop packets out of sequence.", _SL_);
367 if (p->i_flags & GRE_CSUM)
368 printf("%s Checksum in received packet is required.", _SL_);
369 if (p->o_flags & GRE_SEQ)
370 printf("%s Sequence packets on output.", _SL_);
371 if (p->o_flags & GRE_CSUM)
372 printf("%s Checksum output packets.", _SL_);
373 }
374
375
376 static void ip_tunnel_parm_initialize(const struct tnl_print_nlmsg_info *info)
377 {
378 struct ip_tunnel_parm *p2 = info->p2;
379
380 memset(p2, 0, sizeof(*p2));
381 }
382
383 static bool ip_tunnel_parm_match(const struct tnl_print_nlmsg_info *info)
384 {
385 const struct ip_tunnel_parm *p1 = info->p1;
386 const struct ip_tunnel_parm *p2 = info->p2;
387
388 return ((!p1->link || p1->link == p2->link) &&
389 (!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
390 (!p1->iph.daddr || p1->iph.daddr == p2->iph.daddr) &&
391 (!p1->iph.saddr || p1->iph.saddr == p2->iph.saddr) &&
392 (!p1->i_key || p1->i_key == p2->i_key));
393 }
394
395 static int do_show(int argc, char **argv)
396 {
397 struct ip_tunnel_parm p, p1;
398 const char *basedev;
399
400 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
401 return -1;
402
403 basedev = tnl_defname(&p);
404 if (!basedev) {
405 struct tnl_print_nlmsg_info info = {
406 .p1 = &p,
407 .p2 = &p1,
408 .init = ip_tunnel_parm_initialize,
409 .match = ip_tunnel_parm_match,
410 .print = print_tunnel,
411 };
412
413 return do_tunnels_list(&info);
414 }
415
416 if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
417 return -1;
418
419 print_tunnel(&p);
420 fputc('\n', stdout);
421 return 0;
422 }
423
424 static int do_prl(int argc, char **argv)
425 {
426 struct ip_tunnel_prl p = {};
427 int count = 0;
428 int cmd = 0;
429 const char *medium = NULL;
430
431 while (argc > 0) {
432 if (strcmp(*argv, "prl-default") == 0) {
433 NEXT_ARG();
434 cmd = SIOCADDPRL;
435 p.addr = get_addr32(*argv);
436 p.flags |= PRL_DEFAULT;
437 count++;
438 } else if (strcmp(*argv, "prl-nodefault") == 0) {
439 NEXT_ARG();
440 cmd = SIOCADDPRL;
441 p.addr = get_addr32(*argv);
442 count++;
443 } else if (strcmp(*argv, "prl-delete") == 0) {
444 NEXT_ARG();
445 cmd = SIOCDELPRL;
446 p.addr = get_addr32(*argv);
447 count++;
448 } else if (strcmp(*argv, "dev") == 0) {
449 NEXT_ARG();
450 if (check_ifname(*argv))
451 invarg("\"dev\" not a valid ifname", *argv);
452 medium = *argv;
453 } else {
454 fprintf(stderr,
455 "Invalid PRL parameter \"%s\"\n", *argv);
456 exit(-1);
457 }
458 if (count > 1) {
459 fprintf(stderr,
460 "One PRL entry at a time\n");
461 exit(-1);
462 }
463 argc--; argv++;
464 }
465 if (!medium) {
466 fprintf(stderr, "Must specify device\n");
467 exit(-1);
468 }
469
470 return tnl_prl_ioctl(cmd, medium, &p);
471 }
472
473 static int do_6rd(int argc, char **argv)
474 {
475 struct ip_tunnel_6rd ip6rd = {};
476 int cmd = 0;
477 const char *medium = NULL;
478 inet_prefix prefix;
479
480 while (argc > 0) {
481 if (strcmp(*argv, "6rd-prefix") == 0) {
482 NEXT_ARG();
483 if (get_prefix(&prefix, *argv, AF_INET6))
484 invarg("invalid 6rd_prefix\n", *argv);
485 cmd = SIOCADD6RD;
486 memcpy(&ip6rd.prefix, prefix.data, 16);
487 ip6rd.prefixlen = prefix.bitlen;
488 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
489 NEXT_ARG();
490 if (get_prefix(&prefix, *argv, AF_INET))
491 invarg("invalid 6rd-relay_prefix\n", *argv);
492 cmd = SIOCADD6RD;
493 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
494 ip6rd.relay_prefixlen = prefix.bitlen;
495 } else if (strcmp(*argv, "6rd-reset") == 0) {
496 cmd = SIOCDEL6RD;
497 } else if (strcmp(*argv, "dev") == 0) {
498 NEXT_ARG();
499 if (check_ifname(*argv))
500 invarg("\"dev\" not a valid ifname", *argv);
501 medium = *argv;
502 } else {
503 fprintf(stderr,
504 "Invalid 6RD parameter \"%s\"\n", *argv);
505 exit(-1);
506 }
507 argc--; argv++;
508 }
509 if (!medium) {
510 fprintf(stderr, "Must specify device\n");
511 exit(-1);
512 }
513
514 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
515 }
516
517 static int tunnel_mode_is_ipv6(char *tunnel_mode)
518 {
519 static const char * const ipv6_modes[] = {
520 "ipv6/ipv6", "ip6ip6",
521 "vti6",
522 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
523 "ip6gre", "gre/ipv6",
524 "any/ipv6", "any"
525 };
526 int i;
527
528 for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
529 if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
530 return 1;
531 }
532 return 0;
533 }
534
535 int do_iptunnel(int argc, char **argv)
536 {
537 int i;
538
539 for (i = 0; i < argc - 1; i++) {
540 if (strcmp(argv[i], "mode") == 0) {
541 if (tunnel_mode_is_ipv6(argv[i + 1]))
542 preferred_family = AF_INET6;
543 break;
544 }
545 }
546 switch (preferred_family) {
547 case AF_UNSPEC:
548 preferred_family = AF_INET;
549 break;
550 case AF_INET:
551 break;
552 /*
553 * This is silly enough but we have no easy way to make it
554 * protocol-independent because of unarranged structure between
555 * IPv4 and IPv6.
556 */
557 case AF_INET6:
558 return do_ip6tunnel(argc, argv);
559 default:
560 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
561 exit(-1);
562 }
563
564 if (argc > 0) {
565 if (matches(*argv, "add") == 0)
566 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
567 if (matches(*argv, "change") == 0)
568 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
569 if (matches(*argv, "delete") == 0)
570 return do_del(argc - 1, argv + 1);
571 if (matches(*argv, "show") == 0 ||
572 matches(*argv, "lst") == 0 ||
573 matches(*argv, "list") == 0)
574 return do_show(argc - 1, argv + 1);
575 if (matches(*argv, "prl") == 0)
576 return do_prl(argc - 1, argv + 1);
577 if (matches(*argv, "6rd") == 0)
578 return do_6rd(argc - 1, argv + 1);
579 if (matches(*argv, "help") == 0)
580 usage();
581 } else
582 return do_show(0, NULL);
583
584 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
585 exit(-1);
586 }