2 * link_gre.c gre driver module
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.
9 * Authors: Herbert Xu <herbert@gondor.apana.org.au>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
20 #include <linux/if_tunnel.h>
23 #include "ip_common.h"
26 static void print_usage(FILE *f
)
29 "Usage: ... { gre | gretap | erspan } [ remote ADDR ]\n"
37 " [ [no]ignore-df ]\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"
48 " [ erspan_ver version ]\n"
50 " [ erspan_dir { ingress | egress } ]\n"
51 " [ erspan_hwid hwid ]\n"
54 "Where: ADDR := { IP_ADDRESS | any }\n"
55 " TOS := { NUMBER | inherit }\n"
56 " TTL := { 1..255 | inherit }\n"
57 " KEY := { DOTTED_QUAD | NUMBER }\n"
58 " MARK := { 0x0..0xffffffff }\n"
62 static void usage(void) __attribute__((noreturn
));
63 static void usage(void)
69 static int gre_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
72 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
77 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
78 .n
.nlmsg_flags
= NLM_F_REQUEST
,
79 .n
.nlmsg_type
= RTM_GETLINK
,
80 .i
.ifi_family
= preferred_family
,
81 .i
.ifi_index
= ifi
->ifi_index
,
83 struct nlmsghdr
*answer
;
84 struct rtattr
*tb
[IFLA_MAX
+ 1];
85 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
86 struct rtattr
*greinfo
[IFLA_GRE_MAX
+ 1];
91 unsigned int saddr
= 0;
92 unsigned int daddr
= 0;
93 unsigned int link
= 0;
100 __u16 encapsport
= 0;
101 __u16 encapdport
= 0;
105 __u32 erspan_idx
= 0;
108 __u16 erspan_hwid
= 0;
110 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
111 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
114 "Failed to get existing tunnel info.\n");
118 len
= answer
->nlmsg_len
;
119 len
-= NLMSG_LENGTH(sizeof(*ifi
));
123 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
125 if (!tb
[IFLA_LINKINFO
])
128 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
130 if (!linkinfo
[IFLA_INFO_DATA
])
133 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
134 linkinfo
[IFLA_INFO_DATA
]);
136 if (greinfo
[IFLA_GRE_IKEY
])
137 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
139 if (greinfo
[IFLA_GRE_OKEY
])
140 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
142 if (greinfo
[IFLA_GRE_IFLAGS
])
143 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
145 if (greinfo
[IFLA_GRE_OFLAGS
])
146 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
148 if (greinfo
[IFLA_GRE_LOCAL
])
149 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
151 if (greinfo
[IFLA_GRE_REMOTE
])
152 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
154 if (greinfo
[IFLA_GRE_PMTUDISC
])
155 pmtudisc
= rta_getattr_u8(
156 greinfo
[IFLA_GRE_PMTUDISC
]);
158 if (greinfo
[IFLA_GRE_TTL
])
159 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
161 if (greinfo
[IFLA_GRE_TOS
])
162 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
164 if (greinfo
[IFLA_GRE_LINK
])
165 link
= rta_getattr_u32(greinfo
[IFLA_GRE_LINK
]);
167 if (greinfo
[IFLA_GRE_ENCAP_TYPE
])
168 encaptype
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_TYPE
]);
169 if (greinfo
[IFLA_GRE_ENCAP_FLAGS
])
170 encapflags
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_FLAGS
]);
171 if (greinfo
[IFLA_GRE_ENCAP_SPORT
])
172 encapsport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_SPORT
]);
173 if (greinfo
[IFLA_GRE_ENCAP_DPORT
])
174 encapdport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_DPORT
]);
176 if (greinfo
[IFLA_GRE_COLLECT_METADATA
])
179 if (greinfo
[IFLA_GRE_IGNORE_DF
])
181 !!rta_getattr_u8(greinfo
[IFLA_GRE_IGNORE_DF
]);
183 if (greinfo
[IFLA_GRE_FWMARK
])
184 fwmark
= rta_getattr_u32(greinfo
[IFLA_GRE_FWMARK
]);
186 if (greinfo
[IFLA_GRE_ERSPAN_INDEX
])
187 erspan_idx
= rta_getattr_u32(greinfo
[IFLA_GRE_ERSPAN_INDEX
]);
189 if (greinfo
[IFLA_GRE_ERSPAN_VER
])
190 erspan_ver
= rta_getattr_u8(greinfo
[IFLA_GRE_ERSPAN_VER
]);
192 if (greinfo
[IFLA_GRE_ERSPAN_DIR
])
193 erspan_dir
= rta_getattr_u8(greinfo
[IFLA_GRE_ERSPAN_DIR
]);
195 if (greinfo
[IFLA_GRE_ERSPAN_HWID
])
196 erspan_hwid
= rta_getattr_u16(greinfo
[IFLA_GRE_ERSPAN_HWID
]);
202 if (!matches(*argv
, "key")) {
206 ikey
= okey
= tnl_parse_key("key", *argv
);
207 } else if (!matches(*argv
, "ikey")) {
210 ikey
= tnl_parse_key("ikey", *argv
);
211 } else if (!matches(*argv
, "okey")) {
214 okey
= tnl_parse_key("okey", *argv
);
215 } else if (!matches(*argv
, "seq")) {
218 } else if (!matches(*argv
, "iseq")) {
220 } else if (!matches(*argv
, "oseq")) {
222 } else if (!matches(*argv
, "csum")) {
225 } else if (!matches(*argv
, "icsum")) {
227 } else if (!matches(*argv
, "ocsum")) {
229 } else if (!matches(*argv
, "nopmtudisc")) {
231 } else if (!matches(*argv
, "pmtudisc")) {
233 } else if (!matches(*argv
, "remote")) {
235 daddr
= get_addr32(*argv
);
236 } else if (!matches(*argv
, "local")) {
238 saddr
= get_addr32(*argv
);
239 } else if (!matches(*argv
, "dev")) {
241 link
= if_nametoindex(*argv
);
243 fprintf(stderr
, "Cannot find device \"%s\"\n",
247 } else if (!matches(*argv
, "ttl") ||
248 !matches(*argv
, "hoplimit")) {
252 if (strcmp(*argv
, "inherit") != 0) {
253 if (get_unsigned(&uval
, *argv
, 0))
254 invarg("invalid TTL\n", *argv
);
256 invarg("TTL must be <= 255\n", *argv
);
260 } else if (!matches(*argv
, "tos") ||
261 !matches(*argv
, "tclass") ||
262 !matches(*argv
, "dsfield")) {
266 if (strcmp(*argv
, "inherit") != 0) {
267 if (rtnl_dsfield_a2n(&uval
, *argv
))
268 invarg("bad TOS value", *argv
);
272 } else if (strcmp(*argv
, "noencap") == 0) {
273 encaptype
= TUNNEL_ENCAP_NONE
;
274 } else if (strcmp(*argv
, "encap") == 0) {
276 if (strcmp(*argv
, "fou") == 0)
277 encaptype
= TUNNEL_ENCAP_FOU
;
278 else if (strcmp(*argv
, "gue") == 0)
279 encaptype
= TUNNEL_ENCAP_GUE
;
280 else if (strcmp(*argv
, "none") == 0)
281 encaptype
= TUNNEL_ENCAP_NONE
;
283 invarg("Invalid encap type.", *argv
);
284 } else if (strcmp(*argv
, "encap-sport") == 0) {
286 if (strcmp(*argv
, "auto") == 0)
288 else if (get_u16(&encapsport
, *argv
, 0))
289 invarg("Invalid source port.", *argv
);
290 } else if (strcmp(*argv
, "encap-dport") == 0) {
292 if (get_u16(&encapdport
, *argv
, 0))
293 invarg("Invalid destination port.", *argv
);
294 } else if (strcmp(*argv
, "encap-csum") == 0) {
295 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
296 } else if (strcmp(*argv
, "noencap-csum") == 0) {
297 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
298 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
299 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
300 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
301 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
302 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
303 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
304 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
305 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
306 } else if (strcmp(*argv
, "external") == 0) {
308 } else if (strcmp(*argv
, "ignore-df") == 0) {
310 } else if (strcmp(*argv
, "noignore-df") == 0) {
312 *only the lsb is significant, use 2 for presence
315 } else if (strcmp(*argv
, "fwmark") == 0) {
317 if (get_u32(&fwmark
, *argv
, 0))
318 invarg("invalid fwmark\n", *argv
);
319 } else if (strcmp(*argv
, "erspan") == 0) {
321 if (get_u32(&erspan_idx
, *argv
, 0))
322 invarg("invalid erspan index\n", *argv
);
323 if (erspan_idx
& ~((1<<20) - 1) || erspan_idx
== 0)
324 invarg("erspan index must be > 0 and <= 20-bit\n", *argv
);
325 } else if (strcmp(*argv
, "erspan_ver") == 0) {
327 if (get_u8(&erspan_ver
, *argv
, 0))
328 invarg("invalid erspan version\n", *argv
);
329 if (erspan_ver
!= 1 && erspan_ver
!= 2)
330 invarg("erspan version must be 1 or 2\n", *argv
);
331 } else if (strcmp(*argv
, "erspan_dir") == 0) {
333 if (matches(*argv
, "ingress") == 0)
335 else if (matches(*argv
, "egress") == 0)
338 invarg("Invalid erspan direction.", *argv
);
339 } else if (strcmp(*argv
, "erspan_hwid") == 0) {
341 if (get_u16(&erspan_hwid
, *argv
, 0))
342 invarg("invalid erspan hwid\n", *argv
);
348 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
352 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
356 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
357 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
362 addattr32(n
, 1024, IFLA_GRE_IKEY
, ikey
);
363 addattr32(n
, 1024, IFLA_GRE_OKEY
, okey
);
364 addattr_l(n
, 1024, IFLA_GRE_IFLAGS
, &iflags
, 2);
365 addattr_l(n
, 1024, IFLA_GRE_OFLAGS
, &oflags
, 2);
366 addattr_l(n
, 1024, IFLA_GRE_LOCAL
, &saddr
, 4);
367 addattr_l(n
, 1024, IFLA_GRE_REMOTE
, &daddr
, 4);
368 addattr_l(n
, 1024, IFLA_GRE_PMTUDISC
, &pmtudisc
, 1);
370 addattr32(n
, 1024, IFLA_GRE_LINK
, link
);
371 addattr_l(n
, 1024, IFLA_GRE_TTL
, &ttl
, 1);
372 addattr_l(n
, 1024, IFLA_GRE_TOS
, &tos
, 1);
373 addattr32(n
, 1024, IFLA_GRE_FWMARK
, fwmark
);
375 addattr8(n
, 1024, IFLA_GRE_ERSPAN_VER
, erspan_ver
);
376 if (erspan_ver
== 1 && erspan_idx
!= 0) {
377 addattr32(n
, 1024, IFLA_GRE_ERSPAN_INDEX
, erspan_idx
);
378 } else if (erspan_ver
== 2) {
379 addattr8(n
, 1024, IFLA_GRE_ERSPAN_DIR
, erspan_dir
);
380 addattr16(n
, 1024, IFLA_GRE_ERSPAN_HWID
, erspan_hwid
);
384 addattr_l(n
, 1024, IFLA_GRE_COLLECT_METADATA
, NULL
, 0);
387 addattr16(n
, 1024, IFLA_GRE_ENCAP_TYPE
, encaptype
);
388 addattr16(n
, 1024, IFLA_GRE_ENCAP_FLAGS
, encapflags
);
389 addattr16(n
, 1024, IFLA_GRE_ENCAP_SPORT
, htons(encapsport
));
390 addattr16(n
, 1024, IFLA_GRE_ENCAP_DPORT
, htons(encapdport
));
393 addattr8(n
, 1024, IFLA_GRE_IGNORE_DF
, ignore_df
& 1);
398 static void gre_print_direct_opt(FILE *f
, struct rtattr
*tb
[])
401 const char *local
= "any";
402 const char *remote
= "any";
403 unsigned int iflags
= 0;
404 unsigned int oflags
= 0;
406 if (tb
[IFLA_GRE_REMOTE
]) {
407 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
410 remote
= format_host(AF_INET
, 4, &addr
);
413 print_string(PRINT_ANY
, "remote", "remote %s ", remote
);
415 if (tb
[IFLA_GRE_LOCAL
]) {
416 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
419 local
= format_host(AF_INET
, 4, &addr
);
422 print_string(PRINT_ANY
, "local", "local %s ", local
);
424 if (tb
[IFLA_GRE_LINK
] && rta_getattr_u32(tb
[IFLA_GRE_LINK
])) {
425 unsigned int link
= rta_getattr_u32(tb
[IFLA_GRE_LINK
]);
426 const char *n
= if_indextoname(link
, s2
);
429 print_string(PRINT_ANY
, "link", "dev %s ", n
);
431 print_uint(PRINT_ANY
, "link_index", "dev %u ", link
);
434 if (tb
[IFLA_GRE_TTL
]) {
435 __u8 ttl
= rta_getattr_u8(tb
[IFLA_GRE_TTL
]);
438 print_int(PRINT_ANY
, "ttl", "ttl %d ", ttl
);
440 print_int(PRINT_JSON
, "ttl", NULL
, ttl
);
442 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
445 if (tb
[IFLA_GRE_TOS
] && rta_getattr_u8(tb
[IFLA_GRE_TOS
])) {
446 int tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
448 if (is_json_context()) {
451 snprintf(b1
, sizeof(b1
), "0x%x", tos
);
452 print_string(PRINT_JSON
, "tos", NULL
, b1
);
456 fputs("inherit ", f
);
458 fprintf(f
, "0x%x ", tos
);
462 if (tb
[IFLA_GRE_PMTUDISC
]) {
463 if (!rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
464 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
466 print_bool(PRINT_JSON
, "pmtudisc", NULL
, true);
469 if (tb
[IFLA_GRE_IFLAGS
])
470 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
472 if (tb
[IFLA_GRE_OFLAGS
])
473 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
475 if ((iflags
& GRE_KEY
) && tb
[IFLA_GRE_IKEY
]) {
476 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_IKEY
]), s2
, sizeof(s2
));
477 print_string(PRINT_ANY
, "ikey", "ikey %s ", s2
);
480 if ((oflags
& GRE_KEY
) && tb
[IFLA_GRE_OKEY
]) {
481 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_OKEY
]), s2
, sizeof(s2
));
482 print_string(PRINT_ANY
, "okey", "okey %s ", s2
);
485 if (iflags
& GRE_SEQ
)
486 print_bool(PRINT_ANY
, "iseq", "iseq ", true);
487 if (oflags
& GRE_SEQ
)
488 print_bool(PRINT_ANY
, "oseq", "oseq ", true);
489 if (iflags
& GRE_CSUM
)
490 print_bool(PRINT_ANY
, "icsum", "icsum ", true);
491 if (oflags
& GRE_CSUM
)
492 print_bool(PRINT_ANY
, "ocsum", "ocsum ", true);
494 if (tb
[IFLA_GRE_FWMARK
]) {
495 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_GRE_FWMARK
]);
498 snprintf(s2
, sizeof(s2
), "0x%x", fwmark
);
500 print_string(PRINT_ANY
, "fwmark", "fwmark %s ", s2
);
505 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
510 if (!tb
[IFLA_GRE_COLLECT_METADATA
])
511 gre_print_direct_opt(f
, tb
);
513 print_bool(PRINT_ANY
, "external", "external ", true);
515 if (tb
[IFLA_GRE_IGNORE_DF
] && rta_getattr_u8(tb
[IFLA_GRE_IGNORE_DF
]))
516 print_bool(PRINT_ANY
, "ignore_df", "ignore-df ", true);
518 if (tb
[IFLA_GRE_ERSPAN_INDEX
]) {
519 __u32 erspan_idx
= rta_getattr_u32(tb
[IFLA_GRE_ERSPAN_INDEX
]);
521 print_uint(PRINT_ANY
, "erspan_index", "erspan_index %u ", erspan_idx
);
524 if (tb
[IFLA_GRE_ERSPAN_VER
]) {
525 __u8 erspan_ver
= rta_getattr_u8(tb
[IFLA_GRE_ERSPAN_VER
]);
527 print_uint(PRINT_ANY
, "erspan_ver", "erspan_ver %u ", erspan_ver
);
530 if (tb
[IFLA_GRE_ERSPAN_DIR
]) {
531 __u8 erspan_dir
= rta_getattr_u8(tb
[IFLA_GRE_ERSPAN_DIR
]);
534 print_string(PRINT_ANY
, "erspan_dir",
535 "erspan_dir ingress ", NULL
);
537 print_string(PRINT_ANY
, "erspan_dir",
538 "erspan_dir egress ", NULL
);
541 if (tb
[IFLA_GRE_ERSPAN_HWID
]) {
542 __u16 erspan_hwid
= rta_getattr_u16(tb
[IFLA_GRE_ERSPAN_HWID
]);
544 print_hex(PRINT_ANY
, "erspan_hwid", "erspan_hwid 0x%x ", erspan_hwid
);
547 if (tb
[IFLA_GRE_ENCAP_TYPE
] &&
548 rta_getattr_u16(tb
[IFLA_GRE_ENCAP_TYPE
]) != TUNNEL_ENCAP_NONE
) {
549 __u16 type
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_TYPE
]);
550 __u16 flags
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_FLAGS
]);
551 __u16 sport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_SPORT
]);
552 __u16 dport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_DPORT
]);
555 open_json_object("encap");
556 print_string(PRINT_FP
, NULL
, "encap ", NULL
);
559 case TUNNEL_ENCAP_FOU
:
560 print_string(PRINT_ANY
, "type", "%s ", "fou");
562 case TUNNEL_ENCAP_GUE
:
563 print_string(PRINT_ANY
, "type", "%s ", "gue");
566 print_null(PRINT_ANY
, "type", "%s ", "unknown");
570 if (is_json_context()) {
571 print_uint(PRINT_JSON
,
574 sport
? ntohs(sport
) : 0);
575 print_uint(PRINT_JSON
, "dport", NULL
, ntohs(dport
));
577 print_bool(PRINT_JSON
,
580 flags
& TUNNEL_ENCAP_FLAG_CSUM
);
582 print_bool(PRINT_JSON
,
585 flags
& TUNNEL_ENCAP_FLAG_CSUM6
);
587 print_bool(PRINT_JSON
,
590 flags
& TUNNEL_ENCAP_FLAG_REMCSUM
);
595 fputs("encap-sport auto ", f
);
597 fprintf(f
, "encap-sport %u", ntohs(sport
));
599 fprintf(f
, "encap-dport %u ", ntohs(dport
));
601 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
602 fputs("encap-csum ", f
);
604 fputs("noencap-csum ", f
);
606 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
607 fputs("encap-csum6 ", f
);
609 fputs("noencap-csum6 ", f
);
611 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
612 fputs("encap-remcsum ", f
);
614 fputs("noencap-remcsum ", f
);
619 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
625 struct link_util gre_link_util
= {
627 .maxattr
= IFLA_GRE_MAX
,
628 .parse_opt
= gre_parse_opt
,
629 .print_opt
= gre_print_opt
,
630 .print_help
= gre_print_help
,
633 struct link_util gretap_link_util
= {
635 .maxattr
= IFLA_GRE_MAX
,
636 .parse_opt
= gre_parse_opt
,
637 .print_opt
= gre_print_opt
,
638 .print_help
= gre_print_help
,
641 struct link_util erspan_link_util
= {
643 .maxattr
= IFLA_GRE_MAX
,
644 .parse_opt
= gre_parse_opt
,
645 .print_opt
= gre_print_opt
,
646 .print_help
= gre_print_help
,