]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_ip6tnl.c
c7b49b02aa099642b660a0377da87df978b899fd
[mirror_iproute2.git] / ip / link_ip6tnl.c
1 /*
2 * link_ip6tnl.c ip6tnl driver module
3 *
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.
8 *
9 * Authors: Nicolas Dichtel <nicolas.dichtel@6wind.com>
10 *
11 */
12
13 #include <string.h>
14 #include <net/if.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
18
19 #include <linux/ip.h>
20 #include <linux/if_tunnel.h>
21 #include <linux/ip6_tunnel.h>
22 #include "rt_names.h"
23 #include "utils.h"
24 #include "ip_common.h"
25 #include "tunnel.h"
26
27 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
28 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
29
30 #define DEFAULT_TNL_HOP_LIMIT (64)
31
32 static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv,
33 FILE *f)
34 {
35 fprintf(f,
36 "Usage: ... %-6s [ remote ADDR ]\n"
37 " [ local ADDR ]\n"
38 " [ encaplimit ELIM ]\n"
39 " [ hoplimit HLIM ]\n"
40 " [ tclass TCLASS ]\n"
41 " [ flowlabel FLOWLABEL ]\n"
42 " [ dscp inherit ]\n"
43 " [ [no]allow-localremote ]\n"
44 " [ dev PHYS_DEV ]\n"
45 " [ fwmark MARK ]\n"
46 " [ external ]\n"
47 " [ noencap ]\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"
55 "\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",
62 lu->id,
63 IPV6_DEFAULT_TNL_ENCAP_LIMIT, DEFAULT_TNL_HOP_LIMIT);
64 }
65
66 static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
67 struct nlmsghdr *n)
68 {
69 struct ifinfomsg *ifi = NLMSG_DATA(n);
70 struct {
71 struct nlmsghdr n;
72 struct ifinfomsg i;
73 } req = {
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,
79 };
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];
84 int len;
85 inet_prefix saddr, daddr;
86 __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
87 __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
88 __u32 flowinfo = 0;
89 __u32 flags = 0;
90 __u8 proto = 0;
91 __u32 link = 0;
92 __u16 encaptype = 0;
93 __u16 encapflags = TUNNEL_ENCAP_FLAG_CSUM6;
94 __u16 encapsport = 0;
95 __u16 encapdport = 0;
96 __u8 metadata = 0;
97 __u32 fwmark = 0;
98
99 inet_prefix_reset(&saddr);
100 inet_prefix_reset(&daddr);
101
102 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
103 const struct rtattr *rta;
104
105 if (rtnl_talk(&rth, &req.n, &answer) < 0) {
106 get_failed:
107 fprintf(stderr,
108 "Failed to get existing tunnel info.\n");
109 return -1;
110 }
111
112 len = answer->nlmsg_len;
113 len -= NLMSG_LENGTH(sizeof(*ifi));
114 if (len < 0)
115 goto get_failed;
116
117 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len);
118
119 if (!tb[IFLA_LINKINFO])
120 goto get_failed;
121
122 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
123
124 if (!linkinfo[IFLA_INFO_DATA])
125 goto get_failed;
126
127 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
128 linkinfo[IFLA_INFO_DATA]);
129
130 rta = iptuninfo[IFLA_IPTUN_LOCAL];
131 if (rta && get_addr_rta(&saddr, rta, AF_INET6))
132 goto get_failed;
133
134 rta = iptuninfo[IFLA_IPTUN_REMOTE];
135 if (rta && get_addr_rta(&daddr, rta, AF_INET6))
136 goto get_failed;
137
138 if (iptuninfo[IFLA_IPTUN_TTL])
139 hop_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
140
141 if (iptuninfo[IFLA_IPTUN_ENCAP_LIMIT])
142 encap_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_ENCAP_LIMIT]);
143
144 if (iptuninfo[IFLA_IPTUN_FLOWINFO])
145 flowinfo = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLOWINFO]);
146
147 if (iptuninfo[IFLA_IPTUN_FLAGS])
148 flags = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLAGS]);
149
150 if (iptuninfo[IFLA_IPTUN_LINK])
151 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
152
153 if (iptuninfo[IFLA_IPTUN_PROTO])
154 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
155 if (iptuninfo[IFLA_IPTUN_COLLECT_METADATA])
156 metadata = 1;
157
158 if (iptuninfo[IFLA_IPTUN_FWMARK])
159 fwmark = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FWMARK]);
160
161 free(answer);
162 }
163
164 while (argc > 0) {
165 if (strcmp(*argv, "mode") == 0) {
166 NEXT_ARG();
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)
177 proto = 0;
178 else
179 invarg("Cannot guess tunnel mode.", *argv);
180 } else if (strcmp(*argv, "remote") == 0) {
181 NEXT_ARG();
182 get_addr(&daddr, *argv, AF_INET6);
183 } else if (strcmp(*argv, "local") == 0) {
184 NEXT_ARG();
185 get_addr(&saddr, *argv, AF_INET6);
186 } else if (matches(*argv, "dev") == 0) {
187 NEXT_ARG();
188 link = ll_name_to_index(*argv);
189 if (!link)
190 exit(nodev(*argv));
191 } else if (strcmp(*argv, "ttl") == 0 ||
192 strcmp(*argv, "hoplimit") == 0 ||
193 strcmp(*argv, "hlim") == 0) {
194 NEXT_ARG();
195 if (strcmp(*argv, "inherit") != 0) {
196 if (get_u8(&hop_limit, *argv, 0))
197 invarg("invalid HLIM\n", *argv);
198 } else
199 hop_limit = 0;
200 } else if (strcmp(*argv, "encaplimit") == 0) {
201 NEXT_ARG();
202 if (strcmp(*argv, "none") == 0) {
203 flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
204 } else {
205 __u8 uval;
206
207 if (get_u8(&uval, *argv, 0) < -1)
208 invarg("invalid ELIM", *argv);
209 encap_limit = uval;
210 flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
211 }
212 } else if (strcmp(*argv, "tos") == 0 ||
213 strcmp(*argv, "tclass") == 0 ||
214 strcmp(*argv, "tc") == 0 ||
215 matches(*argv, "dsfield") == 0) {
216 __u8 uval;
217
218 NEXT_ARG();
219 flowinfo &= ~IP6_FLOWINFO_TCLASS;
220 if (strcmp(*argv, "inherit") == 0)
221 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
222 else {
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;
227 }
228 } else if (strcmp(*argv, "flowlabel") == 0 ||
229 strcmp(*argv, "fl") == 0) {
230 __u32 uval;
231
232 NEXT_ARG();
233 flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
234 if (strcmp(*argv, "inherit") == 0)
235 flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
236 else {
237 if (get_u32(&uval, *argv, 16))
238 invarg("invalid Flowlabel", *argv);
239 if (uval > 0xFFFFF)
240 invarg("invalid Flowlabel", *argv);
241 flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
242 flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
243 }
244 } else if (strcmp(*argv, "dscp") == 0) {
245 NEXT_ARG();
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) {
250 NEXT_ARG();
251 if (strcmp(*argv, "inherit") == 0) {
252 flags |= IP6_TNL_F_USE_ORIG_FWMARK;
253 fwmark = 0;
254 } else {
255 if (get_u32(&fwmark, *argv, 0))
256 invarg("invalid fwmark\n", *argv);
257 flags &= ~IP6_TNL_F_USE_ORIG_FWMARK;
258 }
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) {
266 NEXT_ARG();
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;
273 else
274 invarg("Invalid encap type.", *argv);
275 } else if (strcmp(*argv, "encap-sport") == 0) {
276 NEXT_ARG();
277 if (strcmp(*argv, "auto") == 0)
278 encapsport = 0;
279 else if (get_u16(&encapsport, *argv, 0))
280 invarg("Invalid source port.", *argv);
281 } else if (strcmp(*argv, "encap-dport") == 0) {
282 NEXT_ARG();
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) {
298 metadata = 1;
299 } else {
300 ip6tunnel_print_help(lu, argc, argv, stderr);
301 return -1;
302 }
303 argc--, argv++;
304 }
305
306 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
307 if (metadata) {
308 addattr_l(n, 1024, IFLA_IPTUN_COLLECT_METADATA, NULL, 0);
309 return 0;
310 }
311
312 if (is_addrtype_inet_not_unspec(&saddr)) {
313 addattr_l(n, 1024, IFLA_IPTUN_LOCAL,
314 saddr.data, saddr.bytelen);
315 }
316 if (is_addrtype_inet_not_unspec(&daddr)) {
317 addattr_l(n, 1024, IFLA_IPTUN_REMOTE,
318 daddr.data, daddr.bytelen);
319 }
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);
326
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));
331
332 return 0;
333 }
334
335 static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
336 {
337 char s2[64];
338 __u32 flags = 0;
339 __u32 flowinfo = 0;
340 __u8 ttl = 0;
341
342 if (!tb)
343 return;
344
345 if (tb[IFLA_IPTUN_COLLECT_METADATA]) {
346 print_bool(PRINT_ANY, "external", "external ", true);
347 return;
348 }
349
350 if (tb[IFLA_IPTUN_FLAGS])
351 flags = rta_getattr_u32(tb[IFLA_IPTUN_FLAGS]);
352
353 if (tb[IFLA_IPTUN_FLOWINFO])
354 flowinfo = rta_getattr_u32(tb[IFLA_IPTUN_FLOWINFO]);
355
356 if (tb[IFLA_IPTUN_PROTO]) {
357 switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
358 case IPPROTO_IPIP:
359 print_string(PRINT_ANY, "proto", "%s ", "ipip6");
360 break;
361 case IPPROTO_IPV6:
362 print_string(PRINT_ANY, "proto", "%s ", "ip6ip6");
363 break;
364 case 0:
365 print_string(PRINT_ANY, "proto", "%s ", "any");
366 break;
367 }
368 }
369
370 tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET6);
371 tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET6);
372
373 if (tb[IFLA_IPTUN_LINK]) {
374 __u32 link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
375
376 if (link) {
377 print_string(PRINT_ANY, "link", "dev %s ",
378 ll_index_to_name(link));
379 }
380 }
381
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);
386 else
387 print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit");
388
389 if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) {
390 print_bool(PRINT_ANY,
391 "ip6_tnl_f_ign_encap_limit",
392 "encaplimit none ",
393 true);
394 } else if (tb[IFLA_IPTUN_ENCAP_LIMIT]) {
395 __u8 val = rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]);
396
397 print_uint(PRINT_ANY, "encap_limit", "encaplimit %u ", val);
398 }
399
400 if (flags & IP6_TNL_F_USE_ORIG_TCLASS) {
401 print_bool(PRINT_ANY,
402 "ip6_tnl_f_use_orig_tclass",
403 "tclass inherit ",
404 true);
405 } else if (tb[IFLA_IPTUN_FLOWINFO]) {
406 __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20;
407
408 snprintf(s2, sizeof(s2), "0x%02x", val);
409 print_string(PRINT_ANY, "tclass", "tclass %s ", s2);
410 }
411
412 if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) {
413 print_bool(PRINT_ANY,
414 "ip6_tnl_f_use_orig_flowlabel",
415 "flowlabel inherit ",
416 true);
417 } else if (tb[IFLA_IPTUN_FLOWINFO]) {
418 __u32 val = ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL);
419
420 snprintf(s2, sizeof(s2), "0x%05x", val);
421 print_string(PRINT_ANY, "flowlabel", "flowlabel %s ", s2);
422 }
423
424 if (flags & IP6_TNL_F_RCV_DSCP_COPY)
425 print_bool(PRINT_ANY,
426 "ip6_tnl_f_rcv_dscp_copy",
427 "dscp inherit ",
428 true);
429
430 if (flags & IP6_TNL_F_MIP6_DEV)
431 print_bool(PRINT_ANY, "ip6_tnl_f_mip6_dev", "mip6 ", true);
432
433 if (flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE)
434 print_bool(PRINT_ANY,
435 "ip6_tnl_f_allow_local_remote",
436 "allow-localremote ",
437 true);
438
439 if (flags & IP6_TNL_F_USE_ORIG_FWMARK) {
440 print_bool(PRINT_ANY,
441 "ip6_tnl_f_use_orig_fwmark",
442 "fwmark inherit ",
443 true);
444 } else if (tb[IFLA_IPTUN_FWMARK]) {
445 __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]);
446
447 if (fwmark) {
448 print_0xhex(PRINT_ANY,
449 "fwmark", "fwmark %#llx ", fwmark);
450 }
451 }
452
453 tnl_print_encap(tb,
454 IFLA_IPTUN_ENCAP_TYPE,
455 IFLA_IPTUN_ENCAP_FLAGS,
456 IFLA_IPTUN_ENCAP_SPORT,
457 IFLA_IPTUN_ENCAP_DPORT);
458 }
459
460 struct link_util ip6tnl_link_util = {
461 .id = "ip6tnl",
462 .maxattr = IFLA_IPTUN_MAX,
463 .parse_opt = ip6tunnel_parse_opt,
464 .print_opt = ip6tunnel_print_opt,
465 .print_help = ip6tunnel_print_help,
466 };