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 #define BIND_8_COMPAT
109 #include <sys/param.h>
111 #include <sys/socket.h>
112 #include <sys/time.h>
115 #include <net/route.h>
117 #include <netinet/in.h>
118 #include <netinet/ip6.h>
119 #include <netinet/icmp6.h>
120 #include <arpa/inet.h>
121 #include <arpa/nameser.h>
128 #if defined(__OpenBSD__) || defined(__NetBSD__)
138 #include <netinet6/ah.h>
139 #include <netinet6/ipsec.h>
142 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
148 #define MAXPACKETLEN 131072
150 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
151 #define ICMP6ECHOTMLEN sizeof(struct timeval)
152 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
153 /* FQDN case, 64 bits of nonce + 32 bits ttl */
154 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
155 #define EXTRA 256 /* for AH and various other headers. weird. */
156 #define DEFDATALEN ICMP6ECHOTMLEN
157 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
158 #define NROUTES 9 /* number of record route slots */
160 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
161 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
162 #define SET(bit) (A(bit) |= B(bit))
163 #define CLR(bit) (A(bit) &= (~B(bit)))
164 #define TST(bit) (A(bit) & B(bit))
166 #define F_FLOOD 0x0001
167 #define F_INTERVAL 0x0002
168 #define F_PINGFILLED 0x0008
169 #define F_QUIET 0x0010
170 #define F_RROUTE 0x0020
171 #define F_SO_DEBUG 0x0040
172 #define F_VERBOSE 0x0100
174 #ifdef IPSEC_POLICY_IPSEC
175 #define F_POLICY 0x0400
177 #define F_AUTHHDR 0x0200
178 #define F_ENCRYPT 0x0400
179 #endif /*IPSEC_POLICY_IPSEC*/
181 #define F_NODEADDR 0x0800
182 #define F_FQDN 0x1000
183 #define F_INTERFACE 0x2000
184 #define F_SRCADDR 0x4000
185 #ifdef IPV6_REACHCONF
186 #define F_REACHCONF 0x8000
188 #define F_HOSTNAME 0x10000
189 #define F_FQDNOLD 0x20000
190 #define F_NIGROUP 0x40000
191 #define F_SUPTYPES 0x80000
192 #define F_NOMINMTU 0x100000
193 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
196 #define IN6LEN sizeof(struct in6_addr)
197 #define SA6LEN sizeof(struct sockaddr_in6)
198 #define DUMMY_PORT 10101
200 #define SIN6(s) ((struct sockaddr_in6 *)(s))
203 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
204 * number of received sequence numbers we can keep track of. Change 128
205 * to 8192 for complete accuracy...
207 #define MAX_DUP_CHK (8 * 8192)
208 int mx_dup_ck
= MAX_DUP_CHK
;
209 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
211 struct addrinfo
*res
;
212 struct sockaddr_in6 dst
; /* who to ping6 */
213 struct sockaddr_in6 src
; /* src addr of this packet */
214 int datalen
= DEFDATALEN
;
215 int s
; /* socket file descriptor */
216 u_char outpack
[MAXPACKETLEN
];
217 char BSPACE
= '\b'; /* characters written for flood */
220 int ident
; /* process id to identify our packets */
221 u_int8_t nonce
[8]; /* nonce field for node information */
222 struct in6_addr srcaddr
;
223 int hoplimit
= -1; /* hoplimit */
224 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
227 long npackets
; /* max packets to transmit */
228 long nreceived
; /* # of packets we got back */
229 long nrepeats
; /* number of duplicates */
230 long ntransmitted
; /* sequence # for outbound packets = #sent */
231 struct timeval interval
= {1, 0}; /* interval between packets */
234 int timing
; /* flag to do timing */
235 double tmin
= 999999999.0; /* minimum round trip time */
236 double tmax
= 0.0; /* maximum round trip time */
237 double tsum
= 0.0; /* sum of all times, for doing average */
238 #if defined(__OpenBSD__) || defined(__NetBSD__)
239 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
242 /* for node addresses */
245 /* for ancillary data(advanced API) */
246 struct msghdr smsghdr
;
247 struct iovec smsgiov
;
251 volatile sig_atomic_t seenalrm
;
252 volatile sig_atomic_t seenint
;
254 volatile sig_atomic_t seeninfo
;
257 int main
__P((int, char *[]));
258 void fill
__P((char *, char *));
259 int get_hoplim
__P((struct msghdr
*));
260 int get_pathmtu
__P((struct msghdr
*));
261 void set_pathmtu
__P((int));
262 struct in6_pktinfo
*get_rcvpktinfo
__P((struct msghdr
*));
263 void onsignal
__P((int));
264 void retransmit
__P((void));
265 void onint
__P((int));
266 size_t pingerlen
__P((void));
267 int pinger
__P((void));
268 const char *pr_addr
__P((struct sockaddr
*, int));
269 void pr_icmph
__P((struct icmp6_hdr
*, u_char
*));
270 void pr_iph
__P((struct ip6_hdr
*));
271 void pr_suptypes
__P((struct icmp6_nodeinfo
*, size_t));
272 void pr_nodeaddr
__P((struct icmp6_nodeinfo
*, int));
273 int myechoreply
__P((const struct icmp6_hdr
*));
274 int mynireply
__P((const struct icmp6_nodeinfo
*));
275 char *dnsdecode
__P((const u_char
**, const u_char
*, const u_char
*,
277 void pr_pack
__P((u_char
*, int, struct msghdr
*));
278 void pr_exthdrs
__P((struct msghdr
*));
279 void pr_ip6opt
__P((void *));
280 void pr_rthdr
__P((void *));
281 int pr_bitrange
__P((u_int32_t
, int, int));
282 void pr_retip
__P((struct ip6_hdr
*, u_char
*));
283 void summary
__P((void));
284 void tvsub
__P((struct timeval
*, struct timeval
*));
285 int setpolicy
__P((int, char *));
286 char *nigroup
__P((char *));
287 void usage
__P((void));
294 struct itimerval itimer
;
295 struct sockaddr_in6 from
;
296 struct timeval timeout
, *tv
;
297 struct addrinfo hints
;
301 int ch
, fromlen
, hold
, packlen
, preload
, optval
, ret_ga
;
302 u_char
*datap
, *packet
;
303 char *e
, *target
, *ifname
= NULL
;
305 struct cmsghdr
*scmsgp
= NULL
;
308 struct in6_pktinfo
*pktinfo
= NULL
;
309 #ifdef USE_RFC2292BIS
310 struct ip6_rthdr
*rthdr
= NULL
;
312 #ifdef IPSEC_POLICY_IPSEC
313 char *policy_in
= NULL
;
314 char *policy_out
= NULL
;
319 /* just to be sure */
320 memset(&smsghdr
, 0, sizeof(&smsghdr
));
321 memset(&smsgiov
, 0, sizeof(&smsgiov
));
324 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
328 #ifdef IPSEC_POLICY_IPSEC
332 #endif /*IPSEC_POLICY_IPSEC*/
334 while ((ch
= getopt(argc
, argv
,
335 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS
)) != -1) {
342 options
&= ~F_NOUSERDATA
;
343 options
|= F_NODEADDR
;
344 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
347 naflags
|= NI_NODEADDR_FLAG_ALL
;
351 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
355 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
359 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
363 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
365 case 'A': /* experimental. not in the spec */
366 #ifdef NI_NODEADDR_FLAG_ANYCAST
367 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
371 "-a A is not supported on the platform");
382 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
383 sockbufsize
= atoi(optarg
);
386 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
390 npackets
= strtol(optarg
, &e
, 10);
391 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
393 "illegal number of packets -- %s", optarg
);
396 options
|= F_SO_DEBUG
;
401 errx(1, "Must be superuser to flood ping");
404 setbuf(stdout
, (char *)NULL
);
407 options
|= F_HOSTNAME
;
409 case 'h': /* hoplimit */
410 hoplimit
= strtol(optarg
, &e
, 10);
411 if (255 < hoplimit
|| hoplimit
< -1)
413 "illegal hoplimit -- %s", optarg
);
417 options
|= F_INTERFACE
;
418 #ifndef USE_SIN6_SCOPE_ID
422 case 'i': /* wait between sending packets */
423 intval
= strtod(optarg
, &e
);
424 if (*optarg
== '\0' || *e
!= '\0')
425 errx(1, "illegal timing interval %s", optarg
);
426 if (intval
< 1 && getuid()) {
427 errx(1, "%s: only root may use interval < 1s",
430 interval
.tv_sec
= (long)intval
;
432 (long)((intval
- interval
.tv_sec
) * 1000000);
433 if (interval
.tv_sec
< 0)
434 errx(1, "illegal timing interval %s", optarg
);
435 /* less than 1/hz does not make sense */
436 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
437 warnx("too small interval, raised to 0.01");
438 interval
.tv_usec
= 10000;
440 options
|= F_INTERVAL
;
445 errx(1, "Must be superuser to preload");
447 preload
= strtol(optarg
, &e
, 10);
448 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
449 errx(1, "illegal preload value -- %s", optarg
);
452 #ifdef IPV6_USE_MIN_MTU
453 options
|= F_NOMINMTU
;
456 errx(1, "-%c is not supported on this platform", ch
);
460 options
&= ~F_HOSTNAME
;
463 options
|= F_NIGROUP
;
465 case 'p': /* fill buffer with user pattern */
466 options
|= F_PINGFILLED
;
467 fill((char *)datap
, optarg
);
473 #ifdef IPV6_REACHCONF
474 options
|= F_REACHCONF
;
477 errx(1, "-R is not supported in this configuration");
480 /* XXX: use getaddrinfo? */
481 if (inet_pton(AF_INET6
, optarg
, (void *)&srcaddr
) != 1)
482 errx(1, "invalid IPv6 address: %s", optarg
);
483 options
|= F_SRCADDR
;
486 case 's': /* size of packet to send */
487 datalen
= strtol(optarg
, &e
, 10);
488 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
489 errx(1, "illegal datalen value -- %s", optarg
);
490 if (datalen
> MAXDATALEN
) {
492 "datalen value too large, maximum is %d",
497 options
&= ~F_NOUSERDATA
;
498 options
|= F_SUPTYPES
;
501 options
|= F_VERBOSE
;
504 options
&= ~F_NOUSERDATA
;
508 options
&= ~F_NOUSERDATA
;
509 options
|= F_FQDNOLD
;
512 #ifdef IPSEC_POLICY_IPSEC
515 if (!strncmp("in", optarg
, 2)) {
516 if ((policy_in
= strdup(optarg
)) == NULL
)
518 } else if (!strncmp("out", optarg
, 3)) {
519 if ((policy_out
= strdup(optarg
)) == NULL
)
522 errx(1, "invalid security policy");
526 options
|= F_AUTHHDR
;
529 options
|= F_ENCRYPT
;
531 #endif /*IPSEC_POLICY_IPSEC*/
547 #ifdef IPV6_RECVRTHDR /* 2292bis */
548 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
551 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
554 errx(1, "too many intermediate hops");
560 if (options
& F_NIGROUP
) {
561 target
= nigroup(argv
[argc
- 1]);
562 if (target
== NULL
) {
567 target
= argv
[argc
- 1];
570 bzero(&hints
, sizeof(struct addrinfo
));
571 hints
.ai_flags
= AI_CANONNAME
;
572 hints
.ai_family
= AF_INET6
;
573 hints
.ai_socktype
= SOCK_RAW
;
574 hints
.ai_protocol
= IPPROTO_ICMPV6
;
576 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
578 fprintf(stderr
, "ping6: %s\n", gai_strerror(ret_ga
));
581 if (res
->ai_canonname
)
582 hostname
= res
->ai_canonname
;
587 errx(1, "getaddrinfo failed");
589 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
591 res
->ai_socktype
= SOCK_RAW
;
592 res
->ai_protocol
= IPPROTO_ICMPV6
;
594 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
595 res
->ai_protocol
)) < 0)
599 * let the kerel pass extension headers of incoming packets,
600 * for privileged socket options
602 if ((options
& F_VERBOSE
) != 0) {
605 #ifdef IPV6_RECVHOPOPTS
606 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
608 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
609 #else /* old adv. API */
610 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
612 err(1, "setsockopt(IPV6_HOPOPTS)");
614 #ifdef IPV6_RECVDSTOPTS
615 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
617 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
618 #else /* old adv. API */
619 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
621 err(1, "setsockopt(IPV6_DSTOPTS)");
623 #ifdef IPV6_RECVRTHDRDSTOPTS
624 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
626 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
630 /* revoke root privilege */
634 if (options
& F_FLOOD
&& options
& F_INTERVAL
)
635 errx(1, "-f and -i incompatible options");
637 if ((options
& F_NOUSERDATA
) == 0) {
638 if (datalen
>= sizeof(struct timeval
)) {
639 /* we can time transfer */
643 /* in F_VERBOSE case, we may get non-echoreply packets*/
644 if (options
& F_VERBOSE
)
645 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
647 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
649 /* suppress timing for node information query */
652 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
655 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
656 err(1, "Unable to allocate packet");
657 if (!(options
& F_PINGFILLED
))
658 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
661 ident
= getpid() & 0xFFFF;
663 gettimeofday(&timeout
, NULL
);
664 srand((unsigned int)(timeout
.tv_sec
^ timeout
.tv_usec
^ (long)ident
));
665 memset(nonce
, 0, sizeof(nonce
));
666 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
667 *((int *)&nonce
[i
]) = rand();
669 memset(nonce
, 0, sizeof(nonce
));
670 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
671 *((u_int32_t
*)&nonce
[i
]) = arc4random();
676 if (options
& F_SO_DEBUG
)
677 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
679 optval
= IPV6_DEFHLIM
;
680 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
681 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
682 &optval
, sizeof(optval
)) == -1)
683 err(1, "IPV6_MULTICAST_HOPS");
684 #ifdef IPV6_USE_MIN_MTU
685 if ((options
& F_NOMINMTU
) == 0) {
687 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
688 &optval
, sizeof(optval
)) == -1)
689 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
691 #ifdef IPV6_RECVPATHMTU
694 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
695 &optval
, sizeof(optval
)) == -1)
696 err(1, "setsockopt(IPV6_RECVPATHMTU)");
698 #endif /* IPV6_RECVPATHMTU */
699 #endif /* IPV6_USE_MIN_MTU */
702 #ifdef IPSEC_POLICY_IPSEC
703 if (options
& F_POLICY
) {
704 if (setpolicy(s
, policy_in
) < 0)
705 errx(1, "%s", ipsec_strerror());
706 if (setpolicy(s
, policy_out
) < 0)
707 errx(1, "%s", ipsec_strerror());
710 if (options
& F_AUTHHDR
) {
711 optval
= IPSEC_LEVEL_REQUIRE
;
712 #ifdef IPV6_AUTH_TRANS_LEVEL
713 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
714 &optval
, sizeof(optval
)) == -1)
715 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
717 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
718 &optval
, sizeof(optval
)) == -1)
719 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
722 if (options
& F_ENCRYPT
) {
723 optval
= IPSEC_LEVEL_REQUIRE
;
724 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
725 &optval
, sizeof(optval
)) == -1)
726 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
728 #endif /*IPSEC_POLICY_IPSEC*/
733 struct icmp6_filter filt
;
734 if (!(options
& F_VERBOSE
)) {
735 ICMP6_FILTER_SETBLOCKALL(&filt
);
736 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
737 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
738 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
740 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
742 ICMP6_FILTER_SETPASSALL(&filt
);
744 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
746 err(1, "setsockopt(ICMP6_FILTER)");
748 #endif /*ICMP6_FILTER*/
750 /* let the kerel pass extension headers of incoming packets */
751 if ((options
& F_VERBOSE
) != 0) {
754 #ifdef IPV6_RECVRTHDR
755 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
757 err(1, "setsockopt(IPV6_RECVRTHDR)");
758 #else /* old adv. API */
759 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
761 err(1, "setsockopt(IPV6_RTHDR)");
767 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
768 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
769 &optval, sizeof(optval)) == -1)
770 err(1, "IPV6_MULTICAST_LOOP");
773 /* Specify the outgoing interface and/or the source address */
775 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
778 ip6optlen
+= CMSG_SPACE(sizeof(int));
780 #ifdef IPV6_REACHCONF
781 if (options
& F_REACHCONF
)
782 ip6optlen
+= CMSG_SPACE(0);
785 /* set IP6 packet options */
787 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
788 errx(1, "can't allocate enough memory");
789 smsghdr
.msg_control
= (caddr_t
)scmsg
;
790 smsghdr
.msg_controllen
= ip6optlen
;
791 scmsgp
= (struct cmsghdr
*)scmsg
;
794 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
795 memset(pktinfo
, 0, sizeof(*pktinfo
));
796 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
797 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
798 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
799 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
802 /* set the outgoing interface */
804 #ifndef USE_SIN6_SCOPE_ID
805 /* pktinfo must have already been allocated */
806 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
807 errx(1, "%s: invalid interface name", ifname
);
809 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
810 errx(1, "%s: invalid interface name", ifname
);
813 /* set the source address */
814 if (options
& F_SRCADDR
)/* pktinfo must be valid */
815 pktinfo
->ipi6_addr
= srcaddr
;
817 if (hoplimit
!= -1) {
818 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
819 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
820 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
821 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
823 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
825 #ifdef IPV6_REACHCONF
826 if (options
& F_REACHCONF
) {
827 scmsgp
->cmsg_len
= CMSG_LEN(0);
828 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
829 scmsgp
->cmsg_type
= IPV6_REACHCONF
;
831 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
835 if (argc
> 1) { /* some intermediate addrs are specified */
837 #ifdef USE_RFC2292BIS
841 #ifdef USE_RFC2292BIS
842 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
843 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
844 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
845 scmsgp
->cmsg_type
= IPV6_RTHDR
;
846 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
847 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
848 IPV6_RTHDR_TYPE_0
, argc
- 1);
850 errx(1, "can't initialize rthdr");
851 #else /* old advanced API */
852 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
853 IPV6_RTHDR_TYPE_0
)) == 0)
854 errx(1, "can't initialize rthdr");
855 #endif /* USE_RFC2292BIS */
857 for (hops
= 0; hops
< argc
- 1; hops
++) {
858 struct addrinfo
*iaip
;
860 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
862 errx(1, "%s", gai_strerror(error
));
863 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
865 "bad addr family of an intermediate addr");
867 #ifdef USE_RFC2292BIS
868 if (inet6_rth_add(rthdr
,
869 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
870 errx(1, "can't add an intermediate node");
871 #else /* old advanced API */
872 if (inet6_rthdr_add(scmsgp
,
873 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
875 errx(1, "can't add an intermediate node");
876 #endif /* USE_RFC2292BIS */
880 #ifndef USE_RFC2292BIS
881 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
882 errx(1, "can't set the last flag");
885 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
892 int dummy
, len
= sizeof(src
);
894 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
895 err(1, "UDP socket");
897 src
.sin6_family
= AF_INET6
;
898 src
.sin6_addr
= dst
.sin6_addr
;
899 src
.sin6_port
= ntohs(DUMMY_PORT
);
900 src
.sin6_scope_id
= dst
.sin6_scope_id
;
902 #ifdef USE_RFC2292BIS
904 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
905 (void *)pktinfo
, sizeof(*pktinfo
)))
906 err(1, "UDP setsockopt(IPV6_PKTINFO)");
908 if (hoplimit
!= -1 &&
909 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_HOPLIMIT
,
910 (void *)&hoplimit
, sizeof(hoplimit
)))
911 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
914 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
915 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
916 err(1, "UDP setsockopt(IPV6_RTHDR)");
917 #else /* old advanced API */
918 if (smsghdr
.msg_control
&&
919 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
920 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
921 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
924 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
925 err(1, "UDP connect");
927 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
928 err(1, "getsockname");
933 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
935 if (datalen
> sockbufsize
)
936 warnx("you need -b to increase socket buffer size");
937 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
938 sizeof(sockbufsize
)) < 0)
939 err(1, "setsockopt(SO_SNDBUF)");
940 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
941 sizeof(sockbufsize
)) < 0)
942 err(1, "setsockopt(SO_RCVBUF)");
945 if (datalen
> 8 * 1024) /*XXX*/
946 warnx("you need -b to increase socket buffer size");
948 * When pinging the broadcast address, you can get a lot of
949 * answers. Doing something so evil is useful if you are trying
950 * to stress the ethernet, or just want to fill the arp cache
951 * to get some stuff for /etc/ethers.
954 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
960 #ifndef USE_SIN6_SCOPE_ID
961 #ifdef IPV6_RECVPKTINFO
962 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
964 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
965 #else /* old adv. API */
966 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
968 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
970 #endif /* USE_SIN6_SCOPE_ID */
971 #ifdef IPV6_RECVHOPLIMIT
972 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
974 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
975 #else /* old adv. API */
976 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
978 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
981 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
982 (unsigned long)(pingerlen() - 8));
983 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
984 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
986 while (preload
--) /* Fire off them quickies. */
989 (void)signal(SIGINT
, onsignal
);
991 (void)signal(SIGINFO
, onsignal
);
994 if ((options
& F_FLOOD
) == 0) {
995 (void)signal(SIGALRM
, onsignal
);
996 itimer
.it_interval
= interval
;
997 itimer
.it_value
= interval
;
998 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1002 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1003 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1006 signo
= seenalrm
= seenint
= 0;
1015 struct iovec iov
[2];
1017 /* signal handling */
1036 if (options
& F_FLOOD
) {
1039 timeout
.tv_usec
= 10000;
1043 memset(fdmaskp
, 0, fdmasks
);
1045 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1047 if (errno
!= EINTR
) {
1055 fromlen
= sizeof(from
);
1056 m
.msg_name
= (caddr_t
)&from
;
1057 m
.msg_namelen
= sizeof(from
);
1058 memset(&iov
, 0, sizeof(iov
));
1059 iov
[0].iov_base
= (caddr_t
)packet
;
1060 iov
[0].iov_len
= packlen
;
1063 cm
= (struct cmsghdr
*)buf
;
1064 m
.msg_control
= (caddr_t
)buf
;
1065 m
.msg_controllen
= sizeof(buf
);
1067 cc
= recvmsg(s
, &m
, 0);
1069 if (errno
!= EINTR
) {
1074 } else if (cc
== 0) {
1078 * receive control messages only. Process the
1079 * exceptions (currently the only possiblity is
1080 * a path MTU notification.)
1082 if ((mtu
= get_pathmtu(&m
)) > 0) {
1083 if ((options
& F_VERBOSE
) != 0) {
1084 printf("new path MTU (%d) is "
1092 * an ICMPv6 message (probably an echoreply) arrived.
1094 pr_pack(packet
, cc
, &m
);
1096 if (npackets
&& nreceived
>= npackets
)
1100 exit(nreceived
== 0);
1125 * This routine transmits another ping6.
1130 struct itimerval itimer
;
1136 * If we're not transmitting any more packets, change the timer
1137 * to wait two round-trip times if we've received any packets or
1138 * ten seconds if we haven't.
1142 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1143 if (itimer
.it_value
.tv_sec
== 0)
1144 itimer
.it_value
.tv_sec
= 1;
1146 itimer
.it_value
.tv_sec
= MAXWAIT
;
1147 itimer
.it_interval
.tv_sec
= 0;
1148 itimer
.it_interval
.tv_usec
= 0;
1149 itimer
.it_value
.tv_usec
= 0;
1151 (void)signal(SIGALRM
, onint
);
1152 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1157 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1158 * will be added on by the kernel. The ID field is our UNIX process ID,
1159 * and the sequence number is an ascending integer. The first 8 bytes
1160 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1161 * byte-order, to compute the round-trip time.
1168 if (options
& F_FQDN
)
1169 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1170 else if (options
& F_FQDNOLD
)
1172 else if (options
& F_NODEADDR
)
1173 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1174 else if (options
& F_SUPTYPES
)
1177 l
= ICMP6ECHOLEN
+ datalen
;
1185 struct icmp6_hdr
*icp
;
1186 struct iovec iov
[2];
1188 struct icmp6_nodeinfo
*nip
;
1191 if (npackets
&& ntransmitted
>= npackets
)
1192 return(-1); /* no more transmission */
1194 icp
= (struct icmp6_hdr
*)outpack
;
1195 nip
= (struct icmp6_nodeinfo
*)outpack
;
1196 memset(icp
, 0, sizeof(*icp
));
1197 icp
->icmp6_cksum
= 0;
1198 seq
= ntransmitted
++;
1199 CLR(seq
% mx_dup_ck
);
1201 if (options
& F_FQDN
) {
1202 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1203 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1204 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1205 nip
->ni_flags
= htons(0);
1207 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1208 sizeof(nip
->icmp6_ni_nonce
));
1209 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1211 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1212 sizeof(dst
.sin6_addr
));
1213 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1215 } else if (options
& F_FQDNOLD
) {
1216 /* packet format in 03 draft - no Subject data on queries */
1217 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1218 icp
->icmp6_code
= 0; /* code field is always 0 */
1219 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1220 nip
->ni_flags
= htons(0);
1222 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1223 sizeof(nip
->icmp6_ni_nonce
));
1224 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1228 } else if (options
& F_NODEADDR
) {
1229 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1230 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1231 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1232 nip
->ni_flags
= naflags
;
1234 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1235 sizeof(nip
->icmp6_ni_nonce
));
1236 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1238 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1239 sizeof(dst
.sin6_addr
));
1240 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1242 } else if (options
& F_SUPTYPES
) {
1243 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1244 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1245 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1246 /* we support compressed bitmap */
1247 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1249 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1250 sizeof(nip
->icmp6_ni_nonce
));
1251 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1255 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1256 icp
->icmp6_code
= 0;
1257 icp
->icmp6_id
= htons(ident
);
1258 icp
->icmp6_seq
= ntohs(seq
);
1260 (void)gettimeofday((struct timeval
*)
1261 &outpack
[ICMP6ECHOLEN
], NULL
);
1262 cc
= ICMP6ECHOLEN
+ datalen
;
1266 if (pingerlen() != cc
)
1267 errx(1, "internal error; length mismatch");
1270 smsghdr
.msg_name
= (caddr_t
)&dst
;
1271 smsghdr
.msg_namelen
= sizeof(dst
);
1272 memset(&iov
, 0, sizeof(iov
));
1273 iov
[0].iov_base
= (caddr_t
)outpack
;
1274 iov
[0].iov_len
= cc
;
1275 smsghdr
.msg_iov
= iov
;
1276 smsghdr
.msg_iovlen
= 1;
1278 i
= sendmsg(s
, &smsghdr
, 0);
1280 if (i
< 0 || i
!= cc
) {
1283 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1286 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1287 (void)write(STDOUT_FILENO
, &DOT
, 1);
1294 const struct icmp6_hdr
*icp
;
1296 if (ntohs(icp
->icmp6_id
) == ident
)
1304 const struct icmp6_nodeinfo
*nip
;
1306 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1307 nonce
+ sizeof(u_int16_t
),
1308 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1315 dnsdecode(sp
, ep
, base
, buf
, bufsiz
)
1318 const u_char
*base
; /*base for compressed name*/
1324 char cresult
[MAXDNAME
+ 1];
1335 if (i
== 0 || cp
!= *sp
) {
1336 if (strlcat(buf
, ".", bufsiz
) >= bufsiz
)
1337 return NULL
; /*result overrun*/
1343 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1344 /* DNS compression */
1348 comp
= base
+ (i
& 0x3f);
1349 if (dnsdecode(&comp
, cp
, base
, cresult
,
1350 sizeof(cresult
)) == NULL
)
1352 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1353 return NULL
; /*result overrun*/
1355 } else if ((i
& 0x3f) == i
) {
1357 return NULL
; /*source overrun*/
1358 while (i
-- > 0 && cp
< ep
) {
1359 l
= snprintf(cresult
, sizeof(cresult
),
1360 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1361 if (l
>= sizeof(cresult
))
1363 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1364 return NULL
; /*result overrun*/
1368 return NULL
; /*invalid label*/
1371 return NULL
; /*not terminated*/
1379 * Print out the packet, if it came from us. This logic is necessary
1380 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1381 * which arrive ('tis only fair). This permits multiple copies of this
1382 * program to be run without having intermingled output (or statistics!).
1385 pr_pack(buf
, cc
, mhdr
)
1388 struct msghdr
*mhdr
;
1390 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1391 struct icmp6_hdr
*icp
;
1392 struct icmp6_nodeinfo
*ni
;
1395 struct sockaddr
*from
;
1397 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1398 struct in6_pktinfo
*pktinfo
= NULL
;
1399 struct timeval tv
, *tp
;
1400 double triptime
= 0;
1405 char dnsname
[MAXDNAME
+ 1];
1407 (void)gettimeofday(&tv
, NULL
);
1409 if (!mhdr
|| !mhdr
->msg_name
||
1410 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1411 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1412 if (options
& F_VERBOSE
)
1413 warnx("invalid peername\n");
1416 from
= (struct sockaddr
*)mhdr
->msg_name
;
1417 fromlen
= mhdr
->msg_namelen
;
1418 if (cc
< sizeof(struct icmp6_hdr
)) {
1419 if (options
& F_VERBOSE
)
1420 warnx("packet too short (%d bytes) from %s\n", cc
,
1421 pr_addr(from
, fromlen
));
1424 icp
= (struct icmp6_hdr
*)buf
;
1425 ni
= (struct icmp6_nodeinfo
*)buf
;
1428 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1429 warnx("failed to get receiving hop limit");
1432 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1433 warnx("failed to get receiving pakcet information");
1437 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1438 seq
= ntohs(icp
->icmp6_seq
);
1441 tp
= (struct timeval
*)(icp
+ 1);
1443 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1444 ((double)tv
.tv_usec
) / 1000.0;
1446 #if defined(__OpenBSD__) || defined(__NetBSD__)
1447 tsumsq
+= triptime
* triptime
;
1449 if (triptime
< tmin
)
1451 if (triptime
> tmax
)
1455 if (TST(seq
% mx_dup_ck
)) {
1460 SET(seq
% mx_dup_ck
);
1464 if (options
& F_QUIET
)
1467 if (options
& F_FLOOD
)
1468 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1470 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1471 pr_addr(from
, fromlen
), seq
);
1472 (void)printf(" hlim=%d", hoplim
);
1473 if ((options
& F_VERBOSE
) != 0) {
1474 struct sockaddr_in6 dstsa
;
1476 memset(&dstsa
, 0, sizeof(dstsa
));
1477 dstsa
.sin6_family
= AF_INET6
;
1479 dstsa
.sin6_len
= sizeof(dstsa
);
1481 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1482 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1483 (void)printf(" dst=%s",
1484 pr_addr((struct sockaddr
*)&dstsa
,
1488 (void)printf(" time=%g ms", triptime
);
1490 (void)printf("(DUP!)");
1491 /* check the data */
1492 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1493 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1494 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1496 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1501 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1502 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1504 if (TST(seq
% mx_dup_ck
)) {
1509 SET(seq
% mx_dup_ck
);
1513 if (options
& F_QUIET
)
1516 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1518 switch (ntohs(ni
->ni_code
)) {
1519 case ICMP6_NI_SUCCESS
:
1521 case ICMP6_NI_REFUSED
:
1522 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1524 case ICMP6_NI_UNKNOWN
:
1525 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1528 printf("unknown code 0x%x, type 0x%x",
1529 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1533 switch (ntohs(ni
->ni_qtype
)) {
1535 printf("NodeInfo NOOP");
1537 case NI_QTYPE_SUPTYPES
:
1538 pr_suptypes(ni
, end
- (u_char
*)ni
);
1540 case NI_QTYPE_NODEADDR
:
1541 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1544 default: /* XXX: for backward compatibility */
1545 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1546 if (buf
[off
+ ICMP6_NIRLEN
] ==
1547 cc
- off
- ICMP6_NIRLEN
- 1)
1552 cp
++; /* skip length */
1554 safeputc(*cp
& 0xff);
1560 if (dnsdecode((const u_char
**)&cp
, end
,
1561 (const u_char
*)(ni
+ 1), dnsname
,
1562 sizeof(dnsname
)) == NULL
) {
1567 * name-lookup special handling for
1570 if (cp
+ 1 <= end
&& !*cp
&&
1571 strlen(dnsname
) > 0) {
1572 dnsname
[strlen(dnsname
) - 1] = '\0';
1575 printf("%s%s", i
> 0 ? "," : "",
1579 if (options
& F_VERBOSE
) {
1583 (void)printf(" ("); /*)*/
1585 switch (ni
->ni_code
) {
1586 case ICMP6_NI_REFUSED
:
1587 (void)printf("refused");
1590 case ICMP6_NI_UNKNOWN
:
1591 (void)printf("unknwon qtype");
1596 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1597 /* case of refusion, unknown */
1602 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1605 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1606 (void)printf("TTL=%d:meaningless",
1610 (void)printf("TTL=%d:invalid",
1613 (void)printf("TTL=%d", ttl
);
1623 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1632 if (buf
[off
+ ICMP6_NIRLEN
] !=
1633 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1636 (void)printf("invalid namelen:%d/%lu",
1637 buf
[off
+ ICMP6_NIRLEN
],
1638 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1648 /* We've got something other than an ECHOREPLY */
1649 if (!(options
& F_VERBOSE
))
1651 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1655 if (!(options
& F_FLOOD
)) {
1656 (void)putchar('\n');
1657 if (options
& F_VERBOSE
)
1659 (void)fflush(stdout
);
1666 struct msghdr
*mhdr
;
1670 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1671 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1672 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1675 switch (cm
->cmsg_type
) {
1677 printf(" HbH Options: ");
1678 pr_ip6opt(CMSG_DATA(cm
));
1681 #ifdef IPV6_RTHDRDSTOPTS
1682 case IPV6_RTHDRDSTOPTS
:
1684 printf(" Dst Options: ");
1685 pr_ip6opt(CMSG_DATA(cm
));
1688 printf(" Routing: ");
1689 pr_rthdr(CMSG_DATA(cm
));
1695 #ifdef USE_RFC2292BIS
1697 pr_ip6opt(void *extbuf
)
1699 struct ip6_hbh
*ext
;
1708 ext
= (struct ip6_hbh
*)extbuf
;
1709 extlen
= (ext
->ip6h_len
+ 1) * 8;
1710 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1711 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1715 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1716 &type
, &len
, &databuf
);
1717 if (currentlen
== -1)
1721 * Note that inet6_opt_next automatically skips any padding
1726 offset
= inet6_opt_get_val(databuf
, offset
,
1727 &value4
, sizeof(value4
));
1728 printf(" Jumbo Payload Opt: Length %u\n",
1729 (u_int32_t
)ntohl(value4
));
1731 case IP6OPT_ROUTER_ALERT
:
1733 offset
= inet6_opt_get_val(databuf
, offset
,
1734 &value2
, sizeof(value2
));
1735 printf(" Router Alert Opt: Type %u\n",
1739 printf(" Received Opt %u len %lu\n",
1740 type
, (unsigned long)len
);
1746 #else /* !USE_RFC2292BIS */
1749 pr_ip6opt(void *extbuf
)
1754 #endif /* USE_RFC2292BIS */
1756 #ifdef USE_RFC2292BIS
1758 pr_rthdr(void *extbuf
)
1760 struct in6_addr
*in6
;
1761 char ntopbuf
[INET6_ADDRSTRLEN
];
1762 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1765 /* print fixed part of the header */
1766 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1767 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1768 if ((segments
= inet6_rth_segments(extbuf
)) >= 0)
1769 printf("%d segments, ", segments
);
1771 printf("segments unknown, ");
1772 printf("%d left\n", rh
->ip6r_segleft
);
1774 for (i
= 0; i
< segments
; i
++) {
1775 in6
= inet6_rth_getaddr(extbuf
, i
);
1777 printf(" [%d]<NULL>\n", i
);
1779 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1781 strncpy(ntopbuf
, "?", sizeof(ntopbuf
));
1782 printf(" [%d]%s\n", i
, ntopbuf
);
1790 #else /* !USE_RFC2292BIS */
1793 pr_rthdr(void *extbuf
)
1798 #endif /* USE_RFC2292BIS */
1801 pr_bitrange(v
, s
, ii
)
1811 /* shift till we have 0x01 */
1812 if ((v
& 0x01) == 0) {
1814 printf("-%u", s
+ off
- 1);
1825 case 0x04: case 0x0c:
1836 /* we have 0x01 with us */
1837 for (i
= 0; i
< 32 - off
; i
++) {
1838 if ((v
& (0x01 << i
)) == 0)
1842 printf(" %u", s
+ off
);
1850 pr_suptypes(ni
, nilen
)
1851 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be SUPTYPES */
1856 const u_char
*cp
, *end
;
1859 u_int16_t words
; /*32bit count*/
1862 #define MAXQTYPES (1 << 16)
1866 cp
= (u_char
*)(ni
+ 1);
1867 end
= ((u_char
*)ni
) + nilen
;
1871 printf("NodeInfo Supported Qtypes");
1872 if (options
& F_VERBOSE
) {
1873 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1874 printf(", compressed bitmap");
1876 printf(", raw bitmap");
1880 clen
= (size_t)(end
- cp
);
1881 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1882 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1888 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1890 memcpy(&cbit
, cp
, sizeof(cbit
));
1891 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1895 clen
= ntohs(cbit
.words
) * sizeof(v
);
1896 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1901 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1902 memcpy(&v
, cp
+ off
, sizeof(v
));
1903 v
= (u_int32_t
)ntohl(v
);
1904 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1906 /* flush the remaining bits */
1907 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1911 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1912 cur
+= ntohs(cbit
.skip
) * 32;
1917 pr_nodeaddr(ni
, nilen
)
1918 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be NODEADDR */
1921 u_char
*cp
= (u_char
*)(ni
+ 1);
1922 char ntop_buf
[INET6_ADDRSTRLEN
];
1925 nilen
-= sizeof(struct icmp6_nodeinfo
);
1927 if (options
& F_VERBOSE
) {
1928 switch (ni
->ni_code
) {
1929 case ICMP6_NI_REFUSED
:
1930 (void)printf("refused");
1932 case ICMP6_NI_UNKNOWN
:
1933 (void)printf("unknown qtype");
1936 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1937 (void)printf(" truncated");
1941 printf(" no address\n");
1944 * In icmp-name-lookups 05 and later, TTL of each returned address
1945 * is contained in the resposne. We try to detect the version
1946 * by the length of the data, but note that the detection algorithm
1947 * is incomplete. We assume the latest draft by default.
1949 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1955 /* XXX: alignment? */
1956 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1957 cp
+= sizeof(u_int32_t
);
1958 nilen
-= sizeof(u_int32_t
);
1961 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1963 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
1964 printf(" %s", ntop_buf
);
1966 if (ttl
== 0xffffffff) {
1968 * XXX: can this convention be applied to all
1969 * type of TTL (i.e. non-ND TTL)?
1971 printf("(TTL=infty)");
1974 printf("(TTL=%u)", ttl
);
1978 nilen
-= sizeof(struct in6_addr
);
1979 cp
+= sizeof(struct in6_addr
);
1985 struct msghdr
*mhdr
;
1989 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1990 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1991 if (cm
->cmsg_len
== 0)
1994 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1995 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1996 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1997 return(*(int *)CMSG_DATA(cm
));
2003 struct in6_pktinfo
*
2004 get_rcvpktinfo(mhdr
)
2005 struct msghdr
*mhdr
;
2009 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2010 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2011 if (cm
->cmsg_len
== 0)
2014 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2015 cm
->cmsg_type
== IPV6_PKTINFO
&&
2016 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2017 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2025 struct msghdr
*mhdr
;
2027 #ifdef IPV6_RECVPATHMTU
2029 struct ip6_mtuinfo
*mtuctl
= NULL
;
2031 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2032 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2033 if (cm
->cmsg_len
== 0)
2036 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2037 cm
->cmsg_type
== IPV6_PATHMTU
&&
2038 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2039 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2042 * If the notified destination is different from
2043 * the one we are pinging, just ignore the info.
2044 * We check the scope ID only when both notified value
2045 * and our own value have non-0 values, because we may
2046 * have used the default scope zone ID for sending,
2047 * in which case the scope ID value is 0.
2049 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2051 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2052 dst
.sin6_scope_id
&&
2053 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2054 dst
.sin6_scope_id
)) {
2055 if ((options
& F_VERBOSE
) != 0) {
2056 printf("path MTU for %s is notified. "
2058 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2059 sizeof(mtuctl
->ip6m_addr
)));
2065 * Ignore an invalid MTU. XXX: can we just believe
2068 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2071 /* notification for our destination. return the MTU. */
2072 return((int)mtuctl
->ip6m_mtu
);
2084 static int firsttime
= 1;
2088 int oldlen
= smsghdr
.msg_controllen
;
2089 char *oldbuf
= smsghdr
.msg_control
;
2091 /* XXX: We need to enlarge control message buffer */
2092 firsttime
= 0; /* prevent further enlargement */
2094 smsghdr
.msg_controllen
= oldlen
+ CMSG_SPACE(sizeof(int));
2095 if ((smsghdr
.msg_control
=
2096 (char *)malloc(smsghdr
.msg_controllen
)) == NULL
)
2097 err(1, "set_pathmtu: malloc");
2098 cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&smsghdr
);
2099 cm
->cmsg_len
= CMSG_LEN(sizeof(int));
2100 cm
->cmsg_level
= IPPROTO_IPV6
;
2101 cm
->cmsg_type
= IPV6_USE_MTU
;
2103 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&smsghdr
, cm
);
2105 memcpy((void *)cm
, (void *)oldbuf
, oldlen
);
2111 * look for a cmsgptr that points MTU structure.
2112 * XXX: this procedure seems redundant at this moment, but we'd better
2113 * keep the code generic enough for future extensions.
2115 for (cm
= CMSG_FIRSTHDR(&smsghdr
); cm
;
2116 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&smsghdr
, cm
)) {
2117 if (cm
->cmsg_len
== 0) /* XXX: paranoid check */
2118 errx(1, "set_pathmtu: internal error");
2120 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2121 cm
->cmsg_type
== IPV6_USE_MTU
&&
2122 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2127 errx(1, "set_pathmtu: internal error: no space for path MTU");
2129 *(int *)CMSG_DATA(cm
) = mtu
;
2135 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2140 register struct timeval
*out
, *in
;
2142 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2144 out
->tv_usec
+= 1000000;
2146 out
->tv_sec
-= in
->tv_sec
;
2160 (void)signal(SIGINT
, SIG_DFL
);
2161 (void)kill(getpid(), SIGINT
);
2169 * Print out statistics.
2175 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2176 (void)printf("%ld packets transmitted, ", ntransmitted
);
2177 (void)printf("%ld packets received, ", nreceived
);
2179 (void)printf("+%ld duplicates, ", nrepeats
);
2181 if (nreceived
> ntransmitted
)
2182 (void)printf("-- somebody's printing up packets!");
2184 (void)printf("%d%% packet loss",
2185 (int) (((ntransmitted
- nreceived
) * 100) /
2188 (void)putchar('\n');
2189 if (nreceived
&& timing
) {
2190 /* Only display average to microseconds */
2191 double num
= nreceived
+ nrepeats
;
2192 double avg
= tsum
/ num
;
2193 #if defined(__OpenBSD__) || defined(__NetBSD__)
2194 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2196 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2197 tmin
, avg
, tmax
, dev
);
2200 "round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
2203 (void)fflush(stdout
);
2205 (void)fflush(stdout
);
2209 static char *niqcode
[] = {
2211 "DNS label", /*or empty*/
2216 static char *nircode
[] = {
2217 "Success", "Refused", "Unknown",
2223 * Print a descriptive string about an ICMP header.
2227 struct icmp6_hdr
*icp
;
2230 char ntop_buf
[INET6_ADDRSTRLEN
];
2231 struct nd_redirect
*red
;
2232 struct icmp6_nodeinfo
*ni
;
2233 char dnsname
[MAXDNAME
+ 1];
2237 switch (icp
->icmp6_type
) {
2238 case ICMP6_DST_UNREACH
:
2239 switch (icp
->icmp6_code
) {
2240 case ICMP6_DST_UNREACH_NOROUTE
:
2241 (void)printf("No Route to Destination\n");
2243 case ICMP6_DST_UNREACH_ADMIN
:
2244 (void)printf("Destination Administratively "
2247 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2248 (void)printf("Destination Unreachable Beyond Scope\n");
2250 case ICMP6_DST_UNREACH_ADDR
:
2251 (void)printf("Destination Host Unreachable\n");
2253 case ICMP6_DST_UNREACH_NOPORT
:
2254 (void)printf("Destination Port Unreachable\n");
2257 (void)printf("Destination Unreachable, Bad Code: %d\n",
2261 /* Print returned IP header information */
2262 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2264 case ICMP6_PACKET_TOO_BIG
:
2265 (void)printf("Packet too big mtu = %d\n",
2266 (int)ntohl(icp
->icmp6_mtu
));
2267 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2269 case ICMP6_TIME_EXCEEDED
:
2270 switch (icp
->icmp6_code
) {
2271 case ICMP6_TIME_EXCEED_TRANSIT
:
2272 (void)printf("Time to live exceeded\n");
2274 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2275 (void)printf("Frag reassembly time exceeded\n");
2278 (void)printf("Time exceeded, Bad Code: %d\n",
2282 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2284 case ICMP6_PARAM_PROB
:
2285 (void)printf("Parameter problem: ");
2286 switch (icp
->icmp6_code
) {
2287 case ICMP6_PARAMPROB_HEADER
:
2288 (void)printf("Erroneous Header ");
2290 case ICMP6_PARAMPROB_NEXTHEADER
:
2291 (void)printf("Unknown Nextheader ");
2293 case ICMP6_PARAMPROB_OPTION
:
2294 (void)printf("Unrecognized Option ");
2297 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2300 (void)printf("pointer = 0x%02x\n",
2301 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2302 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2304 case ICMP6_ECHO_REQUEST
:
2305 (void)printf("Echo Request");
2306 /* XXX ID + Seq + Data */
2308 case ICMP6_ECHO_REPLY
:
2309 (void)printf("Echo Reply");
2310 /* XXX ID + Seq + Data */
2312 case ICMP6_MEMBERSHIP_QUERY
:
2313 (void)printf("Listener Query");
2315 case ICMP6_MEMBERSHIP_REPORT
:
2316 (void)printf("Listener Report");
2318 case ICMP6_MEMBERSHIP_REDUCTION
:
2319 (void)printf("Listener Done");
2321 case ND_ROUTER_SOLICIT
:
2322 (void)printf("Router Solicitation");
2324 case ND_ROUTER_ADVERT
:
2325 (void)printf("Router Advertisement");
2327 case ND_NEIGHBOR_SOLICIT
:
2328 (void)printf("Neighbor Solicitation");
2330 case ND_NEIGHBOR_ADVERT
:
2331 (void)printf("Neighbor Advertisement");
2334 red
= (struct nd_redirect
*)icp
;
2335 (void)printf("Redirect\n");
2336 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2338 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2339 (void)printf("Destination: %s", ntop_buf
);
2340 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2342 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2343 (void)printf(" New Target: %s", ntop_buf
);
2345 case ICMP6_NI_QUERY
:
2346 (void)printf("Node Information Query");
2347 /* XXX ID + Seq + Data */
2348 ni
= (struct icmp6_nodeinfo
*)icp
;
2349 l
= end
- (u_char
*)(ni
+ 1);
2351 switch (ntohs(ni
->ni_qtype
)) {
2353 (void)printf("NOOP");
2355 case NI_QTYPE_SUPTYPES
:
2356 (void)printf("Supported qtypes");
2359 (void)printf("DNS name");
2361 case NI_QTYPE_NODEADDR
:
2362 (void)printf("nodeaddr");
2364 case NI_QTYPE_IPV4ADDR
:
2365 (void)printf("IPv4 nodeaddr");
2368 (void)printf("unknown qtype");
2371 if (options
& F_VERBOSE
) {
2372 switch (ni
->ni_code
) {
2373 case ICMP6_NI_SUBJ_IPV6
:
2374 if (l
== sizeof(struct in6_addr
) &&
2375 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2376 sizeof(ntop_buf
)) != NULL
) {
2377 (void)printf(", subject=%s(%s)",
2378 niqcode
[ni
->ni_code
], ntop_buf
);
2381 /* backward compat to -W */
2382 (void)printf(", oldfqdn");
2384 (void)printf(", invalid");
2388 case ICMP6_NI_SUBJ_FQDN
:
2389 if (end
== (u_char
*)(ni
+ 1)) {
2390 (void)printf(", no subject");
2393 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2394 cp
= (const u_char
*)(ni
+ 1);
2395 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2396 sizeof(dnsname
)) != NULL
)
2397 printf("(%s)", dnsname
);
2399 printf("(invalid)");
2401 case ICMP6_NI_SUBJ_IPV4
:
2402 if (l
== sizeof(struct in_addr
) &&
2403 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2404 sizeof(ntop_buf
)) != NULL
) {
2405 (void)printf(", subject=%s(%s)",
2406 niqcode
[ni
->ni_code
], ntop_buf
);
2408 (void)printf(", invalid");
2411 (void)printf(", invalid");
2416 case ICMP6_NI_REPLY
:
2417 (void)printf("Node Information Reply");
2418 /* XXX ID + Seq + Data */
2419 ni
= (struct icmp6_nodeinfo
*)icp
;
2421 switch (ntohs(ni
->ni_qtype
)) {
2423 (void)printf("NOOP");
2425 case NI_QTYPE_SUPTYPES
:
2426 (void)printf("Supported qtypes");
2429 (void)printf("DNS name");
2431 case NI_QTYPE_NODEADDR
:
2432 (void)printf("nodeaddr");
2434 case NI_QTYPE_IPV4ADDR
:
2435 (void)printf("IPv4 nodeaddr");
2438 (void)printf("unknown qtype");
2441 if (options
& F_VERBOSE
) {
2442 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2443 printf(", invalid");
2445 printf(", %s", nircode
[ni
->ni_code
]);
2449 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2455 * Print an IP6 header.
2459 struct ip6_hdr
*ip6
;
2461 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2463 char ntop_buf
[INET6_ADDRSTRLEN
];
2465 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2466 tc
= (tc
>> 4) & 0x0f;
2467 tc
|= (ip6
->ip6_vfc
<< 4);
2469 printf("Vr TC Flow Plen Nxt Hlim\n");
2470 printf(" %1x %02x %05x %04x %02x %02x\n",
2471 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2472 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2473 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2474 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2475 printf("%s->", ntop_buf
);
2476 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2477 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2478 printf("%s\n", ntop_buf
);
2483 * Return an ascii host address as a dotted quad and optionally with
2487 pr_addr(addr
, addrlen
)
2488 struct sockaddr
*addr
;
2491 static char buf
[NI_MAXHOST
];
2494 #ifdef NI_WITHSCOPEID
2495 flag
= NI_WITHSCOPEID
;
2499 if ((options
& F_HOSTNAME
) == 0)
2500 flag
|= NI_NUMERICHOST
;
2502 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2510 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2514 struct ip6_hdr
*ip6
;
2517 u_char
*cp
= (u_char
*)ip6
, nh
;
2520 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2525 hlen
= sizeof(*ip6
);
2529 while (end
- cp
>= 8) {
2531 case IPPROTO_HOPOPTS
:
2533 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2534 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2536 case IPPROTO_DSTOPTS
:
2538 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2539 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2541 case IPPROTO_FRAGMENT
:
2543 hlen
= sizeof(struct ip6_frag
);
2544 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2546 case IPPROTO_ROUTING
:
2548 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2549 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2554 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2555 nh
= ((struct ah
*)cp
)->ah_nxt
;
2558 case IPPROTO_ICMPV6
:
2559 printf("ICMP6: type = %d, code = %d\n",
2566 printf("TCP: from port %u, to port %u (decimal)\n",
2567 (*cp
* 256 + *(cp
+ 1)),
2568 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2571 printf("UDP: from port %u, to port %u (decimal)\n",
2572 (*cp
* 256 + *(cp
+ 1)),
2573 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2576 printf("Unknown Header(%d)\n", nh
);
2580 if ((cp
+= hlen
) >= end
)
2598 register int ii
, jj
, kk
;
2602 for (cp
= patp
; *cp
; cp
++)
2604 errx(1, "patterns must be specified as hex digits");
2606 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2607 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2608 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2609 &pat
[13], &pat
[14], &pat
[15]);
2614 kk
<= MAXDATALEN
- (8 + sizeof(struct timeval
) + ii
);
2616 for (jj
= 0; jj
< ii
; ++jj
)
2617 bp
[jj
+ kk
] = pat
[jj
];
2618 if (!(options
& F_QUIET
)) {
2619 (void)printf("PATTERN: 0x");
2620 for (jj
= 0; jj
< ii
; ++jj
)
2621 (void)printf("%02x", bp
[jj
] & 0xFF);
2627 #ifdef IPSEC_POLICY_IPSEC
2629 setpolicy(so
, policy
)
2636 return 0; /* ignore */
2638 buf
= ipsec_set_policy(policy
, strlen(policy
));
2640 errx(1, "%s", ipsec_strerror());
2641 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2642 ipsec_get_policylen(buf
)) < 0)
2643 warnx("Unable to set IPSec policy");
2658 u_int8_t digest
[16];
2661 char hbuf
[NI_MAXHOST
];
2662 struct in6_addr in6
;
2664 p
= strchr(name
, '.');
2666 p
= name
+ strlen(name
);
2668 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2669 return NULL
; /*label too long*/
2670 strncpy(hbuf
, name
, l
);
2671 hbuf
[(int)l
] = '\0';
2673 for (q
= name
; *q
; q
++) {
2678 /* generate 8 bytes of pseudo-random value. */
2679 bzero(&ctxt
, sizeof(ctxt
));
2682 MD5Update(&ctxt
, &c
, sizeof(c
));
2683 MD5Update(&ctxt
, name
, l
);
2684 MD5Final(digest
, &ctxt
);
2686 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2687 return NULL
; /*XXX*/
2688 bcopy(digest
, &in6
.s6_addr
[12], 4);
2690 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2693 return strdup(hbuf
);
2699 (void)fprintf(stderr
,
2700 "usage: ping6 [-dfH"
2701 #ifdef IPV6_USE_MIN_MTU
2705 #ifdef IPV6_REACHCONF
2709 #ifdef IPSEC_POLICY_IPSEC
2715 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
2716 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2718 "\t[-s packetsize] [-h hoplimit] [hops...] host\n");