1 /* $KAME: ping6.c,v 1.126 2001/05/17 03:39:08 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static char copyright[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid[] =
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
126 #if defined(__OpenBSD__) || defined(__NetBSD__)
136 #include <netinet6/ah.h>
137 #include <netinet6/ipsec.h>
140 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
146 #define MAXPACKETLEN 131072
148 #define ICMP6ECHOLEN 8 /* icmp echo header length excluding time */
149 #define ICMP6ECHOTMLEN sizeof(struct timeval)
150 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
151 /* FQDN case, 64 bits of nonce + 32 bits ttl */
152 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
153 #define EXTRA 256 /* for AH and various other headers. weird. */
154 #define DEFDATALEN ICMP6ECHOTMLEN
155 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
156 #define NROUTES 9 /* number of record route slots */
158 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
159 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
160 #define SET(bit) (A(bit) |= B(bit))
161 #define CLR(bit) (A(bit) &= (~B(bit)))
162 #define TST(bit) (A(bit) & B(bit))
164 #define F_FLOOD 0x0001
165 #define F_INTERVAL 0x0002
166 #define F_PINGFILLED 0x0008
167 #define F_QUIET 0x0010
168 #define F_RROUTE 0x0020
169 #define F_SO_DEBUG 0x0040
170 #define F_VERBOSE 0x0100
172 #ifdef IPSEC_POLICY_IPSEC
173 #define F_POLICY 0x0400
175 #define F_AUTHHDR 0x0200
176 #define F_ENCRYPT 0x0400
177 #endif /*IPSEC_POLICY_IPSEC*/
179 #define F_NODEADDR 0x0800
180 #define F_FQDN 0x1000
181 #define F_INTERFACE 0x2000
182 #define F_SRCADDR 0x4000
183 #ifdef IPV6_REACHCONF
184 #define F_REACHCONF 0x8000
186 #define F_HOSTNAME 0x10000
187 #define F_FQDNOLD 0x20000
188 #define F_NIGROUP 0x40000
189 #define F_SUPTYPES 0x80000
190 #define F_NOMINMTU 0x100000
191 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
194 #define IN6LEN sizeof(struct in6_addr)
195 #define SA6LEN sizeof(struct sockaddr_in6)
196 #define DUMMY_PORT 10101
198 #define SIN6(s) ((struct sockaddr_in6 *)(s))
201 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
202 * number of received sequence numbers we can keep track of. Change 128
203 * to 8192 for complete accuracy...
205 #define MAX_DUP_CHK (8 * 8192)
206 int mx_dup_ck = MAX_DUP_CHK;
207 char rcvd_tbl[MAX_DUP_CHK / 8];
209 struct addrinfo *res;
210 struct sockaddr_in6 dst; /* who to ping6 */
211 struct sockaddr_in6 src; /* src addr of this packet */
212 int datalen = DEFDATALEN;
213 int s; /* socket file descriptor */
214 u_char outpack[MAXPACKETLEN];
215 char BSPACE = '\b'; /* characters written for flood */
218 int ident; /* process id to identify our packets */
219 u_int8_t nonce[8]; /* nonce field for node information */
220 struct in6_addr srcaddr;
221 int hoplimit = -1; /* hoplimit */
222 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
225 long npackets; /* max packets to transmit */
226 long nreceived; /* # of packets we got back */
227 long nrepeats; /* number of duplicates */
228 long ntransmitted; /* sequence # for outbound packets = #sent */
229 struct timeval interval = {1, 0}; /* interval between packets */
232 int timing; /* flag to do timing */
233 double tmin = 999999999.0; /* minimum round trip time */
234 double tmax = 0.0; /* maximum round trip time */
235 double tsum = 0.0; /* sum of all times, for doing average */
236 #if defined(__OpenBSD__) || defined(__NetBSD__)
237 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
240 /* for node addresses */
243 /* for ancillary data(advanced API) */
244 struct msghdr smsghdr;
245 struct iovec smsgiov;
249 volatile sig_atomic_t seenalrm;
250 volatile sig_atomic_t seenint;
252 volatile sig_atomic_t seeninfo;
255 int main __P((int, char *[]));
256 void fill __P((char *, char *));
257 int get_hoplim __P((struct msghdr *));
258 int get_pathmtu __P((struct msghdr *));
259 void set_pathmtu __P((int));
260 struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
261 void onsignal __P((int));
262 void retransmit __P((void));
263 void onint __P((int));
264 size_t pingerlen __P((void));
265 int pinger __P((void));
266 const char *pr_addr __P((struct sockaddr *, int));
267 void pr_icmph __P((struct icmp6_hdr *, u_char *));
268 void pr_iph __P((struct ip6_hdr *));
269 void pr_suptypes __P((struct icmp6_nodeinfo *, size_t));
270 void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
271 int myechoreply __P((const struct icmp6_hdr *));
272 int mynireply __P((const struct icmp6_nodeinfo *));
273 char *dnsdecode __P((const u_char **, const u_char *, const u_char *,
275 void pr_pack __P((u_char *, int, struct msghdr *));
276 void pr_exthdrs __P((struct msghdr *));
277 void pr_ip6opt __P((void *));
278 void pr_rthdr __P((void *));
279 int pr_bitrange __P((u_int32_t, int, int));
280 void pr_retip __P((struct ip6_hdr *, u_char *));
281 void summary __P((void));
282 void tvsub __P((struct timeval *, struct timeval *));
283 int setpolicy __P((int, char *));
284 char *nigroup __P((char *));
285 void usage __P((void));
292 struct itimerval itimer;
293 struct sockaddr_in6 from;
294 struct timeval timeout, *tv;
295 struct addrinfo hints;
299 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
300 u_char *datap, *packet;
301 char *e, *target, *ifname = NULL;
303 struct cmsghdr *scmsgp = NULL;
306 struct in6_pktinfo *pktinfo = NULL;
307 #ifdef USE_RFC2292BIS
308 struct ip6_rthdr *rthdr = NULL;
310 #ifdef IPSEC_POLICY_IPSEC
311 char *policy_in = NULL;
312 char *policy_out = NULL;
317 /* just to be sure */
318 memset(&smsghdr, 0, sizeof(&smsghdr));
319 memset(&smsgiov, 0, sizeof(&smsgiov));
322 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
326 #ifdef IPSEC_POLICY_IPSEC
330 #endif /*IPSEC_POLICY_IPSEC*/
332 while ((ch = getopt(argc, argv,
333 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {
340 options &= ~F_NOUSERDATA;
341 options |= F_NODEADDR;
342 for (cp = optarg; *cp != '\0'; cp++) {
345 naflags |= NI_NODEADDR_FLAG_ALL;
349 naflags |= NI_NODEADDR_FLAG_COMPAT;
353 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
357 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
361 naflags |= NI_NODEADDR_FLAG_GLOBAL;
363 case 'A': /* experimental. not in the spec */
364 #ifdef NI_NODEADDR_FLAG_ANYCAST
365 naflags |= NI_NODEADDR_FLAG_ANYCAST;
369 "-a A is not supported on the platform");
380 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
381 sockbufsize = atoi(optarg);
384 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
388 npackets = strtol(optarg, &e, 10);
389 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
391 "illegal number of packets -- %s", optarg);
394 options |= F_SO_DEBUG;
398 errx(1, "must be superuser to flood ping");
400 setbuf(stdout, (char *)NULL);
403 options |= F_HOSTNAME;
405 case 'h': /* hoplimit */
406 hoplimit = strtol(optarg, &e, 10);
407 if (255 < hoplimit || hoplimit < -1)
409 "illegal hoplimit -- %s", optarg);
413 options |= F_INTERFACE;
414 #ifndef USE_SIN6_SCOPE_ID
418 case 'i': /* wait between sending packets */
419 intval = strtod(optarg, &e);
420 if (*optarg == '\0' || *e != '\0')
421 errx(1, "illegal timing interval %s", optarg);
422 if (intval < 1 && getuid()) {
423 errx(1, "%s: only root may use interval < 1s",
426 interval.tv_sec = (long)intval;
428 (long)((intval - interval.tv_sec) * 1000000);
429 if (interval.tv_sec < 0)
430 errx(1, "illegal timing interval %s", optarg);
431 /* less than 1/hz does not make sense */
432 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
433 warnx("too small interval, raised to 0.01");
434 interval.tv_usec = 10000;
436 options |= F_INTERVAL;
440 errx(1, "must be superuser to preload");
441 preload = strtol(optarg, &e, 10);
442 if (preload < 0 || *optarg == '\0' || *e != '\0')
443 errx(1, "illegal preload value -- %s", optarg);
446 #ifdef IPV6_USE_MIN_MTU
447 options |= F_NOMINMTU;
450 errx(1, "-%c is not supported on this platform", ch);
454 options &= ~F_HOSTNAME;
457 options |= F_NIGROUP;
459 case 'p': /* fill buffer with user pattern */
460 options |= F_PINGFILLED;
461 fill((char *)datap, optarg);
467 #ifdef IPV6_REACHCONF
468 options |= F_REACHCONF;
471 errx(1, "-R is not supported in this configuration");
474 /* XXX: use getaddrinfo? */
475 if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
476 errx(1, "invalid IPv6 address: %s", optarg);
477 options |= F_SRCADDR;
480 case 's': /* size of packet to send */
481 datalen = strtol(optarg, &e, 10);
482 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
483 errx(1, "illegal datalen value -- %s", optarg);
484 if (datalen > MAXDATALEN) {
486 "datalen value too large, maximum is %d",
491 options &= ~F_NOUSERDATA;
492 options |= F_SUPTYPES;
495 options |= F_VERBOSE;
498 options &= ~F_NOUSERDATA;
502 options &= ~F_NOUSERDATA;
503 options |= F_FQDNOLD;
506 #ifdef IPSEC_POLICY_IPSEC
509 if (!strncmp("in", optarg, 2)) {
510 if ((policy_in = strdup(optarg)) == NULL)
512 } else if (!strncmp("out", optarg, 3)) {
513 if ((policy_out = strdup(optarg)) == NULL)
516 errx(1, "invalid security policy");
520 options |= F_AUTHHDR;
523 options |= F_ENCRYPT;
525 #endif /*IPSEC_POLICY_IPSEC*/
541 #ifdef IPV6_RECVRTHDR /* 2292bis */
542 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
545 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
548 errx(1, "too many intermediate hops");
554 if (options & F_NIGROUP) {
555 target = nigroup(argv[argc - 1]);
556 if (target == NULL) {
561 target = argv[argc - 1];
564 bzero(&hints, sizeof(struct addrinfo));
565 hints.ai_flags = AI_CANONNAME;
566 hints.ai_family = AF_INET6;
567 hints.ai_socktype = SOCK_RAW;
568 hints.ai_protocol = IPPROTO_ICMPV6;
570 ret_ga = getaddrinfo(target, NULL, &hints, &res);
572 errx(1, "%s", gai_strerror(ret_ga));
573 if (res->ai_canonname)
574 hostname = res->ai_canonname;
579 errx(1, "getaddrinfo failed");
581 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
583 if ((s = socket(res->ai_family, res->ai_socktype,
584 res->ai_protocol)) < 0)
588 * let the kerel pass extension headers of incoming packets,
589 * for privileged socket options
591 if ((options & F_VERBOSE) != 0) {
594 #ifdef IPV6_RECVHOPOPTS
595 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
597 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
598 #else /* old adv. API */
599 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
601 err(1, "setsockopt(IPV6_HOPOPTS)");
603 #ifdef IPV6_RECVDSTOPTS
604 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
606 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
607 #else /* old adv. API */
608 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
610 err(1, "setsockopt(IPV6_DSTOPTS)");
612 #ifdef IPV6_RECVRTHDRDSTOPTS
613 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
615 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
619 /* revoke root privilege */
623 if (options & F_FLOOD && options & F_INTERVAL)
624 errx(1, "-f and -i incompatible options");
626 if ((options & F_NOUSERDATA) == 0) {
627 if (datalen >= sizeof(struct timeval)) {
628 /* we can time transfer */
632 /* in F_VERBOSE case, we may get non-echoreply packets*/
633 if (options & F_VERBOSE)
634 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
636 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
638 /* suppress timing for node information query */
641 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
644 if (!(packet = (u_char *)malloc((u_int)packlen)))
645 errx(1, "unable to allocate packet");
646 if (!(options & F_PINGFILLED))
647 for (i = ICMP6ECHOLEN; i < packlen; ++i)
650 ident = getpid() & 0xFFFF;
652 gettimeofday(&timeout, NULL);
653 srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));
654 memset(nonce, 0, sizeof(nonce));
655 for (i = 0; i < sizeof(nonce); i += sizeof(int))
656 *((int *)&nonce[i]) = rand();
658 memset(nonce, 0, sizeof(nonce));
659 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
660 *((u_int32_t *)&nonce[i]) = arc4random();
665 if (options & F_SO_DEBUG)
666 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
668 optval = IPV6_DEFHLIM;
669 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
670 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
671 &optval, sizeof(optval)) == -1)
672 err(1, "IPV6_MULTICAST_HOPS");
673 #ifdef IPV6_USE_MIN_MTU
674 if ((options & F_NOMINMTU) == 0) {
676 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
677 &optval, sizeof(optval)) == -1)
678 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
680 #ifdef IPV6_RECVPATHMTU
683 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
684 &optval, sizeof(optval)) == -1)
685 err(1, "setsockopt(IPV6_RECVPATHMTU)");
687 #endif /* IPV6_RECVPATHMTU */
688 #endif /* IPV6_USE_MIN_MTU */
691 #ifdef IPSEC_POLICY_IPSEC
692 if (options & F_POLICY) {
693 if (setpolicy(s, policy_in) < 0)
694 errx(1, "%s", ipsec_strerror());
695 if (setpolicy(s, policy_out) < 0)
696 errx(1, "%s", ipsec_strerror());
699 if (options & F_AUTHHDR) {
700 optval = IPSEC_LEVEL_REQUIRE;
701 #ifdef IPV6_AUTH_TRANS_LEVEL
702 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
703 &optval, sizeof(optval)) == -1)
704 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
706 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
707 &optval, sizeof(optval)) == -1)
708 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
711 if (options & F_ENCRYPT) {
712 optval = IPSEC_LEVEL_REQUIRE;
713 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
714 &optval, sizeof(optval)) == -1)
715 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
717 #endif /*IPSEC_POLICY_IPSEC*/
722 struct icmp6_filter filt;
723 if (!(options & F_VERBOSE)) {
724 ICMP6_FILTER_SETBLOCKALL(&filt);
725 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
726 (options & F_NODEADDR) || (options & F_SUPTYPES))
727 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
729 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
731 ICMP6_FILTER_SETPASSALL(&filt);
733 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
735 err(1, "setsockopt(ICMP6_FILTER)");
737 #endif /*ICMP6_FILTER*/
739 /* let the kerel pass extension headers of incoming packets */
740 if ((options & F_VERBOSE) != 0) {
743 #ifdef IPV6_RECVRTHDR
744 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
746 err(1, "setsockopt(IPV6_RECVRTHDR)");
747 #else /* old adv. API */
748 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
750 err(1, "setsockopt(IPV6_RTHDR)");
756 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
757 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
758 &optval, sizeof(optval)) == -1)
759 err(1, "IPV6_MULTICAST_LOOP");
762 /* Specify the outgoing interface and/or the source address */
764 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
767 ip6optlen += CMSG_SPACE(sizeof(int));
769 #ifdef IPV6_REACHCONF
770 if (options & F_REACHCONF)
771 ip6optlen += CMSG_SPACE(0);
774 /* set IP6 packet options */
776 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
777 errx(1, "can't allocate enough memory");
778 smsghdr.msg_control = (caddr_t)scmsg;
779 smsghdr.msg_controllen = ip6optlen;
780 scmsgp = (struct cmsghdr *)scmsg;
783 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
784 memset(pktinfo, 0, sizeof(*pktinfo));
785 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
786 scmsgp->cmsg_level = IPPROTO_IPV6;
787 scmsgp->cmsg_type = IPV6_PKTINFO;
788 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
791 /* set the outgoing interface */
793 #ifndef USE_SIN6_SCOPE_ID
794 /* pktinfo must have already been allocated */
795 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
796 errx(1, "%s: invalid interface name", ifname);
798 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
799 errx(1, "%s: invalid interface name", ifname);
802 /* set the source address */
803 if (options & F_SRCADDR)/* pktinfo must be valid */
804 pktinfo->ipi6_addr = srcaddr;
806 if (hoplimit != -1) {
807 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
808 scmsgp->cmsg_level = IPPROTO_IPV6;
809 scmsgp->cmsg_type = IPV6_HOPLIMIT;
810 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
812 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
814 #ifdef IPV6_REACHCONF
815 if (options & F_REACHCONF) {
816 scmsgp->cmsg_len = CMSG_LEN(0);
817 scmsgp->cmsg_level = IPPROTO_IPV6;
818 scmsgp->cmsg_type = IPV6_REACHCONF;
820 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
824 if (argc > 1) { /* some intermediate addrs are specified */
826 #ifdef USE_RFC2292BIS
830 #ifdef USE_RFC2292BIS
831 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
832 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
833 scmsgp->cmsg_level = IPPROTO_IPV6;
834 scmsgp->cmsg_type = IPV6_RTHDR;
835 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
836 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
837 IPV6_RTHDR_TYPE_0, argc - 1);
839 errx(1, "can't initialize rthdr");
840 #else /* old advanced API */
841 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
842 IPV6_RTHDR_TYPE_0)) == 0)
843 errx(1, "can't initialize rthdr");
844 #endif /* USE_RFC2292BIS */
846 for (hops = 0; hops < argc - 1; hops++) {
847 struct addrinfo *iaip;
849 if ((error = getaddrinfo(argv[hops], NULL, &hints,
851 errx(1, "%s", gai_strerror(error));
852 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
854 "bad addr family of an intermediate addr");
856 #ifdef USE_RFC2292BIS
857 if (inet6_rth_add(rthdr,
858 &(SIN6(iaip->ai_addr))->sin6_addr))
859 errx(1, "can't add an intermediate node");
860 #else /* old advanced API */
861 if (inet6_rthdr_add(scmsgp,
862 &(SIN6(iaip->ai_addr))->sin6_addr,
864 errx(1, "can't add an intermediate node");
865 #endif /* USE_RFC2292BIS */
869 #ifndef USE_RFC2292BIS
870 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
871 errx(1, "can't set the last flag");
874 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
881 int dummy, len = sizeof(src);
883 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
884 err(1, "UDP socket");
886 src.sin6_family = AF_INET6;
887 src.sin6_addr = dst.sin6_addr;
888 src.sin6_port = ntohs(DUMMY_PORT);
889 src.sin6_scope_id = dst.sin6_scope_id;
891 #ifdef USE_RFC2292BIS
893 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
894 (void *)pktinfo, sizeof(*pktinfo)))
895 err(1, "UDP setsockopt(IPV6_PKTINFO)");
897 if (hoplimit != -1 &&
898 setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,
899 (void *)&hoplimit, sizeof(hoplimit)))
900 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
903 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
904 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
905 err(1, "UDP setsockopt(IPV6_RTHDR)");
906 #else /* old advanced API */
907 if (smsghdr.msg_control &&
908 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
909 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
910 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
913 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
914 err(1, "UDP connect");
916 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
917 err(1, "getsockname");
922 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
924 if (datalen > sockbufsize)
925 warnx("you need -b to increase socket buffer size");
926 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
927 sizeof(sockbufsize)) < 0)
928 err(1, "setsockopt(SO_SNDBUF)");
929 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
930 sizeof(sockbufsize)) < 0)
931 err(1, "setsockopt(SO_RCVBUF)");
934 if (datalen > 8 * 1024) /*XXX*/
935 warnx("you need -b to increase socket buffer size");
937 * When pinging the broadcast address, you can get a lot of
938 * answers. Doing something so evil is useful if you are trying
939 * to stress the ethernet, or just want to fill the arp cache
940 * to get some stuff for /etc/ethers.
943 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
949 #ifndef USE_SIN6_SCOPE_ID
950 #ifdef IPV6_RECVPKTINFO
951 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
953 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
954 #else /* old adv. API */
955 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
957 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
959 #endif /* USE_SIN6_SCOPE_ID */
960 #ifdef IPV6_RECVHOPLIMIT
961 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
963 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
964 #else /* old adv. API */
965 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
967 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
970 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
971 (unsigned long)(pingerlen() - 8));
972 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
973 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
975 while (preload--) /* Fire off them quickies. */
978 (void)signal(SIGINT, onsignal);
980 (void)signal(SIGINFO, onsignal);
983 if ((options & F_FLOOD) == 0) {
984 (void)signal(SIGALRM, onsignal);
985 itimer.it_interval = interval;
986 itimer.it_value = interval;
987 (void)setitimer(ITIMER_REAL, &itimer, NULL);
988 if (ntransmitted == 0)
992 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
993 if ((fdmaskp = malloc(fdmasks)) == NULL)
996 signo = seenalrm = seenint = 0;
1005 struct iovec iov[2];
1007 /* signal handling */
1026 if (options & F_FLOOD) {
1029 timeout.tv_usec = 10000;
1033 memset(fdmaskp, 0, fdmasks);
1035 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1037 if (errno != EINTR) {
1045 fromlen = sizeof(from);
1046 m.msg_name = (caddr_t)&from;
1047 m.msg_namelen = sizeof(from);
1048 memset(&iov, 0, sizeof(iov));
1049 iov[0].iov_base = (caddr_t)packet;
1050 iov[0].iov_len = packlen;
1053 cm = (struct cmsghdr *)buf;
1054 m.msg_control = (caddr_t)buf;
1055 m.msg_controllen = sizeof(buf);
1057 cc = recvmsg(s, &m, 0);
1059 if (errno != EINTR) {
1064 } else if (cc == 0) {
1068 * receive control messages only. Process the
1069 * exceptions (currently the only possiblity is
1070 * a path MTU notification.)
1072 if ((mtu = get_pathmtu(&m)) > 0) {
1073 if ((options & F_VERBOSE) != 0) {
1074 printf("new path MTU (%d) is "
1082 * an ICMPv6 message (probably an echoreply) arrived.
1084 pr_pack(packet, cc, &m);
1086 if (npackets && nreceived >= npackets)
1090 exit(nreceived == 0);
1115 * This routine transmits another ping6.
1120 struct itimerval itimer;
1126 * If we're not transmitting any more packets, change the timer
1127 * to wait two round-trip times if we've received any packets or
1128 * ten seconds if we haven't.
1132 itimer.it_value.tv_sec = 2 * tmax / 1000;
1133 if (itimer.it_value.tv_sec == 0)
1134 itimer.it_value.tv_sec = 1;
1136 itimer.it_value.tv_sec = MAXWAIT;
1137 itimer.it_interval.tv_sec = 0;
1138 itimer.it_interval.tv_usec = 0;
1139 itimer.it_value.tv_usec = 0;
1141 (void)signal(SIGALRM, onint);
1142 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1147 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1148 * will be added on by the kernel. The ID field is our UNIX process ID,
1149 * and the sequence number is an ascending integer. The first 8 bytes
1150 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1151 * byte-order, to compute the round-trip time.
1158 if (options & F_FQDN)
1159 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1160 else if (options & F_FQDNOLD)
1162 else if (options & F_NODEADDR)
1163 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1164 else if (options & F_SUPTYPES)
1167 l = ICMP6ECHOLEN + datalen;
1175 struct icmp6_hdr *icp;
1176 struct iovec iov[2];
1178 struct icmp6_nodeinfo *nip;
1181 if (npackets && ntransmitted >= npackets)
1182 return(-1); /* no more transmission */
1184 icp = (struct icmp6_hdr *)outpack;
1185 nip = (struct icmp6_nodeinfo *)outpack;
1186 memset(icp, 0, sizeof(*icp));
1187 icp->icmp6_cksum = 0;
1188 seq = ntransmitted++;
1189 CLR(seq % mx_dup_ck);
1191 if (options & F_FQDN) {
1192 icp->icmp6_type = ICMP6_NI_QUERY;
1193 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1194 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1195 nip->ni_flags = htons(0);
1197 memcpy(nip->icmp6_ni_nonce, nonce,
1198 sizeof(nip->icmp6_ni_nonce));
1199 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1201 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1202 sizeof(dst.sin6_addr));
1203 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1205 } else if (options & F_FQDNOLD) {
1206 /* packet format in 03 draft - no Subject data on queries */
1207 icp->icmp6_type = ICMP6_NI_QUERY;
1208 icp->icmp6_code = 0; /* code field is always 0 */
1209 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1210 nip->ni_flags = htons(0);
1212 memcpy(nip->icmp6_ni_nonce, nonce,
1213 sizeof(nip->icmp6_ni_nonce));
1214 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1218 } else if (options & F_NODEADDR) {
1219 icp->icmp6_type = ICMP6_NI_QUERY;
1220 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1221 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1222 nip->ni_flags = naflags;
1224 memcpy(nip->icmp6_ni_nonce, nonce,
1225 sizeof(nip->icmp6_ni_nonce));
1226 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1228 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1229 sizeof(dst.sin6_addr));
1230 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1232 } else if (options & F_SUPTYPES) {
1233 icp->icmp6_type = ICMP6_NI_QUERY;
1234 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1235 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1236 /* we support compressed bitmap */
1237 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1239 memcpy(nip->icmp6_ni_nonce, nonce,
1240 sizeof(nip->icmp6_ni_nonce));
1241 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1245 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1246 icp->icmp6_code = 0;
1247 icp->icmp6_id = htons(ident);
1248 icp->icmp6_seq = ntohs(seq);
1250 (void)gettimeofday((struct timeval *)
1251 &outpack[ICMP6ECHOLEN], NULL);
1252 cc = ICMP6ECHOLEN + datalen;
1256 if (pingerlen() != cc)
1257 errx(1, "internal error; length mismatch");
1260 smsghdr.msg_name = (caddr_t)&dst;
1261 smsghdr.msg_namelen = sizeof(dst);
1262 memset(&iov, 0, sizeof(iov));
1263 iov[0].iov_base = (caddr_t)outpack;
1264 iov[0].iov_len = cc;
1265 smsghdr.msg_iov = iov;
1266 smsghdr.msg_iovlen = 1;
1268 i = sendmsg(s, &smsghdr, 0);
1270 if (i < 0 || i != cc) {
1273 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1276 if (!(options & F_QUIET) && options & F_FLOOD)
1277 (void)write(STDOUT_FILENO, &DOT, 1);
1284 const struct icmp6_hdr *icp;
1286 if (ntohs(icp->icmp6_id) == ident)
1294 const struct icmp6_nodeinfo *nip;
1296 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1297 nonce + sizeof(u_int16_t),
1298 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1305 dnsdecode(sp, ep, base, buf, bufsiz)
1308 const u_char *base; /*base for compressed name*/
1314 char cresult[MAXDNAME + 1];
1325 if (i == 0 || cp != *sp) {
1326 if (strlcat(buf, ".", bufsiz) >= bufsiz)
1327 return NULL; /*result overrun*/
1333 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1334 /* DNS compression */
1338 comp = base + (i & 0x3f);
1339 if (dnsdecode(&comp, cp, base, cresult,
1340 sizeof(cresult)) == NULL)
1342 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1343 return NULL; /*result overrun*/
1345 } else if ((i & 0x3f) == i) {
1347 return NULL; /*source overrun*/
1348 while (i-- > 0 && cp < ep) {
1349 l = snprintf(cresult, sizeof(cresult),
1350 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1351 if (l < 0 || l >= sizeof(cresult))
1353 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1354 return NULL; /*result overrun*/
1358 return NULL; /*invalid label*/
1361 return NULL; /*not terminated*/
1369 * Print out the packet, if it came from us. This logic is necessary
1370 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1371 * which arrive ('tis only fair). This permits multiple copies of this
1372 * program to be run without having intermingled output (or statistics!).
1375 pr_pack(buf, cc, mhdr)
1378 struct msghdr *mhdr;
1380 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1381 struct icmp6_hdr *icp;
1382 struct icmp6_nodeinfo *ni;
1385 struct sockaddr *from;
1387 u_char *cp = NULL, *dp, *end = buf + cc;
1388 struct in6_pktinfo *pktinfo = NULL;
1389 struct timeval tv, *tp;
1390 double triptime = 0;
1395 char dnsname[MAXDNAME + 1];
1397 (void)gettimeofday(&tv, NULL);
1399 if (!mhdr || !mhdr->msg_name ||
1400 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1401 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1402 if (options & F_VERBOSE)
1403 warnx("invalid peername\n");
1406 from = (struct sockaddr *)mhdr->msg_name;
1407 fromlen = mhdr->msg_namelen;
1408 if (cc < sizeof(struct icmp6_hdr)) {
1409 if (options & F_VERBOSE)
1410 warnx("packet too short (%d bytes) from %s", cc,
1411 pr_addr(from, fromlen));
1414 icp = (struct icmp6_hdr *)buf;
1415 ni = (struct icmp6_nodeinfo *)buf;
1418 if ((hoplim = get_hoplim(mhdr)) == -1) {
1419 warnx("failed to get receiving hop limit");
1422 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1423 warnx("failed to get receiving packet information");
1427 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1428 seq = ntohs(icp->icmp6_seq);
1431 tp = (struct timeval *)(icp + 1);
1433 triptime = ((double)tv.tv_sec) * 1000.0 +
1434 ((double)tv.tv_usec) / 1000.0;
1436 #if defined(__OpenBSD__) || defined(__NetBSD__)
1437 tsumsq += triptime * triptime;
1439 if (triptime < tmin)
1441 if (triptime > tmax)
1445 if (TST(seq % mx_dup_ck)) {
1450 SET(seq % mx_dup_ck);
1454 if (options & F_QUIET)
1457 if (options & F_FLOOD)
1458 (void)write(STDOUT_FILENO, &BSPACE, 1);
1460 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1461 pr_addr(from, fromlen), seq);
1462 (void)printf(" hlim=%d", hoplim);
1463 if ((options & F_VERBOSE) != 0) {
1464 struct sockaddr_in6 dstsa;
1466 memset(&dstsa, 0, sizeof(dstsa));
1467 dstsa.sin6_family = AF_INET6;
1469 dstsa.sin6_len = sizeof(dstsa);
1471 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1472 dstsa.sin6_addr = pktinfo->ipi6_addr;
1473 (void)printf(" dst=%s",
1474 pr_addr((struct sockaddr *)&dstsa,
1478 (void)printf(" time=%g ms", triptime);
1480 (void)printf("(DUP!)");
1481 /* check the data */
1482 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1483 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1484 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1486 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1491 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1492 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1494 if (TST(seq % mx_dup_ck)) {
1499 SET(seq % mx_dup_ck);
1503 if (options & F_QUIET)
1506 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1508 switch (ntohs(ni->ni_code)) {
1509 case ICMP6_NI_SUCCESS:
1511 case ICMP6_NI_REFUSED:
1512 printf("refused, type 0x%x", ntohs(ni->ni_type));
1514 case ICMP6_NI_UNKNOWN:
1515 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1518 printf("unknown code 0x%x, type 0x%x",
1519 ntohs(ni->ni_code), ntohs(ni->ni_type));
1523 switch (ntohs(ni->ni_qtype)) {
1525 printf("NodeInfo NOOP");
1527 case NI_QTYPE_SUPTYPES:
1528 pr_suptypes(ni, end - (u_char *)ni);
1530 case NI_QTYPE_NODEADDR:
1531 pr_nodeaddr(ni, end - (u_char *)ni);
1534 default: /* XXX: for backward compatibility */
1535 cp = (u_char *)ni + ICMP6_NIRLEN;
1536 if (buf[off + ICMP6_NIRLEN] ==
1537 cc - off - ICMP6_NIRLEN - 1)
1542 cp++; /* skip length */
1544 safeputc(*cp & 0xff);
1550 if (dnsdecode((const u_char **)&cp, end,
1551 (const u_char *)(ni + 1), dnsname,
1552 sizeof(dnsname)) == NULL) {
1557 * name-lookup special handling for
1560 if (cp + 1 <= end && !*cp &&
1561 strlen(dnsname) > 0) {
1562 dnsname[strlen(dnsname) - 1] = '\0';
1565 printf("%s%s", i > 0 ? "," : "",
1569 if (options & F_VERBOSE) {
1573 (void)printf(" ("); /*)*/
1575 switch (ni->ni_code) {
1576 case ICMP6_NI_REFUSED:
1577 (void)printf("refused");
1580 case ICMP6_NI_UNKNOWN:
1581 (void)printf("unknown qtype");
1586 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1587 /* case of refusion, unknown */
1592 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1595 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1596 (void)printf("TTL=%d:meaningless",
1600 (void)printf("TTL=%d:invalid",
1603 (void)printf("TTL=%d", ttl);
1613 cp = (u_char *)ni + ICMP6_NIRLEN;
1622 if (buf[off + ICMP6_NIRLEN] !=
1623 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1626 (void)printf("invalid namelen:%d/%lu",
1627 buf[off + ICMP6_NIRLEN],
1628 (u_long)cc - off - ICMP6_NIRLEN - 1);
1638 /* We've got something other than an ECHOREPLY */
1639 if (!(options & F_VERBOSE))
1641 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1645 if (!(options & F_FLOOD)) {
1646 (void)putchar('\n');
1647 if (options & F_VERBOSE)
1649 (void)fflush(stdout);
1656 struct msghdr *mhdr;
1660 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1661 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1662 if (cm->cmsg_level != IPPROTO_IPV6)
1665 switch (cm->cmsg_type) {
1667 printf(" HbH Options: ");
1668 pr_ip6opt(CMSG_DATA(cm));
1671 #ifdef IPV6_RTHDRDSTOPTS
1672 case IPV6_RTHDRDSTOPTS:
1674 printf(" Dst Options: ");
1675 pr_ip6opt(CMSG_DATA(cm));
1678 printf(" Routing: ");
1679 pr_rthdr(CMSG_DATA(cm));
1685 #ifdef USE_RFC2292BIS
1687 pr_ip6opt(void *extbuf)
1689 struct ip6_hbh *ext;
1698 ext = (struct ip6_hbh *)extbuf;
1699 extlen = (ext->ip6h_len + 1) * 8;
1700 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1701 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1705 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1706 &type, &len, &databuf);
1707 if (currentlen == -1)
1711 * Note that inet6_opt_next automatically skips any padding
1716 offset = inet6_opt_get_val(databuf, offset,
1717 &value4, sizeof(value4));
1718 printf(" Jumbo Payload Opt: Length %u\n",
1719 (u_int32_t)ntohl(value4));
1721 case IP6OPT_ROUTER_ALERT:
1723 offset = inet6_opt_get_val(databuf, offset,
1724 &value2, sizeof(value2));
1725 printf(" Router Alert Opt: Type %u\n",
1729 printf(" Received Opt %u len %lu\n",
1730 type, (unsigned long)len);
1736 #else /* !USE_RFC2292BIS */
1739 pr_ip6opt(void *extbuf)
1744 #endif /* USE_RFC2292BIS */
1746 #ifdef USE_RFC2292BIS
1748 pr_rthdr(void *extbuf)
1750 struct in6_addr *in6;
1751 char ntopbuf[INET6_ADDRSTRLEN];
1752 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1755 /* print fixed part of the header */
1756 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1757 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1758 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1759 printf("%d segments, ", segments);
1761 printf("segments unknown, ");
1762 printf("%d left\n", rh->ip6r_segleft);
1764 for (i = 0; i < segments; i++) {
1765 in6 = inet6_rth_getaddr(extbuf, i);
1767 printf(" [%d]<NULL>\n", i);
1769 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1771 strncpy(ntopbuf, "?", sizeof(ntopbuf));
1772 printf(" [%d]%s\n", i, ntopbuf);
1780 #else /* !USE_RFC2292BIS */
1783 pr_rthdr(void *extbuf)
1788 #endif /* USE_RFC2292BIS */
1791 pr_bitrange(v, s, ii)
1801 /* shift till we have 0x01 */
1802 if ((v & 0x01) == 0) {
1804 printf("-%u", s + off - 1);
1815 case 0x04: case 0x0c:
1826 /* we have 0x01 with us */
1827 for (i = 0; i < 32 - off; i++) {
1828 if ((v & (0x01 << i)) == 0)
1832 printf(" %u", s + off);
1840 pr_suptypes(ni, nilen)
1841 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
1846 const u_char *cp, *end;
1849 u_int16_t words; /*32bit count*/
1852 #define MAXQTYPES (1 << 16)
1856 cp = (u_char *)(ni + 1);
1857 end = ((u_char *)ni) + nilen;
1861 printf("NodeInfo Supported Qtypes");
1862 if (options & F_VERBOSE) {
1863 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1864 printf(", compressed bitmap");
1866 printf(", raw bitmap");
1870 clen = (size_t)(end - cp);
1871 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1872 if (clen == 0 || clen > MAXQTYPES / 8 ||
1878 if (clen < sizeof(cbit) || clen % sizeof(v))
1880 memcpy(&cbit, cp, sizeof(cbit));
1881 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1885 clen = ntohs(cbit.words) * sizeof(v);
1886 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1891 for (off = 0; off < clen; off += sizeof(v)) {
1892 memcpy(&v, cp + off, sizeof(v));
1893 v = (u_int32_t)ntohl(v);
1894 b = pr_bitrange(v, (int)(cur + off * 8), b);
1896 /* flush the remaining bits */
1897 b = pr_bitrange(0, (int)(cur + off * 8), b);
1901 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1902 cur += ntohs(cbit.skip) * 32;
1907 pr_nodeaddr(ni, nilen)
1908 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1911 u_char *cp = (u_char *)(ni + 1);
1912 char ntop_buf[INET6_ADDRSTRLEN];
1915 nilen -= sizeof(struct icmp6_nodeinfo);
1917 if (options & F_VERBOSE) {
1918 switch (ni->ni_code) {
1919 case ICMP6_NI_REFUSED:
1920 (void)printf("refused");
1922 case ICMP6_NI_UNKNOWN:
1923 (void)printf("unknown qtype");
1926 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
1927 (void)printf(" truncated");
1931 printf(" no address\n");
1934 * In icmp-name-lookups 05 and later, TTL of each returned address
1935 * is contained in the resposne. We try to detect the version
1936 * by the length of the data, but note that the detection algorithm
1937 * is incomplete. We assume the latest draft by default.
1939 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
1945 /* XXX: alignment? */
1946 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
1947 cp += sizeof(u_int32_t);
1948 nilen -= sizeof(u_int32_t);
1951 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
1953 strncpy(ntop_buf, "?", sizeof(ntop_buf));
1954 printf(" %s", ntop_buf);
1956 if (ttl == 0xffffffff) {
1958 * XXX: can this convention be applied to all
1959 * type of TTL (i.e. non-ND TTL)?
1961 printf("(TTL=infty)");
1964 printf("(TTL=%u)", ttl);
1968 nilen -= sizeof(struct in6_addr);
1969 cp += sizeof(struct in6_addr);
1975 struct msghdr *mhdr;
1979 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1980 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1981 if (cm->cmsg_len == 0)
1984 if (cm->cmsg_level == IPPROTO_IPV6 &&
1985 cm->cmsg_type == IPV6_HOPLIMIT &&
1986 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1987 return(*(int *)CMSG_DATA(cm));
1993 struct in6_pktinfo *
1994 get_rcvpktinfo(mhdr)
1995 struct msghdr *mhdr;
1999 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2000 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2001 if (cm->cmsg_len == 0)
2004 if (cm->cmsg_level == IPPROTO_IPV6 &&
2005 cm->cmsg_type == IPV6_PKTINFO &&
2006 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2007 return((struct in6_pktinfo *)CMSG_DATA(cm));
2015 struct msghdr *mhdr;
2017 #ifdef IPV6_RECVPATHMTU
2019 struct ip6_mtuinfo *mtuctl = NULL;
2021 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2022 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2023 if (cm->cmsg_len == 0)
2026 if (cm->cmsg_level == IPPROTO_IPV6 &&
2027 cm->cmsg_type == IPV6_PATHMTU &&
2028 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2029 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2032 * If the notified destination is different from
2033 * the one we are pinging, just ignore the info.
2034 * We check the scope ID only when both notified value
2035 * and our own value have non-0 values, because we may
2036 * have used the default scope zone ID for sending,
2037 * in which case the scope ID value is 0.
2039 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2041 (mtuctl->ip6m_addr.sin6_scope_id &&
2042 dst.sin6_scope_id &&
2043 mtuctl->ip6m_addr.sin6_scope_id !=
2044 dst.sin6_scope_id)) {
2045 if ((options & F_VERBOSE) != 0) {
2046 printf("path MTU for %s is notified. "
2048 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2049 sizeof(mtuctl->ip6m_addr)));
2055 * Ignore an invalid MTU. XXX: can we just believe
2058 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2061 /* notification for our destination. return the MTU. */
2062 return((int)mtuctl->ip6m_mtu);
2074 static int firsttime = 1;
2078 int oldlen = smsghdr.msg_controllen;
2079 char *oldbuf = smsghdr.msg_control;
2081 /* XXX: We need to enlarge control message buffer */
2082 firsttime = 0; /* prevent further enlargement */
2084 smsghdr.msg_controllen = oldlen + CMSG_SPACE(sizeof(int));
2085 if ((smsghdr.msg_control =
2086 (char *)malloc(smsghdr.msg_controllen)) == NULL)
2087 err(1, "set_pathmtu: malloc");
2088 cm = (struct cmsghdr *)CMSG_FIRSTHDR(&smsghdr);
2089 cm->cmsg_len = CMSG_LEN(sizeof(int));
2090 cm->cmsg_level = IPPROTO_IPV6;
2091 cm->cmsg_type = IPV6_USE_MTU;
2093 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm);
2095 memcpy((void *)cm, (void *)oldbuf, oldlen);
2101 * look for a cmsgptr that points MTU structure.
2102 * XXX: this procedure seems redundant at this moment, but we'd better
2103 * keep the code generic enough for future extensions.
2105 for (cm = CMSG_FIRSTHDR(&smsghdr); cm;
2106 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm)) {
2107 if (cm->cmsg_len == 0) /* XXX: paranoid check */
2108 errx(1, "set_pathmtu: internal error");
2110 if (cm->cmsg_level == IPPROTO_IPV6 &&
2111 cm->cmsg_type == IPV6_USE_MTU &&
2112 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2117 errx(1, "set_pathmtu: internal error: no space for path MTU");
2119 *(int *)CMSG_DATA(cm) = mtu;
2125 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2130 register struct timeval *out, *in;
2132 if ((out->tv_usec -= in->tv_usec) < 0) {
2134 out->tv_usec += 1000000;
2136 out->tv_sec -= in->tv_sec;
2150 (void)signal(SIGINT, SIG_DFL);
2151 (void)kill(getpid(), SIGINT);
2159 * Print out statistics.
2165 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2166 (void)printf("%ld packets transmitted, ", ntransmitted);
2167 (void)printf("%ld packets received, ", nreceived);
2169 (void)printf("+%ld duplicates, ", nrepeats);
2171 if (nreceived > ntransmitted)
2172 (void)printf("-- somebody's printing up packets!");
2174 (void)printf("%d%% packet loss",
2175 (int) (((ntransmitted - nreceived) * 100) /
2178 (void)putchar('\n');
2179 if (nreceived && timing) {
2180 /* Only display average to microseconds */
2181 double num = nreceived + nrepeats;
2182 double avg = tsum / num;
2183 #if defined(__OpenBSD__) || defined(__NetBSD__)
2184 double dev = sqrt(tsumsq / num - avg * avg);
2186 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2187 tmin, avg, tmax, dev);
2190 "round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
2193 (void)fflush(stdout);
2195 (void)fflush(stdout);
2199 static char *niqcode[] = {
2201 "DNS label", /*or empty*/
2206 static char *nircode[] = {
2207 "Success", "Refused", "Unknown",
2213 * Print a descriptive string about an ICMP header.
2217 struct icmp6_hdr *icp;
2220 char ntop_buf[INET6_ADDRSTRLEN];
2221 struct nd_redirect *red;
2222 struct icmp6_nodeinfo *ni;
2223 char dnsname[MAXDNAME + 1];
2227 switch (icp->icmp6_type) {
2228 case ICMP6_DST_UNREACH:
2229 switch (icp->icmp6_code) {
2230 case ICMP6_DST_UNREACH_NOROUTE:
2231 (void)printf("No Route to Destination\n");
2233 case ICMP6_DST_UNREACH_ADMIN:
2234 (void)printf("Destination Administratively "
2237 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2238 (void)printf("Destination Unreachable Beyond Scope\n");
2240 case ICMP6_DST_UNREACH_ADDR:
2241 (void)printf("Destination Host Unreachable\n");
2243 case ICMP6_DST_UNREACH_NOPORT:
2244 (void)printf("Destination Port Unreachable\n");
2247 (void)printf("Destination Unreachable, Bad Code: %d\n",
2251 /* Print returned IP header information */
2252 pr_retip((struct ip6_hdr *)(icp + 1), end);
2254 case ICMP6_PACKET_TOO_BIG:
2255 (void)printf("Packet too big mtu = %d\n",
2256 (int)ntohl(icp->icmp6_mtu));
2257 pr_retip((struct ip6_hdr *)(icp + 1), end);
2259 case ICMP6_TIME_EXCEEDED:
2260 switch (icp->icmp6_code) {
2261 case ICMP6_TIME_EXCEED_TRANSIT:
2262 (void)printf("Time to live exceeded\n");
2264 case ICMP6_TIME_EXCEED_REASSEMBLY:
2265 (void)printf("Frag reassembly time exceeded\n");
2268 (void)printf("Time exceeded, Bad Code: %d\n",
2272 pr_retip((struct ip6_hdr *)(icp + 1), end);
2274 case ICMP6_PARAM_PROB:
2275 (void)printf("Parameter problem: ");
2276 switch (icp->icmp6_code) {
2277 case ICMP6_PARAMPROB_HEADER:
2278 (void)printf("Erroneous Header ");
2280 case ICMP6_PARAMPROB_NEXTHEADER:
2281 (void)printf("Unknown Nextheader ");
2283 case ICMP6_PARAMPROB_OPTION:
2284 (void)printf("Unrecognized Option ");
2287 (void)printf("Bad code(%d) ", icp->icmp6_code);
2290 (void)printf("pointer = 0x%02x\n",
2291 (u_int32_t)ntohl(icp->icmp6_pptr));
2292 pr_retip((struct ip6_hdr *)(icp + 1), end);
2294 case ICMP6_ECHO_REQUEST:
2295 (void)printf("Echo Request");
2296 /* XXX ID + Seq + Data */
2298 case ICMP6_ECHO_REPLY:
2299 (void)printf("Echo Reply");
2300 /* XXX ID + Seq + Data */
2302 case ICMP6_MEMBERSHIP_QUERY:
2303 (void)printf("Listener Query");
2305 case ICMP6_MEMBERSHIP_REPORT:
2306 (void)printf("Listener Report");
2308 case ICMP6_MEMBERSHIP_REDUCTION:
2309 (void)printf("Listener Done");
2311 case ND_ROUTER_SOLICIT:
2312 (void)printf("Router Solicitation");
2314 case ND_ROUTER_ADVERT:
2315 (void)printf("Router Advertisement");
2317 case ND_NEIGHBOR_SOLICIT:
2318 (void)printf("Neighbor Solicitation");
2320 case ND_NEIGHBOR_ADVERT:
2321 (void)printf("Neighbor Advertisement");
2324 red = (struct nd_redirect *)icp;
2325 (void)printf("Redirect\n");
2326 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2328 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2329 (void)printf("Destination: %s", ntop_buf);
2330 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2332 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2333 (void)printf(" New Target: %s", ntop_buf);
2335 case ICMP6_NI_QUERY:
2336 (void)printf("Node Information Query");
2337 /* XXX ID + Seq + Data */
2338 ni = (struct icmp6_nodeinfo *)icp;
2339 l = end - (u_char *)(ni + 1);
2341 switch (ntohs(ni->ni_qtype)) {
2343 (void)printf("NOOP");
2345 case NI_QTYPE_SUPTYPES:
2346 (void)printf("Supported qtypes");
2349 (void)printf("DNS name");
2351 case NI_QTYPE_NODEADDR:
2352 (void)printf("nodeaddr");
2354 case NI_QTYPE_IPV4ADDR:
2355 (void)printf("IPv4 nodeaddr");
2358 (void)printf("unknown qtype");
2361 if (options & F_VERBOSE) {
2362 switch (ni->ni_code) {
2363 case ICMP6_NI_SUBJ_IPV6:
2364 if (l == sizeof(struct in6_addr) &&
2365 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2366 sizeof(ntop_buf)) != NULL) {
2367 (void)printf(", subject=%s(%s)",
2368 niqcode[ni->ni_code], ntop_buf);
2371 /* backward compat to -W */
2372 (void)printf(", oldfqdn");
2374 (void)printf(", invalid");
2378 case ICMP6_NI_SUBJ_FQDN:
2379 if (end == (u_char *)(ni + 1)) {
2380 (void)printf(", no subject");
2383 printf(", subject=%s", niqcode[ni->ni_code]);
2384 cp = (const u_char *)(ni + 1);
2385 if (dnsdecode(&cp, end, NULL, dnsname,
2386 sizeof(dnsname)) != NULL)
2387 printf("(%s)", dnsname);
2389 printf("(invalid)");
2391 case ICMP6_NI_SUBJ_IPV4:
2392 if (l == sizeof(struct in_addr) &&
2393 inet_ntop(AF_INET, ni + 1, ntop_buf,
2394 sizeof(ntop_buf)) != NULL) {
2395 (void)printf(", subject=%s(%s)",
2396 niqcode[ni->ni_code], ntop_buf);
2398 (void)printf(", invalid");
2401 (void)printf(", invalid");
2406 case ICMP6_NI_REPLY:
2407 (void)printf("Node Information Reply");
2408 /* XXX ID + Seq + Data */
2409 ni = (struct icmp6_nodeinfo *)icp;
2411 switch (ntohs(ni->ni_qtype)) {
2413 (void)printf("NOOP");
2415 case NI_QTYPE_SUPTYPES:
2416 (void)printf("Supported qtypes");
2419 (void)printf("DNS name");
2421 case NI_QTYPE_NODEADDR:
2422 (void)printf("nodeaddr");
2424 case NI_QTYPE_IPV4ADDR:
2425 (void)printf("IPv4 nodeaddr");
2428 (void)printf("unknown qtype");
2431 if (options & F_VERBOSE) {
2432 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2433 printf(", invalid");
2435 printf(", %s", nircode[ni->ni_code]);
2439 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2445 * Print an IP6 header.
2449 struct ip6_hdr *ip6;
2451 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2453 char ntop_buf[INET6_ADDRSTRLEN];
2455 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2456 tc = (tc >> 4) & 0x0f;
2457 tc |= (ip6->ip6_vfc << 4);
2459 printf("Vr TC Flow Plen Nxt Hlim\n");
2460 printf(" %1x %02x %05x %04x %02x %02x\n",
2461 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2462 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2463 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2464 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2465 printf("%s->", ntop_buf);
2466 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2467 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2468 printf("%s\n", ntop_buf);
2473 * Return an ascii host address as a dotted quad and optionally with
2477 pr_addr(addr, addrlen)
2478 struct sockaddr *addr;
2481 static char buf[NI_MAXHOST];
2484 #ifdef NI_WITHSCOPEID
2485 flag = NI_WITHSCOPEID;
2489 if ((options & F_HOSTNAME) == 0)
2490 flag |= NI_NUMERICHOST;
2492 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2500 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2504 struct ip6_hdr *ip6;
2507 u_char *cp = (u_char *)ip6, nh;
2510 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2515 hlen = sizeof(*ip6);
2519 while (end - cp >= 8) {
2521 case IPPROTO_HOPOPTS:
2523 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2524 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2526 case IPPROTO_DSTOPTS:
2528 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2529 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2531 case IPPROTO_FRAGMENT:
2533 hlen = sizeof(struct ip6_frag);
2534 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2536 case IPPROTO_ROUTING:
2538 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2539 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2544 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2545 nh = ((struct ah *)cp)->ah_nxt;
2548 case IPPROTO_ICMPV6:
2549 printf("ICMP6: type = %d, code = %d\n",
2556 printf("TCP: from port %u, to port %u (decimal)\n",
2557 (*cp * 256 + *(cp + 1)),
2558 (*(cp + 2) * 256 + *(cp + 3)));
2561 printf("UDP: from port %u, to port %u (decimal)\n",
2562 (*cp * 256 + *(cp + 1)),
2563 (*(cp + 2) * 256 + *(cp + 3)));
2566 printf("Unknown Header(%d)\n", nh);
2570 if ((cp += hlen) >= end)
2588 register int ii, jj, kk;
2592 for (cp = patp; *cp; cp++)
2594 errx(1, "patterns must be specified as hex digits");
2596 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2597 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2598 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2599 &pat[13], &pat[14], &pat[15]);
2604 kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
2606 for (jj = 0; jj < ii; ++jj)
2607 bp[jj + kk] = pat[jj];
2608 if (!(options & F_QUIET)) {
2609 (void)printf("PATTERN: 0x");
2610 for (jj = 0; jj < ii; ++jj)
2611 (void)printf("%02x", bp[jj] & 0xFF);
2617 #ifdef IPSEC_POLICY_IPSEC
2619 setpolicy(so, policy)
2626 return 0; /* ignore */
2628 buf = ipsec_set_policy(policy, strlen(policy));
2630 errx(1, "%s", ipsec_strerror());
2631 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2632 ipsec_get_policylen(buf)) < 0)
2633 warnx("unable to set IPSec policy");
2648 u_int8_t digest[16];
2651 char hbuf[NI_MAXHOST];
2652 struct in6_addr in6;
2654 p = strchr(name, '.');
2656 p = name + strlen(name);
2658 if (l > 63 || l > sizeof(hbuf) - 1)
2659 return NULL; /*label too long*/
2660 strncpy(hbuf, name, l);
2661 hbuf[(int)l] = '\0';
2663 for (q = name; *q; q++) {
2668 /* generate 8 bytes of pseudo-random value. */
2669 bzero(&ctxt, sizeof(ctxt));
2672 MD5Update(&ctxt, &c, sizeof(c));
2673 MD5Update(&ctxt, name, l);
2674 MD5Final(digest, &ctxt);
2676 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2677 return NULL; /*XXX*/
2678 bcopy(digest, &in6.s6_addr[12], 4);
2680 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2683 return strdup(hbuf);
2689 (void)fprintf(stderr,
2690 "usage: ping6 [-dfH"
2691 #ifdef IPV6_USE_MIN_MTU
2695 #ifdef IPV6_REACHCONF
2699 #ifdef IPSEC_POLICY_IPSEC
2705 "] [-a [aAclsg]] [-b sockbufsiz] [-c count]\n"
2706 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2708 "\t[-s packetsize] [-h hoplimit] [hops...] host\n");