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 int mx_dup_ck = MAX_DUP_CHK;
209 char rcvd_tbl[MAX_DUP_CHK / 8];
211 struct sockaddr_in6 dst; /* who to ping6 */
212 struct sockaddr_in6 src; /* src addr of this packet */
214 int datalen = DEFDATALEN;
215 int s; /* socket file descriptor */
216 u_char outpack[MAXPACKETLEN];
217 char BSPACE = '\b'; /* characters written for flood */
218 char BBELL = '\a'; /* characters written for AUDIBLE */
221 int ident; /* process id to identify our packets */
222 u_int8_t nonce[8]; /* nonce field for node information */
223 int hoplimit = -1; /* hoplimit */
224 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
225 u_char *packet = NULL;
228 long nmissedmax; /* max value of ntransmitted - nreceived - 1 */
229 long npackets; /* max packets to transmit */
230 long nreceived; /* # of packets we got back */
231 long nrepeats; /* number of duplicates */
232 long ntransmitted; /* sequence # for outbound packets = #sent */
233 int interval = 1000; /* interval between packets in ms */
234 int waittime = MAXWAIT; /* timeout for each packet */
235 long nrcvtimeout = 0; /* # of packets we got back after waittime */
238 int timing; /* flag to do timing */
239 double tmin = 999999999.0; /* minimum round trip time */
240 double tmax = 0.0; /* maximum round trip time */
241 double tsum = 0.0; /* sum of all times, for doing average */
242 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
244 /* for node addresses */
247 /* for ancillary data(advanced API) */
248 struct msghdr smsghdr;
249 struct iovec smsgiov;
252 volatile sig_atomic_t seenint;
254 volatile sig_atomic_t seeninfo;
257 int main(int, char *[]);
258 void fill(char *, char *);
259 int get_hoplim(struct msghdr *);
260 int get_pathmtu(struct msghdr *);
261 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
264 size_t pingerlen(void);
266 const char *pr_addr(struct sockaddr *, int);
267 void pr_icmph(struct icmp6_hdr *, u_char *);
268 void pr_iph(struct ip6_hdr *);
269 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
270 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
271 int myechoreply(const struct icmp6_hdr *);
272 int mynireply(const struct icmp6_nodeinfo *);
273 char *dnsdecode(const u_char **, const u_char *, const u_char *,
275 void pr_pack(u_char *, int, struct msghdr *);
276 void pr_exthdrs(struct msghdr *);
277 void pr_ip6opt(void *, size_t);
278 void pr_rthdr(void *, size_t);
279 int pr_bitrange(u_int32_t, int, int);
280 void pr_retip(struct ip6_hdr *, u_char *);
282 void tvsub(struct timeval *, struct timeval *);
283 int setpolicy(int, char *);
284 char *nigroup(char *, int);
288 main(int argc, char *argv[])
290 struct timeval last, intvl;
291 struct sockaddr_in6 from, *sin6;
292 struct addrinfo hints, *res;
293 struct sigaction si_sa;
295 int almost_done, ch, hold, packlen, preload, optval, error;
296 int nig_oldmcprefix = -1;
298 char *e, *target, *ifname = NULL, *gateway = NULL;
300 struct cmsghdr *scmsgp = NULL;
301 /* For control (ancillary) data received from recvmsg() */
302 struct cmsghdr cm[CONTROLLEN];
303 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
308 struct in6_pktinfo *pktinfo = NULL;
309 #ifdef USE_RFC2292BIS
310 struct ip6_rthdr *rthdr = NULL;
312 #ifdef IPSEC_POLICY_IPSEC
313 char *policy_in = NULL;
314 char *policy_out = NULL;
319 #ifdef IPV6_USE_MIN_MTU
323 /* just to be sure */
324 memset(&smsghdr, 0, sizeof(smsghdr));
325 memset(&smsgiov, 0, sizeof(smsgiov));
327 alarmtimeout = preload = 0;
328 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
332 #ifdef IPSEC_POLICY_IPSEC
336 #endif /*IPSEC_POLICY_IPSEC*/
338 while ((ch = getopt(argc, argv,
339 "a:b:c:DdfHg:h:I:i:l:mnNop:qrRS:s:tvwWx:X:" ADDOPTS)) != -1) {
346 options &= ~F_NOUSERDATA;
347 options |= F_NODEADDR;
348 for (cp = optarg; *cp != '\0'; cp++) {
351 naflags |= NI_NODEADDR_FLAG_ALL;
355 naflags |= NI_NODEADDR_FLAG_COMPAT;
359 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
363 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
367 naflags |= NI_NODEADDR_FLAG_GLOBAL;
369 case 'A': /* experimental. not in the spec */
370 #ifdef NI_NODEADDR_FLAG_ANYCAST
371 naflags |= NI_NODEADDR_FLAG_ANYCAST;
375 "-a A is not supported on the platform");
386 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
389 lsockbufsize = strtoul(optarg, &e, 10);
390 sockbufsize = lsockbufsize;
391 if (errno || !*optarg || *e ||
392 sockbufsize != lsockbufsize)
393 errx(1, "invalid socket buffer size");
396 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
400 npackets = strtol(optarg, &e, 10);
401 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
403 "illegal number of packets -- %s", optarg);
406 options |= F_DONTFRAG;
409 options |= F_SO_DEBUG;
414 errx(1, "Must be superuser to flood ping");
417 setbuf(stdout, (char *)NULL);
423 options |= F_HOSTNAME;
425 case 'h': /* hoplimit */
426 hoplimit = strtol(optarg, &e, 10);
427 if (*optarg == '\0' || *e != '\0')
428 errx(1, "illegal hoplimit %s", optarg);
429 if (255 < hoplimit || hoplimit < -1)
431 "illegal hoplimit -- %s", optarg);
435 options |= F_INTERFACE;
436 #ifndef USE_SIN6_SCOPE_ID
440 case 'i': /* wait between sending packets */
441 t = strtod(optarg, &e);
442 if (*optarg == '\0' || *e != '\0')
443 errx(1, "illegal timing interval %s", optarg);
444 if (t < 1 && getuid()) {
445 errx(1, "%s: only root may use interval < 1s",
448 intvl.tv_sec = (long)t;
450 (long)((t - intvl.tv_sec) * 1000000);
451 if (intvl.tv_sec < 0)
452 errx(1, "illegal timing interval %s", optarg);
453 /* less than 1/hz does not make sense */
454 if (intvl.tv_sec == 0 && intvl.tv_usec < 1) {
455 warnx("too small interval, raised to .000001");
458 options |= F_INTERVAL;
463 errx(1, "Must be superuser to preload");
465 preload = strtol(optarg, &e, 10);
466 if (preload < 0 || *optarg == '\0' || *e != '\0')
467 errx(1, "illegal preload value -- %s", optarg);
470 #ifdef IPV6_USE_MIN_MTU
474 errx(1, "-%c is not supported on this platform", ch);
478 options &= ~F_HOSTNAME;
481 options |= F_NIGROUP;
487 case 'p': /* fill buffer with user pattern */
488 options |= F_PINGFILLED;
489 fill((char *)datap, optarg);
495 options |= F_AUDIBLE;
501 memset(&hints, 0, sizeof(struct addrinfo));
502 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
503 hints.ai_family = AF_INET6;
504 hints.ai_socktype = SOCK_RAW;
505 hints.ai_protocol = IPPROTO_ICMPV6;
507 error = getaddrinfo(optarg, NULL, &hints, &res);
509 errx(1, "invalid source address: %s",
510 gai_strerror(error));
513 * res->ai_family must be AF_INET6 and res->ai_addrlen
514 * must be sizeof(src).
516 memcpy(&src, res->ai_addr, res->ai_addrlen);
517 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 = 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)
648 /* set the source address if specified. */
649 if ((options & F_SRCADDR) &&
650 bind(s, (struct sockaddr *)&src, srclen) != 0) {
654 /* set the gateway (next hop) if specified */
656 memset(&hints, 0, sizeof(hints));
657 hints.ai_family = AF_INET6;
658 hints.ai_socktype = SOCK_RAW;
659 hints.ai_protocol = IPPROTO_ICMPV6;
661 error = getaddrinfo(gateway, NULL, &hints, &res);
663 errx(1, "getaddrinfo for the gateway %s: %s",
664 gateway, gai_strerror(error));
666 if (res->ai_next && (options & F_VERBOSE))
667 warnx("gateway resolves to multiple addresses");
669 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
670 res->ai_addr, res->ai_addrlen)) {
671 err(1, "setsockopt(IPV6_NEXTHOP)");
678 * let the kerel pass extension headers of incoming packets,
679 * for privileged socket options
681 if ((options & F_VERBOSE) != 0) {
684 #ifdef IPV6_RECVHOPOPTS
685 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
687 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
688 #else /* old adv. API */
689 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
691 err(1, "setsockopt(IPV6_HOPOPTS)");
693 #ifdef IPV6_RECVDSTOPTS
694 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
696 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
697 #else /* old adv. API */
698 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
700 err(1, "setsockopt(IPV6_DSTOPTS)");
702 #ifdef IPV6_RECVRTHDRDSTOPTS
703 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
705 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
709 /* revoke root privilege */
710 if (seteuid(getuid()) != 0)
711 err(1, "seteuid() failed");
712 if (setuid(getuid()) != 0)
713 err(1, "setuid() failed");
715 if ((options & F_FLOOD) && (options & F_INTERVAL))
716 errx(1, "-f and -i incompatible options");
718 if ((options & F_NOUSERDATA) == 0) {
719 if (datalen >= sizeof(struct tv32)) {
720 /* we can time transfer */
724 /* in F_VERBOSE case, we may get non-echoreply packets*/
725 if (options & F_VERBOSE)
726 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
728 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
730 /* suppress timing for node information query */
733 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
736 if (!(packet = (u_char *)malloc((u_int)packlen)))
737 err(1, "Unable to allocate packet");
738 if (!(options & F_PINGFILLED))
739 for (i = ICMP6ECHOLEN; i < packlen; ++i)
742 ident = getpid() & 0xFFFF;
743 arc4random_buf(nonce, sizeof(nonce));
745 if (options & F_DONTFRAG)
746 if (setsockopt(s, IPPROTO_IPV6, IPV6_DONTFRAG,
747 &optval, sizeof(optval)) == -1)
748 err(1, "IPV6_DONTFRAG");
751 if (options & F_SO_DEBUG)
752 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
754 optval = IPV6_DEFHLIM;
755 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
756 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
757 &optval, sizeof(optval)) == -1)
758 err(1, "IPV6_MULTICAST_HOPS");
759 #ifdef IPV6_USE_MIN_MTU
761 optval = mflag > 1 ? 0 : 1;
763 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
764 &optval, sizeof(optval)) == -1)
765 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
767 #ifdef IPV6_RECVPATHMTU
770 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
771 &optval, sizeof(optval)) == -1)
772 err(1, "setsockopt(IPV6_RECVPATHMTU)");
774 #endif /* IPV6_RECVPATHMTU */
775 #endif /* IPV6_USE_MIN_MTU */
778 #ifdef IPSEC_POLICY_IPSEC
779 if (options & F_POLICY) {
780 if (setpolicy(s, policy_in) < 0)
781 errx(1, "%s", ipsec_strerror());
782 if (setpolicy(s, policy_out) < 0)
783 errx(1, "%s", ipsec_strerror());
786 if (options & F_AUTHHDR) {
787 optval = IPSEC_LEVEL_REQUIRE;
788 #ifdef IPV6_AUTH_TRANS_LEVEL
789 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
790 &optval, sizeof(optval)) == -1)
791 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
793 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
794 &optval, sizeof(optval)) == -1)
795 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
798 if (options & F_ENCRYPT) {
799 optval = IPSEC_LEVEL_REQUIRE;
800 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
801 &optval, sizeof(optval)) == -1)
802 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
804 #endif /*IPSEC_POLICY_IPSEC*/
809 struct icmp6_filter filt;
810 if (!(options & F_VERBOSE)) {
811 ICMP6_FILTER_SETBLOCKALL(&filt);
812 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
813 (options & F_NODEADDR) || (options & F_SUPTYPES))
814 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
816 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
818 ICMP6_FILTER_SETPASSALL(&filt);
820 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
822 err(1, "setsockopt(ICMP6_FILTER)");
824 #endif /*ICMP6_FILTER*/
826 /* let the kerel pass extension headers of incoming packets */
827 if ((options & F_VERBOSE) != 0) {
830 #ifdef IPV6_RECVRTHDR
831 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
833 err(1, "setsockopt(IPV6_RECVRTHDR)");
834 #else /* old adv. API */
835 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
837 err(1, "setsockopt(IPV6_RTHDR)");
843 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
844 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
845 &optval, sizeof(optval)) == -1)
846 err(1, "IPV6_MULTICAST_LOOP");
849 /* Specify the outgoing interface and/or the source address */
851 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
854 ip6optlen += CMSG_SPACE(sizeof(int));
856 /* set IP6 packet options */
858 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
859 errx(1, "can't allocate enough memory");
860 smsghdr.msg_control = (caddr_t)scmsg;
861 smsghdr.msg_controllen = ip6optlen;
862 scmsgp = (struct cmsghdr *)scmsg;
865 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
866 memset(pktinfo, 0, sizeof(*pktinfo));
867 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
868 scmsgp->cmsg_level = IPPROTO_IPV6;
869 scmsgp->cmsg_type = IPV6_PKTINFO;
870 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
873 /* set the outgoing interface */
875 #ifndef USE_SIN6_SCOPE_ID
876 /* pktinfo must have already been allocated */
877 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
878 errx(1, "%s: invalid interface name", ifname);
880 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
881 errx(1, "%s: invalid interface name", ifname);
884 if (hoplimit != -1) {
885 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
886 scmsgp->cmsg_level = IPPROTO_IPV6;
887 scmsgp->cmsg_type = IPV6_HOPLIMIT;
888 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
890 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
893 if (argc > 1) { /* some intermediate addrs are specified */
895 #ifdef USE_RFC2292BIS
899 #ifdef USE_RFC2292BIS
900 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
901 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
902 scmsgp->cmsg_level = IPPROTO_IPV6;
903 scmsgp->cmsg_type = IPV6_RTHDR;
904 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
905 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
906 IPV6_RTHDR_TYPE_0, argc - 1);
908 errx(1, "can't initialize rthdr");
909 #else /* old advanced API */
910 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
911 IPV6_RTHDR_TYPE_0)) == 0)
912 errx(1, "can't initialize rthdr");
913 #endif /* USE_RFC2292BIS */
915 for (hops = 0; hops < argc - 1; hops++) {
916 memset(&hints, 0, sizeof(hints));
917 hints.ai_family = AF_INET6;
919 if ((error = getaddrinfo(argv[hops], NULL, &hints,
921 errx(1, "%s", gai_strerror(error));
922 if (res->ai_addr->sa_family != AF_INET6)
924 "bad addr family of an intermediate addr");
925 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
926 #ifdef USE_RFC2292BIS
927 if (inet6_rth_add(rthdr, &sin6->sin6_addr))
928 errx(1, "can't add an intermediate node");
929 #else /* old advanced API */
930 if (inet6_rthdr_add(scmsg, &sin6->sin6_addr,
932 errx(1, "can't add an intermediate node");
933 #endif /* USE_RFC2292BIS */
937 #ifndef USE_RFC2292BIS
938 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
939 errx(1, "can't set the last flag");
942 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
945 if (!(options & F_SRCADDR)) {
947 * get the source address. XXX since we revoked the root
948 * privilege, we cannot use a raw socket for this.
951 socklen_t len = sizeof(src);
953 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
954 err(1, "UDP socket");
956 src.sin6_family = AF_INET6;
957 src.sin6_addr = dst.sin6_addr;
958 src.sin6_port = ntohs(DUMMY_PORT);
959 src.sin6_scope_id = dst.sin6_scope_id;
961 #ifdef USE_RFC2292BIS
963 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
964 (void *)pktinfo, sizeof(*pktinfo)))
965 err(1, "UDP setsockopt(IPV6_PKTINFO)");
967 if (hoplimit != -1 &&
968 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
969 (void *)&hoplimit, sizeof(hoplimit)))
970 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
972 if (hoplimit != -1 &&
973 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
974 (void *)&hoplimit, sizeof(hoplimit)))
975 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
978 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
979 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
980 err(1, "UDP setsockopt(IPV6_RTHDR)");
981 #else /* old advanced API */
982 if (smsghdr.msg_control &&
983 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
984 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
985 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
988 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
989 err(1, "UDP connect");
991 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
992 err(1, "getsockname");
997 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
999 if (datalen > sockbufsize)
1000 warnx("you need -b to increase socket buffer size");
1001 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
1002 sizeof(sockbufsize)) < 0)
1003 err(1, "setsockopt(SO_SNDBUF)");
1004 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
1005 sizeof(sockbufsize)) < 0)
1006 err(1, "setsockopt(SO_RCVBUF)");
1009 if (datalen > 8 * 1024) /*XXX*/
1010 warnx("you need -b to increase socket buffer size");
1012 * When pinging the broadcast address, you can get a lot of
1013 * answers. Doing something so evil is useful if you are trying
1014 * to stress the ethernet, or just want to fill the arp cache
1015 * to get some stuff for /etc/ethers.
1018 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1024 #ifndef USE_SIN6_SCOPE_ID
1025 #ifdef IPV6_RECVPKTINFO
1026 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1027 sizeof(optval)) < 0)
1028 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1029 #else /* old adv. API */
1030 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1031 sizeof(optval)) < 0)
1032 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1034 #endif /* USE_SIN6_SCOPE_ID */
1035 #ifdef IPV6_RECVHOPLIMIT
1036 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1037 sizeof(optval)) < 0)
1038 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1039 #else /* old adv. API */
1040 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1041 sizeof(optval)) < 0)
1042 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1045 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1046 (unsigned long)(pingerlen() - 8));
1047 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1048 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1053 if (npackets != 0 && preload > npackets)
1058 gettimeofday(&last, NULL);
1060 sigemptyset(&si_sa.sa_mask);
1062 si_sa.sa_handler = onsignal;
1063 if (sigaction(SIGINT, &si_sa, 0) == -1)
1064 err(EX_OSERR, "sigaction SIGINT");
1067 if (sigaction(SIGINFO, &si_sa, 0) == -1)
1068 err(EX_OSERR, "sigaction SIGINFO");
1071 if (alarmtimeout > 0) {
1072 if (sigaction(SIGALRM, &si_sa, 0) == -1)
1073 err(EX_OSERR, "sigaction SIGALRM");
1075 if (options & F_FLOOD) {
1077 intvl.tv_usec = 10000;
1078 } else if ((options & F_INTERVAL) == 0) {
1079 intvl.tv_sec = interval / 1000;
1080 intvl.tv_usec = interval % 1000 * 1000;
1084 while (seenint == 0) {
1085 struct timeval now, timeout;
1087 struct iovec iov[2];
1091 /* signal handling */
1103 gettimeofday(&now, NULL);
1104 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
1105 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
1106 while (timeout.tv_usec < 0) {
1107 timeout.tv_usec += 1000000;
1110 while (timeout.tv_usec > 1000000) {
1111 timeout.tv_usec -= 1000000;
1114 if (timeout.tv_sec < 0)
1115 timeout.tv_sec = timeout.tv_usec = 0;
1117 n = select(s + 1, &rfds, NULL, NULL, &timeout);
1119 continue; /* EINTR */
1121 m.msg_name = (caddr_t)&from;
1122 m.msg_namelen = sizeof(from);
1123 memset(&iov, 0, sizeof(iov));
1124 iov[0].iov_base = (caddr_t)packet;
1125 iov[0].iov_len = packlen;
1128 memset(cm, 0, CONTROLLEN);
1129 m.msg_control = (void *)cm;
1130 m.msg_controllen = CONTROLLEN;
1132 cc = recvmsg(s, &m, 0);
1134 if (errno != EINTR) {
1139 } else if (cc == 0) {
1143 * receive control messages only. Process the
1144 * exceptions (currently the only possibility is
1145 * a path MTU notification.)
1147 if ((mtu = get_pathmtu(&m)) > 0) {
1148 if ((options & F_VERBOSE) != 0) {
1149 printf("new path MTU (%d) is "
1156 * an ICMPv6 message (probably an echoreply)
1159 pr_pack(packet, cc, &m);
1161 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1162 (npackets > 0 && nreceived >= npackets))
1165 if (n == 0 || (options & F_FLOOD)) {
1166 if (npackets == 0 || ntransmitted < npackets)
1173 * If we're not transmitting any more packets,
1174 * change the timer to wait two round-trip times
1175 * if we've received any packets or (waittime)
1176 * milliseconds if we haven't.
1180 intvl.tv_sec = 2 * tmax / 1000;
1181 if (intvl.tv_sec == 0)
1184 intvl.tv_sec = waittime / 1000;
1185 intvl.tv_usec = waittime % 1000 * 1000;
1188 gettimeofday(&last, NULL);
1189 if (ntransmitted - nreceived - 1 > nmissedmax) {
1190 nmissedmax = ntransmitted - nreceived - 1;
1191 if (options & F_MISSED)
1192 (void)write(STDOUT_FILENO, &BBELL, 1);
1196 sigemptyset(&si_sa.sa_mask);
1198 si_sa.sa_handler = SIG_IGN;
1199 sigaction(SIGINT, &si_sa, 0);
1200 sigaction(SIGALRM, &si_sa, 0);
1209 exit(nreceived == 0 ? 2 : 0);
1231 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1232 * will be added on by the kernel. The ID field is our UNIX process ID,
1233 * and the sequence number is an ascending integer. The first 8 bytes
1234 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1235 * byte-order, to compute the round-trip time.
1242 if (options & F_FQDN)
1243 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1244 else if (options & F_FQDNOLD)
1246 else if (options & F_NODEADDR)
1247 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1248 else if (options & F_SUPTYPES)
1251 l = ICMP6ECHOLEN + datalen;
1259 struct icmp6_hdr *icp;
1260 struct iovec iov[2];
1262 struct icmp6_nodeinfo *nip;
1265 if (npackets && ntransmitted >= npackets)
1266 return(-1); /* no more transmission */
1268 icp = (struct icmp6_hdr *)outpack;
1269 nip = (struct icmp6_nodeinfo *)outpack;
1270 memset(icp, 0, sizeof(*icp));
1271 icp->icmp6_cksum = 0;
1272 seq = ntransmitted++;
1273 CLR(seq % mx_dup_ck);
1275 if (options & F_FQDN) {
1276 icp->icmp6_type = ICMP6_NI_QUERY;
1277 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1278 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1279 nip->ni_flags = htons(0);
1281 memcpy(nip->icmp6_ni_nonce, nonce,
1282 sizeof(nip->icmp6_ni_nonce));
1283 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1285 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1286 sizeof(dst.sin6_addr));
1287 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1289 } else if (options & F_FQDNOLD) {
1290 /* packet format in 03 draft - no Subject data on queries */
1291 icp->icmp6_type = ICMP6_NI_QUERY;
1292 icp->icmp6_code = 0; /* code field is always 0 */
1293 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1294 nip->ni_flags = htons(0);
1296 memcpy(nip->icmp6_ni_nonce, nonce,
1297 sizeof(nip->icmp6_ni_nonce));
1298 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1302 } else if (options & F_NODEADDR) {
1303 icp->icmp6_type = ICMP6_NI_QUERY;
1304 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1305 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1306 nip->ni_flags = naflags;
1308 memcpy(nip->icmp6_ni_nonce, nonce,
1309 sizeof(nip->icmp6_ni_nonce));
1310 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1312 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1313 sizeof(dst.sin6_addr));
1314 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1316 } else if (options & F_SUPTYPES) {
1317 icp->icmp6_type = ICMP6_NI_QUERY;
1318 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1319 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1320 /* we support compressed bitmap */
1321 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1323 memcpy(nip->icmp6_ni_nonce, nonce,
1324 sizeof(nip->icmp6_ni_nonce));
1325 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1329 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1330 icp->icmp6_code = 0;
1331 icp->icmp6_id = htons(ident);
1332 icp->icmp6_seq = ntohs(seq);
1336 (void)gettimeofday(&tv, NULL);
1337 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1338 tv32->tv32_sec = htonl(tv.tv_sec);
1339 tv32->tv32_usec = htonl(tv.tv_usec);
1341 cc = ICMP6ECHOLEN + datalen;
1345 if (pingerlen() != cc)
1346 errx(1, "internal error; length mismatch");
1349 smsghdr.msg_name = (caddr_t)&dst;
1350 smsghdr.msg_namelen = sizeof(dst);
1351 memset(&iov, 0, sizeof(iov));
1352 iov[0].iov_base = (caddr_t)outpack;
1353 iov[0].iov_len = cc;
1354 smsghdr.msg_iov = iov;
1355 smsghdr.msg_iovlen = 1;
1357 i = sendmsg(s, &smsghdr, 0);
1359 if (i < 0 || i != cc) {
1362 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1365 if (!(options & F_QUIET) && options & F_FLOOD)
1366 (void)write(STDOUT_FILENO, &DOT, 1);
1372 myechoreply(const struct icmp6_hdr *icp)
1374 if (ntohs(icp->icmp6_id) == ident)
1381 mynireply(const struct icmp6_nodeinfo *nip)
1383 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1384 nonce + sizeof(u_int16_t),
1385 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1392 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1394 /*base for compressed name*/
1398 char cresult[MAXDNAME + 1];
1409 if (i == 0 || cp != *sp) {
1410 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1411 return NULL; /*result overrun*/
1417 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1418 /* DNS compression */
1422 comp = base + (i & 0x3f);
1423 if (dnsdecode(&comp, cp, base, cresult,
1424 sizeof(cresult)) == NULL)
1426 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1427 return NULL; /*result overrun*/
1429 } else if ((i & 0x3f) == i) {
1431 return NULL; /*source overrun*/
1432 while (i-- > 0 && cp < ep) {
1433 l = snprintf(cresult, sizeof(cresult),
1434 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1435 if (l >= sizeof(cresult) || l < 0)
1437 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1438 return NULL; /*result overrun*/
1442 return NULL; /*invalid label*/
1445 return NULL; /*not terminated*/
1453 * Print out the packet, if it came from us. This logic is necessary
1454 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1455 * which arrive ('tis only fair). This permits multiple copies of this
1456 * program to be run without having intermingled output (or statistics!).
1459 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1461 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1462 struct icmp6_hdr *icp;
1463 struct icmp6_nodeinfo *ni;
1466 struct sockaddr *from;
1468 u_char *cp = NULL, *dp, *end = buf + cc;
1469 struct in6_pktinfo *pktinfo = NULL;
1470 struct timeval tv, tp;
1472 double triptime = 0;
1477 char dnsname[MAXDNAME + 1];
1479 (void)gettimeofday(&tv, NULL);
1481 if (!mhdr || !mhdr->msg_name ||
1482 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1483 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1484 if (options & F_VERBOSE)
1485 warnx("invalid peername");
1488 from = (struct sockaddr *)mhdr->msg_name;
1489 fromlen = mhdr->msg_namelen;
1490 if (cc < sizeof(struct icmp6_hdr)) {
1491 if (options & F_VERBOSE)
1492 warnx("packet too short (%d bytes) from %s", cc,
1493 pr_addr(from, fromlen));
1496 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1497 (options & F_VERBOSE) != 0)
1498 warnx("some control data discarded, insufficient buffer size");
1499 icp = (struct icmp6_hdr *)buf;
1500 ni = (struct icmp6_nodeinfo *)buf;
1503 if ((hoplim = get_hoplim(mhdr)) == -1) {
1504 warnx("failed to get receiving hop limit");
1507 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1508 warnx("failed to get receiving packet information");
1512 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1513 seq = ntohs(icp->icmp6_seq);
1516 tpp = (struct tv32 *)(icp + 1);
1517 tp.tv_sec = ntohl(tpp->tv32_sec);
1518 tp.tv_usec = ntohl(tpp->tv32_usec);
1520 triptime = ((double)tv.tv_sec) * 1000.0 +
1521 ((double)tv.tv_usec) / 1000.0;
1523 tsumsq += triptime * triptime;
1524 if (triptime < tmin)
1526 if (triptime > tmax)
1530 if (TST(seq % mx_dup_ck)) {
1535 SET(seq % mx_dup_ck);
1539 if (options & F_QUIET)
1542 if (options & F_WAITTIME && triptime > waittime) {
1547 if (options & F_FLOOD)
1548 (void)write(STDOUT_FILENO, &BSPACE, 1);
1550 if (options & F_AUDIBLE)
1551 (void)write(STDOUT_FILENO, &BBELL, 1);
1552 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1553 pr_addr(from, fromlen), seq);
1554 (void)printf(" hlim=%d", hoplim);
1555 if ((options & F_VERBOSE) != 0) {
1556 struct sockaddr_in6 dstsa;
1558 memset(&dstsa, 0, sizeof(dstsa));
1559 dstsa.sin6_family = AF_INET6;
1560 dstsa.sin6_len = sizeof(dstsa);
1561 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1562 dstsa.sin6_addr = pktinfo->ipi6_addr;
1563 (void)printf(" dst=%s",
1564 pr_addr((struct sockaddr *)&dstsa,
1568 (void)printf(" time=%.3f ms", triptime);
1570 (void)printf("(DUP!)");
1571 /* check the data */
1572 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1573 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1574 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1576 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1581 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1582 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1584 if (TST(seq % mx_dup_ck)) {
1589 SET(seq % mx_dup_ck);
1593 if (options & F_QUIET)
1596 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1598 switch (ntohs(ni->ni_code)) {
1599 case ICMP6_NI_SUCCESS:
1601 case ICMP6_NI_REFUSED:
1602 printf("refused, type 0x%x", ntohs(ni->ni_type));
1604 case ICMP6_NI_UNKNOWN:
1605 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1608 printf("unknown code 0x%x, type 0x%x",
1609 ntohs(ni->ni_code), ntohs(ni->ni_type));
1613 switch (ntohs(ni->ni_qtype)) {
1615 printf("NodeInfo NOOP");
1617 case NI_QTYPE_SUPTYPES:
1618 pr_suptypes(ni, end - (u_char *)ni);
1620 case NI_QTYPE_NODEADDR:
1621 pr_nodeaddr(ni, end - (u_char *)ni);
1624 default: /* XXX: for backward compatibility */
1625 cp = (u_char *)ni + ICMP6_NIRLEN;
1626 if (buf[off + ICMP6_NIRLEN] ==
1627 cc - off - ICMP6_NIRLEN - 1)
1632 cp++; /* skip length */
1634 safeputc(*cp & 0xff);
1640 if (dnsdecode((const u_char **)&cp, end,
1641 (const u_char *)(ni + 1), dnsname,
1642 sizeof(dnsname)) == NULL) {
1647 * name-lookup special handling for
1650 if (cp + 1 <= end && !*cp &&
1651 strlen(dnsname) > 0) {
1652 dnsname[strlen(dnsname) - 1] = '\0';
1655 printf("%s%s", i > 0 ? "," : "",
1659 if (options & F_VERBOSE) {
1663 (void)printf(" ("); /*)*/
1665 switch (ni->ni_code) {
1666 case ICMP6_NI_REFUSED:
1667 (void)printf("refused");
1670 case ICMP6_NI_UNKNOWN:
1671 (void)printf("unknown qtype");
1676 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1677 /* case of refusion, unknown */
1682 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1685 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1686 (void)printf("TTL=%d:meaningless",
1690 (void)printf("TTL=%d:invalid",
1693 (void)printf("TTL=%d", ttl);
1703 cp = (u_char *)ni + ICMP6_NIRLEN;
1712 if (buf[off + ICMP6_NIRLEN] !=
1713 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1716 (void)printf("invalid namelen:%d/%lu",
1717 buf[off + ICMP6_NIRLEN],
1718 (u_long)cc - off - ICMP6_NIRLEN - 1);
1728 /* We've got something other than an ECHOREPLY */
1729 if (!(options & F_VERBOSE))
1731 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1735 if (!(options & F_FLOOD)) {
1736 (void)putchar('\n');
1737 if (options & F_VERBOSE)
1739 (void)fflush(stdout);
1745 pr_exthdrs(struct msghdr *mhdr)
1752 bufp = mhdr->msg_control;
1753 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1754 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1755 if (cm->cmsg_level != IPPROTO_IPV6)
1758 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1761 switch (cm->cmsg_type) {
1763 printf(" HbH Options: ");
1764 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1767 #ifdef IPV6_RTHDRDSTOPTS
1768 case IPV6_RTHDRDSTOPTS:
1770 printf(" Dst Options: ");
1771 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1774 printf(" Routing: ");
1775 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1781 #ifdef USE_RFC2292BIS
1783 pr_ip6opt(void *extbuf, size_t bufsize)
1785 struct ip6_hbh *ext;
1788 socklen_t extlen, len;
1794 ext = (struct ip6_hbh *)extbuf;
1795 extlen = (ext->ip6h_len + 1) * 8;
1796 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1797 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1800 * Bounds checking on the ancillary data buffer:
1801 * subtract the size of a cmsg structure from the buffer size.
1803 if (bufsize < (extlen + CMSG_SPACE(0))) {
1804 extlen = bufsize - CMSG_SPACE(0);
1805 warnx("options truncated, showing only %u (total=%u)",
1806 (unsigned int)(extlen / 8 - 1),
1807 (unsigned int)(ext->ip6h_len));
1812 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1813 &type, &len, &databuf);
1814 if (currentlen == -1)
1818 * Note that inet6_opt_next automatically skips any padding
1823 offset = inet6_opt_get_val(databuf, offset,
1824 &value4, sizeof(value4));
1825 printf(" Jumbo Payload Opt: Length %u\n",
1826 (u_int32_t)ntohl(value4));
1828 case IP6OPT_ROUTER_ALERT:
1830 offset = inet6_opt_get_val(databuf, offset,
1831 &value2, sizeof(value2));
1832 printf(" Router Alert Opt: Type %u\n",
1836 printf(" Received Opt %u len %lu\n",
1837 type, (unsigned long)len);
1843 #else /* !USE_RFC2292BIS */
1846 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1851 #endif /* USE_RFC2292BIS */
1853 #ifdef USE_RFC2292BIS
1855 pr_rthdr(void *extbuf, size_t bufsize)
1857 struct in6_addr *in6;
1858 char ntopbuf[INET6_ADDRSTRLEN];
1859 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1860 int i, segments, origsegs, rthsize, size0, size1;
1862 /* print fixed part of the header */
1863 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1864 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1865 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1866 printf("%d segments, ", segments);
1867 printf("%d left\n", rh->ip6r_segleft);
1869 printf("segments unknown, ");
1870 printf("%d left\n", rh->ip6r_segleft);
1875 * Bounds checking on the ancillary data buffer. When calculating
1876 * the number of items to show keep in mind:
1877 * - The size of the cmsg structure
1878 * - The size of one segment (the size of a Type 0 routing header)
1879 * - When dividing add a fudge factor of one in case the
1880 * dividend is not evenly divisible by the divisor
1882 rthsize = (rh->ip6r_len + 1) * 8;
1883 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1884 origsegs = segments;
1885 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1886 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1887 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1888 (size1 - size0) + 1;
1889 warnx("segments truncated, showing only %d (total=%d)",
1890 segments, origsegs);
1893 for (i = 0; i < segments; i++) {
1894 in6 = inet6_rth_getaddr(extbuf, i);
1896 printf(" [%d]<NULL>\n", i);
1898 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1900 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1901 printf(" [%d]%s\n", i, ntopbuf);
1909 #else /* !USE_RFC2292BIS */
1912 pr_rthdr(void *extbuf, size_t bufsize __unused)
1917 #endif /* USE_RFC2292BIS */
1920 pr_bitrange(u_int32_t v, int soff, int ii)
1927 /* shift till we have 0x01 */
1928 if ((v & 0x01) == 0) {
1930 printf("-%u", soff + off - 1);
1941 case 0x04: case 0x0c:
1952 /* we have 0x01 with us */
1953 for (i = 0; i < 32 - off; i++) {
1954 if ((v & (0x01 << i)) == 0)
1958 printf(" %u", soff + off);
1966 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1967 /* ni->qtype must be SUPTYPES */
1971 const u_char *cp, *end;
1974 u_int16_t words; /*32bit count*/
1977 #define MAXQTYPES (1 << 16)
1981 cp = (u_char *)(ni + 1);
1982 end = ((u_char *)ni) + nilen;
1986 printf("NodeInfo Supported Qtypes");
1987 if (options & F_VERBOSE) {
1988 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1989 printf(", compressed bitmap");
1991 printf(", raw bitmap");
1995 clen = (size_t)(end - cp);
1996 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1997 if (clen == 0 || clen > MAXQTYPES / 8 ||
2003 if (clen < sizeof(cbit) || clen % sizeof(v))
2005 memcpy(&cbit, cp, sizeof(cbit));
2006 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2010 clen = ntohs(cbit.words) * sizeof(v);
2011 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2016 for (off = 0; off < clen; off += sizeof(v)) {
2017 memcpy(&v, cp + off, sizeof(v));
2018 v = (u_int32_t)ntohl(v);
2019 b = pr_bitrange(v, (int)(cur + off * 8), b);
2021 /* flush the remaining bits */
2022 b = pr_bitrange(0, (int)(cur + off * 8), b);
2026 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2027 cur += ntohs(cbit.skip) * 32;
2032 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
2033 /* ni->qtype must be NODEADDR */
2035 u_char *cp = (u_char *)(ni + 1);
2036 char ntop_buf[INET6_ADDRSTRLEN];
2039 nilen -= sizeof(struct icmp6_nodeinfo);
2041 if (options & F_VERBOSE) {
2042 switch (ni->ni_code) {
2043 case ICMP6_NI_REFUSED:
2044 (void)printf("refused");
2046 case ICMP6_NI_UNKNOWN:
2047 (void)printf("unknown qtype");
2050 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2051 (void)printf(" truncated");
2055 printf(" no address\n");
2058 * In icmp-name-lookups 05 and later, TTL of each returned address
2059 * is contained in the resposne. We try to detect the version
2060 * by the length of the data, but note that the detection algorithm
2061 * is incomplete. We assume the latest draft by default.
2063 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2069 /* XXX: alignment? */
2070 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2071 cp += sizeof(u_int32_t);
2072 nilen -= sizeof(u_int32_t);
2075 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2077 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2078 printf(" %s", ntop_buf);
2080 if (ttl == 0xffffffff) {
2082 * XXX: can this convention be applied to all
2083 * type of TTL (i.e. non-ND TTL)?
2085 printf("(TTL=infty)");
2088 printf("(TTL=%u)", ttl);
2092 nilen -= sizeof(struct in6_addr);
2093 cp += sizeof(struct in6_addr);
2098 get_hoplim(struct msghdr *mhdr)
2102 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2103 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2104 if (cm->cmsg_len == 0)
2107 if (cm->cmsg_level == IPPROTO_IPV6 &&
2108 cm->cmsg_type == IPV6_HOPLIMIT &&
2109 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2110 return(*(int *)CMSG_DATA(cm));
2116 struct in6_pktinfo *
2117 get_rcvpktinfo(struct msghdr *mhdr)
2121 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2122 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2123 if (cm->cmsg_len == 0)
2126 if (cm->cmsg_level == IPPROTO_IPV6 &&
2127 cm->cmsg_type == IPV6_PKTINFO &&
2128 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2129 return((struct in6_pktinfo *)CMSG_DATA(cm));
2136 get_pathmtu(struct msghdr *mhdr)
2138 #ifdef IPV6_RECVPATHMTU
2140 struct ip6_mtuinfo *mtuctl = NULL;
2142 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2143 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2144 if (cm->cmsg_len == 0)
2147 if (cm->cmsg_level == IPPROTO_IPV6 &&
2148 cm->cmsg_type == IPV6_PATHMTU &&
2149 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2150 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2153 * If the notified destination is different from
2154 * the one we are pinging, just ignore the info.
2155 * We check the scope ID only when both notified value
2156 * and our own value have non-0 values, because we may
2157 * have used the default scope zone ID for sending,
2158 * in which case the scope ID value is 0.
2160 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2162 (mtuctl->ip6m_addr.sin6_scope_id &&
2163 dst.sin6_scope_id &&
2164 mtuctl->ip6m_addr.sin6_scope_id !=
2165 dst.sin6_scope_id)) {
2166 if ((options & F_VERBOSE) != 0) {
2167 printf("path MTU for %s is notified. "
2169 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2170 sizeof(mtuctl->ip6m_addr)));
2176 * Ignore an invalid MTU. XXX: can we just believe
2179 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2182 /* notification for our destination. return the MTU. */
2183 return((int)mtuctl->ip6m_mtu);
2192 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2196 tvsub(struct timeval *out, struct timeval *in)
2198 if ((out->tv_usec -= in->tv_usec) < 0) {
2200 out->tv_usec += 1000000;
2202 out->tv_sec -= in->tv_sec;
2211 onint(int notused __unused)
2214 * When doing reverse DNS lookups, the seenint flag might not
2215 * be noticed for a while. Just exit if we get a second SIGINT.
2217 if ((options & F_HOSTNAME) && seenint != 0)
2218 _exit(nreceived ? 0 : 2);
2223 * Print out statistics.
2229 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2230 (void)printf("%ld packets transmitted, ", ntransmitted);
2231 (void)printf("%ld packets received, ", nreceived);
2233 (void)printf("+%ld duplicates, ", nrepeats);
2235 if (nreceived > ntransmitted)
2236 (void)printf("-- somebody's duplicating packets!");
2238 (void)printf("%.1f%% packet loss",
2239 ((((double)ntransmitted - nreceived) * 100.0) /
2243 printf(", %ld packets out of wait time", nrcvtimeout);
2244 (void)putchar('\n');
2245 if (nreceived && timing) {
2246 /* Only display average to microseconds */
2247 double num = nreceived + nrepeats;
2248 double avg = tsum / num;
2249 double dev = sqrt(tsumsq / num - avg * avg);
2251 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2252 tmin, avg, tmax, dev);
2253 (void)fflush(stdout);
2255 (void)fflush(stdout);
2259 static const char *niqcode[] = {
2261 "DNS label", /*or empty*/
2266 static const char *nircode[] = {
2267 "Success", "Refused", "Unknown",
2273 * Print a descriptive string about an ICMP header.
2276 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2278 char ntop_buf[INET6_ADDRSTRLEN];
2279 struct nd_redirect *red;
2280 struct icmp6_nodeinfo *ni;
2281 char dnsname[MAXDNAME + 1];
2285 switch (icp->icmp6_type) {
2286 case ICMP6_DST_UNREACH:
2287 switch (icp->icmp6_code) {
2288 case ICMP6_DST_UNREACH_NOROUTE:
2289 (void)printf("No Route to Destination\n");
2291 case ICMP6_DST_UNREACH_ADMIN:
2292 (void)printf("Destination Administratively "
2295 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2296 (void)printf("Destination Unreachable Beyond Scope\n");
2298 case ICMP6_DST_UNREACH_ADDR:
2299 (void)printf("Destination Host Unreachable\n");
2301 case ICMP6_DST_UNREACH_NOPORT:
2302 (void)printf("Destination Port Unreachable\n");
2305 (void)printf("Destination Unreachable, Bad Code: %d\n",
2309 /* Print returned IP header information */
2310 pr_retip((struct ip6_hdr *)(icp + 1), end);
2312 case ICMP6_PACKET_TOO_BIG:
2313 (void)printf("Packet too big mtu = %d\n",
2314 (int)ntohl(icp->icmp6_mtu));
2315 pr_retip((struct ip6_hdr *)(icp + 1), end);
2317 case ICMP6_TIME_EXCEEDED:
2318 switch (icp->icmp6_code) {
2319 case ICMP6_TIME_EXCEED_TRANSIT:
2320 (void)printf("Time to live exceeded\n");
2322 case ICMP6_TIME_EXCEED_REASSEMBLY:
2323 (void)printf("Frag reassembly time exceeded\n");
2326 (void)printf("Time exceeded, Bad Code: %d\n",
2330 pr_retip((struct ip6_hdr *)(icp + 1), end);
2332 case ICMP6_PARAM_PROB:
2333 (void)printf("Parameter problem: ");
2334 switch (icp->icmp6_code) {
2335 case ICMP6_PARAMPROB_HEADER:
2336 (void)printf("Erroneous Header ");
2338 case ICMP6_PARAMPROB_NEXTHEADER:
2339 (void)printf("Unknown Nextheader ");
2341 case ICMP6_PARAMPROB_OPTION:
2342 (void)printf("Unrecognized Option ");
2345 (void)printf("Bad code(%d) ", icp->icmp6_code);
2348 (void)printf("pointer = 0x%02x\n",
2349 (u_int32_t)ntohl(icp->icmp6_pptr));
2350 pr_retip((struct ip6_hdr *)(icp + 1), end);
2352 case ICMP6_ECHO_REQUEST:
2353 (void)printf("Echo Request");
2354 /* XXX ID + Seq + Data */
2356 case ICMP6_ECHO_REPLY:
2357 (void)printf("Echo Reply");
2358 /* XXX ID + Seq + Data */
2360 case ICMP6_MEMBERSHIP_QUERY:
2361 (void)printf("Listener Query");
2363 case ICMP6_MEMBERSHIP_REPORT:
2364 (void)printf("Listener Report");
2366 case ICMP6_MEMBERSHIP_REDUCTION:
2367 (void)printf("Listener Done");
2369 case ND_ROUTER_SOLICIT:
2370 (void)printf("Router Solicitation");
2372 case ND_ROUTER_ADVERT:
2373 (void)printf("Router Advertisement");
2375 case ND_NEIGHBOR_SOLICIT:
2376 (void)printf("Neighbor Solicitation");
2378 case ND_NEIGHBOR_ADVERT:
2379 (void)printf("Neighbor Advertisement");
2382 red = (struct nd_redirect *)icp;
2383 (void)printf("Redirect\n");
2384 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2386 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2387 (void)printf("Destination: %s", ntop_buf);
2388 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2390 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2391 (void)printf(" New Target: %s", ntop_buf);
2393 case ICMP6_NI_QUERY:
2394 (void)printf("Node Information Query");
2395 /* XXX ID + Seq + Data */
2396 ni = (struct icmp6_nodeinfo *)icp;
2397 l = end - (u_char *)(ni + 1);
2399 switch (ntohs(ni->ni_qtype)) {
2401 (void)printf("NOOP");
2403 case NI_QTYPE_SUPTYPES:
2404 (void)printf("Supported qtypes");
2407 (void)printf("DNS name");
2409 case NI_QTYPE_NODEADDR:
2410 (void)printf("nodeaddr");
2412 case NI_QTYPE_IPV4ADDR:
2413 (void)printf("IPv4 nodeaddr");
2416 (void)printf("unknown qtype");
2419 if (options & F_VERBOSE) {
2420 switch (ni->ni_code) {
2421 case ICMP6_NI_SUBJ_IPV6:
2422 if (l == sizeof(struct in6_addr) &&
2423 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2424 sizeof(ntop_buf)) != NULL) {
2425 (void)printf(", subject=%s(%s)",
2426 niqcode[ni->ni_code], ntop_buf);
2429 /* backward compat to -W */
2430 (void)printf(", oldfqdn");
2432 (void)printf(", invalid");
2436 case ICMP6_NI_SUBJ_FQDN:
2437 if (end == (u_char *)(ni + 1)) {
2438 (void)printf(", no subject");
2441 printf(", subject=%s", niqcode[ni->ni_code]);
2442 cp = (const u_char *)(ni + 1);
2443 if (dnsdecode(&cp, end, NULL, dnsname,
2444 sizeof(dnsname)) != NULL)
2445 printf("(%s)", dnsname);
2447 printf("(invalid)");
2449 case ICMP6_NI_SUBJ_IPV4:
2450 if (l == sizeof(struct in_addr) &&
2451 inet_ntop(AF_INET, ni + 1, ntop_buf,
2452 sizeof(ntop_buf)) != NULL) {
2453 (void)printf(", subject=%s(%s)",
2454 niqcode[ni->ni_code], ntop_buf);
2456 (void)printf(", invalid");
2459 (void)printf(", invalid");
2464 case ICMP6_NI_REPLY:
2465 (void)printf("Node Information Reply");
2466 /* XXX ID + Seq + Data */
2467 ni = (struct icmp6_nodeinfo *)icp;
2469 switch (ntohs(ni->ni_qtype)) {
2471 (void)printf("NOOP");
2473 case NI_QTYPE_SUPTYPES:
2474 (void)printf("Supported qtypes");
2477 (void)printf("DNS name");
2479 case NI_QTYPE_NODEADDR:
2480 (void)printf("nodeaddr");
2482 case NI_QTYPE_IPV4ADDR:
2483 (void)printf("IPv4 nodeaddr");
2486 (void)printf("unknown qtype");
2489 if (options & F_VERBOSE) {
2490 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2491 printf(", invalid");
2493 printf(", %s", nircode[ni->ni_code]);
2497 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2503 * Print an IP6 header.
2506 pr_iph(struct ip6_hdr *ip6)
2508 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2510 char ntop_buf[INET6_ADDRSTRLEN];
2512 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2513 tc = (tc >> 4) & 0x0f;
2514 tc |= (ip6->ip6_vfc << 4);
2516 printf("Vr TC Flow Plen Nxt Hlim\n");
2517 printf(" %1x %02x %05x %04x %02x %02x\n",
2518 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2519 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2520 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2521 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2522 printf("%s->", ntop_buf);
2523 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2524 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2525 printf("%s\n", ntop_buf);
2530 * Return an ascii host address as a dotted quad and optionally with
2534 pr_addr(struct sockaddr *addr, int addrlen)
2536 static char buf[NI_MAXHOST];
2539 if ((options & F_HOSTNAME) == 0)
2540 flag |= NI_NUMERICHOST;
2542 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2550 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2553 pr_retip(struct ip6_hdr *ip6, u_char *end)
2555 u_char *cp = (u_char *)ip6, nh;
2558 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2563 hlen = sizeof(*ip6);
2567 while (end - cp >= 8) {
2569 case IPPROTO_HOPOPTS:
2571 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2572 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2574 case IPPROTO_DSTOPTS:
2576 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2577 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2579 case IPPROTO_FRAGMENT:
2581 hlen = sizeof(struct ip6_frag);
2582 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2584 case IPPROTO_ROUTING:
2586 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2587 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2592 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2593 nh = ((struct ah *)cp)->ah_nxt;
2596 case IPPROTO_ICMPV6:
2597 printf("ICMP6: type = %d, code = %d\n",
2604 printf("TCP: from port %u, to port %u (decimal)\n",
2605 (*cp * 256 + *(cp + 1)),
2606 (*(cp + 2) * 256 + *(cp + 3)));
2609 printf("UDP: from port %u, to port %u (decimal)\n",
2610 (*cp * 256 + *(cp + 1)),
2611 (*(cp + 2) * 256 + *(cp + 3)));
2614 printf("Unknown Header(%d)\n", nh);
2618 if ((cp += hlen) >= end)
2633 fill(char *bp, char *patp)
2639 for (cp = patp; *cp; cp++)
2641 errx(1, "patterns must be specified as hex digits");
2643 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2644 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2645 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2646 &pat[13], &pat[14], &pat[15]);
2651 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);
2653 for (jj = 0; jj < ii; ++jj)
2654 bp[jj + kk] = pat[jj];
2655 if (!(options & F_QUIET)) {
2656 (void)printf("PATTERN: 0x");
2657 for (jj = 0; jj < ii; ++jj)
2658 (void)printf("%02x", bp[jj] & 0xFF);
2664 #ifdef IPSEC_POLICY_IPSEC
2666 setpolicy(int so __unused, char *policy)
2671 return 0; /* ignore */
2673 buf = ipsec_set_policy(policy, strlen(policy));
2675 errx(1, "%s", ipsec_strerror());
2676 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2677 ipsec_get_policylen(buf)) < 0)
2678 warnx("Unable to set IPsec policy");
2687 nigroup(char *name, int nig_oldmcprefix)
2692 u_int8_t digest[16];
2695 char hbuf[NI_MAXHOST];
2696 struct in6_addr in6;
2699 p = strchr(name, '.');
2701 p = name + strlen(name);
2703 if (l > 63 || l > sizeof(hbuf) - 1)
2704 return NULL; /*label too long*/
2705 strncpy(hbuf, name, l);
2706 hbuf[(int)l] = '\0';
2708 for (q = name; *q; q++) {
2709 if (isupper(*(unsigned char *)q))
2710 *q = tolower(*(unsigned char *)q);
2713 /* generate 16 bytes of pseudo-random value. */
2714 memset(&ctxt, 0, sizeof(ctxt));
2717 MD5Update(&ctxt, &c, sizeof(c));
2718 MD5Update(&ctxt, (unsigned char *)name, l);
2719 MD5Final(digest, &ctxt);
2721 if (nig_oldmcprefix) {
2722 /* draft-ietf-ipngwg-icmp-name-lookup */
2723 valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
2726 valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
2729 return NULL; /*XXX*/
2731 if (nig_oldmcprefix) {
2732 /* draft-ietf-ipngwg-icmp-name-lookup */
2733 bcopy(digest, &in6.s6_addr[12], 4);
2736 bcopy(digest, &in6.s6_addr[13], 3);
2739 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2742 return strdup(hbuf);
2748 (void)fprintf(stderr,
2749 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2754 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2758 #ifdef IPV6_USE_MIN_MTU
2762 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2763 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2764 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2768 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2770 " [-X timeout] [hops ...] host\n");