]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptunnel.c
rdma: Add rdma statistic counter per-port auto mode support
[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 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 static void ip_tunnel_parm_initialize(const struct tnl_print_nlmsg_info *info)
378 {
379 struct ip_tunnel_parm *p2 = info->p2;
380
381 memset(p2, 0, sizeof(*p2));
382 }
383
384 static bool ip_tunnel_parm_match(const struct tnl_print_nlmsg_info *info)
385 {
386 const struct ip_tunnel_parm *p1 = info->p1;
387 const struct ip_tunnel_parm *p2 = info->p2;
388
389 return ((!p1->link || p1->link == p2->link) &&
390 (!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
391 (!p1->iph.daddr || p1->iph.daddr == p2->iph.daddr) &&
392 (!p1->iph.saddr || p1->iph.saddr == p2->iph.saddr) &&
393 (!p1->i_key || p1->i_key == p2->i_key));
394 }
395
396 static int do_show(int argc, char **argv)
397 {
398 struct ip_tunnel_parm p, p1;
399 const char *basedev;
400
401 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
402 return -1;
403
404 basedev = tnl_defname(&p);
405 if (!basedev) {
406 struct tnl_print_nlmsg_info info = {
407 .p1 = &p,
408 .p2 = &p1,
409 .init = ip_tunnel_parm_initialize,
410 .match = ip_tunnel_parm_match,
411 .print = print_tunnel,
412 };
413
414 return do_tunnels_list(&info);
415 }
416
417 if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
418 return -1;
419
420 print_tunnel(&p);
421 fputc('\n', stdout);
422 return 0;
423 }
424
425 static int do_prl(int argc, char **argv)
426 {
427 struct ip_tunnel_prl p = {};
428 int count = 0;
429 int cmd = 0;
430 const char *medium = NULL;
431
432 while (argc > 0) {
433 if (strcmp(*argv, "prl-default") == 0) {
434 NEXT_ARG();
435 cmd = SIOCADDPRL;
436 p.addr = get_addr32(*argv);
437 p.flags |= PRL_DEFAULT;
438 count++;
439 } else if (strcmp(*argv, "prl-nodefault") == 0) {
440 NEXT_ARG();
441 cmd = SIOCADDPRL;
442 p.addr = get_addr32(*argv);
443 count++;
444 } else if (strcmp(*argv, "prl-delete") == 0) {
445 NEXT_ARG();
446 cmd = SIOCDELPRL;
447 p.addr = get_addr32(*argv);
448 count++;
449 } else if (strcmp(*argv, "dev") == 0) {
450 NEXT_ARG();
451 if (check_ifname(*argv))
452 invarg("\"dev\" not a valid ifname", *argv);
453 medium = *argv;
454 } else {
455 fprintf(stderr,
456 "Invalid PRL parameter \"%s\"\n", *argv);
457 exit(-1);
458 }
459 if (count > 1) {
460 fprintf(stderr,
461 "One PRL entry at a time\n");
462 exit(-1);
463 }
464 argc--; argv++;
465 }
466 if (!medium) {
467 fprintf(stderr, "Must specify device\n");
468 exit(-1);
469 }
470
471 return tnl_prl_ioctl(cmd, medium, &p);
472 }
473
474 static int do_6rd(int argc, char **argv)
475 {
476 struct ip_tunnel_6rd ip6rd = {};
477 int cmd = 0;
478 const char *medium = NULL;
479 inet_prefix prefix;
480
481 while (argc > 0) {
482 if (strcmp(*argv, "6rd-prefix") == 0) {
483 NEXT_ARG();
484 if (get_prefix(&prefix, *argv, AF_INET6))
485 invarg("invalid 6rd_prefix\n", *argv);
486 cmd = SIOCADD6RD;
487 memcpy(&ip6rd.prefix, prefix.data, 16);
488 ip6rd.prefixlen = prefix.bitlen;
489 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
490 NEXT_ARG();
491 if (get_prefix(&prefix, *argv, AF_INET))
492 invarg("invalid 6rd-relay_prefix\n", *argv);
493 cmd = SIOCADD6RD;
494 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
495 ip6rd.relay_prefixlen = prefix.bitlen;
496 } else if (strcmp(*argv, "6rd-reset") == 0) {
497 cmd = SIOCDEL6RD;
498 } else if (strcmp(*argv, "dev") == 0) {
499 NEXT_ARG();
500 if (check_ifname(*argv))
501 invarg("\"dev\" not a valid ifname", *argv);
502 medium = *argv;
503 } else {
504 fprintf(stderr,
505 "Invalid 6RD parameter \"%s\"\n", *argv);
506 exit(-1);
507 }
508 argc--; argv++;
509 }
510 if (!medium) {
511 fprintf(stderr, "Must specify device\n");
512 exit(-1);
513 }
514
515 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
516 }
517
518 static int tunnel_mode_is_ipv6(char *tunnel_mode)
519 {
520 static const char * const ipv6_modes[] = {
521 "ipv6/ipv6", "ip6ip6",
522 "vti6",
523 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
524 "ip6gre", "gre/ipv6",
525 "any/ipv6", "any"
526 };
527 int i;
528
529 for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
530 if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
531 return 1;
532 }
533 return 0;
534 }
535
536 int do_iptunnel(int argc, char **argv)
537 {
538 int i;
539
540 for (i = 0; i < argc - 1; i++) {
541 if (strcmp(argv[i], "mode") == 0) {
542 if (tunnel_mode_is_ipv6(argv[i + 1]))
543 preferred_family = AF_INET6;
544 break;
545 }
546 }
547 switch (preferred_family) {
548 case AF_UNSPEC:
549 preferred_family = AF_INET;
550 break;
551 case AF_INET:
552 break;
553 /*
554 * This is silly enough but we have no easy way to make it
555 * protocol-independent because of unarranged structure between
556 * IPv4 and IPv6.
557 */
558 case AF_INET6:
559 return do_ip6tunnel(argc, argv);
560 default:
561 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
562 exit(-1);
563 }
564
565 if (argc > 0) {
566 if (matches(*argv, "add") == 0)
567 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
568 if (matches(*argv, "change") == 0)
569 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
570 if (matches(*argv, "delete") == 0)
571 return do_del(argc - 1, argv + 1);
572 if (matches(*argv, "show") == 0 ||
573 matches(*argv, "lst") == 0 ||
574 matches(*argv, "list") == 0)
575 return do_show(argc - 1, argv + 1);
576 if (matches(*argv, "prl") == 0)
577 return do_prl(argc - 1, argv + 1);
578 if (matches(*argv, "6rd") == 0)
579 return do_6rd(argc - 1, argv + 1);
580 if (matches(*argv, "help") == 0)
581 usage();
582 } else
583 return do_show(0, NULL);
584
585 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
586 exit(-1);
587 }