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