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 * 4. 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;
519 options |= F_SRCADDR;
521 case 's': /* size of packet to send */
522 datalen = strtol(optarg, &e, 10);
523 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
524 errx(1, "illegal datalen value -- %s", optarg);
525 if (datalen > MAXDATALEN) {
527 "datalen value too large, maximum is %d",
532 options &= ~F_NOUSERDATA;
533 options |= F_SUPTYPES;
536 options |= F_VERBOSE;
539 options &= ~F_NOUSERDATA;
543 options &= ~F_NOUSERDATA;
544 options |= F_FQDNOLD;
547 t = strtod(optarg, &e);
548 if (*e || e == optarg || t > (double)INT_MAX)
549 err(EX_USAGE, "invalid timing interval: `%s'",
551 options |= F_WAITTIME;
555 alarmtimeout = strtoul(optarg, &e, 0);
556 if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
557 errx(EX_USAGE, "invalid timeout: `%s'",
559 if (alarmtimeout > MAXALARM)
560 errx(EX_USAGE, "invalid timeout: `%s' > %d",
562 alarm((int)alarmtimeout);
565 #ifdef IPSEC_POLICY_IPSEC
568 if (!strncmp("in", optarg, 2)) {
569 if ((policy_in = strdup(optarg)) == NULL)
571 } else if (!strncmp("out", optarg, 3)) {
572 if ((policy_out = strdup(optarg)) == NULL)
575 errx(1, "invalid security policy");
579 options |= F_AUTHHDR;
582 options |= F_ENCRYPT;
584 #endif /*IPSEC_POLICY_IPSEC*/
601 #ifdef IPV6_RECVRTHDR /* 2292bis */
602 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
605 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
608 errx(1, "too many intermediate hops");
614 if (options & F_NIGROUP) {
615 target = nigroup(argv[argc - 1], nig_oldmcprefix);
616 if (target == NULL) {
621 target = argv[argc - 1];
624 memset(&hints, 0, sizeof(struct addrinfo));
625 hints.ai_flags = AI_CANONNAME;
626 hints.ai_family = AF_INET6;
627 hints.ai_socktype = SOCK_RAW;
628 hints.ai_protocol = IPPROTO_ICMPV6;
630 error = getaddrinfo(target, NULL, &hints, &res);
632 errx(1, "%s", gai_strerror(error));
633 if (res->ai_canonname)
634 hostname = res->ai_canonname;
639 errx(1, "getaddrinfo failed");
641 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
643 if ((s = socket(res->ai_family, res->ai_socktype,
644 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)) == 0)
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)) == 0)
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);
1217 exit(nreceived == 0 ? 2 : 0);
1239 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1240 * will be added on by the kernel. The ID field is our UNIX process ID,
1241 * and the sequence number is an ascending integer. The first 8 bytes
1242 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1243 * byte-order, to compute the round-trip time.
1250 if (options & F_FQDN)
1251 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1252 else if (options & F_FQDNOLD)
1254 else if (options & F_NODEADDR)
1255 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1256 else if (options & F_SUPTYPES)
1259 l = ICMP6ECHOLEN + datalen;
1267 struct icmp6_hdr *icp;
1268 struct iovec iov[2];
1270 struct icmp6_nodeinfo *nip;
1273 if (npackets && ntransmitted >= npackets)
1274 return(-1); /* no more transmission */
1276 icp = (struct icmp6_hdr *)outpack;
1277 nip = (struct icmp6_nodeinfo *)outpack;
1278 memset(icp, 0, sizeof(*icp));
1279 icp->icmp6_cksum = 0;
1280 seq = ntransmitted++;
1281 CLR(seq % mx_dup_ck);
1283 if (options & F_FQDN) {
1284 icp->icmp6_type = ICMP6_NI_QUERY;
1285 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1286 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1287 nip->ni_flags = htons(0);
1289 memcpy(nip->icmp6_ni_nonce, nonce,
1290 sizeof(nip->icmp6_ni_nonce));
1291 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1293 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1294 sizeof(dst.sin6_addr));
1295 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1297 } else if (options & F_FQDNOLD) {
1298 /* packet format in 03 draft - no Subject data on queries */
1299 icp->icmp6_type = ICMP6_NI_QUERY;
1300 icp->icmp6_code = 0; /* code field is always 0 */
1301 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1302 nip->ni_flags = htons(0);
1304 memcpy(nip->icmp6_ni_nonce, nonce,
1305 sizeof(nip->icmp6_ni_nonce));
1306 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1310 } else if (options & F_NODEADDR) {
1311 icp->icmp6_type = ICMP6_NI_QUERY;
1312 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1313 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1314 nip->ni_flags = naflags;
1316 memcpy(nip->icmp6_ni_nonce, nonce,
1317 sizeof(nip->icmp6_ni_nonce));
1318 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1320 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1321 sizeof(dst.sin6_addr));
1322 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1324 } else if (options & F_SUPTYPES) {
1325 icp->icmp6_type = ICMP6_NI_QUERY;
1326 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1327 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1328 /* we support compressed bitmap */
1329 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1331 memcpy(nip->icmp6_ni_nonce, nonce,
1332 sizeof(nip->icmp6_ni_nonce));
1333 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1337 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1338 icp->icmp6_code = 0;
1339 icp->icmp6_id = htons(ident);
1340 icp->icmp6_seq = ntohs(seq);
1344 (void)gettimeofday(&tv, NULL);
1345 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1346 tv32->tv32_sec = htonl(tv.tv_sec);
1347 tv32->tv32_usec = htonl(tv.tv_usec);
1349 cc = ICMP6ECHOLEN + datalen;
1353 if (pingerlen() != cc)
1354 errx(1, "internal error; length mismatch");
1357 smsghdr.msg_name = (caddr_t)&dst;
1358 smsghdr.msg_namelen = sizeof(dst);
1359 memset(&iov, 0, sizeof(iov));
1360 iov[0].iov_base = (caddr_t)outpack;
1361 iov[0].iov_len = cc;
1362 smsghdr.msg_iov = iov;
1363 smsghdr.msg_iovlen = 1;
1365 i = sendmsg(s, &smsghdr, 0);
1367 if (i < 0 || i != cc) {
1370 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1373 if (!(options & F_QUIET) && options & F_FLOOD)
1374 (void)write(STDOUT_FILENO, &DOT, 1);
1380 myechoreply(const struct icmp6_hdr *icp)
1382 if (ntohs(icp->icmp6_id) == ident)
1389 mynireply(const struct icmp6_nodeinfo *nip)
1391 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1392 nonce + sizeof(u_int16_t),
1393 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1400 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1402 /*base for compressed name*/
1406 char cresult[MAXDNAME + 1];
1417 if (i == 0 || cp != *sp) {
1418 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1419 return NULL; /*result overrun*/
1425 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1426 /* DNS compression */
1430 comp = base + (i & 0x3f);
1431 if (dnsdecode(&comp, cp, base, cresult,
1432 sizeof(cresult)) == NULL)
1434 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1435 return NULL; /*result overrun*/
1437 } else if ((i & 0x3f) == i) {
1439 return NULL; /*source overrun*/
1440 while (i-- > 0 && cp < ep) {
1441 l = snprintf(cresult, sizeof(cresult),
1442 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1443 if ((size_t)l >= sizeof(cresult) || l < 0)
1445 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1446 return NULL; /*result overrun*/
1450 return NULL; /*invalid label*/
1453 return NULL; /*not terminated*/
1461 * Print out the packet, if it came from us. This logic is necessary
1462 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1463 * which arrive ('tis only fair). This permits multiple copies of this
1464 * program to be run without having intermingled output (or statistics!).
1467 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1469 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1470 struct icmp6_hdr *icp;
1471 struct icmp6_nodeinfo *ni;
1474 struct sockaddr *from;
1476 u_char *cp = NULL, *dp, *end = buf + cc;
1477 struct in6_pktinfo *pktinfo = NULL;
1478 struct timeval tv, tp;
1480 double triptime = 0;
1485 char dnsname[MAXDNAME + 1];
1487 (void)gettimeofday(&tv, NULL);
1489 if (!mhdr || !mhdr->msg_name ||
1490 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1491 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1492 if (options & F_VERBOSE)
1493 warnx("invalid peername");
1496 from = (struct sockaddr *)mhdr->msg_name;
1497 fromlen = mhdr->msg_namelen;
1498 if (cc < (int)sizeof(struct icmp6_hdr)) {
1499 if (options & F_VERBOSE)
1500 warnx("packet too short (%d bytes) from %s", cc,
1501 pr_addr(from, fromlen));
1504 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1505 (options & F_VERBOSE) != 0)
1506 warnx("some control data discarded, insufficient buffer size");
1507 icp = (struct icmp6_hdr *)buf;
1508 ni = (struct icmp6_nodeinfo *)buf;
1511 if ((hoplim = get_hoplim(mhdr)) == -1) {
1512 warnx("failed to get receiving hop limit");
1515 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1516 warnx("failed to get receiving packet information");
1520 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1521 seq = ntohs(icp->icmp6_seq);
1524 tpp = (struct tv32 *)(icp + 1);
1525 tp.tv_sec = ntohl(tpp->tv32_sec);
1526 tp.tv_usec = ntohl(tpp->tv32_usec);
1528 triptime = ((double)tv.tv_sec) * 1000.0 +
1529 ((double)tv.tv_usec) / 1000.0;
1531 tsumsq += triptime * triptime;
1532 if (triptime < tmin)
1534 if (triptime > tmax)
1538 if (TST(seq % mx_dup_ck)) {
1543 SET(seq % mx_dup_ck);
1547 if (options & F_QUIET)
1550 if (options & F_WAITTIME && triptime > waittime) {
1555 if (options & F_FLOOD)
1556 (void)write(STDOUT_FILENO, &BSPACE, 1);
1558 if (options & F_AUDIBLE)
1559 (void)write(STDOUT_FILENO, &BBELL, 1);
1560 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1561 pr_addr(from, fromlen), seq);
1562 (void)printf(" hlim=%d", hoplim);
1563 if ((options & F_VERBOSE) != 0) {
1564 struct sockaddr_in6 dstsa;
1566 memset(&dstsa, 0, sizeof(dstsa));
1567 dstsa.sin6_family = AF_INET6;
1568 dstsa.sin6_len = sizeof(dstsa);
1569 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1570 dstsa.sin6_addr = pktinfo->ipi6_addr;
1571 (void)printf(" dst=%s",
1572 pr_addr((struct sockaddr *)&dstsa,
1576 (void)printf(" time=%.3f ms", triptime);
1578 (void)printf("(DUP!)");
1579 /* check the data */
1580 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1581 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1582 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1584 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1589 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1590 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1592 if (TST(seq % mx_dup_ck)) {
1597 SET(seq % mx_dup_ck);
1601 if (options & F_QUIET)
1604 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1606 switch (ntohs(ni->ni_code)) {
1607 case ICMP6_NI_SUCCESS:
1609 case ICMP6_NI_REFUSED:
1610 printf("refused, type 0x%x", ntohs(ni->ni_type));
1612 case ICMP6_NI_UNKNOWN:
1613 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1616 printf("unknown code 0x%x, type 0x%x",
1617 ntohs(ni->ni_code), ntohs(ni->ni_type));
1621 switch (ntohs(ni->ni_qtype)) {
1623 printf("NodeInfo NOOP");
1625 case NI_QTYPE_SUPTYPES:
1626 pr_suptypes(ni, end - (u_char *)ni);
1628 case NI_QTYPE_NODEADDR:
1629 pr_nodeaddr(ni, end - (u_char *)ni);
1632 default: /* XXX: for backward compatibility */
1633 cp = (u_char *)ni + ICMP6_NIRLEN;
1634 if (buf[off + ICMP6_NIRLEN] ==
1635 cc - off - ICMP6_NIRLEN - 1)
1640 cp++; /* skip length */
1642 safeputc(*cp & 0xff);
1648 if (dnsdecode((const u_char **)&cp, end,
1649 (const u_char *)(ni + 1), dnsname,
1650 sizeof(dnsname)) == NULL) {
1655 * name-lookup special handling for
1658 if (cp + 1 <= end && !*cp &&
1659 strlen(dnsname) > 0) {
1660 dnsname[strlen(dnsname) - 1] = '\0';
1663 printf("%s%s", i > 0 ? "," : "",
1667 if (options & F_VERBOSE) {
1671 (void)printf(" ("); /*)*/
1673 switch (ni->ni_code) {
1674 case ICMP6_NI_REFUSED:
1675 (void)printf("refused");
1678 case ICMP6_NI_UNKNOWN:
1679 (void)printf("unknown qtype");
1684 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1685 /* case of refusion, unknown */
1690 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1693 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1694 (void)printf("TTL=%d:meaningless",
1698 (void)printf("TTL=%d:invalid",
1701 (void)printf("TTL=%d", ttl);
1711 cp = (u_char *)ni + ICMP6_NIRLEN;
1720 if (buf[off + ICMP6_NIRLEN] !=
1721 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1724 (void)printf("invalid namelen:%d/%lu",
1725 buf[off + ICMP6_NIRLEN],
1726 (u_long)cc - off - ICMP6_NIRLEN - 1);
1736 /* We've got something other than an ECHOREPLY */
1737 if (!(options & F_VERBOSE))
1739 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1743 if (!(options & F_FLOOD)) {
1744 (void)putchar('\n');
1745 if (options & F_VERBOSE)
1747 (void)fflush(stdout);
1753 pr_exthdrs(struct msghdr *mhdr)
1760 bufp = mhdr->msg_control;
1761 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1762 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1763 if (cm->cmsg_level != IPPROTO_IPV6)
1766 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1769 switch (cm->cmsg_type) {
1771 printf(" HbH Options: ");
1772 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1775 #ifdef IPV6_RTHDRDSTOPTS
1776 case IPV6_RTHDRDSTOPTS:
1778 printf(" Dst Options: ");
1779 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1782 printf(" Routing: ");
1783 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1789 #ifdef USE_RFC2292BIS
1791 pr_ip6opt(void *extbuf, size_t bufsize)
1793 struct ip6_hbh *ext;
1796 socklen_t extlen, len;
1802 ext = (struct ip6_hbh *)extbuf;
1803 extlen = (ext->ip6h_len + 1) * 8;
1804 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1805 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1808 * Bounds checking on the ancillary data buffer:
1809 * subtract the size of a cmsg structure from the buffer size.
1811 if (bufsize < (extlen + CMSG_SPACE(0))) {
1812 extlen = bufsize - CMSG_SPACE(0);
1813 warnx("options truncated, showing only %u (total=%u)",
1814 (unsigned int)(extlen / 8 - 1),
1815 (unsigned int)(ext->ip6h_len));
1820 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1821 &type, &len, &databuf);
1822 if (currentlen == -1)
1826 * Note that inet6_opt_next automatically skips any padding
1831 offset = inet6_opt_get_val(databuf, offset,
1832 &value4, sizeof(value4));
1833 printf(" Jumbo Payload Opt: Length %u\n",
1834 (u_int32_t)ntohl(value4));
1836 case IP6OPT_ROUTER_ALERT:
1838 offset = inet6_opt_get_val(databuf, offset,
1839 &value2, sizeof(value2));
1840 printf(" Router Alert Opt: Type %u\n",
1844 printf(" Received Opt %u len %lu\n",
1845 type, (unsigned long)len);
1851 #else /* !USE_RFC2292BIS */
1854 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1859 #endif /* USE_RFC2292BIS */
1861 #ifdef USE_RFC2292BIS
1863 pr_rthdr(void *extbuf, size_t bufsize)
1865 struct in6_addr *in6;
1866 char ntopbuf[INET6_ADDRSTRLEN];
1867 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1868 int i, segments, origsegs, rthsize, size0, size1;
1870 /* print fixed part of the header */
1871 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1872 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1873 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1874 printf("%d segments, ", segments);
1875 printf("%d left\n", rh->ip6r_segleft);
1877 printf("segments unknown, ");
1878 printf("%d left\n", rh->ip6r_segleft);
1883 * Bounds checking on the ancillary data buffer. When calculating
1884 * the number of items to show keep in mind:
1885 * - The size of the cmsg structure
1886 * - The size of one segment (the size of a Type 0 routing header)
1887 * - When dividing add a fudge factor of one in case the
1888 * dividend is not evenly divisible by the divisor
1890 rthsize = (rh->ip6r_len + 1) * 8;
1891 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1892 origsegs = segments;
1893 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1894 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1895 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1896 (size1 - size0) + 1;
1897 warnx("segments truncated, showing only %d (total=%d)",
1898 segments, origsegs);
1901 for (i = 0; i < segments; i++) {
1902 in6 = inet6_rth_getaddr(extbuf, i);
1904 printf(" [%d]<NULL>\n", i);
1906 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1908 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1909 printf(" [%d]%s\n", i, ntopbuf);
1917 #else /* !USE_RFC2292BIS */
1920 pr_rthdr(void *extbuf, size_t bufsize __unused)
1925 #endif /* USE_RFC2292BIS */
1928 pr_bitrange(u_int32_t v, int soff, int ii)
1935 /* shift till we have 0x01 */
1936 if ((v & 0x01) == 0) {
1938 printf("-%u", soff + off - 1);
1949 case 0x04: case 0x0c:
1960 /* we have 0x01 with us */
1961 for (i = 0; i < 32 - off; i++) {
1962 if ((v & (0x01 << i)) == 0)
1966 printf(" %u", soff + off);
1974 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1975 /* ni->qtype must be SUPTYPES */
1979 const u_char *cp, *end;
1982 u_int16_t words; /*32bit count*/
1985 #define MAXQTYPES (1 << 16)
1989 cp = (u_char *)(ni + 1);
1990 end = ((u_char *)ni) + nilen;
1994 printf("NodeInfo Supported Qtypes");
1995 if (options & F_VERBOSE) {
1996 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1997 printf(", compressed bitmap");
1999 printf(", raw bitmap");
2003 clen = (size_t)(end - cp);
2004 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2005 if (clen == 0 || clen > MAXQTYPES / 8 ||
2011 if (clen < sizeof(cbit) || clen % sizeof(v))
2013 memcpy(&cbit, cp, sizeof(cbit));
2014 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2018 clen = ntohs(cbit.words) * sizeof(v);
2019 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2024 for (off = 0; off < clen; off += sizeof(v)) {
2025 memcpy(&v, cp + off, sizeof(v));
2026 v = (u_int32_t)ntohl(v);
2027 b = pr_bitrange(v, (int)(cur + off * 8), b);
2029 /* flush the remaining bits */
2030 b = pr_bitrange(0, (int)(cur + off * 8), b);
2034 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2035 cur += ntohs(cbit.skip) * 32;
2040 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
2041 /* ni->qtype must be NODEADDR */
2043 u_char *cp = (u_char *)(ni + 1);
2044 char ntop_buf[INET6_ADDRSTRLEN];
2047 nilen -= sizeof(struct icmp6_nodeinfo);
2049 if (options & F_VERBOSE) {
2050 switch (ni->ni_code) {
2051 case ICMP6_NI_REFUSED:
2052 (void)printf("refused");
2054 case ICMP6_NI_UNKNOWN:
2055 (void)printf("unknown qtype");
2058 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2059 (void)printf(" truncated");
2063 printf(" no address\n");
2066 * In icmp-name-lookups 05 and later, TTL of each returned address
2067 * is contained in the resposne. We try to detect the version
2068 * by the length of the data, but note that the detection algorithm
2069 * is incomplete. We assume the latest draft by default.
2071 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2077 /* XXX: alignment? */
2078 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2079 cp += sizeof(u_int32_t);
2080 nilen -= sizeof(u_int32_t);
2083 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2085 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2086 printf(" %s", ntop_buf);
2088 if (ttl == 0xffffffff) {
2090 * XXX: can this convention be applied to all
2091 * type of TTL (i.e. non-ND TTL)?
2093 printf("(TTL=infty)");
2096 printf("(TTL=%u)", ttl);
2100 nilen -= sizeof(struct in6_addr);
2101 cp += sizeof(struct in6_addr);
2106 get_hoplim(struct msghdr *mhdr)
2110 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2111 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2112 if (cm->cmsg_len == 0)
2115 if (cm->cmsg_level == IPPROTO_IPV6 &&
2116 cm->cmsg_type == IPV6_HOPLIMIT &&
2117 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2118 return(*(int *)CMSG_DATA(cm));
2124 static struct in6_pktinfo *
2125 get_rcvpktinfo(struct msghdr *mhdr)
2129 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2130 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2131 if (cm->cmsg_len == 0)
2134 if (cm->cmsg_level == IPPROTO_IPV6 &&
2135 cm->cmsg_type == IPV6_PKTINFO &&
2136 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2137 return((struct in6_pktinfo *)CMSG_DATA(cm));
2144 get_pathmtu(struct msghdr *mhdr)
2146 #ifdef IPV6_RECVPATHMTU
2148 struct ip6_mtuinfo *mtuctl = NULL;
2150 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2151 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2152 if (cm->cmsg_len == 0)
2155 if (cm->cmsg_level == IPPROTO_IPV6 &&
2156 cm->cmsg_type == IPV6_PATHMTU &&
2157 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2158 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2161 * If the notified destination is different from
2162 * the one we are pinging, just ignore the info.
2163 * We check the scope ID only when both notified value
2164 * and our own value have non-0 values, because we may
2165 * have used the default scope zone ID for sending,
2166 * in which case the scope ID value is 0.
2168 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2170 (mtuctl->ip6m_addr.sin6_scope_id &&
2171 dst.sin6_scope_id &&
2172 mtuctl->ip6m_addr.sin6_scope_id !=
2173 dst.sin6_scope_id)) {
2174 if ((options & F_VERBOSE) != 0) {
2175 printf("path MTU for %s is notified. "
2177 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2178 sizeof(mtuctl->ip6m_addr)));
2184 * Ignore an invalid MTU. XXX: can we just believe
2187 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2190 /* notification for our destination. return the MTU. */
2191 return((int)mtuctl->ip6m_mtu);
2200 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2204 tvsub(struct timeval *out, struct timeval *in)
2206 if ((out->tv_usec -= in->tv_usec) < 0) {
2208 out->tv_usec += 1000000;
2210 out->tv_sec -= in->tv_sec;
2219 onint(int notused __unused)
2222 * When doing reverse DNS lookups, the seenint flag might not
2223 * be noticed for a while. Just exit if we get a second SIGINT.
2225 if ((options & F_HOSTNAME) && seenint != 0)
2226 _exit(nreceived ? 0 : 2);
2231 * Print out statistics.
2237 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2238 (void)printf("%ld packets transmitted, ", ntransmitted);
2239 (void)printf("%ld packets received, ", nreceived);
2241 (void)printf("+%ld duplicates, ", nrepeats);
2243 if (nreceived > ntransmitted)
2244 (void)printf("-- somebody's duplicating packets!");
2246 (void)printf("%.1f%% packet loss",
2247 ((((double)ntransmitted - nreceived) * 100.0) /
2251 printf(", %ld packets out of wait time", nrcvtimeout);
2252 (void)putchar('\n');
2253 if (nreceived && timing) {
2254 /* Only display average to microseconds */
2255 double num = nreceived + nrepeats;
2256 double avg = tsum / num;
2257 double dev = sqrt(tsumsq / num - avg * avg);
2259 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2260 tmin, avg, tmax, dev);
2261 (void)fflush(stdout);
2263 (void)fflush(stdout);
2267 static const char *niqcode[] = {
2269 "DNS label", /*or empty*/
2274 static const char *nircode[] = {
2275 "Success", "Refused", "Unknown",
2281 * Print a descriptive string about an ICMP header.
2284 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2286 char ntop_buf[INET6_ADDRSTRLEN];
2287 struct nd_redirect *red;
2288 struct icmp6_nodeinfo *ni;
2289 char dnsname[MAXDNAME + 1];
2293 switch (icp->icmp6_type) {
2294 case ICMP6_DST_UNREACH:
2295 switch (icp->icmp6_code) {
2296 case ICMP6_DST_UNREACH_NOROUTE:
2297 (void)printf("No Route to Destination\n");
2299 case ICMP6_DST_UNREACH_ADMIN:
2300 (void)printf("Destination Administratively "
2303 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2304 (void)printf("Destination Unreachable Beyond Scope\n");
2306 case ICMP6_DST_UNREACH_ADDR:
2307 (void)printf("Destination Host Unreachable\n");
2309 case ICMP6_DST_UNREACH_NOPORT:
2310 (void)printf("Destination Port Unreachable\n");
2313 (void)printf("Destination Unreachable, Bad Code: %d\n",
2317 /* Print returned IP header information */
2318 pr_retip((struct ip6_hdr *)(icp + 1), end);
2320 case ICMP6_PACKET_TOO_BIG:
2321 (void)printf("Packet too big mtu = %d\n",
2322 (int)ntohl(icp->icmp6_mtu));
2323 pr_retip((struct ip6_hdr *)(icp + 1), end);
2325 case ICMP6_TIME_EXCEEDED:
2326 switch (icp->icmp6_code) {
2327 case ICMP6_TIME_EXCEED_TRANSIT:
2328 (void)printf("Time to live exceeded\n");
2330 case ICMP6_TIME_EXCEED_REASSEMBLY:
2331 (void)printf("Frag reassembly time exceeded\n");
2334 (void)printf("Time exceeded, Bad Code: %d\n",
2338 pr_retip((struct ip6_hdr *)(icp + 1), end);
2340 case ICMP6_PARAM_PROB:
2341 (void)printf("Parameter problem: ");
2342 switch (icp->icmp6_code) {
2343 case ICMP6_PARAMPROB_HEADER:
2344 (void)printf("Erroneous Header ");
2346 case ICMP6_PARAMPROB_NEXTHEADER:
2347 (void)printf("Unknown Nextheader ");
2349 case ICMP6_PARAMPROB_OPTION:
2350 (void)printf("Unrecognized Option ");
2353 (void)printf("Bad code(%d) ", icp->icmp6_code);
2356 (void)printf("pointer = 0x%02x\n",
2357 (u_int32_t)ntohl(icp->icmp6_pptr));
2358 pr_retip((struct ip6_hdr *)(icp + 1), end);
2360 case ICMP6_ECHO_REQUEST:
2361 (void)printf("Echo Request");
2362 /* XXX ID + Seq + Data */
2364 case ICMP6_ECHO_REPLY:
2365 (void)printf("Echo Reply");
2366 /* XXX ID + Seq + Data */
2368 case ICMP6_MEMBERSHIP_QUERY:
2369 (void)printf("Listener Query");
2371 case ICMP6_MEMBERSHIP_REPORT:
2372 (void)printf("Listener Report");
2374 case ICMP6_MEMBERSHIP_REDUCTION:
2375 (void)printf("Listener Done");
2377 case ND_ROUTER_SOLICIT:
2378 (void)printf("Router Solicitation");
2380 case ND_ROUTER_ADVERT:
2381 (void)printf("Router Advertisement");
2383 case ND_NEIGHBOR_SOLICIT:
2384 (void)printf("Neighbor Solicitation");
2386 case ND_NEIGHBOR_ADVERT:
2387 (void)printf("Neighbor Advertisement");
2390 red = (struct nd_redirect *)icp;
2391 (void)printf("Redirect\n");
2392 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2394 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2395 (void)printf("Destination: %s", ntop_buf);
2396 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2398 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2399 (void)printf(" New Target: %s", ntop_buf);
2401 case ICMP6_NI_QUERY:
2402 (void)printf("Node Information Query");
2403 /* XXX ID + Seq + Data */
2404 ni = (struct icmp6_nodeinfo *)icp;
2405 l = end - (u_char *)(ni + 1);
2407 switch (ntohs(ni->ni_qtype)) {
2409 (void)printf("NOOP");
2411 case NI_QTYPE_SUPTYPES:
2412 (void)printf("Supported qtypes");
2415 (void)printf("DNS name");
2417 case NI_QTYPE_NODEADDR:
2418 (void)printf("nodeaddr");
2420 case NI_QTYPE_IPV4ADDR:
2421 (void)printf("IPv4 nodeaddr");
2424 (void)printf("unknown qtype");
2427 if (options & F_VERBOSE) {
2428 switch (ni->ni_code) {
2429 case ICMP6_NI_SUBJ_IPV6:
2430 if (l == sizeof(struct in6_addr) &&
2431 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2432 sizeof(ntop_buf)) != NULL) {
2433 (void)printf(", subject=%s(%s)",
2434 niqcode[ni->ni_code], ntop_buf);
2437 /* backward compat to -W */
2438 (void)printf(", oldfqdn");
2440 (void)printf(", invalid");
2444 case ICMP6_NI_SUBJ_FQDN:
2445 if (end == (u_char *)(ni + 1)) {
2446 (void)printf(", no subject");
2449 printf(", subject=%s", niqcode[ni->ni_code]);
2450 cp = (const u_char *)(ni + 1);
2451 if (dnsdecode(&cp, end, NULL, dnsname,
2452 sizeof(dnsname)) != NULL)
2453 printf("(%s)", dnsname);
2455 printf("(invalid)");
2457 case ICMP6_NI_SUBJ_IPV4:
2458 if (l == sizeof(struct in_addr) &&
2459 inet_ntop(AF_INET, ni + 1, ntop_buf,
2460 sizeof(ntop_buf)) != NULL) {
2461 (void)printf(", subject=%s(%s)",
2462 niqcode[ni->ni_code], ntop_buf);
2464 (void)printf(", invalid");
2467 (void)printf(", invalid");
2472 case ICMP6_NI_REPLY:
2473 (void)printf("Node Information Reply");
2474 /* XXX ID + Seq + Data */
2475 ni = (struct icmp6_nodeinfo *)icp;
2477 switch (ntohs(ni->ni_qtype)) {
2479 (void)printf("NOOP");
2481 case NI_QTYPE_SUPTYPES:
2482 (void)printf("Supported qtypes");
2485 (void)printf("DNS name");
2487 case NI_QTYPE_NODEADDR:
2488 (void)printf("nodeaddr");
2490 case NI_QTYPE_IPV4ADDR:
2491 (void)printf("IPv4 nodeaddr");
2494 (void)printf("unknown qtype");
2497 if (options & F_VERBOSE) {
2498 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2499 printf(", invalid");
2501 printf(", %s", nircode[ni->ni_code]);
2505 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2511 * Print an IP6 header.
2514 pr_iph(struct ip6_hdr *ip6)
2516 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2518 char ntop_buf[INET6_ADDRSTRLEN];
2520 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2521 tc = (tc >> 4) & 0x0f;
2522 tc |= (ip6->ip6_vfc << 4);
2524 printf("Vr TC Flow Plen Nxt Hlim\n");
2525 printf(" %1x %02x %05x %04x %02x %02x\n",
2526 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2527 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2528 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2529 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2530 printf("%s->", ntop_buf);
2531 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2532 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2533 printf("%s\n", ntop_buf);
2538 * Return an ascii host address as a dotted quad and optionally with
2542 pr_addr(struct sockaddr *addr, int addrlen)
2544 static char buf[NI_MAXHOST];
2547 if ((options & F_HOSTNAME) == 0)
2548 flag |= NI_NUMERICHOST;
2550 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2558 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2561 pr_retip(struct ip6_hdr *ip6, u_char *end)
2563 u_char *cp = (u_char *)ip6, nh;
2566 if ((size_t)(end - (u_char *)ip6) < sizeof(*ip6)) {
2571 hlen = sizeof(*ip6);
2575 while (end - cp >= 8) {
2577 case IPPROTO_HOPOPTS:
2579 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2580 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2582 case IPPROTO_DSTOPTS:
2584 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2585 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2587 case IPPROTO_FRAGMENT:
2589 hlen = sizeof(struct ip6_frag);
2590 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2592 case IPPROTO_ROUTING:
2594 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2595 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2600 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2601 nh = ((struct ah *)cp)->ah_nxt;
2604 case IPPROTO_ICMPV6:
2605 printf("ICMP6: type = %d, code = %d\n",
2612 printf("TCP: from port %u, to port %u (decimal)\n",
2613 (*cp * 256 + *(cp + 1)),
2614 (*(cp + 2) * 256 + *(cp + 3)));
2617 printf("UDP: from port %u, to port %u (decimal)\n",
2618 (*cp * 256 + *(cp + 1)),
2619 (*(cp + 2) * 256 + *(cp + 3)));
2622 printf("Unknown Header(%d)\n", nh);
2626 if ((cp += hlen) >= end)
2641 fill(char *bp, char *patp)
2647 for (cp = patp; *cp; cp++)
2649 errx(1, "patterns must be specified as hex digits");
2651 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2652 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2653 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2654 &pat[13], &pat[14], &pat[15]);
2659 (size_t)kk <= MAXDATALEN - 8 + sizeof(struct tv32) + ii;
2661 for (jj = 0; jj < ii; ++jj)
2662 bp[jj + kk] = pat[jj];
2663 if (!(options & F_QUIET)) {
2664 (void)printf("PATTERN: 0x");
2665 for (jj = 0; jj < ii; ++jj)
2666 (void)printf("%02x", bp[jj] & 0xFF);
2672 #ifdef IPSEC_POLICY_IPSEC
2674 setpolicy(int so __unused, char *policy)
2679 return 0; /* ignore */
2681 buf = ipsec_set_policy(policy, strlen(policy));
2683 errx(1, "%s", ipsec_strerror());
2684 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2685 ipsec_get_policylen(buf)) < 0)
2686 warnx("Unable to set IPsec policy");
2695 nigroup(char *name, int nig_oldmcprefix)
2700 u_int8_t digest[16];
2703 char hbuf[NI_MAXHOST];
2704 struct in6_addr in6;
2707 p = strchr(name, '.');
2709 p = name + strlen(name);
2711 if (l > 63 || l > sizeof(hbuf) - 1)
2712 return NULL; /*label too long*/
2713 strncpy(hbuf, name, l);
2714 hbuf[(int)l] = '\0';
2716 for (q = name; *q; q++) {
2717 if (isupper(*(unsigned char *)q))
2718 *q = tolower(*(unsigned char *)q);
2721 /* generate 16 bytes of pseudo-random value. */
2722 memset(&ctxt, 0, sizeof(ctxt));
2725 MD5Update(&ctxt, &c, sizeof(c));
2726 MD5Update(&ctxt, (unsigned char *)name, l);
2727 MD5Final(digest, &ctxt);
2729 if (nig_oldmcprefix) {
2730 /* draft-ietf-ipngwg-icmp-name-lookup */
2731 valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
2734 valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
2737 return NULL; /*XXX*/
2739 if (nig_oldmcprefix) {
2740 /* draft-ietf-ipngwg-icmp-name-lookup */
2741 bcopy(digest, &in6.s6_addr[12], 4);
2744 bcopy(digest, &in6.s6_addr[13], 3);
2747 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2750 return strdup(hbuf);
2756 (void)fprintf(stderr,
2757 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2762 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2766 #ifdef IPV6_USE_MIN_MTU
2770 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2771 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2772 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2776 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2778 " [-X timeout] [hops ...] host\n");