Contents of /trunk/mkinitrd-magellan/busybox/networking/libiproute/iptunnel.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 13383 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 13383 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * iptunnel.c "ip tunnel" |
4 | * |
5 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
6 | * |
7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
8 | * |
9 | * |
10 | * Changes: |
11 | * |
12 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
13 | * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit |
14 | * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag |
15 | */ |
16 | |
17 | #include "libbb.h" |
18 | #include <sys/socket.h> |
19 | #include <sys/ioctl.h> |
20 | |
21 | #include <string.h> |
22 | #include <unistd.h> |
23 | |
24 | #include <netinet/ip.h> |
25 | |
26 | #include <net/if.h> |
27 | #include <net/if_arp.h> |
28 | |
29 | #include <asm/types.h> |
30 | #ifndef __constant_htons |
31 | #define __constant_htons htons |
32 | #endif |
33 | #include <linux/if_tunnel.h> |
34 | |
35 | #include "rt_names.h" |
36 | #include "utils.h" |
37 | #include "ip_common.h" |
38 | |
39 | |
40 | static int do_ioctl_get_ifindex(char *dev) |
41 | { |
42 | struct ifreq ifr; |
43 | int fd; |
44 | |
45 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
46 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
47 | if (ioctl(fd, SIOCGIFINDEX, &ifr)) { |
48 | bb_perror_msg("ioctl"); |
49 | return 0; |
50 | } |
51 | close(fd); |
52 | return ifr.ifr_ifindex; |
53 | } |
54 | |
55 | static int do_ioctl_get_iftype(char *dev) |
56 | { |
57 | struct ifreq ifr; |
58 | int fd; |
59 | |
60 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
61 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
62 | if (ioctl(fd, SIOCGIFHWADDR, &ifr)) { |
63 | bb_perror_msg("ioctl"); |
64 | return -1; |
65 | } |
66 | close(fd); |
67 | return ifr.ifr_addr.sa_family; |
68 | } |
69 | |
70 | |
71 | static char *do_ioctl_get_ifname(int idx) |
72 | { |
73 | static struct ifreq ifr; |
74 | int fd; |
75 | |
76 | ifr.ifr_ifindex = idx; |
77 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
78 | if (ioctl(fd, SIOCGIFNAME, &ifr)) { |
79 | bb_perror_msg("ioctl"); |
80 | return NULL; |
81 | } |
82 | close(fd); |
83 | return ifr.ifr_name; |
84 | } |
85 | |
86 | |
87 | |
88 | static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p) |
89 | { |
90 | struct ifreq ifr; |
91 | int fd; |
92 | int err; |
93 | |
94 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
95 | ifr.ifr_ifru.ifru_data = (void*)p; |
96 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
97 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
98 | if (err) { |
99 | bb_perror_msg("ioctl"); |
100 | } |
101 | close(fd); |
102 | return err; |
103 | } |
104 | |
105 | static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p) |
106 | { |
107 | struct ifreq ifr; |
108 | int fd; |
109 | int err; |
110 | |
111 | if (cmd == SIOCCHGTUNNEL && p->name[0]) { |
112 | strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name)); |
113 | } else { |
114 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
115 | } |
116 | ifr.ifr_ifru.ifru_data = (void*)p; |
117 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
118 | err = ioctl(fd, cmd, &ifr); |
119 | if (err) { |
120 | bb_perror_msg("ioctl"); |
121 | } |
122 | close(fd); |
123 | return err; |
124 | } |
125 | |
126 | static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p) |
127 | { |
128 | struct ifreq ifr; |
129 | int fd; |
130 | int err; |
131 | |
132 | if (p->name[0]) { |
133 | strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name)); |
134 | } else { |
135 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
136 | } |
137 | ifr.ifr_ifru.ifru_data = (void*)p; |
138 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
139 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
140 | if (err) { |
141 | bb_perror_msg("ioctl"); |
142 | } |
143 | close(fd); |
144 | return err; |
145 | } |
146 | |
147 | static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) |
148 | { |
149 | int count = 0; |
150 | char medium[IFNAMSIZ]; |
151 | memset(p, 0, sizeof(*p)); |
152 | memset(&medium, 0, sizeof(medium)); |
153 | |
154 | p->iph.version = 4; |
155 | p->iph.ihl = 5; |
156 | #ifndef IP_DF |
157 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
158 | #endif |
159 | p->iph.frag_off = htons(IP_DF); |
160 | |
161 | while (argc > 0) { |
162 | if (strcmp(*argv, "mode") == 0) { |
163 | NEXT_ARG(); |
164 | if (strcmp(*argv, "ipip") == 0 || |
165 | strcmp(*argv, "ip/ip") == 0) { |
166 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
167 | bb_error_msg("you managed to ask for more than one tunnel mode"); |
168 | exit(-1); |
169 | } |
170 | p->iph.protocol = IPPROTO_IPIP; |
171 | } else if (strcmp(*argv, "gre") == 0 || |
172 | strcmp(*argv, "gre/ip") == 0) { |
173 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
174 | bb_error_msg("you managed to ask for more than one tunnel mode"); |
175 | exit(-1); |
176 | } |
177 | p->iph.protocol = IPPROTO_GRE; |
178 | } else if (strcmp(*argv, "sit") == 0 || |
179 | strcmp(*argv, "ipv6/ip") == 0) { |
180 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
181 | bb_error_msg("you managed to ask for more than one tunnel mode"); |
182 | exit(-1); |
183 | } |
184 | p->iph.protocol = IPPROTO_IPV6; |
185 | } else { |
186 | bb_error_msg("cannot guess tunnel mode"); |
187 | exit(-1); |
188 | } |
189 | } else if (strcmp(*argv, "key") == 0) { |
190 | unsigned uval; |
191 | NEXT_ARG(); |
192 | p->i_flags |= GRE_KEY; |
193 | p->o_flags |= GRE_KEY; |
194 | if (strchr(*argv, '.')) |
195 | p->i_key = p->o_key = get_addr32(*argv); |
196 | else { |
197 | if (get_unsigned(&uval, *argv, 0)<0) { |
198 | bb_error_msg("invalid value of \"key\""); |
199 | exit(-1); |
200 | } |
201 | p->i_key = p->o_key = htonl(uval); |
202 | } |
203 | } else if (strcmp(*argv, "ikey") == 0) { |
204 | unsigned uval; |
205 | NEXT_ARG(); |
206 | p->i_flags |= GRE_KEY; |
207 | if (strchr(*argv, '.')) |
208 | p->o_key = get_addr32(*argv); |
209 | else { |
210 | if (get_unsigned(&uval, *argv, 0)<0) { |
211 | bb_error_msg("invalid value of \"ikey\""); |
212 | exit(-1); |
213 | } |
214 | p->i_key = htonl(uval); |
215 | } |
216 | } else if (strcmp(*argv, "okey") == 0) { |
217 | unsigned uval; |
218 | NEXT_ARG(); |
219 | p->o_flags |= GRE_KEY; |
220 | if (strchr(*argv, '.')) |
221 | p->o_key = get_addr32(*argv); |
222 | else { |
223 | if (get_unsigned(&uval, *argv, 0)<0) { |
224 | bb_error_msg("invalid value of \"okey\""); |
225 | exit(-1); |
226 | } |
227 | p->o_key = htonl(uval); |
228 | } |
229 | } else if (strcmp(*argv, "seq") == 0) { |
230 | p->i_flags |= GRE_SEQ; |
231 | p->o_flags |= GRE_SEQ; |
232 | } else if (strcmp(*argv, "iseq") == 0) { |
233 | p->i_flags |= GRE_SEQ; |
234 | } else if (strcmp(*argv, "oseq") == 0) { |
235 | p->o_flags |= GRE_SEQ; |
236 | } else if (strcmp(*argv, "csum") == 0) { |
237 | p->i_flags |= GRE_CSUM; |
238 | p->o_flags |= GRE_CSUM; |
239 | } else if (strcmp(*argv, "icsum") == 0) { |
240 | p->i_flags |= GRE_CSUM; |
241 | } else if (strcmp(*argv, "ocsum") == 0) { |
242 | p->o_flags |= GRE_CSUM; |
243 | } else if (strcmp(*argv, "nopmtudisc") == 0) { |
244 | p->iph.frag_off = 0; |
245 | } else if (strcmp(*argv, "pmtudisc") == 0) { |
246 | p->iph.frag_off = htons(IP_DF); |
247 | } else if (strcmp(*argv, "remote") == 0) { |
248 | NEXT_ARG(); |
249 | if (strcmp(*argv, "any")) |
250 | p->iph.daddr = get_addr32(*argv); |
251 | } else if (strcmp(*argv, "local") == 0) { |
252 | NEXT_ARG(); |
253 | if (strcmp(*argv, "any")) |
254 | p->iph.saddr = get_addr32(*argv); |
255 | } else if (strcmp(*argv, "dev") == 0) { |
256 | NEXT_ARG(); |
257 | strncpy(medium, *argv, IFNAMSIZ-1); |
258 | } else if (strcmp(*argv, "ttl") == 0) { |
259 | unsigned uval; |
260 | NEXT_ARG(); |
261 | if (strcmp(*argv, "inherit") != 0) { |
262 | if (get_unsigned(&uval, *argv, 0)) |
263 | invarg(*argv, "TTL"); |
264 | if (uval > 255) |
265 | invarg(*argv, "TTL must be <=255"); |
266 | p->iph.ttl = uval; |
267 | } |
268 | } else if (strcmp(*argv, "tos") == 0 || |
269 | matches(*argv, "dsfield") == 0) { |
270 | uint32_t uval; |
271 | NEXT_ARG(); |
272 | if (strcmp(*argv, "inherit") != 0) { |
273 | if (rtnl_dsfield_a2n(&uval, *argv)) |
274 | invarg(*argv, "TOS"); |
275 | p->iph.tos = uval; |
276 | } else |
277 | p->iph.tos = 1; |
278 | } else { |
279 | if (strcmp(*argv, "name") == 0) { |
280 | NEXT_ARG(); |
281 | } |
282 | if (p->name[0]) |
283 | duparg2("name", *argv); |
284 | strncpy(p->name, *argv, IFNAMSIZ); |
285 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
286 | struct ip_tunnel_parm old_p; |
287 | memset(&old_p, 0, sizeof(old_p)); |
288 | if (do_get_ioctl(*argv, &old_p)) |
289 | return -1; |
290 | *p = old_p; |
291 | } |
292 | } |
293 | count++; |
294 | argc--; argv++; |
295 | } |
296 | |
297 | |
298 | if (p->iph.protocol == 0) { |
299 | if (memcmp(p->name, "gre", 3) == 0) |
300 | p->iph.protocol = IPPROTO_GRE; |
301 | else if (memcmp(p->name, "ipip", 4) == 0) |
302 | p->iph.protocol = IPPROTO_IPIP; |
303 | else if (memcmp(p->name, "sit", 3) == 0) |
304 | p->iph.protocol = IPPROTO_IPV6; |
305 | } |
306 | |
307 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
308 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
309 | bb_error_msg("keys are not allowed with ipip and sit"); |
310 | return -1; |
311 | } |
312 | } |
313 | |
314 | if (medium[0]) { |
315 | p->link = do_ioctl_get_ifindex(medium); |
316 | if (p->link == 0) |
317 | return -1; |
318 | } |
319 | |
320 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
321 | p->i_key = p->iph.daddr; |
322 | p->i_flags |= GRE_KEY; |
323 | } |
324 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
325 | p->o_key = p->iph.daddr; |
326 | p->o_flags |= GRE_KEY; |
327 | } |
328 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { |
329 | bb_error_msg("broadcast tunnel requires a source address"); |
330 | return -1; |
331 | } |
332 | return 0; |
333 | } |
334 | |
335 | |
336 | static int do_add(int cmd, int argc, char **argv) |
337 | { |
338 | struct ip_tunnel_parm p; |
339 | |
340 | if (parse_args(argc, argv, cmd, &p) < 0) |
341 | return -1; |
342 | |
343 | if (p.iph.ttl && p.iph.frag_off == 0) { |
344 | bb_error_msg("ttl != 0 and noptmudisc are incompatible"); |
345 | return -1; |
346 | } |
347 | |
348 | switch (p.iph.protocol) { |
349 | case IPPROTO_IPIP: |
350 | return do_add_ioctl(cmd, "tunl0", &p); |
351 | case IPPROTO_GRE: |
352 | return do_add_ioctl(cmd, "gre0", &p); |
353 | case IPPROTO_IPV6: |
354 | return do_add_ioctl(cmd, "sit0", &p); |
355 | default: |
356 | bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)"); |
357 | return -1; |
358 | } |
359 | return -1; |
360 | } |
361 | |
362 | static int do_del(int argc, char **argv) |
363 | { |
364 | struct ip_tunnel_parm p; |
365 | |
366 | if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0) |
367 | return -1; |
368 | |
369 | switch (p.iph.protocol) { |
370 | case IPPROTO_IPIP: |
371 | return do_del_ioctl("tunl0", &p); |
372 | case IPPROTO_GRE: |
373 | return do_del_ioctl("gre0", &p); |
374 | case IPPROTO_IPV6: |
375 | return do_del_ioctl("sit0", &p); |
376 | default: |
377 | return do_del_ioctl(p.name, &p); |
378 | } |
379 | return -1; |
380 | } |
381 | |
382 | static void print_tunnel(struct ip_tunnel_parm *p) |
383 | { |
384 | char s1[256]; |
385 | char s2[256]; |
386 | char s3[64]; |
387 | char s4[64]; |
388 | |
389 | format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)); |
390 | format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)); |
391 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); |
392 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); |
393 | |
394 | printf("%s: %s/ip remote %s local %s ", |
395 | p->name, |
396 | p->iph.protocol == IPPROTO_IPIP ? "ip" : |
397 | (p->iph.protocol == IPPROTO_GRE ? "gre" : |
398 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), |
399 | p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any"); |
400 | if (p->link) { |
401 | char *n = do_ioctl_get_ifname(p->link); |
402 | if (n) |
403 | printf(" dev %s ", n); |
404 | } |
405 | if (p->iph.ttl) |
406 | printf(" ttl %d ", p->iph.ttl); |
407 | else |
408 | printf(" ttl inherit "); |
409 | if (p->iph.tos) { |
410 | SPRINT_BUF(b1); |
411 | printf(" tos"); |
412 | if (p->iph.tos & 1) |
413 | printf(" inherit"); |
414 | if (p->iph.tos & ~1) |
415 | printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', |
416 | rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1))); |
417 | } |
418 | if (!(p->iph.frag_off & htons(IP_DF))) |
419 | printf(" nopmtudisc"); |
420 | |
421 | if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key) |
422 | printf(" key %s", s3); |
423 | else if ((p->i_flags | p->o_flags) & GRE_KEY) { |
424 | if (p->i_flags & GRE_KEY) |
425 | printf(" ikey %s ", s3); |
426 | if (p->o_flags & GRE_KEY) |
427 | printf(" okey %s ", s4); |
428 | } |
429 | |
430 | if (p->i_flags & GRE_SEQ) |
431 | printf("%s Drop packets out of sequence.\n", _SL_); |
432 | if (p->i_flags & GRE_CSUM) |
433 | printf("%s Checksum in received packet is required.", _SL_); |
434 | if (p->o_flags & GRE_SEQ) |
435 | printf("%s Sequence packets on output.", _SL_); |
436 | if (p->o_flags & GRE_CSUM) |
437 | printf("%s Checksum output packets.", _SL_); |
438 | } |
439 | |
440 | static int do_tunnels_list(struct ip_tunnel_parm *p) |
441 | { |
442 | char name[IFNAMSIZ]; |
443 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
444 | rx_fifo, rx_frame, |
445 | tx_bytes, tx_packets, tx_errs, tx_drops, |
446 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
447 | int type; |
448 | struct ip_tunnel_parm p1; |
449 | char buf[512]; |
450 | FILE *fp = fopen("/proc/net/dev", "r"); |
451 | |
452 | if (fp == NULL) { |
453 | perror("fopen"); |
454 | return -1; |
455 | } |
456 | |
457 | fgets(buf, sizeof(buf), fp); |
458 | fgets(buf, sizeof(buf), fp); |
459 | |
460 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
461 | char *ptr; |
462 | |
463 | /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ |
464 | ptr = strchr(buf, ':'); |
465 | if (ptr == NULL || |
466 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
467 | bb_error_msg("wrong format of /proc/net/dev. Sorry"); |
468 | return -1; |
469 | } |
470 | if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", |
471 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
472 | &rx_fifo, &rx_frame, &rx_multi, |
473 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
474 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
475 | continue; |
476 | if (p->name[0] && strcmp(p->name, name)) |
477 | continue; |
478 | type = do_ioctl_get_iftype(name); |
479 | if (type == -1) { |
480 | bb_error_msg("failed to get type of [%s]", name); |
481 | continue; |
482 | } |
483 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) |
484 | continue; |
485 | memset(&p1, 0, sizeof(p1)); |
486 | if (do_get_ioctl(name, &p1)) |
487 | continue; |
488 | if ((p->link && p1.link != p->link) || |
489 | (p->name[0] && strcmp(p1.name, p->name)) || |
490 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || |
491 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || |
492 | (p->i_key && p1.i_key != p->i_key)) |
493 | continue; |
494 | print_tunnel(&p1); |
495 | puts(""); |
496 | } |
497 | return 0; |
498 | } |
499 | |
500 | static int do_show(int argc, char **argv) |
501 | { |
502 | int err; |
503 | struct ip_tunnel_parm p; |
504 | |
505 | if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) |
506 | return -1; |
507 | |
508 | switch (p.iph.protocol) { |
509 | case IPPROTO_IPIP: |
510 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
511 | break; |
512 | case IPPROTO_GRE: |
513 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
514 | break; |
515 | case IPPROTO_IPV6: |
516 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
517 | break; |
518 | default: |
519 | do_tunnels_list(&p); |
520 | return 0; |
521 | } |
522 | if (err) |
523 | return -1; |
524 | |
525 | print_tunnel(&p); |
526 | puts(""); |
527 | return 0; |
528 | } |
529 | |
530 | int do_iptunnel(int argc, char **argv) |
531 | { |
532 | if (argc > 0) { |
533 | if (matches(*argv, "add") == 0) |
534 | return do_add(SIOCADDTUNNEL, argc-1, argv+1); |
535 | if (matches(*argv, "change") == 0) |
536 | return do_add(SIOCCHGTUNNEL, argc-1, argv+1); |
537 | if (matches(*argv, "del") == 0) |
538 | return do_del(argc-1, argv+1); |
539 | if (matches(*argv, "show") == 0 || |
540 | matches(*argv, "lst") == 0 || |
541 | matches(*argv, "list") == 0) |
542 | return do_show(argc-1, argv+1); |
543 | } else |
544 | return do_show(0, NULL); |
545 | |
546 | bb_error_msg_and_die("command \"%s\" is unknown", *argv); |
547 | } |