blob: 72867f346a4e7fd022652ec2bbe082194ae14b41 [file] [log] [blame]
Eric Andersen51b8bd62002-07-03 11:46:38 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $
Eric Andersen51b8bd62002-07-03 11:46:38 +00004 * Mini ping implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This version of ping is adapted from the ping in netkit-base 0.10,
23 * which is:
24 *
25 * Copyright (c) 1989 The Regents of the University of California.
26 * All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Mike Muuss.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000030 *
Eric Andersen51b8bd62002-07-03 11:46:38 +000031 * Original copyright notice is retained at the end of this file.
32 *
33 * This version is an adaptation of ping.c from busybox.
34 * The code was modified by Bart Visscher <magick@linux-fan.com>
35 */
36
37#include <sys/param.h>
38#include <sys/socket.h>
39#include <sys/file.h>
40#include <sys/time.h>
41#include <sys/times.h>
42#include <sys/signal.h>
43
44#include <netinet/in.h>
45#include <netinet/ip6.h>
46#include <netinet/icmp6.h>
47#include <arpa/inet.h>
48#include <net/if.h>
49#include <netdb.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <errno.h>
53#include <unistd.h>
54#include <string.h>
55#include <stdlib.h>
56#include <stddef.h> /* offsetof */
57#include "busybox.h"
58
59static const int DEFDATALEN = 56;
60static const int MAXIPLEN = 60;
61static const int MAXICMPLEN = 76;
62static const int MAXPACKET = 65468;
63#define MAX_DUP_CHK (8 * 128)
64static const int MAXWAIT = 10;
65static const int PINGINTERVAL = 1; /* second */
66
67#define O_QUIET (1 << 0)
68#define O_VERBOSE (1 << 1)
69
70#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
71#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
72#define SET(bit) (A(bit) |= B(bit))
73#define CLR(bit) (A(bit) &= (~B(bit)))
74#define TST(bit) (A(bit) & B(bit))
75
76static void ping(const char *host);
77
78/* simple version */
79#ifndef CONFIG_FEATURE_FANCY_PING6
Eric Andersen787ff552003-05-22 07:10:22 +000080static struct hostent *h;
81
Eric Andersen4e486a52003-01-12 06:08:33 +000082void noresp(int ign)
83{
84 printf("No response from %s\n", h->h_name);
85 exit(EXIT_FAILURE);
86}
Eric Andersen51b8bd62002-07-03 11:46:38 +000087
88static void ping(const char *host)
89{
Eric Andersen51b8bd62002-07-03 11:46:38 +000090 struct sockaddr_in6 pingaddr;
91 struct icmp6_hdr *pkt;
92 int pingsock, c;
93 int sockopt;
94 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
95
Eric Andersen51b8bd62002-07-03 11:46:38 +000096 pingsock = create_icmp6_socket();
97
98 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
99
100 pingaddr.sin6_family = AF_INET6;
101 h = xgethostbyname2(host, AF_INET6);
102 memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
103
104 pkt = (struct icmp6_hdr *) packet;
105 memset(pkt, 0, sizeof(packet));
106 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
107
108 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
109 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
110 sizeof(sockopt));
111
112 c = sendto(pingsock, packet, sizeof(packet), 0,
113 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
114
115 if (c < 0 || c != sizeof(packet))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 bb_perror_msg_and_die("sendto");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000117
118 signal(SIGALRM, noresp);
119 alarm(5); /* give the host 5000ms to respond */
120 /* listen for replies */
121 while (1) {
122 struct sockaddr_in6 from;
123 size_t fromlen = sizeof(from);
124
125 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
126 (struct sockaddr *) &from, &fromlen)) < 0) {
127 if (errno == EINTR)
128 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_perror_msg("recvfrom");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000130 continue;
131 }
132 if (c >= 8) { /* icmp6_hdr */
133 pkt = (struct icmp6_hdr *) packet;
134 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
135 break;
136 }
137 }
138 printf("%s is alive!\n", h->h_name);
139 return;
140}
141
142extern int ping6_main(int argc, char **argv)
143{
144 argc--;
145 argv++;
146 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000148 ping(*argv);
149 return EXIT_SUCCESS;
150}
151
152#else /* ! CONFIG_FEATURE_FANCY_PING6 */
153/* full(er) version */
154static struct sockaddr_in6 pingaddr;
155static int pingsock = -1;
156static int datalen; /* intentionally uninitialized to work around gcc bug */
157static char* ifname;
158
159static long ntransmitted, nreceived, nrepeats, pingcount;
160static int myid, options;
161static unsigned long tmin = ULONG_MAX, tmax, tsum;
162static char rcvd_tbl[MAX_DUP_CHK / 8];
163
Glenn L McGrathefc6fbd2002-11-26 03:03:41 +0000164# ifdef CONFIG_FEATURE_FANCY_PING
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000165extern
Glenn L McGrathefc6fbd2002-11-26 03:03:41 +0000166# endif
Eric Andersen51b8bd62002-07-03 11:46:38 +0000167 struct hostent *hostent;
168
169static void sendping(int);
170static void pingstats(int);
171static void unpack(char *, int, struct sockaddr_in6 *, int);
172
173/**************************************************************************/
174
175static void pingstats(int junk)
176{
177 int status;
178
179 signal(SIGINT, SIG_IGN);
180
181 printf("\n--- %s ping statistics ---\n", hostent->h_name);
182 printf("%ld packets transmitted, ", ntransmitted);
183 printf("%ld packets received, ", nreceived);
184 if (nrepeats)
185 printf("%ld duplicates, ", nrepeats);
186 if (ntransmitted)
187 printf("%ld%% packet loss\n",
188 (ntransmitted - nreceived) * 100 / ntransmitted);
189 if (nreceived)
190 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
191 tmin / 10, tmin % 10,
192 (tsum / (nreceived + nrepeats)) / 10,
193 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
194 if (nreceived != 0)
195 status = EXIT_SUCCESS;
196 else
197 status = EXIT_FAILURE;
198 exit(status);
199}
200
201static void sendping(int junk)
202{
203 struct icmp6_hdr *pkt;
204 int i;
205 char packet[datalen + 8];
206
207 pkt = (struct icmp6_hdr *) packet;
208
209 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
210 pkt->icmp6_code = 0;
211 pkt->icmp6_cksum = 0;
212 pkt->icmp6_seq = ntransmitted++;
213 pkt->icmp6_id = myid;
214 CLR(pkt->icmp6_seq % MAX_DUP_CHK);
215
216 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
217
218 i = sendto(pingsock, packet, sizeof(packet), 0,
219 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
220
221 if (i < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_perror_msg_and_die("sendto");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000223 else if ((size_t)i != sizeof(packet))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000225 (int)sizeof(packet));
226
227 signal(SIGALRM, sendping);
228 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
229 alarm(PINGINTERVAL);
230 } else { /* done, wait for the last ping to come back */
231 /* todo, don't necessarily need to wait so long... */
232 signal(SIGALRM, pingstats);
233 alarm(MAXWAIT);
234 }
235}
236
237static char *icmp6_type_name (int id)
238{
239 switch (id) {
240 case ICMP6_DST_UNREACH: return "Destination Unreachable";
241 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
242 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
243 case ICMP6_PARAM_PROB: return "Parameter Problem";
244 case ICMP6_ECHO_REPLY: return "Echo Reply";
245 case ICMP6_ECHO_REQUEST: return "Echo Request";
246 case ICMP6_MEMBERSHIP_QUERY: return "Membership Query";
247 case ICMP6_MEMBERSHIP_REPORT: return "Membership Report";
248 case ICMP6_MEMBERSHIP_REDUCTION: return "Membership Reduction";
249 default: return "unknown ICMP type";
250 }
251}
252
253static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
254{
255 struct icmp6_hdr *icmppkt;
256 struct timeval tv, *tp;
257 int dupflag;
258 unsigned long triptime;
259 char buf[INET6_ADDRSTRLEN];
260
261 gettimeofday(&tv, NULL);
262
263 /* discard if too short */
264 if (sz < (datalen + sizeof(struct icmp6_hdr)))
265 return;
266
267 icmppkt = (struct icmp6_hdr *) packet;
268
269 if (icmppkt->icmp6_id != myid)
270 return; /* not our ping */
271
272 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
273 ++nreceived;
274 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
275
276 if ((tv.tv_usec -= tp->tv_usec) < 0) {
277 --tv.tv_sec;
278 tv.tv_usec += 1000000;
279 }
280 tv.tv_sec -= tp->tv_sec;
281
282 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
283 tsum += triptime;
284 if (triptime < tmin)
285 tmin = triptime;
286 if (triptime > tmax)
287 tmax = triptime;
288
289 if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
290 ++nrepeats;
291 --nreceived;
292 dupflag = 1;
293 } else {
294 SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
295 dupflag = 0;
296 }
297
298 if (options & O_QUIET)
299 return;
300
301 printf("%d bytes from %s: icmp6_seq=%u", sz,
302 inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
303 buf, sizeof(buf)),
304 icmppkt->icmp6_seq);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000305 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000306 triptime / 10, triptime % 10);
307 if (dupflag)
308 printf(" (DUP!)");
309 printf("\n");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000310 } else
Eric Andersen51b8bd62002-07-03 11:46:38 +0000311 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000312 bb_error_msg("Warning: Got ICMP %d (%s)",
Eric Andersen51b8bd62002-07-03 11:46:38 +0000313 icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type));
314}
315
316static void ping(const char *host)
317{
318 char packet[datalen + MAXIPLEN + MAXICMPLEN];
319 char buf[INET6_ADDRSTRLEN];
320 int sockopt;
321 struct msghdr msg;
322 struct sockaddr_in6 from;
323 struct iovec iov;
324 char control_buf[CMSG_SPACE(36)];
325
326 pingsock = create_icmp6_socket();
327
328 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
329
330 pingaddr.sin6_family = AF_INET6;
331 hostent = xgethostbyname2(host, AF_INET6);
332 if (hostent->h_addrtype != AF_INET6)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000333 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000334
335 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
336
337#ifdef ICMP6_FILTER
338 {
339 struct icmp6_filter filt;
340 if (!(options & O_VERBOSE)) {
341 ICMP6_FILTER_SETBLOCKALL(&filt);
342#if 0
343 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
344 (options & F_NODEADDR) || (options & F_SUPTYPES))
345 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
346 else
347#endif
348 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
349 } else {
350 ICMP6_FILTER_SETPASSALL(&filt);
351 }
352 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
353 sizeof(filt)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000354 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000355 }
356#endif /*ICMP6_FILTER*/
357
358 /* enable broadcast pings */
359 sockopt = 1;
360 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
361 sizeof(sockopt));
362
363 /* set recv buf for broadcast pings */
364 sockopt = 48 * 1024;
365 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
366 sizeof(sockopt));
367
368 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
369 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
370 sizeof(sockopt));
371
372 sockopt = 1;
373 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
374 sizeof(sockopt));
375
376 if (ifname) {
377 if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000378 bb_error_msg_and_die("%s: invalid interface name", ifname);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000379 }
380
381 printf("PING %s (%s): %d data bytes\n",
382 hostent->h_name,
383 inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
384 buf, sizeof(buf)),
385 datalen);
386
387 signal(SIGINT, pingstats);
388
389 /* start the ping's going ... */
390 sendping(0);
391
392 /* listen for replies */
393 msg.msg_name=&from;
394 msg.msg_namelen=sizeof(from);
395 msg.msg_iov=&iov;
396 msg.msg_iovlen=1;
397 msg.msg_control=control_buf;
398 iov.iov_base=packet;
399 iov.iov_len=sizeof(packet);
400 while (1) {
401 int c;
402 struct cmsghdr *cmsgptr = NULL;
403 int hoplimit=-1;
404 msg.msg_controllen=sizeof(control_buf);
405
406 if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
407 if (errno == EINTR)
408 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000409 bb_perror_msg("recvfrom");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000410 continue;
411 }
412 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
413 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
414 if (cmsgptr->cmsg_level == SOL_IPV6 &&
415 cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
416 hoplimit=*(int*)CMSG_DATA(cmsgptr);
417 }
418 }
419 unpack(packet, c, &from, hoplimit);
420 if (pingcount > 0 && nreceived >= pingcount)
421 break;
422 }
423 pingstats(0);
424}
425
426extern int ping6_main(int argc, char **argv)
427{
428 char *thisarg;
429
430 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
431
432 argc--;
433 argv++;
434 options = 0;
435 /* Parse any options */
436 while (argc >= 1 && **argv == '-') {
437 thisarg = *argv;
438 thisarg++;
439 switch (*thisarg) {
440 case 'v':
441 options &= ~O_QUIET;
442 options |= O_VERBOSE;
443 break;
444 case 'q':
445 options &= ~O_VERBOSE;
446 options |= O_QUIET;
447 break;
448 case 'c':
449 if (--argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000450 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000451 argv++;
452 pingcount = atoi(*argv);
453 break;
454 case 's':
455 if (--argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000456 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000457 argv++;
458 datalen = atoi(*argv);
459 break;
460 case 'I':
461 if (--argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000462 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000463 argv++;
464 ifname = *argv;
465 break;
466 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000467 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000468 }
469 argc--;
470 argv++;
471 }
472 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000473 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000474
475 myid = getpid() & 0xFFFF;
476 ping(*argv);
477 return EXIT_SUCCESS;
478}
479#endif /* ! CONFIG_FEATURE_FANCY_PING6 */
480
481/*
482 * Copyright (c) 1989 The Regents of the University of California.
483 * All rights reserved.
484 *
485 * This code is derived from software contributed to Berkeley by
486 * Mike Muuss.
487 *
488 * Redistribution and use in source and binary forms, with or without
489 * modification, are permitted provided that the following conditions
490 * are met:
491 * 1. Redistributions of source code must retain the above copyright
492 * notice, this list of conditions and the following disclaimer.
493 * 2. Redistributions in binary form must reproduce the above copyright
494 * notice, this list of conditions and the following disclaimer in the
495 * documentation and/or other materials provided with the distribution.
496 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000497 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
498 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen51b8bd62002-07-03 11:46:38 +0000499 *
500 * 4. Neither the name of the University nor the names of its contributors
501 * may be used to endorse or promote products derived from this software
502 * without specific prior written permission.
503 *
504 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
505 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
506 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
507 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
508 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
509 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
510 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
511 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
512 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
513 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
514 * SUCH DAMAGE.
515 */