1 /* vi: set sw=4 ts=4: */
3 * Mini ping implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * Adapted from the ping in netkit-base 0.10:
8 * Copyright (c) 1989 The Regents of the University of California.
11 * This code is derived from software contributed to Berkeley by
14 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
19 * This version of ping is adapted from the ping in netkit-base 0.10,
22 * Original copyright notice is retained at the end of this file.
24 * This version is an adaptation of ping.c from busybox.
25 * The code was modified by Bart Visscher <magick@linux-fan.com>
29 #include <netinet/ip_icmp.h>
33 #include <netinet/icmp6.h>
34 /* I see RENUMBERED constants in bits/in.h - !!?
35 * What a fuck is going on with libc? Is it a glibc joke? */
36 #ifdef IPV6_2292HOPLIMIT
38 #define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
47 MAX_DUP_CHK = (8 * 128),
49 PINGINTERVAL = 1, /* 1 second */
54 static int in_cksum(unsigned short *buf, int sz)
58 unsigned short *w = buf;
59 unsigned short ans = 0;
67 *(unsigned char *) (&ans) = *(unsigned char *) w;
71 sum = (sum >> 16) + (sum & 0xFFFF);
77 #if !ENABLE_FEATURE_FANCY_PING
81 static char *hostname;
83 static void noresp(int ign ATTRIBUTE_UNUSED)
85 printf("No response from %s\n", hostname);
89 static void ping4(len_and_sockaddr *lsa)
91 struct sockaddr_in pingaddr;
94 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
96 pingsock = create_icmp_socket();
99 pkt = (struct icmp *) packet;
100 memset(pkt, 0, sizeof(packet));
101 pkt->icmp_type = ICMP_ECHO;
102 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
104 c = xsendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN,
105 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
107 /* listen for replies */
109 struct sockaddr_in from;
110 socklen_t fromlen = sizeof(from);
112 c = recvfrom(pingsock, packet, sizeof(packet), 0,
113 (struct sockaddr *) &from, &fromlen);
116 bb_perror_msg("recvfrom");
119 if (c >= 76) { /* ip + icmp */
120 struct iphdr *iphdr = (struct iphdr *) packet;
122 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
123 if (pkt->icmp_type == ICMP_ECHOREPLY)
127 if (ENABLE_FEATURE_CLEAN_UP)
132 static void ping6(len_and_sockaddr *lsa)
134 struct sockaddr_in6 pingaddr;
135 struct icmp6_hdr *pkt;
138 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
140 pingsock = create_icmp6_socket();
141 pingaddr = lsa->sin6;
143 pkt = (struct icmp6_hdr *) packet;
144 memset(pkt, 0, sizeof(packet));
145 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
147 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
148 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
150 c = xsendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr),
151 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
153 /* listen for replies */
155 struct sockaddr_in6 from;
156 socklen_t fromlen = sizeof(from);
158 c = recvfrom(pingsock, packet, sizeof(packet), 0,
159 (struct sockaddr *) &from, &fromlen);
162 bb_perror_msg("recvfrom");
165 if (c >= 8) { /* icmp6_hdr */
166 pkt = (struct icmp6_hdr *) packet;
167 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
171 if (ENABLE_FEATURE_CLEAN_UP)
176 int ping_main(int argc, char **argv);
177 int ping_main(int argc, char **argv)
179 len_and_sockaddr *lsa;
181 sa_family_t af = AF_UNSPEC;
183 while ((++argv)[0] && argv[0][0] == '-') {
184 if (argv[0][1] == '4') {
188 if (argv[0][1] == '6') {
203 lsa = xhost_and_af2sockaddr(hostname, 0, af);
205 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
207 /* Set timer _after_ DNS resolution */
208 signal(SIGALRM, noresp);
209 alarm(5); /* give the host 5000ms to respond */
212 if (lsa->sa.sa_family == AF_INET6)
217 printf("%s is alive!\n", hostname);
222 #else /* FEATURE_FANCY_PING */
225 /* full(er) version */
227 #define OPT_STRING ("qvc:s:I:4" USE_PING6("6"))
230 OPT_VERBOSE = 1 << 1,
235 OPT_IPV6 = (1 << 6) * ENABLE_PING6,
241 len_and_sockaddr *source_lsa;
244 unsigned long ntransmitted, nreceived, nrepeats, pingcount;
246 unsigned tmin, tmax; /* in us */
247 unsigned long long tsum; /* in us, sum of all times */
248 const char *hostname;
252 struct sockaddr_in sin;
254 struct sockaddr_in6 sin6;
257 char rcvd_tbl[MAX_DUP_CHK / 8];
259 #define G (*(struct globals*)&bb_common_bufsiz1)
260 #define pingsock (G.pingsock )
261 #define source_lsa (G.source_lsa )
262 #define datalen (G.datalen )
263 #define if_index (G.if_index )
264 #define ntransmitted (G.ntransmitted)
265 #define nreceived (G.nreceived )
266 #define nrepeats (G.nrepeats )
267 #define pingcount (G.pingcount )
268 #define myid (G.myid )
269 #define tmin (G.tmin )
270 #define tmax (G.tmax )
271 #define tsum (G.tsum )
272 #define hostname (G.hostname )
273 #define dotted (G.dotted )
274 #define pingaddr (G.pingaddr )
275 #define rcvd_tbl (G.rcvd_tbl )
276 void BUG_ping_globals_too_big(void);
277 #define INIT_G() do { \
278 if (sizeof(G) > COMMON_BUFSIZE) \
279 BUG_ping_globals_too_big(); \
285 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
286 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
287 #define SET(bit) (A(bit) |= B(bit))
288 #define CLR(bit) (A(bit) &= (~B(bit)))
289 #define TST(bit) (A(bit) & B(bit))
291 /**************************************************************************/
293 static void pingstats(int junk ATTRIBUTE_UNUSED)
295 signal(SIGINT, SIG_IGN);
297 printf("\n--- %s ping statistics ---\n", hostname);
298 printf("%lu packets transmitted, ", ntransmitted);
299 printf("%lu packets received, ", nreceived);
301 printf("%lu duplicates, ", nrepeats);
303 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
304 printf("%lu%% packet loss\n", ntransmitted);
305 if (tmin != UINT_MAX) {
306 unsigned tavg = tsum / (nreceived + nrepeats);
307 printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
308 tmin / 1000, tmin % 1000,
309 tavg / 1000, tavg % 1000,
310 tmax / 1000, tmax % 1000);
312 exit(nreceived == 0); /* (nreceived == 0) is true (1) -- 'failure' */
315 static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
319 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
322 /* sizeof(pingaddr) can be larger than real sa size, but I think
323 * it doesn't matter */
324 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
326 bb_error_msg_and_die(bb_msg_write_error);
329 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
331 } else { /* done, wait for the last ping to come back */
332 /* todo, don't necessarily need to wait so long... */
333 signal(SIGALRM, pingstats);
338 static void sendping4(int junk ATTRIBUTE_UNUSED)
340 /* +4 reserves a place for timestamp, which may end up sitting
341 * *after* packet. Saves one if() */
342 struct icmp *pkt = alloca(datalen + ICMP_MINLEN + 4);
344 pkt->icmp_type = ICMP_ECHO;
347 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
350 /* We don't do hton, because we will read it back on the same machine */
351 /*if (datalen >= 4)*/
352 *(uint32_t*)&pkt->icmp_dun = monotonic_us();
354 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
356 sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
359 static void sendping6(int junk ATTRIBUTE_UNUSED)
361 struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr) + 4);
363 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
365 pkt->icmp6_cksum = 0;
366 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
367 pkt->icmp6_id = myid;
369 /*if (datalen >= 4)*/
370 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
372 sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
376 static const char *icmp_type_name(int id)
379 case ICMP_ECHOREPLY: return "Echo Reply";
380 case ICMP_DEST_UNREACH: return "Destination Unreachable";
381 case ICMP_SOURCE_QUENCH: return "Source Quench";
382 case ICMP_REDIRECT: return "Redirect (change route)";
383 case ICMP_ECHO: return "Echo Request";
384 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
385 case ICMP_PARAMETERPROB: return "Parameter Problem";
386 case ICMP_TIMESTAMP: return "Timestamp Request";
387 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
388 case ICMP_INFO_REQUEST: return "Information Request";
389 case ICMP_INFO_REPLY: return "Information Reply";
390 case ICMP_ADDRESS: return "Address Mask Request";
391 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
392 default: return "unknown ICMP type";
396 /* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
397 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
398 #ifndef MLD_LISTENER_QUERY
399 # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
401 #ifndef MLD_LISTENER_REPORT
402 # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
404 #ifndef MLD_LISTENER_REDUCTION
405 # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
407 static const char *icmp6_type_name(int id)
410 case ICMP6_DST_UNREACH: return "Destination Unreachable";
411 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
412 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
413 case ICMP6_PARAM_PROB: return "Parameter Problem";
414 case ICMP6_ECHO_REPLY: return "Echo Reply";
415 case ICMP6_ECHO_REQUEST: return "Echo Request";
416 case MLD_LISTENER_QUERY: return "Listener Query";
417 case MLD_LISTENER_REPORT: return "Listener Report";
418 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
419 default: return "unknown ICMP type";
424 static void unpack_tail(int sz, uint32_t *tp,
425 const char *from_str,
426 uint16_t recv_seq, int ttl)
428 const char *dupmsg = " (DUP!)";
429 unsigned triptime = triptime; /* for gcc */
434 /* (int32_t) cast is for hypothetical 64-bit unsigned */
435 /* (doesn't hurt 32-bit real-world anyway) */
436 triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
444 if (TST(recv_seq % MAX_DUP_CHK)) {
448 SET(recv_seq % MAX_DUP_CHK);
452 if (option_mask32 & OPT_QUIET)
455 printf("%d bytes from %s: seq=%u ttl=%d", sz,
456 from_str, recv_seq, ttl);
458 printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
462 static void unpack4(char *buf, int sz, struct sockaddr_in *from)
464 struct icmp *icmppkt;
468 /* discard if too short */
469 if (sz < (datalen + ICMP_MINLEN))
472 /* check IP header */
473 iphdr = (struct iphdr *) buf;
474 hlen = iphdr->ihl << 2;
476 icmppkt = (struct icmp *) (buf + hlen);
477 if (icmppkt->icmp_id != myid)
478 return; /* not our ping */
480 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
481 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
484 if (sz >= ICMP_MINLEN + sizeof(uint32_t))
485 tp = (uint32_t *) icmppkt->icmp_data;
487 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
488 recv_seq, iphdr->ttl);
489 } else if (icmppkt->icmp_type != ICMP_ECHO) {
490 bb_error_msg("warning: got ICMP %d (%s)",
492 icmp_type_name(icmppkt->icmp_type));
496 static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
498 struct icmp6_hdr *icmppkt;
499 char buf[INET6_ADDRSTRLEN];
501 /* discard if too short */
502 if (sz < (datalen + sizeof(struct icmp6_hdr)))
505 icmppkt = (struct icmp6_hdr *) packet;
506 if (icmppkt->icmp6_id != myid)
507 return; /* not our ping */
509 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
510 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
513 if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
514 tp = (uint32_t *) &icmppkt->icmp6_data8[4];
516 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
519 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
520 bb_error_msg("warning: got ICMP %d (%s)",
522 icmp6_type_name(icmppkt->icmp6_type));
527 static void ping4(len_and_sockaddr *lsa)
529 char packet[datalen + MAXIPLEN + MAXICMPLEN];
532 pingsock = create_icmp_socket();
533 pingaddr.sin = lsa->sin;
535 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
536 &source_lsa->sa, source_lsa->len))
537 bb_error_msg_and_die("can't set multicast source interface");
538 xbind(pingsock, &source_lsa->sa, source_lsa->len);
541 /* enable broadcast pings */
542 setsockopt_broadcast(pingsock);
544 /* set recv buf for broadcast pings */
545 sockopt = 48 * 1024; /* explain why 48k? */
546 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
548 signal(SIGINT, pingstats);
550 /* start the ping's going ... */
553 /* listen for replies */
555 struct sockaddr_in from;
556 socklen_t fromlen = (socklen_t) sizeof(from);
559 c = recvfrom(pingsock, packet, sizeof(packet), 0,
560 (struct sockaddr *) &from, &fromlen);
563 bb_perror_msg("recvfrom");
566 unpack4(packet, c, &from);
567 if (pingcount > 0 && nreceived >= pingcount)
572 extern int BUG_bad_offsetof_icmp6_cksum(void);
573 static void ping6(len_and_sockaddr *lsa)
575 char packet[datalen + MAXIPLEN + MAXICMPLEN];
578 struct sockaddr_in6 from;
580 char control_buf[CMSG_SPACE(36)];
582 pingsock = create_icmp6_socket();
583 pingaddr.sin6 = lsa->sin6;
584 /* untested whether "-I addr" really works for IPv6: */
586 xbind(pingsock, &source_lsa->sa, source_lsa->len);
590 struct icmp6_filter filt;
591 if (!(option_mask32 & OPT_VERBOSE)) {
592 ICMP6_FILTER_SETBLOCKALL(&filt);
593 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
595 ICMP6_FILTER_SETPASSALL(&filt);
597 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
599 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
601 #endif /*ICMP6_FILTER*/
603 /* enable broadcast pings */
604 setsockopt_broadcast(pingsock);
606 /* set recv buf for broadcast pings */
607 sockopt = 48 * 1024; /* explain why 48k? */
608 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
610 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
611 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
612 BUG_bad_offsetof_icmp6_cksum();
613 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
615 /* request ttl info to be returned in ancillary data */
616 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
619 pingaddr.sin6.sin6_scope_id = if_index;
621 signal(SIGINT, pingstats);
623 /* start the ping's going ... */
626 /* listen for replies */
627 msg.msg_name = &from;
628 msg.msg_namelen = sizeof(from);
631 msg.msg_control = control_buf;
632 iov.iov_base = packet;
633 iov.iov_len = sizeof(packet);
638 msg.msg_controllen = sizeof(control_buf);
640 c = recvmsg(pingsock, &msg, 0);
643 bb_perror_msg("recvfrom");
646 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
647 if (mp->cmsg_level == SOL_IPV6
648 && mp->cmsg_type == IPV6_HOPLIMIT
649 /* don't check len - we trust the kernel: */
650 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
652 hoplimit = *(int*)CMSG_DATA(mp);
655 unpack6(packet, c, &from, hoplimit);
656 if (pingcount > 0 && nreceived >= pingcount)
662 static void ping(len_and_sockaddr *lsa)
664 printf("PING %s (%s)", hostname, dotted);
667 xmalloc_sockaddr2dotted_noport(&source_lsa->sa));
669 printf(": %d data bytes\n", datalen);
672 if (lsa->sa.sa_family == AF_INET6)
679 int ping_main(int argc, char **argv);
680 int ping_main(int argc, char **argv)
682 len_and_sockaddr *lsa;
683 char *opt_c, *opt_s, *opt_I;
684 USE_PING6(sa_family_t af = AF_UNSPEC;)
688 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
690 /* exactly one argument needed, -v and -q don't mix */
691 opt_complementary = "=1:q--v:v--q";
692 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
693 if (option_mask32 & OPT_c) pingcount = xatoul(opt_c); // -c
694 if (option_mask32 & OPT_s) datalen = xatou16(opt_s); // -s
695 if (option_mask32 & OPT_I) { // -I
696 if_index = if_nametoindex(opt_I);
698 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
699 source_lsa = xdotted2sockaddr(opt_I, 0);
702 myid = (uint16_t) getpid();
703 hostname = argv[optind];
705 if (option_mask32 & OPT_IPV4)
707 if (option_mask32 & OPT_IPV6)
709 lsa = xhost_and_af2sockaddr(hostname, 0, af);
711 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
714 if (source_lsa && source_lsa->sa.sa_family != lsa->sa.sa_family)
715 /* leaking it here... */
718 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa);
723 #endif /* FEATURE_FANCY_PING */
727 int ping6_main(int argc, char **argv);
728 int ping6_main(int argc, char **argv)
730 argv[0] = (char*)"-6";
731 return ping_main(argc + 1, argv - 1);
736 * Copyright (c) 1989 The Regents of the University of California.
737 * All rights reserved.
739 * This code is derived from software contributed to Berkeley by
742 * Redistribution and use in source and binary forms, with or without
743 * modification, are permitted provided that the following conditions
745 * 1. Redistributions of source code must retain the above copyright
746 * notice, this list of conditions and the following disclaimer.
747 * 2. Redistributions in binary form must reproduce the above copyright
748 * notice, this list of conditions and the following disclaimer in the
749 * documentation and/or other materials provided with the distribution.
751 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
752 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
754 * 4. Neither the name of the University nor the names of its contributors
755 * may be used to endorse or promote products derived from this software
756 * without specific prior written permission.
758 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
759 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
760 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
761 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
762 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
763 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
764 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
765 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
766 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
767 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF