]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_gre.c
3b99e56f40748ee439f0386d78e5b0c741d229af
[mirror_iproute2.git] / ip / link_gre.c
1 /*
2 * link_gre.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: Herbert Xu <herbert@gondor.apana.org.au>
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 "rt_names.h"
22 #include "utils.h"
23 #include "ip_common.h"
24 #include "tunnel.h"
25
26 static void print_usage(FILE *f)
27 {
28 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
29 fprintf(f, " type { gre | gretap } [ remote ADDR ] [ local ADDR ]\n");
30 fprintf(f, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
31 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
32 fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n");
33 fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n");
34 fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n");
35 fprintf(f, "\n");
36 fprintf(f, "Where: NAME := STRING\n");
37 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
38 fprintf(f, " TOS := { NUMBER | inherit }\n");
39 fprintf(f, " TTL := { 1..255 | inherit }\n");
40 fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n");
41 }
42
43 static void usage(void) __attribute__((noreturn));
44 static void usage(void)
45 {
46 print_usage(stderr);
47 exit(-1);
48 }
49
50 static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
51 struct nlmsghdr *n)
52 {
53 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
54 struct {
55 struct nlmsghdr n;
56 struct ifinfomsg i;
57 char buf[16384];
58 } req = {
59 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
60 .n.nlmsg_flags = NLM_F_REQUEST,
61 .n.nlmsg_type = RTM_GETLINK,
62 .i.ifi_family = preferred_family,
63 .i.ifi_index = ifi->ifi_index,
64 };
65 struct rtattr *tb[IFLA_MAX + 1];
66 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
67 struct rtattr *greinfo[IFLA_GRE_MAX + 1];
68 __u16 iflags = 0;
69 __u16 oflags = 0;
70 unsigned int ikey = 0;
71 unsigned int okey = 0;
72 unsigned int saddr = 0;
73 unsigned int daddr = 0;
74 unsigned int link = 0;
75 __u8 pmtudisc = 1;
76 __u8 ttl = 0;
77 __u8 tos = 0;
78 int len;
79 __u16 encaptype = 0;
80 __u16 encapflags = 0;
81 __u16 encapsport = 0;
82 __u16 encapdport = 0;
83 __u8 metadata = 0;
84
85 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
86 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
87 get_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(greinfo, IFLA_GRE_MAX,
109 linkinfo[IFLA_INFO_DATA]);
110
111 if (greinfo[IFLA_GRE_IKEY])
112 ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
113
114 if (greinfo[IFLA_GRE_OKEY])
115 okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
116
117 if (greinfo[IFLA_GRE_IFLAGS])
118 iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
119
120 if (greinfo[IFLA_GRE_OFLAGS])
121 oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
122
123 if (greinfo[IFLA_GRE_LOCAL])
124 saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]);
125
126 if (greinfo[IFLA_GRE_REMOTE])
127 daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]);
128
129 if (greinfo[IFLA_GRE_PMTUDISC])
130 pmtudisc = rta_getattr_u8(
131 greinfo[IFLA_GRE_PMTUDISC]);
132
133 if (greinfo[IFLA_GRE_TTL])
134 ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
135
136 if (greinfo[IFLA_GRE_TOS])
137 tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]);
138
139 if (greinfo[IFLA_GRE_LINK])
140 link = rta_getattr_u8(greinfo[IFLA_GRE_LINK]);
141
142 if (greinfo[IFLA_GRE_ENCAP_TYPE])
143 encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]);
144 if (greinfo[IFLA_GRE_ENCAP_FLAGS])
145 encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]);
146 if (greinfo[IFLA_GRE_ENCAP_SPORT])
147 encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]);
148 if (greinfo[IFLA_GRE_ENCAP_DPORT])
149 encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]);
150
151 if (greinfo[IFLA_GRE_COLLECT_METADATA])
152 metadata = 1;
153 }
154
155 while (argc > 0) {
156 if (!matches(*argv, "key")) {
157 unsigned int uval;
158
159 NEXT_ARG();
160 iflags |= GRE_KEY;
161 oflags |= GRE_KEY;
162 if (strchr(*argv, '.'))
163 uval = get_addr32(*argv);
164 else {
165 if (get_unsigned(&uval, *argv, 0) < 0) {
166 fprintf(stderr,
167 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
168 exit(-1);
169 }
170 uval = htonl(uval);
171 }
172
173 ikey = okey = uval;
174 } else if (!matches(*argv, "ikey")) {
175 unsigned int uval;
176
177 NEXT_ARG();
178 iflags |= GRE_KEY;
179 if (strchr(*argv, '.'))
180 uval = get_addr32(*argv);
181 else {
182 if (get_unsigned(&uval, *argv, 0) < 0) {
183 fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
184 exit(-1);
185 }
186 uval = htonl(uval);
187 }
188 ikey = uval;
189 } else if (!matches(*argv, "okey")) {
190 unsigned int uval;
191
192 NEXT_ARG();
193 oflags |= GRE_KEY;
194 if (strchr(*argv, '.'))
195 uval = get_addr32(*argv);
196 else {
197 if (get_unsigned(&uval, *argv, 0) < 0) {
198 fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
199 exit(-1);
200 }
201 uval = htonl(uval);
202 }
203 okey = uval;
204 } else if (!matches(*argv, "seq")) {
205 iflags |= GRE_SEQ;
206 oflags |= GRE_SEQ;
207 } else if (!matches(*argv, "iseq")) {
208 iflags |= GRE_SEQ;
209 } else if (!matches(*argv, "oseq")) {
210 oflags |= GRE_SEQ;
211 } else if (!matches(*argv, "csum")) {
212 iflags |= GRE_CSUM;
213 oflags |= GRE_CSUM;
214 } else if (!matches(*argv, "icsum")) {
215 iflags |= GRE_CSUM;
216 } else if (!matches(*argv, "ocsum")) {
217 oflags |= GRE_CSUM;
218 } else if (!matches(*argv, "nopmtudisc")) {
219 pmtudisc = 0;
220 } else if (!matches(*argv, "pmtudisc")) {
221 pmtudisc = 1;
222 } else if (!matches(*argv, "remote")) {
223 NEXT_ARG();
224 if (strcmp(*argv, "any"))
225 daddr = get_addr32(*argv);
226 } else if (!matches(*argv, "local")) {
227 NEXT_ARG();
228 if (strcmp(*argv, "any"))
229 saddr = get_addr32(*argv);
230 } else if (!matches(*argv, "dev")) {
231 NEXT_ARG();
232 link = if_nametoindex(*argv);
233 if (link == 0) {
234 fprintf(stderr, "Cannot find device \"%s\"\n",
235 *argv);
236 exit(-1);
237 }
238 } else if (!matches(*argv, "ttl") ||
239 !matches(*argv, "hoplimit")) {
240 unsigned int uval;
241
242 NEXT_ARG();
243 if (strcmp(*argv, "inherit") != 0) {
244 if (get_unsigned(&uval, *argv, 0))
245 invarg("invalid TTL\n", *argv);
246 if (uval > 255)
247 invarg("TTL must be <= 255\n", *argv);
248 ttl = uval;
249 }
250 } else if (!matches(*argv, "tos") ||
251 !matches(*argv, "tclass") ||
252 !matches(*argv, "dsfield")) {
253 __u32 uval;
254
255 NEXT_ARG();
256 if (strcmp(*argv, "inherit") != 0) {
257 if (rtnl_dsfield_a2n(&uval, *argv))
258 invarg("bad TOS value", *argv);
259 tos = uval;
260 } else
261 tos = 1;
262 } else if (strcmp(*argv, "noencap") == 0) {
263 encaptype = TUNNEL_ENCAP_NONE;
264 } else if (strcmp(*argv, "encap") == 0) {
265 NEXT_ARG();
266 if (strcmp(*argv, "fou") == 0)
267 encaptype = TUNNEL_ENCAP_FOU;
268 else if (strcmp(*argv, "gue") == 0)
269 encaptype = TUNNEL_ENCAP_GUE;
270 else if (strcmp(*argv, "none") == 0)
271 encaptype = TUNNEL_ENCAP_NONE;
272 else
273 invarg("Invalid encap type.", *argv);
274 } else if (strcmp(*argv, "encap-sport") == 0) {
275 NEXT_ARG();
276 if (strcmp(*argv, "auto") == 0)
277 encapsport = 0;
278 else if (get_u16(&encapsport, *argv, 0))
279 invarg("Invalid source port.", *argv);
280 } else if (strcmp(*argv, "encap-dport") == 0) {
281 NEXT_ARG();
282 if (get_u16(&encapdport, *argv, 0))
283 invarg("Invalid destination port.", *argv);
284 } else if (strcmp(*argv, "encap-csum") == 0) {
285 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
286 } else if (strcmp(*argv, "noencap-csum") == 0) {
287 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
288 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
289 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
290 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
291 encapflags |= ~TUNNEL_ENCAP_FLAG_CSUM6;
292 } else if (strcmp(*argv, "encap-remcsum") == 0) {
293 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
294 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
295 encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM;
296 } else if (strcmp(*argv, "external") == 0) {
297 metadata = 1;
298 } else
299 usage();
300 argc--; argv++;
301 }
302
303 if (!ikey && IN_MULTICAST(ntohl(daddr))) {
304 ikey = daddr;
305 iflags |= GRE_KEY;
306 }
307 if (!okey && IN_MULTICAST(ntohl(daddr))) {
308 okey = daddr;
309 oflags |= GRE_KEY;
310 }
311 if (IN_MULTICAST(ntohl(daddr)) && !saddr) {
312 fprintf(stderr, "A broadcast tunnel requires a source address.\n");
313 return -1;
314 }
315
316 if (!metadata) {
317 addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
318 addattr32(n, 1024, IFLA_GRE_OKEY, okey);
319 addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
320 addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
321 addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4);
322 addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4);
323 addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1);
324 if (link)
325 addattr32(n, 1024, IFLA_GRE_LINK, link);
326 addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1);
327 addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1);
328 } else {
329 addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0);
330 }
331
332 addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype);
333 addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags);
334 addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport));
335 addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport));
336
337 return 0;
338 }
339
340 static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
341 {
342 char s2[64];
343 const char *local = "any";
344 const char *remote = "any";
345 unsigned int iflags = 0;
346 unsigned int oflags = 0;
347
348 if (tb[IFLA_GRE_REMOTE]) {
349 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
350
351 if (addr)
352 remote = format_host(AF_INET, 4, &addr);
353 }
354
355 fprintf(f, "remote %s ", remote);
356
357 if (tb[IFLA_GRE_LOCAL]) {
358 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
359
360 if (addr)
361 local = format_host(AF_INET, 4, &addr);
362 }
363
364 fprintf(f, "local %s ", local);
365
366 if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
367 unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
368 const char *n = if_indextoname(link, s2);
369
370 if (n)
371 fprintf(f, "dev %s ", n);
372 else
373 fprintf(f, "dev %u ", link);
374 }
375
376 if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL]))
377 fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_GRE_TTL]));
378 else
379 fprintf(f, "ttl inherit ");
380
381 if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) {
382 int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]);
383
384 fputs("tos ", f);
385 if (tos == 1)
386 fputs("inherit ", f);
387 else
388 fprintf(f, "0x%x ", tos);
389 }
390
391 if (tb[IFLA_GRE_PMTUDISC] &&
392 !rta_getattr_u8(tb[IFLA_GRE_PMTUDISC]))
393 fputs("nopmtudisc ", f);
394
395 if (tb[IFLA_GRE_IFLAGS])
396 iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
397
398 if (tb[IFLA_GRE_OFLAGS])
399 oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
400
401 if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
402 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
403 fprintf(f, "ikey %s ", s2);
404 }
405
406 if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
407 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
408 fprintf(f, "okey %s ", s2);
409 }
410
411 if (iflags & GRE_SEQ)
412 fputs("iseq ", f);
413 if (oflags & GRE_SEQ)
414 fputs("oseq ", f);
415 if (iflags & GRE_CSUM)
416 fputs("icsum ", f);
417 if (oflags & GRE_CSUM)
418 fputs("ocsum ", f);
419 }
420
421 static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
422 {
423 if (!tb)
424 return;
425
426 if (!tb[IFLA_GRE_COLLECT_METADATA])
427 gre_print_direct_opt(f, tb);
428 else
429 fputs("external ", f);
430
431 if (tb[IFLA_GRE_ENCAP_TYPE] &&
432 rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
433 __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]);
434 __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]);
435 __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]);
436 __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]);
437
438 fputs("encap ", f);
439 switch (type) {
440 case TUNNEL_ENCAP_FOU:
441 fputs("fou ", f);
442 break;
443 case TUNNEL_ENCAP_GUE:
444 fputs("gue ", f);
445 break;
446 default:
447 fputs("unknown ", f);
448 break;
449 }
450
451 if (sport == 0)
452 fputs("encap-sport auto ", f);
453 else
454 fprintf(f, "encap-sport %u", ntohs(sport));
455
456 fprintf(f, "encap-dport %u ", ntohs(dport));
457
458 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
459 fputs("encap-csum ", f);
460 else
461 fputs("noencap-csum ", f);
462
463 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
464 fputs("encap-csum6 ", f);
465 else
466 fputs("noencap-csum6 ", f);
467
468 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
469 fputs("encap-remcsum ", f);
470 else
471 fputs("noencap-remcsum ", f);
472 }
473 }
474
475 static void gre_print_help(struct link_util *lu, int argc, char **argv,
476 FILE *f)
477 {
478 print_usage(f);
479 }
480
481 struct link_util gre_link_util = {
482 .id = "gre",
483 .maxattr = IFLA_GRE_MAX,
484 .parse_opt = gre_parse_opt,
485 .print_opt = gre_print_opt,
486 .print_help = gre_print_help,
487 };
488
489 struct link_util gretap_link_util = {
490 .id = "gretap",
491 .maxattr = IFLA_GRE_MAX,
492 .parse_opt = gre_parse_opt,
493 .print_opt = gre_print_opt,
494 .print_help = gre_print_help,
495 };