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