1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static const char copyright[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid[] =
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
137 #include <netipsec/ah.h>
138 #include <netipsec/ipsec.h>
148 #define MAXPACKETLEN 131072
150 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
151 #define ICMP6ECHOTMLEN sizeof(struct tv32)
152 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
153 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
154 /* FQDN case, 64 bits of nonce + 32 bits ttl */
155 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
156 #define EXTRA 256 /* for AH and various other headers. weird. */
157 #define DEFDATALEN ICMP6ECHOTMLEN
158 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
159 #define NROUTES 9 /* number of record route slots */
161 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
162 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
163 #define SET(bit) (A(bit) |= B(bit))
164 #define CLR(bit) (A(bit) &= (~B(bit)))
165 #define TST(bit) (A(bit) & B(bit))
167 #define F_FLOOD 0x0001
168 #define F_INTERVAL 0x0002
169 #define F_PINGFILLED 0x0008
170 #define F_QUIET 0x0010
171 #define F_RROUTE 0x0020
172 #define F_SO_DEBUG 0x0040
173 #define F_VERBOSE 0x0100
175 #ifdef IPSEC_POLICY_IPSEC
176 #define F_POLICY 0x0400
178 #define F_AUTHHDR 0x0200
179 #define F_ENCRYPT 0x0400
180 #endif /*IPSEC_POLICY_IPSEC*/
182 #define F_NODEADDR 0x0800
183 #define F_FQDN 0x1000
184 #define F_INTERFACE 0x2000
185 #define F_SRCADDR 0x4000
186 #define F_HOSTNAME 0x10000
187 #define F_FQDNOLD 0x20000
188 #define F_NIGROUP 0x40000
189 #define F_SUPTYPES 0x80000
190 #define F_NOMINMTU 0x100000
191 #define F_ONCE 0x200000
192 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
195 #define IN6LEN sizeof(struct in6_addr)
196 #define SA6LEN sizeof(struct sockaddr_in6)
197 #define DUMMY_PORT 10101
199 #define SIN6(s) ((struct sockaddr_in6 *)(s))
202 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
203 * number of received sequence numbers we can keep track of. Change 128
204 * to 8192 for complete accuracy...
206 #define MAX_DUP_CHK (8 * 8192)
207 int mx_dup_ck = MAX_DUP_CHK;
208 char rcvd_tbl[MAX_DUP_CHK / 8];
210 struct addrinfo *res;
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 */
220 int ident; /* process id to identify our packets */
221 u_int8_t nonce[8]; /* nonce field for node information */
222 int hoplimit = -1; /* hoplimit */
223 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
226 long npackets; /* max packets to transmit */
227 long nreceived; /* # of packets we got back */
228 long nrepeats; /* number of duplicates */
229 long ntransmitted; /* sequence # for outbound packets = #sent */
230 struct timeval interval = {1, 0}; /* interval between packets */
233 int timing; /* flag to do timing */
234 double tmin = 999999999.0; /* minimum round trip time */
235 double tmax = 0.0; /* maximum round trip time */
236 double tsum = 0.0; /* sum of all times, for doing average */
237 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
239 /* for node addresses */
242 /* for ancillary data(advanced API) */
243 struct msghdr smsghdr;
244 struct iovec smsgiov;
247 volatile sig_atomic_t seenalrm;
248 volatile sig_atomic_t seenint;
250 volatile sig_atomic_t seeninfo;
253 int main(int, char *[]);
254 void fill(char *, char *);
255 int get_hoplim(struct msghdr *);
256 int get_pathmtu(struct msghdr *);
257 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
259 void retransmit(void);
261 size_t pingerlen(void);
263 const char *pr_addr(struct sockaddr *, int);
264 void pr_icmph(struct icmp6_hdr *, u_char *);
265 void pr_iph(struct ip6_hdr *);
266 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
267 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
268 int myechoreply(const struct icmp6_hdr *);
269 int mynireply(const struct icmp6_nodeinfo *);
270 char *dnsdecode(const u_char **, const u_char *, const u_char *,
272 void pr_pack(u_char *, int, struct msghdr *);
273 void pr_exthdrs(struct msghdr *);
274 void pr_ip6opt(void *, size_t);
275 void pr_rthdr(void *, size_t);
276 int pr_bitrange(u_int32_t, int, int);
277 void pr_retip(struct ip6_hdr *, u_char *);
279 void tvsub(struct timeval *, struct timeval *);
280 int setpolicy(int, char *);
281 char *nigroup(char *);
289 struct itimerval itimer;
290 struct sockaddr_in6 from;
291 #ifndef HAVE_ARC4RANDOM
297 struct timeval timeout, *tv;
299 struct addrinfo hints;
301 struct pollfd fdmaskp[1];
307 int ch, hold, packlen, preload, optval, ret_ga;
308 u_char *datap, *packet;
309 char *e, *target, *ifname = NULL, *gateway = NULL;
311 struct cmsghdr *scmsgp = NULL;
313 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
318 struct in6_pktinfo *pktinfo = NULL;
319 #ifdef USE_RFC2292BIS
320 struct ip6_rthdr *rthdr = NULL;
322 #ifdef IPSEC_POLICY_IPSEC
323 char *policy_in = NULL;
324 char *policy_out = NULL;
328 #ifdef IPV6_USE_MIN_MTU
332 /* just to be sure */
333 memset(&smsghdr, 0, sizeof(smsghdr));
334 memset(&smsgiov, 0, sizeof(smsgiov));
337 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
341 #ifdef IPSEC_POLICY_IPSEC
345 #endif /*IPSEC_POLICY_IPSEC*/
347 while ((ch = getopt(argc, argv,
348 "a:b:c:dfHg:h:I:i:l:mnNop:qS:s:tvwW" ADDOPTS)) != -1) {
355 options &= ~F_NOUSERDATA;
356 options |= F_NODEADDR;
357 for (cp = optarg; *cp != '\0'; cp++) {
360 naflags |= NI_NODEADDR_FLAG_ALL;
364 naflags |= NI_NODEADDR_FLAG_COMPAT;
368 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
372 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
376 naflags |= NI_NODEADDR_FLAG_GLOBAL;
378 case 'A': /* experimental. not in the spec */
379 #ifdef NI_NODEADDR_FLAG_ANYCAST
380 naflags |= NI_NODEADDR_FLAG_ANYCAST;
384 "-a A is not supported on the platform");
395 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
398 lsockbufsize = strtoul(optarg, &e, 10);
399 sockbufsize = lsockbufsize;
400 if (errno || !*optarg || *e ||
401 sockbufsize != lsockbufsize)
402 errx(1, "invalid socket buffer size");
405 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
409 npackets = strtol(optarg, &e, 10);
410 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
412 "illegal number of packets -- %s", optarg);
415 options |= F_SO_DEBUG;
420 errx(1, "Must be superuser to flood ping");
423 setbuf(stdout, (char *)NULL);
429 options |= F_HOSTNAME;
431 case 'h': /* hoplimit */
432 hoplimit = strtol(optarg, &e, 10);
433 if (*optarg == '\0' || *e != '\0')
434 errx(1, "illegal hoplimit %s", optarg);
435 if (255 < hoplimit || hoplimit < -1)
437 "illegal hoplimit -- %s", optarg);
441 options |= F_INTERFACE;
442 #ifndef USE_SIN6_SCOPE_ID
446 case 'i': /* wait between sending packets */
447 intval = strtod(optarg, &e);
448 if (*optarg == '\0' || *e != '\0')
449 errx(1, "illegal timing interval %s", optarg);
450 if (intval < 1 && getuid()) {
451 errx(1, "%s: only root may use interval < 1s",
454 interval.tv_sec = (long)intval;
456 (long)((intval - interval.tv_sec) * 1000000);
457 if (interval.tv_sec < 0)
458 errx(1, "illegal timing interval %s", optarg);
459 /* less than 1/hz does not make sense */
460 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
461 warnx("too small interval, raised to 0.01");
462 interval.tv_usec = 10000;
464 options |= F_INTERVAL;
469 errx(1, "Must be superuser to preload");
471 preload = strtol(optarg, &e, 10);
472 if (preload < 0 || *optarg == '\0' || *e != '\0')
473 errx(1, "illegal preload value -- %s", optarg);
476 #ifdef IPV6_USE_MIN_MTU
480 errx(1, "-%c is not supported on this platform", ch);
484 options &= ~F_HOSTNAME;
487 options |= F_NIGROUP;
492 case 'p': /* fill buffer with user pattern */
493 options |= F_PINGFILLED;
494 fill((char *)datap, optarg);
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 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
508 errx(1, "invalid source address: %s",
509 gai_strerror(ret_ga));
512 * res->ai_family must be AF_INET6 and res->ai_addrlen
513 * must be sizeof(src).
515 memcpy(&src, res->ai_addr, res->ai_addrlen);
516 srclen = res->ai_addrlen;
518 options |= F_SRCADDR;
520 case 's': /* size of packet to send */
521 datalen = strtol(optarg, &e, 10);
522 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
523 errx(1, "illegal datalen value -- %s", optarg);
524 if (datalen > MAXDATALEN) {
526 "datalen value too large, maximum is %d",
531 options &= ~F_NOUSERDATA;
532 options |= F_SUPTYPES;
535 options |= F_VERBOSE;
538 options &= ~F_NOUSERDATA;
542 options &= ~F_NOUSERDATA;
543 options |= F_FQDNOLD;
546 #ifdef IPSEC_POLICY_IPSEC
549 if (!strncmp("in", optarg, 2)) {
550 if ((policy_in = strdup(optarg)) == NULL)
552 } else if (!strncmp("out", optarg, 3)) {
553 if ((policy_out = strdup(optarg)) == NULL)
556 errx(1, "invalid security policy");
560 options |= F_AUTHHDR;
563 options |= F_ENCRYPT;
565 #endif /*IPSEC_POLICY_IPSEC*/
582 #ifdef IPV6_RECVRTHDR /* 2292bis */
583 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
586 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
589 errx(1, "too many intermediate hops");
595 if (options & F_NIGROUP) {
596 target = nigroup(argv[argc - 1]);
597 if (target == NULL) {
602 target = argv[argc - 1];
605 memset(&hints, 0, sizeof(struct addrinfo));
606 hints.ai_flags = AI_CANONNAME;
607 hints.ai_family = AF_INET6;
608 hints.ai_socktype = SOCK_RAW;
609 hints.ai_protocol = IPPROTO_ICMPV6;
611 ret_ga = getaddrinfo(target, NULL, &hints, &res);
613 errx(1, "%s", gai_strerror(ret_ga));
614 if (res->ai_canonname)
615 hostname = res->ai_canonname;
620 errx(1, "getaddrinfo failed");
622 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
624 if ((s = socket(res->ai_family, res->ai_socktype,
625 res->ai_protocol)) < 0)
628 /* set the source address if specified. */
629 if ((options & F_SRCADDR) &&
630 bind(s, (struct sockaddr *)&src, srclen) != 0) {
634 /* set the gateway (next hop) if specified */
636 struct addrinfo ghints, *gres;
639 memset(&ghints, 0, sizeof(ghints));
640 ghints.ai_family = AF_INET6;
641 ghints.ai_socktype = SOCK_RAW;
642 ghints.ai_protocol = IPPROTO_ICMPV6;
644 error = getaddrinfo(gateway, NULL, &hints, &gres);
646 errx(1, "getaddrinfo for the gateway %s: %s",
647 gateway, gai_strerror(error));
649 if (gres->ai_next && (options & F_VERBOSE))
650 warnx("gateway resolves to multiple addresses");
652 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
653 gres->ai_addr, gres->ai_addrlen)) {
654 err(1, "setsockopt(IPV6_NEXTHOP)");
661 * let the kerel pass extension headers of incoming packets,
662 * for privileged socket options
664 if ((options & F_VERBOSE) != 0) {
667 #ifdef IPV6_RECVHOPOPTS
668 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
670 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
671 #else /* old adv. API */
672 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
674 err(1, "setsockopt(IPV6_HOPOPTS)");
676 #ifdef IPV6_RECVDSTOPTS
677 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
679 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
680 #else /* old adv. API */
681 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
683 err(1, "setsockopt(IPV6_DSTOPTS)");
685 #ifdef IPV6_RECVRTHDRDSTOPTS
686 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
688 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
692 /* revoke root privilege */
696 if ((options & F_FLOOD) && (options & F_INTERVAL))
697 errx(1, "-f and -i incompatible options");
699 if ((options & F_NOUSERDATA) == 0) {
700 if (datalen >= sizeof(struct tv32)) {
701 /* we can time transfer */
705 /* in F_VERBOSE case, we may get non-echoreply packets*/
706 if (options & F_VERBOSE)
707 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
709 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
711 /* suppress timing for node information query */
714 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
717 if (!(packet = (u_char *)malloc((u_int)packlen)))
718 err(1, "Unable to allocate packet");
719 if (!(options & F_PINGFILLED))
720 for (i = ICMP6ECHOLEN; i < packlen; ++i)
723 ident = getpid() & 0xFFFF;
724 #ifndef HAVE_ARC4RANDOM
725 gettimeofday(&seed, NULL);
726 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
727 memset(nonce, 0, sizeof(nonce));
728 for (i = 0; i < sizeof(nonce); i += sizeof(int))
729 *((int *)&nonce[i]) = rand();
731 memset(nonce, 0, sizeof(nonce));
732 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
733 *((u_int32_t *)&nonce[i]) = arc4random();
738 if (options & F_SO_DEBUG)
739 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
741 optval = IPV6_DEFHLIM;
742 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
743 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
744 &optval, sizeof(optval)) == -1)
745 err(1, "IPV6_MULTICAST_HOPS");
746 #ifdef IPV6_USE_MIN_MTU
748 optval = mflag > 1 ? 0 : 1;
750 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
751 &optval, sizeof(optval)) == -1)
752 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
754 #ifdef IPV6_RECVPATHMTU
757 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
758 &optval, sizeof(optval)) == -1)
759 err(1, "setsockopt(IPV6_RECVPATHMTU)");
761 #endif /* IPV6_RECVPATHMTU */
762 #endif /* IPV6_USE_MIN_MTU */
765 #ifdef IPSEC_POLICY_IPSEC
766 if (options & F_POLICY) {
767 if (setpolicy(s, policy_in) < 0)
768 errx(1, "%s", ipsec_strerror());
769 if (setpolicy(s, policy_out) < 0)
770 errx(1, "%s", ipsec_strerror());
773 if (options & F_AUTHHDR) {
774 optval = IPSEC_LEVEL_REQUIRE;
775 #ifdef IPV6_AUTH_TRANS_LEVEL
776 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
777 &optval, sizeof(optval)) == -1)
778 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
780 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
781 &optval, sizeof(optval)) == -1)
782 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
785 if (options & F_ENCRYPT) {
786 optval = IPSEC_LEVEL_REQUIRE;
787 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
788 &optval, sizeof(optval)) == -1)
789 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
791 #endif /*IPSEC_POLICY_IPSEC*/
796 struct icmp6_filter filt;
797 if (!(options & F_VERBOSE)) {
798 ICMP6_FILTER_SETBLOCKALL(&filt);
799 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
800 (options & F_NODEADDR) || (options & F_SUPTYPES))
801 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
803 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
805 ICMP6_FILTER_SETPASSALL(&filt);
807 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
809 err(1, "setsockopt(ICMP6_FILTER)");
811 #endif /*ICMP6_FILTER*/
813 /* let the kerel pass extension headers of incoming packets */
814 if ((options & F_VERBOSE) != 0) {
817 #ifdef IPV6_RECVRTHDR
818 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
820 err(1, "setsockopt(IPV6_RECVRTHDR)");
821 #else /* old adv. API */
822 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
824 err(1, "setsockopt(IPV6_RTHDR)");
830 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
831 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
832 &optval, sizeof(optval)) == -1)
833 err(1, "IPV6_MULTICAST_LOOP");
836 /* Specify the outgoing interface and/or the source address */
838 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
841 ip6optlen += CMSG_SPACE(sizeof(int));
843 /* set IP6 packet options */
845 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
846 errx(1, "can't allocate enough memory");
847 smsghdr.msg_control = (caddr_t)scmsg;
848 smsghdr.msg_controllen = ip6optlen;
849 scmsgp = (struct cmsghdr *)scmsg;
852 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
853 memset(pktinfo, 0, sizeof(*pktinfo));
854 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
855 scmsgp->cmsg_level = IPPROTO_IPV6;
856 scmsgp->cmsg_type = IPV6_PKTINFO;
857 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
860 /* set the outgoing interface */
862 #ifndef USE_SIN6_SCOPE_ID
863 /* pktinfo must have already been allocated */
864 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
865 errx(1, "%s: invalid interface name", ifname);
867 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
868 errx(1, "%s: invalid interface name", ifname);
871 if (hoplimit != -1) {
872 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
873 scmsgp->cmsg_level = IPPROTO_IPV6;
874 scmsgp->cmsg_type = IPV6_HOPLIMIT;
875 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
877 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
880 if (argc > 1) { /* some intermediate addrs are specified */
882 #ifdef USE_RFC2292BIS
886 #ifdef USE_RFC2292BIS
887 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
888 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
889 scmsgp->cmsg_level = IPPROTO_IPV6;
890 scmsgp->cmsg_type = IPV6_RTHDR;
891 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
892 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
893 IPV6_RTHDR_TYPE_0, argc - 1);
895 errx(1, "can't initialize rthdr");
896 #else /* old advanced API */
897 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
898 IPV6_RTHDR_TYPE_0)) == 0)
899 errx(1, "can't initialize rthdr");
900 #endif /* USE_RFC2292BIS */
902 for (hops = 0; hops < argc - 1; hops++) {
903 struct addrinfo *iaip;
905 if ((error = getaddrinfo(argv[hops], NULL, &hints,
907 errx(1, "%s", gai_strerror(error));
908 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
910 "bad addr family of an intermediate addr");
912 #ifdef USE_RFC2292BIS
913 if (inet6_rth_add(rthdr,
914 &(SIN6(iaip->ai_addr))->sin6_addr))
915 errx(1, "can't add an intermediate node");
916 #else /* old advanced API */
917 if (inet6_rthdr_add(scmsgp,
918 &(SIN6(iaip->ai_addr))->sin6_addr,
920 errx(1, "can't add an intermediate node");
921 #endif /* USE_RFC2292BIS */
925 #ifndef USE_RFC2292BIS
926 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
927 errx(1, "can't set the last flag");
930 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
933 if (!(options & F_SRCADDR)) {
935 * get the source address. XXX since we revoked the root
936 * privilege, we cannot use a raw socket for this.
939 socklen_t len = sizeof(src);
941 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
942 err(1, "UDP socket");
944 src.sin6_family = AF_INET6;
945 src.sin6_addr = dst.sin6_addr;
946 src.sin6_port = ntohs(DUMMY_PORT);
947 src.sin6_scope_id = dst.sin6_scope_id;
949 #ifdef USE_RFC2292BIS
951 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
952 (void *)pktinfo, sizeof(*pktinfo)))
953 err(1, "UDP setsockopt(IPV6_PKTINFO)");
955 if (hoplimit != -1 &&
956 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
957 (void *)&hoplimit, sizeof(hoplimit)))
958 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
960 if (hoplimit != -1 &&
961 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
962 (void *)&hoplimit, sizeof(hoplimit)))
963 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
966 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
967 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
968 err(1, "UDP setsockopt(IPV6_RTHDR)");
969 #else /* old advanced API */
970 if (smsghdr.msg_control &&
971 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
972 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
973 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
976 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
977 err(1, "UDP connect");
979 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
980 err(1, "getsockname");
985 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
987 if (datalen > sockbufsize)
988 warnx("you need -b to increase socket buffer size");
989 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
990 sizeof(sockbufsize)) < 0)
991 err(1, "setsockopt(SO_SNDBUF)");
992 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
993 sizeof(sockbufsize)) < 0)
994 err(1, "setsockopt(SO_RCVBUF)");
997 if (datalen > 8 * 1024) /*XXX*/
998 warnx("you need -b to increase socket buffer size");
1000 * When pinging the broadcast address, you can get a lot of
1001 * answers. Doing something so evil is useful if you are trying
1002 * to stress the ethernet, or just want to fill the arp cache
1003 * to get some stuff for /etc/ethers.
1006 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1012 #ifndef USE_SIN6_SCOPE_ID
1013 #ifdef IPV6_RECVPKTINFO
1014 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1015 sizeof(optval)) < 0)
1016 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1017 #else /* old adv. API */
1018 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1019 sizeof(optval)) < 0)
1020 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1022 #endif /* USE_SIN6_SCOPE_ID */
1023 #ifdef IPV6_RECVHOPLIMIT
1024 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1025 sizeof(optval)) < 0)
1026 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1027 #else /* old adv. API */
1028 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1029 sizeof(optval)) < 0)
1030 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1033 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1034 (unsigned long)(pingerlen() - 8));
1035 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1036 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1038 while (preload--) /* Fire off them quickies. */
1041 (void)signal(SIGINT, onsignal);
1043 (void)signal(SIGINFO, onsignal);
1046 if ((options & F_FLOOD) == 0) {
1047 (void)signal(SIGALRM, onsignal);
1048 itimer.it_interval = interval;
1049 itimer.it_value = interval;
1050 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1051 if (ntransmitted == 0)
1056 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
1057 if ((fdmaskp = malloc(fdmasks)) == NULL)
1061 seenalrm = seenint = 0;
1066 /* For control (ancillary) data received from recvmsg() */
1067 cm = (struct cmsghdr *)malloc(CONTROLLEN);
1073 struct iovec iov[2];
1075 /* signal handling */
1094 if (options & F_FLOOD) {
1100 timeout.tv_usec = 10000;
1112 fdmaskp[0].events = POLLIN;
1113 cc = poll(fdmaskp, 1, timeout);
1115 memset(fdmaskp, 0, fdmasks);
1117 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1120 if (errno != EINTR) {
1132 m.msg_name = (caddr_t)&from;
1133 m.msg_namelen = sizeof(from);
1134 memset(&iov, 0, sizeof(iov));
1135 iov[0].iov_base = (caddr_t)packet;
1136 iov[0].iov_len = packlen;
1139 memset(cm, 0, CONTROLLEN);
1140 m.msg_control = (void *)cm;
1141 m.msg_controllen = CONTROLLEN;
1143 cc = recvmsg(s, &m, 0);
1145 if (errno != EINTR) {
1150 } else if (cc == 0) {
1154 * receive control messages only. Process the
1155 * exceptions (currently the only possiblity is
1156 * a path MTU notification.)
1158 if ((mtu = get_pathmtu(&m)) > 0) {
1159 if ((options & F_VERBOSE) != 0) {
1160 printf("new path MTU (%d) is "
1167 * an ICMPv6 message (probably an echoreply) arrived.
1169 pr_pack(packet, cc, &m);
1171 if (((options & F_ONCE) != 0 && nreceived > 0) ||
1172 (npackets > 0 && nreceived >= npackets))
1176 exit(nreceived == 0);
1201 * This routine transmits another ping6.
1206 struct itimerval itimer;
1212 * If we're not transmitting any more packets, change the timer
1213 * to wait two round-trip times if we've received any packets or
1214 * ten seconds if we haven't.
1218 itimer.it_value.tv_sec = 2 * tmax / 1000;
1219 if (itimer.it_value.tv_sec == 0)
1220 itimer.it_value.tv_sec = 1;
1222 itimer.it_value.tv_sec = MAXWAIT;
1223 itimer.it_interval.tv_sec = 0;
1224 itimer.it_interval.tv_usec = 0;
1225 itimer.it_value.tv_usec = 0;
1227 (void)signal(SIGALRM, onint);
1228 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1233 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1234 * will be added on by the kernel. The ID field is our UNIX process ID,
1235 * and the sequence number is an ascending integer. The first 8 bytes
1236 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1237 * byte-order, to compute the round-trip time.
1244 if (options & F_FQDN)
1245 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1246 else if (options & F_FQDNOLD)
1248 else if (options & F_NODEADDR)
1249 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1250 else if (options & F_SUPTYPES)
1253 l = ICMP6ECHOLEN + datalen;
1261 struct icmp6_hdr *icp;
1262 struct iovec iov[2];
1264 struct icmp6_nodeinfo *nip;
1267 if (npackets && ntransmitted >= npackets)
1268 return(-1); /* no more transmission */
1270 icp = (struct icmp6_hdr *)outpack;
1271 nip = (struct icmp6_nodeinfo *)outpack;
1272 memset(icp, 0, sizeof(*icp));
1273 icp->icmp6_cksum = 0;
1274 seq = ntransmitted++;
1275 CLR(seq % mx_dup_ck);
1277 if (options & F_FQDN) {
1278 icp->icmp6_type = ICMP6_NI_QUERY;
1279 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1280 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1281 nip->ni_flags = htons(0);
1283 memcpy(nip->icmp6_ni_nonce, nonce,
1284 sizeof(nip->icmp6_ni_nonce));
1285 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1287 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1288 sizeof(dst.sin6_addr));
1289 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1291 } else if (options & F_FQDNOLD) {
1292 /* packet format in 03 draft - no Subject data on queries */
1293 icp->icmp6_type = ICMP6_NI_QUERY;
1294 icp->icmp6_code = 0; /* code field is always 0 */
1295 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1296 nip->ni_flags = htons(0);
1298 memcpy(nip->icmp6_ni_nonce, nonce,
1299 sizeof(nip->icmp6_ni_nonce));
1300 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1304 } else if (options & F_NODEADDR) {
1305 icp->icmp6_type = ICMP6_NI_QUERY;
1306 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1307 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1308 nip->ni_flags = naflags;
1310 memcpy(nip->icmp6_ni_nonce, nonce,
1311 sizeof(nip->icmp6_ni_nonce));
1312 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1314 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1315 sizeof(dst.sin6_addr));
1316 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1318 } else if (options & F_SUPTYPES) {
1319 icp->icmp6_type = ICMP6_NI_QUERY;
1320 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1321 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1322 /* we support compressed bitmap */
1323 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1325 memcpy(nip->icmp6_ni_nonce, nonce,
1326 sizeof(nip->icmp6_ni_nonce));
1327 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1331 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1332 icp->icmp6_code = 0;
1333 icp->icmp6_id = htons(ident);
1334 icp->icmp6_seq = ntohs(seq);
1338 (void)gettimeofday(&tv, NULL);
1339 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1340 tv32->tv32_sec = htonl(tv.tv_sec);
1341 tv32->tv32_usec = htonl(tv.tv_usec);
1343 cc = ICMP6ECHOLEN + datalen;
1347 if (pingerlen() != cc)
1348 errx(1, "internal error; length mismatch");
1351 smsghdr.msg_name = (caddr_t)&dst;
1352 smsghdr.msg_namelen = sizeof(dst);
1353 memset(&iov, 0, sizeof(iov));
1354 iov[0].iov_base = (caddr_t)outpack;
1355 iov[0].iov_len = cc;
1356 smsghdr.msg_iov = iov;
1357 smsghdr.msg_iovlen = 1;
1359 i = sendmsg(s, &smsghdr, 0);
1361 if (i < 0 || i != cc) {
1364 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1367 if (!(options & F_QUIET) && options & F_FLOOD)
1368 (void)write(STDOUT_FILENO, &DOT, 1);
1375 const struct icmp6_hdr *icp;
1377 if (ntohs(icp->icmp6_id) == ident)
1385 const struct icmp6_nodeinfo *nip;
1387 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1388 nonce + sizeof(u_int16_t),
1389 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1396 dnsdecode(sp, ep, base, buf, bufsiz)
1399 const u_char *base; /*base for compressed name*/
1405 char cresult[MAXDNAME + 1];
1416 if (i == 0 || cp != *sp) {
1417 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1418 return NULL; /*result overrun*/
1424 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1425 /* DNS compression */
1429 comp = base + (i & 0x3f);
1430 if (dnsdecode(&comp, cp, base, cresult,
1431 sizeof(cresult)) == NULL)
1433 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1434 return NULL; /*result overrun*/
1436 } else if ((i & 0x3f) == i) {
1438 return NULL; /*source overrun*/
1439 while (i-- > 0 && cp < ep) {
1440 l = snprintf(cresult, sizeof(cresult),
1441 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1442 if (l >= sizeof(cresult) || l < 0)
1444 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1445 return NULL; /*result overrun*/
1449 return NULL; /*invalid label*/
1452 return NULL; /*not terminated*/
1460 * Print out the packet, if it came from us. This logic is necessary
1461 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1462 * which arrive ('tis only fair). This permits multiple copies of this
1463 * program to be run without having intermingled output (or statistics!).
1466 pr_pack(buf, cc, mhdr)
1469 struct msghdr *mhdr;
1471 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1472 struct icmp6_hdr *icp;
1473 struct icmp6_nodeinfo *ni;
1476 struct sockaddr *from;
1478 u_char *cp = NULL, *dp, *end = buf + cc;
1479 struct in6_pktinfo *pktinfo = NULL;
1480 struct timeval tv, tp;
1482 double triptime = 0;
1487 char dnsname[MAXDNAME + 1];
1489 (void)gettimeofday(&tv, NULL);
1491 if (!mhdr || !mhdr->msg_name ||
1492 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1493 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1494 if (options & F_VERBOSE)
1495 warnx("invalid peername");
1498 from = (struct sockaddr *)mhdr->msg_name;
1499 fromlen = mhdr->msg_namelen;
1500 if (cc < sizeof(struct icmp6_hdr)) {
1501 if (options & F_VERBOSE)
1502 warnx("packet too short (%d bytes) from %s", cc,
1503 pr_addr(from, fromlen));
1506 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1507 (options & F_VERBOSE) != 0)
1508 warnx("some control data discarded, insufficient buffer size");
1509 icp = (struct icmp6_hdr *)buf;
1510 ni = (struct icmp6_nodeinfo *)buf;
1513 if ((hoplim = get_hoplim(mhdr)) == -1) {
1514 warnx("failed to get receiving hop limit");
1517 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1518 warnx("failed to get receiving packet information");
1522 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1523 seq = ntohs(icp->icmp6_seq);
1526 tpp = (struct tv32 *)(icp + 1);
1527 tp.tv_sec = ntohl(tpp->tv32_sec);
1528 tp.tv_usec = ntohl(tpp->tv32_usec);
1530 triptime = ((double)tv.tv_sec) * 1000.0 +
1531 ((double)tv.tv_usec) / 1000.0;
1533 tsumsq += triptime * triptime;
1534 if (triptime < tmin)
1536 if (triptime > tmax)
1540 if (TST(seq % mx_dup_ck)) {
1545 SET(seq % mx_dup_ck);
1549 if (options & F_QUIET)
1552 if (options & F_FLOOD)
1553 (void)write(STDOUT_FILENO, &BSPACE, 1);
1555 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1556 pr_addr(from, fromlen), seq);
1557 (void)printf(" hlim=%d", hoplim);
1558 if ((options & F_VERBOSE) != 0) {
1559 struct sockaddr_in6 dstsa;
1561 memset(&dstsa, 0, sizeof(dstsa));
1562 dstsa.sin6_family = AF_INET6;
1563 dstsa.sin6_len = sizeof(dstsa);
1564 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1565 dstsa.sin6_addr = pktinfo->ipi6_addr;
1566 (void)printf(" dst=%s",
1567 pr_addr((struct sockaddr *)&dstsa,
1571 (void)printf(" time=%.3f ms", triptime);
1573 (void)printf("(DUP!)");
1574 /* check the data */
1575 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1576 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1577 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1579 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1584 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1585 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1587 if (TST(seq % mx_dup_ck)) {
1592 SET(seq % mx_dup_ck);
1596 if (options & F_QUIET)
1599 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1601 switch (ntohs(ni->ni_code)) {
1602 case ICMP6_NI_SUCCESS:
1604 case ICMP6_NI_REFUSED:
1605 printf("refused, type 0x%x", ntohs(ni->ni_type));
1607 case ICMP6_NI_UNKNOWN:
1608 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1611 printf("unknown code 0x%x, type 0x%x",
1612 ntohs(ni->ni_code), ntohs(ni->ni_type));
1616 switch (ntohs(ni->ni_qtype)) {
1618 printf("NodeInfo NOOP");
1620 case NI_QTYPE_SUPTYPES:
1621 pr_suptypes(ni, end - (u_char *)ni);
1623 case NI_QTYPE_NODEADDR:
1624 pr_nodeaddr(ni, end - (u_char *)ni);
1627 default: /* XXX: for backward compatibility */
1628 cp = (u_char *)ni + ICMP6_NIRLEN;
1629 if (buf[off + ICMP6_NIRLEN] ==
1630 cc - off - ICMP6_NIRLEN - 1)
1635 cp++; /* skip length */
1637 safeputc(*cp & 0xff);
1643 if (dnsdecode((const u_char **)&cp, end,
1644 (const u_char *)(ni + 1), dnsname,
1645 sizeof(dnsname)) == NULL) {
1650 * name-lookup special handling for
1653 if (cp + 1 <= end && !*cp &&
1654 strlen(dnsname) > 0) {
1655 dnsname[strlen(dnsname) - 1] = '\0';
1658 printf("%s%s", i > 0 ? "," : "",
1662 if (options & F_VERBOSE) {
1666 (void)printf(" ("); /*)*/
1668 switch (ni->ni_code) {
1669 case ICMP6_NI_REFUSED:
1670 (void)printf("refused");
1673 case ICMP6_NI_UNKNOWN:
1674 (void)printf("unknown qtype");
1679 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1680 /* case of refusion, unknown */
1685 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1688 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1689 (void)printf("TTL=%d:meaningless",
1693 (void)printf("TTL=%d:invalid",
1696 (void)printf("TTL=%d", ttl);
1706 cp = (u_char *)ni + ICMP6_NIRLEN;
1715 if (buf[off + ICMP6_NIRLEN] !=
1716 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1719 (void)printf("invalid namelen:%d/%lu",
1720 buf[off + ICMP6_NIRLEN],
1721 (u_long)cc - off - ICMP6_NIRLEN - 1);
1731 /* We've got something other than an ECHOREPLY */
1732 if (!(options & F_VERBOSE))
1734 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1738 if (!(options & F_FLOOD)) {
1739 (void)putchar('\n');
1740 if (options & F_VERBOSE)
1742 (void)fflush(stdout);
1749 struct msghdr *mhdr;
1756 bufp = mhdr->msg_control;
1757 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1758 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1759 if (cm->cmsg_level != IPPROTO_IPV6)
1762 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1765 switch (cm->cmsg_type) {
1767 printf(" HbH Options: ");
1768 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1771 #ifdef IPV6_RTHDRDSTOPTS
1772 case IPV6_RTHDRDSTOPTS:
1774 printf(" Dst Options: ");
1775 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1778 printf(" Routing: ");
1779 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1785 #ifdef USE_RFC2292BIS
1787 pr_ip6opt(void *extbuf, size_t bufsize)
1789 struct ip6_hbh *ext;
1792 socklen_t extlen, len, origextlen;
1798 ext = (struct ip6_hbh *)extbuf;
1799 extlen = (ext->ip6h_len + 1) * 8;
1800 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1801 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1804 * Bounds checking on the ancillary data buffer:
1805 * subtract the size of a cmsg structure from the buffer size.
1807 if (bufsize < (extlen + CMSG_SPACE(0))) {
1808 origextlen = extlen;
1809 extlen = bufsize - CMSG_SPACE(0);
1810 warnx("options truncated, showing only %u (total=%u)",
1811 (unsigned int)(extlen / 8 - 1),
1812 (unsigned int)(ext->ip6h_len));
1817 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1818 &type, &len, &databuf);
1819 if (currentlen == -1)
1823 * Note that inet6_opt_next automatically skips any padding
1828 offset = inet6_opt_get_val(databuf, offset,
1829 &value4, sizeof(value4));
1830 printf(" Jumbo Payload Opt: Length %u\n",
1831 (u_int32_t)ntohl(value4));
1833 case IP6OPT_ROUTER_ALERT:
1835 offset = inet6_opt_get_val(databuf, offset,
1836 &value2, sizeof(value2));
1837 printf(" Router Alert Opt: Type %u\n",
1841 printf(" Received Opt %u len %lu\n",
1842 type, (unsigned long)len);
1848 #else /* !USE_RFC2292BIS */
1851 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1856 #endif /* USE_RFC2292BIS */
1858 #ifdef USE_RFC2292BIS
1860 pr_rthdr(void *extbuf, size_t bufsize)
1862 struct in6_addr *in6;
1863 char ntopbuf[INET6_ADDRSTRLEN];
1864 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1865 int i, segments, origsegs, rthsize, size0, size1;
1867 /* print fixed part of the header */
1868 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1869 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1870 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1871 printf("%d segments, ", segments);
1872 printf("%d left\n", rh->ip6r_segleft);
1874 printf("segments unknown, ");
1875 printf("%d left\n", rh->ip6r_segleft);
1880 * Bounds checking on the ancillary data buffer. When calculating
1881 * the number of items to show keep in mind:
1882 * - The size of the cmsg structure
1883 * - The size of one segment (the size of a Type 0 routing header)
1884 * - When dividing add a fudge factor of one in case the
1885 * dividend is not evenly divisible by the divisor
1887 rthsize = (rh->ip6r_len + 1) * 8;
1888 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1889 origsegs = segments;
1890 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1891 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1892 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1893 (size1 - size0) + 1;
1894 warnx("segments truncated, showing only %d (total=%d)",
1895 segments, origsegs);
1898 for (i = 0; i < segments; i++) {
1899 in6 = inet6_rth_getaddr(extbuf, i);
1901 printf(" [%d]<NULL>\n", i);
1903 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1905 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1906 printf(" [%d]%s\n", i, ntopbuf);
1914 #else /* !USE_RFC2292BIS */
1917 pr_rthdr(void *extbuf, size_t bufsize __unused)
1922 #endif /* USE_RFC2292BIS */
1925 pr_bitrange(v, soff, 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(ni, nilen)
1975 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
1980 const u_char *cp, *end;
1983 u_int16_t words; /*32bit count*/
1986 #define MAXQTYPES (1 << 16)
1990 cp = (u_char *)(ni + 1);
1991 end = ((u_char *)ni) + nilen;
1995 printf("NodeInfo Supported Qtypes");
1996 if (options & F_VERBOSE) {
1997 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1998 printf(", compressed bitmap");
2000 printf(", raw bitmap");
2004 clen = (size_t)(end - cp);
2005 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2006 if (clen == 0 || clen > MAXQTYPES / 8 ||
2012 if (clen < sizeof(cbit) || clen % sizeof(v))
2014 memcpy(&cbit, cp, sizeof(cbit));
2015 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2019 clen = ntohs(cbit.words) * sizeof(v);
2020 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2025 for (off = 0; off < clen; off += sizeof(v)) {
2026 memcpy(&v, cp + off, sizeof(v));
2027 v = (u_int32_t)ntohl(v);
2028 b = pr_bitrange(v, (int)(cur + off * 8), b);
2030 /* flush the remaining bits */
2031 b = pr_bitrange(0, (int)(cur + off * 8), b);
2035 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2036 cur += ntohs(cbit.skip) * 32;
2041 pr_nodeaddr(ni, nilen)
2042 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
2045 u_char *cp = (u_char *)(ni + 1);
2046 char ntop_buf[INET6_ADDRSTRLEN];
2049 nilen -= sizeof(struct icmp6_nodeinfo);
2051 if (options & F_VERBOSE) {
2052 switch (ni->ni_code) {
2053 case ICMP6_NI_REFUSED:
2054 (void)printf("refused");
2056 case ICMP6_NI_UNKNOWN:
2057 (void)printf("unknown qtype");
2060 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2061 (void)printf(" truncated");
2065 printf(" no address\n");
2068 * In icmp-name-lookups 05 and later, TTL of each returned address
2069 * is contained in the resposne. We try to detect the version
2070 * by the length of the data, but note that the detection algorithm
2071 * is incomplete. We assume the latest draft by default.
2073 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2079 /* XXX: alignment? */
2080 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2081 cp += sizeof(u_int32_t);
2082 nilen -= sizeof(u_int32_t);
2085 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2087 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2088 printf(" %s", ntop_buf);
2090 if (ttl == 0xffffffff) {
2092 * XXX: can this convention be applied to all
2093 * type of TTL (i.e. non-ND TTL)?
2095 printf("(TTL=infty)");
2098 printf("(TTL=%u)", ttl);
2102 nilen -= sizeof(struct in6_addr);
2103 cp += sizeof(struct in6_addr);
2109 struct msghdr *mhdr;
2113 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2114 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2115 if (cm->cmsg_len == 0)
2118 if (cm->cmsg_level == IPPROTO_IPV6 &&
2119 cm->cmsg_type == IPV6_HOPLIMIT &&
2120 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2121 return(*(int *)CMSG_DATA(cm));
2127 struct in6_pktinfo *
2128 get_rcvpktinfo(mhdr)
2129 struct msghdr *mhdr;
2133 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2134 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2135 if (cm->cmsg_len == 0)
2138 if (cm->cmsg_level == IPPROTO_IPV6 &&
2139 cm->cmsg_type == IPV6_PKTINFO &&
2140 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2141 return((struct in6_pktinfo *)CMSG_DATA(cm));
2149 struct msghdr *mhdr;
2151 #ifdef IPV6_RECVPATHMTU
2153 struct ip6_mtuinfo *mtuctl = NULL;
2155 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2156 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2157 if (cm->cmsg_len == 0)
2160 if (cm->cmsg_level == IPPROTO_IPV6 &&
2161 cm->cmsg_type == IPV6_PATHMTU &&
2162 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2163 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2166 * If the notified destination is different from
2167 * the one we are pinging, just ignore the info.
2168 * We check the scope ID only when both notified value
2169 * and our own value have non-0 values, because we may
2170 * have used the default scope zone ID for sending,
2171 * in which case the scope ID value is 0.
2173 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2175 (mtuctl->ip6m_addr.sin6_scope_id &&
2176 dst.sin6_scope_id &&
2177 mtuctl->ip6m_addr.sin6_scope_id !=
2178 dst.sin6_scope_id)) {
2179 if ((options & F_VERBOSE) != 0) {
2180 printf("path MTU for %s is notified. "
2182 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2183 sizeof(mtuctl->ip6m_addr)));
2189 * Ignore an invalid MTU. XXX: can we just believe
2192 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2195 /* notification for our destination. return the MTU. */
2196 return((int)mtuctl->ip6m_mtu);
2205 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2210 struct timeval *out, *in;
2212 if ((out->tv_usec -= in->tv_usec) < 0) {
2214 out->tv_usec += 1000000;
2216 out->tv_sec -= in->tv_sec;
2230 (void)signal(SIGINT, SIG_DFL);
2231 (void)kill(getpid(), SIGINT);
2239 * Print out statistics.
2245 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2246 (void)printf("%ld packets transmitted, ", ntransmitted);
2247 (void)printf("%ld packets received, ", nreceived);
2249 (void)printf("+%ld duplicates, ", nrepeats);
2251 if (nreceived > ntransmitted)
2252 (void)printf("-- somebody's duplicating packets!");
2254 (void)printf("%.1f%% packet loss",
2255 ((((double)ntransmitted - nreceived) * 100.0) /
2258 (void)putchar('\n');
2259 if (nreceived && timing) {
2260 /* Only display average to microseconds */
2261 double num = nreceived + nrepeats;
2262 double avg = tsum / num;
2263 double dev = sqrt(tsumsq / num - avg * avg);
2265 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2266 tmin, avg, tmax, dev);
2267 (void)fflush(stdout);
2269 (void)fflush(stdout);
2273 static const char *niqcode[] = {
2275 "DNS label", /*or empty*/
2280 static const char *nircode[] = {
2281 "Success", "Refused", "Unknown",
2287 * Print a descriptive string about an ICMP header.
2291 struct icmp6_hdr *icp;
2294 char ntop_buf[INET6_ADDRSTRLEN];
2295 struct nd_redirect *red;
2296 struct icmp6_nodeinfo *ni;
2297 char dnsname[MAXDNAME + 1];
2301 switch (icp->icmp6_type) {
2302 case ICMP6_DST_UNREACH:
2303 switch (icp->icmp6_code) {
2304 case ICMP6_DST_UNREACH_NOROUTE:
2305 (void)printf("No Route to Destination\n");
2307 case ICMP6_DST_UNREACH_ADMIN:
2308 (void)printf("Destination Administratively "
2311 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2312 (void)printf("Destination Unreachable Beyond Scope\n");
2314 case ICMP6_DST_UNREACH_ADDR:
2315 (void)printf("Destination Host Unreachable\n");
2317 case ICMP6_DST_UNREACH_NOPORT:
2318 (void)printf("Destination Port Unreachable\n");
2321 (void)printf("Destination Unreachable, Bad Code: %d\n",
2325 /* Print returned IP header information */
2326 pr_retip((struct ip6_hdr *)(icp + 1), end);
2328 case ICMP6_PACKET_TOO_BIG:
2329 (void)printf("Packet too big mtu = %d\n",
2330 (int)ntohl(icp->icmp6_mtu));
2331 pr_retip((struct ip6_hdr *)(icp + 1), end);
2333 case ICMP6_TIME_EXCEEDED:
2334 switch (icp->icmp6_code) {
2335 case ICMP6_TIME_EXCEED_TRANSIT:
2336 (void)printf("Time to live exceeded\n");
2338 case ICMP6_TIME_EXCEED_REASSEMBLY:
2339 (void)printf("Frag reassembly time exceeded\n");
2342 (void)printf("Time exceeded, Bad Code: %d\n",
2346 pr_retip((struct ip6_hdr *)(icp + 1), end);
2348 case ICMP6_PARAM_PROB:
2349 (void)printf("Parameter problem: ");
2350 switch (icp->icmp6_code) {
2351 case ICMP6_PARAMPROB_HEADER:
2352 (void)printf("Erroneous Header ");
2354 case ICMP6_PARAMPROB_NEXTHEADER:
2355 (void)printf("Unknown Nextheader ");
2357 case ICMP6_PARAMPROB_OPTION:
2358 (void)printf("Unrecognized Option ");
2361 (void)printf("Bad code(%d) ", icp->icmp6_code);
2364 (void)printf("pointer = 0x%02x\n",
2365 (u_int32_t)ntohl(icp->icmp6_pptr));
2366 pr_retip((struct ip6_hdr *)(icp + 1), end);
2368 case ICMP6_ECHO_REQUEST:
2369 (void)printf("Echo Request");
2370 /* XXX ID + Seq + Data */
2372 case ICMP6_ECHO_REPLY:
2373 (void)printf("Echo Reply");
2374 /* XXX ID + Seq + Data */
2376 case ICMP6_MEMBERSHIP_QUERY:
2377 (void)printf("Listener Query");
2379 case ICMP6_MEMBERSHIP_REPORT:
2380 (void)printf("Listener Report");
2382 case ICMP6_MEMBERSHIP_REDUCTION:
2383 (void)printf("Listener Done");
2385 case ND_ROUTER_SOLICIT:
2386 (void)printf("Router Solicitation");
2388 case ND_ROUTER_ADVERT:
2389 (void)printf("Router Advertisement");
2391 case ND_NEIGHBOR_SOLICIT:
2392 (void)printf("Neighbor Solicitation");
2394 case ND_NEIGHBOR_ADVERT:
2395 (void)printf("Neighbor Advertisement");
2398 red = (struct nd_redirect *)icp;
2399 (void)printf("Redirect\n");
2400 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2402 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2403 (void)printf("Destination: %s", ntop_buf);
2404 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2406 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2407 (void)printf(" New Target: %s", ntop_buf);
2409 case ICMP6_NI_QUERY:
2410 (void)printf("Node Information Query");
2411 /* XXX ID + Seq + Data */
2412 ni = (struct icmp6_nodeinfo *)icp;
2413 l = end - (u_char *)(ni + 1);
2415 switch (ntohs(ni->ni_qtype)) {
2417 (void)printf("NOOP");
2419 case NI_QTYPE_SUPTYPES:
2420 (void)printf("Supported qtypes");
2423 (void)printf("DNS name");
2425 case NI_QTYPE_NODEADDR:
2426 (void)printf("nodeaddr");
2428 case NI_QTYPE_IPV4ADDR:
2429 (void)printf("IPv4 nodeaddr");
2432 (void)printf("unknown qtype");
2435 if (options & F_VERBOSE) {
2436 switch (ni->ni_code) {
2437 case ICMP6_NI_SUBJ_IPV6:
2438 if (l == sizeof(struct in6_addr) &&
2439 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2440 sizeof(ntop_buf)) != NULL) {
2441 (void)printf(", subject=%s(%s)",
2442 niqcode[ni->ni_code], ntop_buf);
2445 /* backward compat to -W */
2446 (void)printf(", oldfqdn");
2448 (void)printf(", invalid");
2452 case ICMP6_NI_SUBJ_FQDN:
2453 if (end == (u_char *)(ni + 1)) {
2454 (void)printf(", no subject");
2457 printf(", subject=%s", niqcode[ni->ni_code]);
2458 cp = (const u_char *)(ni + 1);
2459 if (dnsdecode(&cp, end, NULL, dnsname,
2460 sizeof(dnsname)) != NULL)
2461 printf("(%s)", dnsname);
2463 printf("(invalid)");
2465 case ICMP6_NI_SUBJ_IPV4:
2466 if (l == sizeof(struct in_addr) &&
2467 inet_ntop(AF_INET, ni + 1, ntop_buf,
2468 sizeof(ntop_buf)) != NULL) {
2469 (void)printf(", subject=%s(%s)",
2470 niqcode[ni->ni_code], ntop_buf);
2472 (void)printf(", invalid");
2475 (void)printf(", invalid");
2480 case ICMP6_NI_REPLY:
2481 (void)printf("Node Information Reply");
2482 /* XXX ID + Seq + Data */
2483 ni = (struct icmp6_nodeinfo *)icp;
2485 switch (ntohs(ni->ni_qtype)) {
2487 (void)printf("NOOP");
2489 case NI_QTYPE_SUPTYPES:
2490 (void)printf("Supported qtypes");
2493 (void)printf("DNS name");
2495 case NI_QTYPE_NODEADDR:
2496 (void)printf("nodeaddr");
2498 case NI_QTYPE_IPV4ADDR:
2499 (void)printf("IPv4 nodeaddr");
2502 (void)printf("unknown qtype");
2505 if (options & F_VERBOSE) {
2506 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2507 printf(", invalid");
2509 printf(", %s", nircode[ni->ni_code]);
2513 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2519 * Print an IP6 header.
2523 struct ip6_hdr *ip6;
2525 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2527 char ntop_buf[INET6_ADDRSTRLEN];
2529 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2530 tc = (tc >> 4) & 0x0f;
2531 tc |= (ip6->ip6_vfc << 4);
2533 printf("Vr TC Flow Plen Nxt Hlim\n");
2534 printf(" %1x %02x %05x %04x %02x %02x\n",
2535 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2536 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2537 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2538 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2539 printf("%s->", ntop_buf);
2540 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2541 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2542 printf("%s\n", ntop_buf);
2547 * Return an ascii host address as a dotted quad and optionally with
2551 pr_addr(addr, addrlen)
2552 struct sockaddr *addr;
2555 static char buf[NI_MAXHOST];
2558 if ((options & F_HOSTNAME) == 0)
2559 flag |= NI_NUMERICHOST;
2561 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2569 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2573 struct ip6_hdr *ip6;
2576 u_char *cp = (u_char *)ip6, nh;
2579 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2584 hlen = sizeof(*ip6);
2588 while (end - cp >= 8) {
2590 case IPPROTO_HOPOPTS:
2592 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2593 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2595 case IPPROTO_DSTOPTS:
2597 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2598 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2600 case IPPROTO_FRAGMENT:
2602 hlen = sizeof(struct ip6_frag);
2603 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2605 case IPPROTO_ROUTING:
2607 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2608 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2613 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2614 nh = ((struct ah *)cp)->ah_nxt;
2617 case IPPROTO_ICMPV6:
2618 printf("ICMP6: type = %d, code = %d\n",
2625 printf("TCP: from port %u, to port %u (decimal)\n",
2626 (*cp * 256 + *(cp + 1)),
2627 (*(cp + 2) * 256 + *(cp + 3)));
2630 printf("UDP: from port %u, to port %u (decimal)\n",
2631 (*cp * 256 + *(cp + 1)),
2632 (*(cp + 2) * 256 + *(cp + 3)));
2635 printf("Unknown Header(%d)\n", nh);
2639 if ((cp += hlen) >= end)
2661 for (cp = patp; *cp; cp++)
2663 errx(1, "patterns must be specified as hex digits");
2665 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2666 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2667 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2668 &pat[13], &pat[14], &pat[15]);
2673 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);
2675 for (jj = 0; jj < ii; ++jj)
2676 bp[jj + kk] = pat[jj];
2677 if (!(options & F_QUIET)) {
2678 (void)printf("PATTERN: 0x");
2679 for (jj = 0; jj < ii; ++jj)
2680 (void)printf("%02x", bp[jj] & 0xFF);
2686 #ifdef IPSEC_POLICY_IPSEC
2688 setpolicy(so, policy)
2695 return 0; /* ignore */
2697 buf = ipsec_set_policy(policy, strlen(policy));
2699 errx(1, "%s", ipsec_strerror());
2700 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2701 ipsec_get_policylen(buf)) < 0)
2702 warnx("Unable to set IPsec policy");
2717 u_int8_t digest[16];
2720 char hbuf[NI_MAXHOST];
2721 struct in6_addr in6;
2723 p = strchr(name, '.');
2725 p = name + strlen(name);
2727 if (l > 63 || l > sizeof(hbuf) - 1)
2728 return NULL; /*label too long*/
2729 strncpy(hbuf, name, l);
2730 hbuf[(int)l] = '\0';
2732 for (q = name; *q; q++) {
2733 if (isupper(*(unsigned char *)q))
2734 *q = tolower(*(unsigned char *)q);
2737 /* generate 8 bytes of pseudo-random value. */
2738 memset(&ctxt, 0, sizeof(ctxt));
2741 MD5Update(&ctxt, &c, sizeof(c));
2742 MD5Update(&ctxt, (unsigned char *)name, l);
2743 MD5Final(digest, &ctxt);
2745 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2746 return NULL; /*XXX*/
2747 bcopy(digest, &in6.s6_addr[12], 4);
2749 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2752 return strdup(hbuf);
2758 (void)fprintf(stderr,
2759 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2764 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2768 #ifdef IPV6_USE_MIN_MTU
2772 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2773 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2774 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2778 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2779 "[hops ...] host\n");