blob: 11ce24eb50791c715d9253ae9b0a2d40b83d6f11 [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
35//config: depends on PLATFORM_LINUX
36//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
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000422static void sendping_tail(void (*sp)(int), const void *pkt, 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
429 /* sizeof(pingaddr) can be larger than real sa size, but I think
430 * it doesn't matter */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000431 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000432 if (sz != size_pkt)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000433 bb_error_msg_and_die(bb_msg_write_error);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000434
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000435 if (pingcount == 0 || deadline || ntransmitted < pingcount) {
436 /* Didn't send all pings yet - schedule next in 1s */
437 signal(SIGALRM, sp);
438 if (deadline) {
439 total_secs += PINGINTERVAL;
440 if (total_secs >= deadline)
441 signal(SIGALRM, print_stats_and_exit);
442 }
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000443 alarm(PINGINTERVAL);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000444 } else { /* -c NN, and all NN are sent (and no deadline) */
445 /* Wait for the last ping to come back.
446 * -W timeout: wait for a response in seconds.
447 * Affects only timeout in absense of any responses,
448 * otherwise ping waits for two RTTs. */
449 unsigned expire = timeout;
450
451 if (nreceived) {
452 /* approx. 2*tmax, in seconds (2 RTT) */
453 expire = tmax / (512*1024);
454 if (expire == 0)
455 expire = 1;
456 }
457 signal(SIGALRM, print_stats_and_exit);
458 alarm(expire);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000459 }
460}
461
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000462static void sendping4(int junk UNUSED_PARAM)
Eric Andersen485b9551999-12-07 23:14:59 +0000463{
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100464 struct icmp *pkt = G.snd_packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000465
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100466 //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 pkt->icmp_type = ICMP_ECHO;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000468 /*pkt->icmp_code = 0;*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100469 pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000470 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 pkt->icmp_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000472
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100473 /* If datalen < 4, we store timestamp _past_ the packet,
474 * but it's ok - we allocated 4 extra bytes in xzalloc() just in case.
475 */
Denis Vlasenko76791452007-06-18 08:55:57 +0000476 /*if (datalen >= 4)*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100477 /* No hton: we'll read it back on the same machine */
Denis Vlasenko76791452007-06-18 08:55:57 +0000478 *(uint32_t*)&pkt->icmp_dun = monotonic_us();
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000479
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000480 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000481
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000482 sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000483}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000484#if ENABLE_PING6
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000485static void sendping6(int junk UNUSED_PARAM)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000486{
Denis Vlasenko76791452007-06-18 08:55:57 +0000487 struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000488
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100489 //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000490 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000491 /*pkt->icmp6_code = 0;*/
492 /*pkt->icmp6_cksum = 0;*/
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000493 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
494 pkt->icmp6_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000495
Denis Vlasenko76791452007-06-18 08:55:57 +0000496 /*if (datalen >= 4)*/
497 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000498
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100499 //TODO? pkt->icmp_cksum = in_cksum(...);
500
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000501 sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000502}
503#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000505static const char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000506{
507 switch (id) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000508 case ICMP_ECHOREPLY: return "Echo Reply";
509 case ICMP_DEST_UNREACH: return "Destination Unreachable";
510 case ICMP_SOURCE_QUENCH: return "Source Quench";
511 case ICMP_REDIRECT: return "Redirect (change route)";
512 case ICMP_ECHO: return "Echo Request";
513 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
514 case ICMP_PARAMETERPROB: return "Parameter Problem";
515 case ICMP_TIMESTAMP: return "Timestamp Request";
516 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
517 case ICMP_INFO_REQUEST: return "Information Request";
518 case ICMP_INFO_REPLY: return "Information Reply";
519 case ICMP_ADDRESS: return "Address Mask Request";
520 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
521 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000522 }
523}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000524#if ENABLE_PING6
525/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
526 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
527#ifndef MLD_LISTENER_QUERY
528# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
529#endif
530#ifndef MLD_LISTENER_REPORT
531# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
532#endif
533#ifndef MLD_LISTENER_REDUCTION
534# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
535#endif
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000536static const char *icmp6_type_name(int id)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000537{
538 switch (id) {
539 case ICMP6_DST_UNREACH: return "Destination Unreachable";
540 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
541 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
542 case ICMP6_PARAM_PROB: return "Parameter Problem";
543 case ICMP6_ECHO_REPLY: return "Echo Reply";
544 case ICMP6_ECHO_REQUEST: return "Echo Request";
545 case MLD_LISTENER_QUERY: return "Listener Query";
546 case MLD_LISTENER_REPORT: return "Listener Report";
547 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
548 default: return "unknown ICMP type";
549 }
550}
551#endif
Erik Andersen227a59b2000-04-25 23:24:55 +0000552
Denis Vlasenko76791452007-06-18 08:55:57 +0000553static void unpack_tail(int sz, uint32_t *tp,
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000554 const char *from_str,
555 uint16_t recv_seq, int ttl)
556{
557 const char *dupmsg = " (DUP!)";
558 unsigned triptime = triptime; /* for gcc */
559
560 ++nreceived;
561
562 if (tp) {
Denis Vlasenko76791452007-06-18 08:55:57 +0000563 /* (int32_t) cast is for hypothetical 64-bit unsigned */
564 /* (doesn't hurt 32-bit real-world anyway) */
565 triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000566 tsum += triptime;
567 if (triptime < tmin)
568 tmin = triptime;
569 if (triptime > tmax)
570 tmax = triptime;
571 }
572
573 if (TST(recv_seq % MAX_DUP_CHK)) {
574 ++nrepeats;
575 --nreceived;
576 } else {
577 SET(recv_seq % MAX_DUP_CHK);
578 dupmsg += 7;
579 }
580
581 if (option_mask32 & OPT_QUIET)
582 return;
583
584 printf("%d bytes from %s: seq=%u ttl=%d", sz,
585 from_str, recv_seq, ttl);
586 if (tp)
Denis Vlasenko76791452007-06-18 08:55:57 +0000587 printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000588 puts(dupmsg);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100589 fflush_all();
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000590}
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000591static void unpack4(char *buf, int sz, struct sockaddr_in *from)
Eric Andersen485b9551999-12-07 23:14:59 +0000592{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 struct icmp *icmppkt;
594 struct iphdr *iphdr;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000595 int hlen;
Eric Andersen485b9551999-12-07 23:14:59 +0000596
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000598 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000600
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000601 /* check IP header */
602 iphdr = (struct iphdr *) buf;
603 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 sz -= hlen;
605 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000606 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000607 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000608
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000610 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000611 uint32_t *tp = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612
Denis Vlasenko76791452007-06-18 08:55:57 +0000613 if (sz >= ICMP_MINLEN + sizeof(uint32_t))
614 tp = (uint32_t *) icmppkt->icmp_data;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000615 unpack_tail(sz, tp,
616 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
617 recv_seq, iphdr->ttl);
618 } else if (icmppkt->icmp_type != ICMP_ECHO) {
619 bb_error_msg("warning: got ICMP %d (%s)",
620 icmppkt->icmp_type,
621 icmp_type_name(icmppkt->icmp_type));
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000622 }
Eric Andersen485b9551999-12-07 23:14:59 +0000623}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000624#if ENABLE_PING6
Denis Vlasenko68404f12008-03-17 09:00:54 +0000625static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000626{
627 struct icmp6_hdr *icmppkt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000628 char buf[INET6_ADDRSTRLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000629
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000630 /* discard if too short */
631 if (sz < (datalen + sizeof(struct icmp6_hdr)))
632 return;
633
634 icmppkt = (struct icmp6_hdr *) packet;
635 if (icmppkt->icmp6_id != myid)
636 return; /* not our ping */
637
638 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
639 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000640 uint32_t *tp = NULL;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000641
Denis Vlasenko76791452007-06-18 08:55:57 +0000642 if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
643 tp = (uint32_t *) &icmppkt->icmp6_data8[4];
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000644 unpack_tail(sz, tp,
645 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
646 buf, sizeof(buf)),
647 recv_seq, hoplimit);
648 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
649 bb_error_msg("warning: got ICMP %d (%s)",
650 icmppkt->icmp6_type,
651 icmp6_type_name(icmppkt->icmp6_type));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000652 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000653}
654#endif
655
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000656static void ping4(len_and_sockaddr *lsa)
Eric Andersen485b9551999-12-07 23:14:59 +0000657{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 int sockopt;
659
Matt Kraai06ef1652001-07-13 20:56:27 +0000660 pingsock = create_icmp_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000661 pingaddr.sin = lsa->u.sin;
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000662 if (source_lsa) {
663 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000664 &source_lsa->u.sa, source_lsa->len))
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000665 bb_error_msg_and_die("can't set multicast source interface");
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000666 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000667 }
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000668 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000669 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000670
Erik Andersene49d5ec2000-02-08 19:58:47 +0000671 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000672 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673
Denis Vlasenko57707152008-08-24 00:02:18 +0000674 /* set recv buf (needed if we can get lots of responses: flood ping,
675 * broadcast ping etc) */
676 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenko703e2022007-01-22 14:12:08 +0000677 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678
Denys Vlasenko3c8799b2010-11-29 12:07:12 +0100679 if (opt_ttl != 0) {
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100680 setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
Denys Vlasenko3c8799b2010-11-29 12:07:12 +0100681 /* above doesnt affect packets sent to bcast IP, so... */
682 setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
683 }
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100684
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000685 signal(SIGINT, print_stats_and_exit);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000686
687 /* start the ping's going ... */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000688 sendping4(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689
690 /* listen for replies */
691 while (1) {
692 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000693 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694 int c;
695
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100696 c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0,
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000697 (struct sockaddr *) &from, &fromlen);
698 if (c < 0) {
699 if (errno != EINTR)
700 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 continue;
702 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100703 unpack4(G.rcv_packet, c, &from);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000704 if (pingcount && nreceived >= pingcount)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 break;
706 }
Eric Andersen485b9551999-12-07 23:14:59 +0000707}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000708#if ENABLE_PING6
709extern int BUG_bad_offsetof_icmp6_cksum(void);
710static void ping6(len_and_sockaddr *lsa)
711{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000712 int sockopt;
713 struct msghdr msg;
714 struct sockaddr_in6 from;
715 struct iovec iov;
716 char control_buf[CMSG_SPACE(36)];
717
718 pingsock = create_icmp6_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000719 pingaddr.sin6 = lsa->u.sin6;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000720 /* untested whether "-I addr" really works for IPv6: */
721 if (source_lsa)
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000722 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000723 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000724 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000725
726#ifdef ICMP6_FILTER
727 {
728 struct icmp6_filter filt;
729 if (!(option_mask32 & OPT_VERBOSE)) {
730 ICMP6_FILTER_SETBLOCKALL(&filt);
731 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
732 } else {
733 ICMP6_FILTER_SETPASSALL(&filt);
734 }
735 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
736 sizeof(filt)) < 0)
737 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
738 }
739#endif /*ICMP6_FILTER*/
740
741 /* enable broadcast pings */
742 setsockopt_broadcast(pingsock);
743
Denis Vlasenko57707152008-08-24 00:02:18 +0000744 /* set recv buf (needed if we can get lots of responses: flood ping,
745 * broadcast ping etc) */
746 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000747 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
748
749 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
750 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
751 BUG_bad_offsetof_icmp6_cksum();
752 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
753
754 /* request ttl info to be returned in ancillary data */
755 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
756
757 if (if_index)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000758 pingaddr.sin6.sin6_scope_id = if_index;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000759
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000760 signal(SIGINT, print_stats_and_exit);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000761
762 /* start the ping's going ... */
763 sendping6(0);
764
765 /* listen for replies */
766 msg.msg_name = &from;
767 msg.msg_namelen = sizeof(from);
768 msg.msg_iov = &iov;
769 msg.msg_iovlen = 1;
770 msg.msg_control = control_buf;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100771 iov.iov_base = G.rcv_packet;
772 iov.iov_len = G.sizeof_rcv_packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000773 while (1) {
774 int c;
775 struct cmsghdr *mp;
776 int hoplimit = -1;
777 msg.msg_controllen = sizeof(control_buf);
778
779 c = recvmsg(pingsock, &msg, 0);
780 if (c < 0) {
781 if (errno != EINTR)
782 bb_perror_msg("recvfrom");
783 continue;
784 }
785 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
786 if (mp->cmsg_level == SOL_IPV6
787 && mp->cmsg_type == IPV6_HOPLIMIT
788 /* don't check len - we trust the kernel: */
789 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
790 ) {
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100791 /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */
792 move_from_unaligned_int(hoplimit, CMSG_DATA(mp));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000793 }
794 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100795 unpack6(G.rcv_packet, c, /*&from,*/ hoplimit);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000796 if (pingcount && nreceived >= pingcount)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000797 break;
798 }
799}
800#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000801
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000802static void ping(len_and_sockaddr *lsa)
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000803{
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000804 printf("PING %s (%s)", hostname, dotted);
805 if (source_lsa) {
806 printf(" from %s",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000807 xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000808 }
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000809 printf(": %d data bytes\n", datalen);
810
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100811 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
812 G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000813#if ENABLE_PING6
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100814 if (lsa->u.sa.sa_family == AF_INET6) {
815 /* +4 reserves a place for timestamp, which may end up sitting
816 * _after_ packet. Saves one if() - see sendping4/6() */
817 G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000818 ping6(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100819 } else
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000820#endif
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100821 {
822 G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000823 ping4(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100824 }
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000825}
826
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100827static int common_ping_main(int opt, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000828{
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000829 len_and_sockaddr *lsa;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000830 char *str_s;
Eric Andersen485b9551999-12-07 23:14:59 +0000831
Denis Vlasenko821cc252007-06-04 10:33:48 +0000832 INIT_G();
833
Joachim Nilsson714e2b72010-11-28 23:01:18 +0100834 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
835 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
836 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000837 if (opt & OPT_s)
838 datalen = xatou16(str_s); // -s
839 if (opt & OPT_I) { // -I
840 if_index = if_nametoindex(str_I);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000841 if (!if_index) {
842 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000843 source_lsa = xdotted2sockaddr(str_I, 0);
844 str_I = NULL; /* don't try to bind to device later */
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000845 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000846 }
Denis Vlasenko1c9ad622007-05-27 00:53:41 +0000847 myid = (uint16_t) getpid();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000848 hostname = argv[optind];
849#if ENABLE_PING6
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000850 {
851 sa_family_t af = AF_UNSPEC;
852 if (opt & OPT_IPV4)
853 af = AF_INET;
854 if (opt & OPT_IPV6)
855 af = AF_INET6;
856 lsa = xhost_and_af2sockaddr(hostname, 0, af);
857 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000858#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000859 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000860#endif
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000861
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000862 if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family)
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000863 /* leaking it here... */
864 source_lsa = NULL;
865
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000866 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000867 ping(lsa);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000868 print_stats_and_exit(EXIT_SUCCESS);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000869 /*return EXIT_SUCCESS;*/
Eric Andersen485b9551999-12-07 23:14:59 +0000870}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000871#endif /* FEATURE_FANCY_PING */
872
873
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100874int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
875int ping_main(int argc UNUSED_PARAM, char **argv)
876{
877#if !ENABLE_FEATURE_FANCY_PING
878 return common_ping_main(AF_UNSPEC, argv);
879#else
880 return common_ping_main(0, argv);
881#endif
882}
883
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000884#if ENABLE_PING6
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000885int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000886int ping6_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000887{
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100888# if !ENABLE_FEATURE_FANCY_PING
889 return common_ping_main(AF_INET6, argv);
890# else
891 return common_ping_main(OPT_IPV6, argv);
892# endif
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000893}
894#endif
895
896/* from ping6.c:
897 * Copyright (c) 1989 The Regents of the University of California.
898 * All rights reserved.
899 *
900 * This code is derived from software contributed to Berkeley by
901 * Mike Muuss.
902 *
903 * Redistribution and use in source and binary forms, with or without
904 * modification, are permitted provided that the following conditions
905 * are met:
906 * 1. Redistributions of source code must retain the above copyright
907 * notice, this list of conditions and the following disclaimer.
908 * 2. Redistributions in binary form must reproduce the above copyright
909 * notice, this list of conditions and the following disclaimer in the
910 * documentation and/or other materials provided with the distribution.
911 *
912 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
913 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
914 *
915 * 4. Neither the name of the University nor the names of its contributors
916 * may be used to endorse or promote products derived from this software
917 * without specific prior written permission.
918 *
919 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
920 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
921 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
922 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
923 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
924 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
925 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
926 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
927 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
928 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
929 * SUCH DAMAGE.
930 */