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