blob: e765843412768d37ebae4fd6d8297ae23acdb6a4 [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
Rob Landley07a637d2006-04-01 17:28:11 +0000104 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000105 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
Rob Landley9b1857f2006-05-31 23:54:50 +0000107 if (c < 0) {
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000108 if (ENABLE_FEATURE_CLEAN_UP)
109 close(pingsock);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg_and_die("sendto");
Rob Landley9b1857f2006-05-31 23:54:50 +0000111 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 /* listen for replies */
114 while (1) {
115 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000116 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117
Denis Vlasenko806116b2006-12-31 12:14:16 +0000118 c = recvfrom(pingsock, packet, sizeof(packet), 0,
119 (struct sockaddr *) &from, &fromlen);
120 if (c < 0) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000121 if (errno != EINTR)
122 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 continue;
124 }
125 if (c >= 76) { /* ip + icmp */
126 struct iphdr *iphdr = (struct iphdr *) packet;
127
128 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
129 if (pkt->icmp_type == ICMP_ECHOREPLY)
130 break;
131 }
132 }
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000133 if (ENABLE_FEATURE_CLEAN_UP)
134 close(pingsock);
Eric Andersen19db07b1999-12-11 08:41:28 +0000135}
136
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000137#if ENABLE_PING6
138static void ping6(len_and_sockaddr *lsa)
139{
140 struct sockaddr_in6 pingaddr;
141 struct icmp6_hdr *pkt;
142 int pingsock, c;
143 int sockopt;
144 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
145
146 pingsock = create_icmp6_socket();
147 pingaddr = lsa->sin6;
148
149 pkt = (struct icmp6_hdr *) packet;
150 memset(pkt, 0, sizeof(packet));
151 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
152
153 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
154 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
155
156 c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
157 (struct sockaddr *) &pingaddr, sizeof(pingaddr));
158
159 if (c < 0) {
160 if (ENABLE_FEATURE_CLEAN_UP)
161 close(pingsock);
162 bb_perror_msg_and_die("sendto");
163 }
164
165 /* listen for replies */
166 while (1) {
167 struct sockaddr_in6 from;
168 socklen_t fromlen = sizeof(from);
169
170 c = recvfrom(pingsock, packet, sizeof(packet), 0,
171 (struct sockaddr *) &from, &fromlen);
172 if (c < 0) {
173 if (errno != EINTR)
174 bb_perror_msg("recvfrom");
175 continue;
176 }
177 if (c >= 8) { /* icmp6_hdr */
178 pkt = (struct icmp6_hdr *) packet;
179 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
180 break;
181 }
182 }
183 if (ENABLE_FEATURE_CLEAN_UP)
184 close(pingsock);
185}
186#endif
187
Denis Vlasenko06af2162007-02-03 17:28:39 +0000188int ping_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000189int ping_main(int argc, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000190{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000191 len_and_sockaddr *lsa;
192#if ENABLE_PING6
193 sa_family_t af = AF_UNSPEC;
Denis Vlasenko4c978632007-02-03 03:31:13 +0000194 while (++argv[0][0] == '-') {
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000195 if (argv[0][1] == '4') {
196 af = AF_INET;
197 continue;
198 }
199 if (argv[0][1] == '6') {
200 af = AF_INET6;
201 continue;
202 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 bb_show_usage();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000204 }
205#else
206 argv++;
207#endif
208
209 hostname = *argv;
210 if (!hostname)
211 bb_show_usage();
212
213#if ENABLE_PING6
Denis Vlasenko42823d52007-02-04 02:39:08 +0000214 lsa = xhost_and_af2sockaddr(hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000215#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000216 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000217#endif
218 /* Set timer _after_ DNS resolution */
219 signal(SIGALRM, noresp);
220 alarm(5); /* give the host 5000ms to respond */
221
222#if ENABLE_PING6
223 if (lsa->sa.sa_family == AF_INET6)
224 ping6(lsa);
225 else
226#endif
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000227 ping4(lsa);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000228 printf("%s is alive!\n", hostname);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000229 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000230}
231
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000232
233#else /* FEATURE_FANCY_PING */
234
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000235
Eric Andersen19db07b1999-12-11 08:41:28 +0000236/* full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000237
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000238#define OPT_STRING ("qvc:s:I:4" USE_PING6("6"))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000239enum {
240 OPT_QUIET = 1 << 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000241 OPT_VERBOSE = 1 << 1,
242 OPT_c = 1 << 2,
243 OPT_s = 1 << 3,
244 OPT_I = 1 << 4,
245 OPT_IPV4 = 1 << 5,
246 OPT_IPV6 = (1 << 6) * ENABLE_PING6,
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000247};
248
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000249
250static union {
251 struct sockaddr sa;
252 struct sockaddr_in sin;
253#if ENABLE_PING6
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000254 struct sockaddr_in6 sin6;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000255#endif
256} pingaddr;
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000257static struct sockaddr_in sourceaddr;
Eric Andersen19db07b1999-12-11 08:41:28 +0000258static int pingsock = -1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000259static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
Eric Andersen19db07b1999-12-11 08:41:28 +0000260
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000261static int if_index;
262
Denis Vlasenko13858992006-10-08 12:49:22 +0000263static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000264static int myid;
Matt Kraai369da772002-02-01 16:54:00 +0000265static unsigned long tmin = ULONG_MAX, tmax, tsum;
Eric Andersen19db07b1999-12-11 08:41:28 +0000266static char rcvd_tbl[MAX_DUP_CHK / 8];
267
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000268static const char *hostname;
269static const char *dotted;
Eric Andersen19db07b1999-12-11 08:41:28 +0000270
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000271#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
272#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
273#define SET(bit) (A(bit) |= B(bit))
274#define CLR(bit) (A(bit) &= (~B(bit)))
275#define TST(bit) (A(bit) & B(bit))
276
Eric Andersen19db07b1999-12-11 08:41:28 +0000277/**************************************************************************/
278
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000279static void pingstats(int junk ATTRIBUTE_UNUSED)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280{
Matt Kraaib938e2f2000-09-20 04:33:30 +0000281 int status;
282
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 signal(SIGINT, SIG_IGN);
284
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000285 printf("\n--- %s ping statistics ---\n", hostname);
Denis Vlasenko13858992006-10-08 12:49:22 +0000286 printf("%lu packets transmitted, ", ntransmitted);
287 printf("%lu packets received, ", nreceived);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000289 printf("%lu duplicates, ", nrepeats);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 if (ntransmitted)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000291 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000292 printf("%lu%% packet loss\n", ntransmitted);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 if (nreceived)
294 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000295 tmin / 10, tmin % 10,
296 (tsum / (nreceived + nrepeats)) / 10,
297 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Matt Kraaib938e2f2000-09-20 04:33:30 +0000298 if (nreceived != 0)
299 status = EXIT_SUCCESS;
300 else
301 status = EXIT_FAILURE;
302 exit(status);
Eric Andersen485b9551999-12-07 23:14:59 +0000303}
304
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000305static void sendping_tail(void (*sp)(int), int sz, int sizeof_packet)
306{
307 if (sz < 0)
308 bb_perror_msg_and_die("sendto");
309 if (sz != sizeof_packet)
310 bb_error_msg_and_die("ping wrote %d chars; %d expected", sz,
311 sizeof_packet);
312
313 signal(SIGALRM, sp);
314 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
315 alarm(PINGINTERVAL);
316 } else { /* done, wait for the last ping to come back */
317 /* todo, don't necessarily need to wait so long... */
318 signal(SIGALRM, pingstats);
319 alarm(MAXWAIT);
320 }
321}
322
323static void sendping4(int junk ATTRIBUTE_UNUSED)
Eric Andersen485b9551999-12-07 23:14:59 +0000324{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 struct icmp *pkt;
326 int i;
Rob Landley07a637d2006-04-01 17:28:11 +0000327 char packet[datalen + ICMP_MINLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000328
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000330
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 pkt->icmp_type = ICMP_ECHO;
332 pkt->icmp_code = 0;
333 pkt->icmp_cksum = 0;
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000334 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 pkt->icmp_id = myid;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000336 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000337 ntransmitted++;
Eric Andersen485b9551999-12-07 23:14:59 +0000338
Rob Landleybbf4e162006-01-11 03:44:11 +0000339 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000341
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 i = sendto(pingsock, packet, sizeof(packet), 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000343 &pingaddr.sa, sizeof(pingaddr.sin));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000345 sendping_tail(sendping4, i, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000346}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000347#if ENABLE_PING6
348static void sendping6(int junk ATTRIBUTE_UNUSED)
349{
350 struct icmp6_hdr *pkt;
351 int i;
352 char packet[datalen + sizeof (struct icmp6_hdr)];
353
354 pkt = (struct icmp6_hdr *) packet;
355
356 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
357 pkt->icmp6_code = 0;
358 pkt->icmp6_cksum = 0;
359 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
360 pkt->icmp6_id = myid;
361 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
362 ntransmitted++;
363
364 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
365
366 i = sendto(pingsock, packet, sizeof(packet), 0,
367 &pingaddr.sa, sizeof(pingaddr.sin6));
368
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000369 sendping_tail(sendping6, i, sizeof(packet));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000370}
371#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000373static const char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000374{
375 switch (id) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000376 case ICMP_ECHOREPLY: return "Echo Reply";
377 case ICMP_DEST_UNREACH: return "Destination Unreachable";
378 case ICMP_SOURCE_QUENCH: return "Source Quench";
379 case ICMP_REDIRECT: return "Redirect (change route)";
380 case ICMP_ECHO: return "Echo Request";
381 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
382 case ICMP_PARAMETERPROB: return "Parameter Problem";
383 case ICMP_TIMESTAMP: return "Timestamp Request";
384 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
385 case ICMP_INFO_REQUEST: return "Information Request";
386 case ICMP_INFO_REPLY: return "Information Reply";
387 case ICMP_ADDRESS: return "Address Mask Request";
388 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
389 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000390 }
391}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000392#if ENABLE_PING6
393/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
394 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
395#ifndef MLD_LISTENER_QUERY
396# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
397#endif
398#ifndef MLD_LISTENER_REPORT
399# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
400#endif
401#ifndef MLD_LISTENER_REDUCTION
402# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
403#endif
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000404static const char *icmp6_type_name(int id)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000405{
406 switch (id) {
407 case ICMP6_DST_UNREACH: return "Destination Unreachable";
408 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
409 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
410 case ICMP6_PARAM_PROB: return "Parameter Problem";
411 case ICMP6_ECHO_REPLY: return "Echo Reply";
412 case ICMP6_ECHO_REQUEST: return "Echo Request";
413 case MLD_LISTENER_QUERY: return "Listener Query";
414 case MLD_LISTENER_REPORT: return "Listener Report";
415 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
416 default: return "unknown ICMP type";
417 }
418}
419#endif
Erik Andersen227a59b2000-04-25 23:24:55 +0000420
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000421static void unpack4(char *buf, int sz, struct sockaddr_in *from)
Eric Andersen485b9551999-12-07 23:14:59 +0000422{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 struct icmp *icmppkt;
424 struct iphdr *iphdr;
425 struct timeval tv, *tp;
426 int hlen, dupflag;
427 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000428
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000430
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000432 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000434
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000435 /* check IP header */
436 iphdr = (struct iphdr *) buf;
437 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 sz -= hlen;
439 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000440 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000441 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000442
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000444 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000445 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 tp = (struct timeval *) icmppkt->icmp_data;
447
448 if ((tv.tv_usec -= tp->tv_usec) < 0) {
449 --tv.tv_sec;
450 tv.tv_usec += 1000000;
451 }
452 tv.tv_sec -= tp->tv_sec;
453
454 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
455 tsum += triptime;
456 if (triptime < tmin)
457 tmin = triptime;
458 if (triptime > tmax)
459 tmax = triptime;
460
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000461 if (TST(recv_seq % MAX_DUP_CHK)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 ++nrepeats;
463 --nreceived;
464 dupflag = 1;
465 } else {
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000466 SET(recv_seq % MAX_DUP_CHK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 dupflag = 0;
468 }
469
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000470 if (option_mask32 & OPT_QUIET)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 return;
472
473 printf("%d bytes from %s: icmp_seq=%u", sz,
474 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000475 recv_seq);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 printf(" ttl=%d", iphdr->ttl);
477 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
478 if (dupflag)
479 printf(" (DUP!)");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000480 puts("");
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000481 } else {
Erik Andersen227a59b2000-04-25 23:24:55 +0000482 if (icmppkt->icmp_type != ICMP_ECHO)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000483 bb_error_msg("warning: got ICMP %d (%s)",
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000484 icmppkt->icmp_type,
485 icmp_type_name(icmppkt->icmp_type));
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000486 }
Rob Landley42eddba2005-12-15 08:04:17 +0000487 fflush(stdout);
Eric Andersen485b9551999-12-07 23:14:59 +0000488}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000489#if ENABLE_PING6
490static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
491{
492 struct icmp6_hdr *icmppkt;
493 struct timeval tv, *tp;
494 int dupflag;
495 unsigned long triptime;
496 char buf[INET6_ADDRSTRLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000497
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000498 gettimeofday(&tv, NULL);
499
500 /* discard if too short */
501 if (sz < (datalen + sizeof(struct icmp6_hdr)))
502 return;
503
504 icmppkt = (struct icmp6_hdr *) packet;
505 if (icmppkt->icmp6_id != myid)
506 return; /* not our ping */
507
508 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
509 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
510 ++nreceived;
511 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
512
513 if ((tv.tv_usec -= tp->tv_usec) < 0) {
514 --tv.tv_sec;
515 tv.tv_usec += 1000000;
516 }
517 tv.tv_sec -= tp->tv_sec;
518
519 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
520 tsum += triptime;
521 if (triptime < tmin)
522 tmin = triptime;
523 if (triptime > tmax)
524 tmax = triptime;
525
526 if (TST(recv_seq % MAX_DUP_CHK)) {
527 ++nrepeats;
528 --nreceived;
529 dupflag = 1;
530 } else {
531 SET(recv_seq % MAX_DUP_CHK);
532 dupflag = 0;
533 }
534
535 if (option_mask32 & OPT_QUIET)
536 return;
537
538 printf("%d bytes from %s: icmp6_seq=%u", sz,
539 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
540 buf, sizeof(buf)),
541 recv_seq);
542 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
543 triptime / 10, triptime % 10);
544 if (dupflag)
545 printf(" (DUP!)");
546 puts("");
547 } else {
548 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
549 bb_error_msg("warning: got ICMP %d (%s)",
550 icmppkt->icmp6_type,
551 icmp6_type_name(icmppkt->icmp6_type));
552 }
553 fflush(stdout);
554}
555#endif
556
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000557static void ping4(len_and_sockaddr *lsa)
Eric Andersen485b9551999-12-07 23:14:59 +0000558{
Pavel Roskin0024abc2000-06-07 20:38:15 +0000559 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 int sockopt;
561
Matt Kraai06ef1652001-07-13 20:56:27 +0000562 pingsock = create_icmp_socket();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000563 pingaddr.sin = lsa->sin;
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000564 if (sourceaddr.sin_addr.s_addr) {
Denis Vlasenkobcf49082006-09-02 17:53:16 +0000565 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000566 }
567
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000569 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570
571 /* set recv buf for broadcast pings */
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000572 sockopt = 48 * 1024; /* explain why 48k? */
Denis Vlasenko703e2022007-01-22 14:12:08 +0000573 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000575 printf("PING %s (%s)", hostname, dotted);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000576 if (sourceaddr.sin_addr.s_addr) {
577 printf(" from %s",
578 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
579 }
580 printf(": %d data bytes\n", datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581
582 signal(SIGINT, pingstats);
583
584 /* start the ping's going ... */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000585 sendping4(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586
587 /* listen for replies */
588 while (1) {
589 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000590 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 int c;
592
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000593 c = recvfrom(pingsock, packet, sizeof(packet), 0,
594 (struct sockaddr *) &from, &fromlen);
595 if (c < 0) {
596 if (errno != EINTR)
597 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 continue;
599 }
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000600 unpack4(packet, c, &from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 if (pingcount > 0 && nreceived >= pingcount)
602 break;
603 }
Eric Andersen485b9551999-12-07 23:14:59 +0000604}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000605#if ENABLE_PING6
606extern int BUG_bad_offsetof_icmp6_cksum(void);
607static void ping6(len_and_sockaddr *lsa)
608{
609 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000610 int sockopt;
611 struct msghdr msg;
612 struct sockaddr_in6 from;
613 struct iovec iov;
614 char control_buf[CMSG_SPACE(36)];
615
616 pingsock = create_icmp6_socket();
617 pingaddr.sin6 = lsa->sin6;
618 //if (sourceaddr.sin_addr.s_addr) {
619 // xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
620 //}
621
622#ifdef ICMP6_FILTER
623 {
624 struct icmp6_filter filt;
625 if (!(option_mask32 & OPT_VERBOSE)) {
626 ICMP6_FILTER_SETBLOCKALL(&filt);
627 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
628 } else {
629 ICMP6_FILTER_SETPASSALL(&filt);
630 }
631 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
632 sizeof(filt)) < 0)
633 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
634 }
635#endif /*ICMP6_FILTER*/
636
637 /* enable broadcast pings */
638 setsockopt_broadcast(pingsock);
639
640 /* set recv buf for broadcast pings */
641 sockopt = 48 * 1024; /* explain why 48k? */
642 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
643
644 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
645 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
646 BUG_bad_offsetof_icmp6_cksum();
647 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
648
649 /* request ttl info to be returned in ancillary data */
650 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
651
652 if (if_index)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000653 pingaddr.sin6.sin6_scope_id = if_index;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000654
655 printf("PING %s (%s): %d data bytes\n", hostname, dotted, datalen);
656
657 signal(SIGINT, pingstats);
658
659 /* start the ping's going ... */
660 sendping6(0);
661
662 /* listen for replies */
663 msg.msg_name = &from;
664 msg.msg_namelen = sizeof(from);
665 msg.msg_iov = &iov;
666 msg.msg_iovlen = 1;
667 msg.msg_control = control_buf;
668 iov.iov_base = packet;
669 iov.iov_len = sizeof(packet);
670 while (1) {
671 int c;
672 struct cmsghdr *mp;
673 int hoplimit = -1;
674 msg.msg_controllen = sizeof(control_buf);
675
676 c = recvmsg(pingsock, &msg, 0);
677 if (c < 0) {
678 if (errno != EINTR)
679 bb_perror_msg("recvfrom");
680 continue;
681 }
682 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
683 if (mp->cmsg_level == SOL_IPV6
684 && mp->cmsg_type == IPV6_HOPLIMIT
685 /* don't check len - we trust the kernel: */
686 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
687 ) {
688 hoplimit = *(int*)CMSG_DATA(mp);
689 }
690 }
691 unpack6(packet, c, &from, hoplimit);
692 if (pingcount > 0 && nreceived >= pingcount)
693 break;
694 }
695}
696#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000697
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000698/* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
699 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
700*/
701static int parse_nipquad(const char *str, struct sockaddr_in* addr)
702{
703 char dummy;
704 unsigned i1, i2, i3, i4;
705 if (sscanf(str, "%u.%u.%u.%u%c",
706 &i1, &i2, &i3, &i4, &dummy) == 4
707 && ( (i1|i2|i3|i4) <= 0xff )
708 ) {
709 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
710 ptr[0] = i1;
711 ptr[1] = i2;
712 ptr[2] = i3;
713 ptr[3] = i4;
714 return 0;
715 }
716 return 1; /* error */
717}
718
Denis Vlasenko06af2162007-02-03 17:28:39 +0000719int ping_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000720int ping_main(int argc, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000721{
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000722 len_and_sockaddr *lsa;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000723 char *opt_c, *opt_s, *opt_I;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000724 USE_PING6(sa_family_t af = AF_UNSPEC;)
Eric Andersen485b9551999-12-07 23:14:59 +0000725
Mark Whitley59ab0252001-01-23 22:30:04 +0000726 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
727
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000728 /* exactly one argument needed, -v and -q don't mix */
Denis Vlasenko581930c2007-01-24 23:55:34 +0000729 opt_complementary = "=1:q--v:v--q";
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000730 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000731 if (option_mask32 & OPT_c) pingcount = xatoul(opt_c); // -c
732 if (option_mask32 & OPT_s) datalen = xatou16(opt_s); // -s
733 if (option_mask32 & OPT_I) { // -I
734 if_index = if_nametoindex(opt_I);
735 if (!if_index)
736 if (parse_nipquad(opt_I, &sourceaddr))
737 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 }
Denis Vlasenkodb7f2e52006-09-02 16:16:23 +0000739 myid = (int16_t) getpid();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000740 hostname = argv[optind];
741#if ENABLE_PING6
742 if (option_mask32 & OPT_IPV4)
743 af = AF_INET;
744 if (option_mask32 & OPT_IPV6)
745 af = AF_INET6;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000746 lsa = xhost_and_af2sockaddr(hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000747#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000748 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000749#endif
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000750 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa, lsa->len);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000751#if ENABLE_PING6
752 if (lsa->sa.sa_family == AF_INET6)
753 ping6(lsa);
754 else
755#endif
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000756 ping4(lsa);
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000757 pingstats(0);
Eric Andersene90e7412002-06-06 11:47:00 +0000758 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000759}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000760#endif /* FEATURE_FANCY_PING */
761
762
763#if ENABLE_PING6
Denis Vlasenko06af2162007-02-03 17:28:39 +0000764int ping6_main(int argc, char **argv);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000765int ping6_main(int argc, char **argv)
766{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000767 argv[0] = (char*)"-6";
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000768 return ping_main(argc + 1, argv - 1);
769}
770#endif
771
772/* from ping6.c:
773 * Copyright (c) 1989 The Regents of the University of California.
774 * All rights reserved.
775 *
776 * This code is derived from software contributed to Berkeley by
777 * Mike Muuss.
778 *
779 * Redistribution and use in source and binary forms, with or without
780 * modification, are permitted provided that the following conditions
781 * are met:
782 * 1. Redistributions of source code must retain the above copyright
783 * notice, this list of conditions and the following disclaimer.
784 * 2. Redistributions in binary form must reproduce the above copyright
785 * notice, this list of conditions and the following disclaimer in the
786 * documentation and/or other materials provided with the distribution.
787 *
788 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
789 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
790 *
791 * 4. Neither the name of the University nor the names of its contributors
792 * may be used to endorse or promote products derived from this software
793 * without specific prior written permission.
794 *
795 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
796 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
797 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
798 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
799 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
800 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
801 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
802 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
803 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
804 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
805 * SUCH DAMAGE.
806 */