1/* vi: set sw=4 ts=4: */ 2/* 3 * Mini ping implementation for busybox 4 * 5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> 6 * 7 * Adapted from the ping in netkit-base 0.10: 8 * Copyright (c) 1989 The Regents of the University of California. 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * Mike Muuss. 13 * 14 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 15 */ 16/* 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 */ 27 28#include <net/if.h> 29#include <netinet/ip_icmp.h> 30#include "libbb.h" 31 32#if ENABLE_PING6 33# include <netinet/icmp6.h> 34/* I see RENUMBERED constants in bits/in.h - !!? 35 * What a fuck is going on with libc? Is it a glibc joke? */ 36# ifdef IPV6_2292HOPLIMIT 37# undef IPV6_HOPLIMIT 38# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT 39# endif 40# if IPV6_PMTUDISC_DONT != IP_PMTUDISC_DONT || \ 41 IPV6_PMTUDISC_WANT != IP_PMTUDISC_WANT || \ 42 IPV6_PMTUDISC_DO != IP_PMTUDISC_DO 43# error IPV6_PMTUDISC_* & IP_PMTUDISC_* constants mismatch 44# endif 45#endif 46 47enum { 48 DEFDATALEN = 56, 49 MAXIPLEN = 60, 50 MAXICMPLEN = 76, 51 MAX_DUP_CHK = (8 * 128), 52 MAXWAIT = 10, 53 PINGINTERVAL = 1, /* 1 second */ 54}; 55 56/* Common routines */ 57 58static int in_cksum(unsigned short *buf, int sz) 59{ 60 int nleft = sz; 61 int sum = 0; 62 unsigned short *w = buf; 63 unsigned short ans = 0; 64 65 while (nleft > 1) { 66 sum += *w++; 67 nleft -= 2; 68 } 69 70 if (nleft == 1) { 71 *(unsigned char *) (&ans) = *(unsigned char *) w; 72 sum += ans; 73 } 74 75 sum = (sum >> 16) + (sum & 0xFFFF); 76 sum += (sum >> 16); 77 ans = ~sum; 78 return ans; 79} 80 81#if !ENABLE_FEATURE_FANCY_PING 82 83/* Simple version */ 84 85struct globals { 86 char *hostname; 87 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; 88} FIX_ALIASING; 89#define G (*(struct globals*)&bb_common_bufsiz1) 90#define INIT_G() do { } while (0) 91 92static void noresp(int ign UNUSED_PARAM) 93{ 94 printf("No response from %s\n", G.hostname); 95 exit(EXIT_FAILURE); 96} 97 98static void ping4(len_and_sockaddr *lsa) 99{ 100 struct icmp *pkt; 101 int pingsock, c; 102 103 pingsock = create_icmp_socket(); 104 105 pkt = (struct icmp *) G.packet; 106 memset(pkt, 0, sizeof(G.packet)); 107 pkt->icmp_type = ICMP_ECHO; 108 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet)); 109 110 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); 111 112 /* listen for replies */ 113 while (1) { 114 struct sockaddr_in from; 115 socklen_t fromlen = sizeof(from); 116 117 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, 118 (struct sockaddr *) &from, &fromlen); 119 if (c < 0) { 120 if (errno != EINTR) 121 bb_perror_msg("recvfrom"); 122 continue; 123 } 124 if (c >= 76) { /* ip + icmp */ 125 struct iphdr *iphdr = (struct iphdr *) G.packet; 126 127 pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */ 128 if (pkt->icmp_type == ICMP_ECHOREPLY) 129 break; 130 } 131 } 132 if (ENABLE_FEATURE_CLEAN_UP) 133 close(pingsock); 134} 135 136#if ENABLE_PING6 137static void ping6(len_and_sockaddr *lsa) 138{ 139 struct icmp6_hdr *pkt; 140 int pingsock, c; 141 int sockopt; 142 143 pingsock = create_icmp6_socket(); 144 145 pkt = (struct icmp6_hdr *) G.packet; 146 memset(pkt, 0, sizeof(G.packet)); 147 pkt->icmp6_type = ICMP6_ECHO_REQUEST; 148 149 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); 150 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); 151 152 xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len); 153 154 /* listen for replies */ 155 while (1) { 156 struct sockaddr_in6 from; 157 socklen_t fromlen = sizeof(from); 158 159 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, 160 (struct sockaddr *) &from, &fromlen); 161 if (c < 0) { 162 if (errno != EINTR) 163 bb_perror_msg("recvfrom"); 164 continue; 165 } 166 if (c >= ICMP_MINLEN) { /* icmp6_hdr */ 167 pkt = (struct icmp6_hdr *) G.packet; 168 if (pkt->icmp6_type == ICMP6_ECHO_REPLY) 169 break; 170 } 171 } 172 if (ENABLE_FEATURE_CLEAN_UP) 173 close(pingsock); 174} 175#endif 176 177#if !ENABLE_PING6 178# define common_ping_main(af, argv) common_ping_main(argv) 179#endif 180static int common_ping_main(sa_family_t af, char **argv) 181{ 182 len_and_sockaddr *lsa; 183 184 INIT_G(); 185 186#if ENABLE_PING6 187 while ((++argv)[0] && argv[0][0] == '-') { 188 if (argv[0][1] == '4') { 189 af = AF_INET; 190 continue; 191 } 192 if (argv[0][1] == '6') { 193 af = AF_INET6; 194 continue; 195 } 196 bb_show_usage(); 197 } 198#else 199 argv++; 200#endif 201 202 G.hostname = *argv; 203 if (!G.hostname) 204 bb_show_usage(); 205 206#if ENABLE_PING6 207 lsa = xhost_and_af2sockaddr(G.hostname, 0, af); 208#else 209 lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET); 210#endif 211 /* Set timer _after_ DNS resolution */ 212 signal(SIGALRM, noresp); 213 alarm(5); /* give the host 5000ms to respond */ 214 215#if ENABLE_PING6 216 if (lsa->u.sa.sa_family == AF_INET6) 217 ping6(lsa); 218 else 219#endif 220 ping4(lsa); 221 printf("%s is alive!\n", G.hostname); 222 return EXIT_SUCCESS; 223} 224 225 226#else /* FEATURE_FANCY_PING */ 227 228 229/* Full(er) version */ 230 231#define OPT_STRING ("qvc:s:t:w:W:I:M:4" IF_PING6("6")) 232enum { 233 OPT_QUIET = 1 << 0, 234 OPT_VERBOSE = 1 << 1, 235 OPT_c = 1 << 2, 236 OPT_s = 1 << 3, 237 OPT_t = 1 << 4, 238 OPT_w = 1 << 5, 239 OPT_W = 1 << 6, 240 OPT_I = 1 << 7, 241 OPT_M = 1 << 8, 242 OPT_IPV4 = 1 << 9, 243 OPT_IPV6 = (1 << 10) * ENABLE_PING6, 244}; 245 246 247struct globals { 248 int pingsock; 249 int if_index; 250 char *str_I; 251 len_and_sockaddr *source_lsa; 252 unsigned datalen; 253 unsigned pingcount; /* must be int-sized */ 254 unsigned opt_ttl; 255 int pmtudisc; 256 unsigned long ntransmitted, nreceived, nrepeats; 257 uint16_t myid; 258 unsigned tmin, tmax; /* in us */ 259 unsigned long long tsum; /* in us, sum of all times */ 260 unsigned deadline; 261 unsigned timeout; 262 unsigned total_secs; 263 unsigned sizeof_rcv_packet; 264 char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */ 265 void *snd_packet; /* [datalen + ipv4/ipv6_const] */ 266 const char *hostname; 267 const char *dotted; 268 union { 269 struct sockaddr sa; 270 struct sockaddr_in sin; 271#if ENABLE_PING6 272 struct sockaddr_in6 sin6; 273#endif 274 } pingaddr; 275 char rcvd_tbl[MAX_DUP_CHK / 8]; 276} FIX_ALIASING; 277#define G (*(struct globals*)&bb_common_bufsiz1) 278#define pingsock (G.pingsock ) 279#define if_index (G.if_index ) 280#define source_lsa (G.source_lsa ) 281#define str_I (G.str_I ) 282#define datalen (G.datalen ) 283#define ntransmitted (G.ntransmitted) 284#define nreceived (G.nreceived ) 285#define nrepeats (G.nrepeats ) 286#define pingcount (G.pingcount ) 287#define opt_ttl (G.opt_ttl ) 288#define pmtudisc (G.pmtudisc ) 289#define myid (G.myid ) 290#define tmin (G.tmin ) 291#define tmax (G.tmax ) 292#define tsum (G.tsum ) 293#define deadline (G.deadline ) 294#define timeout (G.timeout ) 295#define total_secs (G.total_secs ) 296#define hostname (G.hostname ) 297#define dotted (G.dotted ) 298#define pingaddr (G.pingaddr ) 299#define rcvd_tbl (G.rcvd_tbl ) 300void BUG_ping_globals_too_big(void); 301#define INIT_G() do { \ 302 if (sizeof(G) > COMMON_BUFSIZE) \ 303 BUG_ping_globals_too_big(); \ 304 pingsock = -1; \ 305 datalen = DEFDATALEN; \ 306 timeout = MAXWAIT; \ 307 tmin = UINT_MAX; \ 308 pmtudisc = -1; \ 309} while (0) 310 311 312#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 313#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 314#define SET(bit) (A(bit) |= B(bit)) 315#define CLR(bit) (A(bit) &= (~B(bit))) 316#define TST(bit) (A(bit) & B(bit)) 317 318/**************************************************************************/ 319 320static void print_stats_and_exit(int junk) NORETURN; 321static void print_stats_and_exit(int junk UNUSED_PARAM) 322{ 323 signal(SIGINT, SIG_IGN); 324 325 printf("\n--- %s ping statistics ---\n", hostname); 326 printf("%lu packets transmitted, ", ntransmitted); 327 printf("%lu packets received, ", nreceived); 328 if (nrepeats) 329 printf("%lu duplicates, ", nrepeats); 330 if (ntransmitted) 331 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted; 332 printf("%lu%% packet loss\n", ntransmitted); 333 if (tmin != UINT_MAX) { 334 unsigned tavg = tsum / (nreceived + nrepeats); 335 printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n", 336 tmin / 1000, tmin % 1000, 337 tavg / 1000, tavg % 1000, 338 tmax / 1000, tmax % 1000); 339 } 340 /* if condition is true, exit with 1 -- 'failure' */ 341 exit(nreceived == 0 || (deadline && nreceived < pingcount)); 342} 343 344static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt) 345{ 346 int sz; 347 348 CLR((uint16_t)ntransmitted % MAX_DUP_CHK); 349 ntransmitted++; 350 351 /* sizeof(pingaddr) can be larger than real sa size, but I think 352 * it doesn't matter */ 353 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr)); 354 if (sz != size_pkt) 355 bb_error_msg_and_die(bb_msg_write_error); 356 357 if (pingcount == 0 || deadline || ntransmitted < pingcount) { 358 /* Didn't send all pings yet - schedule next in 1s */ 359 signal(SIGALRM, sp); 360 if (deadline) { 361 total_secs += PINGINTERVAL; 362 if (total_secs >= deadline) 363 signal(SIGALRM, print_stats_and_exit); 364 } 365 alarm(PINGINTERVAL); 366 } else { /* -c NN, and all NN are sent (and no deadline) */ 367 /* Wait for the last ping to come back. 368 * -W timeout: wait for a response in seconds. 369 * Affects only timeout in absense of any responses, 370 * otherwise ping waits for two RTTs. */ 371 unsigned expire = timeout; 372 373 if (nreceived) { 374 /* approx. 2*tmax, in seconds (2 RTT) */ 375 expire = tmax / (512*1024); 376 if (expire == 0) 377 expire = 1; 378 } 379 signal(SIGALRM, print_stats_and_exit); 380 alarm(expire); 381 } 382} 383 384static void sendping4(int junk UNUSED_PARAM) 385{ 386 struct icmp *pkt = G.snd_packet; 387 388 //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced 389 pkt->icmp_type = ICMP_ECHO; 390 /*pkt->icmp_code = 0;*/ 391 pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */ 392 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ 393 pkt->icmp_id = myid; 394 395 /* If datalen < 4, we store timestamp _past_ the packet, 396 * but it's ok - we allocated 4 extra bytes in xzalloc() just in case. 397 */ 398 /*if (datalen >= 4)*/ 399 /* No hton: we'll read it back on the same machine */ 400 *(uint32_t*)&pkt->icmp_dun = monotonic_us(); 401 402 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN); 403 404 sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN); 405} 406#if ENABLE_PING6 407static void sendping6(int junk UNUSED_PARAM) 408{ 409 struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr) + 4); 410 411 //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4); 412 pkt->icmp6_type = ICMP6_ECHO_REQUEST; 413 /*pkt->icmp6_code = 0;*/ 414 /*pkt->icmp6_cksum = 0;*/ 415 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ 416 pkt->icmp6_id = myid; 417 418 /*if (datalen >= 4)*/ 419 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); 420 421 //TODO? pkt->icmp_cksum = in_cksum(...); 422 423 sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr)); 424} 425#endif 426 427static const char *icmp_type_name(int id) 428{ 429 switch (id) { 430 case ICMP_ECHOREPLY: return "Echo Reply"; 431 case ICMP_DEST_UNREACH: return "Destination Unreachable"; 432 case ICMP_SOURCE_QUENCH: return "Source Quench"; 433 case ICMP_REDIRECT: return "Redirect (change route)"; 434 case ICMP_ECHO: return "Echo Request"; 435 case ICMP_TIME_EXCEEDED: return "Time Exceeded"; 436 case ICMP_PARAMETERPROB: return "Parameter Problem"; 437 case ICMP_TIMESTAMP: return "Timestamp Request"; 438 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; 439 case ICMP_INFO_REQUEST: return "Information Request"; 440 case ICMP_INFO_REPLY: return "Information Reply"; 441 case ICMP_ADDRESS: return "Address Mask Request"; 442 case ICMP_ADDRESSREPLY: return "Address Mask Reply"; 443 default: return "unknown ICMP type"; 444 } 445} 446#if ENABLE_PING6 447/* RFC3542 changed some definitions from RFC2292 for no good reason, whee! 448 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */ 449#ifndef MLD_LISTENER_QUERY 450# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY 451#endif 452#ifndef MLD_LISTENER_REPORT 453# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT 454#endif 455#ifndef MLD_LISTENER_REDUCTION 456# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION 457#endif 458static const char *icmp6_type_name(int id) 459{ 460 switch (id) { 461 case ICMP6_DST_UNREACH: return "Destination Unreachable"; 462 case ICMP6_PACKET_TOO_BIG: return "Packet too big"; 463 case ICMP6_TIME_EXCEEDED: return "Time Exceeded"; 464 case ICMP6_PARAM_PROB: return "Parameter Problem"; 465 case ICMP6_ECHO_REPLY: return "Echo Reply"; 466 case ICMP6_ECHO_REQUEST: return "Echo Request"; 467 case MLD_LISTENER_QUERY: return "Listener Query"; 468 case MLD_LISTENER_REPORT: return "Listener Report"; 469 case MLD_LISTENER_REDUCTION: return "Listener Reduction"; 470 default: return "unknown ICMP type"; 471 } 472} 473#endif 474 475static void unpack_tail(int sz, uint32_t *tp, 476 const char *from_str, 477 uint16_t recv_seq, int ttl) 478{ 479 const char *dupmsg = " (DUP!)"; 480 unsigned triptime = triptime; /* for gcc */ 481 482 ++nreceived; 483 484 if (tp) { 485 /* (int32_t) cast is for hypothetical 64-bit unsigned */ 486 /* (doesn't hurt 32-bit real-world anyway) */ 487 triptime = (int32_t) ((uint32_t)monotonic_us() - *tp); 488 tsum += triptime; 489 if (triptime < tmin) 490 tmin = triptime; 491 if (triptime > tmax) 492 tmax = triptime; 493 } 494 495 if (TST(recv_seq % MAX_DUP_CHK)) { 496 ++nrepeats; 497 --nreceived; 498 } else { 499 SET(recv_seq % MAX_DUP_CHK); 500 dupmsg += 7; 501 } 502 503 if (option_mask32 & OPT_QUIET) 504 return; 505 506 printf("%d bytes from %s: seq=%u ttl=%d", sz, 507 from_str, recv_seq, ttl); 508 if (tp) 509 printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000); 510 puts(dupmsg); 511 fflush_all(); 512} 513static void unpack4(char *buf, int sz, struct sockaddr_in *from) 514{ 515 struct icmp *icmppkt; 516 struct iphdr *iphdr; 517 int hlen; 518 519 /* discard if too short */ 520 if (sz < (datalen + ICMP_MINLEN)) 521 return; 522 523 /* check IP header */ 524 iphdr = (struct iphdr *) buf; 525 hlen = iphdr->ihl << 2; 526 sz -= hlen; 527 icmppkt = (struct icmp *) (buf + hlen); 528 if (icmppkt->icmp_id != myid) 529 return; /* not our ping */ 530 531 if (icmppkt->icmp_type == ICMP_ECHOREPLY) { 532 uint16_t recv_seq = ntohs(icmppkt->icmp_seq); 533 uint32_t *tp = NULL; 534 535 if (sz >= ICMP_MINLEN + sizeof(uint32_t)) 536 tp = (uint32_t *) icmppkt->icmp_data; 537 unpack_tail(sz, tp, 538 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), 539 recv_seq, iphdr->ttl); 540 } else if (icmppkt->icmp_type != ICMP_ECHO) { 541 bb_error_msg("warning: got ICMP %d (%s)", 542 icmppkt->icmp_type, 543 icmp_type_name(icmppkt->icmp_type)); 544 } 545} 546#if ENABLE_PING6 547static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit) 548{ 549 struct icmp6_hdr *icmppkt; 550 char buf[INET6_ADDRSTRLEN]; 551 552 /* discard if too short */ 553 if (sz < (datalen + sizeof(struct icmp6_hdr))) 554 return; 555 556 icmppkt = (struct icmp6_hdr *) packet; 557 if (icmppkt->icmp6_id != myid) 558 return; /* not our ping */ 559 560 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { 561 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq); 562 uint32_t *tp = NULL; 563 564 if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t)) 565 tp = (uint32_t *) &icmppkt->icmp6_data8[4]; 566 unpack_tail(sz, tp, 567 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr, 568 buf, sizeof(buf)), 569 recv_seq, hoplimit); 570 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) { 571 bb_error_msg("warning: got ICMP %d (%s)", 572 icmppkt->icmp6_type, 573 icmp6_type_name(icmppkt->icmp6_type)); 574 } 575} 576#endif 577 578static void ping4(len_and_sockaddr *lsa) 579{ 580 int sockopt; 581 582 pingsock = create_icmp_socket(); 583 pingaddr.sin = lsa->u.sin; 584 if (source_lsa) { 585 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF, 586 &source_lsa->u.sa, source_lsa->len)) 587 bb_error_msg_and_die("can't set multicast source interface"); 588 xbind(pingsock, &source_lsa->u.sa, source_lsa->len); 589 } 590 if (str_I) 591 setsockopt_bindtodevice(pingsock, str_I); 592 593 /* enable broadcast pings */ 594 setsockopt_broadcast(pingsock); 595 596 /* set recv buf (needed if we can get lots of responses: flood ping, 597 * broadcast ping etc) */ 598 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ 599 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); 600 601 if (opt_ttl != 0) { 602 setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl)); 603 /* above doesnt affect packets sent to bcast IP, so... */ 604 setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl)); 605 } 606 607 if (IN_MULTICAST(ntohl(pingaddr.sin.sin_addr.s_addr))) { 608 if (myid && pmtudisc >= 0 && pmtudisc != IPV6_PMTUDISC_DO) 609 bb_error_msg_and_die("multicast ping does not fragment"); 610 if (pmtudisc < 0) 611 pmtudisc = IPV6_PMTUDISC_DO; 612 } 613 if (pmtudisc >= 0) 614 setsockopt(pingsock, SOL_IP, IP_MTU_DISCOVER, &pmtudisc, sizeof(pmtudisc)); 615 616 signal(SIGINT, print_stats_and_exit); 617 618 /* start the ping's going ... */ 619 sendping4(0); 620 621 /* listen for replies */ 622 while (1) { 623 struct sockaddr_in from; 624 socklen_t fromlen = (socklen_t) sizeof(from); 625 int c; 626 627 c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0, 628 (struct sockaddr *) &from, &fromlen); 629 if (c < 0) { 630 if (errno != EINTR) 631 bb_perror_msg("recvfrom"); 632 continue; 633 } 634 unpack4(G.rcv_packet, c, &from); 635 if (pingcount && nreceived >= pingcount) 636 break; 637 } 638} 639#if ENABLE_PING6 640extern int BUG_bad_offsetof_icmp6_cksum(void); 641static void ping6(len_and_sockaddr *lsa) 642{ 643 int sockopt; 644 struct msghdr msg; 645 struct sockaddr_in6 from; 646 struct iovec iov; 647 char control_buf[CMSG_SPACE(36)]; 648 649 pingsock = create_icmp6_socket(); 650 pingaddr.sin6 = lsa->u.sin6; 651 /* untested whether "-I addr" really works for IPv6: */ 652 if (source_lsa) 653 xbind(pingsock, &source_lsa->u.sa, source_lsa->len); 654 if (str_I) 655 setsockopt_bindtodevice(pingsock, str_I); 656 657#ifdef ICMP6_FILTER 658 { 659 struct icmp6_filter filt; 660 if (!(option_mask32 & OPT_VERBOSE)) { 661 ICMP6_FILTER_SETBLOCKALL(&filt); 662 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); 663 } else { 664 ICMP6_FILTER_SETPASSALL(&filt); 665 } 666 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 667 sizeof(filt)) < 0) 668 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); 669 } 670#endif /*ICMP6_FILTER*/ 671 672 /* enable broadcast pings */ 673 setsockopt_broadcast(pingsock); 674 675 /* set recv buf (needed if we can get lots of responses: flood ping, 676 * broadcast ping etc) */ 677 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ 678 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); 679 680 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); 681 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) 682 BUG_bad_offsetof_icmp6_cksum(); 683 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); 684 685 if (opt_ttl != 0) { 686 setsockopt(pingsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &opt_ttl, sizeof(opt_ttl)); 687 setsockopt(pingsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &opt_ttl, sizeof(opt_ttl)); 688 } 689 690 /* request ttl info to be returned in ancillary data */ 691 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1)); 692 693 if (if_index) 694 pingaddr.sin6.sin6_scope_id = if_index; 695 696 if ((pingaddr.sin6.sin6_addr.s6_addr16[0] & htons(0xff00)) == htons(0xff00)) { 697 if (myid && pmtudisc >= 0 && pmtudisc != IPV6_PMTUDISC_DO) 698 bb_error_msg_and_die("multicast ping does not fragment"); 699 if (pmtudisc < 0) 700 pmtudisc = IPV6_PMTUDISC_DO; 701 } 702 if (pmtudisc >= 0) 703 setsockopt(pingsock, SOL_IPV6, IPV6_MTU_DISCOVER, &pmtudisc, sizeof(pmtudisc)); 704 705 signal(SIGINT, print_stats_and_exit); 706 707 /* start the ping's going ... */ 708 sendping6(0); 709 710 /* listen for replies */ 711 msg.msg_name = &from; 712 msg.msg_namelen = sizeof(from); 713 msg.msg_iov = &iov; 714 msg.msg_iovlen = 1; 715 msg.msg_control = control_buf; 716 iov.iov_base = G.rcv_packet; 717 iov.iov_len = G.sizeof_rcv_packet; 718 while (1) { 719 int c; 720 struct cmsghdr *mp; 721 int hoplimit = -1; 722 msg.msg_controllen = sizeof(control_buf); 723 724 c = recvmsg(pingsock, &msg, 0); 725 if (c < 0) { 726 if (errno != EINTR) 727 bb_perror_msg("recvfrom"); 728 continue; 729 } 730 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) { 731 if (mp->cmsg_level == SOL_IPV6 732 && mp->cmsg_type == IPV6_HOPLIMIT 733 /* don't check len - we trust the kernel: */ 734 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */ 735 ) { 736 /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */ 737 move_from_unaligned_int(hoplimit, CMSG_DATA(mp)); 738 } 739 } 740 unpack6(G.rcv_packet, c, /*&from,*/ hoplimit); 741 if (pingcount && nreceived >= pingcount) 742 break; 743 } 744} 745#endif 746 747static void ping(len_and_sockaddr *lsa) 748{ 749 printf("PING %s (%s)", hostname, dotted); 750 if (source_lsa) { 751 printf(" from %s", 752 xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa)); 753 } 754 printf(": %d data bytes\n", datalen); 755 756 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN; 757 G.rcv_packet = xzalloc(G.sizeof_rcv_packet); 758#if ENABLE_PING6 759 if (lsa->u.sa.sa_family == AF_INET6) { 760 /* +4 reserves a place for timestamp, which may end up sitting 761 * _after_ packet. Saves one if() - see sendping4/6() */ 762 G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4); 763 ping6(lsa); 764 } else 765#endif 766 { 767 G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4); 768 ping4(lsa); 769 } 770} 771 772static int common_ping_main(int opt, char **argv) 773{ 774 len_and_sockaddr *lsa; 775 char *str_s, *str_M; 776 777 INIT_G(); 778 779 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */ 780 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+"; 781 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_M); 782 if (opt & OPT_s) 783 datalen = xatou16(str_s); // -s 784 if (opt & OPT_I) { // -I 785 if_index = if_nametoindex(str_I); 786 if (!if_index) { 787 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */ 788 source_lsa = xdotted2sockaddr(str_I, 0); 789 str_I = NULL; /* don't try to bind to device later */ 790 } 791 } 792 myid = (uint16_t) getpid(); 793 hostname = argv[optind]; 794#if ENABLE_PING6 795 { 796 sa_family_t af = AF_UNSPEC; 797 if (opt & OPT_IPV4) 798 af = AF_INET; 799 if (opt & OPT_IPV6) 800 af = AF_INET6; 801 lsa = xhost_and_af2sockaddr(hostname, 0, af); 802 } 803#else 804 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET); 805#endif 806 if (opt & OPT_M) { // -M 807 if (strcmp(str_M, "do") == 0) 808 pmtudisc = IP_PMTUDISC_DO; 809 else if (strcmp(str_M, "dont") == 0) 810 pmtudisc = IP_PMTUDISC_DONT; 811 else if (strcmp(str_M, "want") == 0) 812 pmtudisc = IP_PMTUDISC_WANT; 813 } 814 815 if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family) 816 /* leaking it here... */ 817 source_lsa = NULL; 818 819 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); 820 ping(lsa); 821 print_stats_and_exit(EXIT_SUCCESS); 822 /*return EXIT_SUCCESS;*/ 823} 824#endif /* FEATURE_FANCY_PING */ 825 826 827int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 828int ping_main(int argc UNUSED_PARAM, char **argv) 829{ 830#if !ENABLE_FEATURE_FANCY_PING 831 return common_ping_main(AF_UNSPEC, argv); 832#else 833 return common_ping_main(0, argv); 834#endif 835} 836 837#if ENABLE_PING6 838int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 839int ping6_main(int argc UNUSED_PARAM, char **argv) 840{ 841# if !ENABLE_FEATURE_FANCY_PING 842 return common_ping_main(AF_INET6, argv); 843# else 844 return common_ping_main(OPT_IPV6, argv); 845# endif 846} 847#endif 848 849/* from ping6.c: 850 * Copyright (c) 1989 The Regents of the University of California. 851 * All rights reserved. 852 * 853 * This code is derived from software contributed to Berkeley by 854 * Mike Muuss. 855 * 856 * Redistribution and use in source and binary forms, with or without 857 * modification, are permitted provided that the following conditions 858 * are met: 859 * 1. Redistributions of source code must retain the above copyright 860 * notice, this list of conditions and the following disclaimer. 861 * 2. Redistributions in binary form must reproduce the above copyright 862 * notice, this list of conditions and the following disclaimer in the 863 * documentation and/or other materials provided with the distribution. 864 * 865 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 866 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 867 * 868 * 4. Neither the name of the University nor the names of its contributors 869 * may be used to endorse or promote products derived from this software 870 * without specific prior written permission. 871 * 872 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 873 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 874 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 875 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 876 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 877 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 878 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 879 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 880 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 881 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 882 * SUCH DAMAGE. 883 */ 884