]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_gre.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / link_gre.c
1 /*
2 * link_gre.c gre driver module
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: Herbert Xu <herbert@gondor.apana.org.au>
10 *
11 */
12
13 #include <string.h>
14 #include <net/if.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
18
19 #include <linux/ip.h>
20 #include <linux/if_tunnel.h>
21 #include "rt_names.h"
22 #include "utils.h"
23 #include "ip_common.h"
24 #include "tunnel.h"
25
26 static void print_usage(FILE *f)
27 {
28 fprintf(f,
29 "Usage: ... { gre | gretap } [ remote ADDR ]\n"
30 " [ local ADDR ]\n"
31 " [ [i|o]seq ]\n"
32 " [ [i|o]key KEY ]\n"
33 " [ [i|o]csum ]\n"
34 " [ ttl TTL ]\n"
35 " [ tos TOS ]\n"
36 " [ [no]pmtudisc ]\n"
37 " [ [no]ignore-df ]\n"
38 " [ dev PHYS_DEV ]\n"
39 " [ noencap ]\n"
40 " [ encap { fou | gue | none } ]\n"
41 " [ encap-sport PORT ]\n"
42 " [ encap-dport PORT ]\n"
43 " [ [no]encap-csum ]\n"
44 " [ [no]encap-csum6 ]\n"
45 " [ [no]encap-remcsum ]\n"
46 " [ fwmark MARK ]\n"
47 "\n"
48 "Where: ADDR := { IP_ADDRESS | any }\n"
49 " TOS := { NUMBER | inherit }\n"
50 " TTL := { 1..255 | inherit }\n"
51 " KEY := { DOTTED_QUAD | NUMBER }\n"
52 " MARK := { 0x0..0xffffffff }\n"
53 );
54 }
55
56 static void usage(void) __attribute__((noreturn));
57 static void usage(void)
58 {
59 print_usage(stderr);
60 exit(-1);
61 }
62
63 static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
64 struct nlmsghdr *n)
65 {
66 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
67 struct {
68 struct nlmsghdr n;
69 struct ifinfomsg i;
70 char buf[16384];
71 } req = {
72 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
73 .n.nlmsg_flags = NLM_F_REQUEST,
74 .n.nlmsg_type = RTM_GETLINK,
75 .i.ifi_family = preferred_family,
76 .i.ifi_index = ifi->ifi_index,
77 };
78 struct rtattr *tb[IFLA_MAX + 1];
79 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
80 struct rtattr *greinfo[IFLA_GRE_MAX + 1];
81 __u16 iflags = 0;
82 __u16 oflags = 0;
83 unsigned int ikey = 0;
84 unsigned int okey = 0;
85 unsigned int saddr = 0;
86 unsigned int daddr = 0;
87 unsigned int link = 0;
88 __u8 pmtudisc = 1;
89 __u8 ttl = 0;
90 __u8 tos = 0;
91 int len;
92 __u16 encaptype = 0;
93 __u16 encapflags = 0;
94 __u16 encapsport = 0;
95 __u16 encapdport = 0;
96 __u8 metadata = 0;
97 __u8 ignore_df = 0;
98 __u32 fwmark = 0;
99
100 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
101 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
102 get_failed:
103 fprintf(stderr,
104 "Failed to get existing tunnel info.\n");
105 return -1;
106 }
107
108 len = req.n.nlmsg_len;
109 len -= NLMSG_LENGTH(sizeof(*ifi));
110 if (len < 0)
111 goto get_failed;
112
113 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
114
115 if (!tb[IFLA_LINKINFO])
116 goto get_failed;
117
118 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
119
120 if (!linkinfo[IFLA_INFO_DATA])
121 goto get_failed;
122
123 parse_rtattr_nested(greinfo, IFLA_GRE_MAX,
124 linkinfo[IFLA_INFO_DATA]);
125
126 if (greinfo[IFLA_GRE_IKEY])
127 ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
128
129 if (greinfo[IFLA_GRE_OKEY])
130 okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
131
132 if (greinfo[IFLA_GRE_IFLAGS])
133 iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
134
135 if (greinfo[IFLA_GRE_OFLAGS])
136 oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
137
138 if (greinfo[IFLA_GRE_LOCAL])
139 saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]);
140
141 if (greinfo[IFLA_GRE_REMOTE])
142 daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]);
143
144 if (greinfo[IFLA_GRE_PMTUDISC])
145 pmtudisc = rta_getattr_u8(
146 greinfo[IFLA_GRE_PMTUDISC]);
147
148 if (greinfo[IFLA_GRE_TTL])
149 ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
150
151 if (greinfo[IFLA_GRE_TOS])
152 tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]);
153
154 if (greinfo[IFLA_GRE_LINK])
155 link = rta_getattr_u8(greinfo[IFLA_GRE_LINK]);
156
157 if (greinfo[IFLA_GRE_ENCAP_TYPE])
158 encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]);
159 if (greinfo[IFLA_GRE_ENCAP_FLAGS])
160 encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]);
161 if (greinfo[IFLA_GRE_ENCAP_SPORT])
162 encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]);
163 if (greinfo[IFLA_GRE_ENCAP_DPORT])
164 encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]);
165
166 if (greinfo[IFLA_GRE_COLLECT_METADATA])
167 metadata = 1;
168
169 if (greinfo[IFLA_GRE_IGNORE_DF])
170 ignore_df =
171 !!rta_getattr_u8(greinfo[IFLA_GRE_IGNORE_DF]);
172
173 if (greinfo[IFLA_GRE_FWMARK])
174 fwmark = rta_getattr_u32(greinfo[IFLA_GRE_FWMARK]);
175 }
176
177 while (argc > 0) {
178 if (!matches(*argv, "key")) {
179 unsigned int uval;
180
181 NEXT_ARG();
182 iflags |= GRE_KEY;
183 oflags |= GRE_KEY;
184 if (strchr(*argv, '.'))
185 uval = get_addr32(*argv);
186 else {
187 if (get_unsigned(&uval, *argv, 0) < 0) {
188 fprintf(stderr,
189 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
190 exit(-1);
191 }
192 uval = htonl(uval);
193 }
194
195 ikey = okey = uval;
196 } else if (!matches(*argv, "ikey")) {
197 unsigned int uval;
198
199 NEXT_ARG();
200 iflags |= GRE_KEY;
201 if (strchr(*argv, '.'))
202 uval = get_addr32(*argv);
203 else {
204 if (get_unsigned(&uval, *argv, 0) < 0) {
205 fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
206 exit(-1);
207 }
208 uval = htonl(uval);
209 }
210 ikey = uval;
211 } else if (!matches(*argv, "okey")) {
212 unsigned int uval;
213
214 NEXT_ARG();
215 oflags |= GRE_KEY;
216 if (strchr(*argv, '.'))
217 uval = get_addr32(*argv);
218 else {
219 if (get_unsigned(&uval, *argv, 0) < 0) {
220 fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
221 exit(-1);
222 }
223 uval = htonl(uval);
224 }
225 okey = uval;
226 } else if (!matches(*argv, "seq")) {
227 iflags |= GRE_SEQ;
228 oflags |= GRE_SEQ;
229 } else if (!matches(*argv, "iseq")) {
230 iflags |= GRE_SEQ;
231 } else if (!matches(*argv, "oseq")) {
232 oflags |= GRE_SEQ;
233 } else if (!matches(*argv, "csum")) {
234 iflags |= GRE_CSUM;
235 oflags |= GRE_CSUM;
236 } else if (!matches(*argv, "icsum")) {
237 iflags |= GRE_CSUM;
238 } else if (!matches(*argv, "ocsum")) {
239 oflags |= GRE_CSUM;
240 } else if (!matches(*argv, "nopmtudisc")) {
241 pmtudisc = 0;
242 } else if (!matches(*argv, "pmtudisc")) {
243 pmtudisc = 1;
244 } else if (!matches(*argv, "remote")) {
245 NEXT_ARG();
246 if (strcmp(*argv, "any"))
247 daddr = get_addr32(*argv);
248 } else if (!matches(*argv, "local")) {
249 NEXT_ARG();
250 if (strcmp(*argv, "any"))
251 saddr = get_addr32(*argv);
252 } else if (!matches(*argv, "dev")) {
253 NEXT_ARG();
254 link = if_nametoindex(*argv);
255 if (link == 0) {
256 fprintf(stderr, "Cannot find device \"%s\"\n",
257 *argv);
258 exit(-1);
259 }
260 } else if (!matches(*argv, "ttl") ||
261 !matches(*argv, "hoplimit")) {
262 unsigned int uval;
263
264 NEXT_ARG();
265 if (strcmp(*argv, "inherit") != 0) {
266 if (get_unsigned(&uval, *argv, 0))
267 invarg("invalid TTL\n", *argv);
268 if (uval > 255)
269 invarg("TTL must be <= 255\n", *argv);
270 ttl = uval;
271 }
272 } else if (!matches(*argv, "tos") ||
273 !matches(*argv, "tclass") ||
274 !matches(*argv, "dsfield")) {
275 __u32 uval;
276
277 NEXT_ARG();
278 if (strcmp(*argv, "inherit") != 0) {
279 if (rtnl_dsfield_a2n(&uval, *argv))
280 invarg("bad TOS value", *argv);
281 tos = uval;
282 } else
283 tos = 1;
284 } else if (strcmp(*argv, "noencap") == 0) {
285 encaptype = TUNNEL_ENCAP_NONE;
286 } else if (strcmp(*argv, "encap") == 0) {
287 NEXT_ARG();
288 if (strcmp(*argv, "fou") == 0)
289 encaptype = TUNNEL_ENCAP_FOU;
290 else if (strcmp(*argv, "gue") == 0)
291 encaptype = TUNNEL_ENCAP_GUE;
292 else if (strcmp(*argv, "none") == 0)
293 encaptype = TUNNEL_ENCAP_NONE;
294 else
295 invarg("Invalid encap type.", *argv);
296 } else if (strcmp(*argv, "encap-sport") == 0) {
297 NEXT_ARG();
298 if (strcmp(*argv, "auto") == 0)
299 encapsport = 0;
300 else if (get_u16(&encapsport, *argv, 0))
301 invarg("Invalid source port.", *argv);
302 } else if (strcmp(*argv, "encap-dport") == 0) {
303 NEXT_ARG();
304 if (get_u16(&encapdport, *argv, 0))
305 invarg("Invalid destination port.", *argv);
306 } else if (strcmp(*argv, "encap-csum") == 0) {
307 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
308 } else if (strcmp(*argv, "noencap-csum") == 0) {
309 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
310 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
311 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
312 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
313 encapflags |= ~TUNNEL_ENCAP_FLAG_CSUM6;
314 } else if (strcmp(*argv, "encap-remcsum") == 0) {
315 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
316 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
317 encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM;
318 } else if (strcmp(*argv, "external") == 0) {
319 metadata = 1;
320 } else if (strcmp(*argv, "ignore-df") == 0) {
321 ignore_df = 1;
322 } else if (strcmp(*argv, "noignore-df") == 0) {
323 /*
324 *only the lsb is significant, use 2 for presence
325 */
326 ignore_df = 2;
327 } else if (strcmp(*argv, "fwmark") == 0) {
328 NEXT_ARG();
329 if (get_u32(&fwmark, *argv, 0))
330 invarg("invalid fwmark\n", *argv);
331 } else
332 usage();
333 argc--; argv++;
334 }
335
336 if (!ikey && IN_MULTICAST(ntohl(daddr))) {
337 ikey = daddr;
338 iflags |= GRE_KEY;
339 }
340 if (!okey && IN_MULTICAST(ntohl(daddr))) {
341 okey = daddr;
342 oflags |= GRE_KEY;
343 }
344 if (IN_MULTICAST(ntohl(daddr)) && !saddr) {
345 fprintf(stderr, "A broadcast tunnel requires a source address.\n");
346 return -1;
347 }
348
349 if (!metadata) {
350 addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
351 addattr32(n, 1024, IFLA_GRE_OKEY, okey);
352 addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
353 addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
354 addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4);
355 addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4);
356 addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1);
357 if (link)
358 addattr32(n, 1024, IFLA_GRE_LINK, link);
359 addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1);
360 addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1);
361 addattr32(n, 1024, IFLA_GRE_FWMARK, fwmark);
362 } else {
363 addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0);
364 }
365
366 addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype);
367 addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags);
368 addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport));
369 addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport));
370
371 if (ignore_df)
372 addattr8(n, 1024, IFLA_GRE_IGNORE_DF, ignore_df & 1);
373
374 return 0;
375 }
376
377 static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
378 {
379 char s2[64];
380 const char *local = "any";
381 const char *remote = "any";
382 unsigned int iflags = 0;
383 unsigned int oflags = 0;
384
385 if (tb[IFLA_GRE_REMOTE]) {
386 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
387
388 if (addr)
389 remote = format_host(AF_INET, 4, &addr);
390 }
391
392 print_string(PRINT_ANY, "remote", "remote %s ", remote);
393
394 if (tb[IFLA_GRE_LOCAL]) {
395 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
396
397 if (addr)
398 local = format_host(AF_INET, 4, &addr);
399 }
400
401 print_string(PRINT_ANY, "local", "local %s ", local);
402
403 if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
404 unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
405 const char *n = if_indextoname(link, s2);
406
407 if (n)
408 print_string(PRINT_ANY, "link", "dev %s ", n);
409 else
410 print_uint(PRINT_ANY, "link_index", "dev %u ", link);
411 }
412
413 if (tb[IFLA_GRE_TTL]) {
414 __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
415
416 if (ttl)
417 print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
418 else
419 print_int(PRINT_JSON, "ttl", NULL, ttl);
420 } else {
421 print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
422 }
423
424 if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) {
425 int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]);
426
427 if (is_json_context()) {
428 SPRINT_BUF(b1);
429
430 snprintf(b1, sizeof(b1), "0x%x", tos);
431 print_string(PRINT_JSON, "tos", NULL, b1);
432 } else {
433 fputs("tos ", f);
434 if (tos == 1)
435 fputs("inherit ", f);
436 else
437 fprintf(f, "0x%x ", tos);
438 }
439 }
440
441 if (tb[IFLA_GRE_PMTUDISC]) {
442 if (!rta_getattr_u8(tb[IFLA_GRE_PMTUDISC]))
443 print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false);
444 else
445 print_bool(PRINT_JSON, "pmtudisc", NULL, true);
446 }
447
448 if (tb[IFLA_GRE_IFLAGS])
449 iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
450
451 if (tb[IFLA_GRE_OFLAGS])
452 oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
453
454 if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
455 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
456 print_string(PRINT_ANY, "ikey", "ikey %s ", s2);
457 }
458
459 if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
460 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
461 print_string(PRINT_ANY, "okey", "okey %s ", s2);
462 }
463
464 if (iflags & GRE_SEQ)
465 print_bool(PRINT_ANY, "iseq", "iseq ", true);
466 if (oflags & GRE_SEQ)
467 print_bool(PRINT_ANY, "oseq", "oseq ", true);
468 if (iflags & GRE_CSUM)
469 print_bool(PRINT_ANY, "icsum", "icsum ", true);
470 if (oflags & GRE_CSUM)
471 print_bool(PRINT_ANY, "ocsum", "ocsum ", true);
472
473 if (tb[IFLA_GRE_FWMARK]) {
474 __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]);
475
476 if (fwmark) {
477 snprintf(s2, sizeof(s2), "0x%x", fwmark);
478
479 print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2);
480 }
481 }
482 }
483
484 static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
485 {
486 if (!tb)
487 return;
488
489 if (!tb[IFLA_GRE_COLLECT_METADATA])
490 gre_print_direct_opt(f, tb);
491 else
492 print_bool(PRINT_ANY, "external", "external ", true);
493
494 if (tb[IFLA_GRE_IGNORE_DF] && rta_getattr_u8(tb[IFLA_GRE_IGNORE_DF]))
495 print_bool(PRINT_ANY, "ignore_df", "ignore-df ", true);
496
497 if (tb[IFLA_GRE_ENCAP_TYPE] &&
498 rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
499 __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]);
500 __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]);
501 __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]);
502 __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]);
503
504
505 open_json_object("encap");
506 print_string(PRINT_FP, NULL, "encap ", NULL);
507
508 switch (type) {
509 case TUNNEL_ENCAP_FOU:
510 print_string(PRINT_ANY, "type", "%s ", "fou");
511 break;
512 case TUNNEL_ENCAP_GUE:
513 print_string(PRINT_ANY, "type", "%s ", "gue");
514 break;
515 default:
516 print_null(PRINT_ANY, "type", "%s ", "unknown");
517 break;
518 }
519
520 if (is_json_context()) {
521 print_uint(PRINT_JSON,
522 "sport",
523 NULL,
524 sport ? ntohs(sport) : 0);
525 print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
526
527 print_bool(PRINT_JSON,
528 "csum",
529 NULL,
530 flags & TUNNEL_ENCAP_FLAG_CSUM);
531
532 print_bool(PRINT_JSON,
533 "csum6",
534 NULL,
535 flags & TUNNEL_ENCAP_FLAG_CSUM6);
536
537 print_bool(PRINT_JSON,
538 "remcsum",
539 NULL,
540 flags & TUNNEL_ENCAP_FLAG_REMCSUM);
541
542 close_json_object();
543 } else {
544 if (sport == 0)
545 fputs("encap-sport auto ", f);
546 else
547 fprintf(f, "encap-sport %u", ntohs(sport));
548
549 fprintf(f, "encap-dport %u ", ntohs(dport));
550
551 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
552 fputs("encap-csum ", f);
553 else
554 fputs("noencap-csum ", f);
555
556 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
557 fputs("encap-csum6 ", f);
558 else
559 fputs("noencap-csum6 ", f);
560
561 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
562 fputs("encap-remcsum ", f);
563 else
564 fputs("noencap-remcsum ", f);
565 }
566 }
567 }
568
569 static void gre_print_help(struct link_util *lu, int argc, char **argv,
570 FILE *f)
571 {
572 print_usage(f);
573 }
574
575 struct link_util gre_link_util = {
576 .id = "gre",
577 .maxattr = IFLA_GRE_MAX,
578 .parse_opt = gre_parse_opt,
579 .print_opt = gre_print_opt,
580 .print_help = gre_print_help,
581 };
582
583 struct link_util gretap_link_util = {
584 .id = "gretap",
585 .maxattr = IFLA_GRE_MAX,
586 .parse_opt = gre_parse_opt,
587 .print_opt = gre_print_opt,
588 .print_help = gre_print_help,
589 };