]>
Commit | Line | Data |
---|---|---|
1ce2de97 ND |
1 | /* |
2 | * link_iptnl.c ipip and sit driver module | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Nicolas Dichtel <nicolas.dichtel@6wind.com> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <string.h> | |
14 | #include <net/if.h> | |
15 | #include <sys/types.h> | |
16 | #include <sys/socket.h> | |
17 | #include <arpa/inet.h> | |
18 | ||
19 | #include <linux/ip.h> | |
20 | #include <linux/if_tunnel.h> | |
21 | #include "rt_names.h" | |
22 | #include "utils.h" | |
23 | #include "ip_common.h" | |
24 | #include "tunnel.h" | |
25 | ||
26 | static void usage(int sit) __attribute__((noreturn)); | |
27 | static void usage(int sit) | |
28 | { | |
29 | fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n"); | |
30 | fprintf(stderr, " type { ipip | sit } [ remote ADDR ] [ local ADDR ]\n"); | |
31 | fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n"); | |
32 | fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n"); | |
77620be8 ND |
33 | if (sit) { |
34 | fprintf(stderr, " [ mode { ip6ip | ipip | any } ]\n"); | |
1ce2de97 | 35 | fprintf(stderr, " [ isatap ]\n"); |
77620be8 | 36 | } |
1ce2de97 ND |
37 | fprintf(stderr, "\n"); |
38 | fprintf(stderr, "Where: NAME := STRING\n"); | |
39 | fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n"); | |
40 | fprintf(stderr, " TOS := { NUMBER | inherit }\n"); | |
41 | fprintf(stderr, " TTL := { 1..255 | inherit }\n"); | |
42 | exit(-1); | |
43 | } | |
44 | ||
45 | static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv, | |
46 | struct nlmsghdr *n) | |
47 | { | |
48 | struct { | |
49 | struct nlmsghdr n; | |
50 | struct ifinfomsg i; | |
51 | char buf[2048]; | |
52 | } req; | |
53 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); | |
54 | struct rtattr *tb[IFLA_MAX + 1]; | |
55 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
56 | struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1]; | |
57 | int len; | |
58 | __u32 link = 0; | |
59 | __u32 laddr = 0; | |
60 | __u32 raddr = 0; | |
61 | __u8 ttl = 0; | |
62 | __u8 tos = 0; | |
63 | __u8 pmtudisc = 1; | |
64 | __u16 iflags = 0; | |
77620be8 | 65 | __u8 proto = 0; |
1ce2de97 ND |
66 | struct in6_addr ip6rdprefix; |
67 | __u16 ip6rdprefixlen = 0; | |
68 | __u32 ip6rdrelayprefix = 0; | |
69 | __u16 ip6rdrelayprefixlen = 0; | |
70 | ||
71 | memset(&ip6rdprefix, 0, sizeof(ip6rdprefix)); | |
72 | ||
73 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
74 | memset(&req, 0, sizeof(req)); | |
75 | ||
76 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)); | |
77 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
78 | req.n.nlmsg_type = RTM_GETLINK; | |
79 | req.i.ifi_family = preferred_family; | |
80 | req.i.ifi_index = ifi->ifi_index; | |
81 | ||
82 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) { | |
83 | get_failed: | |
84 | fprintf(stderr, | |
85 | "Failed to get existing tunnel info.\n"); | |
86 | return -1; | |
87 | } | |
88 | ||
89 | len = req.n.nlmsg_len; | |
90 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
91 | if (len < 0) | |
92 | goto get_failed; | |
93 | ||
94 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); | |
95 | ||
96 | if (!tb[IFLA_LINKINFO]) | |
97 | goto get_failed; | |
98 | ||
99 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
100 | ||
101 | if (!linkinfo[IFLA_INFO_DATA]) | |
102 | goto get_failed; | |
103 | ||
104 | parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX, | |
105 | linkinfo[IFLA_INFO_DATA]); | |
106 | ||
107 | if (iptuninfo[IFLA_IPTUN_LOCAL]) | |
108 | laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]); | |
109 | ||
110 | if (iptuninfo[IFLA_IPTUN_REMOTE]) | |
111 | raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]); | |
112 | ||
113 | if (iptuninfo[IFLA_IPTUN_TTL]) | |
114 | ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]); | |
115 | ||
116 | if (iptuninfo[IFLA_IPTUN_TOS]) | |
117 | tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]); | |
118 | ||
119 | if (iptuninfo[IFLA_IPTUN_PMTUDISC]) | |
120 | pmtudisc = | |
121 | rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]); | |
122 | ||
123 | if (iptuninfo[IFLA_IPTUN_FLAGS]) | |
124 | iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]); | |
125 | ||
126 | if (iptuninfo[IFLA_IPTUN_LINK]) | |
127 | link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]); | |
128 | ||
77620be8 ND |
129 | if (iptuninfo[IFLA_IPTUN_PROTO]) |
130 | proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]); | |
131 | ||
1ce2de97 ND |
132 | if (iptuninfo[IFLA_IPTUN_6RD_PREFIX]) |
133 | memcpy(&ip6rdprefix, | |
134 | RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]), | |
135 | sizeof(laddr)); | |
136 | ||
137 | if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]) | |
138 | ip6rdprefixlen = | |
139 | rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]); | |
140 | ||
141 | if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]) | |
142 | ip6rdrelayprefix = | |
143 | rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]); | |
144 | ||
145 | if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) | |
146 | ip6rdrelayprefixlen = | |
147 | rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]); | |
148 | } | |
149 | ||
150 | while (argc > 0) { | |
151 | if (strcmp(*argv, "remote") == 0) { | |
152 | NEXT_ARG(); | |
153 | if (strcmp(*argv, "any")) | |
154 | raddr = get_addr32(*argv); | |
155 | else | |
156 | raddr = 0; | |
157 | } else if (strcmp(*argv, "local") == 0) { | |
158 | NEXT_ARG(); | |
159 | if (strcmp(*argv, "any")) | |
160 | laddr = get_addr32(*argv); | |
161 | else | |
162 | laddr = 0; | |
163 | } else if (matches(*argv, "dev") == 0) { | |
164 | NEXT_ARG(); | |
165 | link = if_nametoindex(*argv); | |
166 | if (link == 0) | |
167 | invarg("\"dev\" is invalid", *argv); | |
168 | } else if (strcmp(*argv, "ttl") == 0 || | |
169 | strcmp(*argv, "hoplimit") == 0) { | |
170 | NEXT_ARG(); | |
171 | if (strcmp(*argv, "inherit") != 0) { | |
172 | if (get_u8(&ttl, *argv, 0)) | |
173 | invarg("invalid TTL\n", *argv); | |
174 | } else | |
175 | ttl = 0; | |
176 | } else if (strcmp(*argv, "tos") == 0 || | |
177 | strcmp(*argv, "tclass") == 0 || | |
178 | matches(*argv, "dsfield") == 0) { | |
179 | __u32 uval; | |
180 | NEXT_ARG(); | |
181 | if (strcmp(*argv, "inherit") != 0) { | |
182 | if (rtnl_dsfield_a2n(&uval, *argv)) | |
183 | invarg("bad TOS value", *argv); | |
184 | tos = uval; | |
185 | } else | |
186 | tos = 1; | |
187 | } else if (strcmp(*argv, "nopmtudisc") == 0) { | |
188 | pmtudisc = 0; | |
189 | } else if (strcmp(*argv, "pmtudisc") == 0) { | |
190 | pmtudisc = 1; | |
191 | } else if (strcmp(lu->id, "sit") == 0 && | |
192 | strcmp(*argv, "isatap") == 0) { | |
193 | iflags |= SIT_ISATAP; | |
77620be8 ND |
194 | } else if (strcmp(lu->id, "sit") == 0 && |
195 | strcmp(*argv, "mode") == 0) { | |
196 | NEXT_ARG(); | |
197 | if (strcmp(*argv, "ipv6/ipv4") == 0 || | |
198 | strcmp(*argv, "ip6ip") == 0) | |
199 | proto = IPPROTO_IPV6; | |
200 | else if (strcmp(*argv, "ipv4/ipv4") == 0 || | |
201 | strcmp(*argv, "ipip") == 0 || | |
202 | strcmp(*argv, "ip4ip4") == 0) | |
203 | proto = IPPROTO_IPIP; | |
204 | else if (strcmp(*argv, "any/ipv4") == 0 || | |
205 | strcmp(*argv, "any") == 0) | |
206 | proto = 0; | |
207 | else | |
208 | invarg("Cannot guess tunnel mode.", *argv); | |
1ce2de97 ND |
209 | } else if (strcmp(*argv, "6rd-prefix") == 0) { |
210 | inet_prefix prefix; | |
211 | NEXT_ARG(); | |
212 | if (get_prefix(&prefix, *argv, AF_INET6)) | |
213 | invarg("invalid 6rd_prefix\n", *argv); | |
214 | memcpy(&ip6rdprefix, prefix.data, 16); | |
215 | ip6rdprefixlen = prefix.bitlen; | |
216 | } else if (strcmp(*argv, "6rd-relay_prefix") == 0) { | |
217 | inet_prefix prefix; | |
218 | NEXT_ARG(); | |
219 | if (get_prefix(&prefix, *argv, AF_INET)) | |
220 | invarg("invalid 6rd-relay_prefix\n", *argv); | |
221 | memcpy(&ip6rdrelayprefix, prefix.data, 4); | |
222 | ip6rdrelayprefixlen = prefix.bitlen; | |
223 | } else if (strcmp(*argv, "6rd-reset") == 0) { | |
224 | inet_prefix prefix; | |
225 | get_prefix(&prefix, "2002::", AF_INET6); | |
226 | memcpy(&ip6rdprefix, prefix.data, 16); | |
227 | ip6rdprefixlen = 16; | |
228 | ip6rdrelayprefix = 0; | |
229 | ip6rdrelayprefixlen = 0; | |
230 | } else | |
231 | usage(strcmp(lu->id, "sit") == 0); | |
232 | argc--, argv++; | |
233 | } | |
234 | ||
235 | if (ttl && pmtudisc == 0) { | |
30d07e9e | 236 | fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n"); |
1ce2de97 ND |
237 | exit(-1); |
238 | } | |
239 | ||
240 | addattr32(n, 1024, IFLA_IPTUN_LINK, link); | |
241 | addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr); | |
242 | addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr); | |
243 | addattr8(n, 1024, IFLA_IPTUN_TTL, ttl); | |
244 | addattr8(n, 1024, IFLA_IPTUN_TOS, tos); | |
245 | addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc); | |
246 | if (strcmp(lu->id, "sit") == 0) { | |
247 | addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags); | |
77620be8 | 248 | addattr8(n, 1024, IFLA_IPTUN_PROTO, proto); |
1ce2de97 ND |
249 | if (ip6rdprefixlen) { |
250 | addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX, | |
251 | &ip6rdprefix, sizeof(ip6rdprefix)); | |
252 | addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN, | |
253 | ip6rdprefixlen); | |
254 | addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX, | |
255 | ip6rdrelayprefix); | |
256 | addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, | |
257 | ip6rdrelayprefixlen); | |
258 | } | |
259 | } | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
264 | static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
265 | { | |
266 | char s1[1024]; | |
267 | char s2[64]; | |
268 | const char *local = "any"; | |
269 | const char *remote = "any"; | |
270 | ||
271 | if (!tb) | |
272 | return; | |
273 | ||
274 | if (tb[IFLA_IPTUN_REMOTE]) { | |
275 | unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]); | |
276 | ||
277 | if (addr) | |
278 | remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); | |
279 | } | |
280 | ||
281 | fprintf(f, "remote %s ", remote); | |
282 | ||
283 | if (tb[IFLA_IPTUN_LOCAL]) { | |
284 | unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]); | |
285 | ||
286 | if (addr) | |
287 | local = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); | |
288 | } | |
289 | ||
290 | fprintf(f, "local %s ", local); | |
291 | ||
292 | if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { | |
293 | unsigned link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); | |
294 | const char *n = if_indextoname(link, s2); | |
295 | ||
296 | if (n) | |
297 | fprintf(f, "dev %s ", n); | |
298 | else | |
299 | fprintf(f, "dev %u ", link); | |
300 | } | |
301 | ||
302 | if (tb[IFLA_IPTUN_TTL] && rta_getattr_u8(tb[IFLA_IPTUN_TTL])) | |
303 | fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_IPTUN_TTL])); | |
304 | else | |
305 | fprintf(f, "ttl inherit "); | |
306 | ||
307 | if (tb[IFLA_IPTUN_TOS] && rta_getattr_u8(tb[IFLA_IPTUN_TOS])) { | |
308 | int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); | |
309 | ||
310 | fputs("tos ", f); | |
311 | if (tos == 1) | |
312 | fputs("inherit ", f); | |
313 | else | |
314 | fprintf(f, "0x%x ", tos); | |
315 | } | |
316 | ||
317 | if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC])) | |
318 | fprintf(f, "pmtudisc "); | |
319 | else | |
320 | fprintf(f, "nopmtudisc "); | |
321 | ||
322 | if (tb[IFLA_IPTUN_FLAGS]) { | |
195f0f62 | 323 | __u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]); |
1ce2de97 | 324 | |
195f0f62 ND |
325 | if (iflags & SIT_ISATAP) |
326 | fprintf(f, "isatap "); | |
1ce2de97 ND |
327 | } |
328 | ||
329 | if (tb[IFLA_IPTUN_6RD_PREFIXLEN] && | |
330 | *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIXLEN])) { | |
331 | __u16 prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]); | |
332 | __u16 relayprefixlen = | |
333 | rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]); | |
334 | __u32 relayprefix = | |
335 | rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]); | |
336 | ||
337 | printf("6rd-prefix %s/%u ", | |
338 | inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]), | |
195f0f62 | 339 | s1, sizeof(s1)), |
1ce2de97 ND |
340 | prefixlen); |
341 | if (relayprefix) { | |
342 | printf("6rd-relay_prefix %s/%u ", | |
343 | format_host(AF_INET, 4, &relayprefix, s1, | |
195f0f62 | 344 | sizeof(s1)), |
1ce2de97 ND |
345 | relayprefixlen); |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | struct link_util ipip_link_util = { | |
351 | .id = "ipip", | |
352 | .maxattr = IFLA_IPTUN_MAX, | |
353 | .parse_opt = iptunnel_parse_opt, | |
354 | .print_opt = iptunnel_print_opt, | |
355 | }; | |
356 | ||
357 | struct link_util sit_link_util = { | |
358 | .id = "sit", | |
359 | .maxattr = IFLA_IPTUN_MAX, | |
360 | .parse_opt = iptunnel_parse_opt, | |
361 | .print_opt = iptunnel_print_opt, | |
362 | }; |