]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/link_gre6.c
tc: m_action: Improve conversion to C99 style initializers
[mirror_iproute2.git] / ip / link_gre6.c
CommitLineData
af89576d 1/*
2 * link_gre6.c gre 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: Dmitry Kozlov <xeb@mail.ru>
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
23#include "rt_names.h"
24#include "utils.h"
25#include "ip_common.h"
26#include "tunnel.h"
27
28#define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
29#define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
30
31#define DEFAULT_TNL_HOP_LIMIT (64)
32
561e650e 33static void print_usage(FILE *f)
af89576d 34{
561e650e 35 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
36 fprintf(f, " type { ip6gre | ip6gretap } [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(f, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
38 fprintf(f, " [ hoplimit TTL ] [ encaplimit ELIM ]\n");
39 fprintf(f, " [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
40 fprintf(f, " [ dscp inherit ] [ dev PHYS_DEV ]\n");
41 fprintf(f, "\n");
42 fprintf(f, "Where: NAME := STRING\n");
43 fprintf(f, " ADDR := IPV6_ADDRESS\n");
44 fprintf(f, " TTL := { 0..255 } (default=%d)\n",
af89576d 45 DEFAULT_TNL_HOP_LIMIT);
561e650e 46 fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n");
47 fprintf(f, " ELIM := { none | 0..255 }(default=%d)\n",
af89576d 48 IPV6_DEFAULT_TNL_ENCAP_LIMIT);
561e650e 49 fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n");
50 fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
51}
52
53static void usage(void) __attribute__((noreturn));
54static void usage(void)
55{
56 print_usage(stderr);
af89576d 57 exit(-1);
58}
59
60static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
61 struct nlmsghdr *n)
62{
63 struct {
64 struct nlmsghdr n;
65 struct ifinfomsg i;
66 char buf[1024];
67 } req;
68 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
69 struct rtattr *tb[IFLA_MAX + 1];
70 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
71 struct rtattr *greinfo[IFLA_GRE_MAX + 1];
72 __u16 iflags = 0;
73 __u16 oflags = 0;
56f5daac
SH
74 unsigned int ikey = 0;
75 unsigned int okey = 0;
af89576d 76 struct in6_addr raddr = IN6ADDR_ANY_INIT;
77 struct in6_addr laddr = IN6ADDR_ANY_INIT;
56f5daac
SH
78 unsigned int link = 0;
79 unsigned int flowinfo = 0;
80 unsigned int flags = 0;
af89576d 81 __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
82 __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
83 int len;
84
85 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
86 memset(&req, 0, sizeof(req));
87
88 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
89 req.n.nlmsg_flags = NLM_F_REQUEST;
90 req.n.nlmsg_type = RTM_GETLINK;
91 req.i.ifi_family = preferred_family;
92 req.i.ifi_index = ifi->ifi_index;
93
c079e121 94 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
af89576d 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(greinfo, IFLA_GRE_MAX,
117 linkinfo[IFLA_INFO_DATA]);
118
119 if (greinfo[IFLA_GRE_IKEY])
120 ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
121
122 if (greinfo[IFLA_GRE_OKEY])
123 okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
124
125 if (greinfo[IFLA_GRE_IFLAGS])
126 iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
127
128 if (greinfo[IFLA_GRE_OFLAGS])
129 oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
130
131 if (greinfo[IFLA_GRE_LOCAL])
132 memcpy(&laddr, RTA_DATA(greinfo[IFLA_GRE_LOCAL]), sizeof(laddr));
133
134 if (greinfo[IFLA_GRE_REMOTE])
135 memcpy(&raddr, RTA_DATA(greinfo[IFLA_GRE_REMOTE]), sizeof(raddr));
136
137 if (greinfo[IFLA_GRE_TTL])
138 hop_limit = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
139
140 if (greinfo[IFLA_GRE_LINK])
141 link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]);
142
143 if (greinfo[IFLA_GRE_ENCAP_LIMIT])
144 encap_limit = rta_getattr_u8(greinfo[IFLA_GRE_ENCAP_LIMIT]);
145
146 if (greinfo[IFLA_GRE_FLOWINFO])
147 flowinfo = rta_getattr_u32(greinfo[IFLA_GRE_FLOWINFO]);
148
149 if (greinfo[IFLA_GRE_FLAGS])
150 flags = rta_getattr_u32(greinfo[IFLA_GRE_FLAGS]);
151 }
152
153 while (argc > 0) {
154 if (!matches(*argv, "key")) {
56f5daac 155 unsigned int uval;
af89576d 156
157 NEXT_ARG();
158 iflags |= GRE_KEY;
159 oflags |= GRE_KEY;
160 if (strchr(*argv, '.'))
161 uval = get_addr32(*argv);
162 else {
163 if (get_unsigned(&uval, *argv, 0) < 0) {
164 fprintf(stderr,
165 "Invalid value for \"key\"\n");
166 exit(-1);
167 }
168 uval = htonl(uval);
169 }
170
171 ikey = okey = uval;
172 } else if (!matches(*argv, "ikey")) {
56f5daac 173 unsigned int uval;
af89576d 174
175 NEXT_ARG();
176 iflags |= GRE_KEY;
177 if (strchr(*argv, '.'))
178 uval = get_addr32(*argv);
179 else {
56f5daac 180 if (get_unsigned(&uval, *argv, 0) < 0) {
af89576d 181 fprintf(stderr, "invalid value of \"ikey\"\n");
182 exit(-1);
183 }
184 uval = htonl(uval);
185 }
186 ikey = uval;
187 } else if (!matches(*argv, "okey")) {
56f5daac 188 unsigned int uval;
af89576d 189
190 NEXT_ARG();
191 oflags |= GRE_KEY;
192 if (strchr(*argv, '.'))
193 uval = get_addr32(*argv);
194 else {
56f5daac 195 if (get_unsigned(&uval, *argv, 0) < 0) {
af89576d 196 fprintf(stderr, "invalid value of \"okey\"\n");
197 exit(-1);
198 }
199 uval = htonl(uval);
200 }
201 okey = uval;
202 } else if (!matches(*argv, "seq")) {
203 iflags |= GRE_SEQ;
204 oflags |= GRE_SEQ;
205 } else if (!matches(*argv, "iseq")) {
206 iflags |= GRE_SEQ;
207 } else if (!matches(*argv, "oseq")) {
208 oflags |= GRE_SEQ;
209 } else if (!matches(*argv, "csum")) {
210 iflags |= GRE_CSUM;
211 oflags |= GRE_CSUM;
212 } else if (!matches(*argv, "icsum")) {
213 iflags |= GRE_CSUM;
214 } else if (!matches(*argv, "ocsum")) {
215 oflags |= GRE_CSUM;
216 } else if (!matches(*argv, "remote")) {
217 inet_prefix addr;
56f5daac 218
af89576d 219 NEXT_ARG();
220 get_prefix(&addr, *argv, preferred_family);
221 if (addr.family == AF_UNSPEC)
222 invarg("\"remote\" address family is AF_UNSPEC", *argv);
223 memcpy(&raddr, &addr.data, sizeof(raddr));
224 } else if (!matches(*argv, "local")) {
225 inet_prefix addr;
56f5daac 226
af89576d 227 NEXT_ARG();
228 get_prefix(&addr, *argv, preferred_family);
229 if (addr.family == AF_UNSPEC)
230 invarg("\"local\" address family is AF_UNSPEC", *argv);
231 memcpy(&laddr, &addr.data, sizeof(laddr));
232 } else if (!matches(*argv, "dev")) {
233 NEXT_ARG();
234 link = if_nametoindex(*argv);
0cb6bb51
CW
235 if (link == 0) {
236 fprintf(stderr, "Cannot find device \"%s\"\n",
237 *argv);
af89576d 238 exit(-1);
0cb6bb51 239 }
af89576d 240 } else if (!matches(*argv, "ttl") ||
241 !matches(*argv, "hoplimit")) {
242 __u8 uval;
56f5daac 243
af89576d 244 NEXT_ARG();
245 if (get_u8(&uval, *argv, 0))
246 invarg("invalid TTL", *argv);
247 hop_limit = uval;
248 } else if (!matches(*argv, "tos") ||
249 !matches(*argv, "tclass") ||
250 !matches(*argv, "dsfield")) {
251 __u8 uval;
56f5daac 252
af89576d 253 NEXT_ARG();
254 if (strcmp(*argv, "inherit") == 0)
255 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
256 else {
257 if (get_u8(&uval, *argv, 16))
258 invarg("invalid TClass", *argv);
259 flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
260 flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
261 }
262 } else if (strcmp(*argv, "flowlabel") == 0 ||
263 strcmp(*argv, "fl") == 0) {
264 __u32 uval;
56f5daac 265
af89576d 266 NEXT_ARG();
267 if (strcmp(*argv, "inherit") == 0)
268 flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
269 else {
270 if (get_u32(&uval, *argv, 16))
271 invarg("invalid Flowlabel", *argv);
272 if (uval > 0xFFFFF)
273 invarg("invalid Flowlabel", *argv);
274 flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
275 flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
276 }
277 } else if (strcmp(*argv, "dscp") == 0) {
278 NEXT_ARG();
279 if (strcmp(*argv, "inherit") != 0)
280 invarg("not inherit", *argv);
281 flags |= IP6_TNL_F_RCV_DSCP_COPY;
282 } else
283 usage();
284 argc--; argv++;
285 }
286
287 addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
288 addattr32(n, 1024, IFLA_GRE_OKEY, okey);
289 addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
290 addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
291 addattr_l(n, 1024, IFLA_GRE_LOCAL, &laddr, sizeof(laddr));
292 addattr_l(n, 1024, IFLA_GRE_REMOTE, &raddr, sizeof(raddr));
293 if (link)
294 addattr32(n, 1024, IFLA_GRE_LINK, link);
295 addattr_l(n, 1024, IFLA_GRE_TTL, &hop_limit, 1);
296 addattr_l(n, 1024, IFLA_GRE_ENCAP_LIMIT, &encap_limit, 1);
297 addattr_l(n, 1024, IFLA_GRE_FLOWINFO, &flowinfo, 4);
298 addattr_l(n, 1024, IFLA_GRE_FLAGS, &flowinfo, 4);
299
300 return 0;
301}
302
303static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
304{
af89576d 305 char s2[64];
306 const char *local = "any";
307 const char *remote = "any";
56f5daac
SH
308 unsigned int iflags = 0;
309 unsigned int oflags = 0;
310 unsigned int flags = 0;
311 unsigned int flowinfo = 0;
af89576d 312 struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT;
313
314 if (!tb)
315 return;
316
317 if (tb[IFLA_GRE_FLAGS])
318 flags = rta_getattr_u32(tb[IFLA_GRE_FLAGS]);
319
320 if (tb[IFLA_GRE_FLOWINFO])
321 flags = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]);
322
323 if (tb[IFLA_GRE_REMOTE]) {
324 struct in6_addr addr;
56f5daac 325
af89576d 326 memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr));
327
328 if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
a418e451 329 remote = format_host(AF_INET6, sizeof(addr), &addr);
af89576d 330 }
331
332 fprintf(f, "remote %s ", remote);
333
334 if (tb[IFLA_GRE_LOCAL]) {
335 struct in6_addr addr;
56f5daac 336
af89576d 337 memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr));
338
339 if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
a418e451 340 local = format_host(AF_INET6, sizeof(addr), &addr);
af89576d 341 }
342
343 fprintf(f, "local %s ", local);
344
345 if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
56f5daac 346 unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
af89576d 347 const char *n = if_indextoname(link, s2);
348
349 if (n)
350 fprintf(f, "dev %s ", n);
351 else
352 fprintf(f, "dev %u ", link);
353 }
354
355 if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL]))
356 fprintf(f, "hoplimit %d ", rta_getattr_u8(tb[IFLA_GRE_TTL]));
357
358 if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
359 fprintf(f, "encaplimit none ");
360 else if (tb[IFLA_GRE_ENCAP_LIMIT]) {
361 int encap_limit = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]);
362
363 fprintf(f, "encaplimit %d ", encap_limit);
364 }
365
366 if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
367 fprintf(f, "flowlabel inherit ");
368 else
369 fprintf(f, "flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL));
370
371 if (flags & IP6_TNL_F_RCV_DSCP_COPY)
372 fprintf(f, "dscp inherit ");
373
374 if (tb[IFLA_GRE_IFLAGS])
375 iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
376
377 if (tb[IFLA_GRE_OFLAGS])
378 oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
379
380 if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
381 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
382 fprintf(f, "ikey %s ", s2);
383 }
384
385 if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
386 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
387 fprintf(f, "okey %s ", s2);
388 }
389
390 if (iflags & GRE_SEQ)
391 fputs("iseq ", f);
392 if (oflags & GRE_SEQ)
393 fputs("oseq ", f);
394 if (iflags & GRE_CSUM)
395 fputs("icsum ", f);
396 if (oflags & GRE_CSUM)
397 fputs("ocsum ", f);
398}
399
561e650e 400static void gre_print_help(struct link_util *lu, int argc, char **argv,
401 FILE *f)
402{
403 print_usage(f);
404}
405
af89576d 406struct link_util ip6gre_link_util = {
407 .id = "ip6gre",
408 .maxattr = IFLA_GRE_MAX,
409 .parse_opt = gre_parse_opt,
410 .print_opt = gre_print_opt,
561e650e 411 .print_help = gre_print_help,
af89576d 412};
413
414struct link_util ip6gretap_link_util = {
415 .id = "ip6gretap",
416 .maxattr = IFLA_GRE_MAX,
417 .parse_opt = gre_parse_opt,
418 .print_opt = gre_print_opt,
561e650e 419 .print_help = gre_print_help,
af89576d 420};