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 ip6tunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
36 "Usage: ... %-6s [ remote ADDR ]\n"
38 " [ encaplimit ELIM ]\n"
39 " [ hoplimit HLIM ]\n"
40 " [ tclass TCLASS ]\n"
41 " [ flowlabel FLOWLABEL ]\n"
43 " [ [no]allow-localremote ]\n"
48 " [ encap { fou | gue | none } ]\n"
49 " [ encap-sport PORT ]\n"
50 " [ encap-dport PORT ]\n"
51 " [ [no]encap-csum ]\n"
52 " [ [no]encap-csum6 ]\n"
53 " [ [no]encap-remcsum ]\n"
54 " [ mode { ip6ip6 | ipip6 | any } ]\n"
56 "Where: ADDR := IPV6_ADDRESS\n"
57 " ELIM := { none | 0..255 }(default=%d)\n"
58 " HLIM := 0..255 (default=%d)\n"
59 " TCLASS := { 0x0..0xff | inherit }\n"
60 " FLOWLABEL := { 0x0..0xfffff | inherit }\n"
61 " MARK := { 0x0..0xffffffff | inherit }\n",
63 IPV6_DEFAULT_TNL_ENCAP_LIMIT
, DEFAULT_TNL_HOP_LIMIT
);
66 static int ip6tunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
69 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
74 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
75 .n
.nlmsg_flags
= NLM_F_REQUEST
,
76 .n
.nlmsg_type
= RTM_GETLINK
,
77 .i
.ifi_family
= preferred_family
,
78 .i
.ifi_index
= ifi
->ifi_index
,
80 struct nlmsghdr
*answer
;
81 struct rtattr
*tb
[IFLA_MAX
+ 1];
82 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
83 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
85 inet_prefix saddr
, daddr
;
86 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
87 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
93 __u16 encapflags
= TUNNEL_ENCAP_FLAG_CSUM6
;
99 inet_prefix_reset(&saddr
);
100 inet_prefix_reset(&daddr
);
102 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
103 const struct rtattr
*rta
;
105 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
108 "Failed to get existing tunnel info.\n");
112 len
= answer
->nlmsg_len
;
113 len
-= NLMSG_LENGTH(sizeof(*ifi
));
117 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
119 if (!tb
[IFLA_LINKINFO
])
122 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
124 if (!linkinfo
[IFLA_INFO_DATA
])
127 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
128 linkinfo
[IFLA_INFO_DATA
]);
130 rta
= iptuninfo
[IFLA_IPTUN_LOCAL
];
131 if (rta
&& get_addr_rta(&saddr
, rta
, AF_INET6
))
134 rta
= iptuninfo
[IFLA_IPTUN_REMOTE
];
135 if (rta
&& get_addr_rta(&daddr
, rta
, AF_INET6
))
138 if (iptuninfo
[IFLA_IPTUN_TTL
])
139 hop_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
141 if (iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
])
142 encap_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
]);
144 if (iptuninfo
[IFLA_IPTUN_FLOWINFO
])
145 flowinfo
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLOWINFO
]);
147 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
148 flags
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLAGS
]);
150 if (iptuninfo
[IFLA_IPTUN_LINK
])
151 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
153 if (iptuninfo
[IFLA_IPTUN_PROTO
])
154 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
155 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
158 if (iptuninfo
[IFLA_IPTUN_FWMARK
])
159 fwmark
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FWMARK
]);
165 if (strcmp(*argv
, "mode") == 0) {
167 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
168 strcmp(*argv
, "ip6ip6") == 0)
169 proto
= IPPROTO_IPV6
;
170 else if (strcmp(*argv
, "ip/ipv6") == 0 ||
171 strcmp(*argv
, "ipv4/ipv6") == 0 ||
172 strcmp(*argv
, "ipip6") == 0 ||
173 strcmp(*argv
, "ip4ip6") == 0)
174 proto
= IPPROTO_IPIP
;
175 else if (strcmp(*argv
, "any/ipv6") == 0 ||
176 strcmp(*argv
, "any") == 0)
179 invarg("Cannot guess tunnel mode.", *argv
);
180 } else if (strcmp(*argv
, "remote") == 0) {
182 get_addr(&daddr
, *argv
, AF_INET6
);
183 } else if (strcmp(*argv
, "local") == 0) {
185 get_addr(&saddr
, *argv
, AF_INET6
);
186 } else if (matches(*argv
, "dev") == 0) {
188 link
= ll_name_to_index(*argv
);
191 } else if (strcmp(*argv
, "ttl") == 0 ||
192 strcmp(*argv
, "hoplimit") == 0 ||
193 strcmp(*argv
, "hlim") == 0) {
195 if (strcmp(*argv
, "inherit") != 0) {
196 if (get_u8(&hop_limit
, *argv
, 0))
197 invarg("invalid HLIM\n", *argv
);
200 } else if (strcmp(*argv
, "encaplimit") == 0) {
202 if (strcmp(*argv
, "none") == 0) {
203 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
207 if (get_u8(&uval
, *argv
, 0) < -1)
208 invarg("invalid ELIM", *argv
);
210 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
212 } else if (strcmp(*argv
, "tos") == 0 ||
213 strcmp(*argv
, "tclass") == 0 ||
214 strcmp(*argv
, "tc") == 0 ||
215 matches(*argv
, "dsfield") == 0) {
219 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
220 if (strcmp(*argv
, "inherit") == 0)
221 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
223 if (get_u8(&uval
, *argv
, 16))
224 invarg("invalid TClass", *argv
);
225 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
226 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
228 } else if (strcmp(*argv
, "flowlabel") == 0 ||
229 strcmp(*argv
, "fl") == 0) {
233 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
234 if (strcmp(*argv
, "inherit") == 0)
235 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
237 if (get_u32(&uval
, *argv
, 16))
238 invarg("invalid Flowlabel", *argv
);
240 invarg("invalid Flowlabel", *argv
);
241 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
242 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
244 } else if (strcmp(*argv
, "dscp") == 0) {
246 if (strcmp(*argv
, "inherit") != 0)
247 invarg("not inherit", *argv
);
248 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
249 } else if (strcmp(*argv
, "fwmark") == 0) {
251 if (strcmp(*argv
, "inherit") == 0) {
252 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
255 if (get_u32(&fwmark
, *argv
, 0))
256 invarg("invalid fwmark\n", *argv
);
257 flags
&= ~IP6_TNL_F_USE_ORIG_FWMARK
;
259 } else if (strcmp(*argv
, "allow-localremote") == 0) {
260 flags
|= IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
261 } else if (strcmp(*argv
, "noallow-localremote") == 0) {
262 flags
&= ~IP6_TNL_F_ALLOW_LOCAL_REMOTE
;
263 } else if (strcmp(*argv
, "noencap") == 0) {
264 encaptype
= TUNNEL_ENCAP_NONE
;
265 } else if (strcmp(*argv
, "encap") == 0) {
267 if (strcmp(*argv
, "fou") == 0)
268 encaptype
= TUNNEL_ENCAP_FOU
;
269 else if (strcmp(*argv
, "gue") == 0)
270 encaptype
= TUNNEL_ENCAP_GUE
;
271 else if (strcmp(*argv
, "none") == 0)
272 encaptype
= TUNNEL_ENCAP_NONE
;
274 invarg("Invalid encap type.", *argv
);
275 } else if (strcmp(*argv
, "encap-sport") == 0) {
277 if (strcmp(*argv
, "auto") == 0)
279 else if (get_u16(&encapsport
, *argv
, 0))
280 invarg("Invalid source port.", *argv
);
281 } else if (strcmp(*argv
, "encap-dport") == 0) {
283 if (get_u16(&encapdport
, *argv
, 0))
284 invarg("Invalid destination port.", *argv
);
285 } else if (strcmp(*argv
, "encap-csum") == 0) {
286 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
287 } else if (strcmp(*argv
, "noencap-csum") == 0) {
288 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
289 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
290 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
291 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
292 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
293 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
294 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
295 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
296 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
297 } else if (strcmp(*argv
, "external") == 0) {
300 ip6tunnel_print_help(lu
, argc
, argv
, stderr
);
306 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
308 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
312 if (is_addrtype_inet_not_unspec(&saddr
)) {
313 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
,
314 saddr
.data
, saddr
.bytelen
);
316 if (is_addrtype_inet_not_unspec(&daddr
)) {
317 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
,
318 daddr
.data
, daddr
.bytelen
);
320 addattr8(n
, 1024, IFLA_IPTUN_TTL
, hop_limit
);
321 addattr8(n
, 1024, IFLA_IPTUN_ENCAP_LIMIT
, encap_limit
);
322 addattr32(n
, 1024, IFLA_IPTUN_FLOWINFO
, flowinfo
);
323 addattr32(n
, 1024, IFLA_IPTUN_FLAGS
, flags
);
324 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
325 addattr32(n
, 1024, IFLA_IPTUN_FWMARK
, fwmark
);
327 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
328 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
329 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
330 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
335 static void ip6tunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
345 if (tb
[IFLA_IPTUN_COLLECT_METADATA
]) {
346 print_bool(PRINT_ANY
, "external", "external ", true);
350 if (tb
[IFLA_IPTUN_FLAGS
])
351 flags
= rta_getattr_u32(tb
[IFLA_IPTUN_FLAGS
]);
353 if (tb
[IFLA_IPTUN_FLOWINFO
])
354 flowinfo
= rta_getattr_u32(tb
[IFLA_IPTUN_FLOWINFO
]);
356 if (tb
[IFLA_IPTUN_PROTO
]) {
357 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
359 print_string(PRINT_ANY
, "proto", "%s ", "ipip6");
362 print_string(PRINT_ANY
, "proto", "%s ", "ip6ip6");
365 print_string(PRINT_ANY
, "proto", "%s ", "any");
370 tnl_print_endpoint("remote", tb
[IFLA_IPTUN_REMOTE
], AF_INET6
);
371 tnl_print_endpoint("local", tb
[IFLA_IPTUN_LOCAL
], AF_INET6
);
373 if (tb
[IFLA_IPTUN_LINK
]) {
374 __u32 link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
377 print_string(PRINT_ANY
, "link", "dev %s ",
378 ll_index_to_name(link
));
382 if (tb
[IFLA_IPTUN_TTL
])
383 ttl
= rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]);
384 if (is_json_context() || ttl
)
385 print_uint(PRINT_ANY
, "ttl", "hoplimit %u ", ttl
);
387 print_string(PRINT_FP
, NULL
, "hoplimit %s ", "inherit");
389 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
) {
390 print_bool(PRINT_ANY
,
391 "ip6_tnl_f_ign_encap_limit",
394 } else if (tb
[IFLA_IPTUN_ENCAP_LIMIT
]) {
395 __u8 val
= rta_getattr_u8(tb
[IFLA_IPTUN_ENCAP_LIMIT
]);
397 print_uint(PRINT_ANY
, "encap_limit", "encaplimit %u ", val
);
400 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
) {
401 print_bool(PRINT_ANY
,
402 "ip6_tnl_f_use_orig_tclass",
405 } else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
406 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
) >> 20;
408 snprintf(s2
, sizeof(s2
), "0x%02x", val
);
409 print_string(PRINT_ANY
, "tclass", "tclass %s ", s2
);
412 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
) {
413 print_bool(PRINT_ANY
,
414 "ip6_tnl_f_use_orig_flowlabel",
415 "flowlabel inherit ",
417 } else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
418 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
);
420 snprintf(s2
, sizeof(s2
), "0x%05x", val
);
421 print_string(PRINT_ANY
, "flowlabel", "flowlabel %s ", s2
);
424 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
425 print_bool(PRINT_ANY
,
426 "ip6_tnl_f_rcv_dscp_copy",
430 if (flags
& IP6_TNL_F_MIP6_DEV
)
431 print_bool(PRINT_ANY
, "ip6_tnl_f_mip6_dev", "mip6 ", true);
433 if (flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
)
434 print_bool(PRINT_ANY
,
435 "ip6_tnl_f_allow_local_remote",
436 "allow-localremote ",
439 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
) {
440 print_bool(PRINT_ANY
,
441 "ip6_tnl_f_use_orig_fwmark",
444 } else if (tb
[IFLA_IPTUN_FWMARK
]) {
445 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_IPTUN_FWMARK
]);
448 print_0xhex(PRINT_ANY
,
449 "fwmark", "fwmark %#llx ", fwmark
);
454 IFLA_IPTUN_ENCAP_TYPE
,
455 IFLA_IPTUN_ENCAP_FLAGS
,
456 IFLA_IPTUN_ENCAP_SPORT
,
457 IFLA_IPTUN_ENCAP_DPORT
);
460 struct link_util ip6tnl_link_util
= {
462 .maxattr
= IFLA_IPTUN_MAX
,
463 .parse_opt
= ip6tunnel_parse_opt
,
464 .print_opt
= ip6tunnel_print_opt
,
465 .print_help
= ip6tunnel_print_help
,