2 * Copyright (c) 2002-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1989, 1993
60 * The Regents of the University of California. All rights reserved.
62 * This code is derived from software contributed to Berkeley by
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 #include <sys/cdefs.h>
97 __unused
static const char copyright
[] =
98 "@(#) Copyright (c) 1989, 1993\n\
99 The Regents of the University of California. All rights reserved.\n";
100 #endif /* not lint */
103 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
104 * measure round-trip-delays and packet loss across network paths.
108 * U. S. Army Ballistic Research Laboratory
112 * Public Domain. Distribution Unlimited.
114 * More statistics could always be gathered.
115 * This program has to run SUID to ROOT to access the ICMP socket.
119 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
120 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
121 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
122 * network attached to 1 or more interfaces)
125 #include <sys/param.h>
127 #include <sys/socket.h>
128 #include <sys/time.h>
131 #include <net/route.h>
133 #include <netinet/in.h>
134 #include <netinet/ip6.h>
135 #include <netinet/icmp6.h>
136 #include <arpa/inet.h>
137 #include <arpa/nameser.h>
153 #include <sysexits.h>
156 #include <netinet6/ah.h>
157 #include <netinet6/ipsec.h>
167 #define MAXPACKETLEN 131072
169 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
170 #define ICMP6ECHOTMLEN sizeof(struct tv32)
171 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
172 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
173 /* FQDN case, 64 bits of nonce + 32 bits ttl */
174 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
175 #define EXTRA 256 /* for AH and various other headers. weird. */
176 #define DEFDATALEN ICMP6ECHOTMLEN
177 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
178 #define NROUTES 9 /* number of record route slots */
180 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
181 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
182 #define SET(bit) (A(bit) |= B(bit))
183 #define CLR(bit) (A(bit) &= (~B(bit)))
184 #define TST(bit) (A(bit) & B(bit))
186 #define F_FLOOD 0x0001
187 #define F_INTERVAL 0x0002
188 #define F_PINGFILLED 0x0008
189 #define F_QUIET 0x0010
190 #define F_RROUTE 0x0020
191 #define F_SO_DEBUG 0x0040
192 #define F_VERBOSE 0x0100
194 #ifdef IPSEC_POLICY_IPSEC
195 #define F_POLICY 0x0400
197 #define F_AUTHHDR 0x0200
198 #define F_ENCRYPT 0x0400
199 #endif /*IPSEC_POLICY_IPSEC*/
201 #define F_NODEADDR 0x0800
202 #define F_FQDN 0x1000
203 #define F_INTERFACE 0x2000
204 #define F_SRCADDR 0x4000
205 #define F_HOSTNAME 0x10000
206 #define F_FQDNOLD 0x20000
207 #define F_NIGROUP 0x40000
208 #define F_SUPTYPES 0x80000
209 #define F_NOMINMTU 0x100000
210 #define F_ONCE 0x200000
211 #define F_AUDIBLE 0x400000
212 #define F_MISSED 0x800000
213 #define F_DONTFRAG 0x1000000
214 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
217 #define IN6LEN sizeof(struct in6_addr)
218 #define SA6LEN sizeof(struct sockaddr_in6)
219 #define DUMMY_PORT 10101
221 #define SIN6(s) ((struct sockaddr_in6 *)(s))
225 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
226 * number of received sequence numbers we can keep track of. Change 128
227 * to 8192 for complete accuracy...
229 #define MAX_DUP_CHK (8 * 8192)
230 int mx_dup_ck
= MAX_DUP_CHK
;
231 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
233 struct addrinfo
*res
;
234 struct sockaddr_in6 dst
; /* who to ping6 */
235 struct sockaddr_in6 src
; /* src addr of this packet */
237 int datalen
= DEFDATALEN
;
238 int s
; /* socket file descriptor */
239 u_char outpack
[MAXPACKETLEN
];
240 char BSPACE
= '\b'; /* characters written for flood */
241 char BBELL
= '\a'; /* characters written for AUDIBLE */
244 int ident
; /* process id to identify our packets */
245 u_int8_t nonce
[8]; /* nonce field for node information */
246 int hoplimit
= -1; /* hoplimit */
247 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
248 u_char
*packet
= NULL
;
249 struct cmsghdr
*cm
= NULL
;
251 unsigned int ifscope
;
254 struct pollfd fdmaskp
[1];
256 fd_set
*fdmaskp
= NULL
;
261 long nmissedmax
; /* max value of ntransmitted - nreceived - 1 */
262 long npackets
; /* max packets to transmit */
263 long nreceived
; /* # of packets we got back */
264 long nrepeats
; /* number of duplicates */
265 long ntransmitted
; /* sequence # for outbound packets = #sent */
266 struct timeval interval
= {1, 0}; /* interval between packets */
269 int timing
; /* flag to do timing */
270 double tmin
= 999999999.0; /* minimum round trip time */
271 double tmax
= 0.0; /* maximum round trip time */
272 double tsum
= 0.0; /* sum of all times, for doing average */
273 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
275 /* for node addresses */
278 /* for ancillary data(advanced API) */
279 struct msghdr smsghdr
;
280 struct iovec smsgiov
;
283 volatile sig_atomic_t seenalrm
;
284 volatile sig_atomic_t seenint
;
286 volatile sig_atomic_t seeninfo
;
291 int how_so_traffic_class
= 0;
292 int so_traffic_class
= SO_TC_CTL
; /* use control class, by default */
294 int main(int, char *[]);
295 void fill(char *, char *);
296 int get_hoplim(struct msghdr
*);
297 int get_pathmtu(struct msghdr
*);
298 int get_tclass(struct msghdr
*);
299 int get_so_traffic_class(struct msghdr
*);
300 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
302 void retransmit(void);
304 size_t pingerlen(void);
306 const char *pr_addr(struct sockaddr
*, int);
307 void pr_icmph(struct icmp6_hdr
*, u_char
*);
308 void pr_iph(struct ip6_hdr
*);
309 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
310 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
311 int myechoreply(const struct icmp6_hdr
*);
312 int mynireply(const struct icmp6_nodeinfo
*);
313 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
315 void pr_pack(u_char
*, int, struct msghdr
*);
316 void pr_exthdrs(struct msghdr
*);
317 void pr_ip6opt(void *, size_t);
318 void pr_rthdr(void *, size_t);
319 int pr_bitrange(u_int32_t
, int, int);
320 void pr_retip(struct ip6_hdr
*, u_char
*);
322 void tvsub(struct timeval
*, struct timeval
*);
323 int setpolicy(int, char *);
324 char *nigroup(char *);
325 static uint32_t str2svc(const char *);
329 main(int argc
, char *argv
[])
331 struct itimerval itimer
;
332 struct sockaddr_in6 from
;
333 #ifndef HAVE_ARC4RANDOM
339 struct timeval timeout
, *tv
;
341 struct addrinfo hints
;
343 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
345 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
347 struct cmsghdr
*scmsgp
= NULL
;
348 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
353 struct in6_pktinfo
*pktinfo
= NULL
;
354 #ifdef USE_RFC2292BIS
355 struct ip6_rthdr
*rthdr
= NULL
;
357 #ifdef IPSEC_POLICY_IPSEC
358 char *policy_in
= NULL
;
359 char *policy_out
= NULL
;
363 #ifdef IPV6_USE_MIN_MTU
366 /* T_CLASS value -1 means default, so -2 means do not bother */
369 /* just to be sure */
370 memset(&smsghdr
, 0, sizeof(smsghdr
));
371 memset(&smsgiov
, 0, sizeof(smsgiov
));
374 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
378 #ifdef IPSEC_POLICY_IPSEC
382 #endif /*IPSEC_POLICY_IPSEC*/
384 while ((ch
= getopt(argc
, argv
,
385 "a:b:B:Cc:DdfHg:h:I:i:k:l:mnNop:qrRS:s:tvwWz:" ADDOPTS
)) != -1) {
392 options
&= ~F_NOUSERDATA
;
393 options
|= F_NODEADDR
;
394 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
397 naflags
|= NI_NODEADDR_FLAG_ALL
;
401 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
405 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
409 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
413 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
415 case 'A': /* experimental. not in the spec */
416 #ifdef NI_NODEADDR_FLAG_ANYCAST
417 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
420 errx(1, "-a A is not supported on "
432 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
435 lsockbufsize
= strtoul(optarg
, &e
, 10);
436 sockbufsize
= lsockbufsize
;
437 if (errno
|| !*optarg
|| *e
||
438 sockbufsize
!= lsockbufsize
)
439 errx(1, "invalid socket buffer size");
441 errx(1, "-b option ignored: SO_SNDBUF/SO_RCVBUF "
442 "socket options not supported");
452 npackets
= strtol(optarg
, &e
, 10);
453 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
455 "illegal number of packets -- %s", optarg
);
458 options
|= F_DONTFRAG
;
461 options
|= F_SO_DEBUG
;
466 errx(1, "Must be superuser to flood ping");
469 setbuf(stdout
, (char *)NULL
);
475 options
|= F_HOSTNAME
;
477 case 'h': /* hoplimit */
478 hoplimit
= strtol(optarg
, &e
, 10);
479 if (*optarg
== '\0' || *e
!= '\0')
480 errx(1, "illegal hoplimit %s", optarg
);
481 if (255 < hoplimit
|| hoplimit
< -1)
483 "illegal hoplimit -- %s", optarg
);
487 options
|= F_INTERFACE
;
488 #ifndef USE_SIN6_SCOPE_ID
492 case 'i': /* wait between sending packets */
493 intval
= strtod(optarg
, &e
);
494 if (*optarg
== '\0' || *e
!= '\0')
495 errx(1, "illegal timing interval %s", optarg
);
496 if (intval
< 0.1 && getuid()) {
497 errx(1, "%s: only root may use interval < 0.1s",
500 interval
.tv_sec
= (long)intval
;
502 (long)((intval
- interval
.tv_sec
) * 1000000);
503 if (interval
.tv_sec
< 0)
504 errx(1, "illegal timing interval %s", optarg
);
505 /* less than 1/hz does not make sense */
506 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 1) {
507 warnx("too small interval, raised to .000001");
508 interval
.tv_usec
= 1;
510 options
|= F_INTERVAL
;
513 how_so_traffic_class
++;
514 so_traffic_class
= str2svc(optarg
);
515 if (so_traffic_class
== UINT32_MAX
)
516 errx(EX_USAGE
, "bad traffic class: `%s'",
523 errx(1, "Must be superuser to preload");
525 preload
= strtol(optarg
, &e
, 10);
526 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
527 errx(1, "illegal preload value -- %s", optarg
);
530 #ifdef IPV6_USE_MIN_MTU
534 errx(1, "-%c is not supported on this platform", ch
);
538 options
&= ~F_HOSTNAME
;
541 options
|= F_NIGROUP
;
546 case 'p': /* fill buffer with user pattern */
547 options
|= F_PINGFILLED
;
548 fill((char *)datap
, optarg
);
554 options
|= F_AUDIBLE
;
560 memset(&hints
, 0, sizeof(struct addrinfo
));
561 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
562 hints
.ai_family
= AF_INET6
;
563 hints
.ai_socktype
= SOCK_RAW
;
564 hints
.ai_protocol
= IPPROTO_ICMPV6
;
566 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
568 errx(1, "invalid source address: %s",
569 gai_strerror(ret_ga
));
572 * res->ai_family must be AF_INET6 and res->ai_addrlen
573 * must be sizeof(src).
575 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
576 srclen
= res
->ai_addrlen
;
579 options
|= F_SRCADDR
;
581 case 's': /* size of packet to send */
582 datalen
= strtol(optarg
, &e
, 10);
583 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
584 errx(1, "illegal datalen value -- %s", optarg
);
585 if (datalen
> MAXDATALEN
) {
587 "datalen value too large, maximum is %d",
592 options
&= ~F_NOUSERDATA
;
593 options
|= F_SUPTYPES
;
596 options
|= F_VERBOSE
;
599 options
&= ~F_NOUSERDATA
;
603 options
&= ~F_NOUSERDATA
;
604 options
|= F_FQDNOLD
;
607 tclass
= (int)strtol(optarg
, &e
, 10);
608 if (tclass
< -1 || *optarg
== '\0' || *e
!= '\0')
609 errx(1, "illegal TOS value -- %s", optarg
);
612 "TOS value too large, maximum is %d",
617 #ifdef IPSEC_POLICY_IPSEC
620 if (!strncmp("in", optarg
, 2)) {
621 if ((policy_in
= strdup(optarg
)) == NULL
)
623 } else if (!strncmp("out", optarg
, 3)) {
624 if ((policy_out
= strdup(optarg
)) == NULL
)
627 errx(1, "invalid security policy");
631 options
|= F_AUTHHDR
;
634 options
|= F_ENCRYPT
;
636 #endif /*IPSEC_POLICY_IPSEC*/
644 if (boundif
!= NULL
&& (ifscope
= if_nametoindex(boundif
)) == 0)
645 errx(1, "bad interface name");
656 #ifdef IPV6_RECVRTHDR /* 2292bis */
657 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
660 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
663 errx(1, "too many intermediate hops");
669 if (options
& F_NIGROUP
) {
670 target
= nigroup(argv
[argc
- 1]);
671 if (target
== NULL
) {
676 target
= argv
[argc
- 1];
679 memset(&hints
, 0, sizeof(struct addrinfo
));
680 hints
.ai_flags
= AI_CANONNAME
;
681 hints
.ai_family
= AF_INET6
;
682 hints
.ai_socktype
= SOCK_RAW
;
683 hints
.ai_protocol
= IPPROTO_ICMPV6
;
685 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
687 errx(1, "getaddrinfo -- %s", gai_strerror(ret_ga
));
688 if (res
->ai_canonname
)
689 hostname
= res
->ai_canonname
;
694 errx(1, "getaddrinfo failed");
696 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
698 res
->ai_socktype
= getuid() ? SOCK_DGRAM
: SOCK_RAW
;
699 res
->ai_protocol
= IPPROTO_ICMPV6
;
701 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
702 res
->ai_protocol
)) < 0)
706 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
707 (char *)&ifscope
, sizeof (ifscope
)) != 0)
708 err(1, "setsockopt(IPV6_BOUND_IF)");
712 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
713 (char *)&nocell
, sizeof (nocell
)) != 0)
714 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
717 /* set the source address if specified. */
718 if ((options
& F_SRCADDR
) &&
719 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
723 /* set the gateway (next hop) if specified */
725 struct addrinfo ghints
, *gres
;
728 memset(&ghints
, 0, sizeof(ghints
));
729 ghints
.ai_family
= AF_INET6
;
730 ghints
.ai_socktype
= SOCK_RAW
;
731 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
733 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
735 errx(1, "getaddrinfo for the gateway %s: %s",
736 gateway
, gai_strerror(error
));
738 if (gres
->ai_next
&& (options
& F_VERBOSE
))
739 warnx("gateway resolves to multiple addresses");
741 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
742 gres
->ai_addr
, gres
->ai_addrlen
)) {
743 err(1, "setsockopt(IPV6_NEXTHOP)");
750 * let the kerel pass extension headers of incoming packets,
751 * for privileged socket options
753 if ((options
& F_VERBOSE
) != 0) {
756 #ifdef IPV6_RECVHOPOPTS
757 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
759 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
760 #else /* old adv. API */
761 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
763 err(1, "setsockopt(IPV6_HOPOPTS)");
765 #ifdef IPV6_RECVDSTOPTS
766 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
768 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
769 #else /* old adv. API */
770 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
772 err(1, "setsockopt(IPV6_DSTOPTS)");
774 #ifdef IPV6_RECVRTHDRDSTOPTS
775 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
777 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
781 /* revoke root privilege */
782 if (seteuid(getuid()) != 0)
783 err(1, "seteuid() failed");
784 if (setuid(getuid()) != 0)
785 err(1, "setuid() failed");
787 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
788 errx(1, "-f and -i incompatible options");
790 if ((options
& F_NOUSERDATA
) == 0) {
791 if (datalen
>= sizeof(struct tv32
)) {
792 /* we can time transfer */
796 /* in F_VERBOSE case, we may get non-echoreply packets*/
797 if (options
& F_VERBOSE
)
798 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
800 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
802 /* suppress timing for node information query */
805 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
808 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
809 err(1, "Unable to allocate packet");
810 if (!(options
& F_PINGFILLED
))
811 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
814 ident
= getpid() & 0xFFFF;
815 #ifndef HAVE_ARC4RANDOM
816 gettimeofday(&seed
, NULL
);
817 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
818 memset(nonce
, 0, sizeof(nonce
));
819 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
820 *((int *)&nonce
[i
]) = rand();
822 memset(nonce
, 0, sizeof(nonce
));
823 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
824 *((u_int32_t
*)&nonce
[i
]) = arc4random();
827 if (options
& F_DONTFRAG
)
828 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
829 &optval
, sizeof(optval
)) == -1)
830 err(1, "IPV6_DONTFRAG");
832 if (options
& F_SO_DEBUG
)
833 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
837 (void) setsockopt(s
, SOL_SOCKET
, SO_RECV_ANYIF
, (char *)&hold
,
840 optval
= IPV6_DEFHLIM
;
841 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
842 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
843 &optval
, sizeof(optval
)) == -1)
844 err(1, "IPV6_MULTICAST_HOPS");
845 #ifdef IPV6_USE_MIN_MTU
847 optval
= mflag
> 1 ? 0 : 1;
849 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
850 &optval
, sizeof(optval
)) == -1)
851 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
853 #ifdef IPV6_RECVPATHMTU
856 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
857 &optval
, sizeof(optval
)) == -1)
858 err(1, "setsockopt(IPV6_RECVPATHMTU)");
860 #endif /* IPV6_RECVPATHMTU */
861 #endif /* IPV6_USE_MIN_MTU */
864 #ifdef IPSEC_POLICY_IPSEC
865 if (options
& F_POLICY
) {
866 if (setpolicy(s
, policy_in
) < 0)
867 errx(1, "%s", ipsec_strerror());
868 if (setpolicy(s
, policy_out
) < 0)
869 errx(1, "%s", ipsec_strerror());
872 if (options
& F_AUTHHDR
) {
873 optval
= IPSEC_LEVEL_REQUIRE
;
874 #ifdef IPV6_AUTH_TRANS_LEVEL
875 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
876 &optval
, sizeof(optval
)) == -1)
877 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
879 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
880 &optval
, sizeof(optval
)) == -1)
881 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
884 if (options
& F_ENCRYPT
) {
885 optval
= IPSEC_LEVEL_REQUIRE
;
886 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
887 &optval
, sizeof(optval
)) == -1)
888 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
890 #endif /*IPSEC_POLICY_IPSEC*/
895 struct icmp6_filter filt
;
896 if (!(options
& F_VERBOSE
)) {
897 ICMP6_FILTER_SETBLOCKALL(&filt
);
898 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
899 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
900 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
902 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
904 ICMP6_FILTER_SETPASSALL(&filt
);
906 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
908 err(1, "setsockopt(ICMP6_FILTER)");
910 #endif /*ICMP6_FILTER*/
912 /* let the kerel pass extension headers of incoming packets */
913 if ((options
& F_VERBOSE
) != 0) {
916 #ifdef IPV6_RECVRTHDR
917 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
919 err(1, "setsockopt(IPV6_RECVRTHDR)");
920 #else /* old adv. API */
921 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
923 err(1, "setsockopt(IPV6_RTHDR)");
930 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVTCLASS
, &on
,
932 err(1, "setsockopt(IPV6_RECVTCLASS)");
934 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
936 err(1, "setsockopt(IPV6_TCLASS)");
941 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
942 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
943 &optval, sizeof(optval)) == -1)
944 err(1, "IPV6_MULTICAST_LOOP");
947 /* Specify the outgoing interface and/or the source address */
949 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
952 ip6optlen
+= CMSG_SPACE(sizeof(int));
954 #ifdef IPV6_USE_MIN_MTU
956 ip6optlen
+= CMSG_SPACE(sizeof(int));
957 #endif /* IPV6_USE_MIN_MTU */
960 ip6optlen
+= CMSG_SPACE(sizeof(int));
962 if (how_so_traffic_class
< 2 && so_traffic_class
>= 0) {
963 (void) setsockopt(s
, SOL_SOCKET
, SO_TRAFFIC_CLASS
,
964 (void *)&so_traffic_class
, sizeof (so_traffic_class
));
966 if (how_so_traffic_class
> 0) {
968 (void) setsockopt(s
, SOL_SOCKET
, SO_RECV_TRAFFIC_CLASS
,
969 (void *)&on
, sizeof (on
));
971 if (how_so_traffic_class
> 1)
972 ip6optlen
+= CMSG_SPACE(sizeof(int));
974 /* set IP6 packet options */
976 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
977 errx(1, "can't allocate enough memory");
978 smsghdr
.msg_control
= (caddr_t
)scmsg
;
979 smsghdr
.msg_controllen
= ip6optlen
;
980 scmsgp
= (struct cmsghdr
*)scmsg
;
983 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
984 memset(pktinfo
, 0, sizeof(*pktinfo
));
985 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
986 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
987 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
988 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
991 /* set the outgoing interface */
993 #ifndef USE_SIN6_SCOPE_ID
994 /* pktinfo must have already been allocated */
995 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
996 errx(1, "%s: invalid interface name", ifname
);
998 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
999 errx(1, "%s: invalid interface name", ifname
);
1002 if (hoplimit
!= -1) {
1003 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1004 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1005 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
1006 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
1008 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1011 #ifdef IPV6_USE_MIN_MTU
1013 optval
= mflag
> 1 ? 0 : 1;
1015 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1016 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1017 scmsgp
->cmsg_type
= IPV6_USE_MIN_MTU
;
1018 *(int *)(CMSG_DATA(scmsgp
)) = optval
;
1020 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1022 #endif /* IPV6_USE_MIN_MTU */
1024 if (argc
> 1) { /* some intermediate addrs are specified */
1026 #ifdef USE_RFC2292BIS
1030 #ifdef USE_RFC2292BIS
1031 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
1032 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
1033 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1034 scmsgp
->cmsg_type
= IPV6_RTHDR
;
1035 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
1036 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
1037 IPV6_RTHDR_TYPE_0
, argc
- 1);
1039 errx(1, "can't initialize rthdr");
1040 #else /* old advanced API */
1041 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
1042 IPV6_RTHDR_TYPE_0
)) == 0)
1043 errx(1, "can't initialize rthdr");
1044 #endif /* USE_RFC2292BIS */
1046 for (hops
= 0; hops
< argc
- 1; hops
++) {
1047 struct addrinfo
*iaip
;
1049 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
1051 errx(1, "%s", gai_strerror(error
));
1052 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
1054 "bad addr family of an intermediate addr");
1056 #ifdef USE_RFC2292BIS
1057 if (inet6_rth_add(rthdr
,
1058 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
1059 errx(1, "can't add an intermediate node");
1060 #else /* old advanced API */
1061 if (inet6_rthdr_add(scmsgp
,
1062 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
1064 errx(1, "can't add an intermediate node");
1065 #endif /* USE_RFC2292BIS */
1069 #ifndef USE_RFC2292BIS
1070 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
1071 errx(1, "can't set the last flag");
1074 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1078 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1079 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1080 scmsgp
->cmsg_type
= IPV6_TCLASS
;
1081 *(int *)(CMSG_DATA(scmsgp
)) = tclass
;
1083 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1085 if (how_so_traffic_class
> 1 && so_traffic_class
>= 0) {
1086 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1087 scmsgp
->cmsg_level
= SOL_SOCKET
;
1088 scmsgp
->cmsg_type
= SO_TRAFFIC_CLASS
;
1089 *(int *)(CMSG_DATA(scmsgp
)) = so_traffic_class
;
1091 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1093 if (!(options
& F_SRCADDR
)) {
1095 * get the source address. XXX since we revoked the root
1096 * privilege, we cannot use a raw socket for this.
1099 socklen_t len
= sizeof(src
);
1101 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1102 err(1, "UDP socket");
1105 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
1106 (char *)&ifscope
, sizeof (ifscope
)) != 0)
1107 err(1, "setsockopt(IPV6_BOUND_IF)");
1111 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
1112 (char *)&nocell
, sizeof (nocell
)) != 0)
1113 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
1116 src
.sin6_family
= AF_INET6
;
1117 src
.sin6_addr
= dst
.sin6_addr
;
1118 src
.sin6_port
= ntohs(DUMMY_PORT
);
1119 src
.sin6_scope_id
= dst
.sin6_scope_id
;
1121 #ifdef USE_RFC2292BIS
1123 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
1124 (void *)pktinfo
, sizeof(*pktinfo
)))
1125 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1127 if (hoplimit
!= -1 &&
1128 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1129 (void *)&hoplimit
, sizeof(hoplimit
)))
1130 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1132 if (hoplimit
!= -1 &&
1133 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1134 (void *)&hoplimit
, sizeof(hoplimit
)))
1135 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1138 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
1139 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
1140 err(1, "UDP setsockopt(IPV6_RTHDR)");
1141 #else /* old advanced API */
1142 if (smsghdr
.msg_control
&&
1143 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
1144 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
1145 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1148 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
1149 err(1, "UDP connect");
1151 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
1152 err(1, "getsockname");
1157 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1159 if (datalen
> sockbufsize
)
1160 warnx("you need -b to increase socket buffer size");
1161 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
1162 sizeof(sockbufsize
)) < 0)
1163 err(1, "setsockopt(SO_SNDBUF)");
1164 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
1165 sizeof(sockbufsize
)) < 0)
1166 err(1, "setsockopt(SO_RCVBUF)");
1169 if (datalen
> 8 * 1024) /*XXX*/
1170 warnx("you need -b to increase socket buffer size");
1172 * When pinging the broadcast address, you can get a lot of
1173 * answers. Doing something so evil is useful if you are trying
1174 * to stress the ethernet, or just want to fill the arp cache
1175 * to get some stuff for /etc/ethers.
1178 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1184 #ifndef USE_SIN6_SCOPE_ID
1185 #ifdef IPV6_RECVPKTINFO
1186 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1187 sizeof(optval
)) < 0)
1188 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1189 #else /* old adv. API */
1190 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1191 sizeof(optval
)) < 0)
1192 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1194 #endif /* USE_SIN6_SCOPE_ID */
1195 #ifdef IPV6_RECVHOPLIMIT
1196 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1197 sizeof(optval
)) < 0)
1198 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1199 #else /* old adv. API */
1200 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1201 sizeof(optval
)) < 0)
1202 warn("setsockopt(IPV6_HOPLIMIT, %d, %lu)",
1203 optval
, sizeof(optval
)); /* XXX err? */
1206 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1207 (unsigned long)(pingerlen() - 8));
1208 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1209 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1211 while (preload
--) /* Fire off them quickies. */
1214 (void)signal(SIGINT
, onsignal
);
1216 (void)signal(SIGINFO
, onsignal
);
1219 if ((options
& F_FLOOD
) == 0) {
1220 (void)signal(SIGALRM
, onsignal
);
1221 itimer
.it_interval
= interval
;
1222 itimer
.it_value
= interval
;
1223 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1224 if (ntransmitted
== 0)
1229 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1230 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1234 seenalrm
= seenint
= 0;
1239 /* For control (ancillary) data received from recvmsg() */
1240 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1246 struct iovec iov
[2];
1248 /* signal handling */
1250 /* last packet sent, timeout reached? */
1251 if (npackets
&& ntransmitted
>= npackets
)
1270 if (options
& F_FLOOD
) {
1276 timeout
.tv_usec
= 10000;
1288 fdmaskp
[0].events
= POLLIN
;
1289 cc
= poll(fdmaskp
, 1, timeout
);
1291 memset(fdmaskp
, 0, fdmasks
);
1293 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1296 if (errno
!= EINTR
) {
1308 m
.msg_name
= (caddr_t
)&from
;
1309 m
.msg_namelen
= sizeof(from
);
1310 memset(&iov
, 0, sizeof(iov
));
1311 iov
[0].iov_base
= (caddr_t
)packet
;
1312 iov
[0].iov_len
= packlen
;
1315 memset(cm
, 0, CONTROLLEN
);
1316 m
.msg_control
= (void *)cm
;
1317 m
.msg_controllen
= CONTROLLEN
;
1319 cc
= recvmsg(s
, &m
, 0);
1321 if (errno
!= EINTR
) {
1326 } else if (cc
== 0) {
1330 * receive control messages only. Process the
1331 * exceptions (currently the only possibility is
1332 * a path MTU notification.)
1334 if ((mtu
= get_pathmtu(&m
)) > 0) {
1335 if ((options
& F_VERBOSE
) != 0) {
1336 printf("new path MTU (%d) is "
1343 * an ICMPv6 message (probably an echoreply) arrived.
1345 pr_pack(packet
, cc
, &m
);
1347 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1348 (npackets
> 0 && nreceived
>= npackets
))
1350 if (ntransmitted
- nreceived
- 1 > nmissedmax
) {
1351 nmissedmax
= ntransmitted
- nreceived
- 1;
1352 if (options
& F_MISSED
)
1353 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1371 if (fdmaskp
!= NULL
)
1375 exit(nreceived
== 0 ? 2 : 0);
1399 * This routine transmits another ping6.
1404 struct itimerval itimer
;
1410 * If we're not transmitting any more packets, change the timer
1411 * to wait two round-trip times if we've received any packets or
1412 * ten seconds if we haven't.
1416 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1417 if (itimer
.it_value
.tv_sec
== 0)
1418 itimer
.it_value
.tv_sec
= 1;
1420 itimer
.it_value
.tv_sec
= MAXWAIT
;
1421 itimer
.it_interval
.tv_sec
= 0;
1422 itimer
.it_interval
.tv_usec
= 0;
1423 itimer
.it_value
.tv_usec
= 0;
1425 (void)signal(SIGALRM
, onsignal
);
1426 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1431 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1432 * will be added on by the kernel. The ID field is our UNIX process ID,
1433 * and the sequence number is an ascending integer. The first 8 bytes
1434 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1435 * byte-order, to compute the round-trip time.
1442 if (options
& F_FQDN
)
1443 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1444 else if (options
& F_FQDNOLD
)
1446 else if (options
& F_NODEADDR
)
1447 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1448 else if (options
& F_SUPTYPES
)
1451 l
= ICMP6ECHOLEN
+ datalen
;
1459 struct icmp6_hdr
*icp
;
1460 struct iovec iov
[2];
1462 struct icmp6_nodeinfo
*nip
;
1465 if (npackets
&& ntransmitted
>= npackets
)
1466 return(-1); /* no more transmission */
1468 icp
= (struct icmp6_hdr
*)outpack
;
1469 nip
= (struct icmp6_nodeinfo
*)outpack
;
1470 memset(icp
, 0, sizeof(*icp
));
1471 icp
->icmp6_cksum
= 0;
1472 seq
= ntransmitted
++;
1473 CLR(seq
% mx_dup_ck
);
1475 if (options
& F_FQDN
) {
1476 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1477 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1478 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1479 nip
->ni_flags
= htons(0);
1481 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1482 sizeof(nip
->icmp6_ni_nonce
));
1483 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1485 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1486 sizeof(dst
.sin6_addr
));
1487 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1489 } else if (options
& F_FQDNOLD
) {
1490 /* packet format in 03 draft - no Subject data on queries */
1491 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1492 icp
->icmp6_code
= 0; /* code field is always 0 */
1493 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1494 nip
->ni_flags
= htons(0);
1496 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1497 sizeof(nip
->icmp6_ni_nonce
));
1498 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1502 } else if (options
& F_NODEADDR
) {
1503 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1504 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1505 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1506 nip
->ni_flags
= naflags
;
1508 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1509 sizeof(nip
->icmp6_ni_nonce
));
1510 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1512 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1513 sizeof(dst
.sin6_addr
));
1514 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1516 } else if (options
& F_SUPTYPES
) {
1517 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1518 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1519 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1520 /* we support compressed bitmap */
1521 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1523 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1524 sizeof(nip
->icmp6_ni_nonce
));
1525 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1529 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1530 icp
->icmp6_code
= 0;
1531 icp
->icmp6_id
= htons(ident
);
1532 icp
->icmp6_seq
= ntohs(seq
);
1536 (void)gettimeofday(&tv
, NULL
);
1537 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1538 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1539 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1541 cc
= ICMP6ECHOLEN
+ datalen
;
1545 if (pingerlen() != cc
)
1546 errx(1, "internal error; length mismatch");
1549 smsghdr
.msg_name
= (caddr_t
)&dst
;
1550 smsghdr
.msg_namelen
= sizeof(dst
);
1551 memset(&iov
, 0, sizeof(iov
));
1552 iov
[0].iov_base
= (caddr_t
)outpack
;
1553 iov
[0].iov_len
= cc
;
1554 smsghdr
.msg_iov
= iov
;
1555 smsghdr
.msg_iovlen
= 1;
1557 i
= sendmsg(s
, &smsghdr
, 0);
1559 if (i
< 0 || i
!= cc
) {
1562 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1565 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1566 (void)write(STDOUT_FILENO
, &DOT
, 1);
1572 myechoreply(const struct icmp6_hdr
*icp
)
1574 if (ntohs(icp
->icmp6_id
) == ident
)
1581 mynireply(const struct icmp6_nodeinfo
*nip
)
1583 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1584 nonce
+ sizeof(u_int16_t
),
1585 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1592 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1594 /*base for compressed name*/
1598 char cresult
[NS_MAXDNAME
+ 1];
1609 if (i
== 0 || cp
!= *sp
) {
1610 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1611 return NULL
; /*result overrun*/
1617 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1618 /* DNS compression */
1622 comp
= base
+ (i
& 0x3f);
1623 if (dnsdecode(&comp
, cp
, base
, cresult
,
1624 sizeof(cresult
)) == NULL
)
1626 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1627 return NULL
; /*result overrun*/
1629 } else if ((i
& 0x3f) == i
) {
1631 return NULL
; /*source overrun*/
1632 while (i
-- > 0 && cp
< ep
) {
1633 l
= snprintf(cresult
, sizeof(cresult
),
1634 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1635 if (l
>= sizeof(cresult
) || l
< 0)
1637 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1638 return NULL
; /*result overrun*/
1642 return NULL
; /*invalid label*/
1645 return NULL
; /*not terminated*/
1653 * Print out the packet, if it came from us. This logic is necessary
1654 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1655 * which arrive ('tis only fair). This permits multiple copies of this
1656 * program to be run without having intermingled output (or statistics!).
1659 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1661 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1662 struct icmp6_hdr
*icp
;
1663 struct icmp6_nodeinfo
*ni
;
1666 struct sockaddr
*from
;
1668 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1669 struct in6_pktinfo
*pktinfo
= NULL
;
1670 struct timeval tv
, tp
;
1672 double triptime
= 0;
1677 char dnsname
[NS_MAXDNAME
+ 1];
1681 (void)gettimeofday(&tv
, NULL
);
1683 if (!mhdr
|| !mhdr
->msg_name
||
1684 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1685 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1686 if (options
& F_VERBOSE
)
1687 warnx("invalid peername");
1690 from
= (struct sockaddr
*)mhdr
->msg_name
;
1691 fromlen
= mhdr
->msg_namelen
;
1692 if (cc
< sizeof(struct icmp6_hdr
)) {
1693 if (options
& F_VERBOSE
)
1694 warnx("packet too short (%d bytes) from %s", cc
,
1695 pr_addr(from
, fromlen
));
1698 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1699 (options
& F_VERBOSE
) != 0)
1700 warnx("some control data discarded, insufficient buffer size");
1701 icp
= (struct icmp6_hdr
*)buf
;
1702 ni
= (struct icmp6_nodeinfo
*)buf
;
1705 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1706 warnx("failed to get receiving hop limit");
1709 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1710 warnx("failed to get receiving packet information");
1713 if (rcvtclass
&& (tclass
= get_tclass(mhdr
)) == -1) {
1714 warnx("failed to get receiving traffic class");
1718 if (how_so_traffic_class
> 0)
1719 sotc
= get_so_traffic_class(mhdr
);
1721 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1722 seq
= ntohs(icp
->icmp6_seq
);
1725 tpp
= (struct tv32
*)(icp
+ 1);
1726 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1727 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1729 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1730 ((double)tv
.tv_usec
) / 1000.0;
1732 tsumsq
+= triptime
* triptime
;
1733 if (triptime
< tmin
)
1735 if (triptime
> tmax
)
1739 if (TST(seq
% mx_dup_ck
)) {
1744 SET(seq
% mx_dup_ck
);
1748 if (options
& F_QUIET
)
1751 if (options
& F_FLOOD
)
1752 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1754 if (options
& F_AUDIBLE
)
1755 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1756 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1757 pr_addr(from
, fromlen
), seq
);
1758 (void)printf(" hlim=%d", hoplim
);
1759 if ((options
& F_VERBOSE
) != 0) {
1760 struct sockaddr_in6 dstsa
;
1762 memset(&dstsa
, 0, sizeof(dstsa
));
1763 dstsa
.sin6_family
= AF_INET6
;
1764 dstsa
.sin6_len
= sizeof(dstsa
);
1765 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1766 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1767 (void)printf(" dst=%s",
1768 pr_addr((struct sockaddr
*)&dstsa
,
1772 (void)printf(" time=%.3f ms", triptime
);
1774 if (!IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
1775 (void)printf("(DUP!)");
1778 (void)printf(" tclass=%d", tclass
);
1780 (void)printf(" sotc=%d", sotc
);
1781 /* check the data */
1782 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1783 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1784 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1786 (void)printf("\nwrong data byte #%d "
1787 "should be 0x%x but was 0x%x",
1793 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1794 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1796 if (TST(seq
% mx_dup_ck
)) {
1801 SET(seq
% mx_dup_ck
);
1805 if (options
& F_QUIET
)
1808 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1810 switch (ntohs(ni
->ni_code
)) {
1811 case ICMP6_NI_SUCCESS
:
1813 case ICMP6_NI_REFUSED
:
1814 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1816 case ICMP6_NI_UNKNOWN
:
1817 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1820 printf("unknown code 0x%x, type 0x%x",
1821 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1825 switch (ntohs(ni
->ni_qtype
)) {
1827 printf("NodeInfo NOOP");
1829 case NI_QTYPE_SUPTYPES
:
1830 pr_suptypes(ni
, end
- (u_char
*)ni
);
1832 case NI_QTYPE_NODEADDR
:
1833 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1836 default: /* XXX: for backward compatibility */
1837 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1838 if (buf
[off
+ ICMP6_NIRLEN
] ==
1839 cc
- off
- ICMP6_NIRLEN
- 1)
1844 cp
++; /* skip length */
1846 safeputc(*cp
& 0xff);
1852 if (dnsdecode((const u_char
**)&cp
, end
,
1853 (const u_char
*)(ni
+ 1), dnsname
,
1854 sizeof(dnsname
)) == NULL
) {
1859 * name-lookup special handling for
1862 if (cp
+ 1 <= end
&& !*cp
&&
1863 strlen(dnsname
) > 0) {
1864 dnsname
[strlen(dnsname
) - 1] = '\0';
1867 printf("%s%s", i
> 0 ? "," : "",
1871 if (options
& F_VERBOSE
) {
1875 (void)printf(" ("); /*)*/
1877 switch (ni
->ni_code
) {
1878 case ICMP6_NI_REFUSED
:
1879 (void)printf("refused");
1882 case ICMP6_NI_UNKNOWN
:
1883 (void)printf("unknown qtype");
1888 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1889 /* case of refusion, unknown */
1894 ttl
= (int32_t)ntohl(*(u_int32_t
*)
1895 &buf
[off
+ICMP6ECHOLEN
+8]);
1898 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1899 (void)printf("TTL=%d:meaningless",
1903 (void)printf("TTL=%d:invalid",
1906 (void)printf("TTL=%d", ttl
);
1916 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1925 if (buf
[off
+ ICMP6_NIRLEN
] !=
1926 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1929 (void)printf("invalid namelen:%d/%lu",
1930 buf
[off
+ ICMP6_NIRLEN
],
1931 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1941 /* We've got something other than an ECHOREPLY */
1942 if (!(options
& F_VERBOSE
))
1944 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1948 if (!(options
& F_FLOOD
)) {
1949 (void)putchar('\n');
1950 if (options
& F_VERBOSE
)
1952 (void)fflush(stdout
);
1958 pr_exthdrs(struct msghdr
*mhdr
)
1965 bufp
= mhdr
->msg_control
;
1966 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1967 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1968 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1971 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1974 switch (cm
->cmsg_type
) {
1976 printf(" HbH Options: ");
1977 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1980 #ifdef IPV6_RTHDRDSTOPTS
1981 case IPV6_RTHDRDSTOPTS
:
1983 printf(" Dst Options: ");
1984 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1987 printf(" Routing: ");
1988 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1994 #ifdef USE_RFC2292BIS
1996 pr_ip6opt(void *extbuf
, size_t bufsize
)
1998 struct ip6_hbh
*ext
;
2001 socklen_t extlen
, len
, origextlen
;
2007 ext
= (struct ip6_hbh
*)extbuf
;
2008 extlen
= (ext
->ip6h_len
+ 1) * 8;
2009 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
2010 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
2013 * Bounds checking on the ancillary data buffer:
2014 * subtract the size of a cmsg structure from the buffer size.
2016 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
2017 origextlen
= extlen
;
2018 extlen
= bufsize
- CMSG_SPACE(0);
2019 warnx("options truncated, showing only %u (total=%u)",
2020 (unsigned int)(extlen
/ 8 - 1),
2021 (unsigned int)(ext
->ip6h_len
));
2026 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
2027 &type
, &len
, &databuf
);
2028 if (currentlen
== -1)
2032 * Note that inet6_opt_next automatically skips any padding
2037 offset
= inet6_opt_get_val(databuf
, offset
,
2038 &value4
, sizeof(value4
));
2039 printf(" Jumbo Payload Opt: Length %u\n",
2040 (u_int32_t
)ntohl(value4
));
2042 case IP6OPT_ROUTER_ALERT
:
2044 offset
= inet6_opt_get_val(databuf
, offset
,
2045 &value2
, sizeof(value2
));
2046 printf(" Router Alert Opt: Type %u\n",
2050 printf(" Received Opt %u len %lu\n",
2051 type
, (unsigned long)len
);
2057 #else /* !USE_RFC2292BIS */
2060 pr_ip6opt(void *extbuf
, size_t bufsize __unused
)
2065 #endif /* USE_RFC2292BIS */
2067 #ifdef USE_RFC2292BIS
2069 pr_rthdr(void *extbuf
, size_t bufsize
)
2071 struct in6_addr
*in6
;
2072 char ntopbuf
[INET6_ADDRSTRLEN
];
2073 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
2074 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
2076 /* print fixed part of the header */
2077 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
2078 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
2079 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
2080 printf("%d segments, ", segments
);
2081 printf("%d left\n", rh
->ip6r_segleft
);
2083 printf("segments unknown, ");
2084 printf("%d left\n", rh
->ip6r_segleft
);
2089 * Bounds checking on the ancillary data buffer. When calculating
2090 * the number of items to show keep in mind:
2091 * - The size of the cmsg structure
2092 * - The size of one segment (the size of a Type 0 routing header)
2093 * - When dividing add a fudge factor of one in case the
2094 * dividend is not evenly divisible by the divisor
2096 rthsize
= (rh
->ip6r_len
+ 1) * 8;
2097 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
2098 origsegs
= segments
;
2099 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
2100 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
2101 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
2102 (size1
- size0
) + 1;
2103 warnx("segments truncated, showing only %d (total=%d)",
2104 segments
, origsegs
);
2107 for (i
= 0; i
< segments
; i
++) {
2108 in6
= inet6_rth_getaddr(extbuf
, i
);
2110 printf(" [%d]<NULL>\n", i
);
2112 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
2114 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
2115 printf(" [%d]%s\n", i
, ntopbuf
);
2123 #else /* !USE_RFC2292BIS */
2126 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
2131 #endif /* USE_RFC2292BIS */
2134 pr_bitrange(u_int32_t v
, int soff
, int ii
)
2141 /* shift till we have 0x01 */
2142 if ((v
& 0x01) == 0) {
2144 printf("-%u", soff
+ off
- 1);
2155 case 0x04: case 0x0c:
2166 /* we have 0x01 with us */
2167 for (i
= 0; i
< 32 - off
; i
++) {
2168 if ((v
& (0x01 << i
)) == 0)
2172 printf(" %u", soff
+ off
);
2180 pr_suptypes(struct icmp6_nodeinfo
*ni
, size_t nilen
)
2181 /* ni->qtype must be SUPTYPES */
2185 const u_char
*cp
, *end
;
2188 u_int16_t words
; /*32bit count*/
2191 #define MAXQTYPES (1 << 16)
2195 cp
= (u_char
*)(ni
+ 1);
2196 end
= ((u_char
*)ni
) + nilen
;
2200 printf("NodeInfo Supported Qtypes");
2201 if (options
& F_VERBOSE
) {
2202 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
2203 printf(", compressed bitmap");
2205 printf(", raw bitmap");
2209 clen
= (size_t)(end
- cp
);
2210 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
2211 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
2217 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
2219 memcpy(&cbit
, cp
, sizeof(cbit
));
2220 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
2224 clen
= ntohs(cbit
.words
) * sizeof(v
);
2225 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
2230 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
2231 memcpy(&v
, cp
+ off
, sizeof(v
));
2232 v
= (u_int32_t
)ntohl(v
);
2233 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
2235 /* flush the remaining bits */
2236 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
2240 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
2241 cur
+= ntohs(cbit
.skip
) * 32;
2246 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, int nilen
)
2247 /* ni->qtype must be NODEADDR */
2249 u_char
*cp
= (u_char
*)(ni
+ 1);
2250 char ntop_buf
[INET6_ADDRSTRLEN
];
2253 nilen
-= sizeof(struct icmp6_nodeinfo
);
2255 if (options
& F_VERBOSE
) {
2256 switch (ni
->ni_code
) {
2257 case ICMP6_NI_REFUSED
:
2258 (void)printf("refused");
2260 case ICMP6_NI_UNKNOWN
:
2261 (void)printf("unknown qtype");
2264 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2265 (void)printf(" truncated");
2269 printf(" no address\n");
2272 * In icmp-name-lookups 05 and later, TTL of each returned address
2273 * is contained in the resposne. We try to detect the version
2274 * by the length of the data, but note that the detection algorithm
2275 * is incomplete. We assume the latest draft by default.
2277 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2283 /* XXX: alignment? */
2284 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2285 cp
+= sizeof(u_int32_t
);
2286 nilen
-= sizeof(u_int32_t
);
2289 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2291 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2292 printf(" %s", ntop_buf
);
2294 if (ttl
== 0xffffffff) {
2296 * XXX: can this convention be applied to all
2297 * type of TTL (i.e. non-ND TTL)?
2299 printf("(TTL=infty)");
2302 printf("(TTL=%u)", ttl
);
2306 nilen
-= sizeof(struct in6_addr
);
2307 cp
+= sizeof(struct in6_addr
);
2312 get_hoplim(struct msghdr
*mhdr
)
2316 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2317 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2318 if (cm
->cmsg_len
== 0)
2321 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2322 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2323 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2324 return(*(int *)CMSG_DATA(cm
));
2330 struct in6_pktinfo
*
2331 get_rcvpktinfo(struct msghdr
*mhdr
)
2335 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2336 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2337 if (cm
->cmsg_len
== 0)
2340 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2341 cm
->cmsg_type
== IPV6_PKTINFO
&&
2342 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2343 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2350 get_pathmtu(struct msghdr
*mhdr
)
2352 #ifdef IPV6_RECVPATHMTU
2354 struct ip6_mtuinfo
*mtuctl
= NULL
;
2356 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2357 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2358 if (cm
->cmsg_len
== 0)
2361 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2362 cm
->cmsg_type
== IPV6_PATHMTU
&&
2363 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2364 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2367 * If the notified destination is different from
2368 * the one we are pinging, just ignore the info.
2369 * We check the scope ID only when both notified value
2370 * and our own value have non-0 values, because we may
2371 * have used the default scope zone ID for sending,
2372 * in which case the scope ID value is 0.
2374 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2376 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2377 dst
.sin6_scope_id
&&
2378 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2379 dst
.sin6_scope_id
)) {
2380 if ((options
& F_VERBOSE
) != 0) {
2381 printf("path MTU for %s is notified. "
2383 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2384 sizeof(mtuctl
->ip6m_addr
)));
2390 * Ignore an invalid MTU. XXX: can we just believe
2393 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2396 /* notification for our destination. return the MTU. */
2397 return((int)mtuctl
->ip6m_mtu
);
2406 struct msghdr
*mhdr
;
2410 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2411 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2412 if (cm
->cmsg_len
== 0)
2415 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2416 cm
->cmsg_type
== IPV6_TCLASS
&&
2417 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2418 return(*(int *)CMSG_DATA(cm
));
2425 get_so_traffic_class(struct msghdr
*mhdr
)
2429 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2430 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2431 if (cm
->cmsg_len
== 0)
2434 if (cm
->cmsg_level
== SOL_SOCKET
&&
2435 cm
->cmsg_type
== SO_TRAFFIC_CLASS
&&
2436 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2437 return(*(int *)CMSG_DATA(cm
));
2445 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2449 tvsub(struct timeval
*out
, struct timeval
*in
)
2451 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2453 out
->tv_usec
+= 1000000;
2455 out
->tv_sec
-= in
->tv_sec
;
2464 onint(int notused __unused
)
2481 if (fdmaskp
!= NULL
)
2485 (void)signal(SIGINT
, SIG_DFL
);
2486 (void)kill(getpid(), SIGINT
);
2494 * Print out statistics.
2499 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2500 (void)printf("%ld packets transmitted, ", ntransmitted
);
2501 (void)printf("%ld packets received, ", nreceived
);
2503 (void)printf("+%ld duplicates, ", nrepeats
);
2505 if (nreceived
> ntransmitted
)
2506 (void)printf("-- somebody's duplicating packets!");
2508 (void)printf("%.1f%% packet loss",
2509 ((((double)ntransmitted
- nreceived
) * 100.0) /
2512 (void)putchar('\n');
2513 if (nreceived
&& timing
) {
2514 /* Only display average to microseconds */
2515 double num
= nreceived
+ nrepeats
;
2516 double avg
= tsum
/ num
;
2517 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2519 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2520 tmin
, avg
, tmax
, dev
);
2521 (void)fflush(stdout
);
2523 (void)fflush(stdout
);
2527 static const char *niqcode
[] = {
2529 "DNS label", /*or empty*/
2534 static const char *nircode
[] = {
2535 "Success", "Refused", "Unknown",
2541 * Print a descriptive string about an ICMP header.
2544 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2546 char ntop_buf
[INET6_ADDRSTRLEN
];
2547 struct nd_redirect
*red
;
2548 struct icmp6_nodeinfo
*ni
;
2549 char dnsname
[NS_MAXDNAME
+ 1];
2553 switch (icp
->icmp6_type
) {
2554 case ICMP6_DST_UNREACH
:
2555 switch (icp
->icmp6_code
) {
2556 case ICMP6_DST_UNREACH_NOROUTE
:
2557 (void)printf("No Route to Destination\n");
2559 case ICMP6_DST_UNREACH_ADMIN
:
2560 (void)printf("Destination Administratively "
2563 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2564 (void)printf("Destination Unreachable Beyond Scope\n");
2566 case ICMP6_DST_UNREACH_ADDR
:
2567 (void)printf("Destination Host Unreachable\n");
2569 case ICMP6_DST_UNREACH_NOPORT
:
2570 (void)printf("Destination Port Unreachable\n");
2573 (void)printf("Destination Unreachable, Bad Code: %d\n",
2577 /* Print returned IP header information */
2578 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2580 case ICMP6_PACKET_TOO_BIG
:
2581 (void)printf("Packet too big mtu = %d\n",
2582 (int)ntohl(icp
->icmp6_mtu
));
2583 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2585 case ICMP6_TIME_EXCEEDED
:
2586 switch (icp
->icmp6_code
) {
2587 case ICMP6_TIME_EXCEED_TRANSIT
:
2588 (void)printf("Time to live exceeded\n");
2590 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2591 (void)printf("Frag reassembly time exceeded\n");
2594 (void)printf("Time exceeded, Bad Code: %d\n",
2598 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2600 case ICMP6_PARAM_PROB
:
2601 (void)printf("Parameter problem: ");
2602 switch (icp
->icmp6_code
) {
2603 case ICMP6_PARAMPROB_HEADER
:
2604 (void)printf("Erroneous Header ");
2606 case ICMP6_PARAMPROB_NEXTHEADER
:
2607 (void)printf("Unknown Nextheader ");
2609 case ICMP6_PARAMPROB_OPTION
:
2610 (void)printf("Unrecognized Option ");
2613 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2616 (void)printf("pointer = 0x%02x\n",
2617 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2618 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2620 case ICMP6_ECHO_REQUEST
:
2621 (void)printf("Echo Request");
2622 /* XXX ID + Seq + Data */
2624 case ICMP6_ECHO_REPLY
:
2625 (void)printf("Echo Reply");
2626 /* XXX ID + Seq + Data */
2628 case ICMP6_MEMBERSHIP_QUERY
:
2629 (void)printf("Listener Query");
2631 case ICMP6_MEMBERSHIP_REPORT
:
2632 (void)printf("Listener Report");
2634 case ICMP6_MEMBERSHIP_REDUCTION
:
2635 (void)printf("Listener Done");
2637 case ND_ROUTER_SOLICIT
:
2638 (void)printf("Router Solicitation");
2640 case ND_ROUTER_ADVERT
:
2641 (void)printf("Router Advertisement");
2643 case ND_NEIGHBOR_SOLICIT
:
2644 (void)printf("Neighbor Solicitation");
2646 case ND_NEIGHBOR_ADVERT
:
2647 (void)printf("Neighbor Advertisement");
2650 red
= (struct nd_redirect
*)icp
;
2651 (void)printf("Redirect\n");
2652 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2654 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2655 (void)printf("Destination: %s", ntop_buf
);
2656 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2658 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2659 (void)printf(" New Target: %s", ntop_buf
);
2661 case ICMP6_NI_QUERY
:
2662 (void)printf("Node Information Query");
2663 /* XXX ID + Seq + Data */
2664 ni
= (struct icmp6_nodeinfo
*)icp
;
2665 l
= end
- (u_char
*)(ni
+ 1);
2667 switch (ntohs(ni
->ni_qtype
)) {
2669 (void)printf("NOOP");
2671 case NI_QTYPE_SUPTYPES
:
2672 (void)printf("Supported qtypes");
2675 (void)printf("DNS name");
2677 case NI_QTYPE_NODEADDR
:
2678 (void)printf("nodeaddr");
2680 case NI_QTYPE_IPV4ADDR
:
2681 (void)printf("IPv4 nodeaddr");
2684 (void)printf("unknown qtype");
2687 if (options
& F_VERBOSE
) {
2688 switch (ni
->ni_code
) {
2689 case ICMP6_NI_SUBJ_IPV6
:
2690 if (l
== sizeof(struct in6_addr
) &&
2691 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2692 sizeof(ntop_buf
)) != NULL
) {
2693 (void)printf(", subject=%s(%s)",
2694 niqcode
[ni
->ni_code
], ntop_buf
);
2697 /* backward compat to -W */
2698 (void)printf(", oldfqdn");
2700 (void)printf(", invalid");
2704 case ICMP6_NI_SUBJ_FQDN
:
2705 if (end
== (u_char
*)(ni
+ 1)) {
2706 (void)printf(", no subject");
2709 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2710 cp
= (const u_char
*)(ni
+ 1);
2711 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2712 sizeof(dnsname
)) != NULL
)
2713 printf("(%s)", dnsname
);
2715 printf("(invalid)");
2717 case ICMP6_NI_SUBJ_IPV4
:
2718 if (l
== sizeof(struct in_addr
) &&
2719 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2720 sizeof(ntop_buf
)) != NULL
) {
2721 (void)printf(", subject=%s(%s)",
2722 niqcode
[ni
->ni_code
], ntop_buf
);
2724 (void)printf(", invalid");
2727 (void)printf(", invalid");
2732 case ICMP6_NI_REPLY
:
2733 (void)printf("Node Information Reply");
2734 /* XXX ID + Seq + Data */
2735 ni
= (struct icmp6_nodeinfo
*)icp
;
2737 switch (ntohs(ni
->ni_qtype
)) {
2739 (void)printf("NOOP");
2741 case NI_QTYPE_SUPTYPES
:
2742 (void)printf("Supported qtypes");
2745 (void)printf("DNS name");
2747 case NI_QTYPE_NODEADDR
:
2748 (void)printf("nodeaddr");
2750 case NI_QTYPE_IPV4ADDR
:
2751 (void)printf("IPv4 nodeaddr");
2754 (void)printf("unknown qtype");
2757 if (options
& F_VERBOSE
) {
2758 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2759 printf(", invalid");
2761 printf(", %s", nircode
[ni
->ni_code
]);
2765 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2771 * Print an IP6 header.
2774 pr_iph(struct ip6_hdr
*ip6
)
2776 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2778 char ntop_buf
[INET6_ADDRSTRLEN
];
2780 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2781 tc
= (tc
>> 4) & 0x0f;
2782 tc
|= (ip6
->ip6_vfc
<< 4);
2784 printf("Vr TC Flow Plen Nxt Hlim\n");
2785 printf(" %1x %02x %05x %04x %02x %02x\n",
2786 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2787 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2788 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2789 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2790 printf("%s->", ntop_buf
);
2791 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2792 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2793 printf("%s\n", ntop_buf
);
2798 * Return an ascii host address as a dotted quad and optionally with
2802 pr_addr(struct sockaddr
*addr
, int addrlen
)
2804 static char buf
[NI_MAXHOST
];
2807 if ((options
& F_HOSTNAME
) == 0)
2808 flag
|= NI_NUMERICHOST
;
2810 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2818 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2821 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2823 u_char
*cp
= (u_char
*)ip6
, nh
;
2826 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2831 hlen
= sizeof(*ip6
);
2835 while (end
- cp
>= 8) {
2837 case IPPROTO_HOPOPTS
:
2839 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2840 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2842 case IPPROTO_DSTOPTS
:
2844 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2845 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2847 case IPPROTO_FRAGMENT
:
2849 hlen
= sizeof(struct ip6_frag
);
2850 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2852 case IPPROTO_ROUTING
:
2854 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2855 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2860 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2861 nh
= ((struct ah
*)cp
)->ah_nxt
;
2864 case IPPROTO_ICMPV6
:
2865 printf("ICMP6: type = %d, code = %d\n",
2872 printf("TCP: from port %u, to port %u (decimal)\n",
2873 (*cp
* 256 + *(cp
+ 1)),
2874 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2877 printf("UDP: from port %u, to port %u (decimal)\n",
2878 (*cp
* 256 + *(cp
+ 1)),
2879 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2882 printf("Unknown Header(%d)\n", nh
);
2886 if ((cp
+= hlen
) >= end
)
2901 fill(char *bp
, char *patp
)
2907 for (cp
= patp
; *cp
; cp
++)
2909 errx(1, "patterns must be specified as hex digits");
2911 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2912 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2913 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2914 &pat
[13], &pat
[14], &pat
[15]);
2919 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
2921 for (jj
= 0; jj
< ii
; ++jj
)
2922 bp
[jj
+ kk
] = pat
[jj
];
2923 if (!(options
& F_QUIET
)) {
2924 (void)printf("PATTERN: 0x");
2925 for (jj
= 0; jj
< ii
; ++jj
)
2926 (void)printf("%02x", bp
[jj
] & 0xFF);
2932 #ifdef IPSEC_POLICY_IPSEC
2934 setpolicy(int so __unused
, char *policy
)
2939 return 0; /* ignore */
2941 buf
= ipsec_set_policy(policy
, strlen(policy
));
2943 errx(1, "%s", ipsec_strerror());
2944 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2945 ipsec_get_policylen(buf
)) < 0)
2946 warnx("Unable to set IPsec policy");
2960 u_int8_t digest
[16];
2963 char hbuf
[NI_MAXHOST
];
2964 struct in6_addr in6
;
2966 p
= strchr(name
, '.');
2968 p
= name
+ strlen(name
);
2970 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2971 return NULL
; /*label too long*/
2972 strncpy(hbuf
, name
, l
);
2973 hbuf
[(int)l
] = '\0';
2975 for (q
= name
; *q
; q
++) {
2976 if (isupper(*(unsigned char *)q
))
2977 *q
= tolower(*(unsigned char *)q
);
2980 /* generate 8 bytes of pseudo-random value. */
2981 memset(&ctxt
, 0, sizeof(ctxt
));
2984 MD5Update(&ctxt
, &c
, sizeof(c
));
2985 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2986 MD5Final(digest
, &ctxt
);
2988 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2989 return NULL
; /*XXX*/
2990 bcopy(digest
, &in6
.s6_addr
[12], 4);
2992 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2995 return strdup(hbuf
);
2999 str2svc(const char *str
)
3004 if (str
== NULL
|| *str
== '\0')
3006 else if (strcasecmp(str
, "BK_SYS") == 0)
3007 return SO_TC_BK_SYS
;
3008 else if (strcasecmp(str
, "BK") == 0)
3010 else if (strcasecmp(str
, "BE") == 0)
3012 else if (strcasecmp(str
, "RD") == 0)
3014 else if (strcasecmp(str
, "OAM") == 0)
3016 else if (strcasecmp(str
, "AV") == 0)
3018 else if (strcasecmp(str
, "RV") == 0)
3020 else if (strcasecmp(str
, "VI") == 0)
3022 else if (strcasecmp(str
, "VO") == 0)
3024 else if (strcasecmp(str
, "CTL") == 0)
3027 svc
= strtoul(str
, &endptr
, 0);
3028 if (*endptr
!= '\0')
3037 (void)fprintf(stderr
,
3038 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3043 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3047 #ifdef IPV6_USE_MIN_MTU
3051 "[-a addrtype] [-b bufsiz] [-B boundif] [-c count]\n"
3052 " [-g gateway] [-h hoplimit] [-I interface] [-i wait] [-l preload]"
3053 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
3057 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
3058 "[hops ...] host\n");