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");
41 fprintf(f
, "Where: NAME := STRING\n");
42 fprintf(f
, " ADDR := IPV6_ADDRESS\n");
43 fprintf(f
, " ELIM := { none | 0..255 }(default=%d)\n",
44 IPV6_DEFAULT_TNL_ENCAP_LIMIT
);
45 fprintf(f
, " HLIM := 0..255 (default=%d)\n",
46 DEFAULT_TNL_HOP_LIMIT
);
47 fprintf(f
, " TCLASS := { 0x0..0xff | inherit }\n");
48 fprintf(f
, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
51 static void usage(void) __attribute__((noreturn
));
52 static void usage(void)
58 static int ip6tunnel_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
66 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
67 struct rtattr
*tb
[IFLA_MAX
+ 1];
68 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
69 struct rtattr
*iptuninfo
[IFLA_IPTUN_MAX
+ 1];
71 struct in6_addr laddr
;
72 struct in6_addr raddr
;
73 __u8 hop_limit
= DEFAULT_TNL_HOP_LIMIT
;
74 __u8 encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
80 memset(&laddr
, 0, sizeof(laddr
));
81 memset(&raddr
, 0, sizeof(raddr
));
83 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
84 memset(&req
, 0, sizeof(req
));
86 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
));
87 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
88 req
.n
.nlmsg_type
= RTM_GETLINK
;
89 req
.i
.ifi_family
= preferred_family
;
90 req
.i
.ifi_index
= ifi
->ifi_index
;
92 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 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 memcpy(&laddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_LOCAL
]),
121 if (iptuninfo
[IFLA_IPTUN_REMOTE
])
122 memcpy(&raddr
, RTA_DATA(iptuninfo
[IFLA_IPTUN_REMOTE
]),
125 if (iptuninfo
[IFLA_IPTUN_TTL
])
126 hop_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_TTL
]);
128 if (iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
])
129 encap_limit
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_ENCAP_LIMIT
]);
131 if (iptuninfo
[IFLA_IPTUN_FLOWINFO
])
132 flowinfo
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLOWINFO
]);
134 if (iptuninfo
[IFLA_IPTUN_FLAGS
])
135 flags
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_FLAGS
]);
137 if (iptuninfo
[IFLA_IPTUN_LINK
])
138 link
= rta_getattr_u32(iptuninfo
[IFLA_IPTUN_LINK
]);
140 if (iptuninfo
[IFLA_IPTUN_PROTO
])
141 proto
= rta_getattr_u8(iptuninfo
[IFLA_IPTUN_PROTO
]);
145 if (matches(*argv
, "mode") == 0) {
147 if (strcmp(*argv
, "ipv6/ipv6") == 0 ||
148 strcmp(*argv
, "ip6ip6") == 0)
149 proto
= IPPROTO_IPV6
;
150 else if (strcmp(*argv
, "ip/ipv6") == 0 ||
151 strcmp(*argv
, "ipv4/ipv6") == 0 ||
152 strcmp(*argv
, "ipip6") == 0 ||
153 strcmp(*argv
, "ip4ip6") == 0)
154 proto
= IPPROTO_IPIP
;
155 else if (strcmp(*argv
, "any/ipv6") == 0 ||
156 strcmp(*argv
, "any") == 0)
159 invarg("Cannot guess tunnel mode.", *argv
);
160 } else if (strcmp(*argv
, "remote") == 0) {
163 get_prefix(&addr
, *argv
, preferred_family
);
164 if (addr
.family
== AF_UNSPEC
)
165 invarg("\"remote\" address family is AF_UNSPEC", *argv
);
166 memcpy(&raddr
, addr
.data
, addr
.bytelen
);
167 } else if (strcmp(*argv
, "local") == 0) {
170 get_prefix(&addr
, *argv
, preferred_family
);
171 if (addr
.family
== AF_UNSPEC
)
172 invarg("\"local\" address family is AF_UNSPEC", *argv
);
173 memcpy(&laddr
, addr
.data
, addr
.bytelen
);
174 } else if (matches(*argv
, "dev") == 0) {
176 link
= if_nametoindex(*argv
);
178 invarg("\"dev\" is invalid", *argv
);
179 } else if (strcmp(*argv
, "hoplimit") == 0 ||
180 strcmp(*argv
, "ttl") == 0 ||
181 strcmp(*argv
, "hlim") == 0) {
184 if (get_u8(&uval
, *argv
, 0))
185 invarg("invalid HLIM", *argv
);
187 } else if (matches(*argv
, "encaplimit") == 0) {
189 if (strcmp(*argv
, "none") == 0) {
190 flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
193 if (get_u8(&uval
, *argv
, 0) < -1)
194 invarg("invalid ELIM", *argv
);
196 flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
198 } else if (strcmp(*argv
, "tclass") == 0 ||
199 strcmp(*argv
, "tc") == 0 ||
200 strcmp(*argv
, "tos") == 0 ||
201 matches(*argv
, "dsfield") == 0) {
204 flowinfo
&= ~IP6_FLOWINFO_TCLASS
;
205 if (strcmp(*argv
, "inherit") == 0)
206 flags
|= IP6_TNL_F_USE_ORIG_TCLASS
;
208 if (get_u8(&uval
, *argv
, 16))
209 invarg("invalid TClass", *argv
);
210 flowinfo
|= htonl((__u32
)uval
<< 20) & IP6_FLOWINFO_TCLASS
;
211 flags
&= ~IP6_TNL_F_USE_ORIG_TCLASS
;
213 } else if (strcmp(*argv
, "flowlabel") == 0 ||
214 strcmp(*argv
, "fl") == 0) {
217 flowinfo
&= ~IP6_FLOWINFO_FLOWLABEL
;
218 if (strcmp(*argv
, "inherit") == 0)
219 flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
221 if (get_u32(&uval
, *argv
, 16))
222 invarg("invalid Flowlabel", *argv
);
224 invarg("invalid Flowlabel", *argv
);
225 flowinfo
|= htonl(uval
) & IP6_FLOWINFO_FLOWLABEL
;
226 flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
228 } else if (strcmp(*argv
, "dscp") == 0) {
230 if (strcmp(*argv
, "inherit") != 0)
231 invarg("not inherit", *argv
);
232 flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
233 } else if (strcmp(*argv
, "fwmark") == 0) {
235 if (strcmp(*argv
, "inherit") != 0)
236 invarg("not inherit", *argv
);
237 flags
|= IP6_TNL_F_USE_ORIG_FWMARK
;
243 addattr8(n
, 1024, IFLA_IPTUN_PROTO
, proto
);
244 addattr_l(n
, 1024, IFLA_IPTUN_LOCAL
, &laddr
, sizeof(laddr
));
245 addattr_l(n
, 1024, IFLA_IPTUN_REMOTE
, &raddr
, sizeof(raddr
));
246 addattr8(n
, 1024, IFLA_IPTUN_TTL
, hop_limit
);
247 addattr8(n
, 1024, IFLA_IPTUN_ENCAP_LIMIT
, encap_limit
);
248 addattr32(n
, 1024, IFLA_IPTUN_FLOWINFO
, flowinfo
);
249 addattr32(n
, 1024, IFLA_IPTUN_FLAGS
, flags
);
250 addattr32(n
, 1024, IFLA_IPTUN_LINK
, link
);
255 static void ip6tunnel_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
265 if (tb
[IFLA_IPTUN_FLAGS
])
266 flags
= rta_getattr_u32(tb
[IFLA_IPTUN_FLAGS
]);
268 if (tb
[IFLA_IPTUN_FLOWINFO
])
269 flowinfo
= rta_getattr_u32(tb
[IFLA_IPTUN_FLOWINFO
]);
271 if (tb
[IFLA_IPTUN_PROTO
]) {
272 switch (rta_getattr_u8(tb
[IFLA_IPTUN_PROTO
])) {
274 fprintf(f
, "ipip6 ");
277 fprintf(f
, "ip6ip6 ");
285 if (tb
[IFLA_IPTUN_REMOTE
]) {
286 fprintf(f
, "remote %s ",
287 rt_addr_n2a(AF_INET6
,
288 RTA_PAYLOAD(tb
[IFLA_IPTUN_REMOTE
]),
289 RTA_DATA(tb
[IFLA_IPTUN_REMOTE
]),
293 if (tb
[IFLA_IPTUN_LOCAL
]) {
294 fprintf(f
, "local %s ",
295 rt_addr_n2a(AF_INET6
,
296 RTA_PAYLOAD(tb
[IFLA_IPTUN_LOCAL
]),
297 RTA_DATA(tb
[IFLA_IPTUN_LOCAL
]),
301 if (tb
[IFLA_IPTUN_LINK
] && rta_getattr_u32(tb
[IFLA_IPTUN_LINK
])) {
302 unsigned link
= rta_getattr_u32(tb
[IFLA_IPTUN_LINK
]);
303 const char *n
= if_indextoname(link
, s2
);
306 fprintf(f
, "dev %s ", n
);
308 fprintf(f
, "dev %u ", link
);
311 if (flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)
312 printf("encaplimit none ");
313 else if (tb
[IFLA_IPTUN_ENCAP_LIMIT
])
314 fprintf(f
, "encaplimit %u ",
315 rta_getattr_u8(tb
[IFLA_IPTUN_ENCAP_LIMIT
]));
317 if (tb
[IFLA_IPTUN_TTL
])
318 fprintf(f
, "hoplimit %u ", rta_getattr_u8(tb
[IFLA_IPTUN_TTL
]));
320 if (flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
321 printf("tclass inherit ");
322 else if (tb
[IFLA_IPTUN_FLOWINFO
]) {
323 __u32 val
= ntohl(flowinfo
& IP6_FLOWINFO_TCLASS
);
325 printf("tclass 0x%02x ", (__u8
)(val
>> 20));
328 if (flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
329 printf("flowlabel inherit ");
331 printf("flowlabel 0x%05x ", ntohl(flowinfo
& IP6_FLOWINFO_FLOWLABEL
));
333 printf("(flowinfo 0x%08x) ", ntohl(flowinfo
));
335 if (flags
& IP6_TNL_F_RCV_DSCP_COPY
)
336 printf("dscp inherit ");
338 if (flags
& IP6_TNL_F_MIP6_DEV
)
341 if (flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
342 fprintf(f
, "fwmark inherit ");
345 static void ip6tunnel_print_help(struct link_util
*lu
, int argc
, char **argv
,
351 struct link_util ip6tnl_link_util
= {
353 .maxattr
= IFLA_IPTUN_MAX
,
354 .parse_opt
= ip6tunnel_parse_opt
,
355 .print_opt
= ip6tunnel_print_opt
,
356 .print_help
= ip6tunnel_print_help
,