| 1 |
/* |
| 2 |
Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#ifdef __sun |
| 22 |
# define _XPG4_2 1 /* OpenSolaris: msg_control */ |
| 23 |
#endif /* __sun */ |
| 24 |
|
| 25 |
#include <sys/socket.h> |
| 26 |
#include <sys/time.h> |
| 27 |
#include <sys/uio.h> |
| 28 |
#include <signal.h> |
| 29 |
|
| 30 |
#include <netinet/in.h> |
| 31 |
#include <netinet/ip6.h> |
| 32 |
#include <netinet/icmp6.h> |
| 33 |
#include <arpa/inet.h> |
| 34 |
|
| 35 |
#include <netdb.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <argp.h> |
| 41 |
#include <ctype.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <limits.h> |
| 44 |
#ifdef HAVE_LOCALE_H |
| 45 |
# include <locale.h> |
| 46 |
#endif |
| 47 |
#if defined HAVE_IDN2_H && defined HAVE_IDN2 |
| 48 |
# include <idn2.h> |
| 49 |
#elif defined HAVE_IDNA_H |
| 50 |
# include <idna.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#include <attribute.h> |
| 54 |
#include <xalloc.h> |
| 55 |
#include <timespec.h> |
| 56 |
#include "ping6.h" |
| 57 |
#include "libinetutils.h" |
| 58 |
|
| 59 |
/* RFC 4443 addition not yet available in libc headers */ |
| 60 |
#ifndef ICMP6_DST_UNREACH_POLICYFAIL |
| 61 |
# define ICMP6_DST_UNREACH_POLICYFAIL 5 |
| 62 |
#endif |
| 63 |
|
| 64 |
/* RFC 4443 addition not yet available in libc headers */ |
| 65 |
#ifndef ICMP6_DST_UNREACH_REJECTROUTE |
| 66 |
# define ICMP6_DST_UNREACH_REJECTROUTE 6 |
| 67 |
#endif |
| 68 |
|
| 69 |
static PING *ping; |
| 70 |
bool is_root = false; |
| 71 |
unsigned char *data_buffer; |
| 72 |
unsigned char *patptr; |
| 73 |
int one = 1; |
| 74 |
int pattern_len = MAXPATTERN; |
| 75 |
size_t data_length = PING_DATALEN; |
| 76 |
size_t count = DEFAULT_PING_COUNT; |
| 77 |
size_t interval; |
| 78 |
int socket_type; |
| 79 |
int timeout = -1; |
| 80 |
int hoplimit = 0; |
| 81 |
unsigned int options; |
| 82 |
static unsigned long preload = 0; |
| 83 |
#ifdef IPV6_TCLASS |
| 84 |
int tclass = -1; /* Kernel sets default: -1, RFC 3542. */ |
| 85 |
#endif |
| 86 |
#ifdef IPV6_FLOWINFO |
| 87 |
int flowinfo; |
| 88 |
#endif |
| 89 |
|
| 90 |
static int ping_echo (char *hostname); |
| 91 |
static void ping_reset (PING * p); |
| 92 |
static int send_echo (PING * ping); |
| 93 |
|
| 94 |
const char args_doc[] = "HOST ..."; |
| 95 |
const char doc[] = "Send ICMP ECHO_REQUEST packets to network hosts." |
| 96 |
"\vOptions marked with (root only) are available only to " "superuser."; |
| 97 |
const char *program_authors[] = { |
| 98 |
"Jeroen Dekkers", |
| 99 |
NULL |
| 100 |
}; |
| 101 |
|
| 102 |
enum |
| 103 |
{ |
| 104 |
ARG_HOPLIMIT = 256, |
| 105 |
}; |
| 106 |
|
| 107 |
static struct argp_option argp_options[] = { |
| 108 |
#define GRP 0 |
| 109 |
{NULL, 0, NULL, 0, "Options valid for all request types:", GRP}, |
| 110 |
{"count", 'c', "NUMBER", 0, "stop after sending NUMBER packets", GRP + 1}, |
| 111 |
{"debug", 'd', NULL, 0, "set the SO_DEBUG option", GRP + 1}, |
| 112 |
#ifdef IPV6_FLOWINFO |
| 113 |
{"flowinfo", 'F', "N", 0, "set N as flow identifier", GRP + 1}, |
| 114 |
#endif |
| 115 |
{"hoplimit", ARG_HOPLIMIT, "N", 0, "specify N as hop-limit", GRP + 1}, |
| 116 |
{"interval", 'i', "NUMBER", 0, "wait NUMBER seconds between sending each " |
| 117 |
"packet", GRP + 1}, |
| 118 |
{"numeric", 'n', NULL, 0, "do not resolve host addresses", GRP + 1}, |
| 119 |
{"ignore-routing", 'r', NULL, 0, "send directly to a host on an attached " |
| 120 |
"network", GRP + 1}, |
| 121 |
#ifdef IPV6_TCLASS |
| 122 |
{"tos", 'T', "N", 0, "set traffic class to N", GRP + 1}, |
| 123 |
#endif |
| 124 |
{"timeout", 'w', "N", 0, "stop after N seconds", GRP + 1}, |
| 125 |
{"ttl", ARG_HOPLIMIT, "N", 0, "synonym for --hoplimit", GRP + 1}, |
| 126 |
{"verbose", 'v', NULL, 0, "verbose output", GRP + 1}, |
| 127 |
#undef GRP |
| 128 |
#define GRP 10 |
| 129 |
{NULL, 0, NULL, 0, "Options valid for --echo requests:", GRP}, |
| 130 |
{"flood", 'f', NULL, 0, "flood ping (root only)", GRP + 1}, |
| 131 |
{"preload", 'l', "NUMBER", 0, "send NUMBER packets as fast as possible " |
| 132 |
"before falling into normal mode of behavior (root only)", GRP + 1}, |
| 133 |
{"pattern", 'p', "PATTERN", 0, "fill ICMP packet with given pattern (hex)", |
| 134 |
GRP + 1}, |
| 135 |
{"quiet", 'q', NULL, 0, "quiet output", GRP + 1}, |
| 136 |
{"size", 's', "NUMBER", 0, "send NUMBER data octets", GRP + 1}, |
| 137 |
#undef GRP |
| 138 |
{NULL, 0, NULL, 0, NULL, 0} |
| 139 |
}; |
| 140 |
|
| 141 |
static error_t |
| 142 |
parse_opt (int key, char *arg, struct argp_state *state) |
| 143 |
{ |
| 144 |
char *endptr; |
| 145 |
static unsigned char pattern[MAXPATTERN]; |
| 146 |
|
| 147 |
switch (key) |
| 148 |
{ |
| 149 |
case 'c': |
| 150 |
count = ping_cvt_number (arg, 0, 0); |
| 151 |
break; |
| 152 |
|
| 153 |
case 'd': |
| 154 |
socket_type |= SO_DEBUG; |
| 155 |
break; |
| 156 |
|
| 157 |
case 'f': |
| 158 |
if (!is_root) |
| 159 |
error (EXIT_FAILURE, 0, "flooding needs root privilege"); |
| 160 |
|
| 161 |
options |= OPT_FLOOD; |
| 162 |
setbuf (stdout, (char *) NULL); |
| 163 |
break; |
| 164 |
|
| 165 |
#ifdef IPV6_FLOWINFO |
| 166 |
case 'F': |
| 167 |
options |= OPT_FLOWINFO; |
| 168 |
flowinfo = ping_cvt_number (arg, 0, 0) & IPV6_FLOWINFO_FLOWLABEL; |
| 169 |
break; |
| 170 |
#endif |
| 171 |
|
| 172 |
case 'i': |
| 173 |
options |= OPT_INTERVAL; |
| 174 |
interval = ping_cvt_number (arg, 0, 0); |
| 175 |
interval *= PING_PRECISION; |
| 176 |
if (!is_root && interval < PING_MIN_USER_INTERVAL) |
| 177 |
error (EXIT_FAILURE, 0, "option value too small: %s", arg); |
| 178 |
break; |
| 179 |
|
| 180 |
case 'l': |
| 181 |
if (!is_root) |
| 182 |
error (EXIT_FAILURE, 0, "preloading needs root privilege"); |
| 183 |
|
| 184 |
preload = strtoul (arg, &endptr, 0); |
| 185 |
if (*endptr || preload > INT_MAX) |
| 186 |
error (EXIT_FAILURE, 0, "preload size too large"); |
| 187 |
|
| 188 |
break; |
| 189 |
|
| 190 |
case 'n': |
| 191 |
options |= OPT_NUMERIC; |
| 192 |
break; |
| 193 |
|
| 194 |
case 'p': |
| 195 |
decode_pattern (arg, &pattern_len, pattern); |
| 196 |
patptr = pattern; |
| 197 |
break; |
| 198 |
|
| 199 |
case 'q': |
| 200 |
options |= OPT_QUIET; |
| 201 |
break; |
| 202 |
|
| 203 |
case 'r': |
| 204 |
socket_type |= SO_DONTROUTE; |
| 205 |
break; |
| 206 |
|
| 207 |
case 's': |
| 208 |
data_length = ping_cvt_number (arg, PING_MAX_DATALEN, 1); |
| 209 |
break; |
| 210 |
|
| 211 |
#ifdef IPV6_TCLASS |
| 212 |
case 'T': |
| 213 |
options |= OPT_TCLASS; |
| 214 |
tclass = ping_cvt_number (arg, 0, 0); |
| 215 |
break; |
| 216 |
#endif |
| 217 |
|
| 218 |
case 'v': |
| 219 |
options |= OPT_VERBOSE; |
| 220 |
break; |
| 221 |
|
| 222 |
case 'w': |
| 223 |
timeout = ping_cvt_number (arg, INT_MAX, 0); |
| 224 |
break; |
| 225 |
|
| 226 |
case ARG_HOPLIMIT: |
| 227 |
hoplimit = ping_cvt_number (arg, 255, 0); |
| 228 |
break; |
| 229 |
|
| 230 |
case ARGP_KEY_NO_ARGS: |
| 231 |
argp_error (state, "missing host operand"); |
| 232 |
|
| 233 |
/* FALLTHROUGH */ |
| 234 |
default: |
| 235 |
return ARGP_ERR_UNKNOWN; |
| 236 |
} |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
static struct argp argp = |
| 242 |
{ argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL }; |
| 243 |
|
| 244 |
int |
| 245 |
main (int argc, char **argv) |
| 246 |
{ |
| 247 |
int index; |
| 248 |
int status = 0; |
| 249 |
|
| 250 |
set_program_name (argv[0]); |
| 251 |
|
| 252 |
#ifdef HAVE_SETLOCALE |
| 253 |
setlocale (LC_ALL, ""); |
| 254 |
#endif |
| 255 |
|
| 256 |
if (getuid () == 0) |
| 257 |
is_root = true; |
| 258 |
|
| 259 |
/* Parse command line */ |
| 260 |
iu_argp_init ("ping6", program_authors); |
| 261 |
argp_parse (&argp, argc, argv, 0, &index, NULL); |
| 262 |
|
| 263 |
ping = ping_init (0, getpid ()); |
| 264 |
if (ping == NULL) |
| 265 |
/* ping_init() prints our error message. */ |
| 266 |
exit (EXIT_FAILURE); |
| 267 |
|
| 268 |
setsockopt (ping->ping_fd, SOL_SOCKET, SO_BROADCAST, (char *) &one, |
| 269 |
sizeof (one)); |
| 270 |
|
| 271 |
/* Reset root privileges */ |
| 272 |
if (setuid (getuid ()) != 0) |
| 273 |
error (EXIT_FAILURE, errno, "setuid"); |
| 274 |
|
| 275 |
/* Force line buffering regardless of output device. */ |
| 276 |
setvbuf (stdout, NULL, _IOLBF, 0); |
| 277 |
|
| 278 |
argc -= index; |
| 279 |
argv += index; |
| 280 |
|
| 281 |
if (count != 0) |
| 282 |
ping_set_count (ping, count); |
| 283 |
|
| 284 |
if (socket_type != 0) |
| 285 |
ping_set_sockopt (ping, socket_type, &one, sizeof (one)); |
| 286 |
|
| 287 |
if (options & OPT_INTERVAL) |
| 288 |
ping_set_interval (ping, interval); |
| 289 |
|
| 290 |
if (hoplimit > 0) |
| 291 |
if (setsockopt (ping->ping_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, |
| 292 |
&hoplimit, sizeof (hoplimit)) < 0) |
| 293 |
error (0, errno, "setsockopt(IPV6_HOPLIMIT)"); |
| 294 |
|
| 295 |
#ifdef IPV6_TCLASS |
| 296 |
if (options & OPT_TCLASS) |
| 297 |
if (setsockopt (ping->ping_fd, IPPROTO_IPV6, IPV6_TCLASS, |
| 298 |
&tclass, sizeof (tclass)) < 0) |
| 299 |
error (EXIT_FAILURE, errno, "setsockopt(IPV6_TCLASS)"); |
| 300 |
#endif |
| 301 |
|
| 302 |
#ifdef IPV6_FLOWINFO |
| 303 |
if (options & OPT_FLOWINFO) |
| 304 |
if (setsockopt (ping->ping_fd, IPPROTO_IPV6, IPV6_FLOWINFO, |
| 305 |
&flowinfo, sizeof (flowinfo)) < 0) |
| 306 |
error (EXIT_FAILURE, errno, "setsockopt(IPV6_FLOWINFO)"); |
| 307 |
#endif |
| 308 |
|
| 309 |
init_data_buffer (patptr, pattern_len); |
| 310 |
|
| 311 |
while (argc--) |
| 312 |
{ |
| 313 |
status |= ping_echo (*argv++); |
| 314 |
ping_reset (ping); |
| 315 |
} |
| 316 |
|
| 317 |
return status; |
| 318 |
} |
| 319 |
|
| 320 |
static volatile int stop = 0; |
| 321 |
|
| 322 |
static void |
| 323 |
sig_int (int signal MAYBE_UNUSED) |
| 324 |
{ |
| 325 |
stop = 1; |
| 326 |
} |
| 327 |
|
| 328 |
static int |
| 329 |
ping_run (PING *ping, int (*finish) (void)) |
| 330 |
{ |
| 331 |
fd_set fdset; |
| 332 |
int fdmax; |
| 333 |
struct timespec resp_time; |
| 334 |
struct timespec last, intvl, now; |
| 335 |
struct timespec *t = NULL; |
| 336 |
int finishing = 0; |
| 337 |
size_t nresp = 0; |
| 338 |
unsigned long i; |
| 339 |
|
| 340 |
signal (SIGINT, sig_int); |
| 341 |
|
| 342 |
fdmax = ping->ping_fd + 1; |
| 343 |
|
| 344 |
for (i = 0; i < preload; i++) |
| 345 |
send_echo (ping); |
| 346 |
|
| 347 |
if (options & OPT_FLOOD) |
| 348 |
{ |
| 349 |
intvl.tv_sec = 0; |
| 350 |
intvl.tv_nsec = 1e7; |
| 351 |
} |
| 352 |
else |
| 353 |
PING_SET_INTERVAL (intvl, ping->ping_interval); |
| 354 |
|
| 355 |
last = current_timespec (); |
| 356 |
send_echo (ping); |
| 357 |
|
| 358 |
while (!stop) |
| 359 |
{ |
| 360 |
int n; |
| 361 |
|
| 362 |
FD_ZERO (&fdset); |
| 363 |
FD_SET (ping->ping_fd, &fdset); |
| 364 |
|
| 365 |
now = current_timespec (); |
| 366 |
resp_time = timespec_sub (timespec_add (last, intvl), now); |
| 367 |
|
| 368 |
if (timespec_sign (resp_time) == -1) |
| 369 |
resp_time.tv_sec = resp_time.tv_nsec = 0; |
| 370 |
|
| 371 |
n = pselect (fdmax, &fdset, NULL, NULL, &resp_time, NULL); |
| 372 |
if (n < 0) |
| 373 |
{ |
| 374 |
if (errno != EINTR) |
| 375 |
error (EXIT_FAILURE, errno, "pselect failed"); |
| 376 |
continue; |
| 377 |
} |
| 378 |
else if (n == 1) |
| 379 |
{ |
| 380 |
if (ping_recv (ping) == 0) |
| 381 |
nresp++; |
| 382 |
if (t == 0) |
| 383 |
{ |
| 384 |
now = current_timespec (); |
| 385 |
t = &now; |
| 386 |
} |
| 387 |
|
| 388 |
if (ping_timeout_p (&ping->ping_start_time, timeout)) |
| 389 |
break; |
| 390 |
|
| 391 |
if (ping->ping_count && nresp >= ping->ping_count) |
| 392 |
break; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
if (!ping->ping_count || ping->ping_num_xmit < ping->ping_count) |
| 397 |
{ |
| 398 |
send_echo (ping); |
| 399 |
if (!(options & OPT_QUIET) && options & OPT_FLOOD) |
| 400 |
{ |
| 401 |
putchar ('.'); |
| 402 |
} |
| 403 |
|
| 404 |
if (ping_timeout_p (&ping->ping_start_time, timeout)) |
| 405 |
break; |
| 406 |
} |
| 407 |
else if (finishing) |
| 408 |
break; |
| 409 |
else |
| 410 |
{ |
| 411 |
finishing = 1; |
| 412 |
|
| 413 |
intvl.tv_sec = MAXWAIT; |
| 414 |
} |
| 415 |
last = current_timespec (); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
ping_unset_data (ping); |
| 420 |
|
| 421 |
if (finish) |
| 422 |
return (*finish) (); |
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
static int |
| 427 |
send_echo (PING *ping) |
| 428 |
{ |
| 429 |
size_t off = 0; |
| 430 |
int rc; |
| 431 |
|
| 432 |
if (PING_TIMING (data_length)) |
| 433 |
{ |
| 434 |
struct timespec now = current_timespec (); |
| 435 |
/* *INDENT-OFF* */ |
| 436 |
struct timeval tv = { .tv_sec = now.tv_sec, |
| 437 |
.tv_usec = now.tv_nsec / 1000 }; |
| 438 |
/* *INDENT-ON* */ |
| 439 |
ping_set_data (ping, &tv, 0, sizeof (tv), USE_IPV6); |
| 440 |
off += sizeof (tv); |
| 441 |
} |
| 442 |
if (data_buffer) |
| 443 |
ping_set_data (ping, data_buffer, off, |
| 444 |
data_length > off ? data_length - off : data_length, |
| 445 |
USE_IPV6); |
| 446 |
|
| 447 |
rc = ping_xmit (ping); |
| 448 |
if (rc < 0) |
| 449 |
error (EXIT_FAILURE, errno, "sending packet"); |
| 450 |
|
| 451 |
return rc; |
| 452 |
} |
| 453 |
|
| 454 |
static int |
| 455 |
ping_finish (void) |
| 456 |
{ |
| 457 |
fflush (stdout); |
| 458 |
printf ("--- %s ping statistics ---\n", ping->ping_hostname); |
| 459 |
printf ("%zu packets transmitted, ", ping->ping_num_xmit); |
| 460 |
printf ("%zu packets received, ", ping->ping_num_recv); |
| 461 |
if (ping->ping_num_rept) |
| 462 |
printf ("+%zu duplicates, ", ping->ping_num_rept); |
| 463 |
if (ping->ping_num_xmit) |
| 464 |
{ |
| 465 |
if (ping->ping_num_recv > ping->ping_num_xmit) |
| 466 |
printf ("-- somebody's printing up packets!"); |
| 467 |
else |
| 468 |
printf ("%d%% packet loss", |
| 469 |
(int) (((ping->ping_num_xmit - ping->ping_num_recv) * 100) / |
| 470 |
ping->ping_num_xmit)); |
| 471 |
|
| 472 |
} |
| 473 |
printf ("\n"); |
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
static int print_echo (int dup, int hops, struct ping_stat *stat, |
| 478 |
struct sockaddr_in6 *dest, struct sockaddr_in6 *from, |
| 479 |
struct icmp6_hdr *icmp6, int datalen); |
| 480 |
static void print_icmp_error (struct sockaddr_in6 *from, |
| 481 |
struct icmp6_hdr *icmp6, int len); |
| 482 |
|
| 483 |
static int echo_finish (void); |
| 484 |
|
| 485 |
static int |
| 486 |
ping_echo (char *hostname) |
| 487 |
{ |
| 488 |
int err; |
| 489 |
char buffer[256]; |
| 490 |
struct ping_stat ping_stat; |
| 491 |
int status; |
| 492 |
|
| 493 |
if (options & OPT_FLOOD && options & OPT_INTERVAL) |
| 494 |
error (EXIT_FAILURE, 0, "-f and -i incompatible options"); |
| 495 |
|
| 496 |
memset (&ping_stat, 0, sizeof (ping_stat)); |
| 497 |
ping_stat.tmin = 999999999.0; |
| 498 |
|
| 499 |
ping->ping_datalen = data_length; |
| 500 |
ping->ping_closure = &ping_stat; |
| 501 |
|
| 502 |
if (ping_set_dest (ping, hostname)) |
| 503 |
error (EXIT_FAILURE, 0, "unknown host %s", hostname); |
| 504 |
|
| 505 |
err = getnameinfo ((struct sockaddr *) &ping->ping_dest.ping_sockaddr6, |
| 506 |
sizeof (ping->ping_dest.ping_sockaddr6), buffer, |
| 507 |
sizeof (buffer), NULL, 0, NI_NUMERICHOST); |
| 508 |
if (err) |
| 509 |
{ |
| 510 |
const char *errmsg; |
| 511 |
|
| 512 |
if (err == EAI_SYSTEM) |
| 513 |
errmsg = strerror (errno); |
| 514 |
else |
| 515 |
errmsg = gai_strerror (err); |
| 516 |
|
| 517 |
error (EXIT_FAILURE, 0, "getnameinfo: %s", errmsg); |
| 518 |
} |
| 519 |
|
| 520 |
printf ("PING %s (%s): %zu data bytes", |
| 521 |
ping->ping_hostname, buffer, data_length); |
| 522 |
if (options & OPT_VERBOSE) |
| 523 |
printf (", id 0x%04x = %u", ping->ping_ident, ping->ping_ident); |
| 524 |
|
| 525 |
printf ("\n"); |
| 526 |
|
| 527 |
status = ping_run (ping, echo_finish); |
| 528 |
free (ping->ping_hostname); |
| 529 |
return status; |
| 530 |
} |
| 531 |
|
| 532 |
static void |
| 533 |
ping_reset (PING *p) |
| 534 |
{ |
| 535 |
p->ping_num_xmit = 0; |
| 536 |
p->ping_num_recv = 0; |
| 537 |
p->ping_num_rept = 0; |
| 538 |
} |
| 539 |
|
| 540 |
static int |
| 541 |
print_echo (int dupflag, int hops, struct ping_stat *ping_stat, |
| 542 |
struct sockaddr_in6 *dest MAYBE_UNUSED, |
| 543 |
struct sockaddr_in6 *from, struct icmp6_hdr *icmp6, int datalen) |
| 544 |
{ |
| 545 |
int err; |
| 546 |
char buf[256]; |
| 547 |
bool timing = false; |
| 548 |
double triptime = 0.0; |
| 549 |
|
| 550 |
/* Do timing */ |
| 551 |
if (PING_TIMING (datalen - sizeof (struct icmp6_hdr))) |
| 552 |
{ |
| 553 |
struct timeval tv; |
| 554 |
struct timespec ts; |
| 555 |
|
| 556 |
timing = true; |
| 557 |
|
| 558 |
/* Avoid unaligned data. */ |
| 559 |
memcpy (&tv, icmp6 + 1, sizeof (tv)); |
| 560 |
/* *INDENT-OFF* */ |
| 561 |
ts = timespec_sub (current_timespec (), |
| 562 |
(struct timespec) { .tv_sec = tv.tv_sec, |
| 563 |
.tv_nsec = tv.tv_usec * 1000 }); |
| 564 |
/* *INDENT-ON* */ |
| 565 |
|
| 566 |
triptime = timespectod (ts) * 1000.0; |
| 567 |
ping_stat->tsum += triptime; |
| 568 |
ping_stat->tsumsq += triptime * triptime; |
| 569 |
if (triptime < ping_stat->tmin) |
| 570 |
ping_stat->tmin = triptime; |
| 571 |
if (triptime > ping_stat->tmax) |
| 572 |
ping_stat->tmax = triptime; |
| 573 |
} |
| 574 |
|
| 575 |
if (options & OPT_QUIET) |
| 576 |
return 0; |
| 577 |
if (options & OPT_FLOOD) |
| 578 |
{ |
| 579 |
putchar ('\b'); |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
err = getnameinfo ((struct sockaddr *) from, sizeof (*from), |
| 584 |
buf, sizeof (buf), NULL, 0, |
| 585 |
(options & OPT_NUMERIC) ? NI_NUMERICHOST |
| 586 |
#ifdef NI_IDN |
| 587 |
: NI_IDN |
| 588 |
#else |
| 589 |
: 0 |
| 590 |
#endif |
| 591 |
); |
| 592 |
if (err) |
| 593 |
{ |
| 594 |
const char *errmsg; |
| 595 |
|
| 596 |
if (err == EAI_SYSTEM) |
| 597 |
errmsg = strerror (errno); |
| 598 |
else |
| 599 |
errmsg = gai_strerror (err); |
| 600 |
|
| 601 |
fprintf (stderr, "ping: getnameinfo: %s\n", errmsg); |
| 602 |
|
| 603 |
strcpy (buf, "unknown"); |
| 604 |
} |
| 605 |
|
| 606 |
printf ("%d bytes from %s: icmp_seq=%u", datalen, buf, |
| 607 |
ntohs (icmp6->icmp6_seq)); |
| 608 |
if (hops >= 0) |
| 609 |
printf (" ttl=%d", hops); |
| 610 |
if (timing) |
| 611 |
printf (" time=%.3f ms", triptime); |
| 612 |
if (dupflag) |
| 613 |
printf (" (DUP!)"); |
| 614 |
|
| 615 |
putchar ('\n'); |
| 616 |
|
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
#define NITEMS(a) sizeof(a)/sizeof((a)[0]) |
| 621 |
|
| 622 |
struct icmp_code_descr |
| 623 |
{ |
| 624 |
int code; |
| 625 |
char *diag; |
| 626 |
}; |
| 627 |
|
| 628 |
static struct icmp_code_descr icmp_dest_unreach_desc[] = { |
| 629 |
{ICMP6_DST_UNREACH_NOROUTE, "No route to destination"}, |
| 630 |
{ICMP6_DST_UNREACH_ADMIN, "Destination administratively prohibited"}, |
| 631 |
{ICMP6_DST_UNREACH_BEYONDSCOPE, "Beyond scope of source address"}, |
| 632 |
{ICMP6_DST_UNREACH_ADDR, "Address unreachable"}, |
| 633 |
{ICMP6_DST_UNREACH_NOPORT, "Port unreachable"}, |
| 634 |
{ICMP6_DST_UNREACH_POLICYFAIL, |
| 635 |
"Source address failed ingress/egress policy"}, |
| 636 |
{ICMP6_DST_UNREACH_REJECTROUTE, "Reject route to destination"}, |
| 637 |
}; |
| 638 |
|
| 639 |
static void |
| 640 |
print_dst_unreach (struct icmp6_hdr *icmp6) |
| 641 |
{ |
| 642 |
struct icmp_code_descr *p; |
| 643 |
|
| 644 |
printf ("Destination unreachable: "); |
| 645 |
for (p = icmp_dest_unreach_desc; |
| 646 |
p < icmp_dest_unreach_desc + NITEMS (icmp_dest_unreach_desc); p++) |
| 647 |
{ |
| 648 |
if (p->code == icmp6->icmp6_code) |
| 649 |
{ |
| 650 |
puts (p->diag); |
| 651 |
return; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
printf ("Unknown code %d\n", icmp6->icmp6_code); |
| 656 |
} |
| 657 |
|
| 658 |
static void |
| 659 |
print_packet_too_big (struct icmp6_hdr *icmp6) |
| 660 |
{ |
| 661 |
printf ("Packet too big, mtu=%u\n", icmp6->icmp6_mtu); |
| 662 |
} |
| 663 |
|
| 664 |
static struct icmp_code_descr icmp_time_exceeded_desc[] = { |
| 665 |
{ICMP6_TIME_EXCEED_TRANSIT, "Hop limit exceeded"}, |
| 666 |
{ICMP6_TIME_EXCEED_REASSEMBLY, "Fragment reassembly timeout"}, |
| 667 |
}; |
| 668 |
|
| 669 |
static void |
| 670 |
print_time_exceeded (struct icmp6_hdr *icmp6) |
| 671 |
{ |
| 672 |
struct icmp_code_descr *p; |
| 673 |
|
| 674 |
printf ("Time exceeded: "); |
| 675 |
for (p = icmp_time_exceeded_desc; |
| 676 |
p < icmp_time_exceeded_desc + NITEMS (icmp_time_exceeded_desc); p++) |
| 677 |
{ |
| 678 |
if (p->code == icmp6->icmp6_code) |
| 679 |
{ |
| 680 |
puts (p->diag); |
| 681 |
return; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
printf ("Unknown code %d\n", icmp6->icmp6_code); |
| 686 |
} |
| 687 |
|
| 688 |
static struct icmp_code_descr icmp_param_prob_desc[] = { |
| 689 |
{ICMP6_PARAMPROB_HEADER, "Erroneous header field"}, |
| 690 |
{ICMP6_PARAMPROB_NEXTHEADER, "Unrecognized Next Header type"}, |
| 691 |
{ICMP6_PARAMPROB_OPTION, "Unrecognized IPv6 option"}, |
| 692 |
}; |
| 693 |
|
| 694 |
static void |
| 695 |
print_param_prob (struct icmp6_hdr *icmp6) |
| 696 |
{ |
| 697 |
struct icmp_code_descr *p; |
| 698 |
|
| 699 |
printf ("Parameter problem: "); |
| 700 |
for (p = icmp_param_prob_desc; |
| 701 |
p < icmp_param_prob_desc + NITEMS (icmp_param_prob_desc); p++) |
| 702 |
{ |
| 703 |
if (p->code == icmp6->icmp6_code) |
| 704 |
{ |
| 705 |
puts (p->diag); |
| 706 |
return; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
printf ("Unknown code %d\n", icmp6->icmp6_code); |
| 711 |
} |
| 712 |
|
| 713 |
void |
| 714 |
print_ip_data (struct icmp6_hdr *icmp6) |
| 715 |
{ |
| 716 |
size_t j; |
| 717 |
struct ip6_hdr *ip = (struct ip6_hdr *) ((char *) icmp6 + sizeof (*icmp6)); |
| 718 |
char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN]; |
| 719 |
|
| 720 |
(void) inet_ntop (AF_INET6, &ip->ip6_dst, dst, sizeof (dst)); |
| 721 |
(void) inet_ntop (AF_INET6, &ip->ip6_src, src, sizeof (src)); |
| 722 |
|
| 723 |
printf ("IP Header Dump:\n "); |
| 724 |
for (j = 0; j < sizeof (*ip) - sizeof (ip->ip6_src) - sizeof (ip->ip6_dst); |
| 725 |
++j) |
| 726 |
printf ("%02x%s", *((unsigned char *) ip + j), (j % 2) ? " " : ""); /* Group bytes two by two. */ |
| 727 |
printf ("(src) (dst)\n"); |
| 728 |
|
| 729 |
printf ("Vr TC Flow Plen Nxt Hop Src\t\t Dst\n"); |
| 730 |
printf (" %1x %02x %04x %4hu %3hhu %3hhu %s %s\n", |
| 731 |
ntohl (ip->ip6_flow) >> 28, |
| 732 |
(ntohl (ip->ip6_flow) & 0x0fffffff) >> 20, |
| 733 |
ntohl (ip->ip6_flow) & 0x0fffff, |
| 734 |
ntohs (ip->ip6_plen), ip->ip6_nxt, ip->ip6_hlim, src, dst); |
| 735 |
|
| 736 |
switch (ip->ip6_nxt) |
| 737 |
{ |
| 738 |
case IPPROTO_ICMPV6: |
| 739 |
{ |
| 740 |
struct icmp6_hdr *hdr = |
| 741 |
(struct icmp6_hdr *) ((unsigned char *) ip + sizeof (*ip)); |
| 742 |
|
| 743 |
printf ("ICMP: type %hhu, code %hhu, size %hu", |
| 744 |
hdr->icmp6_type, hdr->icmp6_code, ntohs (ip->ip6_plen)); |
| 745 |
switch (hdr->icmp6_type) |
| 746 |
{ |
| 747 |
case ICMP6_ECHO_REQUEST: |
| 748 |
case ICMP6_ECHO_REPLY: |
| 749 |
printf (", id 0x%04x, seq 0x%04x", |
| 750 |
ntohs (hdr->icmp6_id), ntohs (hdr->icmp6_seq)); |
| 751 |
break; |
| 752 |
default: |
| 753 |
break; |
| 754 |
} |
| 755 |
} |
| 756 |
break; |
| 757 |
default: |
| 758 |
break; |
| 759 |
} |
| 760 |
|
| 761 |
printf ("\n"); |
| 762 |
} |
| 763 |
|
| 764 |
static struct icmp_diag |
| 765 |
{ |
| 766 |
int type; |
| 767 |
void (*func) (struct icmp6_hdr *); |
| 768 |
} icmp_diag[] = { |
| 769 |
{ICMP6_DST_UNREACH, print_dst_unreach}, |
| 770 |
{ICMP6_PACKET_TOO_BIG, print_packet_too_big}, |
| 771 |
{ICMP6_TIME_EXCEEDED, print_time_exceeded}, |
| 772 |
{ICMP6_PARAM_PROB, print_param_prob}, |
| 773 |
}; |
| 774 |
|
| 775 |
static void |
| 776 |
print_icmp_error (struct sockaddr_in6 *from, struct icmp6_hdr *icmp6, int len) |
| 777 |
{ |
| 778 |
char *s; |
| 779 |
struct icmp_diag *p; |
| 780 |
|
| 781 |
s = ipaddr2str ((struct sockaddr *) from, sizeof (*from)); |
| 782 |
printf ("%d bytes from %s: ", len, s); |
| 783 |
free (s); |
| 784 |
|
| 785 |
for (p = icmp_diag; p < icmp_diag + NITEMS (icmp_diag); p++) |
| 786 |
{ |
| 787 |
if (p->type == icmp6->icmp6_type) |
| 788 |
{ |
| 789 |
p->func (icmp6); |
| 790 |
if (options & OPT_VERBOSE) |
| 791 |
print_ip_data (icmp6); |
| 792 |
|
| 793 |
return; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
/* This should never happen because of the ICMP6_FILTER set in |
| 798 |
ping_init(). */ |
| 799 |
printf ("Unknown ICMP type: %d\n", icmp6->icmp6_type); |
| 800 |
} |
| 801 |
|
| 802 |
static int |
| 803 |
echo_finish (void) |
| 804 |
{ |
| 805 |
ping_finish (); |
| 806 |
if (ping->ping_num_recv && PING_TIMING (data_length)) |
| 807 |
{ |
| 808 |
struct ping_stat *ping_stat = (struct ping_stat *) ping->ping_closure; |
| 809 |
double total = ping->ping_num_recv + ping->ping_num_rept; |
| 810 |
double avg = ping_stat->tsum / total; |
| 811 |
double vari = ping_stat->tsumsq / total - avg * avg; |
| 812 |
|
| 813 |
printf ("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", |
| 814 |
ping_stat->tmin, avg, ping_stat->tmax, nsqrt (vari, 0.0005)); |
| 815 |
} |
| 816 |
return (ping->ping_num_recv == 0); |
| 817 |
} |
| 818 |
|
| 819 |
static PING * |
| 820 |
ping_init (int type MAYBE_UNUSED, int ident) |
| 821 |
{ |
| 822 |
int fd, err; |
| 823 |
const int on = 1; |
| 824 |
PING *p; |
| 825 |
struct icmp6_filter filter; |
| 826 |
|
| 827 |
/* Initialize raw ICMPv6 socket */ |
| 828 |
fd = socket (PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
| 829 |
if (fd < 0) |
| 830 |
{ |
| 831 |
if (errno == EPERM || errno == EACCES) |
| 832 |
error (EXIT_FAILURE, errno, "raw socket"); |
| 833 |
|
| 834 |
return NULL; |
| 835 |
} |
| 836 |
|
| 837 |
/* Tell which ICMPs we are interested in. */ |
| 838 |
ICMP6_FILTER_SETBLOCKALL (&filter); |
| 839 |
ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &filter); |
| 840 |
ICMP6_FILTER_SETPASS (ICMP6_DST_UNREACH, &filter); |
| 841 |
ICMP6_FILTER_SETPASS (ICMP6_PACKET_TOO_BIG, &filter); |
| 842 |
ICMP6_FILTER_SETPASS (ICMP6_TIME_EXCEEDED, &filter); |
| 843 |
ICMP6_FILTER_SETPASS (ICMP6_PARAM_PROB, &filter); |
| 844 |
|
| 845 |
err = |
| 846 |
setsockopt (fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof (filter)); |
| 847 |
if (err) |
| 848 |
{ |
| 849 |
close (fd); |
| 850 |
return NULL; |
| 851 |
} |
| 852 |
|
| 853 |
err = setsockopt (fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof (on)); |
| 854 |
if (err) |
| 855 |
{ |
| 856 |
close (fd); |
| 857 |
return NULL; |
| 858 |
} |
| 859 |
|
| 860 |
/* Allocate PING structure and initialize it to default values */ |
| 861 |
p = malloc (sizeof (*p)); |
| 862 |
if (!p) |
| 863 |
{ |
| 864 |
close (fd); |
| 865 |
return NULL; |
| 866 |
} |
| 867 |
|
| 868 |
memset (p, 0, sizeof (*p)); |
| 869 |
|
| 870 |
p->ping_fd = fd; |
| 871 |
p->ping_count = DEFAULT_PING_COUNT; |
| 872 |
p->ping_interval = PING_DEFAULT_INTERVAL; |
| 873 |
p->ping_datalen = sizeof (struct timeval); |
| 874 |
/* Make sure we use only 16 bits in this field, id for icmp is a unsigned short. */ |
| 875 |
p->ping_ident = ident & 0xFFFF; |
| 876 |
p->ping_cktab_size = PING_CKTABSIZE; |
| 877 |
p->ping_start_time = current_timespec (); |
| 878 |
return p; |
| 879 |
} |
| 880 |
|
| 881 |
static int |
| 882 |
ping_xmit (PING *p) |
| 883 |
{ |
| 884 |
int i, buflen; |
| 885 |
struct icmp6_hdr *icmp6; |
| 886 |
|
| 887 |
if (_ping_setbuf (p, USE_IPV6)) |
| 888 |
return -1; |
| 889 |
|
| 890 |
buflen = p->ping_datalen + sizeof (struct icmp6_hdr); |
| 891 |
|
| 892 |
/* Mark sequence number as sent */ |
| 893 |
_PING_CLR (p, p->ping_num_xmit); |
| 894 |
|
| 895 |
icmp6 = (struct icmp6_hdr *) p->ping_buffer; |
| 896 |
icmp6->icmp6_type = ICMP6_ECHO_REQUEST; |
| 897 |
icmp6->icmp6_code = 0; |
| 898 |
/* The checksum will be calculated by the TCP/IP stack. */ |
| 899 |
icmp6->icmp6_cksum = 0; |
| 900 |
icmp6->icmp6_id = htons (p->ping_ident); |
| 901 |
icmp6->icmp6_seq = htons (p->ping_num_xmit); |
| 902 |
|
| 903 |
i = sendto (p->ping_fd, (char *) p->ping_buffer, buflen, 0, |
| 904 |
(struct sockaddr *) &p->ping_dest.ping_sockaddr6, |
| 905 |
sizeof (p->ping_dest.ping_sockaddr6)); |
| 906 |
if (i < 0) |
| 907 |
return -1; |
| 908 |
else |
| 909 |
{ |
| 910 |
p->ping_num_xmit++; |
| 911 |
if (i != buflen) |
| 912 |
printf ("ping: wrote %s %d chars, ret=%d\n", |
| 913 |
p->ping_hostname, buflen, i); |
| 914 |
} |
| 915 |
|
| 916 |
return 0; |
| 917 |
} |
| 918 |
|
| 919 |
static int |
| 920 |
my_echo_reply (PING *p, struct icmp6_hdr *icmp6) |
| 921 |
{ |
| 922 |
struct ip6_hdr *orig_ip = (struct ip6_hdr *) (icmp6 + 1); |
| 923 |
struct icmp6_hdr *orig_icmp = (struct icmp6_hdr *) (orig_ip + 1); |
| 924 |
|
| 925 |
return IN6_ARE_ADDR_EQUAL (&orig_ip->ip6_dst, |
| 926 |
&ping->ping_dest.ping_sockaddr6.sin6_addr) |
| 927 |
&& orig_ip->ip6_nxt == IPPROTO_ICMPV6 |
| 928 |
&& orig_icmp->icmp6_type == ICMP6_ECHO_REQUEST |
| 929 |
&& orig_icmp->icmp6_id == htons (p->ping_ident); |
| 930 |
} |
| 931 |
|
| 932 |
static int |
| 933 |
ping_recv (PING *p) |
| 934 |
{ |
| 935 |
int dupflag, n; |
| 936 |
int hops = -1; |
| 937 |
struct msghdr msg; |
| 938 |
struct iovec iov; |
| 939 |
struct icmp6_hdr *icmp6; |
| 940 |
struct cmsghdr *cmsg; |
| 941 |
char cmsg_data[1024]; |
| 942 |
|
| 943 |
iov.iov_base = p->ping_buffer; |
| 944 |
iov.iov_len = _PING_BUFLEN (p, USE_IPV6); |
| 945 |
msg.msg_name = &p->ping_from.ping_sockaddr6; |
| 946 |
msg.msg_namelen = sizeof (p->ping_from.ping_sockaddr6); |
| 947 |
msg.msg_iov = &iov; |
| 948 |
msg.msg_iovlen = 1; |
| 949 |
msg.msg_control = cmsg_data; |
| 950 |
msg.msg_controllen = sizeof (cmsg_data); |
| 951 |
msg.msg_flags = 0; |
| 952 |
|
| 953 |
n = recvmsg (p->ping_fd, &msg, 0); |
| 954 |
if (n < 0) |
| 955 |
return -1; |
| 956 |
|
| 957 |
for (cmsg = CMSG_FIRSTHDR (&msg); cmsg; cmsg = CMSG_NXTHDR (&msg, cmsg)) |
| 958 |
{ |
| 959 |
if (cmsg->cmsg_level == IPPROTO_IPV6 |
| 960 |
&& cmsg->cmsg_type == IPV6_HOPLIMIT) |
| 961 |
{ |
| 962 |
hops = *(int *) CMSG_DATA (cmsg); |
| 963 |
break; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
icmp6 = (struct icmp6_hdr *) p->ping_buffer; |
| 968 |
if (icmp6->icmp6_type == ICMP6_ECHO_REPLY) |
| 969 |
{ |
| 970 |
/* We got an echo reply. */ |
| 971 |
if (ntohs (icmp6->icmp6_id) != p->ping_ident) |
| 972 |
return -1; /* It's not a response to us. */ |
| 973 |
|
| 974 |
if (_PING_TST (p, ntohs (icmp6->icmp6_seq))) |
| 975 |
{ |
| 976 |
/* We already got the reply for this echo request. */ |
| 977 |
p->ping_num_rept++; |
| 978 |
dupflag = 1; |
| 979 |
} |
| 980 |
else |
| 981 |
{ |
| 982 |
_PING_SET (p, ntohs (icmp6->icmp6_seq)); |
| 983 |
p->ping_num_recv++; |
| 984 |
dupflag = 0; |
| 985 |
} |
| 986 |
|
| 987 |
print_echo (dupflag, hops, p->ping_closure, |
| 988 |
&p->ping_dest.ping_sockaddr6, &p->ping_from.ping_sockaddr6, |
| 989 |
icmp6, n); |
| 990 |
|
| 991 |
} |
| 992 |
else |
| 993 |
{ |
| 994 |
/* We got an error reply. */ |
| 995 |
if (!my_echo_reply (p, icmp6)) |
| 996 |
return -1; /* It's not for us. */ |
| 997 |
|
| 998 |
print_icmp_error (&p->ping_from.ping_sockaddr6, icmp6, n); |
| 999 |
} |
| 1000 |
|
| 1001 |
return 0; |
| 1002 |
} |
| 1003 |
|
| 1004 |
static int |
| 1005 |
ping_set_dest (PING *ping, const char *host) |
| 1006 |
{ |
| 1007 |
int err; |
| 1008 |
struct addrinfo *result, hints; |
| 1009 |
char *rhost; |
| 1010 |
|
| 1011 |
#if defined HAVE_IDN || defined HAVE_IDN2 |
| 1012 |
err = idna_to_ascii_lz (host, &rhost, 0); |
| 1013 |
if (err) |
| 1014 |
return 1; |
| 1015 |
host = rhost; |
| 1016 |
#else /* !HAVE_IDN && !HAVE_IDN2 */ |
| 1017 |
rhost = NULL; |
| 1018 |
#endif |
| 1019 |
|
| 1020 |
memset (&hints, 0, sizeof (hints)); |
| 1021 |
hints.ai_family = AF_INET6; |
| 1022 |
hints.ai_flags = AI_CANONNAME; |
| 1023 |
#ifdef AI_IDN |
| 1024 |
hints.ai_flags |= AI_IDN; |
| 1025 |
#endif |
| 1026 |
#ifdef AI_CANONIDN |
| 1027 |
hints.ai_flags |= AI_CANONIDN; |
| 1028 |
#endif |
| 1029 |
|
| 1030 |
err = getaddrinfo (host, NULL, &hints, &result); |
| 1031 |
if (err) |
| 1032 |
{ |
| 1033 |
free (rhost); |
| 1034 |
return 1; |
| 1035 |
} |
| 1036 |
|
| 1037 |
memcpy (&ping->ping_dest.ping_sockaddr6, result->ai_addr, |
| 1038 |
result->ai_addrlen); |
| 1039 |
|
| 1040 |
if (result->ai_canonname) |
| 1041 |
ping->ping_hostname = strdup (result->ai_canonname); |
| 1042 |
else |
| 1043 |
#if defined HAVE_IDN || defined HAVE_IDN2 |
| 1044 |
ping->ping_hostname = host; |
| 1045 |
#else |
| 1046 |
ping->ping_hostname = strdup (host); |
| 1047 |
#endif |
| 1048 |
freeaddrinfo (result); |
| 1049 |
|
| 1050 |
if (!ping->ping_hostname) |
| 1051 |
return 1; |
| 1052 |
|
| 1053 |
return 0; |
| 1054 |
} |