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 // If GNUisms are not available...
70 //static void *mempcpy(void *_dst, const void *_src, int n)
72 // memcpy(_dst, _src, n);
73 // return (char*)_dst + n;
76 static int send_pack(struct in_addr *src_addr,
77 struct in_addr *dst_addr, struct sockaddr_ll *ME,
78 struct sockaddr_ll *HE)
81 unsigned char buf[256];
82 struct arphdr *ah = (struct arphdr *) buf;
83 unsigned char *p = (unsigned char *) (ah + 1);
85 ah->ar_hrd = htons(ARPHRD_ETHER);
86 ah->ar_pro = htons(ETH_P_IP);
87 ah->ar_hln = ME->sll_halen;
89 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
91 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
92 p = mempcpy(p, src_addr, 4);
94 if (option_mask32 & ADVERT)
95 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
97 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
99 p = mempcpy(p, dst_addr, 4);
101 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
102 if (err == p - buf) {
103 last = MONOTONIC_US();
105 if (!(option_mask32 & UNICASTING))
111 static void finish(void) ATTRIBUTE_NORETURN;
112 static void finish(void)
114 if (!(option_mask32 & QUIET)) {
115 printf("Sent %u probe(s) (%u broadcast(s))\n"
117 " (%u request(s), %u broadcast(s))\n",
119 received, (received == 1) ? "ies" : "y",
122 if (option_mask32 & DAD)
124 if (option_mask32 & UNSOLICITED)
129 static void catcher(void)
133 now = MONOTONIC_US();
137 if (count == 0 || (timeout_us && (now - start) > timeout_us))
140 /* count < 0 means "infinite count" */
144 if (last == 0 || (now - last) > 500000) {
145 send_pack(&src, &dst, &me, &he);
146 if (count == 0 && (option_mask32 & UNSOLICITED))
152 static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
154 struct arphdr *ah = (struct arphdr *) buf;
155 unsigned char *p = (unsigned char *) (ah + 1);
156 struct in_addr src_ip, dst_ip;
158 /* Filter out wild packets */
159 if (FROM->sll_pkttype != PACKET_HOST
160 && FROM->sll_pkttype != PACKET_BROADCAST
161 && FROM->sll_pkttype != PACKET_MULTICAST)
164 /* Only these types are recognised */
165 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
168 /* ARPHRD check and this darned FDDI hack here :-( */
169 if (ah->ar_hrd != htons(FROM->sll_hatype)
170 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
173 /* Protocol must be IP. */
174 if (ah->ar_pro != htons(ETH_P_IP)
176 || (ah->ar_hln != me.sll_halen)
177 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
180 memcpy(&src_ip, p + ah->ar_hln, 4);
181 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
183 if (dst.s_addr != src_ip.s_addr)
185 if (!(option_mask32 & DAD)) {
186 if ((src.s_addr != dst_ip.s_addr)
187 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
191 src_ip = 0 (or some src)
193 dst_ip = tested address
196 We fail, if receive request/reply with:
197 src_ip = tested_address
199 if src_ip in request was not zero, check
200 also that it matches to dst_ip, otherwise
201 dst_ip/dst_hw do not matter.
203 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
204 || (src.s_addr && src.s_addr != dst_ip.s_addr))
207 if (!(option_mask32 & QUIET)) {
210 printf("%scast re%s from %s [%s]",
211 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
212 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
214 ether_ntoa((struct ether_addr *) p));
215 if (dst_ip.s_addr != src.s_addr) {
216 printf("for %s ", inet_ntoa(dst_ip));
219 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
223 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
227 unsigned diff = MONOTONIC_US() - last;
228 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
230 printf(" UNSOLICITED?\n");
235 if (FROM->sll_pkttype != PACKET_HOST)
237 if (ah->ar_op == htons(ARPOP_REQUEST))
239 if (option_mask32 & QUIT_ON_REPLY)
241 if (!(option_mask32 & BCAST_ONLY)) {
242 memcpy(he.sll_addr, p, me.sll_halen);
243 option_mask32 |= UNICASTING;
248 int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
249 int arping_main(int argc ATTRIBUTE_UNUSED, char **argv)
251 const char *device = "eth0";
254 unsigned char *packet;
259 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
261 // Drop suid root privileges
262 // Need to remove SUID_NEVER from applets.h for this to work
265 err_str = xasprintf("interface %s %%s", device);
270 /* Dad also sets quit_on_reply.
271 * Advert also sets unsolicited.
273 opt_complementary = "=1:Df:AU:c+";
274 opt = getopt32(argv, "DUAqfbc:w:I:s:",
275 &count, &str_timeout, &device, &source);
276 if (opt & 0x80) /* -w: timeout */
277 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
278 //if (opt & 0x200) /* -s: source */
279 option_mask32 &= 0x3f; /* set respective flags */
282 target = argv[optind];
284 xfunc_error_retval = 2;
289 memset(&ifr, 0, sizeof(ifr));
290 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
291 /* We use ifr.ifr_name in error msg so that problem
292 * with truncated name will be visible */
293 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
294 me.sll_ifindex = ifr.ifr_ifindex;
296 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
298 if (!(ifr.ifr_flags & IFF_UP)) {
299 bb_error_msg_and_die(err_str, "is down");
301 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
302 bb_error_msg(err_str, "is not ARPable");
303 return (option_mask32 & DAD ? 0 : 2);
307 /* if (!inet_aton(target, &dst)) - not needed */ {
308 len_and_sockaddr *lsa;
309 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
310 memcpy(&dst, &lsa->u.sin.sin_addr.s_addr, 4);
311 if (ENABLE_FEATURE_CLEAN_UP)
315 if (source && !inet_aton(source, &src)) {
316 bb_error_msg_and_die("invalid source address %s", source);
319 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
322 if (!(option_mask32 & DAD) || src.s_addr) {
323 struct sockaddr_in saddr;
324 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
326 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
327 bb_perror_msg("cannot bind to device %s", device);
328 memset(&saddr, 0, sizeof(saddr));
329 saddr.sin_family = AF_INET;
331 /* Check that this is indeed our IP */
332 saddr.sin_addr = src;
333 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
334 } else { /* !(option_mask32 & DAD) case */
335 /* Find IP address on this iface */
336 socklen_t alen = sizeof(saddr);
338 saddr.sin_port = htons(1025);
339 saddr.sin_addr = dst;
341 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
342 bb_perror_msg("setsockopt(SO_DONTROUTE)");
343 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
344 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
345 bb_perror_msg_and_die("getsockname");
347 if (saddr.sin_family != AF_INET)
348 bb_error_msg_and_die("no IP address configured");
349 src = saddr.sin_addr;
354 me.sll_family = AF_PACKET;
355 //me.sll_ifindex = ifindex; - done before
356 me.sll_protocol = htons(ETH_P_ARP);
357 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
360 socklen_t alen = sizeof(me);
362 if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) {
363 bb_perror_msg_and_die("getsockname");
366 if (me.sll_halen == 0) {
367 bb_error_msg(err_str, "is not ARPable (no ll address)");
368 return (option_mask32 & DAD ? 0 : 2);
371 memset(he.sll_addr, -1, he.sll_halen);
373 if (!(option_mask32 & QUIET)) {
374 /* inet_ntoa uses static storage, can't use in same printf */
375 printf("ARPING to %s", inet_ntoa(dst));
376 printf(" from %s via %s\n", inet_ntoa(src), device);
379 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
380 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
384 packet = xmalloc(4096);
386 sigset_t sset, osset;
387 struct sockaddr_ll from;
388 socklen_t alen = sizeof(from);
391 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
393 bb_perror_msg("recvfrom");
397 sigaddset(&sset, SIGALRM);
398 sigaddset(&sset, SIGINT);
399 sigprocmask(SIG_BLOCK, &sset, &osset);
400 recv_pack(packet, cc, &from);
401 sigprocmask(SIG_SETMASK, &osset, NULL);