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>
20 #include <linux/if_tunnel.h>
23 #include "ip_common.h"
26 static void print_usage(FILE *f
, int sit
)
28 fprintf(f
, "Usage: ip link { add | set | change | replace | del } NAME\n");
29 fprintf(f
, " type { ipip | sit } [ remote ADDR ] [ local ADDR ]\n");
30 fprintf(f
, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
31 fprintf(f
, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\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
, " [ mode { ip6ip | ipip | any } ]\n");
37 fprintf(f
, " [ isatap ]\n");
39 fprintf(f
, " [ external ]\n");
41 fprintf(f
, "Where: NAME := STRING\n");
42 fprintf(f
, " ADDR := { IP_ADDRESS | any }\n");
43 fprintf(f
, " TOS := { NUMBER | inherit }\n");
44 fprintf(f
, " TTL := { 1..255 | inherit }\n");
47 static void usage(int sit
) __attribute__((noreturn
));
48 static void usage(int sit
)
50 print_usage(stderr
, sit
);
54 static int iptunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
57 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
63 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
64 .n
.nlmsg_flags
= NLM_F_REQUEST
,
65 .n
.nlmsg_type
= RTM_GETLINK
,
66 .i
.ifi_family
= preferred_family
,
67 .i
.ifi_index
= ifi
->ifi_index
,
69 struct rtattr
*tb
[IFLA_MAX
+ 1];
70 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
71 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
81 struct in6_addr ip6rdprefix
= {};
82 __u16 ip6rdprefixlen
= 0;
83 __u32 ip6rdrelayprefix
= 0;
84 __u16 ip6rdrelayprefixlen
= 0;
91 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
92 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
95 "Failed to get existing tunnel info.\n");
99 len
= req
.n
.nlmsg_len
;
100 len
-= NLMSG_LENGTH(sizeof(*ifi
));
104 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
106 if (!tb
[IFLA_LINKINFO
])
109 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
111 if (!linkinfo
[IFLA_INFO_DATA
])
114 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
115 linkinfo
[IFLA_INFO_DATA
]);
117 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
118 laddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LOCAL
]);
120 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
121 raddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_REMOTE
]);
123 if (iptuninfo
[IFLA_IPTUN_TTL
])
124 ttl
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
126 if (iptuninfo
[IFLA_IPTUN_TOS
])
127 tos
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TOS
]);
129 if (iptuninfo
[IFLA_IPTUN_PMTUDISC
])
131 rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PMTUDISC
]);
133 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
134 iflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_FLAGS
]);
136 if (iptuninfo
[IFLA_IPTUN_LINK
])
137 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
139 if (iptuninfo
[IFLA_IPTUN_PROTO
])
140 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
142 if (iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
])
143 encaptype
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
]);
144 if (iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
])
145 encapflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
]);
146 if (iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
])
147 encapsport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
]);
148 if (iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
])
149 encapdport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
]);
150 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIX
])
152 RTA_DATA(iptuninfo
[IFLA_IPTUN_6RD_PREFIX
]),
155 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
])
157 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
]);
159 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
])
161 rta_getattr_u32(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
163 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
])
164 ip6rdrelayprefixlen
=
165 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
166 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
171 if (strcmp(*argv
, "remote") == 0) {
173 if (strcmp(*argv
, "any"))
174 raddr
= get_addr32(*argv
);
177 } else if (strcmp(*argv
, "local") == 0) {
179 if (strcmp(*argv
, "any"))
180 laddr
= get_addr32(*argv
);
183 } else if (matches(*argv
, "dev") == 0) {
185 link
= if_nametoindex(*argv
);
187 invarg("\"dev\" is invalid", *argv
);
188 } else if (strcmp(*argv
, "ttl") == 0 ||
189 strcmp(*argv
, "hoplimit") == 0) {
191 if (strcmp(*argv
, "inherit") != 0) {
192 if (get_u8(&ttl
, *argv
, 0))
193 invarg("invalid TTL\n", *argv
);
196 } else if (strcmp(*argv
, "tos") == 0 ||
197 strcmp(*argv
, "tclass") == 0 ||
198 matches(*argv
, "dsfield") == 0) {
202 if (strcmp(*argv
, "inherit") != 0) {
203 if (rtnl_dsfield_a2n(&uval
, *argv
))
204 invarg("bad TOS value", *argv
);
208 } else if (strcmp(*argv
, "nopmtudisc") == 0) {
210 } else if (strcmp(*argv
, "pmtudisc") == 0) {
212 } else if (strcmp(lu
->id
, "sit") == 0 &&
213 strcmp(*argv
, "isatap") == 0) {
214 iflags
|= SIT_ISATAP
;
215 } else if (strcmp(lu
->id
, "sit") == 0 &&
216 strcmp(*argv
, "mode") == 0) {
218 if (strcmp(*argv
, "ipv6/ipv4") == 0 ||
219 strcmp(*argv
, "ip6ip") == 0)
220 proto
= IPPROTO_IPV6
;
221 else if (strcmp(*argv
, "ipv4/ipv4") == 0 ||
222 strcmp(*argv
, "ipip") == 0 ||
223 strcmp(*argv
, "ip4ip4") == 0)
224 proto
= IPPROTO_IPIP
;
225 else if (strcmp(*argv
, "any/ipv4") == 0 ||
226 strcmp(*argv
, "any") == 0)
229 invarg("Cannot guess tunnel mode.", *argv
);
230 } else if (strcmp(*argv
, "noencap") == 0) {
231 encaptype
= TUNNEL_ENCAP_NONE
;
232 } else if (strcmp(*argv
, "encap") == 0) {
234 if (strcmp(*argv
, "fou") == 0)
235 encaptype
= TUNNEL_ENCAP_FOU
;
236 else if (strcmp(*argv
, "gue") == 0)
237 encaptype
= TUNNEL_ENCAP_GUE
;
238 else if (strcmp(*argv
, "none") == 0)
239 encaptype
= TUNNEL_ENCAP_NONE
;
241 invarg("Invalid encap type.", *argv
);
242 } else if (strcmp(*argv
, "encap-sport") == 0) {
244 if (strcmp(*argv
, "auto") == 0)
246 else if (get_u16(&encapsport
, *argv
, 0))
247 invarg("Invalid source port.", *argv
);
248 } else if (strcmp(*argv
, "encap-dport") == 0) {
250 if (get_u16(&encapdport
, *argv
, 0))
251 invarg("Invalid destination port.", *argv
);
252 } else if (strcmp(*argv
, "encap-csum") == 0) {
253 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
254 } else if (strcmp(*argv
, "noencap-csum") == 0) {
255 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
256 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
257 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
258 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
259 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
260 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
261 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
262 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
263 encapflags
&= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
264 } else if (strcmp(*argv
, "external") == 0) {
266 } else if (strcmp(*argv
, "6rd-prefix") == 0) {
270 if (get_prefix(&prefix
, *argv
, AF_INET6
))
271 invarg("invalid 6rd_prefix\n", *argv
);
272 memcpy(&ip6rdprefix
, prefix
.data
, 16);
273 ip6rdprefixlen
= prefix
.bitlen
;
274 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
278 if (get_prefix(&prefix
, *argv
, AF_INET
))
279 invarg("invalid 6rd-relay_prefix\n", *argv
);
280 memcpy(&ip6rdrelayprefix
, prefix
.data
, 4);
281 ip6rdrelayprefixlen
= prefix
.bitlen
;
282 } else if (strcmp(*argv
, "6rd-reset") == 0) {
285 get_prefix(&prefix
, "2002::", AF_INET6
);
286 memcpy(&ip6rdprefix
, prefix
.data
, 16);
288 ip6rdrelayprefix
= 0;
289 ip6rdrelayprefixlen
= 0;
291 usage(strcmp(lu
->id
, "sit") == 0);
295 if (ttl
&& pmtudisc
== 0) {
296 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
301 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
305 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
306 addattr32(n
, 1024, IFLA_IPTUN_LOCAL
, laddr
);
307 addattr32(n
, 1024, IFLA_IPTUN_REMOTE
, raddr
);
308 addattr8(n
, 1024, IFLA_IPTUN_TTL
, ttl
);
309 addattr8(n
, 1024, IFLA_IPTUN_TOS
, tos
);
310 addattr8(n
, 1024, IFLA_IPTUN_PMTUDISC
, pmtudisc
);
312 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
313 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
314 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
315 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
317 if (strcmp(lu
->id
, "sit") == 0) {
318 addattr16(n
, 1024, IFLA_IPTUN_FLAGS
, iflags
);
319 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
320 if (ip6rdprefixlen
) {
321 addattr_l(n
, 1024, IFLA_IPTUN_6RD_PREFIX
,
322 &ip6rdprefix
, sizeof(ip6rdprefix
));
323 addattr16(n
, 1024, IFLA_IPTUN_6RD_PREFIXLEN
,
325 addattr32(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX
,
327 addattr16(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN
,
328 ip6rdrelayprefixlen
);
335 static void iptunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
339 const char *local
= "any";
340 const char *remote
= "any";
341 __u16 prefixlen
, type
;
346 if (tb
[IFLA_IPTUN_REMOTE
]) {
347 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_REMOTE
]);
350 remote
= format_host(AF_INET
, 4, &addr
);
353 fprintf(f
, "remote %s ", remote
);
355 if (tb
[IFLA_IPTUN_LOCAL
]) {
356 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_LOCAL
]);
359 local
= format_host(AF_INET
, 4, &addr
);
362 fprintf(f
, "local %s ", local
);
364 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
365 unsigned int link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
366 const char *n
= if_indextoname(link
, s2
);
369 fprintf(f
, "dev %s ", n
);
371 fprintf(f
, "dev %u ", link
);
374 if (tb
[IFLA_IPTUN_TTL
] && rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]))
375 fprintf(f
, "ttl %d ", rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
377 fprintf(f
, "ttl inherit ");
379 if (tb
[IFLA_IPTUN_TOS
] && rta_getattr_u8(tb
[IFLA_IPTUN_TOS
])) {
380 int tos
= rta_getattr_u8(tb
[IFLA_IPTUN_TOS
]);
384 fputs("inherit ", f
);
386 fprintf(f
, "0x%x ", tos
);
389 if (tb
[IFLA_IPTUN_PMTUDISC
] && rta_getattr_u8(tb
[IFLA_IPTUN_PMTUDISC
]))
390 fprintf(f
, "pmtudisc ");
392 fprintf(f
, "nopmtudisc ");
394 if (tb
[IFLA_IPTUN_FLAGS
]) {
395 __u16 iflags
= rta_getattr_u16(tb
[IFLA_IPTUN_FLAGS
]);
397 if (iflags
& SIT_ISATAP
)
398 fprintf(f
, "isatap ");
401 if (tb
[IFLA_IPTUN_6RD_PREFIXLEN
] &&
402 (prefixlen
= rta_getattr_u16(tb
[IFLA_IPTUN_6RD_PREFIXLEN
]))) {
403 __u16 relayprefixlen
=
404 rta_getattr_u16(tb
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
406 rta_getattr_u32(tb
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
408 printf("6rd-prefix %s/%u ",
409 inet_ntop(AF_INET6
, RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIX
]),
413 printf("6rd-relay_prefix %s/%u ",
414 format_host(AF_INET
, 4, &relayprefix
),
419 if (tb
[IFLA_IPTUN_ENCAP_TYPE
] &&
420 (type
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
])) != TUNNEL_ENCAP_NONE
) {
421 __u16 flags
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_FLAGS
]);
422 __u16 sport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_SPORT
]);
423 __u16 dport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_DPORT
]);
427 case TUNNEL_ENCAP_FOU
:
430 case TUNNEL_ENCAP_GUE
:
434 fputs("unknown ", f
);
439 fputs("encap-sport auto ", f
);
441 fprintf(f
, "encap-sport %u", ntohs(sport
));
443 fprintf(f
, "encap-dport %u ", ntohs(dport
));
445 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
446 fputs("encap-csum ", f
);
448 fputs("noencap-csum ", f
);
450 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
451 fputs("encap-csum6 ", f
);
453 fputs("noencap-csum6 ", f
);
455 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
456 fputs("encap-remcsum ", f
);
458 fputs("noencap-remcsum ", f
);
462 static void iptunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
465 print_usage(f
, strcmp(lu
->id
, "sit") == 0);
468 struct link_util ipip_link_util
= {
470 .maxattr
= IFLA_IPTUN_MAX
,
471 .parse_opt
= iptunnel_parse_opt
,
472 .print_opt
= iptunnel_print_opt
,
473 .print_help
= iptunnel_print_help
,
476 struct link_util sit_link_util
= {
478 .maxattr
= IFLA_IPTUN_MAX
,
479 .parse_opt
= iptunnel_parse_opt
,
480 .print_opt
= iptunnel_print_opt
,
481 .print_help
= iptunnel_print_help
,