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];
78 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
79 memset(&req
, 0, sizeof(req
));
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
;
87 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 0) {
90 "Failed to get existing tunnel info.\n");
94 len
= req
.n
.nlmsg_len
;
95 len
-= NLMSG_LENGTH(sizeof(*ifi
));
99 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
101 if (!tb
[IFLA_LINKINFO
])
104 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
106 if (!linkinfo
[IFLA_INFO_DATA
])
109 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
110 linkinfo
[IFLA_INFO_DATA
]);
112 if (greinfo
[IFLA_GRE_IKEY
])
113 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
115 if (greinfo
[IFLA_GRE_OKEY
])
116 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
118 if (greinfo
[IFLA_GRE_IFLAGS
])
119 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
121 if (greinfo
[IFLA_GRE_OFLAGS
])
122 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
124 if (greinfo
[IFLA_GRE_LOCAL
])
125 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
127 if (greinfo
[IFLA_GRE_REMOTE
])
128 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
130 if (greinfo
[IFLA_GRE_PMTUDISC
])
131 pmtudisc
= rta_getattr_u8(
132 greinfo
[IFLA_GRE_PMTUDISC
]);
134 if (greinfo
[IFLA_GRE_TTL
])
135 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
137 if (greinfo
[IFLA_GRE_TOS
])
138 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
140 if (greinfo
[IFLA_GRE_LINK
])
141 link
= rta_getattr_u8(greinfo
[IFLA_GRE_LINK
]);
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
]);
154 if (!matches(*argv
, "key")) {
160 if (strchr(*argv
, '.'))
161 uval
= get_addr32(*argv
);
163 if (get_unsigned(&uval
, *argv
, 0) < 0) {
165 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
172 } else if (!matches(*argv
, "ikey")) {
177 if (strchr(*argv
, '.'))
178 uval
= get_addr32(*argv
);
180 if (get_unsigned(&uval
, *argv
, 0)<0) {
181 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
187 } else if (!matches(*argv
, "okey")) {
192 if (strchr(*argv
, '.'))
193 uval
= get_addr32(*argv
);
195 if (get_unsigned(&uval
, *argv
, 0)<0) {
196 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
202 } else if (!matches(*argv
, "seq")) {
205 } else if (!matches(*argv
, "iseq")) {
207 } else if (!matches(*argv
, "oseq")) {
209 } else if (!matches(*argv
, "csum")) {
212 } else if (!matches(*argv
, "icsum")) {
214 } else if (!matches(*argv
, "ocsum")) {
216 } else if (!matches(*argv
, "nopmtudisc")) {
218 } else if (!matches(*argv
, "pmtudisc")) {
220 } else if (!matches(*argv
, "remote")) {
222 if (strcmp(*argv
, "any"))
223 daddr
= get_addr32(*argv
);
224 } else if (!matches(*argv
, "local")) {
226 if (strcmp(*argv
, "any"))
227 saddr
= get_addr32(*argv
);
228 } else if (!matches(*argv
, "dev")) {
230 link
= if_nametoindex(*argv
);
232 fprintf(stderr
, "Cannot find device \"%s\"\n",
236 } else if (!matches(*argv
, "ttl") ||
237 !matches(*argv
, "hoplimit")) {
241 if (strcmp(*argv
, "inherit") != 0) {
242 if (get_unsigned(&uval
, *argv
, 0))
243 invarg("invalid TTL\n", *argv
);
245 invarg("TTL must be <= 255\n", *argv
);
248 } else if (!matches(*argv
, "tos") ||
249 !matches(*argv
, "tclass") ||
250 !matches(*argv
, "dsfield")) {
254 if (strcmp(*argv
, "inherit") != 0) {
255 if (rtnl_dsfield_a2n(&uval
, *argv
))
256 invarg("bad TOS value", *argv
);
260 } else if (strcmp(*argv
, "noencap") == 0) {
261 encaptype
= TUNNEL_ENCAP_NONE
;
262 } else if (strcmp(*argv
, "encap") == 0) {
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
;
271 invarg("Invalid encap type.", *argv
);
272 } else if (strcmp(*argv
, "encap-sport") == 0) {
274 if (strcmp(*argv
, "auto") == 0)
276 else if (get_u16(&encapsport
, *argv
, 0))
277 invarg("Invalid source port.", *argv
);
278 } else if (strcmp(*argv
, "encap-dport") == 0) {
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
;
299 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
303 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
307 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
308 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
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);
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);
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
));
332 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
336 const char *local
= "any";
337 const char *remote
= "any";
344 if (tb
[IFLA_GRE_REMOTE
]) {
345 unsigned addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
348 remote
= format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
));
351 fprintf(f
, "remote %s ", remote
);
353 if (tb
[IFLA_GRE_LOCAL
]) {
354 unsigned addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
357 local
= format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
));
360 fprintf(f
, "local %s ", local
);
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
);
367 fprintf(f
, "dev %s ", n
);
369 fprintf(f
, "dev %u ", link
);
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
]));
375 fprintf(f
, "ttl inherit ");
377 if (tb
[IFLA_GRE_TOS
] && rta_getattr_u8(tb
[IFLA_GRE_TOS
])) {
378 int tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
382 fputs("inherit ", f
);
384 fprintf(f
, "0x%x ", tos
);
387 if (tb
[IFLA_GRE_PMTUDISC
] &&
388 !rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
389 fputs("nopmtudisc ", f
);
391 if (tb
[IFLA_GRE_IFLAGS
])
392 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
394 if (tb
[IFLA_GRE_OFLAGS
])
395 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
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
);
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
);
407 if (iflags
& GRE_SEQ
)
409 if (oflags
& GRE_SEQ
)
411 if (iflags
& GRE_CSUM
)
413 if (oflags
& GRE_CSUM
)
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
]);
425 case TUNNEL_ENCAP_FOU
:
428 case TUNNEL_ENCAP_GUE
:
432 fputs("unknown ", f
);
437 fputs("encap-sport auto ", f
);
439 fprintf(f
, "encap-sport %u", ntohs(sport
));
441 fprintf(f
, "encap-dport %u ", ntohs(dport
));
443 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
444 fputs("encap-csum ", f
);
446 fputs("noencap-csum ", f
);
448 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
449 fputs("encap-csum6 ", f
);
451 fputs("noencap-csum6 ", f
);
453 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
454 fputs("encap-remcsum ", f
);
456 fputs("noencap-remcsum ", f
);
460 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
466 struct link_util gre_link_util
= {
468 .maxattr
= IFLA_GRE_MAX
,
469 .parse_opt
= gre_parse_opt
,
470 .print_opt
= gre_print_opt
,
471 .print_help
= gre_print_help
,
474 struct link_util gretap_link_util
= {
476 .maxattr
= IFLA_GRE_MAX
,
477 .parse_opt
= gre_parse_opt
,
478 .print_opt
= gre_print_opt
,
479 .print_help
= gre_print_help
,