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;
41 static struct cfg_s cfg;
44 static int count = -1;
53 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
54 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
56 static void set_signal(int signo, void (*handler) (void))
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = (void (*)(int)) handler;
62 sa.sa_flags = SA_RESTART;
63 sigaction(signo, &sa, NULL);
67 static 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)
73 RESERVE_CONFIG_UBUFFER(buf, 256);
74 struct arphdr *ah = (struct arphdr *) buf;
75 unsigned char *p = (unsigned char *) (ah + 1);
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;
82 ah->ar_op = cfg.advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
84 memcpy(p, &ME->sll_addr, ah->ar_hln);
87 memcpy(p, src_addr, 4);
91 memcpy(p, &ME->sll_addr, ah->ar_hln);
93 memcpy(p, &HE->sll_addr, ah->ar_hln);
96 memcpy(p, dst_addr, 4);
99 gettimeofday(&now, NULL);
100 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
101 if (err == p - buf) {
107 RELEASE_CONFIG_BUFFER(buf);
111 static void finish(void)
114 printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
115 printf("Received %d repl%s", received, (received > 1) ? "ies" : "y");
116 if (brd_recv || req_recv) {
119 printf("%d request(s)", req_recv);
121 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
134 static void catcher(void)
137 static struct timeval start;
139 gettimeofday(&tv, NULL);
141 if (start.tv_sec == 0)
145 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
148 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
149 send_pack(s, &src, &dst, &me, &he);
150 if (count == 0 && cfg.unsolicited)
156 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
158 struct arphdr *ah = (struct arphdr *) buf;
159 unsigned char *p = (unsigned char *) (ah + 1);
160 struct in_addr src_ip, dst_ip;
162 /* Filter out wild packets */
163 if (FROM->sll_pkttype != PACKET_HOST &&
164 FROM->sll_pkttype != PACKET_BROADCAST &&
165 FROM->sll_pkttype != PACKET_MULTICAST)
168 /* Only these types are recognised */
169 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
172 /* ARPHRD check and this darned FDDI hack here :-( */
173 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
174 (FROM->sll_hatype != ARPHRD_FDDI
175 || ah->ar_hrd != htons(ARPHRD_ETHER)))
178 /* Protocol must be IP. */
179 if (ah->ar_pro != htons(ETH_P_IP))
183 if (ah->ar_hln != me.sll_halen)
185 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
187 memcpy(&src_ip, p + ah->ar_hln, 4);
188 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
190 if (src_ip.s_addr != dst.s_addr)
192 if (src.s_addr != dst_ip.s_addr)
194 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
198 src_ip = 0 (or some src)
200 dst_ip = tested address
203 We fail, if receive request/reply with:
204 src_ip = tested_address
206 if src_ip in request was not zero, check
207 also that it matches to dst_ip, otherwise
208 dst_ip/dst_hw do not matter.
210 if (src_ip.s_addr != dst.s_addr)
212 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
214 if (src.s_addr && src.s_addr != dst_ip.s_addr)
221 gettimeofday(&tv, NULL);
224 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast");
226 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
227 printf("%s ", inet_ntoa(src_ip));
228 printf("[%s]", ether_ntoa((struct ether_addr *) p));
229 if (dst_ip.s_addr != src.s_addr) {
230 printf("for %s ", inet_ntoa(dst_ip));
233 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
237 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
241 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
242 tv.tv_usec - last.tv_usec;
243 long msecs = (usecs + 500) / 1000;
245 usecs -= msecs * 1000 - 500;
246 printf(" %ld.%03ldms\n", msecs, usecs);
248 printf(" UNSOLICITED?\n");
253 if (FROM->sll_pkttype != PACKET_HOST)
255 if (ah->ar_op == htons(ARPOP_REQUEST))
257 if (cfg.quit_on_reply)
259 if (!cfg.broadcast_only) {
260 memcpy(he.sll_addr, p, me.sll_halen);
266 int arping_main(int argc, char **argv)
269 char *device = "eth0";
274 s = socket(PF_PACKET, SOCK_DGRAM, 0);
279 while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:")) != EOF) {
282 cfg.broadcast_only = 1;
286 cfg.quit_on_reply = 1;
299 count = atoi(optarg);
302 timeout = atoi(optarg);
307 if (bb_strlen(optarg) > IF_NAMESIZE) {
308 bb_error_msg_and_die("Interface name `%s' must be less than %d",
309 optarg, IF_NAMESIZE);
314 cfg.quit_on_reply = 1;
332 bb_default_error_retval = 2;
335 bb_perror_msg("socket");
342 memset(&ifr, 0, sizeof(ifr));
343 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
344 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
345 bb_error_msg_and_die("Interface %s not found", device);
347 ifindex = ifr.ifr_ifindex;
349 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
350 bb_error_msg_and_die("SIOCGIFFLAGS");
352 if (!(ifr.ifr_flags & IFF_UP)) {
353 bb_error_msg_and_die("Interface %s is down", device);
355 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
356 bb_error_msg("Interface %s is not ARPable", device);
357 exit(cfg.dad ? 0 : 2);
361 if (!inet_aton(target, &dst)) {
364 hp = gethostbyname2(target, AF_INET);
366 bb_error_msg_and_die("invalid or unknown target %s", target);
368 memcpy(&dst, hp->h_addr, 4);
371 if (source && !inet_aton(source, &src)) {
372 bb_error_msg_and_die("invalid source address %s", source);
375 if (!cfg.dad && cfg.unsolicited && src.s_addr == 0)
378 if (!cfg.dad || src.s_addr) {
379 struct sockaddr_in saddr;
380 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
383 bb_error_msg_and_die("socket");
387 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
388 strlen(device) + 1) == -1)
389 bb_error_msg("WARNING: interface %s is ignored", device);
391 memset(&saddr, 0, sizeof(saddr));
392 saddr.sin_family = AF_INET;
394 saddr.sin_addr = src;
395 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
396 bb_error_msg_and_die("bind");
398 } else if (!cfg.dad) {
400 socklen_t alen = sizeof(saddr);
402 saddr.sin_port = htons(1025);
403 saddr.sin_addr = dst;
406 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
408 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
409 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
411 bb_error_msg_and_die("connect");
413 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
415 bb_error_msg_and_die("getsockname");
417 src = saddr.sin_addr;
422 me.sll_family = AF_PACKET;
423 me.sll_ifindex = ifindex;
424 me.sll_protocol = htons(ETH_P_ARP);
425 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
426 bb_error_msg_and_die("bind");
430 socklen_t alen = sizeof(me);
432 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
433 bb_error_msg_and_die("getsockname");
436 if (me.sll_halen == 0) {
437 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
438 exit(cfg.dad ? 0 : 2);
441 memset(he.sll_addr, -1, he.sll_halen);
444 printf("ARPING to %s", inet_ntoa(dst));
445 printf(" from %s via %s\n", inet_ntoa(src),
446 device ? device : "unknown");
449 if (!src.s_addr && !cfg.dad) {
450 bb_error_msg_and_die("no src address in the non-DAD mode");
456 memset(&sa, 0, sizeof(sa));
457 sa.sa_flags = SA_RESTART;
459 sa.sa_handler = (void (*)(int)) finish;
460 sigaction(SIGINT, &sa, NULL);
462 sa.sa_handler = (void (*)(int)) catcher;
463 sigaction(SIGALRM, &sa, NULL);
469 sigset_t sset, osset;
470 unsigned char packet[4096];
471 struct sockaddr_ll from;
472 socklen_t alen = sizeof(from);
475 if ((cc = recvfrom(s, packet, sizeof(packet), 0,
476 (struct sockaddr *) &from, &alen)) < 0) {
481 sigaddset(&sset, SIGALRM);
482 sigaddset(&sset, SIGINT);
483 sigprocmask(SIG_BLOCK, &sset, &osset);
484 recv_pack(packet, cc, &from);
485 sigprocmask(SIG_SETMASK, &osset, NULL);