1 /* vi: set sw=4 ts=4: */
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>
14 #include <arpa/inet.h>
16 #include <netinet/ether.h>
17 #include <netpacket/packet.h>
21 static struct in_addr src;
22 static struct in_addr dst;
23 static struct sockaddr_ll me;
24 static struct sockaddr_ll he;
25 static struct timeval last;
39 static unsigned count = UINT_MAX;
40 static unsigned timeout;
47 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
48 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
50 static int send_pack(int sock, struct in_addr *src_addr,
51 struct in_addr *dst_addr, struct sockaddr_ll *ME,
52 struct sockaddr_ll *HE)
56 RESERVE_CONFIG_UBUFFER(buf, 256);
57 struct arphdr *ah = (struct arphdr *) buf;
58 unsigned char *p = (unsigned char *) (ah + 1);
60 ah->ar_hrd = htons(ME->sll_hatype);
61 ah->ar_hrd = htons(ARPHRD_ETHER);
62 ah->ar_pro = htons(ETH_P_IP);
63 ah->ar_hln = ME->sll_halen;
65 ah->ar_op = cfg & advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
67 memcpy(p, &ME->sll_addr, ah->ar_hln);
70 memcpy(p, src_addr, 4);
74 memcpy(p, &ME->sll_addr, ah->ar_hln);
76 memcpy(p, &HE->sll_addr, ah->ar_hln);
79 memcpy(p, dst_addr, 4);
82 gettimeofday(&now, NULL);
83 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
87 if (!(cfg & unicasting))
90 RELEASE_CONFIG_BUFFER(buf);
94 static void finish(void)
97 printf("Sent %d probe(s) (%d broadcast(s))\n"
99 " (%d request(s), %d broadcast(s))\n",
101 received, (received == 1) ? "ies" : "y",
106 if (cfg & unsolicited)
111 static void catcher(void)
114 static struct timeval start;
116 gettimeofday(&tv, NULL);
118 if (start.tv_sec == 0)
122 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
125 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
126 send_pack(s, &src, &dst, &me, &he);
127 if (count == 0 && (cfg & unsolicited))
133 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
135 struct arphdr *ah = (struct arphdr *) buf;
136 unsigned char *p = (unsigned char *) (ah + 1);
137 struct in_addr src_ip, dst_ip;
139 /* Filter out wild packets */
140 if (FROM->sll_pkttype != PACKET_HOST
141 && FROM->sll_pkttype != PACKET_BROADCAST
142 && FROM->sll_pkttype != PACKET_MULTICAST)
145 /* Only these types are recognised */
146 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
149 /* ARPHRD check and this darned FDDI hack here :-( */
150 if (ah->ar_hrd != htons(FROM->sll_hatype)
151 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
154 /* Protocol must be IP. */
155 if (ah->ar_pro != htons(ETH_P_IP))
159 if (ah->ar_hln != me.sll_halen)
161 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
163 memcpy(&src_ip, p + ah->ar_hln, 4);
164 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
166 if (src_ip.s_addr != dst.s_addr)
168 if (src.s_addr != dst_ip.s_addr)
170 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
174 src_ip = 0 (or some src)
176 dst_ip = tested address
179 We fail, if receive request/reply with:
180 src_ip = tested_address
182 if src_ip in request was not zero, check
183 also that it matches to dst_ip, otherwise
184 dst_ip/dst_hw do not matter.
186 if (src_ip.s_addr != dst.s_addr)
188 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
190 if (src.s_addr && src.s_addr != dst_ip.s_addr)
193 if (!(cfg & quiet)) {
197 gettimeofday(&tv, NULL);
199 printf("%scast re%s from %s [%s]",
200 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
201 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
203 ether_ntoa((struct ether_addr *) p));
204 if (dst_ip.s_addr != src.s_addr) {
205 printf("for %s ", inet_ntoa(dst_ip));
208 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
212 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
216 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
217 tv.tv_usec - last.tv_usec;
218 long msecs = (usecs + 500) / 1000;
220 usecs -= msecs * 1000 - 500;
221 printf(" %ld.%03ldms\n", msecs, usecs);
223 printf(" UNSOLICITED?\n");
228 if (FROM->sll_pkttype != PACKET_HOST)
230 if (ah->ar_op == htons(ARPOP_REQUEST))
232 if (cfg & quit_on_reply)
234 if (!(cfg & broadcast_only)) {
235 memcpy(he.sll_addr, p, me.sll_halen);
241 int arping_main(int argc, char **argv);
242 int arping_main(int argc, char **argv)
244 const char *device = "eth0";
249 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
251 // Drop suid root privileges
256 char *_count, *_timeout;
258 /* Dad also sets quit_on_reply.
259 * Advert also sets unsolicited.
261 opt_complementary = "Df:AU";
262 opt = getopt32(argc, argv, "DUAqfbc:w:i:s:",
263 &_count, &_timeout, &device, &source);
264 cfg |= opt & 0x3f; /* set respective flags */
265 if (opt & 0x40) /* -c: count */
266 count = xatou(_count);
267 if (opt & 0x80) /* -w: timeout */
268 timeout = xatoul_range(_timeout, 0, INT_MAX/2000);
269 //if (opt & 0x100) /* -i: interface */
270 if (strlen(device) > IF_NAMESIZE) {
271 bb_error_msg_and_die("interface name '%s' is too long",
274 //if (opt & 0x200) /* -s: source */
284 xfunc_error_retval = 2;
289 memset(&ifr, 0, sizeof(ifr));
290 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
291 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
292 bb_error_msg_and_die("interface %s not found", device);
294 ifindex = ifr.ifr_ifindex;
296 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
297 bb_error_msg_and_die("SIOCGIFFLAGS");
299 if (!(ifr.ifr_flags & IFF_UP)) {
300 bb_error_msg_and_die("interface %s is down", device);
302 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
303 bb_error_msg("interface %s is not ARPable", device);
304 return (cfg & dad ? 0 : 2);
308 if (!inet_aton(target, &dst)) {
309 len_and_sockaddr *lsa;
310 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
311 memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
312 if (ENABLE_FEATURE_CLEAN_UP)
316 if (source && !inet_aton(source, &src)) {
317 bb_error_msg_and_die("invalid source address %s", source);
320 if (!(cfg & dad) && (cfg & unsolicited) && src.s_addr == 0)
323 if (!(cfg & dad) || src.s_addr) {
324 struct sockaddr_in saddr;
325 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
328 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
329 bb_error_msg("warning: interface %s is ignored", device);
331 memset(&saddr, 0, sizeof(saddr));
332 saddr.sin_family = AF_INET;
334 saddr.sin_addr = src;
335 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
336 } else if (!(cfg & dad)) {
337 socklen_t alen = sizeof(saddr);
339 saddr.sin_port = htons(1025);
340 saddr.sin_addr = dst;
342 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
343 bb_perror_msg("warning: setsockopt(SO_DONTROUTE)");
344 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
345 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
346 bb_error_msg_and_die("getsockname");
348 src = saddr.sin_addr;
353 me.sll_family = AF_PACKET;
354 me.sll_ifindex = ifindex;
355 me.sll_protocol = htons(ETH_P_ARP);
356 xbind(s, (struct sockaddr *) &me, sizeof(me));
359 socklen_t alen = sizeof(me);
361 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
362 bb_error_msg_and_die("getsockname");
365 if (me.sll_halen == 0) {
366 bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
367 return (cfg & dad ? 0 : 2);
370 memset(he.sll_addr, -1, he.sll_halen);
372 if (!(cfg & quiet)) {
373 printf("ARPING to %s from %s via %s\n",
374 inet_ntoa(dst), inet_ntoa(src),
375 device ? device : "unknown");
378 if (!src.s_addr && !(cfg & dad)) {
379 bb_error_msg_and_die("no src address in the non-DAD mode");
385 memset(&sa, 0, sizeof(sa));
386 sa.sa_flags = SA_RESTART;
388 sa.sa_handler = (void (*)(int)) finish;
389 sigaction(SIGINT, &sa, NULL);
391 sa.sa_handler = (void (*)(int)) catcher;
392 sigaction(SIGALRM, &sa, NULL);
398 sigset_t sset, osset;
399 RESERVE_CONFIG_UBUFFER(packet, 4096);
400 struct sockaddr_ll from;
401 socklen_t alen = sizeof(from);
404 cc = recvfrom(s, packet, 4096, 0, (struct sockaddr *) &from, &alen);
406 bb_perror_msg("recvfrom");
410 sigaddset(&sset, SIGALRM);
411 sigaddset(&sset, SIGINT);
412 sigprocmask(SIG_BLOCK, &sset, &osset);
413 recv_pack(packet, cc, &from);
414 sigprocmask(SIG_SETMASK, &osset, NULL);
415 RELEASE_CONFIG_BUFFER(packet);