blob: 7a9c2d1f03ffbacb8a70a6175316771807323885 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Denis Vlasenkoe9356022007-01-29 18:03:54 +000031
Joachim Nilsson714e2b72010-11-28 23:01:18 +010032//config:config PING
33//config: bool "ping"
34//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010035//config: select PLATFORM_LINUX
Joachim Nilsson714e2b72010-11-28 23:01:18 +010036//config: help
37//config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
38//config: elicit an ICMP ECHO_RESPONSE from a host or gateway.
39//config:
40//config:config PING6
41//config: bool "ping6"
42//config: default y
43//config: depends on FEATURE_IPV6 && PING
44//config: help
45//config: This will give you a ping that can talk IPv6.
46//config:
47//config:config FEATURE_FANCY_PING
48//config: bool "Enable fancy ping output"
49//config: default y
50//config: depends on PING
51//config: help
52//config: Make the output from the ping applet include statistics, and at the
53//config: same time provide full support for ICMP packets.
54
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010055/* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */
56//applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE))
57//applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE))
Joachim Nilsson714e2b72010-11-28 23:01:18 +010058
59//kbuild:lib-$(CONFIG_PING) += ping.o
60//kbuild:lib-$(CONFIG_PING6) += ping.o
61
62//usage:#if !ENABLE_FEATURE_FANCY_PING
63//usage:# define ping_trivial_usage
64//usage: "HOST"
65//usage:# define ping_full_usage "\n\n"
66//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
67//usage:# define ping6_trivial_usage
68//usage: "HOST"
69//usage:# define ping6_full_usage "\n\n"
70//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
71//usage:#else
72//usage:# define ping_trivial_usage
73//usage: "[OPTIONS] HOST"
74//usage:# define ping_full_usage "\n\n"
75//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
76//usage: "\nOptions:"
77//usage: "\n -4,-6 Force IP or IPv6 name resolution"
78//usage: "\n -c CNT Send only CNT pings"
79//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
80//usage: "\n -t TTL Set TTL"
81//usage: "\n -I IFACE/IP Use interface or IP address as source"
82//usage: "\n -W SEC Seconds to wait for the first response (default:10)"
83//usage: "\n (after all -c CNT packets are sent)"
84//usage: "\n -w SEC Seconds until ping exits (default:infinite)"
85//usage: "\n (can exit earlier with -c CNT)"
86//usage: "\n -q Quiet, only displays output at start"
87//usage: "\n and when finished"
88//usage:
89//usage:# define ping6_trivial_usage
90//usage: "[OPTIONS] HOST"
91//usage:# define ping6_full_usage "\n\n"
92//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
93//usage: "\nOptions:"
94//usage: "\n -c CNT Send only CNT pings"
95//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
96//usage: "\n -I IFACE/IP Use interface or IP address as source"
97//usage: "\n -q Quiet, only displays output at start"
98//usage: "\n and when finished"
99//usage:
100//usage:#endif
101//usage:
102//usage:#define ping_example_usage
103//usage: "$ ping localhost\n"
104//usage: "PING slag (127.0.0.1): 56 data bytes\n"
105//usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
106//usage: "\n"
107//usage: "--- debian ping statistics ---\n"
108//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
109//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
110//usage:#define ping6_example_usage
111//usage: "$ ping6 ip6-localhost\n"
112//usage: "PING ip6-localhost (::1): 56 data bytes\n"
113//usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
114//usage: "\n"
115//usage: "--- ip6-localhost ping statistics ---\n"
116//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
117//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
118
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000119#if ENABLE_PING6
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100120# include <netinet/icmp6.h>
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000121/* I see RENUMBERED constants in bits/in.h - !!?
122 * What a fuck is going on with libc? Is it a glibc joke? */
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100123# ifdef IPV6_2292HOPLIMIT
124# undef IPV6_HOPLIMIT
125# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
126# endif
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000127#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000128
Rob Landleybc68cd12006-03-10 19:22:06 +0000129enum {
130 DEFDATALEN = 56,
131 MAXIPLEN = 60,
132 MAXICMPLEN = 76,
Rob Landleybc68cd12006-03-10 19:22:06 +0000133 MAX_DUP_CHK = (8 * 128),
134 MAXWAIT = 10,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000135 PINGINTERVAL = 1, /* 1 second */
Rob Landleybc68cd12006-03-10 19:22:06 +0000136};
Eric Andersen485b9551999-12-07 23:14:59 +0000137
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100138/* Common routines */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000139
Eric Andersen485b9551999-12-07 23:14:59 +0000140static int in_cksum(unsigned short *buf, int sz)
141{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 int nleft = sz;
143 int sum = 0;
144 unsigned short *w = buf;
145 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000146
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 while (nleft > 1) {
148 sum += *w++;
149 nleft -= 2;
150 }
Eric Andersen485b9551999-12-07 23:14:59 +0000151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (nleft == 1) {
153 *(unsigned char *) (&ans) = *(unsigned char *) w;
154 sum += ans;
155 }
Eric Andersen485b9551999-12-07 23:14:59 +0000156
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 sum = (sum >> 16) + (sum & 0xFFFF);
158 sum += (sum >> 16);
159 ans = ~sum;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000160 return ans;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161}
Eric Andersen485b9551999-12-07 23:14:59 +0000162
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000163#if !ENABLE_FEATURE_FANCY_PING
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000164
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100165/* Simple version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000166
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100167struct globals {
168 char *hostname;
169 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
170} FIX_ALIASING;
171#define G (*(struct globals*)&bb_common_bufsiz1)
172#define INIT_G() do { } while (0)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000173
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000174static void noresp(int ign UNUSED_PARAM)
Eric Andersenb5474c42002-03-20 11:59:28 +0000175{
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100176 printf("No response from %s\n", G.hostname);
Eric Andersen4e486a52003-01-12 06:08:33 +0000177 exit(EXIT_FAILURE);
Eric Andersenb5474c42002-03-20 11:59:28 +0000178}
Eric Andersen19db07b1999-12-11 08:41:28 +0000179
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000180static void ping4(len_and_sockaddr *lsa)
Eric Andersen19db07b1999-12-11 08:41:28 +0000181{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 struct icmp *pkt;
183 int pingsock, c;
Eric Andersen19db07b1999-12-11 08:41:28 +0000184
Matt Kraai06ef1652001-07-13 20:56:27 +0000185 pingsock = create_icmp_socket();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100187 pkt = (struct icmp *) G.packet;
188 memset(pkt, 0, sizeof(G.packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 pkt->icmp_type = ICMP_ECHO;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100190 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100192 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 /* listen for replies */
195 while (1) {
196 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000197 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100199 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
Denis Vlasenko806116b2006-12-31 12:14:16 +0000200 (struct sockaddr *) &from, &fromlen);
201 if (c < 0) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000202 if (errno != EINTR)
203 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 continue;
205 }
206 if (c >= 76) { /* ip + icmp */
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100207 struct iphdr *iphdr = (struct iphdr *) G.packet;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100209 pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 if (pkt->icmp_type == ICMP_ECHOREPLY)
211 break;
212 }
213 }
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000214 if (ENABLE_FEATURE_CLEAN_UP)
215 close(pingsock);
Eric Andersen19db07b1999-12-11 08:41:28 +0000216}
217
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000218#if ENABLE_PING6
219static void ping6(len_and_sockaddr *lsa)
220{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000221 struct icmp6_hdr *pkt;
222 int pingsock, c;
223 int sockopt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000224
225 pingsock = create_icmp6_socket();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000226
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100227 pkt = (struct icmp6_hdr *) G.packet;
228 memset(pkt, 0, sizeof(G.packet));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000229 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
230
231 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
232 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
233
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100234 xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000235
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000236 /* listen for replies */
237 while (1) {
238 struct sockaddr_in6 from;
239 socklen_t fromlen = sizeof(from);
240
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100241 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000242 (struct sockaddr *) &from, &fromlen);
243 if (c < 0) {
244 if (errno != EINTR)
245 bb_perror_msg("recvfrom");
246 continue;
247 }
Bernhard Reutner-Fischerdfd38a42010-01-23 12:52:40 +0100248 if (c >= ICMP_MINLEN) { /* icmp6_hdr */
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100249 pkt = (struct icmp6_hdr *) G.packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000250 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
251 break;
252 }
253 }
254 if (ENABLE_FEATURE_CLEAN_UP)
255 close(pingsock);
256}
257#endif
258
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100259#if !ENABLE_PING6
260# define common_ping_main(af, argv) common_ping_main(argv)
261#endif
262static int common_ping_main(sa_family_t af, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000263{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000264 len_and_sockaddr *lsa;
Denis Vlasenko3483e852007-07-02 15:47:52 +0000265
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100266 INIT_G();
267
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100268#if ENABLE_PING6
Denis Vlasenko3483e852007-07-02 15:47:52 +0000269 while ((++argv)[0] && argv[0][0] == '-') {
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000270 if (argv[0][1] == '4') {
271 af = AF_INET;
272 continue;
273 }
274 if (argv[0][1] == '6') {
275 af = AF_INET6;
276 continue;
277 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000278 bb_show_usage();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000279 }
280#else
281 argv++;
282#endif
283
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100284 G.hostname = *argv;
285 if (!G.hostname)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000286 bb_show_usage();
287
288#if ENABLE_PING6
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100289 lsa = xhost_and_af2sockaddr(G.hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000290#else
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100291 lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000292#endif
293 /* Set timer _after_ DNS resolution */
294 signal(SIGALRM, noresp);
295 alarm(5); /* give the host 5000ms to respond */
296
297#if ENABLE_PING6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000298 if (lsa->u.sa.sa_family == AF_INET6)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000299 ping6(lsa);
300 else
301#endif
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000302 ping4(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100303 printf("%s is alive!\n", G.hostname);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000304 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000305}
306
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000307
308#else /* FEATURE_FANCY_PING */
309
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000310
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100311/* Full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000312
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100313#define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000314enum {
315 OPT_QUIET = 1 << 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000316 OPT_VERBOSE = 1 << 1,
317 OPT_c = 1 << 2,
318 OPT_s = 1 << 3,
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100319 OPT_t = 1 << 4,
320 OPT_w = 1 << 5,
321 OPT_W = 1 << 6,
322 OPT_I = 1 << 7,
323 OPT_IPV4 = 1 << 8,
324 OPT_IPV6 = (1 << 9) * ENABLE_PING6,
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000325};
326
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000327
Denis Vlasenko821cc252007-06-04 10:33:48 +0000328struct globals {
329 int pingsock;
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000330 int if_index;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000331 char *str_I;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000332 len_and_sockaddr *source_lsa;
333 unsigned datalen;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000334 unsigned pingcount; /* must be int-sized */
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100335 unsigned opt_ttl;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000336 unsigned long ntransmitted, nreceived, nrepeats;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000337 uint16_t myid;
Denis Vlasenko76791452007-06-18 08:55:57 +0000338 unsigned tmin, tmax; /* in us */
339 unsigned long long tsum; /* in us, sum of all times */
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000340 unsigned deadline;
341 unsigned timeout;
342 unsigned total_secs;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100343 unsigned sizeof_rcv_packet;
344 char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */
345 void *snd_packet; /* [datalen + ipv4/ipv6_const] */
Denis Vlasenko821cc252007-06-04 10:33:48 +0000346 const char *hostname;
347 const char *dotted;
348 union {
349 struct sockaddr sa;
350 struct sockaddr_in sin;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000351#if ENABLE_PING6
Denis Vlasenko821cc252007-06-04 10:33:48 +0000352 struct sockaddr_in6 sin6;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000353#endif
Denis Vlasenko821cc252007-06-04 10:33:48 +0000354 } pingaddr;
355 char rcvd_tbl[MAX_DUP_CHK / 8];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100356} FIX_ALIASING;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000357#define G (*(struct globals*)&bb_common_bufsiz1)
358#define pingsock (G.pingsock )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000359#define if_index (G.if_index )
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000360#define source_lsa (G.source_lsa )
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000361#define str_I (G.str_I )
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000362#define datalen (G.datalen )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000363#define ntransmitted (G.ntransmitted)
364#define nreceived (G.nreceived )
365#define nrepeats (G.nrepeats )
366#define pingcount (G.pingcount )
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100367#define opt_ttl (G.opt_ttl )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000368#define myid (G.myid )
369#define tmin (G.tmin )
370#define tmax (G.tmax )
371#define tsum (G.tsum )
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000372#define deadline (G.deadline )
373#define timeout (G.timeout )
374#define total_secs (G.total_secs )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000375#define hostname (G.hostname )
376#define dotted (G.dotted )
377#define pingaddr (G.pingaddr )
378#define rcvd_tbl (G.rcvd_tbl )
379void BUG_ping_globals_too_big(void);
380#define INIT_G() do { \
Denis Vlasenko6b404432008-01-07 16:13:14 +0000381 if (sizeof(G) > COMMON_BUFSIZE) \
382 BUG_ping_globals_too_big(); \
Denis Vlasenko821cc252007-06-04 10:33:48 +0000383 pingsock = -1; \
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000384 datalen = DEFDATALEN; \
385 timeout = MAXWAIT; \
Denis Vlasenko821cc252007-06-04 10:33:48 +0000386 tmin = UINT_MAX; \
387} while (0)
Eric Andersen19db07b1999-12-11 08:41:28 +0000388
Eric Andersen19db07b1999-12-11 08:41:28 +0000389
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200390#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
391#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
392#define SET(bit) (A(bit) |= B(bit))
393#define CLR(bit) (A(bit) &= (~B(bit)))
394#define TST(bit) (A(bit) & B(bit))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000395
Eric Andersen19db07b1999-12-11 08:41:28 +0000396/**************************************************************************/
397
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000398static void print_stats_and_exit(int junk) NORETURN;
399static void print_stats_and_exit(int junk UNUSED_PARAM)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400{
401 signal(SIGINT, SIG_IGN);
402
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000403 printf("\n--- %s ping statistics ---\n", hostname);
Denis Vlasenko13858992006-10-08 12:49:22 +0000404 printf("%lu packets transmitted, ", ntransmitted);
405 printf("%lu packets received, ", nreceived);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000407 printf("%lu duplicates, ", nrepeats);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 if (ntransmitted)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000409 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000410 printf("%lu%% packet loss\n", ntransmitted);
Denis Vlasenko76791452007-06-18 08:55:57 +0000411 if (tmin != UINT_MAX) {
412 unsigned tavg = tsum / (nreceived + nrepeats);
413 printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
414 tmin / 1000, tmin % 1000,
415 tavg / 1000, tavg % 1000,
416 tmax / 1000, tmax % 1000);
417 }
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000418 /* if condition is true, exit with 1 -- 'failure' */
419 exit(nreceived == 0 || (deadline && nreceived < pingcount));
Eric Andersen485b9551999-12-07 23:14:59 +0000420}
421
Denys Vlasenko281e7b82011-02-06 17:51:45 +0100422static void sendping_tail(void (*sp)(int), int size_pkt)
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000423{
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000424 int sz;
425
426 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
427 ntransmitted++;
428
Denys Vlasenko281e7b82011-02-06 17:51:45 +0100429 size_pkt += datalen;
430
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000431 /* sizeof(pingaddr) can be larger than real sa size, but I think
432 * it doesn't matter */
Denys Vlasenko281e7b82011-02-06 17:51:45 +0100433 sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr));
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000434 if (sz != size_pkt)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000435 bb_error_msg_and_die(bb_msg_write_error);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000436
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000437 if (pingcount == 0 || deadline || ntransmitted < pingcount) {
438 /* Didn't send all pings yet - schedule next in 1s */
439 signal(SIGALRM, sp);
440 if (deadline) {
441 total_secs += PINGINTERVAL;
442 if (total_secs >= deadline)
443 signal(SIGALRM, print_stats_and_exit);
444 }
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000445 alarm(PINGINTERVAL);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000446 } else { /* -c NN, and all NN are sent (and no deadline) */
447 /* Wait for the last ping to come back.
448 * -W timeout: wait for a response in seconds.
449 * Affects only timeout in absense of any responses,
450 * otherwise ping waits for two RTTs. */
451 unsigned expire = timeout;
452
453 if (nreceived) {
454 /* approx. 2*tmax, in seconds (2 RTT) */
455 expire = tmax / (512*1024);
456 if (expire == 0)
457 expire = 1;
458 }
459 signal(SIGALRM, print_stats_and_exit);
460 alarm(expire);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000461 }
462}
463
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000464static void sendping4(int junk UNUSED_PARAM)
Eric Andersen485b9551999-12-07 23:14:59 +0000465{
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100466 struct icmp *pkt = G.snd_packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000467
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100468 //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 pkt->icmp_type = ICMP_ECHO;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000470 /*pkt->icmp_code = 0;*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100471 pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000472 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 pkt->icmp_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000474
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100475 /* If datalen < 4, we store timestamp _past_ the packet,
476 * but it's ok - we allocated 4 extra bytes in xzalloc() just in case.
477 */
Denis Vlasenko76791452007-06-18 08:55:57 +0000478 /*if (datalen >= 4)*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100479 /* No hton: we'll read it back on the same machine */
Denis Vlasenko76791452007-06-18 08:55:57 +0000480 *(uint32_t*)&pkt->icmp_dun = monotonic_us();
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000481
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000482 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000483
Denys Vlasenko281e7b82011-02-06 17:51:45 +0100484 sendping_tail(sendping4, ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000485}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000486#if ENABLE_PING6
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000487static void sendping6(int junk UNUSED_PARAM)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000488{
Denys Vlasenko1bb52a92011-02-05 03:58:43 +0100489 struct icmp6_hdr *pkt = G.snd_packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000490
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100491 //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000492 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000493 /*pkt->icmp6_code = 0;*/
494 /*pkt->icmp6_cksum = 0;*/
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000495 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
496 pkt->icmp6_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000497
Denis Vlasenko76791452007-06-18 08:55:57 +0000498 /*if (datalen >= 4)*/
499 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000500
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100501 //TODO? pkt->icmp_cksum = in_cksum(...);
502
Denys Vlasenko281e7b82011-02-06 17:51:45 +0100503 sendping_tail(sendping6, sizeof(struct icmp6_hdr));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000504}
505#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000507static const char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000508{
509 switch (id) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000510 case ICMP_ECHOREPLY: return "Echo Reply";
511 case ICMP_DEST_UNREACH: return "Destination Unreachable";
512 case ICMP_SOURCE_QUENCH: return "Source Quench";
513 case ICMP_REDIRECT: return "Redirect (change route)";
514 case ICMP_ECHO: return "Echo Request";
515 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
516 case ICMP_PARAMETERPROB: return "Parameter Problem";
517 case ICMP_TIMESTAMP: return "Timestamp Request";
518 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
519 case ICMP_INFO_REQUEST: return "Information Request";
520 case ICMP_INFO_REPLY: return "Information Reply";
521 case ICMP_ADDRESS: return "Address Mask Request";
522 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
523 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000524 }
525}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000526#if ENABLE_PING6
527/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
528 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
529#ifndef MLD_LISTENER_QUERY
530# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
531#endif
532#ifndef MLD_LISTENER_REPORT
533# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
534#endif
535#ifndef MLD_LISTENER_REDUCTION
536# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
537#endif
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000538static const char *icmp6_type_name(int id)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000539{
540 switch (id) {
541 case ICMP6_DST_UNREACH: return "Destination Unreachable";
542 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
543 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
544 case ICMP6_PARAM_PROB: return "Parameter Problem";
545 case ICMP6_ECHO_REPLY: return "Echo Reply";
546 case ICMP6_ECHO_REQUEST: return "Echo Request";
547 case MLD_LISTENER_QUERY: return "Listener Query";
548 case MLD_LISTENER_REPORT: return "Listener Report";
549 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
550 default: return "unknown ICMP type";
551 }
552}
553#endif
Erik Andersen227a59b2000-04-25 23:24:55 +0000554
Denis Vlasenko76791452007-06-18 08:55:57 +0000555static void unpack_tail(int sz, uint32_t *tp,
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000556 const char *from_str,
557 uint16_t recv_seq, int ttl)
558{
559 const char *dupmsg = " (DUP!)";
560 unsigned triptime = triptime; /* for gcc */
561
562 ++nreceived;
563
564 if (tp) {
Denis Vlasenko76791452007-06-18 08:55:57 +0000565 /* (int32_t) cast is for hypothetical 64-bit unsigned */
566 /* (doesn't hurt 32-bit real-world anyway) */
567 triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000568 tsum += triptime;
569 if (triptime < tmin)
570 tmin = triptime;
571 if (triptime > tmax)
572 tmax = triptime;
573 }
574
575 if (TST(recv_seq % MAX_DUP_CHK)) {
576 ++nrepeats;
577 --nreceived;
578 } else {
579 SET(recv_seq % MAX_DUP_CHK);
580 dupmsg += 7;
581 }
582
583 if (option_mask32 & OPT_QUIET)
584 return;
585
586 printf("%d bytes from %s: seq=%u ttl=%d", sz,
587 from_str, recv_seq, ttl);
588 if (tp)
Denis Vlasenko76791452007-06-18 08:55:57 +0000589 printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000590 puts(dupmsg);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100591 fflush_all();
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000592}
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000593static void unpack4(char *buf, int sz, struct sockaddr_in *from)
Eric Andersen485b9551999-12-07 23:14:59 +0000594{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 struct icmp *icmppkt;
596 struct iphdr *iphdr;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000597 int hlen;
Eric Andersen485b9551999-12-07 23:14:59 +0000598
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000600 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000602
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000603 /* check IP header */
604 iphdr = (struct iphdr *) buf;
605 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 sz -= hlen;
607 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000608 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000609 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000610
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000612 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000613 uint32_t *tp = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614
Denis Vlasenko76791452007-06-18 08:55:57 +0000615 if (sz >= ICMP_MINLEN + sizeof(uint32_t))
616 tp = (uint32_t *) icmppkt->icmp_data;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000617 unpack_tail(sz, tp,
618 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
619 recv_seq, iphdr->ttl);
620 } else if (icmppkt->icmp_type != ICMP_ECHO) {
621 bb_error_msg("warning: got ICMP %d (%s)",
622 icmppkt->icmp_type,
623 icmp_type_name(icmppkt->icmp_type));
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000624 }
Eric Andersen485b9551999-12-07 23:14:59 +0000625}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000626#if ENABLE_PING6
Denis Vlasenko68404f12008-03-17 09:00:54 +0000627static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000628{
629 struct icmp6_hdr *icmppkt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000630 char buf[INET6_ADDRSTRLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000631
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000632 /* discard if too short */
633 if (sz < (datalen + sizeof(struct icmp6_hdr)))
634 return;
635
636 icmppkt = (struct icmp6_hdr *) packet;
637 if (icmppkt->icmp6_id != myid)
638 return; /* not our ping */
639
640 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
641 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000642 uint32_t *tp = NULL;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000643
Denis Vlasenko76791452007-06-18 08:55:57 +0000644 if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
645 tp = (uint32_t *) &icmppkt->icmp6_data8[4];
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000646 unpack_tail(sz, tp,
647 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
648 buf, sizeof(buf)),
649 recv_seq, hoplimit);
650 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
651 bb_error_msg("warning: got ICMP %d (%s)",
652 icmppkt->icmp6_type,
653 icmp6_type_name(icmppkt->icmp6_type));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000654 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000655}
656#endif
657
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000658static void ping4(len_and_sockaddr *lsa)
Eric Andersen485b9551999-12-07 23:14:59 +0000659{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 int sockopt;
661
Matt Kraai06ef1652001-07-13 20:56:27 +0000662 pingsock = create_icmp_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000663 pingaddr.sin = lsa->u.sin;
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000664 if (source_lsa) {
665 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000666 &source_lsa->u.sa, source_lsa->len))
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000667 bb_error_msg_and_die("can't set multicast source interface");
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000668 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000669 }
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000670 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000671 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000672
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000674 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675
Denis Vlasenko57707152008-08-24 00:02:18 +0000676 /* set recv buf (needed if we can get lots of responses: flood ping,
677 * broadcast ping etc) */
678 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenko703e2022007-01-22 14:12:08 +0000679 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680
Denys Vlasenko3c8799b2010-11-29 12:07:12 +0100681 if (opt_ttl != 0) {
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100682 setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
Denys Vlasenko3c8799b2010-11-29 12:07:12 +0100683 /* above doesnt affect packets sent to bcast IP, so... */
684 setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
685 }
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100686
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000687 signal(SIGINT, print_stats_and_exit);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000688
689 /* start the ping's going ... */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000690 sendping4(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691
692 /* listen for replies */
693 while (1) {
694 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000695 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 int c;
697
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100698 c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0,
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000699 (struct sockaddr *) &from, &fromlen);
700 if (c < 0) {
701 if (errno != EINTR)
702 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 continue;
704 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100705 unpack4(G.rcv_packet, c, &from);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000706 if (pingcount && nreceived >= pingcount)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 break;
708 }
Eric Andersen485b9551999-12-07 23:14:59 +0000709}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000710#if ENABLE_PING6
711extern int BUG_bad_offsetof_icmp6_cksum(void);
712static void ping6(len_and_sockaddr *lsa)
713{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000714 int sockopt;
715 struct msghdr msg;
716 struct sockaddr_in6 from;
717 struct iovec iov;
718 char control_buf[CMSG_SPACE(36)];
719
720 pingsock = create_icmp6_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000721 pingaddr.sin6 = lsa->u.sin6;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000722 /* untested whether "-I addr" really works for IPv6: */
723 if (source_lsa)
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000724 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000725 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000726 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000727
728#ifdef ICMP6_FILTER
729 {
730 struct icmp6_filter filt;
731 if (!(option_mask32 & OPT_VERBOSE)) {
732 ICMP6_FILTER_SETBLOCKALL(&filt);
733 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
734 } else {
735 ICMP6_FILTER_SETPASSALL(&filt);
736 }
737 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
738 sizeof(filt)) < 0)
739 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
740 }
741#endif /*ICMP6_FILTER*/
742
743 /* enable broadcast pings */
744 setsockopt_broadcast(pingsock);
745
Denis Vlasenko57707152008-08-24 00:02:18 +0000746 /* set recv buf (needed if we can get lots of responses: flood ping,
747 * broadcast ping etc) */
748 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000749 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
750
751 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
752 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
753 BUG_bad_offsetof_icmp6_cksum();
754 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
755
756 /* request ttl info to be returned in ancillary data */
757 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
758
759 if (if_index)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000760 pingaddr.sin6.sin6_scope_id = if_index;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000761
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000762 signal(SIGINT, print_stats_and_exit);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000763
764 /* start the ping's going ... */
765 sendping6(0);
766
767 /* listen for replies */
768 msg.msg_name = &from;
769 msg.msg_namelen = sizeof(from);
770 msg.msg_iov = &iov;
771 msg.msg_iovlen = 1;
772 msg.msg_control = control_buf;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100773 iov.iov_base = G.rcv_packet;
774 iov.iov_len = G.sizeof_rcv_packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000775 while (1) {
776 int c;
777 struct cmsghdr *mp;
778 int hoplimit = -1;
779 msg.msg_controllen = sizeof(control_buf);
780
781 c = recvmsg(pingsock, &msg, 0);
782 if (c < 0) {
783 if (errno != EINTR)
784 bb_perror_msg("recvfrom");
785 continue;
786 }
787 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
788 if (mp->cmsg_level == SOL_IPV6
789 && mp->cmsg_type == IPV6_HOPLIMIT
790 /* don't check len - we trust the kernel: */
791 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
792 ) {
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100793 /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */
794 move_from_unaligned_int(hoplimit, CMSG_DATA(mp));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000795 }
796 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100797 unpack6(G.rcv_packet, c, /*&from,*/ hoplimit);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000798 if (pingcount && nreceived >= pingcount)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000799 break;
800 }
801}
802#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000803
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000804static void ping(len_and_sockaddr *lsa)
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000805{
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000806 printf("PING %s (%s)", hostname, dotted);
807 if (source_lsa) {
808 printf(" from %s",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000809 xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000810 }
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000811 printf(": %d data bytes\n", datalen);
812
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100813 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
814 G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000815#if ENABLE_PING6
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100816 if (lsa->u.sa.sa_family == AF_INET6) {
817 /* +4 reserves a place for timestamp, which may end up sitting
818 * _after_ packet. Saves one if() - see sendping4/6() */
819 G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000820 ping6(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100821 } else
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000822#endif
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100823 {
824 G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000825 ping4(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100826 }
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000827}
828
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100829static int common_ping_main(int opt, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000830{
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000831 len_and_sockaddr *lsa;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000832 char *str_s;
Eric Andersen485b9551999-12-07 23:14:59 +0000833
Denis Vlasenko821cc252007-06-04 10:33:48 +0000834 INIT_G();
835
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100836 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
837 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
838 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000839 if (opt & OPT_s)
840 datalen = xatou16(str_s); // -s
841 if (opt & OPT_I) { // -I
842 if_index = if_nametoindex(str_I);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000843 if (!if_index) {
844 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000845 source_lsa = xdotted2sockaddr(str_I, 0);
846 str_I = NULL; /* don't try to bind to device later */
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000847 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 }
Denis Vlasenko1c9ad622007-05-27 00:53:41 +0000849 myid = (uint16_t) getpid();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000850 hostname = argv[optind];
851#if ENABLE_PING6
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000852 {
853 sa_family_t af = AF_UNSPEC;
854 if (opt & OPT_IPV4)
855 af = AF_INET;
856 if (opt & OPT_IPV6)
857 af = AF_INET6;
858 lsa = xhost_and_af2sockaddr(hostname, 0, af);
859 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000860#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000861 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000862#endif
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000863
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000864 if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family)
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000865 /* leaking it here... */
866 source_lsa = NULL;
867
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000868 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000869 ping(lsa);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000870 print_stats_and_exit(EXIT_SUCCESS);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000871 /*return EXIT_SUCCESS;*/
Eric Andersen485b9551999-12-07 23:14:59 +0000872}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000873#endif /* FEATURE_FANCY_PING */
874
875
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100876int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
877int ping_main(int argc UNUSED_PARAM, char **argv)
878{
879#if !ENABLE_FEATURE_FANCY_PING
880 return common_ping_main(AF_UNSPEC, argv);
881#else
882 return common_ping_main(0, argv);
883#endif
884}
885
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000886#if ENABLE_PING6
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000887int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000888int ping6_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000889{
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100890# if !ENABLE_FEATURE_FANCY_PING
891 return common_ping_main(AF_INET6, argv);
892# else
893 return common_ping_main(OPT_IPV6, argv);
894# endif
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000895}
896#endif
897
898/* from ping6.c:
899 * Copyright (c) 1989 The Regents of the University of California.
900 * All rights reserved.
901 *
902 * This code is derived from software contributed to Berkeley by
903 * Mike Muuss.
904 *
905 * Redistribution and use in source and binary forms, with or without
906 * modification, are permitted provided that the following conditions
907 * are met:
908 * 1. Redistributions of source code must retain the above copyright
909 * notice, this list of conditions and the following disclaimer.
910 * 2. Redistributions in binary form must reproduce the above copyright
911 * notice, this list of conditions and the following disclaimer in the
912 * documentation and/or other materials provided with the distribution.
913 *
914 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
915 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
916 *
917 * 4. Neither the name of the University nor the names of its contributors
918 * may be used to endorse or promote products derived from this software
919 * without specific prior written permission.
920 *
921 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
922 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
923 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
924 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
925 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
926 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
927 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
928 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
929 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
930 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
931 * SUCH DAMAGE.
932 */