source: MondoRescue/trunk/mindi-busybox/networking/arping.c@ 904

Last change on this file since 904 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 10.6 KB
Line 
1/* 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 <sys/ioctl.h>
12#include <signal.h>
13
14#include <errno.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18
19#include <arpa/inet.h>
20#include <net/if.h>
21#include <netinet/ether.h>
22#include <netpacket/packet.h>
23
24#include "busybox.h"
25
26static struct in_addr src;
27static struct in_addr dst;
28static struct sockaddr_ll me;
29static struct sockaddr_ll he;
30static struct timeval last;
31
32enum cfg_e {
33 dad = 1,
34 unsolicited = 2,
35 advert = 4,
36 quiet = 8,
37 quit_on_reply = 16,
38 broadcast_only = 32,
39 unicasting = 64
40};
41static int cfg;
42
43static int s;
44static int count = -1;
45static int timeout;
46static int sent;
47static int brd_sent;
48static int received;
49static int brd_recv;
50static int req_recv;
51
52
53#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
54 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
55#if 0
56static void set_signal(int signo, void (*handler) (void))
57{
58 struct sigaction sa;
59
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = (void (*)(int)) handler;
62 sa.sa_flags = SA_RESTART;
63 sigaction(signo, &sa, NULL);
64}
65#endif
66
67static int send_pack(int sock, struct in_addr *src_addr,
68 struct in_addr *dst_addr, struct sockaddr_ll *ME,
69 struct sockaddr_ll *HE)
70{
71 int err;
72 struct timeval now;
73 RESERVE_CONFIG_UBUFFER(buf, 256);
74 struct arphdr *ah = (struct arphdr *) buf;
75 unsigned char *p = (unsigned char *) (ah + 1);
76
77 ah->ar_hrd = htons(ME->sll_hatype);
78 ah->ar_hrd = htons(ARPHRD_ETHER);
79 ah->ar_pro = htons(ETH_P_IP);
80 ah->ar_hln = ME->sll_halen;
81 ah->ar_pln = 4;
82 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
83
84 memcpy(p, &ME->sll_addr, ah->ar_hln);
85 p += ME->sll_halen;
86
87 memcpy(p, src_addr, 4);
88 p += 4;
89
90 if (cfg&advert)
91 memcpy(p, &ME->sll_addr, ah->ar_hln);
92 else
93 memcpy(p, &HE->sll_addr, ah->ar_hln);
94 p += ah->ar_hln;
95
96 memcpy(p, dst_addr, 4);
97 p += 4;
98
99 gettimeofday(&now, NULL);
100 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
101 if (err == p - buf) {
102 last = now;
103 sent++;
104 if (!(cfg&unicasting))
105 brd_sent++;
106 }
107 RELEASE_CONFIG_BUFFER(buf);
108 return err;
109}
110
111static void finish(void)
112{
113 if (!(cfg&quiet)) {
114 printf("Sent %d probes (%d broadcast(s))\n"
115 "Received %d repl%s",
116 sent, brd_sent,
117 received, (received > 1) ? "ies" : "y");
118 if (brd_recv || req_recv) {
119 printf(" (");
120 if (req_recv)
121 printf("%d request(s)", req_recv);
122 if (brd_recv)
123 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
124 putchar(')');
125 }
126 putchar('\n');
127 fflush(stdout);
128 }
129 if (cfg&dad)
130 exit(!!received);
131 if (cfg&unsolicited)
132 exit(0);
133 exit(!received);
134}
135
136static void catcher(void)
137{
138 struct timeval tv;
139 static struct timeval start;
140
141 gettimeofday(&tv, NULL);
142
143 if (start.tv_sec == 0)
144 start = tv;
145
146 if (count-- == 0
147 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
148 finish();
149
150 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
151 send_pack(s, &src, &dst, &me, &he);
152 if (count == 0 && cfg&unsolicited)
153 finish();
154 }
155 alarm(1);
156}
157
158static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
159{
160 struct arphdr *ah = (struct arphdr *) buf;
161 unsigned char *p = (unsigned char *) (ah + 1);
162 struct in_addr src_ip, dst_ip;
163
164 /* Filter out wild packets */
165 if (FROM->sll_pkttype != PACKET_HOST &&
166 FROM->sll_pkttype != PACKET_BROADCAST &&
167 FROM->sll_pkttype != PACKET_MULTICAST)
168 return 0;
169
170 /* Only these types are recognised */
171 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
172 return 0;
173
174 /* ARPHRD check and this darned FDDI hack here :-( */
175 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
176 (FROM->sll_hatype != ARPHRD_FDDI
177 || ah->ar_hrd != htons(ARPHRD_ETHER)))
178 return 0;
179
180 /* Protocol must be IP. */
181 if (ah->ar_pro != htons(ETH_P_IP))
182 return 0;
183 if (ah->ar_pln != 4)
184 return 0;
185 if (ah->ar_hln != me.sll_halen)
186 return 0;
187 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
188 return 0;
189 memcpy(&src_ip, p + ah->ar_hln, 4);
190 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
191 if (!(cfg&dad)) {
192 if (src_ip.s_addr != dst.s_addr)
193 return 0;
194 if (src.s_addr != dst_ip.s_addr)
195 return 0;
196 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
197 return 0;
198 } else {
199 /* DAD packet was:
200 src_ip = 0 (or some src)
201 src_hw = ME
202 dst_ip = tested address
203 dst_hw = <unspec>
204
205 We fail, if receive request/reply with:
206 src_ip = tested_address
207 src_hw != ME
208 if src_ip in request was not zero, check
209 also that it matches to dst_ip, otherwise
210 dst_ip/dst_hw do not matter.
211 */
212 if (src_ip.s_addr != dst.s_addr)
213 return 0;
214 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
215 return 0;
216 if (src.s_addr && src.s_addr != dst_ip.s_addr)
217 return 0;
218 }
219 if (!(cfg&quiet)) {
220 int s_printed = 0;
221 struct timeval tv;
222
223 gettimeofday(&tv, NULL);
224
225 printf("%s %s from %s [%s]",
226 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
227 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
228 inet_ntoa(src_ip),
229 ether_ntoa((struct ether_addr *) p));
230 if (dst_ip.s_addr != src.s_addr) {
231 printf("for %s ", inet_ntoa(dst_ip));
232 s_printed = 1;
233 }
234 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
235 if (!s_printed)
236 printf("for ");
237 printf("[%s]",
238 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
239 }
240
241 if (last.tv_sec) {
242 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
243 tv.tv_usec - last.tv_usec;
244 long msecs = (usecs + 500) / 1000;
245
246 usecs -= msecs * 1000 - 500;
247 printf(" %ld.%03ldms\n", msecs, usecs);
248 } else {
249 printf(" UNSOLICITED?\n");
250 }
251 fflush(stdout);
252 }
253 received++;
254 if (FROM->sll_pkttype != PACKET_HOST)
255 brd_recv++;
256 if (ah->ar_op == htons(ARPOP_REQUEST))
257 req_recv++;
258 if (cfg&quit_on_reply)
259 finish();
260 if (!(cfg&broadcast_only)) {
261 memcpy(he.sll_addr, p, me.sll_halen);
262 cfg |= unicasting;
263 }
264 return 1;
265}
266
267int arping_main(int argc, char **argv)
268{
269 char *device = "eth0";
270 int ifindex;
271 char *source = NULL;
272 char *target;
273
274 s = socket(PF_PACKET, SOCK_DGRAM, 0);
275 ifindex = errno;
276
277 // Drop suid root privileges
278 xsetuid(getuid());
279
280 {
281 unsigned long opt;
282 char *_count, *_timeout, *_device;
283
284 /* Dad also sets quit_on_reply.
285 * Advert also sets unsolicited.
286 */
287 bb_opt_complementally = "Df:AU";
288 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
289 &_count, &_timeout, &_device);
290 cfg |= opt & 63; /* set respective flags */
291 if (opt & 64) /* count */
292 count = atoi(_count);
293 if (opt & 128) /* timeout */
294 timeout = atoi(_timeout);
295 if (opt & 256) { /* interface */
296 if (strlen(_device) > IF_NAMESIZE) {
297 bb_error_msg_and_die("Interface name `%s' must be less than %d",
298 _device, IF_NAMESIZE);
299 }
300 device = _device;
301 }
302 if (opt & 512) /* source */
303 source = optarg;
304 }
305 argc -= optind;
306 argv += optind;
307
308 if (argc != 1)
309 bb_show_usage();
310
311 target = *argv;
312
313
314 if (s < 0) {
315 bb_default_error_retval = ifindex;
316 bb_perror_msg_and_die("socket");
317 }
318 bb_default_error_retval = 2;
319
320 {
321 struct ifreq ifr;
322
323 memset(&ifr, 0, sizeof(ifr));
324 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
325 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
326 bb_error_msg_and_die("Interface %s not found", device);
327 }
328 ifindex = ifr.ifr_ifindex;
329
330 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
331 bb_error_msg_and_die("SIOCGIFFLAGS");
332 }
333 if (!(ifr.ifr_flags & IFF_UP)) {
334 bb_error_msg_and_die("Interface %s is down", device);
335 }
336 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
337 bb_error_msg("Interface %s is not ARPable", device);
338 exit(cfg&dad ? 0 : 2);
339 }
340 }
341
342 if (!inet_aton(target, &dst)) {
343 struct hostent *hp;
344
345 hp = gethostbyname2(target, AF_INET);
346 if (!hp) {
347 bb_error_msg_and_die("invalid or unknown target %s", target);
348 }
349 memcpy(&dst, hp->h_addr, 4);
350 }
351
352 if (source && !inet_aton(source, &src)) {
353 bb_error_msg_and_die("invalid source address %s", source);
354 }
355
356 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
357 src = dst;
358
359 if (!(cfg&dad) || src.s_addr) {
360 struct sockaddr_in saddr;
361 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
362
363 if (probe_fd < 0) {
364 bb_error_msg_and_die("socket");
365 }
366 if (device) {
367 if (setsockopt
368 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
369 strlen(device) + 1) == -1)
370 bb_error_msg("WARNING: interface %s is ignored", device);
371 }
372 memset(&saddr, 0, sizeof(saddr));
373 saddr.sin_family = AF_INET;
374 if (src.s_addr) {
375 saddr.sin_addr = src;
376 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
377 bb_error_msg_and_die("bind");
378 }
379 } else if (!(cfg&dad)) {
380 int on = 1;
381 socklen_t alen = sizeof(saddr);
382
383 saddr.sin_port = htons(1025);
384 saddr.sin_addr = dst;
385
386 if (setsockopt
387 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
388 sizeof(on)) == -1)
389 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
390 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
391 == -1) {
392 bb_error_msg_and_die("connect");
393 }
394 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
395 -1) {
396 bb_error_msg_and_die("getsockname");
397 }
398 src = saddr.sin_addr;
399 }
400 close(probe_fd);
401 };
402
403 me.sll_family = AF_PACKET;
404 me.sll_ifindex = ifindex;
405 me.sll_protocol = htons(ETH_P_ARP);
406 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
407 bb_error_msg_and_die("bind");
408 }
409
410 {
411 socklen_t alen = sizeof(me);
412
413 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
414 bb_error_msg_and_die("getsockname");
415 }
416 }
417 if (me.sll_halen == 0) {
418 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
419 exit(cfg&dad ? 0 : 2);
420 }
421 he = me;
422 memset(he.sll_addr, -1, he.sll_halen);
423
424 if (!(cfg&quiet)) {
425 printf("ARPING to %s from %s via %s\n",
426 inet_ntoa(dst), inet_ntoa(src),
427 device ? device : "unknown");
428 }
429
430 if (!src.s_addr && !(cfg&dad)) {
431 bb_error_msg_and_die("no src address in the non-DAD mode");
432 }
433
434 {
435 struct sigaction sa;
436
437 memset(&sa, 0, sizeof(sa));
438 sa.sa_flags = SA_RESTART;
439
440 sa.sa_handler = (void (*)(int)) finish;
441 sigaction(SIGINT, &sa, NULL);
442
443 sa.sa_handler = (void (*)(int)) catcher;
444 sigaction(SIGALRM, &sa, NULL);
445 }
446
447 catcher();
448
449 while (1) {
450 sigset_t sset, osset;
451 RESERVE_CONFIG_UBUFFER(packet, 4096);
452 struct sockaddr_ll from;
453 socklen_t alen = sizeof(from);
454 int cc;
455
456 if ((cc = recvfrom(s, packet, 4096, 0,
457 (struct sockaddr *) &from, &alen)) < 0) {
458 perror("recvfrom");
459 continue;
460 }
461 sigemptyset(&sset);
462 sigaddset(&sset, SIGALRM);
463 sigaddset(&sset, SIGINT);
464 sigprocmask(SIG_BLOCK, &sset, &osset);
465 recv_pack(packet, cc, &from);
466 sigprocmask(SIG_SETMASK, &osset, NULL);
467 RELEASE_CONFIG_BUFFER(packet);
468 }
469}
Note: See TracBrowser for help on using the repository browser.