1 /* $NetBSD: ping6.c,v 1.71 2008/10/13 13:47:35 dholland Exp $ */
2 /* $KAME: ping6.c,v 1.164 2002/11/16 14:05:37 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
36 * Copyright (c) 1989, 1993
37 * The Regents of the University of California. All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 static char copyright
[] =
70 "@(#) Copyright (c) 1989, 1993\n\
71 The Regents of the University of California. All rights reserved.\n";
75 static char sccsid
[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
78 #include <sys/cdefs.h>
80 __RCSID("$NetBSD: ping6.c,v 1.71 2008/10/13 13:47:35 dholland Exp $");
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
135 #include <netinet6/ah.h>
136 #include <netinet6/ipsec.h>
146 #define MAXPACKETLEN 131072
148 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
149 #define ICMP6ECHOTMLEN sizeof(struct tv32)
150 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
151 /* FQDN case, 64 bits of nonce + 32 bits ttl */
152 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
153 #define EXTRA 256 /* for AH and various other headers. weird. */
154 #define DEFDATALEN ICMP6ECHOTMLEN
155 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
156 #define NROUTES 9 /* number of record route slots */
158 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
159 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
160 #define SET(bit) (A(bit) |= B(bit))
161 #define CLR(bit) (A(bit) &= (~B(bit)))
162 #define TST(bit) (A(bit) & B(bit))
164 #define F_FLOOD 0x0001
165 #define F_INTERVAL 0x0002
166 #define F_PINGFILLED 0x0008
167 #define F_QUIET 0x0010
168 #define F_RROUTE 0x0020
169 #define F_SO_DEBUG 0x0040
170 #define F_VERBOSE 0x0100
172 #ifdef IPSEC_POLICY_IPSEC
173 #define F_POLICY 0x0400
175 #define F_AUTHHDR 0x0200
176 #define F_ENCRYPT 0x0400
177 #endif /*IPSEC_POLICY_IPSEC*/
179 #define F_NODEADDR 0x0800
180 #define F_FQDN 0x1000
181 #define F_INTERFACE 0x2000
182 #define F_SRCADDR 0x4000
183 #ifdef IPV6_REACHCONF
184 #define F_REACHCONF 0x8000
186 #define F_HOSTNAME 0x10000
187 #define F_FQDNOLD 0x20000
188 #define F_NIGROUP 0x40000
189 #define F_SUPTYPES 0x80000
190 #define F_NOMINMTU 0x100000
191 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
194 #define IN6LEN sizeof(struct in6_addr)
195 #define SA6LEN sizeof(struct sockaddr_in6)
196 #define DUMMY_PORT 10101
198 #define SIN6(s) ((struct sockaddr_in6 *)(s))
201 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
202 * number of received sequence numbers we can keep track of. Change 128
203 * to 8192 for complete accuracy...
205 #define MAX_DUP_CHK (8 * 8192)
206 int mx_dup_ck
= MAX_DUP_CHK
;
207 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
209 struct addrinfo
*res
;
210 struct sockaddr_in6 dst
; /* who to ping6 */
211 struct sockaddr_in6 src
; /* src addr of this packet */
213 int datalen
= DEFDATALEN
;
214 int s
; /* socket file descriptor */
215 u_char outpack
[MAXPACKETLEN
];
216 char BSPACE
= '\b'; /* characters written for flood */
219 int ident
; /* process id to identify our packets */
220 u_int8_t nonce
[8]; /* nonce field for node information */
221 int hoplimit
= -1; /* hoplimit */
222 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
225 long npackets
; /* max packets to transmit */
226 long nreceived
; /* # of packets we got back */
227 long nrepeats
; /* number of duplicates */
228 long ntransmitted
; /* sequence # for outbound packets = #sent */
229 struct timeval interval
= {1, 0}; /* interval between packets */
232 int timing
; /* flag to do timing */
233 double tmin
= 999999999.0; /* minimum round trip time */
234 double tmax
= 0.0; /* maximum round trip time */
235 double tsum
= 0.0; /* sum of all times, for doing average */
236 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
238 /* for node addresses */
241 /* for ancillary data(advanced API) */
242 struct msghdr smsghdr
;
243 struct iovec smsgiov
;
246 volatile sig_atomic_t seenalrm
;
247 volatile sig_atomic_t seenint
;
249 volatile sig_atomic_t seeninfo
;
252 void fill(char *, char *);
253 int get_hoplim(struct msghdr
*);
254 int get_pathmtu(struct msghdr
*);
255 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
257 void retransmit(void);
259 size_t pingerlen(void);
261 const char *pr_addr(struct sockaddr
*, int);
262 void pr_icmph(struct icmp6_hdr
*, u_char
*);
263 void pr_iph(struct ip6_hdr
*);
264 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
265 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
266 int myechoreply(const struct icmp6_hdr
*);
267 int mynireply(const struct icmp6_nodeinfo
*);
268 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
270 void pr_pack(u_char
*, int, struct msghdr
*);
271 void pr_exthdrs(struct msghdr
*);
272 void pr_ip6opt(void *);
273 void pr_rthdr(void *);
274 int pr_bitrange(u_int32_t
, int, int);
275 void pr_retip(struct ip6_hdr
*, u_char
*);
277 void tvsub(struct timeval
*, struct timeval
*);
278 int setpolicy(int, char *);
279 char *nigroup(char *);
283 main(int argc
, char *argv
[])
285 struct itimerval itimer
;
286 struct sockaddr_in6 from
;
288 struct addrinfo hints
;
289 struct pollfd fdmaskp
[1];
292 int ch
, hold
, preload
, optval
, ret_ga
;
293 u_char
*datap
, *packet
;
294 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
296 struct cmsghdr
*scmsgp
= NULL
;
297 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
302 struct in6_pktinfo
*pktinfo
= NULL
;
303 struct ip6_rthdr
*rthdr
= NULL
;
304 #ifdef IPSEC_POLICY_IPSEC
305 char *policy_in
= NULL
;
306 char *policy_out
= NULL
;
310 #ifdef IPV6_USE_MIN_MTU
314 /* just to be sure */
315 memset(&smsghdr
, 0, sizeof(smsghdr
));
316 memset(&smsgiov
, 0, sizeof(smsgiov
));
319 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
323 #ifdef IPSEC_POLICY_IPSEC
327 #endif /*IPSEC_POLICY_IPSEC*/
329 while ((ch
= getopt(argc
, argv
,
330 "a:b:c:dfHg:h:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS
)) != -1) {
337 options
&= ~F_NOUSERDATA
;
338 options
|= F_NODEADDR
;
339 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
342 naflags
|= NI_NODEADDR_FLAG_ALL
;
346 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
350 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
354 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
358 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
360 case 'A': /* experimental. not in the spec */
361 #ifdef NI_NODEADDR_FLAG_ANYCAST
362 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
366 "-a A is not supported on the platform");
377 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
380 lsockbufsize
= strtoul(optarg
, &e
, 10);
381 sockbufsize
= lsockbufsize
;
382 if (errno
|| !*optarg
|| *e
||
383 (u_long
)sockbufsize
!= lsockbufsize
)
384 errx(1, "invalid socket buffer size");
387 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
391 npackets
= strtol(optarg
, &e
, 10);
392 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
394 "illegal number of packets -- %s", optarg
);
397 options
|= F_SO_DEBUG
;
402 errx(1, "Must be superuser to flood ping");
405 setbuf(stdout
, (char *)NULL
);
411 options
|= F_HOSTNAME
;
413 case 'h': /* hoplimit */
414 hoplimit
= strtol(optarg
, &e
, 10);
415 if (*optarg
== '\0' || *e
!= '\0')
416 errx(1, "illegal hoplimit %s", optarg
);
417 if (255 < hoplimit
|| hoplimit
< -1)
419 "illegal hoplimit -- %s", optarg
);
423 options
|= F_INTERFACE
;
424 #ifndef USE_SIN6_SCOPE_ID
428 case 'i': /* wait between sending packets */
429 intval
= strtod(optarg
, &e
);
430 if (*optarg
== '\0' || *e
!= '\0')
431 errx(1, "illegal timing interval %s", optarg
);
432 if (intval
< 1 && getuid()) {
433 errx(1, "%s: only root may use interval < 1s",
436 interval
.tv_sec
= (long)intval
;
438 (long)((intval
- interval
.tv_sec
) * 1000000);
439 if (interval
.tv_sec
< 0)
440 errx(1, "illegal timing interval %s", optarg
);
441 /* less than 1/hz does not make sense */
442 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
443 warnx("too small interval, raised to 0.01");
444 interval
.tv_usec
= 10000;
446 options
|= F_INTERVAL
;
451 errx(1, "Must be superuser to preload");
453 preload
= strtol(optarg
, &e
, 10);
454 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
455 errx(1, "illegal preload value -- %s", optarg
);
458 #ifdef IPV6_USE_MIN_MTU
462 errx(1, "-%c is not supported on this platform", ch
);
466 options
&= ~F_HOSTNAME
;
469 options
|= F_NIGROUP
;
471 case 'p': /* fill buffer with user pattern */
472 options
|= F_PINGFILLED
;
473 fill((char *)datap
, optarg
);
479 #ifdef IPV6_REACHCONF
480 options
|= F_REACHCONF
;
483 errx(1, "-R is not supported in this configuration");
486 memset(&hints
, 0, sizeof(struct addrinfo
));
487 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
488 hints
.ai_family
= AF_INET6
;
489 hints
.ai_socktype
= SOCK_RAW
;
490 hints
.ai_protocol
= IPPROTO_ICMPV6
;
492 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
494 errx(1, "invalid source address: %s",
495 gai_strerror(ret_ga
));
498 * res->ai_family must be AF_INET6 and res->ai_addrlen
499 * must be sizeof(src).
501 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
502 srclen
= res
->ai_addrlen
;
504 options
|= F_SRCADDR
;
506 case 's': /* size of packet to send */
507 datalen
= strtol(optarg
, &e
, 10);
508 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
509 errx(1, "illegal datalen value -- %s", optarg
);
510 if (datalen
> MAXDATALEN
) {
512 "datalen value too large, maximum is %d",
517 options
&= ~F_NOUSERDATA
;
518 options
|= F_SUPTYPES
;
521 options
|= F_VERBOSE
;
524 options
&= ~F_NOUSERDATA
;
528 options
&= ~F_NOUSERDATA
;
529 options
|= F_FQDNOLD
;
532 #ifdef IPSEC_POLICY_IPSEC
535 if (!strncmp("in", optarg
, 2)) {
536 if ((policy_in
= strdup(optarg
)) == NULL
)
538 } else if (!strncmp("out", optarg
, 3)) {
539 if ((policy_out
= strdup(optarg
)) == NULL
)
542 errx(1, "invalid security policy");
546 options
|= F_AUTHHDR
;
549 options
|= F_ENCRYPT
;
551 #endif /*IPSEC_POLICY_IPSEC*/
568 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
571 errx(1, "too many intermediate hops");
577 if (options
& F_NIGROUP
) {
578 target
= nigroup(argv
[argc
- 1]);
579 if (target
== NULL
) {
584 target
= argv
[argc
- 1];
587 memset(&hints
, 0, sizeof(struct addrinfo
));
588 hints
.ai_flags
= AI_CANONNAME
;
589 hints
.ai_family
= AF_INET6
;
590 hints
.ai_socktype
= SOCK_RAW
;
591 hints
.ai_protocol
= IPPROTO_ICMPV6
;
593 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
595 errx(1, "%s", gai_strerror(ret_ga
));
596 if (res
->ai_canonname
)
597 hostname
= res
->ai_canonname
;
602 errx(1, "getaddrinfo failed");
604 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
606 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
607 res
->ai_protocol
)) < 0)
610 /* set the source address if specified. */
611 if ((options
& F_SRCADDR
) &&
612 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
616 /* set the gateway (next hop) if specified */
618 struct addrinfo ghints
, *gres
;
621 memset(&ghints
, 0, sizeof(ghints
));
622 ghints
.ai_family
= AF_INET6
;
623 ghints
.ai_socktype
= SOCK_RAW
;
624 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
626 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
628 errx(1, "getaddrinfo for the gateway %s: %s",
629 gateway
, gai_strerror(error
));
631 if (gres
->ai_next
&& (options
& F_VERBOSE
))
632 warnx("gateway resolves to multiple addresses");
634 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
635 gres
->ai_addr
, gres
->ai_addrlen
)) {
636 err(1, "setsockopt(IPV6_NEXTHOP)");
643 * let the kerel pass extension headers of incoming packets,
644 * for privileged socket options
646 if ((options
& F_VERBOSE
) != 0) {
649 #ifdef IPV6_RECVHOPOPTS
650 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
652 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
653 #else /* old adv. API */
654 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
656 err(1, "setsockopt(IPV6_HOPOPTS)");
658 #ifdef IPV6_RECVDSTOPTS
659 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
661 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
662 #else /* old adv. API */
663 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
665 err(1, "setsockopt(IPV6_DSTOPTS)");
667 #ifdef IPV6_RECVRTHDRDSTOPTS
668 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
670 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
674 /* revoke root privilege */
678 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
679 errx(1, "-f and -i incompatible options");
681 if ((options
& F_NOUSERDATA
) == 0) {
682 if (datalen
>= (int)sizeof(struct tv32
)) {
683 /* we can time transfer */
687 /* in F_VERBOSE case, we may get non-echoreply packets*/
688 if (options
& F_VERBOSE
)
689 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
691 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
693 /* suppress timing for node information query */
696 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
699 if (!(packet
= (u_char
*)malloc(packlen
)))
700 err(1, "Unable to allocate packet");
701 if (!(options
& F_PINGFILLED
))
702 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
705 ident
= arc4random() & 0xFFFF;
706 memset(nonce
, 0, sizeof(nonce
));
707 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
708 *((u_int32_t
*)&nonce
[i
]) = arc4random();
712 if (options
& F_SO_DEBUG
)
713 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
715 optval
= IPV6_DEFHLIM
;
716 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
717 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
718 &optval
, sizeof(optval
)) == -1)
719 err(1, "IPV6_MULTICAST_HOPS");
720 #ifdef IPV6_USE_MIN_MTU
722 optval
= mflag
> 1 ? 0 : 1;
724 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
725 &optval
, sizeof(optval
)) == -1)
726 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
728 #ifdef IPV6_RECVPATHMTU
731 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
732 &optval
, sizeof(optval
)) == -1)
733 err(1, "setsockopt(IPV6_RECVPATHMTU)");
735 #endif /* IPV6_RECVPATHMTU */
736 #endif /* IPV6_USE_MIN_MTU */
739 #ifdef IPSEC_POLICY_IPSEC
740 if (options
& F_POLICY
) {
741 if (setpolicy(s
, policy_in
) < 0)
742 errx(1, "%s", ipsec_strerror());
743 if (setpolicy(s
, policy_out
) < 0)
744 errx(1, "%s", ipsec_strerror());
747 if (options
& F_AUTHHDR
) {
748 optval
= IPSEC_LEVEL_REQUIRE
;
749 #ifdef IPV6_AUTH_TRANS_LEVEL
750 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
751 &optval
, sizeof(optval
)) == -1)
752 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
754 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
755 &optval
, sizeof(optval
)) == -1)
756 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
759 if (options
& F_ENCRYPT
) {
760 optval
= IPSEC_LEVEL_REQUIRE
;
761 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
762 &optval
, sizeof(optval
)) == -1)
763 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
765 #endif /*IPSEC_POLICY_IPSEC*/
770 struct icmp6_filter filt
;
771 if (!(options
& F_VERBOSE
)) {
772 ICMP6_FILTER_SETBLOCKALL(&filt
);
773 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
774 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
775 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
777 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
779 ICMP6_FILTER_SETPASSALL(&filt
);
781 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
783 err(1, "setsockopt(ICMP6_FILTER)");
785 #endif /*ICMP6_FILTER*/
787 /* let the kerel pass extension headers of incoming packets */
788 if ((options
& F_VERBOSE
) != 0) {
791 #ifdef IPV6_RECVRTHDR
792 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
794 err(1, "setsockopt(IPV6_RECVRTHDR)");
795 #else /* old adv. API */
796 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
798 err(1, "setsockopt(IPV6_RTHDR)");
804 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
805 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
806 &optval, sizeof(optval)) == -1)
807 err(1, "IPV6_MULTICAST_LOOP");
810 /* Specify the outgoing interface and/or the source address */
812 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
815 ip6optlen
+= CMSG_SPACE(sizeof(int));
817 #ifdef IPV6_REACHCONF
818 if (options
& F_REACHCONF
)
819 ip6optlen
+= CMSG_SPACE(0);
822 /* set IP6 packet options */
824 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
825 errx(1, "can't allocate enough memory");
826 smsghdr
.msg_control
= (caddr_t
)scmsg
;
827 smsghdr
.msg_controllen
= ip6optlen
;
828 scmsgp
= (struct cmsghdr
*)scmsg
;
831 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
832 memset(pktinfo
, 0, sizeof(*pktinfo
));
833 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
834 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
835 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
836 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
839 /* set the outgoing interface */
841 #ifndef USE_SIN6_SCOPE_ID
842 /* pktinfo must have already been allocated */
843 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
844 errx(1, "%s: invalid interface name", ifname
);
846 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
847 errx(1, "%s: invalid interface name", ifname
);
850 if (hoplimit
!= -1) {
851 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
852 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
853 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
854 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
856 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
858 #ifdef IPV6_REACHCONF
859 if (options
& F_REACHCONF
) {
860 scmsgp
->cmsg_len
= CMSG_LEN(0);
861 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
862 scmsgp
->cmsg_type
= IPV6_REACHCONF
;
864 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
868 if (argc
> 1) { /* some intermediate addrs are specified */
872 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
873 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
874 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
875 scmsgp
->cmsg_type
= IPV6_RTHDR
;
876 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
877 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
878 IPV6_RTHDR_TYPE_0
, argc
- 1);
880 errx(1, "can't initialize rthdr");
882 for (hops
= 0; hops
< argc
- 1; hops
++) {
883 struct addrinfo
*iaip
;
885 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
887 errx(1, "%s", gai_strerror(error
));
888 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
890 "bad addr family of an intermediate addr");
892 if (inet6_rth_add(rthdr
,
893 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
894 errx(1, "can't add an intermediate node");
898 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
901 if (!(options
& F_SRCADDR
)) {
903 * get the source address. XXX since we revoked the root
904 * privilege, we cannot use a raw socket for this.
907 socklen_t len
= sizeof(src
);
909 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
910 err(1, "UDP socket");
912 src
.sin6_family
= AF_INET6
;
913 src
.sin6_addr
= dst
.sin6_addr
;
914 src
.sin6_port
= ntohs(DUMMY_PORT
);
915 src
.sin6_scope_id
= dst
.sin6_scope_id
;
918 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
919 (void *)pktinfo
, sizeof(*pktinfo
)))
920 err(1, "UDP setsockopt(IPV6_PKTINFO)");
922 if (hoplimit
!= -1 &&
923 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
924 (void *)&hoplimit
, sizeof(hoplimit
)))
925 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
927 if (hoplimit
!= -1 &&
928 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
929 (void *)&hoplimit
, sizeof(hoplimit
)))
930 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
933 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
934 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
935 err(1, "UDP setsockopt(IPV6_RTHDR)");
937 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
938 err(1, "UDP connect");
940 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
941 err(1, "getsockname");
946 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
948 if (datalen
> sockbufsize
)
949 warnx("you need -b to increase socket buffer size");
950 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
951 sizeof(sockbufsize
)) < 0)
952 err(1, "setsockopt(SO_SNDBUF)");
953 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
954 sizeof(sockbufsize
)) < 0)
955 err(1, "setsockopt(SO_RCVBUF)");
958 if (datalen
> 8 * 1024) /*XXX*/
959 warnx("you need -b to increase socket buffer size");
961 * When pinging the broadcast address, you can get a lot of
962 * answers. Doing something so evil is useful if you are trying
963 * to stress the ethernet, or just want to fill the arp cache
964 * to get some stuff for /etc/ethers.
967 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
973 #ifndef USE_SIN6_SCOPE_ID
974 #ifdef IPV6_RECVPKTINFO
975 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
977 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
978 #else /* old adv. API */
979 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
981 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
983 #endif /* USE_SIN6_SCOPE_ID */
984 #ifdef IPV6_RECVHOPLIMIT
985 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
987 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
988 #else /* old adv. API */
989 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
991 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
994 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
995 (unsigned long)(pingerlen() - 8));
996 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
997 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
999 while (preload
--) /* Fire off them quickies. */
1002 (void)signal(SIGINT
, onsignal
);
1004 (void)signal(SIGINFO
, onsignal
);
1007 if ((options
& F_FLOOD
) == 0) {
1008 (void)signal(SIGALRM
, onsignal
);
1009 itimer
.it_interval
= interval
;
1010 itimer
.it_value
= interval
;
1011 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1012 if (ntransmitted
== 0)
1016 seenalrm
= seenint
= 0;
1025 struct iovec iov
[2];
1027 /* signal handling */
1046 if (options
& F_FLOOD
) {
1053 fdmaskp
[0].events
= POLLIN
;
1054 cc
= poll(fdmaskp
, 1, timeout
);
1056 if (errno
!= EINTR
) {
1064 m
.msg_name
= (caddr_t
)&from
;
1065 m
.msg_namelen
= sizeof(from
);
1066 memset(&iov
, 0, sizeof(iov
));
1067 iov
[0].iov_base
= (caddr_t
)packet
;
1068 iov
[0].iov_len
= packlen
;
1071 cm
= (struct cmsghdr
*)buf
;
1072 m
.msg_control
= (caddr_t
)buf
;
1073 m
.msg_controllen
= sizeof(buf
);
1075 cc
= recvmsg(s
, &m
, 0);
1077 if (errno
!= EINTR
) {
1082 } else if (cc
== 0) {
1086 * receive control messages only. Process the
1087 * exceptions (currently the only possiblity is
1088 * a path MTU notification.)
1090 if ((mtu
= get_pathmtu(&m
)) > 0) {
1091 if ((options
& F_VERBOSE
) != 0) {
1092 printf("new path MTU (%d) is "
1099 * an ICMPv6 message (probably an echoreply) arrived.
1101 pr_pack(packet
, cc
, &m
);
1103 if (npackets
&& nreceived
>= npackets
)
1107 exit(nreceived
== 0);
1131 * This routine transmits another ping6.
1136 struct itimerval itimer
;
1142 * If we're not transmitting any more packets, change the timer
1143 * to wait two round-trip times if we've received any packets or
1144 * ten seconds if we haven't.
1148 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1149 if (itimer
.it_value
.tv_sec
== 0)
1150 itimer
.it_value
.tv_sec
= 1;
1152 itimer
.it_value
.tv_sec
= MAXWAIT
;
1153 itimer
.it_interval
.tv_sec
= 0;
1154 itimer
.it_interval
.tv_usec
= 0;
1155 itimer
.it_value
.tv_usec
= 0;
1157 (void)signal(SIGALRM
, onint
);
1158 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1163 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1164 * will be added on by the kernel. The ID field is our UNIX process ID,
1165 * and the sequence number is an ascending integer. The first 8 bytes
1166 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1167 * byte-order, to compute the round-trip time.
1174 if (options
& F_FQDN
)
1175 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1176 else if (options
& F_FQDNOLD
)
1178 else if (options
& F_NODEADDR
)
1179 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1180 else if (options
& F_SUPTYPES
)
1183 l
= ICMP6ECHOLEN
+ datalen
;
1191 struct icmp6_hdr
*icp
;
1192 struct iovec iov
[2];
1194 struct icmp6_nodeinfo
*nip
;
1197 if (npackets
&& ntransmitted
>= npackets
)
1198 return(-1); /* no more transmission */
1200 icp
= (struct icmp6_hdr
*)outpack
;
1201 nip
= (struct icmp6_nodeinfo
*)outpack
;
1202 memset(icp
, 0, sizeof(*icp
));
1203 icp
->icmp6_cksum
= 0;
1204 seq
= ntransmitted
++;
1205 CLR(seq
% mx_dup_ck
);
1207 if (options
& F_FQDN
) {
1208 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1209 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1210 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1211 nip
->ni_flags
= htons(0);
1213 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1214 sizeof(nip
->icmp6_ni_nonce
));
1215 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1217 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1218 sizeof(dst
.sin6_addr
));
1219 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1221 } else if (options
& F_FQDNOLD
) {
1222 /* packet format in 03 draft - no Subject data on queries */
1223 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1224 icp
->icmp6_code
= 0; /* code field is always 0 */
1225 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1226 nip
->ni_flags
= htons(0);
1228 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1229 sizeof(nip
->icmp6_ni_nonce
));
1230 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1234 } else if (options
& F_NODEADDR
) {
1235 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1236 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1237 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1238 nip
->ni_flags
= naflags
;
1240 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1241 sizeof(nip
->icmp6_ni_nonce
));
1242 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1244 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1245 sizeof(dst
.sin6_addr
));
1246 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1248 } else if (options
& F_SUPTYPES
) {
1249 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1250 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1251 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1252 /* we support compressed bitmap */
1253 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1255 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1256 sizeof(nip
->icmp6_ni_nonce
));
1257 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1261 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1262 icp
->icmp6_code
= 0;
1263 icp
->icmp6_id
= htons(ident
);
1264 icp
->icmp6_seq
= ntohs(seq
);
1268 (void)gettimeofday(&tv
, NULL
);
1269 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1270 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1271 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1273 cc
= ICMP6ECHOLEN
+ datalen
;
1277 if (pingerlen() != cc
)
1278 errx(1, "internal error; length mismatch");
1281 smsghdr
.msg_name
= (caddr_t
)&dst
;
1282 smsghdr
.msg_namelen
= sizeof(dst
);
1283 memset(&iov
, 0, sizeof(iov
));
1284 iov
[0].iov_base
= (caddr_t
)outpack
;
1285 iov
[0].iov_len
= cc
;
1286 smsghdr
.msg_iov
= iov
;
1287 smsghdr
.msg_iovlen
= 1;
1289 i
= sendmsg(s
, &smsghdr
, 0);
1291 if (i
< 0 || i
!= cc
) {
1294 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1297 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1298 (void)write(STDOUT_FILENO
, &DOT
, 1);
1304 myechoreply(const struct icmp6_hdr
*icp
)
1306 if (ntohs(icp
->icmp6_id
) == ident
)
1313 mynireply(const struct icmp6_nodeinfo
*nip
)
1315 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1316 nonce
+ sizeof(u_int16_t
),
1317 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1324 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1329 char cresult
[MAXDNAME
+ 1];
1333 i
= 0; /* XXXGCC -Wuninitialized [sun2] */
1342 if (i
== 0 || cp
!= *sp
) {
1343 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1344 return NULL
; /*result overrun*/
1350 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1351 /* DNS compression */
1355 comp
= base
+ (i
& 0x3f);
1356 if (dnsdecode(&comp
, cp
, base
, cresult
,
1357 sizeof(cresult
)) == NULL
)
1359 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1360 return NULL
; /*result overrun*/
1362 } else if ((i
& 0x3f) == i
) {
1364 return NULL
; /*source overrun*/
1365 while (i
-- > 0 && cp
< ep
) {
1366 l
= snprintf(cresult
, sizeof(cresult
),
1367 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1368 if (l
>= (int)sizeof(cresult
) || l
< 0)
1370 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1371 return NULL
; /*result overrun*/
1375 return NULL
; /*invalid label*/
1378 return NULL
; /*not terminated*/
1386 * Print out the packet, if it came from us. This logic is necessary
1387 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1388 * which arrive ('tis only fair). This permits multiple copies of this
1389 * program to be run without having intermingled output (or statistics!).
1392 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1394 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1395 struct icmp6_hdr
*icp
;
1396 struct icmp6_nodeinfo
*ni
;
1399 struct sockaddr
*from
;
1401 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1402 struct in6_pktinfo
*pktinfo
= NULL
;
1403 struct timeval tv
, tp
;
1405 double triptime
= 0;
1410 char dnsname
[MAXDNAME
+ 1];
1412 (void)gettimeofday(&tv
, NULL
);
1414 if (!mhdr
|| !mhdr
->msg_name
||
1415 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1416 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1417 if (options
& F_VERBOSE
)
1418 warnx("invalid peername");
1421 from
= (struct sockaddr
*)mhdr
->msg_name
;
1422 fromlen
= mhdr
->msg_namelen
;
1423 if (cc
< (int)sizeof(struct icmp6_hdr
)) {
1424 if (options
& F_VERBOSE
)
1425 warnx("packet too short (%d bytes) from %s", cc
,
1426 pr_addr(from
, fromlen
));
1429 icp
= (struct icmp6_hdr
*)buf
;
1430 ni
= (struct icmp6_nodeinfo
*)buf
;
1433 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1434 warnx("failed to get receiving hop limit");
1437 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1438 warnx("failed to get receiving packet information");
1442 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1443 seq
= ntohs(icp
->icmp6_seq
);
1446 tpp
= (struct tv32
*)(icp
+ 1);
1447 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1448 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1450 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1451 ((double)tv
.tv_usec
) / 1000.0;
1453 tsumsq
+= triptime
* triptime
;
1454 if (triptime
< tmin
)
1456 if (triptime
> tmax
)
1460 if (TST(seq
% mx_dup_ck
)) {
1465 SET(seq
% mx_dup_ck
);
1469 if (options
& F_QUIET
)
1472 if (options
& F_FLOOD
)
1473 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1475 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1476 pr_addr(from
, fromlen
), seq
);
1477 (void)printf(" hlim=%d", hoplim
);
1478 if ((options
& F_VERBOSE
) != 0) {
1479 struct sockaddr_in6 dstsa
;
1481 memset(&dstsa
, 0, sizeof(dstsa
));
1482 dstsa
.sin6_family
= AF_INET6
;
1484 dstsa
.sin6_len
= sizeof(dstsa
);
1486 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1487 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1488 (void)printf(" dst=%s",
1489 pr_addr((struct sockaddr
*)&dstsa
,
1493 (void)printf(" time=%.3f ms", triptime
);
1495 (void)printf("(DUP!)");
1496 /* check the data */
1497 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1498 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1499 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1501 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1506 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1507 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1509 if (TST(seq
% mx_dup_ck
)) {
1514 SET(seq
% mx_dup_ck
);
1518 if (options
& F_QUIET
)
1521 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1523 switch (ntohs(ni
->ni_code
)) {
1524 case ICMP6_NI_SUCCESS
:
1526 case ICMP6_NI_REFUSED
:
1527 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1529 case ICMP6_NI_UNKNOWN
:
1530 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1533 printf("unknown code 0x%x, type 0x%x",
1534 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1538 switch (ntohs(ni
->ni_qtype
)) {
1540 printf("NodeInfo NOOP");
1542 case NI_QTYPE_SUPTYPES
:
1543 pr_suptypes(ni
, end
- (u_char
*)ni
);
1545 case NI_QTYPE_NODEADDR
:
1546 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1549 default: /* XXX: for backward compatibility */
1550 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1551 if (buf
[off
+ ICMP6_NIRLEN
] ==
1552 cc
- off
- ICMP6_NIRLEN
- 1)
1557 cp
++; /* skip length */
1559 safeputc(*cp
& 0xff);
1565 if (dnsdecode((const u_char
**)&cp
, end
,
1566 (const u_char
*)(ni
+ 1), dnsname
,
1567 sizeof(dnsname
)) == NULL
) {
1572 * name-lookup special handling for
1575 if (cp
+ 1 <= end
&& !*cp
&&
1576 strlen(dnsname
) > 0) {
1577 dnsname
[strlen(dnsname
) - 1] = '\0';
1580 printf("%s%s", i
> 0 ? "," : "",
1584 if (options
& F_VERBOSE
) {
1588 (void)printf(" ("); /*)*/
1590 switch (ni
->ni_code
) {
1591 case ICMP6_NI_REFUSED
:
1592 (void)printf("refused");
1595 case ICMP6_NI_UNKNOWN
:
1596 (void)printf("unknown qtype");
1601 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1602 /* case of refusion, unknown */
1607 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1610 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1611 (void)printf("TTL=%d:meaningless",
1615 (void)printf("TTL=%d:invalid",
1618 (void)printf("TTL=%d", ttl
);
1628 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1637 if (buf
[off
+ ICMP6_NIRLEN
] !=
1638 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1641 (void)printf("invalid namelen:%d/%lu",
1642 buf
[off
+ ICMP6_NIRLEN
],
1643 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1653 /* We've got something other than an ECHOREPLY */
1654 if (!(options
& F_VERBOSE
))
1656 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1660 if (!(options
& F_FLOOD
)) {
1661 (void)putchar('\n');
1662 if (options
& F_VERBOSE
)
1664 (void)fflush(stdout
);
1670 pr_exthdrs(struct msghdr
*mhdr
)
1674 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1675 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1676 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1679 switch (cm
->cmsg_type
) {
1681 printf(" HbH Options: ");
1682 pr_ip6opt(CMSG_DATA(cm
));
1685 #ifdef IPV6_RTHDRDSTOPTS
1686 case IPV6_RTHDRDSTOPTS
:
1688 printf(" Dst Options: ");
1689 pr_ip6opt(CMSG_DATA(cm
));
1692 printf(" Routing: ");
1693 pr_rthdr(CMSG_DATA(cm
));
1700 pr_ip6opt(void *extbuf
)
1702 struct ip6_hbh
*ext
;
1712 ext
= (struct ip6_hbh
*)extbuf
;
1713 extlen
= (ext
->ip6h_len
+ 1) * 8;
1714 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1715 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1719 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1720 &type
, &len
, &databuf
);
1721 if (currentlen
== -1)
1725 * Note that inet6_opt_next automatically skips any padding
1730 offset
= inet6_opt_get_val(databuf
, offset
,
1731 &value4
, sizeof(value4
));
1732 printf(" Jumbo Payload Opt: Length %u\n",
1733 (u_int32_t
)ntohl(value4
));
1735 case IP6OPT_ROUTER_ALERT
:
1737 offset
= inet6_opt_get_val(databuf
, offset
,
1738 &value2
, sizeof(value2
));
1739 printf(" Router Alert Opt: Type %u\n",
1743 printf(" Received Opt %u len %lu\n",
1744 type
, (unsigned long)len
);
1752 pr_rthdr(void *extbuf
)
1754 struct in6_addr
*in6
;
1755 char ntopbuf
[INET6_ADDRSTRLEN
];
1756 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1759 /* print fixed part of the header */
1760 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1761 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1762 if ((segments
= inet6_rth_segments(extbuf
)) >= 0)
1763 printf("%d segments, ", segments
);
1765 printf("segments unknown, ");
1766 printf("%d left\n", rh
->ip6r_segleft
);
1768 for (i
= 0; i
< segments
; i
++) {
1769 in6
= inet6_rth_getaddr(extbuf
, i
);
1771 printf(" [%d]<NULL>\n", i
);
1773 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1775 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
1776 printf(" [%d]%s\n", i
, ntopbuf
);
1785 pr_bitrange(u_int32_t v
, int soff
, int ii
)
1792 /* shift till we have 0x01 */
1793 if ((v
& 0x01) == 0) {
1795 printf("-%u", soff
+ off
- 1);
1806 case 0x04: case 0x0c:
1817 /* we have 0x01 with us */
1818 for (i
= 0; i
< 32 - off
; i
++) {
1819 if ((v
& (0x01 << i
)) == 0)
1823 printf(" %u", soff
+ off
);
1831 pr_suptypes(struct icmp6_nodeinfo
*ni
/* ni->qtype must be SUPTYPES */,
1836 const u_char
*cp
, *end
;
1839 u_int16_t words
; /*32bit count*/
1842 #define MAXQTYPES (1 << 16)
1846 cp
= (u_char
*)(ni
+ 1);
1847 end
= ((u_char
*)ni
) + nilen
;
1851 printf("NodeInfo Supported Qtypes");
1852 if (options
& F_VERBOSE
) {
1853 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1854 printf(", compressed bitmap");
1856 printf(", raw bitmap");
1860 clen
= (size_t)(end
- cp
);
1861 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1862 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1868 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1870 memcpy(&cbit
, cp
, sizeof(cbit
));
1871 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1875 clen
= ntohs(cbit
.words
) * sizeof(v
);
1876 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1881 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1882 memcpy(&v
, cp
+ off
, sizeof(v
));
1883 v
= (u_int32_t
)ntohl(v
);
1884 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1886 /* flush the remaining bits */
1887 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1891 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1892 cur
+= ntohs(cbit
.skip
) * 32;
1897 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, /* ni->qtype must be NODEADDR */
1900 u_char
*cp
= (u_char
*)(ni
+ 1);
1901 char ntop_buf
[INET6_ADDRSTRLEN
];
1904 nilen
-= sizeof(struct icmp6_nodeinfo
);
1906 if (options
& F_VERBOSE
) {
1907 switch (ni
->ni_code
) {
1908 case ICMP6_NI_REFUSED
:
1909 (void)printf("refused");
1911 case ICMP6_NI_UNKNOWN
:
1912 (void)printf("unknown qtype");
1915 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1916 (void)printf(" truncated");
1920 printf(" no address\n");
1923 * In icmp-name-lookups 05 and later, TTL of each returned address
1924 * is contained in the resposne. We try to detect the version
1925 * by the length of the data, but note that the detection algorithm
1926 * is incomplete. We assume the latest draft by default.
1928 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1934 /* XXX: alignment? */
1935 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1936 cp
+= sizeof(u_int32_t
);
1937 nilen
-= sizeof(u_int32_t
);
1940 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1942 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
1943 printf(" %s", ntop_buf
);
1945 if (ttl
== 0xffffffff) {
1947 * XXX: can this convention be applied to all
1948 * type of TTL (i.e. non-ND TTL)?
1950 printf("(TTL=infty)");
1953 printf("(TTL=%u)", ttl
);
1957 nilen
-= sizeof(struct in6_addr
);
1958 cp
+= sizeof(struct in6_addr
);
1963 get_hoplim(struct msghdr
*mhdr
)
1967 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1968 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1969 if (cm
->cmsg_len
== 0)
1972 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1973 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1974 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1975 return(*(int *)CMSG_DATA(cm
));
1981 struct in6_pktinfo
*
1982 get_rcvpktinfo(struct msghdr
*mhdr
)
1986 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1987 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1988 if (cm
->cmsg_len
== 0)
1991 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1992 cm
->cmsg_type
== IPV6_PKTINFO
&&
1993 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
1994 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2001 get_pathmtu(struct msghdr
*mhdr
)
2003 #ifdef IPV6_RECVPATHMTU
2005 struct ip6_mtuinfo
*mtuctl
= NULL
;
2007 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2008 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2009 if (cm
->cmsg_len
== 0)
2012 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2013 cm
->cmsg_type
== IPV6_PATHMTU
&&
2014 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2015 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2018 * If the notified destination is different from
2019 * the one we are pinging, just ignore the info.
2020 * We check the scope ID only when both notified value
2021 * and our own value have non-0 values, because we may
2022 * have used the default scope zone ID for sending,
2023 * in which case the scope ID value is 0.
2025 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2027 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2028 dst
.sin6_scope_id
&&
2029 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2030 dst
.sin6_scope_id
)) {
2031 if ((options
& F_VERBOSE
) != 0) {
2032 printf("path MTU for %s is notified. "
2034 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2035 sizeof(mtuctl
->ip6m_addr
)));
2041 * Ignore an invalid MTU. XXX: can we just believe
2044 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2047 /* notification for our destination. return the MTU. */
2048 return((int)mtuctl
->ip6m_mtu
);
2057 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2061 tvsub(struct timeval
*out
, struct timeval
*in
)
2063 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2065 out
->tv_usec
+= 1000000;
2067 out
->tv_sec
-= in
->tv_sec
;
2080 (void)signal(SIGINT
, SIG_DFL
);
2081 (void)kill(getpid(), SIGINT
);
2089 * Print out statistics.
2095 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2096 (void)printf("%ld packets transmitted, ", ntransmitted
);
2097 (void)printf("%ld packets received, ", nreceived
);
2099 (void)printf("+%ld duplicates, ", nrepeats
);
2101 if (nreceived
> ntransmitted
)
2102 (void)printf("-- somebody's duplicating packets!");
2104 (void)printf("%.1f%% packet loss",
2105 ((((double)ntransmitted
- nreceived
) * 100.0) /
2108 (void)putchar('\n');
2109 if (nreceived
&& timing
) {
2110 /* Only display average to microseconds */
2111 double num
= nreceived
+ nrepeats
;
2112 double avg
= tsum
/ num
;
2113 double dev
= sqrt((tsumsq
- num
* avg
* avg
) / (num
- 1));
2115 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2116 tmin
, avg
, tmax
, dev
);
2117 (void)fflush(stdout
);
2119 (void)fflush(stdout
);
2123 static const char *niqcode
[] = {
2125 "DNS label", /*or empty*/
2130 static const char *nircode
[] = {
2131 "Success", "Refused", "Unknown",
2137 * Print a descriptive string about an ICMP header.
2140 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2142 char ntop_buf
[INET6_ADDRSTRLEN
];
2143 struct nd_redirect
*red
;
2144 struct icmp6_nodeinfo
*ni
;
2145 char dnsname
[MAXDNAME
+ 1];
2149 switch (icp
->icmp6_type
) {
2150 case ICMP6_DST_UNREACH
:
2151 switch (icp
->icmp6_code
) {
2152 case ICMP6_DST_UNREACH_NOROUTE
:
2153 (void)printf("No Route to Destination\n");
2155 case ICMP6_DST_UNREACH_ADMIN
:
2156 (void)printf("Destination Administratively "
2159 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2160 (void)printf("Destination Unreachable Beyond Scope\n");
2162 case ICMP6_DST_UNREACH_ADDR
:
2163 (void)printf("Destination Host Unreachable\n");
2165 case ICMP6_DST_UNREACH_NOPORT
:
2166 (void)printf("Destination Port Unreachable\n");
2169 (void)printf("Destination Unreachable, Bad Code: %d\n",
2173 /* Print returned IP header information */
2174 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2176 case ICMP6_PACKET_TOO_BIG
:
2177 (void)printf("Packet too big mtu = %d\n",
2178 (int)ntohl(icp
->icmp6_mtu
));
2179 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2181 case ICMP6_TIME_EXCEEDED
:
2182 switch (icp
->icmp6_code
) {
2183 case ICMP6_TIME_EXCEED_TRANSIT
:
2184 (void)printf("Time to live exceeded\n");
2186 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2187 (void)printf("Frag reassembly time exceeded\n");
2190 (void)printf("Time exceeded, Bad Code: %d\n",
2194 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2196 case ICMP6_PARAM_PROB
:
2197 (void)printf("Parameter problem: ");
2198 switch (icp
->icmp6_code
) {
2199 case ICMP6_PARAMPROB_HEADER
:
2200 (void)printf("Erroneous Header ");
2202 case ICMP6_PARAMPROB_NEXTHEADER
:
2203 (void)printf("Unknown Nextheader ");
2205 case ICMP6_PARAMPROB_OPTION
:
2206 (void)printf("Unrecognized Option ");
2209 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2212 (void)printf("pointer = 0x%02x\n",
2213 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2214 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2216 case ICMP6_ECHO_REQUEST
:
2217 (void)printf("Echo Request");
2218 /* XXX ID + Seq + Data */
2220 case ICMP6_ECHO_REPLY
:
2221 (void)printf("Echo Reply");
2222 /* XXX ID + Seq + Data */
2224 case ICMP6_MEMBERSHIP_QUERY
:
2225 (void)printf("Listener Query");
2227 case ICMP6_MEMBERSHIP_REPORT
:
2228 (void)printf("Listener Report");
2230 case ICMP6_MEMBERSHIP_REDUCTION
:
2231 (void)printf("Listener Done");
2233 case ND_ROUTER_SOLICIT
:
2234 (void)printf("Router Solicitation");
2236 case ND_ROUTER_ADVERT
:
2237 (void)printf("Router Advertisement");
2239 case ND_NEIGHBOR_SOLICIT
:
2240 (void)printf("Neighbor Solicitation");
2242 case ND_NEIGHBOR_ADVERT
:
2243 (void)printf("Neighbor Advertisement");
2246 red
= (struct nd_redirect
*)icp
;
2247 (void)printf("Redirect\n");
2248 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2250 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2251 (void)printf("Destination: %s", ntop_buf
);
2252 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2254 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2255 (void)printf(" New Target: %s", ntop_buf
);
2257 case ICMP6_NI_QUERY
:
2258 (void)printf("Node Information Query");
2259 /* XXX ID + Seq + Data */
2260 ni
= (struct icmp6_nodeinfo
*)icp
;
2261 l
= end
- (u_char
*)(ni
+ 1);
2263 switch (ntohs(ni
->ni_qtype
)) {
2265 (void)printf("NOOP");
2267 case NI_QTYPE_SUPTYPES
:
2268 (void)printf("Supported qtypes");
2271 (void)printf("DNS name");
2273 case NI_QTYPE_NODEADDR
:
2274 (void)printf("nodeaddr");
2276 case NI_QTYPE_IPV4ADDR
:
2277 (void)printf("IPv4 nodeaddr");
2280 (void)printf("unknown qtype");
2283 if (options
& F_VERBOSE
) {
2284 switch (ni
->ni_code
) {
2285 case ICMP6_NI_SUBJ_IPV6
:
2286 if (l
== sizeof(struct in6_addr
) &&
2287 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2288 sizeof(ntop_buf
)) != NULL
) {
2289 (void)printf(", subject=%s(%s)",
2290 niqcode
[ni
->ni_code
], ntop_buf
);
2293 /* backward compat to -W */
2294 (void)printf(", oldfqdn");
2296 (void)printf(", invalid");
2300 case ICMP6_NI_SUBJ_FQDN
:
2301 if (end
== (u_char
*)(ni
+ 1)) {
2302 (void)printf(", no subject");
2305 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2306 cp
= (const u_char
*)(ni
+ 1);
2307 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2308 sizeof(dnsname
)) != NULL
)
2309 printf("(%s)", dnsname
);
2311 printf("(invalid)");
2313 case ICMP6_NI_SUBJ_IPV4
:
2314 if (l
== sizeof(struct in_addr
) &&
2315 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2316 sizeof(ntop_buf
)) != NULL
) {
2317 (void)printf(", subject=%s(%s)",
2318 niqcode
[ni
->ni_code
], ntop_buf
);
2320 (void)printf(", invalid");
2323 (void)printf(", invalid");
2328 case ICMP6_NI_REPLY
:
2329 (void)printf("Node Information Reply");
2330 /* XXX ID + Seq + Data */
2331 ni
= (struct icmp6_nodeinfo
*)icp
;
2333 switch (ntohs(ni
->ni_qtype
)) {
2335 (void)printf("NOOP");
2337 case NI_QTYPE_SUPTYPES
:
2338 (void)printf("Supported qtypes");
2341 (void)printf("DNS name");
2343 case NI_QTYPE_NODEADDR
:
2344 (void)printf("nodeaddr");
2346 case NI_QTYPE_IPV4ADDR
:
2347 (void)printf("IPv4 nodeaddr");
2350 (void)printf("unknown qtype");
2353 if (options
& F_VERBOSE
) {
2354 if (ni
->ni_code
>= sizeof(nircode
) / sizeof(nircode
[0]))
2355 printf(", invalid");
2357 printf(", %s", nircode
[ni
->ni_code
]);
2361 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2367 * Print an IP6 header.
2370 pr_iph(struct ip6_hdr
*ip6
)
2372 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2374 char ntop_buf
[INET6_ADDRSTRLEN
];
2376 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2377 tc
= (tc
>> 4) & 0x0f;
2378 tc
|= (ip6
->ip6_vfc
<< 4);
2380 printf("Vr TC Flow Plen Nxt Hlim\n");
2381 printf(" %1x %02x %05x %04x %02x %02x\n",
2382 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2383 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2384 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2385 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2386 printf("%s->", ntop_buf
);
2387 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2388 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2389 printf("%s\n", ntop_buf
);
2394 * Return an ascii host address as a dotted quad and optionally with
2398 pr_addr(struct sockaddr
*addr
, int addrlen
)
2400 static char buf
[NI_MAXHOST
];
2403 if ((options
& F_HOSTNAME
) == 0)
2404 flag
|= NI_NUMERICHOST
;
2406 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2414 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2417 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2419 u_char
*cp
= (u_char
*)ip6
, nh
;
2422 if (end
- (u_char
*)ip6
< (intptr_t)sizeof(*ip6
)) {
2427 hlen
= sizeof(*ip6
);
2431 while (end
- cp
>= 8) {
2433 case IPPROTO_HOPOPTS
:
2435 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2436 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2438 case IPPROTO_DSTOPTS
:
2440 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2441 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2443 case IPPROTO_FRAGMENT
:
2445 hlen
= sizeof(struct ip6_frag
);
2446 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2448 case IPPROTO_ROUTING
:
2450 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2451 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2456 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2457 nh
= ((struct ah
*)cp
)->ah_nxt
;
2460 case IPPROTO_ICMPV6
:
2461 printf("ICMP6: type = %d, code = %d\n",
2468 printf("TCP: from port %u, to port %u (decimal)\n",
2469 (*cp
* 256 + *(cp
+ 1)),
2470 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2473 printf("UDP: from port %u, to port %u (decimal)\n",
2474 (*cp
* 256 + *(cp
+ 1)),
2475 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2478 printf("Unknown Header(%d)\n", nh
);
2482 if ((cp
+= hlen
) >= end
)
2497 fill(char *bp
, char *patp
)
2503 for (cp
= patp
; *cp
; cp
++)
2504 if (!isxdigit((unsigned char)*cp
))
2505 errx(1, "patterns must be specified as hex digits");
2507 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2508 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2509 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2510 &pat
[13], &pat
[14], &pat
[15]);
2515 kk
<= (int)(MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
));
2517 for (jj
= 0; jj
< ii
; ++jj
)
2518 bp
[jj
+ kk
] = pat
[jj
];
2519 if (!(options
& F_QUIET
)) {
2520 (void)printf("PATTERN: 0x");
2521 for (jj
= 0; jj
< ii
; ++jj
)
2522 (void)printf("%02x", bp
[jj
] & 0xFF);
2528 #ifdef IPSEC_POLICY_IPSEC
2530 setpolicy(int so
, char *policy
)
2535 return 0; /* ignore */
2537 buf
= ipsec_set_policy(policy
, strlen(policy
));
2539 errx(1, "%s", ipsec_strerror());
2540 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2541 ipsec_get_policylen(buf
)) < 0)
2542 warnx("Unable to set IPsec policy");
2556 u_int8_t digest
[16];
2559 char hbuf
[NI_MAXHOST
];
2560 struct in6_addr in6
;
2562 p
= strchr(name
, '.');
2564 p
= name
+ strlen(name
);
2566 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2567 return NULL
; /*label too long*/
2568 strncpy(hbuf
, name
, l
);
2569 hbuf
[(int)l
] = '\0';
2571 for (q
= name
; *q
; q
++) {
2572 if (isupper(*(unsigned char *)q
))
2573 *q
= tolower(*(unsigned char *)q
);
2576 /* generate 8 bytes of pseudo-random value. */
2577 memset(&ctxt
, 0, sizeof(ctxt
));
2580 MD5Update(&ctxt
, &c
, sizeof(c
));
2581 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2582 MD5Final(digest
, &ctxt
);
2584 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2585 return NULL
; /*XXX*/
2586 bcopy(digest
, &in6
.s6_addr
[12], 4);
2588 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2591 return strdup(hbuf
);
2597 (void)fprintf(stderr
,
2598 "usage: ping6 [-dfH"
2599 #ifdef IPV6_USE_MIN_MTU
2603 #ifdef IPV6_REACHCONF
2607 #ifdef IPSEC_POLICY_IPSEC
2613 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
2614 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2616 "\t[-s packetsize] [-h hoplimit] [-g gateway] [hops...] host\n");