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"
50 "Where: ADDR := { IP_ADDRESS | any }\n"
51 " TOS := { NUMBER | inherit }\n"
52 " TTL := { 1..255 | inherit }\n"
53 " KEY := { DOTTED_QUAD | NUMBER }\n"
54 " MARK := { 0x0..0xffffffff }\n"
58 static void usage(void) __attribute__((noreturn
));
59 static void usage(void)
65 static int gre_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
68 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
73 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
74 .n
.nlmsg_flags
= NLM_F_REQUEST
,
75 .n
.nlmsg_type
= RTM_GETLINK
,
76 .i
.ifi_family
= preferred_family
,
77 .i
.ifi_index
= ifi
->ifi_index
,
79 struct nlmsghdr
*answer
;
80 struct rtattr
*tb
[IFLA_MAX
+ 1];
81 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
82 struct rtattr
*greinfo
[IFLA_GRE_MAX
+ 1];
87 unsigned int saddr
= 0;
88 unsigned int daddr
= 0;
89 unsigned int link
= 0;
101 __u32 erspan_idx
= 0;
103 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
104 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
107 "Failed to get existing tunnel info.\n");
111 len
= answer
->nlmsg_len
;
112 len
-= NLMSG_LENGTH(sizeof(*ifi
));
116 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
118 if (!tb
[IFLA_LINKINFO
])
121 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
123 if (!linkinfo
[IFLA_INFO_DATA
])
126 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
127 linkinfo
[IFLA_INFO_DATA
]);
129 if (greinfo
[IFLA_GRE_IKEY
])
130 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
132 if (greinfo
[IFLA_GRE_OKEY
])
133 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
135 if (greinfo
[IFLA_GRE_IFLAGS
])
136 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
138 if (greinfo
[IFLA_GRE_OFLAGS
])
139 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
141 if (greinfo
[IFLA_GRE_LOCAL
])
142 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
144 if (greinfo
[IFLA_GRE_REMOTE
])
145 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
147 if (greinfo
[IFLA_GRE_PMTUDISC
])
148 pmtudisc
= rta_getattr_u8(
149 greinfo
[IFLA_GRE_PMTUDISC
]);
151 if (greinfo
[IFLA_GRE_TTL
])
152 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
154 if (greinfo
[IFLA_GRE_TOS
])
155 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
157 if (greinfo
[IFLA_GRE_LINK
])
158 link
= rta_getattr_u32(greinfo
[IFLA_GRE_LINK
]);
160 if (greinfo
[IFLA_GRE_ENCAP_TYPE
])
161 encaptype
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_TYPE
]);
162 if (greinfo
[IFLA_GRE_ENCAP_FLAGS
])
163 encapflags
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_FLAGS
]);
164 if (greinfo
[IFLA_GRE_ENCAP_SPORT
])
165 encapsport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_SPORT
]);
166 if (greinfo
[IFLA_GRE_ENCAP_DPORT
])
167 encapdport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_DPORT
]);
169 if (greinfo
[IFLA_GRE_COLLECT_METADATA
])
172 if (greinfo
[IFLA_GRE_IGNORE_DF
])
174 !!rta_getattr_u8(greinfo
[IFLA_GRE_IGNORE_DF
]);
176 if (greinfo
[IFLA_GRE_FWMARK
])
177 fwmark
= rta_getattr_u32(greinfo
[IFLA_GRE_FWMARK
]);
179 if (greinfo
[IFLA_GRE_ERSPAN_INDEX
])
180 erspan_idx
= rta_getattr_u32(greinfo
[IFLA_GRE_ERSPAN_INDEX
]);
186 if (!matches(*argv
, "key")) {
190 ikey
= okey
= tnl_parse_key("key", *argv
);
191 } else if (!matches(*argv
, "ikey")) {
194 ikey
= tnl_parse_key("ikey", *argv
);
195 } else if (!matches(*argv
, "okey")) {
198 okey
= tnl_parse_key("okey", *argv
);
199 } else if (!matches(*argv
, "seq")) {
202 } else if (!matches(*argv
, "iseq")) {
204 } else if (!matches(*argv
, "oseq")) {
206 } else if (!matches(*argv
, "csum")) {
209 } else if (!matches(*argv
, "icsum")) {
211 } else if (!matches(*argv
, "ocsum")) {
213 } else if (!matches(*argv
, "nopmtudisc")) {
215 } else if (!matches(*argv
, "pmtudisc")) {
217 } else if (!matches(*argv
, "remote")) {
219 daddr
= get_addr32(*argv
);
220 } else if (!matches(*argv
, "local")) {
222 saddr
= get_addr32(*argv
);
223 } else if (!matches(*argv
, "dev")) {
225 link
= if_nametoindex(*argv
);
227 fprintf(stderr
, "Cannot find device \"%s\"\n",
231 } else if (!matches(*argv
, "ttl") ||
232 !matches(*argv
, "hoplimit")) {
236 if (strcmp(*argv
, "inherit") != 0) {
237 if (get_unsigned(&uval
, *argv
, 0))
238 invarg("invalid TTL\n", *argv
);
240 invarg("TTL must be <= 255\n", *argv
);
244 } else if (!matches(*argv
, "tos") ||
245 !matches(*argv
, "tclass") ||
246 !matches(*argv
, "dsfield")) {
250 if (strcmp(*argv
, "inherit") != 0) {
251 if (rtnl_dsfield_a2n(&uval
, *argv
))
252 invarg("bad TOS value", *argv
);
256 } else if (strcmp(*argv
, "noencap") == 0) {
257 encaptype
= TUNNEL_ENCAP_NONE
;
258 } else if (strcmp(*argv
, "encap") == 0) {
260 if (strcmp(*argv
, "fou") == 0)
261 encaptype
= TUNNEL_ENCAP_FOU
;
262 else if (strcmp(*argv
, "gue") == 0)
263 encaptype
= TUNNEL_ENCAP_GUE
;
264 else if (strcmp(*argv
, "none") == 0)
265 encaptype
= TUNNEL_ENCAP_NONE
;
267 invarg("Invalid encap type.", *argv
);
268 } else if (strcmp(*argv
, "encap-sport") == 0) {
270 if (strcmp(*argv
, "auto") == 0)
272 else if (get_u16(&encapsport
, *argv
, 0))
273 invarg("Invalid source port.", *argv
);
274 } else if (strcmp(*argv
, "encap-dport") == 0) {
276 if (get_u16(&encapdport
, *argv
, 0))
277 invarg("Invalid destination port.", *argv
);
278 } else if (strcmp(*argv
, "encap-csum") == 0) {
279 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
280 } else if (strcmp(*argv
, "noencap-csum") == 0) {
281 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
282 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
283 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
284 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
285 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
286 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
287 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
288 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
289 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
290 } else if (strcmp(*argv
, "external") == 0) {
292 } else if (strcmp(*argv
, "ignore-df") == 0) {
294 } else if (strcmp(*argv
, "noignore-df") == 0) {
296 *only the lsb is significant, use 2 for presence
299 } else if (strcmp(*argv
, "fwmark") == 0) {
301 if (get_u32(&fwmark
, *argv
, 0))
302 invarg("invalid fwmark\n", *argv
);
303 } else if (strcmp(*argv
, "erspan") == 0) {
305 if (get_u32(&erspan_idx
, *argv
, 0))
306 invarg("invalid erspan index\n", *argv
);
307 if (erspan_idx
& ~((1<<20) - 1) || erspan_idx
== 0)
308 invarg("erspan index must be > 0 and <= 20-bit\n", *argv
);
314 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
318 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
322 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
323 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
328 addattr32(n
, 1024, IFLA_GRE_IKEY
, ikey
);
329 addattr32(n
, 1024, IFLA_GRE_OKEY
, okey
);
330 addattr_l(n
, 1024, IFLA_GRE_IFLAGS
, &iflags
, 2);
331 addattr_l(n
, 1024, IFLA_GRE_OFLAGS
, &oflags
, 2);
332 addattr_l(n
, 1024, IFLA_GRE_LOCAL
, &saddr
, 4);
333 addattr_l(n
, 1024, IFLA_GRE_REMOTE
, &daddr
, 4);
334 addattr_l(n
, 1024, IFLA_GRE_PMTUDISC
, &pmtudisc
, 1);
336 addattr32(n
, 1024, IFLA_GRE_LINK
, link
);
337 addattr_l(n
, 1024, IFLA_GRE_TTL
, &ttl
, 1);
338 addattr_l(n
, 1024, IFLA_GRE_TOS
, &tos
, 1);
339 addattr32(n
, 1024, IFLA_GRE_FWMARK
, fwmark
);
341 addattr32(n
, 1024, IFLA_GRE_ERSPAN_INDEX
, erspan_idx
);
343 addattr_l(n
, 1024, IFLA_GRE_COLLECT_METADATA
, NULL
, 0);
346 addattr16(n
, 1024, IFLA_GRE_ENCAP_TYPE
, encaptype
);
347 addattr16(n
, 1024, IFLA_GRE_ENCAP_FLAGS
, encapflags
);
348 addattr16(n
, 1024, IFLA_GRE_ENCAP_SPORT
, htons(encapsport
));
349 addattr16(n
, 1024, IFLA_GRE_ENCAP_DPORT
, htons(encapdport
));
352 addattr8(n
, 1024, IFLA_GRE_IGNORE_DF
, ignore_df
& 1);
357 static void gre_print_direct_opt(FILE *f
, struct rtattr
*tb
[])
360 const char *local
= "any";
361 const char *remote
= "any";
362 unsigned int iflags
= 0;
363 unsigned int oflags
= 0;
367 if (tb
[IFLA_GRE_REMOTE
]) {
368 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
371 remote
= format_host(AF_INET
, 4, &addr
);
374 print_string(PRINT_ANY
, "remote", "remote %s ", remote
);
376 if (tb
[IFLA_GRE_LOCAL
]) {
377 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
380 local
= format_host(AF_INET
, 4, &addr
);
383 print_string(PRINT_ANY
, "local", "local %s ", local
);
385 if (tb
[IFLA_GRE_LINK
]) {
386 unsigned int link
= rta_getattr_u32(tb
[IFLA_GRE_LINK
]);
389 print_string(PRINT_ANY
, "link", "dev %s ",
390 ll_index_to_name(link
));
394 if (tb
[IFLA_GRE_TTL
])
395 ttl
= rta_getattr_u8(tb
[IFLA_GRE_TTL
]);
396 if (is_json_context() || ttl
)
397 print_uint(PRINT_ANY
, "ttl", "ttl %u ", ttl
);
399 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
401 if (tb
[IFLA_GRE_TOS
])
402 tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
404 if (is_json_context() || tos
!= 1)
405 print_0xhex(PRINT_ANY
, "tos", "tos 0x%x ", tos
);
407 print_string(PRINT_FP
, NULL
, "tos %s ", "inherit");
410 if (tb
[IFLA_GRE_PMTUDISC
]) {
411 if (!rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
412 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
414 print_bool(PRINT_JSON
, "pmtudisc", NULL
, true);
417 if (tb
[IFLA_GRE_IFLAGS
])
418 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
420 if (tb
[IFLA_GRE_OFLAGS
])
421 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
423 if ((iflags
& GRE_KEY
) && tb
[IFLA_GRE_IKEY
]) {
424 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_IKEY
]), s2
, sizeof(s2
));
425 print_string(PRINT_ANY
, "ikey", "ikey %s ", s2
);
428 if ((oflags
& GRE_KEY
) && tb
[IFLA_GRE_OKEY
]) {
429 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_OKEY
]), s2
, sizeof(s2
));
430 print_string(PRINT_ANY
, "okey", "okey %s ", s2
);
433 if (iflags
& GRE_SEQ
)
434 print_bool(PRINT_ANY
, "iseq", "iseq ", true);
435 if (oflags
& GRE_SEQ
)
436 print_bool(PRINT_ANY
, "oseq", "oseq ", true);
437 if (iflags
& GRE_CSUM
)
438 print_bool(PRINT_ANY
, "icsum", "icsum ", true);
439 if (oflags
& GRE_CSUM
)
440 print_bool(PRINT_ANY
, "ocsum", "ocsum ", true);
442 if (tb
[IFLA_GRE_FWMARK
]) {
443 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_GRE_FWMARK
]);
446 print_0xhex(PRINT_ANY
,
447 "fwmark", "fwmark 0x%x ", fwmark
);
452 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
457 if (!tb
[IFLA_GRE_COLLECT_METADATA
])
458 gre_print_direct_opt(f
, tb
);
460 print_bool(PRINT_ANY
, "external", "external ", true);
462 if (tb
[IFLA_GRE_IGNORE_DF
] && rta_getattr_u8(tb
[IFLA_GRE_IGNORE_DF
]))
463 print_bool(PRINT_ANY
, "ignore_df", "ignore-df ", true);
465 if (tb
[IFLA_GRE_ERSPAN_INDEX
]) {
466 __u32 erspan_idx
= rta_getattr_u32(tb
[IFLA_GRE_ERSPAN_INDEX
]);
468 print_uint(PRINT_ANY
,
469 "erspan_index", "erspan_index %u ", erspan_idx
);
474 IFLA_GRE_ENCAP_FLAGS
,
475 IFLA_GRE_ENCAP_SPORT
,
476 IFLA_GRE_ENCAP_DPORT
);
479 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
485 struct link_util gre_link_util
= {
487 .maxattr
= IFLA_GRE_MAX
,
488 .parse_opt
= gre_parse_opt
,
489 .print_opt
= gre_print_opt
,
490 .print_help
= gre_print_help
,
493 struct link_util gretap_link_util
= {
495 .maxattr
= IFLA_GRE_MAX
,
496 .parse_opt
= gre_parse_opt
,
497 .print_opt
= gre_print_opt
,
498 .print_help
= gre_print_help
,
501 struct link_util erspan_link_util
= {
503 .maxattr
= IFLA_GRE_MAX
,
504 .parse_opt
= gre_parse_opt
,
505 .print_opt
= gre_print_opt
,
506 .print_help
= gre_print_help
,