1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
9 //config: bool "arping (9.3 kb)"
11 //config: select PLATFORM_LINUX
13 //config: Ping hosts by ARP packets.
16 //applet:IF_ARPING(APPLET(arping, BB_DIR_USR_SBIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_ARPING) += arping.o
20 //usage:#define arping_trivial_usage
21 //usage: "[-fqbDUA] [-c CNT] [-w TIMEOUT] [-I IFACE] [-s SRC_IP] DST_IP"
22 //usage:#define arping_full_usage "\n\n"
23 //usage: "Send ARP requests/replies\n"
24 //usage: "\n -f Quit on first ARP reply"
25 //usage: "\n -q Quiet"
26 //usage: "\n -b Keep broadcasting, don't go unicast"
27 //usage: "\n -D Exit with 1 if DST_IP replies"
28 //usage: "\n -U Unsolicited ARP mode, update your neighbors"
29 //usage: "\n -A ARP answer mode, update your neighbors"
30 //usage: "\n -c N Stop after sending N ARP requests"
31 //usage: "\n -w TIMEOUT Seconds to wait for ARP reply"
32 //usage: "\n -I IFACE Interface to use (default eth0)"
33 //usage: "\n -s SRC_IP Sender IP address"
34 //usage: "\n DST_IP Target IP address"
36 #include <arpa/inet.h>
38 #include <netinet/ether.h>
39 #include <netpacket/packet.h>
42 #include "common_bufsiz.h"
44 /* We don't expect to see 1000+ seconds delay, unsigned is enough */
45 #define MONOTONIC_US() ((unsigned)monotonic_us())
60 struct sockaddr_ll me;
61 struct sockaddr_ll he;
75 #define G (*(struct globals*)bb_common_bufsiz1)
80 #define sock_fd (G.sock_fd )
81 #define count (G.count )
82 #define last (G.last )
83 #define timeout_us (G.timeout_us)
84 #define start (G.start )
85 #define sent (G.sent )
86 #define brd_sent (G.brd_sent )
87 #define received (G.received )
88 #define brd_recv (G.brd_recv )
89 #define req_recv (G.req_recv )
90 #define INIT_G() do { \
91 setup_common_bufsiz(); \
95 // If GNUisms are not available...
96 //static void *mempcpy(void *_dst, const void *_src, int n)
98 // memcpy(_dst, _src, n);
99 // return (char*)_dst + n;
102 static int send_pack(struct in_addr *src_addr,
103 struct in_addr *dst_addr, struct sockaddr_ll *ME,
104 struct sockaddr_ll *HE)
107 unsigned char buf[256];
108 struct arphdr *ah = (struct arphdr *) buf;
109 unsigned char *p = (unsigned char *) (ah + 1);
111 ah->ar_hrd = htons(ARPHRD_ETHER);
112 ah->ar_pro = htons(ETH_P_IP);
113 ah->ar_hln = ME->sll_halen;
115 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
117 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
118 p = mempcpy(p, src_addr, 4);
120 if (option_mask32 & ADVERT)
121 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
123 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
125 p = mempcpy(p, dst_addr, 4);
127 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
128 if (err == p - buf) {
129 last = MONOTONIC_US();
131 if (!(option_mask32 & UNICASTING))
137 static void finish(void) NORETURN;
138 static void finish(void)
140 if (!(option_mask32 & QUIET)) {
141 printf("Sent %u probe(s) (%u broadcast(s))\n"
143 " (%u request(s), %u broadcast(s))\n",
145 received, (received == 1) ? "ies" : "y",
148 if (option_mask32 & DAD)
150 if (option_mask32 & UNSOLICITED)
155 static void catcher(void)
159 now = MONOTONIC_US();
163 if (count == 0 || (timeout_us && (now - start) > timeout_us))
166 /* count < 0 means "infinite count" */
170 if (last == 0 || (now - last) > 500000) {
171 send_pack(&src, &dst, &me, &he);
172 if (count == 0 && (option_mask32 & UNSOLICITED))
178 static void recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
180 struct arphdr *ah = (struct arphdr *) buf;
181 unsigned char *p = (unsigned char *) (ah + 1);
182 struct in_addr src_ip, dst_ip;
183 /* moves below assume in_addr is 4 bytes big, ensure that */
184 struct BUG_in_addr_must_be_4 {
185 char BUG_in_addr_must_be_4[
186 sizeof(struct in_addr) == 4 ? 1 : -1
188 char BUG_s_addr_must_be_4[
189 sizeof(src_ip.s_addr) == 4 ? 1 : -1
193 /* Filter out wild packets */
194 if (FROM->sll_pkttype != PACKET_HOST
195 && FROM->sll_pkttype != PACKET_BROADCAST
196 && FROM->sll_pkttype != PACKET_MULTICAST)
199 /* Only these types are recognized */
200 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
203 /* ARPHRD check and this darned FDDI hack here :-( */
204 if (ah->ar_hrd != htons(FROM->sll_hatype)
205 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
208 /* Protocol must be IP. */
209 if (ah->ar_pro != htons(ETH_P_IP)
211 || (ah->ar_hln != me.sll_halen)
212 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
215 move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
216 move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
218 if (dst.s_addr != src_ip.s_addr)
220 if (!(option_mask32 & DAD)) {
221 if ((src.s_addr != dst_ip.s_addr)
222 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
226 src_ip = 0 (or some src)
228 dst_ip = tested address
231 We fail, if receive request/reply with:
232 src_ip = tested_address
234 if src_ip in request was not zero, check
235 also that it matches to dst_ip, otherwise
236 dst_ip/dst_hw do not matter.
238 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
239 || (src.s_addr && src.s_addr != dst_ip.s_addr))
242 if (!(option_mask32 & QUIET)) {
245 printf("%scast re%s from %s [%02x:%02x:%02x:%02x:%02x:%02x]",
246 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
247 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
249 p[0], p[1], p[2], p[3], p[4], p[5]
251 if (dst_ip.s_addr != src.s_addr) {
252 printf("for %s ", inet_ntoa(dst_ip));
255 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
256 unsigned char *pp = p + ah->ar_hln + 4;
259 printf("[%02x:%02x:%02x:%02x:%02x:%02x]",
260 pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]
265 unsigned diff = MONOTONIC_US() - last;
266 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
268 puts(" UNSOLICITED?");
273 if (FROM->sll_pkttype != PACKET_HOST)
275 if (ah->ar_op == htons(ARPOP_REQUEST))
277 if (option_mask32 & QUIT_ON_REPLY)
279 if (!(option_mask32 & BCAST_ONLY)) {
280 memcpy(he.sll_addr, p, me.sll_halen);
281 option_mask32 |= UNICASTING;
285 int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
286 int arping_main(int argc UNUSED_PARAM, char **argv)
288 const char *device = "eth0";
291 unsigned char *packet;
296 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
298 // If you ever change BB_SUID_DROP to BB_SUID_REQUIRE,
299 // drop suid root privileges here:
306 /* Dad also sets quit_on_reply.
307 * Advert also sets unsolicited.
309 opt = getopt32(argv, "^" "DUAqfbc:+w:I:s:" "\0" "=1:Df:AU",
310 &count, &str_timeout, &device, &source
312 if (opt & 0x80) /* -w: timeout */
313 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
314 //if (opt & 0x200) /* -s: source */
315 option_mask32 &= 0x3f; /* set respective flags */
318 target = argv[optind];
319 err_str = xasprintf("interface %s %%s", device);
320 xfunc_error_retval = 2;
325 memset(&ifr, 0, sizeof(ifr));
326 strncpy_IFNAMSIZ(ifr.ifr_name, device);
327 /* We use ifr.ifr_name in error msg so that problem
328 * with truncated name will be visible */
329 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
330 me.sll_ifindex = ifr.ifr_ifindex;
332 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
334 if (!(ifr.ifr_flags & IFF_UP)) {
335 bb_error_msg_and_die(err_str, "is down");
337 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
338 bb_error_msg(err_str, "is not ARPable");
339 return (option_mask32 & DAD ? 0 : 2);
343 /* if (!inet_aton(target, &dst)) - not needed */ {
344 len_and_sockaddr *lsa;
345 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
346 dst = lsa->u.sin.sin_addr;
347 if (ENABLE_FEATURE_CLEAN_UP)
351 if (source && !inet_aton(source, &src)) {
352 bb_error_msg_and_die("invalid source address %s", source);
355 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
358 if (!(option_mask32 & DAD) || src.s_addr) {
359 struct sockaddr_in saddr;
360 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
362 setsockopt_bindtodevice(probe_fd, device);
363 memset(&saddr, 0, sizeof(saddr));
364 saddr.sin_family = AF_INET;
366 /* Check that this is indeed our IP */
367 saddr.sin_addr = src;
368 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
369 } else { /* !(option_mask32 & DAD) case */
370 /* Find IP address on this iface */
371 socklen_t alen = sizeof(saddr);
373 saddr.sin_port = htons(1025);
374 saddr.sin_addr = dst;
376 if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0)
377 bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE");
378 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
379 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
381 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
382 // bb_perror_msg_and_die("getsockname");
383 if (saddr.sin_family != AF_INET)
384 bb_error_msg_and_die("no IP address configured");
385 src = saddr.sin_addr;
390 me.sll_family = AF_PACKET;
391 //me.sll_ifindex = ifindex; - done before
392 me.sll_protocol = htons(ETH_P_ARP);
393 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
396 socklen_t alen = sizeof(me);
397 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
399 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
400 // bb_perror_msg_and_die("getsockname");
402 if (me.sll_halen == 0) {
403 bb_error_msg(err_str, "is not ARPable (no ll address)");
404 return (option_mask32 & DAD ? 0 : 2);
407 memset(he.sll_addr, -1, he.sll_halen);
409 if (!(option_mask32 & QUIET)) {
410 /* inet_ntoa uses static storage, can't use in same printf */
411 printf("ARPING to %s", inet_ntoa(dst));
412 printf(" from %s via %s\n", inet_ntoa(src), device);
415 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
416 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
420 packet = xmalloc(4096);
422 sigset_t sset, osset;
423 struct sockaddr_ll from;
424 socklen_t alen = sizeof(from);
427 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
429 bb_perror_msg("recvfrom");
433 sigaddset(&sset, SIGALRM);
434 sigaddset(&sset, SIGINT);
435 sigprocmask(SIG_BLOCK, &sset, &osset);
436 recv_pack(packet, cc, &from);
437 sigprocmask(SIG_SETMASK, &osset, NULL);