1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
37 * Copyright (c) 1989, 1993
38 * The Regents of the University of California. All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 static const char copyright[] =
70 "@(#) Copyright (c) 1989, 1993\n\
71 The Regents of the University of California. All rights reserved.\n";
76 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 #include <sys/cdefs.h>
81 __FBSDID("$FreeBSD$");
84 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
85 * measure round-trip-delays and packet loss across network paths.
89 * U. S. Army Ballistic Research Laboratory
93 * Public Domain. Distribution Unlimited.
95 * More statistics could always be gathered.
96 * This program has to run SUID to ROOT to access the ICMP socket.
100 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
101 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
102 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
103 * network attached to 1 or more interfaces)
106 #include <sys/param.h>
108 #include <sys/socket.h>
109 #include <sys/time.h>
112 #include <net/route.h>
114 #include <netinet/in.h>
115 #include <netinet/ip6.h>
116 #include <netinet/icmp6.h>
117 #include <arpa/inet.h>
118 #include <arpa/nameser.h>
130 #include <sysexits.h>
134 #include <netipsec/ah.h>
135 #include <netipsec/ipsec.h>
145 #define MAXPACKETLEN 131072
147 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
148 #define ICMP6ECHOTMLEN sizeof(struct tv32)
149 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
150 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
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 */
157 #define MAXWAIT 10000 /* max ms to wait for response */
158 #define MAXALARM (60 * 60) /* max seconds for alarm timeout */
160 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
161 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
162 #define SET(bit) (A(bit) |= B(bit))
163 #define CLR(bit) (A(bit) &= (~B(bit)))
164 #define TST(bit) (A(bit) & B(bit))
166 #define F_FLOOD 0x0001
167 #define F_INTERVAL 0x0002
168 #define F_PINGFILLED 0x0008
169 #define F_QUIET 0x0010
170 #define F_RROUTE 0x0020
171 #define F_SO_DEBUG 0x0040
172 #define F_VERBOSE 0x0100
174 #ifdef IPSEC_POLICY_IPSEC
175 #define F_POLICY 0x0400
177 #define F_AUTHHDR 0x0200
178 #define F_ENCRYPT 0x0400
179 #endif /*IPSEC_POLICY_IPSEC*/
181 #define F_NODEADDR 0x0800
182 #define F_FQDN 0x1000
183 #define F_INTERFACE 0x2000
184 #define F_SRCADDR 0x4000
185 #define F_HOSTNAME 0x10000
186 #define F_FQDNOLD 0x20000
187 #define F_NIGROUP 0x40000
188 #define F_SUPTYPES 0x80000
189 #define F_NOMINMTU 0x100000
190 #define F_ONCE 0x200000
191 #define F_AUDIBLE 0x400000
192 #define F_MISSED 0x800000
193 #define F_DONTFRAG 0x1000000
194 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
195 #define F_WAITTIME 0x2000000
198 #define IN6LEN sizeof(struct in6_addr)
199 #define SA6LEN sizeof(struct sockaddr_in6)
200 #define DUMMY_PORT 10101
202 #define SIN6(s) ((struct sockaddr_in6 *)(s))
205 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
206 * number of received sequence numbers we can keep track of. Change 128
207 * to 8192 for complete accuracy...
209 #define MAX_DUP_CHK (8 * 8192)
210 static int mx_dup_ck = MAX_DUP_CHK;
211 static char rcvd_tbl[MAX_DUP_CHK / 8];
213 static struct sockaddr_in6 dst; /* who to ping6 */
214 static struct sockaddr_in6 src; /* src addr of this packet */
215 static socklen_t srclen;
216 static size_t datalen = DEFDATALEN;
217 static int s; /* socket file descriptor */
218 static u_char outpack[MAXPACKETLEN];
219 static char BSPACE = '\b'; /* characters written for flood */
220 static char BBELL = '\a'; /* characters written for AUDIBLE */
221 static char DOT = '.';
222 static char *hostname;
223 static int ident; /* process id to identify our packets */
224 static u_int8_t nonce[8]; /* nonce field for node information */
225 static int hoplimit = -1; /* hoplimit */
226 static u_char *packet = NULL;
229 static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */
230 static long npackets; /* max packets to transmit */
231 static long nreceived; /* # of packets we got back */
232 static long nrepeats; /* number of duplicates */
233 static long ntransmitted; /* sequence # for outbound packets = #sent */
234 static int interval = 1000; /* interval between packets in ms */
235 static int waittime = MAXWAIT; /* timeout for each packet */
236 static long nrcvtimeout = 0; /* # of packets we got back after waittime */
239 static int timing; /* flag to do timing */
240 static double tmin = 999999999.0; /* minimum round trip time */
241 static double tmax = 0.0; /* maximum round trip time */
242 static double tsum = 0.0; /* sum of all times, for doing average */
243 static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
245 /* for node addresses */
246 static u_short naflags;
248 /* for ancillary data(advanced API) */
249 static struct msghdr smsghdr;
250 static struct iovec smsgiov;
251 static char *scmsg = 0;
253 static volatile sig_atomic_t seenint;
255 static volatile sig_atomic_t seeninfo;
258 int main(int, char *[]);
259 static void fill(char *, char *);
260 static int get_hoplim(struct msghdr *);
261 static int get_pathmtu(struct msghdr *);
262 static struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
263 static void onsignal(int);
264 static void onint(int);
265 static size_t pingerlen(void);
266 static int pinger(void);
267 static const char *pr_addr(struct sockaddr *, int);
268 static void pr_icmph(struct icmp6_hdr *, u_char *);
269 static void pr_iph(struct ip6_hdr *);
270 static void pr_suptypes(struct icmp6_nodeinfo *, size_t);
271 static void pr_nodeaddr(struct icmp6_nodeinfo *, int);
272 static int myechoreply(const struct icmp6_hdr *);
273 static int mynireply(const struct icmp6_nodeinfo *);
274 static char *dnsdecode(const u_char **, const u_char *, const u_char *,
276 static void pr_pack(u_char *, int, struct msghdr *);
277 static void pr_exthdrs(struct msghdr *);
278 static void pr_ip6opt(void *, size_t);
279 static void pr_rthdr(void *, size_t);
280 static int pr_bitrange(u_int32_t, int, int);
281 static void pr_retip(struct ip6_hdr *, u_char *);
282 static void summary(void);
283 static void tvsub(struct timeval *, struct timeval *);
284 static int setpolicy(int, char *);
285 static char *nigroup(char *, int);
286 static void usage(void);
289 main(int argc, char *argv[])
291 struct timeval last, intvl;
292 struct sockaddr_in6 from, *sin6;
293 struct addrinfo hints, *res;
294 struct sigaction si_sa;
296 int almost_done, ch, hold, packlen, preload, optval, error;
297 int nig_oldmcprefix = -1;
299 char *e, *target, *ifname = NULL, *gateway = NULL;
301 struct cmsghdr *scmsgp = NULL;
302 /* For control (ancillary) data received from recvmsg() */
303 struct cmsghdr cm[CONTROLLEN];
304 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
309 struct in6_pktinfo *pktinfo = NULL;
310 struct ip6_rthdr *rthdr = NULL;
311 #ifdef IPSEC_POLICY_IPSEC
312 char *policy_in = NULL;
313 char *policy_out = NULL;
318 #ifdef IPV6_USE_MIN_MTU
322 /* just to be sure */
323 memset(&smsghdr, 0, sizeof(smsghdr));
324 memset(&smsgiov, 0, sizeof(smsgiov));
326 alarmtimeout = preload = 0;
327 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
331 #ifdef IPSEC_POLICY_IPSEC
335 #endif /*IPSEC_POLICY_IPSEC*/
337 while ((ch = getopt(argc, argv,
338 "a:b:c:DdfHg:h:I:i:l:mnNop:qrRS:s:tvwWx:X:" ADDOPTS)) != -1) {
345 options &= ~F_NOUSERDATA;
346 options |= F_NODEADDR;
347 for (cp = optarg; *cp != '\0'; cp++) {
350 naflags |= NI_NODEADDR_FLAG_ALL;
354 naflags |= NI_NODEADDR_FLAG_COMPAT;
358 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
362 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
366 naflags |= NI_NODEADDR_FLAG_GLOBAL;
368 case 'A': /* experimental. not in the spec */
369 #ifdef NI_NODEADDR_FLAG_ANYCAST
370 naflags |= NI_NODEADDR_FLAG_ANYCAST;
374 "-a A is not supported on the platform");
385 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
388 lsockbufsize = strtoul(optarg, &e, 10);
389 sockbufsize = (int)lsockbufsize;
390 if (errno || !*optarg || *e ||
391 lsockbufsize > INT_MAX)
392 errx(1, "invalid socket buffer size");
395 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
399 npackets = strtol(optarg, &e, 10);
400 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
402 "illegal number of packets -- %s", optarg);
405 options |= F_DONTFRAG;
408 options |= F_SO_DEBUG;
413 errx(1, "Must be superuser to flood ping");
416 setbuf(stdout, (char *)NULL);
422 options |= F_HOSTNAME;
424 case 'h': /* hoplimit */
425 hoplimit = strtol(optarg, &e, 10);
426 if (*optarg == '\0' || *e != '\0')
427 errx(1, "illegal hoplimit %s", optarg);
428 if (255 < hoplimit || hoplimit < -1)
430 "illegal hoplimit -- %s", optarg);
434 options |= F_INTERFACE;
435 #ifndef USE_SIN6_SCOPE_ID
439 case 'i': /* wait between sending packets */
440 t = strtod(optarg, &e);
441 if (*optarg == '\0' || *e != '\0')
442 errx(1, "illegal timing interval %s", optarg);
443 if (t < 1 && getuid()) {
444 errx(1, "%s: only root may use interval < 1s",
447 intvl.tv_sec = (long)t;
449 (long)((t - intvl.tv_sec) * 1000000);
450 if (intvl.tv_sec < 0)
451 errx(1, "illegal timing interval %s", optarg);
452 /* less than 1/hz does not make sense */
453 if (intvl.tv_sec == 0 && intvl.tv_usec < 1) {
454 warnx("too small interval, raised to .000001");
457 options |= F_INTERVAL;
462 errx(1, "Must be superuser to preload");
464 preload = strtol(optarg, &e, 10);
465 if (preload < 0 || *optarg == '\0' || *e != '\0')
466 errx(1, "illegal preload value -- %s", optarg);
469 #ifdef IPV6_USE_MIN_MTU
473 errx(1, "-%c is not supported on this platform", ch);
477 options &= ~F_HOSTNAME;
480 options |= F_NIGROUP;
486 case 'p': /* fill buffer with user pattern */
487 options |= F_PINGFILLED;
488 fill((char *)datap, optarg);
494 options |= F_AUDIBLE;
500 memset(&hints, 0, sizeof(struct addrinfo));
501 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
502 hints.ai_family = AF_INET6;
503 hints.ai_socktype = SOCK_RAW;
504 hints.ai_protocol = IPPROTO_ICMPV6;
506 error = getaddrinfo(optarg, NULL, &hints, &res);
508 errx(1, "invalid source address: %s",
509 gai_strerror(error));
512 * res->ai_family must be AF_INET6 and res->ai_addrlen
513 * must be sizeof(src).
515 memcpy(&src, res->ai_addr, res->ai_addrlen);
516 srclen = res->ai_addrlen;
518 options |= F_SRCADDR;
520 case 's': /* size of packet to send */
521 datalen = strtol(optarg, &e, 10);
522 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
523 errx(1, "illegal datalen value -- %s", optarg);
524 if (datalen > MAXDATALEN) {
526 "datalen value too large, maximum is %d",
531 options &= ~F_NOUSERDATA;
532 options |= F_SUPTYPES;
535 options |= F_VERBOSE;
538 options &= ~F_NOUSERDATA;
542 options &= ~F_NOUSERDATA;
543 options |= F_FQDNOLD;
546 t = strtod(optarg, &e);
547 if (*e || e == optarg || t > (double)INT_MAX)
548 err(EX_USAGE, "invalid timing interval: `%s'",
550 options |= F_WAITTIME;
554 alarmtimeout = strtoul(optarg, &e, 0);
555 if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
556 errx(EX_USAGE, "invalid timeout: `%s'",
558 if (alarmtimeout > MAXALARM)
559 errx(EX_USAGE, "invalid timeout: `%s' > %d",
561 alarm((int)alarmtimeout);
564 #ifdef IPSEC_POLICY_IPSEC
567 if (!strncmp("in", optarg, 2)) {
568 if ((policy_in = strdup(optarg)) == NULL)
570 } else if (!strncmp("out", optarg, 3)) {
571 if ((policy_out = strdup(optarg)) == NULL)
574 errx(1, "invalid security policy");
578 options |= F_AUTHHDR;
581 options |= F_ENCRYPT;
583 #endif /*IPSEC_POLICY_IPSEC*/
600 #ifdef IPV6_RECVRTHDR /* 2292bis */
601 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
604 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
607 errx(1, "too many intermediate hops");
613 if (options & F_NIGROUP) {
614 target = nigroup(argv[argc - 1], nig_oldmcprefix);
615 if (target == NULL) {
620 target = argv[argc - 1];
623 memset(&hints, 0, sizeof(struct addrinfo));
624 hints.ai_flags = AI_CANONNAME;
625 hints.ai_family = AF_INET6;
626 hints.ai_socktype = SOCK_RAW;
627 hints.ai_protocol = IPPROTO_ICMPV6;
629 error = getaddrinfo(target, NULL, &hints, &res);
631 errx(1, "%s", gai_strerror(error));
632 if (res->ai_canonname)
633 hostname = strdup(res->ai_canonname);
638 errx(1, "getaddrinfo failed");
640 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
642 if ((s = socket(res->ai_family, res->ai_socktype,
643 res->ai_protocol)) < 0)
647 /* set the source address if specified. */
648 if ((options & F_SRCADDR) != 0) {
649 /* properly fill sin6_scope_id */
650 if (IN6_IS_ADDR_LINKLOCAL(&src.sin6_addr) && (
651 IN6_IS_ADDR_LINKLOCAL(&dst.sin6_addr) ||
652 IN6_IS_ADDR_MC_LINKLOCAL(&dst.sin6_addr) ||
653 IN6_IS_ADDR_MC_NODELOCAL(&dst.sin6_addr))) {
654 if (src.sin6_scope_id == 0)
655 src.sin6_scope_id = dst.sin6_scope_id;
656 if (dst.sin6_scope_id == 0)
657 dst.sin6_scope_id = src.sin6_scope_id;
659 if (bind(s, (struct sockaddr *)&src, srclen) != 0)
662 /* set the gateway (next hop) if specified */
664 memset(&hints, 0, sizeof(hints));
665 hints.ai_family = AF_INET6;
666 hints.ai_socktype = SOCK_RAW;
667 hints.ai_protocol = IPPROTO_ICMPV6;
669 error = getaddrinfo(gateway, NULL, &hints, &res);
671 errx(1, "getaddrinfo for the gateway %s: %s",
672 gateway, gai_strerror(error));
674 if (res->ai_next && (options & F_VERBOSE))
675 warnx("gateway resolves to multiple addresses");
677 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
678 res->ai_addr, res->ai_addrlen)) {
679 err(1, "setsockopt(IPV6_NEXTHOP)");
686 * let the kerel pass extension headers of incoming packets,
687 * for privileged socket options
689 if ((options & F_VERBOSE) != 0) {
692 #ifdef IPV6_RECVHOPOPTS
693 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
695 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
696 #else /* old adv. API */
697 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
699 err(1, "setsockopt(IPV6_HOPOPTS)");
701 #ifdef IPV6_RECVDSTOPTS
702 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
704 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
705 #else /* old adv. API */
706 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
708 err(1, "setsockopt(IPV6_DSTOPTS)");
710 #ifdef IPV6_RECVRTHDRDSTOPTS
711 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
713 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
717 /* revoke root privilege */
718 if (seteuid(getuid()) != 0)
719 err(1, "seteuid() failed");
720 if (setuid(getuid()) != 0)
721 err(1, "setuid() failed");
723 if ((options & F_FLOOD) && (options & F_INTERVAL))
724 errx(1, "-f and -i incompatible options");
726 if ((options & F_NOUSERDATA) == 0) {
727 if (datalen >= sizeof(struct tv32)) {
728 /* we can time transfer */
732 /* in F_VERBOSE case, we may get non-echoreply packets*/
733 if (options & F_VERBOSE)
734 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
736 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
738 /* suppress timing for node information query */
741 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
744 if (!(packet = (u_char *)malloc((u_int)packlen)))
745 err(1, "Unable to allocate packet");
746 if (!(options & F_PINGFILLED))
747 for (i = ICMP6ECHOLEN; i < packlen; ++i)
750 ident = getpid() & 0xFFFF;
751 arc4random_buf(nonce, sizeof(nonce));
753 if (options & F_DONTFRAG)
754 if (setsockopt(s, IPPROTO_IPV6, IPV6_DONTFRAG,
755 &optval, sizeof(optval)) == -1)
756 err(1, "IPV6_DONTFRAG");
759 if (options & F_SO_DEBUG)
760 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
762 optval = IPV6_DEFHLIM;
763 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
764 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
765 &optval, sizeof(optval)) == -1)
766 err(1, "IPV6_MULTICAST_HOPS");
767 #ifdef IPV6_USE_MIN_MTU
769 optval = mflag > 1 ? 0 : 1;
771 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
772 &optval, sizeof(optval)) == -1)
773 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
775 #ifdef IPV6_RECVPATHMTU
778 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
779 &optval, sizeof(optval)) == -1)
780 err(1, "setsockopt(IPV6_RECVPATHMTU)");
782 #endif /* IPV6_RECVPATHMTU */
783 #endif /* IPV6_USE_MIN_MTU */
786 #ifdef IPSEC_POLICY_IPSEC
787 if (options & F_POLICY) {
788 if (setpolicy(s, policy_in) < 0)
789 errx(1, "%s", ipsec_strerror());
790 if (setpolicy(s, policy_out) < 0)
791 errx(1, "%s", ipsec_strerror());
794 if (options & F_AUTHHDR) {
795 optval = IPSEC_LEVEL_REQUIRE;
796 #ifdef IPV6_AUTH_TRANS_LEVEL
797 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
798 &optval, sizeof(optval)) == -1)
799 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
801 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
802 &optval, sizeof(optval)) == -1)
803 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
806 if (options & F_ENCRYPT) {
807 optval = IPSEC_LEVEL_REQUIRE;
808 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
809 &optval, sizeof(optval)) == -1)
810 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
812 #endif /*IPSEC_POLICY_IPSEC*/
817 struct icmp6_filter filt;
818 if (!(options & F_VERBOSE)) {
819 ICMP6_FILTER_SETBLOCKALL(&filt);
820 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
821 (options & F_NODEADDR) || (options & F_SUPTYPES))
822 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
824 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
826 ICMP6_FILTER_SETPASSALL(&filt);
828 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
830 err(1, "setsockopt(ICMP6_FILTER)");
832 #endif /*ICMP6_FILTER*/
834 /* let the kerel pass extension headers of incoming packets */
835 if ((options & F_VERBOSE) != 0) {
838 #ifdef IPV6_RECVRTHDR
839 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
841 err(1, "setsockopt(IPV6_RECVRTHDR)");
842 #else /* old adv. API */
843 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
845 err(1, "setsockopt(IPV6_RTHDR)");
851 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
852 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
853 &optval, sizeof(optval)) == -1)
854 err(1, "IPV6_MULTICAST_LOOP");
857 /* Specify the outgoing interface and/or the source address */
859 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
862 ip6optlen += CMSG_SPACE(sizeof(int));
864 /* set IP6 packet options */
866 if ((scmsg = (char *)malloc(ip6optlen)) == NULL)
867 errx(1, "can't allocate enough memory");
868 smsghdr.msg_control = (caddr_t)scmsg;
869 smsghdr.msg_controllen = ip6optlen;
870 scmsgp = (struct cmsghdr *)scmsg;
873 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
874 memset(pktinfo, 0, sizeof(*pktinfo));
875 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
876 scmsgp->cmsg_level = IPPROTO_IPV6;
877 scmsgp->cmsg_type = IPV6_PKTINFO;
878 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
881 /* set the outgoing interface */
883 #ifndef USE_SIN6_SCOPE_ID
884 /* pktinfo must have already been allocated */
885 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
886 errx(1, "%s: invalid interface name", ifname);
888 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
889 errx(1, "%s: invalid interface name", ifname);
892 if (hoplimit != -1) {
893 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
894 scmsgp->cmsg_level = IPPROTO_IPV6;
895 scmsgp->cmsg_type = IPV6_HOPLIMIT;
896 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
898 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
901 if (argc > 1) { /* some intermediate addrs are specified */
905 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
906 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
907 scmsgp->cmsg_level = IPPROTO_IPV6;
908 scmsgp->cmsg_type = IPV6_RTHDR;
909 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
910 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
911 IPV6_RTHDR_TYPE_0, argc - 1);
913 errx(1, "can't initialize rthdr");
915 for (hops = 0; hops < argc - 1; hops++) {
916 memset(&hints, 0, sizeof(hints));
917 hints.ai_family = AF_INET6;
919 if ((error = getaddrinfo(argv[hops], NULL, &hints,
921 errx(1, "%s", gai_strerror(error));
922 if (res->ai_addr->sa_family != AF_INET6)
924 "bad addr family of an intermediate addr");
925 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
926 if (inet6_rth_add(rthdr, &sin6->sin6_addr))
927 errx(1, "can't add an intermediate node");
931 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
934 if (!(options & F_SRCADDR)) {
936 * get the source address. XXX since we revoked the root
937 * privilege, we cannot use a raw socket for this.
940 socklen_t len = sizeof(src);
942 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
943 err(1, "UDP socket");
945 src.sin6_family = AF_INET6;
946 src.sin6_addr = dst.sin6_addr;
947 src.sin6_port = ntohs(DUMMY_PORT);
948 src.sin6_scope_id = dst.sin6_scope_id;
951 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
952 (void *)pktinfo, sizeof(*pktinfo)))
953 err(1, "UDP setsockopt(IPV6_PKTINFO)");
955 if (hoplimit != -1 &&
956 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
957 (void *)&hoplimit, sizeof(hoplimit)))
958 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
960 if (hoplimit != -1 &&
961 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
962 (void *)&hoplimit, sizeof(hoplimit)))
963 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
966 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
967 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
968 err(1, "UDP setsockopt(IPV6_RTHDR)");
970 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
971 err(1, "UDP connect");
973 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
974 err(1, "getsockname");
979 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
981 if (datalen > (size_t)sockbufsize)
982 warnx("you need -b to increase socket buffer size");
983 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
984 sizeof(sockbufsize)) < 0)
985 err(1, "setsockopt(SO_SNDBUF)");
986 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
987 sizeof(sockbufsize)) < 0)
988 err(1, "setsockopt(SO_RCVBUF)");
991 if (datalen > 8 * 1024) /*XXX*/
992 warnx("you need -b to increase socket buffer size");
994 * When pinging the broadcast address, you can get a lot of
995 * answers. Doing something so evil is useful if you are trying
996 * to stress the ethernet, or just want to fill the arp cache
997 * to get some stuff for /etc/ethers.
1000 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1006 #ifndef USE_SIN6_SCOPE_ID
1007 #ifdef IPV6_RECVPKTINFO
1008 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1009 sizeof(optval)) < 0)
1010 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1011 #else /* old adv. API */
1012 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1013 sizeof(optval)) < 0)
1014 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1016 #endif /* USE_SIN6_SCOPE_ID */
1017 #ifdef IPV6_RECVHOPLIMIT
1018 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1019 sizeof(optval)) < 0)
1020 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1021 #else /* old adv. API */
1022 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1023 sizeof(optval)) < 0)
1024 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1027 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1028 (unsigned long)(pingerlen() - 8));
1029 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1030 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1035 if (npackets != 0 && preload > npackets)
1040 gettimeofday(&last, NULL);
1042 sigemptyset(&si_sa.sa_mask);
1044 si_sa.sa_handler = onsignal;
1045 if (sigaction(SIGINT, &si_sa, 0) == -1)
1046 err(EX_OSERR, "sigaction SIGINT");
1049 if (sigaction(SIGINFO, &si_sa, 0) == -1)
1050 err(EX_OSERR, "sigaction SIGINFO");
1053 if (alarmtimeout > 0) {
1054 if (sigaction(SIGALRM, &si_sa, 0) == -1)
1055 err(EX_OSERR, "sigaction SIGALRM");
1057 if (options & F_FLOOD) {
1059 intvl.tv_usec = 10000;
1060 } else if ((options & F_INTERVAL) == 0) {
1061 intvl.tv_sec = interval / 1000;
1062 intvl.tv_usec = interval % 1000 * 1000;
1066 while (seenint == 0) {
1067 struct timeval now, timeout;
1069 struct iovec iov[2];
1073 /* signal handling */
1085 gettimeofday(&now, NULL);
1086 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
1087 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
1088 while (timeout.tv_usec < 0) {
1089 timeout.tv_usec += 1000000;
1092 while (timeout.tv_usec > 1000000) {
1093 timeout.tv_usec -= 1000000;
1096 if (timeout.tv_sec < 0)
1097 timeout.tv_sec = timeout.tv_usec = 0;
1099 n = select(s + 1, &rfds, NULL, NULL, &timeout);
1101 continue; /* EINTR */
1103 m.msg_name = (caddr_t)&from;
1104 m.msg_namelen = sizeof(from);
1105 memset(&iov, 0, sizeof(iov));
1106 iov[0].iov_base = (caddr_t)packet;
1107 iov[0].iov_len = packlen;
1110 memset(cm, 0, CONTROLLEN);
1111 m.msg_control = (void *)cm;
1112 m.msg_controllen = CONTROLLEN;
1114 cc = recvmsg(s, &m, 0);
1116 if (errno != EINTR) {
1121 } else if (cc == 0) {
1125 * receive control messages only. Process the
1126 * exceptions (currently the only possibility is
1127 * a path MTU notification.)
1129 if ((mtu = get_pathmtu(&m)) > 0) {
1130 if ((options & F_VERBOSE) != 0) {
1131 printf("new path MTU (%d) is "
1138 * an ICMPv6 message (probably an echoreply)
1141 pr_pack(packet, cc, &m);
1143 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1144 (npackets > 0 && nreceived >= npackets))
1147 if (n == 0 || (options & F_FLOOD)) {
1148 if (npackets == 0 || ntransmitted < npackets)
1155 * If we're not transmitting any more packets,
1156 * change the timer to wait two round-trip times
1157 * if we've received any packets or (waittime)
1158 * milliseconds if we haven't.
1162 intvl.tv_sec = 2 * tmax / 1000;
1163 if (intvl.tv_sec == 0)
1166 intvl.tv_sec = waittime / 1000;
1167 intvl.tv_usec = waittime % 1000 * 1000;
1170 gettimeofday(&last, NULL);
1171 if (ntransmitted - nreceived - 1 > nmissedmax) {
1172 nmissedmax = ntransmitted - nreceived - 1;
1173 if (options & F_MISSED)
1174 (void)write(STDOUT_FILENO, &BBELL, 1);
1178 sigemptyset(&si_sa.sa_mask);
1180 si_sa.sa_handler = SIG_IGN;
1181 sigaction(SIGINT, &si_sa, 0);
1182 sigaction(SIGALRM, &si_sa, 0);
1188 exit(nreceived == 0 ? 2 : 0);
1210 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1211 * will be added on by the kernel. The ID field is our UNIX process ID,
1212 * and the sequence number is an ascending integer. The first 8 bytes
1213 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1214 * byte-order, to compute the round-trip time.
1221 if (options & F_FQDN)
1222 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1223 else if (options & F_FQDNOLD)
1225 else if (options & F_NODEADDR)
1226 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1227 else if (options & F_SUPTYPES)
1230 l = ICMP6ECHOLEN + datalen;
1238 struct icmp6_hdr *icp;
1239 struct iovec iov[2];
1241 struct icmp6_nodeinfo *nip;
1244 if (npackets && ntransmitted >= npackets)
1245 return(-1); /* no more transmission */
1247 icp = (struct icmp6_hdr *)outpack;
1248 nip = (struct icmp6_nodeinfo *)outpack;
1249 memset(icp, 0, sizeof(*icp));
1250 icp->icmp6_cksum = 0;
1251 seq = ntransmitted++;
1252 CLR(seq % mx_dup_ck);
1254 if (options & F_FQDN) {
1255 icp->icmp6_type = ICMP6_NI_QUERY;
1256 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1257 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1258 nip->ni_flags = htons(0);
1260 memcpy(nip->icmp6_ni_nonce, nonce,
1261 sizeof(nip->icmp6_ni_nonce));
1262 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1264 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1265 sizeof(dst.sin6_addr));
1266 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1268 } else if (options & F_FQDNOLD) {
1269 /* packet format in 03 draft - no Subject data on queries */
1270 icp->icmp6_type = ICMP6_NI_QUERY;
1271 icp->icmp6_code = 0; /* code field is always 0 */
1272 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1273 nip->ni_flags = htons(0);
1275 memcpy(nip->icmp6_ni_nonce, nonce,
1276 sizeof(nip->icmp6_ni_nonce));
1277 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1281 } else if (options & F_NODEADDR) {
1282 icp->icmp6_type = ICMP6_NI_QUERY;
1283 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1284 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1285 nip->ni_flags = naflags;
1287 memcpy(nip->icmp6_ni_nonce, nonce,
1288 sizeof(nip->icmp6_ni_nonce));
1289 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1291 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1292 sizeof(dst.sin6_addr));
1293 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1295 } else if (options & F_SUPTYPES) {
1296 icp->icmp6_type = ICMP6_NI_QUERY;
1297 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1298 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1299 /* we support compressed bitmap */
1300 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1302 memcpy(nip->icmp6_ni_nonce, nonce,
1303 sizeof(nip->icmp6_ni_nonce));
1304 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1308 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1309 icp->icmp6_code = 0;
1310 icp->icmp6_id = htons(ident);
1311 icp->icmp6_seq = ntohs(seq);
1315 (void)gettimeofday(&tv, NULL);
1316 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1317 tv32->tv32_sec = htonl(tv.tv_sec);
1318 tv32->tv32_usec = htonl(tv.tv_usec);
1320 cc = ICMP6ECHOLEN + datalen;
1324 if (pingerlen() != cc)
1325 errx(1, "internal error; length mismatch");
1328 smsghdr.msg_name = (caddr_t)&dst;
1329 smsghdr.msg_namelen = sizeof(dst);
1330 memset(&iov, 0, sizeof(iov));
1331 iov[0].iov_base = (caddr_t)outpack;
1332 iov[0].iov_len = cc;
1333 smsghdr.msg_iov = iov;
1334 smsghdr.msg_iovlen = 1;
1336 i = sendmsg(s, &smsghdr, 0);
1338 if (i < 0 || i != cc) {
1341 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1344 if (!(options & F_QUIET) && options & F_FLOOD)
1345 (void)write(STDOUT_FILENO, &DOT, 1);
1351 myechoreply(const struct icmp6_hdr *icp)
1353 if (ntohs(icp->icmp6_id) == ident)
1360 mynireply(const struct icmp6_nodeinfo *nip)
1362 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1363 nonce + sizeof(u_int16_t),
1364 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1371 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1373 /*base for compressed name*/
1377 char cresult[MAXDNAME + 1];
1388 if (i == 0 || cp != *sp) {
1389 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1390 return NULL; /*result overrun*/
1396 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1397 /* DNS compression */
1401 comp = base + (i & 0x3f);
1402 if (dnsdecode(&comp, cp, base, cresult,
1403 sizeof(cresult)) == NULL)
1405 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1406 return NULL; /*result overrun*/
1408 } else if ((i & 0x3f) == i) {
1410 return NULL; /*source overrun*/
1411 while (i-- > 0 && cp < ep) {
1412 l = snprintf(cresult, sizeof(cresult),
1413 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1414 if ((size_t)l >= sizeof(cresult) || l < 0)
1416 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1417 return NULL; /*result overrun*/
1421 return NULL; /*invalid label*/
1424 return NULL; /*not terminated*/
1432 * Print out the packet, if it came from us. This logic is necessary
1433 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1434 * which arrive ('tis only fair). This permits multiple copies of this
1435 * program to be run without having intermingled output (or statistics!).
1438 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1440 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1441 struct icmp6_hdr *icp;
1442 struct icmp6_nodeinfo *ni;
1445 struct sockaddr *from;
1447 u_char *cp = NULL, *dp, *end = buf + cc;
1448 struct in6_pktinfo *pktinfo = NULL;
1449 struct timeval tv, tp;
1451 double triptime = 0;
1456 char dnsname[MAXDNAME + 1];
1458 (void)gettimeofday(&tv, NULL);
1460 if (!mhdr || !mhdr->msg_name ||
1461 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1462 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1463 if (options & F_VERBOSE)
1464 warnx("invalid peername");
1467 from = (struct sockaddr *)mhdr->msg_name;
1468 fromlen = mhdr->msg_namelen;
1469 if (cc < (int)sizeof(struct icmp6_hdr)) {
1470 if (options & F_VERBOSE)
1471 warnx("packet too short (%d bytes) from %s", cc,
1472 pr_addr(from, fromlen));
1475 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1476 (options & F_VERBOSE) != 0)
1477 warnx("some control data discarded, insufficient buffer size");
1478 icp = (struct icmp6_hdr *)buf;
1479 ni = (struct icmp6_nodeinfo *)buf;
1482 if ((hoplim = get_hoplim(mhdr)) == -1) {
1483 warnx("failed to get receiving hop limit");
1486 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1487 warnx("failed to get receiving packet information");
1491 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1492 seq = ntohs(icp->icmp6_seq);
1495 tpp = (struct tv32 *)(icp + 1);
1496 tp.tv_sec = ntohl(tpp->tv32_sec);
1497 tp.tv_usec = ntohl(tpp->tv32_usec);
1499 triptime = ((double)tv.tv_sec) * 1000.0 +
1500 ((double)tv.tv_usec) / 1000.0;
1502 tsumsq += triptime * triptime;
1503 if (triptime < tmin)
1505 if (triptime > tmax)
1509 if (TST(seq % mx_dup_ck)) {
1514 SET(seq % mx_dup_ck);
1518 if (options & F_QUIET)
1521 if (options & F_WAITTIME && triptime > waittime) {
1526 if (options & F_FLOOD)
1527 (void)write(STDOUT_FILENO, &BSPACE, 1);
1529 if (options & F_AUDIBLE)
1530 (void)write(STDOUT_FILENO, &BBELL, 1);
1531 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1532 pr_addr(from, fromlen), seq);
1533 (void)printf(" hlim=%d", hoplim);
1534 if ((options & F_VERBOSE) != 0) {
1535 struct sockaddr_in6 dstsa;
1537 memset(&dstsa, 0, sizeof(dstsa));
1538 dstsa.sin6_family = AF_INET6;
1539 dstsa.sin6_len = sizeof(dstsa);
1540 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1541 dstsa.sin6_addr = pktinfo->ipi6_addr;
1542 (void)printf(" dst=%s",
1543 pr_addr((struct sockaddr *)&dstsa,
1547 (void)printf(" time=%.3f ms", triptime);
1549 (void)printf("(DUP!)");
1550 /* check the data */
1551 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1552 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1553 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1555 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1560 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1561 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1563 if (TST(seq % mx_dup_ck)) {
1568 SET(seq % mx_dup_ck);
1572 if (options & F_QUIET)
1575 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1577 switch (ntohs(ni->ni_code)) {
1578 case ICMP6_NI_SUCCESS:
1580 case ICMP6_NI_REFUSED:
1581 printf("refused, type 0x%x", ntohs(ni->ni_type));
1583 case ICMP6_NI_UNKNOWN:
1584 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1587 printf("unknown code 0x%x, type 0x%x",
1588 ntohs(ni->ni_code), ntohs(ni->ni_type));
1592 switch (ntohs(ni->ni_qtype)) {
1594 printf("NodeInfo NOOP");
1596 case NI_QTYPE_SUPTYPES:
1597 pr_suptypes(ni, end - (u_char *)ni);
1599 case NI_QTYPE_NODEADDR:
1600 pr_nodeaddr(ni, end - (u_char *)ni);
1603 default: /* XXX: for backward compatibility */
1604 cp = (u_char *)ni + ICMP6_NIRLEN;
1605 if (buf[off + ICMP6_NIRLEN] ==
1606 cc - off - ICMP6_NIRLEN - 1)
1611 cp++; /* skip length */
1613 safeputc(*cp & 0xff);
1619 if (dnsdecode((const u_char **)&cp, end,
1620 (const u_char *)(ni + 1), dnsname,
1621 sizeof(dnsname)) == NULL) {
1626 * name-lookup special handling for
1629 if (cp + 1 <= end && !*cp &&
1630 strlen(dnsname) > 0) {
1631 dnsname[strlen(dnsname) - 1] = '\0';
1634 printf("%s%s", i > 0 ? "," : "",
1638 if (options & F_VERBOSE) {
1642 (void)printf(" ("); /*)*/
1644 switch (ni->ni_code) {
1645 case ICMP6_NI_REFUSED:
1646 (void)printf("refused");
1649 case ICMP6_NI_UNKNOWN:
1650 (void)printf("unknown qtype");
1655 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1656 /* case of refusion, unknown */
1661 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1664 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1665 (void)printf("TTL=%d:meaningless",
1669 (void)printf("TTL=%d:invalid",
1672 (void)printf("TTL=%d", ttl);
1682 cp = (u_char *)ni + ICMP6_NIRLEN;
1691 if (buf[off + ICMP6_NIRLEN] !=
1692 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1695 (void)printf("invalid namelen:%d/%lu",
1696 buf[off + ICMP6_NIRLEN],
1697 (u_long)cc - off - ICMP6_NIRLEN - 1);
1707 /* We've got something other than an ECHOREPLY */
1708 if (!(options & F_VERBOSE))
1710 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1714 if (!(options & F_FLOOD)) {
1715 (void)putchar('\n');
1716 if (options & F_VERBOSE)
1718 (void)fflush(stdout);
1724 pr_exthdrs(struct msghdr *mhdr)
1731 bufp = mhdr->msg_control;
1732 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1733 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1734 if (cm->cmsg_level != IPPROTO_IPV6)
1737 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1740 switch (cm->cmsg_type) {
1742 printf(" HbH Options: ");
1743 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1746 #ifdef IPV6_RTHDRDSTOPTS
1747 case IPV6_RTHDRDSTOPTS:
1749 printf(" Dst Options: ");
1750 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1753 printf(" Routing: ");
1754 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1761 pr_ip6opt(void *extbuf, size_t bufsize)
1763 struct ip6_hbh *ext;
1766 socklen_t extlen, len;
1772 ext = (struct ip6_hbh *)extbuf;
1773 extlen = (ext->ip6h_len + 1) * 8;
1774 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1775 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1778 * Bounds checking on the ancillary data buffer:
1779 * subtract the size of a cmsg structure from the buffer size.
1781 if (bufsize < (extlen + CMSG_SPACE(0))) {
1782 extlen = bufsize - CMSG_SPACE(0);
1783 warnx("options truncated, showing only %u (total=%u)",
1784 (unsigned int)(extlen / 8 - 1),
1785 (unsigned int)(ext->ip6h_len));
1790 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1791 &type, &len, &databuf);
1792 if (currentlen == -1)
1796 * Note that inet6_opt_next automatically skips any padding
1801 offset = inet6_opt_get_val(databuf, offset,
1802 &value4, sizeof(value4));
1803 printf(" Jumbo Payload Opt: Length %u\n",
1804 (u_int32_t)ntohl(value4));
1806 case IP6OPT_ROUTER_ALERT:
1808 offset = inet6_opt_get_val(databuf, offset,
1809 &value2, sizeof(value2));
1810 printf(" Router Alert Opt: Type %u\n",
1814 printf(" Received Opt %u len %lu\n",
1815 type, (unsigned long)len);
1823 pr_rthdr(void *extbuf, size_t bufsize)
1825 struct in6_addr *in6;
1826 char ntopbuf[INET6_ADDRSTRLEN];
1827 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1828 int i, segments, origsegs, rthsize, size0, size1;
1830 /* print fixed part of the header */
1831 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1832 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1833 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1834 printf("%d segments, ", segments);
1835 printf("%d left\n", rh->ip6r_segleft);
1837 printf("segments unknown, ");
1838 printf("%d left\n", rh->ip6r_segleft);
1843 * Bounds checking on the ancillary data buffer. When calculating
1844 * the number of items to show keep in mind:
1845 * - The size of the cmsg structure
1846 * - The size of one segment (the size of a Type 0 routing header)
1847 * - When dividing add a fudge factor of one in case the
1848 * dividend is not evenly divisible by the divisor
1850 rthsize = (rh->ip6r_len + 1) * 8;
1851 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1852 origsegs = segments;
1853 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1854 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1855 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1856 (size1 - size0) + 1;
1857 warnx("segments truncated, showing only %d (total=%d)",
1858 segments, origsegs);
1861 for (i = 0; i < segments; i++) {
1862 in6 = inet6_rth_getaddr(extbuf, i);
1864 printf(" [%d]<NULL>\n", i);
1866 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1868 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1869 printf(" [%d]%s\n", i, ntopbuf);
1878 pr_bitrange(u_int32_t v, int soff, int ii)
1885 /* shift till we have 0x01 */
1886 if ((v & 0x01) == 0) {
1888 printf("-%u", soff + off - 1);
1899 case 0x04: case 0x0c:
1910 /* we have 0x01 with us */
1911 for (i = 0; i < 32 - off; i++) {
1912 if ((v & (0x01 << i)) == 0)
1916 printf(" %u", soff + off);
1924 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1925 /* ni->qtype must be SUPTYPES */
1929 const u_char *cp, *end;
1932 u_int16_t words; /*32bit count*/
1935 #define MAXQTYPES (1 << 16)
1939 cp = (u_char *)(ni + 1);
1940 end = ((u_char *)ni) + nilen;
1944 printf("NodeInfo Supported Qtypes");
1945 if (options & F_VERBOSE) {
1946 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1947 printf(", compressed bitmap");
1949 printf(", raw bitmap");
1953 clen = (size_t)(end - cp);
1954 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1955 if (clen == 0 || clen > MAXQTYPES / 8 ||
1961 if (clen < sizeof(cbit) || clen % sizeof(v))
1963 memcpy(&cbit, cp, sizeof(cbit));
1964 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1968 clen = ntohs(cbit.words) * sizeof(v);
1969 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1974 for (off = 0; off < clen; off += sizeof(v)) {
1975 memcpy(&v, cp + off, sizeof(v));
1976 v = (u_int32_t)ntohl(v);
1977 b = pr_bitrange(v, (int)(cur + off * 8), b);
1979 /* flush the remaining bits */
1980 b = pr_bitrange(0, (int)(cur + off * 8), b);
1984 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1985 cur += ntohs(cbit.skip) * 32;
1990 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
1991 /* ni->qtype must be NODEADDR */
1993 u_char *cp = (u_char *)(ni + 1);
1994 char ntop_buf[INET6_ADDRSTRLEN];
1997 nilen -= sizeof(struct icmp6_nodeinfo);
1999 if (options & F_VERBOSE) {
2000 switch (ni->ni_code) {
2001 case ICMP6_NI_REFUSED:
2002 (void)printf("refused");
2004 case ICMP6_NI_UNKNOWN:
2005 (void)printf("unknown qtype");
2008 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2009 (void)printf(" truncated");
2013 printf(" no address\n");
2016 * In icmp-name-lookups 05 and later, TTL of each returned address
2017 * is contained in the resposne. We try to detect the version
2018 * by the length of the data, but note that the detection algorithm
2019 * is incomplete. We assume the latest draft by default.
2021 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2027 /* XXX: alignment? */
2028 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2029 cp += sizeof(u_int32_t);
2030 nilen -= sizeof(u_int32_t);
2033 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2035 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2036 printf(" %s", ntop_buf);
2038 if (ttl == 0xffffffff) {
2040 * XXX: can this convention be applied to all
2041 * type of TTL (i.e. non-ND TTL)?
2043 printf("(TTL=infty)");
2046 printf("(TTL=%u)", ttl);
2050 nilen -= sizeof(struct in6_addr);
2051 cp += sizeof(struct in6_addr);
2056 get_hoplim(struct msghdr *mhdr)
2060 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2061 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2062 if (cm->cmsg_len == 0)
2065 if (cm->cmsg_level == IPPROTO_IPV6 &&
2066 cm->cmsg_type == IPV6_HOPLIMIT &&
2067 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2068 return(*(int *)CMSG_DATA(cm));
2074 static struct in6_pktinfo *
2075 get_rcvpktinfo(struct msghdr *mhdr)
2079 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2080 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2081 if (cm->cmsg_len == 0)
2084 if (cm->cmsg_level == IPPROTO_IPV6 &&
2085 cm->cmsg_type == IPV6_PKTINFO &&
2086 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2087 return((struct in6_pktinfo *)CMSG_DATA(cm));
2094 get_pathmtu(struct msghdr *mhdr)
2096 #ifdef IPV6_RECVPATHMTU
2098 struct ip6_mtuinfo *mtuctl = NULL;
2100 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2101 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2102 if (cm->cmsg_len == 0)
2105 if (cm->cmsg_level == IPPROTO_IPV6 &&
2106 cm->cmsg_type == IPV6_PATHMTU &&
2107 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2108 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2111 * If the notified destination is different from
2112 * the one we are pinging, just ignore the info.
2113 * We check the scope ID only when both notified value
2114 * and our own value have non-0 values, because we may
2115 * have used the default scope zone ID for sending,
2116 * in which case the scope ID value is 0.
2118 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2120 (mtuctl->ip6m_addr.sin6_scope_id &&
2121 dst.sin6_scope_id &&
2122 mtuctl->ip6m_addr.sin6_scope_id !=
2123 dst.sin6_scope_id)) {
2124 if ((options & F_VERBOSE) != 0) {
2125 printf("path MTU for %s is notified. "
2127 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2128 sizeof(mtuctl->ip6m_addr)));
2134 * Ignore an invalid MTU. XXX: can we just believe
2137 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2140 /* notification for our destination. return the MTU. */
2141 return((int)mtuctl->ip6m_mtu);
2150 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2154 tvsub(struct timeval *out, struct timeval *in)
2156 if ((out->tv_usec -= in->tv_usec) < 0) {
2158 out->tv_usec += 1000000;
2160 out->tv_sec -= in->tv_sec;
2169 onint(int notused __unused)
2172 * When doing reverse DNS lookups, the seenint flag might not
2173 * be noticed for a while. Just exit if we get a second SIGINT.
2175 if ((options & F_HOSTNAME) && seenint != 0)
2176 _exit(nreceived ? 0 : 2);
2181 * Print out statistics.
2187 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2188 (void)printf("%ld packets transmitted, ", ntransmitted);
2189 (void)printf("%ld packets received, ", nreceived);
2191 (void)printf("+%ld duplicates, ", nrepeats);
2193 if (nreceived > ntransmitted)
2194 (void)printf("-- somebody's duplicating packets!");
2196 (void)printf("%.1f%% packet loss",
2197 ((((double)ntransmitted - nreceived) * 100.0) /
2201 printf(", %ld packets out of wait time", nrcvtimeout);
2202 (void)putchar('\n');
2203 if (nreceived && timing) {
2204 /* Only display average to microseconds */
2205 double num = nreceived + nrepeats;
2206 double avg = tsum / num;
2207 double dev = sqrt(tsumsq / num - avg * avg);
2209 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2210 tmin, avg, tmax, dev);
2211 (void)fflush(stdout);
2213 (void)fflush(stdout);
2217 static const char *niqcode[] = {
2219 "DNS label", /*or empty*/
2224 static const char *nircode[] = {
2225 "Success", "Refused", "Unknown",
2231 * Print a descriptive string about an ICMP header.
2234 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2236 char ntop_buf[INET6_ADDRSTRLEN];
2237 struct nd_redirect *red;
2238 struct icmp6_nodeinfo *ni;
2239 char dnsname[MAXDNAME + 1];
2243 switch (icp->icmp6_type) {
2244 case ICMP6_DST_UNREACH:
2245 switch (icp->icmp6_code) {
2246 case ICMP6_DST_UNREACH_NOROUTE:
2247 (void)printf("No Route to Destination\n");
2249 case ICMP6_DST_UNREACH_ADMIN:
2250 (void)printf("Destination Administratively "
2253 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2254 (void)printf("Destination Unreachable Beyond Scope\n");
2256 case ICMP6_DST_UNREACH_ADDR:
2257 (void)printf("Destination Host Unreachable\n");
2259 case ICMP6_DST_UNREACH_NOPORT:
2260 (void)printf("Destination Port Unreachable\n");
2263 (void)printf("Destination Unreachable, Bad Code: %d\n",
2267 /* Print returned IP header information */
2268 pr_retip((struct ip6_hdr *)(icp + 1), end);
2270 case ICMP6_PACKET_TOO_BIG:
2271 (void)printf("Packet too big mtu = %d\n",
2272 (int)ntohl(icp->icmp6_mtu));
2273 pr_retip((struct ip6_hdr *)(icp + 1), end);
2275 case ICMP6_TIME_EXCEEDED:
2276 switch (icp->icmp6_code) {
2277 case ICMP6_TIME_EXCEED_TRANSIT:
2278 (void)printf("Time to live exceeded\n");
2280 case ICMP6_TIME_EXCEED_REASSEMBLY:
2281 (void)printf("Frag reassembly time exceeded\n");
2284 (void)printf("Time exceeded, Bad Code: %d\n",
2288 pr_retip((struct ip6_hdr *)(icp + 1), end);
2290 case ICMP6_PARAM_PROB:
2291 (void)printf("Parameter problem: ");
2292 switch (icp->icmp6_code) {
2293 case ICMP6_PARAMPROB_HEADER:
2294 (void)printf("Erroneous Header ");
2296 case ICMP6_PARAMPROB_NEXTHEADER:
2297 (void)printf("Unknown Nextheader ");
2299 case ICMP6_PARAMPROB_OPTION:
2300 (void)printf("Unrecognized Option ");
2303 (void)printf("Bad code(%d) ", icp->icmp6_code);
2306 (void)printf("pointer = 0x%02x\n",
2307 (u_int32_t)ntohl(icp->icmp6_pptr));
2308 pr_retip((struct ip6_hdr *)(icp + 1), end);
2310 case ICMP6_ECHO_REQUEST:
2311 (void)printf("Echo Request");
2312 /* XXX ID + Seq + Data */
2314 case ICMP6_ECHO_REPLY:
2315 (void)printf("Echo Reply");
2316 /* XXX ID + Seq + Data */
2318 case ICMP6_MEMBERSHIP_QUERY:
2319 (void)printf("Listener Query");
2321 case ICMP6_MEMBERSHIP_REPORT:
2322 (void)printf("Listener Report");
2324 case ICMP6_MEMBERSHIP_REDUCTION:
2325 (void)printf("Listener Done");
2327 case ND_ROUTER_SOLICIT:
2328 (void)printf("Router Solicitation");
2330 case ND_ROUTER_ADVERT:
2331 (void)printf("Router Advertisement");
2333 case ND_NEIGHBOR_SOLICIT:
2334 (void)printf("Neighbor Solicitation");
2336 case ND_NEIGHBOR_ADVERT:
2337 (void)printf("Neighbor Advertisement");
2340 red = (struct nd_redirect *)icp;
2341 (void)printf("Redirect\n");
2342 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2344 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2345 (void)printf("Destination: %s", ntop_buf);
2346 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2348 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2349 (void)printf(" New Target: %s", ntop_buf);
2351 case ICMP6_NI_QUERY:
2352 (void)printf("Node Information Query");
2353 /* XXX ID + Seq + Data */
2354 ni = (struct icmp6_nodeinfo *)icp;
2355 l = end - (u_char *)(ni + 1);
2357 switch (ntohs(ni->ni_qtype)) {
2359 (void)printf("NOOP");
2361 case NI_QTYPE_SUPTYPES:
2362 (void)printf("Supported qtypes");
2365 (void)printf("DNS name");
2367 case NI_QTYPE_NODEADDR:
2368 (void)printf("nodeaddr");
2370 case NI_QTYPE_IPV4ADDR:
2371 (void)printf("IPv4 nodeaddr");
2374 (void)printf("unknown qtype");
2377 if (options & F_VERBOSE) {
2378 switch (ni->ni_code) {
2379 case ICMP6_NI_SUBJ_IPV6:
2380 if (l == sizeof(struct in6_addr) &&
2381 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2382 sizeof(ntop_buf)) != NULL) {
2383 (void)printf(", subject=%s(%s)",
2384 niqcode[ni->ni_code], ntop_buf);
2387 /* backward compat to -W */
2388 (void)printf(", oldfqdn");
2390 (void)printf(", invalid");
2394 case ICMP6_NI_SUBJ_FQDN:
2395 if (end == (u_char *)(ni + 1)) {
2396 (void)printf(", no subject");
2399 printf(", subject=%s", niqcode[ni->ni_code]);
2400 cp = (const u_char *)(ni + 1);
2401 if (dnsdecode(&cp, end, NULL, dnsname,
2402 sizeof(dnsname)) != NULL)
2403 printf("(%s)", dnsname);
2405 printf("(invalid)");
2407 case ICMP6_NI_SUBJ_IPV4:
2408 if (l == sizeof(struct in_addr) &&
2409 inet_ntop(AF_INET, ni + 1, ntop_buf,
2410 sizeof(ntop_buf)) != NULL) {
2411 (void)printf(", subject=%s(%s)",
2412 niqcode[ni->ni_code], ntop_buf);
2414 (void)printf(", invalid");
2417 (void)printf(", invalid");
2422 case ICMP6_NI_REPLY:
2423 (void)printf("Node Information Reply");
2424 /* XXX ID + Seq + Data */
2425 ni = (struct icmp6_nodeinfo *)icp;
2427 switch (ntohs(ni->ni_qtype)) {
2429 (void)printf("NOOP");
2431 case NI_QTYPE_SUPTYPES:
2432 (void)printf("Supported qtypes");
2435 (void)printf("DNS name");
2437 case NI_QTYPE_NODEADDR:
2438 (void)printf("nodeaddr");
2440 case NI_QTYPE_IPV4ADDR:
2441 (void)printf("IPv4 nodeaddr");
2444 (void)printf("unknown qtype");
2447 if (options & F_VERBOSE) {
2448 if (ni->ni_code > nitems(nircode))
2449 printf(", invalid");
2451 printf(", %s", nircode[ni->ni_code]);
2455 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2461 * Print an IP6 header.
2464 pr_iph(struct ip6_hdr *ip6)
2466 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2468 char ntop_buf[INET6_ADDRSTRLEN];
2470 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2471 tc = (tc >> 4) & 0x0f;
2472 tc |= (ip6->ip6_vfc << 4);
2474 printf("Vr TC Flow Plen Nxt Hlim\n");
2475 printf(" %1x %02x %05x %04x %02x %02x\n",
2476 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2477 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2478 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2479 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2480 printf("%s->", ntop_buf);
2481 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2482 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2483 printf("%s\n", ntop_buf);
2488 * Return an ascii host address as a dotted quad and optionally with
2492 pr_addr(struct sockaddr *addr, int addrlen)
2494 static char buf[NI_MAXHOST];
2497 if ((options & F_HOSTNAME) == 0)
2498 flag |= NI_NUMERICHOST;
2500 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2508 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2511 pr_retip(struct ip6_hdr *ip6, u_char *end)
2513 u_char *cp = (u_char *)ip6, nh;
2516 if ((size_t)(end - (u_char *)ip6) < sizeof(*ip6)) {
2521 hlen = sizeof(*ip6);
2525 while (end - cp >= 8) {
2527 case IPPROTO_HOPOPTS:
2529 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2530 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2532 case IPPROTO_DSTOPTS:
2534 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2535 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2537 case IPPROTO_FRAGMENT:
2539 hlen = sizeof(struct ip6_frag);
2540 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2542 case IPPROTO_ROUTING:
2544 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2545 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2550 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2551 nh = ((struct ah *)cp)->ah_nxt;
2554 case IPPROTO_ICMPV6:
2555 printf("ICMP6: type = %d, code = %d\n",
2562 printf("TCP: from port %u, to port %u (decimal)\n",
2563 (*cp * 256 + *(cp + 1)),
2564 (*(cp + 2) * 256 + *(cp + 3)));
2567 printf("UDP: from port %u, to port %u (decimal)\n",
2568 (*cp * 256 + *(cp + 1)),
2569 (*(cp + 2) * 256 + *(cp + 3)));
2572 printf("Unknown Header(%d)\n", nh);
2576 if ((cp += hlen) >= end)
2591 fill(char *bp, char *patp)
2597 for (cp = patp; *cp; cp++)
2599 errx(1, "patterns must be specified as hex digits");
2601 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2602 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2603 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2604 &pat[13], &pat[14], &pat[15]);
2609 (size_t)kk <= MAXDATALEN - 8 + sizeof(struct tv32) + ii;
2611 for (jj = 0; jj < ii; ++jj)
2612 bp[jj + kk] = pat[jj];
2613 if (!(options & F_QUIET)) {
2614 (void)printf("PATTERN: 0x");
2615 for (jj = 0; jj < ii; ++jj)
2616 (void)printf("%02x", bp[jj] & 0xFF);
2622 #ifdef IPSEC_POLICY_IPSEC
2624 setpolicy(int so __unused, char *policy)
2629 return 0; /* ignore */
2631 buf = ipsec_set_policy(policy, strlen(policy));
2633 errx(1, "%s", ipsec_strerror());
2634 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2635 ipsec_get_policylen(buf)) < 0)
2636 warnx("Unable to set IPsec policy");
2645 nigroup(char *name, int nig_oldmcprefix)
2650 u_int8_t digest[16];
2653 char hbuf[NI_MAXHOST];
2654 struct in6_addr in6;
2657 p = strchr(name, '.');
2659 p = name + strlen(name);
2661 if (l > 63 || l > sizeof(hbuf) - 1)
2662 return NULL; /*label too long*/
2663 strncpy(hbuf, name, l);
2664 hbuf[(int)l] = '\0';
2666 for (q = name; *q; q++) {
2667 if (isupper(*(unsigned char *)q))
2668 *q = tolower(*(unsigned char *)q);
2671 /* generate 16 bytes of pseudo-random value. */
2672 memset(&ctxt, 0, sizeof(ctxt));
2675 MD5Update(&ctxt, &c, sizeof(c));
2676 MD5Update(&ctxt, (unsigned char *)name, l);
2677 MD5Final(digest, &ctxt);
2679 if (nig_oldmcprefix) {
2680 /* draft-ietf-ipngwg-icmp-name-lookup */
2681 valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
2684 valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
2687 return NULL; /*XXX*/
2689 if (nig_oldmcprefix) {
2690 /* draft-ietf-ipngwg-icmp-name-lookup */
2691 bcopy(digest, &in6.s6_addr[12], 4);
2694 bcopy(digest, &in6.s6_addr[13], 3);
2697 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2700 return strdup(hbuf);
2706 (void)fprintf(stderr,
2707 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2712 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2716 #ifdef IPV6_USE_MIN_MTU
2720 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2721 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2722 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2726 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2728 " [-X timeout] [hops ...] host\n");