2 * arping.c - Ping hosts by ARP requests/replies
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
10 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
13 #include <sys/ioctl.h>
14 #include <sys/signal.h>
22 #include <arpa/inet.h>
24 #include <netinet/ether.h>
25 #include <netpacket/packet.h>
29 #define APPLET_NAME "arping"
31 static struct in_addr src;
32 static struct in_addr dst;
33 static struct sockaddr_ll me;
34 static struct sockaddr_ll he;
35 static struct timeval last;
37 static int unsolicited;
40 static int quit_on_reply = 0;
41 static int count = -1;
43 static int unicasting;
45 static int broadcast_only;
52 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
53 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
55 static void set_signal(int signo, void (*handler) (void))
59 memset(&sa, 0, sizeof(sa));
60 sa.sa_handler = (void (*)(int)) handler;
61 sa.sa_flags = SA_RESTART;
62 sigaction(signo, &sa, NULL);
66 static int send_pack(int sock, struct in_addr *src_addr,
67 struct in_addr *dst_addr, struct sockaddr_ll *ME,
68 struct sockaddr_ll *HE)
72 unsigned char buf[256];
73 struct arphdr *ah = (struct arphdr *) buf;
74 unsigned char *p = (unsigned char *) (ah + 1);
76 ah->ar_hrd = htons(ME->sll_hatype);
77 ah->ar_hrd = htons(ARPHRD_ETHER);
78 ah->ar_pro = htons(ETH_P_IP);
79 ah->ar_hln = ME->sll_halen;
81 ah->ar_op = advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
83 memcpy(p, &ME->sll_addr, ah->ar_hln);
86 memcpy(p, src_addr, 4);
90 memcpy(p, &ME->sll_addr, ah->ar_hln);
92 memcpy(p, &HE->sll_addr, ah->ar_hln);
95 memcpy(p, dst_addr, 4);
98 gettimeofday(&now, NULL);
99 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
100 if (err == p - buf) {
112 printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
113 printf("Received %d repl%s", received, (received > 1) ? "ies" : "y");
114 if (brd_recv || req_recv) {
117 printf("%d request(s)", req_recv);
119 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
135 static struct timeval start;
137 gettimeofday(&tv, NULL);
139 if (start.tv_sec == 0)
143 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
146 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
147 send_pack(s, &src, &dst, &me, &he);
148 if (count == 0 && unsolicited)
154 int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
157 struct arphdr *ah = (struct arphdr *) buf;
158 unsigned char *p = (unsigned char *) (ah + 1);
159 struct in_addr src_ip, dst_ip;
161 gettimeofday(&tv, NULL);
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 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast");
224 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
225 printf("%s ", inet_ntoa(src_ip));
226 printf("[%s]", ether_ntoa((struct ether_addr *) p));
227 if (dst_ip.s_addr != src.s_addr) {
228 printf("for %s ", inet_ntoa(dst_ip));
231 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
235 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
238 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
239 tv.tv_usec - last.tv_usec;
240 long msecs = (usecs + 500) / 1000;
242 usecs -= msecs * 1000 - 500;
243 printf(" %ld.%03ldms\n", msecs, usecs);
245 printf(" UNSOLICITED?\n");
250 if (FROM->sll_pkttype != PACKET_HOST)
252 if (ah->ar_op == htons(ARPOP_REQUEST))
256 if (!broadcast_only) {
257 memcpy(he.sll_addr, p, me.sll_halen);
263 int arping_main(int argc, char **argv)
267 uid_t uid = getuid();
268 char *device = "eth0";
273 s = socket(PF_PACKET, SOCK_DGRAM, 0);
274 socket_errno = errno;
278 while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:")) != EOF) {
298 count = atoi(optarg);
301 timeout = atoi(optarg);
306 if (bb_strlen(optarg) > IF_NAMESIZE) {
307 bb_error_msg("Interface name `%s' must be less than %d", optarg,
335 bb_error_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("Interface %s not found", device);
348 ifindex = ifr.ifr_ifindex;
350 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
351 bb_error_msg("SIOCGIFFLAGS");
354 if (!(ifr.ifr_flags & IFF_UP)) {
355 bb_error_msg("Interface %s is down", device);
358 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
359 bb_error_msg("Interface %s is not ARPable", device);
364 if (!inet_aton(target, &dst)) {
367 hp = gethostbyname2(target, AF_INET);
369 bb_error_msg("invalid or unknown target %s", target);
372 memcpy(&dst, hp->h_addr, 4);
375 if (source && !inet_aton(source, &src)) {
376 bb_error_msg("invalid source address %s", source);
380 if (!dad && unsolicited && src.s_addr == 0)
383 if (!dad || src.s_addr) {
384 struct sockaddr_in saddr;
385 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
388 bb_error_msg("socket");
393 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
394 strlen(device) + 1) == -1)
395 bb_error_msg("WARNING: interface %s is ignored", device);
397 memset(&saddr, 0, sizeof(saddr));
398 saddr.sin_family = AF_INET;
400 saddr.sin_addr = src;
401 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
402 bb_error_msg("bind");
407 int alen = sizeof(saddr);
409 saddr.sin_port = htons(1025);
410 saddr.sin_addr = dst;
413 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
415 perror("WARNING: setsockopt(SO_DONTROUTE)");
416 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
418 bb_error_msg("connect");
421 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
423 bb_error_msg("getsockname");
426 src = saddr.sin_addr;
431 me.sll_family = AF_PACKET;
432 me.sll_ifindex = ifindex;
433 me.sll_protocol = htons(ETH_P_ARP);
434 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
435 bb_error_msg("bind");
440 int alen = sizeof(me);
442 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
443 bb_error_msg("getsockname");
447 if (me.sll_halen == 0) {
448 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
452 memset(he.sll_addr, -1, he.sll_halen);
455 printf("ARPING to %s", inet_ntoa(dst));
456 printf(" from %s via %s\n", inet_ntoa(src),
457 device ? device : "unknown");
460 if (!src.s_addr && !dad) {
461 bb_error_msg("no src address in the non-DAD mode");
468 memset(&sa, 0, sizeof(sa));
469 sa.sa_flags = SA_RESTART;
471 sa.sa_handler = (void (*)(int)) finish;
472 sigaction(SIGINT, &sa, NULL);
474 sa.sa_handler = (void (*)(int)) catcher;
475 sigaction(SIGALRM, &sa, NULL);
481 sigset_t sset, osset;
483 struct sockaddr_ll from;
484 int alen = sizeof(from);
487 if ((cc = recvfrom(s, packet, sizeof(packet), 0,
488 (struct sockaddr *) &from, &alen)) < 0) {
493 sigaddset(&sset, SIGALRM);
494 sigaddset(&sset, SIGINT);
495 sigprocmask(SIG_BLOCK, &sset, &osset);
496 recv_pack(packet, cc, &from);
497 sigprocmask(SIG_SETMASK, &osset, NULL);