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>
19 #include <arpa/inet.h>
21 #include <netinet/ether.h>
22 #include <netpacket/packet.h>
26 static struct in_addr src;
27 static struct in_addr dst;
28 static struct sockaddr_ll me;
29 static struct sockaddr_ll he;
30 static struct timeval last;
44 static int count = -1;
53 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
54 ((tv1).tv_usec-(tv2).tv_usec)/1000 )
56 static void set_signal(int signo, void (*handler) (void))
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = (void (*)(int)) handler;
62 sa.sa_flags = SA_RESTART;
63 sigaction(signo, &sa, NULL);
67 static int send_pack(int sock, struct in_addr *src_addr,
68 struct in_addr *dst_addr, struct sockaddr_ll *ME,
69 struct sockaddr_ll *HE)
73 RESERVE_CONFIG_UBUFFER(buf, 256);
74 struct arphdr *ah = (struct arphdr *) buf;
75 unsigned char *p = (unsigned char *) (ah + 1);
77 ah->ar_hrd = htons(ME->sll_hatype);
78 ah->ar_hrd = htons(ARPHRD_ETHER);
79 ah->ar_pro = htons(ETH_P_IP);
80 ah->ar_hln = ME->sll_halen;
82 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
84 memcpy(p, &ME->sll_addr, ah->ar_hln);
87 memcpy(p, src_addr, 4);
91 memcpy(p, &ME->sll_addr, ah->ar_hln);
93 memcpy(p, &HE->sll_addr, ah->ar_hln);
96 memcpy(p, dst_addr, 4);
99 gettimeofday(&now, NULL);
100 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
101 if (err == p - buf) {
104 if (!(cfg&unicasting))
107 RELEASE_CONFIG_BUFFER(buf);
111 static void finish(void)
114 printf("Sent %d probes (%d broadcast(s))\n"
115 "Received %d repl%s",
117 received, (received > 1) ? "ies" : "y");
118 if (brd_recv || req_recv) {
121 printf("%d request(s)", req_recv);
123 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
136 static void catcher(void)
139 static struct timeval start;
141 gettimeofday(&tv, NULL);
143 if (start.tv_sec == 0)
147 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
150 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
151 send_pack(s, &src, &dst, &me, &he);
152 if (count == 0 && cfg&unsolicited)
158 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
160 struct arphdr *ah = (struct arphdr *) buf;
161 unsigned char *p = (unsigned char *) (ah + 1);
162 struct in_addr src_ip, dst_ip;
164 /* Filter out wild packets */
165 if (FROM->sll_pkttype != PACKET_HOST &&
166 FROM->sll_pkttype != PACKET_BROADCAST &&
167 FROM->sll_pkttype != PACKET_MULTICAST)
170 /* Only these types are recognised */
171 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
174 /* ARPHRD check and this darned FDDI hack here :-( */
175 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
176 (FROM->sll_hatype != ARPHRD_FDDI
177 || ah->ar_hrd != htons(ARPHRD_ETHER)))
180 /* Protocol must be IP. */
181 if (ah->ar_pro != htons(ETH_P_IP))
185 if (ah->ar_hln != me.sll_halen)
187 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
189 memcpy(&src_ip, p + ah->ar_hln, 4);
190 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
192 if (src_ip.s_addr != dst.s_addr)
194 if (src.s_addr != dst_ip.s_addr)
196 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
200 src_ip = 0 (or some src)
202 dst_ip = tested address
205 We fail, if receive request/reply with:
206 src_ip = tested_address
208 if src_ip in request was not zero, check
209 also that it matches to dst_ip, otherwise
210 dst_ip/dst_hw do not matter.
212 if (src_ip.s_addr != dst.s_addr)
214 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
216 if (src.s_addr && src.s_addr != dst_ip.s_addr)
223 gettimeofday(&tv, NULL);
225 printf("%s %s from %s [%s]",
226 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
227 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
229 ether_ntoa((struct ether_addr *) p));
230 if (dst_ip.s_addr != src.s_addr) {
231 printf("for %s ", inet_ntoa(dst_ip));
234 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
238 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
242 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
243 tv.tv_usec - last.tv_usec;
244 long msecs = (usecs + 500) / 1000;
246 usecs -= msecs * 1000 - 500;
247 printf(" %ld.%03ldms\n", msecs, usecs);
249 printf(" UNSOLICITED?\n");
254 if (FROM->sll_pkttype != PACKET_HOST)
256 if (ah->ar_op == htons(ARPOP_REQUEST))
258 if (cfg&quit_on_reply)
260 if (!(cfg&broadcast_only)) {
261 memcpy(he.sll_addr, p, me.sll_halen);
267 int arping_main(int argc, char **argv)
269 char *device = "eth0";
274 s = socket(PF_PACKET, SOCK_DGRAM, 0);
281 char *_count, *_timeout, *_device;
283 /* Dad also sets quit_on_reply.
284 * Advert also sets unsolicited.
286 bb_opt_complementally = "Df:AU";
287 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
288 &_count, &_timeout, &_device);
289 cfg |= opt & 63; /* set respective flags */
290 if (opt & 64) /* count */
291 count = atoi(_count);
292 if (opt & 128) /* timeout */
293 timeout = atoi(_timeout);
294 if (opt & 256) { /* interface */
295 if (strlen(_device) > IF_NAMESIZE) {
296 bb_error_msg_and_die("Interface name `%s' must be less than %d",
297 _device, IF_NAMESIZE);
301 if (opt & 512) /* source */
314 bb_default_error_retval = ifindex;
315 bb_perror_msg_and_die("socket");
317 bb_default_error_retval = 2;
322 memset(&ifr, 0, sizeof(ifr));
323 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
324 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
325 bb_error_msg_and_die("Interface %s not found", device);
327 ifindex = ifr.ifr_ifindex;
329 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
330 bb_error_msg_and_die("SIOCGIFFLAGS");
332 if (!(ifr.ifr_flags & IFF_UP)) {
333 bb_error_msg_and_die("Interface %s is down", device);
335 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
336 bb_error_msg("Interface %s is not ARPable", device);
337 exit(cfg&dad ? 0 : 2);
341 if (!inet_aton(target, &dst)) {
344 hp = gethostbyname2(target, AF_INET);
346 bb_error_msg_and_die("invalid or unknown target %s", target);
348 memcpy(&dst, hp->h_addr, 4);
351 if (source && !inet_aton(source, &src)) {
352 bb_error_msg_and_die("invalid source address %s", source);
355 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
358 if (!(cfg&dad) || src.s_addr) {
359 struct sockaddr_in saddr;
360 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
363 bb_error_msg_and_die("socket");
367 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
368 strlen(device) + 1) == -1)
369 bb_error_msg("WARNING: interface %s is ignored", device);
371 memset(&saddr, 0, sizeof(saddr));
372 saddr.sin_family = AF_INET;
374 saddr.sin_addr = src;
375 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
376 bb_error_msg_and_die("bind");
378 } else if (!(cfg&dad)) {
380 socklen_t alen = sizeof(saddr);
382 saddr.sin_port = htons(1025);
383 saddr.sin_addr = dst;
386 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
388 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
389 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
391 bb_error_msg_and_die("connect");
393 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
395 bb_error_msg_and_die("getsockname");
397 src = saddr.sin_addr;
402 me.sll_family = AF_PACKET;
403 me.sll_ifindex = ifindex;
404 me.sll_protocol = htons(ETH_P_ARP);
405 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
406 bb_error_msg_and_die("bind");
410 socklen_t alen = sizeof(me);
412 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
413 bb_error_msg_and_die("getsockname");
416 if (me.sll_halen == 0) {
417 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
418 exit(cfg&dad ? 0 : 2);
421 memset(he.sll_addr, -1, he.sll_halen);
424 printf("ARPING to %s from %s via %s\n",
425 inet_ntoa(dst), inet_ntoa(src),
426 device ? device : "unknown");
429 if (!src.s_addr && !(cfg&dad)) {
430 bb_error_msg_and_die("no src address in the non-DAD mode");
436 memset(&sa, 0, sizeof(sa));
437 sa.sa_flags = SA_RESTART;
439 sa.sa_handler = (void (*)(int)) finish;
440 sigaction(SIGINT, &sa, NULL);
442 sa.sa_handler = (void (*)(int)) catcher;
443 sigaction(SIGALRM, &sa, NULL);
449 sigset_t sset, osset;
450 RESERVE_CONFIG_UBUFFER(packet, 4096);
451 struct sockaddr_ll from;
452 socklen_t alen = sizeof(from);
455 if ((cc = recvfrom(s, packet, 4096, 0,
456 (struct sockaddr *) &from, &alen)) < 0) {
461 sigaddset(&sset, SIGALRM);
462 sigaddset(&sset, SIGINT);
463 sigprocmask(SIG_BLOCK, &sset, &osset);
464 recv_pack(packet, cc, &from);
465 sigprocmask(SIG_SETMASK, &osset, NULL);
466 RELEASE_CONFIG_BUFFER(packet);