2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1989, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
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 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
68 * measure round-trip-delays and packet loss across network paths.
72 * U. S. Army Ballistic Research Laboratory
76 * Public Domain. Distribution Unlimited.
78 * More statistics could always be gathered.
79 * This program has to run SUID to ROOT to access the ICMP socket.
84 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
85 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
86 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
87 * network attached to 1 or more interfaces)
89 #define USE_SIN6_SCOPE_ID 1
91 #define USE_RFC2292BIS 1
93 #define CMSG_SENDING_UNSUPPORTED 1
97 #include <sys/param.h>
99 #include <sys/socket.h>
100 #include <sys/time.h>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <netinet/ip6.h>
107 #include <netinet/icmp6.h>
108 #include <arpa/inet.h>
109 #include <arpa/nameser.h>
127 #include <netipsec/ah.h>
128 #include <netipsec/ipsec.h>
137 #define MAXPACKETLEN (IPV6_MAXPACKET - IP6LEN)
138 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
139 #define ICMP6ECHOTMLEN sizeof(struct tv32)
140 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
141 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
142 /* FQDN case, 64 bits of nonce + 32 bits ttl */
143 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
144 #define EXTRA 256 /* for AH and various other headers. weird. */
145 #define DEFDATALEN ICMP6ECHOTMLEN
146 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
147 #define NROUTES 9 /* number of record route slots */
149 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
150 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
151 #define SET(bit) (A(bit) |= B(bit))
152 #define CLR(bit) (A(bit) &= (~B(bit)))
153 #define TST(bit) (A(bit) & B(bit))
155 #define F_FLOOD 0x0001
156 #define F_INTERVAL 0x0002
157 #define F_PINGFILLED 0x0008
158 #define F_QUIET 0x0010
159 #define F_RROUTE 0x0020
160 #define F_SO_DEBUG 0x0040
161 #define F_VERBOSE 0x0100
163 #ifdef IPSEC_POLICY_IPSEC
164 #define F_POLICY 0x0400
166 #define F_AUTHHDR 0x0200
167 #define F_ENCRYPT 0x0400
168 #endif /*IPSEC_POLICY_IPSEC*/
170 #define F_NODEADDR 0x0800
171 #define F_FQDN 0x1000
172 #define F_INTERFACE 0x2000
173 #define F_SRCADDR 0x4000
174 #define F_HOSTNAME 0x10000
175 #define F_FQDNOLD 0x20000
176 #define F_NIGROUP 0x40000
177 #define F_SUPTYPES 0x80000
178 #define F_NOMINMTU 0x100000
179 #define F_ONCE 0x200000
180 #define F_AUDIBLE 0x400000
181 #define F_MISSED 0x800000
182 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
185 #define IN6LEN sizeof(struct in6_addr)
186 #define SA6LEN sizeof(struct sockaddr_in6)
187 #define DUMMY_PORT 10101
189 #define SIN6(s) ((struct sockaddr_in6 *)(s))
192 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
193 * number of received sequence numbers we can keep track of. Change 128
194 * to 8192 for complete accuracy...
196 #define MAX_DUP_CHK (8 * 8192)
197 int mx_dup_ck
= MAX_DUP_CHK
;
198 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
200 struct addrinfo
*res
;
201 struct sockaddr_in6 dst
; /* who to ping6 */
202 struct sockaddr_in6 src
; /* src addr of this packet */
204 int datalen
= DEFDATALEN
;
205 int s
; /* socket file descriptor */
206 u_char outpack
[MAXPACKETLEN
];
207 char BSPACE
= '\b'; /* characters written for flood */
208 char BBELL
= '\a'; /* characters written for AUDIBLE */
211 int ident
; /* process id to identify our packets */
212 u_int8_t nonce
[8]; /* nonce field for node information */
213 int hoplimit
= -1; /* hoplimit */
214 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
217 long nmissedmax
; /* max value of ntransmitted - nreceived - 1 */
218 long npackets
; /* max packets to transmit */
219 long nreceived
; /* # of packets we got back */
220 long nrepeats
; /* number of duplicates */
221 long ntransmitted
; /* sequence # for outbound packets = #sent */
222 struct timeval interval
= {1, 0}; /* interval between packets */
225 int timing
; /* flag to do timing */
226 double tmin
= 999999999.0; /* minimum round trip time */
227 double tmax
= 0.0; /* maximum round trip time */
228 double tsum
= 0.0; /* sum of all times, for doing average */
229 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
231 /* for node addresses */
234 /* for ancillary data(advanced API) */
235 struct msghdr smsghdr
;
236 struct iovec smsgiov
;
239 volatile sig_atomic_t seenalrm
;
240 volatile sig_atomic_t seenint
;
242 volatile sig_atomic_t seeninfo
;
245 int main(int, char *[]);
246 void fill(char *, char *);
247 int get_hoplim(struct msghdr
*);
248 int get_pathmtu(struct msghdr
*);
249 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
251 void retransmit(void);
253 size_t pingerlen(void);
255 const char *pr_addr(struct sockaddr
*, int);
256 void pr_icmph(struct icmp6_hdr
*, u_char
*);
257 void pr_iph(struct ip6_hdr
*);
258 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
259 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
260 int myechoreply(const struct icmp6_hdr
*);
261 int mynireply(const struct icmp6_nodeinfo
*);
262 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
264 void pr_pack(u_char
*, int, struct msghdr
*);
265 void pr_exthdrs(struct msghdr
*);
266 void pr_ip6opt(void *, size_t);
267 void pr_rthdr(void *, size_t);
268 int pr_bitrange(u_int32_t
, int, int);
269 void pr_retip(struct ip6_hdr
*, u_char
*);
271 void tvsub(struct timeval
*, struct timeval
*);
272 int setpolicy(int, char *);
273 char *nigroup(char *);
282 struct itimerval itimer
;
283 struct sockaddr_in6 from
;
284 #ifndef HAVE_ARC4RANDOM
290 struct timeval timeout
, *tv
;
292 struct addrinfo hints
;
294 struct pollfd fdmaskp
[1];
300 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
301 u_char
*datap
, *packet
;
302 char *e
, *target
, *ifname
= NULL
;
304 struct cmsghdr
*scmsgp
= NULL
;
306 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
311 struct in6_pktinfo
*pktinfo
= NULL
;
312 #ifdef IPSEC_POLICY_IPSEC
313 char *policy_in
= NULL
;
314 char *policy_out
= NULL
;
317 #ifdef IPV6_USE_MIN_MTU
321 /* just to be sure */
322 memset(&smsghdr
, 0, sizeof(smsghdr
));
323 memset(&smsgiov
, 0, sizeof(smsgiov
));
326 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
330 #ifdef IPSEC_POLICY_IPSEC
334 #endif /*IPSEC_POLICY_IPSEC*/
336 while ((ch
= getopt(argc
, argv
,
337 "a:b:c:dfHh:I:i:l:mnop:qrRS:s:tvwW" ADDOPTS
)) != -1) {
344 options
&= ~F_NOUSERDATA
;
345 options
|= F_NODEADDR
;
346 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
349 naflags
|= NI_NODEADDR_FLAG_ALL
;
353 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
357 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
361 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
365 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
367 case 'A': /* experimental. not in the spec */
368 #ifdef NI_NODEADDR_FLAG_ANYCAST
369 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
373 "-a A is not supported on the platform");
384 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
387 lsockbufsize
= strtoul(optarg
, &e
, 10);
388 sockbufsize
= lsockbufsize
;
389 if (errno
|| !*optarg
|| *e
||
390 sockbufsize
!= lsockbufsize
)
391 errx(1, "invalid socket buffer size");
394 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
398 npackets
= strtol(optarg
, &e
, 10);
399 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
401 "illegal number of packets -- %s", optarg
);
404 options
|= F_SO_DEBUG
;
409 errx(1, "Must be superuser to flood ping");
412 setbuf(stdout
, (char *)NULL
);
420 options
|= F_HOSTNAME
;
422 case 'h': /* hoplimit */
423 hoplimit
= strtol(optarg
, &e
, 10);
424 if (*optarg
== '\0' || *e
!= '\0')
425 errx(1, "illegal hoplimit %s", optarg
);
426 if (255 < hoplimit
|| hoplimit
< -1)
428 "illegal hoplimit -- %s", optarg
);
432 options
|= F_INTERFACE
;
433 #ifndef USE_SIN6_SCOPE_ID
437 case 'i': /* wait between sending packets */
438 intval
= strtod(optarg
, &e
);
439 if (*optarg
== '\0' || *e
!= '\0')
440 errx(1, "illegal timing interval %s", optarg
);
441 if (intval
< 1 && getuid()) {
442 errx(1, "%s: only root may use interval < 1s",
445 interval
.tv_sec
= (long)intval
;
447 (long)((intval
- interval
.tv_sec
) * 1000000);
448 if (interval
.tv_sec
< 0)
449 errx(1, "illegal timing interval %s", optarg
);
450 /* less than 1/hz does not make sense */
451 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 1) {
452 warnx("too small interval, raised to .000001");
453 interval
.tv_usec
= 1;
455 options
|= F_INTERVAL
;
460 errx(1, "Must be superuser to preload");
462 preload
= strtol(optarg
, &e
, 10);
463 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
464 errx(1, "illegal preload value -- %s", optarg
);
467 #ifdef IPV6_USE_MIN_MTU
471 errx(1, "-%c is not supported on this platform", ch
);
475 options
&= ~F_HOSTNAME
;
479 options
|= F_NIGROUP
;
485 case 'p': /* fill buffer with user pattern */
486 options
|= F_PINGFILLED
;
487 fill((char *)datap
, optarg
);
493 options
|= F_AUDIBLE
;
499 memset(&hints
, 0, sizeof(struct addrinfo
));
500 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
501 hints
.ai_family
= AF_INET6
;
502 hints
.ai_socktype
= SOCK_RAW
;
503 hints
.ai_protocol
= IPPROTO_ICMPV6
;
505 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
507 errx(1, "invalid source address: %s",
508 gai_strerror(ret_ga
));
511 * res->ai_family must be AF_INET6 and res->ai_addrlen
512 * must be sizeof(src).
514 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
515 srclen
= res
->ai_addrlen
;
517 options
|= F_SRCADDR
;
519 case 's': /* size of packet to send */
520 datalen
= strtol(optarg
, &e
, 10);
521 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
522 errx(1, "illegal datalen value -- %s", optarg
);
523 if (datalen
> MAXDATALEN
) {
525 "datalen value too large, maximum is %d",
530 options
&= ~F_NOUSERDATA
;
531 options
|= F_SUPTYPES
;
534 options
|= F_VERBOSE
;
537 options
&= ~F_NOUSERDATA
;
541 options
&= ~F_NOUSERDATA
;
542 options
|= F_FQDNOLD
;
545 #ifdef IPSEC_POLICY_IPSEC
548 if (!strncmp("in", optarg
, 2)) {
549 if ((policy_in
= strdup(optarg
)) == NULL
)
551 } else if (!strncmp("out", optarg
, 3)) {
552 if ((policy_out
= strdup(optarg
)) == NULL
)
555 errx(1, "invalid security policy");
559 options
|= F_AUTHHDR
;
562 options
|= F_ENCRYPT
;
564 #endif /*IPSEC_POLICY_IPSEC*/
581 if (options
& F_NIGROUP
) {
582 target
= nigroup(argv
[argc
- 1]);
583 if (target
== NULL
) {
589 target
= argv
[argc
- 1];
592 memset(&hints
, 0, sizeof(struct addrinfo
));
593 hints
.ai_flags
= AI_CANONNAME
;
594 hints
.ai_family
= AF_INET6
;
595 hints
.ai_socktype
= SOCK_RAW
;
596 hints
.ai_protocol
= IPPROTO_ICMPV6
;
598 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
600 errx(1, "%s", gai_strerror(ret_ga
));
601 if (res
->ai_canonname
)
602 hostname
= res
->ai_canonname
;
607 errx(1, "getaddrinfo failed");
609 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
611 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
612 res
->ai_protocol
)) < 0)
615 /* set the source address if specified. */
616 if ((options
& F_SRCADDR
) &&
617 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
622 /* set the gateway (next hop) if specified */
624 struct addrinfo ghints
, *gres
;
627 memset(&ghints
, 0, sizeof(ghints
));
628 ghints
.ai_family
= AF_INET6
;
629 ghints
.ai_socktype
= SOCK_RAW
;
630 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
632 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
634 errx(1, "getaddrinfo for the gateway %s: %s",
635 gateway
, gai_strerror(error
));
637 if (gres
->ai_next
&& (options
& F_VERBOSE
))
638 warnx("gateway resolves to multiple addresses");
640 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
641 gres
->ai_addr
, gres
->ai_addrlen
)) {
642 err(1, "setsockopt(IPV6_NEXTHOP)");
650 * let the kerel pass extension headers of incoming packets,
651 * for privileged socket options
653 if ((options
& F_VERBOSE
) != 0) {
656 #ifdef IPV6_RECVHOPOPTS
657 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
659 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
661 #ifdef IPV6_RECVDSTOPTS
662 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
664 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
666 #ifdef IPV6_RECVRTHDRDSTOPTS
667 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
669 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
673 /* revoke root privilege */
677 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
678 errx(1, "-f and -i incompatible options");
680 if ((options
& F_NOUSERDATA
) == 0) {
681 if (datalen
>= sizeof(struct tv32
)) {
682 /* we can time transfer */
686 /* in F_VERBOSE case, we may get non-echoreply packets*/
687 if (options
& F_VERBOSE
)
688 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
690 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
692 /* suppress timing for node information query */
695 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
698 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
699 err(1, "Unable to allocate packet");
700 if (!(options
& F_PINGFILLED
))
701 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
704 ident
= getpid() & 0xFFFF;
705 #ifndef HAVE_ARC4RANDOM
706 gettimeofday(&seed
, NULL
);
707 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
708 memset(nonce
, 0, sizeof(nonce
));
709 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
710 *((int *)&nonce
[i
]) = rand();
712 memset(nonce
, 0, sizeof(nonce
));
713 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
714 *((u_int32_t
*)&nonce
[i
]) = arc4random();
719 if (options
& F_SO_DEBUG
)
720 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
722 optval
= IPV6_DEFHLIM
;
723 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
724 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
725 &optval
, sizeof(optval
)) == -1)
726 err(1, "IPV6_MULTICAST_HOPS");
727 #ifdef IPV6_USE_MIN_MTU
729 optval
= mflag
> 1 ? 0 : 1;
731 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
732 &optval
, sizeof(optval
)) == -1)
733 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
735 #ifdef IPV6_RECVPATHMTU
738 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
739 &optval
, sizeof(optval
)) == -1)
740 err(1, "setsockopt(IPV6_RECVPATHMTU)");
742 #endif /* IPV6_RECVPATHMTU */
743 #endif /* IPV6_USE_MIN_MTU */
746 #ifdef IPSEC_POLICY_IPSEC
747 if (options
& F_POLICY
) {
748 if (setpolicy(s
, policy_in
) < 0)
749 errx(1, "%s", ipsec_strerror());
750 if (setpolicy(s
, policy_out
) < 0)
751 errx(1, "%s", ipsec_strerror());
754 if (options
& F_AUTHHDR
) {
755 optval
= IPSEC_LEVEL_REQUIRE
;
756 #ifdef IPV6_AUTH_TRANS_LEVEL
757 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
758 &optval
, sizeof(optval
)) == -1)
759 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
761 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
762 &optval
, sizeof(optval
)) == -1)
763 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
766 if (options
& F_ENCRYPT
) {
767 optval
= IPSEC_LEVEL_REQUIRE
;
768 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
769 &optval
, sizeof(optval
)) == -1)
770 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
772 #endif /*IPSEC_POLICY_IPSEC*/
777 struct icmp6_filter filt
;
778 if (!(options
& F_VERBOSE
)) {
779 ICMP6_FILTER_SETBLOCKALL(&filt
);
780 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
781 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
782 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
784 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
786 ICMP6_FILTER_SETPASSALL(&filt
);
788 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
790 err(1, "setsockopt(ICMP6_FILTER)");
792 #endif /*ICMP6_FILTER*/
794 /* let the kerel pass extension headers of incoming packets */
795 if ((options
& F_VERBOSE
) != 0) {
798 #ifdef IPV6_RECVRTHDR
799 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
801 err(1, "setsockopt(IPV6_RECVRTHDR)");
807 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
808 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
809 &optval, sizeof(optval)) == -1)
810 err(1, "IPV6_MULTICAST_LOOP");
813 /* Specify the outgoing interface and/or the source address */
815 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
818 ip6optlen
+= CMSG_SPACE(sizeof(int));
820 /* set IP6 packet options */
822 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
823 errx(1, "can't allocate enough memory");
824 smsghdr
.msg_control
= (caddr_t
)scmsg
;
825 smsghdr
.msg_controllen
= ip6optlen
;
826 scmsgp
= (struct cmsghdr
*)scmsg
;
829 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
830 memset(pktinfo
, 0, sizeof(*pktinfo
));
831 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
832 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
833 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
834 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
837 /* set the outgoing interface */
839 #ifndef USE_SIN6_SCOPE_ID
840 /* pktinfo must have already been allocated */
841 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
842 errx(1, "%s: invalid interface name", ifname
);
844 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
845 errx(1, "%s: invalid interface name", ifname
);
848 if (hoplimit
!= -1) {
849 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
850 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
851 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
852 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
854 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
857 if (!(options
& F_SRCADDR
)) {
859 * get the source address. XXX since we revoked the root
860 * privilege, we cannot use a raw socket for this.
863 socklen_t len
= sizeof(src
);
865 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
866 err(1, "UDP socket");
868 src
.sin6_family
= AF_INET6
;
869 src
.sin6_addr
= dst
.sin6_addr
;
870 src
.sin6_port
= ntohs(DUMMY_PORT
);
871 src
.sin6_scope_id
= dst
.sin6_scope_id
;
873 #ifdef USE_RFC2292BIS
875 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
876 (void *)pktinfo
, sizeof(*pktinfo
)))
877 err(1, "UDP setsockopt(IPV6_PKTINFO)");
879 if (hoplimit
!= -1 &&
880 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
881 (void *)&hoplimit
, sizeof(hoplimit
)))
882 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
884 if (hoplimit
!= -1 &&
885 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
886 (void *)&hoplimit
, sizeof(hoplimit
)))
887 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
889 #else /* old advanced API */
890 if (smsghdr
.msg_control
&&
891 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
892 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
893 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
896 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
897 err(1, "UDP connect");
899 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
900 err(1, "getsockname");
905 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
907 if (datalen
> sockbufsize
)
908 warnx("you need -b to increase socket buffer size");
909 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
910 sizeof(sockbufsize
)) < 0)
911 err(1, "setsockopt(SO_SNDBUF)");
912 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
913 sizeof(sockbufsize
)) < 0)
914 err(1, "setsockopt(SO_RCVBUF)");
917 if (datalen
> 8 * 1024) /*XXX*/
918 warnx("you need -b to increase socket buffer size");
920 * When pinging the broadcast address, you can get a lot of
921 * answers. Doing something so evil is useful if you are trying
922 * to stress the ethernet, or just want to fill the arp cache
923 * to get some stuff for /etc/ethers.
926 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
932 //#ifndef USE_SIN6_SCOPE_ID
933 #ifdef IPV6_RECVPKTINFO
934 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
936 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
937 #else /* old adv. API */
938 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
940 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
942 //#endif /* USE_SIN6_SCOPE_ID */
943 #ifdef IPV6_RECVHOPLIMIT
944 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
946 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
947 #else /* old adv. API */
948 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
950 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
953 if (hoplimit
!= -1 &&
954 setsockopt(s
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
955 (void *)&hoplimit
, sizeof(hoplimit
)))
956 err(1, "setsockopt(IPV6_UNICAST_HOPS)");
958 if (hoplimit
!= -1 &&
959 setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
960 (void *)&hoplimit
, sizeof(hoplimit
)))
961 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
964 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
965 (unsigned long)(pingerlen() - 8));
966 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
967 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
969 while (preload
--) /* Fire off them quickies. */
972 (void)signal(SIGINT
, onsignal
);
974 (void)signal(SIGINFO
, onsignal
);
977 if ((options
& F_FLOOD
) == 0) {
978 (void)signal(SIGALRM
, onsignal
);
979 itimer
.it_interval
= interval
;
980 itimer
.it_value
= interval
;
981 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
982 if (ntransmitted
== 0)
987 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
988 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
992 seenalrm
= seenint
= 0;
997 /* For control (ancillary) data received from recvmsg() */
998 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1004 struct iovec iov
[2];
1006 /* signal handling */
1008 /* last packet sent, timeout reached? */
1009 if (npackets
&& ntransmitted
>= npackets
)
1028 if (options
& F_FLOOD
) {
1034 timeout
.tv_usec
= 10000;
1046 fdmaskp
[0].events
= POLLIN
;
1047 cc
= poll(fdmaskp
, 1, timeout
);
1049 memset(fdmaskp
, 0, fdmasks
);
1051 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1054 if (errno
!= EINTR
) {
1066 m
.msg_name
= (caddr_t
)&from
;
1067 m
.msg_namelen
= sizeof(from
);
1068 memset(&iov
, 0, sizeof(iov
));
1069 iov
[0].iov_base
= (caddr_t
)packet
;
1070 iov
[0].iov_len
= packlen
;
1073 memset(cm
, 0, CONTROLLEN
);
1074 m
.msg_control
= (void *)cm
;
1075 m
.msg_controllen
= CONTROLLEN
;
1077 cc
= recvmsg(s
, &m
, 0);
1079 if (errno
!= EINTR
) {
1084 } else if (cc
== 0) {
1088 * receive control messages only. Process the
1089 * exceptions (currently the only possiblity is
1090 * a path MTU notification.)
1092 if ((mtu
= get_pathmtu(&m
)) > 0) {
1093 if ((options
& F_VERBOSE
) != 0) {
1094 printf("new path MTU (%d) is "
1101 * an ICMPv6 message (probably an echoreply) arrived.
1103 pr_pack(packet
, cc
, &m
);
1105 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1106 (npackets
> 0 && nreceived
>= npackets
))
1108 if (ntransmitted
- nreceived
- 1 > nmissedmax
) {
1109 nmissedmax
= ntransmitted
- nreceived
- 1;
1110 if (options
& F_MISSED
)
1111 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1115 exit(nreceived
== 0 ? 2 : 0);
1140 * This routine transmits another ping6.
1145 struct itimerval itimer
;
1151 * If we're not transmitting any more packets, change the timer
1152 * to wait two round-trip times if we've received any packets or
1153 * ten seconds if we haven't.
1157 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1158 if (itimer
.it_value
.tv_sec
== 0)
1159 itimer
.it_value
.tv_sec
= 1;
1161 itimer
.it_value
.tv_sec
= MAXWAIT
;
1162 itimer
.it_interval
.tv_sec
= 0;
1163 itimer
.it_interval
.tv_usec
= 0;
1164 itimer
.it_value
.tv_usec
= 0;
1166 (void)signal(SIGALRM
, onsignal
);
1167 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1172 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1173 * will be added on by the kernel. The ID field is our UNIX process ID,
1174 * and the sequence number is an ascending integer. The first 8 bytes
1175 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1176 * byte-order, to compute the round-trip time.
1183 if (options
& F_FQDN
)
1184 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1185 else if (options
& F_FQDNOLD
)
1187 else if (options
& F_NODEADDR
)
1188 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1189 else if (options
& F_SUPTYPES
)
1192 l
= ICMP6ECHOLEN
+ datalen
;
1200 struct icmp6_hdr
*icp
;
1201 struct iovec iov
[2];
1203 struct icmp6_nodeinfo
*nip
;
1206 if (npackets
&& ntransmitted
>= npackets
)
1207 return(-1); /* no more transmission */
1209 icp
= (struct icmp6_hdr
*)outpack
;
1210 nip
= (struct icmp6_nodeinfo
*)outpack
;
1211 memset(icp
, 0, sizeof(*icp
));
1212 icp
->icmp6_cksum
= 0;
1213 seq
= ntransmitted
++;
1214 CLR(seq
% mx_dup_ck
);
1216 if (options
& F_FQDN
) {
1217 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1218 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1219 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1220 nip
->ni_flags
= htons(0);
1222 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1223 sizeof(nip
->icmp6_ni_nonce
));
1224 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1226 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1227 sizeof(dst
.sin6_addr
));
1228 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1230 } else if (options
& F_FQDNOLD
) {
1231 /* packet format in 03 draft - no Subject data on queries */
1232 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1233 icp
->icmp6_code
= 0; /* code field is always 0 */
1234 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1235 nip
->ni_flags
= htons(0);
1237 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1238 sizeof(nip
->icmp6_ni_nonce
));
1239 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1243 } else if (options
& F_NODEADDR
) {
1244 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1245 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1246 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1247 nip
->ni_flags
= naflags
;
1249 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1250 sizeof(nip
->icmp6_ni_nonce
));
1251 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1253 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1254 sizeof(dst
.sin6_addr
));
1255 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1257 } else if (options
& F_SUPTYPES
) {
1258 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1259 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1260 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1261 /* we support compressed bitmap */
1262 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1264 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1265 sizeof(nip
->icmp6_ni_nonce
));
1266 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1270 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1271 icp
->icmp6_code
= 0;
1272 icp
->icmp6_id
= htons(ident
);
1273 icp
->icmp6_seq
= ntohs(seq
);
1277 (void)gettimeofday(&tv
, NULL
);
1278 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1279 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1280 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1282 cc
= ICMP6ECHOLEN
+ datalen
;
1286 if (pingerlen() != cc
)
1287 errx(1, "internal error; length mismatch");
1290 smsghdr
.msg_name
= (caddr_t
)&dst
;
1291 smsghdr
.msg_namelen
= sizeof(dst
);
1292 memset(&iov
, 0, sizeof(iov
));
1293 iov
[0].iov_base
= (caddr_t
)outpack
;
1294 iov
[0].iov_len
= cc
;
1295 smsghdr
.msg_iov
= iov
;
1296 smsghdr
.msg_iovlen
= 1;
1298 #ifdef CMSG_SENDING_UNSUPPORTED
1299 smsghdr
.msg_control
= NULL
;
1300 smsghdr
.msg_controllen
= 0;
1303 i
= sendmsg(s
, &smsghdr
, 0);
1305 if (i
< 0 || i
!= cc
) {
1308 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1311 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1312 (void)write(STDOUT_FILENO
, &DOT
, 1);
1319 const struct icmp6_hdr
*icp
;
1321 if (ntohs(icp
->icmp6_id
) == ident
)
1329 const struct icmp6_nodeinfo
*nip
;
1331 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1332 nonce
+ sizeof(u_int16_t
),
1333 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1340 dnsdecode(sp
, ep
, base
, buf
, bufsiz
)
1343 const u_char
*base
; /*base for compressed name*/
1349 char cresult
[MAXDNAME
+ 1];
1360 if (i
== 0 || cp
!= *sp
) {
1361 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1362 return NULL
; /*result overrun*/
1368 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1369 /* DNS compression */
1373 comp
= base
+ (i
& 0x3f);
1374 if (dnsdecode(&comp
, cp
, base
, cresult
,
1375 sizeof(cresult
)) == NULL
)
1377 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1378 return NULL
; /*result overrun*/
1380 } else if ((i
& 0x3f) == i
) {
1382 return NULL
; /*source overrun*/
1383 while (i
-- > 0 && cp
< ep
) {
1384 l
= snprintf(cresult
, sizeof(cresult
),
1385 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1386 if (l
>= sizeof(cresult
) || l
< 0)
1388 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1389 return NULL
; /*result overrun*/
1393 return NULL
; /*invalid label*/
1396 return NULL
; /*not terminated*/
1404 * Print out the packet, if it came from us. This logic is necessary
1405 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1406 * which arrive ('tis only fair). This permits multiple copies of this
1407 * program to be run without having intermingled output (or statistics!).
1410 pr_pack(buf
, cc
, mhdr
)
1413 struct msghdr
*mhdr
;
1415 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1416 struct icmp6_hdr
*icp
;
1417 struct icmp6_nodeinfo
*ni
;
1420 struct sockaddr
*from
;
1422 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1423 struct in6_pktinfo
*pktinfo
= NULL
;
1424 struct timeval tv
, tp
;
1426 double triptime
= 0;
1431 char dnsname
[MAXDNAME
+ 1];
1433 (void)gettimeofday(&tv
, NULL
);
1435 if (!mhdr
|| !mhdr
->msg_name
||
1436 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1437 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1438 if (options
& F_VERBOSE
)
1439 warnx("invalid peername");
1442 from
= (struct sockaddr
*)mhdr
->msg_name
;
1443 fromlen
= mhdr
->msg_namelen
;
1444 if (cc
< sizeof(struct icmp6_hdr
)) {
1445 if (options
& F_VERBOSE
)
1446 warnx("packet too short (%d bytes) from %s", cc
,
1447 pr_addr(from
, fromlen
));
1450 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1451 (options
& F_VERBOSE
) != 0)
1452 warnx("some control data discarded, insufficient buffer size");
1453 icp
= (struct icmp6_hdr
*)buf
;
1454 ni
= (struct icmp6_nodeinfo
*)buf
;
1457 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1458 warnx("failed to get receiving hop limit");
1461 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1462 warnx("failed to get receiving packet information");
1466 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1467 seq
= ntohs(icp
->icmp6_seq
);
1470 tpp
= (struct tv32
*)(icp
+ 1);
1471 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1472 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1474 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1475 ((double)tv
.tv_usec
) / 1000.0;
1477 tsumsq
+= triptime
* triptime
;
1478 if (triptime
< tmin
)
1480 if (triptime
> tmax
)
1484 if (TST(seq
% mx_dup_ck
)) {
1489 SET(seq
% mx_dup_ck
);
1493 if (options
& F_QUIET
)
1496 if (options
& F_FLOOD
)
1497 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1499 if (options
& F_AUDIBLE
)
1500 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1501 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1502 pr_addr(from
, fromlen
), seq
);
1503 (void)printf(" hlim=%d", hoplim
);
1504 if ((options
& F_VERBOSE
) != 0) {
1505 struct sockaddr_in6 dstsa
;
1507 memset(&dstsa
, 0, sizeof(dstsa
));
1508 dstsa
.sin6_family
= AF_INET6
;
1509 dstsa
.sin6_len
= sizeof(dstsa
);
1510 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1511 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1512 (void)printf(" dst=%s",
1513 pr_addr((struct sockaddr
*)&dstsa
,
1517 (void)printf(" time=%.3f ms", triptime
);
1519 (void)printf("(DUP!)");
1520 /* check the data */
1521 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1522 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1523 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1525 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1530 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1531 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1533 if (TST(seq
% mx_dup_ck
)) {
1538 SET(seq
% mx_dup_ck
);
1542 if (options
& F_QUIET
)
1545 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1547 switch (ntohs(ni
->ni_code
)) {
1548 case ICMP6_NI_SUCCESS
:
1550 case ICMP6_NI_REFUSED
:
1551 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1553 case ICMP6_NI_UNKNOWN
:
1554 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1557 printf("unknown code 0x%x, type 0x%x",
1558 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1562 switch (ntohs(ni
->ni_qtype
)) {
1564 printf("NodeInfo NOOP");
1566 case NI_QTYPE_SUPTYPES
:
1567 pr_suptypes(ni
, end
- (u_char
*)ni
);
1569 case NI_QTYPE_NODEADDR
:
1570 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1573 default: /* XXX: for backward compatibility */
1574 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1575 if (buf
[off
+ ICMP6_NIRLEN
] ==
1576 cc
- off
- ICMP6_NIRLEN
- 1)
1581 cp
++; /* skip length */
1583 safeputc(*cp
& 0xff);
1589 if (dnsdecode((const u_char
**)&cp
, end
,
1590 (const u_char
*)(ni
+ 1), dnsname
,
1591 sizeof(dnsname
)) == NULL
) {
1596 * name-lookup special handling for
1599 if (cp
+ 1 <= end
&& !*cp
&&
1600 strlen(dnsname
) > 0) {
1601 dnsname
[strlen(dnsname
) - 1] = '\0';
1604 printf("%s%s", i
> 0 ? "," : "",
1608 if (options
& F_VERBOSE
) {
1612 (void)printf(" ("); /*)*/
1614 switch (ni
->ni_code
) {
1615 case ICMP6_NI_REFUSED
:
1616 (void)printf("refused");
1619 case ICMP6_NI_UNKNOWN
:
1620 (void)printf("unknown qtype");
1625 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1626 /* case of refusion, unknown */
1631 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1634 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1635 (void)printf("TTL=%d:meaningless",
1639 (void)printf("TTL=%d:invalid",
1642 (void)printf("TTL=%d", ttl
);
1652 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1661 if (buf
[off
+ ICMP6_NIRLEN
] !=
1662 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1665 (void)printf("invalid namelen:%d/%lu",
1666 buf
[off
+ ICMP6_NIRLEN
],
1667 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1677 /* We've got something other than an ECHOREPLY */
1678 if (!(options
& F_VERBOSE
))
1680 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1684 if (!(options
& F_FLOOD
)) {
1685 (void)putchar('\n');
1686 if (options
& F_VERBOSE
)
1688 (void)fflush(stdout
);
1695 struct msghdr
*mhdr
;
1702 bufp
= mhdr
->msg_control
;
1703 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1704 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1705 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1708 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1711 switch (cm
->cmsg_type
) {
1713 printf(" HbH Options: ");
1714 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1717 #ifdef IPV6_RTHDRDSTOPTS
1718 case IPV6_RTHDRDSTOPTS
:
1720 printf(" Dst Options: ");
1721 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1724 printf(" Routing: ");
1725 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1731 #if defined USE_RFC2292BIS && defined IPV6_OPTIONS
1733 pr_ip6opt(void *extbuf
, size_t bufsize
)
1735 struct ip6_hbh
*ext
;
1738 socklen_t extlen
, len
, origextlen
;
1744 ext
= (struct ip6_hbh
*)extbuf
;
1745 extlen
= (ext
->ip6h_len
+ 1) * 8;
1746 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1747 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1750 * Bounds checking on the ancillary data buffer:
1751 * subtract the size of a cmsg structure from the buffer size.
1753 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
1754 origextlen
= extlen
;
1755 extlen
= bufsize
- CMSG_SPACE(0);
1756 warnx("options truncated, showing only %u (total=%u)",
1757 (unsigned int)(extlen
/ 8 - 1),
1758 (unsigned int)(ext
->ip6h_len
));
1763 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1764 &type
, &len
, &databuf
);
1765 if (currentlen
== -1)
1769 * Note that inet6_opt_next automatically skips any padding
1774 offset
= inet6_opt_get_val(databuf
, offset
,
1775 &value4
, sizeof(value4
));
1776 printf(" Jumbo Payload Opt: Length %u\n",
1777 (u_int32_t
)ntohl(value4
));
1779 case IP6OPT_ROUTER_ALERT
:
1781 offset
= inet6_opt_get_val(databuf
, offset
,
1782 &value2
, sizeof(value2
));
1783 printf(" Router Alert Opt: Type %u\n",
1787 printf(" Received Opt %u len %lu\n",
1788 type
, (unsigned long)len
);
1794 #else /* !USE_RFC2292BIS */
1797 pr_ip6opt(void *extbuf
, size_t bufsize
)
1804 #endif /* USE_RFC2292BIS */
1806 #if defined USE_RFC2292BIS && defined IPV6_OPTIONS
1808 pr_rthdr(void *extbuf
, size_t bufsize
)
1810 struct in6_addr
*in6
;
1811 char ntopbuf
[INET6_ADDRSTRLEN
];
1812 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1813 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
1815 /* print fixed part of the header */
1816 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1817 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1818 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
1819 printf("%d segments, ", segments
);
1820 printf("%d left\n", rh
->ip6r_segleft
);
1822 printf("segments unknown, ");
1823 printf("%d left\n", rh
->ip6r_segleft
);
1828 * Bounds checking on the ancillary data buffer. When calculating
1829 * the number of items to show keep in mind:
1830 * - The size of the cmsg structure
1831 * - The size of one segment (the size of a Type 0 routing header)
1832 * - When dividing add a fudge factor of one in case the
1833 * dividend is not evenly divisible by the divisor
1835 rthsize
= (rh
->ip6r_len
+ 1) * 8;
1836 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
1837 origsegs
= segments
;
1838 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
1839 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
1840 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
1841 (size1
- size0
) + 1;
1842 warnx("segments truncated, showing only %d (total=%d)",
1843 segments
, origsegs
);
1846 for (i
= 0; i
< segments
; i
++) {
1847 in6
= inet6_rth_getaddr(extbuf
, i
);
1849 printf(" [%d]<NULL>\n", i
);
1851 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1853 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
1854 printf(" [%d]%s\n", i
, ntopbuf
);
1862 #else /* !USE_RFC2292BIS */
1865 pr_rthdr(void *extbuf
, size_t bufsize
)
1872 #endif /* USE_RFC2292BIS */
1875 pr_bitrange(v
, soff
, ii
)
1885 /* shift till we have 0x01 */
1886 if ((v
& 0x01) == 0) {
1888 printf("-%u", soff
+ off
- 1);
1899 case 0x04: case 0x0c:
1910 /* we have 0x01 with us */
1911 for (i
= 0; i
< 32 - off
; i
++) {
1912 if ((v
& (0x01 << i
)) == 0)
1916 printf(" %u", soff
+ off
);
1924 pr_suptypes(ni
, nilen
)
1925 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be SUPTYPES */
1930 const u_char
*cp
, *end
;
1933 u_int16_t words
; /*32bit count*/
1936 #define MAXQTYPES (1 << 16)
1940 cp
= (u_char
*)(ni
+ 1);
1941 end
= ((u_char
*)ni
) + nilen
;
1945 printf("NodeInfo Supported Qtypes");
1946 if (options
& F_VERBOSE
) {
1947 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1948 printf(", compressed bitmap");
1950 printf(", raw bitmap");
1954 clen
= (size_t)(end
- cp
);
1955 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1956 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1962 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1964 memcpy(&cbit
, cp
, sizeof(cbit
));
1965 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1969 clen
= ntohs(cbit
.words
) * sizeof(v
);
1970 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1975 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1976 memcpy(&v
, cp
+ off
, sizeof(v
));
1977 v
= (u_int32_t
)ntohl(v
);
1978 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1980 /* flush the remaining bits */
1981 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1985 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1986 cur
+= ntohs(cbit
.skip
) * 32;
1991 pr_nodeaddr(ni
, nilen
)
1992 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be NODEADDR */
1995 u_char
*cp
= (u_char
*)(ni
+ 1);
1996 char ntop_buf
[INET6_ADDRSTRLEN
];
1999 nilen
-= sizeof(struct icmp6_nodeinfo
);
2001 if (options
& F_VERBOSE
) {
2002 switch (ni
->ni_code
) {
2003 case ICMP6_NI_REFUSED
:
2004 (void)printf("refused");
2006 case ICMP6_NI_UNKNOWN
:
2007 (void)printf("unknown qtype");
2010 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2011 (void)printf(" truncated");
2015 printf(" no address\n");
2018 * In icmp-name-lookups 05 and later, TTL of each returned address
2019 * is contained in the resposne. We try to detect the version
2020 * by the length of the data, but note that the detection algorithm
2021 * is incomplete. We assume the latest draft by default.
2023 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2029 /* XXX: alignment? */
2030 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2031 cp
+= sizeof(u_int32_t
);
2032 nilen
-= sizeof(u_int32_t
);
2035 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2037 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2038 printf(" %s", ntop_buf
);
2040 if (ttl
== 0xffffffff) {
2042 * XXX: can this convention be applied to all
2043 * type of TTL (i.e. non-ND TTL)?
2045 printf("(TTL=infty)");
2048 printf("(TTL=%u)", ttl
);
2052 nilen
-= sizeof(struct in6_addr
);
2053 cp
+= sizeof(struct in6_addr
);
2059 struct msghdr
*mhdr
;
2063 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2064 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2065 if (cm
->cmsg_len
== 0)
2068 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2069 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2070 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2071 return(*(int *)CMSG_DATA(cm
));
2077 struct in6_pktinfo
*
2078 get_rcvpktinfo(mhdr
)
2079 struct msghdr
*mhdr
;
2083 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2084 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2085 if (cm
->cmsg_len
== 0)
2088 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2089 cm
->cmsg_type
== IPV6_PKTINFO
&&
2090 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2091 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2099 struct msghdr
*mhdr
;
2101 #ifdef IPV6_RECVPATHMTU
2103 struct ip6_mtuinfo
*mtuctl
= NULL
;
2105 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2106 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2107 if (cm
->cmsg_len
== 0)
2110 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2111 cm
->cmsg_type
== IPV6_PATHMTU
&&
2112 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2113 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2116 * If the notified destination is different from
2117 * the one we are pinging, just ignore the info.
2118 * We check the scope ID only when both notified value
2119 * and our own value have non-0 values, because we may
2120 * have used the default scope zone ID for sending,
2121 * in which case the scope ID value is 0.
2123 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2125 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2126 dst
.sin6_scope_id
&&
2127 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2128 dst
.sin6_scope_id
)) {
2129 if ((options
& F_VERBOSE
) != 0) {
2130 printf("path MTU for %s is notified. "
2132 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2133 sizeof(mtuctl
->ip6m_addr
)));
2139 * Ignore an invalid MTU. XXX: can we just believe
2142 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2145 /* notification for our destination. return the MTU. */
2146 return((int)mtuctl
->ip6m_mtu
);
2155 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2160 struct timeval
*out
, *in
;
2162 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2164 out
->tv_usec
+= 1000000;
2166 out
->tv_sec
-= in
->tv_sec
;
2180 (void)signal(SIGINT
, SIG_DFL
);
2181 (void)kill(getpid(), SIGINT
);
2189 * Print out statistics.
2194 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2195 (void)printf("%ld packets transmitted, ", ntransmitted
);
2196 (void)printf("%ld packets received, ", nreceived
);
2198 (void)printf("+%ld duplicates, ", nrepeats
);
2200 if (nreceived
> ntransmitted
)
2201 (void)printf("-- somebody's duplicating packets!");
2203 (void)printf("%.1f%% packet loss",
2204 ((((double)ntransmitted
- nreceived
) * 100.0) /
2207 (void)putchar('\n');
2208 if (nreceived
&& timing
) {
2209 /* Only display average to microseconds */
2210 double num
= nreceived
+ nrepeats
;
2211 double avg
= tsum
/ num
;
2212 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2214 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2215 tmin
, avg
, tmax
, dev
);
2216 (void)fflush(stdout
);
2218 (void)fflush(stdout
);
2222 static const char *niqcode
[] = {
2224 "DNS label", /*or empty*/
2229 static const char *nircode
[] = {
2230 "Success", "Refused", "Unknown",
2236 * Print a descriptive string about an ICMP header.
2240 struct icmp6_hdr
*icp
;
2243 char ntop_buf
[INET6_ADDRSTRLEN
];
2244 struct nd_redirect
*red
;
2245 struct icmp6_nodeinfo
*ni
;
2246 char dnsname
[MAXDNAME
+ 1];
2250 switch (icp
->icmp6_type
) {
2251 case ICMP6_DST_UNREACH
:
2252 switch (icp
->icmp6_code
) {
2253 case ICMP6_DST_UNREACH_NOROUTE
:
2254 (void)printf("No Route to Destination\n");
2256 case ICMP6_DST_UNREACH_ADMIN
:
2257 (void)printf("Destination Administratively "
2260 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2261 (void)printf("Destination Unreachable Beyond Scope\n");
2263 case ICMP6_DST_UNREACH_ADDR
:
2264 (void)printf("Destination Host Unreachable\n");
2266 case ICMP6_DST_UNREACH_NOPORT
:
2267 (void)printf("Destination Port Unreachable\n");
2270 (void)printf("Destination Unreachable, Bad Code: %d\n",
2274 /* Print returned IP header information */
2275 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2277 case ICMP6_PACKET_TOO_BIG
:
2278 (void)printf("Packet too big mtu = %d\n",
2279 (int)ntohl(icp
->icmp6_mtu
));
2280 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2282 case ICMP6_TIME_EXCEEDED
:
2283 switch (icp
->icmp6_code
) {
2284 case ICMP6_TIME_EXCEED_TRANSIT
:
2285 (void)printf("Time to live exceeded\n");
2287 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2288 (void)printf("Frag reassembly time exceeded\n");
2291 (void)printf("Time exceeded, Bad Code: %d\n",
2295 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2297 case ICMP6_PARAM_PROB
:
2298 (void)printf("Parameter problem: ");
2299 switch (icp
->icmp6_code
) {
2300 case ICMP6_PARAMPROB_HEADER
:
2301 (void)printf("Erroneous Header ");
2303 case ICMP6_PARAMPROB_NEXTHEADER
:
2304 (void)printf("Unknown Nextheader ");
2306 case ICMP6_PARAMPROB_OPTION
:
2307 (void)printf("Unrecognized Option ");
2310 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2313 (void)printf("pointer = 0x%02x\n",
2314 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2315 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2317 case ICMP6_ECHO_REQUEST
:
2318 (void)printf("Echo Request");
2319 /* XXX ID + Seq + Data */
2321 case ICMP6_ECHO_REPLY
:
2322 (void)printf("Echo Reply");
2323 /* XXX ID + Seq + Data */
2325 case ICMP6_MEMBERSHIP_QUERY
:
2326 (void)printf("Listener Query");
2328 case ICMP6_MEMBERSHIP_REPORT
:
2329 (void)printf("Listener Report");
2331 case ICMP6_MEMBERSHIP_REDUCTION
:
2332 (void)printf("Listener Done");
2334 case ND_ROUTER_SOLICIT
:
2335 (void)printf("Router Solicitation");
2337 case ND_ROUTER_ADVERT
:
2338 (void)printf("Router Advertisement");
2340 case ND_NEIGHBOR_SOLICIT
:
2341 (void)printf("Neighbor Solicitation");
2343 case ND_NEIGHBOR_ADVERT
:
2344 (void)printf("Neighbor Advertisement");
2347 red
= (struct nd_redirect
*)icp
;
2348 (void)printf("Redirect\n");
2349 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2351 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2352 (void)printf("Destination: %s", ntop_buf
);
2353 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2355 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2356 (void)printf(" New Target: %s", ntop_buf
);
2358 case ICMP6_NI_QUERY
:
2359 (void)printf("Node Information Query");
2360 /* XXX ID + Seq + Data */
2361 ni
= (struct icmp6_nodeinfo
*)icp
;
2362 l
= end
- (u_char
*)(ni
+ 1);
2364 switch (ntohs(ni
->ni_qtype
)) {
2366 (void)printf("NOOP");
2368 case NI_QTYPE_SUPTYPES
:
2369 (void)printf("Supported qtypes");
2372 (void)printf("DNS name");
2374 case NI_QTYPE_NODEADDR
:
2375 (void)printf("nodeaddr");
2377 case NI_QTYPE_IPV4ADDR
:
2378 (void)printf("IPv4 nodeaddr");
2381 (void)printf("unknown qtype");
2384 if (options
& F_VERBOSE
) {
2385 switch (ni
->ni_code
) {
2386 case ICMP6_NI_SUBJ_IPV6
:
2387 if (l
== sizeof(struct in6_addr
) &&
2388 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2389 sizeof(ntop_buf
)) != NULL
) {
2390 (void)printf(", subject=%s(%s)",
2391 niqcode
[ni
->ni_code
], ntop_buf
);
2394 /* backward compat to -W */
2395 (void)printf(", oldfqdn");
2397 (void)printf(", invalid");
2401 case ICMP6_NI_SUBJ_FQDN
:
2402 if (end
== (u_char
*)(ni
+ 1)) {
2403 (void)printf(", no subject");
2406 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2407 cp
= (const u_char
*)(ni
+ 1);
2408 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2409 sizeof(dnsname
)) != NULL
)
2410 printf("(%s)", dnsname
);
2412 printf("(invalid)");
2414 case ICMP6_NI_SUBJ_IPV4
:
2415 if (l
== sizeof(struct in_addr
) &&
2416 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2417 sizeof(ntop_buf
)) != NULL
) {
2418 (void)printf(", subject=%s(%s)",
2419 niqcode
[ni
->ni_code
], ntop_buf
);
2421 (void)printf(", invalid");
2424 (void)printf(", invalid");
2429 case ICMP6_NI_REPLY
:
2430 (void)printf("Node Information Reply");
2431 /* XXX ID + Seq + Data */
2432 ni
= (struct icmp6_nodeinfo
*)icp
;
2434 switch (ntohs(ni
->ni_qtype
)) {
2436 (void)printf("NOOP");
2438 case NI_QTYPE_SUPTYPES
:
2439 (void)printf("Supported qtypes");
2442 (void)printf("DNS name");
2444 case NI_QTYPE_NODEADDR
:
2445 (void)printf("nodeaddr");
2447 case NI_QTYPE_IPV4ADDR
:
2448 (void)printf("IPv4 nodeaddr");
2451 (void)printf("unknown qtype");
2454 if (options
& F_VERBOSE
) {
2455 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2456 printf(", invalid");
2458 printf(", %s", nircode
[ni
->ni_code
]);
2462 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2468 * Print an IP6 header.
2472 struct ip6_hdr
*ip6
;
2474 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2476 char ntop_buf
[INET6_ADDRSTRLEN
];
2478 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2479 tc
= (tc
>> 4) & 0x0f;
2480 tc
|= (ip6
->ip6_vfc
<< 4);
2482 printf("Vr TC Flow Plen Nxt Hlim\n");
2483 printf(" %1x %02x %05x %04x %02x %02x\n",
2484 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2485 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2486 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2487 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2488 printf("%s->", ntop_buf
);
2489 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2490 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2491 printf("%s\n", ntop_buf
);
2496 * Return an ascii host address as a dotted quad and optionally with
2500 pr_addr(addr
, addrlen
)
2501 struct sockaddr
*addr
;
2504 static char buf
[NI_MAXHOST
];
2507 if ((options
& F_HOSTNAME
) == 0)
2508 flag
|= NI_NUMERICHOST
;
2510 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2518 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2522 struct ip6_hdr
*ip6
;
2525 u_char
*cp
= (u_char
*)ip6
, nh
;
2528 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2533 hlen
= sizeof(*ip6
);
2537 while (end
- cp
>= 8) {
2539 case IPPROTO_HOPOPTS
:
2541 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2542 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2544 case IPPROTO_DSTOPTS
:
2546 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2547 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2549 case IPPROTO_FRAGMENT
:
2551 hlen
= sizeof(struct ip6_frag
);
2552 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2554 case IPPROTO_ROUTING
:
2556 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2557 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2562 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2563 nh
= ((struct ah
*)cp
)->ah_nxt
;
2566 case IPPROTO_ICMPV6
:
2567 printf("ICMP6: type = %d, code = %d\n",
2574 printf("TCP: from port %u, to port %u (decimal)\n",
2575 (*cp
* 256 + *(cp
+ 1)),
2576 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2579 printf("UDP: from port %u, to port %u (decimal)\n",
2580 (*cp
* 256 + *(cp
+ 1)),
2581 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2584 printf("Unknown Header(%d)\n", nh
);
2588 if ((cp
+= hlen
) >= end
)
2610 for (cp
= patp
; *cp
; cp
++)
2612 errx(1, "patterns must be specified as hex digits");
2614 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2615 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2616 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2617 &pat
[13], &pat
[14], &pat
[15]);
2622 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
2624 for (jj
= 0; jj
< ii
; ++jj
)
2625 bp
[jj
+ kk
] = pat
[jj
];
2626 if (!(options
& F_QUIET
)) {
2627 (void)printf("PATTERN: 0x");
2628 for (jj
= 0; jj
< ii
; ++jj
)
2629 (void)printf("%02x", bp
[jj
] & 0xFF);
2635 #ifdef IPSEC_POLICY_IPSEC
2637 setpolicy(so
, policy
)
2644 return 0; /* ignore */
2646 buf
= ipsec_set_policy(policy
, strlen(policy
));
2648 errx(1, "%s", ipsec_strerror());
2649 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2650 ipsec_get_policylen(buf
)) < 0)
2651 warnx("Unable to set IPsec policy");
2662 (void)fprintf(stderr
,
2663 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2668 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2672 #ifdef IPV6_USE_MIN_MTU
2676 "[-a addrtype] [-b bufsiz] [-c count]\n"
2677 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2678 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2682 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2683 "[hops ...] host\n");