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
,
33 "Usage: ... %-6s [ remote ADDR ]\n",
41 " [ 6rd-prefix ADDR ]\n"
42 " [ 6rd-relay_prefix ADDR ]\n"
48 " [ encap { fou | gue | none } ]\n"
49 " [ encap-sport PORT ]\n"
50 " [ encap-dport PORT ]\n"
51 " [ [no]encap-csum ]\n"
52 " [ [no]encap-csum6 ]\n"
53 " [ [no]encap-remcsum ]\n"
55 if (strcmp(lu
->id
, "sit") == 0) {
56 mode
= "{ ip6ip | ipip | mplsip | any } ]\n"
59 mode
= "{ ipip | mplsip | any }";
67 "Where: ADDR := { IP_ADDRESS | any }\n"
68 " TOS := { NUMBER | inherit }\n"
69 " TTL := { 1..255 | inherit }\n"
70 " MARK := { 0x0..0xffffffff }\n"
74 static int iptunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
77 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
82 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
83 .n
.nlmsg_flags
= NLM_F_REQUEST
,
84 .n
.nlmsg_type
= RTM_GETLINK
,
85 .i
.ifi_family
= preferred_family
,
86 .i
.ifi_index
= ifi
->ifi_index
,
88 struct nlmsghdr
*answer
;
89 struct rtattr
*tb
[IFLA_MAX
+ 1];
90 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
91 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
93 inet_prefix saddr
, daddr
, ip6rdprefix
, ip6rdrelayprefix
;
101 __u16 encapflags
= 0;
102 __u16 encapsport
= 0;
103 __u16 encapdport
= 0;
107 inet_prefix_reset(&saddr
);
108 inet_prefix_reset(&daddr
);
110 inet_prefix_reset(&ip6rdprefix
);
111 inet_prefix_reset(&ip6rdrelayprefix
);
113 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
114 const struct rtattr
*rta
;
116 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
119 "Failed to get existing tunnel info.\n");
123 len
= answer
->nlmsg_len
;
124 len
-= NLMSG_LENGTH(sizeof(*ifi
));
128 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(NLMSG_DATA(answer
)), len
);
130 if (!tb
[IFLA_LINKINFO
])
133 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
135 if (!linkinfo
[IFLA_INFO_DATA
])
138 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
139 linkinfo
[IFLA_INFO_DATA
]);
141 rta
= iptuninfo
[IFLA_IPTUN_LOCAL
];
142 if (rta
&& get_addr_rta(&saddr
, rta
, AF_INET
))
145 rta
= iptuninfo
[IFLA_IPTUN_REMOTE
];
146 if (rta
&& get_addr_rta(&daddr
, rta
, AF_INET
))
149 rta
= iptuninfo
[IFLA_IPTUN_6RD_PREFIX
];
150 if (rta
&& get_addr_rta(&ip6rdprefix
, rta
, AF_INET6
))
153 rta
= iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
];
154 if (rta
&& get_addr_rta(&ip6rdrelayprefix
, rta
, AF_INET
))
157 rta
= iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
];
158 ip6rdprefix
.bitlen
= rta
? rta_getattr_u16(rta
) : 0;
160 rta
= iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
];
161 ip6rdrelayprefix
.bitlen
= rta
? rta_getattr_u16(rta
) : 0;
163 if (iptuninfo
[IFLA_IPTUN_TTL
])
164 ttl
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
166 if (iptuninfo
[IFLA_IPTUN_PMTUDISC
])
168 rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PMTUDISC
]);
170 if (iptuninfo
[IFLA_IPTUN_TOS
])
171 tos
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TOS
]);
173 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
174 iflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_FLAGS
]);
176 if (iptuninfo
[IFLA_IPTUN_LINK
])
177 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
179 if (iptuninfo
[IFLA_IPTUN_PROTO
])
180 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
182 if (iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
])
183 encaptype
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
]);
184 if (iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
])
185 encapflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
]);
186 if (iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
])
187 encapsport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
]);
188 if (iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
])
189 encapdport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
]);
191 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
194 if (iptuninfo
[IFLA_IPTUN_FWMARK
])
195 fwmark
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FWMARK
]);
201 if (strcmp(*argv
, "mode") == 0) {
203 if (strcmp(lu
->id
, "sit") == 0 &&
204 (strcmp(*argv
, "ipv6/ipv4") == 0 ||
205 strcmp(*argv
, "ip6ip") == 0))
206 proto
= IPPROTO_IPV6
;
207 else if (strcmp(*argv
, "ipv4/ipv4") == 0 ||
208 strcmp(*argv
, "ipip") == 0 ||
209 strcmp(*argv
, "ip4ip4") == 0)
210 proto
= IPPROTO_IPIP
;
211 else if (strcmp(*argv
, "mpls/ipv4") == 0 ||
212 strcmp(*argv
, "mplsip") == 0)
213 proto
= IPPROTO_MPLS
;
214 else if (strcmp(*argv
, "any/ipv4") == 0 ||
215 strcmp(*argv
, "any") == 0)
218 invarg("Cannot guess tunnel mode.", *argv
);
219 } else if (strcmp(*argv
, "remote") == 0) {
221 get_addr(&daddr
, *argv
, AF_INET
);
222 } else if (strcmp(*argv
, "local") == 0) {
224 get_addr(&saddr
, *argv
, AF_INET
);
225 } else if (matches(*argv
, "dev") == 0) {
227 link
= ll_name_to_index(*argv
);
230 } else if (strcmp(*argv
, "ttl") == 0 ||
231 strcmp(*argv
, "hoplimit") == 0 ||
232 strcmp(*argv
, "hlim") == 0) {
234 if (strcmp(*argv
, "inherit") != 0) {
235 if (get_u8(&ttl
, *argv
, 0))
236 invarg("invalid TTL\n", *argv
);
239 } else if (strcmp(*argv
, "tos") == 0 ||
240 strcmp(*argv
, "tclass") == 0 ||
241 strcmp(*argv
, "tc") == 0 ||
242 matches(*argv
, "dsfield") == 0) {
246 if (strcmp(*argv
, "inherit") != 0) {
247 if (rtnl_dsfield_a2n(&uval
, *argv
))
248 invarg("bad TOS value", *argv
);
252 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
254 } else if (strcmp(*argv
, "pmtudisc") == 0) {
256 } else if (strcmp(lu
->id
, "sit") == 0 &&
257 strcmp(*argv
, "isatap") == 0) {
258 iflags
|= SIT_ISATAP
;
259 } else if (strcmp(*argv
, "noencap") == 0) {
260 encaptype
= TUNNEL_ENCAP_NONE
;
261 } else if (strcmp(*argv
, "encap") == 0) {
263 if (strcmp(*argv
, "fou") == 0)
264 encaptype
= TUNNEL_ENCAP_FOU
;
265 else if (strcmp(*argv
, "gue") == 0)
266 encaptype
= TUNNEL_ENCAP_GUE
;
267 else if (strcmp(*argv
, "none") == 0)
268 encaptype
= TUNNEL_ENCAP_NONE
;
270 invarg("Invalid encap type.", *argv
);
271 } else if (strcmp(*argv
, "encap-sport") == 0) {
273 if (strcmp(*argv
, "auto") == 0)
275 else if (get_u16(&encapsport
, *argv
, 0))
276 invarg("Invalid source port.", *argv
);
277 } else if (strcmp(*argv
, "encap-dport") == 0) {
279 if (get_u16(&encapdport
, *argv
, 0))
280 invarg("Invalid destination port.", *argv
);
281 } else if (strcmp(*argv
, "encap-csum") == 0) {
282 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
283 } else if (strcmp(*argv
, "noencap-csum") == 0) {
284 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
285 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
286 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
287 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
288 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
289 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
290 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
291 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
292 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
293 } else if (strcmp(*argv
, "external") == 0) {
295 } else if (strcmp(*argv
, "6rd-prefix") == 0) {
297 if (get_prefix(&ip6rdprefix
, *argv
, AF_INET6
))
298 invarg("invalid 6rd_prefix\n", *argv
);
299 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
301 if (get_prefix(&ip6rdrelayprefix
, *argv
, AF_INET
))
302 invarg("invalid 6rd-relay_prefix\n", *argv
);
303 } else if (strcmp(*argv
, "6rd-reset") == 0) {
304 get_prefix(&ip6rdprefix
, "2002::/16", AF_INET6
);
305 inet_prefix_reset(&ip6rdrelayprefix
);
306 } else if (strcmp(*argv
, "fwmark") == 0) {
308 if (get_u32(&fwmark
, *argv
, 0))
309 invarg("invalid fwmark\n", *argv
);
311 iptunnel_print_help(lu
, argc
, argv
, stderr
);
317 if (ttl
&& pmtudisc
== 0) {
318 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
322 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
324 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
328 if (is_addrtype_inet_not_unspec(&saddr
)) {
329 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
,
330 saddr
.data
, saddr
.bytelen
);
332 if (is_addrtype_inet_not_unspec(&daddr
)) {
333 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
,
334 daddr
.data
, daddr
.bytelen
);
336 addattr8(n
, 1024, IFLA_IPTUN_PMTUDISC
, pmtudisc
);
337 addattr8(n
, 1024, IFLA_IPTUN_TOS
, tos
);
338 addattr8(n
, 1024, IFLA_IPTUN_TTL
, ttl
);
339 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
340 addattr32(n
, 1024, IFLA_IPTUN_FWMARK
, fwmark
);
342 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
343 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
344 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
345 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
347 if (strcmp(lu
->id
, "sit") == 0) {
348 addattr16(n
, 1024, IFLA_IPTUN_FLAGS
, iflags
);
349 if (is_addrtype_inet(&ip6rdprefix
)) {
350 addattr_l(n
, 1024, IFLA_IPTUN_6RD_PREFIX
,
351 ip6rdprefix
.data
, ip6rdprefix
.bytelen
);
352 addattr16(n
, 1024, IFLA_IPTUN_6RD_PREFIXLEN
,
355 if (is_addrtype_inet(&ip6rdrelayprefix
)) {
356 addattr32(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX
,
357 ip6rdrelayprefix
.data
[0]);
358 addattr16(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN
,
359 ip6rdrelayprefix
.bitlen
);
366 static void iptunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
376 if (tb
[IFLA_IPTUN_COLLECT_METADATA
]) {
377 print_bool(PRINT_ANY
, "external", "external", true);
381 if (tb
[IFLA_IPTUN_PROTO
]) {
382 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
384 print_string(PRINT_ANY
, "proto", "%s ", "ipip");
387 print_string(PRINT_ANY
, "proto", "%s ", "ip6ip");
390 print_string(PRINT_ANY
, "proto", "%s ", "mplsip");
393 print_string(PRINT_ANY
, "proto", "%s ", "any");
398 tnl_print_endpoint("remote", tb
[IFLA_IPTUN_REMOTE
], AF_INET
);
399 tnl_print_endpoint("local", tb
[IFLA_IPTUN_LOCAL
], AF_INET
);
401 if (tb
[IFLA_IPTUN_LINK
]) {
402 __u32 link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
405 print_string(PRINT_ANY
, "link", "dev %s ",
406 ll_index_to_name(link
));
410 if (tb
[IFLA_IPTUN_TTL
])
411 ttl
= rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]);
412 if (is_json_context() || ttl
)
413 print_uint(PRINT_ANY
, "ttl", "ttl %u ", ttl
);
415 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
417 if (tb
[IFLA_IPTUN_TOS
])
418 tos
= rta_getattr_u8(tb
[IFLA_IPTUN_TOS
]);
420 if (is_json_context() || tos
!= 1)
421 print_0xhex(PRINT_ANY
, "tos", "tos 0x%x ", tos
);
423 print_string(PRINT_FP
, NULL
, "tos %s ", "inherit");
426 if (tb
[IFLA_IPTUN_PMTUDISC
] && rta_getattr_u8(tb
[IFLA_IPTUN_PMTUDISC
]))
427 print_bool(PRINT_ANY
, "pmtudisc", "pmtudisc ", true);
429 print_bool(PRINT_ANY
, "pmtudisc", "nopmtudisc ", false);
431 if (tb
[IFLA_IPTUN_FLAGS
]) {
432 __u16 iflags
= rta_getattr_u16(tb
[IFLA_IPTUN_FLAGS
]);
434 if (iflags
& SIT_ISATAP
)
435 print_bool(PRINT_ANY
, "isatap", "isatap ", true);
438 if (tb
[IFLA_IPTUN_6RD_PREFIXLEN
] &&
439 (prefixlen
= rta_getattr_u16(tb
[IFLA_IPTUN_6RD_PREFIXLEN
]))) {
440 __u16 relayprefixlen
=
441 rta_getattr_u16(tb
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
443 rta_getattr_u32(tb
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
445 const char *prefix
= inet_ntop(AF_INET6
,
446 RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIX
]),
449 if (is_json_context()) {
450 print_string(PRINT_JSON
, "prefix", NULL
, prefix
);
451 print_int(PRINT_JSON
, "prefixlen", NULL
, prefixlen
);
453 print_string(PRINT_JSON
,
459 print_int(PRINT_JSON
,
465 printf("6rd-prefix %s/%u ", prefix
, prefixlen
);
467 printf("6rd-relay_prefix %s/%u ",
468 format_host(AF_INET
, 4, &relayprefix
),
474 if (tb
[IFLA_IPTUN_FWMARK
]) {
475 __u32 fwmark
= rta_getattr_u32(tb
[IFLA_IPTUN_FWMARK
]);
478 print_0xhex(PRINT_ANY
,
479 "fwmark", "fwmark 0x%x ", fwmark
);
484 IFLA_IPTUN_ENCAP_TYPE
,
485 IFLA_IPTUN_ENCAP_FLAGS
,
486 IFLA_IPTUN_ENCAP_SPORT
,
487 IFLA_IPTUN_ENCAP_DPORT
);
490 struct link_util ipip_link_util
= {
492 .maxattr
= IFLA_IPTUN_MAX
,
493 .parse_opt
= iptunnel_parse_opt
,
494 .print_opt
= iptunnel_print_opt
,
495 .print_help
= iptunnel_print_help
,
498 struct link_util sit_link_util
= {
500 .maxattr
= IFLA_IPTUN_MAX
,
501 .parse_opt
= iptunnel_parse_opt
,
502 .print_opt
= iptunnel_print_opt
,
503 .print_help
= iptunnel_print_help
,