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