2 * link_iptnl.c ipip and sit 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: Nicolas Dichtel <nicolas.dichtel@6wind.com>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
21 #include <linux/if_tunnel.h>
24 #include "ip_common.h"
27 static void print_usage(FILE *f
, int sit
)
29 const char *type
= sit
? "sit " : "ipip";
32 "Usage: ... %s [ remote ADDR ]\n"
38 " [ 6rd-prefix ADDR ]\n"
39 " [ 6rd-relay_prefix ADDR ]\n"
42 " [ encap { fou | gue | none } ]\n"
43 " [ encap-sport PORT ]\n"
44 " [ encap-dport PORT ]\n"
45 " [ [no]encap-csum ]\n"
46 " [ [no]encap-csum6 ]\n"
47 " [ [no]encap-remcsum ]\n",
51 fprintf(f
, " [ mode { ip6ip | ipip | mplsip | any } ]\n");
52 fprintf(f
, " [ isatap ]\n");
54 fprintf(f
, " [ mode { ipip | mplsip | any } ]\n");
56 fprintf(f
, " [ external ]\n");
57 fprintf(f
, " [ fwmark MARK ]\n");
59 fprintf(f
, "Where: ADDR := { IP_ADDRESS | any }\n");
60 fprintf(f
, " TOS := { NUMBER | inherit }\n");
61 fprintf(f
, " TTL := { 1..255 | inherit }\n");
62 fprintf(f
, " MARK := { 0x0..0xffffffff }\n");
65 static void usage(int sit
) __attribute__((noreturn
));
66 static void usage(int sit
)
68 print_usage(stderr
, sit
);
72 static int iptunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
75 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
80 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
81 .n
.nlmsg_flags
= NLM_F_REQUEST
,
82 .n
.nlmsg_type
= RTM_GETLINK
,
83 .i
.ifi_family
= preferred_family
,
84 .i
.ifi_index
= ifi
->ifi_index
,
86 struct nlmsghdr
*answer
= NULL
;
87 struct rtattr
*tb
[IFLA_MAX
+ 1];
88 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
89 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
99 struct in6_addr ip6rdprefix
= {};
100 __u16 ip6rdprefixlen
= 0;
101 __u32 ip6rdrelayprefix
= 0;
102 __u16 ip6rdrelayprefixlen
= 0;
104 __u16 encapflags
= 0;
105 __u16 encapsport
= 0;
106 __u16 encapdport
= 0;
110 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
111 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
114 "Failed to get existing tunnel info.\n");
119 len
= answer
->nlmsg_len
;
120 len
-= NLMSG_LENGTH(sizeof(*ifi
));
124 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
126 if (!tb
[IFLA_LINKINFO
])
129 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
131 if (!linkinfo
[IFLA_INFO_DATA
])
134 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
135 linkinfo
[IFLA_INFO_DATA
]);
137 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
138 laddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LOCAL
]);
140 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
141 raddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_REMOTE
]);
143 if (iptuninfo
[IFLA_IPTUN_TTL
])
144 ttl
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
146 if (iptuninfo
[IFLA_IPTUN_TOS
])
147 tos
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TOS
]);
149 if (iptuninfo
[IFLA_IPTUN_PMTUDISC
])
151 rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PMTUDISC
]);
153 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
154 iflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_FLAGS
]);
156 if (iptuninfo
[IFLA_IPTUN_LINK
])
157 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
159 if (iptuninfo
[IFLA_IPTUN_PROTO
])
160 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
162 if (iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
])
163 encaptype
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
]);
164 if (iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
])
165 encapflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
]);
166 if (iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
])
167 encapsport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
]);
168 if (iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
])
169 encapdport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
]);
170 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIX
])
172 RTA_DATA(iptuninfo
[IFLA_IPTUN_6RD_PREFIX
]),
175 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
])
177 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
]);
179 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
])
181 rta_getattr_u32(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
183 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
])
184 ip6rdrelayprefixlen
=
185 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
186 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
189 if (iptuninfo
[IFLA_IPTUN_FWMARK
])
190 fwmark
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FWMARK
]);
196 if (strcmp(*argv
, "remote") == 0) {
198 raddr
= get_addr32(*argv
);
199 } else if (strcmp(*argv
, "local") == 0) {
201 laddr
= get_addr32(*argv
);
202 } else if (matches(*argv
, "dev") == 0) {
204 link
= if_nametoindex(*argv
);
206 invarg("\"dev\" is invalid", *argv
);
207 } else if (strcmp(*argv
, "ttl") == 0 ||
208 strcmp(*argv
, "hoplimit") == 0) {
210 if (strcmp(*argv
, "inherit") != 0) {
211 if (get_u8(&ttl
, *argv
, 0))
212 invarg("invalid TTL\n", *argv
);
215 } else if (strcmp(*argv
, "tos") == 0 ||
216 strcmp(*argv
, "tclass") == 0 ||
217 matches(*argv
, "dsfield") == 0) {
221 if (strcmp(*argv
, "inherit") != 0) {
222 if (rtnl_dsfield_a2n(&uval
, *argv
))
223 invarg("bad TOS value", *argv
);
227 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
229 } else if (strcmp(*argv
, "pmtudisc") == 0) {
231 } else if (strcmp(lu
->id
, "sit") == 0 &&
232 strcmp(*argv
, "isatap") == 0) {
233 iflags
|= SIT_ISATAP
;
234 } else if (strcmp(lu
->id
, "sit") == 0 &&
235 strcmp(*argv
, "mode") == 0) {
237 if (strcmp(*argv
, "ipv6/ipv4") == 0 ||
238 strcmp(*argv
, "ip6ip") == 0)
239 proto
= IPPROTO_IPV6
;
240 else if (strcmp(*argv
, "ipv4/ipv4") == 0 ||
241 strcmp(*argv
, "ipip") == 0 ||
242 strcmp(*argv
, "ip4ip4") == 0)
243 proto
= IPPROTO_IPIP
;
244 else if (strcmp(*argv
, "mpls/ipv4") == 0 ||
245 strcmp(*argv
, "mplsip") == 0)
246 proto
= IPPROTO_MPLS
;
247 else if (strcmp(*argv
, "any/ipv4") == 0 ||
248 strcmp(*argv
, "any") == 0)
251 invarg("Cannot guess tunnel mode.", *argv
);
252 } else if (strcmp(lu
->id
, "ipip") == 0 &&
253 strcmp(*argv
, "mode") == 0) {
255 if (strcmp(*argv
, "ipv4/ipv4") == 0 ||
256 strcmp(*argv
, "ipip") == 0 ||
257 strcmp(*argv
, "ip4ip4") == 0)
258 proto
= IPPROTO_IPIP
;
259 else if (strcmp(*argv
, "mpls/ipv4") == 0 ||
260 strcmp(*argv
, "mplsip") == 0)
261 proto
= IPPROTO_MPLS
;
262 else if (strcmp(*argv
, "any/ipv4") == 0 ||
263 strcmp(*argv
, "any") == 0)
266 invarg("Cannot guess tunnel mode.", *argv
);
267 } else if (strcmp(*argv
, "noencap") == 0) {
268 encaptype
= TUNNEL_ENCAP_NONE
;
269 } else if (strcmp(*argv
, "encap") == 0) {
271 if (strcmp(*argv
, "fou") == 0)
272 encaptype
= TUNNEL_ENCAP_FOU
;
273 else if (strcmp(*argv
, "gue") == 0)
274 encaptype
= TUNNEL_ENCAP_GUE
;
275 else if (strcmp(*argv
, "none") == 0)
276 encaptype
= TUNNEL_ENCAP_NONE
;
278 invarg("Invalid encap type.", *argv
);
279 } else if (strcmp(*argv
, "encap-sport") == 0) {
281 if (strcmp(*argv
, "auto") == 0)
283 else if (get_u16(&encapsport
, *argv
, 0))
284 invarg("Invalid source port.", *argv
);
285 } else if (strcmp(*argv
, "encap-dport") == 0) {
287 if (get_u16(&encapdport
, *argv
, 0))
288 invarg("Invalid destination port.", *argv
);
289 } else if (strcmp(*argv
, "encap-csum") == 0) {
290 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
291 } else if (strcmp(*argv
, "noencap-csum") == 0) {
292 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
293 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
294 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
295 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
296 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
297 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
298 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
299 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
300 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
301 } else if (strcmp(*argv
, "external") == 0) {
303 } else if (strcmp(*argv
, "6rd-prefix") == 0) {
307 if (get_prefix(&prefix
, *argv
, AF_INET6
))
308 invarg("invalid 6rd_prefix\n", *argv
);
309 memcpy(&ip6rdprefix
, prefix
.data
, 16);
310 ip6rdprefixlen
= prefix
.bitlen
;
311 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
315 if (get_prefix(&prefix
, *argv
, AF_INET
))
316 invarg("invalid 6rd-relay_prefix\n", *argv
);
317 memcpy(&ip6rdrelayprefix
, prefix
.data
, 4);
318 ip6rdrelayprefixlen
= prefix
.bitlen
;
319 } else if (strcmp(*argv
, "6rd-reset") == 0) {
322 get_prefix(&prefix
, "2002::", AF_INET6
);
323 memcpy(&ip6rdprefix
, prefix
.data
, 16);
325 ip6rdrelayprefix
= 0;
326 ip6rdrelayprefixlen
= 0;
327 } else if (strcmp(*argv
, "fwmark") == 0) {
329 if (get_u32(&fwmark
, *argv
, 0))
330 invarg("invalid fwmark\n", *argv
);
332 usage(strcmp(lu
->id
, "sit") == 0);
336 if (ttl
&& pmtudisc
== 0) {
337 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
342 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
346 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
347 addattr32(n
, 1024, IFLA_IPTUN_LOCAL
, laddr
);
348 addattr32(n
, 1024, IFLA_IPTUN_REMOTE
, raddr
);
349 addattr8(n
, 1024, IFLA_IPTUN_TTL
, ttl
);
350 addattr8(n
, 1024, IFLA_IPTUN_TOS
, tos
);
351 addattr8(n
, 1024, IFLA_IPTUN_PMTUDISC
, pmtudisc
);
352 addattr32(n
, 1024, IFLA_IPTUN_FWMARK
, fwmark
);
354 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
355 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
356 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
357 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
359 if (strcmp(lu
->id
, "ipip") == 0 || strcmp(lu
->id
, "sit") == 0)
360 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
362 if (strcmp(lu
->id
, "sit") == 0) {
363 addattr16(n
, 1024, IFLA_IPTUN_FLAGS
, iflags
);
364 if (ip6rdprefixlen
) {
365 addattr_l(n
, 1024, IFLA_IPTUN_6RD_PREFIX
,
366 &ip6rdprefix
, sizeof(ip6rdprefix
));
367 addattr16(n
, 1024, IFLA_IPTUN_6RD_PREFIXLEN
,
369 addattr32(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX
,
371 addattr16(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN
,
372 ip6rdrelayprefixlen
);
379 static void iptunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
383 const char *local
= "any";
384 const char *remote
= "any";
385 __u16 prefixlen
, type
;
390 if (tb
[IFLA_IPTUN_COLLECT_METADATA
])
391 print_bool(PRINT_ANY
, "external", "external ", true);
393 if (tb
[IFLA_IPTUN_REMOTE
]) {
394 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_REMOTE
]);
397 remote
= format_host(AF_INET
, 4, &addr
);
400 print_string(PRINT_ANY
, "remote", "remote %s ", remote
);
402 if (tb
[IFLA_IPTUN_LOCAL
]) {
403 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_LOCAL
]);
406 local
= format_host(AF_INET
, 4, &addr
);
409 print_string(PRINT_ANY
, "local", "local %s ", local
);
411 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
412 unsigned int link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
413 const char *n
= if_indextoname(link
, s2
);
416 print_string(PRINT_ANY
, "link", "dev %s ", n
);
418 print_int(PRINT_ANY
, "link_index", "dev %u ", link
);
421 if (tb
[IFLA_IPTUN_TTL
]) {
422 __u8 ttl
= rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]);
425 print_int(PRINT_ANY
, "ttl", "ttl %d ", ttl
);
427 print_int(PRINT_JSON
, "ttl", NULL
, ttl
);
429 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
432 if (tb
[IFLA_IPTUN_TOS
]) {
433 int tos
= rta_getattr_u8(tb
[IFLA_IPTUN_TOS
]);
436 if (is_json_context()) {
437 print_0xhex(PRINT_JSON
, "tos", "%#x", tos
);
441 fputs("inherit ", f
);
443 fprintf(f
, "0x%x ", tos
);
448 if (tb
[IFLA_IPTUN_PMTUDISC
] && rta_getattr_u8(tb
[IFLA_IPTUN_PMTUDISC
]))
449 print_bool(PRINT_ANY
, "pmtudisc", "pmtudisc ", true);
451 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
453 if (tb
[IFLA_IPTUN_FLAGS
]) {
454 __u16 iflags
= rta_getattr_u16(tb
[IFLA_IPTUN_FLAGS
]);
456 if (iflags
& SIT_ISATAP
)
457 print_bool(PRINT_ANY
, "isatap", "isatap ", true);
460 if (tb
[IFLA_IPTUN_6RD_PREFIXLEN
] &&
461 (prefixlen
= rta_getattr_u16(tb
[IFLA_IPTUN_6RD_PREFIXLEN
]))) {
462 __u16 relayprefixlen
=
463 rta_getattr_u16(tb
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
465 rta_getattr_u32(tb
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
467 const char *prefix
= inet_ntop(AF_INET6
,
468 RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIX
]),
471 if (is_json_context()) {
472 print_string(PRINT_JSON
, "prefix", NULL
, prefix
);
473 print_int(PRINT_JSON
, "prefixlen", NULL
, prefixlen
);
475 print_string(PRINT_JSON
,
481 print_int(PRINT_JSON
,
487 printf("6rd-prefix %s/%u ", prefix
, prefixlen
);
489 printf("6rd-relay_prefix %s/%u ",
490 format_host(AF_INET
, 4, &relayprefix
),
496 if (tb
[IFLA_IPTUN_ENCAP_TYPE
] &&
497 (type
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
])) != TUNNEL_ENCAP_NONE
) {
498 __u16 flags
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_FLAGS
]);
499 __u16 sport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_SPORT
]);
500 __u16 dport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_DPORT
]);
502 print_string(PRINT_FP
, NULL
, "encap ", NULL
);
504 case TUNNEL_ENCAP_FOU
:
505 print_string(PRINT_ANY
, "type", "%s ", "fou");
507 case TUNNEL_ENCAP_GUE
:
508 print_string(PRINT_ANY
, "type", "%s ", "gue");
511 print_null(PRINT_ANY
, "type", "unknown ", NULL
);
515 if (is_json_context()) {
516 print_uint(PRINT_JSON
,
519 sport
? ntohs(sport
) : 0);
520 print_uint(PRINT_JSON
, "dport", NULL
, ntohs(dport
));
521 print_bool(PRINT_JSON
,
524 flags
& TUNNEL_ENCAP_FLAG_CSUM
);
525 print_bool(PRINT_JSON
,
528 flags
& TUNNEL_ENCAP_FLAG_CSUM6
);
529 print_bool(PRINT_JSON
,
532 flags
& TUNNEL_ENCAP_FLAG_REMCSUM
);
536 fputs("encap-sport auto ", f
);
538 fprintf(f
, "encap-sport %u", ntohs(sport
));
540 fprintf(f
, "encap-dport %u ", ntohs(dport
));
542 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
543 fputs("encap-csum ", f
);
545 fputs("noencap-csum ", f
);
547 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
548 fputs("encap-csum6 ", f
);
550 fputs("noencap-csum6 ", f
);
552 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
553 fputs("encap-remcsum ", f
);
555 fputs("noencap-remcsum ", f
);
559 if (tb
[IFLA_IPTUN_FWMARK
]) {
560 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_IPTUN_FWMARK
]);
563 snprintf(s2
, sizeof(s2
), "0x%x", fwmark
);
565 print_string(PRINT_ANY
, "fwmark", "fwmark %s ", s2
);
570 static void iptunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
573 print_usage(f
, strcmp(lu
->id
, "sit") == 0);
576 struct link_util ipip_link_util
= {
578 .maxattr
= IFLA_IPTUN_MAX
,
579 .parse_opt
= iptunnel_parse_opt
,
580 .print_opt
= iptunnel_print_opt
,
581 .print_help
= iptunnel_print_help
,
584 struct link_util sit_link_util
= {
586 .maxattr
= IFLA_IPTUN_MAX
,
587 .parse_opt
= iptunnel_parse_opt
,
588 .print_opt
= iptunnel_print_opt
,
589 .print_help
= iptunnel_print_help
,