1 /* $NetBSD: ping6.c,v 1.88 2015/08/06 14:45:54 ozaki-r Exp $ */
2 /* $KAME: ping6.c,v 1.164 2002/11/16 14:05:37 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
36 * Copyright (c) 1989, 1993
37 * The Regents of the University of California. All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 static char copyright
[] =
70 "@(#) Copyright (c) 1989, 1993\n\
71 The Regents of the University of California. All rights reserved.\n";
75 static char sccsid
[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
78 #include <sys/cdefs.h>
80 __RCSID("$NetBSD: ping6.c,v 1.88 2015/08/06 14:45:54 ozaki-r Exp $");
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
136 #include <netinet/ip6.h>
137 #include <netipsec/ipsec.h>
142 #include "prog_ops.h"
149 #define MAXPACKETLEN 131072
151 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
152 #define ICMP6ECHOTMLEN sizeof(struct tv32)
153 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
154 /* FQDN case, 64 bits of nonce + 32 bits ttl */
155 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
156 #define EXTRA 256 /* for AH and various other headers. weird. */
157 #define DEFDATALEN ICMP6ECHOTMLEN
158 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
159 #define NROUTES 9 /* number of record route slots */
161 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
162 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
163 #define SET(bit) (A(bit) |= B(bit))
164 #define CLR(bit) (A(bit) &= (~B(bit)))
165 #define TST(bit) (A(bit) & B(bit))
167 #define F_FLOOD 0x0001
168 #define F_INTERVAL 0x0002
169 #define F_PINGFILLED 0x0008
170 #define F_QUIET 0x0010
171 #define F_RROUTE 0x0020
172 #define F_SO_DEBUG 0x0040
173 #define F_VERBOSE 0x0100
175 #ifdef IPSEC_POLICY_IPSEC
176 #define F_POLICY 0x0400
178 #define F_AUTHHDR 0x0200
179 #define F_ENCRYPT 0x0400
180 #endif /*IPSEC_POLICY_IPSEC*/
182 #define F_NODEADDR 0x0800
183 #define F_FQDN 0x1000
184 #define F_INTERFACE 0x2000
185 #define F_SRCADDR 0x4000
186 #ifdef IPV6_REACHCONF
187 #define F_REACHCONF 0x8000
189 #define F_HOSTNAME 0x10000
190 #define F_FQDNOLD 0x20000
191 #define F_NIGROUP 0x40000
192 #define F_SUPTYPES 0x80000
193 #define F_NOMINMTU 0x100000
194 #define F_ONCE 0x200000
195 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
196 static u_int options
;
198 #define IN6LEN sizeof(struct in6_addr)
199 #define SA6LEN sizeof(struct sockaddr_in6)
200 #define DUMMY_PORT 10101
202 #define SIN6(s) ((struct sockaddr_in6 *)(s))
205 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
206 * number of received sequence numbers we can keep track of. Change 128
207 * to 8192 for complete accuracy...
209 #define MAX_DUP_CHK (8 * 8192)
210 static int mx_dup_ck
= MAX_DUP_CHK
;
211 static char rcvd_tbl
[MAX_DUP_CHK
/ 8];
213 static struct addrinfo
*res
;
214 static struct sockaddr_in6 dst
; /* who to ping6 */
215 static struct sockaddr_in6 src
; /* src addr of this packet */
216 static socklen_t srclen
;
217 static int datalen
= DEFDATALEN
;
218 static int s
; /* socket file descriptor */
219 static u_char outpack
[MAXPACKETLEN
];
220 static char BSPACE
= '\b'; /* characters written for flood */
221 static char DOT
= '.';
222 static char *hostname
;
223 static int ident
; /* process id to identify our packets */
224 static u_int8_t nonce
[8]; /* nonce field for node information */
225 static int hoplimit
= -1; /* hoplimit */
228 static long npackets
; /* max packets to transmit */
229 static long nreceived
; /* # of packets we got back */
230 static long nrepeats
; /* number of duplicates */
231 static long ntransmitted
; /* sequence # for outbound packets = #sent */
232 static struct timeval interval
= {1, 0}; /* interval between packets */
235 static int timing
; /* flag to do timing */
236 static double tmin
= 999999999.0; /* minimum round trip time */
237 static double tmax
= 0.0; /* maximum round trip time */
238 static double tsum
= 0.0; /* sum of all times, for doing average */
239 static double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
240 static double maxwait
= 0.0; /* maxwait for reply in ms */
241 static double deadline
= 0.0; /* max running time in seconds */
243 /* for node addresses */
244 static u_short naflags
;
246 /* for ancillary data(advanced API) */
247 static struct msghdr smsghdr
;
248 static struct iovec smsgiov
;
249 static char *scmsg
= 0;
251 static volatile sig_atomic_t seenalrm
;
252 static volatile sig_atomic_t seenint
;
254 static volatile sig_atomic_t seeninfo
;
257 static void fill(char *, char *);
258 static int get_hoplim(struct msghdr
*);
259 static int get_pathmtu(struct msghdr
*);
260 static struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
261 static void onsignal(int);
262 static void retransmit(void);
263 __dead
static void onsigexit(int);
264 static size_t pingerlen(void);
265 static int pinger(void);
266 static const char *pr_addr(struct sockaddr
*, int);
267 static void pr_icmph(struct icmp6_hdr
*, u_char
*);
268 static void pr_iph(struct ip6_hdr
*);
269 static void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
270 static void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
271 static int myechoreply(const struct icmp6_hdr
*);
272 static int mynireply(const struct icmp6_nodeinfo
*);
273 static char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
275 static void pr_pack(u_char
*, int, struct msghdr
*);
276 static void pr_exthdrs(struct msghdr
*);
277 static void pr_ip6opt(void *);
278 static void pr_rthdr(void *);
279 static int pr_bitrange(u_int32_t
, int, int);
280 static void pr_retip(struct ip6_hdr
*, u_char
*);
281 static void summary(void);
282 static void tvsub(struct timeval
*, struct timeval
*);
283 #if !defined(__minix) /* this is really a netbsd bug */
284 static int setpolicy(int, char *);
285 #endif /* !defined(__minix) */
286 static char *nigroup(char *);
287 static double timespec_to_sec(const struct timespec
*tp
);
288 __dead
static void usage(void);
291 main(int argc
, char *argv
[])
293 struct itimerval itimer
;
294 struct sockaddr_in6 from
;
296 struct addrinfo hints
;
297 struct pollfd fdmaskp
[1];
300 int ch
, hold
, preload
, optval
, ret_ga
;
301 u_char
*datap
, *packet
;
302 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
304 struct cmsghdr
*scmsgp
= NULL
;
305 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
310 struct in6_pktinfo
*pktinfo
= NULL
;
311 struct ip6_rthdr
*rthdr
= NULL
;
312 #ifdef IPSEC_POLICY_IPSEC
313 char *policy_in
= NULL
;
314 char *policy_out
= NULL
;
318 #ifdef IPV6_USE_MIN_MTU
324 /* just to be sure */
325 memset(&smsghdr
, 0, sizeof(smsghdr
));
326 memset(&smsgiov
, 0, sizeof(smsgiov
));
329 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
333 #ifdef IPSEC_POLICY_IPSEC
337 #endif /*IPSEC_POLICY_IPSEC*/
340 if (prog_init
&& prog_init() == -1)
341 err(EXIT_FAILURE
, "init failed");
343 while ((ch
= getopt(argc
, argv
,
344 "a:b:c:dfHg:h:I:i:l:mnNop:qRS:s:tvwWx:X:" ADDOPTS
)) != -1) {
351 options
&= ~F_NOUSERDATA
;
352 options
|= F_NODEADDR
;
353 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
356 naflags
|= NI_NODEADDR_FLAG_ALL
;
360 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
364 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
368 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
372 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
374 case 'A': /* experimental. not in the spec */
375 #ifdef NI_NODEADDR_FLAG_ANYCAST
376 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
380 "-a A is not supported on the platform");
391 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
394 lsockbufsize
= strtoul(optarg
, &e
, 10);
395 sockbufsize
= lsockbufsize
;
396 if (errno
|| !*optarg
|| *e
||
397 (u_long
)sockbufsize
!= lsockbufsize
)
398 errx(1, "invalid socket buffer size");
401 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
405 npackets
= strtol(optarg
, &e
, 10);
406 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
408 "illegal number of packets -- %s", optarg
);
411 options
|= F_SO_DEBUG
;
416 errx(1, "Must be superuser to flood ping");
419 setbuf(stdout
, NULL
);
425 options
|= F_HOSTNAME
;
427 case 'h': /* hoplimit */
428 hoplimit
= strtol(optarg
, &e
, 10);
429 if (*optarg
== '\0' || *e
!= '\0')
430 errx(1, "illegal hoplimit %s", optarg
);
431 if (255 < hoplimit
|| hoplimit
< -1)
433 "illegal hoplimit -- %s", optarg
);
437 options
|= F_INTERFACE
;
438 #ifndef USE_SIN6_SCOPE_ID
442 case 'i': /* wait between sending packets */
443 intval
= strtod(optarg
, &e
);
444 if (*optarg
== '\0' || *e
!= '\0')
445 errx(1, "illegal timing interval %s", optarg
);
446 if (intval
< 1 && prog_getuid()) {
447 errx(1, "%s: only root may use interval < 1s",
450 interval
.tv_sec
= (long)intval
;
452 (long)((intval
- interval
.tv_sec
) * 1000000);
453 if (interval
.tv_sec
< 0)
454 errx(1, "illegal timing interval %s", optarg
);
455 /* less than 1/hz does not make sense */
456 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
457 warnx("too small interval, raised to 0.01");
458 interval
.tv_usec
= 10000;
460 options
|= F_INTERVAL
;
465 errx(1, "Must be superuser to preload");
467 preload
= strtol(optarg
, &e
, 10);
468 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
469 errx(1, "illegal preload value -- %s", optarg
);
472 #ifdef IPV6_USE_MIN_MTU
476 errx(1, "-%c is not supported on this platform", ch
);
480 options
&= ~F_HOSTNAME
;
483 options
|= F_NIGROUP
;
488 case 'p': /* fill buffer with user pattern */
489 options
|= F_PINGFILLED
;
490 fill((char *)datap
, optarg
);
496 #ifdef IPV6_REACHCONF
497 options
|= F_REACHCONF
;
500 errx(1, "-R is not supported in this configuration");
503 memset(&hints
, 0, sizeof(struct addrinfo
));
504 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
505 hints
.ai_family
= AF_INET6
;
506 hints
.ai_socktype
= SOCK_RAW
;
507 hints
.ai_protocol
= IPPROTO_ICMPV6
;
509 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
511 errx(1, "invalid source address: %s",
512 gai_strerror(ret_ga
));
515 * res->ai_family must be AF_INET6 and res->ai_addrlen
516 * must be sizeof(src).
518 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
519 srclen
= res
->ai_addrlen
;
521 options
|= F_SRCADDR
;
523 case 's': /* size of packet to send */
524 datalen
= strtol(optarg
, &e
, 10);
525 if (datalen
< 0 || *optarg
== '\0' || *e
!= '\0')
526 errx(1, "illegal datalen value -- %s", optarg
);
527 if (datalen
> MAXDATALEN
) {
529 "datalen value too large, maximum is %d",
534 options
&= ~F_NOUSERDATA
;
535 options
|= F_SUPTYPES
;
538 options
|= F_VERBOSE
;
541 options
&= ~F_NOUSERDATA
;
545 options
&= ~F_NOUSERDATA
;
546 options
|= F_FQDNOLD
;
549 maxwait
= strtod(optarg
, &e
);
550 if (*e
!= '\0' || maxwait
<= 0)
551 errx(EXIT_FAILURE
, "Bad/invalid maxwait time: "
555 deadline
= strtod(optarg
, &e
);
556 if (*e
!= '\0' || deadline
<= 0)
557 errx(EXIT_FAILURE
, "Bad/invalid deadline time: "
561 #ifdef IPSEC_POLICY_IPSEC
564 if (!strncmp("in", optarg
, 2)) {
565 if ((policy_in
= strdup(optarg
)) == NULL
)
567 } else if (!strncmp("out", optarg
, 3)) {
568 if ((policy_out
= strdup(optarg
)) == NULL
)
571 errx(1, "invalid security policy");
575 options
|= F_AUTHHDR
;
578 options
|= F_ENCRYPT
;
580 #endif /*IPSEC_POLICY_IPSEC*/
597 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
600 errx(1, "too many intermediate hops");
606 if (options
& F_NIGROUP
) {
607 target
= nigroup(argv
[argc
- 1]);
608 if (target
== NULL
) {
613 target
= argv
[argc
- 1];
616 memset(&hints
, 0, sizeof(struct addrinfo
));
617 hints
.ai_flags
= AI_CANONNAME
;
618 hints
.ai_family
= AF_INET6
;
619 hints
.ai_socktype
= SOCK_RAW
;
620 hints
.ai_protocol
= IPPROTO_ICMPV6
;
622 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
624 errx(1, "%s", gai_strerror(ret_ga
));
625 if (res
->ai_canonname
)
626 hostname
= res
->ai_canonname
;
631 errx(1, "getaddrinfo failed");
633 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
635 if ((s
= prog_socket(res
->ai_family
, res
->ai_socktype
,
636 res
->ai_protocol
)) < 0)
639 /* set the source address if specified. */
640 if ((options
& F_SRCADDR
) &&
641 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
645 /* set the gateway (next hop) if specified */
647 struct addrinfo ghints
, *gres
;
650 memset(&ghints
, 0, sizeof(ghints
));
651 ghints
.ai_family
= AF_INET6
;
652 ghints
.ai_socktype
= SOCK_RAW
;
653 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
655 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
657 errx(1, "getaddrinfo for the gateway %s: %s",
658 gateway
, gai_strerror(error
));
660 if (gres
->ai_next
&& (options
& F_VERBOSE
))
661 warnx("gateway resolves to multiple addresses");
663 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
664 gres
->ai_addr
, gres
->ai_addrlen
)) {
665 err(1, "setsockopt(IPV6_NEXTHOP)");
672 * let the kerel pass extension headers of incoming packets,
673 * for privileged socket options
675 if ((options
& F_VERBOSE
) != 0) {
678 #ifdef IPV6_RECVHOPOPTS
679 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
681 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
682 #else /* old adv. API */
683 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
685 err(1, "setsockopt(IPV6_HOPOPTS)");
687 #ifdef IPV6_RECVDSTOPTS
688 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
690 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
691 #else /* old adv. API */
692 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
694 err(1, "setsockopt(IPV6_DSTOPTS)");
696 #ifdef IPV6_RECVRTHDRDSTOPTS
697 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
699 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
703 /* revoke root privilege */
704 prog_seteuid(prog_getuid());
705 prog_setuid(prog_getuid());
707 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
708 errx(1, "-f and -i incompatible options");
710 if ((options
& F_NOUSERDATA
) == 0) {
711 if (datalen
>= (int)sizeof(struct tv32
)) {
712 /* we can time transfer */
716 /* in F_VERBOSE case, we may get non-echoreply packets*/
717 if (options
& F_VERBOSE
)
718 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
720 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
722 /* suppress timing for node information query */
725 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
728 if (!(packet
= (u_char
*)malloc(packlen
)))
729 err(1, "Unable to allocate packet");
730 if (!(options
& F_PINGFILLED
))
731 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
734 ident
= arc4random() & 0xFFFF;
735 memset(nonce
, 0, sizeof(nonce
));
736 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
)) {
737 uint32_t r
= arc4random();
738 memcpy(&nonce
[i
], &r
, sizeof(r
));
743 if (options
& F_SO_DEBUG
)
744 (void)prog_setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
746 optval
= IPV6_DEFHLIM
;
747 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
748 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
749 &optval
, sizeof(optval
)) == -1)
750 err(1, "IPV6_MULTICAST_HOPS");
751 #ifdef IPV6_USE_MIN_MTU
753 optval
= mflag
> 1 ? 0 : 1;
755 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
756 &optval
, sizeof(optval
)) == -1)
757 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
759 #ifdef IPV6_RECVPATHMTU
762 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
763 &optval
, sizeof(optval
)) == -1)
764 err(1, "setsockopt(IPV6_RECVPATHMTU)");
766 #endif /* IPV6_RECVPATHMTU */
767 #endif /* IPV6_USE_MIN_MTU */
770 #ifdef IPSEC_POLICY_IPSEC
771 if (options
& F_POLICY
) {
772 if (setpolicy(s
, policy_in
) < 0)
773 errx(1, "%s", ipsec_strerror());
774 if (setpolicy(s
, policy_out
) < 0)
775 errx(1, "%s", ipsec_strerror());
778 if (options
& F_AUTHHDR
) {
779 optval
= IPSEC_LEVEL_REQUIRE
;
780 #ifdef IPV6_AUTH_TRANS_LEVEL
781 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
782 &optval
, sizeof(optval
)) == -1)
783 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
785 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
786 &optval
, sizeof(optval
)) == -1)
787 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
790 if (options
& F_ENCRYPT
) {
791 optval
= IPSEC_LEVEL_REQUIRE
;
792 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
793 &optval
, sizeof(optval
)) == -1)
794 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
796 #endif /*IPSEC_POLICY_IPSEC*/
801 struct icmp6_filter filt
;
802 if (!(options
& F_VERBOSE
)) {
803 ICMP6_FILTER_SETBLOCKALL(&filt
);
804 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
805 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
806 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
808 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
810 ICMP6_FILTER_SETPASSALL(&filt
);
812 if (prog_setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
814 err(1, "setsockopt(ICMP6_FILTER)");
816 #endif /*ICMP6_FILTER*/
818 /* let the kernel pass extension headers of incoming packets */
819 if ((options
& F_VERBOSE
) != 0) {
822 #ifdef IPV6_RECVRTHDR
823 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
825 err(1, "setsockopt(IPV6_RECVRTHDR)");
826 #else /* old adv. API */
827 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
829 err(1, "setsockopt(IPV6_RTHDR)");
835 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
836 if (prog_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
837 &optval, sizeof(optval)) == -1)
838 err(1, "IPV6_MULTICAST_LOOP");
841 /* Specify the outgoing interface and/or the source address */
843 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
846 ip6optlen
+= CMSG_SPACE(sizeof(int));
848 #ifdef IPV6_REACHCONF
849 if (options
& F_REACHCONF
)
850 ip6optlen
+= CMSG_SPACE(0);
853 /* set IP6 packet options */
855 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
856 errx(1, "can't allocate enough memory");
857 smsghdr
.msg_control
= (caddr_t
)scmsg
;
858 smsghdr
.msg_controllen
= ip6optlen
;
859 scmsgp
= (struct cmsghdr
*)scmsg
;
862 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
863 memset(pktinfo
, 0, sizeof(*pktinfo
));
864 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
865 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
866 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
867 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
870 /* set the outgoing interface */
872 #ifndef USE_SIN6_SCOPE_ID
873 /* pktinfo must have already been allocated */
874 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
875 errx(1, "%s: invalid interface name", ifname
);
877 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
878 errx(1, "%s: invalid interface name", ifname
);
881 if (hoplimit
!= -1) {
882 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
883 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
884 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
885 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
887 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
889 #ifdef IPV6_REACHCONF
890 if (options
& F_REACHCONF
) {
891 scmsgp
->cmsg_len
= CMSG_LEN(0);
892 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
893 scmsgp
->cmsg_type
= IPV6_REACHCONF
;
895 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
899 if (argc
> 1) { /* some intermediate addrs are specified */
903 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
904 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
905 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
906 scmsgp
->cmsg_type
= IPV6_RTHDR
;
907 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
908 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
909 IPV6_RTHDR_TYPE_0
, argc
- 1);
911 errx(1, "can't initialize rthdr");
913 for (hops
= 0; hops
< argc
- 1; hops
++) {
914 struct addrinfo
*iaip
;
916 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
918 errx(1, "%s", gai_strerror(error
));
919 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
921 "bad addr family of an intermediate addr");
923 if (inet6_rth_add(rthdr
,
924 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
925 errx(1, "can't add an intermediate node");
929 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
932 if (!(options
& F_SRCADDR
)) {
934 * get the source address. XXX since we revoked the root
935 * privilege, we cannot use a raw socket for this.
938 socklen_t len
= sizeof(src
);
940 if ((dummy
= prog_socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
941 err(1, "UDP socket");
943 src
.sin6_family
= AF_INET6
;
944 src
.sin6_addr
= dst
.sin6_addr
;
945 src
.sin6_port
= ntohs(DUMMY_PORT
);
946 src
.sin6_scope_id
= dst
.sin6_scope_id
;
949 prog_setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
950 (void *)pktinfo
, sizeof(*pktinfo
)))
951 err(1, "UDP setsockopt(IPV6_PKTINFO)");
953 if (hoplimit
!= -1 &&
954 prog_setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
955 (void *)&hoplimit
, sizeof(hoplimit
)))
956 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
958 if (hoplimit
!= -1 &&
959 prog_setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
960 (void *)&hoplimit
, sizeof(hoplimit
)))
961 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
964 prog_setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
965 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
966 err(1, "UDP setsockopt(IPV6_RTHDR)");
968 if (prog_connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
969 err(1, "UDP connect");
971 if (prog_getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
972 err(1, "getsockname");
977 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
979 if (datalen
> sockbufsize
)
980 warnx("you need -b to increase socket buffer size");
981 if (prog_setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
982 sizeof(sockbufsize
)) < 0)
983 err(1, "setsockopt(SO_SNDBUF)");
984 if (prog_setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
985 sizeof(sockbufsize
)) < 0)
986 err(1, "setsockopt(SO_RCVBUF)");
989 if (datalen
> 8 * 1024) /*XXX*/
990 warnx("you need -b to increase socket buffer size");
992 * When pinging the broadcast address, you can get a lot of
993 * answers. Doing something so evil is useful if you are trying
994 * to stress the ethernet, or just want to fill the arp cache
995 * to get some stuff for /etc/ethers.
998 prog_setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1004 #ifndef USE_SIN6_SCOPE_ID
1005 #ifdef IPV6_RECVPKTINFO
1006 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1007 sizeof(optval
)) < 0)
1008 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1009 #else /* old adv. API */
1010 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1011 sizeof(optval
)) < 0)
1012 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1014 #endif /* USE_SIN6_SCOPE_ID */
1015 #ifdef IPV6_RECVHOPLIMIT
1016 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1017 sizeof(optval
)) < 0)
1018 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1019 #else /* old adv. API */
1020 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1021 sizeof(optval
)) < 0)
1022 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1025 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1026 (unsigned long)(pingerlen() - 8));
1027 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1028 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1030 while (preload
--) /* Fire off them quickies. */
1033 (void)signal(SIGINT
, onsignal
);
1035 (void)signal(SIGINFO
, onsignal
);
1038 if ((options
& F_FLOOD
) == 0) {
1039 (void)signal(SIGALRM
, onsignal
);
1040 itimer
.it_interval
= interval
;
1041 itimer
.it_value
= interval
;
1042 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1043 if (ntransmitted
== 0)
1048 clock_gettime(CLOCK_MONOTONIC
, &now
);
1049 exitat
= timespec_to_sec(&now
) + deadline
;
1052 seenalrm
= seenint
= 0;
1060 struct iovec iov
[2];
1062 /* check deadline */
1064 clock_gettime(CLOCK_MONOTONIC
, &now
);
1065 if (exitat
<= timespec_to_sec(&now
))
1069 /* signal handling */
1087 if (options
& F_FLOOD
) {
1090 } else if (deadline
> 0) {
1091 timeout
= (int)floor(deadline
* 1000);
1096 fdmaskp
[0].events
= POLLIN
;
1097 cc
= prog_poll(fdmaskp
, 1, timeout
);
1099 if (errno
!= EINTR
) {
1107 m
.msg_name
= (caddr_t
)&from
;
1108 m
.msg_namelen
= sizeof(from
);
1109 memset(&iov
, 0, sizeof(iov
));
1110 iov
[0].iov_base
= (caddr_t
)packet
;
1111 iov
[0].iov_len
= packlen
;
1114 m
.msg_control
= (caddr_t
)buf
;
1115 m
.msg_controllen
= sizeof(buf
);
1117 cc
= prog_recvmsg(s
, &m
, 0);
1119 if (errno
!= EINTR
) {
1124 } else if (cc
== 0) {
1128 * receive control messages only. Process the
1129 * exceptions (currently the only possiblity is
1130 * a path MTU notification.)
1132 if ((mtu
= get_pathmtu(&m
)) > 0) {
1133 if ((options
& F_VERBOSE
) != 0) {
1134 printf("new path MTU (%d) is "
1141 * an ICMPv6 message (probably an echoreply) arrived.
1143 pr_pack(packet
, cc
, &m
);
1145 if (npackets
&& nreceived
>= npackets
)
1147 if (nreceived
!= 0 && (options
& F_ONCE
))
1151 exit(nreceived
== 0);
1175 * This routine transmits another ping6.
1180 struct itimerval itimer
;
1186 * If we're not transmitting any more packets, change the timer
1187 * to wait two round-trip times if we've received any packets or
1188 * ten seconds if we haven't.
1192 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1193 if (itimer
.it_value
.tv_sec
== 0)
1194 itimer
.it_value
.tv_sec
= 1;
1196 itimer
.it_value
.tv_sec
= MAXWAIT
;
1197 itimer
.it_interval
.tv_sec
= 0;
1198 itimer
.it_interval
.tv_usec
= 0;
1199 itimer
.it_value
.tv_usec
= 0;
1201 (void)signal(SIGALRM
, onsigexit
);
1202 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1207 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1208 * will be added on by the kernel. The ID field is our UNIX process ID,
1209 * and the sequence number is an ascending integer. The first 8 bytes
1210 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1211 * byte-order, to compute the round-trip time.
1218 if (options
& F_FQDN
)
1219 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1220 else if (options
& F_FQDNOLD
)
1222 else if (options
& F_NODEADDR
)
1223 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1224 else if (options
& F_SUPTYPES
)
1227 l
= ICMP6ECHOLEN
+ datalen
;
1235 struct icmp6_hdr
*icp
;
1236 struct iovec iov
[2];
1238 struct icmp6_nodeinfo
*nip
;
1241 if (npackets
&& ntransmitted
>= npackets
)
1242 return(-1); /* no more transmission */
1244 icp
= (struct icmp6_hdr
*)outpack
;
1245 nip
= (struct icmp6_nodeinfo
*)outpack
;
1246 memset(icp
, 0, sizeof(*icp
));
1247 icp
->icmp6_cksum
= 0;
1248 seq
= ntransmitted
++;
1249 CLR(seq
% mx_dup_ck
);
1252 if (options
& F_FQDN
) {
1253 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1254 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1255 nip
->ni_flags
= htons(0);
1257 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1258 sizeof(dst
.sin6_addr
));
1259 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1260 } else if (options
& F_FQDNOLD
) {
1261 /* packet format in 03 draft - no Subject data on queries */
1262 icp
->icmp6_code
= 0; /* code field is always 0 */
1263 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1264 nip
->ni_flags
= htons(0);
1267 } else if (options
& F_NODEADDR
) {
1268 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1269 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1270 nip
->ni_flags
= naflags
;
1272 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1273 sizeof(dst
.sin6_addr
));
1274 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1275 } else if (options
& F_SUPTYPES
) {
1276 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1277 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1278 /* we support compressed bitmap */
1279 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1283 cc
= 0; /* XXX: gcc */
1285 if (options
& (F_FQDN
|F_FQDNOLD
|F_NODEADDR
|F_SUPTYPES
)) {
1286 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1287 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1288 sizeof(nip
->icmp6_ni_nonce
));
1289 memcpy(nip
->icmp6_ni_nonce
, &seq
, sizeof(seq
));
1292 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1293 icp
->icmp6_code
= 0;
1294 icp
->icmp6_id
= htons(ident
);
1295 icp
->icmp6_seq
= seq
;
1299 (void)gettimeofday(&tv
, NULL
);
1300 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1301 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1302 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1304 cc
= ICMP6ECHOLEN
+ datalen
;
1308 if (pingerlen() != cc
)
1309 errx(1, "internal error; length mismatch");
1312 smsghdr
.msg_name
= (caddr_t
)&dst
;
1313 smsghdr
.msg_namelen
= sizeof(dst
);
1314 memset(&iov
, 0, sizeof(iov
));
1315 iov
[0].iov_base
= (caddr_t
)outpack
;
1316 iov
[0].iov_len
= cc
;
1317 smsghdr
.msg_iov
= iov
;
1318 smsghdr
.msg_iovlen
= 1;
1320 i
= prog_sendmsg(s
, &smsghdr
, 0);
1322 if (i
< 0 || i
!= cc
) {
1325 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1328 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1329 (void)write(STDOUT_FILENO
, &DOT
, 1);
1335 myechoreply(const struct icmp6_hdr
*icp
)
1337 if (ntohs(icp
->icmp6_id
) == ident
)
1344 mynireply(const struct icmp6_nodeinfo
*nip
)
1346 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1347 nonce
+ sizeof(u_int16_t
),
1348 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1355 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1360 char cresult
[MAXDNAME
+ 1];
1364 i
= 0; /* XXXGCC -Wuninitialized [sun2] */
1373 if (i
== 0 || cp
!= *sp
) {
1374 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1375 return NULL
; /*result overrun*/
1381 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1382 /* DNS compression */
1386 comp
= base
+ (i
& 0x3f);
1387 if (dnsdecode(&comp
, cp
, base
, cresult
,
1388 sizeof(cresult
)) == NULL
)
1390 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1391 return NULL
; /*result overrun*/
1393 } else if ((i
& 0x3f) == i
) {
1395 return NULL
; /*source overrun*/
1396 while (i
-- > 0 && cp
< ep
) {
1397 l
= snprintf(cresult
, sizeof(cresult
),
1398 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1399 if (l
>= (int)sizeof(cresult
) || l
< 0)
1401 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1402 return NULL
; /*result overrun*/
1406 return NULL
; /*invalid label*/
1409 return NULL
; /*not terminated*/
1417 * Print out the packet, if it came from us. This logic is necessary
1418 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1419 * which arrive ('tis only fair). This permits multiple copies of this
1420 * program to be run without having intermingled output (or statistics!).
1423 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1425 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1426 struct icmp6_hdr
*icp
;
1427 struct icmp6_nodeinfo
*ni
;
1430 struct sockaddr
*from
;
1432 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1433 struct in6_pktinfo
*pktinfo
= NULL
;
1434 struct timeval tv
, tp
;
1436 double triptime
= 0;
1441 char dnsname
[MAXDNAME
+ 1];
1443 (void)gettimeofday(&tv
, NULL
);
1445 if (!mhdr
|| !mhdr
->msg_name
||
1446 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1447 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1448 if (options
& F_VERBOSE
)
1449 warnx("invalid peername");
1452 from
= (struct sockaddr
*)mhdr
->msg_name
;
1453 fromlen
= mhdr
->msg_namelen
;
1454 if (cc
< (int)sizeof(struct icmp6_hdr
)) {
1455 if (options
& F_VERBOSE
)
1456 warnx("packet too short (%d bytes) from %s", cc
,
1457 pr_addr(from
, fromlen
));
1460 icp
= (struct icmp6_hdr
*)buf
;
1461 ni
= (struct icmp6_nodeinfo
*)buf
;
1464 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1465 warnx("failed to get receiving hop limit");
1468 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1469 warnx("failed to get receiving packet information");
1473 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1474 seq
= ntohs(icp
->icmp6_seq
);
1477 tpp
= (struct tv32
*)(icp
+ 1);
1478 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1479 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1481 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1482 ((double)tv
.tv_usec
) / 1000.0;
1483 if (maxwait
> 0 && triptime
> maxwait
) {
1485 return; /* DISCARD */
1488 tsumsq
+= triptime
* triptime
;
1489 if (triptime
< tmin
)
1491 if (triptime
> tmax
)
1495 if (TST(seq
% mx_dup_ck
)) {
1500 SET(seq
% mx_dup_ck
);
1504 if (options
& F_QUIET
)
1507 if (options
& F_FLOOD
)
1508 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1510 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1511 pr_addr(from
, fromlen
), seq
);
1512 (void)printf(" hlim=%d", hoplim
);
1513 if ((options
& F_VERBOSE
) != 0) {
1514 struct sockaddr_in6 dstsa
;
1516 memset(&dstsa
, 0, sizeof(dstsa
));
1517 dstsa
.sin6_family
= AF_INET6
;
1519 dstsa
.sin6_len
= sizeof(dstsa
);
1521 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1522 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1523 (void)printf(" dst=%s",
1524 pr_addr((struct sockaddr
*)&dstsa
,
1528 (void)printf(" time=%.3f ms", triptime
);
1530 (void)printf("(DUP!)");
1531 /* check the data */
1532 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1533 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1534 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1536 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1541 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1542 memcpy(&seq
, ni
->icmp6_ni_nonce
, sizeof(seq
));
1545 if (TST(seq
% mx_dup_ck
)) {
1550 SET(seq
% mx_dup_ck
);
1554 if (options
& F_QUIET
)
1557 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1559 switch (ntohs(ni
->ni_code
)) {
1560 case ICMP6_NI_SUCCESS
:
1562 case ICMP6_NI_REFUSED
:
1563 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1565 case ICMP6_NI_UNKNOWN
:
1566 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1569 printf("unknown code 0x%x, type 0x%x",
1570 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1574 switch (ntohs(ni
->ni_qtype
)) {
1576 printf("NodeInfo NOOP");
1578 case NI_QTYPE_SUPTYPES
:
1579 pr_suptypes(ni
, end
- (u_char
*)ni
);
1581 case NI_QTYPE_NODEADDR
:
1582 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1585 default: /* XXX: for backward compatibility */
1586 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1587 if (buf
[off
+ ICMP6_NIRLEN
] ==
1588 cc
- off
- ICMP6_NIRLEN
- 1)
1593 cp
++; /* skip length */
1595 safeputc(*cp
& 0xff);
1601 if (dnsdecode((void *)&cp
, end
,
1602 (const u_char
*)(ni
+ 1), dnsname
,
1603 sizeof(dnsname
)) == NULL
) {
1608 * name-lookup special handling for
1611 if (cp
+ 1 <= end
&& !*cp
&&
1612 strlen(dnsname
) > 0) {
1613 dnsname
[strlen(dnsname
) - 1] = '\0';
1616 printf("%s%s", i
> 0 ? "," : "",
1620 if (options
& F_VERBOSE
) {
1624 (void)printf(" ("); /*)*/
1626 switch (ni
->ni_code
) {
1627 case ICMP6_NI_REFUSED
:
1628 (void)printf("refused");
1631 case ICMP6_NI_UNKNOWN
:
1632 (void)printf("unknown qtype");
1637 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1638 /* case of refusion, unknown */
1643 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1646 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1647 (void)printf("TTL=%d:meaningless",
1651 (void)printf("TTL=%d:invalid",
1654 (void)printf("TTL=%d", ttl
);
1664 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1673 if (buf
[off
+ ICMP6_NIRLEN
] !=
1674 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1677 (void)printf("invalid namelen:%d/%lu",
1678 buf
[off
+ ICMP6_NIRLEN
],
1679 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1689 /* We've got something other than an ECHOREPLY */
1690 if (!(options
& F_VERBOSE
))
1692 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1696 if (!(options
& F_FLOOD
)) {
1697 (void)putchar('\n');
1698 if (options
& F_VERBOSE
)
1700 (void)fflush(stdout
);
1706 pr_exthdrs(struct msghdr
*mhdr
)
1710 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1711 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1712 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1715 switch (cm
->cmsg_type
) {
1717 printf(" HbH Options: ");
1718 pr_ip6opt(CMSG_DATA(cm
));
1721 #ifdef IPV6_RTHDRDSTOPTS
1722 case IPV6_RTHDRDSTOPTS
:
1724 printf(" Dst Options: ");
1725 pr_ip6opt(CMSG_DATA(cm
));
1728 printf(" Routing: ");
1729 pr_rthdr(CMSG_DATA(cm
));
1736 pr_ip6opt(void *extbuf
)
1738 struct ip6_hbh
*ext
;
1748 ext
= (struct ip6_hbh
*)extbuf
;
1749 extlen
= (ext
->ip6h_len
+ 1) * 8;
1750 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1751 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1755 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1756 &type
, &len
, &databuf
);
1757 if (currentlen
== -1)
1761 * Note that inet6_opt_next automatically skips any padding
1766 offset
= inet6_opt_get_val(databuf
, offset
,
1767 &value4
, sizeof(value4
));
1768 printf(" Jumbo Payload Opt: Length %u\n",
1769 (u_int32_t
)ntohl(value4
));
1771 case IP6OPT_ROUTER_ALERT
:
1773 offset
= inet6_opt_get_val(databuf
, offset
,
1774 &value2
, sizeof(value2
));
1775 printf(" Router Alert Opt: Type %u\n",
1779 printf(" Received Opt %u len %lu\n",
1780 type
, (unsigned long)len
);
1788 pr_rthdr(void *extbuf
)
1790 struct in6_addr
*in6
;
1791 char ntopbuf
[INET6_ADDRSTRLEN
];
1792 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1795 /* print fixed part of the header */
1796 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1797 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1798 if ((segments
= inet6_rth_segments(extbuf
)) >= 0)
1799 printf("%d segments, ", segments
);
1801 printf("segments unknown, ");
1802 printf("%d left\n", rh
->ip6r_segleft
);
1804 for (i
= 0; i
< segments
; i
++) {
1805 in6
= inet6_rth_getaddr(extbuf
, i
);
1807 printf(" [%d]<NULL>\n", i
);
1809 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1811 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
1812 printf(" [%d]%s\n", i
, ntopbuf
);
1821 pr_bitrange(u_int32_t v
, int soff
, int ii
)
1828 /* shift till we have 0x01 */
1829 if ((v
& 0x01) == 0) {
1831 printf("-%u", soff
+ off
- 1);
1842 case 0x04: case 0x0c:
1853 /* we have 0x01 with us */
1854 for (i
= 0; i
< 32 - off
; i
++) {
1855 if ((v
& (0x01 << i
)) == 0)
1859 printf(" %u", soff
+ off
);
1867 pr_suptypes(struct icmp6_nodeinfo
*ni
/* ni->qtype must be SUPTYPES */,
1872 const u_char
*cp
, *end
;
1875 u_int16_t words
; /*32bit count*/
1878 #define MAXQTYPES (1 << 16)
1882 cp
= (u_char
*)(ni
+ 1);
1883 end
= ((u_char
*)ni
) + nilen
;
1887 printf("NodeInfo Supported Qtypes");
1888 if (options
& F_VERBOSE
) {
1889 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1890 printf(", compressed bitmap");
1892 printf(", raw bitmap");
1897 clen
= (size_t)(end
- cp
);
1898 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1899 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1905 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1907 memcpy(&cbit
, cp
, sizeof(cbit
));
1908 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1912 clen
= ntohs(cbit
.words
) * sizeof(v
);
1913 skip
= (size_t)ntohs(cbit
.skip
) * 32;
1914 if (cur
+ clen
* 8 + skip
> MAXQTYPES
)
1918 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1919 memcpy(&v
, cp
+ off
, sizeof(v
));
1920 v
= (u_int32_t
)ntohl(v
);
1921 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1923 /* flush the remaining bits */
1924 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1927 cur
+= clen
* 8 + skip
;
1932 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, /* ni->qtype must be NODEADDR */
1935 u_char
*cp
= (u_char
*)(ni
+ 1);
1936 char ntop_buf
[INET6_ADDRSTRLEN
];
1939 nilen
-= sizeof(struct icmp6_nodeinfo
);
1941 if (options
& F_VERBOSE
) {
1942 switch (ni
->ni_code
) {
1943 case ICMP6_NI_REFUSED
:
1944 (void)printf("refused");
1946 case ICMP6_NI_UNKNOWN
:
1947 (void)printf("unknown qtype");
1950 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1951 (void)printf(" truncated");
1955 printf(" no address\n");
1958 * In icmp-name-lookups 05 and later, TTL of each returned address
1959 * is contained in the resposne. We try to detect the version
1960 * by the length of the data, but note that the detection algorithm
1961 * is incomplete. We assume the latest draft by default.
1963 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1969 /* XXX: alignment? */
1970 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1971 cp
+= sizeof(u_int32_t
);
1972 nilen
-= sizeof(u_int32_t
);
1975 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1977 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
1978 printf(" %s", ntop_buf
);
1980 if (ttl
== 0xffffffff) {
1982 * XXX: can this convention be applied to all
1983 * type of TTL (i.e. non-ND TTL)?
1985 printf("(TTL=infty)");
1988 printf("(TTL=%u)", ttl
);
1992 nilen
-= sizeof(struct in6_addr
);
1993 cp
+= sizeof(struct in6_addr
);
1998 get_hoplim(struct msghdr
*mhdr
)
2002 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2003 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2004 if (cm
->cmsg_len
== 0)
2007 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2008 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2009 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2010 return(*(int *)CMSG_DATA(cm
));
2016 static struct in6_pktinfo
*
2017 get_rcvpktinfo(struct msghdr
*mhdr
)
2021 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2022 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2023 if (cm
->cmsg_len
== 0)
2026 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2027 cm
->cmsg_type
== IPV6_PKTINFO
&&
2028 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2029 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2036 get_pathmtu(struct msghdr
*mhdr
)
2038 #ifdef IPV6_RECVPATHMTU
2040 struct ip6_mtuinfo
*mtuctl
= NULL
;
2042 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2043 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2044 if (cm
->cmsg_len
== 0)
2047 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2048 cm
->cmsg_type
== IPV6_PATHMTU
&&
2049 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2050 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2053 * If the notified destination is different from
2054 * the one we are pinging, just ignore the info.
2055 * We check the scope ID only when both notified value
2056 * and our own value have non-0 values, because we may
2057 * have used the default scope zone ID for sending,
2058 * in which case the scope ID value is 0.
2060 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2062 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2063 dst
.sin6_scope_id
&&
2064 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2065 dst
.sin6_scope_id
)) {
2066 if ((options
& F_VERBOSE
) != 0) {
2067 printf("path MTU for %s is notified. "
2069 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2070 sizeof(mtuctl
->ip6m_addr
)));
2076 * Ignore an invalid MTU. XXX: can we just believe
2079 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2082 /* notification for our destination. return the MTU. */
2083 return((int)mtuctl
->ip6m_mtu
);
2092 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2096 tvsub(struct timeval
*out
, struct timeval
*in
)
2098 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2100 out
->tv_usec
+= 1000000;
2102 out
->tv_sec
-= in
->tv_sec
;
2113 if (sig
== SIGINT
) {
2114 (void)signal(SIGINT
, SIG_DFL
);
2115 (void)kill(getpid(), SIGINT
);
2123 * Print out statistics.
2129 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2130 (void)printf("%ld packets transmitted, ", ntransmitted
);
2131 (void)printf("%ld packets received, ", nreceived
);
2133 (void)printf("+%ld duplicates, ", nrepeats
);
2135 if (nreceived
> ntransmitted
)
2136 (void)printf("-- somebody's duplicating packets!");
2138 (void)printf("%.1f%% packet loss",
2139 ((((double)ntransmitted
- nreceived
) * 100.0) /
2142 (void)putchar('\n');
2143 if (nreceived
&& timing
) {
2144 /* Only display average to microseconds */
2145 double num
= nreceived
+ nrepeats
;
2149 dev
= sqrt((tsumsq
- num
* avg
* avg
) / (num
- 1));
2155 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2156 tmin
, avg
, tmax
, dev
);
2157 (void)fflush(stdout
);
2159 (void)fflush(stdout
);
2163 static const char *niqcode
[] = {
2165 "DNS label", /*or empty*/
2170 static const char *nircode
[] = {
2171 "Success", "Refused", "Unknown",
2177 * Print a descriptive string about an ICMP header.
2180 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2182 char ntop_buf
[INET6_ADDRSTRLEN
];
2183 struct nd_redirect
*red
;
2184 struct icmp6_nodeinfo
*ni
;
2185 char dnsname
[MAXDNAME
+ 1];
2189 switch (icp
->icmp6_type
) {
2190 case ICMP6_DST_UNREACH
:
2191 switch (icp
->icmp6_code
) {
2192 case ICMP6_DST_UNREACH_NOROUTE
:
2193 (void)printf("No Route to Destination\n");
2195 case ICMP6_DST_UNREACH_ADMIN
:
2196 (void)printf("Destination Administratively "
2199 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2200 (void)printf("Destination Unreachable Beyond Scope\n");
2202 case ICMP6_DST_UNREACH_ADDR
:
2203 (void)printf("Destination Host Unreachable\n");
2205 case ICMP6_DST_UNREACH_NOPORT
:
2206 (void)printf("Destination Port Unreachable\n");
2209 (void)printf("Destination Unreachable, Bad Code: %d\n",
2213 /* Print returned IP header information */
2214 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2216 case ICMP6_PACKET_TOO_BIG
:
2217 (void)printf("Packet too big mtu = %d\n",
2218 (int)ntohl(icp
->icmp6_mtu
));
2219 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2221 case ICMP6_TIME_EXCEEDED
:
2222 switch (icp
->icmp6_code
) {
2223 case ICMP6_TIME_EXCEED_TRANSIT
:
2224 (void)printf("Time to live exceeded\n");
2226 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2227 (void)printf("Frag reassembly time exceeded\n");
2230 (void)printf("Time exceeded, Bad Code: %d\n",
2234 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2236 case ICMP6_PARAM_PROB
:
2237 (void)printf("Parameter problem: ");
2238 switch (icp
->icmp6_code
) {
2239 case ICMP6_PARAMPROB_HEADER
:
2240 (void)printf("Erroneous Header ");
2242 case ICMP6_PARAMPROB_NEXTHEADER
:
2243 (void)printf("Unknown Nextheader ");
2245 case ICMP6_PARAMPROB_OPTION
:
2246 (void)printf("Unrecognized Option ");
2249 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2252 (void)printf("pointer = 0x%02x\n",
2253 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2254 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2256 case ICMP6_ECHO_REQUEST
:
2257 (void)printf("Echo Request");
2258 /* XXX ID + Seq + Data */
2260 case ICMP6_ECHO_REPLY
:
2261 (void)printf("Echo Reply");
2262 /* XXX ID + Seq + Data */
2264 case ICMP6_MEMBERSHIP_QUERY
:
2265 (void)printf("Listener Query");
2267 case ICMP6_MEMBERSHIP_REPORT
:
2268 (void)printf("Listener Report");
2270 case ICMP6_MEMBERSHIP_REDUCTION
:
2271 (void)printf("Listener Done");
2273 case ND_ROUTER_SOLICIT
:
2274 (void)printf("Router Solicitation");
2276 case ND_ROUTER_ADVERT
:
2277 (void)printf("Router Advertisement");
2279 case ND_NEIGHBOR_SOLICIT
:
2280 (void)printf("Neighbor Solicitation");
2282 case ND_NEIGHBOR_ADVERT
:
2283 (void)printf("Neighbor Advertisement");
2286 red
= (struct nd_redirect
*)icp
;
2287 (void)printf("Redirect\n");
2288 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2290 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2291 (void)printf("Destination: %s", ntop_buf
);
2292 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2294 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2295 (void)printf(" New Target: %s", ntop_buf
);
2297 case ICMP6_NI_QUERY
:
2298 (void)printf("Node Information Query");
2299 /* XXX ID + Seq + Data */
2300 ni
= (struct icmp6_nodeinfo
*)icp
;
2301 l
= end
- (u_char
*)(ni
+ 1);
2303 switch (ntohs(ni
->ni_qtype
)) {
2305 (void)printf("NOOP");
2307 case NI_QTYPE_SUPTYPES
:
2308 (void)printf("Supported qtypes");
2311 (void)printf("DNS name");
2313 case NI_QTYPE_NODEADDR
:
2314 (void)printf("nodeaddr");
2316 case NI_QTYPE_IPV4ADDR
:
2317 (void)printf("IPv4 nodeaddr");
2320 (void)printf("unknown qtype");
2323 if (options
& F_VERBOSE
) {
2324 switch (ni
->ni_code
) {
2325 case ICMP6_NI_SUBJ_IPV6
:
2326 if (l
== sizeof(struct in6_addr
) &&
2327 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2328 sizeof(ntop_buf
)) != NULL
) {
2329 (void)printf(", subject=%s(%s)",
2330 niqcode
[ni
->ni_code
], ntop_buf
);
2333 /* backward compat to -W */
2334 (void)printf(", oldfqdn");
2336 (void)printf(", invalid");
2340 case ICMP6_NI_SUBJ_FQDN
:
2341 if (end
== (u_char
*)(ni
+ 1)) {
2342 (void)printf(", no subject");
2345 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2346 cp
= (const u_char
*)(ni
+ 1);
2347 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2348 sizeof(dnsname
)) != NULL
)
2349 printf("(%s)", dnsname
);
2351 printf("(invalid)");
2353 case ICMP6_NI_SUBJ_IPV4
:
2354 if (l
== sizeof(struct in_addr
) &&
2355 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2356 sizeof(ntop_buf
)) != NULL
) {
2357 (void)printf(", subject=%s(%s)",
2358 niqcode
[ni
->ni_code
], ntop_buf
);
2360 (void)printf(", invalid");
2363 (void)printf(", invalid");
2368 case ICMP6_NI_REPLY
:
2369 (void)printf("Node Information Reply");
2370 /* XXX ID + Seq + Data */
2371 ni
= (struct icmp6_nodeinfo
*)icp
;
2373 switch (ntohs(ni
->ni_qtype
)) {
2375 (void)printf("NOOP");
2377 case NI_QTYPE_SUPTYPES
:
2378 (void)printf("Supported qtypes");
2381 (void)printf("DNS name");
2383 case NI_QTYPE_NODEADDR
:
2384 (void)printf("nodeaddr");
2386 case NI_QTYPE_IPV4ADDR
:
2387 (void)printf("IPv4 nodeaddr");
2390 (void)printf("unknown qtype");
2393 if (options
& F_VERBOSE
) {
2394 if (ni
->ni_code
>= sizeof(nircode
) / sizeof(nircode
[0]))
2395 printf(", invalid");
2397 printf(", %s", nircode
[ni
->ni_code
]);
2401 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2407 * Print an IP6 header.
2410 pr_iph(struct ip6_hdr
*ip6
)
2412 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2414 char ntop_buf
[INET6_ADDRSTRLEN
];
2416 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2417 tc
= (tc
>> 4) & 0x0f;
2418 tc
|= (ip6
->ip6_vfc
<< 4);
2420 printf("Vr TC Flow Plen Nxt Hlim\n");
2421 printf(" %1x %02x %05x %04x %02x %02x\n",
2422 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2423 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2424 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2425 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2426 printf("%s->", ntop_buf
);
2427 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2428 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2429 printf("%s\n", ntop_buf
);
2434 * Return an ascii host address as a dotted quad and optionally with
2438 pr_addr(struct sockaddr
*addr
, int addrlen
)
2440 static char buf
[NI_MAXHOST
];
2443 if ((options
& F_HOSTNAME
) == 0)
2444 flag
|= NI_NUMERICHOST
;
2446 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2454 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2457 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2459 u_char
*cp
= (u_char
*)ip6
, nh
;
2462 if (end
- (u_char
*)ip6
< (intptr_t)sizeof(*ip6
)) {
2467 hlen
= sizeof(*ip6
);
2471 while (end
- cp
>= 8) {
2473 case IPPROTO_HOPOPTS
:
2475 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2476 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2478 case IPPROTO_DSTOPTS
:
2480 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2481 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2483 case IPPROTO_FRAGMENT
:
2485 hlen
= sizeof(struct ip6_frag
);
2486 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2488 case IPPROTO_ROUTING
:
2490 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2491 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2496 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+2) << 2;
2497 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
2500 case IPPROTO_ICMPV6
:
2501 printf("ICMP6: type = %d, code = %d\n",
2508 printf("TCP: from port %u, to port %u (decimal)\n",
2509 (*cp
* 256 + *(cp
+ 1)),
2510 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2513 printf("UDP: from port %u, to port %u (decimal)\n",
2514 (*cp
* 256 + *(cp
+ 1)),
2515 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2518 printf("Unknown Header(%d)\n", nh
);
2522 if ((cp
+= hlen
) >= end
)
2537 fill(char *bp
, char *patp
)
2543 for (cp
= patp
; *cp
; cp
++)
2544 if (!isxdigit((unsigned char)*cp
))
2545 errx(1, "patterns must be specified as hex digits");
2547 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2548 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2549 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2550 &pat
[13], &pat
[14], &pat
[15]);
2555 kk
<= (int)(MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
));
2557 for (jj
= 0; jj
< ii
; ++jj
)
2558 bp
[jj
+ kk
] = pat
[jj
];
2559 if (!(options
& F_QUIET
)) {
2560 (void)printf("PATTERN: 0x");
2561 for (jj
= 0; jj
< ii
; ++jj
)
2562 (void)printf("%02x", bp
[jj
] & 0xFF);
2568 #ifdef IPSEC_POLICY_IPSEC
2570 setpolicy(int so
, char *policy
)
2575 return 0; /* ignore */
2577 buf
= ipsec_set_policy(policy
, strlen(policy
));
2579 errx(1, "%s", ipsec_strerror());
2580 if (prog_setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2581 ipsec_get_policylen(buf
)) < 0)
2582 warnx("Unable to set IPsec policy");
2596 u_int8_t digest
[16];
2599 char hbuf
[NI_MAXHOST
];
2600 struct in6_addr in6
;
2602 p
= strchr(name
, '.');
2604 p
= name
+ strlen(name
);
2606 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2607 return NULL
; /*label too long*/
2608 strncpy(hbuf
, name
, l
);
2609 hbuf
[(int)l
] = '\0';
2611 for (q
= name
; *q
; q
++) {
2612 if (isupper(*(unsigned char *)q
))
2613 *q
= tolower(*(unsigned char *)q
);
2616 /* generate 8 bytes of pseudo-random value. */
2617 memset(&ctxt
, 0, sizeof(ctxt
));
2620 MD5Update(&ctxt
, &c
, sizeof(c
));
2621 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2622 MD5Final(digest
, &ctxt
);
2624 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2625 return NULL
; /*XXX*/
2626 bcopy(digest
, &in6
.s6_addr
[12], 4);
2628 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2631 return strdup(hbuf
);
2635 timespec_to_sec(const struct timespec
*tp
)
2637 return tp
->tv_sec
+ tp
->tv_nsec
/ 1000000000.0;
2643 (void)fprintf(stderr
,
2644 "usage: ping6 [-dfH"
2645 #ifdef IPV6_USE_MIN_MTU
2649 #ifdef IPV6_REACHCONF
2653 #ifdef IPSEC_POLICY_IPSEC
2659 "] [-a [aAclsg]] [-b sockbufsiz] [-c count]\n"
2660 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2662 "\t[-x maxwait] [-S sourceaddr] "
2663 "[-s packetsize] [-h hoplimit]\n"
2664 "\t[-g gateway] [hops...] host\n");