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 #ifdef USE_RFC2292BIS
311 struct ip6_rthdr *rthdr = NULL;
313 #ifdef IPSEC_POLICY_IPSEC
314 char *policy_in = NULL;
315 char *policy_out = NULL;
320 #ifdef IPV6_USE_MIN_MTU
324 /* just to be sure */
325 memset(&smsghdr, 0, sizeof(smsghdr));
326 memset(&smsgiov, 0, sizeof(smsgiov));
328 alarmtimeout = preload = 0;
329 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
333 #ifdef IPSEC_POLICY_IPSEC
337 #endif /*IPSEC_POLICY_IPSEC*/
339 while ((ch = getopt(argc, argv,
340 "a:b:c:DdfHg:h:I:i:l:mnNop:qrRS:s:tvwWx:X:" ADDOPTS)) != -1) {
347 options &= ~F_NOUSERDATA;
348 options |= F_NODEADDR;
349 for (cp = optarg; *cp != '\0'; cp++) {
352 naflags |= NI_NODEADDR_FLAG_ALL;
356 naflags |= NI_NODEADDR_FLAG_COMPAT;
360 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
364 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
368 naflags |= NI_NODEADDR_FLAG_GLOBAL;
370 case 'A': /* experimental. not in the spec */
371 #ifdef NI_NODEADDR_FLAG_ANYCAST
372 naflags |= NI_NODEADDR_FLAG_ANYCAST;
376 "-a A is not supported on the platform");
387 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
390 lsockbufsize = strtoul(optarg, &e, 10);
391 sockbufsize = (int)lsockbufsize;
392 if (errno || !*optarg || *e ||
393 lsockbufsize > INT_MAX)
394 errx(1, "invalid socket buffer size");
397 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
401 npackets = strtol(optarg, &e, 10);
402 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
404 "illegal number of packets -- %s", optarg);
407 options |= F_DONTFRAG;
410 options |= F_SO_DEBUG;
415 errx(1, "Must be superuser to flood ping");
418 setbuf(stdout, (char *)NULL);
424 options |= F_HOSTNAME;
426 case 'h': /* hoplimit */
427 hoplimit = strtol(optarg, &e, 10);
428 if (*optarg == '\0' || *e != '\0')
429 errx(1, "illegal hoplimit %s", optarg);
430 if (255 < hoplimit || hoplimit < -1)
432 "illegal hoplimit -- %s", optarg);
436 options |= F_INTERFACE;
437 #ifndef USE_SIN6_SCOPE_ID
441 case 'i': /* wait between sending packets */
442 t = strtod(optarg, &e);
443 if (*optarg == '\0' || *e != '\0')
444 errx(1, "illegal timing interval %s", optarg);
445 if (t < 1 && getuid()) {
446 errx(1, "%s: only root may use interval < 1s",
449 intvl.tv_sec = (long)t;
451 (long)((t - intvl.tv_sec) * 1000000);
452 if (intvl.tv_sec < 0)
453 errx(1, "illegal timing interval %s", optarg);
454 /* less than 1/hz does not make sense */
455 if (intvl.tv_sec == 0 && intvl.tv_usec < 1) {
456 warnx("too small interval, raised to .000001");
459 options |= F_INTERVAL;
464 errx(1, "Must be superuser to preload");
466 preload = strtol(optarg, &e, 10);
467 if (preload < 0 || *optarg == '\0' || *e != '\0')
468 errx(1, "illegal preload value -- %s", optarg);
471 #ifdef IPV6_USE_MIN_MTU
475 errx(1, "-%c is not supported on this platform", ch);
479 options &= ~F_HOSTNAME;
482 options |= F_NIGROUP;
488 case 'p': /* fill buffer with user pattern */
489 options |= F_PINGFILLED;
490 fill((char *)datap, optarg);
496 options |= F_AUDIBLE;
502 memset(&hints, 0, sizeof(struct addrinfo));
503 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
504 hints.ai_family = AF_INET6;
505 hints.ai_socktype = SOCK_RAW;
506 hints.ai_protocol = IPPROTO_ICMPV6;
508 error = getaddrinfo(optarg, NULL, &hints, &res);
510 errx(1, "invalid source address: %s",
511 gai_strerror(error));
514 * res->ai_family must be AF_INET6 and res->ai_addrlen
515 * must be sizeof(src).
517 memcpy(&src, res->ai_addr, res->ai_addrlen);
518 srclen = res->ai_addrlen;
520 options |= F_SRCADDR;
522 case 's': /* size of packet to send */
523 datalen = strtol(optarg, &e, 10);
524 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
525 errx(1, "illegal datalen value -- %s", optarg);
526 if (datalen > MAXDATALEN) {
528 "datalen value too large, maximum is %d",
533 options &= ~F_NOUSERDATA;
534 options |= F_SUPTYPES;
537 options |= F_VERBOSE;
540 options &= ~F_NOUSERDATA;
544 options &= ~F_NOUSERDATA;
545 options |= F_FQDNOLD;
548 t = strtod(optarg, &e);
549 if (*e || e == optarg || t > (double)INT_MAX)
550 err(EX_USAGE, "invalid timing interval: `%s'",
552 options |= F_WAITTIME;
556 alarmtimeout = strtoul(optarg, &e, 0);
557 if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
558 errx(EX_USAGE, "invalid timeout: `%s'",
560 if (alarmtimeout > MAXALARM)
561 errx(EX_USAGE, "invalid timeout: `%s' > %d",
563 alarm((int)alarmtimeout);
566 #ifdef IPSEC_POLICY_IPSEC
569 if (!strncmp("in", optarg, 2)) {
570 if ((policy_in = strdup(optarg)) == NULL)
572 } else if (!strncmp("out", optarg, 3)) {
573 if ((policy_out = strdup(optarg)) == NULL)
576 errx(1, "invalid security policy");
580 options |= F_AUTHHDR;
583 options |= F_ENCRYPT;
585 #endif /*IPSEC_POLICY_IPSEC*/
602 #ifdef IPV6_RECVRTHDR /* 2292bis */
603 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
606 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
609 errx(1, "too many intermediate hops");
615 if (options & F_NIGROUP) {
616 target = nigroup(argv[argc - 1], nig_oldmcprefix);
617 if (target == NULL) {
622 target = argv[argc - 1];
625 memset(&hints, 0, sizeof(struct addrinfo));
626 hints.ai_flags = AI_CANONNAME;
627 hints.ai_family = AF_INET6;
628 hints.ai_socktype = SOCK_RAW;
629 hints.ai_protocol = IPPROTO_ICMPV6;
631 error = getaddrinfo(target, NULL, &hints, &res);
633 errx(1, "%s", gai_strerror(error));
634 if (res->ai_canonname)
635 hostname = strdup(res->ai_canonname);
640 errx(1, "getaddrinfo failed");
642 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
644 if ((s = socket(res->ai_family, res->ai_socktype,
645 res->ai_protocol)) < 0)
649 /* set the source address if specified. */
650 if ((options & F_SRCADDR) != 0) {
651 /* properly fill sin6_scope_id */
652 if (IN6_IS_ADDR_LINKLOCAL(&src.sin6_addr) && (
653 IN6_IS_ADDR_LINKLOCAL(&dst.sin6_addr) ||
654 IN6_IS_ADDR_MC_LINKLOCAL(&dst.sin6_addr) ||
655 IN6_IS_ADDR_MC_NODELOCAL(&dst.sin6_addr))) {
656 if (src.sin6_scope_id == 0)
657 src.sin6_scope_id = dst.sin6_scope_id;
658 if (dst.sin6_scope_id == 0)
659 dst.sin6_scope_id = src.sin6_scope_id;
661 if (bind(s, (struct sockaddr *)&src, srclen) != 0)
664 /* set the gateway (next hop) if specified */
666 memset(&hints, 0, sizeof(hints));
667 hints.ai_family = AF_INET6;
668 hints.ai_socktype = SOCK_RAW;
669 hints.ai_protocol = IPPROTO_ICMPV6;
671 error = getaddrinfo(gateway, NULL, &hints, &res);
673 errx(1, "getaddrinfo for the gateway %s: %s",
674 gateway, gai_strerror(error));
676 if (res->ai_next && (options & F_VERBOSE))
677 warnx("gateway resolves to multiple addresses");
679 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
680 res->ai_addr, res->ai_addrlen)) {
681 err(1, "setsockopt(IPV6_NEXTHOP)");
688 * let the kerel pass extension headers of incoming packets,
689 * for privileged socket options
691 if ((options & F_VERBOSE) != 0) {
694 #ifdef IPV6_RECVHOPOPTS
695 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
697 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
698 #else /* old adv. API */
699 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
701 err(1, "setsockopt(IPV6_HOPOPTS)");
703 #ifdef IPV6_RECVDSTOPTS
704 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
706 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
707 #else /* old adv. API */
708 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
710 err(1, "setsockopt(IPV6_DSTOPTS)");
712 #ifdef IPV6_RECVRTHDRDSTOPTS
713 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
715 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
719 /* revoke root privilege */
720 if (seteuid(getuid()) != 0)
721 err(1, "seteuid() failed");
722 if (setuid(getuid()) != 0)
723 err(1, "setuid() failed");
725 if ((options & F_FLOOD) && (options & F_INTERVAL))
726 errx(1, "-f and -i incompatible options");
728 if ((options & F_NOUSERDATA) == 0) {
729 if (datalen >= sizeof(struct tv32)) {
730 /* we can time transfer */
734 /* in F_VERBOSE case, we may get non-echoreply packets*/
735 if (options & F_VERBOSE)
736 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
738 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
740 /* suppress timing for node information query */
743 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
746 if (!(packet = (u_char *)malloc((u_int)packlen)))
747 err(1, "Unable to allocate packet");
748 if (!(options & F_PINGFILLED))
749 for (i = ICMP6ECHOLEN; i < packlen; ++i)
752 ident = getpid() & 0xFFFF;
753 arc4random_buf(nonce, sizeof(nonce));
755 if (options & F_DONTFRAG)
756 if (setsockopt(s, IPPROTO_IPV6, IPV6_DONTFRAG,
757 &optval, sizeof(optval)) == -1)
758 err(1, "IPV6_DONTFRAG");
761 if (options & F_SO_DEBUG)
762 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
764 optval = IPV6_DEFHLIM;
765 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
766 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
767 &optval, sizeof(optval)) == -1)
768 err(1, "IPV6_MULTICAST_HOPS");
769 #ifdef IPV6_USE_MIN_MTU
771 optval = mflag > 1 ? 0 : 1;
773 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
774 &optval, sizeof(optval)) == -1)
775 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
777 #ifdef IPV6_RECVPATHMTU
780 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
781 &optval, sizeof(optval)) == -1)
782 err(1, "setsockopt(IPV6_RECVPATHMTU)");
784 #endif /* IPV6_RECVPATHMTU */
785 #endif /* IPV6_USE_MIN_MTU */
788 #ifdef IPSEC_POLICY_IPSEC
789 if (options & F_POLICY) {
790 if (setpolicy(s, policy_in) < 0)
791 errx(1, "%s", ipsec_strerror());
792 if (setpolicy(s, policy_out) < 0)
793 errx(1, "%s", ipsec_strerror());
796 if (options & F_AUTHHDR) {
797 optval = IPSEC_LEVEL_REQUIRE;
798 #ifdef IPV6_AUTH_TRANS_LEVEL
799 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
800 &optval, sizeof(optval)) == -1)
801 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
803 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
804 &optval, sizeof(optval)) == -1)
805 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
808 if (options & F_ENCRYPT) {
809 optval = IPSEC_LEVEL_REQUIRE;
810 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
811 &optval, sizeof(optval)) == -1)
812 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
814 #endif /*IPSEC_POLICY_IPSEC*/
819 struct icmp6_filter filt;
820 if (!(options & F_VERBOSE)) {
821 ICMP6_FILTER_SETBLOCKALL(&filt);
822 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
823 (options & F_NODEADDR) || (options & F_SUPTYPES))
824 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
826 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
828 ICMP6_FILTER_SETPASSALL(&filt);
830 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
832 err(1, "setsockopt(ICMP6_FILTER)");
834 #endif /*ICMP6_FILTER*/
836 /* let the kerel pass extension headers of incoming packets */
837 if ((options & F_VERBOSE) != 0) {
840 #ifdef IPV6_RECVRTHDR
841 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
843 err(1, "setsockopt(IPV6_RECVRTHDR)");
844 #else /* old adv. API */
845 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
847 err(1, "setsockopt(IPV6_RTHDR)");
853 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
854 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
855 &optval, sizeof(optval)) == -1)
856 err(1, "IPV6_MULTICAST_LOOP");
859 /* Specify the outgoing interface and/or the source address */
861 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
864 ip6optlen += CMSG_SPACE(sizeof(int));
866 /* set IP6 packet options */
868 if ((scmsg = (char *)malloc(ip6optlen)) == NULL)
869 errx(1, "can't allocate enough memory");
870 smsghdr.msg_control = (caddr_t)scmsg;
871 smsghdr.msg_controllen = ip6optlen;
872 scmsgp = (struct cmsghdr *)scmsg;
875 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
876 memset(pktinfo, 0, sizeof(*pktinfo));
877 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
878 scmsgp->cmsg_level = IPPROTO_IPV6;
879 scmsgp->cmsg_type = IPV6_PKTINFO;
880 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
883 /* set the outgoing interface */
885 #ifndef USE_SIN6_SCOPE_ID
886 /* pktinfo must have already been allocated */
887 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
888 errx(1, "%s: invalid interface name", ifname);
890 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
891 errx(1, "%s: invalid interface name", ifname);
894 if (hoplimit != -1) {
895 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
896 scmsgp->cmsg_level = IPPROTO_IPV6;
897 scmsgp->cmsg_type = IPV6_HOPLIMIT;
898 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
900 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
903 if (argc > 1) { /* some intermediate addrs are specified */
905 #ifdef USE_RFC2292BIS
909 #ifdef USE_RFC2292BIS
910 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
911 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
912 scmsgp->cmsg_level = IPPROTO_IPV6;
913 scmsgp->cmsg_type = IPV6_RTHDR;
914 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
915 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
916 IPV6_RTHDR_TYPE_0, argc - 1);
918 errx(1, "can't initialize rthdr");
919 #else /* old advanced API */
920 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
921 IPV6_RTHDR_TYPE_0)) == NULL)
922 errx(1, "can't initialize rthdr");
923 #endif /* USE_RFC2292BIS */
925 for (hops = 0; hops < argc - 1; hops++) {
926 memset(&hints, 0, sizeof(hints));
927 hints.ai_family = AF_INET6;
929 if ((error = getaddrinfo(argv[hops], NULL, &hints,
931 errx(1, "%s", gai_strerror(error));
932 if (res->ai_addr->sa_family != AF_INET6)
934 "bad addr family of an intermediate addr");
935 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
936 #ifdef USE_RFC2292BIS
937 if (inet6_rth_add(rthdr, &sin6->sin6_addr))
938 errx(1, "can't add an intermediate node");
939 #else /* old advanced API */
940 if (inet6_rthdr_add(scmsg, &sin6->sin6_addr,
942 errx(1, "can't add an intermediate node");
943 #endif /* USE_RFC2292BIS */
947 #ifndef USE_RFC2292BIS
948 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
949 errx(1, "can't set the last flag");
952 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
955 if (!(options & F_SRCADDR)) {
957 * get the source address. XXX since we revoked the root
958 * privilege, we cannot use a raw socket for this.
961 socklen_t len = sizeof(src);
963 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
964 err(1, "UDP socket");
966 src.sin6_family = AF_INET6;
967 src.sin6_addr = dst.sin6_addr;
968 src.sin6_port = ntohs(DUMMY_PORT);
969 src.sin6_scope_id = dst.sin6_scope_id;
971 #ifdef USE_RFC2292BIS
973 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
974 (void *)pktinfo, sizeof(*pktinfo)))
975 err(1, "UDP setsockopt(IPV6_PKTINFO)");
977 if (hoplimit != -1 &&
978 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
979 (void *)&hoplimit, sizeof(hoplimit)))
980 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
982 if (hoplimit != -1 &&
983 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
984 (void *)&hoplimit, sizeof(hoplimit)))
985 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
988 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
989 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
990 err(1, "UDP setsockopt(IPV6_RTHDR)");
991 #else /* old advanced API */
992 if (smsghdr.msg_control &&
993 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
994 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
995 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
998 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
999 err(1, "UDP connect");
1001 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
1002 err(1, "getsockname");
1007 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1009 if (datalen > (size_t)sockbufsize)
1010 warnx("you need -b to increase socket buffer size");
1011 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
1012 sizeof(sockbufsize)) < 0)
1013 err(1, "setsockopt(SO_SNDBUF)");
1014 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
1015 sizeof(sockbufsize)) < 0)
1016 err(1, "setsockopt(SO_RCVBUF)");
1019 if (datalen > 8 * 1024) /*XXX*/
1020 warnx("you need -b to increase socket buffer size");
1022 * When pinging the broadcast address, you can get a lot of
1023 * answers. Doing something so evil is useful if you are trying
1024 * to stress the ethernet, or just want to fill the arp cache
1025 * to get some stuff for /etc/ethers.
1028 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1034 #ifndef USE_SIN6_SCOPE_ID
1035 #ifdef IPV6_RECVPKTINFO
1036 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1037 sizeof(optval)) < 0)
1038 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1039 #else /* old adv. API */
1040 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1041 sizeof(optval)) < 0)
1042 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1044 #endif /* USE_SIN6_SCOPE_ID */
1045 #ifdef IPV6_RECVHOPLIMIT
1046 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1047 sizeof(optval)) < 0)
1048 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1049 #else /* old adv. API */
1050 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1051 sizeof(optval)) < 0)
1052 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1055 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1056 (unsigned long)(pingerlen() - 8));
1057 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1058 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1063 if (npackets != 0 && preload > npackets)
1068 gettimeofday(&last, NULL);
1070 sigemptyset(&si_sa.sa_mask);
1072 si_sa.sa_handler = onsignal;
1073 if (sigaction(SIGINT, &si_sa, 0) == -1)
1074 err(EX_OSERR, "sigaction SIGINT");
1077 if (sigaction(SIGINFO, &si_sa, 0) == -1)
1078 err(EX_OSERR, "sigaction SIGINFO");
1081 if (alarmtimeout > 0) {
1082 if (sigaction(SIGALRM, &si_sa, 0) == -1)
1083 err(EX_OSERR, "sigaction SIGALRM");
1085 if (options & F_FLOOD) {
1087 intvl.tv_usec = 10000;
1088 } else if ((options & F_INTERVAL) == 0) {
1089 intvl.tv_sec = interval / 1000;
1090 intvl.tv_usec = interval % 1000 * 1000;
1094 while (seenint == 0) {
1095 struct timeval now, timeout;
1097 struct iovec iov[2];
1101 /* signal handling */
1113 gettimeofday(&now, NULL);
1114 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
1115 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
1116 while (timeout.tv_usec < 0) {
1117 timeout.tv_usec += 1000000;
1120 while (timeout.tv_usec > 1000000) {
1121 timeout.tv_usec -= 1000000;
1124 if (timeout.tv_sec < 0)
1125 timeout.tv_sec = timeout.tv_usec = 0;
1127 n = select(s + 1, &rfds, NULL, NULL, &timeout);
1129 continue; /* EINTR */
1131 m.msg_name = (caddr_t)&from;
1132 m.msg_namelen = sizeof(from);
1133 memset(&iov, 0, sizeof(iov));
1134 iov[0].iov_base = (caddr_t)packet;
1135 iov[0].iov_len = packlen;
1138 memset(cm, 0, CONTROLLEN);
1139 m.msg_control = (void *)cm;
1140 m.msg_controllen = CONTROLLEN;
1142 cc = recvmsg(s, &m, 0);
1144 if (errno != EINTR) {
1149 } else if (cc == 0) {
1153 * receive control messages only. Process the
1154 * exceptions (currently the only possibility is
1155 * a path MTU notification.)
1157 if ((mtu = get_pathmtu(&m)) > 0) {
1158 if ((options & F_VERBOSE) != 0) {
1159 printf("new path MTU (%d) is "
1166 * an ICMPv6 message (probably an echoreply)
1169 pr_pack(packet, cc, &m);
1171 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1172 (npackets > 0 && nreceived >= npackets))
1175 if (n == 0 || (options & F_FLOOD)) {
1176 if (npackets == 0 || ntransmitted < npackets)
1183 * If we're not transmitting any more packets,
1184 * change the timer to wait two round-trip times
1185 * if we've received any packets or (waittime)
1186 * milliseconds if we haven't.
1190 intvl.tv_sec = 2 * tmax / 1000;
1191 if (intvl.tv_sec == 0)
1194 intvl.tv_sec = waittime / 1000;
1195 intvl.tv_usec = waittime % 1000 * 1000;
1198 gettimeofday(&last, NULL);
1199 if (ntransmitted - nreceived - 1 > nmissedmax) {
1200 nmissedmax = ntransmitted - nreceived - 1;
1201 if (options & F_MISSED)
1202 (void)write(STDOUT_FILENO, &BBELL, 1);
1206 sigemptyset(&si_sa.sa_mask);
1208 si_sa.sa_handler = SIG_IGN;
1209 sigaction(SIGINT, &si_sa, 0);
1210 sigaction(SIGALRM, &si_sa, 0);
1216 exit(nreceived == 0 ? 2 : 0);
1238 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1239 * will be added on by the kernel. The ID field is our UNIX process ID,
1240 * and the sequence number is an ascending integer. The first 8 bytes
1241 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1242 * byte-order, to compute the round-trip time.
1249 if (options & F_FQDN)
1250 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1251 else if (options & F_FQDNOLD)
1253 else if (options & F_NODEADDR)
1254 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1255 else if (options & F_SUPTYPES)
1258 l = ICMP6ECHOLEN + datalen;
1266 struct icmp6_hdr *icp;
1267 struct iovec iov[2];
1269 struct icmp6_nodeinfo *nip;
1272 if (npackets && ntransmitted >= npackets)
1273 return(-1); /* no more transmission */
1275 icp = (struct icmp6_hdr *)outpack;
1276 nip = (struct icmp6_nodeinfo *)outpack;
1277 memset(icp, 0, sizeof(*icp));
1278 icp->icmp6_cksum = 0;
1279 seq = ntransmitted++;
1280 CLR(seq % mx_dup_ck);
1282 if (options & F_FQDN) {
1283 icp->icmp6_type = ICMP6_NI_QUERY;
1284 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1285 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1286 nip->ni_flags = htons(0);
1288 memcpy(nip->icmp6_ni_nonce, nonce,
1289 sizeof(nip->icmp6_ni_nonce));
1290 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1292 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1293 sizeof(dst.sin6_addr));
1294 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1296 } else if (options & F_FQDNOLD) {
1297 /* packet format in 03 draft - no Subject data on queries */
1298 icp->icmp6_type = ICMP6_NI_QUERY;
1299 icp->icmp6_code = 0; /* code field is always 0 */
1300 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1301 nip->ni_flags = htons(0);
1303 memcpy(nip->icmp6_ni_nonce, nonce,
1304 sizeof(nip->icmp6_ni_nonce));
1305 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1309 } else if (options & F_NODEADDR) {
1310 icp->icmp6_type = ICMP6_NI_QUERY;
1311 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1312 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1313 nip->ni_flags = naflags;
1315 memcpy(nip->icmp6_ni_nonce, nonce,
1316 sizeof(nip->icmp6_ni_nonce));
1317 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1319 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1320 sizeof(dst.sin6_addr));
1321 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1323 } else if (options & F_SUPTYPES) {
1324 icp->icmp6_type = ICMP6_NI_QUERY;
1325 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1326 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1327 /* we support compressed bitmap */
1328 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1330 memcpy(nip->icmp6_ni_nonce, nonce,
1331 sizeof(nip->icmp6_ni_nonce));
1332 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1336 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1337 icp->icmp6_code = 0;
1338 icp->icmp6_id = htons(ident);
1339 icp->icmp6_seq = ntohs(seq);
1343 (void)gettimeofday(&tv, NULL);
1344 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1345 tv32->tv32_sec = htonl(tv.tv_sec);
1346 tv32->tv32_usec = htonl(tv.tv_usec);
1348 cc = ICMP6ECHOLEN + datalen;
1352 if (pingerlen() != cc)
1353 errx(1, "internal error; length mismatch");
1356 smsghdr.msg_name = (caddr_t)&dst;
1357 smsghdr.msg_namelen = sizeof(dst);
1358 memset(&iov, 0, sizeof(iov));
1359 iov[0].iov_base = (caddr_t)outpack;
1360 iov[0].iov_len = cc;
1361 smsghdr.msg_iov = iov;
1362 smsghdr.msg_iovlen = 1;
1364 i = sendmsg(s, &smsghdr, 0);
1366 if (i < 0 || i != cc) {
1369 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1372 if (!(options & F_QUIET) && options & F_FLOOD)
1373 (void)write(STDOUT_FILENO, &DOT, 1);
1379 myechoreply(const struct icmp6_hdr *icp)
1381 if (ntohs(icp->icmp6_id) == ident)
1388 mynireply(const struct icmp6_nodeinfo *nip)
1390 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1391 nonce + sizeof(u_int16_t),
1392 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1399 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1401 /*base for compressed name*/
1405 char cresult[MAXDNAME + 1];
1416 if (i == 0 || cp != *sp) {
1417 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1418 return NULL; /*result overrun*/
1424 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1425 /* DNS compression */
1429 comp = base + (i & 0x3f);
1430 if (dnsdecode(&comp, cp, base, cresult,
1431 sizeof(cresult)) == NULL)
1433 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1434 return NULL; /*result overrun*/
1436 } else if ((i & 0x3f) == i) {
1438 return NULL; /*source overrun*/
1439 while (i-- > 0 && cp < ep) {
1440 l = snprintf(cresult, sizeof(cresult),
1441 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1442 if ((size_t)l >= sizeof(cresult) || l < 0)
1444 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1445 return NULL; /*result overrun*/
1449 return NULL; /*invalid label*/
1452 return NULL; /*not terminated*/
1460 * Print out the packet, if it came from us. This logic is necessary
1461 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1462 * which arrive ('tis only fair). This permits multiple copies of this
1463 * program to be run without having intermingled output (or statistics!).
1466 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1468 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1469 struct icmp6_hdr *icp;
1470 struct icmp6_nodeinfo *ni;
1473 struct sockaddr *from;
1475 u_char *cp = NULL, *dp, *end = buf + cc;
1476 struct in6_pktinfo *pktinfo = NULL;
1477 struct timeval tv, tp;
1479 double triptime = 0;
1484 char dnsname[MAXDNAME + 1];
1486 (void)gettimeofday(&tv, NULL);
1488 if (!mhdr || !mhdr->msg_name ||
1489 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1490 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1491 if (options & F_VERBOSE)
1492 warnx("invalid peername");
1495 from = (struct sockaddr *)mhdr->msg_name;
1496 fromlen = mhdr->msg_namelen;
1497 if (cc < (int)sizeof(struct icmp6_hdr)) {
1498 if (options & F_VERBOSE)
1499 warnx("packet too short (%d bytes) from %s", cc,
1500 pr_addr(from, fromlen));
1503 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1504 (options & F_VERBOSE) != 0)
1505 warnx("some control data discarded, insufficient buffer size");
1506 icp = (struct icmp6_hdr *)buf;
1507 ni = (struct icmp6_nodeinfo *)buf;
1510 if ((hoplim = get_hoplim(mhdr)) == -1) {
1511 warnx("failed to get receiving hop limit");
1514 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1515 warnx("failed to get receiving packet information");
1519 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1520 seq = ntohs(icp->icmp6_seq);
1523 tpp = (struct tv32 *)(icp + 1);
1524 tp.tv_sec = ntohl(tpp->tv32_sec);
1525 tp.tv_usec = ntohl(tpp->tv32_usec);
1527 triptime = ((double)tv.tv_sec) * 1000.0 +
1528 ((double)tv.tv_usec) / 1000.0;
1530 tsumsq += triptime * triptime;
1531 if (triptime < tmin)
1533 if (triptime > tmax)
1537 if (TST(seq % mx_dup_ck)) {
1542 SET(seq % mx_dup_ck);
1546 if (options & F_QUIET)
1549 if (options & F_WAITTIME && triptime > waittime) {
1554 if (options & F_FLOOD)
1555 (void)write(STDOUT_FILENO, &BSPACE, 1);
1557 if (options & F_AUDIBLE)
1558 (void)write(STDOUT_FILENO, &BBELL, 1);
1559 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1560 pr_addr(from, fromlen), seq);
1561 (void)printf(" hlim=%d", hoplim);
1562 if ((options & F_VERBOSE) != 0) {
1563 struct sockaddr_in6 dstsa;
1565 memset(&dstsa, 0, sizeof(dstsa));
1566 dstsa.sin6_family = AF_INET6;
1567 dstsa.sin6_len = sizeof(dstsa);
1568 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1569 dstsa.sin6_addr = pktinfo->ipi6_addr;
1570 (void)printf(" dst=%s",
1571 pr_addr((struct sockaddr *)&dstsa,
1575 (void)printf(" time=%.3f ms", triptime);
1577 (void)printf("(DUP!)");
1578 /* check the data */
1579 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1580 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1581 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1583 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1588 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1589 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1591 if (TST(seq % mx_dup_ck)) {
1596 SET(seq % mx_dup_ck);
1600 if (options & F_QUIET)
1603 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1605 switch (ntohs(ni->ni_code)) {
1606 case ICMP6_NI_SUCCESS:
1608 case ICMP6_NI_REFUSED:
1609 printf("refused, type 0x%x", ntohs(ni->ni_type));
1611 case ICMP6_NI_UNKNOWN:
1612 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1615 printf("unknown code 0x%x, type 0x%x",
1616 ntohs(ni->ni_code), ntohs(ni->ni_type));
1620 switch (ntohs(ni->ni_qtype)) {
1622 printf("NodeInfo NOOP");
1624 case NI_QTYPE_SUPTYPES:
1625 pr_suptypes(ni, end - (u_char *)ni);
1627 case NI_QTYPE_NODEADDR:
1628 pr_nodeaddr(ni, end - (u_char *)ni);
1631 default: /* XXX: for backward compatibility */
1632 cp = (u_char *)ni + ICMP6_NIRLEN;
1633 if (buf[off + ICMP6_NIRLEN] ==
1634 cc - off - ICMP6_NIRLEN - 1)
1639 cp++; /* skip length */
1641 safeputc(*cp & 0xff);
1647 if (dnsdecode((const u_char **)&cp, end,
1648 (const u_char *)(ni + 1), dnsname,
1649 sizeof(dnsname)) == NULL) {
1654 * name-lookup special handling for
1657 if (cp + 1 <= end && !*cp &&
1658 strlen(dnsname) > 0) {
1659 dnsname[strlen(dnsname) - 1] = '\0';
1662 printf("%s%s", i > 0 ? "," : "",
1666 if (options & F_VERBOSE) {
1670 (void)printf(" ("); /*)*/
1672 switch (ni->ni_code) {
1673 case ICMP6_NI_REFUSED:
1674 (void)printf("refused");
1677 case ICMP6_NI_UNKNOWN:
1678 (void)printf("unknown qtype");
1683 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1684 /* case of refusion, unknown */
1689 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1692 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1693 (void)printf("TTL=%d:meaningless",
1697 (void)printf("TTL=%d:invalid",
1700 (void)printf("TTL=%d", ttl);
1710 cp = (u_char *)ni + ICMP6_NIRLEN;
1719 if (buf[off + ICMP6_NIRLEN] !=
1720 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1723 (void)printf("invalid namelen:%d/%lu",
1724 buf[off + ICMP6_NIRLEN],
1725 (u_long)cc - off - ICMP6_NIRLEN - 1);
1735 /* We've got something other than an ECHOREPLY */
1736 if (!(options & F_VERBOSE))
1738 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1742 if (!(options & F_FLOOD)) {
1743 (void)putchar('\n');
1744 if (options & F_VERBOSE)
1746 (void)fflush(stdout);
1752 pr_exthdrs(struct msghdr *mhdr)
1759 bufp = mhdr->msg_control;
1760 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1761 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1762 if (cm->cmsg_level != IPPROTO_IPV6)
1765 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1768 switch (cm->cmsg_type) {
1770 printf(" HbH Options: ");
1771 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1774 #ifdef IPV6_RTHDRDSTOPTS
1775 case IPV6_RTHDRDSTOPTS:
1777 printf(" Dst Options: ");
1778 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1781 printf(" Routing: ");
1782 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1788 #ifdef USE_RFC2292BIS
1790 pr_ip6opt(void *extbuf, size_t bufsize)
1792 struct ip6_hbh *ext;
1795 socklen_t extlen, len;
1801 ext = (struct ip6_hbh *)extbuf;
1802 extlen = (ext->ip6h_len + 1) * 8;
1803 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1804 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1807 * Bounds checking on the ancillary data buffer:
1808 * subtract the size of a cmsg structure from the buffer size.
1810 if (bufsize < (extlen + CMSG_SPACE(0))) {
1811 extlen = bufsize - CMSG_SPACE(0);
1812 warnx("options truncated, showing only %u (total=%u)",
1813 (unsigned int)(extlen / 8 - 1),
1814 (unsigned int)(ext->ip6h_len));
1819 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1820 &type, &len, &databuf);
1821 if (currentlen == -1)
1825 * Note that inet6_opt_next automatically skips any padding
1830 offset = inet6_opt_get_val(databuf, offset,
1831 &value4, sizeof(value4));
1832 printf(" Jumbo Payload Opt: Length %u\n",
1833 (u_int32_t)ntohl(value4));
1835 case IP6OPT_ROUTER_ALERT:
1837 offset = inet6_opt_get_val(databuf, offset,
1838 &value2, sizeof(value2));
1839 printf(" Router Alert Opt: Type %u\n",
1843 printf(" Received Opt %u len %lu\n",
1844 type, (unsigned long)len);
1850 #else /* !USE_RFC2292BIS */
1853 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1858 #endif /* USE_RFC2292BIS */
1860 #ifdef USE_RFC2292BIS
1862 pr_rthdr(void *extbuf, size_t bufsize)
1864 struct in6_addr *in6;
1865 char ntopbuf[INET6_ADDRSTRLEN];
1866 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1867 int i, segments, origsegs, rthsize, size0, size1;
1869 /* print fixed part of the header */
1870 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1871 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1872 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1873 printf("%d segments, ", segments);
1874 printf("%d left\n", rh->ip6r_segleft);
1876 printf("segments unknown, ");
1877 printf("%d left\n", rh->ip6r_segleft);
1882 * Bounds checking on the ancillary data buffer. When calculating
1883 * the number of items to show keep in mind:
1884 * - The size of the cmsg structure
1885 * - The size of one segment (the size of a Type 0 routing header)
1886 * - When dividing add a fudge factor of one in case the
1887 * dividend is not evenly divisible by the divisor
1889 rthsize = (rh->ip6r_len + 1) * 8;
1890 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1891 origsegs = segments;
1892 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1893 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1894 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1895 (size1 - size0) + 1;
1896 warnx("segments truncated, showing only %d (total=%d)",
1897 segments, origsegs);
1900 for (i = 0; i < segments; i++) {
1901 in6 = inet6_rth_getaddr(extbuf, i);
1903 printf(" [%d]<NULL>\n", i);
1905 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1907 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1908 printf(" [%d]%s\n", i, ntopbuf);
1916 #else /* !USE_RFC2292BIS */
1919 pr_rthdr(void *extbuf, size_t bufsize __unused)
1924 #endif /* USE_RFC2292BIS */
1927 pr_bitrange(u_int32_t v, int soff, int ii)
1934 /* shift till we have 0x01 */
1935 if ((v & 0x01) == 0) {
1937 printf("-%u", soff + off - 1);
1948 case 0x04: case 0x0c:
1959 /* we have 0x01 with us */
1960 for (i = 0; i < 32 - off; i++) {
1961 if ((v & (0x01 << i)) == 0)
1965 printf(" %u", soff + off);
1973 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1974 /* ni->qtype must be SUPTYPES */
1978 const u_char *cp, *end;
1981 u_int16_t words; /*32bit count*/
1984 #define MAXQTYPES (1 << 16)
1988 cp = (u_char *)(ni + 1);
1989 end = ((u_char *)ni) + nilen;
1993 printf("NodeInfo Supported Qtypes");
1994 if (options & F_VERBOSE) {
1995 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1996 printf(", compressed bitmap");
1998 printf(", raw bitmap");
2002 clen = (size_t)(end - cp);
2003 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2004 if (clen == 0 || clen > MAXQTYPES / 8 ||
2010 if (clen < sizeof(cbit) || clen % sizeof(v))
2012 memcpy(&cbit, cp, sizeof(cbit));
2013 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2017 clen = ntohs(cbit.words) * sizeof(v);
2018 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2023 for (off = 0; off < clen; off += sizeof(v)) {
2024 memcpy(&v, cp + off, sizeof(v));
2025 v = (u_int32_t)ntohl(v);
2026 b = pr_bitrange(v, (int)(cur + off * 8), b);
2028 /* flush the remaining bits */
2029 b = pr_bitrange(0, (int)(cur + off * 8), b);
2033 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2034 cur += ntohs(cbit.skip) * 32;
2039 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
2040 /* ni->qtype must be NODEADDR */
2042 u_char *cp = (u_char *)(ni + 1);
2043 char ntop_buf[INET6_ADDRSTRLEN];
2046 nilen -= sizeof(struct icmp6_nodeinfo);
2048 if (options & F_VERBOSE) {
2049 switch (ni->ni_code) {
2050 case ICMP6_NI_REFUSED:
2051 (void)printf("refused");
2053 case ICMP6_NI_UNKNOWN:
2054 (void)printf("unknown qtype");
2057 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2058 (void)printf(" truncated");
2062 printf(" no address\n");
2065 * In icmp-name-lookups 05 and later, TTL of each returned address
2066 * is contained in the resposne. We try to detect the version
2067 * by the length of the data, but note that the detection algorithm
2068 * is incomplete. We assume the latest draft by default.
2070 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2076 /* XXX: alignment? */
2077 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2078 cp += sizeof(u_int32_t);
2079 nilen -= sizeof(u_int32_t);
2082 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2084 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2085 printf(" %s", ntop_buf);
2087 if (ttl == 0xffffffff) {
2089 * XXX: can this convention be applied to all
2090 * type of TTL (i.e. non-ND TTL)?
2092 printf("(TTL=infty)");
2095 printf("(TTL=%u)", ttl);
2099 nilen -= sizeof(struct in6_addr);
2100 cp += sizeof(struct in6_addr);
2105 get_hoplim(struct msghdr *mhdr)
2109 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2110 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2111 if (cm->cmsg_len == 0)
2114 if (cm->cmsg_level == IPPROTO_IPV6 &&
2115 cm->cmsg_type == IPV6_HOPLIMIT &&
2116 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2117 return(*(int *)CMSG_DATA(cm));
2123 static struct in6_pktinfo *
2124 get_rcvpktinfo(struct msghdr *mhdr)
2128 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2129 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2130 if (cm->cmsg_len == 0)
2133 if (cm->cmsg_level == IPPROTO_IPV6 &&
2134 cm->cmsg_type == IPV6_PKTINFO &&
2135 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2136 return((struct in6_pktinfo *)CMSG_DATA(cm));
2143 get_pathmtu(struct msghdr *mhdr)
2145 #ifdef IPV6_RECVPATHMTU
2147 struct ip6_mtuinfo *mtuctl = NULL;
2149 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2150 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2151 if (cm->cmsg_len == 0)
2154 if (cm->cmsg_level == IPPROTO_IPV6 &&
2155 cm->cmsg_type == IPV6_PATHMTU &&
2156 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2157 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2160 * If the notified destination is different from
2161 * the one we are pinging, just ignore the info.
2162 * We check the scope ID only when both notified value
2163 * and our own value have non-0 values, because we may
2164 * have used the default scope zone ID for sending,
2165 * in which case the scope ID value is 0.
2167 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2169 (mtuctl->ip6m_addr.sin6_scope_id &&
2170 dst.sin6_scope_id &&
2171 mtuctl->ip6m_addr.sin6_scope_id !=
2172 dst.sin6_scope_id)) {
2173 if ((options & F_VERBOSE) != 0) {
2174 printf("path MTU for %s is notified. "
2176 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2177 sizeof(mtuctl->ip6m_addr)));
2183 * Ignore an invalid MTU. XXX: can we just believe
2186 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2189 /* notification for our destination. return the MTU. */
2190 return((int)mtuctl->ip6m_mtu);
2199 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2203 tvsub(struct timeval *out, struct timeval *in)
2205 if ((out->tv_usec -= in->tv_usec) < 0) {
2207 out->tv_usec += 1000000;
2209 out->tv_sec -= in->tv_sec;
2218 onint(int notused __unused)
2221 * When doing reverse DNS lookups, the seenint flag might not
2222 * be noticed for a while. Just exit if we get a second SIGINT.
2224 if ((options & F_HOSTNAME) && seenint != 0)
2225 _exit(nreceived ? 0 : 2);
2230 * Print out statistics.
2236 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2237 (void)printf("%ld packets transmitted, ", ntransmitted);
2238 (void)printf("%ld packets received, ", nreceived);
2240 (void)printf("+%ld duplicates, ", nrepeats);
2242 if (nreceived > ntransmitted)
2243 (void)printf("-- somebody's duplicating packets!");
2245 (void)printf("%.1f%% packet loss",
2246 ((((double)ntransmitted - nreceived) * 100.0) /
2250 printf(", %ld packets out of wait time", nrcvtimeout);
2251 (void)putchar('\n');
2252 if (nreceived && timing) {
2253 /* Only display average to microseconds */
2254 double num = nreceived + nrepeats;
2255 double avg = tsum / num;
2256 double dev = sqrt(tsumsq / num - avg * avg);
2258 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2259 tmin, avg, tmax, dev);
2260 (void)fflush(stdout);
2262 (void)fflush(stdout);
2266 static const char *niqcode[] = {
2268 "DNS label", /*or empty*/
2273 static const char *nircode[] = {
2274 "Success", "Refused", "Unknown",
2280 * Print a descriptive string about an ICMP header.
2283 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2285 char ntop_buf[INET6_ADDRSTRLEN];
2286 struct nd_redirect *red;
2287 struct icmp6_nodeinfo *ni;
2288 char dnsname[MAXDNAME + 1];
2292 switch (icp->icmp6_type) {
2293 case ICMP6_DST_UNREACH:
2294 switch (icp->icmp6_code) {
2295 case ICMP6_DST_UNREACH_NOROUTE:
2296 (void)printf("No Route to Destination\n");
2298 case ICMP6_DST_UNREACH_ADMIN:
2299 (void)printf("Destination Administratively "
2302 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2303 (void)printf("Destination Unreachable Beyond Scope\n");
2305 case ICMP6_DST_UNREACH_ADDR:
2306 (void)printf("Destination Host Unreachable\n");
2308 case ICMP6_DST_UNREACH_NOPORT:
2309 (void)printf("Destination Port Unreachable\n");
2312 (void)printf("Destination Unreachable, Bad Code: %d\n",
2316 /* Print returned IP header information */
2317 pr_retip((struct ip6_hdr *)(icp + 1), end);
2319 case ICMP6_PACKET_TOO_BIG:
2320 (void)printf("Packet too big mtu = %d\n",
2321 (int)ntohl(icp->icmp6_mtu));
2322 pr_retip((struct ip6_hdr *)(icp + 1), end);
2324 case ICMP6_TIME_EXCEEDED:
2325 switch (icp->icmp6_code) {
2326 case ICMP6_TIME_EXCEED_TRANSIT:
2327 (void)printf("Time to live exceeded\n");
2329 case ICMP6_TIME_EXCEED_REASSEMBLY:
2330 (void)printf("Frag reassembly time exceeded\n");
2333 (void)printf("Time exceeded, Bad Code: %d\n",
2337 pr_retip((struct ip6_hdr *)(icp + 1), end);
2339 case ICMP6_PARAM_PROB:
2340 (void)printf("Parameter problem: ");
2341 switch (icp->icmp6_code) {
2342 case ICMP6_PARAMPROB_HEADER:
2343 (void)printf("Erroneous Header ");
2345 case ICMP6_PARAMPROB_NEXTHEADER:
2346 (void)printf("Unknown Nextheader ");
2348 case ICMP6_PARAMPROB_OPTION:
2349 (void)printf("Unrecognized Option ");
2352 (void)printf("Bad code(%d) ", icp->icmp6_code);
2355 (void)printf("pointer = 0x%02x\n",
2356 (u_int32_t)ntohl(icp->icmp6_pptr));
2357 pr_retip((struct ip6_hdr *)(icp + 1), end);
2359 case ICMP6_ECHO_REQUEST:
2360 (void)printf("Echo Request");
2361 /* XXX ID + Seq + Data */
2363 case ICMP6_ECHO_REPLY:
2364 (void)printf("Echo Reply");
2365 /* XXX ID + Seq + Data */
2367 case ICMP6_MEMBERSHIP_QUERY:
2368 (void)printf("Listener Query");
2370 case ICMP6_MEMBERSHIP_REPORT:
2371 (void)printf("Listener Report");
2373 case ICMP6_MEMBERSHIP_REDUCTION:
2374 (void)printf("Listener Done");
2376 case ND_ROUTER_SOLICIT:
2377 (void)printf("Router Solicitation");
2379 case ND_ROUTER_ADVERT:
2380 (void)printf("Router Advertisement");
2382 case ND_NEIGHBOR_SOLICIT:
2383 (void)printf("Neighbor Solicitation");
2385 case ND_NEIGHBOR_ADVERT:
2386 (void)printf("Neighbor Advertisement");
2389 red = (struct nd_redirect *)icp;
2390 (void)printf("Redirect\n");
2391 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2393 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2394 (void)printf("Destination: %s", ntop_buf);
2395 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2397 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2398 (void)printf(" New Target: %s", ntop_buf);
2400 case ICMP6_NI_QUERY:
2401 (void)printf("Node Information Query");
2402 /* XXX ID + Seq + Data */
2403 ni = (struct icmp6_nodeinfo *)icp;
2404 l = end - (u_char *)(ni + 1);
2406 switch (ntohs(ni->ni_qtype)) {
2408 (void)printf("NOOP");
2410 case NI_QTYPE_SUPTYPES:
2411 (void)printf("Supported qtypes");
2414 (void)printf("DNS name");
2416 case NI_QTYPE_NODEADDR:
2417 (void)printf("nodeaddr");
2419 case NI_QTYPE_IPV4ADDR:
2420 (void)printf("IPv4 nodeaddr");
2423 (void)printf("unknown qtype");
2426 if (options & F_VERBOSE) {
2427 switch (ni->ni_code) {
2428 case ICMP6_NI_SUBJ_IPV6:
2429 if (l == sizeof(struct in6_addr) &&
2430 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2431 sizeof(ntop_buf)) != NULL) {
2432 (void)printf(", subject=%s(%s)",
2433 niqcode[ni->ni_code], ntop_buf);
2436 /* backward compat to -W */
2437 (void)printf(", oldfqdn");
2439 (void)printf(", invalid");
2443 case ICMP6_NI_SUBJ_FQDN:
2444 if (end == (u_char *)(ni + 1)) {
2445 (void)printf(", no subject");
2448 printf(", subject=%s", niqcode[ni->ni_code]);
2449 cp = (const u_char *)(ni + 1);
2450 if (dnsdecode(&cp, end, NULL, dnsname,
2451 sizeof(dnsname)) != NULL)
2452 printf("(%s)", dnsname);
2454 printf("(invalid)");
2456 case ICMP6_NI_SUBJ_IPV4:
2457 if (l == sizeof(struct in_addr) &&
2458 inet_ntop(AF_INET, ni + 1, ntop_buf,
2459 sizeof(ntop_buf)) != NULL) {
2460 (void)printf(", subject=%s(%s)",
2461 niqcode[ni->ni_code], ntop_buf);
2463 (void)printf(", invalid");
2466 (void)printf(", invalid");
2471 case ICMP6_NI_REPLY:
2472 (void)printf("Node Information Reply");
2473 /* XXX ID + Seq + Data */
2474 ni = (struct icmp6_nodeinfo *)icp;
2476 switch (ntohs(ni->ni_qtype)) {
2478 (void)printf("NOOP");
2480 case NI_QTYPE_SUPTYPES:
2481 (void)printf("Supported qtypes");
2484 (void)printf("DNS name");
2486 case NI_QTYPE_NODEADDR:
2487 (void)printf("nodeaddr");
2489 case NI_QTYPE_IPV4ADDR:
2490 (void)printf("IPv4 nodeaddr");
2493 (void)printf("unknown qtype");
2496 if (options & F_VERBOSE) {
2497 if (ni->ni_code > nitems(nircode))
2498 printf(", invalid");
2500 printf(", %s", nircode[ni->ni_code]);
2504 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2510 * Print an IP6 header.
2513 pr_iph(struct ip6_hdr *ip6)
2515 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2517 char ntop_buf[INET6_ADDRSTRLEN];
2519 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2520 tc = (tc >> 4) & 0x0f;
2521 tc |= (ip6->ip6_vfc << 4);
2523 printf("Vr TC Flow Plen Nxt Hlim\n");
2524 printf(" %1x %02x %05x %04x %02x %02x\n",
2525 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2526 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2527 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2528 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2529 printf("%s->", ntop_buf);
2530 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2531 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2532 printf("%s\n", ntop_buf);
2537 * Return an ascii host address as a dotted quad and optionally with
2541 pr_addr(struct sockaddr *addr, int addrlen)
2543 static char buf[NI_MAXHOST];
2546 if ((options & F_HOSTNAME) == 0)
2547 flag |= NI_NUMERICHOST;
2549 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2557 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2560 pr_retip(struct ip6_hdr *ip6, u_char *end)
2562 u_char *cp = (u_char *)ip6, nh;
2565 if ((size_t)(end - (u_char *)ip6) < sizeof(*ip6)) {
2570 hlen = sizeof(*ip6);
2574 while (end - cp >= 8) {
2576 case IPPROTO_HOPOPTS:
2578 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2579 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2581 case IPPROTO_DSTOPTS:
2583 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2584 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2586 case IPPROTO_FRAGMENT:
2588 hlen = sizeof(struct ip6_frag);
2589 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2591 case IPPROTO_ROUTING:
2593 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2594 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2599 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2600 nh = ((struct ah *)cp)->ah_nxt;
2603 case IPPROTO_ICMPV6:
2604 printf("ICMP6: type = %d, code = %d\n",
2611 printf("TCP: from port %u, to port %u (decimal)\n",
2612 (*cp * 256 + *(cp + 1)),
2613 (*(cp + 2) * 256 + *(cp + 3)));
2616 printf("UDP: from port %u, to port %u (decimal)\n",
2617 (*cp * 256 + *(cp + 1)),
2618 (*(cp + 2) * 256 + *(cp + 3)));
2621 printf("Unknown Header(%d)\n", nh);
2625 if ((cp += hlen) >= end)
2640 fill(char *bp, char *patp)
2646 for (cp = patp; *cp; cp++)
2648 errx(1, "patterns must be specified as hex digits");
2650 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2651 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2652 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2653 &pat[13], &pat[14], &pat[15]);
2658 (size_t)kk <= MAXDATALEN - 8 + sizeof(struct tv32) + ii;
2660 for (jj = 0; jj < ii; ++jj)
2661 bp[jj + kk] = pat[jj];
2662 if (!(options & F_QUIET)) {
2663 (void)printf("PATTERN: 0x");
2664 for (jj = 0; jj < ii; ++jj)
2665 (void)printf("%02x", bp[jj] & 0xFF);
2671 #ifdef IPSEC_POLICY_IPSEC
2673 setpolicy(int so __unused, char *policy)
2678 return 0; /* ignore */
2680 buf = ipsec_set_policy(policy, strlen(policy));
2682 errx(1, "%s", ipsec_strerror());
2683 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2684 ipsec_get_policylen(buf)) < 0)
2685 warnx("Unable to set IPsec policy");
2694 nigroup(char *name, int nig_oldmcprefix)
2699 u_int8_t digest[16];
2702 char hbuf[NI_MAXHOST];
2703 struct in6_addr in6;
2706 p = strchr(name, '.');
2708 p = name + strlen(name);
2710 if (l > 63 || l > sizeof(hbuf) - 1)
2711 return NULL; /*label too long*/
2712 strncpy(hbuf, name, l);
2713 hbuf[(int)l] = '\0';
2715 for (q = name; *q; q++) {
2716 if (isupper(*(unsigned char *)q))
2717 *q = tolower(*(unsigned char *)q);
2720 /* generate 16 bytes of pseudo-random value. */
2721 memset(&ctxt, 0, sizeof(ctxt));
2724 MD5Update(&ctxt, &c, sizeof(c));
2725 MD5Update(&ctxt, (unsigned char *)name, l);
2726 MD5Final(digest, &ctxt);
2728 if (nig_oldmcprefix) {
2729 /* draft-ietf-ipngwg-icmp-name-lookup */
2730 valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
2733 valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
2736 return NULL; /*XXX*/
2738 if (nig_oldmcprefix) {
2739 /* draft-ietf-ipngwg-icmp-name-lookup */
2740 bcopy(digest, &in6.s6_addr[12], 4);
2743 bcopy(digest, &in6.s6_addr[13], 3);
2746 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2749 return strdup(hbuf);
2755 (void)fprintf(stderr,
2756 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2761 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2765 #ifdef IPV6_USE_MIN_MTU
2769 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2770 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2771 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2775 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2777 " [-X timeout] [hops ...] host\n");