blob: e413bec701abbc677fb1a0d1e3fb1b22b66009eb [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini ping implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Rob Landley42eddba2005-12-15 08:04:17 +00007 * Adapted from the ping in netkit-base 0.10:
Eric Andersen485b9551999-12-07 23:14:59 +00008 * Copyright (c) 1989 The Regents of the University of California.
Denis Vlasenko35d4da02007-01-22 14:04:27 +00009 * All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Mike Muuss.
Eric Andersen485b9551999-12-07 23:14:59 +000013 *
Rob Landley42eddba2005-12-15 08:04:17 +000014 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen485b9551999-12-07 23:14:59 +000015 */
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000016/* from ping6.c:
17 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
18 *
19 * This version of ping is adapted from the ping in netkit-base 0.10,
20 * which is:
21 *
22 * Original copyright notice is retained at the end of this file.
23 *
24 * This version is an adaptation of ping.c from busybox.
25 * The code was modified by Bart Visscher <magick@linux-fan.com>
26 */
Eric Andersen485b9551999-12-07 23:14:59 +000027
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000028#include <net/if.h>
Eric Andersen485b9551999-12-07 23:14:59 +000029#include <netinet/ip_icmp.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Denis Vlasenkoe9356022007-01-29 18:03:54 +000031
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000032#if ENABLE_PING6
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
37#undef IPV6_HOPLIMIT
38#define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
39#endif
40#endif
Eric Andersen485b9551999-12-07 23:14:59 +000041
Rob Landleybc68cd12006-03-10 19:22:06 +000042enum {
43 DEFDATALEN = 56,
44 MAXIPLEN = 60,
45 MAXICMPLEN = 76,
46 MAXPACKET = 65468,
47 MAX_DUP_CHK = (8 * 128),
48 MAXWAIT = 10,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000049 PINGINTERVAL = 1, /* 1 second */
Rob Landleybc68cd12006-03-10 19:22:06 +000050};
Eric Andersen485b9551999-12-07 23:14:59 +000051
Eric Andersen19db07b1999-12-11 08:41:28 +000052/* common routines */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000053
Eric Andersen485b9551999-12-07 23:14:59 +000054static int in_cksum(unsigned short *buf, int sz)
55{
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 int nleft = sz;
57 int sum = 0;
58 unsigned short *w = buf;
59 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +000060
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 while (nleft > 1) {
62 sum += *w++;
63 nleft -= 2;
64 }
Eric Andersen485b9551999-12-07 23:14:59 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 if (nleft == 1) {
67 *(unsigned char *) (&ans) = *(unsigned char *) w;
68 sum += ans;
69 }
Eric Andersen485b9551999-12-07 23:14:59 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 sum = (sum >> 16) + (sum & 0xFFFF);
72 sum += (sum >> 16);
73 ans = ~sum;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000074 return ans;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075}
Eric Andersen485b9551999-12-07 23:14:59 +000076
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000077#if !ENABLE_FEATURE_FANCY_PING
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000078
79/* simple version */
80
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000081static char *hostname;
82
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000083static void noresp(int ign ATTRIBUTE_UNUSED)
Eric Andersenb5474c42002-03-20 11:59:28 +000084{
Eric Andersenb8886822002-03-21 14:04:43 +000085 printf("No response from %s\n", hostname);
Eric Andersen4e486a52003-01-12 06:08:33 +000086 exit(EXIT_FAILURE);
Eric Andersenb5474c42002-03-20 11:59:28 +000087}
Eric Andersen19db07b1999-12-11 08:41:28 +000088
Denis Vlasenkoe9356022007-01-29 18:03:54 +000089static void ping4(len_and_sockaddr *lsa)
Eric Andersen19db07b1999-12-11 08:41:28 +000090{
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 struct sockaddr_in pingaddr;
92 struct icmp *pkt;
93 int pingsock, c;
94 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +000095
Matt Kraai06ef1652001-07-13 20:56:27 +000096 pingsock = create_icmp_socket();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000097 pingaddr = lsa->sin;
Erik Andersene49d5ec2000-02-08 19:58:47 +000098
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));
103
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000104 c = xsendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000105 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 /* listen for replies */
108 while (1) {
109 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000110 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111
Denis Vlasenko806116b2006-12-31 12:14:16 +0000112 c = recvfrom(pingsock, packet, sizeof(packet), 0,
113 (struct sockaddr *) &from, &fromlen);
114 if (c < 0) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000115 if (errno != EINTR)
116 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 continue;
118 }
119 if (c >= 76) { /* ip + icmp */
120 struct iphdr *iphdr = (struct iphdr *) packet;
121
122 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
123 if (pkt->icmp_type == ICMP_ECHOREPLY)
124 break;
125 }
126 }
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000127 if (ENABLE_FEATURE_CLEAN_UP)
128 close(pingsock);
Eric Andersen19db07b1999-12-11 08:41:28 +0000129}
130
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000131#if ENABLE_PING6
132static void ping6(len_and_sockaddr *lsa)
133{
134 struct sockaddr_in6 pingaddr;
135 struct icmp6_hdr *pkt;
136 int pingsock, c;
137 int sockopt;
138 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
139
140 pingsock = create_icmp6_socket();
141 pingaddr = lsa->sin6;
142
143 pkt = (struct icmp6_hdr *) packet;
144 memset(pkt, 0, sizeof(packet));
145 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
146
147 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
148 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
149
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000150 c = xsendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr),
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000151 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
152
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000153 /* listen for replies */
154 while (1) {
155 struct sockaddr_in6 from;
156 socklen_t fromlen = sizeof(from);
157
158 c = recvfrom(pingsock, packet, sizeof(packet), 0,
159 (struct sockaddr *) &from, &fromlen);
160 if (c < 0) {
161 if (errno != EINTR)
162 bb_perror_msg("recvfrom");
163 continue;
164 }
165 if (c >= 8) { /* icmp6_hdr */
166 pkt = (struct icmp6_hdr *) packet;
167 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
168 break;
169 }
170 }
171 if (ENABLE_FEATURE_CLEAN_UP)
172 close(pingsock);
173}
174#endif
175
Denis Vlasenko06af2162007-02-03 17:28:39 +0000176int ping_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000177int ping_main(int argc, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000178{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000179 len_and_sockaddr *lsa;
180#if ENABLE_PING6
181 sa_family_t af = AF_UNSPEC;
Denis Vlasenko4c978632007-02-03 03:31:13 +0000182 while (++argv[0][0] == '-') {
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000183 if (argv[0][1] == '4') {
184 af = AF_INET;
185 continue;
186 }
187 if (argv[0][1] == '6') {
188 af = AF_INET6;
189 continue;
190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 bb_show_usage();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000192 }
193#else
194 argv++;
195#endif
196
197 hostname = *argv;
198 if (!hostname)
199 bb_show_usage();
200
201#if ENABLE_PING6
Denis Vlasenko42823d52007-02-04 02:39:08 +0000202 lsa = xhost_and_af2sockaddr(hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000203#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000204 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000205#endif
206 /* Set timer _after_ DNS resolution */
207 signal(SIGALRM, noresp);
208 alarm(5); /* give the host 5000ms to respond */
209
210#if ENABLE_PING6
211 if (lsa->sa.sa_family == AF_INET6)
212 ping6(lsa);
213 else
214#endif
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000215 ping4(lsa);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000216 printf("%s is alive!\n", hostname);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000217 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000218}
219
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000220
221#else /* FEATURE_FANCY_PING */
222
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000223
Eric Andersen19db07b1999-12-11 08:41:28 +0000224/* full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000225
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000226#define OPT_STRING ("qvc:s:I:4" USE_PING6("6"))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000227enum {
228 OPT_QUIET = 1 << 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000229 OPT_VERBOSE = 1 << 1,
230 OPT_c = 1 << 2,
231 OPT_s = 1 << 3,
232 OPT_I = 1 << 4,
233 OPT_IPV4 = 1 << 5,
234 OPT_IPV6 = (1 << 6) * ENABLE_PING6,
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000235};
236
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000237
238static union {
239 struct sockaddr sa;
240 struct sockaddr_in sin;
241#if ENABLE_PING6
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000242 struct sockaddr_in6 sin6;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000243#endif
244} pingaddr;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000245static len_and_sockaddr *source_lsa;
Eric Andersen19db07b1999-12-11 08:41:28 +0000246static int pingsock = -1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000247static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
Eric Andersen19db07b1999-12-11 08:41:28 +0000248
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000249static int if_index;
250
Denis Vlasenko13858992006-10-08 12:49:22 +0000251static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000252static int myid;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000253static unsigned tmin = UINT_MAX, tmax;
254static unsigned long tsum;
Eric Andersen19db07b1999-12-11 08:41:28 +0000255static char rcvd_tbl[MAX_DUP_CHK / 8];
256
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000257static const char *hostname;
258static const char *dotted;
Eric Andersen19db07b1999-12-11 08:41:28 +0000259
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000260#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
261#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
262#define SET(bit) (A(bit) |= B(bit))
263#define CLR(bit) (A(bit) &= (~B(bit)))
264#define TST(bit) (A(bit) & B(bit))
265
Eric Andersen19db07b1999-12-11 08:41:28 +0000266/**************************************************************************/
267
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000268static void pingstats(int junk ATTRIBUTE_UNUSED)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269{
270 signal(SIGINT, SIG_IGN);
271
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000272 printf("\n--- %s ping statistics ---\n", hostname);
Denis Vlasenko13858992006-10-08 12:49:22 +0000273 printf("%lu packets transmitted, ", ntransmitted);
274 printf("%lu packets received, ", nreceived);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000276 printf("%lu duplicates, ", nrepeats);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 if (ntransmitted)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000278 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000279 printf("%lu%% packet loss\n", ntransmitted);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000280 if (tmin != UINT_MAX)
281 printf("round-trip min/avg/max = %u.%u/%lu.%lu/%u.%u ms\n",
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000282 tmin / 10, tmin % 10,
283 (tsum / (nreceived + nrepeats)) / 10,
284 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000285 exit(nreceived == 0); /* (nreceived == 0) is true (1) -- 'failure' */
Eric Andersen485b9551999-12-07 23:14:59 +0000286}
287
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000288static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000289{
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000290 int sz;
291
292 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
293 ntransmitted++;
294
295 /* sizeof(pingaddr) can be larger than real sa size, but I think
296 * it doesn't matter */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000297 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000298 if (sz != size_pkt)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000299 bb_error_msg_and_die(bb_msg_write_error);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000300
301 signal(SIGALRM, sp);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000302 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000303 alarm(PINGINTERVAL);
304 } else { /* done, wait for the last ping to come back */
305 /* todo, don't necessarily need to wait so long... */
306 signal(SIGALRM, pingstats);
307 alarm(MAXWAIT);
308 }
309}
310
311static void sendping4(int junk ATTRIBUTE_UNUSED)
Eric Andersen485b9551999-12-07 23:14:59 +0000312{
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000313 struct icmp *pkt = alloca(datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000314
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 pkt->icmp_type = ICMP_ECHO;
316 pkt->icmp_code = 0;
317 pkt->icmp_cksum = 0;
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000318 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 pkt->icmp_id = myid;
Rob Landleybbf4e162006-01-11 03:44:11 +0000320 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000321 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000322
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000323 sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000324}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000325#if ENABLE_PING6
326static void sendping6(int junk ATTRIBUTE_UNUSED)
327{
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000328 struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000329
330 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
331 pkt->icmp6_code = 0;
332 pkt->icmp6_cksum = 0;
333 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
334 pkt->icmp6_id = myid;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000335 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
336
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000337 sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000338}
339#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000341static const char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000342{
343 switch (id) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000344 case ICMP_ECHOREPLY: return "Echo Reply";
345 case ICMP_DEST_UNREACH: return "Destination Unreachable";
346 case ICMP_SOURCE_QUENCH: return "Source Quench";
347 case ICMP_REDIRECT: return "Redirect (change route)";
348 case ICMP_ECHO: return "Echo Request";
349 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
350 case ICMP_PARAMETERPROB: return "Parameter Problem";
351 case ICMP_TIMESTAMP: return "Timestamp Request";
352 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
353 case ICMP_INFO_REQUEST: return "Information Request";
354 case ICMP_INFO_REPLY: return "Information Reply";
355 case ICMP_ADDRESS: return "Address Mask Request";
356 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
357 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000358 }
359}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000360#if ENABLE_PING6
361/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
362 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
363#ifndef MLD_LISTENER_QUERY
364# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
365#endif
366#ifndef MLD_LISTENER_REPORT
367# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
368#endif
369#ifndef MLD_LISTENER_REDUCTION
370# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
371#endif
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000372static const char *icmp6_type_name(int id)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000373{
374 switch (id) {
375 case ICMP6_DST_UNREACH: return "Destination Unreachable";
376 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
377 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
378 case ICMP6_PARAM_PROB: return "Parameter Problem";
379 case ICMP6_ECHO_REPLY: return "Echo Reply";
380 case ICMP6_ECHO_REQUEST: return "Echo Request";
381 case MLD_LISTENER_QUERY: return "Listener Query";
382 case MLD_LISTENER_REPORT: return "Listener Report";
383 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
384 default: return "unknown ICMP type";
385 }
386}
387#endif
Erik Andersen227a59b2000-04-25 23:24:55 +0000388
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000389static void unpack_tail(int sz, struct timeval *tp,
390 const char *from_str,
391 uint16_t recv_seq, int ttl)
392{
393 const char *dupmsg = " (DUP!)";
394 unsigned triptime = triptime; /* for gcc */
395
396 ++nreceived;
397
398 if (tp) {
399 struct timeval tv;
400
401 gettimeofday(&tv, NULL);
402 tv.tv_usec -= tp->tv_usec;
403 if (tv.tv_usec < 0) {
404 --tv.tv_sec;
405 tv.tv_usec += 1000000;
406 }
407 tv.tv_sec -= tp->tv_sec;
408
409 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
410 tsum += triptime;
411 if (triptime < tmin)
412 tmin = triptime;
413 if (triptime > tmax)
414 tmax = triptime;
415 }
416
417 if (TST(recv_seq % MAX_DUP_CHK)) {
418 ++nrepeats;
419 --nreceived;
420 } else {
421 SET(recv_seq % MAX_DUP_CHK);
422 dupmsg += 7;
423 }
424
425 if (option_mask32 & OPT_QUIET)
426 return;
427
428 printf("%d bytes from %s: seq=%u ttl=%d", sz,
429 from_str, recv_seq, ttl);
430 if (tp)
431 printf(" time=%u.%u ms", triptime / 10, triptime % 10);
432 puts(dupmsg);
433 fflush(stdout);
434}
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000435static void unpack4(char *buf, int sz, struct sockaddr_in *from)
Eric Andersen485b9551999-12-07 23:14:59 +0000436{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 struct icmp *icmppkt;
438 struct iphdr *iphdr;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000439 int hlen;
Eric Andersen485b9551999-12-07 23:14:59 +0000440
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000442 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000444
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000445 /* check IP header */
446 iphdr = (struct iphdr *) buf;
447 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 sz -= hlen;
449 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000450 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000451 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000454 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000455 struct timeval *tp = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000457 if (sz >= ICMP_MINLEN + sizeof(struct timeval))
458 tp = (struct timeval *) icmppkt->icmp_data;
459 unpack_tail(sz, tp,
460 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
461 recv_seq, iphdr->ttl);
462 } else if (icmppkt->icmp_type != ICMP_ECHO) {
463 bb_error_msg("warning: got ICMP %d (%s)",
464 icmppkt->icmp_type,
465 icmp_type_name(icmppkt->icmp_type));
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000466 }
Eric Andersen485b9551999-12-07 23:14:59 +0000467}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000468#if ENABLE_PING6
469static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
470{
471 struct icmp6_hdr *icmppkt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000472 char buf[INET6_ADDRSTRLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000473
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000474 /* discard if too short */
475 if (sz < (datalen + sizeof(struct icmp6_hdr)))
476 return;
477
478 icmppkt = (struct icmp6_hdr *) packet;
479 if (icmppkt->icmp6_id != myid)
480 return; /* not our ping */
481
482 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
483 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000484 struct timeval *tp = NULL;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000485
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000486 if (sz >= sizeof(struct icmp6_hdr) + sizeof(struct timeval))
487 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
488 unpack_tail(sz, tp,
489 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
490 buf, sizeof(buf)),
491 recv_seq, hoplimit);
492 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
493 bb_error_msg("warning: got ICMP %d (%s)",
494 icmppkt->icmp6_type,
495 icmp6_type_name(icmppkt->icmp6_type));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000496 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000497}
498#endif
499
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000500static void ping4(len_and_sockaddr *lsa)
Eric Andersen485b9551999-12-07 23:14:59 +0000501{
Pavel Roskin0024abc2000-06-07 20:38:15 +0000502 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 int sockopt;
504
Matt Kraai06ef1652001-07-13 20:56:27 +0000505 pingsock = create_icmp_socket();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000506 pingaddr.sin = lsa->sin;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000507 if (source_lsa)
508 xbind(pingsock, &lsa->sa, lsa->len);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000509
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000511 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512
513 /* set recv buf for broadcast pings */
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000514 sockopt = 48 * 1024; /* explain why 48k? */
Denis Vlasenko703e2022007-01-22 14:12:08 +0000515 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 signal(SIGINT, pingstats);
518
519 /* start the ping's going ... */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000520 sendping4(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521
522 /* listen for replies */
523 while (1) {
524 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000525 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 int c;
527
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000528 c = recvfrom(pingsock, packet, sizeof(packet), 0,
529 (struct sockaddr *) &from, &fromlen);
530 if (c < 0) {
531 if (errno != EINTR)
532 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 continue;
534 }
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000535 unpack4(packet, c, &from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 if (pingcount > 0 && nreceived >= pingcount)
537 break;
538 }
Eric Andersen485b9551999-12-07 23:14:59 +0000539}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000540#if ENABLE_PING6
541extern int BUG_bad_offsetof_icmp6_cksum(void);
542static void ping6(len_and_sockaddr *lsa)
543{
544 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000545 int sockopt;
546 struct msghdr msg;
547 struct sockaddr_in6 from;
548 struct iovec iov;
549 char control_buf[CMSG_SPACE(36)];
550
551 pingsock = create_icmp6_socket();
552 pingaddr.sin6 = lsa->sin6;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000553 /* untested whether "-I addr" really works for IPv6: */
554 if (source_lsa)
555 xbind(pingsock, &lsa->sa, lsa->len);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000556
557#ifdef ICMP6_FILTER
558 {
559 struct icmp6_filter filt;
560 if (!(option_mask32 & OPT_VERBOSE)) {
561 ICMP6_FILTER_SETBLOCKALL(&filt);
562 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
563 } else {
564 ICMP6_FILTER_SETPASSALL(&filt);
565 }
566 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
567 sizeof(filt)) < 0)
568 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
569 }
570#endif /*ICMP6_FILTER*/
571
572 /* enable broadcast pings */
573 setsockopt_broadcast(pingsock);
574
575 /* set recv buf for broadcast pings */
576 sockopt = 48 * 1024; /* explain why 48k? */
577 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
578
579 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
580 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
581 BUG_bad_offsetof_icmp6_cksum();
582 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
583
584 /* request ttl info to be returned in ancillary data */
585 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
586
587 if (if_index)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000588 pingaddr.sin6.sin6_scope_id = if_index;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000589
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000590 signal(SIGINT, pingstats);
591
592 /* start the ping's going ... */
593 sendping6(0);
594
595 /* listen for replies */
596 msg.msg_name = &from;
597 msg.msg_namelen = sizeof(from);
598 msg.msg_iov = &iov;
599 msg.msg_iovlen = 1;
600 msg.msg_control = control_buf;
601 iov.iov_base = packet;
602 iov.iov_len = sizeof(packet);
603 while (1) {
604 int c;
605 struct cmsghdr *mp;
606 int hoplimit = -1;
607 msg.msg_controllen = sizeof(control_buf);
608
609 c = recvmsg(pingsock, &msg, 0);
610 if (c < 0) {
611 if (errno != EINTR)
612 bb_perror_msg("recvfrom");
613 continue;
614 }
615 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
616 if (mp->cmsg_level == SOL_IPV6
617 && mp->cmsg_type == IPV6_HOPLIMIT
618 /* don't check len - we trust the kernel: */
619 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
620 ) {
621 hoplimit = *(int*)CMSG_DATA(mp);
622 }
623 }
624 unpack6(packet, c, &from, hoplimit);
625 if (pingcount > 0 && nreceived >= pingcount)
626 break;
627 }
628}
629#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000630
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000631static void ping(len_and_sockaddr *lsa)
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000632{
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000633 printf("PING %s (%s)", hostname, dotted);
634 if (source_lsa) {
635 printf(" from %s",
636 xmalloc_sockaddr2dotted_noport(&lsa->sa, lsa->len));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000637 }
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000638 printf(": %d data bytes\n", datalen);
639
640#if ENABLE_PING6
641 if (lsa->sa.sa_family == AF_INET6)
642 ping6(lsa);
643 else
644#endif
645 ping4(lsa);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000646}
647
Denis Vlasenko06af2162007-02-03 17:28:39 +0000648int ping_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000649int ping_main(int argc, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000650{
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000651 len_and_sockaddr *lsa;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000652 char *opt_c, *opt_s, *opt_I;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000653 USE_PING6(sa_family_t af = AF_UNSPEC;)
Eric Andersen485b9551999-12-07 23:14:59 +0000654
Mark Whitley59ab0252001-01-23 22:30:04 +0000655 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
656
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000657 /* exactly one argument needed, -v and -q don't mix */
Denis Vlasenko581930c2007-01-24 23:55:34 +0000658 opt_complementary = "=1:q--v:v--q";
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000659 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000660 if (option_mask32 & OPT_c) pingcount = xatoul(opt_c); // -c
661 if (option_mask32 & OPT_s) datalen = xatou16(opt_s); // -s
662 if (option_mask32 & OPT_I) { // -I
663 if_index = if_nametoindex(opt_I);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000664 if (!if_index) {
665 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
666 /* (ping doesn't support source IPv6 addresses yet anyway) */
667 source_lsa = xdotted2sockaddr(opt_I, 0);
668 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 }
Denis Vlasenkodb7f2e52006-09-02 16:16:23 +0000670 myid = (int16_t) getpid();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000671 hostname = argv[optind];
672#if ENABLE_PING6
673 if (option_mask32 & OPT_IPV4)
674 af = AF_INET;
675 if (option_mask32 & OPT_IPV6)
676 af = AF_INET6;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000677 lsa = xhost_and_af2sockaddr(hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000678#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000679 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000680#endif
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000681
682 if (source_lsa && source_lsa->sa.sa_family != lsa->sa.sa_family)
683 /* leaking it here... */
684 source_lsa = NULL;
685
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000686 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa, lsa->len);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000687 ping(lsa);
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000688 pingstats(0);
Eric Andersene90e7412002-06-06 11:47:00 +0000689 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000690}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000691#endif /* FEATURE_FANCY_PING */
692
693
694#if ENABLE_PING6
Denis Vlasenko06af2162007-02-03 17:28:39 +0000695int ping6_main(int argc, char **argv);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000696int ping6_main(int argc, char **argv)
697{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000698 argv[0] = (char*)"-6";
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000699 return ping_main(argc + 1, argv - 1);
700}
701#endif
702
703/* from ping6.c:
704 * Copyright (c) 1989 The Regents of the University of California.
705 * All rights reserved.
706 *
707 * This code is derived from software contributed to Berkeley by
708 * Mike Muuss.
709 *
710 * Redistribution and use in source and binary forms, with or without
711 * modification, are permitted provided that the following conditions
712 * are met:
713 * 1. Redistributions of source code must retain the above copyright
714 * notice, this list of conditions and the following disclaimer.
715 * 2. Redistributions in binary form must reproduce the above copyright
716 * notice, this list of conditions and the following disclaimer in the
717 * documentation and/or other materials provided with the distribution.
718 *
719 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
720 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
721 *
722 * 4. Neither the name of the University nor the names of its contributors
723 * may be used to endorse or promote products derived from this software
724 * without specific prior written permission.
725 *
726 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
727 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
728 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
729 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
730 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
731 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
732 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
733 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
734 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
735 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
736 * SUCH DAMAGE.
737 */