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