Annotation of /trunk/mkinitrd-magellan/busybox/networking/libiproute/iptunnel.c
Parent Directory | Revision Log
Revision 1123 -
(hide annotations)
(download)
Wed Aug 18 21:56:57 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 15183 byte(s)
Wed Aug 18 21:56:57 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 15183 byte(s)
-updated to busybox-1.17.1
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 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
79 | niro | 532 | 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 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
92 | niro | 532 | 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 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
118 | niro | 532 | 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 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, p->name); |
133 | niro | 532 | } else { |
134 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
135 | niro | 532 | } |
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 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, p->name); |
159 | niro | 532 | } else { |
160 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, basedev); |
161 | niro | 532 | } |
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 | niro | 984 | medium[0] = '\0'; |
193 | niro | 532 | |
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 | niro | 1123 | key == ARG_ip_ip |
208 | ) { | ||
209 | niro | 532 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
210 | niro | 816 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
211 | niro | 532 | } |
212 | p->iph.protocol = IPPROTO_IPIP; | ||
213 | niro | 816 | } else if (key == ARG_gre || |
214 | niro | 1123 | key == ARG_gre_ip |
215 | ) { | ||
216 | niro | 532 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
217 | niro | 816 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
218 | niro | 532 | } |
219 | p->iph.protocol = IPPROTO_GRE; | ||
220 | niro | 816 | } else if (key == ARG_sit || |
221 | niro | 1123 | key == ARG_ip6_ip |
222 | ) { | ||
223 | niro | 532 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
224 | niro | 816 | bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); |
225 | niro | 532 | } |
226 | p->iph.protocol = IPPROTO_IPV6; | ||
227 | } else { | ||
228 | niro | 984 | bb_error_msg_and_die("%s tunnel mode", "can't guess"); |
229 | niro | 532 | } |
230 | niro | 816 | } else if (key == ARG_key) { |
231 | niro | 532 | unsigned uval; |
232 | NEXT_ARG(); | ||
233 | p->i_flags |= GRE_KEY; | ||
234 | p->o_flags |= GRE_KEY; | ||
235 | if (strchr(*argv, '.')) | ||
236 | p->i_key = p->o_key = get_addr32(*argv); | ||
237 | else { | ||
238 | niro | 984 | uval = get_unsigned(*argv, "key"); |
239 | niro | 532 | p->i_key = p->o_key = htonl(uval); |
240 | } | ||
241 | niro | 816 | } else if (key == ARG_ikey) { |
242 | niro | 532 | unsigned uval; |
243 | NEXT_ARG(); | ||
244 | p->i_flags |= GRE_KEY; | ||
245 | if (strchr(*argv, '.')) | ||
246 | p->o_key = get_addr32(*argv); | ||
247 | else { | ||
248 | niro | 984 | uval = get_unsigned(*argv, "ikey"); |
249 | niro | 532 | p->i_key = htonl(uval); |
250 | } | ||
251 | niro | 816 | } else if (key == ARG_okey) { |
252 | niro | 532 | unsigned uval; |
253 | NEXT_ARG(); | ||
254 | p->o_flags |= GRE_KEY; | ||
255 | if (strchr(*argv, '.')) | ||
256 | p->o_key = get_addr32(*argv); | ||
257 | else { | ||
258 | niro | 984 | uval = get_unsigned(*argv, "okey"); |
259 | niro | 532 | p->o_key = htonl(uval); |
260 | } | ||
261 | niro | 816 | } else if (key == ARG_seq) { |
262 | niro | 532 | p->i_flags |= GRE_SEQ; |
263 | p->o_flags |= GRE_SEQ; | ||
264 | niro | 816 | } else if (key == ARG_iseq) { |
265 | niro | 532 | p->i_flags |= GRE_SEQ; |
266 | niro | 816 | } else if (key == ARG_oseq) { |
267 | niro | 532 | p->o_flags |= GRE_SEQ; |
268 | niro | 816 | } else if (key == ARG_csum) { |
269 | niro | 532 | p->i_flags |= GRE_CSUM; |
270 | p->o_flags |= GRE_CSUM; | ||
271 | niro | 816 | } else if (key == ARG_icsum) { |
272 | niro | 532 | p->i_flags |= GRE_CSUM; |
273 | niro | 816 | } else if (key == ARG_ocsum) { |
274 | niro | 532 | p->o_flags |= GRE_CSUM; |
275 | niro | 816 | } else if (key == ARG_nopmtudisc) { |
276 | niro | 532 | p->iph.frag_off = 0; |
277 | niro | 816 | } else if (key == ARG_pmtudisc) { |
278 | niro | 532 | p->iph.frag_off = htons(IP_DF); |
279 | niro | 816 | } else if (key == ARG_remote) { |
280 | niro | 532 | NEXT_ARG(); |
281 | niro | 816 | key = index_in_strings(keywords, *argv); |
282 | if (key != ARG_any) | ||
283 | niro | 532 | p->iph.daddr = get_addr32(*argv); |
284 | niro | 816 | } else if (key == ARG_local) { |
285 | niro | 532 | NEXT_ARG(); |
286 | niro | 816 | key = index_in_strings(keywords, *argv); |
287 | if (key != ARG_any) | ||
288 | niro | 532 | p->iph.saddr = get_addr32(*argv); |
289 | niro | 816 | } else if (key == ARG_dev) { |
290 | niro | 532 | NEXT_ARG(); |
291 | niro | 984 | strncpy_IFNAMSIZ(medium, *argv); |
292 | niro | 816 | } else if (key == ARG_ttl) { |
293 | niro | 532 | unsigned uval; |
294 | NEXT_ARG(); | ||
295 | niro | 816 | key = index_in_strings(keywords, *argv); |
296 | if (key != ARG_inherit) { | ||
297 | niro | 984 | uval = get_unsigned(*argv, "TTL"); |
298 | niro | 532 | if (uval > 255) |
299 | invarg(*argv, "TTL must be <=255"); | ||
300 | p->iph.ttl = uval; | ||
301 | } | ||
302 | niro | 816 | } else if (key == ARG_tos || |
303 | niro | 1123 | key == ARG_dsfield |
304 | ) { | ||
305 | niro | 532 | uint32_t uval; |
306 | NEXT_ARG(); | ||
307 | niro | 816 | key = index_in_strings(keywords, *argv); |
308 | if (key != ARG_inherit) { | ||
309 | niro | 532 | if (rtnl_dsfield_a2n(&uval, *argv)) |
310 | invarg(*argv, "TOS"); | ||
311 | p->iph.tos = uval; | ||
312 | } else | ||
313 | p->iph.tos = 1; | ||
314 | } else { | ||
315 | niro | 816 | if (key == ARG_name) { |
316 | niro | 532 | NEXT_ARG(); |
317 | } | ||
318 | if (p->name[0]) | ||
319 | duparg2("name", *argv); | ||
320 | niro | 984 | strncpy_IFNAMSIZ(p->name, *argv); |
321 | niro | 532 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
322 | struct ip_tunnel_parm old_p; | ||
323 | memset(&old_p, 0, sizeof(old_p)); | ||
324 | if (do_get_ioctl(*argv, &old_p)) | ||
325 | niro | 816 | exit(EXIT_FAILURE); |
326 | niro | 532 | *p = old_p; |
327 | } | ||
328 | } | ||
329 | count++; | ||
330 | niro | 816 | argv++; |
331 | niro | 532 | } |
332 | |||
333 | if (p->iph.protocol == 0) { | ||
334 | if (memcmp(p->name, "gre", 3) == 0) | ||
335 | p->iph.protocol = IPPROTO_GRE; | ||
336 | else if (memcmp(p->name, "ipip", 4) == 0) | ||
337 | p->iph.protocol = IPPROTO_IPIP; | ||
338 | else if (memcmp(p->name, "sit", 3) == 0) | ||
339 | p->iph.protocol = IPPROTO_IPV6; | ||
340 | } | ||
341 | |||
342 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { | ||
343 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { | ||
344 | niro | 816 | bb_error_msg_and_die("keys are not allowed with ipip and sit"); |
345 | niro | 532 | } |
346 | } | ||
347 | |||
348 | if (medium[0]) { | ||
349 | p->link = do_ioctl_get_ifindex(medium); | ||
350 | } | ||
351 | |||
352 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { | ||
353 | p->i_key = p->iph.daddr; | ||
354 | p->i_flags |= GRE_KEY; | ||
355 | } | ||
356 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { | ||
357 | p->o_key = p->iph.daddr; | ||
358 | p->o_flags |= GRE_KEY; | ||
359 | } | ||
360 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { | ||
361 | niro | 816 | bb_error_msg_and_die("broadcast tunnel requires a source address"); |
362 | niro | 532 | } |
363 | } | ||
364 | |||
365 | niro | 816 | /* Return value becomes exitcode. It's okay to not return at all */ |
366 | static int do_add(int cmd, char **argv) | ||
367 | niro | 532 | { |
368 | struct ip_tunnel_parm p; | ||
369 | |||
370 | niro | 816 | parse_args(argv, cmd, &p); |
371 | niro | 532 | |
372 | if (p.iph.ttl && p.iph.frag_off == 0) { | ||
373 | niro | 816 | bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible"); |
374 | niro | 532 | } |
375 | |||
376 | switch (p.iph.protocol) { | ||
377 | case IPPROTO_IPIP: | ||
378 | return do_add_ioctl(cmd, "tunl0", &p); | ||
379 | case IPPROTO_GRE: | ||
380 | return do_add_ioctl(cmd, "gre0", &p); | ||
381 | case IPPROTO_IPV6: | ||
382 | return do_add_ioctl(cmd, "sit0", &p); | ||
383 | default: | ||
384 | niro | 984 | bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)"); |
385 | niro | 532 | } |
386 | } | ||
387 | |||
388 | niro | 816 | /* Return value becomes exitcode. It's okay to not return at all */ |
389 | static int do_del(char **argv) | ||
390 | niro | 532 | { |
391 | struct ip_tunnel_parm p; | ||
392 | |||
393 | niro | 816 | parse_args(argv, SIOCDELTUNNEL, &p); |
394 | niro | 532 | |
395 | switch (p.iph.protocol) { | ||
396 | case IPPROTO_IPIP: | ||
397 | return do_del_ioctl("tunl0", &p); | ||
398 | case IPPROTO_GRE: | ||
399 | return do_del_ioctl("gre0", &p); | ||
400 | case IPPROTO_IPV6: | ||
401 | return do_del_ioctl("sit0", &p); | ||
402 | default: | ||
403 | return do_del_ioctl(p.name, &p); | ||
404 | } | ||
405 | } | ||
406 | |||
407 | static void print_tunnel(struct ip_tunnel_parm *p) | ||
408 | { | ||
409 | char s1[256]; | ||
410 | char s2[256]; | ||
411 | char s3[64]; | ||
412 | char s4[64]; | ||
413 | |||
414 | format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)); | ||
415 | format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)); | ||
416 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); | ||
417 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); | ||
418 | |||
419 | printf("%s: %s/ip remote %s local %s ", | ||
420 | p->name, | ||
421 | p->iph.protocol == IPPROTO_IPIP ? "ip" : | ||
422 | (p->iph.protocol == IPPROTO_GRE ? "gre" : | ||
423 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), | ||
424 | p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any"); | ||
425 | if (p->link) { | ||
426 | char *n = do_ioctl_get_ifname(p->link); | ||
427 | niro | 816 | if (n) { |
428 | niro | 532 | printf(" dev %s ", n); |
429 | niro | 816 | free(n); |
430 | } | ||
431 | niro | 532 | } |
432 | if (p->iph.ttl) | ||
433 | printf(" ttl %d ", p->iph.ttl); | ||
434 | else | ||
435 | printf(" ttl inherit "); | ||
436 | if (p->iph.tos) { | ||
437 | SPRINT_BUF(b1); | ||
438 | printf(" tos"); | ||
439 | if (p->iph.tos & 1) | ||
440 | printf(" inherit"); | ||
441 | if (p->iph.tos & ~1) | ||
442 | printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', | ||
443 | niro | 984 | rtnl_dsfield_n2a(p->iph.tos & ~1, b1)); |
444 | niro | 532 | } |
445 | if (!(p->iph.frag_off & htons(IP_DF))) | ||
446 | printf(" nopmtudisc"); | ||
447 | |||
448 | if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key) | ||
449 | printf(" key %s", s3); | ||
450 | else if ((p->i_flags | p->o_flags) & GRE_KEY) { | ||
451 | if (p->i_flags & GRE_KEY) | ||
452 | printf(" ikey %s ", s3); | ||
453 | if (p->o_flags & GRE_KEY) | ||
454 | printf(" okey %s ", s4); | ||
455 | } | ||
456 | |||
457 | if (p->i_flags & GRE_SEQ) | ||
458 | niro | 816 | printf("%c Drop packets out of sequence.\n", _SL_); |
459 | niro | 532 | if (p->i_flags & GRE_CSUM) |
460 | niro | 816 | printf("%c Checksum in received packet is required.", _SL_); |
461 | niro | 532 | if (p->o_flags & GRE_SEQ) |
462 | niro | 816 | printf("%c Sequence packets on output.", _SL_); |
463 | niro | 532 | if (p->o_flags & GRE_CSUM) |
464 | niro | 816 | printf("%c Checksum output packets.", _SL_); |
465 | niro | 532 | } |
466 | |||
467 | niro | 816 | static void do_tunnels_list(struct ip_tunnel_parm *p) |
468 | niro | 532 | { |
469 | char name[IFNAMSIZ]; | ||
470 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, | ||
471 | rx_fifo, rx_frame, | ||
472 | tx_bytes, tx_packets, tx_errs, tx_drops, | ||
473 | tx_fifo, tx_colls, tx_carrier, rx_multi; | ||
474 | int type; | ||
475 | struct ip_tunnel_parm p1; | ||
476 | char buf[512]; | ||
477 | niro | 816 | FILE *fp = fopen_or_warn("/proc/net/dev", "r"); |
478 | niro | 532 | |
479 | if (fp == NULL) { | ||
480 | niro | 816 | return; |
481 | niro | 532 | } |
482 | niro | 816 | /* skip headers */ |
483 | niro | 532 | fgets(buf, sizeof(buf), fp); |
484 | fgets(buf, sizeof(buf), fp); | ||
485 | |||
486 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
487 | char *ptr; | ||
488 | |||
489 | /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ | ||
490 | ptr = strchr(buf, ':'); | ||
491 | if (ptr == NULL || | ||
492 | niro | 1123 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1) |
493 | ) { | ||
494 | niro | 816 | bb_error_msg("wrong format of /proc/net/dev"); |
495 | return; | ||
496 | niro | 532 | } |
497 | if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", | ||
498 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, | ||
499 | &rx_fifo, &rx_frame, &rx_multi, | ||
500 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, | ||
501 | &tx_fifo, &tx_colls, &tx_carrier) != 14) | ||
502 | continue; | ||
503 | if (p->name[0] && strcmp(p->name, name)) | ||
504 | continue; | ||
505 | type = do_ioctl_get_iftype(name); | ||
506 | if (type == -1) { | ||
507 | niro | 984 | bb_error_msg("can't get type of [%s]", name); |
508 | niro | 532 | continue; |
509 | } | ||
510 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) | ||
511 | continue; | ||
512 | memset(&p1, 0, sizeof(p1)); | ||
513 | if (do_get_ioctl(name, &p1)) | ||
514 | continue; | ||
515 | if ((p->link && p1.link != p->link) || | ||
516 | (p->name[0] && strcmp(p1.name, p->name)) || | ||
517 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || | ||
518 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || | ||
519 | niro | 1123 | (p->i_key && p1.i_key != p->i_key) |
520 | ) { | ||
521 | niro | 532 | continue; |
522 | niro | 1123 | } |
523 | niro | 532 | print_tunnel(&p1); |
524 | niro | 816 | bb_putchar('\n'); |
525 | niro | 532 | } |
526 | } | ||
527 | |||
528 | niro | 816 | /* Return value becomes exitcode. It's okay to not return at all */ |
529 | static int do_show(char **argv) | ||
530 | niro | 532 | { |
531 | int err; | ||
532 | struct ip_tunnel_parm p; | ||
533 | |||
534 | niro | 816 | parse_args(argv, SIOCGETTUNNEL, &p); |
535 | niro | 532 | |
536 | switch (p.iph.protocol) { | ||
537 | case IPPROTO_IPIP: | ||
538 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); | ||
539 | break; | ||
540 | case IPPROTO_GRE: | ||
541 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); | ||
542 | break; | ||
543 | case IPPROTO_IPV6: | ||
544 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); | ||
545 | break; | ||
546 | default: | ||
547 | do_tunnels_list(&p); | ||
548 | return 0; | ||
549 | } | ||
550 | if (err) | ||
551 | return -1; | ||
552 | |||
553 | print_tunnel(&p); | ||
554 | niro | 816 | bb_putchar('\n'); |
555 | niro | 532 | return 0; |
556 | } | ||
557 | |||
558 | niro | 816 | /* Return value becomes exitcode. It's okay to not return at all */ |
559 | int do_iptunnel(char **argv) | ||
560 | niro | 532 | { |
561 | niro | 816 | static const char keywords[] ALIGN1 = |
562 | "add\0""change\0""delete\0""show\0""list\0""lst\0"; | ||
563 | enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst }; | ||
564 | niro | 532 | |
565 | niro | 816 | if (*argv) { |
566 | niro | 1123 | smalluint key = index_in_substrings(keywords, *argv); |
567 | if (key > 5) | ||
568 | niro | 816 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
569 | argv++; | ||
570 | if (key == ARG_add) | ||
571 | return do_add(SIOCADDTUNNEL, argv); | ||
572 | if (key == ARG_change) | ||
573 | return do_add(SIOCCHGTUNNEL, argv); | ||
574 | if (key == ARG_del) | ||
575 | return do_del(argv); | ||
576 | } | ||
577 | return do_show(argv); | ||
578 | niro | 532 | } |