1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 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. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 static const char copyright[] =
68 "@(#) Copyright (c) 1989, 1993\n\
69 The Regents of the University of California. All rights reserved.\n";
74 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
78 #include <sys/cdefs.h>
79 __FBSDID("$FreeBSD$");
82 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
83 * measure round-trip-delays and packet loss across network paths.
87 * U. S. Army Ballistic Research Laboratory
91 * Public Domain. Distribution Unlimited.
93 * More statistics could always be gathered.
94 * This program has to run SUID to ROOT to access the ICMP socket.
98 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
99 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
100 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
101 * network attached to 1 or more interfaces)
104 #include <sys/param.h>
106 #include <sys/socket.h>
107 #include <sys/time.h>
110 #include <net/route.h>
112 #include <netinet/in.h>
113 #include <netinet/ip6.h>
114 #include <netinet/icmp6.h>
115 #include <arpa/inet.h>
116 #include <arpa/nameser.h>
128 #include <sysexits.h>
132 #include <netipsec/ah.h>
133 #include <netipsec/ipsec.h>
143 #define MAXPACKETLEN 131072
145 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
146 #define ICMP6ECHOTMLEN sizeof(struct tv32)
147 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
148 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
149 /* FQDN case, 64 bits of nonce + 32 bits ttl */
150 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
151 #define EXTRA 256 /* for AH and various other headers. weird. */
152 #define DEFDATALEN ICMP6ECHOTMLEN
153 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
154 #define NROUTES 9 /* number of record route slots */
155 #define MAXWAIT 10000 /* max ms to wait for response */
156 #define MAXALARM (60 * 60) /* max seconds for alarm timeout */
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 #define F_HOSTNAME 0x10000
184 #define F_FQDNOLD 0x20000
185 #define F_NIGROUP 0x40000
186 #define F_SUPTYPES 0x80000
187 #define F_NOMINMTU 0x100000
188 #define F_ONCE 0x200000
189 #define F_AUDIBLE 0x400000
190 #define F_MISSED 0x800000
191 #define F_DONTFRAG 0x1000000
192 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
193 #define F_WAITTIME 0x2000000
196 #define IN6LEN sizeof(struct in6_addr)
197 #define SA6LEN sizeof(struct sockaddr_in6)
198 #define DUMMY_PORT 10101
200 #define SIN6(s) ((struct sockaddr_in6 *)(s))
203 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
204 * number of received sequence numbers we can keep track of. Change 128
205 * to 8192 for complete accuracy...
207 #define MAX_DUP_CHK (8 * 8192)
208 static int mx_dup_ck = MAX_DUP_CHK;
209 static char rcvd_tbl[MAX_DUP_CHK / 8];
211 static struct sockaddr_in6 dst; /* who to ping6 */
212 static struct sockaddr_in6 src; /* src addr of this packet */
213 static socklen_t srclen;
214 static size_t datalen = DEFDATALEN;
215 static int s; /* socket file descriptor */
216 static u_char outpack[MAXPACKETLEN];
217 static char BSPACE = '\b'; /* characters written for flood */
218 static char BBELL = '\a'; /* characters written for AUDIBLE */
219 static char DOT = '.';
220 static char *hostname;
221 static int ident; /* process id to identify our packets */
222 static u_int8_t nonce[8]; /* nonce field for node information */
223 static int hoplimit = -1; /* hoplimit */
224 static u_char *packet = NULL;
227 static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */
228 static long npackets; /* max packets to transmit */
229 static long nreceived; /* # of packets we got back */
230 static long nrepeats; /* number of duplicates */
231 static long ntransmitted; /* sequence # for outbound packets = #sent */
232 static int interval = 1000; /* interval between packets in ms */
233 static int waittime = MAXWAIT; /* timeout for each packet */
234 static long nrcvtimeout = 0; /* # of packets we got back after waittime */
237 static int timing; /* flag to do timing */
238 static double tmin = 999999999.0; /* minimum round trip time */
239 static double tmax = 0.0; /* maximum round trip time */
240 static double tsum = 0.0; /* sum of all times, for doing average */
241 static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
243 /* for node addresses */
244 static u_short naflags;
246 /* for ancillary data(advanced API) */
247 static struct msghdr smsghdr;
248 static struct iovec smsgiov;
249 static char *scmsg = 0;
251 static volatile sig_atomic_t seenint;
253 static volatile sig_atomic_t seeninfo;
256 int main(int, char *[]);
257 static void fill(char *, char *);
258 static int get_hoplim(struct msghdr *);
259 static int get_pathmtu(struct msghdr *);
260 static struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
261 static void onsignal(int);
262 static void onint(int);
263 static size_t pingerlen(void);
264 static int pinger(void);
265 static const char *pr_addr(struct sockaddr *, int);
266 static void pr_icmph(struct icmp6_hdr *, u_char *);
267 static void pr_iph(struct ip6_hdr *);
268 static void pr_suptypes(struct icmp6_nodeinfo *, size_t);
269 static void pr_nodeaddr(struct icmp6_nodeinfo *, int);
270 static int myechoreply(const struct icmp6_hdr *);
271 static int mynireply(const struct icmp6_nodeinfo *);
272 static char *dnsdecode(const u_char **, const u_char *, const u_char *,
274 static void pr_pack(u_char *, int, struct msghdr *);
275 static void pr_exthdrs(struct msghdr *);
276 static void pr_ip6opt(void *, size_t);
277 static void pr_rthdr(void *, size_t);
278 static int pr_bitrange(u_int32_t, int, int);
279 static void pr_retip(struct ip6_hdr *, u_char *);
280 static void summary(void);
281 static void tvsub(struct timeval *, struct timeval *);
282 static int setpolicy(int, char *);
283 static char *nigroup(char *, int);
284 static void usage(void);
287 main(int argc, char *argv[])
289 struct timeval last, intvl;
290 struct sockaddr_in6 from, *sin6;
291 struct addrinfo hints, *res;
292 struct sigaction si_sa;
294 int almost_done, ch, hold, packlen, preload, optval, error;
295 int nig_oldmcprefix = -1;
297 char *e, *target, *ifname = NULL, *gateway = NULL;
299 struct cmsghdr *scmsgp = NULL;
300 /* For control (ancillary) data received from recvmsg() */
301 struct cmsghdr cm[CONTROLLEN];
302 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
307 struct in6_pktinfo *pktinfo = NULL;
308 #ifdef USE_RFC2292BIS
309 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 */
903 #ifdef USE_RFC2292BIS
907 #ifdef USE_RFC2292BIS
908 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
909 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
910 scmsgp->cmsg_level = IPPROTO_IPV6;
911 scmsgp->cmsg_type = IPV6_RTHDR;
912 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
913 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
914 IPV6_RTHDR_TYPE_0, argc - 1);
916 errx(1, "can't initialize rthdr");
917 #else /* old advanced API */
918 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
919 IPV6_RTHDR_TYPE_0)) == NULL)
920 errx(1, "can't initialize rthdr");
921 #endif /* USE_RFC2292BIS */
923 for (hops = 0; hops < argc - 1; hops++) {
924 memset(&hints, 0, sizeof(hints));
925 hints.ai_family = AF_INET6;
927 if ((error = getaddrinfo(argv[hops], NULL, &hints,
929 errx(1, "%s", gai_strerror(error));
930 if (res->ai_addr->sa_family != AF_INET6)
932 "bad addr family of an intermediate addr");
933 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
934 #ifdef USE_RFC2292BIS
935 if (inet6_rth_add(rthdr, &sin6->sin6_addr))
936 errx(1, "can't add an intermediate node");
937 #else /* old advanced API */
938 if (inet6_rthdr_add(scmsg, &sin6->sin6_addr,
940 errx(1, "can't add an intermediate node");
941 #endif /* USE_RFC2292BIS */
945 #ifndef USE_RFC2292BIS
946 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
947 errx(1, "can't set the last flag");
950 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
953 if (!(options & F_SRCADDR)) {
955 * get the source address. XXX since we revoked the root
956 * privilege, we cannot use a raw socket for this.
959 socklen_t len = sizeof(src);
961 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
962 err(1, "UDP socket");
964 src.sin6_family = AF_INET6;
965 src.sin6_addr = dst.sin6_addr;
966 src.sin6_port = ntohs(DUMMY_PORT);
967 src.sin6_scope_id = dst.sin6_scope_id;
969 #ifdef USE_RFC2292BIS
971 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
972 (void *)pktinfo, sizeof(*pktinfo)))
973 err(1, "UDP setsockopt(IPV6_PKTINFO)");
975 if (hoplimit != -1 &&
976 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
977 (void *)&hoplimit, sizeof(hoplimit)))
978 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
980 if (hoplimit != -1 &&
981 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
982 (void *)&hoplimit, sizeof(hoplimit)))
983 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
986 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
987 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
988 err(1, "UDP setsockopt(IPV6_RTHDR)");
989 #else /* old advanced API */
990 if (smsghdr.msg_control &&
991 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
992 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
993 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
996 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
997 err(1, "UDP connect");
999 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
1000 err(1, "getsockname");
1005 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1007 if (datalen > (size_t)sockbufsize)
1008 warnx("you need -b to increase socket buffer size");
1009 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
1010 sizeof(sockbufsize)) < 0)
1011 err(1, "setsockopt(SO_SNDBUF)");
1012 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
1013 sizeof(sockbufsize)) < 0)
1014 err(1, "setsockopt(SO_RCVBUF)");
1017 if (datalen > 8 * 1024) /*XXX*/
1018 warnx("you need -b to increase socket buffer size");
1020 * When pinging the broadcast address, you can get a lot of
1021 * answers. Doing something so evil is useful if you are trying
1022 * to stress the ethernet, or just want to fill the arp cache
1023 * to get some stuff for /etc/ethers.
1026 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1032 #ifndef USE_SIN6_SCOPE_ID
1033 #ifdef IPV6_RECVPKTINFO
1034 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1035 sizeof(optval)) < 0)
1036 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1037 #else /* old adv. API */
1038 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1039 sizeof(optval)) < 0)
1040 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1042 #endif /* USE_SIN6_SCOPE_ID */
1043 #ifdef IPV6_RECVHOPLIMIT
1044 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1045 sizeof(optval)) < 0)
1046 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1047 #else /* old adv. API */
1048 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1049 sizeof(optval)) < 0)
1050 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1053 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1054 (unsigned long)(pingerlen() - 8));
1055 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1056 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1061 if (npackets != 0 && preload > npackets)
1066 gettimeofday(&last, NULL);
1068 sigemptyset(&si_sa.sa_mask);
1070 si_sa.sa_handler = onsignal;
1071 if (sigaction(SIGINT, &si_sa, 0) == -1)
1072 err(EX_OSERR, "sigaction SIGINT");
1075 if (sigaction(SIGINFO, &si_sa, 0) == -1)
1076 err(EX_OSERR, "sigaction SIGINFO");
1079 if (alarmtimeout > 0) {
1080 if (sigaction(SIGALRM, &si_sa, 0) == -1)
1081 err(EX_OSERR, "sigaction SIGALRM");
1083 if (options & F_FLOOD) {
1085 intvl.tv_usec = 10000;
1086 } else if ((options & F_INTERVAL) == 0) {
1087 intvl.tv_sec = interval / 1000;
1088 intvl.tv_usec = interval % 1000 * 1000;
1092 while (seenint == 0) {
1093 struct timeval now, timeout;
1095 struct iovec iov[2];
1099 /* signal handling */
1111 gettimeofday(&now, NULL);
1112 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
1113 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
1114 while (timeout.tv_usec < 0) {
1115 timeout.tv_usec += 1000000;
1118 while (timeout.tv_usec > 1000000) {
1119 timeout.tv_usec -= 1000000;
1122 if (timeout.tv_sec < 0)
1123 timeout.tv_sec = timeout.tv_usec = 0;
1125 n = select(s + 1, &rfds, NULL, NULL, &timeout);
1127 continue; /* EINTR */
1129 m.msg_name = (caddr_t)&from;
1130 m.msg_namelen = sizeof(from);
1131 memset(&iov, 0, sizeof(iov));
1132 iov[0].iov_base = (caddr_t)packet;
1133 iov[0].iov_len = packlen;
1136 memset(cm, 0, CONTROLLEN);
1137 m.msg_control = (void *)cm;
1138 m.msg_controllen = CONTROLLEN;
1140 cc = recvmsg(s, &m, 0);
1142 if (errno != EINTR) {
1147 } else if (cc == 0) {
1151 * receive control messages only. Process the
1152 * exceptions (currently the only possibility is
1153 * a path MTU notification.)
1155 if ((mtu = get_pathmtu(&m)) > 0) {
1156 if ((options & F_VERBOSE) != 0) {
1157 printf("new path MTU (%d) is "
1164 * an ICMPv6 message (probably an echoreply)
1167 pr_pack(packet, cc, &m);
1169 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1170 (npackets > 0 && nreceived >= npackets))
1173 if (n == 0 || (options & F_FLOOD)) {
1174 if (npackets == 0 || ntransmitted < npackets)
1181 * If we're not transmitting any more packets,
1182 * change the timer to wait two round-trip times
1183 * if we've received any packets or (waittime)
1184 * milliseconds if we haven't.
1188 intvl.tv_sec = 2 * tmax / 1000;
1189 if (intvl.tv_sec == 0)
1192 intvl.tv_sec = waittime / 1000;
1193 intvl.tv_usec = waittime % 1000 * 1000;
1196 gettimeofday(&last, NULL);
1197 if (ntransmitted - nreceived - 1 > nmissedmax) {
1198 nmissedmax = ntransmitted - nreceived - 1;
1199 if (options & F_MISSED)
1200 (void)write(STDOUT_FILENO, &BBELL, 1);
1204 sigemptyset(&si_sa.sa_mask);
1206 si_sa.sa_handler = SIG_IGN;
1207 sigaction(SIGINT, &si_sa, 0);
1208 sigaction(SIGALRM, &si_sa, 0);
1214 exit(nreceived == 0 ? 2 : 0);
1236 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1237 * will be added on by the kernel. The ID field is our UNIX process ID,
1238 * and the sequence number is an ascending integer. The first 8 bytes
1239 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1240 * byte-order, to compute the round-trip time.
1247 if (options & F_FQDN)
1248 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1249 else if (options & F_FQDNOLD)
1251 else if (options & F_NODEADDR)
1252 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1253 else if (options & F_SUPTYPES)
1256 l = ICMP6ECHOLEN + datalen;
1264 struct icmp6_hdr *icp;
1265 struct iovec iov[2];
1267 struct icmp6_nodeinfo *nip;
1270 if (npackets && ntransmitted >= npackets)
1271 return(-1); /* no more transmission */
1273 icp = (struct icmp6_hdr *)outpack;
1274 nip = (struct icmp6_nodeinfo *)outpack;
1275 memset(icp, 0, sizeof(*icp));
1276 icp->icmp6_cksum = 0;
1277 seq = ntransmitted++;
1278 CLR(seq % mx_dup_ck);
1280 if (options & F_FQDN) {
1281 icp->icmp6_type = ICMP6_NI_QUERY;
1282 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1283 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1284 nip->ni_flags = htons(0);
1286 memcpy(nip->icmp6_ni_nonce, nonce,
1287 sizeof(nip->icmp6_ni_nonce));
1288 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1290 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1291 sizeof(dst.sin6_addr));
1292 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1294 } else if (options & F_FQDNOLD) {
1295 /* packet format in 03 draft - no Subject data on queries */
1296 icp->icmp6_type = ICMP6_NI_QUERY;
1297 icp->icmp6_code = 0; /* code field is always 0 */
1298 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1299 nip->ni_flags = htons(0);
1301 memcpy(nip->icmp6_ni_nonce, nonce,
1302 sizeof(nip->icmp6_ni_nonce));
1303 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1307 } else if (options & F_NODEADDR) {
1308 icp->icmp6_type = ICMP6_NI_QUERY;
1309 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1310 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1311 nip->ni_flags = naflags;
1313 memcpy(nip->icmp6_ni_nonce, nonce,
1314 sizeof(nip->icmp6_ni_nonce));
1315 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1317 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1318 sizeof(dst.sin6_addr));
1319 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1321 } else if (options & F_SUPTYPES) {
1322 icp->icmp6_type = ICMP6_NI_QUERY;
1323 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1324 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1325 /* we support compressed bitmap */
1326 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1328 memcpy(nip->icmp6_ni_nonce, nonce,
1329 sizeof(nip->icmp6_ni_nonce));
1330 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1334 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1335 icp->icmp6_code = 0;
1336 icp->icmp6_id = htons(ident);
1337 icp->icmp6_seq = ntohs(seq);
1341 (void)gettimeofday(&tv, NULL);
1342 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1343 tv32->tv32_sec = htonl(tv.tv_sec);
1344 tv32->tv32_usec = htonl(tv.tv_usec);
1346 cc = ICMP6ECHOLEN + datalen;
1350 if (pingerlen() != cc)
1351 errx(1, "internal error; length mismatch");
1354 smsghdr.msg_name = (caddr_t)&dst;
1355 smsghdr.msg_namelen = sizeof(dst);
1356 memset(&iov, 0, sizeof(iov));
1357 iov[0].iov_base = (caddr_t)outpack;
1358 iov[0].iov_len = cc;
1359 smsghdr.msg_iov = iov;
1360 smsghdr.msg_iovlen = 1;
1362 i = sendmsg(s, &smsghdr, 0);
1364 if (i < 0 || i != cc) {
1367 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1370 if (!(options & F_QUIET) && options & F_FLOOD)
1371 (void)write(STDOUT_FILENO, &DOT, 1);
1377 myechoreply(const struct icmp6_hdr *icp)
1379 if (ntohs(icp->icmp6_id) == ident)
1386 mynireply(const struct icmp6_nodeinfo *nip)
1388 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1389 nonce + sizeof(u_int16_t),
1390 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1397 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1399 /*base for compressed name*/
1403 char cresult[MAXDNAME + 1];
1414 if (i == 0 || cp != *sp) {
1415 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1416 return NULL; /*result overrun*/
1422 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1423 /* DNS compression */
1427 comp = base + (i & 0x3f);
1428 if (dnsdecode(&comp, cp, base, cresult,
1429 sizeof(cresult)) == NULL)
1431 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1432 return NULL; /*result overrun*/
1434 } else if ((i & 0x3f) == i) {
1436 return NULL; /*source overrun*/
1437 while (i-- > 0 && cp < ep) {
1438 l = snprintf(cresult, sizeof(cresult),
1439 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1440 if ((size_t)l >= sizeof(cresult) || l < 0)
1442 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1443 return NULL; /*result overrun*/
1447 return NULL; /*invalid label*/
1450 return NULL; /*not terminated*/
1458 * Print out the packet, if it came from us. This logic is necessary
1459 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1460 * which arrive ('tis only fair). This permits multiple copies of this
1461 * program to be run without having intermingled output (or statistics!).
1464 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1466 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1467 struct icmp6_hdr *icp;
1468 struct icmp6_nodeinfo *ni;
1471 struct sockaddr *from;
1473 u_char *cp = NULL, *dp, *end = buf + cc;
1474 struct in6_pktinfo *pktinfo = NULL;
1475 struct timeval tv, tp;
1477 double triptime = 0;
1482 char dnsname[MAXDNAME + 1];
1484 (void)gettimeofday(&tv, NULL);
1486 if (!mhdr || !mhdr->msg_name ||
1487 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1488 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1489 if (options & F_VERBOSE)
1490 warnx("invalid peername");
1493 from = (struct sockaddr *)mhdr->msg_name;
1494 fromlen = mhdr->msg_namelen;
1495 if (cc < (int)sizeof(struct icmp6_hdr)) {
1496 if (options & F_VERBOSE)
1497 warnx("packet too short (%d bytes) from %s", cc,
1498 pr_addr(from, fromlen));
1501 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1502 (options & F_VERBOSE) != 0)
1503 warnx("some control data discarded, insufficient buffer size");
1504 icp = (struct icmp6_hdr *)buf;
1505 ni = (struct icmp6_nodeinfo *)buf;
1508 if ((hoplim = get_hoplim(mhdr)) == -1) {
1509 warnx("failed to get receiving hop limit");
1512 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1513 warnx("failed to get receiving packet information");
1517 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1518 seq = ntohs(icp->icmp6_seq);
1521 tpp = (struct tv32 *)(icp + 1);
1522 tp.tv_sec = ntohl(tpp->tv32_sec);
1523 tp.tv_usec = ntohl(tpp->tv32_usec);
1525 triptime = ((double)tv.tv_sec) * 1000.0 +
1526 ((double)tv.tv_usec) / 1000.0;
1528 tsumsq += triptime * triptime;
1529 if (triptime < tmin)
1531 if (triptime > tmax)
1535 if (TST(seq % mx_dup_ck)) {
1540 SET(seq % mx_dup_ck);
1544 if (options & F_QUIET)
1547 if (options & F_WAITTIME && triptime > waittime) {
1552 if (options & F_FLOOD)
1553 (void)write(STDOUT_FILENO, &BSPACE, 1);
1555 if (options & F_AUDIBLE)
1556 (void)write(STDOUT_FILENO, &BBELL, 1);
1557 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1558 pr_addr(from, fromlen), seq);
1559 (void)printf(" hlim=%d", hoplim);
1560 if ((options & F_VERBOSE) != 0) {
1561 struct sockaddr_in6 dstsa;
1563 memset(&dstsa, 0, sizeof(dstsa));
1564 dstsa.sin6_family = AF_INET6;
1565 dstsa.sin6_len = sizeof(dstsa);
1566 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1567 dstsa.sin6_addr = pktinfo->ipi6_addr;
1568 (void)printf(" dst=%s",
1569 pr_addr((struct sockaddr *)&dstsa,
1573 (void)printf(" time=%.3f ms", triptime);
1575 (void)printf("(DUP!)");
1576 /* check the data */
1577 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1578 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1579 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1581 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1586 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1587 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1589 if (TST(seq % mx_dup_ck)) {
1594 SET(seq % mx_dup_ck);
1598 if (options & F_QUIET)
1601 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1603 switch (ntohs(ni->ni_code)) {
1604 case ICMP6_NI_SUCCESS:
1606 case ICMP6_NI_REFUSED:
1607 printf("refused, type 0x%x", ntohs(ni->ni_type));
1609 case ICMP6_NI_UNKNOWN:
1610 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1613 printf("unknown code 0x%x, type 0x%x",
1614 ntohs(ni->ni_code), ntohs(ni->ni_type));
1618 switch (ntohs(ni->ni_qtype)) {
1620 printf("NodeInfo NOOP");
1622 case NI_QTYPE_SUPTYPES:
1623 pr_suptypes(ni, end - (u_char *)ni);
1625 case NI_QTYPE_NODEADDR:
1626 pr_nodeaddr(ni, end - (u_char *)ni);
1629 default: /* XXX: for backward compatibility */
1630 cp = (u_char *)ni + ICMP6_NIRLEN;
1631 if (buf[off + ICMP6_NIRLEN] ==
1632 cc - off - ICMP6_NIRLEN - 1)
1637 cp++; /* skip length */
1639 safeputc(*cp & 0xff);
1645 if (dnsdecode((const u_char **)&cp, end,
1646 (const u_char *)(ni + 1), dnsname,
1647 sizeof(dnsname)) == NULL) {
1652 * name-lookup special handling for
1655 if (cp + 1 <= end && !*cp &&
1656 strlen(dnsname) > 0) {
1657 dnsname[strlen(dnsname) - 1] = '\0';
1660 printf("%s%s", i > 0 ? "," : "",
1664 if (options & F_VERBOSE) {
1668 (void)printf(" ("); /*)*/
1670 switch (ni->ni_code) {
1671 case ICMP6_NI_REFUSED:
1672 (void)printf("refused");
1675 case ICMP6_NI_UNKNOWN:
1676 (void)printf("unknown qtype");
1681 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1682 /* case of refusion, unknown */
1687 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1690 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1691 (void)printf("TTL=%d:meaningless",
1695 (void)printf("TTL=%d:invalid",
1698 (void)printf("TTL=%d", ttl);
1708 cp = (u_char *)ni + ICMP6_NIRLEN;
1717 if (buf[off + ICMP6_NIRLEN] !=
1718 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1721 (void)printf("invalid namelen:%d/%lu",
1722 buf[off + ICMP6_NIRLEN],
1723 (u_long)cc - off - ICMP6_NIRLEN - 1);
1733 /* We've got something other than an ECHOREPLY */
1734 if (!(options & F_VERBOSE))
1736 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1740 if (!(options & F_FLOOD)) {
1741 (void)putchar('\n');
1742 if (options & F_VERBOSE)
1744 (void)fflush(stdout);
1750 pr_exthdrs(struct msghdr *mhdr)
1757 bufp = mhdr->msg_control;
1758 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1759 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1760 if (cm->cmsg_level != IPPROTO_IPV6)
1763 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1766 switch (cm->cmsg_type) {
1768 printf(" HbH Options: ");
1769 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1772 #ifdef IPV6_RTHDRDSTOPTS
1773 case IPV6_RTHDRDSTOPTS:
1775 printf(" Dst Options: ");
1776 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1779 printf(" Routing: ");
1780 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1786 #ifdef USE_RFC2292BIS
1788 pr_ip6opt(void *extbuf, size_t bufsize)
1790 struct ip6_hbh *ext;
1793 socklen_t extlen, len;
1799 ext = (struct ip6_hbh *)extbuf;
1800 extlen = (ext->ip6h_len + 1) * 8;
1801 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1802 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1805 * Bounds checking on the ancillary data buffer:
1806 * subtract the size of a cmsg structure from the buffer size.
1808 if (bufsize < (extlen + CMSG_SPACE(0))) {
1809 extlen = bufsize - CMSG_SPACE(0);
1810 warnx("options truncated, showing only %u (total=%u)",
1811 (unsigned int)(extlen / 8 - 1),
1812 (unsigned int)(ext->ip6h_len));
1817 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1818 &type, &len, &databuf);
1819 if (currentlen == -1)
1823 * Note that inet6_opt_next automatically skips any padding
1828 offset = inet6_opt_get_val(databuf, offset,
1829 &value4, sizeof(value4));
1830 printf(" Jumbo Payload Opt: Length %u\n",
1831 (u_int32_t)ntohl(value4));
1833 case IP6OPT_ROUTER_ALERT:
1835 offset = inet6_opt_get_val(databuf, offset,
1836 &value2, sizeof(value2));
1837 printf(" Router Alert Opt: Type %u\n",
1841 printf(" Received Opt %u len %lu\n",
1842 type, (unsigned long)len);
1848 #else /* !USE_RFC2292BIS */
1851 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1856 #endif /* USE_RFC2292BIS */
1858 #ifdef USE_RFC2292BIS
1860 pr_rthdr(void *extbuf, size_t bufsize)
1862 struct in6_addr *in6;
1863 char ntopbuf[INET6_ADDRSTRLEN];
1864 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1865 int i, segments, origsegs, rthsize, size0, size1;
1867 /* print fixed part of the header */
1868 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1869 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1870 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1871 printf("%d segments, ", segments);
1872 printf("%d left\n", rh->ip6r_segleft);
1874 printf("segments unknown, ");
1875 printf("%d left\n", rh->ip6r_segleft);
1880 * Bounds checking on the ancillary data buffer. When calculating
1881 * the number of items to show keep in mind:
1882 * - The size of the cmsg structure
1883 * - The size of one segment (the size of a Type 0 routing header)
1884 * - When dividing add a fudge factor of one in case the
1885 * dividend is not evenly divisible by the divisor
1887 rthsize = (rh->ip6r_len + 1) * 8;
1888 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1889 origsegs = segments;
1890 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1891 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1892 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1893 (size1 - size0) + 1;
1894 warnx("segments truncated, showing only %d (total=%d)",
1895 segments, origsegs);
1898 for (i = 0; i < segments; i++) {
1899 in6 = inet6_rth_getaddr(extbuf, i);
1901 printf(" [%d]<NULL>\n", i);
1903 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1905 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1906 printf(" [%d]%s\n", i, ntopbuf);
1914 #else /* !USE_RFC2292BIS */
1917 pr_rthdr(void *extbuf, size_t bufsize __unused)
1922 #endif /* USE_RFC2292BIS */
1925 pr_bitrange(u_int32_t v, int soff, int ii)
1932 /* shift till we have 0x01 */
1933 if ((v & 0x01) == 0) {
1935 printf("-%u", soff + off - 1);
1946 case 0x04: case 0x0c:
1957 /* we have 0x01 with us */
1958 for (i = 0; i < 32 - off; i++) {
1959 if ((v & (0x01 << i)) == 0)
1963 printf(" %u", soff + off);
1971 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1972 /* ni->qtype must be SUPTYPES */
1976 const u_char *cp, *end;
1979 u_int16_t words; /*32bit count*/
1982 #define MAXQTYPES (1 << 16)
1986 cp = (u_char *)(ni + 1);
1987 end = ((u_char *)ni) + nilen;
1991 printf("NodeInfo Supported Qtypes");
1992 if (options & F_VERBOSE) {
1993 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1994 printf(", compressed bitmap");
1996 printf(", raw bitmap");
2000 clen = (size_t)(end - cp);
2001 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2002 if (clen == 0 || clen > MAXQTYPES / 8 ||
2008 if (clen < sizeof(cbit) || clen % sizeof(v))
2010 memcpy(&cbit, cp, sizeof(cbit));
2011 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2015 clen = ntohs(cbit.words) * sizeof(v);
2016 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2021 for (off = 0; off < clen; off += sizeof(v)) {
2022 memcpy(&v, cp + off, sizeof(v));
2023 v = (u_int32_t)ntohl(v);
2024 b = pr_bitrange(v, (int)(cur + off * 8), b);
2026 /* flush the remaining bits */
2027 b = pr_bitrange(0, (int)(cur + off * 8), b);
2031 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2032 cur += ntohs(cbit.skip) * 32;
2037 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
2038 /* ni->qtype must be NODEADDR */
2040 u_char *cp = (u_char *)(ni + 1);
2041 char ntop_buf[INET6_ADDRSTRLEN];
2044 nilen -= sizeof(struct icmp6_nodeinfo);
2046 if (options & F_VERBOSE) {
2047 switch (ni->ni_code) {
2048 case ICMP6_NI_REFUSED:
2049 (void)printf("refused");
2051 case ICMP6_NI_UNKNOWN:
2052 (void)printf("unknown qtype");
2055 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2056 (void)printf(" truncated");
2060 printf(" no address\n");
2063 * In icmp-name-lookups 05 and later, TTL of each returned address
2064 * is contained in the resposne. We try to detect the version
2065 * by the length of the data, but note that the detection algorithm
2066 * is incomplete. We assume the latest draft by default.
2068 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2074 /* XXX: alignment? */
2075 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2076 cp += sizeof(u_int32_t);
2077 nilen -= sizeof(u_int32_t);
2080 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2082 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2083 printf(" %s", ntop_buf);
2085 if (ttl == 0xffffffff) {
2087 * XXX: can this convention be applied to all
2088 * type of TTL (i.e. non-ND TTL)?
2090 printf("(TTL=infty)");
2093 printf("(TTL=%u)", ttl);
2097 nilen -= sizeof(struct in6_addr);
2098 cp += sizeof(struct in6_addr);
2103 get_hoplim(struct msghdr *mhdr)
2107 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2108 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2109 if (cm->cmsg_len == 0)
2112 if (cm->cmsg_level == IPPROTO_IPV6 &&
2113 cm->cmsg_type == IPV6_HOPLIMIT &&
2114 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2115 return(*(int *)CMSG_DATA(cm));
2121 static struct in6_pktinfo *
2122 get_rcvpktinfo(struct msghdr *mhdr)
2126 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2127 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2128 if (cm->cmsg_len == 0)
2131 if (cm->cmsg_level == IPPROTO_IPV6 &&
2132 cm->cmsg_type == IPV6_PKTINFO &&
2133 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2134 return((struct in6_pktinfo *)CMSG_DATA(cm));
2141 get_pathmtu(struct msghdr *mhdr)
2143 #ifdef IPV6_RECVPATHMTU
2145 struct ip6_mtuinfo *mtuctl = NULL;
2147 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2148 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2149 if (cm->cmsg_len == 0)
2152 if (cm->cmsg_level == IPPROTO_IPV6 &&
2153 cm->cmsg_type == IPV6_PATHMTU &&
2154 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2155 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2158 * If the notified destination is different from
2159 * the one we are pinging, just ignore the info.
2160 * We check the scope ID only when both notified value
2161 * and our own value have non-0 values, because we may
2162 * have used the default scope zone ID for sending,
2163 * in which case the scope ID value is 0.
2165 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2167 (mtuctl->ip6m_addr.sin6_scope_id &&
2168 dst.sin6_scope_id &&
2169 mtuctl->ip6m_addr.sin6_scope_id !=
2170 dst.sin6_scope_id)) {
2171 if ((options & F_VERBOSE) != 0) {
2172 printf("path MTU for %s is notified. "
2174 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2175 sizeof(mtuctl->ip6m_addr)));
2181 * Ignore an invalid MTU. XXX: can we just believe
2184 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2187 /* notification for our destination. return the MTU. */
2188 return((int)mtuctl->ip6m_mtu);
2197 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2201 tvsub(struct timeval *out, struct timeval *in)
2203 if ((out->tv_usec -= in->tv_usec) < 0) {
2205 out->tv_usec += 1000000;
2207 out->tv_sec -= in->tv_sec;
2216 onint(int notused __unused)
2219 * When doing reverse DNS lookups, the seenint flag might not
2220 * be noticed for a while. Just exit if we get a second SIGINT.
2222 if ((options & F_HOSTNAME) && seenint != 0)
2223 _exit(nreceived ? 0 : 2);
2228 * Print out statistics.
2234 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2235 (void)printf("%ld packets transmitted, ", ntransmitted);
2236 (void)printf("%ld packets received, ", nreceived);
2238 (void)printf("+%ld duplicates, ", nrepeats);
2240 if (nreceived > ntransmitted)
2241 (void)printf("-- somebody's duplicating packets!");
2243 (void)printf("%.1f%% packet loss",
2244 ((((double)ntransmitted - nreceived) * 100.0) /
2248 printf(", %ld packets out of wait time", nrcvtimeout);
2249 (void)putchar('\n');
2250 if (nreceived && timing) {
2251 /* Only display average to microseconds */
2252 double num = nreceived + nrepeats;
2253 double avg = tsum / num;
2254 double dev = sqrt(tsumsq / num - avg * avg);
2256 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2257 tmin, avg, tmax, dev);
2258 (void)fflush(stdout);
2260 (void)fflush(stdout);
2264 static const char *niqcode[] = {
2266 "DNS label", /*or empty*/
2271 static const char *nircode[] = {
2272 "Success", "Refused", "Unknown",
2278 * Print a descriptive string about an ICMP header.
2281 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2283 char ntop_buf[INET6_ADDRSTRLEN];
2284 struct nd_redirect *red;
2285 struct icmp6_nodeinfo *ni;
2286 char dnsname[MAXDNAME + 1];
2290 switch (icp->icmp6_type) {
2291 case ICMP6_DST_UNREACH:
2292 switch (icp->icmp6_code) {
2293 case ICMP6_DST_UNREACH_NOROUTE:
2294 (void)printf("No Route to Destination\n");
2296 case ICMP6_DST_UNREACH_ADMIN:
2297 (void)printf("Destination Administratively "
2300 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2301 (void)printf("Destination Unreachable Beyond Scope\n");
2303 case ICMP6_DST_UNREACH_ADDR:
2304 (void)printf("Destination Host Unreachable\n");
2306 case ICMP6_DST_UNREACH_NOPORT:
2307 (void)printf("Destination Port Unreachable\n");
2310 (void)printf("Destination Unreachable, Bad Code: %d\n",
2314 /* Print returned IP header information */
2315 pr_retip((struct ip6_hdr *)(icp + 1), end);
2317 case ICMP6_PACKET_TOO_BIG:
2318 (void)printf("Packet too big mtu = %d\n",
2319 (int)ntohl(icp->icmp6_mtu));
2320 pr_retip((struct ip6_hdr *)(icp + 1), end);
2322 case ICMP6_TIME_EXCEEDED:
2323 switch (icp->icmp6_code) {
2324 case ICMP6_TIME_EXCEED_TRANSIT:
2325 (void)printf("Time to live exceeded\n");
2327 case ICMP6_TIME_EXCEED_REASSEMBLY:
2328 (void)printf("Frag reassembly time exceeded\n");
2331 (void)printf("Time exceeded, Bad Code: %d\n",
2335 pr_retip((struct ip6_hdr *)(icp + 1), end);
2337 case ICMP6_PARAM_PROB:
2338 (void)printf("Parameter problem: ");
2339 switch (icp->icmp6_code) {
2340 case ICMP6_PARAMPROB_HEADER:
2341 (void)printf("Erroneous Header ");
2343 case ICMP6_PARAMPROB_NEXTHEADER:
2344 (void)printf("Unknown Nextheader ");
2346 case ICMP6_PARAMPROB_OPTION:
2347 (void)printf("Unrecognized Option ");
2350 (void)printf("Bad code(%d) ", icp->icmp6_code);
2353 (void)printf("pointer = 0x%02x\n",
2354 (u_int32_t)ntohl(icp->icmp6_pptr));
2355 pr_retip((struct ip6_hdr *)(icp + 1), end);
2357 case ICMP6_ECHO_REQUEST:
2358 (void)printf("Echo Request");
2359 /* XXX ID + Seq + Data */
2361 case ICMP6_ECHO_REPLY:
2362 (void)printf("Echo Reply");
2363 /* XXX ID + Seq + Data */
2365 case ICMP6_MEMBERSHIP_QUERY:
2366 (void)printf("Listener Query");
2368 case ICMP6_MEMBERSHIP_REPORT:
2369 (void)printf("Listener Report");
2371 case ICMP6_MEMBERSHIP_REDUCTION:
2372 (void)printf("Listener Done");
2374 case ND_ROUTER_SOLICIT:
2375 (void)printf("Router Solicitation");
2377 case ND_ROUTER_ADVERT:
2378 (void)printf("Router Advertisement");
2380 case ND_NEIGHBOR_SOLICIT:
2381 (void)printf("Neighbor Solicitation");
2383 case ND_NEIGHBOR_ADVERT:
2384 (void)printf("Neighbor Advertisement");
2387 red = (struct nd_redirect *)icp;
2388 (void)printf("Redirect\n");
2389 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2391 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2392 (void)printf("Destination: %s", ntop_buf);
2393 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2395 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2396 (void)printf(" New Target: %s", ntop_buf);
2398 case ICMP6_NI_QUERY:
2399 (void)printf("Node Information Query");
2400 /* XXX ID + Seq + Data */
2401 ni = (struct icmp6_nodeinfo *)icp;
2402 l = end - (u_char *)(ni + 1);
2404 switch (ntohs(ni->ni_qtype)) {
2406 (void)printf("NOOP");
2408 case NI_QTYPE_SUPTYPES:
2409 (void)printf("Supported qtypes");
2412 (void)printf("DNS name");
2414 case NI_QTYPE_NODEADDR:
2415 (void)printf("nodeaddr");
2417 case NI_QTYPE_IPV4ADDR:
2418 (void)printf("IPv4 nodeaddr");
2421 (void)printf("unknown qtype");
2424 if (options & F_VERBOSE) {
2425 switch (ni->ni_code) {
2426 case ICMP6_NI_SUBJ_IPV6:
2427 if (l == sizeof(struct in6_addr) &&
2428 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2429 sizeof(ntop_buf)) != NULL) {
2430 (void)printf(", subject=%s(%s)",
2431 niqcode[ni->ni_code], ntop_buf);
2434 /* backward compat to -W */
2435 (void)printf(", oldfqdn");
2437 (void)printf(", invalid");
2441 case ICMP6_NI_SUBJ_FQDN:
2442 if (end == (u_char *)(ni + 1)) {
2443 (void)printf(", no subject");
2446 printf(", subject=%s", niqcode[ni->ni_code]);
2447 cp = (const u_char *)(ni + 1);
2448 if (dnsdecode(&cp, end, NULL, dnsname,
2449 sizeof(dnsname)) != NULL)
2450 printf("(%s)", dnsname);
2452 printf("(invalid)");
2454 case ICMP6_NI_SUBJ_IPV4:
2455 if (l == sizeof(struct in_addr) &&
2456 inet_ntop(AF_INET, ni + 1, ntop_buf,
2457 sizeof(ntop_buf)) != NULL) {
2458 (void)printf(", subject=%s(%s)",
2459 niqcode[ni->ni_code], ntop_buf);
2461 (void)printf(", invalid");
2464 (void)printf(", invalid");
2469 case ICMP6_NI_REPLY:
2470 (void)printf("Node Information Reply");
2471 /* XXX ID + Seq + Data */
2472 ni = (struct icmp6_nodeinfo *)icp;
2474 switch (ntohs(ni->ni_qtype)) {
2476 (void)printf("NOOP");
2478 case NI_QTYPE_SUPTYPES:
2479 (void)printf("Supported qtypes");
2482 (void)printf("DNS name");
2484 case NI_QTYPE_NODEADDR:
2485 (void)printf("nodeaddr");
2487 case NI_QTYPE_IPV4ADDR:
2488 (void)printf("IPv4 nodeaddr");
2491 (void)printf("unknown qtype");
2494 if (options & F_VERBOSE) {
2495 if (ni->ni_code > nitems(nircode))
2496 printf(", invalid");
2498 printf(", %s", nircode[ni->ni_code]);
2502 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2508 * Print an IP6 header.
2511 pr_iph(struct ip6_hdr *ip6)
2513 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2515 char ntop_buf[INET6_ADDRSTRLEN];
2517 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2518 tc = (tc >> 4) & 0x0f;
2519 tc |= (ip6->ip6_vfc << 4);
2521 printf("Vr TC Flow Plen Nxt Hlim\n");
2522 printf(" %1x %02x %05x %04x %02x %02x\n",
2523 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2524 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2525 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2526 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2527 printf("%s->", ntop_buf);
2528 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2529 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2530 printf("%s\n", ntop_buf);
2535 * Return an ascii host address as a dotted quad and optionally with
2539 pr_addr(struct sockaddr *addr, int addrlen)
2541 static char buf[NI_MAXHOST];
2544 if ((options & F_HOSTNAME) == 0)
2545 flag |= NI_NUMERICHOST;
2547 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2555 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2558 pr_retip(struct ip6_hdr *ip6, u_char *end)
2560 u_char *cp = (u_char *)ip6, nh;
2563 if ((size_t)(end - (u_char *)ip6) < sizeof(*ip6)) {
2568 hlen = sizeof(*ip6);
2572 while (end - cp >= 8) {
2574 case IPPROTO_HOPOPTS:
2576 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2577 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2579 case IPPROTO_DSTOPTS:
2581 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2582 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2584 case IPPROTO_FRAGMENT:
2586 hlen = sizeof(struct ip6_frag);
2587 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2589 case IPPROTO_ROUTING:
2591 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2592 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2597 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2598 nh = ((struct ah *)cp)->ah_nxt;
2601 case IPPROTO_ICMPV6:
2602 printf("ICMP6: type = %d, code = %d\n",
2609 printf("TCP: from port %u, to port %u (decimal)\n",
2610 (*cp * 256 + *(cp + 1)),
2611 (*(cp + 2) * 256 + *(cp + 3)));
2614 printf("UDP: from port %u, to port %u (decimal)\n",
2615 (*cp * 256 + *(cp + 1)),
2616 (*(cp + 2) * 256 + *(cp + 3)));
2619 printf("Unknown Header(%d)\n", nh);
2623 if ((cp += hlen) >= end)
2638 fill(char *bp, char *patp)
2644 for (cp = patp; *cp; cp++)
2646 errx(1, "patterns must be specified as hex digits");
2648 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2649 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2650 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2651 &pat[13], &pat[14], &pat[15]);
2656 (size_t)kk <= MAXDATALEN - 8 + sizeof(struct tv32) + ii;
2658 for (jj = 0; jj < ii; ++jj)
2659 bp[jj + kk] = pat[jj];
2660 if (!(options & F_QUIET)) {
2661 (void)printf("PATTERN: 0x");
2662 for (jj = 0; jj < ii; ++jj)
2663 (void)printf("%02x", bp[jj] & 0xFF);
2669 #ifdef IPSEC_POLICY_IPSEC
2671 setpolicy(int so __unused, char *policy)
2676 return 0; /* ignore */
2678 buf = ipsec_set_policy(policy, strlen(policy));
2680 errx(1, "%s", ipsec_strerror());
2681 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2682 ipsec_get_policylen(buf)) < 0)
2683 warnx("Unable to set IPsec policy");
2692 nigroup(char *name, int nig_oldmcprefix)
2697 u_int8_t digest[16];
2700 char hbuf[NI_MAXHOST];
2701 struct in6_addr in6;
2704 p = strchr(name, '.');
2706 p = name + strlen(name);
2708 if (l > 63 || l > sizeof(hbuf) - 1)
2709 return NULL; /*label too long*/
2710 strncpy(hbuf, name, l);
2711 hbuf[(int)l] = '\0';
2713 for (q = name; *q; q++) {
2714 if (isupper(*(unsigned char *)q))
2715 *q = tolower(*(unsigned char *)q);
2718 /* generate 16 bytes of pseudo-random value. */
2719 memset(&ctxt, 0, sizeof(ctxt));
2722 MD5Update(&ctxt, &c, sizeof(c));
2723 MD5Update(&ctxt, (unsigned char *)name, l);
2724 MD5Final(digest, &ctxt);
2726 if (nig_oldmcprefix) {
2727 /* draft-ietf-ipngwg-icmp-name-lookup */
2728 valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
2731 valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
2734 return NULL; /*XXX*/
2736 if (nig_oldmcprefix) {
2737 /* draft-ietf-ipngwg-icmp-name-lookup */
2738 bcopy(digest, &in6.s6_addr[12], 4);
2741 bcopy(digest, &in6.s6_addr[13], 3);
2744 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2747 return strdup(hbuf);
2753 (void)fprintf(stderr,
2754 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2759 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2763 #ifdef IPV6_USE_MIN_MTU
2767 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2768 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2769 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2773 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2775 " [-X timeout] [hops ...] host\n");