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