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 )
55 static int send_pack(int sock, struct in_addr *src_addr,
56 struct in_addr *dst_addr, struct sockaddr_ll *ME,
57 struct sockaddr_ll *HE)
61 RESERVE_CONFIG_UBUFFER(buf, 256);
62 struct arphdr *ah = (struct arphdr *) buf;
63 unsigned char *p = (unsigned char *) (ah + 1);
65 ah->ar_hrd = htons(ME->sll_hatype);
66 ah->ar_hrd = htons(ARPHRD_ETHER);
67 ah->ar_pro = htons(ETH_P_IP);
68 ah->ar_hln = ME->sll_halen;
70 ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
72 memcpy(p, &ME->sll_addr, ah->ar_hln);
75 memcpy(p, src_addr, 4);
79 memcpy(p, &ME->sll_addr, ah->ar_hln);
81 memcpy(p, &HE->sll_addr, ah->ar_hln);
84 memcpy(p, dst_addr, 4);
87 gettimeofday(&now, NULL);
88 err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
92 if (!(cfg&unicasting))
95 RELEASE_CONFIG_BUFFER(buf);
99 static void finish(void)
102 printf("Sent %d probes (%d broadcast(s))\n"
103 "Received %d repl%s",
105 received, (received > 1) ? "ies" : "y");
106 if (brd_recv || req_recv) {
109 printf("%d request(s)", req_recv);
111 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
124 static void catcher(void)
127 static struct timeval start;
129 gettimeofday(&tv, NULL);
131 if (start.tv_sec == 0)
135 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
138 if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
139 send_pack(s, &src, &dst, &me, &he);
140 if (count == 0 && cfg&unsolicited)
146 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
148 struct arphdr *ah = (struct arphdr *) buf;
149 unsigned char *p = (unsigned char *) (ah + 1);
150 struct in_addr src_ip, dst_ip;
152 /* Filter out wild packets */
153 if (FROM->sll_pkttype != PACKET_HOST &&
154 FROM->sll_pkttype != PACKET_BROADCAST &&
155 FROM->sll_pkttype != PACKET_MULTICAST)
158 /* Only these types are recognised */
159 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
162 /* ARPHRD check and this darned FDDI hack here :-( */
163 if (ah->ar_hrd != htons(FROM->sll_hatype) &&
164 (FROM->sll_hatype != ARPHRD_FDDI
165 || ah->ar_hrd != htons(ARPHRD_ETHER)))
168 /* Protocol must be IP. */
169 if (ah->ar_pro != htons(ETH_P_IP))
173 if (ah->ar_hln != me.sll_halen)
175 if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
177 memcpy(&src_ip, p + ah->ar_hln, 4);
178 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
180 if (src_ip.s_addr != dst.s_addr)
182 if (src.s_addr != dst_ip.s_addr)
184 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
188 src_ip = 0 (or some src)
190 dst_ip = tested address
193 We fail, if receive request/reply with:
194 src_ip = tested_address
196 if src_ip in request was not zero, check
197 also that it matches to dst_ip, otherwise
198 dst_ip/dst_hw do not matter.
200 if (src_ip.s_addr != dst.s_addr)
202 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
204 if (src.s_addr && src.s_addr != dst_ip.s_addr)
211 gettimeofday(&tv, NULL);
213 printf("%s %s from %s [%s]",
214 FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
215 ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
217 ether_ntoa((struct ether_addr *) p));
218 if (dst_ip.s_addr != src.s_addr) {
219 printf("for %s ", inet_ntoa(dst_ip));
222 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
226 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
230 long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
231 tv.tv_usec - last.tv_usec;
232 long msecs = (usecs + 500) / 1000;
234 usecs -= msecs * 1000 - 500;
235 printf(" %ld.%03ldms\n", msecs, usecs);
237 printf(" UNSOLICITED?\n");
242 if (FROM->sll_pkttype != PACKET_HOST)
244 if (ah->ar_op == htons(ARPOP_REQUEST))
246 if (cfg&quit_on_reply)
248 if (!(cfg&broadcast_only)) {
249 memcpy(he.sll_addr, p, me.sll_halen);
255 int arping_main(int argc, char **argv)
257 char *device = "eth0";
262 s = socket(PF_PACKET, SOCK_DGRAM, 0);
269 char *_count, *_timeout, *_device;
271 /* Dad also sets quit_on_reply.
272 * Advert also sets unsolicited.
274 bb_opt_complementally = "Df:AU";
275 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
276 &_count, &_timeout, &_device);
277 cfg |= opt & 63; /* set respective flags */
278 if (opt & 64) /* count */
279 count = atoi(_count);
280 if (opt & 128) /* timeout */
281 timeout = atoi(_timeout);
282 if (opt & 256) { /* interface */
283 if (strlen(_device) > IF_NAMESIZE) {
284 bb_error_msg_and_die("Interface name `%s' must be less than %d",
285 _device, IF_NAMESIZE);
289 if (opt & 512) /* source */
302 bb_default_error_retval = ifindex;
303 bb_perror_msg_and_die("socket");
305 bb_default_error_retval = 2;
310 memset(&ifr, 0, sizeof(ifr));
311 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
312 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
313 bb_error_msg_and_die("Interface %s not found", device);
315 ifindex = ifr.ifr_ifindex;
317 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
318 bb_error_msg_and_die("SIOCGIFFLAGS");
320 if (!(ifr.ifr_flags & IFF_UP)) {
321 bb_error_msg_and_die("Interface %s is down", device);
323 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
324 bb_error_msg("Interface %s is not ARPable", device);
325 exit(cfg&dad ? 0 : 2);
329 if (!inet_aton(target, &dst)) {
332 hp = gethostbyname2(target, AF_INET);
334 bb_error_msg_and_die("invalid or unknown target %s", target);
336 memcpy(&dst, hp->h_addr, 4);
339 if (source && !inet_aton(source, &src)) {
340 bb_error_msg_and_die("invalid source address %s", source);
343 if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
346 if (!(cfg&dad) || src.s_addr) {
347 struct sockaddr_in saddr;
348 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
351 bb_error_msg_and_die("socket");
355 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
356 strlen(device) + 1) == -1)
357 bb_error_msg("WARNING: interface %s is ignored", device);
359 memset(&saddr, 0, sizeof(saddr));
360 saddr.sin_family = AF_INET;
362 saddr.sin_addr = src;
363 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
364 bb_error_msg_and_die("bind");
366 } else if (!(cfg&dad)) {
368 socklen_t alen = sizeof(saddr);
370 saddr.sin_port = htons(1025);
371 saddr.sin_addr = dst;
374 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
376 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
377 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
379 bb_error_msg_and_die("connect");
381 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
383 bb_error_msg_and_die("getsockname");
385 src = saddr.sin_addr;
390 me.sll_family = AF_PACKET;
391 me.sll_ifindex = ifindex;
392 me.sll_protocol = htons(ETH_P_ARP);
393 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
394 bb_error_msg_and_die("bind");
398 socklen_t alen = sizeof(me);
400 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
401 bb_error_msg_and_die("getsockname");
404 if (me.sll_halen == 0) {
405 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
406 exit(cfg&dad ? 0 : 2);
409 memset(he.sll_addr, -1, he.sll_halen);
412 printf("ARPING to %s from %s via %s\n",
413 inet_ntoa(dst), inet_ntoa(src),
414 device ? device : "unknown");
417 if (!src.s_addr && !(cfg&dad)) {
418 bb_error_msg_and_die("no src address in the non-DAD mode");
424 memset(&sa, 0, sizeof(sa));
425 sa.sa_flags = SA_RESTART;
427 sa.sa_handler = (void (*)(int)) finish;
428 sigaction(SIGINT, &sa, NULL);
430 sa.sa_handler = (void (*)(int)) catcher;
431 sigaction(SIGALRM, &sa, NULL);
437 sigset_t sset, osset;
438 RESERVE_CONFIG_UBUFFER(packet, 4096);
439 struct sockaddr_ll from;
440 socklen_t alen = sizeof(from);
443 if ((cc = recvfrom(s, packet, 4096, 0,
444 (struct sockaddr *) &from, &alen)) < 0) {
449 sigaddset(&sset, SIGALRM);
450 sigaddset(&sset, SIGINT);
451 sigprocmask(SIG_BLOCK, &sset, &osset);
452 recv_pack(packet, cc, &from);
453 sigprocmask(SIG_SETMASK, &osset, NULL);
454 RELEASE_CONFIG_BUFFER(packet);