1 /* $KAME: ping6.c,v 1.126 2001/05/17 03:39:08 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static char copyright
[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid
[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid
[] =
81 "$FreeBSD: src/sbin/ping6/ping6.c,v 1.4.2.6 2001/07/06 08:56:47 ume 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>
126 #if defined(__OpenBSD__) || defined(__NetBSD__)
136 #include <netinet6/ah.h>
137 #include <netinet6/ipsec.h>
140 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
146 #define MAXPACKETLEN 131072
148 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
149 #define ICMP6ECHOTMLEN sizeof(struct timeval)
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 */
212 int datalen
= DEFDATALEN
;
213 int s
; /* socket file descriptor */
214 u_char outpack
[MAXPACKETLEN
];
215 char BSPACE
= '\b'; /* characters written for flood */
218 int ident
; /* process id to identify our packets */
219 u_int8_t nonce
[8]; /* nonce field for node information */
220 struct in6_addr srcaddr
;
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 #if defined(__OpenBSD__) || defined(__NetBSD__)
237 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
240 /* for node addresses */
243 /* for ancillary data(advanced API) */
244 struct msghdr smsghdr
;
245 struct iovec smsgiov
;
249 volatile sig_atomic_t seenalrm
;
250 volatile sig_atomic_t seenint
;
252 volatile sig_atomic_t seeninfo
;
255 int main
__P((int, char *[]));
256 void fill
__P((char *, char *));
257 int get_hoplim
__P((struct msghdr
*));
258 int get_pathmtu
__P((struct msghdr
*));
259 void set_pathmtu
__P((int));
260 struct in6_pktinfo
*get_rcvpktinfo
__P((struct msghdr
*));
261 void onsignal
__P((int));
262 void retransmit
__P((void));
263 void onint
__P((int));
264 size_t pingerlen
__P((void));
265 int pinger
__P((void));
266 const char *pr_addr
__P((struct sockaddr
*, int));
267 void pr_icmph
__P((struct icmp6_hdr
*, u_char
*));
268 void pr_iph
__P((struct ip6_hdr
*));
269 void pr_suptypes
__P((struct icmp6_nodeinfo
*, size_t));
270 void pr_nodeaddr
__P((struct icmp6_nodeinfo
*, int));
271 int myechoreply
__P((const struct icmp6_hdr
*));
272 int mynireply
__P((const struct icmp6_nodeinfo
*));
273 char *dnsdecode
__P((const u_char
**, const u_char
*, const u_char
*,
275 void pr_pack
__P((u_char
*, int, struct msghdr
*));
276 void pr_exthdrs
__P((struct msghdr
*));
277 void pr_ip6opt
__P((void *));
278 void pr_rthdr
__P((void *));
279 int pr_bitrange
__P((u_int32_t
, int, int));
280 void pr_retip
__P((struct ip6_hdr
*, u_char
*));
281 void summary
__P((void));
282 void tvsub
__P((struct timeval
*, struct timeval
*));
283 int setpolicy
__P((int, char *));
284 char *nigroup
__P((char *));
285 void usage
__P((void));
292 struct itimerval itimer
;
293 struct sockaddr_in6 from
;
294 struct timeval timeout
, *tv
;
295 struct addrinfo hints
;
299 int ch
, fromlen
, hold
, packlen
, preload
, optval
, ret_ga
;
300 u_char
*datap
, *packet
;
301 char *e
, *target
, *ifname
= NULL
;
303 struct cmsghdr
*scmsgp
= NULL
;
306 struct in6_pktinfo
*pktinfo
= NULL
;
307 #ifdef USE_RFC2292BIS
308 struct ip6_rthdr
*rthdr
= NULL
;
310 #ifdef IPSEC_POLICY_IPSEC
311 char *policy_in
= NULL
;
312 char *policy_out
= NULL
;
317 /* just to be sure */
318 memset(&smsghdr
, 0, sizeof(&smsghdr
));
319 memset(&smsgiov
, 0, sizeof(&smsgiov
));
322 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
326 #ifdef IPSEC_POLICY_IPSEC
330 #endif /*IPSEC_POLICY_IPSEC*/
332 while ((ch
= getopt(argc
, argv
,
333 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS
)) != -1) {
340 options
&= ~F_NOUSERDATA
;
341 options
|= F_NODEADDR
;
342 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
345 naflags
|= NI_NODEADDR_FLAG_ALL
;
349 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
353 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
357 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
361 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
363 case 'A': /* experimental. not in the spec */
364 #ifdef NI_NODEADDR_FLAG_ANYCAST
365 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
369 "-a A is not supported on the platform");
380 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
381 sockbufsize
= atoi(optarg
);
384 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
388 npackets
= strtol(optarg
, &e
, 10);
389 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
391 "illegal number of packets -- %s", optarg
);
394 options
|= F_SO_DEBUG
;
399 errx(1, "Must be superuser to flood ping");
402 setbuf(stdout
, (char *)NULL
);
405 options
|= F_HOSTNAME
;
407 case 'h': /* hoplimit */
408 hoplimit
= strtol(optarg
, &e
, 10);
409 if (255 < hoplimit
|| hoplimit
< -1)
411 "illegal hoplimit -- %s", optarg
);
415 options
|= F_INTERFACE
;
416 #ifndef USE_SIN6_SCOPE_ID
420 case 'i': /* wait between sending packets */
421 intval
= strtod(optarg
, &e
);
422 if (*optarg
== '\0' || *e
!= '\0')
423 errx(1, "illegal timing interval %s", optarg
);
424 if (intval
< 1 && getuid()) {
425 errx(1, "%s: only root may use interval < 1s",
428 interval
.tv_sec
= (long)intval
;
430 (long)((intval
- interval
.tv_sec
) * 1000000);
431 if (interval
.tv_sec
< 0)
432 errx(1, "illegal timing interval %s", optarg
);
433 /* less than 1/hz does not make sense */
434 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
435 warnx("too small interval, raised to 0.01");
436 interval
.tv_usec
= 10000;
438 options
|= F_INTERVAL
;
443 errx(1, "Must be superuser to preload");
445 preload
= strtol(optarg
, &e
, 10);
446 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
447 errx(1, "illegal preload value -- %s", optarg
);
450 #ifdef IPV6_USE_MIN_MTU
451 options
|= F_NOMINMTU
;
454 errx(1, "-%c is not supported on this platform", ch
);
458 options
&= ~F_HOSTNAME
;
461 options
|= F_NIGROUP
;
463 case 'p': /* fill buffer with user pattern */
464 options
|= F_PINGFILLED
;
465 fill((char *)datap
, optarg
);
471 #ifdef IPV6_REACHCONF
472 options
|= F_REACHCONF
;
475 errx(1, "-R is not supported in this configuration");
478 /* XXX: use getaddrinfo? */
479 if (inet_pton(AF_INET6
, optarg
, (void *)&srcaddr
) != 1)
480 errx(1, "invalid IPv6 address: %s", optarg
);
481 options
|= F_SRCADDR
;
484 case 's': /* size of packet to send */
485 datalen
= strtol(optarg
, &e
, 10);
486 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
487 errx(1, "illegal datalen value -- %s", optarg
);
488 if (datalen
> MAXDATALEN
) {
490 "datalen value too large, maximum is %d",
495 options
&= ~F_NOUSERDATA
;
496 options
|= F_SUPTYPES
;
499 options
|= F_VERBOSE
;
502 options
&= ~F_NOUSERDATA
;
506 options
&= ~F_NOUSERDATA
;
507 options
|= F_FQDNOLD
;
510 #ifdef IPSEC_POLICY_IPSEC
513 if (!strncmp("in", optarg
, 2)) {
514 if ((policy_in
= strdup(optarg
)) == NULL
)
516 } else if (!strncmp("out", optarg
, 3)) {
517 if ((policy_out
= strdup(optarg
)) == NULL
)
520 errx(1, "invalid security policy");
524 options
|= F_AUTHHDR
;
527 options
|= F_ENCRYPT
;
529 #endif /*IPSEC_POLICY_IPSEC*/
545 #ifdef IPV6_RECVRTHDR /* 2292bis */
546 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
549 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
552 errx(1, "too many intermediate hops");
558 if (options
& F_NIGROUP
) {
559 target
= nigroup(argv
[argc
- 1]);
560 if (target
== NULL
) {
565 target
= argv
[argc
- 1];
568 bzero(&hints
, sizeof(struct addrinfo
));
569 hints
.ai_flags
= AI_CANONNAME
;
570 hints
.ai_family
= AF_INET6
;
571 hints
.ai_socktype
= SOCK_RAW
;
572 hints
.ai_protocol
= IPPROTO_ICMPV6
;
574 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
576 fprintf(stderr
, "ping6: %s\n", gai_strerror(ret_ga
));
579 if (res
->ai_canonname
)
580 hostname
= res
->ai_canonname
;
585 errx(1, "getaddrinfo failed");
587 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
589 res
->ai_socktype
= SOCK_RAW
;
590 res
->ai_protocol
= IPPROTO_ICMPV6
;
592 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
593 res
->ai_protocol
)) < 0)
597 * let the kerel pass extension headers of incoming packets,
598 * for privileged socket options
600 if ((options
& F_VERBOSE
) != 0) {
603 #ifdef IPV6_RECVHOPOPTS
604 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
606 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
607 #else /* old adv. API */
608 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
610 err(1, "setsockopt(IPV6_HOPOPTS)");
612 #ifdef IPV6_RECVDSTOPTS
613 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
615 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
616 #else /* old adv. API */
617 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
619 err(1, "setsockopt(IPV6_DSTOPTS)");
621 #ifdef IPV6_RECVRTHDRDSTOPTS
622 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
624 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
628 /* revoke root privilege */
632 if (options
& F_FLOOD
&& options
& F_INTERVAL
)
633 errx(1, "-f and -i incompatible options");
635 if ((options
& F_NOUSERDATA
) == 0) {
636 if (datalen
>= sizeof(struct timeval
)) {
637 /* we can time transfer */
641 /* in F_VERBOSE case, we may get non-echoreply packets*/
642 if (options
& F_VERBOSE
)
643 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
645 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
647 /* suppress timing for node information query */
650 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
653 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
654 err(1, "Unable to allocate packet");
655 if (!(options
& F_PINGFILLED
))
656 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
659 ident
= getpid() & 0xFFFF;
661 gettimeofday(&timeout
, NULL
);
662 srand((unsigned int)(timeout
.tv_sec
^ timeout
.tv_usec
^ (long)ident
));
663 memset(nonce
, 0, sizeof(nonce
));
664 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
665 *((int *)&nonce
[i
]) = rand();
667 memset(nonce
, 0, sizeof(nonce
));
668 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
669 *((u_int32_t
*)&nonce
[i
]) = arc4random();
674 if (options
& F_SO_DEBUG
)
675 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
677 optval
= IPV6_DEFHLIM
;
678 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
679 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
680 &optval
, sizeof(optval
)) == -1)
681 err(1, "IPV6_MULTICAST_HOPS");
682 #ifdef IPV6_USE_MIN_MTU
683 if ((options
& F_NOMINMTU
) == 0) {
685 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
686 &optval
, sizeof(optval
)) == -1)
687 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
689 #ifdef IPV6_RECVPATHMTU
692 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
693 &optval
, sizeof(optval
)) == -1)
694 err(1, "setsockopt(IPV6_RECVPATHMTU)");
696 #endif /* IPV6_RECVPATHMTU */
697 #endif /* IPV6_USE_MIN_MTU */
700 #ifdef IPSEC_POLICY_IPSEC
701 if (options
& F_POLICY
) {
702 if (setpolicy(s
, policy_in
) < 0)
703 errx(1, "%s", ipsec_strerror());
704 if (setpolicy(s
, policy_out
) < 0)
705 errx(1, "%s", ipsec_strerror());
708 if (options
& F_AUTHHDR
) {
709 optval
= IPSEC_LEVEL_REQUIRE
;
710 #ifdef IPV6_AUTH_TRANS_LEVEL
711 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
712 &optval
, sizeof(optval
)) == -1)
713 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
715 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
716 &optval
, sizeof(optval
)) == -1)
717 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
720 if (options
& F_ENCRYPT
) {
721 optval
= IPSEC_LEVEL_REQUIRE
;
722 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
723 &optval
, sizeof(optval
)) == -1)
724 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
726 #endif /*IPSEC_POLICY_IPSEC*/
731 struct icmp6_filter filt
;
732 if (!(options
& F_VERBOSE
)) {
733 ICMP6_FILTER_SETBLOCKALL(&filt
);
734 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
735 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
736 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
738 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
740 ICMP6_FILTER_SETPASSALL(&filt
);
742 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
744 err(1, "setsockopt(ICMP6_FILTER)");
746 #endif /*ICMP6_FILTER*/
748 /* let the kerel pass extension headers of incoming packets */
749 if ((options
& F_VERBOSE
) != 0) {
752 #ifdef IPV6_RECVRTHDR
753 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
755 err(1, "setsockopt(IPV6_RECVRTHDR)");
756 #else /* old adv. API */
757 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
759 err(1, "setsockopt(IPV6_RTHDR)");
765 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
766 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
767 &optval, sizeof(optval)) == -1)
768 err(1, "IPV6_MULTICAST_LOOP");
771 /* Specify the outgoing interface and/or the source address */
773 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
776 ip6optlen
+= CMSG_SPACE(sizeof(int));
778 #ifdef IPV6_REACHCONF
779 if (options
& F_REACHCONF
)
780 ip6optlen
+= CMSG_SPACE(0);
783 /* set IP6 packet options */
785 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
786 errx(1, "can't allocate enough memory");
787 smsghdr
.msg_control
= (caddr_t
)scmsg
;
788 smsghdr
.msg_controllen
= ip6optlen
;
789 scmsgp
= (struct cmsghdr
*)scmsg
;
792 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
793 memset(pktinfo
, 0, sizeof(*pktinfo
));
794 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
795 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
796 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
797 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
800 /* set the outgoing interface */
802 #ifndef USE_SIN6_SCOPE_ID
803 /* pktinfo must have already been allocated */
804 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
805 errx(1, "%s: invalid interface name", ifname
);
807 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
808 errx(1, "%s: invalid interface name", ifname
);
811 /* set the source address */
812 if (options
& F_SRCADDR
)/* pktinfo must be valid */
813 pktinfo
->ipi6_addr
= srcaddr
;
815 if (hoplimit
!= -1) {
816 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
817 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
818 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
819 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
821 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
823 #ifdef IPV6_REACHCONF
824 if (options
& F_REACHCONF
) {
825 scmsgp
->cmsg_len
= CMSG_LEN(0);
826 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
827 scmsgp
->cmsg_type
= IPV6_REACHCONF
;
829 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
833 if (argc
> 1) { /* some intermediate addrs are specified */
835 #ifdef USE_RFC2292BIS
839 #ifdef USE_RFC2292BIS
840 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
841 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
842 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
843 scmsgp
->cmsg_type
= IPV6_RTHDR
;
844 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
845 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
846 IPV6_RTHDR_TYPE_0
, argc
- 1);
848 errx(1, "can't initialize rthdr");
849 #else /* old advanced API */
850 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
851 IPV6_RTHDR_TYPE_0
)) == 0)
852 errx(1, "can't initialize rthdr");
853 #endif /* USE_RFC2292BIS */
855 for (hops
= 0; hops
< argc
- 1; hops
++) {
856 struct addrinfo
*iaip
;
858 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
860 errx(1, "%s", gai_strerror(error
));
861 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
863 "bad addr family of an intermediate addr");
865 #ifdef USE_RFC2292BIS
866 if (inet6_rth_add(rthdr
,
867 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
868 errx(1, "can't add an intermediate node");
869 #else /* old advanced API */
870 if (inet6_rthdr_add(scmsgp
,
871 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
873 errx(1, "can't add an intermediate node");
874 #endif /* USE_RFC2292BIS */
878 #ifndef USE_RFC2292BIS
879 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
880 errx(1, "can't set the last flag");
883 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
890 int dummy
, len
= sizeof(src
);
892 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
893 err(1, "UDP socket");
895 src
.sin6_family
= AF_INET6
;
896 src
.sin6_addr
= dst
.sin6_addr
;
897 src
.sin6_port
= ntohs(DUMMY_PORT
);
898 src
.sin6_scope_id
= dst
.sin6_scope_id
;
900 #ifdef USE_RFC2292BIS
902 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
903 (void *)pktinfo
, sizeof(*pktinfo
)))
904 err(1, "UDP setsockopt(IPV6_PKTINFO)");
906 if (hoplimit
!= -1 &&
907 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_HOPLIMIT
,
908 (void *)&hoplimit
, sizeof(hoplimit
)))
909 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
912 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
913 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
914 err(1, "UDP setsockopt(IPV6_RTHDR)");
915 #else /* old advanced API */
916 if (smsghdr
.msg_control
&&
917 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
918 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
919 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
922 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
923 err(1, "UDP connect");
925 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
926 err(1, "getsockname");
931 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
933 if (datalen
> sockbufsize
)
934 warnx("you need -b to increase socket buffer size");
935 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
936 sizeof(sockbufsize
)) < 0)
937 err(1, "setsockopt(SO_SNDBUF)");
938 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
939 sizeof(sockbufsize
)) < 0)
940 err(1, "setsockopt(SO_RCVBUF)");
943 if (datalen
> 8 * 1024) /*XXX*/
944 warnx("you need -b to increase socket buffer size");
946 * When pinging the broadcast address, you can get a lot of
947 * answers. Doing something so evil is useful if you are trying
948 * to stress the ethernet, or just want to fill the arp cache
949 * to get some stuff for /etc/ethers.
952 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
958 #ifndef USE_SIN6_SCOPE_ID
959 #ifdef IPV6_RECVPKTINFO
960 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
962 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
963 #else /* old adv. API */
964 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
966 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
968 #endif /* USE_SIN6_SCOPE_ID */
969 #ifdef IPV6_RECVHOPLIMIT
970 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
972 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
973 #else /* old adv. API */
974 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
976 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
979 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
980 (unsigned long)(pingerlen() - 8));
981 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
982 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
984 while (preload
--) /* Fire off them quickies. */
987 (void)signal(SIGINT
, onsignal
);
989 (void)signal(SIGINFO
, onsignal
);
992 if ((options
& F_FLOOD
) == 0) {
993 (void)signal(SIGALRM
, onsignal
);
994 itimer
.it_interval
= interval
;
995 itimer
.it_value
= interval
;
996 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1000 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1001 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1004 signo
= seenalrm
= seenint
= 0;
1013 struct iovec iov
[2];
1015 /* signal handling */
1034 if (options
& F_FLOOD
) {
1037 timeout
.tv_usec
= 10000;
1041 memset(fdmaskp
, 0, fdmasks
);
1043 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1045 if (errno
!= EINTR
) {
1053 fromlen
= sizeof(from
);
1054 m
.msg_name
= (caddr_t
)&from
;
1055 m
.msg_namelen
= sizeof(from
);
1056 memset(&iov
, 0, sizeof(iov
));
1057 iov
[0].iov_base
= (caddr_t
)packet
;
1058 iov
[0].iov_len
= packlen
;
1061 cm
= (struct cmsghdr
*)buf
;
1062 m
.msg_control
= (caddr_t
)buf
;
1063 m
.msg_controllen
= sizeof(buf
);
1065 cc
= recvmsg(s
, &m
, 0);
1067 if (errno
!= EINTR
) {
1072 } else if (cc
== 0) {
1076 * receive control messages only. Process the
1077 * exceptions (currently the only possiblity is
1078 * a path MTU notification.)
1080 if ((mtu
= get_pathmtu(&m
)) > 0) {
1081 if ((options
& F_VERBOSE
) != 0) {
1082 printf("new path MTU (%d) is "
1090 * an ICMPv6 message (probably an echoreply) arrived.
1092 pr_pack(packet
, cc
, &m
);
1094 if (npackets
&& nreceived
>= npackets
)
1098 exit(nreceived
== 0);
1123 * This routine transmits another ping6.
1128 struct itimerval itimer
;
1134 * If we're not transmitting any more packets, change the timer
1135 * to wait two round-trip times if we've received any packets or
1136 * ten seconds if we haven't.
1140 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1141 if (itimer
.it_value
.tv_sec
== 0)
1142 itimer
.it_value
.tv_sec
= 1;
1144 itimer
.it_value
.tv_sec
= MAXWAIT
;
1145 itimer
.it_interval
.tv_sec
= 0;
1146 itimer
.it_interval
.tv_usec
= 0;
1147 itimer
.it_value
.tv_usec
= 0;
1149 (void)signal(SIGALRM
, onint
);
1150 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1155 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1156 * will be added on by the kernel. The ID field is our UNIX process ID,
1157 * and the sequence number is an ascending integer. The first 8 bytes
1158 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1159 * byte-order, to compute the round-trip time.
1166 if (options
& F_FQDN
)
1167 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1168 else if (options
& F_FQDNOLD
)
1170 else if (options
& F_NODEADDR
)
1171 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1172 else if (options
& F_SUPTYPES
)
1175 l
= ICMP6ECHOLEN
+ datalen
;
1183 struct icmp6_hdr
*icp
;
1184 struct iovec iov
[2];
1186 struct icmp6_nodeinfo
*nip
;
1189 if (npackets
&& ntransmitted
>= npackets
)
1190 return(-1); /* no more transmission */
1192 icp
= (struct icmp6_hdr
*)outpack
;
1193 nip
= (struct icmp6_nodeinfo
*)outpack
;
1194 memset(icp
, 0, sizeof(*icp
));
1195 icp
->icmp6_cksum
= 0;
1196 seq
= ntransmitted
++;
1197 CLR(seq
% mx_dup_ck
);
1199 if (options
& F_FQDN
) {
1200 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1201 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1202 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1203 nip
->ni_flags
= htons(0);
1205 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1206 sizeof(nip
->icmp6_ni_nonce
));
1207 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1209 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1210 sizeof(dst
.sin6_addr
));
1211 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1213 } else if (options
& F_FQDNOLD
) {
1214 /* packet format in 03 draft - no Subject data on queries */
1215 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1216 icp
->icmp6_code
= 0; /* code field is always 0 */
1217 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1218 nip
->ni_flags
= htons(0);
1220 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1221 sizeof(nip
->icmp6_ni_nonce
));
1222 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1226 } else if (options
& F_NODEADDR
) {
1227 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1228 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1229 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1230 nip
->ni_flags
= naflags
;
1232 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1233 sizeof(nip
->icmp6_ni_nonce
));
1234 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1236 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1237 sizeof(dst
.sin6_addr
));
1238 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1240 } else if (options
& F_SUPTYPES
) {
1241 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1242 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1243 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1244 /* we support compressed bitmap */
1245 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1247 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1248 sizeof(nip
->icmp6_ni_nonce
));
1249 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1253 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1254 icp
->icmp6_code
= 0;
1255 icp
->icmp6_id
= htons(ident
);
1256 icp
->icmp6_seq
= ntohs(seq
);
1258 (void)gettimeofday((struct timeval
*)
1259 &outpack
[ICMP6ECHOLEN
], NULL
);
1260 cc
= ICMP6ECHOLEN
+ datalen
;
1264 if (pingerlen() != cc
)
1265 errx(1, "internal error; length mismatch");
1268 smsghdr
.msg_name
= (caddr_t
)&dst
;
1269 smsghdr
.msg_namelen
= sizeof(dst
);
1270 memset(&iov
, 0, sizeof(iov
));
1271 iov
[0].iov_base
= (caddr_t
)outpack
;
1272 iov
[0].iov_len
= cc
;
1273 smsghdr
.msg_iov
= iov
;
1274 smsghdr
.msg_iovlen
= 1;
1276 i
= sendmsg(s
, &smsghdr
, 0);
1278 if (i
< 0 || i
!= cc
) {
1281 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1284 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1285 (void)write(STDOUT_FILENO
, &DOT
, 1);
1292 const struct icmp6_hdr
*icp
;
1294 if (ntohs(icp
->icmp6_id
) == ident
)
1302 const struct icmp6_nodeinfo
*nip
;
1304 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1305 nonce
+ sizeof(u_int16_t
),
1306 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1313 dnsdecode(sp
, ep
, base
, buf
, bufsiz
)
1316 const u_char
*base
; /*base for compressed name*/
1322 char cresult
[MAXDNAME
+ 1];
1333 if (i
== 0 || cp
!= *sp
) {
1334 if (strlcat(buf
, ".", bufsiz
) >= bufsiz
)
1335 return NULL
; /*result overrun*/
1341 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1342 /* DNS compression */
1346 comp
= base
+ (i
& 0x3f);
1347 if (dnsdecode(&comp
, cp
, base
, cresult
,
1348 sizeof(cresult
)) == NULL
)
1350 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1351 return NULL
; /*result overrun*/
1353 } else if ((i
& 0x3f) == i
) {
1355 return NULL
; /*source overrun*/
1356 while (i
-- > 0 && cp
< ep
) {
1357 l
= snprintf(cresult
, sizeof(cresult
),
1358 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1359 if (l
>= sizeof(cresult
))
1361 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1362 return NULL
; /*result overrun*/
1366 return NULL
; /*invalid label*/
1369 return NULL
; /*not terminated*/
1377 * Print out the packet, if it came from us. This logic is necessary
1378 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1379 * which arrive ('tis only fair). This permits multiple copies of this
1380 * program to be run without having intermingled output (or statistics!).
1383 pr_pack(buf
, cc
, mhdr
)
1386 struct msghdr
*mhdr
;
1388 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1389 struct icmp6_hdr
*icp
;
1390 struct icmp6_nodeinfo
*ni
;
1393 struct sockaddr
*from
;
1395 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1396 struct in6_pktinfo
*pktinfo
= NULL
;
1397 struct timeval tv
, *tp
;
1398 double triptime
= 0;
1403 char dnsname
[MAXDNAME
+ 1];
1405 (void)gettimeofday(&tv
, NULL
);
1407 if (!mhdr
|| !mhdr
->msg_name
||
1408 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1409 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1410 if (options
& F_VERBOSE
)
1411 warnx("invalid peername\n");
1414 from
= (struct sockaddr
*)mhdr
->msg_name
;
1415 fromlen
= mhdr
->msg_namelen
;
1416 if (cc
< sizeof(struct icmp6_hdr
)) {
1417 if (options
& F_VERBOSE
)
1418 warnx("packet too short (%d bytes) from %s\n", cc
,
1419 pr_addr(from
, fromlen
));
1422 icp
= (struct icmp6_hdr
*)buf
;
1423 ni
= (struct icmp6_nodeinfo
*)buf
;
1426 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1427 warnx("failed to get receiving hop limit");
1430 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1431 warnx("failed to get receiving pakcet information");
1435 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1436 seq
= ntohs(icp
->icmp6_seq
);
1439 tp
= (struct timeval
*)(icp
+ 1);
1441 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1442 ((double)tv
.tv_usec
) / 1000.0;
1444 #if defined(__OpenBSD__) || defined(__NetBSD__)
1445 tsumsq
+= triptime
* triptime
;
1447 if (triptime
< tmin
)
1449 if (triptime
> tmax
)
1453 if (TST(seq
% mx_dup_ck
)) {
1458 SET(seq
% mx_dup_ck
);
1462 if (options
& F_QUIET
)
1465 if (options
& F_FLOOD
)
1466 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1468 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1469 pr_addr(from
, fromlen
), seq
);
1470 (void)printf(" hlim=%d", hoplim
);
1471 if ((options
& F_VERBOSE
) != 0) {
1472 struct sockaddr_in6 dstsa
;
1474 memset(&dstsa
, 0, sizeof(dstsa
));
1475 dstsa
.sin6_family
= AF_INET6
;
1477 dstsa
.sin6_len
= sizeof(dstsa
);
1479 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1480 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1481 (void)printf(" dst=%s",
1482 pr_addr((struct sockaddr
*)&dstsa
,
1486 (void)printf(" time=%g ms", triptime
);
1488 (void)printf("(DUP!)");
1489 /* check the data */
1490 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1491 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1492 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1494 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1499 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1500 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1502 if (TST(seq
% mx_dup_ck
)) {
1507 SET(seq
% mx_dup_ck
);
1511 if (options
& F_QUIET
)
1514 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1516 switch (ntohs(ni
->ni_code
)) {
1517 case ICMP6_NI_SUCCESS
:
1519 case ICMP6_NI_REFUSED
:
1520 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1522 case ICMP6_NI_UNKNOWN
:
1523 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1526 printf("unknown code 0x%x, type 0x%x",
1527 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1531 switch (ntohs(ni
->ni_qtype
)) {
1533 printf("NodeInfo NOOP");
1535 case NI_QTYPE_SUPTYPES
:
1536 pr_suptypes(ni
, end
- (u_char
*)ni
);
1538 case NI_QTYPE_NODEADDR
:
1539 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1542 default: /* XXX: for backward compatibility */
1543 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1544 if (buf
[off
+ ICMP6_NIRLEN
] ==
1545 cc
- off
- ICMP6_NIRLEN
- 1)
1550 cp
++; /* skip length */
1552 safeputc(*cp
& 0xff);
1558 if (dnsdecode((const u_char
**)&cp
, end
,
1559 (const u_char
*)(ni
+ 1), dnsname
,
1560 sizeof(dnsname
)) == NULL
) {
1565 * name-lookup special handling for
1568 if (cp
+ 1 <= end
&& !*cp
&&
1569 strlen(dnsname
) > 0) {
1570 dnsname
[strlen(dnsname
) - 1] = '\0';
1573 printf("%s%s", i
> 0 ? "," : "",
1577 if (options
& F_VERBOSE
) {
1581 (void)printf(" ("); /*)*/
1583 switch (ni
->ni_code
) {
1584 case ICMP6_NI_REFUSED
:
1585 (void)printf("refused");
1588 case ICMP6_NI_UNKNOWN
:
1589 (void)printf("unknwon qtype");
1594 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1595 /* case of refusion, unknown */
1600 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1603 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1604 (void)printf("TTL=%d:meaningless",
1608 (void)printf("TTL=%d:invalid",
1611 (void)printf("TTL=%d", ttl
);
1621 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1630 if (buf
[off
+ ICMP6_NIRLEN
] !=
1631 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1634 (void)printf("invalid namelen:%d/%lu",
1635 buf
[off
+ ICMP6_NIRLEN
],
1636 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1646 /* We've got something other than an ECHOREPLY */
1647 if (!(options
& F_VERBOSE
))
1649 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1653 if (!(options
& F_FLOOD
)) {
1654 (void)putchar('\n');
1655 if (options
& F_VERBOSE
)
1657 (void)fflush(stdout
);
1664 struct msghdr
*mhdr
;
1668 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1669 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1670 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1673 switch (cm
->cmsg_type
) {
1675 printf(" HbH Options: ");
1676 pr_ip6opt(CMSG_DATA(cm
));
1679 #ifdef IPV6_RTHDRDSTOPTS
1680 case IPV6_RTHDRDSTOPTS
:
1682 printf(" Dst Options: ");
1683 pr_ip6opt(CMSG_DATA(cm
));
1686 printf(" Routing: ");
1687 pr_rthdr(CMSG_DATA(cm
));
1693 #ifdef USE_RFC2292BIS
1695 pr_ip6opt(void *extbuf
)
1697 struct ip6_hbh
*ext
;
1706 ext
= (struct ip6_hbh
*)extbuf
;
1707 extlen
= (ext
->ip6h_len
+ 1) * 8;
1708 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1709 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1713 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1714 &type
, &len
, &databuf
);
1715 if (currentlen
== -1)
1719 * Note that inet6_opt_next automatically skips any padding
1724 offset
= inet6_opt_get_val(databuf
, offset
,
1725 &value4
, sizeof(value4
));
1726 printf(" Jumbo Payload Opt: Length %u\n",
1727 (u_int32_t
)ntohl(value4
));
1729 case IP6OPT_ROUTER_ALERT
:
1731 offset
= inet6_opt_get_val(databuf
, offset
,
1732 &value2
, sizeof(value2
));
1733 printf(" Router Alert Opt: Type %u\n",
1737 printf(" Received Opt %u len %lu\n",
1738 type
, (unsigned long)len
);
1744 #else /* !USE_RFC2292BIS */
1747 pr_ip6opt(void *extbuf
)
1752 #endif /* USE_RFC2292BIS */
1754 #ifdef USE_RFC2292BIS
1756 pr_rthdr(void *extbuf
)
1758 struct in6_addr
*in6
;
1759 char ntopbuf
[INET6_ADDRSTRLEN
];
1760 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1763 /* print fixed part of the header */
1764 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1765 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1766 if ((segments
= inet6_rth_segments(extbuf
)) >= 0)
1767 printf("%d segments, ", segments
);
1769 printf("segments unknown, ");
1770 printf("%d left\n", rh
->ip6r_segleft
);
1772 for (i
= 0; i
< segments
; i
++) {
1773 in6
= inet6_rth_getaddr(extbuf
, i
);
1775 printf(" [%d]<NULL>\n", i
);
1777 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1779 strncpy(ntopbuf
, "?", sizeof(ntopbuf
));
1780 printf(" [%d]%s\n", i
, ntopbuf
);
1788 #else /* !USE_RFC2292BIS */
1791 pr_rthdr(void *extbuf
)
1796 #endif /* USE_RFC2292BIS */
1799 pr_bitrange(v
, s
, ii
)
1809 /* shift till we have 0x01 */
1810 if ((v
& 0x01) == 0) {
1812 printf("-%u", s
+ off
- 1);
1823 case 0x04: case 0x0c:
1834 /* we have 0x01 with us */
1835 for (i
= 0; i
< 32 - off
; i
++) {
1836 if ((v
& (0x01 << i
)) == 0)
1840 printf(" %u", s
+ off
);
1848 pr_suptypes(ni
, nilen
)
1849 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be SUPTYPES */
1854 const u_char
*cp
, *end
;
1857 u_int16_t words
; /*32bit count*/
1860 #define MAXQTYPES (1 << 16)
1864 cp
= (u_char
*)(ni
+ 1);
1865 end
= ((u_char
*)ni
) + nilen
;
1869 printf("NodeInfo Supported Qtypes");
1870 if (options
& F_VERBOSE
) {
1871 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1872 printf(", compressed bitmap");
1874 printf(", raw bitmap");
1878 clen
= (size_t)(end
- cp
);
1879 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1880 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1886 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1888 memcpy(&cbit
, cp
, sizeof(cbit
));
1889 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1893 clen
= ntohs(cbit
.words
) * sizeof(v
);
1894 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1899 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1900 memcpy(&v
, cp
+ off
, sizeof(v
));
1901 v
= (u_int32_t
)ntohl(v
);
1902 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1904 /* flush the remaining bits */
1905 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1909 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1910 cur
+= ntohs(cbit
.skip
) * 32;
1915 pr_nodeaddr(ni
, nilen
)
1916 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be NODEADDR */
1919 u_char
*cp
= (u_char
*)(ni
+ 1);
1920 char ntop_buf
[INET6_ADDRSTRLEN
];
1923 nilen
-= sizeof(struct icmp6_nodeinfo
);
1925 if (options
& F_VERBOSE
) {
1926 switch (ni
->ni_code
) {
1927 case ICMP6_NI_REFUSED
:
1928 (void)printf("refused");
1930 case ICMP6_NI_UNKNOWN
:
1931 (void)printf("unknown qtype");
1934 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1935 (void)printf(" truncated");
1939 printf(" no address\n");
1942 * In icmp-name-lookups 05 and later, TTL of each returned address
1943 * is contained in the resposne. We try to detect the version
1944 * by the length of the data, but note that the detection algorithm
1945 * is incomplete. We assume the latest draft by default.
1947 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1953 /* XXX: alignment? */
1954 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1955 cp
+= sizeof(u_int32_t
);
1956 nilen
-= sizeof(u_int32_t
);
1959 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1961 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
1962 printf(" %s", ntop_buf
);
1964 if (ttl
== 0xffffffff) {
1966 * XXX: can this convention be applied to all
1967 * type of TTL (i.e. non-ND TTL)?
1969 printf("(TTL=infty)");
1972 printf("(TTL=%u)", ttl
);
1976 nilen
-= sizeof(struct in6_addr
);
1977 cp
+= sizeof(struct in6_addr
);
1983 struct msghdr
*mhdr
;
1987 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1988 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1989 if (cm
->cmsg_len
== 0)
1992 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1993 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1994 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1995 return(*(int *)CMSG_DATA(cm
));
2001 struct in6_pktinfo
*
2002 get_rcvpktinfo(mhdr
)
2003 struct msghdr
*mhdr
;
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_PKTINFO
&&
2014 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2015 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2023 struct msghdr
*mhdr
;
2025 #ifdef IPV6_RECVPATHMTU
2027 struct ip6_mtuinfo
*mtuctl
= NULL
;
2029 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2030 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2031 if (cm
->cmsg_len
== 0)
2034 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2035 cm
->cmsg_type
== IPV6_PATHMTU
&&
2036 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2037 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2040 * If the notified destination is different from
2041 * the one we are pinging, just ignore the info.
2042 * We check the scope ID only when both notified value
2043 * and our own value have non-0 values, because we may
2044 * have used the default scope zone ID for sending,
2045 * in which case the scope ID value is 0.
2047 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2049 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2050 dst
.sin6_scope_id
&&
2051 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2052 dst
.sin6_scope_id
)) {
2053 if ((options
& F_VERBOSE
) != 0) {
2054 printf("path MTU for %s is notified. "
2056 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2057 sizeof(mtuctl
->ip6m_addr
)));
2063 * Ignore an invalid MTU. XXX: can we just believe
2066 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2069 /* notification for our destination. return the MTU. */
2070 return((int)mtuctl
->ip6m_mtu
);
2082 static int firsttime
= 1;
2086 int oldlen
= smsghdr
.msg_controllen
;
2087 char *oldbuf
= smsghdr
.msg_control
;
2089 /* XXX: We need to enlarge control message buffer */
2090 firsttime
= 0; /* prevent further enlargement */
2092 smsghdr
.msg_controllen
= oldlen
+ CMSG_SPACE(sizeof(int));
2093 if ((smsghdr
.msg_control
=
2094 (char *)malloc(smsghdr
.msg_controllen
)) == NULL
)
2095 err(1, "set_pathmtu: malloc");
2096 cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&smsghdr
);
2097 cm
->cmsg_len
= CMSG_LEN(sizeof(int));
2098 cm
->cmsg_level
= IPPROTO_IPV6
;
2099 cm
->cmsg_type
= IPV6_USE_MTU
;
2101 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&smsghdr
, cm
);
2103 memcpy((void *)cm
, (void *)oldbuf
, oldlen
);
2109 * look for a cmsgptr that points MTU structure.
2110 * XXX: this procedure seems redundant at this moment, but we'd better
2111 * keep the code generic enough for future extensions.
2113 for (cm
= CMSG_FIRSTHDR(&smsghdr
); cm
;
2114 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&smsghdr
, cm
)) {
2115 if (cm
->cmsg_len
== 0) /* XXX: paranoid check */
2116 errx(1, "set_pathmtu: internal error");
2118 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2119 cm
->cmsg_type
== IPV6_USE_MTU
&&
2120 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2125 errx(1, "set_pathmtu: internal error: no space for path MTU");
2127 *(int *)CMSG_DATA(cm
) = mtu
;
2133 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2138 register struct timeval
*out
, *in
;
2140 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2142 out
->tv_usec
+= 1000000;
2144 out
->tv_sec
-= in
->tv_sec
;
2158 (void)signal(SIGINT
, SIG_DFL
);
2159 (void)kill(getpid(), SIGINT
);
2167 * Print out statistics.
2173 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2174 (void)printf("%ld packets transmitted, ", ntransmitted
);
2175 (void)printf("%ld packets received, ", nreceived
);
2177 (void)printf("+%ld duplicates, ", nrepeats
);
2179 if (nreceived
> ntransmitted
)
2180 (void)printf("-- somebody's printing up packets!");
2182 (void)printf("%d%% packet loss",
2183 (int) (((ntransmitted
- nreceived
) * 100) /
2186 (void)putchar('\n');
2187 if (nreceived
&& timing
) {
2188 /* Only display average to microseconds */
2189 double num
= nreceived
+ nrepeats
;
2190 double avg
= tsum
/ num
;
2191 #if defined(__OpenBSD__) || defined(__NetBSD__)
2192 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2194 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2195 tmin
, avg
, tmax
, dev
);
2198 "round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
2201 (void)fflush(stdout
);
2203 (void)fflush(stdout
);
2207 static char *niqcode
[] = {
2209 "DNS label", /*or empty*/
2214 static char *nircode
[] = {
2215 "Success", "Refused", "Unknown",
2221 * Print a descriptive string about an ICMP header.
2225 struct icmp6_hdr
*icp
;
2228 char ntop_buf
[INET6_ADDRSTRLEN
];
2229 struct nd_redirect
*red
;
2230 struct icmp6_nodeinfo
*ni
;
2231 char dnsname
[MAXDNAME
+ 1];
2235 switch (icp
->icmp6_type
) {
2236 case ICMP6_DST_UNREACH
:
2237 switch (icp
->icmp6_code
) {
2238 case ICMP6_DST_UNREACH_NOROUTE
:
2239 (void)printf("No Route to Destination\n");
2241 case ICMP6_DST_UNREACH_ADMIN
:
2242 (void)printf("Destination Administratively "
2245 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2246 (void)printf("Destination Unreachable Beyond Scope\n");
2248 case ICMP6_DST_UNREACH_ADDR
:
2249 (void)printf("Destination Host Unreachable\n");
2251 case ICMP6_DST_UNREACH_NOPORT
:
2252 (void)printf("Destination Port Unreachable\n");
2255 (void)printf("Destination Unreachable, Bad Code: %d\n",
2259 /* Print returned IP header information */
2260 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2262 case ICMP6_PACKET_TOO_BIG
:
2263 (void)printf("Packet too big mtu = %d\n",
2264 (int)ntohl(icp
->icmp6_mtu
));
2265 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2267 case ICMP6_TIME_EXCEEDED
:
2268 switch (icp
->icmp6_code
) {
2269 case ICMP6_TIME_EXCEED_TRANSIT
:
2270 (void)printf("Time to live exceeded\n");
2272 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2273 (void)printf("Frag reassembly time exceeded\n");
2276 (void)printf("Time exceeded, Bad Code: %d\n",
2280 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2282 case ICMP6_PARAM_PROB
:
2283 (void)printf("Parameter problem: ");
2284 switch (icp
->icmp6_code
) {
2285 case ICMP6_PARAMPROB_HEADER
:
2286 (void)printf("Erroneous Header ");
2288 case ICMP6_PARAMPROB_NEXTHEADER
:
2289 (void)printf("Unknown Nextheader ");
2291 case ICMP6_PARAMPROB_OPTION
:
2292 (void)printf("Unrecognized Option ");
2295 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2298 (void)printf("pointer = 0x%02x\n",
2299 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2300 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2302 case ICMP6_ECHO_REQUEST
:
2303 (void)printf("Echo Request");
2304 /* XXX ID + Seq + Data */
2306 case ICMP6_ECHO_REPLY
:
2307 (void)printf("Echo Reply");
2308 /* XXX ID + Seq + Data */
2310 case ICMP6_MEMBERSHIP_QUERY
:
2311 (void)printf("Listener Query");
2313 case ICMP6_MEMBERSHIP_REPORT
:
2314 (void)printf("Listener Report");
2316 case ICMP6_MEMBERSHIP_REDUCTION
:
2317 (void)printf("Listener Done");
2319 case ND_ROUTER_SOLICIT
:
2320 (void)printf("Router Solicitation");
2322 case ND_ROUTER_ADVERT
:
2323 (void)printf("Router Advertisement");
2325 case ND_NEIGHBOR_SOLICIT
:
2326 (void)printf("Neighbor Solicitation");
2328 case ND_NEIGHBOR_ADVERT
:
2329 (void)printf("Neighbor Advertisement");
2332 red
= (struct nd_redirect
*)icp
;
2333 (void)printf("Redirect\n");
2334 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2336 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2337 (void)printf("Destination: %s", ntop_buf
);
2338 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2340 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2341 (void)printf(" New Target: %s", ntop_buf
);
2343 case ICMP6_NI_QUERY
:
2344 (void)printf("Node Information Query");
2345 /* XXX ID + Seq + Data */
2346 ni
= (struct icmp6_nodeinfo
*)icp
;
2347 l
= end
- (u_char
*)(ni
+ 1);
2349 switch (ntohs(ni
->ni_qtype
)) {
2351 (void)printf("NOOP");
2353 case NI_QTYPE_SUPTYPES
:
2354 (void)printf("Supported qtypes");
2357 (void)printf("DNS name");
2359 case NI_QTYPE_NODEADDR
:
2360 (void)printf("nodeaddr");
2362 case NI_QTYPE_IPV4ADDR
:
2363 (void)printf("IPv4 nodeaddr");
2366 (void)printf("unknown qtype");
2369 if (options
& F_VERBOSE
) {
2370 switch (ni
->ni_code
) {
2371 case ICMP6_NI_SUBJ_IPV6
:
2372 if (l
== sizeof(struct in6_addr
) &&
2373 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2374 sizeof(ntop_buf
)) != NULL
) {
2375 (void)printf(", subject=%s(%s)",
2376 niqcode
[ni
->ni_code
], ntop_buf
);
2379 /* backward compat to -W */
2380 (void)printf(", oldfqdn");
2382 (void)printf(", invalid");
2386 case ICMP6_NI_SUBJ_FQDN
:
2387 if (end
== (u_char
*)(ni
+ 1)) {
2388 (void)printf(", no subject");
2391 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2392 cp
= (const u_char
*)(ni
+ 1);
2393 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2394 sizeof(dnsname
)) != NULL
)
2395 printf("(%s)", dnsname
);
2397 printf("(invalid)");
2399 case ICMP6_NI_SUBJ_IPV4
:
2400 if (l
== sizeof(struct in_addr
) &&
2401 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2402 sizeof(ntop_buf
)) != NULL
) {
2403 (void)printf(", subject=%s(%s)",
2404 niqcode
[ni
->ni_code
], ntop_buf
);
2406 (void)printf(", invalid");
2409 (void)printf(", invalid");
2414 case ICMP6_NI_REPLY
:
2415 (void)printf("Node Information Reply");
2416 /* XXX ID + Seq + Data */
2417 ni
= (struct icmp6_nodeinfo
*)icp
;
2419 switch (ntohs(ni
->ni_qtype
)) {
2421 (void)printf("NOOP");
2423 case NI_QTYPE_SUPTYPES
:
2424 (void)printf("Supported qtypes");
2427 (void)printf("DNS name");
2429 case NI_QTYPE_NODEADDR
:
2430 (void)printf("nodeaddr");
2432 case NI_QTYPE_IPV4ADDR
:
2433 (void)printf("IPv4 nodeaddr");
2436 (void)printf("unknown qtype");
2439 if (options
& F_VERBOSE
) {
2440 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2441 printf(", invalid");
2443 printf(", %s", nircode
[ni
->ni_code
]);
2447 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2453 * Print an IP6 header.
2457 struct ip6_hdr
*ip6
;
2459 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2461 char ntop_buf
[INET6_ADDRSTRLEN
];
2463 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2464 tc
= (tc
>> 4) & 0x0f;
2465 tc
|= (ip6
->ip6_vfc
<< 4);
2467 printf("Vr TC Flow Plen Nxt Hlim\n");
2468 printf(" %1x %02x %05x %04x %02x %02x\n",
2469 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2470 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2471 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2472 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2473 printf("%s->", ntop_buf
);
2474 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2475 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2476 printf("%s\n", ntop_buf
);
2481 * Return an ascii host address as a dotted quad and optionally with
2485 pr_addr(addr
, addrlen
)
2486 struct sockaddr
*addr
;
2489 static char buf
[NI_MAXHOST
];
2492 #ifdef NI_WITHSCOPEID
2493 flag
= NI_WITHSCOPEID
;
2497 if ((options
& F_HOSTNAME
) == 0)
2498 flag
|= NI_NUMERICHOST
;
2500 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2508 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2512 struct ip6_hdr
*ip6
;
2515 u_char
*cp
= (u_char
*)ip6
, nh
;
2518 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2523 hlen
= sizeof(*ip6
);
2527 while (end
- cp
>= 8) {
2529 case IPPROTO_HOPOPTS
:
2531 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2532 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2534 case IPPROTO_DSTOPTS
:
2536 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2537 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2539 case IPPROTO_FRAGMENT
:
2541 hlen
= sizeof(struct ip6_frag
);
2542 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2544 case IPPROTO_ROUTING
:
2546 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2547 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2552 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2553 nh
= ((struct ah
*)cp
)->ah_nxt
;
2556 case IPPROTO_ICMPV6
:
2557 printf("ICMP6: type = %d, code = %d\n",
2564 printf("TCP: from port %u, to port %u (decimal)\n",
2565 (*cp
* 256 + *(cp
+ 1)),
2566 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2569 printf("UDP: from port %u, to port %u (decimal)\n",
2570 (*cp
* 256 + *(cp
+ 1)),
2571 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2574 printf("Unknown Header(%d)\n", nh
);
2578 if ((cp
+= hlen
) >= end
)
2596 register int ii
, jj
, kk
;
2600 for (cp
= patp
; *cp
; cp
++)
2602 errx(1, "patterns must be specified as hex digits");
2604 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2605 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2606 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2607 &pat
[13], &pat
[14], &pat
[15]);
2612 kk
<= MAXDATALEN
- (8 + sizeof(struct timeval
) + ii
);
2614 for (jj
= 0; jj
< ii
; ++jj
)
2615 bp
[jj
+ kk
] = pat
[jj
];
2616 if (!(options
& F_QUIET
)) {
2617 (void)printf("PATTERN: 0x");
2618 for (jj
= 0; jj
< ii
; ++jj
)
2619 (void)printf("%02x", bp
[jj
] & 0xFF);
2625 #ifdef IPSEC_POLICY_IPSEC
2627 setpolicy(so
, policy
)
2634 return 0; /* ignore */
2636 buf
= ipsec_set_policy(policy
, strlen(policy
));
2638 errx(1, "%s", ipsec_strerror());
2639 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2640 ipsec_get_policylen(buf
)) < 0)
2641 warnx("Unable to set IPSec policy");
2656 u_int8_t digest
[16];
2659 char hbuf
[NI_MAXHOST
];
2660 struct in6_addr in6
;
2662 p
= strchr(name
, '.');
2664 p
= name
+ strlen(name
);
2666 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2667 return NULL
; /*label too long*/
2668 strncpy(hbuf
, name
, l
);
2669 hbuf
[(int)l
] = '\0';
2671 for (q
= name
; *q
; q
++) {
2676 /* generate 8 bytes of pseudo-random value. */
2677 bzero(&ctxt
, sizeof(ctxt
));
2680 MD5Update(&ctxt
, &c
, sizeof(c
));
2681 MD5Update(&ctxt
, name
, l
);
2682 MD5Final(digest
, &ctxt
);
2684 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2685 return NULL
; /*XXX*/
2686 bcopy(digest
, &in6
.s6_addr
[12], 4);
2688 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2691 return strdup(hbuf
);
2697 (void)fprintf(stderr
,
2698 "usage: ping6 [-dfH"
2699 #ifdef IPV6_USE_MIN_MTU
2703 #ifdef IPV6_REACHCONF
2707 #ifdef IPSEC_POLICY_IPSEC
2713 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
2714 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2716 "\t[-s packetsize] [-h hoplimit] [hops...] host\n");