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 iptunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
32 if (strcmp(lu
->id
, "sit") == 0) {
33 mode
= "{ ip6ip | ipip | mplsip | any } ]\n"
36 mode
= "{ ipip | mplsip | any }";
40 "Usage: ... %-6s [ remote ADDR ]\n"
45 " [ 6rd-prefix ADDR ]\n"
46 " [ 6rd-relay_prefix ADDR ]\n"
52 " [ encap { fou | gue | none } ]\n"
53 " [ encap-sport PORT ]\n"
54 " [ encap-dport PORT ]\n"
55 " [ [no]encap-csum ]\n"
56 " [ [no]encap-csum6 ]\n"
57 " [ [no]encap-remcsum ]\n"
60 "Where: ADDR := { IP_ADDRESS | any }\n"
61 " TOS := { NUMBER | inherit }\n"
62 " TTL := { 1..255 | inherit }\n"
63 " MARK := { 0x0..0xffffffff }\n",
67 static int iptunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
70 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
75 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
76 .n
.nlmsg_flags
= NLM_F_REQUEST
,
77 .n
.nlmsg_type
= RTM_GETLINK
,
78 .i
.ifi_family
= preferred_family
,
79 .i
.ifi_index
= ifi
->ifi_index
,
81 struct nlmsghdr
*answer
;
82 struct rtattr
*tb
[IFLA_MAX
+ 1];
83 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
84 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
86 inet_prefix saddr
, daddr
, ip6rdprefix
, ip6rdrelayprefix
;
100 inet_prefix_reset(&saddr
);
101 inet_prefix_reset(&daddr
);
103 inet_prefix_reset(&ip6rdprefix
);
104 inet_prefix_reset(&ip6rdrelayprefix
);
106 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
107 const struct rtattr
*rta
;
109 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
112 "Failed to get existing tunnel info.\n");
116 len
= answer
->nlmsg_len
;
117 len
-= NLMSG_LENGTH(sizeof(*ifi
));
121 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
123 if (!tb
[IFLA_LINKINFO
])
126 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
128 if (!linkinfo
[IFLA_INFO_DATA
])
131 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
132 linkinfo
[IFLA_INFO_DATA
]);
134 rta
= iptuninfo
[IFLA_IPTUN_LOCAL
];
135 if (rta
&& get_addr_rta(&saddr
, rta
, AF_INET
))
138 rta
= iptuninfo
[IFLA_IPTUN_REMOTE
];
139 if (rta
&& get_addr_rta(&daddr
, rta
, AF_INET
))
142 rta
= iptuninfo
[IFLA_IPTUN_6RD_PREFIX
];
143 if (rta
&& get_addr_rta(&ip6rdprefix
, rta
, AF_INET6
))
146 rta
= iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
];
147 if (rta
&& get_addr_rta(&ip6rdrelayprefix
, rta
, AF_INET
))
150 rta
= iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
];
151 ip6rdprefix
.bitlen
= rta
? rta_getattr_u16(rta
) : 0;
153 rta
= iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
];
154 ip6rdrelayprefix
.bitlen
= rta
? rta_getattr_u16(rta
) : 0;
156 if (iptuninfo
[IFLA_IPTUN_TTL
])
157 ttl
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
159 if (iptuninfo
[IFLA_IPTUN_PMTUDISC
])
161 rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PMTUDISC
]);
163 if (iptuninfo
[IFLA_IPTUN_TOS
])
164 tos
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TOS
]);
166 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
167 iflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_FLAGS
]);
169 if (iptuninfo
[IFLA_IPTUN_LINK
])
170 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
172 if (iptuninfo
[IFLA_IPTUN_PROTO
])
173 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
175 if (iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
])
176 encaptype
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
]);
177 if (iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
])
178 encapflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
]);
179 if (iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
])
180 encapsport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
]);
181 if (iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
])
182 encapdport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
]);
184 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
187 if (iptuninfo
[IFLA_IPTUN_FWMARK
])
188 fwmark
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FWMARK
]);
194 if (strcmp(*argv
, "mode") == 0) {
196 if (strcmp(lu
->id
, "sit") == 0 &&
197 (strcmp(*argv
, "ipv6/ipv4") == 0 ||
198 strcmp(*argv
, "ip6ip") == 0))
199 proto
= IPPROTO_IPV6
;
200 else if (strcmp(*argv
, "ipv4/ipv4") == 0 ||
201 strcmp(*argv
, "ipip") == 0 ||
202 strcmp(*argv
, "ip4ip4") == 0)
203 proto
= IPPROTO_IPIP
;
204 else if (strcmp(*argv
, "mpls/ipv4") == 0 ||
205 strcmp(*argv
, "mplsip") == 0)
206 proto
= IPPROTO_MPLS
;
207 else if (strcmp(*argv
, "any/ipv4") == 0 ||
208 strcmp(*argv
, "any") == 0)
211 invarg("Cannot guess tunnel mode.", *argv
);
212 } else if (strcmp(*argv
, "remote") == 0) {
214 get_addr(&daddr
, *argv
, AF_INET
);
215 } else if (strcmp(*argv
, "local") == 0) {
217 get_addr(&saddr
, *argv
, AF_INET
);
218 } else if (matches(*argv
, "dev") == 0) {
220 link
= ll_name_to_index(*argv
);
223 } else if (strcmp(*argv
, "ttl") == 0 ||
224 strcmp(*argv
, "hoplimit") == 0 ||
225 strcmp(*argv
, "hlim") == 0) {
227 if (strcmp(*argv
, "inherit") != 0) {
228 if (get_u8(&ttl
, *argv
, 0))
229 invarg("invalid TTL\n", *argv
);
232 } else if (strcmp(*argv
, "tos") == 0 ||
233 strcmp(*argv
, "tclass") == 0 ||
234 strcmp(*argv
, "tc") == 0 ||
235 matches(*argv
, "dsfield") == 0) {
239 if (strcmp(*argv
, "inherit") != 0) {
240 if (rtnl_dsfield_a2n(&uval
, *argv
))
241 invarg("bad TOS value", *argv
);
245 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
247 } else if (strcmp(*argv
, "pmtudisc") == 0) {
249 } else if (strcmp(lu
->id
, "sit") == 0 &&
250 strcmp(*argv
, "isatap") == 0) {
251 iflags
|= SIT_ISATAP
;
252 } else if (strcmp(*argv
, "noencap") == 0) {
253 encaptype
= TUNNEL_ENCAP_NONE
;
254 } else if (strcmp(*argv
, "encap") == 0) {
256 if (strcmp(*argv
, "fou") == 0)
257 encaptype
= TUNNEL_ENCAP_FOU
;
258 else if (strcmp(*argv
, "gue") == 0)
259 encaptype
= TUNNEL_ENCAP_GUE
;
260 else if (strcmp(*argv
, "none") == 0)
261 encaptype
= TUNNEL_ENCAP_NONE
;
263 invarg("Invalid encap type.", *argv
);
264 } else if (strcmp(*argv
, "encap-sport") == 0) {
266 if (strcmp(*argv
, "auto") == 0)
268 else if (get_u16(&encapsport
, *argv
, 0))
269 invarg("Invalid source port.", *argv
);
270 } else if (strcmp(*argv
, "encap-dport") == 0) {
272 if (get_u16(&encapdport
, *argv
, 0))
273 invarg("Invalid destination port.", *argv
);
274 } else if (strcmp(*argv
, "encap-csum") == 0) {
275 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
276 } else if (strcmp(*argv
, "noencap-csum") == 0) {
277 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
278 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
279 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
280 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
281 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
282 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
283 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
284 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
285 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
286 } else if (strcmp(*argv
, "external") == 0) {
288 } else if (strcmp(*argv
, "6rd-prefix") == 0) {
290 if (get_prefix(&ip6rdprefix
, *argv
, AF_INET6
))
291 invarg("invalid 6rd_prefix\n", *argv
);
292 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
294 if (get_prefix(&ip6rdrelayprefix
, *argv
, AF_INET
))
295 invarg("invalid 6rd-relay_prefix\n", *argv
);
296 } else if (strcmp(*argv
, "6rd-reset") == 0) {
297 get_prefix(&ip6rdprefix
, "2002::/16", AF_INET6
);
298 inet_prefix_reset(&ip6rdrelayprefix
);
299 } else if (strcmp(*argv
, "fwmark") == 0) {
301 if (get_u32(&fwmark
, *argv
, 0))
302 invarg("invalid fwmark\n", *argv
);
304 iptunnel_print_help(lu
, argc
, argv
, stderr
);
310 if (ttl
&& pmtudisc
== 0) {
311 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
315 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
317 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
321 if (is_addrtype_inet_not_unspec(&saddr
)) {
322 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
,
323 saddr
.data
, saddr
.bytelen
);
325 if (is_addrtype_inet_not_unspec(&daddr
)) {
326 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
,
327 daddr
.data
, daddr
.bytelen
);
329 addattr8(n
, 1024, IFLA_IPTUN_PMTUDISC
, pmtudisc
);
330 addattr8(n
, 1024, IFLA_IPTUN_TOS
, tos
);
331 addattr8(n
, 1024, IFLA_IPTUN_TTL
, ttl
);
332 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
333 addattr32(n
, 1024, IFLA_IPTUN_FWMARK
, fwmark
);
335 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
336 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
337 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
338 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
340 if (strcmp(lu
->id
, "sit") == 0) {
341 addattr16(n
, 1024, IFLA_IPTUN_FLAGS
, iflags
);
342 if (is_addrtype_inet(&ip6rdprefix
)) {
343 addattr_l(n
, 1024, IFLA_IPTUN_6RD_PREFIX
,
344 ip6rdprefix
.data
, ip6rdprefix
.bytelen
);
345 addattr16(n
, 1024, IFLA_IPTUN_6RD_PREFIXLEN
,
348 if (is_addrtype_inet(&ip6rdrelayprefix
)) {
349 addattr32(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX
,
350 ip6rdrelayprefix
.data
[0]);
351 addattr16(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN
,
352 ip6rdrelayprefix
.bitlen
);
359 static void iptunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
369 if (tb
[IFLA_IPTUN_COLLECT_METADATA
]) {
370 print_bool(PRINT_ANY
, "external", "external ", true);
374 if (tb
[IFLA_IPTUN_PROTO
]) {
375 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
377 print_string(PRINT_ANY
, "proto", "%s ", "ipip");
380 print_string(PRINT_ANY
, "proto", "%s ", "ip6ip");
383 print_string(PRINT_ANY
, "proto", "%s ", "mplsip");
386 print_string(PRINT_ANY
, "proto", "%s ", "any");
391 tnl_print_endpoint("remote", tb
[IFLA_IPTUN_REMOTE
], AF_INET
);
392 tnl_print_endpoint("local", tb
[IFLA_IPTUN_LOCAL
], AF_INET
);
394 if (tb
[IFLA_IPTUN_LINK
]) {
395 __u32 link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
398 print_string(PRINT_ANY
, "link", "dev %s ",
399 ll_index_to_name(link
));
403 if (tb
[IFLA_IPTUN_TTL
])
404 ttl
= rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]);
405 if (is_json_context() || ttl
)
406 print_uint(PRINT_ANY
, "ttl", "ttl %u ", ttl
);
408 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
410 if (tb
[IFLA_IPTUN_TOS
])
411 tos
= rta_getattr_u8(tb
[IFLA_IPTUN_TOS
]);
413 if (is_json_context() || tos
!= 1)
414 print_0xhex(PRINT_ANY
, "tos", "tos %#llx ", tos
);
416 print_string(PRINT_FP
, NULL
, "tos %s ", "inherit");
419 if (tb
[IFLA_IPTUN_PMTUDISC
] && rta_getattr_u8(tb
[IFLA_IPTUN_PMTUDISC
]))
420 print_bool(PRINT_ANY
, "pmtudisc", "pmtudisc ", true);
422 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
424 if (tb
[IFLA_IPTUN_FLAGS
]) {
425 __u16 iflags
= rta_getattr_u16(tb
[IFLA_IPTUN_FLAGS
]);
427 if (iflags
& SIT_ISATAP
)
428 print_bool(PRINT_ANY
, "isatap", "isatap ", true);
431 if (tb
[IFLA_IPTUN_6RD_PREFIXLEN
] &&
432 (prefixlen
= rta_getattr_u16(tb
[IFLA_IPTUN_6RD_PREFIXLEN
]))) {
433 __u16 relayprefixlen
=
434 rta_getattr_u16(tb
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
436 rta_getattr_u32(tb
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
438 const char *prefix
= inet_ntop(AF_INET6
,
439 RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIX
]),
442 if (is_json_context()) {
443 print_string(PRINT_JSON
, "prefix", NULL
, prefix
);
444 print_int(PRINT_JSON
, "prefixlen", NULL
, prefixlen
);
446 print_string(PRINT_JSON
,
452 print_int(PRINT_JSON
,
458 printf("6rd-prefix %s/%u ", prefix
, prefixlen
);
460 printf("6rd-relay_prefix %s/%u ",
461 format_host(AF_INET
, 4, &relayprefix
),
467 if (tb
[IFLA_IPTUN_FWMARK
]) {
468 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_IPTUN_FWMARK
]);
471 print_0xhex(PRINT_ANY
,
472 "fwmark", "fwmark %#llx ", fwmark
);
477 IFLA_IPTUN_ENCAP_TYPE
,
478 IFLA_IPTUN_ENCAP_FLAGS
,
479 IFLA_IPTUN_ENCAP_SPORT
,
480 IFLA_IPTUN_ENCAP_DPORT
);
483 struct link_util ipip_link_util
= {
485 .maxattr
= IFLA_IPTUN_MAX
,
486 .parse_opt
= iptunnel_parse_opt
,
487 .print_opt
= iptunnel_print_opt
,
488 .print_help
= iptunnel_print_help
,
491 struct link_util sit_link_util
= {
493 .maxattr
= IFLA_IPTUN_MAX
,
494 .parse_opt
= iptunnel_parse_opt
,
495 .print_opt
= iptunnel_print_opt
,
496 .print_help
= iptunnel_print_help
,