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>
20 #include <arpa/inet.h>
22 #include <netinet/ether.h>
23 #include <netpacket/packet.h>
27 static struct in_addr src;
28 static struct in_addr dst;
29 static struct sockaddr_ll me;
30 static struct sockaddr_ll he;
31 static struct timeval last;
45 static int count = -1;
54 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
55 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
57 static void set_signal(int signo, void (*handler) (void))
61 memset(&sa, 0, sizeof(sa));
62 sa.sa_handler = (void (*)(int)) handler;
63 sa.sa_flags = SA_RESTART;
64 sigaction(signo, &sa, NULL);
68 static int send_pack(int sock, struct in_addr *src_addr,
69 struct in_addr *dst_addr, struct sockaddr_ll *ME,
70 struct sockaddr_ll *HE)
74 RESERVE_CONFIG_UBUFFER(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 = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
85 memcpy(p, &ME->sll_addr, ah->ar_hln);
88 memcpy(p, src_addr, 4);
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 gettimeofday(&now, NULL);
101 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
102 if (err == p - buf) {
105 if (!(cfg&unicasting))
108 RELEASE_CONFIG_BUFFER(buf);
112 static void finish(void)
115 printf("Sent %d probes (%d broadcast(s))\n"
116 "Received %d repl%s",
118 received, (received > 1) ? "ies" : "y");
119 if (brd_recv || req_recv) {
122 printf("%d request(s)", req_recv);
124 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
137 static void catcher(void)
140 static struct timeval start;
142 gettimeofday(&tv, NULL);
144 if (start.tv_sec == 0)
148 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
151 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
152 send_pack(s, &src, &dst, &me, &he);
153 if (count == 0 && cfg&unsolicited)
159 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
161 struct arphdr *ah = (struct arphdr *) buf;
162 unsigned char *p = (unsigned char *) (ah + 1);
163 struct in_addr src_ip, dst_ip;
165 /* Filter out wild packets */
166 if (FROM->sll_pkttype != PACKET_HOST &&
167 FROM->sll_pkttype != PACKET_BROADCAST &&
168 FROM->sll_pkttype != PACKET_MULTICAST)
171 /* Only these types are recognised */
172 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
175 /* ARPHRD check and this darned FDDI hack here :-( */
176 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
177 (FROM->sll_hatype != ARPHRD_FDDI
178 || ah->ar_hrd != htons(ARPHRD_ETHER)))
181 /* Protocol must be IP. */
182 if (ah->ar_pro != htons(ETH_P_IP))
186 if (ah->ar_hln != me.sll_halen)
188 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
190 memcpy(&src_ip, p + ah->ar_hln, 4);
191 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
193 if (src_ip.s_addr != dst.s_addr)
195 if (src.s_addr != dst_ip.s_addr)
197 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
201 src_ip = 0 (or some src)
203 dst_ip = tested address
206 We fail, if receive request/reply with:
207 src_ip = tested_address
209 if src_ip in request was not zero, check
210 also that it matches to dst_ip, otherwise
211 dst_ip/dst_hw do not matter.
213 if (src_ip.s_addr != dst.s_addr)
215 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
217 if (src.s_addr && src.s_addr != dst_ip.s_addr)
224 gettimeofday(&tv, NULL);
226 printf("%s %s from %s [%s]",
227 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
228 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
230 ether_ntoa((struct ether_addr *) p));
231 if (dst_ip.s_addr != src.s_addr) {
232 printf("for %s ", inet_ntoa(dst_ip));
235 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
239 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
243 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
244 tv.tv_usec - last.tv_usec;
245 long msecs = (usecs + 500) / 1000;
247 usecs -= msecs * 1000 - 500;
248 printf(" %ld.%03ldms\n", msecs, usecs);
250 printf(" UNSOLICITED?\n");
255 if (FROM->sll_pkttype != PACKET_HOST)
257 if (ah->ar_op == htons(ARPOP_REQUEST))
259 if (cfg&quit_on_reply)
261 if (!(cfg&broadcast_only)) {
262 memcpy(he.sll_addr, p, me.sll_halen);
268 int arping_main(int argc, char **argv)
270 char *device = "eth0";
275 s = socket(PF_PACKET, SOCK_DGRAM, 0);
282 char *_count, *_timeout, *_device;
284 /* Dad also sets quit_on_reply.
285 * Advert also sets unsolicited.
287 bb_opt_complementally = "Df:AU";
288 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
289 &_count, &_timeout, &_device);
290 cfg |= opt & 63; /* set respective flags */
291 if (opt & 64) /* count */
292 count = atoi(_count);
293 if (opt & 128) /* timeout */
294 timeout = atoi(_timeout);
295 if (opt & 256) { /* interface */
296 if (bb_strlen(_device) > IF_NAMESIZE) {
297 bb_error_msg_and_die("Interface name `%s' must be less than %d",
298 _device, IF_NAMESIZE);
302 if (opt & 512) /* source */
315 bb_default_error_retval = ifindex;
316 bb_perror_msg_and_die("socket");
318 bb_default_error_retval = 2;
323 memset(&ifr, 0, sizeof(ifr));
324 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
325 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
326 bb_error_msg_and_die("Interface %s not found", device);
328 ifindex = ifr.ifr_ifindex;
330 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
331 bb_error_msg_and_die("SIOCGIFFLAGS");
333 if (!(ifr.ifr_flags & IFF_UP)) {
334 bb_error_msg_and_die("Interface %s is down", device);
336 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
337 bb_error_msg("Interface %s is not ARPable", device);
338 exit(cfg&dad ? 0 : 2);
342 if (!inet_aton(target, &dst)) {
345 hp = gethostbyname2(target, AF_INET);
347 bb_error_msg_and_die("invalid or unknown target %s", target);
349 memcpy(&dst, hp->h_addr, 4);
352 if (source && !inet_aton(source, &src)) {
353 bb_error_msg_and_die("invalid source address %s", source);
356 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
359 if (!(cfg&dad) || src.s_addr) {
360 struct sockaddr_in saddr;
361 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
364 bb_error_msg_and_die("socket");
368 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
369 strlen(device) + 1) == -1)
370 bb_error_msg("WARNING: interface %s is ignored", device);
372 memset(&saddr, 0, sizeof(saddr));
373 saddr.sin_family = AF_INET;
375 saddr.sin_addr = src;
376 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
377 bb_error_msg_and_die("bind");
379 } else if (!(cfg&dad)) {
381 socklen_t alen = sizeof(saddr);
383 saddr.sin_port = htons(1025);
384 saddr.sin_addr = dst;
387 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
389 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
390 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
392 bb_error_msg_and_die("connect");
394 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
396 bb_error_msg_and_die("getsockname");
398 src = saddr.sin_addr;
403 me.sll_family = AF_PACKET;
404 me.sll_ifindex = ifindex;
405 me.sll_protocol = htons(ETH_P_ARP);
406 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
407 bb_error_msg_and_die("bind");
411 socklen_t alen = sizeof(me);
413 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
414 bb_error_msg_and_die("getsockname");
417 if (me.sll_halen == 0) {
418 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
419 exit(cfg&dad ? 0 : 2);
422 memset(he.sll_addr, -1, he.sll_halen);
425 printf("ARPING to %s from %s via %s\n",
426 inet_ntoa(dst), inet_ntoa(src),
427 device ? device : "unknown");
430 if (!src.s_addr && !(cfg&dad)) {
431 bb_error_msg_and_die("no src address in the non-DAD mode");
437 memset(&sa, 0, sizeof(sa));
438 sa.sa_flags = SA_RESTART;
440 sa.sa_handler = (void (*)(int)) finish;
441 sigaction(SIGINT, &sa, NULL);
443 sa.sa_handler = (void (*)(int)) catcher;
444 sigaction(SIGALRM, &sa, NULL);
450 sigset_t sset, osset;
451 RESERVE_CONFIG_UBUFFER(packet, 4096);
452 struct sockaddr_ll from;
453 socklen_t alen = sizeof(from);
456 if ((cc = recvfrom(s, packet, 4096, 0,
457 (struct sockaddr *) &from, &alen)) < 0) {
462 sigaddset(&sset, SIGALRM);
463 sigaddset(&sset, SIGINT);
464 sigprocmask(SIG_BLOCK, &sset, &osset);
465 recv_pack(packet, cc, &from);
466 sigprocmask(SIG_SETMASK, &osset, NULL);
467 RELEASE_CONFIG_BUFFER(packet);