]>
Commit | Line | Data |
---|---|---|
af89576d | 1 | /* |
2 | * link_gre6.c gre 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: Dmitry Kozlov <xeb@mail.ru> | |
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 <linux/ip6_tunnel.h> | |
22 | ||
23 | #include "rt_names.h" | |
24 | #include "utils.h" | |
25 | #include "ip_common.h" | |
26 | #include "tunnel.h" | |
27 | ||
28 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) | |
29 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) | |
30 | ||
31 | #define DEFAULT_TNL_HOP_LIMIT (64) | |
32 | ||
561e650e | 33 | static void print_usage(FILE *f) |
af89576d | 34 | { |
561e650e | 35 | fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n"); |
36 | fprintf(f, " type { ip6gre | ip6gretap } [ remote ADDR ] [ local ADDR ]\n"); | |
37 | fprintf(f, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); | |
38 | fprintf(f, " [ hoplimit TTL ] [ encaplimit ELIM ]\n"); | |
39 | fprintf(f, " [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"); | |
40 | fprintf(f, " [ dscp inherit ] [ dev PHYS_DEV ]\n"); | |
41 | fprintf(f, "\n"); | |
42 | fprintf(f, "Where: NAME := STRING\n"); | |
43 | fprintf(f, " ADDR := IPV6_ADDRESS\n"); | |
44 | fprintf(f, " TTL := { 0..255 } (default=%d)\n", | |
af89576d | 45 | DEFAULT_TNL_HOP_LIMIT); |
561e650e | 46 | fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
47 | fprintf(f, " ELIM := { none | 0..255 }(default=%d)\n", | |
af89576d | 48 | IPV6_DEFAULT_TNL_ENCAP_LIMIT); |
561e650e | 49 | fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n"); |
50 | fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n"); | |
51 | } | |
52 | ||
53 | static void usage(void) __attribute__((noreturn)); | |
54 | static void usage(void) | |
55 | { | |
56 | print_usage(stderr); | |
af89576d | 57 | exit(-1); |
58 | } | |
59 | ||
60 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, | |
61 | struct nlmsghdr *n) | |
62 | { | |
d17b136f | 63 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
af89576d | 64 | struct { |
65 | struct nlmsghdr n; | |
66 | struct ifinfomsg i; | |
67 | char buf[1024]; | |
d17b136f PS |
68 | } req = { |
69 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), | |
70 | .n.nlmsg_flags = NLM_F_REQUEST, | |
71 | .n.nlmsg_type = RTM_GETLINK, | |
72 | .i.ifi_family = preferred_family, | |
73 | .i.ifi_index = ifi->ifi_index, | |
74 | }; | |
af89576d | 75 | struct rtattr *tb[IFLA_MAX + 1]; |
76 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
77 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; | |
78 | __u16 iflags = 0; | |
79 | __u16 oflags = 0; | |
56f5daac SH |
80 | unsigned int ikey = 0; |
81 | unsigned int okey = 0; | |
af89576d | 82 | struct in6_addr raddr = IN6ADDR_ANY_INIT; |
83 | struct in6_addr laddr = IN6ADDR_ANY_INIT; | |
56f5daac SH |
84 | unsigned int link = 0; |
85 | unsigned int flowinfo = 0; | |
86 | unsigned int flags = 0; | |
af89576d | 87 | __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT; |
88 | __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; | |
89 | int len; | |
90 | ||
91 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
c079e121 | 92 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { |
af89576d | 93 | get_failed: |
94 | fprintf(stderr, | |
95 | "Failed to get existing tunnel info.\n"); | |
96 | return -1; | |
97 | } | |
98 | ||
99 | len = req.n.nlmsg_len; | |
100 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
101 | if (len < 0) | |
102 | goto get_failed; | |
103 | ||
104 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); | |
105 | ||
106 | if (!tb[IFLA_LINKINFO]) | |
107 | goto get_failed; | |
108 | ||
109 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
110 | ||
111 | if (!linkinfo[IFLA_INFO_DATA]) | |
112 | goto get_failed; | |
113 | ||
114 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, | |
115 | linkinfo[IFLA_INFO_DATA]); | |
116 | ||
117 | if (greinfo[IFLA_GRE_IKEY]) | |
118 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); | |
119 | ||
120 | if (greinfo[IFLA_GRE_OKEY]) | |
121 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); | |
122 | ||
123 | if (greinfo[IFLA_GRE_IFLAGS]) | |
124 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); | |
125 | ||
126 | if (greinfo[IFLA_GRE_OFLAGS]) | |
127 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); | |
128 | ||
129 | if (greinfo[IFLA_GRE_LOCAL]) | |
130 | memcpy(&laddr, RTA_DATA(greinfo[IFLA_GRE_LOCAL]), sizeof(laddr)); | |
131 | ||
132 | if (greinfo[IFLA_GRE_REMOTE]) | |
133 | memcpy(&raddr, RTA_DATA(greinfo[IFLA_GRE_REMOTE]), sizeof(raddr)); | |
134 | ||
135 | if (greinfo[IFLA_GRE_TTL]) | |
136 | hop_limit = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); | |
137 | ||
138 | if (greinfo[IFLA_GRE_LINK]) | |
139 | link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]); | |
140 | ||
141 | if (greinfo[IFLA_GRE_ENCAP_LIMIT]) | |
142 | encap_limit = rta_getattr_u8(greinfo[IFLA_GRE_ENCAP_LIMIT]); | |
143 | ||
144 | if (greinfo[IFLA_GRE_FLOWINFO]) | |
145 | flowinfo = rta_getattr_u32(greinfo[IFLA_GRE_FLOWINFO]); | |
146 | ||
147 | if (greinfo[IFLA_GRE_FLAGS]) | |
148 | flags = rta_getattr_u32(greinfo[IFLA_GRE_FLAGS]); | |
149 | } | |
150 | ||
151 | while (argc > 0) { | |
152 | if (!matches(*argv, "key")) { | |
56f5daac | 153 | unsigned int uval; |
af89576d | 154 | |
155 | NEXT_ARG(); | |
156 | iflags |= GRE_KEY; | |
157 | oflags |= GRE_KEY; | |
158 | if (strchr(*argv, '.')) | |
159 | uval = get_addr32(*argv); | |
160 | else { | |
161 | if (get_unsigned(&uval, *argv, 0) < 0) { | |
162 | fprintf(stderr, | |
163 | "Invalid value for \"key\"\n"); | |
164 | exit(-1); | |
165 | } | |
166 | uval = htonl(uval); | |
167 | } | |
168 | ||
169 | ikey = okey = uval; | |
170 | } else if (!matches(*argv, "ikey")) { | |
56f5daac | 171 | unsigned int uval; |
af89576d | 172 | |
173 | NEXT_ARG(); | |
174 | iflags |= GRE_KEY; | |
175 | if (strchr(*argv, '.')) | |
176 | uval = get_addr32(*argv); | |
177 | else { | |
56f5daac | 178 | if (get_unsigned(&uval, *argv, 0) < 0) { |
af89576d | 179 | fprintf(stderr, "invalid value of \"ikey\"\n"); |
180 | exit(-1); | |
181 | } | |
182 | uval = htonl(uval); | |
183 | } | |
184 | ikey = uval; | |
185 | } else if (!matches(*argv, "okey")) { | |
56f5daac | 186 | unsigned int uval; |
af89576d | 187 | |
188 | NEXT_ARG(); | |
189 | oflags |= GRE_KEY; | |
190 | if (strchr(*argv, '.')) | |
191 | uval = get_addr32(*argv); | |
192 | else { | |
56f5daac | 193 | if (get_unsigned(&uval, *argv, 0) < 0) { |
af89576d | 194 | fprintf(stderr, "invalid value of \"okey\"\n"); |
195 | exit(-1); | |
196 | } | |
197 | uval = htonl(uval); | |
198 | } | |
199 | okey = uval; | |
200 | } else if (!matches(*argv, "seq")) { | |
201 | iflags |= GRE_SEQ; | |
202 | oflags |= GRE_SEQ; | |
203 | } else if (!matches(*argv, "iseq")) { | |
204 | iflags |= GRE_SEQ; | |
205 | } else if (!matches(*argv, "oseq")) { | |
206 | oflags |= GRE_SEQ; | |
207 | } else if (!matches(*argv, "csum")) { | |
208 | iflags |= GRE_CSUM; | |
209 | oflags |= GRE_CSUM; | |
210 | } else if (!matches(*argv, "icsum")) { | |
211 | iflags |= GRE_CSUM; | |
212 | } else if (!matches(*argv, "ocsum")) { | |
213 | oflags |= GRE_CSUM; | |
214 | } else if (!matches(*argv, "remote")) { | |
215 | inet_prefix addr; | |
56f5daac | 216 | |
af89576d | 217 | NEXT_ARG(); |
218 | get_prefix(&addr, *argv, preferred_family); | |
219 | if (addr.family == AF_UNSPEC) | |
220 | invarg("\"remote\" address family is AF_UNSPEC", *argv); | |
221 | memcpy(&raddr, &addr.data, sizeof(raddr)); | |
222 | } else if (!matches(*argv, "local")) { | |
223 | inet_prefix addr; | |
56f5daac | 224 | |
af89576d | 225 | NEXT_ARG(); |
226 | get_prefix(&addr, *argv, preferred_family); | |
227 | if (addr.family == AF_UNSPEC) | |
228 | invarg("\"local\" address family is AF_UNSPEC", *argv); | |
229 | memcpy(&laddr, &addr.data, sizeof(laddr)); | |
230 | } else if (!matches(*argv, "dev")) { | |
231 | NEXT_ARG(); | |
232 | link = if_nametoindex(*argv); | |
0cb6bb51 CW |
233 | if (link == 0) { |
234 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
235 | *argv); | |
af89576d | 236 | exit(-1); |
0cb6bb51 | 237 | } |
af89576d | 238 | } else if (!matches(*argv, "ttl") || |
239 | !matches(*argv, "hoplimit")) { | |
240 | __u8 uval; | |
56f5daac | 241 | |
af89576d | 242 | NEXT_ARG(); |
243 | if (get_u8(&uval, *argv, 0)) | |
244 | invarg("invalid TTL", *argv); | |
245 | hop_limit = uval; | |
246 | } else if (!matches(*argv, "tos") || | |
247 | !matches(*argv, "tclass") || | |
248 | !matches(*argv, "dsfield")) { | |
249 | __u8 uval; | |
56f5daac | 250 | |
af89576d | 251 | NEXT_ARG(); |
252 | if (strcmp(*argv, "inherit") == 0) | |
253 | flags |= IP6_TNL_F_USE_ORIG_TCLASS; | |
254 | else { | |
255 | if (get_u8(&uval, *argv, 16)) | |
256 | invarg("invalid TClass", *argv); | |
257 | flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; | |
258 | flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; | |
259 | } | |
260 | } else if (strcmp(*argv, "flowlabel") == 0 || | |
261 | strcmp(*argv, "fl") == 0) { | |
262 | __u32 uval; | |
56f5daac | 263 | |
af89576d | 264 | NEXT_ARG(); |
265 | if (strcmp(*argv, "inherit") == 0) | |
266 | flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
267 | else { | |
268 | if (get_u32(&uval, *argv, 16)) | |
269 | invarg("invalid Flowlabel", *argv); | |
270 | if (uval > 0xFFFFF) | |
271 | invarg("invalid Flowlabel", *argv); | |
272 | flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; | |
273 | flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
274 | } | |
275 | } else if (strcmp(*argv, "dscp") == 0) { | |
276 | NEXT_ARG(); | |
277 | if (strcmp(*argv, "inherit") != 0) | |
278 | invarg("not inherit", *argv); | |
279 | flags |= IP6_TNL_F_RCV_DSCP_COPY; | |
280 | } else | |
281 | usage(); | |
282 | argc--; argv++; | |
283 | } | |
284 | ||
285 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); | |
286 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); | |
287 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); | |
288 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); | |
289 | addattr_l(n, 1024, IFLA_GRE_LOCAL, &laddr, sizeof(laddr)); | |
290 | addattr_l(n, 1024, IFLA_GRE_REMOTE, &raddr, sizeof(raddr)); | |
291 | if (link) | |
292 | addattr32(n, 1024, IFLA_GRE_LINK, link); | |
293 | addattr_l(n, 1024, IFLA_GRE_TTL, &hop_limit, 1); | |
294 | addattr_l(n, 1024, IFLA_GRE_ENCAP_LIMIT, &encap_limit, 1); | |
295 | addattr_l(n, 1024, IFLA_GRE_FLOWINFO, &flowinfo, 4); | |
296 | addattr_l(n, 1024, IFLA_GRE_FLAGS, &flowinfo, 4); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
302 | { | |
af89576d | 303 | char s2[64]; |
304 | const char *local = "any"; | |
305 | const char *remote = "any"; | |
56f5daac SH |
306 | unsigned int iflags = 0; |
307 | unsigned int oflags = 0; | |
308 | unsigned int flags = 0; | |
309 | unsigned int flowinfo = 0; | |
af89576d | 310 | struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; |
311 | ||
312 | if (!tb) | |
313 | return; | |
314 | ||
315 | if (tb[IFLA_GRE_FLAGS]) | |
316 | flags = rta_getattr_u32(tb[IFLA_GRE_FLAGS]); | |
317 | ||
318 | if (tb[IFLA_GRE_FLOWINFO]) | |
319 | flags = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]); | |
320 | ||
321 | if (tb[IFLA_GRE_REMOTE]) { | |
322 | struct in6_addr addr; | |
56f5daac | 323 | |
af89576d | 324 | memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr)); |
325 | ||
326 | if (memcmp(&addr, &in6_addr_any, sizeof(addr))) | |
a418e451 | 327 | remote = format_host(AF_INET6, sizeof(addr), &addr); |
af89576d | 328 | } |
329 | ||
330 | fprintf(f, "remote %s ", remote); | |
331 | ||
332 | if (tb[IFLA_GRE_LOCAL]) { | |
333 | struct in6_addr addr; | |
56f5daac | 334 | |
af89576d | 335 | memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr)); |
336 | ||
337 | if (memcmp(&addr, &in6_addr_any, sizeof(addr))) | |
a418e451 | 338 | local = format_host(AF_INET6, sizeof(addr), &addr); |
af89576d | 339 | } |
340 | ||
341 | fprintf(f, "local %s ", local); | |
342 | ||
343 | if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { | |
56f5daac | 344 | unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
af89576d | 345 | const char *n = if_indextoname(link, s2); |
346 | ||
347 | if (n) | |
348 | fprintf(f, "dev %s ", n); | |
349 | else | |
350 | fprintf(f, "dev %u ", link); | |
351 | } | |
352 | ||
353 | if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL])) | |
354 | fprintf(f, "hoplimit %d ", rta_getattr_u8(tb[IFLA_GRE_TTL])); | |
355 | ||
356 | if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) | |
357 | fprintf(f, "encaplimit none "); | |
358 | else if (tb[IFLA_GRE_ENCAP_LIMIT]) { | |
359 | int encap_limit = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]); | |
360 | ||
361 | fprintf(f, "encaplimit %d ", encap_limit); | |
362 | } | |
363 | ||
364 | if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) | |
365 | fprintf(f, "flowlabel inherit "); | |
366 | else | |
367 | fprintf(f, "flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); | |
368 | ||
369 | if (flags & IP6_TNL_F_RCV_DSCP_COPY) | |
370 | fprintf(f, "dscp inherit "); | |
371 | ||
372 | if (tb[IFLA_GRE_IFLAGS]) | |
373 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); | |
374 | ||
375 | if (tb[IFLA_GRE_OFLAGS]) | |
376 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); | |
377 | ||
378 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { | |
379 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); | |
380 | fprintf(f, "ikey %s ", s2); | |
381 | } | |
382 | ||
383 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { | |
384 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); | |
385 | fprintf(f, "okey %s ", s2); | |
386 | } | |
387 | ||
388 | if (iflags & GRE_SEQ) | |
389 | fputs("iseq ", f); | |
390 | if (oflags & GRE_SEQ) | |
391 | fputs("oseq ", f); | |
392 | if (iflags & GRE_CSUM) | |
393 | fputs("icsum ", f); | |
394 | if (oflags & GRE_CSUM) | |
395 | fputs("ocsum ", f); | |
396 | } | |
397 | ||
561e650e | 398 | static void gre_print_help(struct link_util *lu, int argc, char **argv, |
399 | FILE *f) | |
400 | { | |
401 | print_usage(f); | |
402 | } | |
403 | ||
af89576d | 404 | struct link_util ip6gre_link_util = { |
405 | .id = "ip6gre", | |
406 | .maxattr = IFLA_GRE_MAX, | |
407 | .parse_opt = gre_parse_opt, | |
408 | .print_opt = gre_print_opt, | |
561e650e | 409 | .print_help = gre_print_help, |
af89576d | 410 | }; |
411 | ||
412 | struct link_util ip6gretap_link_util = { | |
413 | .id = "ip6gretap", | |
414 | .maxattr = IFLA_GRE_MAX, | |
415 | .parse_opt = gre_parse_opt, | |
416 | .print_opt = gre_print_opt, | |
561e650e | 417 | .print_help = gre_print_help, |
af89576d | 418 | }; |