2 * link_ip6tnl.c ip6tnl 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>
21 #include <linux/ip6_tunnel.h>
24 #include "ip_common.h"
27 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
28 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
30 #define DEFAULT_TNL_HOP_LIMIT (64)
32 static void print_usage(FILE *f
)
34 fprintf(f
, "Usage: ip link { add | set | change | replace | del } NAME\n");
35 fprintf(f
, " [ mode { ip6ip6 | ipip6 | any } ]\n");
36 fprintf(f
, " type ip6tnl [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(f
, " [ dev PHYS_DEV ] [ encaplimit ELIM ]\n");
38 fprintf(f
, " [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
39 fprintf(f
, " [ dscp inherit ] [ fwmark inherit ]\n");
40 fprintf(f
, " [ noencap ] [ encap { fou | gue | none } ]\n");
41 fprintf(f
, " [ encap-sport PORT ] [ encap-dport PORT ]\n");
42 fprintf(f
, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n");
43 fprintf(f
, " [ external ]\n");
45 fprintf(f
, "Where: NAME := STRING\n");
46 fprintf(f
, " ADDR := IPV6_ADDRESS\n");
47 fprintf(f
, " ELIM := { none | 0..255 }(default=%d)\n",
48 IPV6_DEFAULT_TNL_ENCAP_LIMIT
);
49 fprintf(f
, " HLIM := 0..255 (default=%d)\n",
50 DEFAULT_TNL_HOP_LIMIT
);
51 fprintf(f
, " TCLASS := { 0x0..0xff | inherit }\n");
52 fprintf(f
, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
55 static void usage(void) __attribute__((noreturn
));
56 static void usage(void)
62 static int ip6tunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
65 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
71 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
)),
72 .n
.nlmsg_flags
= NLM_F_REQUEST
,
73 .n
.nlmsg_type
= RTM_GETLINK
,
74 .i
.ifi_family
= preferred_family
,
75 .i
.ifi_index
= ifi
->ifi_index
,
77 struct rtattr
*tb
[IFLA_MAX
+ 1];
78 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
79 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
81 struct in6_addr laddr
= {};
82 struct in6_addr raddr
= {};
83 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
84 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
90 __u16 encapflags
= TUNNEL_ENCAP_FLAG_CSUM6
;
95 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
96 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
99 "Failed to get existing tunnel info.\n");
103 len
= req
.n
.nlmsg_len
;
104 len
-= NLMSG_LENGTH(sizeof(*ifi
));
108 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
110 if (!tb
[IFLA_LINKINFO
])
113 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
115 if (!linkinfo
[IFLA_INFO_DATA
])
118 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
119 linkinfo
[IFLA_INFO_DATA
]);
121 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
122 memcpy(&laddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_LOCAL
]),
125 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
126 memcpy(&raddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_REMOTE
]),
129 if (iptuninfo
[IFLA_IPTUN_TTL
])
130 hop_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
132 if (iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
])
133 encap_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
]);
135 if (iptuninfo
[IFLA_IPTUN_FLOWINFO
])
136 flowinfo
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLOWINFO
]);
138 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
139 flags
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLAGS
]);
141 if (iptuninfo
[IFLA_IPTUN_LINK
])
142 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
144 if (iptuninfo
[IFLA_IPTUN_PROTO
])
145 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
146 if (iptuninfo
[IFLA_IPTUN_COLLECT_METADATA
])
151 if (matches(*argv
, "mode") == 0) {
153 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
154 strcmp(*argv
, "ip6ip6") == 0)
155 proto
= IPPROTO_IPV6
;
156 else if (strcmp(*argv
, "ip/ipv6") == 0 ||
157 strcmp(*argv
, "ipv4/ipv6") == 0 ||
158 strcmp(*argv
, "ipip6") == 0 ||
159 strcmp(*argv
, "ip4ip6") == 0)
160 proto
= IPPROTO_IPIP
;
161 else if (strcmp(*argv
, "any/ipv6") == 0 ||
162 strcmp(*argv
, "any") == 0)
165 invarg("Cannot guess tunnel mode.", *argv
);
166 } else if (strcmp(*argv
, "remote") == 0) {
170 get_prefix(&addr
, *argv
, preferred_family
);
171 if (addr
.family
== AF_UNSPEC
)
172 invarg("\"remote\" address family is AF_UNSPEC", *argv
);
173 memcpy(&raddr
, addr
.data
, addr
.bytelen
);
174 } else if (strcmp(*argv
, "local") == 0) {
178 get_prefix(&addr
, *argv
, preferred_family
);
179 if (addr
.family
== AF_UNSPEC
)
180 invarg("\"local\" address family is AF_UNSPEC", *argv
);
181 memcpy(&laddr
, addr
.data
, addr
.bytelen
);
182 } else if (matches(*argv
, "dev") == 0) {
184 link
= if_nametoindex(*argv
);
186 invarg("\"dev\" is invalid", *argv
);
187 } else if (strcmp(*argv
, "hoplimit") == 0 ||
188 strcmp(*argv
, "ttl") == 0 ||
189 strcmp(*argv
, "hlim") == 0) {
193 if (get_u8(&uval
, *argv
, 0))
194 invarg("invalid HLIM", *argv
);
196 } else if (strcmp(*argv
, "encaplimit") == 0) {
198 if (strcmp(*argv
, "none") == 0) {
199 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
203 if (get_u8(&uval
, *argv
, 0) < -1)
204 invarg("invalid ELIM", *argv
);
206 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
208 } else if (strcmp(*argv
, "tclass") == 0 ||
209 strcmp(*argv
, "tc") == 0 ||
210 strcmp(*argv
, "tos") == 0 ||
211 matches(*argv
, "dsfield") == 0) {
215 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
216 if (strcmp(*argv
, "inherit") == 0)
217 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
219 if (get_u8(&uval
, *argv
, 16))
220 invarg("invalid TClass", *argv
);
221 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
222 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
224 } else if (strcmp(*argv
, "flowlabel") == 0 ||
225 strcmp(*argv
, "fl") == 0) {
229 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
230 if (strcmp(*argv
, "inherit") == 0)
231 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
233 if (get_u32(&uval
, *argv
, 16))
234 invarg("invalid Flowlabel", *argv
);
236 invarg("invalid Flowlabel", *argv
);
237 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
238 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
240 } else if (strcmp(*argv
, "dscp") == 0) {
242 if (strcmp(*argv
, "inherit") != 0)
243 invarg("not inherit", *argv
);
244 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
245 } else if (strcmp(*argv
, "fwmark") == 0) {
247 if (strcmp(*argv
, "inherit") != 0)
248 invarg("not inherit", *argv
);
249 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
250 } else if (strcmp(*argv
, "noencap") == 0) {
251 encaptype
= TUNNEL_ENCAP_NONE
;
252 } else if (strcmp(*argv
, "encap") == 0) {
254 if (strcmp(*argv
, "fou") == 0)
255 encaptype
= TUNNEL_ENCAP_FOU
;
256 else if (strcmp(*argv
, "gue") == 0)
257 encaptype
= TUNNEL_ENCAP_GUE
;
258 else if (strcmp(*argv
, "none") == 0)
259 encaptype
= TUNNEL_ENCAP_NONE
;
261 invarg("Invalid encap type.", *argv
);
262 } else if (strcmp(*argv
, "encap-sport") == 0) {
264 if (strcmp(*argv
, "auto") == 0)
266 else if (get_u16(&encapsport
, *argv
, 0))
267 invarg("Invalid source port.", *argv
);
268 } else if (strcmp(*argv
, "encap-dport") == 0) {
270 if (get_u16(&encapdport
, *argv
, 0))
271 invarg("Invalid destination port.", *argv
);
272 } else if (strcmp(*argv
, "encap-csum") == 0) {
273 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM
;
274 } else if (strcmp(*argv
, "noencap-csum") == 0) {
275 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM
;
276 } else if (strcmp(*argv
, "encap-udp6-csum") == 0) {
277 encapflags
|= TUNNEL_ENCAP_FLAG_CSUM6
;
278 } else if (strcmp(*argv
, "noencap-udp6-csum") == 0) {
279 encapflags
&= ~TUNNEL_ENCAP_FLAG_CSUM6
;
280 } else if (strcmp(*argv
, "encap-remcsum") == 0) {
281 encapflags
|= TUNNEL_ENCAP_FLAG_REMCSUM
;
282 } else if (strcmp(*argv
, "noencap-remcsum") == 0) {
283 encapflags
|= ~TUNNEL_ENCAP_FLAG_REMCSUM
;
284 } else if (strcmp(*argv
, "external") == 0) {
291 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
293 addattr_l(n
, 1024, IFLA_IPTUN_COLLECT_METADATA
, NULL
, 0);
296 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
, &laddr
, sizeof(laddr
));
297 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
, &raddr
, sizeof(raddr
));
298 addattr8(n
, 1024, IFLA_IPTUN_TTL
, hop_limit
);
299 addattr8(n
, 1024, IFLA_IPTUN_ENCAP_LIMIT
, encap_limit
);
300 addattr32(n
, 1024, IFLA_IPTUN_FLOWINFO
, flowinfo
);
301 addattr32(n
, 1024, IFLA_IPTUN_FLAGS
, flags
);
302 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
304 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_TYPE
, encaptype
);
305 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_FLAGS
, encapflags
);
306 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_SPORT
, htons(encapsport
));
307 addattr16(n
, 1024, IFLA_IPTUN_ENCAP_DPORT
, htons(encapdport
));
312 static void ip6tunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
321 if (tb
[IFLA_IPTUN_FLAGS
])
322 flags
= rta_getattr_u32(tb
[IFLA_IPTUN_FLAGS
]);
324 if (tb
[IFLA_IPTUN_FLOWINFO
])
325 flowinfo
= rta_getattr_u32(tb
[IFLA_IPTUN_FLOWINFO
]);
327 if (tb
[IFLA_IPTUN_PROTO
]) {
328 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
330 fprintf(f
, "ipip6 ");
333 fprintf(f
, "ip6ip6 ");
341 if (tb
[IFLA_IPTUN_REMOTE
]) {
342 fprintf(f
, "remote %s ",
343 rt_addr_n2a_rta(AF_INET6
, tb
[IFLA_IPTUN_REMOTE
]));
346 if (tb
[IFLA_IPTUN_LOCAL
]) {
347 fprintf(f
, "local %s ",
348 rt_addr_n2a_rta(AF_INET6
, tb
[IFLA_IPTUN_LOCAL
]));
351 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
352 unsigned int link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
353 const char *n
= if_indextoname(link
, s2
);
356 fprintf(f
, "dev %s ", n
);
358 fprintf(f
, "dev %u ", link
);
361 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
362 printf("encaplimit none ");
363 else if (tb
[IFLA_IPTUN_ENCAP_LIMIT
])
364 fprintf(f
, "encaplimit %u ",
365 rta_getattr_u8(tb
[IFLA_IPTUN_ENCAP_LIMIT
]));
367 if (tb
[IFLA_IPTUN_TTL
])
368 fprintf(f
, "hoplimit %u ", rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
370 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
371 printf("tclass inherit ");
372 else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
373 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
);
375 printf("tclass 0x%02x ", (__u8
)(val
>> 20));
378 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
379 printf("flowlabel inherit ");
381 printf("flowlabel 0x%05x ", ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
383 printf("(flowinfo 0x%08x) ", ntohl(flowinfo
));
385 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
386 printf("dscp inherit ");
388 if (flags
& IP6_TNL_F_MIP6_DEV
)
391 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
392 fprintf(f
, "fwmark inherit ");
394 if (tb
[IFLA_IPTUN_ENCAP_TYPE
] &&
395 rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
]) !=
397 __u16 type
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_TYPE
]);
398 __u16 flags
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_FLAGS
]);
399 __u16 sport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_SPORT
]);
400 __u16 dport
= rta_getattr_u16(tb
[IFLA_IPTUN_ENCAP_DPORT
]);
404 case TUNNEL_ENCAP_FOU
:
407 case TUNNEL_ENCAP_GUE
:
411 fputs("unknown ", f
);
416 fputs("encap-sport auto ", f
);
418 fprintf(f
, "encap-sport %u", ntohs(sport
));
420 fprintf(f
, "encap-dport %u ", ntohs(dport
));
422 if (flags
& TUNNEL_ENCAP_FLAG_CSUM
)
423 fputs("encap-csum ", f
);
425 fputs("noencap-csum ", f
);
427 if (flags
& TUNNEL_ENCAP_FLAG_CSUM6
)
428 fputs("encap-csum6 ", f
);
430 fputs("noencap-csum6 ", f
);
432 if (flags
& TUNNEL_ENCAP_FLAG_REMCSUM
)
433 fputs("encap-remcsum ", f
);
435 fputs("noencap-remcsum ", f
);
439 static void ip6tunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
445 struct link_util ip6tnl_link_util
= {
447 .maxattr
= IFLA_IPTUN_MAX
,
448 .parse_opt
= ip6tunnel_parse_opt
,
449 .print_opt
= ip6tunnel_print_opt
,
450 .print_help
= ip6tunnel_print_help
,