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 <arpa/inet.h>
13 #include <netinet/ether.h>
14 #include <netpacket/packet.h>
18 /* We don't expect to see 1000+ seconds delay, unsigned is enough */
19 #define MONOTONIC_US() ((unsigned)monotonic_us())
34 struct sockaddr_ll me;
35 struct sockaddr_ll he;
49 #define G (*(struct globals*)&bb_common_bufsiz1)
54 #define sock_fd (G.sock_fd )
55 #define count (G.count )
56 #define last (G.last )
57 #define timeout_us (G.timeout_us)
58 #define start (G.start )
59 #define sent (G.sent )
60 #define brd_sent (G.brd_sent )
61 #define received (G.received )
62 #define brd_recv (G.brd_recv )
63 #define req_recv (G.req_recv )
69 static int send_pack(struct in_addr *src_addr,
70 struct in_addr *dst_addr, struct sockaddr_ll *ME,
71 struct sockaddr_ll *HE)
74 unsigned char 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 = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
85 memcpy(p, &ME->sll_addr, ah->ar_hln);
88 memcpy(p, src_addr, 4);
91 if (option_mask32 & ADVERT)
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 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
101 if (err == p - buf) {
102 last = MONOTONIC_US();
104 if (!(option_mask32 & UNICASTING))
110 static void finish(void) ATTRIBUTE_NORETURN;
111 static void finish(void)
113 if (!(option_mask32 & QUIET)) {
114 printf("Sent %u probe(s) (%u broadcast(s))\n"
116 " (%u request(s), %u broadcast(s))\n",
118 received, (received == 1) ? "ies" : "y",
121 if (option_mask32 & DAD)
123 if (option_mask32 & UNSOLICITED)
128 static void catcher(void)
132 now = MONOTONIC_US();
136 if (count == 0 || (timeout_us && (now - start) > timeout_us))
139 /* count < 0 means "infinite count" */
143 if (last == 0 || (now - last) > 500000) {
144 send_pack(&src, &dst, &me, &he);
145 if (count == 0 && (option_mask32 & UNSOLICITED))
151 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
153 struct arphdr *ah = (struct arphdr *) buf;
154 unsigned char *p = (unsigned char *) (ah + 1);
155 struct in_addr src_ip, dst_ip;
157 /* Filter out wild packets */
158 if (FROM->sll_pkttype != PACKET_HOST
159 && FROM->sll_pkttype != PACKET_BROADCAST
160 && FROM->sll_pkttype != PACKET_MULTICAST)
163 /* Only these types are recognised */
164 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
167 /* ARPHRD check and this darned FDDI hack here :-( */
168 if (ah->ar_hrd != htons(FROM->sll_hatype)
169 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
172 /* Protocol must be IP. */
173 if (ah->ar_pro != htons(ETH_P_IP))
177 if (ah->ar_hln != me.sll_halen)
179 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
181 memcpy(&src_ip, p + ah->ar_hln, 4);
182 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
183 if (!(option_mask32 & DAD)) {
184 if (dst.s_addr != src_ip.s_addr)
186 if (src.s_addr != dst_ip.s_addr)
188 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
192 src_ip = 0 (or some src)
194 dst_ip = tested address
197 We fail, if receive request/reply with:
198 src_ip = tested_address
200 if src_ip in request was not zero, check
201 also that it matches to dst_ip, otherwise
202 dst_ip/dst_hw do not matter.
204 if (src_ip.s_addr != dst.s_addr)
206 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
208 if (src.s_addr && src.s_addr != dst_ip.s_addr)
211 if (!(option_mask32 & QUIET)) {
214 printf("%scast re%s from %s [%s]",
215 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
216 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
218 ether_ntoa((struct ether_addr *) p));
219 if (dst_ip.s_addr != src.s_addr) {
220 printf("for %s ", inet_ntoa(dst_ip));
223 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
227 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
231 unsigned diff = MONOTONIC_US() - last;
232 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
234 printf(" UNSOLICITED?\n");
239 if (FROM->sll_pkttype != PACKET_HOST)
241 if (ah->ar_op == htons(ARPOP_REQUEST))
243 if (option_mask32 & QUIT_ON_REPLY)
245 if (!(option_mask32 & BCAST_ONLY)) {
246 memcpy(he.sll_addr, p, me.sll_halen);
247 option_mask32 |= UNICASTING;
252 int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
253 int arping_main(int argc, char **argv)
255 const char *device = "eth0";
258 unsigned char *packet;
262 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
264 // Drop suid root privileges
265 // Need to remove SUID_NEVER from applets.h for this to work
270 char *str_count, *str_timeout;
272 /* Dad also sets quit_on_reply.
273 * Advert also sets unsolicited.
275 opt_complementary = "=1:Df:AU";
276 opt = getopt32(argv, "DUAqfbc:w:I:s:",
277 &str_count, &str_timeout, &device, &source);
278 if (opt & 0x40) /* -c: count */
279 count = xatoi_u(str_count);
280 if (opt & 0x80) /* -w: timeout */
281 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
282 //if (opt & 0x200) /* -s: source */
283 option_mask32 &= 0x3f; /* set respective flags */
286 target = argv[optind];
288 xfunc_error_retval = 2;
293 memset(&ifr, 0, sizeof(ifr));
294 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
295 /* We use ifr.ifr_name in error msg so that problem
296 * with truncated name will be visible */
297 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, "interface %s not found", ifr.ifr_name);
298 me.sll_ifindex = ifr.ifr_ifindex;
300 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
302 if (!(ifr.ifr_flags & IFF_UP)) {
303 bb_error_msg_and_die("interface %s is down", device);
305 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
306 bb_error_msg("interface %s is not ARPable", device);
307 return (option_mask32 & DAD ? 0 : 2);
311 /* if (!inet_aton(target, &dst)) - not needed */ {
312 len_and_sockaddr *lsa;
313 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
314 memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
315 if (ENABLE_FEATURE_CLEAN_UP)
319 if (source && !inet_aton(source, &src)) {
320 bb_error_msg_and_die("invalid source address %s", source);
323 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
326 if (!(option_mask32 & DAD) || src.s_addr) {
327 struct sockaddr_in saddr;
328 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
330 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
331 bb_perror_msg("cannot bind to device %s", device);
332 memset(&saddr, 0, sizeof(saddr));
333 saddr.sin_family = AF_INET;
335 /* Check that this is indeed our IP */
336 saddr.sin_addr = src;
337 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
338 } else { /* !(option_mask32 & DAD) case */
339 /* Find IP address on this iface */
340 socklen_t alen = sizeof(saddr);
342 saddr.sin_port = htons(1025);
343 saddr.sin_addr = dst;
345 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
346 bb_perror_msg("setsockopt(SO_DONTROUTE)");
347 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
348 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
349 bb_perror_msg_and_die("getsockname");
351 if (saddr.sin_family != AF_INET)
352 bb_error_msg_and_die("no IP address configured");
353 src = saddr.sin_addr;
358 me.sll_family = AF_PACKET;
359 //me.sll_ifindex = ifindex; - done before
360 me.sll_protocol = htons(ETH_P_ARP);
361 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
364 socklen_t alen = sizeof(me);
366 if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) {
367 bb_perror_msg_and_die("getsockname");
370 if (me.sll_halen == 0) {
371 bb_error_msg("interface %s is not ARPable (no ll address)", device);
372 return (option_mask32 & DAD ? 0 : 2);
375 memset(he.sll_addr, -1, he.sll_halen);
377 if (!(option_mask32 & QUIET)) {
378 /* inet_ntoa uses static storage, can't use in same printf */
379 printf("ARPING to %s", inet_ntoa(dst));
380 printf(" from %s via %s\n", inet_ntoa(src), device);
386 memset(&sa, 0, sizeof(sa));
387 sa.sa_flags = SA_RESTART;
389 sa.sa_handler = (void (*)(int)) finish;
390 sigaction(SIGINT, &sa, NULL);
392 sa.sa_handler = (void (*)(int)) catcher;
393 sigaction(SIGALRM, &sa, NULL);
398 packet = xmalloc(4096);
400 sigset_t sset, osset;
401 struct sockaddr_ll from;
402 socklen_t alen = sizeof(from);
405 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
407 bb_perror_msg("recvfrom");
411 sigaddset(&sset, SIGALRM);
412 sigaddset(&sset, SIGINT);
413 sigprocmask(SIG_BLOCK, &sset, &osset);
414 recv_pack(packet, cc, &from);
415 sigprocmask(SIG_SETMASK, &osset, NULL);