]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_iptnl.c
iproute: Set ip/ip6 lwtunnel flags
[mirror_iproute2.git] / ip / link_iptnl.c
1 /*
2 * link_iptnl.c ipip and sit 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/in.h>
20 #include <linux/ip.h>
21 #include <linux/if_tunnel.h>
22 #include "rt_names.h"
23 #include "utils.h"
24 #include "ip_common.h"
25 #include "tunnel.h"
26
27 static void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
28 FILE *f)
29 {
30 const char *mode;
31
32 fprintf(f,
33 "Usage: ... %-6s [ remote ADDR ]\n",
34 lu->id
35 );
36 fprintf(f,
37 " [ local ADDR ]\n"
38 " [ ttl TTL ]\n"
39 " [ tos TOS ]\n"
40 " [ [no]pmtudisc ]\n"
41 " [ 6rd-prefix ADDR ]\n"
42 " [ 6rd-relay_prefix ADDR ]\n"
43 " [ 6rd-reset ]\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 );
55 if (strcmp(lu->id, "sit") == 0) {
56 mode = "{ ip6ip | ipip | mplsip | any } ]\n"
57 " [ isatap";
58 } else {
59 mode = "{ ipip | mplsip | any }";
60 }
61 fprintf(f,
62 " [ mode %s ]\n"
63 "\n",
64 mode
65 );
66 fprintf(f,
67 "Where: ADDR := { IP_ADDRESS | any }\n"
68 " TOS := { NUMBER | inherit }\n"
69 " TTL := { 1..255 | inherit }\n"
70 " MARK := { 0x0..0xffffffff }\n"
71 );
72 }
73
74 static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
75 struct nlmsghdr *n)
76 {
77 struct ifinfomsg *ifi = NLMSG_DATA(n);
78 struct {
79 struct nlmsghdr n;
80 struct ifinfomsg i;
81 } req = {
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,
87 };
88 struct nlmsghdr *answer;
89 struct rtattr *tb[IFLA_MAX + 1];
90 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
91 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
92 int len;
93 inet_prefix saddr, daddr, ip6rdprefix, ip6rdrelayprefix;
94 __u8 pmtudisc = 1;
95 __u8 tos = 0;
96 __u16 iflags = 0;
97 __u8 ttl = 0;
98 __u8 proto = 0;
99 __u32 link = 0;
100 __u16 encaptype = 0;
101 __u16 encapflags = 0;
102 __u16 encapsport = 0;
103 __u16 encapdport = 0;
104 __u8 metadata = 0;
105 __u32 fwmark = 0;
106
107 inet_prefix_reset(&saddr);
108 inet_prefix_reset(&daddr);
109
110 inet_prefix_reset(&ip6rdprefix);
111 inet_prefix_reset(&ip6rdrelayprefix);
112
113 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
114 const struct rtattr *rta;
115
116 if (rtnl_talk(&rth, &req.n, &answer) < 0) {
117 get_failed:
118 fprintf(stderr,
119 "Failed to get existing tunnel info.\n");
120 return -1;
121 }
122
123 len = answer->nlmsg_len;
124 len -= NLMSG_LENGTH(sizeof(*ifi));
125 if (len < 0)
126 goto get_failed;
127
128 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len);
129
130 if (!tb[IFLA_LINKINFO])
131 goto get_failed;
132
133 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
134
135 if (!linkinfo[IFLA_INFO_DATA])
136 goto get_failed;
137
138 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
139 linkinfo[IFLA_INFO_DATA]);
140
141 rta = iptuninfo[IFLA_IPTUN_LOCAL];
142 if (rta && get_addr_rta(&saddr, rta, AF_INET))
143 goto get_failed;
144
145 rta = iptuninfo[IFLA_IPTUN_REMOTE];
146 if (rta && get_addr_rta(&daddr, rta, AF_INET))
147 goto get_failed;
148
149 rta = iptuninfo[IFLA_IPTUN_6RD_PREFIX];
150 if (rta && get_addr_rta(&ip6rdprefix, rta, AF_INET6))
151 goto get_failed;
152
153 rta = iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX];
154 if (rta && get_addr_rta(&ip6rdrelayprefix, rta, AF_INET))
155 goto get_failed;
156
157 rta = iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN];
158 ip6rdprefix.bitlen = rta ? rta_getattr_u16(rta) : 0;
159
160 rta = iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN];
161 ip6rdrelayprefix.bitlen = rta ? rta_getattr_u16(rta) : 0;
162
163 if (iptuninfo[IFLA_IPTUN_TTL])
164 ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
165
166 if (iptuninfo[IFLA_IPTUN_PMTUDISC])
167 pmtudisc =
168 rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]);
169
170 if (iptuninfo[IFLA_IPTUN_TOS])
171 tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
172
173 if (iptuninfo[IFLA_IPTUN_FLAGS])
174 iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]);
175
176 if (iptuninfo[IFLA_IPTUN_LINK])
177 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
178
179 if (iptuninfo[IFLA_IPTUN_PROTO])
180 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
181
182 if (iptuninfo[IFLA_IPTUN_ENCAP_TYPE])
183 encaptype = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_TYPE]);
184 if (iptuninfo[IFLA_IPTUN_ENCAP_FLAGS])
185 encapflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]);
186 if (iptuninfo[IFLA_IPTUN_ENCAP_SPORT])
187 encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]);
188 if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT])
189 encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]);
190
191 if (iptuninfo[IFLA_IPTUN_COLLECT_METADATA])
192 metadata = 1;
193
194 if (iptuninfo[IFLA_IPTUN_FWMARK])
195 fwmark = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FWMARK]);
196
197 free(answer);
198 }
199
200 while (argc > 0) {
201 if (strcmp(*argv, "mode") == 0) {
202 NEXT_ARG();
203 if (strcmp(lu->id, "sit") == 0 &&
204 (strcmp(*argv, "ipv6/ipv4") == 0 ||
205 strcmp(*argv, "ip6ip") == 0))
206 proto = IPPROTO_IPV6;
207 else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
208 strcmp(*argv, "ipip") == 0 ||
209 strcmp(*argv, "ip4ip4") == 0)
210 proto = IPPROTO_IPIP;
211 else if (strcmp(*argv, "mpls/ipv4") == 0 ||
212 strcmp(*argv, "mplsip") == 0)
213 proto = IPPROTO_MPLS;
214 else if (strcmp(*argv, "any/ipv4") == 0 ||
215 strcmp(*argv, "any") == 0)
216 proto = 0;
217 else
218 invarg("Cannot guess tunnel mode.", *argv);
219 } else if (strcmp(*argv, "remote") == 0) {
220 NEXT_ARG();
221 get_addr(&daddr, *argv, AF_INET);
222 } else if (strcmp(*argv, "local") == 0) {
223 NEXT_ARG();
224 get_addr(&saddr, *argv, AF_INET);
225 } else if (matches(*argv, "dev") == 0) {
226 NEXT_ARG();
227 link = ll_name_to_index(*argv);
228 if (!link)
229 exit(nodev(*argv));
230 } else if (strcmp(*argv, "ttl") == 0 ||
231 strcmp(*argv, "hoplimit") == 0 ||
232 strcmp(*argv, "hlim") == 0) {
233 NEXT_ARG();
234 if (strcmp(*argv, "inherit") != 0) {
235 if (get_u8(&ttl, *argv, 0))
236 invarg("invalid TTL\n", *argv);
237 } else
238 ttl = 0;
239 } else if (strcmp(*argv, "tos") == 0 ||
240 strcmp(*argv, "tclass") == 0 ||
241 strcmp(*argv, "tc") == 0 ||
242 matches(*argv, "dsfield") == 0) {
243 __u32 uval;
244
245 NEXT_ARG();
246 if (strcmp(*argv, "inherit") != 0) {
247 if (rtnl_dsfield_a2n(&uval, *argv))
248 invarg("bad TOS value", *argv);
249 tos = uval;
250 } else
251 tos = 1;
252 } else if (strcmp(*argv, "nopmtudisc") == 0) {
253 pmtudisc = 0;
254 } else if (strcmp(*argv, "pmtudisc") == 0) {
255 pmtudisc = 1;
256 } else if (strcmp(lu->id, "sit") == 0 &&
257 strcmp(*argv, "isatap") == 0) {
258 iflags |= SIT_ISATAP;
259 } else if (strcmp(*argv, "noencap") == 0) {
260 encaptype = TUNNEL_ENCAP_NONE;
261 } else if (strcmp(*argv, "encap") == 0) {
262 NEXT_ARG();
263 if (strcmp(*argv, "fou") == 0)
264 encaptype = TUNNEL_ENCAP_FOU;
265 else if (strcmp(*argv, "gue") == 0)
266 encaptype = TUNNEL_ENCAP_GUE;
267 else if (strcmp(*argv, "none") == 0)
268 encaptype = TUNNEL_ENCAP_NONE;
269 else
270 invarg("Invalid encap type.", *argv);
271 } else if (strcmp(*argv, "encap-sport") == 0) {
272 NEXT_ARG();
273 if (strcmp(*argv, "auto") == 0)
274 encapsport = 0;
275 else if (get_u16(&encapsport, *argv, 0))
276 invarg("Invalid source port.", *argv);
277 } else if (strcmp(*argv, "encap-dport") == 0) {
278 NEXT_ARG();
279 if (get_u16(&encapdport, *argv, 0))
280 invarg("Invalid destination port.", *argv);
281 } else if (strcmp(*argv, "encap-csum") == 0) {
282 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
283 } else if (strcmp(*argv, "noencap-csum") == 0) {
284 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
285 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
286 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
287 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
288 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
289 } else if (strcmp(*argv, "encap-remcsum") == 0) {
290 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
291 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
292 encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM;
293 } else if (strcmp(*argv, "external") == 0) {
294 metadata = 1;
295 } else if (strcmp(*argv, "6rd-prefix") == 0) {
296 NEXT_ARG();
297 if (get_prefix(&ip6rdprefix, *argv, AF_INET6))
298 invarg("invalid 6rd_prefix\n", *argv);
299 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
300 NEXT_ARG();
301 if (get_prefix(&ip6rdrelayprefix, *argv, AF_INET))
302 invarg("invalid 6rd-relay_prefix\n", *argv);
303 } else if (strcmp(*argv, "6rd-reset") == 0) {
304 get_prefix(&ip6rdprefix, "2002::/16", AF_INET6);
305 inet_prefix_reset(&ip6rdrelayprefix);
306 } else if (strcmp(*argv, "fwmark") == 0) {
307 NEXT_ARG();
308 if (get_u32(&fwmark, *argv, 0))
309 invarg("invalid fwmark\n", *argv);
310 } else {
311 iptunnel_print_help(lu, argc, argv, stderr);
312 return -1;
313 }
314 argc--, argv++;
315 }
316
317 if (ttl && pmtudisc == 0) {
318 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
319 exit(-1);
320 }
321
322 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
323 if (metadata) {
324 addattr_l(n, 1024, IFLA_IPTUN_COLLECT_METADATA, NULL, 0);
325 return 0;
326 }
327
328 if (is_addrtype_inet_not_unspec(&saddr)) {
329 addattr_l(n, 1024, IFLA_IPTUN_LOCAL,
330 saddr.data, saddr.bytelen);
331 }
332 if (is_addrtype_inet_not_unspec(&daddr)) {
333 addattr_l(n, 1024, IFLA_IPTUN_REMOTE,
334 daddr.data, daddr.bytelen);
335 }
336 addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc);
337 addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
338 addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
339 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
340 addattr32(n, 1024, IFLA_IPTUN_FWMARK, fwmark);
341
342 addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
343 addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags);
344 addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
345 addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
346
347 if (strcmp(lu->id, "sit") == 0) {
348 addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags);
349 if (is_addrtype_inet(&ip6rdprefix)) {
350 addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX,
351 ip6rdprefix.data, ip6rdprefix.bytelen);
352 addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN,
353 ip6rdprefix.bitlen);
354 }
355 if (is_addrtype_inet(&ip6rdrelayprefix)) {
356 addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX,
357 ip6rdrelayprefix.data[0]);
358 addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
359 ip6rdrelayprefix.bitlen);
360 }
361 }
362
363 return 0;
364 }
365
366 static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
367 {
368 char s2[64];
369 __u16 prefixlen;
370 __u8 ttl = 0;
371 __u8 tos = 0;
372
373 if (!tb)
374 return;
375
376 if (tb[IFLA_IPTUN_COLLECT_METADATA]) {
377 print_bool(PRINT_ANY, "external", "external", true);
378 return;
379 }
380
381 if (tb[IFLA_IPTUN_PROTO]) {
382 switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
383 case IPPROTO_IPIP:
384 print_string(PRINT_ANY, "proto", "%s ", "ipip");
385 break;
386 case IPPROTO_IPV6:
387 print_string(PRINT_ANY, "proto", "%s ", "ip6ip");
388 break;
389 case IPPROTO_MPLS:
390 print_string(PRINT_ANY, "proto", "%s ", "mplsip");
391 break;
392 case 0:
393 print_string(PRINT_ANY, "proto", "%s ", "any");
394 break;
395 }
396 }
397
398 tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET);
399 tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET);
400
401 if (tb[IFLA_IPTUN_LINK]) {
402 __u32 link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
403
404 if (link) {
405 print_string(PRINT_ANY, "link", "dev %s ",
406 ll_index_to_name(link));
407 }
408 }
409
410 if (tb[IFLA_IPTUN_TTL])
411 ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]);
412 if (is_json_context() || ttl)
413 print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
414 else
415 print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
416
417 if (tb[IFLA_IPTUN_TOS])
418 tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);
419 if (tos) {
420 if (is_json_context() || tos != 1)
421 print_0xhex(PRINT_ANY, "tos", "tos %#llx ", tos);
422 else
423 print_string(PRINT_FP, NULL, "tos %s ", "inherit");
424 }
425
426 if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC]))
427 print_bool(PRINT_ANY, "pmtudisc", "pmtudisc ", true);
428 else
429 print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false);
430
431 if (tb[IFLA_IPTUN_FLAGS]) {
432 __u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]);
433
434 if (iflags & SIT_ISATAP)
435 print_bool(PRINT_ANY, "isatap", "isatap ", true);
436 }
437
438 if (tb[IFLA_IPTUN_6RD_PREFIXLEN] &&
439 (prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]))) {
440 __u16 relayprefixlen =
441 rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
442 __u32 relayprefix =
443 rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
444
445 const char *prefix = inet_ntop(AF_INET6,
446 RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]),
447 s2, sizeof(s2));
448
449 if (is_json_context()) {
450 print_string(PRINT_JSON, "prefix", NULL, prefix);
451 print_int(PRINT_JSON, "prefixlen", NULL, prefixlen);
452 if (relayprefix) {
453 print_string(PRINT_JSON,
454 "relay_prefix",
455 NULL,
456 format_host(AF_INET,
457 4,
458 &relayprefix));
459 print_int(PRINT_JSON,
460 "relay_prefixlen",
461 NULL,
462 relayprefixlen);
463 }
464 } else {
465 printf("6rd-prefix %s/%u ", prefix, prefixlen);
466 if (relayprefix) {
467 printf("6rd-relay_prefix %s/%u ",
468 format_host(AF_INET, 4, &relayprefix),
469 relayprefixlen);
470 }
471 }
472 }
473
474 if (tb[IFLA_IPTUN_FWMARK]) {
475 __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]);
476
477 if (fwmark) {
478 print_0xhex(PRINT_ANY,
479 "fwmark", "fwmark %#llx ", fwmark);
480 }
481 }
482
483 tnl_print_encap(tb,
484 IFLA_IPTUN_ENCAP_TYPE,
485 IFLA_IPTUN_ENCAP_FLAGS,
486 IFLA_IPTUN_ENCAP_SPORT,
487 IFLA_IPTUN_ENCAP_DPORT);
488 }
489
490 struct link_util ipip_link_util = {
491 .id = "ipip",
492 .maxattr = IFLA_IPTUN_MAX,
493 .parse_opt = iptunnel_parse_opt,
494 .print_opt = iptunnel_print_opt,
495 .print_help = iptunnel_print_help,
496 };
497
498 struct link_util sit_link_util = {
499 .id = "sit",
500 .maxattr = IFLA_IPTUN_MAX,
501 .parse_opt = iptunnel_parse_opt,
502 .print_opt = iptunnel_print_opt,
503 .print_help = iptunnel_print_help,
504 };