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");
40 fprintf(f
, "Where: NAME := STRING\n");
41 fprintf(f
, " ADDR := { IP_ADDRESS | any }\n");
42 fprintf(f
, " TOS := { NUMBER | inherit }\n");
43 fprintf(f
, " TTL := { 1..255 | inherit }\n");
46 static void usage(int sit
) __attribute__((noreturn
));
47 static void usage(int sit
)
49 print_usage(stderr
, sit
);
53 static int iptunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
61 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
62 struct rtattr
*tb
[IFLA_MAX
+ 1];
63 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
64 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
74 struct in6_addr ip6rdprefix
;
75 __u16 ip6rdprefixlen
= 0;
76 __u32 ip6rdrelayprefix
= 0;
77 __u16 ip6rdrelayprefixlen
= 0;
83 memset(&ip6rdprefix
, 0, sizeof(ip6rdprefix
));
85 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
86 memset(&req
, 0, sizeof(req
));
88 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
));
89 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
90 req
.n
.nlmsg_type
= RTM_GETLINK
;
91 req
.i
.ifi_family
= preferred_family
;
92 req
.i
.ifi_index
= ifi
->ifi_index
;
94 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
97 "Failed to get existing tunnel info.\n");
101 len
= req
.n
.nlmsg_len
;
102 len
-= NLMSG_LENGTH(sizeof(*ifi
));
106 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
108 if (!tb
[IFLA_LINKINFO
])
111 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
113 if (!linkinfo
[IFLA_INFO_DATA
])
116 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
117 linkinfo
[IFLA_INFO_DATA
]);
119 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
120 laddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LOCAL
]);
122 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
123 raddr
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_REMOTE
]);
125 if (iptuninfo
[IFLA_IPTUN_TTL
])
126 ttl
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
128 if (iptuninfo
[IFLA_IPTUN_TOS
])
129 tos
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TOS
]);
131 if (iptuninfo
[IFLA_IPTUN_PMTUDISC
])
133 rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PMTUDISC
]);
135 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
136 iflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_FLAGS
]);
138 if (iptuninfo
[IFLA_IPTUN_LINK
])
139 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
141 if (iptuninfo
[IFLA_IPTUN_PROTO
])
142 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
144 if (iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
])
145 encaptype
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_TYPE
]);
146 if (iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
])
147 encapflags
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_FLAGS
]);
148 if (iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
])
149 encapsport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_SPORT
]);
150 if (iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
])
151 encapdport
= rta_getattr_u16(iptuninfo
[IFLA_IPTUN_ENCAP_DPORT
]);
152 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIX
])
154 RTA_DATA(iptuninfo
[IFLA_IPTUN_6RD_PREFIX
]),
157 if (iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
])
159 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_PREFIXLEN
]);
161 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
])
163 rta_getattr_u32(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
165 if (iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
])
166 ip6rdrelayprefixlen
=
167 rta_getattr_u16(iptuninfo
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
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
, "6rd-prefix") == 0) {
268 if (get_prefix(&prefix
, *argv
, AF_INET6
))
269 invarg("invalid 6rd_prefix\n", *argv
);
270 memcpy(&ip6rdprefix
, prefix
.data
, 16);
271 ip6rdprefixlen
= prefix
.bitlen
;
272 } else if (strcmp(*argv
, "6rd-relay_prefix") == 0) {
276 if (get_prefix(&prefix
, *argv
, AF_INET
))
277 invarg("invalid 6rd-relay_prefix\n", *argv
);
278 memcpy(&ip6rdrelayprefix
, prefix
.data
, 4);
279 ip6rdrelayprefixlen
= prefix
.bitlen
;
280 } else if (strcmp(*argv
, "6rd-reset") == 0) {
283 get_prefix(&prefix
, "2002::", AF_INET6
);
284 memcpy(&ip6rdprefix
, prefix
.data
, 16);
286 ip6rdrelayprefix
= 0;
287 ip6rdrelayprefixlen
= 0;
289 usage(strcmp(lu
->id
, "sit") == 0);
293 if (ttl
&& pmtudisc
== 0) {
294 fprintf(stderr
, "ttl != 0 and nopmtudisc are incompatible\n");
298 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
299 addattr32(n
, 1024, IFLA_IPTUN_LOCAL
, laddr
);
300 addattr32(n
, 1024, IFLA_IPTUN_REMOTE
, raddr
);
301 addattr8(n
, 1024, IFLA_IPTUN_TTL
, ttl
);
302 addattr8(n
, 1024, IFLA_IPTUN_TOS
, tos
);
303 addattr8(n
, 1024, IFLA_IPTUN_PMTUDISC
, pmtudisc
);
305 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
306 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
307 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
308 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
310 if (strcmp(lu
->id
, "sit") == 0) {
311 addattr16(n
, 1024, IFLA_IPTUN_FLAGS
, iflags
);
312 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
313 if (ip6rdprefixlen
) {
314 addattr_l(n
, 1024, IFLA_IPTUN_6RD_PREFIX
,
315 &ip6rdprefix
, sizeof(ip6rdprefix
));
316 addattr16(n
, 1024, IFLA_IPTUN_6RD_PREFIXLEN
,
318 addattr32(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX
,
320 addattr16(n
, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN
,
321 ip6rdrelayprefixlen
);
328 static void iptunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
332 const char *local
= "any";
333 const char *remote
= "any";
338 if (tb
[IFLA_IPTUN_REMOTE
]) {
339 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_REMOTE
]);
342 remote
= format_host(AF_INET
, 4, &addr
);
345 fprintf(f
, "remote %s ", remote
);
347 if (tb
[IFLA_IPTUN_LOCAL
]) {
348 unsigned int addr
= rta_getattr_u32(tb
[IFLA_IPTUN_LOCAL
]);
351 local
= format_host(AF_INET
, 4, &addr
);
354 fprintf(f
, "local %s ", local
);
356 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
357 unsigned int link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
358 const char *n
= if_indextoname(link
, s2
);
361 fprintf(f
, "dev %s ", n
);
363 fprintf(f
, "dev %u ", link
);
366 if (tb
[IFLA_IPTUN_TTL
] && rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]))
367 fprintf(f
, "ttl %d ", rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
369 fprintf(f
, "ttl inherit ");
371 if (tb
[IFLA_IPTUN_TOS
] && rta_getattr_u8(tb
[IFLA_IPTUN_TOS
])) {
372 int tos
= rta_getattr_u8(tb
[IFLA_IPTUN_TOS
]);
376 fputs("inherit ", f
);
378 fprintf(f
, "0x%x ", tos
);
381 if (tb
[IFLA_IPTUN_PMTUDISC
] && rta_getattr_u8(tb
[IFLA_IPTUN_PMTUDISC
]))
382 fprintf(f
, "pmtudisc ");
384 fprintf(f
, "nopmtudisc ");
386 if (tb
[IFLA_IPTUN_FLAGS
]) {
387 __u16 iflags
= rta_getattr_u16(tb
[IFLA_IPTUN_FLAGS
]);
389 if (iflags
& SIT_ISATAP
)
390 fprintf(f
, "isatap ");
393 if (tb
[IFLA_IPTUN_6RD_PREFIXLEN
] &&
394 *(__u16
*)RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIXLEN
])) {
395 __u16 prefixlen
= rta_getattr_u16(tb
[IFLA_IPTUN_6RD_PREFIXLEN
]);
396 __u16 relayprefixlen
=
397 rta_getattr_u16(tb
[IFLA_IPTUN_6RD_RELAY_PREFIXLEN
]);
399 rta_getattr_u32(tb
[IFLA_IPTUN_6RD_RELAY_PREFIX
]);
401 printf("6rd-prefix %s/%u ",
402 inet_ntop(AF_INET6
, RTA_DATA(tb
[IFLA_IPTUN_6RD_PREFIX
]),
406 printf("6rd-relay_prefix %s/%u ",
407 format_host(AF_INET
, 4, &relayprefix
),
412 if (tb
[IFLA_IPTUN_ENCAP_TYPE
] &&
413 *(__u16
*)RTA_DATA(tb
[IFLA_IPTUN_ENCAP_TYPE
]) != TUNNEL_ENCAP_NONE
) {
414 __u16 type
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
]);
415 __u16 flags
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_FLAGS
]);
416 __u16 sport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_SPORT
]);
417 __u16 dport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_DPORT
]);
421 case TUNNEL_ENCAP_FOU
:
424 case TUNNEL_ENCAP_GUE
:
428 fputs("unknown ", f
);
433 fputs("encap-sport auto ", f
);
435 fprintf(f
, "encap-sport %u", ntohs(sport
));
437 fprintf(f
, "encap-dport %u ", ntohs(dport
));
439 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
440 fputs("encap-csum ", f
);
442 fputs("noencap-csum ", f
);
444 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
445 fputs("encap-csum6 ", f
);
447 fputs("noencap-csum6 ", f
);
449 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
450 fputs("encap-remcsum ", f
);
452 fputs("noencap-remcsum ", f
);
456 static void iptunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
459 print_usage(f
, strcmp(lu
->id
, "sit") == 0);
462 struct link_util ipip_link_util
= {
464 .maxattr
= IFLA_IPTUN_MAX
,
465 .parse_opt
= iptunnel_parse_opt
,
466 .print_opt
= iptunnel_print_opt
,
467 .print_help
= iptunnel_print_help
,
470 struct link_util sit_link_util
= {
472 .maxattr
= IFLA_IPTUN_MAX
,
473 .parse_opt
= iptunnel_parse_opt
,
474 .print_opt
= iptunnel_print_opt
,
475 .print_help
= iptunnel_print_help
,