3
0

ping6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $
  4. * Mini ping implementation for busybox
  5. *
  6. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. *
  10. * This version of ping is adapted from the ping in netkit-base 0.10,
  11. * which is:
  12. *
  13. * Copyright (c) 1989 The Regents of the University of California.
  14. * All rights reserved.
  15. *
  16. * This code is derived from software contributed to Berkeley by
  17. * Mike Muuss.
  18. *
  19. * Original copyright notice is retained at the end of this file.
  20. *
  21. * This version is an adaptation of ping.c from busybox.
  22. * The code was modified by Bart Visscher <magick@linux-fan.com>
  23. */
  24. #include <netinet/icmp6.h>
  25. #include <net/if.h>
  26. #include "busybox.h"
  27. /* I see RENUMBERED constants in bits/in.h - !!?
  28. * What a fuck is going on with libc? Is it a glibc joke? */
  29. #ifdef IPV6_2292HOPLIMIT
  30. #undef IPV6_HOPLIMIT
  31. #define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
  32. #endif
  33. enum {
  34. DEFDATALEN = 56,
  35. MAXIPLEN = 60,
  36. MAXICMPLEN = 76,
  37. MAXPACKET = 65468,
  38. MAX_DUP_CHK = (8 * 128),
  39. MAXWAIT = 10,
  40. PINGINTERVAL = 1 /* second */
  41. };
  42. static void ping(const char *host);
  43. #ifndef CONFIG_FEATURE_FANCY_PING6
  44. /* simple version */
  45. static struct hostent *h;
  46. static void noresp(int ign)
  47. {
  48. printf("No response from %s\n", h->h_name);
  49. exit(EXIT_FAILURE);
  50. }
  51. static void ping(const char *host)
  52. {
  53. struct sockaddr_in6 pingaddr;
  54. struct icmp6_hdr *pkt;
  55. int pingsock, c;
  56. int sockopt;
  57. char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  58. pingsock = create_icmp6_socket();
  59. memset(&pingaddr, 0, sizeof(struct sockaddr_in));
  60. pingaddr.sin6_family = AF_INET6;
  61. h = xgethostbyname2(host, AF_INET6);
  62. memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
  63. pkt = (struct icmp6_hdr *) packet;
  64. memset(pkt, 0, sizeof(packet));
  65. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  66. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  67. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
  68. sizeof(sockopt));
  69. c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
  70. (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
  71. if (c < 0) {
  72. if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
  73. bb_perror_msg_and_die("sendto");
  74. }
  75. signal(SIGALRM, noresp);
  76. alarm(5); /* give the host 5000ms to respond */
  77. /* listen for replies */
  78. while (1) {
  79. struct sockaddr_in6 from;
  80. socklen_t fromlen = sizeof(from);
  81. c = recvfrom(pingsock, packet, sizeof(packet), 0,
  82. (struct sockaddr *) &from, &fromlen);
  83. if (c < 0) {
  84. if (errno != EINTR)
  85. bb_perror_msg("recvfrom");
  86. continue;
  87. }
  88. if (c >= 8) { /* icmp6_hdr */
  89. pkt = (struct icmp6_hdr *) packet;
  90. if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
  91. break;
  92. }
  93. }
  94. if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
  95. printf("%s is alive!\n", h->h_name);
  96. }
  97. int ping6_main(int argc, char **argv)
  98. {
  99. argc--;
  100. argv++;
  101. if (argc < 1)
  102. bb_show_usage();
  103. ping(*argv);
  104. return EXIT_SUCCESS;
  105. }
  106. #else /* ! CONFIG_FEATURE_FANCY_PING6 */
  107. /* full(er) version */
  108. #define OPT_STRING "qvc:s:I:"
  109. enum {
  110. OPT_QUIET = 1 << 0,
  111. OPT_VERBOSE = 1 << 1,
  112. };
  113. static struct sockaddr_in6 pingaddr;
  114. static int pingsock = -1;
  115. static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
  116. static int if_index;
  117. static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
  118. static int myid;
  119. static unsigned long tmin = ULONG_MAX, tmax, tsum;
  120. static char rcvd_tbl[MAX_DUP_CHK / 8];
  121. static struct hostent *hostent;
  122. static void sendping(int);
  123. static void pingstats(int);
  124. static void unpack(char *, int, struct sockaddr_in6 *, int);
  125. #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
  126. #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
  127. #define SET(bit) (A(bit) |= B(bit))
  128. #define CLR(bit) (A(bit) &= (~B(bit)))
  129. #define TST(bit) (A(bit) & B(bit))
  130. /**************************************************************************/
  131. static void pingstats(int junk)
  132. {
  133. int status;
  134. signal(SIGINT, SIG_IGN);
  135. printf("\n--- %s ping statistics ---\n", hostent->h_name);
  136. printf("%lu packets transmitted, ", ntransmitted);
  137. printf("%lu packets received, ", nreceived);
  138. if (nrepeats)
  139. printf("%lu duplicates, ", nrepeats);
  140. if (ntransmitted)
  141. printf("%lu%% packet loss\n",
  142. (ntransmitted - nreceived) * 100 / ntransmitted);
  143. if (nreceived)
  144. printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
  145. tmin / 10, tmin % 10,
  146. (tsum / (nreceived + nrepeats)) / 10,
  147. (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
  148. if (nreceived != 0)
  149. status = EXIT_SUCCESS;
  150. else
  151. status = EXIT_FAILURE;
  152. exit(status);
  153. }
  154. static void sendping(int junk)
  155. {
  156. struct icmp6_hdr *pkt;
  157. int i;
  158. char packet[datalen + sizeof (struct icmp6_hdr)];
  159. pkt = (struct icmp6_hdr *) packet;
  160. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  161. pkt->icmp6_code = 0;
  162. pkt->icmp6_cksum = 0;
  163. pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
  164. pkt->icmp6_id = myid;
  165. CLR(pkt->icmp6_seq % MAX_DUP_CHK);
  166. ntransmitted++;
  167. gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
  168. i = sendto(pingsock, packet, sizeof(packet), 0,
  169. (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
  170. if (i < 0)
  171. bb_perror_msg_and_die("sendto");
  172. if ((size_t)i != sizeof(packet))
  173. bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
  174. (int)sizeof(packet));
  175. signal(SIGALRM, sendping);
  176. if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
  177. alarm(PINGINTERVAL);
  178. } else { /* done, wait for the last ping to come back */
  179. /* todo, don't necessarily need to wait so long... */
  180. signal(SIGALRM, pingstats);
  181. alarm(MAXWAIT);
  182. }
  183. }
  184. /* RFC3542 changed some definitions from RFC2292 for no good reason, whee !
  185. * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
  186. #ifndef MLD_LISTENER_QUERY
  187. # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
  188. #endif
  189. #ifndef MLD_LISTENER_REPORT
  190. # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
  191. #endif
  192. #ifndef MLD_LISTENER_REDUCTION
  193. # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
  194. #endif
  195. static char *icmp6_type_name(int id)
  196. {
  197. switch (id) {
  198. case ICMP6_DST_UNREACH: return "Destination Unreachable";
  199. case ICMP6_PACKET_TOO_BIG: return "Packet too big";
  200. case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
  201. case ICMP6_PARAM_PROB: return "Parameter Problem";
  202. case ICMP6_ECHO_REPLY: return "Echo Reply";
  203. case ICMP6_ECHO_REQUEST: return "Echo Request";
  204. case MLD_LISTENER_QUERY: return "Listener Query";
  205. case MLD_LISTENER_REPORT: return "Listener Report";
  206. case MLD_LISTENER_REDUCTION: return "Listener Reduction";
  207. default: return "unknown ICMP type";
  208. }
  209. }
  210. static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
  211. {
  212. struct icmp6_hdr *icmppkt;
  213. struct timeval tv, *tp;
  214. int dupflag;
  215. unsigned long triptime;
  216. char buf[INET6_ADDRSTRLEN];
  217. gettimeofday(&tv, NULL);
  218. /* discard if too short */
  219. if (sz < (datalen + sizeof(struct icmp6_hdr)))
  220. return;
  221. icmppkt = (struct icmp6_hdr *) packet;
  222. if (icmppkt->icmp6_id != myid)
  223. return; /* not our ping */
  224. if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
  225. ++nreceived;
  226. tp = (struct timeval *) &icmppkt->icmp6_data8[4];
  227. if ((tv.tv_usec -= tp->tv_usec) < 0) {
  228. --tv.tv_sec;
  229. tv.tv_usec += 1000000;
  230. }
  231. tv.tv_sec -= tp->tv_sec;
  232. triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
  233. tsum += triptime;
  234. if (triptime < tmin)
  235. tmin = triptime;
  236. if (triptime > tmax)
  237. tmax = triptime;
  238. if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
  239. ++nrepeats;
  240. --nreceived;
  241. dupflag = 1;
  242. } else {
  243. SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
  244. dupflag = 0;
  245. }
  246. if (option_mask32 & OPT_QUIET)
  247. return;
  248. printf("%d bytes from %s: icmp6_seq=%u", sz,
  249. inet_ntop(AF_INET6, &pingaddr.sin6_addr,
  250. buf, sizeof(buf)),
  251. ntohs(icmppkt->icmp6_seq));
  252. printf(" ttl=%d time=%lu.%lu ms", hoplimit,
  253. triptime / 10, triptime % 10);
  254. if (dupflag)
  255. printf(" (DUP!)");
  256. puts("");
  257. } else
  258. if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
  259. bb_error_msg("warning: got ICMP %d (%s)",
  260. icmppkt->icmp6_type, icmp6_type_name(icmppkt->icmp6_type));
  261. }
  262. static void ping(const char *host)
  263. {
  264. char packet[datalen + MAXIPLEN + MAXICMPLEN];
  265. char buf[INET6_ADDRSTRLEN];
  266. int sockopt;
  267. struct msghdr msg;
  268. struct sockaddr_in6 from;
  269. struct iovec iov;
  270. char control_buf[CMSG_SPACE(36)];
  271. pingsock = create_icmp6_socket();
  272. memset(&pingaddr, 0, sizeof(struct sockaddr_in));
  273. pingaddr.sin6_family = AF_INET6;
  274. hostent = xgethostbyname2(host, AF_INET6);
  275. if (hostent->h_addrtype != AF_INET6)
  276. bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported");
  277. memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
  278. #ifdef ICMP6_FILTER
  279. {
  280. struct icmp6_filter filt;
  281. if (!(option_mask32 & OPT_VERBOSE)) {
  282. ICMP6_FILTER_SETBLOCKALL(&filt);
  283. ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
  284. } else {
  285. ICMP6_FILTER_SETPASSALL(&filt);
  286. }
  287. if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
  288. sizeof(filt)) < 0)
  289. bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
  290. }
  291. #endif /*ICMP6_FILTER*/
  292. /* enable broadcast pings */
  293. setsockopt_broadcast(pingsock);
  294. /* set recv buf for broadcast pings */
  295. sockopt = 48 * 1024; /* explain why 48k? */
  296. setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
  297. sizeof(sockopt));
  298. sockopt = 2; /* iputils-ss020927 does this */
  299. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  300. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
  301. sizeof(sockopt));
  302. /* request ttl info to be returned in ancillary data */
  303. sockopt = 1;
  304. setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
  305. sizeof(sockopt));
  306. if (if_index)
  307. pingaddr.sin6_scope_id = if_index;
  308. printf("PING %s (%s): %d data bytes\n",
  309. hostent->h_name,
  310. inet_ntop(AF_INET6, &pingaddr.sin6_addr,
  311. buf, sizeof(buf)),
  312. datalen);
  313. signal(SIGINT, pingstats);
  314. /* start the ping's going ... */
  315. sendping(0);
  316. /* listen for replies */
  317. msg.msg_name = &from;
  318. msg.msg_namelen = sizeof(from);
  319. msg.msg_iov = &iov;
  320. msg.msg_iovlen = 1;
  321. msg.msg_control = control_buf;
  322. iov.iov_base = packet;
  323. iov.iov_len = sizeof(packet);
  324. while (1) {
  325. int c;
  326. struct cmsghdr *mp;
  327. int hoplimit = -1;
  328. msg.msg_controllen = sizeof(control_buf);
  329. c = recvmsg(pingsock, &msg, 0);
  330. if (c < 0) {
  331. if (errno != EINTR)
  332. bb_perror_msg("recvfrom");
  333. continue;
  334. }
  335. for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
  336. if (mp->cmsg_level == SOL_IPV6
  337. && mp->cmsg_type == IPV6_HOPLIMIT
  338. /* don't check len - we trust the kernel: */
  339. /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
  340. ) {
  341. hoplimit = *(int*)CMSG_DATA(mp);
  342. }
  343. }
  344. unpack(packet, c, &from, hoplimit);
  345. if (pingcount > 0 && nreceived >= pingcount)
  346. break;
  347. }
  348. pingstats(0);
  349. }
  350. int ping6_main(int argc, char **argv)
  351. {
  352. char *opt_c, *opt_s, *opt_I;
  353. datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
  354. /* exactly one argument needed, -v and -q don't mix */
  355. opt_complementary = "=1:q--v:v--q";
  356. getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
  357. if (option_mask32 & 4) pingcount = xatoul(opt_c); // -c
  358. if (option_mask32 & 8) datalen = xatou16(opt_s); // -s
  359. if (option_mask32 & 0x10) { // -I
  360. if_index = if_nametoindex(opt_I);
  361. if (!if_index)
  362. bb_error_msg_and_die(
  363. "%s: invalid interface name", opt_I);
  364. }
  365. myid = (int16_t)getpid();
  366. ping(argv[optind]);
  367. return EXIT_SUCCESS;
  368. }
  369. #endif /* ! CONFIG_FEATURE_FANCY_PING6 */
  370. /*
  371. * Copyright (c) 1989 The Regents of the University of California.
  372. * All rights reserved.
  373. *
  374. * This code is derived from software contributed to Berkeley by
  375. * Mike Muuss.
  376. *
  377. * Redistribution and use in source and binary forms, with or without
  378. * modification, are permitted provided that the following conditions
  379. * are met:
  380. * 1. Redistributions of source code must retain the above copyright
  381. * notice, this list of conditions and the following disclaimer.
  382. * 2. Redistributions in binary form must reproduce the above copyright
  383. * notice, this list of conditions and the following disclaimer in the
  384. * documentation and/or other materials provided with the distribution.
  385. *
  386. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  387. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  388. *
  389. * 4. Neither the name of the University nor the names of its contributors
  390. * may be used to endorse or promote products derived from this software
  391. * without specific prior written permission.
  392. *
  393. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  394. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  395. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  396. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  397. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  398. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  399. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  400. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  401. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  402. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  403. * SUCH DAMAGE.
  404. */