Annotation of /trunk/mkinitrd-magellan/busybox/networking/arping.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: 10782 byte(s)
Wed Aug 18 21:56:57 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 10782 byte(s)
-updated to busybox-1.17.1
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * arping.c - Ping hosts by ARP requests/replies | ||
4 | * | ||
5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
6 | * | ||
7 | * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | ||
8 | * Busybox port: Nick Fedchik <nick@fedchik.org.ua> | ||
9 | */ | ||
10 | |||
11 | #include <arpa/inet.h> | ||
12 | #include <net/if.h> | ||
13 | #include <netinet/ether.h> | ||
14 | #include <netpacket/packet.h> | ||
15 | |||
16 | niro | 816 | #include "libbb.h" |
17 | niro | 532 | |
18 | niro | 816 | /* We don't expect to see 1000+ seconds delay, unsigned is enough */ |
19 | #define MONOTONIC_US() ((unsigned)monotonic_us()) | ||
20 | niro | 532 | |
21 | niro | 816 | enum { |
22 | DAD = 1, | ||
23 | UNSOLICITED = 2, | ||
24 | ADVERT = 4, | ||
25 | QUIET = 8, | ||
26 | QUIT_ON_REPLY = 16, | ||
27 | BCAST_ONLY = 32, | ||
28 | UNICASTING = 64 | ||
29 | niro | 532 | }; |
30 | |||
31 | niro | 816 | struct globals { |
32 | struct in_addr src; | ||
33 | struct in_addr dst; | ||
34 | struct sockaddr_ll me; | ||
35 | struct sockaddr_ll he; | ||
36 | int sock_fd; | ||
37 | niro | 532 | |
38 | niro | 816 | int count; // = -1; |
39 | unsigned last; | ||
40 | unsigned timeout_us; | ||
41 | unsigned start; | ||
42 | niro | 532 | |
43 | niro | 816 | unsigned sent; |
44 | unsigned brd_sent; | ||
45 | unsigned received; | ||
46 | unsigned brd_recv; | ||
47 | unsigned req_recv; | ||
48 | niro | 1123 | } FIX_ALIASING; |
49 | niro | 816 | #define G (*(struct globals*)&bb_common_bufsiz1) |
50 | #define src (G.src ) | ||
51 | #define dst (G.dst ) | ||
52 | #define me (G.me ) | ||
53 | #define he (G.he ) | ||
54 | #define sock_fd (G.sock_fd ) | ||
55 | #define count (G.count ) | ||
56 | #define last (G.last ) | ||
57 | #define timeout_us (G.timeout_us) | ||
58 | #define start (G.start ) | ||
59 | #define sent (G.sent ) | ||
60 | #define brd_sent (G.brd_sent ) | ||
61 | #define received (G.received ) | ||
62 | #define brd_recv (G.brd_recv ) | ||
63 | #define req_recv (G.req_recv ) | ||
64 | #define INIT_G() do { \ | ||
65 | count = -1; \ | ||
66 | } while (0) | ||
67 | |||
68 | // If GNUisms are not available... | ||
69 | //static void *mempcpy(void *_dst, const void *_src, int n) | ||
70 | //{ | ||
71 | // memcpy(_dst, _src, n); | ||
72 | // return (char*)_dst + n; | ||
73 | //} | ||
74 | |||
75 | static int send_pack(struct in_addr *src_addr, | ||
76 | niro | 532 | struct in_addr *dst_addr, struct sockaddr_ll *ME, |
77 | struct sockaddr_ll *HE) | ||
78 | { | ||
79 | int err; | ||
80 | niro | 816 | unsigned char buf[256]; |
81 | niro | 532 | struct arphdr *ah = (struct arphdr *) buf; |
82 | unsigned char *p = (unsigned char *) (ah + 1); | ||
83 | |||
84 | ah->ar_hrd = htons(ARPHRD_ETHER); | ||
85 | ah->ar_pro = htons(ETH_P_IP); | ||
86 | ah->ar_hln = ME->sll_halen; | ||
87 | ah->ar_pln = 4; | ||
88 | niro | 816 | ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST); |
89 | niro | 532 | |
90 | niro | 816 | p = mempcpy(p, &ME->sll_addr, ah->ar_hln); |
91 | p = mempcpy(p, src_addr, 4); | ||
92 | niro | 532 | |
93 | niro | 816 | if (option_mask32 & ADVERT) |
94 | p = mempcpy(p, &ME->sll_addr, ah->ar_hln); | ||
95 | niro | 532 | else |
96 | niro | 816 | p = mempcpy(p, &HE->sll_addr, ah->ar_hln); |
97 | niro | 532 | |
98 | niro | 816 | p = mempcpy(p, dst_addr, 4); |
99 | niro | 532 | |
100 | niro | 816 | err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE)); |
101 | niro | 532 | if (err == p - buf) { |
102 | niro | 816 | last = MONOTONIC_US(); |
103 | niro | 532 | sent++; |
104 | niro | 816 | if (!(option_mask32 & UNICASTING)) |
105 | niro | 532 | brd_sent++; |
106 | } | ||
107 | return err; | ||
108 | } | ||
109 | |||
110 | niro | 816 | static void finish(void) NORETURN; |
111 | niro | 532 | static void finish(void) |
112 | { | ||
113 | niro | 816 | if (!(option_mask32 & QUIET)) { |
114 | printf("Sent %u probe(s) (%u broadcast(s))\n" | ||
115 | "Received %u repl%s" | ||
116 | " (%u request(s), %u broadcast(s))\n", | ||
117 | niro | 532 | sent, brd_sent, |
118 | received, (received == 1) ? "ies" : "y", | ||
119 | req_recv, brd_recv); | ||
120 | } | ||
121 | niro | 816 | if (option_mask32 & DAD) |
122 | niro | 532 | exit(!!received); |
123 | niro | 816 | if (option_mask32 & UNSOLICITED) |
124 | exit(EXIT_SUCCESS); | ||
125 | niro | 532 | exit(!received); |
126 | } | ||
127 | |||
128 | static void catcher(void) | ||
129 | { | ||
130 | niro | 816 | unsigned now; |
131 | niro | 532 | |
132 | niro | 816 | now = MONOTONIC_US(); |
133 | if (start == 0) | ||
134 | start = now; | ||
135 | niro | 532 | |
136 | niro | 816 | if (count == 0 || (timeout_us && (now - start) > timeout_us)) |
137 | niro | 532 | finish(); |
138 | |||
139 | niro | 816 | /* count < 0 means "infinite count" */ |
140 | if (count > 0) | ||
141 | count--; | ||
142 | |||
143 | if (last == 0 || (now - last) > 500000) { | ||
144 | send_pack(&src, &dst, &me, &he); | ||
145 | if (count == 0 && (option_mask32 & UNSOLICITED)) | ||
146 | niro | 532 | finish(); |
147 | } | ||
148 | alarm(1); | ||
149 | } | ||
150 | |||
151 | niro | 816 | static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM) |
152 | niro | 532 | { |
153 | struct arphdr *ah = (struct arphdr *) buf; | ||
154 | unsigned char *p = (unsigned char *) (ah + 1); | ||
155 | struct in_addr src_ip, dst_ip; | ||
156 | niro | 984 | /* moves below assume in_addr is 4 bytes big, ensure that */ |
157 | struct BUG_in_addr_must_be_4 { | ||
158 | char BUG_in_addr_must_be_4[ | ||
159 | sizeof(struct in_addr) == 4 ? 1 : -1 | ||
160 | ]; | ||
161 | char BUG_s_addr_must_be_4[ | ||
162 | sizeof(src_ip.s_addr) == 4 ? 1 : -1 | ||
163 | ]; | ||
164 | }; | ||
165 | niro | 532 | |
166 | /* Filter out wild packets */ | ||
167 | if (FROM->sll_pkttype != PACKET_HOST | ||
168 | && FROM->sll_pkttype != PACKET_BROADCAST | ||
169 | && FROM->sll_pkttype != PACKET_MULTICAST) | ||
170 | niro | 816 | return false; |
171 | niro | 532 | |
172 | niro | 984 | /* Only these types are recognized */ |
173 | niro | 532 | if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY)) |
174 | niro | 816 | return false; |
175 | niro | 532 | |
176 | /* ARPHRD check and this darned FDDI hack here :-( */ | ||
177 | if (ah->ar_hrd != htons(FROM->sll_hatype) | ||
178 | && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER))) | ||
179 | niro | 816 | return false; |
180 | niro | 532 | |
181 | /* Protocol must be IP. */ | ||
182 | niro | 816 | if (ah->ar_pro != htons(ETH_P_IP) |
183 | niro | 984 | || (ah->ar_pln != 4) |
184 | || (ah->ar_hln != me.sll_halen) | ||
185 | || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln)))) | ||
186 | niro | 816 | return false; |
187 | |||
188 | niro | 984 | move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln); |
189 | move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln); | ||
190 | niro | 816 | |
191 | if (dst.s_addr != src_ip.s_addr) | ||
192 | return false; | ||
193 | if (!(option_mask32 & DAD)) { | ||
194 | if ((src.s_addr != dst_ip.s_addr) | ||
195 | || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))) | ||
196 | return false; | ||
197 | niro | 532 | } else { |
198 | /* DAD packet was: | ||
199 | src_ip = 0 (or some src) | ||
200 | src_hw = ME | ||
201 | dst_ip = tested address | ||
202 | dst_hw = <unspec> | ||
203 | |||
204 | We fail, if receive request/reply with: | ||
205 | src_ip = tested_address | ||
206 | src_hw != ME | ||
207 | if src_ip in request was not zero, check | ||
208 | also that it matches to dst_ip, otherwise | ||
209 | dst_ip/dst_hw do not matter. | ||
210 | */ | ||
211 | niro | 816 | if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0) |
212 | niro | 984 | || (src.s_addr && src.s_addr != dst_ip.s_addr)) |
213 | niro | 816 | return false; |
214 | niro | 532 | } |
215 | niro | 816 | if (!(option_mask32 & QUIET)) { |
216 | niro | 532 | int s_printed = 0; |
217 | |||
218 | printf("%scast re%s from %s [%s]", | ||
219 | FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad", | ||
220 | ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest", | ||
221 | inet_ntoa(src_ip), | ||
222 | ether_ntoa((struct ether_addr *) p)); | ||
223 | if (dst_ip.s_addr != src.s_addr) { | ||
224 | printf("for %s ", inet_ntoa(dst_ip)); | ||
225 | s_printed = 1; | ||
226 | } | ||
227 | if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) { | ||
228 | if (!s_printed) | ||
229 | printf("for "); | ||
230 | printf("[%s]", | ||
231 | ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4)); | ||
232 | } | ||
233 | |||
234 | niro | 816 | if (last) { |
235 | unsigned diff = MONOTONIC_US() - last; | ||
236 | printf(" %u.%03ums\n", diff / 1000, diff % 1000); | ||
237 | niro | 532 | } else { |
238 | printf(" UNSOLICITED?\n"); | ||
239 | } | ||
240 | niro | 984 | fflush_all(); |
241 | niro | 532 | } |
242 | received++; | ||
243 | if (FROM->sll_pkttype != PACKET_HOST) | ||
244 | brd_recv++; | ||
245 | if (ah->ar_op == htons(ARPOP_REQUEST)) | ||
246 | req_recv++; | ||
247 | niro | 816 | if (option_mask32 & QUIT_ON_REPLY) |
248 | niro | 532 | finish(); |
249 | niro | 816 | if (!(option_mask32 & BCAST_ONLY)) { |
250 | niro | 532 | memcpy(he.sll_addr, p, me.sll_halen); |
251 | niro | 816 | option_mask32 |= UNICASTING; |
252 | niro | 532 | } |
253 | niro | 816 | return true; |
254 | niro | 532 | } |
255 | |||
256 | niro | 816 | int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
257 | int arping_main(int argc UNUSED_PARAM, char **argv) | ||
258 | niro | 532 | { |
259 | niro | 816 | const char *device = "eth0"; |
260 | niro | 532 | char *source = NULL; |
261 | char *target; | ||
262 | niro | 816 | unsigned char *packet; |
263 | char *err_str; | ||
264 | niro | 532 | |
265 | niro | 816 | INIT_G(); |
266 | niro | 532 | |
267 | niro | 816 | sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0); |
268 | |||
269 | niro | 532 | // Drop suid root privileges |
270 | niro | 816 | // Need to remove SUID_NEVER from applets.h for this to work |
271 | //xsetuid(getuid()); | ||
272 | niro | 532 | |
273 | niro | 816 | err_str = xasprintf("interface %s %%s", device); |
274 | niro | 532 | { |
275 | unsigned opt; | ||
276 | niro | 816 | char *str_timeout; |
277 | niro | 532 | |
278 | /* Dad also sets quit_on_reply. | ||
279 | * Advert also sets unsolicited. | ||
280 | */ | ||
281 | niro | 816 | opt_complementary = "=1:Df:AU:c+"; |
282 | opt = getopt32(argv, "DUAqfbc:w:I:s:", | ||
283 | &count, &str_timeout, &device, &source); | ||
284 | niro | 532 | if (opt & 0x80) /* -w: timeout */ |
285 | niro | 816 | timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000; |
286 | niro | 532 | //if (opt & 0x200) /* -s: source */ |
287 | niro | 816 | option_mask32 &= 0x3f; /* set respective flags */ |
288 | niro | 532 | } |
289 | |||
290 | niro | 816 | target = argv[optind]; |
291 | niro | 532 | |
292 | xfunc_error_retval = 2; | ||
293 | |||
294 | { | ||
295 | struct ifreq ifr; | ||
296 | |||
297 | memset(&ifr, 0, sizeof(ifr)); | ||
298 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, device); |
299 | niro | 816 | /* We use ifr.ifr_name in error msg so that problem |
300 | * with truncated name will be visible */ | ||
301 | ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found"); | ||
302 | me.sll_ifindex = ifr.ifr_ifindex; | ||
303 | niro | 532 | |
304 | niro | 816 | xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr); |
305 | |||
306 | niro | 532 | if (!(ifr.ifr_flags & IFF_UP)) { |
307 | niro | 816 | bb_error_msg_and_die(err_str, "is down"); |
308 | niro | 532 | } |
309 | if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { | ||
310 | niro | 816 | bb_error_msg(err_str, "is not ARPable"); |
311 | return (option_mask32 & DAD ? 0 : 2); | ||
312 | niro | 532 | } |
313 | } | ||
314 | |||
315 | niro | 816 | /* if (!inet_aton(target, &dst)) - not needed */ { |
316 | len_and_sockaddr *lsa; | ||
317 | lsa = xhost_and_af2sockaddr(target, 0, AF_INET); | ||
318 | niro | 984 | dst = lsa->u.sin.sin_addr; |
319 | niro | 816 | if (ENABLE_FEATURE_CLEAN_UP) |
320 | free(lsa); | ||
321 | niro | 532 | } |
322 | |||
323 | if (source && !inet_aton(source, &src)) { | ||
324 | bb_error_msg_and_die("invalid source address %s", source); | ||
325 | } | ||
326 | |||
327 | niro | 816 | if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0) |
328 | niro | 532 | src = dst; |
329 | |||
330 | niro | 816 | if (!(option_mask32 & DAD) || src.s_addr) { |
331 | niro | 532 | struct sockaddr_in saddr; |
332 | int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); | ||
333 | |||
334 | niro | 816 | setsockopt_bindtodevice(probe_fd, device); |
335 | niro | 532 | memset(&saddr, 0, sizeof(saddr)); |
336 | saddr.sin_family = AF_INET; | ||
337 | if (src.s_addr) { | ||
338 | niro | 816 | /* Check that this is indeed our IP */ |
339 | niro | 532 | saddr.sin_addr = src; |
340 | xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); | ||
341 | niro | 816 | } else { /* !(option_mask32 & DAD) case */ |
342 | /* Find IP address on this iface */ | ||
343 | niro | 532 | socklen_t alen = sizeof(saddr); |
344 | |||
345 | saddr.sin_port = htons(1025); | ||
346 | saddr.sin_addr = dst; | ||
347 | |||
348 | niro | 816 | if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1) |
349 | bb_perror_msg("setsockopt(SO_DONTROUTE)"); | ||
350 | niro | 532 | xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); |
351 | niro | 984 | getsockname(probe_fd, (struct sockaddr *) &saddr, &alen); |
352 | //never happens: | ||
353 | //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) | ||
354 | // bb_perror_msg_and_die("getsockname"); | ||
355 | niro | 816 | if (saddr.sin_family != AF_INET) |
356 | bb_error_msg_and_die("no IP address configured"); | ||
357 | niro | 532 | src = saddr.sin_addr; |
358 | } | ||
359 | close(probe_fd); | ||
360 | } | ||
361 | |||
362 | me.sll_family = AF_PACKET; | ||
363 | niro | 816 | //me.sll_ifindex = ifindex; - done before |
364 | niro | 532 | me.sll_protocol = htons(ETH_P_ARP); |
365 | niro | 816 | xbind(sock_fd, (struct sockaddr *) &me, sizeof(me)); |
366 | niro | 532 | |
367 | { | ||
368 | socklen_t alen = sizeof(me); | ||
369 | niro | 984 | getsockname(sock_fd, (struct sockaddr *) &me, &alen); |
370 | //never happens: | ||
371 | //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) | ||
372 | // bb_perror_msg_and_die("getsockname"); | ||
373 | niro | 532 | } |
374 | if (me.sll_halen == 0) { | ||
375 | niro | 816 | bb_error_msg(err_str, "is not ARPable (no ll address)"); |
376 | return (option_mask32 & DAD ? 0 : 2); | ||
377 | niro | 532 | } |
378 | he = me; | ||
379 | memset(he.sll_addr, -1, he.sll_halen); | ||
380 | |||
381 | niro | 816 | if (!(option_mask32 & QUIET)) { |
382 | /* inet_ntoa uses static storage, can't use in same printf */ | ||
383 | printf("ARPING to %s", inet_ntoa(dst)); | ||
384 | printf(" from %s via %s\n", inet_ntoa(src), device); | ||
385 | niro | 532 | } |
386 | |||
387 | niro | 816 | signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish); |
388 | signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher); | ||
389 | niro | 532 | |
390 | catcher(); | ||
391 | |||
392 | niro | 816 | packet = xmalloc(4096); |
393 | niro | 532 | while (1) { |
394 | sigset_t sset, osset; | ||
395 | struct sockaddr_ll from; | ||
396 | socklen_t alen = sizeof(from); | ||
397 | int cc; | ||
398 | |||
399 | niro | 816 | cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen); |
400 | niro | 532 | if (cc < 0) { |
401 | bb_perror_msg("recvfrom"); | ||
402 | continue; | ||
403 | } | ||
404 | sigemptyset(&sset); | ||
405 | sigaddset(&sset, SIGALRM); | ||
406 | sigaddset(&sset, SIGINT); | ||
407 | sigprocmask(SIG_BLOCK, &sset, &osset); | ||
408 | recv_pack(packet, cc, &from); | ||
409 | sigprocmask(SIG_SETMASK, &osset, NULL); | ||
410 | } | ||
411 | } |