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