2 * link_ip6tnl.c ip6tnl 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: Nicolas Dichtel <nicolas.dichtel@6wind.com>
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>
24 #include "ip_common.h"
27 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
28 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
30 #define DEFAULT_TNL_HOP_LIMIT (64)
32 static void print_usage(FILE *f
)
35 "Usage: ... ip6tnl [ mode { ip6ip6 | ipip6 | any } ]\n"
39 " [ encaplimit ELIM ]\n"
40 " [ hoplimit HLIM ]\n"
41 " [ tclass TCLASS ]\n"
42 " [ flowlabel FLOWLABEL ]\n"
45 " [ [no]allow-localremote ]\n"
47 " [ encap { fou | gue | none } ]\n"
48 " [ encap-sport PORT ]\n"
49 " [ encap-dport PORT ]\n"
50 " [ [no]encap-csum ]\n"
51 " [ [no]encap-csum6 ]\n"
52 " [ [no]encap-remcsum ]\n"
55 "Where: ADDR := IPV6_ADDRESS\n"
56 " ELIM := { none | 0..255 }(default=%d)\n"
57 " HLIM := 0..255 (default=%d)\n"
58 " TCLASS := { 0x0..0xff | inherit }\n"
59 " FLOWLABEL := { 0x0..0xfffff | inherit }\n"
60 " MARK := { 0x0..0xffffffff | inherit }\n",
61 IPV6_DEFAULT_TNL_ENCAP_LIMIT
, DEFAULT_TNL_HOP_LIMIT
65 static void usage(void) __attribute__((noreturn
));
66 static void usage(void)
72 static int ip6tunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
75 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
80 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
81 .n
.nlmsg_flags
= NLM_F_REQUEST
,
82 .n
.nlmsg_type
= RTM_GETLINK
,
83 .i
.ifi_family
= preferred_family
,
84 .i
.ifi_index
= ifi
->ifi_index
,
86 struct nlmsghdr
*answer
;
87 struct rtattr
*tb
[IFLA_MAX
+ 1];
88 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
89 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
91 struct in6_addr laddr
= IN6ADDR_ANY_INIT
;
92 struct in6_addr raddr
= IN6ADDR_ANY_INIT
;
93 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
94 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
100 __u16 encapflags
= TUNNEL_ENCAP_FLAG_CSUM6
;
101 __u16 encapsport
= 0;
102 __u16 encapdport
= 0;
106 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
107 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
110 "Failed to get existing tunnel info.\n");
114 len
= answer
->nlmsg_len
;
115 len
-= NLMSG_LENGTH(sizeof(*ifi
));
119 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
121 if (!tb
[IFLA_LINKINFO
])
124 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
126 if (!linkinfo
[IFLA_INFO_DATA
])
129 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
130 linkinfo
[IFLA_INFO_DATA
]);
132 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
133 memcpy(&laddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_LOCAL
]),
136 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
137 memcpy(&raddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_REMOTE
]),
140 if (iptuninfo
[IFLA_IPTUN_TTL
])
141 hop_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
143 if (iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
])
144 encap_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
]);
146 if (iptuninfo
[IFLA_IPTUN_FLOWINFO
])
147 flowinfo
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLOWINFO
]);
149 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
150 flags
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLAGS
]);
152 if (iptuninfo
[IFLA_IPTUN_LINK
])
153 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
155 if (iptuninfo
[IFLA_IPTUN_PROTO
])
156 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
157 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
160 if (iptuninfo
[IFLA_IPTUN_FWMARK
])
161 fwmark
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FWMARK
]);
167 if (matches(*argv
, "mode") == 0) {
169 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
170 strcmp(*argv
, "ip6ip6") == 0)
171 proto
= IPPROTO_IPV6
;
172 else if (strcmp(*argv
, "ip/ipv6") == 0 ||
173 strcmp(*argv
, "ipv4/ipv6") == 0 ||
174 strcmp(*argv
, "ipip6") == 0 ||
175 strcmp(*argv
, "ip4ip6") == 0)
176 proto
= IPPROTO_IPIP
;
177 else if (strcmp(*argv
, "any/ipv6") == 0 ||
178 strcmp(*argv
, "any") == 0)
181 invarg("Cannot guess tunnel mode.", *argv
);
182 } else if (strcmp(*argv
, "remote") == 0) {
186 get_addr(&addr
, *argv
, AF_INET6
);
187 memcpy(&raddr
, addr
.data
, sizeof(raddr
));
188 } else if (strcmp(*argv
, "local") == 0) {
192 get_addr(&addr
, *argv
, AF_INET6
);
193 memcpy(&laddr
, addr
.data
, sizeof(laddr
));
194 } else if (matches(*argv
, "dev") == 0) {
196 link
= if_nametoindex(*argv
);
198 invarg("\"dev\" is invalid", *argv
);
199 } else if (strcmp(*argv
, "hoplimit") == 0 ||
200 strcmp(*argv
, "ttl") == 0 ||
201 strcmp(*argv
, "hlim") == 0) {
205 if (get_u8(&uval
, *argv
, 0))
206 invarg("invalid HLIM", *argv
);
208 } else if (strcmp(*argv
, "encaplimit") == 0) {
210 if (strcmp(*argv
, "none") == 0) {
211 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
215 if (get_u8(&uval
, *argv
, 0) < -1)
216 invarg("invalid ELIM", *argv
);
218 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
220 } else if (strcmp(*argv
, "tclass") == 0 ||
221 strcmp(*argv
, "tc") == 0 ||
222 strcmp(*argv
, "tos") == 0 ||
223 matches(*argv
, "dsfield") == 0) {
227 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
228 if (strcmp(*argv
, "inherit") == 0)
229 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
231 if (get_u8(&uval
, *argv
, 16))
232 invarg("invalid TClass", *argv
);
233 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
234 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
236 } else if (strcmp(*argv
, "flowlabel") == 0 ||
237 strcmp(*argv
, "fl") == 0) {
241 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
242 if (strcmp(*argv
, "inherit") == 0)
243 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
245 if (get_u32(&uval
, *argv
, 16))
246 invarg("invalid Flowlabel", *argv
);
248 invarg("invalid Flowlabel", *argv
);
249 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
250 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
252 } else if (strcmp(*argv
, "dscp") == 0) {
254 if (strcmp(*argv
, "inherit") != 0)
255 invarg("not inherit", *argv
);
256 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
257 } else if (strcmp(*argv
, "fwmark") == 0) {
259 if (strcmp(*argv
, "inherit") == 0) {
260 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
263 if (get_u32(&fwmark
, *argv
, 0))
264 invarg("invalid fwmark\n", *argv
);
265 flags
&= ~IP6_TNL_F_USE_ORIG_FWMARK
;
267 } else if (strcmp(*argv
, "allow-localremote") == 0) {
268 flags
|= IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
269 } else if (strcmp(*argv
, "noallow-localremote") == 0) {
270 flags
&= ~IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
271 } else if (strcmp(*argv
, "noencap") == 0) {
272 encaptype
= TUNNEL_ENCAP_NONE
;
273 } else if (strcmp(*argv
, "encap") == 0) {
275 if (strcmp(*argv
, "fou") == 0)
276 encaptype
= TUNNEL_ENCAP_FOU
;
277 else if (strcmp(*argv
, "gue") == 0)
278 encaptype
= TUNNEL_ENCAP_GUE
;
279 else if (strcmp(*argv
, "none") == 0)
280 encaptype
= TUNNEL_ENCAP_NONE
;
282 invarg("Invalid encap type.", *argv
);
283 } else if (strcmp(*argv
, "encap-sport") == 0) {
285 if (strcmp(*argv
, "auto") == 0)
287 else if (get_u16(&encapsport
, *argv
, 0))
288 invarg("Invalid source port.", *argv
);
289 } else if (strcmp(*argv
, "encap-dport") == 0) {
291 if (get_u16(&encapdport
, *argv
, 0))
292 invarg("Invalid destination port.", *argv
);
293 } else if (strcmp(*argv
, "encap-csum") == 0) {
294 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
295 } else if (strcmp(*argv
, "noencap-csum") == 0) {
296 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
297 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
298 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
299 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
300 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
301 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
302 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
303 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
304 encapflags
|= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
305 } else if (strcmp(*argv
, "external") == 0) {
312 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
314 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
317 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
, &laddr
, sizeof(laddr
));
318 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
, &raddr
, sizeof(raddr
));
319 addattr8(n
, 1024, IFLA_IPTUN_TTL
, hop_limit
);
320 addattr8(n
, 1024, IFLA_IPTUN_ENCAP_LIMIT
, encap_limit
);
321 addattr32(n
, 1024, IFLA_IPTUN_FLOWINFO
, flowinfo
);
322 addattr32(n
, 1024, IFLA_IPTUN_FLAGS
, flags
);
323 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
324 addattr32(n
, 1024, IFLA_IPTUN_FWMARK
, fwmark
);
326 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
327 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
328 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
329 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
334 static void ip6tunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
343 if (tb
[IFLA_IPTUN_COLLECT_METADATA
])
344 print_bool(PRINT_ANY
, "external", "external ", true);
346 if (tb
[IFLA_IPTUN_FLAGS
])
347 flags
= rta_getattr_u32(tb
[IFLA_IPTUN_FLAGS
]);
349 if (tb
[IFLA_IPTUN_FLOWINFO
])
350 flowinfo
= rta_getattr_u32(tb
[IFLA_IPTUN_FLOWINFO
]);
352 if (tb
[IFLA_IPTUN_PROTO
]) {
353 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
355 print_string(PRINT_ANY
, "proto", "%s ", "ipip6");
358 print_string(PRINT_ANY
, "proto", "%s ", "ip6ip6");
361 print_string(PRINT_ANY
, "proto", "%s ", "any");
366 if (tb
[IFLA_IPTUN_REMOTE
]) {
367 print_string(PRINT_ANY
,
370 rt_addr_n2a_rta(AF_INET6
, tb
[IFLA_IPTUN_REMOTE
]));
373 if (tb
[IFLA_IPTUN_LOCAL
]) {
374 print_string(PRINT_ANY
,
377 rt_addr_n2a_rta(AF_INET6
, tb
[IFLA_IPTUN_LOCAL
]));
380 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
381 unsigned int link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
382 const char *n
= if_indextoname(link
, s2
);
385 print_string(PRINT_ANY
, "link", "dev %s ", n
);
387 print_uint(PRINT_ANY
, "link_index", "dev %u ", link
);
390 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
391 print_bool(PRINT_ANY
,
392 "ip6_tnl_f_ign_encap_limit",
395 else if (tb
[IFLA_IPTUN_ENCAP_LIMIT
])
396 print_uint(PRINT_ANY
,
399 rta_getattr_u8(tb
[IFLA_IPTUN_ENCAP_LIMIT
]));
401 if (tb
[IFLA_IPTUN_TTL
])
402 print_uint(PRINT_ANY
,
405 rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
407 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
408 print_bool(PRINT_ANY
,
409 "ip6_tnl_f_use_orig_tclass",
412 else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
413 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
);
415 if (is_json_context()) {
418 snprintf(b1
, sizeof(b1
), "0x%02x", (__u8
)(val
>> 20));
419 print_string(PRINT_JSON
, "flowinfo_tclass", NULL
, b1
);
421 printf("tclass 0x%02x ", (__u8
)(val
>> 20));
425 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
) {
426 print_bool(PRINT_ANY
,
427 "ip6_tnl_f_use_orig_flowlabel",
428 "flowlabel inherit ",
431 if (is_json_context()) {
434 snprintf(b1
, sizeof(b1
), "0x%05x",
435 ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
436 print_string(PRINT_JSON
, "flowlabel", NULL
, b1
);
438 printf("flowlabel 0x%05x ",
439 ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
443 if (is_json_context()) {
446 snprintf(flwinfo
, sizeof(flwinfo
), "0x%08x", ntohl(flowinfo
));
447 print_string(PRINT_JSON
, "flowinfo", NULL
, flwinfo
);
449 printf("(flowinfo 0x%08x) ", ntohl(flowinfo
));
453 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
454 print_bool(PRINT_ANY
,
455 "ip6_tnl_f_rcv_dscp_copy",
459 if (flags
& IP6_TNL_F_MIP6_DEV
)
460 print_bool(PRINT_ANY
, "ip6_tnl_f_mip6_dev", "mip6 ", true);
462 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
) {
463 print_bool(PRINT_ANY
,
464 "ip6_tnl_f_use_orig_fwmark",
467 } else if (tb
[IFLA_IPTUN_FWMARK
]) {
468 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_IPTUN_FWMARK
]);
473 snprintf(b1
, sizeof(b1
), "0x%x", fwmark
);
474 print_string(PRINT_ANY
, "fwmark", "fwmark %s ", b1
);
478 if (flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
)
479 print_bool(PRINT_ANY
,
480 "ip6_tnl_f_allow_local_remote",
481 "allow-localremote ",
484 if (tb
[IFLA_IPTUN_ENCAP_TYPE
] &&
485 rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
]) != TUNNEL_ENCAP_NONE
) {
486 __u16 type
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
]);
487 __u16 flags
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_FLAGS
]);
488 __u16 sport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_SPORT
]);
489 __u16 dport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_DPORT
]);
491 open_json_object("encap");
492 print_string(PRINT_FP
, NULL
, "encap ", NULL
);
494 case TUNNEL_ENCAP_FOU
:
495 print_string(PRINT_ANY
, "type", "%s ", "fou");
497 case TUNNEL_ENCAP_GUE
:
498 print_string(PRINT_ANY
, "type", "%s ", "gue");
501 print_null(PRINT_ANY
, "type", "unknown ", NULL
);
505 if (is_json_context()) {
506 print_uint(PRINT_JSON
,
509 sport
? ntohs(sport
) : 0);
510 print_uint(PRINT_JSON
, "dport", NULL
, ntohs(dport
));
511 print_bool(PRINT_JSON
, "csum", NULL
,
512 flags
& TUNNEL_ENCAP_FLAG_CSUM
);
513 print_bool(PRINT_JSON
, "csum6", NULL
,
514 flags
& TUNNEL_ENCAP_FLAG_CSUM6
);
515 print_bool(PRINT_JSON
, "remcsum", NULL
,
516 flags
& TUNNEL_ENCAP_FLAG_REMCSUM
);
520 fputs("encap-sport auto ", f
);
522 fprintf(f
, "encap-sport %u", ntohs(sport
));
524 fprintf(f
, "encap-dport %u ", ntohs(dport
));
526 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
527 fputs("encap-csum ", f
);
529 fputs("noencap-csum ", f
);
531 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
532 fputs("encap-csum6 ", f
);
534 fputs("noencap-csum6 ", f
);
536 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
537 fputs("encap-remcsum ", f
);
539 fputs("noencap-remcsum ", f
);
544 static void ip6tunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
550 struct link_util ip6tnl_link_util
= {
552 .maxattr
= IFLA_IPTUN_MAX
,
553 .parse_opt
= ip6tunnel_parse_opt
,
554 .print_opt
= ip6tunnel_print_opt
,
555 .print_help
= ip6tunnel_print_help
,