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 usage(void) __attribute__((noreturn
));
33 static void usage(void)
35 fprintf(stderr
, "Usage: ip link { add | set | change | replace | del } NAME\n");
36 fprintf(stderr
, " type ip6tnl [ remote ADDR ] [ local ADDR ]\n");
37 fprintf(stderr
, " [ dev PHYS_DEV ] [ encaplimit ELIM ]\n");
38 fprintf(stderr
," [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
39 fprintf(stderr
, " [ dscp inherit ] [ fwmark inherit ]\n");
40 fprintf(stderr
, "\n");
41 fprintf(stderr
, "Where: NAME := STRING\n");
42 fprintf(stderr
, " ADDR := IPV6_ADDRESS\n");
43 fprintf(stderr
, " ELIM := { none | 0..255 }(default=%d)\n",
44 IPV6_DEFAULT_TNL_ENCAP_LIMIT
);
45 fprintf(stderr
, " HLIM := 0..255 (default=%d)\n",
46 DEFAULT_TNL_HOP_LIMIT
);
47 fprintf(stderr
, " TCLASS := { 0x0..0xff | inherit }\n");
48 fprintf(stderr
, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
52 static int ip6tunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
60 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
61 struct rtattr
*tb
[IFLA_MAX
+ 1];
62 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
63 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
65 struct in6_addr laddr
;
66 struct in6_addr raddr
;
67 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
68 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
74 memset(&laddr
, 0, sizeof(laddr
));
75 memset(&raddr
, 0, sizeof(raddr
));
77 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
78 memset(&req
, 0, sizeof(req
));
80 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
));
81 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
82 req
.n
.nlmsg_type
= RTM_GETLINK
;
83 req
.i
.ifi_family
= preferred_family
;
84 req
.i
.ifi_index
= ifi
->ifi_index
;
86 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 0) {
89 "Failed to get existing tunnel info.\n");
93 len
= req
.n
.nlmsg_len
;
94 len
-= NLMSG_LENGTH(sizeof(*ifi
));
98 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
100 if (!tb
[IFLA_LINKINFO
])
103 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
105 if (!linkinfo
[IFLA_INFO_DATA
])
108 parse_rtattr_nested(iptuninfo
, IFLA_IPTUN_MAX
,
109 linkinfo
[IFLA_INFO_DATA
]);
111 if (iptuninfo
[IFLA_IPTUN_LOCAL
])
112 memcpy(&laddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_LOCAL
]),
115 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
116 memcpy(&raddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_REMOTE
]),
119 if (iptuninfo
[IFLA_IPTUN_TTL
])
120 hop_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
122 if (iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
])
123 encap_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
]);
125 if (iptuninfo
[IFLA_IPTUN_FLOWINFO
])
126 flowinfo
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLOWINFO
]);
128 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
129 flags
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLAGS
]);
131 if (iptuninfo
[IFLA_IPTUN_LINK
])
132 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
134 if (iptuninfo
[IFLA_IPTUN_PROTO
])
135 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
139 if (matches(*argv
, "mode") == 0) {
141 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
142 strcmp(*argv
, "ip6ip6") == 0)
143 proto
= IPPROTO_IPV6
;
144 else if (strcmp(*argv
, "ip/ipv6") == 0 ||
145 strcmp(*argv
, "ipv4/ipv6") == 0 ||
146 strcmp(*argv
, "ipip6") == 0 ||
147 strcmp(*argv
, "ip4ip6") == 0)
148 proto
= IPPROTO_IPIP
;
149 else if (strcmp(*argv
, "any/ipv6") == 0 ||
150 strcmp(*argv
, "any") == 0)
153 invarg("Cannot guess tunnel mode.", *argv
);
154 } else if (strcmp(*argv
, "remote") == 0) {
157 get_prefix(&addr
, *argv
, preferred_family
);
158 if (addr
.family
== AF_UNSPEC
)
159 invarg("\"remote\" address family is AF_UNSPEC", *argv
);
160 memcpy(&raddr
, addr
.data
, addr
.bytelen
);
161 } else if (strcmp(*argv
, "local") == 0) {
164 get_prefix(&addr
, *argv
, preferred_family
);
165 if (addr
.family
== AF_UNSPEC
)
166 invarg("\"local\" address family is AF_UNSPEC", *argv
);
167 memcpy(&laddr
, addr
.data
, addr
.bytelen
);
168 } else if (matches(*argv
, "dev") == 0) {
170 link
= if_nametoindex(*argv
);
172 invarg("\"dev\" is invalid", *argv
);
173 } else if (strcmp(*argv
, "hoplimit") == 0 ||
174 strcmp(*argv
, "ttl") == 0 ||
175 strcmp(*argv
, "hlim") == 0) {
178 if (get_u8(&uval
, *argv
, 0))
179 invarg("invalid HLIM", *argv
);
181 } else if (matches(*argv
, "encaplimit") == 0) {
183 if (strcmp(*argv
, "none") == 0) {
184 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
187 if (get_u8(&uval
, *argv
, 0) < -1)
188 invarg("invalid ELIM", *argv
);
190 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
192 } else if (strcmp(*argv
, "tclass") == 0 ||
193 strcmp(*argv
, "tc") == 0 ||
194 strcmp(*argv
, "tos") == 0 ||
195 matches(*argv
, "dsfield") == 0) {
198 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
199 if (strcmp(*argv
, "inherit") == 0)
200 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
202 if (get_u8(&uval
, *argv
, 16))
203 invarg("invalid TClass", *argv
);
204 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
205 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
207 } else if (strcmp(*argv
, "flowlabel") == 0 ||
208 strcmp(*argv
, "fl") == 0) {
211 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
212 if (strcmp(*argv
, "inherit") == 0)
213 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
215 if (get_u32(&uval
, *argv
, 16))
216 invarg("invalid Flowlabel", *argv
);
218 invarg("invalid Flowlabel", *argv
);
219 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
220 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
222 } else if (strcmp(*argv
, "dscp") == 0) {
224 if (strcmp(*argv
, "inherit") != 0)
225 invarg("not inherit", *argv
);
226 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
227 } else if (strcmp(*argv
, "fwmark") == 0) {
229 if (strcmp(*argv
, "inherit") != 0)
230 invarg("not inherit", *argv
);
231 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
237 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
238 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
, &laddr
, sizeof(laddr
));
239 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
, &raddr
, sizeof(raddr
));
240 addattr8(n
, 1024, IFLA_IPTUN_TTL
, hop_limit
);
241 addattr8(n
, 1024, IFLA_IPTUN_ENCAP_LIMIT
, encap_limit
);
242 addattr32(n
, 1024, IFLA_IPTUN_FLOWINFO
, flowinfo
);
243 addattr32(n
, 1024, IFLA_IPTUN_FLAGS
, flags
);
244 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
249 static void ip6tunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
259 if (tb
[IFLA_IPTUN_FLAGS
])
260 flags
= rta_getattr_u32(tb
[IFLA_IPTUN_FLAGS
]);
262 if (tb
[IFLA_IPTUN_FLOWINFO
])
263 flowinfo
= rta_getattr_u32(tb
[IFLA_IPTUN_FLOWINFO
]);
265 if (tb
[IFLA_IPTUN_PROTO
]) {
266 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
268 fprintf(f
, "ipip6 ");
271 fprintf(f
, "ip6ip6 ");
279 if (tb
[IFLA_IPTUN_REMOTE
]) {
280 fprintf(f
, "remote %s ",
281 rt_addr_n2a(AF_INET6
,
282 RTA_PAYLOAD(tb
[IFLA_IPTUN_REMOTE
]),
283 RTA_DATA(tb
[IFLA_IPTUN_REMOTE
]),
287 if (tb
[IFLA_IPTUN_LOCAL
]) {
288 fprintf(f
, "local %s ",
289 rt_addr_n2a(AF_INET6
,
290 RTA_PAYLOAD(tb
[IFLA_IPTUN_LOCAL
]),
291 RTA_DATA(tb
[IFLA_IPTUN_LOCAL
]),
295 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
296 unsigned link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
297 const char *n
= if_indextoname(link
, s2
);
300 fprintf(f
, "dev %s ", n
);
302 fprintf(f
, "dev %u ", link
);
305 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
306 printf("encaplimit none ");
307 else if (tb
[IFLA_IPTUN_ENCAP_LIMIT
])
308 fprintf(f
, "encaplimit %u ",
309 rta_getattr_u8(tb
[IFLA_IPTUN_ENCAP_LIMIT
]));
311 if (tb
[IFLA_IPTUN_TTL
])
312 fprintf(f
, "hoplimit %u ", rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
314 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
315 printf("tclass inherit ");
316 else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
317 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
);
319 printf("tclass 0x%02x ", (__u8
)(val
>> 20));
322 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
323 printf("flowlabel inherit ");
325 printf("flowlabel 0x%05x ", ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
327 printf("(flowinfo 0x%08x) ", ntohl(flowinfo
));
329 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
330 printf("dscp inherit ");
332 if (flags
& IP6_TNL_F_MIP6_DEV
)
335 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
336 fprintf(f
, "fwmark inherit ");
339 struct link_util ip6tnl_link_util
= {
341 .maxattr
= IFLA_IPTUN_MAX
,
342 .parse_opt
= ip6tunnel_parse_opt
,
343 .print_opt
= ip6tunnel_print_opt
,