3 * arping.c - Ping hosts by ARP requests/replies
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
11 #include <sys/ioctl.h>
20 #include <arpa/inet.h>
22 #include <netinet/ether.h>
23 #include <netpacket/packet.h>
27 static struct in_addr src;
28 static struct in_addr dst;
29 static struct sockaddr_ll me;
30 static struct sockaddr_ll he;
31 static struct timeval last;
45 static int count = -1;
54 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
55 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
57 static void set_signal(int signo, void (*handler) (void))
61 memset(&sa, 0, sizeof(sa));
62 sa.sa_handler = (void (*)(int)) handler;
63 sa.sa_flags = SA_RESTART;
64 sigaction(signo, &sa, NULL);
68 static int send_pack(int sock, struct in_addr *src_addr,
69 struct in_addr *dst_addr, struct sockaddr_ll *ME,
70 struct sockaddr_ll *HE)
74 RESERVE_CONFIG_UBUFFER(buf, 256);
75 struct arphdr *ah = (struct arphdr *) buf;
76 unsigned char *p = (unsigned char *) (ah + 1);
78 ah->ar_hrd = htons(ME->sll_hatype);
79 ah->ar_hrd = htons(ARPHRD_ETHER);
80 ah->ar_pro = htons(ETH_P_IP);
81 ah->ar_hln = ME->sll_halen;
83 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
85 memcpy(p, &ME->sll_addr, ah->ar_hln);
88 memcpy(p, src_addr, 4);
92 memcpy(p, &ME->sll_addr, ah->ar_hln);
94 memcpy(p, &HE->sll_addr, ah->ar_hln);
97 memcpy(p, dst_addr, 4);
100 gettimeofday(&now, NULL);
101 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
102 if (err == p - buf) {
105 if (!(cfg&unicasting))
108 RELEASE_CONFIG_BUFFER(buf);
112 static void finish(void)
115 printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
116 printf("Received %d repl%s", received, (received > 1) ? "ies" : "y");
117 if (brd_recv || req_recv) {
120 printf("%d request(s)", req_recv);
122 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
135 static void catcher(void)
138 static struct timeval start;
140 gettimeofday(&tv, NULL);
142 if (start.tv_sec == 0)
146 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
149 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
150 send_pack(s, &src, &dst, &me, &he);
151 if (count == 0 && cfg&unsolicited)
157 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
159 struct arphdr *ah = (struct arphdr *) buf;
160 unsigned char *p = (unsigned char *) (ah + 1);
161 struct in_addr src_ip, dst_ip;
163 /* Filter out wild packets */
164 if (FROM->sll_pkttype != PACKET_HOST &&
165 FROM->sll_pkttype != PACKET_BROADCAST &&
166 FROM->sll_pkttype != PACKET_MULTICAST)
169 /* Only these types are recognised */
170 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
173 /* ARPHRD check and this darned FDDI hack here :-( */
174 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
175 (FROM->sll_hatype != ARPHRD_FDDI
176 || ah->ar_hrd != htons(ARPHRD_ETHER)))
179 /* Protocol must be IP. */
180 if (ah->ar_pro != htons(ETH_P_IP))
184 if (ah->ar_hln != me.sll_halen)
186 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
188 memcpy(&src_ip, p + ah->ar_hln, 4);
189 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
191 if (src_ip.s_addr != dst.s_addr)
193 if (src.s_addr != dst_ip.s_addr)
195 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
199 src_ip = 0 (or some src)
201 dst_ip = tested address
204 We fail, if receive request/reply with:
205 src_ip = tested_address
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.
211 if (src_ip.s_addr != dst.s_addr)
213 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
215 if (src.s_addr && src.s_addr != dst_ip.s_addr)
222 gettimeofday(&tv, NULL);
225 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast");
227 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
228 printf("%s ", inet_ntoa(src_ip));
229 printf("[%s]", ether_ntoa((struct ether_addr *) p));
230 if (dst_ip.s_addr != src.s_addr) {
231 printf("for %s ", inet_ntoa(dst_ip));
234 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
238 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
242 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
243 tv.tv_usec - last.tv_usec;
244 long msecs = (usecs + 500) / 1000;
246 usecs -= msecs * 1000 - 500;
247 printf(" %ld.%03ldms\n", msecs, usecs);
249 printf(" UNSOLICITED?\n");
254 if (FROM->sll_pkttype != PACKET_HOST)
256 if (ah->ar_op == htons(ARPOP_REQUEST))
258 if (cfg&quit_on_reply)
260 if (!(cfg&broadcast_only)) {
261 memcpy(he.sll_addr, p, me.sll_halen);
267 int arping_main(int argc, char **argv)
269 char *device = "eth0";
274 s = socket(PF_PACKET, SOCK_DGRAM, 0);
281 char *_count, *_timeout, *_device;
282 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
283 &_count, &_timeout, &_device);
284 if (opt & 1) { /* Dad */
286 cfg |= quit_on_reply;
288 if (opt & 2) /* Unsolicited */
290 if (opt & 4) { /* Advert */
294 if (opt & 8) /* quiet */
296 if (opt & 16) /* quit on reply */
297 cfg |= quit_on_reply;
298 if (opt & 32) /* broadcast only */
299 cfg |= broadcast_only;
300 if (opt & 64) /* count */
301 count = atoi(_count);
302 if (opt & 128) /* timeout */
303 timeout = atoi(_timeout);
304 if (opt & 256) { /* interface */
305 if (bb_strlen(_device) > IF_NAMESIZE) {
306 bb_error_msg_and_die("Interface name `%s' must be less than %d",
307 _device, IF_NAMESIZE);
311 if (opt & 512) /* source */
324 bb_default_error_retval = ifindex;
325 bb_perror_msg_and_die("socket");
327 bb_default_error_retval = 2;
332 memset(&ifr, 0, sizeof(ifr));
333 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
334 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
335 bb_error_msg_and_die("Interface %s not found", device);
337 ifindex = ifr.ifr_ifindex;
339 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
340 bb_error_msg_and_die("SIOCGIFFLAGS");
342 if (!(ifr.ifr_flags & IFF_UP)) {
343 bb_error_msg_and_die("Interface %s is down", device);
345 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
346 bb_error_msg("Interface %s is not ARPable", device);
347 exit(cfg&dad ? 0 : 2);
351 if (!inet_aton(target, &dst)) {
354 hp = gethostbyname2(target, AF_INET);
356 bb_error_msg_and_die("invalid or unknown target %s", target);
358 memcpy(&dst, hp->h_addr, 4);
361 if (source && !inet_aton(source, &src)) {
362 bb_error_msg_and_die("invalid source address %s", source);
365 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
368 if (!(cfg&dad) || src.s_addr) {
369 struct sockaddr_in saddr;
370 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
373 bb_error_msg_and_die("socket");
377 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
378 strlen(device) + 1) == -1)
379 bb_error_msg("WARNING: interface %s is ignored", device);
381 memset(&saddr, 0, sizeof(saddr));
382 saddr.sin_family = AF_INET;
384 saddr.sin_addr = src;
385 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
386 bb_error_msg_and_die("bind");
388 } else if (!(cfg&dad)) {
390 socklen_t alen = sizeof(saddr);
392 saddr.sin_port = htons(1025);
393 saddr.sin_addr = dst;
396 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
398 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
399 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
401 bb_error_msg_and_die("connect");
403 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
405 bb_error_msg_and_die("getsockname");
407 src = saddr.sin_addr;
412 me.sll_family = AF_PACKET;
413 me.sll_ifindex = ifindex;
414 me.sll_protocol = htons(ETH_P_ARP);
415 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
416 bb_error_msg_and_die("bind");
420 socklen_t alen = sizeof(me);
422 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
423 bb_error_msg_and_die("getsockname");
426 if (me.sll_halen == 0) {
427 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
428 exit(cfg&dad ? 0 : 2);
431 memset(he.sll_addr, -1, he.sll_halen);
434 printf("ARPING to %s", inet_ntoa(dst));
435 printf(" from %s via %s\n", inet_ntoa(src),
436 device ? device : "unknown");
439 if (!src.s_addr && !(cfg&dad)) {
440 bb_error_msg_and_die("no src address in the non-DAD mode");
446 memset(&sa, 0, sizeof(sa));
447 sa.sa_flags = SA_RESTART;
449 sa.sa_handler = (void (*)(int)) finish;
450 sigaction(SIGINT, &sa, NULL);
452 sa.sa_handler = (void (*)(int)) catcher;
453 sigaction(SIGALRM, &sa, NULL);
459 sigset_t sset, osset;
460 RESERVE_CONFIG_UBUFFER(packet, 4096);
461 struct sockaddr_ll from;
462 socklen_t alen = sizeof(from);
465 if ((cc = recvfrom(s, packet, 4096, 0,
466 (struct sockaddr *) &from, &alen)) < 0) {
471 sigaddset(&sset, SIGALRM);
472 sigaddset(&sset, SIGINT);
473 sigprocmask(SIG_BLOCK, &sset, &osset);
474 recv_pack(packet, cc, &from);
475 sigprocmask(SIG_SETMASK, &osset, NULL);
476 RELEASE_CONFIG_BUFFER(packet);