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
,
53 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
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
,
65 struct rtattr
*tb
[IFLA_MAX
+ 1];
66 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
67 struct rtattr
*greinfo
[IFLA_GRE_MAX
+ 1];
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;
85 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
86 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
89 "Failed to get existing tunnel info.\n");
93 len
= req
.n
.nlmsg_len
;
94 len
-= NLMSG_LENGTH(sizeof(*ifi
));
98 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
100 if (!tb
[IFLA_LINKINFO
])
103 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
105 if (!linkinfo
[IFLA_INFO_DATA
])
108 parse_rtattr_nested(greinfo
, IFLA_GRE_MAX
,
109 linkinfo
[IFLA_INFO_DATA
]);
111 if (greinfo
[IFLA_GRE_IKEY
])
112 ikey
= rta_getattr_u32(greinfo
[IFLA_GRE_IKEY
]);
114 if (greinfo
[IFLA_GRE_OKEY
])
115 okey
= rta_getattr_u32(greinfo
[IFLA_GRE_OKEY
]);
117 if (greinfo
[IFLA_GRE_IFLAGS
])
118 iflags
= rta_getattr_u16(greinfo
[IFLA_GRE_IFLAGS
]);
120 if (greinfo
[IFLA_GRE_OFLAGS
])
121 oflags
= rta_getattr_u16(greinfo
[IFLA_GRE_OFLAGS
]);
123 if (greinfo
[IFLA_GRE_LOCAL
])
124 saddr
= rta_getattr_u32(greinfo
[IFLA_GRE_LOCAL
]);
126 if (greinfo
[IFLA_GRE_REMOTE
])
127 daddr
= rta_getattr_u32(greinfo
[IFLA_GRE_REMOTE
]);
129 if (greinfo
[IFLA_GRE_PMTUDISC
])
130 pmtudisc
= rta_getattr_u8(
131 greinfo
[IFLA_GRE_PMTUDISC
]);
133 if (greinfo
[IFLA_GRE_TTL
])
134 ttl
= rta_getattr_u8(greinfo
[IFLA_GRE_TTL
]);
136 if (greinfo
[IFLA_GRE_TOS
])
137 tos
= rta_getattr_u8(greinfo
[IFLA_GRE_TOS
]);
139 if (greinfo
[IFLA_GRE_LINK
])
140 link
= rta_getattr_u8(greinfo
[IFLA_GRE_LINK
]);
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
]);
151 if (greinfo
[IFLA_GRE_COLLECT_METADATA
])
156 if (!matches(*argv
, "key")) {
162 if (strchr(*argv
, '.'))
163 uval
= get_addr32(*argv
);
165 if (get_unsigned(&uval
, *argv
, 0) < 0) {
167 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
174 } else if (!matches(*argv
, "ikey")) {
179 if (strchr(*argv
, '.'))
180 uval
= get_addr32(*argv
);
182 if (get_unsigned(&uval
, *argv
, 0) < 0) {
183 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
189 } else if (!matches(*argv
, "okey")) {
194 if (strchr(*argv
, '.'))
195 uval
= get_addr32(*argv
);
197 if (get_unsigned(&uval
, *argv
, 0) < 0) {
198 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
204 } else if (!matches(*argv
, "seq")) {
207 } else if (!matches(*argv
, "iseq")) {
209 } else if (!matches(*argv
, "oseq")) {
211 } else if (!matches(*argv
, "csum")) {
214 } else if (!matches(*argv
, "icsum")) {
216 } else if (!matches(*argv
, "ocsum")) {
218 } else if (!matches(*argv
, "nopmtudisc")) {
220 } else if (!matches(*argv
, "pmtudisc")) {
222 } else if (!matches(*argv
, "remote")) {
224 if (strcmp(*argv
, "any"))
225 daddr
= get_addr32(*argv
);
226 } else if (!matches(*argv
, "local")) {
228 if (strcmp(*argv
, "any"))
229 saddr
= get_addr32(*argv
);
230 } else if (!matches(*argv
, "dev")) {
232 link
= if_nametoindex(*argv
);
234 fprintf(stderr
, "Cannot find device \"%s\"\n",
238 } else if (!matches(*argv
, "ttl") ||
239 !matches(*argv
, "hoplimit")) {
243 if (strcmp(*argv
, "inherit") != 0) {
244 if (get_unsigned(&uval
, *argv
, 0))
245 invarg("invalid TTL\n", *argv
);
247 invarg("TTL must be <= 255\n", *argv
);
250 } else if (!matches(*argv
, "tos") ||
251 !matches(*argv
, "tclass") ||
252 !matches(*argv
, "dsfield")) {
256 if (strcmp(*argv
, "inherit") != 0) {
257 if (rtnl_dsfield_a2n(&uval
, *argv
))
258 invarg("bad TOS value", *argv
);
262 } else if (strcmp(*argv
, "noencap") == 0) {
263 encaptype
= TUNNEL_ENCAP_NONE
;
264 } else if (strcmp(*argv
, "encap") == 0) {
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
;
273 invarg("Invalid encap type.", *argv
);
274 } else if (strcmp(*argv
, "encap-sport") == 0) {
276 if (strcmp(*argv
, "auto") == 0)
278 else if (get_u16(&encapsport
, *argv
, 0))
279 invarg("Invalid source port.", *argv
);
280 } else if (strcmp(*argv
, "encap-dport") == 0) {
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) {
303 if (!ikey
&& IN_MULTICAST(ntohl(daddr
))) {
307 if (!okey
&& IN_MULTICAST(ntohl(daddr
))) {
311 if (IN_MULTICAST(ntohl(daddr
)) && !saddr
) {
312 fprintf(stderr
, "A broadcast tunnel requires a source address.\n");
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);
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);
329 addattr_l(n
, 1024, IFLA_GRE_COLLECT_METADATA
, NULL
, 0);
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
));
340 static void gre_print_direct_opt(FILE *f
, struct rtattr
*tb
[])
343 const char *local
= "any";
344 const char *remote
= "any";
345 unsigned int iflags
= 0;
346 unsigned int oflags
= 0;
348 if (tb
[IFLA_GRE_REMOTE
]) {
349 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_REMOTE
]);
352 remote
= format_host(AF_INET
, 4, &addr
);
355 fprintf(f
, "remote %s ", remote
);
357 if (tb
[IFLA_GRE_LOCAL
]) {
358 unsigned int addr
= rta_getattr_u32(tb
[IFLA_GRE_LOCAL
]);
361 local
= format_host(AF_INET
, 4, &addr
);
364 fprintf(f
, "local %s ", local
);
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
);
371 fprintf(f
, "dev %s ", n
);
373 fprintf(f
, "dev %u ", link
);
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
]));
379 fprintf(f
, "ttl inherit ");
381 if (tb
[IFLA_GRE_TOS
] && rta_getattr_u8(tb
[IFLA_GRE_TOS
])) {
382 int tos
= rta_getattr_u8(tb
[IFLA_GRE_TOS
]);
386 fputs("inherit ", f
);
388 fprintf(f
, "0x%x ", tos
);
391 if (tb
[IFLA_GRE_PMTUDISC
] &&
392 !rta_getattr_u8(tb
[IFLA_GRE_PMTUDISC
]))
393 fputs("nopmtudisc ", f
);
395 if (tb
[IFLA_GRE_IFLAGS
])
396 iflags
= rta_getattr_u16(tb
[IFLA_GRE_IFLAGS
]);
398 if (tb
[IFLA_GRE_OFLAGS
])
399 oflags
= rta_getattr_u16(tb
[IFLA_GRE_OFLAGS
]);
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
);
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
);
411 if (iflags
& GRE_SEQ
)
413 if (oflags
& GRE_SEQ
)
415 if (iflags
& GRE_CSUM
)
417 if (oflags
& GRE_CSUM
)
421 static void gre_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
426 if (!tb
[IFLA_GRE_COLLECT_METADATA
])
427 gre_print_direct_opt(f
, tb
);
429 fputs("external ", f
);
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
]);
440 case TUNNEL_ENCAP_FOU
:
443 case TUNNEL_ENCAP_GUE
:
447 fputs("unknown ", f
);
452 fputs("encap-sport auto ", f
);
454 fprintf(f
, "encap-sport %u", ntohs(sport
));
456 fprintf(f
, "encap-dport %u ", ntohs(dport
));
458 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
459 fputs("encap-csum ", f
);
461 fputs("noencap-csum ", f
);
463 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
464 fputs("encap-csum6 ", f
);
466 fputs("noencap-csum6 ", f
);
468 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
469 fputs("encap-remcsum ", f
);
471 fputs("noencap-remcsum ", f
);
475 static void gre_print_help(struct link_util
*lu
, int argc
, char **argv
,
481 struct link_util gre_link_util
= {
483 .maxattr
= IFLA_GRE_MAX
,
484 .parse_opt
= gre_parse_opt
,
485 .print_opt
= gre_print_opt
,
486 .print_help
= gre_print_help
,
489 struct link_util gretap_link_util
= {
491 .maxattr
= IFLA_GRE_MAX
,
492 .parse_opt
= gre_parse_opt
,
493 .print_opt
= gre_print_opt
,
494 .print_help
= gre_print_help
,