2 * link_gre6.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: Dmitry Kozlov <xeb@mail.ru>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
20 #include <linux/if_tunnel.h>
21 #include <linux/ip6_tunnel.h>
25 #include "ip_common.h"
28 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
29 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
31 #define DEFAULT_TNL_HOP_LIMIT (64)
33 static void print_usage(FILE *f
)
36 "Usage: ... { ip6gre | ip6gretap } [ remote ADDR ]\n"
42 " [ encaplimit ELIM ]\n"
43 " [ tclass TCLASS ]\n"
44 " [ flowlabel FLOWLABEL ]\n"
49 " [ encap { fou | gue | none } ]\n"
50 " [ encap-sport PORT ]\n"
51 " [ encap-dport PORT ]\n"
52 " [ [no]encap-csum ]\n"
53 " [ [no]encap-csum6 ]\n"
54 " [ [no]encap-remcsum ]\n"
56 "Where: ADDR := IPV6_ADDRESS\n"
57 " TTL := { 0..255 } (default=%d)\n"
58 " KEY := { DOTTED_QUAD | NUMBER }\n"
59 " ELIM := { none | 0..255 }(default=%d)\n"
60 " TCLASS := { 0x0..0xff | inherit }\n"
61 " FLOWLABEL := { 0x0..0xfffff | inherit }\n"
62 " MARK := { 0x0..0xffffffff | inherit }\n",
63 DEFAULT_TNL_HOP_LIMIT
, IPV6_DEFAULT_TNL_ENCAP_LIMIT
67 static void usage(void) __attribute__((noreturn
));
68 static void usage(void)
74 static int gre_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
77 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
82 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
83 .n
.nlmsg_flags
= NLM_F_REQUEST
,
84 .n
.nlmsg_type
= RTM_GETLINK
,
85 .i
.ifi_family
= preferred_family
,
86 .i
.ifi_index
= ifi
->ifi_index
,
88 struct nlmsghdr
*answer
= NULL
;
89 struct rtattr
*tb
[IFLA_MAX
+ 1];
90 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
91 struct rtattr
*greinfo
[IFLA_GRE_MAX
+ 1];
94 unsigned int ikey
= 0;
95 unsigned int okey
= 0;
96 struct in6_addr raddr
= IN6ADDR_ANY_INIT
;
97 struct in6_addr laddr
= IN6ADDR_ANY_INIT
;
98 unsigned int link
= 0;
99 unsigned int flowinfo
= 0;
100 unsigned int flags
= 0;
101 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
102 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
104 __u16 encapflags
= TUNNEL_ENCAP_FLAG_CSUM6
;
105 __u16 encapsport
= 0;
106 __u16 encapdport
= 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");
119 len
= answer
->nlmsg_len
;
120 len
-= NLMSG_LENGTH(sizeof(*ifi
));
124 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
126 if (!tb
[IFLA_LINKINFO
])
129 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
131 if (!linkinfo
[IFLA_INFO_DATA
])
134 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
135 linkinfo
[IFLA_INFO_DATA
]);
137 if (greinfo
[IFLA_GRE_IKEY
])
138 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
140 if (greinfo
[IFLA_GRE_OKEY
])
141 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
143 if (greinfo
[IFLA_GRE_IFLAGS
])
144 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
146 if (greinfo
[IFLA_GRE_OFLAGS
])
147 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
149 if (greinfo
[IFLA_GRE_LOCAL
])
150 memcpy(&laddr
, RTA_DATA(greinfo
[IFLA_GRE_LOCAL
]), sizeof(laddr
));
152 if (greinfo
[IFLA_GRE_REMOTE
])
153 memcpy(&raddr
, RTA_DATA(greinfo
[IFLA_GRE_REMOTE
]), sizeof(raddr
));
155 if (greinfo
[IFLA_GRE_TTL
])
156 hop_limit
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
158 if (greinfo
[IFLA_GRE_LINK
])
159 link
= rta_getattr_u32(greinfo
[IFLA_GRE_LINK
]);
161 if (greinfo
[IFLA_GRE_ENCAP_LIMIT
])
162 encap_limit
= rta_getattr_u8(greinfo
[IFLA_GRE_ENCAP_LIMIT
]);
164 if (greinfo
[IFLA_GRE_FLOWINFO
])
165 flowinfo
= rta_getattr_u32(greinfo
[IFLA_GRE_FLOWINFO
]);
167 if (greinfo
[IFLA_GRE_FLAGS
])
168 flags
= rta_getattr_u32(greinfo
[IFLA_GRE_FLAGS
]);
170 if (greinfo
[IFLA_GRE_ENCAP_TYPE
])
171 encaptype
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_TYPE
]);
173 if (greinfo
[IFLA_GRE_ENCAP_FLAGS
])
174 encapflags
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_FLAGS
]);
176 if (greinfo
[IFLA_GRE_ENCAP_SPORT
])
177 encapsport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_SPORT
]);
179 if (greinfo
[IFLA_GRE_ENCAP_DPORT
])
180 encapdport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_DPORT
]);
182 if (greinfo
[IFLA_GRE_FWMARK
])
183 fwmark
= rta_getattr_u32(greinfo
[IFLA_GRE_FWMARK
]);
189 if (!matches(*argv
, "key")) {
195 if (strchr(*argv
, '.'))
196 uval
= get_addr32(*argv
);
198 if (get_unsigned(&uval
, *argv
, 0) < 0) {
200 "Invalid value for \"key\"\n");
207 } else if (!matches(*argv
, "ikey")) {
212 if (strchr(*argv
, '.'))
213 uval
= get_addr32(*argv
);
215 if (get_unsigned(&uval
, *argv
, 0) < 0) {
216 fprintf(stderr
, "invalid value of \"ikey\"\n");
222 } else if (!matches(*argv
, "okey")) {
227 if (strchr(*argv
, '.'))
228 uval
= get_addr32(*argv
);
230 if (get_unsigned(&uval
, *argv
, 0) < 0) {
231 fprintf(stderr
, "invalid value of \"okey\"\n");
237 } else if (!matches(*argv
, "seq")) {
240 } else if (!matches(*argv
, "iseq")) {
242 } else if (!matches(*argv
, "oseq")) {
244 } else if (!matches(*argv
, "csum")) {
247 } else if (!matches(*argv
, "icsum")) {
249 } else if (!matches(*argv
, "ocsum")) {
251 } else if (!matches(*argv
, "remote")) {
255 get_prefix(&addr
, *argv
, preferred_family
);
256 if (addr
.family
== AF_UNSPEC
)
257 invarg("\"remote\" address family is AF_UNSPEC", *argv
);
258 memcpy(&raddr
, &addr
.data
, sizeof(raddr
));
259 } else if (!matches(*argv
, "local")) {
263 get_prefix(&addr
, *argv
, preferred_family
);
264 if (addr
.family
== AF_UNSPEC
)
265 invarg("\"local\" address family is AF_UNSPEC", *argv
);
266 memcpy(&laddr
, &addr
.data
, sizeof(laddr
));
267 } else if (!matches(*argv
, "dev")) {
269 link
= if_nametoindex(*argv
);
271 fprintf(stderr
, "Cannot find device \"%s\"\n",
275 } else if (!matches(*argv
, "ttl") ||
276 !matches(*argv
, "hoplimit")) {
280 if (get_u8(&uval
, *argv
, 0))
281 invarg("invalid TTL", *argv
);
283 } else if (!matches(*argv
, "tos") ||
284 !matches(*argv
, "tclass") ||
285 !matches(*argv
, "dsfield")) {
289 if (strcmp(*argv
, "inherit") == 0)
290 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
292 if (get_u8(&uval
, *argv
, 16))
293 invarg("invalid TClass", *argv
);
294 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
295 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
296 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
298 } else if (strcmp(*argv
, "flowlabel") == 0 ||
299 strcmp(*argv
, "fl") == 0) {
303 if (strcmp(*argv
, "inherit") == 0)
304 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
306 if (get_u32(&uval
, *argv
, 16))
307 invarg("invalid Flowlabel", *argv
);
309 invarg("invalid Flowlabel", *argv
);
310 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
311 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
312 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
314 } else if (strcmp(*argv
, "dscp") == 0) {
316 if (strcmp(*argv
, "inherit") != 0)
317 invarg("not inherit", *argv
);
318 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
319 } else if (strcmp(*argv
, "noencap") == 0) {
320 encaptype
= TUNNEL_ENCAP_NONE
;
321 } else if (strcmp(*argv
, "encap") == 0) {
323 if (strcmp(*argv
, "fou") == 0)
324 encaptype
= TUNNEL_ENCAP_FOU
;
325 else if (strcmp(*argv
, "gue") == 0)
326 encaptype
= TUNNEL_ENCAP_GUE
;
327 else if (strcmp(*argv
, "none") == 0)
328 encaptype
= TUNNEL_ENCAP_NONE
;
330 invarg("Invalid encap type.", *argv
);
331 } else if (strcmp(*argv
, "encap-sport") == 0) {
333 if (strcmp(*argv
, "auto") == 0)
335 else if (get_u16(&encapsport
, *argv
, 0))
336 invarg("Invalid source port.", *argv
);
337 } else if (strcmp(*argv
, "encap-dport") == 0) {
339 if (get_u16(&encapdport
, *argv
, 0))
340 invarg("Invalid destination port.", *argv
);
341 } else if (strcmp(*argv
, "encap-csum") == 0) {
342 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
343 } else if (strcmp(*argv
, "noencap-csum") == 0) {
344 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
345 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
346 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
347 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
348 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
349 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
350 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
351 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
352 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
353 } else if (strcmp(*argv
, "fwmark") == 0) {
355 if (strcmp(*argv
, "inherit") == 0) {
356 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
359 if (get_u32(&fwmark
, *argv
, 0))
360 invarg("invalid fwmark\n", *argv
);
361 flags
&= ~IP6_TNL_F_USE_ORIG_FWMARK
;
363 } else if (strcmp(*argv
, "encaplimit") == 0) {
365 if (strcmp(*argv
, "none") == 0) {
366 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
370 if (get_u8(&uval
, *argv
, 0) < -1)
371 invarg("invalid ELIM", *argv
);
373 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
380 addattr32(n
, 1024, IFLA_GRE_IKEY
, ikey
);
381 addattr32(n
, 1024, IFLA_GRE_OKEY
, okey
);
382 addattr_l(n
, 1024, IFLA_GRE_IFLAGS
, &iflags
, 2);
383 addattr_l(n
, 1024, IFLA_GRE_OFLAGS
, &oflags
, 2);
384 addattr_l(n
, 1024, IFLA_GRE_LOCAL
, &laddr
, sizeof(laddr
));
385 addattr_l(n
, 1024, IFLA_GRE_REMOTE
, &raddr
, sizeof(raddr
));
387 addattr32(n
, 1024, IFLA_GRE_LINK
, link
);
388 addattr_l(n
, 1024, IFLA_GRE_TTL
, &hop_limit
, 1);
389 addattr_l(n
, 1024, IFLA_GRE_ENCAP_LIMIT
, &encap_limit
, 1);
390 addattr_l(n
, 1024, IFLA_GRE_FLOWINFO
, &flowinfo
, 4);
391 addattr32(n
, 1024, IFLA_GRE_FLAGS
, flags
);
392 addattr32(n
, 1024, IFLA_GRE_FWMARK
, fwmark
);
394 addattr16(n
, 1024, IFLA_GRE_ENCAP_TYPE
, encaptype
);
395 addattr16(n
, 1024, IFLA_GRE_ENCAP_FLAGS
, encapflags
);
396 addattr16(n
, 1024, IFLA_GRE_ENCAP_SPORT
, htons(encapsport
));
397 addattr16(n
, 1024, IFLA_GRE_ENCAP_DPORT
, htons(encapdport
));
402 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
405 const char *local
= "any";
406 const char *remote
= "any";
407 unsigned int iflags
= 0;
408 unsigned int oflags
= 0;
409 unsigned int flags
= 0;
410 unsigned int flowinfo
= 0;
411 struct in6_addr in6_addr_any
= IN6ADDR_ANY_INIT
;
416 if (tb
[IFLA_GRE_FLAGS
])
417 flags
= rta_getattr_u32(tb
[IFLA_GRE_FLAGS
]);
419 if (tb
[IFLA_GRE_FLOWINFO
])
420 flowinfo
= rta_getattr_u32(tb
[IFLA_GRE_FLOWINFO
]);
422 if (tb
[IFLA_GRE_REMOTE
]) {
423 struct in6_addr addr
;
425 memcpy(&addr
, RTA_DATA(tb
[IFLA_GRE_REMOTE
]), sizeof(addr
));
427 if (memcmp(&addr
, &in6_addr_any
, sizeof(addr
)))
428 remote
= format_host(AF_INET6
, sizeof(addr
), &addr
);
431 print_string(PRINT_ANY
, "remote", "remote %s ", remote
);
433 if (tb
[IFLA_GRE_LOCAL
]) {
434 struct in6_addr addr
;
436 memcpy(&addr
, RTA_DATA(tb
[IFLA_GRE_LOCAL
]), sizeof(addr
));
438 if (memcmp(&addr
, &in6_addr_any
, sizeof(addr
)))
439 local
= format_host(AF_INET6
, sizeof(addr
), &addr
);
442 print_string(PRINT_ANY
, "local", "local %s ", local
);
444 if (tb
[IFLA_GRE_LINK
] && rta_getattr_u32(tb
[IFLA_GRE_LINK
])) {
445 unsigned int link
= rta_getattr_u32(tb
[IFLA_GRE_LINK
]);
446 const char *n
= if_indextoname(link
, s2
);
449 print_string(PRINT_ANY
, "link", "dev %s ", n
);
451 print_uint(PRINT_ANY
, "link_index", "dev %u ", link
);
454 if (tb
[IFLA_GRE_TTL
]) {
455 __u8 ttl
= rta_getattr_u8(tb
[IFLA_GRE_TTL
]);
458 print_int(PRINT_ANY
, "ttl", "hoplimit %d ", ttl
);
460 print_int(PRINT_JSON
, "ttl", NULL
, ttl
);
463 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
464 print_bool(PRINT_ANY
,
465 "ip6_tnl_f_ign_encap_limit",
468 else if (tb
[IFLA_GRE_ENCAP_LIMIT
]) {
469 int encap_limit
= rta_getattr_u8(tb
[IFLA_GRE_ENCAP_LIMIT
]);
477 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
) {
478 print_bool(PRINT_ANY
,
479 "ip6_tnl_f_use_orig_flowlabel",
480 "flowlabel inherit ",
483 if (is_json_context()) {
486 snprintf(b1
, sizeof(b1
), "0x%05x",
487 ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
488 print_string(PRINT_JSON
, "flowlabel", NULL
, b1
);
491 fprintf(f
, "flowlabel 0x%05x ",
492 ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
496 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
) {
497 print_bool(PRINT_ANY
,
498 "ip6_tnl_f_use_orig_tclass",
502 if (is_json_context()) {
505 snprintf(b1
, sizeof(b1
), "0x%05x",
506 ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
) >> 20);
507 print_string(PRINT_JSON
, "tclass", NULL
, b1
);
509 fprintf(f
, "tclass 0x%02x ",
510 ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
) >> 20);
514 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
515 print_bool(PRINT_ANY
,
516 "ip6_tnl_f_rcv_dscp_copy",
520 if (tb
[IFLA_GRE_IFLAGS
])
521 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
523 if (tb
[IFLA_GRE_OFLAGS
])
524 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
526 if ((iflags
& GRE_KEY
) && tb
[IFLA_GRE_IKEY
]) {
527 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_IKEY
]), s2
, sizeof(s2
));
528 print_string(PRINT_ANY
, "ikey", "ikey %s ", s2
);
531 if ((oflags
& GRE_KEY
) && tb
[IFLA_GRE_OKEY
]) {
532 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_OKEY
]), s2
, sizeof(s2
));
533 print_string(PRINT_ANY
, "okey", "okey %s ", s2
);
536 if (iflags
& GRE_SEQ
)
537 print_bool(PRINT_ANY
, "iseq", "iseq ", true);
538 if (oflags
& GRE_SEQ
)
539 print_bool(PRINT_ANY
, "oseq", "oseq ", true);
540 if (iflags
& GRE_CSUM
)
541 print_bool(PRINT_ANY
, "icsum", "icsum ", true);
542 if (oflags
& GRE_CSUM
)
543 print_bool(PRINT_ANY
, "ocsum", "ocsum ", true);
545 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
546 print_bool(PRINT_ANY
,
547 "ip6_tnl_f_use_orig_fwmark",
550 else if (tb
[IFLA_GRE_FWMARK
]) {
551 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_GRE_FWMARK
]);
554 snprintf(s2
, sizeof(s2
), "0x%x", fwmark
);
556 print_string(PRINT_ANY
, "fwmark", "fwmark %s ", s2
);
560 if (tb
[IFLA_GRE_ENCAP_TYPE
] &&
561 rta_getattr_u16(tb
[IFLA_GRE_ENCAP_TYPE
]) != TUNNEL_ENCAP_NONE
) {
562 __u16 type
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_TYPE
]);
563 __u16 flags
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_FLAGS
]);
564 __u16 sport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_SPORT
]);
565 __u16 dport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_DPORT
]);
567 open_json_object("encap");
569 print_string(PRINT_FP
, NULL
, "encap ", NULL
);
571 case TUNNEL_ENCAP_FOU
:
572 print_string(PRINT_ANY
, "type", "%s ", "fou");
574 case TUNNEL_ENCAP_GUE
:
575 print_string(PRINT_ANY
, "type", "%s ", "gue");
578 print_null(PRINT_ANY
, "type", "unknown ", NULL
);
582 if (is_json_context()) {
583 print_uint(PRINT_JSON
,
586 sport
? ntohs(sport
) : 0);
587 print_uint(PRINT_JSON
, "dport", NULL
, ntohs(dport
));
588 print_bool(PRINT_JSON
, "csum", NULL
,
589 flags
& TUNNEL_ENCAP_FLAG_CSUM
);
590 print_bool(PRINT_JSON
, "csum6", NULL
,
591 flags
& TUNNEL_ENCAP_FLAG_CSUM6
);
592 print_bool(PRINT_JSON
, "remcsum", NULL
,
593 flags
& TUNNEL_ENCAP_FLAG_REMCSUM
);
597 fputs("encap-sport auto ", f
);
599 fprintf(f
, "encap-sport %u", ntohs(sport
));
601 fprintf(f
, "encap-dport %u ", ntohs(dport
));
603 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
604 fputs("encap-csum ", f
);
606 fputs("noencap-csum ", f
);
608 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
609 fputs("encap-csum6 ", f
);
611 fputs("noencap-csum6 ", f
);
613 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
614 fputs("encap-remcsum ", f
);
616 fputs("noencap-remcsum ", f
);
621 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
627 struct link_util ip6gre_link_util
= {
629 .maxattr
= IFLA_GRE_MAX
,
630 .parse_opt
= gre_parse_opt
,
631 .print_opt
= gre_print_opt
,
632 .print_help
= gre_print_help
,
635 struct link_util ip6gretap_link_util
= {
637 .maxattr
= IFLA_GRE_MAX
,
638 .parse_opt
= gre_parse_opt
,
639 .print_opt
= gre_print_opt
,
640 .print_help
= gre_print_help
,