]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_ip6tnl.c
Use C99 style initializers everywhere
[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 print_usage(FILE *f)
33 {
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");
38 fprintf(f, " [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
39 fprintf(f, " [ dscp inherit ] [ fwmark inherit ]\n");
40 fprintf(f, "\n");
41 fprintf(f, "Where: NAME := STRING\n");
42 fprintf(f, " ADDR := IPV6_ADDRESS\n");
43 fprintf(f, " ELIM := { none | 0..255 }(default=%d)\n",
44 IPV6_DEFAULT_TNL_ENCAP_LIMIT);
45 fprintf(f, " HLIM := 0..255 (default=%d)\n",
46 DEFAULT_TNL_HOP_LIMIT);
47 fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n");
48 fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
49 }
50
51 static void usage(void) __attribute__((noreturn));
52 static void usage(void)
53 {
54 print_usage(stderr);
55 exit(-1);
56 }
57
58 static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
59 struct nlmsghdr *n)
60 {
61 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
62 struct {
63 struct nlmsghdr n;
64 struct ifinfomsg i;
65 char buf[2048];
66 } req = {
67 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
68 .n.nlmsg_flags = NLM_F_REQUEST,
69 .n.nlmsg_type = RTM_GETLINK,
70 .i.ifi_family = preferred_family,
71 .i.ifi_index = ifi->ifi_index,
72 };
73 struct rtattr *tb[IFLA_MAX + 1];
74 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
75 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
76 int len;
77 struct in6_addr laddr = {};
78 struct in6_addr raddr = {};
79 __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
80 __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
81 __u32 flowinfo = 0;
82 __u32 flags = 0;
83 __u32 link = 0;
84 __u8 proto = 0;
85
86 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
87 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 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
158 NEXT_ARG();
159 get_prefix(&addr, *argv, preferred_family);
160 if (addr.family == AF_UNSPEC)
161 invarg("\"remote\" address family is AF_UNSPEC", *argv);
162 memcpy(&raddr, addr.data, addr.bytelen);
163 } else if (strcmp(*argv, "local") == 0) {
164 inet_prefix addr;
165
166 NEXT_ARG();
167 get_prefix(&addr, *argv, preferred_family);
168 if (addr.family == AF_UNSPEC)
169 invarg("\"local\" address family is AF_UNSPEC", *argv);
170 memcpy(&laddr, addr.data, addr.bytelen);
171 } else if (matches(*argv, "dev") == 0) {
172 NEXT_ARG();
173 link = if_nametoindex(*argv);
174 if (link == 0)
175 invarg("\"dev\" is invalid", *argv);
176 } else if (strcmp(*argv, "hoplimit") == 0 ||
177 strcmp(*argv, "ttl") == 0 ||
178 strcmp(*argv, "hlim") == 0) {
179 __u8 uval;
180
181 NEXT_ARG();
182 if (get_u8(&uval, *argv, 0))
183 invarg("invalid HLIM", *argv);
184 hop_limit = uval;
185 } else if (matches(*argv, "encaplimit") == 0) {
186 NEXT_ARG();
187 if (strcmp(*argv, "none") == 0) {
188 flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
189 } else {
190 __u8 uval;
191
192 if (get_u8(&uval, *argv, 0) < -1)
193 invarg("invalid ELIM", *argv);
194 encap_limit = uval;
195 flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
196 }
197 } else if (strcmp(*argv, "tclass") == 0 ||
198 strcmp(*argv, "tc") == 0 ||
199 strcmp(*argv, "tos") == 0 ||
200 matches(*argv, "dsfield") == 0) {
201 __u8 uval;
202
203 NEXT_ARG();
204 flowinfo &= ~IP6_FLOWINFO_TCLASS;
205 if (strcmp(*argv, "inherit") == 0)
206 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
207 else {
208 if (get_u8(&uval, *argv, 16))
209 invarg("invalid TClass", *argv);
210 flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
211 flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
212 }
213 } else if (strcmp(*argv, "flowlabel") == 0 ||
214 strcmp(*argv, "fl") == 0) {
215 __u32 uval;
216
217 NEXT_ARG();
218 flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
219 if (strcmp(*argv, "inherit") == 0)
220 flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
221 else {
222 if (get_u32(&uval, *argv, 16))
223 invarg("invalid Flowlabel", *argv);
224 if (uval > 0xFFFFF)
225 invarg("invalid Flowlabel", *argv);
226 flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
227 flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
228 }
229 } else if (strcmp(*argv, "dscp") == 0) {
230 NEXT_ARG();
231 if (strcmp(*argv, "inherit") != 0)
232 invarg("not inherit", *argv);
233 flags |= IP6_TNL_F_RCV_DSCP_COPY;
234 } else if (strcmp(*argv, "fwmark") == 0) {
235 NEXT_ARG();
236 if (strcmp(*argv, "inherit") != 0)
237 invarg("not inherit", *argv);
238 flags |= IP6_TNL_F_USE_ORIG_FWMARK;
239 } else
240 usage();
241 argc--, argv++;
242 }
243
244 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
245 addattr_l(n, 1024, IFLA_IPTUN_LOCAL, &laddr, sizeof(laddr));
246 addattr_l(n, 1024, IFLA_IPTUN_REMOTE, &raddr, sizeof(raddr));
247 addattr8(n, 1024, IFLA_IPTUN_TTL, hop_limit);
248 addattr8(n, 1024, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
249 addattr32(n, 1024, IFLA_IPTUN_FLOWINFO, flowinfo);
250 addattr32(n, 1024, IFLA_IPTUN_FLAGS, flags);
251 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
252
253 return 0;
254 }
255
256 static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
257 {
258 char s2[64];
259 int flags = 0;
260 __u32 flowinfo = 0;
261
262 if (!tb)
263 return;
264
265 if (tb[IFLA_IPTUN_FLAGS])
266 flags = rta_getattr_u32(tb[IFLA_IPTUN_FLAGS]);
267
268 if (tb[IFLA_IPTUN_FLOWINFO])
269 flowinfo = rta_getattr_u32(tb[IFLA_IPTUN_FLOWINFO]);
270
271 if (tb[IFLA_IPTUN_PROTO]) {
272 switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
273 case IPPROTO_IPIP:
274 fprintf(f, "ipip6 ");
275 break;
276 case IPPROTO_IPV6:
277 fprintf(f, "ip6ip6 ");
278 break;
279 case 0:
280 fprintf(f, "any ");
281 break;
282 }
283 }
284
285 if (tb[IFLA_IPTUN_REMOTE]) {
286 fprintf(f, "remote %s ",
287 rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
288 }
289
290 if (tb[IFLA_IPTUN_LOCAL]) {
291 fprintf(f, "local %s ",
292 rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
293 }
294
295 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
296 unsigned int 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
339 static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv,
340 FILE *f)
341 {
342 print_usage(f);
343 }
344
345 struct link_util ip6tnl_link_util = {
346 .id = "ip6tnl",
347 .maxattr = IFLA_IPTUN_MAX,
348 .parse_opt = ip6tunnel_parse_opt,
349 .print_opt = ip6tunnel_print_opt,
350 .print_help = ip6tunnel_print_help,
351 };