]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_ip6tnl.c
f5b12454cf5f07503eb0157f18688ca3a548c339
[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 usage(void) __attribute__((noreturn));
33 static void usage(void)
34 {
35 fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n");
36 fprintf(stderr, " [ mode { ip6ip6 | ipip6 | any } ]\n");
37 fprintf(stderr, " type ip6tnl [ remote ADDR ] [ local ADDR ]\n");
38 fprintf(stderr, " [ dev PHYS_DEV ] [ encaplimit ELIM ]\n");
39 fprintf(stderr ," [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
40 fprintf(stderr, " [ dscp inherit ] [ fwmark inherit ]\n");
41 fprintf(stderr, "\n");
42 fprintf(stderr, "Where: NAME := STRING\n");
43 fprintf(stderr, " ADDR := IPV6_ADDRESS\n");
44 fprintf(stderr, " ELIM := { none | 0..255 }(default=%d)\n",
45 IPV6_DEFAULT_TNL_ENCAP_LIMIT);
46 fprintf(stderr, " HLIM := 0..255 (default=%d)\n",
47 DEFAULT_TNL_HOP_LIMIT);
48 fprintf(stderr, " TCLASS := { 0x0..0xff | inherit }\n");
49 fprintf(stderr, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
50 exit(-1);
51 }
52
53 static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
54 struct nlmsghdr *n)
55 {
56 struct {
57 struct nlmsghdr n;
58 struct ifinfomsg i;
59 char buf[2048];
60 } req;
61 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
62 struct rtattr *tb[IFLA_MAX + 1];
63 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
64 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
65 int len;
66 struct in6_addr laddr;
67 struct in6_addr raddr;
68 __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
69 __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
70 __u32 flowinfo = 0;
71 __u32 flags = 0;
72 __u32 link = 0;
73 __u8 proto = 0;
74
75 memset(&laddr, 0, sizeof(laddr));
76 memset(&raddr, 0, sizeof(raddr));
77
78 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
79 memset(&req, 0, sizeof(req));
80
81 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
82 req.n.nlmsg_flags = NLM_F_REQUEST;
83 req.n.nlmsg_type = RTM_GETLINK;
84 req.i.ifi_family = preferred_family;
85 req.i.ifi_index = ifi->ifi_index;
86
87 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) {
88 get_failed:
89 fprintf(stderr,
90 "Failed to get existing tunnel info.\n");
91 return -1;
92 }
93
94 len = req.n.nlmsg_len;
95 len -= NLMSG_LENGTH(sizeof(*ifi));
96 if (len < 0)
97 goto get_failed;
98
99 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
100
101 if (!tb[IFLA_LINKINFO])
102 goto get_failed;
103
104 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
105
106 if (!linkinfo[IFLA_INFO_DATA])
107 goto get_failed;
108
109 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
110 linkinfo[IFLA_INFO_DATA]);
111
112 if (iptuninfo[IFLA_IPTUN_LOCAL])
113 memcpy(&laddr, RTA_DATA(iptuninfo[IFLA_IPTUN_LOCAL]),
114 sizeof(laddr));
115
116 if (iptuninfo[IFLA_IPTUN_REMOTE])
117 memcpy(&raddr, RTA_DATA(iptuninfo[IFLA_IPTUN_REMOTE]),
118 sizeof(raddr));
119
120 if (iptuninfo[IFLA_IPTUN_TTL])
121 hop_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
122
123 if (iptuninfo[IFLA_IPTUN_ENCAP_LIMIT])
124 encap_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_ENCAP_LIMIT]);
125
126 if (iptuninfo[IFLA_IPTUN_FLOWINFO])
127 flowinfo = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLOWINFO]);
128
129 if (iptuninfo[IFLA_IPTUN_FLAGS])
130 flags = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLAGS]);
131
132 if (iptuninfo[IFLA_IPTUN_LINK])
133 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
134
135 if (iptuninfo[IFLA_IPTUN_PROTO])
136 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
137 }
138
139 while (argc > 0) {
140 if (matches(*argv, "mode") == 0) {
141 NEXT_ARG();
142 if (strcmp(*argv, "ipv6/ipv6") == 0 ||
143 strcmp(*argv, "ip6ip6") == 0)
144 proto = IPPROTO_IPV6;
145 else if (strcmp(*argv, "ip/ipv6") == 0 ||
146 strcmp(*argv, "ipv4/ipv6") == 0 ||
147 strcmp(*argv, "ipip6") == 0 ||
148 strcmp(*argv, "ip4ip6") == 0)
149 proto = IPPROTO_IPIP;
150 else if (strcmp(*argv, "any/ipv6") == 0 ||
151 strcmp(*argv, "any") == 0)
152 proto = 0;
153 else
154 invarg("Cannot guess tunnel mode.", *argv);
155 } else if (strcmp(*argv, "remote") == 0) {
156 inet_prefix addr;
157 NEXT_ARG();
158 get_prefix(&addr, *argv, preferred_family);
159 if (addr.family == AF_UNSPEC)
160 invarg("\"remote\" address family is AF_UNSPEC", *argv);
161 memcpy(&raddr, addr.data, addr.bytelen);
162 } else if (strcmp(*argv, "local") == 0) {
163 inet_prefix addr;
164 NEXT_ARG();
165 get_prefix(&addr, *argv, preferred_family);
166 if (addr.family == AF_UNSPEC)
167 invarg("\"local\" address family is AF_UNSPEC", *argv);
168 memcpy(&laddr, addr.data, addr.bytelen);
169 } else if (matches(*argv, "dev") == 0) {
170 NEXT_ARG();
171 link = if_nametoindex(*argv);
172 if (link == 0)
173 invarg("\"dev\" is invalid", *argv);
174 } else if (strcmp(*argv, "hoplimit") == 0 ||
175 strcmp(*argv, "ttl") == 0 ||
176 strcmp(*argv, "hlim") == 0) {
177 __u8 uval;
178 NEXT_ARG();
179 if (get_u8(&uval, *argv, 0))
180 invarg("invalid HLIM", *argv);
181 hop_limit = uval;
182 } else if (matches(*argv, "encaplimit") == 0) {
183 NEXT_ARG();
184 if (strcmp(*argv, "none") == 0) {
185 flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
186 } else {
187 __u8 uval;
188 if (get_u8(&uval, *argv, 0) < -1)
189 invarg("invalid ELIM", *argv);
190 encap_limit = uval;
191 flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
192 }
193 } else if (strcmp(*argv, "tclass") == 0 ||
194 strcmp(*argv, "tc") == 0 ||
195 strcmp(*argv, "tos") == 0 ||
196 matches(*argv, "dsfield") == 0) {
197 __u8 uval;
198 NEXT_ARG();
199 flowinfo &= ~IP6_FLOWINFO_TCLASS;
200 if (strcmp(*argv, "inherit") == 0)
201 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
202 else {
203 if (get_u8(&uval, *argv, 16))
204 invarg("invalid TClass", *argv);
205 flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
206 flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
207 }
208 } else if (strcmp(*argv, "flowlabel") == 0 ||
209 strcmp(*argv, "fl") == 0) {
210 __u32 uval;
211 NEXT_ARG();
212 flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
213 if (strcmp(*argv, "inherit") == 0)
214 flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
215 else {
216 if (get_u32(&uval, *argv, 16))
217 invarg("invalid Flowlabel", *argv);
218 if (uval > 0xFFFFF)
219 invarg("invalid Flowlabel", *argv);
220 flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
221 flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
222 }
223 } else if (strcmp(*argv, "dscp") == 0) {
224 NEXT_ARG();
225 if (strcmp(*argv, "inherit") != 0)
226 invarg("not inherit", *argv);
227 flags |= IP6_TNL_F_RCV_DSCP_COPY;
228 } else if (strcmp(*argv, "fwmark") == 0) {
229 NEXT_ARG();
230 if (strcmp(*argv, "inherit") != 0)
231 invarg("not inherit", *argv);
232 flags |= IP6_TNL_F_USE_ORIG_FWMARK;
233 } else
234 usage();
235 argc--, argv++;
236 }
237
238 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
239 addattr_l(n, 1024, IFLA_IPTUN_LOCAL, &laddr, sizeof(laddr));
240 addattr_l(n, 1024, IFLA_IPTUN_REMOTE, &raddr, sizeof(raddr));
241 addattr8(n, 1024, IFLA_IPTUN_TTL, hop_limit);
242 addattr8(n, 1024, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
243 addattr32(n, 1024, IFLA_IPTUN_FLOWINFO, flowinfo);
244 addattr32(n, 1024, IFLA_IPTUN_FLAGS, flags);
245 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
246
247 return 0;
248 }
249
250 static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
251 {
252 char s1[256];
253 char s2[64];
254 int flags = 0;
255 __u32 flowinfo = 0;
256
257 if (!tb)
258 return;
259
260 if (tb[IFLA_IPTUN_FLAGS])
261 flags = rta_getattr_u32(tb[IFLA_IPTUN_FLAGS]);
262
263 if (tb[IFLA_IPTUN_FLOWINFO])
264 flowinfo = rta_getattr_u32(tb[IFLA_IPTUN_FLOWINFO]);
265
266 if (tb[IFLA_IPTUN_PROTO]) {
267 switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
268 case IPPROTO_IPIP:
269 fprintf(f, "ipip6 ");
270 break;
271 case IPPROTO_IPV6:
272 fprintf(f, "ip6ip6 ");
273 break;
274 case 0:
275 fprintf(f, "any ");
276 break;
277 }
278 }
279
280 if (tb[IFLA_IPTUN_REMOTE]) {
281 fprintf(f, "remote %s ",
282 rt_addr_n2a(AF_INET6,
283 RTA_PAYLOAD(tb[IFLA_IPTUN_REMOTE]),
284 RTA_DATA(tb[IFLA_IPTUN_REMOTE]),
285 s1, sizeof(s1)));
286 }
287
288 if (tb[IFLA_IPTUN_LOCAL]) {
289 fprintf(f, "local %s ",
290 rt_addr_n2a(AF_INET6,
291 RTA_PAYLOAD(tb[IFLA_IPTUN_LOCAL]),
292 RTA_DATA(tb[IFLA_IPTUN_LOCAL]),
293 s1, sizeof(s1)));
294 }
295
296 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
297 unsigned link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
298 const char *n = if_indextoname(link, s2);
299
300 if (n)
301 fprintf(f, "dev %s ", n);
302 else
303 fprintf(f, "dev %u ", link);
304 }
305
306 if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
307 printf("encaplimit none ");
308 else if (tb[IFLA_IPTUN_ENCAP_LIMIT])
309 fprintf(f, "encaplimit %u ",
310 rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]));
311
312 if (tb[IFLA_IPTUN_TTL])
313 fprintf(f, "hoplimit %u ", rta_getattr_u8(tb[IFLA_IPTUN_TTL]));
314
315 if (flags & IP6_TNL_F_USE_ORIG_TCLASS)
316 printf("tclass inherit ");
317 else if (tb[IFLA_IPTUN_FLOWINFO]) {
318 __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS);
319
320 printf("tclass 0x%02x ", (__u8)(val >> 20));
321 }
322
323 if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
324 printf("flowlabel inherit ");
325 else
326 printf("flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL));
327
328 printf("(flowinfo 0x%08x) ", ntohl(flowinfo));
329
330 if (flags & IP6_TNL_F_RCV_DSCP_COPY)
331 printf("dscp inherit ");
332
333 if (flags & IP6_TNL_F_MIP6_DEV)
334 fprintf(f, "mip6 ");
335
336 if (flags & IP6_TNL_F_USE_ORIG_FWMARK)
337 fprintf(f, "fwmark inherit ");
338 }
339
340 struct link_util ip6tnl_link_util = {
341 .id = "ip6tnl",
342 .maxattr = IFLA_IPTUN_MAX,
343 .parse_opt = ip6tunnel_parse_opt,
344 .print_opt = ip6tunnel_print_opt,
345 };