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
70 #include <sys/cdefs.h>
72 __unused
static char copyright
[] =
73 "@(#) Copyright (c) 1989, 1993\n\
74 The Regents of the University of California. All rights reserved.\n";
79 static char sccsid
[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
81 __unused
static const char rcsid
[] =
82 "$FreeBSD: src/sbin/ping6/ping6.c,v 1.4.2.6 2001/07/06 08:56:47 ume Exp $";
86 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
87 * measure round-trip-delays and packet loss across network paths.
91 * U. S. Army Ballistic Research Laboratory
95 * Public Domain. Distribution Unlimited.
97 * More statistics could always be gathered.
98 * This program has to run SUID to ROOT to access the ICMP socket.
102 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
103 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
104 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
105 * network attached to 1 or more interfaces)
108 #include <sys/param.h>
110 #include <sys/socket.h>
111 #include <sys/time.h>
114 #include <net/route.h>
116 #include <netinet/in.h>
117 #include <netinet/ip6.h>
118 #include <netinet/icmp6.h>
119 #include <arpa/inet.h>
120 #include <arpa/nameser.h>
127 #if defined(__OpenBSD__) || defined(__NetBSD__)
137 #include <netinet6/ah.h>
138 #include <netinet6/ipsec.h>
141 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
147 #define MAXPACKETLEN 131072
149 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
150 #define ICMP6ECHOTMLEN sizeof(struct timeval)
151 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
152 /* FQDN case, 64 bits of nonce + 32 bits ttl */
153 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
154 #define EXTRA 256 /* for AH and various other headers. weird. */
155 #define DEFDATALEN ICMP6ECHOTMLEN
156 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
157 #define NROUTES 9 /* number of record route slots */
159 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
160 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
161 #define SET(bit) (A(bit) |= B(bit))
162 #define CLR(bit) (A(bit) &= (~B(bit)))
163 #define TST(bit) (A(bit) & B(bit))
165 #define F_FLOOD 0x0001
166 #define F_INTERVAL 0x0002
167 #define F_PINGFILLED 0x0008
168 #define F_QUIET 0x0010
169 #define F_RROUTE 0x0020
170 #define F_SO_DEBUG 0x0040
171 #define F_VERBOSE 0x0100
173 #ifdef IPSEC_POLICY_IPSEC
174 #define F_POLICY 0x0400
176 #define F_AUTHHDR 0x0200
177 #define F_ENCRYPT 0x0400
178 #endif /*IPSEC_POLICY_IPSEC*/
180 #define F_NODEADDR 0x0800
181 #define F_FQDN 0x1000
182 #define F_INTERFACE 0x2000
183 #define F_SRCADDR 0x4000
184 #ifdef IPV6_REACHCONF
185 #define F_REACHCONF 0x8000
187 #define F_HOSTNAME 0x10000
188 #define F_FQDNOLD 0x20000
189 #define F_NIGROUP 0x40000
190 #define F_SUPTYPES 0x80000
191 #define F_NOMINMTU 0x100000
192 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
195 #define IN6LEN sizeof(struct in6_addr)
196 #define SA6LEN sizeof(struct sockaddr_in6)
197 #define DUMMY_PORT 10101
199 #define SIN6(s) ((struct sockaddr_in6 *)(s))
202 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
203 * number of received sequence numbers we can keep track of. Change 128
204 * to 8192 for complete accuracy...
206 #define MAX_DUP_CHK (8 * 8192)
207 int mx_dup_ck
= MAX_DUP_CHK
;
208 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
210 struct addrinfo
*res
;
211 struct sockaddr_in6 dst
; /* who to ping6 */
212 struct sockaddr_in6 src
; /* src addr of this packet */
213 int datalen
= DEFDATALEN
;
214 int s
; /* socket file descriptor */
215 u_char outpack
[MAXPACKETLEN
];
216 char BSPACE
= '\b'; /* characters written for flood */
219 int ident
; /* process id to identify our packets */
220 u_int8_t nonce
[8]; /* nonce field for node information */
221 struct in6_addr srcaddr
;
222 int hoplimit
= -1; /* hoplimit */
223 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
226 long npackets
; /* max packets to transmit */
227 long nreceived
; /* # of packets we got back */
228 long nrepeats
; /* number of duplicates */
229 long ntransmitted
; /* sequence # for outbound packets = #sent */
230 struct timeval interval
= {1, 0}; /* interval between packets */
233 int timing
; /* flag to do timing */
234 double tmin
= 999999999.0; /* minimum round trip time */
235 double tmax
= 0.0; /* maximum round trip time */
236 double tsum
= 0.0; /* sum of all times, for doing average */
237 #if defined(__OpenBSD__) || defined(__NetBSD__)
238 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
241 /* for node addresses */
244 /* for ancillary data(advanced API) */
245 struct msghdr smsghdr
;
246 struct iovec smsgiov
;
250 volatile sig_atomic_t seenalrm
;
251 volatile sig_atomic_t seenint
;
253 volatile sig_atomic_t seeninfo
;
256 int main
__P((int, char *[]));
257 void fill
__P((char *, char *));
258 int get_hoplim
__P((struct msghdr
*));
259 int get_pathmtu
__P((struct msghdr
*));
260 void set_pathmtu
__P((int));
261 struct in6_pktinfo
*get_rcvpktinfo
__P((struct msghdr
*));
262 void onsignal
__P((int));
263 void retransmit
__P((void));
264 void onint
__P((int));
265 size_t pingerlen
__P((void));
266 int pinger
__P((void));
267 const char *pr_addr
__P((struct sockaddr
*, int));
268 void pr_icmph
__P((struct icmp6_hdr
*, u_char
*));
269 void pr_iph
__P((struct ip6_hdr
*));
270 void pr_suptypes
__P((struct icmp6_nodeinfo
*, size_t));
271 void pr_nodeaddr
__P((struct icmp6_nodeinfo
*, int));
272 int myechoreply
__P((const struct icmp6_hdr
*));
273 int mynireply
__P((const struct icmp6_nodeinfo
*));
274 char *dnsdecode
__P((const u_char
**, const u_char
*, const u_char
*,
276 void pr_pack
__P((u_char
*, int, struct msghdr
*));
277 void pr_exthdrs
__P((struct msghdr
*));
278 void pr_ip6opt
__P((void *));
279 void pr_rthdr
__P((void *));
280 int pr_bitrange
__P((u_int32_t
, int, int));
281 void pr_retip
__P((struct ip6_hdr
*, u_char
*));
282 void summary
__P((void));
283 void tvsub
__P((struct timeval
*, struct timeval
*));
284 int setpolicy
__P((int, char *));
285 char *nigroup
__P((char *));
286 void usage
__P((void));
293 struct itimerval itimer
;
294 struct sockaddr_in6 from
;
295 struct timeval timeout
, *tv
;
296 struct addrinfo hints
;
300 int ch
, fromlen
, hold
, packlen
, preload
, optval
, ret_ga
;
301 u_char
*datap
, *packet
;
302 char *e
, *target
, *ifname
= NULL
;
304 struct cmsghdr
*scmsgp
= NULL
;
307 struct in6_pktinfo
*pktinfo
= NULL
;
308 #ifdef USE_RFC2292BIS
309 struct ip6_rthdr
*rthdr
= NULL
;
311 #ifdef IPSEC_POLICY_IPSEC
312 char *policy_in
= NULL
;
313 char *policy_out
= NULL
;
318 /* just to be sure */
319 memset(&smsghdr
, 0, sizeof(&smsghdr
));
320 memset(&smsgiov
, 0, sizeof(&smsgiov
));
323 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
327 #ifdef IPSEC_POLICY_IPSEC
331 #endif /*IPSEC_POLICY_IPSEC*/
333 while ((ch
= getopt(argc
, argv
,
334 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS
)) != -1) {
341 options
&= ~F_NOUSERDATA
;
342 options
|= F_NODEADDR
;
343 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
346 naflags
|= NI_NODEADDR_FLAG_ALL
;
350 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
354 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
358 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
362 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
364 case 'A': /* experimental. not in the spec */
365 #ifdef NI_NODEADDR_FLAG_ANYCAST
366 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
370 "-a A is not supported on the platform");
381 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
382 sockbufsize
= atoi(optarg
);
385 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
389 npackets
= strtol(optarg
, &e
, 10);
390 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
392 "illegal number of packets -- %s", optarg
);
395 options
|= F_SO_DEBUG
;
400 errx(1, "Must be superuser to flood ping");
403 setbuf(stdout
, (char *)NULL
);
406 options
|= F_HOSTNAME
;
408 case 'h': /* hoplimit */
409 hoplimit
= strtol(optarg
, &e
, 10);
410 if (255 < hoplimit
|| hoplimit
< -1)
412 "illegal hoplimit -- %s", optarg
);
416 options
|= F_INTERFACE
;
417 #ifndef USE_SIN6_SCOPE_ID
421 case 'i': /* wait between sending packets */
422 intval
= strtod(optarg
, &e
);
423 if (*optarg
== '\0' || *e
!= '\0')
424 errx(1, "illegal timing interval %s", optarg
);
425 if (intval
< 1 && getuid()) {
426 errx(1, "%s: only root may use interval < 1s",
429 interval
.tv_sec
= (long)intval
;
431 (long)((intval
- interval
.tv_sec
) * 1000000);
432 if (interval
.tv_sec
< 0)
433 errx(1, "illegal timing interval %s", optarg
);
434 /* less than 1/hz does not make sense */
435 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
436 warnx("too small interval, raised to 0.01");
437 interval
.tv_usec
= 10000;
439 options
|= F_INTERVAL
;
444 errx(1, "Must be superuser to preload");
446 preload
= strtol(optarg
, &e
, 10);
447 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
448 errx(1, "illegal preload value -- %s", optarg
);
451 #ifdef IPV6_USE_MIN_MTU
452 options
|= F_NOMINMTU
;
455 errx(1, "-%c is not supported on this platform", ch
);
459 options
&= ~F_HOSTNAME
;
462 options
|= F_NIGROUP
;
464 case 'p': /* fill buffer with user pattern */
465 options
|= F_PINGFILLED
;
466 fill((char *)datap
, optarg
);
472 #ifdef IPV6_REACHCONF
473 options
|= F_REACHCONF
;
476 errx(1, "-R is not supported in this configuration");
479 /* XXX: use getaddrinfo? */
480 if (inet_pton(AF_INET6
, optarg
, (void *)&srcaddr
) != 1)
481 errx(1, "invalid IPv6 address: %s", optarg
);
482 options
|= F_SRCADDR
;
485 case 's': /* size of packet to send */
486 datalen
= strtol(optarg
, &e
, 10);
487 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
488 errx(1, "illegal datalen value -- %s", optarg
);
489 if (datalen
> MAXDATALEN
) {
491 "datalen value too large, maximum is %d",
496 options
&= ~F_NOUSERDATA
;
497 options
|= F_SUPTYPES
;
500 options
|= F_VERBOSE
;
503 options
&= ~F_NOUSERDATA
;
507 options
&= ~F_NOUSERDATA
;
508 options
|= F_FQDNOLD
;
511 #ifdef IPSEC_POLICY_IPSEC
514 if (!strncmp("in", optarg
, 2)) {
515 if ((policy_in
= strdup(optarg
)) == NULL
)
517 } else if (!strncmp("out", optarg
, 3)) {
518 if ((policy_out
= strdup(optarg
)) == NULL
)
521 errx(1, "invalid security policy");
525 options
|= F_AUTHHDR
;
528 options
|= F_ENCRYPT
;
530 #endif /*IPSEC_POLICY_IPSEC*/
546 #ifdef IPV6_RECVRTHDR /* 2292bis */
547 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
550 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
553 errx(1, "too many intermediate hops");
559 if (options
& F_NIGROUP
) {
560 target
= nigroup(argv
[argc
- 1]);
561 if (target
== NULL
) {
566 target
= argv
[argc
- 1];
569 bzero(&hints
, sizeof(struct addrinfo
));
570 hints
.ai_flags
= AI_CANONNAME
;
571 hints
.ai_family
= AF_INET6
;
572 hints
.ai_socktype
= SOCK_RAW
;
573 hints
.ai_protocol
= IPPROTO_ICMPV6
;
575 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
577 fprintf(stderr
, "ping6: %s\n", gai_strerror(ret_ga
));
580 if (res
->ai_canonname
)
581 hostname
= res
->ai_canonname
;
586 errx(1, "getaddrinfo failed");
588 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
590 res
->ai_socktype
= SOCK_RAW
;
591 res
->ai_protocol
= IPPROTO_ICMPV6
;
593 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
594 res
->ai_protocol
)) < 0)
598 * let the kerel pass extension headers of incoming packets,
599 * for privileged socket options
601 if ((options
& F_VERBOSE
) != 0) {
604 #ifdef IPV6_RECVHOPOPTS
605 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
607 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
608 #else /* old adv. API */
609 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
611 err(1, "setsockopt(IPV6_HOPOPTS)");
613 #ifdef IPV6_RECVDSTOPTS
614 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
616 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
617 #else /* old adv. API */
618 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
620 err(1, "setsockopt(IPV6_DSTOPTS)");
622 #ifdef IPV6_RECVRTHDRDSTOPTS
623 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
625 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
629 /* revoke root privilege */
633 if (options
& F_FLOOD
&& options
& F_INTERVAL
)
634 errx(1, "-f and -i incompatible options");
636 if ((options
& F_NOUSERDATA
) == 0) {
637 if (datalen
>= sizeof(struct timeval
)) {
638 /* we can time transfer */
642 /* in F_VERBOSE case, we may get non-echoreply packets*/
643 if (options
& F_VERBOSE
)
644 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
646 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
648 /* suppress timing for node information query */
651 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
654 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
655 err(1, "Unable to allocate packet");
656 if (!(options
& F_PINGFILLED
))
657 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
660 ident
= getpid() & 0xFFFF;
662 gettimeofday(&timeout
, NULL
);
663 srand((unsigned int)(timeout
.tv_sec
^ timeout
.tv_usec
^ (long)ident
));
664 memset(nonce
, 0, sizeof(nonce
));
665 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
666 *((int *)&nonce
[i
]) = rand();
668 memset(nonce
, 0, sizeof(nonce
));
669 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
670 *((u_int32_t
*)&nonce
[i
]) = arc4random();
675 if (options
& F_SO_DEBUG
)
676 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
678 optval
= IPV6_DEFHLIM
;
679 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
680 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
681 &optval
, sizeof(optval
)) == -1)
682 err(1, "IPV6_MULTICAST_HOPS");
683 #ifdef IPV6_USE_MIN_MTU
684 if ((options
& F_NOMINMTU
) == 0) {
686 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
687 &optval
, sizeof(optval
)) == -1)
688 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
690 #ifdef IPV6_RECVPATHMTU
693 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
694 &optval
, sizeof(optval
)) == -1)
695 err(1, "setsockopt(IPV6_RECVPATHMTU)");
697 #endif /* IPV6_RECVPATHMTU */
698 #endif /* IPV6_USE_MIN_MTU */
701 #ifdef IPSEC_POLICY_IPSEC
702 if (options
& F_POLICY
) {
703 if (setpolicy(s
, policy_in
) < 0)
704 errx(1, "%s", ipsec_strerror());
705 if (setpolicy(s
, policy_out
) < 0)
706 errx(1, "%s", ipsec_strerror());
709 if (options
& F_AUTHHDR
) {
710 optval
= IPSEC_LEVEL_REQUIRE
;
711 #ifdef IPV6_AUTH_TRANS_LEVEL
712 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
713 &optval
, sizeof(optval
)) == -1)
714 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
716 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
717 &optval
, sizeof(optval
)) == -1)
718 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
721 if (options
& F_ENCRYPT
) {
722 optval
= IPSEC_LEVEL_REQUIRE
;
723 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
724 &optval
, sizeof(optval
)) == -1)
725 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
727 #endif /*IPSEC_POLICY_IPSEC*/
732 struct icmp6_filter filt
;
733 if (!(options
& F_VERBOSE
)) {
734 ICMP6_FILTER_SETBLOCKALL(&filt
);
735 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
736 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
737 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
739 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
741 ICMP6_FILTER_SETPASSALL(&filt
);
743 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
745 err(1, "setsockopt(ICMP6_FILTER)");
747 #endif /*ICMP6_FILTER*/
749 /* let the kerel pass extension headers of incoming packets */
750 if ((options
& F_VERBOSE
) != 0) {
753 #ifdef IPV6_RECVRTHDR
754 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
756 err(1, "setsockopt(IPV6_RECVRTHDR)");
757 #else /* old adv. API */
758 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
760 err(1, "setsockopt(IPV6_RTHDR)");
766 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
767 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
768 &optval, sizeof(optval)) == -1)
769 err(1, "IPV6_MULTICAST_LOOP");
772 /* Specify the outgoing interface and/or the source address */
774 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
777 ip6optlen
+= CMSG_SPACE(sizeof(int));
779 #ifdef IPV6_REACHCONF
780 if (options
& F_REACHCONF
)
781 ip6optlen
+= CMSG_SPACE(0);
784 /* set IP6 packet options */
786 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
787 errx(1, "can't allocate enough memory");
788 smsghdr
.msg_control
= (caddr_t
)scmsg
;
789 smsghdr
.msg_controllen
= ip6optlen
;
790 scmsgp
= (struct cmsghdr
*)scmsg
;
793 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
794 memset(pktinfo
, 0, sizeof(*pktinfo
));
795 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
796 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
797 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
798 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
801 /* set the outgoing interface */
803 #ifndef USE_SIN6_SCOPE_ID
804 /* pktinfo must have already been allocated */
805 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
806 errx(1, "%s: invalid interface name", ifname
);
808 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
809 errx(1, "%s: invalid interface name", ifname
);
812 /* set the source address */
813 if (options
& F_SRCADDR
)/* pktinfo must be valid */
814 pktinfo
->ipi6_addr
= srcaddr
;
816 if (hoplimit
!= -1) {
817 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
818 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
819 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
820 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
822 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
824 #ifdef IPV6_REACHCONF
825 if (options
& F_REACHCONF
) {
826 scmsgp
->cmsg_len
= CMSG_LEN(0);
827 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
828 scmsgp
->cmsg_type
= IPV6_REACHCONF
;
830 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
834 if (argc
> 1) { /* some intermediate addrs are specified */
836 #ifdef USE_RFC2292BIS
840 #ifdef USE_RFC2292BIS
841 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
842 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
843 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
844 scmsgp
->cmsg_type
= IPV6_RTHDR
;
845 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
846 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
847 IPV6_RTHDR_TYPE_0
, argc
- 1);
849 errx(1, "can't initialize rthdr");
850 #else /* old advanced API */
851 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
852 IPV6_RTHDR_TYPE_0
)) == 0)
853 errx(1, "can't initialize rthdr");
854 #endif /* USE_RFC2292BIS */
856 for (hops
= 0; hops
< argc
- 1; hops
++) {
857 struct addrinfo
*iaip
;
859 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
861 errx(1, "%s", gai_strerror(error
));
862 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
864 "bad addr family of an intermediate addr");
866 #ifdef USE_RFC2292BIS
867 if (inet6_rth_add(rthdr
,
868 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
869 errx(1, "can't add an intermediate node");
870 #else /* old advanced API */
871 if (inet6_rthdr_add(scmsgp
,
872 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
874 errx(1, "can't add an intermediate node");
875 #endif /* USE_RFC2292BIS */
879 #ifndef USE_RFC2292BIS
880 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
881 errx(1, "can't set the last flag");
884 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
892 socklen_t 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
[NS_MAXDNAME
+ 1];
1335 if (i
== 0 || cp
!= *sp
) {
1336 if (strlcat((char *)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
, (u_char
*)cresult
,
1350 sizeof(cresult
)) == NULL
)
1352 if (strlcat((char *)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((char *)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
[NS_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), (u_char
*)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
[NS_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
, (u_char
*)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
= (unsigned char *)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
, (unsigned char *)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");