2 * link_gre.c gre driver module
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.
9 * Authors: Herbert Xu <herbert@gondor.apana.org.au>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
20 #include <linux/if_tunnel.h>
23 #include "ip_common.h"
26 static void print_usage(FILE *f
)
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");
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");
43 static void usage(void) __attribute__((noreturn
));
44 static void usage(void)
50 static int gre_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
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];
64 unsigned int ikey
= 0;
65 unsigned int okey
= 0;
66 unsigned int saddr
= 0;
67 unsigned int daddr
= 0;
68 unsigned int link
= 0;
79 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
80 memset(&req
, 0, sizeof(req
));
82 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
));
83 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
84 req
.n
.nlmsg_type
= RTM_GETLINK
;
85 req
.i
.ifi_family
= preferred_family
;
86 req
.i
.ifi_index
= ifi
->ifi_index
;
88 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
91 "Failed to get existing tunnel info.\n");
95 len
= req
.n
.nlmsg_len
;
96 len
-= NLMSG_LENGTH(sizeof(*ifi
));
100 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
102 if (!tb
[IFLA_LINKINFO
])
105 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
107 if (!linkinfo
[IFLA_INFO_DATA
])
110 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
111 linkinfo
[IFLA_INFO_DATA
]);
113 if (greinfo
[IFLA_GRE_IKEY
])
114 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
116 if (greinfo
[IFLA_GRE_OKEY
])
117 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
119 if (greinfo
[IFLA_GRE_IFLAGS
])
120 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
122 if (greinfo
[IFLA_GRE_OFLAGS
])
123 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
125 if (greinfo
[IFLA_GRE_LOCAL
])
126 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
128 if (greinfo
[IFLA_GRE_REMOTE
])
129 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
131 if (greinfo
[IFLA_GRE_PMTUDISC
])
132 pmtudisc
= rta_getattr_u8(
133 greinfo
[IFLA_GRE_PMTUDISC
]);
135 if (greinfo
[IFLA_GRE_TTL
])
136 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
138 if (greinfo
[IFLA_GRE_TOS
])
139 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
141 if (greinfo
[IFLA_GRE_LINK
])
142 link
= rta_getattr_u8(greinfo
[IFLA_GRE_LINK
]);
144 if (greinfo
[IFLA_GRE_ENCAP_TYPE
])
145 encaptype
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_TYPE
]);
146 if (greinfo
[IFLA_GRE_ENCAP_FLAGS
])
147 encapflags
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_FLAGS
]);
148 if (greinfo
[IFLA_GRE_ENCAP_SPORT
])
149 encapsport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_SPORT
]);
150 if (greinfo
[IFLA_GRE_ENCAP_DPORT
])
151 encapdport
= rta_getattr_u16(greinfo
[IFLA_GRE_ENCAP_DPORT
]);
153 if (greinfo
[IFLA_GRE_COLLECT_METADATA
])
158 if (!matches(*argv
, "key")) {
164 if (strchr(*argv
, '.'))
165 uval
= get_addr32(*argv
);
167 if (get_unsigned(&uval
, *argv
, 0) < 0) {
169 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
176 } else if (!matches(*argv
, "ikey")) {
181 if (strchr(*argv
, '.'))
182 uval
= get_addr32(*argv
);
184 if (get_unsigned(&uval
, *argv
, 0) < 0) {
185 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
191 } else if (!matches(*argv
, "okey")) {
196 if (strchr(*argv
, '.'))
197 uval
= get_addr32(*argv
);
199 if (get_unsigned(&uval
, *argv
, 0) < 0) {
200 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
206 } else if (!matches(*argv
, "seq")) {
209 } else if (!matches(*argv
, "iseq")) {
211 } else if (!matches(*argv
, "oseq")) {
213 } else if (!matches(*argv
, "csum")) {
216 } else if (!matches(*argv
, "icsum")) {
218 } else if (!matches(*argv
, "ocsum")) {
220 } else if (!matches(*argv
, "nopmtudisc")) {
222 } else if (!matches(*argv
, "pmtudisc")) {
224 } else if (!matches(*argv
, "remote")) {
226 if (strcmp(*argv
, "any"))
227 daddr
= get_addr32(*argv
);
228 } else if (!matches(*argv
, "local")) {
230 if (strcmp(*argv
, "any"))
231 saddr
= get_addr32(*argv
);
232 } else if (!matches(*argv
, "dev")) {
234 link
= if_nametoindex(*argv
);
236 fprintf(stderr
, "Cannot find device \"%s\"\n",
240 } else if (!matches(*argv
, "ttl") ||
241 !matches(*argv
, "hoplimit")) {
245 if (strcmp(*argv
, "inherit") != 0) {
246 if (get_unsigned(&uval
, *argv
, 0))
247 invarg("invalid TTL\n", *argv
);
249 invarg("TTL must be <= 255\n", *argv
);
252 } else if (!matches(*argv
, "tos") ||
253 !matches(*argv
, "tclass") ||
254 !matches(*argv
, "dsfield")) {
258 if (strcmp(*argv
, "inherit") != 0) {
259 if (rtnl_dsfield_a2n(&uval
, *argv
))
260 invarg("bad TOS value", *argv
);
264 } else if (strcmp(*argv
, "noencap") == 0) {
265 encaptype
= TUNNEL_ENCAP_NONE
;
266 } else if (strcmp(*argv
, "encap") == 0) {
268 if (strcmp(*argv
, "fou") == 0)
269 encaptype
= TUNNEL_ENCAP_FOU
;
270 else if (strcmp(*argv
, "gue") == 0)
271 encaptype
= TUNNEL_ENCAP_GUE
;
272 else if (strcmp(*argv
, "none") == 0)
273 encaptype
= TUNNEL_ENCAP_NONE
;
275 invarg("Invalid encap type.", *argv
);
276 } else if (strcmp(*argv
, "encap-sport") == 0) {
278 if (strcmp(*argv
, "auto") == 0)
280 else if (get_u16(&encapsport
, *argv
, 0))
281 invarg("Invalid source port.", *argv
);
282 } else if (strcmp(*argv
, "encap-dport") == 0) {
284 if (get_u16(&encapdport
, *argv
, 0))
285 invarg("Invalid destination port.", *argv
);
286 } else if (strcmp(*argv
, "encap-csum") == 0) {
287 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
288 } else if (strcmp(*argv
, "noencap-csum") == 0) {
289 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
290 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
291 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
292 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
293 encapflags
|= ~TUNNEL_ENCAP_FLAG_CSUM6
;
294 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
295 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
296 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
297 encapflags
|= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
298 } else if (strcmp(*argv
, "external") == 0) {
305 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
309 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
313 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
314 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
318 addattr32(n
, 1024, IFLA_GRE_IKEY
, ikey
);
319 addattr32(n
, 1024, IFLA_GRE_OKEY
, okey
);
320 addattr_l(n
, 1024, IFLA_GRE_IFLAGS
, &iflags
, 2);
321 addattr_l(n
, 1024, IFLA_GRE_OFLAGS
, &oflags
, 2);
322 addattr_l(n
, 1024, IFLA_GRE_LOCAL
, &saddr
, 4);
323 addattr_l(n
, 1024, IFLA_GRE_REMOTE
, &daddr
, 4);
324 addattr_l(n
, 1024, IFLA_GRE_PMTUDISC
, &pmtudisc
, 1);
326 addattr32(n
, 1024, IFLA_GRE_LINK
, link
);
327 addattr_l(n
, 1024, IFLA_GRE_TTL
, &ttl
, 1);
328 addattr_l(n
, 1024, IFLA_GRE_TOS
, &tos
, 1);
330 addattr16(n
, 1024, IFLA_GRE_ENCAP_TYPE
, encaptype
);
331 addattr16(n
, 1024, IFLA_GRE_ENCAP_FLAGS
, encapflags
);
332 addattr16(n
, 1024, IFLA_GRE_ENCAP_SPORT
, htons(encapsport
));
333 addattr16(n
, 1024, IFLA_GRE_ENCAP_DPORT
, htons(encapdport
));
335 addattr_l(n
, 1024, IFLA_GRE_COLLECT_METADATA
, NULL
, 0);
340 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
344 const char *local
= "any";
345 const char *remote
= "any";
346 unsigned int iflags
= 0;
347 unsigned int oflags
= 0;
352 if (tb
[IFLA_GRE_REMOTE
]) {
353 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
356 remote
= format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
));
359 fprintf(f
, "remote %s ", remote
);
361 if (tb
[IFLA_GRE_LOCAL
]) {
362 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
365 local
= format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
));
368 fprintf(f
, "local %s ", local
);
370 if (tb
[IFLA_GRE_LINK
] && rta_getattr_u32(tb
[IFLA_GRE_LINK
])) {
371 unsigned int link
= rta_getattr_u32(tb
[IFLA_GRE_LINK
]);
372 const char *n
= if_indextoname(link
, s2
);
375 fprintf(f
, "dev %s ", n
);
377 fprintf(f
, "dev %u ", link
);
380 if (tb
[IFLA_GRE_TTL
] && rta_getattr_u8(tb
[IFLA_GRE_TTL
]))
381 fprintf(f
, "ttl %d ", rta_getattr_u8(tb
[IFLA_GRE_TTL
]));
383 fprintf(f
, "ttl inherit ");
385 if (tb
[IFLA_GRE_TOS
] && rta_getattr_u8(tb
[IFLA_GRE_TOS
])) {
386 int tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
390 fputs("inherit ", f
);
392 fprintf(f
, "0x%x ", tos
);
395 if (tb
[IFLA_GRE_PMTUDISC
] &&
396 !rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
397 fputs("nopmtudisc ", f
);
399 if (tb
[IFLA_GRE_IFLAGS
])
400 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
402 if (tb
[IFLA_GRE_OFLAGS
])
403 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
405 if ((iflags
& GRE_KEY
) && tb
[IFLA_GRE_IKEY
]) {
406 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_IKEY
]), s2
, sizeof(s2
));
407 fprintf(f
, "ikey %s ", s2
);
410 if ((oflags
& GRE_KEY
) && tb
[IFLA_GRE_OKEY
]) {
411 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_GRE_OKEY
]), s2
, sizeof(s2
));
412 fprintf(f
, "okey %s ", s2
);
415 if (iflags
& GRE_SEQ
)
417 if (oflags
& GRE_SEQ
)
419 if (iflags
& GRE_CSUM
)
421 if (oflags
& GRE_CSUM
)
424 if (tb
[IFLA_GRE_COLLECT_METADATA
])
425 fputs("external ", f
);
427 if (tb
[IFLA_GRE_ENCAP_TYPE
] &&
428 *(__u16
*)RTA_DATA(tb
[IFLA_GRE_ENCAP_TYPE
]) != TUNNEL_ENCAP_NONE
) {
429 __u16 type
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_TYPE
]);
430 __u16 flags
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_FLAGS
]);
431 __u16 sport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_SPORT
]);
432 __u16 dport
= rta_getattr_u16(tb
[IFLA_GRE_ENCAP_DPORT
]);
436 case TUNNEL_ENCAP_FOU
:
439 case TUNNEL_ENCAP_GUE
:
443 fputs("unknown ", f
);
448 fputs("encap-sport auto ", f
);
450 fprintf(f
, "encap-sport %u", ntohs(sport
));
452 fprintf(f
, "encap-dport %u ", ntohs(dport
));
454 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
455 fputs("encap-csum ", f
);
457 fputs("noencap-csum ", f
);
459 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
460 fputs("encap-csum6 ", f
);
462 fputs("noencap-csum6 ", f
);
464 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
465 fputs("encap-remcsum ", f
);
467 fputs("noencap-remcsum ", f
);
471 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
477 struct link_util gre_link_util
= {
479 .maxattr
= IFLA_GRE_MAX
,
480 .parse_opt
= gre_parse_opt
,
481 .print_opt
= gre_print_opt
,
482 .print_help
= gre_print_help
,
485 struct link_util gretap_link_util
= {
487 .maxattr
= IFLA_GRE_MAX
,
488 .parse_opt
= gre_parse_opt
,
489 .print_opt
= gre_print_opt
,
490 .print_help
= gre_print_help
,