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 } [ 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 "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"
56 static void usage(void) __attribute__((noreturn
));
57 static void usage(void)
63 static int gre_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
66 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
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
,
78 struct rtattr
*tb
[IFLA_MAX
+ 1];
79 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
80 struct rtattr
*greinfo
[IFLA_GRE_MAX
+ 1];
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;
100 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
101 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
104 "Failed to get existing tunnel info.\n");
108 len
= req
.n
.nlmsg_len
;
109 len
-= NLMSG_LENGTH(sizeof(*ifi
));
113 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
115 if (!tb
[IFLA_LINKINFO
])
118 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
120 if (!linkinfo
[IFLA_INFO_DATA
])
123 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
124 linkinfo
[IFLA_INFO_DATA
]);
126 if (greinfo
[IFLA_GRE_IKEY
])
127 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
129 if (greinfo
[IFLA_GRE_OKEY
])
130 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
132 if (greinfo
[IFLA_GRE_IFLAGS
])
133 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
135 if (greinfo
[IFLA_GRE_OFLAGS
])
136 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
138 if (greinfo
[IFLA_GRE_LOCAL
])
139 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
141 if (greinfo
[IFLA_GRE_REMOTE
])
142 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
144 if (greinfo
[IFLA_GRE_PMTUDISC
])
145 pmtudisc
= rta_getattr_u8(
146 greinfo
[IFLA_GRE_PMTUDISC
]);
148 if (greinfo
[IFLA_GRE_TTL
])
149 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
151 if (greinfo
[IFLA_GRE_TOS
])
152 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
154 if (greinfo
[IFLA_GRE_LINK
])
155 link
= rta_getattr_u8(greinfo
[IFLA_GRE_LINK
]);
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
]);
166 if (greinfo
[IFLA_GRE_COLLECT_METADATA
])
169 if (greinfo
[IFLA_GRE_IGNORE_DF
])
171 !!rta_getattr_u8(greinfo
[IFLA_GRE_IGNORE_DF
]);
173 if (greinfo
[IFLA_GRE_FWMARK
])
174 fwmark
= rta_getattr_u32(greinfo
[IFLA_GRE_FWMARK
]);
178 if (!matches(*argv
, "key")) {
184 if (strchr(*argv
, '.'))
185 uval
= get_addr32(*argv
);
187 if (get_unsigned(&uval
, *argv
, 0) < 0) {
189 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
196 } else if (!matches(*argv
, "ikey")) {
201 if (strchr(*argv
, '.'))
202 uval
= get_addr32(*argv
);
204 if (get_unsigned(&uval
, *argv
, 0) < 0) {
205 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
211 } else if (!matches(*argv
, "okey")) {
216 if (strchr(*argv
, '.'))
217 uval
= get_addr32(*argv
);
219 if (get_unsigned(&uval
, *argv
, 0) < 0) {
220 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
226 } else if (!matches(*argv
, "seq")) {
229 } else if (!matches(*argv
, "iseq")) {
231 } else if (!matches(*argv
, "oseq")) {
233 } else if (!matches(*argv
, "csum")) {
236 } else if (!matches(*argv
, "icsum")) {
238 } else if (!matches(*argv
, "ocsum")) {
240 } else if (!matches(*argv
, "nopmtudisc")) {
242 } else if (!matches(*argv
, "pmtudisc")) {
244 } else if (!matches(*argv
, "remote")) {
246 if (strcmp(*argv
, "any"))
247 daddr
= get_addr32(*argv
);
248 } else if (!matches(*argv
, "local")) {
250 if (strcmp(*argv
, "any"))
251 saddr
= get_addr32(*argv
);
252 } else if (!matches(*argv
, "dev")) {
254 link
= if_nametoindex(*argv
);
256 fprintf(stderr
, "Cannot find device \"%s\"\n",
260 } else if (!matches(*argv
, "ttl") ||
261 !matches(*argv
, "hoplimit")) {
265 if (strcmp(*argv
, "inherit") != 0) {
266 if (get_unsigned(&uval
, *argv
, 0))
267 invarg("invalid TTL\n", *argv
);
269 invarg("TTL must be <= 255\n", *argv
);
272 } else if (!matches(*argv
, "tos") ||
273 !matches(*argv
, "tclass") ||
274 !matches(*argv
, "dsfield")) {
278 if (strcmp(*argv
, "inherit") != 0) {
279 if (rtnl_dsfield_a2n(&uval
, *argv
))
280 invarg("bad TOS value", *argv
);
284 } else if (strcmp(*argv
, "noencap") == 0) {
285 encaptype
= TUNNEL_ENCAP_NONE
;
286 } else if (strcmp(*argv
, "encap") == 0) {
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
;
295 invarg("Invalid encap type.", *argv
);
296 } else if (strcmp(*argv
, "encap-sport") == 0) {
298 if (strcmp(*argv
, "auto") == 0)
300 else if (get_u16(&encapsport
, *argv
, 0))
301 invarg("Invalid source port.", *argv
);
302 } else if (strcmp(*argv
, "encap-dport") == 0) {
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) {
320 } else if (strcmp(*argv
, "ignore-df") == 0) {
322 } else if (strcmp(*argv
, "noignore-df") == 0) {
324 *only the lsb is significant, use 2 for presence
327 } else if (strcmp(*argv
, "fwmark") == 0) {
329 if (get_u32(&fwmark
, *argv
, 0))
330 invarg("invalid fwmark\n", *argv
);
336 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
340 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
344 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
345 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
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);
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
);
363 addattr_l(n
, 1024, IFLA_GRE_COLLECT_METADATA
, NULL
, 0);
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
));
372 addattr8(n
, 1024, IFLA_GRE_IGNORE_DF
, ignore_df
& 1);
377 static void gre_print_direct_opt(FILE *f
, struct rtattr
*tb
[])
380 const char *local
= "any";
381 const char *remote
= "any";
382 unsigned int iflags
= 0;
383 unsigned int oflags
= 0;
385 if (tb
[IFLA_GRE_REMOTE
]) {
386 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
389 remote
= format_host(AF_INET
, 4, &addr
);
392 print_string(PRINT_ANY
, "remote", "remote %s ", remote
);
394 if (tb
[IFLA_GRE_LOCAL
]) {
395 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
398 local
= format_host(AF_INET
, 4, &addr
);
401 print_string(PRINT_ANY
, "local", "local %s ", local
);
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
);
408 print_string(PRINT_ANY
, "link", "dev %s ", n
);
410 print_uint(PRINT_ANY
, "link_index", "dev %u ", link
);
413 if (tb
[IFLA_GRE_TTL
]) {
414 __u8 ttl
= rta_getattr_u8(tb
[IFLA_GRE_TTL
]);
417 print_int(PRINT_ANY
, "ttl", "ttl %d ", ttl
);
419 print_int(PRINT_JSON
, "ttl", NULL
, ttl
);
421 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
424 if (tb
[IFLA_GRE_TOS
] && rta_getattr_u8(tb
[IFLA_GRE_TOS
])) {
425 int tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
427 if (is_json_context()) {
430 snprintf(b1
, sizeof(b1
), "0x%x", tos
);
431 print_string(PRINT_JSON
, "tos", NULL
, b1
);
435 fputs("inherit ", f
);
437 fprintf(f
, "0x%x ", tos
);
441 if (tb
[IFLA_GRE_PMTUDISC
]) {
442 if (!rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
443 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
445 print_bool(PRINT_JSON
, "pmtudisc", NULL
, true);
448 if (tb
[IFLA_GRE_IFLAGS
])
449 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
451 if (tb
[IFLA_GRE_OFLAGS
])
452 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
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
);
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
);
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);
473 if (tb
[IFLA_GRE_FWMARK
]) {
474 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_GRE_FWMARK
]);
477 snprintf(s2
, sizeof(s2
), "0x%x", fwmark
);
479 print_string(PRINT_ANY
, "fwmark", "fwmark %s ", s2
);
484 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
489 if (!tb
[IFLA_GRE_COLLECT_METADATA
])
490 gre_print_direct_opt(f
, tb
);
492 print_bool(PRINT_ANY
, "external", "external ", true);
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);
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
]);
505 open_json_object("encap");
506 print_string(PRINT_FP
, NULL
, "encap ", NULL
);
509 case TUNNEL_ENCAP_FOU
:
510 print_string(PRINT_ANY
, "type", "%s ", "fou");
512 case TUNNEL_ENCAP_GUE
:
513 print_string(PRINT_ANY
, "type", "%s ", "gue");
516 print_null(PRINT_ANY
, "type", "%s ", "unknown");
520 if (is_json_context()) {
521 print_uint(PRINT_JSON
,
524 sport
? ntohs(sport
) : 0);
525 print_uint(PRINT_JSON
, "dport", NULL
, ntohs(dport
));
527 print_bool(PRINT_JSON
,
530 flags
& TUNNEL_ENCAP_FLAG_CSUM
);
532 print_bool(PRINT_JSON
,
535 flags
& TUNNEL_ENCAP_FLAG_CSUM6
);
537 print_bool(PRINT_JSON
,
540 flags
& TUNNEL_ENCAP_FLAG_REMCSUM
);
545 fputs("encap-sport auto ", f
);
547 fprintf(f
, "encap-sport %u", ntohs(sport
));
549 fprintf(f
, "encap-dport %u ", ntohs(dport
));
551 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
552 fputs("encap-csum ", f
);
554 fputs("noencap-csum ", f
);
556 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
557 fputs("encap-csum6 ", f
);
559 fputs("noencap-csum6 ", f
);
561 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
562 fputs("encap-remcsum ", f
);
564 fputs("noencap-remcsum ", f
);
569 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
575 struct link_util gre_link_util
= {
577 .maxattr
= IFLA_GRE_MAX
,
578 .parse_opt
= gre_parse_opt
,
579 .print_opt
= gre_print_opt
,
580 .print_help
= gre_print_help
,
583 struct link_util gretap_link_util
= {
585 .maxattr
= IFLA_GRE_MAX
,
586 .parse_opt
= gre_parse_opt
,
587 .print_opt
= gre_print_opt
,
588 .print_help
= gre_print_help
,