]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/link_ip6tnl.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / link_ip6tnl.c
CommitLineData
9d0efc10
ND
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
561e650e 32static void print_usage(FILE *f)
9d0efc10 33{
561e650e 34 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
35 fprintf(f, " [ mode { ip6ip6 | ipip6 | any } ]\n");
36 fprintf(f, " type ip6tnl [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(f, " [ dev PHYS_DEV ] [ encaplimit ELIM ]\n");
56f5daac 38 fprintf(f, " [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
561e650e 39 fprintf(f, " [ dscp inherit ] [ fwmark inherit ]\n");
73516e12
TH
40 fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n");
41 fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n");
42 fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n");
561e650e 43 fprintf(f, "\n");
44 fprintf(f, "Where: NAME := STRING\n");
45 fprintf(f, " ADDR := IPV6_ADDRESS\n");
46 fprintf(f, " ELIM := { none | 0..255 }(default=%d)\n",
9d0efc10 47 IPV6_DEFAULT_TNL_ENCAP_LIMIT);
561e650e 48 fprintf(f, " HLIM := 0..255 (default=%d)\n",
9d0efc10 49 DEFAULT_TNL_HOP_LIMIT);
561e650e 50 fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n");
51 fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
52}
53
54static void usage(void) __attribute__((noreturn));
55static void usage(void)
56{
57 print_usage(stderr);
9d0efc10
ND
58 exit(-1);
59}
60
61static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
62 struct nlmsghdr *n)
63{
d17b136f 64 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
9d0efc10
ND
65 struct {
66 struct nlmsghdr n;
67 struct ifinfomsg i;
68 char buf[2048];
d17b136f
PS
69 } req = {
70 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
71 .n.nlmsg_flags = NLM_F_REQUEST,
72 .n.nlmsg_type = RTM_GETLINK,
73 .i.ifi_family = preferred_family,
74 .i.ifi_index = ifi->ifi_index,
75 };
9d0efc10
ND
76 struct rtattr *tb[IFLA_MAX + 1];
77 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
78 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
79 int len;
d17b136f
PS
80 struct in6_addr laddr = {};
81 struct in6_addr raddr = {};
9d0efc10
ND
82 __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
83 __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
84 __u32 flowinfo = 0;
85 __u32 flags = 0;
86 __u32 link = 0;
87 __u8 proto = 0;
73516e12
TH
88 __u16 encaptype = 0;
89 __u16 encapflags = TUNNEL_ENCAP_FLAG_CSUM6;
90 __u16 encapsport = 0;
91 __u16 encapdport = 0;
9d0efc10 92
9d0efc10 93 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c079e121 94 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
9d0efc10
ND
95get_failed:
96 fprintf(stderr,
97 "Failed to get existing tunnel info.\n");
98 return -1;
99 }
100
101 len = req.n.nlmsg_len;
102 len -= NLMSG_LENGTH(sizeof(*ifi));
103 if (len < 0)
104 goto get_failed;
105
106 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
107
108 if (!tb[IFLA_LINKINFO])
109 goto get_failed;
110
111 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
112
113 if (!linkinfo[IFLA_INFO_DATA])
114 goto get_failed;
115
116 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
117 linkinfo[IFLA_INFO_DATA]);
118
119 if (iptuninfo[IFLA_IPTUN_LOCAL])
120 memcpy(&laddr, RTA_DATA(iptuninfo[IFLA_IPTUN_LOCAL]),
121 sizeof(laddr));
122
123 if (iptuninfo[IFLA_IPTUN_REMOTE])
124 memcpy(&raddr, RTA_DATA(iptuninfo[IFLA_IPTUN_REMOTE]),
125 sizeof(raddr));
126
127 if (iptuninfo[IFLA_IPTUN_TTL])
128 hop_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
129
130 if (iptuninfo[IFLA_IPTUN_ENCAP_LIMIT])
131 encap_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_ENCAP_LIMIT]);
132
133 if (iptuninfo[IFLA_IPTUN_FLOWINFO])
134 flowinfo = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLOWINFO]);
135
136 if (iptuninfo[IFLA_IPTUN_FLAGS])
137 flags = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLAGS]);
138
139 if (iptuninfo[IFLA_IPTUN_LINK])
140 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
141
142 if (iptuninfo[IFLA_IPTUN_PROTO])
143 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
144 }
145
146 while (argc > 0) {
147 if (matches(*argv, "mode") == 0) {
148 NEXT_ARG();
149 if (strcmp(*argv, "ipv6/ipv6") == 0 ||
150 strcmp(*argv, "ip6ip6") == 0)
151 proto = IPPROTO_IPV6;
152 else if (strcmp(*argv, "ip/ipv6") == 0 ||
153 strcmp(*argv, "ipv4/ipv6") == 0 ||
154 strcmp(*argv, "ipip6") == 0 ||
155 strcmp(*argv, "ip4ip6") == 0)
156 proto = IPPROTO_IPIP;
157 else if (strcmp(*argv, "any/ipv6") == 0 ||
158 strcmp(*argv, "any") == 0)
159 proto = 0;
160 else
161 invarg("Cannot guess tunnel mode.", *argv);
162 } else if (strcmp(*argv, "remote") == 0) {
163 inet_prefix addr;
56f5daac 164
9d0efc10
ND
165 NEXT_ARG();
166 get_prefix(&addr, *argv, preferred_family);
167 if (addr.family == AF_UNSPEC)
168 invarg("\"remote\" address family is AF_UNSPEC", *argv);
169 memcpy(&raddr, addr.data, addr.bytelen);
170 } else if (strcmp(*argv, "local") == 0) {
171 inet_prefix addr;
56f5daac 172
9d0efc10
ND
173 NEXT_ARG();
174 get_prefix(&addr, *argv, preferred_family);
175 if (addr.family == AF_UNSPEC)
176 invarg("\"local\" address family is AF_UNSPEC", *argv);
177 memcpy(&laddr, addr.data, addr.bytelen);
178 } else if (matches(*argv, "dev") == 0) {
179 NEXT_ARG();
180 link = if_nametoindex(*argv);
181 if (link == 0)
182 invarg("\"dev\" is invalid", *argv);
183 } else if (strcmp(*argv, "hoplimit") == 0 ||
184 strcmp(*argv, "ttl") == 0 ||
185 strcmp(*argv, "hlim") == 0) {
186 __u8 uval;
56f5daac 187
9d0efc10
ND
188 NEXT_ARG();
189 if (get_u8(&uval, *argv, 0))
190 invarg("invalid HLIM", *argv);
191 hop_limit = uval;
73516e12 192 } else if (strcmp(*argv, "encaplimit") == 0) {
9d0efc10
ND
193 NEXT_ARG();
194 if (strcmp(*argv, "none") == 0) {
195 flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
196 } else {
197 __u8 uval;
56f5daac 198
9d0efc10
ND
199 if (get_u8(&uval, *argv, 0) < -1)
200 invarg("invalid ELIM", *argv);
201 encap_limit = uval;
202 flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
203 }
204 } else if (strcmp(*argv, "tclass") == 0 ||
205 strcmp(*argv, "tc") == 0 ||
206 strcmp(*argv, "tos") == 0 ||
207 matches(*argv, "dsfield") == 0) {
208 __u8 uval;
56f5daac 209
9d0efc10
ND
210 NEXT_ARG();
211 flowinfo &= ~IP6_FLOWINFO_TCLASS;
212 if (strcmp(*argv, "inherit") == 0)
213 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
214 else {
215 if (get_u8(&uval, *argv, 16))
216 invarg("invalid TClass", *argv);
217 flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
218 flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
219 }
220 } else if (strcmp(*argv, "flowlabel") == 0 ||
221 strcmp(*argv, "fl") == 0) {
222 __u32 uval;
56f5daac 223
9d0efc10
ND
224 NEXT_ARG();
225 flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
226 if (strcmp(*argv, "inherit") == 0)
227 flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
228 else {
229 if (get_u32(&uval, *argv, 16))
230 invarg("invalid Flowlabel", *argv);
231 if (uval > 0xFFFFF)
232 invarg("invalid Flowlabel", *argv);
233 flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
234 flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
235 }
236 } else if (strcmp(*argv, "dscp") == 0) {
237 NEXT_ARG();
238 if (strcmp(*argv, "inherit") != 0)
239 invarg("not inherit", *argv);
240 flags |= IP6_TNL_F_RCV_DSCP_COPY;
241 } else if (strcmp(*argv, "fwmark") == 0) {
242 NEXT_ARG();
243 if (strcmp(*argv, "inherit") != 0)
244 invarg("not inherit", *argv);
245 flags |= IP6_TNL_F_USE_ORIG_FWMARK;
73516e12
TH
246 } else if (strcmp(*argv, "noencap") == 0) {
247 encaptype = TUNNEL_ENCAP_NONE;
248 } else if (strcmp(*argv, "encap") == 0) {
249 NEXT_ARG();
250 if (strcmp(*argv, "fou") == 0)
251 encaptype = TUNNEL_ENCAP_FOU;
252 else if (strcmp(*argv, "gue") == 0)
253 encaptype = TUNNEL_ENCAP_GUE;
254 else if (strcmp(*argv, "none") == 0)
255 encaptype = TUNNEL_ENCAP_NONE;
256 else
257 invarg("Invalid encap type.", *argv);
258 } else if (strcmp(*argv, "encap-sport") == 0) {
259 NEXT_ARG();
260 if (strcmp(*argv, "auto") == 0)
261 encapsport = 0;
262 else if (get_u16(&encapsport, *argv, 0))
263 invarg("Invalid source port.", *argv);
264 } else if (strcmp(*argv, "encap-dport") == 0) {
265 NEXT_ARG();
266 if (get_u16(&encapdport, *argv, 0))
267 invarg("Invalid destination port.", *argv);
268 } else if (strcmp(*argv, "encap-csum") == 0) {
269 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
270 } else if (strcmp(*argv, "noencap-csum") == 0) {
271 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
272 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
273 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
274 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
275 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
276 } else if (strcmp(*argv, "encap-remcsum") == 0) {
277 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
278 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
279 encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM;
9d0efc10
ND
280 } else
281 usage();
282 argc--, argv++;
283 }
284
285 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
286 addattr_l(n, 1024, IFLA_IPTUN_LOCAL, &laddr, sizeof(laddr));
287 addattr_l(n, 1024, IFLA_IPTUN_REMOTE, &raddr, sizeof(raddr));
288 addattr8(n, 1024, IFLA_IPTUN_TTL, hop_limit);
289 addattr8(n, 1024, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
290 addattr32(n, 1024, IFLA_IPTUN_FLOWINFO, flowinfo);
291 addattr32(n, 1024, IFLA_IPTUN_FLAGS, flags);
292 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
293
73516e12
TH
294 addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
295 addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags);
296 addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
297 addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
298
9d0efc10
ND
299 return 0;
300}
301
302static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
303{
9d0efc10
ND
304 char s2[64];
305 int flags = 0;
306 __u32 flowinfo = 0;
307
308 if (!tb)
309 return;
310
311 if (tb[IFLA_IPTUN_FLAGS])
312 flags = rta_getattr_u32(tb[IFLA_IPTUN_FLAGS]);
313
314 if (tb[IFLA_IPTUN_FLOWINFO])
315 flowinfo = rta_getattr_u32(tb[IFLA_IPTUN_FLOWINFO]);
316
317 if (tb[IFLA_IPTUN_PROTO]) {
318 switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
319 case IPPROTO_IPIP:
320 fprintf(f, "ipip6 ");
321 break;
322 case IPPROTO_IPV6:
323 fprintf(f, "ip6ip6 ");
324 break;
325 case 0:
326 fprintf(f, "any ");
327 break;
328 }
329 }
330
331 if (tb[IFLA_IPTUN_REMOTE]) {
332 fprintf(f, "remote %s ",
7faf1588 333 rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
9d0efc10
ND
334 }
335
336 if (tb[IFLA_IPTUN_LOCAL]) {
337 fprintf(f, "local %s ",
7faf1588 338 rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
9d0efc10
ND
339 }
340
341 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
56f5daac 342 unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
9d0efc10
ND
343 const char *n = if_indextoname(link, s2);
344
345 if (n)
346 fprintf(f, "dev %s ", n);
347 else
348 fprintf(f, "dev %u ", link);
349 }
350
351 if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
352 printf("encaplimit none ");
353 else if (tb[IFLA_IPTUN_ENCAP_LIMIT])
354 fprintf(f, "encaplimit %u ",
355 rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]));
356
357 if (tb[IFLA_IPTUN_TTL])
358 fprintf(f, "hoplimit %u ", rta_getattr_u8(tb[IFLA_IPTUN_TTL]));
359
360 if (flags & IP6_TNL_F_USE_ORIG_TCLASS)
361 printf("tclass inherit ");
362 else if (tb[IFLA_IPTUN_FLOWINFO]) {
363 __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS);
364
365 printf("tclass 0x%02x ", (__u8)(val >> 20));
366 }
367
368 if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
369 printf("flowlabel inherit ");
370 else
371 printf("flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL));
372
373 printf("(flowinfo 0x%08x) ", ntohl(flowinfo));
374
375 if (flags & IP6_TNL_F_RCV_DSCP_COPY)
376 printf("dscp inherit ");
377
378 if (flags & IP6_TNL_F_MIP6_DEV)
379 fprintf(f, "mip6 ");
380
381 if (flags & IP6_TNL_F_USE_ORIG_FWMARK)
382 fprintf(f, "fwmark inherit ");
73516e12
TH
383
384 if (tb[IFLA_IPTUN_ENCAP_TYPE] &&
385 rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]) !=
386 TUNNEL_ENCAP_NONE) {
387 __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]);
388 __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]);
389 __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]);
390 __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]);
391
392 fputs("encap ", f);
393 switch (type) {
394 case TUNNEL_ENCAP_FOU:
395 fputs("fou ", f);
396 break;
397 case TUNNEL_ENCAP_GUE:
398 fputs("gue ", f);
399 break;
400 default:
401 fputs("unknown ", f);
402 break;
403 }
404
405 if (sport == 0)
406 fputs("encap-sport auto ", f);
407 else
408 fprintf(f, "encap-sport %u", ntohs(sport));
409
410 fprintf(f, "encap-dport %u ", ntohs(dport));
411
412 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
413 fputs("encap-csum ", f);
414 else
415 fputs("noencap-csum ", f);
416
417 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
418 fputs("encap-csum6 ", f);
419 else
420 fputs("noencap-csum6 ", f);
421
422 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
423 fputs("encap-remcsum ", f);
424 else
425 fputs("noencap-remcsum ", f);
426 }
9d0efc10
ND
427}
428
561e650e 429static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv,
430 FILE *f)
431{
432 print_usage(f);
433}
434
9d0efc10
ND
435struct link_util ip6tnl_link_util = {
436 .id = "ip6tnl",
437 .maxattr = IFLA_IPTUN_MAX,
438 .parse_opt = ip6tunnel_parse_opt,
439 .print_opt = ip6tunnel_print_opt,
561e650e 440 .print_help = ip6tunnel_print_help,
9d0efc10 441};