1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static const char copyright
[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid
[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid
[] =
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>
137 #include <netinet6/ah.h>
138 #include <netinet6/ipsec.h>
141 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
152 #define MAXPACKETLEN 131072
154 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
155 #define ICMP6ECHOTMLEN sizeof(struct tv32)
156 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
157 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
158 /* FQDN case, 64 bits of nonce + 32 bits ttl */
159 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
160 #define EXTRA 256 /* for AH and various other headers. weird. */
161 #define DEFDATALEN ICMP6ECHOTMLEN
162 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
163 #define NROUTES 9 /* number of record route slots */
165 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
166 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
167 #define SET(bit) (A(bit) |= B(bit))
168 #define CLR(bit) (A(bit) &= (~B(bit)))
169 #define TST(bit) (A(bit) & B(bit))
171 #define F_FLOOD 0x0001
172 #define F_INTERVAL 0x0002
173 #define F_PINGFILLED 0x0008
174 #define F_QUIET 0x0010
175 #define F_RROUTE 0x0020
176 #define F_SO_DEBUG 0x0040
177 #define F_VERBOSE 0x0100
179 #ifdef IPSEC_POLICY_IPSEC
180 #define F_POLICY 0x0400
182 #define F_AUTHHDR 0x0200
183 #define F_ENCRYPT 0x0400
184 #endif /*IPSEC_POLICY_IPSEC*/
186 #define F_NODEADDR 0x0800
187 #define F_FQDN 0x1000
188 #define F_INTERFACE 0x2000
189 #define F_SRCADDR 0x4000
190 #define F_HOSTNAME 0x10000
191 #define F_FQDNOLD 0x20000
192 #define F_NIGROUP 0x40000
193 #define F_SUPTYPES 0x80000
194 #define F_NOMINMTU 0x100000
195 #define F_ONCE 0x200000
196 #define F_AUDIBLE 0x400000
197 #define F_MISSED 0x800000
198 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
201 #define IN6LEN sizeof(struct in6_addr)
202 #define SA6LEN sizeof(struct sockaddr_in6)
203 #define DUMMY_PORT 10101
205 #define SIN6(s) ((struct sockaddr_in6 *)(s))
209 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
210 * number of received sequence numbers we can keep track of. Change 128
211 * to 8192 for complete accuracy...
213 #define MAX_DUP_CHK (8 * 8192)
214 int mx_dup_ck
= MAX_DUP_CHK
;
215 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
217 struct addrinfo
*res
;
218 struct sockaddr_in6 dst
; /* who to ping6 */
219 struct sockaddr_in6 src
; /* src addr of this packet */
221 int datalen
= DEFDATALEN
;
222 int s
; /* socket file descriptor */
223 u_char outpack
[MAXPACKETLEN
];
224 char BSPACE
= '\b'; /* characters written for flood */
225 char BBELL
= '\a'; /* characters written for AUDIBLE */
228 int ident
; /* process id to identify our packets */
229 u_int8_t nonce
[8]; /* nonce field for node information */
230 int hoplimit
= -1; /* hoplimit */
231 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
233 unsigned int ifscope
;
237 long nmissedmax
; /* max value of ntransmitted - nreceived - 1 */
238 long npackets
; /* max packets to transmit */
239 long nreceived
; /* # of packets we got back */
240 long nrepeats
; /* number of duplicates */
241 long ntransmitted
; /* sequence # for outbound packets = #sent */
242 struct timeval interval
= {1, 0}; /* interval between packets */
245 int timing
; /* flag to do timing */
246 double tmin
= 999999999.0; /* minimum round trip time */
247 double tmax
= 0.0; /* maximum round trip time */
248 double tsum
= 0.0; /* sum of all times, for doing average */
249 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
251 /* for node addresses */
254 /* for ancillary data(advanced API) */
255 struct msghdr smsghdr
;
256 struct iovec smsgiov
;
259 volatile sig_atomic_t seenalrm
;
260 volatile sig_atomic_t seenint
;
262 volatile sig_atomic_t seeninfo
;
267 int how_so_traffic_class
= 0;
268 int so_traffic_class
= -1;
270 int main(int, char *[]);
271 void fill(char *, char *);
272 int get_hoplim(struct msghdr
*);
273 int get_pathmtu(struct msghdr
*);
274 int get_tclass(struct msghdr
*);
275 int get_so_traffic_class(struct msghdr
*);
276 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
278 void retransmit(void);
280 size_t pingerlen(void);
282 const char *pr_addr(struct sockaddr
*, int);
283 void pr_icmph(struct icmp6_hdr
*, u_char
*);
284 void pr_iph(struct ip6_hdr
*);
285 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
286 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
287 int myechoreply(const struct icmp6_hdr
*);
288 int mynireply(const struct icmp6_nodeinfo
*);
289 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
291 void pr_pack(u_char
*, int, struct msghdr
*);
292 void pr_exthdrs(struct msghdr
*);
293 void pr_ip6opt(void *, size_t);
294 void pr_rthdr(void *, size_t);
295 int pr_bitrange(u_int32_t
, int, int);
296 void pr_retip(struct ip6_hdr
*, u_char
*);
298 void tvsub(struct timeval
*, struct timeval
*);
299 int setpolicy(int, char *);
300 char *nigroup(char *);
308 struct itimerval itimer
;
309 struct sockaddr_in6 from
;
310 #ifndef HAVE_ARC4RANDOM
316 struct timeval timeout
, *tv
;
318 struct addrinfo hints
;
320 struct pollfd fdmaskp
[1];
326 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
327 u_char
*datap
, *packet
;
328 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
330 struct cmsghdr
*scmsgp
= NULL
;
332 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
337 struct in6_pktinfo
*pktinfo
= NULL
;
338 #ifdef USE_RFC2292BIS
339 struct ip6_rthdr
*rthdr
= NULL
;
341 #ifdef IPSEC_POLICY_IPSEC
342 char *policy_in
= NULL
;
343 char *policy_out
= NULL
;
347 #ifdef IPV6_USE_MIN_MTU
350 int tclass
= -2; /* T_CLASS value -1 means default, so -2 means do not bother */
352 /* just to be sure */
353 memset(&smsghdr
, 0, sizeof(smsghdr
));
354 memset(&smsgiov
, 0, sizeof(smsgiov
));
357 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
361 #ifdef IPSEC_POLICY_IPSEC
365 #endif /*IPSEC_POLICY_IPSEC*/
367 while ((ch
= getopt(argc
, argv
,
368 "a:b:B:Cc:dfHg:h:I:i:k:l:mnNop:qrRS:s:tvwWz:" ADDOPTS
)) != -1) {
375 options
&= ~F_NOUSERDATA
;
376 options
|= F_NODEADDR
;
377 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
380 naflags
|= NI_NODEADDR_FLAG_ALL
;
384 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
388 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
392 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
396 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
398 case 'A': /* experimental. not in the spec */
399 #ifdef NI_NODEADDR_FLAG_ANYCAST
400 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
404 "-a A is not supported on the platform");
415 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
418 lsockbufsize
= strtoul(optarg
, &e
, 10);
419 sockbufsize
= lsockbufsize
;
420 if (errno
|| !*optarg
|| *e
||
421 sockbufsize
!= lsockbufsize
)
422 errx(1, "invalid socket buffer size");
425 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
435 npackets
= strtol(optarg
, &e
, 10);
436 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
438 "illegal number of packets -- %s", optarg
);
441 options
|= F_SO_DEBUG
;
446 errx(1, "Must be superuser to flood ping");
449 setbuf(stdout
, (char *)NULL
);
455 options
|= F_HOSTNAME
;
457 case 'h': /* hoplimit */
458 hoplimit
= strtol(optarg
, &e
, 10);
459 if (*optarg
== '\0' || *e
!= '\0')
460 errx(1, "illegal hoplimit %s", optarg
);
461 if (255 < hoplimit
|| hoplimit
< -1)
463 "illegal hoplimit -- %s", optarg
);
467 options
|= F_INTERFACE
;
468 #ifndef USE_SIN6_SCOPE_ID
472 case 'i': /* wait between sending packets */
473 intval
= strtod(optarg
, &e
);
474 if (*optarg
== '\0' || *e
!= '\0')
475 errx(1, "illegal timing interval %s", optarg
);
476 if (intval
< 1 && getuid()) {
477 errx(1, "%s: only root may use interval < 1s",
480 interval
.tv_sec
= (long)intval
;
482 (long)((intval
- interval
.tv_sec
) * 1000000);
483 if (interval
.tv_sec
< 0)
484 errx(1, "illegal timing interval %s", optarg
);
485 /* less than 1/hz does not make sense */
486 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 1) {
487 warnx("too small interval, raised to .000001");
488 interval
.tv_usec
= 1;
490 options
|= F_INTERVAL
;
493 how_so_traffic_class
++;
494 so_traffic_class
= atoi(optarg
);
500 errx(1, "Must be superuser to preload");
502 preload
= strtol(optarg
, &e
, 10);
503 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
504 errx(1, "illegal preload value -- %s", optarg
);
507 #ifdef IPV6_USE_MIN_MTU
511 errx(1, "-%c is not supported on this platform", ch
);
515 options
&= ~F_HOSTNAME
;
518 options
|= F_NIGROUP
;
523 case 'p': /* fill buffer with user pattern */
524 options
|= F_PINGFILLED
;
525 fill((char *)datap
, optarg
);
531 options
|= F_AUDIBLE
;
537 memset(&hints
, 0, sizeof(struct addrinfo
));
538 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
539 hints
.ai_family
= AF_INET6
;
540 hints
.ai_socktype
= SOCK_RAW
;
541 hints
.ai_protocol
= IPPROTO_ICMPV6
;
543 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
545 errx(1, "invalid source address: %s",
546 gai_strerror(ret_ga
));
549 * res->ai_family must be AF_INET6 and res->ai_addrlen
550 * must be sizeof(src).
552 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
553 srclen
= res
->ai_addrlen
;
555 options
|= F_SRCADDR
;
557 case 's': /* size of packet to send */
558 datalen
= strtol(optarg
, &e
, 10);
559 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
560 errx(1, "illegal datalen value -- %s", optarg
);
561 if (datalen
> MAXDATALEN
) {
563 "datalen value too large, maximum is %d",
568 options
&= ~F_NOUSERDATA
;
569 options
|= F_SUPTYPES
;
572 options
|= F_VERBOSE
;
575 options
&= ~F_NOUSERDATA
;
579 options
&= ~F_NOUSERDATA
;
580 options
|= F_FQDNOLD
;
583 tclass
= (int)strtol(optarg
, &e
, 10);
584 if (tclass
< -1 || *optarg
== '\0' || *e
!= '\0')
585 errx(1, "illegal TOS value -- %s", optarg
);
588 "TOS value too large, maximum is %d",
593 #ifdef IPSEC_POLICY_IPSEC
596 if (!strncmp("in", optarg
, 2)) {
597 if ((policy_in
= strdup(optarg
)) == NULL
)
599 } else if (!strncmp("out", optarg
, 3)) {
600 if ((policy_out
= strdup(optarg
)) == NULL
)
603 errx(1, "invalid security policy");
607 options
|= F_AUTHHDR
;
610 options
|= F_ENCRYPT
;
612 #endif /*IPSEC_POLICY_IPSEC*/
620 if (boundif
!= NULL
&& (ifscope
= if_nametoindex(boundif
)) == 0)
621 errx(1, "bad interface name");
632 #ifdef IPV6_RECVRTHDR /* 2292bis */
633 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
636 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
639 errx(1, "too many intermediate hops");
645 if (options
& F_NIGROUP
) {
646 target
= nigroup(argv
[argc
- 1]);
647 if (target
== NULL
) {
652 target
= argv
[argc
- 1];
655 memset(&hints
, 0, sizeof(struct addrinfo
));
656 hints
.ai_flags
= AI_CANONNAME
;
657 hints
.ai_family
= AF_INET6
;
658 hints
.ai_socktype
= SOCK_RAW
;
659 hints
.ai_protocol
= IPPROTO_ICMPV6
;
661 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
663 errx(1, "getaddrinfo -- %s", gai_strerror(ret_ga
));
664 if (res
->ai_canonname
)
665 hostname
= res
->ai_canonname
;
670 errx(1, "getaddrinfo failed");
672 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
674 res
->ai_socktype
= getuid() ? SOCK_DGRAM
: SOCK_RAW
;
675 res
->ai_protocol
= IPPROTO_ICMPV6
;
677 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
678 res
->ai_protocol
)) < 0)
682 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
683 (char *)&ifscope
, sizeof (ifscope
)) != 0)
684 err(1, "setsockopt(IPV6_BOUND_IF)");
688 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
689 (char *)&nocell
, sizeof (nocell
)) != 0)
690 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
693 /* set the source address if specified. */
694 if ((options
& F_SRCADDR
) &&
695 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
699 /* set the gateway (next hop) if specified */
701 struct addrinfo ghints
, *gres
;
704 memset(&ghints
, 0, sizeof(ghints
));
705 ghints
.ai_family
= AF_INET6
;
706 ghints
.ai_socktype
= SOCK_RAW
;
707 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
709 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
711 errx(1, "getaddrinfo for the gateway %s: %s",
712 gateway
, gai_strerror(error
));
714 if (gres
->ai_next
&& (options
& F_VERBOSE
))
715 warnx("gateway resolves to multiple addresses");
717 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
718 gres
->ai_addr
, gres
->ai_addrlen
)) {
719 err(1, "setsockopt(IPV6_NEXTHOP)");
726 * let the kerel pass extension headers of incoming packets,
727 * for privileged socket options
729 if ((options
& F_VERBOSE
) != 0) {
732 #ifdef IPV6_RECVHOPOPTS
733 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
735 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
736 #else /* old adv. API */
737 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
739 err(1, "setsockopt(IPV6_HOPOPTS)");
741 #ifdef IPV6_RECVDSTOPTS
742 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
744 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
745 #else /* old adv. API */
746 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
748 err(1, "setsockopt(IPV6_DSTOPTS)");
750 #ifdef IPV6_RECVRTHDRDSTOPTS
751 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
753 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
757 /* revoke root privilege */
761 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
762 errx(1, "-f and -i incompatible options");
764 if ((options
& F_NOUSERDATA
) == 0) {
765 if (datalen
>= sizeof(struct tv32
)) {
766 /* we can time transfer */
770 /* in F_VERBOSE case, we may get non-echoreply packets*/
771 if (options
& F_VERBOSE
)
772 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
774 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
776 /* suppress timing for node information query */
779 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
782 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
783 err(1, "Unable to allocate packet");
784 if (!(options
& F_PINGFILLED
))
785 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
788 ident
= getpid() & 0xFFFF;
789 #ifndef HAVE_ARC4RANDOM
790 gettimeofday(&seed
, NULL
);
791 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
792 memset(nonce
, 0, sizeof(nonce
));
793 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
794 *((int *)&nonce
[i
]) = rand();
796 memset(nonce
, 0, sizeof(nonce
));
797 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
798 *((u_int32_t
*)&nonce
[i
]) = arc4random();
803 if (options
& F_SO_DEBUG
)
804 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
806 optval
= IPV6_DEFHLIM
;
807 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
808 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
809 &optval
, sizeof(optval
)) == -1)
810 err(1, "IPV6_MULTICAST_HOPS");
811 #ifdef IPV6_USE_MIN_MTU
813 optval
= mflag
> 1 ? 0 : 1;
815 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
816 &optval
, sizeof(optval
)) == -1)
817 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
819 #ifdef IPV6_RECVPATHMTU
822 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
823 &optval
, sizeof(optval
)) == -1)
824 err(1, "setsockopt(IPV6_RECVPATHMTU)");
826 #endif /* IPV6_RECVPATHMTU */
827 #endif /* IPV6_USE_MIN_MTU */
830 #ifdef IPSEC_POLICY_IPSEC
831 if (options
& F_POLICY
) {
832 if (setpolicy(s
, policy_in
) < 0)
833 errx(1, "%s", ipsec_strerror());
834 if (setpolicy(s
, policy_out
) < 0)
835 errx(1, "%s", ipsec_strerror());
838 if (options
& F_AUTHHDR
) {
839 optval
= IPSEC_LEVEL_REQUIRE
;
840 #ifdef IPV6_AUTH_TRANS_LEVEL
841 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
842 &optval
, sizeof(optval
)) == -1)
843 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
845 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
846 &optval
, sizeof(optval
)) == -1)
847 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
850 if (options
& F_ENCRYPT
) {
851 optval
= IPSEC_LEVEL_REQUIRE
;
852 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
853 &optval
, sizeof(optval
)) == -1)
854 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
856 #endif /*IPSEC_POLICY_IPSEC*/
861 struct icmp6_filter filt
;
862 if (!(options
& F_VERBOSE
)) {
863 ICMP6_FILTER_SETBLOCKALL(&filt
);
864 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
865 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
866 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
868 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
870 ICMP6_FILTER_SETPASSALL(&filt
);
872 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
874 err(1, "setsockopt(ICMP6_FILTER)");
876 #endif /*ICMP6_FILTER*/
878 /* let the kerel pass extension headers of incoming packets */
879 if ((options
& F_VERBOSE
) != 0) {
882 #ifdef IPV6_RECVRTHDR
883 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
885 err(1, "setsockopt(IPV6_RECVRTHDR)");
886 #else /* old adv. API */
887 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
889 err(1, "setsockopt(IPV6_RTHDR)");
896 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVTCLASS
, &on
, sizeof(on
)))
897 err(1, "setsockopt(IPV6_RECVTCLASS)");
899 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
, sizeof(tclass
)))
900 err(1, "setsockopt(IPV6_TCLASS)");
905 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
906 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
907 &optval, sizeof(optval)) == -1)
908 err(1, "IPV6_MULTICAST_LOOP");
911 /* Specify the outgoing interface and/or the source address */
913 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
916 ip6optlen
+= CMSG_SPACE(sizeof(int));
919 ip6optlen
+= CMSG_SPACE(sizeof(int));
921 if (how_so_traffic_class
== 1 && so_traffic_class
> 0) {
922 (void)setsockopt(s
, SOL_SOCKET
, SO_TRAFFIC_CLASS
, (void *)&so_traffic_class
,
923 sizeof(so_traffic_class
));
925 if (how_so_traffic_class
> 0) {
927 (void)setsockopt(s
, SOL_SOCKET
, SO_RECV_TRAFFIC_CLASS
, (void *)&on
,
930 if (how_so_traffic_class
> 1)
931 ip6optlen
+= CMSG_SPACE(sizeof(int));
933 /* set IP6 packet options */
935 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
936 errx(1, "can't allocate enough memory");
937 smsghdr
.msg_control
= (caddr_t
)scmsg
;
938 smsghdr
.msg_controllen
= ip6optlen
;
939 scmsgp
= (struct cmsghdr
*)scmsg
;
942 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
943 memset(pktinfo
, 0, sizeof(*pktinfo
));
944 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
945 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
946 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
947 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
950 /* set the outgoing interface */
952 #ifndef USE_SIN6_SCOPE_ID
953 /* pktinfo must have already been allocated */
954 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
955 errx(1, "%s: invalid interface name", ifname
);
957 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
958 errx(1, "%s: invalid interface name", ifname
);
961 if (hoplimit
!= -1) {
962 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
963 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
964 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
965 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
967 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
970 if (argc
> 1) { /* some intermediate addrs are specified */
972 #ifdef USE_RFC2292BIS
976 #ifdef USE_RFC2292BIS
977 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
978 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
979 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
980 scmsgp
->cmsg_type
= IPV6_RTHDR
;
981 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
982 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
983 IPV6_RTHDR_TYPE_0
, argc
- 1);
985 errx(1, "can't initialize rthdr");
986 #else /* old advanced API */
987 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
988 IPV6_RTHDR_TYPE_0
)) == 0)
989 errx(1, "can't initialize rthdr");
990 #endif /* USE_RFC2292BIS */
992 for (hops
= 0; hops
< argc
- 1; hops
++) {
993 struct addrinfo
*iaip
;
995 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
997 errx(1, "%s", gai_strerror(error
));
998 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
1000 "bad addr family of an intermediate addr");
1002 #ifdef USE_RFC2292BIS
1003 if (inet6_rth_add(rthdr
,
1004 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
1005 errx(1, "can't add an intermediate node");
1006 #else /* old advanced API */
1007 if (inet6_rthdr_add(scmsgp
,
1008 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
1010 errx(1, "can't add an intermediate node");
1011 #endif /* USE_RFC2292BIS */
1015 #ifndef USE_RFC2292BIS
1016 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
1017 errx(1, "can't set the last flag");
1020 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1024 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1025 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1026 scmsgp
->cmsg_type
= IPV6_TCLASS
;
1027 *(int *)(CMSG_DATA(scmsgp
)) = tclass
;
1029 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1031 if (how_so_traffic_class
> 1) {
1032 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1033 scmsgp
->cmsg_level
= SOL_SOCKET
;
1034 scmsgp
->cmsg_type
= SO_TRAFFIC_CLASS
;
1035 *(int *)(CMSG_DATA(scmsgp
)) = so_traffic_class
;
1037 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1039 if (!(options
& F_SRCADDR
)) {
1041 * get the source address. XXX since we revoked the root
1042 * privilege, we cannot use a raw socket for this.
1045 socklen_t len
= sizeof(src
);
1047 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1048 err(1, "UDP socket");
1051 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
1052 (char *)&ifscope
, sizeof (ifscope
)) != 0)
1053 err(1, "setsockopt(IPV6_BOUND_IF)");
1057 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
1058 (char *)&nocell
, sizeof (nocell
)) != 0)
1059 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
1062 src
.sin6_family
= AF_INET6
;
1063 src
.sin6_addr
= dst
.sin6_addr
;
1064 src
.sin6_port
= ntohs(DUMMY_PORT
);
1065 src
.sin6_scope_id
= dst
.sin6_scope_id
;
1067 #ifdef USE_RFC2292BIS
1069 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
1070 (void *)pktinfo
, sizeof(*pktinfo
)))
1071 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1073 if (hoplimit
!= -1 &&
1074 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1075 (void *)&hoplimit
, sizeof(hoplimit
)))
1076 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1078 if (hoplimit
!= -1 &&
1079 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1080 (void *)&hoplimit
, sizeof(hoplimit
)))
1081 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1084 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
1085 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
1086 err(1, "UDP setsockopt(IPV6_RTHDR)");
1087 #else /* old advanced API */
1088 if (smsghdr
.msg_control
&&
1089 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
1090 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
1091 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1094 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
1095 err(1, "UDP connect");
1097 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
1098 err(1, "getsockname");
1103 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1105 if (datalen
> sockbufsize
)
1106 warnx("you need -b to increase socket buffer size");
1107 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
1108 sizeof(sockbufsize
)) < 0)
1109 err(1, "setsockopt(SO_SNDBUF)");
1110 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
1111 sizeof(sockbufsize
)) < 0)
1112 err(1, "setsockopt(SO_RCVBUF)");
1115 if (datalen
> 8 * 1024) /*XXX*/
1116 warnx("you need -b to increase socket buffer size");
1118 * When pinging the broadcast address, you can get a lot of
1119 * answers. Doing something so evil is useful if you are trying
1120 * to stress the ethernet, or just want to fill the arp cache
1121 * to get some stuff for /etc/ethers.
1124 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1130 #ifndef USE_SIN6_SCOPE_ID
1131 #ifdef IPV6_RECVPKTINFO
1132 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1133 sizeof(optval
)) < 0)
1134 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1135 #else /* old adv. API */
1136 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1137 sizeof(optval
)) < 0)
1138 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1140 #endif /* USE_SIN6_SCOPE_ID */
1141 #ifdef IPV6_RECVHOPLIMIT
1142 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1143 sizeof(optval
)) < 0)
1144 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1145 #else /* old adv. API */
1146 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1147 sizeof(optval
)) < 0)
1148 warn("setsockopt(IPV6_HOPLIMIT, %d, %lu)", optval
, sizeof(optval
)); /* XXX err? */
1151 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1152 (unsigned long)(pingerlen() - 8));
1153 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1154 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1156 while (preload
--) /* Fire off them quickies. */
1159 (void)signal(SIGINT
, onsignal
);
1161 (void)signal(SIGINFO
, onsignal
);
1164 if ((options
& F_FLOOD
) == 0) {
1165 (void)signal(SIGALRM
, onsignal
);
1166 itimer
.it_interval
= interval
;
1167 itimer
.it_value
= interval
;
1168 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1169 if (ntransmitted
== 0)
1174 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1175 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1179 seenalrm
= seenint
= 0;
1184 /* For control (ancillary) data received from recvmsg() */
1185 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1191 struct iovec iov
[2];
1193 /* signal handling */
1195 /* last packet sent, timeout reached? */
1196 if (npackets
&& ntransmitted
>= npackets
)
1215 if (options
& F_FLOOD
) {
1221 timeout
.tv_usec
= 10000;
1233 fdmaskp
[0].events
= POLLIN
;
1234 cc
= poll(fdmaskp
, 1, timeout
);
1236 memset(fdmaskp
, 0, fdmasks
);
1238 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1241 if (errno
!= EINTR
) {
1253 m
.msg_name
= (caddr_t
)&from
;
1254 m
.msg_namelen
= sizeof(from
);
1255 memset(&iov
, 0, sizeof(iov
));
1256 iov
[0].iov_base
= (caddr_t
)packet
;
1257 iov
[0].iov_len
= packlen
;
1260 memset(cm
, 0, CONTROLLEN
);
1261 m
.msg_control
= (void *)cm
;
1262 m
.msg_controllen
= CONTROLLEN
;
1264 cc
= recvmsg(s
, &m
, 0);
1266 if (errno
!= EINTR
) {
1271 } else if (cc
== 0) {
1275 * receive control messages only. Process the
1276 * exceptions (currently the only possiblity is
1277 * a path MTU notification.)
1279 if ((mtu
= get_pathmtu(&m
)) > 0) {
1280 if ((options
& F_VERBOSE
) != 0) {
1281 printf("new path MTU (%d) is "
1288 * an ICMPv6 message (probably an echoreply) arrived.
1290 pr_pack(packet
, cc
, &m
);
1292 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1293 (npackets
> 0 && nreceived
>= npackets
))
1295 if (ntransmitted
- nreceived
- 1 > nmissedmax
) {
1296 nmissedmax
= ntransmitted
- nreceived
- 1;
1297 if (options
& F_MISSED
)
1298 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1302 exit(nreceived
== 0 ? 2 : 0);
1327 * This routine transmits another ping6.
1332 struct itimerval itimer
;
1338 * If we're not transmitting any more packets, change the timer
1339 * to wait two round-trip times if we've received any packets or
1340 * ten seconds if we haven't.
1344 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1345 if (itimer
.it_value
.tv_sec
== 0)
1346 itimer
.it_value
.tv_sec
= 1;
1348 itimer
.it_value
.tv_sec
= MAXWAIT
;
1349 itimer
.it_interval
.tv_sec
= 0;
1350 itimer
.it_interval
.tv_usec
= 0;
1351 itimer
.it_value
.tv_usec
= 0;
1353 (void)signal(SIGALRM
, onsignal
);
1354 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1359 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1360 * will be added on by the kernel. The ID field is our UNIX process ID,
1361 * and the sequence number is an ascending integer. The first 8 bytes
1362 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1363 * byte-order, to compute the round-trip time.
1370 if (options
& F_FQDN
)
1371 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1372 else if (options
& F_FQDNOLD
)
1374 else if (options
& F_NODEADDR
)
1375 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1376 else if (options
& F_SUPTYPES
)
1379 l
= ICMP6ECHOLEN
+ datalen
;
1387 struct icmp6_hdr
*icp
;
1388 struct iovec iov
[2];
1390 struct icmp6_nodeinfo
*nip
;
1393 if (npackets
&& ntransmitted
>= npackets
)
1394 return(-1); /* no more transmission */
1396 icp
= (struct icmp6_hdr
*)outpack
;
1397 nip
= (struct icmp6_nodeinfo
*)outpack
;
1398 memset(icp
, 0, sizeof(*icp
));
1399 icp
->icmp6_cksum
= 0;
1400 seq
= ntransmitted
++;
1401 CLR(seq
% mx_dup_ck
);
1403 if (options
& F_FQDN
) {
1404 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1405 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1406 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1407 nip
->ni_flags
= htons(0);
1409 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1410 sizeof(nip
->icmp6_ni_nonce
));
1411 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1413 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1414 sizeof(dst
.sin6_addr
));
1415 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1417 } else if (options
& F_FQDNOLD
) {
1418 /* packet format in 03 draft - no Subject data on queries */
1419 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1420 icp
->icmp6_code
= 0; /* code field is always 0 */
1421 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1422 nip
->ni_flags
= htons(0);
1424 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1425 sizeof(nip
->icmp6_ni_nonce
));
1426 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1430 } else if (options
& F_NODEADDR
) {
1431 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1432 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1433 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1434 nip
->ni_flags
= naflags
;
1436 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1437 sizeof(nip
->icmp6_ni_nonce
));
1438 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1440 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1441 sizeof(dst
.sin6_addr
));
1442 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1444 } else if (options
& F_SUPTYPES
) {
1445 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1446 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1447 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1448 /* we support compressed bitmap */
1449 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1451 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1452 sizeof(nip
->icmp6_ni_nonce
));
1453 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1457 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1458 icp
->icmp6_code
= 0;
1459 icp
->icmp6_id
= htons(ident
);
1460 icp
->icmp6_seq
= ntohs(seq
);
1464 (void)gettimeofday(&tv
, NULL
);
1465 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1466 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1467 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1469 cc
= ICMP6ECHOLEN
+ datalen
;
1473 if (pingerlen() != cc
)
1474 errx(1, "internal error; length mismatch");
1477 smsghdr
.msg_name
= (caddr_t
)&dst
;
1478 smsghdr
.msg_namelen
= sizeof(dst
);
1479 memset(&iov
, 0, sizeof(iov
));
1480 iov
[0].iov_base
= (caddr_t
)outpack
;
1481 iov
[0].iov_len
= cc
;
1482 smsghdr
.msg_iov
= iov
;
1483 smsghdr
.msg_iovlen
= 1;
1485 i
= sendmsg(s
, &smsghdr
, 0);
1487 if (i
< 0 || i
!= cc
) {
1490 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1493 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1494 (void)write(STDOUT_FILENO
, &DOT
, 1);
1501 const struct icmp6_hdr
*icp
;
1503 if (ntohs(icp
->icmp6_id
) == ident
)
1511 const struct icmp6_nodeinfo
*nip
;
1513 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1514 nonce
+ sizeof(u_int16_t
),
1515 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1522 dnsdecode(sp
, ep
, base
, buf
, bufsiz
)
1525 const u_char
*base
; /*base for compressed name*/
1531 char cresult
[NS_MAXDNAME
+ 1];
1542 if (i
== 0 || cp
!= *sp
) {
1543 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1544 return NULL
; /*result overrun*/
1550 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1551 /* DNS compression */
1555 comp
= base
+ (i
& 0x3f);
1556 if (dnsdecode(&comp
, cp
, base
, cresult
,
1557 sizeof(cresult
)) == NULL
)
1559 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1560 return NULL
; /*result overrun*/
1562 } else if ((i
& 0x3f) == i
) {
1564 return NULL
; /*source overrun*/
1565 while (i
-- > 0 && cp
< ep
) {
1566 l
= snprintf(cresult
, sizeof(cresult
),
1567 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1568 if (l
>= sizeof(cresult
) || l
< 0)
1570 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1571 return NULL
; /*result overrun*/
1575 return NULL
; /*invalid label*/
1578 return NULL
; /*not terminated*/
1586 * Print out the packet, if it came from us. This logic is necessary
1587 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1588 * which arrive ('tis only fair). This permits multiple copies of this
1589 * program to be run without having intermingled output (or statistics!).
1592 pr_pack(buf
, cc
, mhdr
)
1595 struct msghdr
*mhdr
;
1597 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1598 struct icmp6_hdr
*icp
;
1599 struct icmp6_nodeinfo
*ni
;
1602 struct sockaddr
*from
;
1604 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1605 struct in6_pktinfo
*pktinfo
= NULL
;
1606 struct timeval tv
, tp
;
1608 double triptime
= 0;
1613 char dnsname
[NS_MAXDNAME
+ 1];
1617 (void)gettimeofday(&tv
, NULL
);
1619 if (!mhdr
|| !mhdr
->msg_name
||
1620 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1621 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1622 if (options
& F_VERBOSE
)
1623 warnx("invalid peername");
1626 from
= (struct sockaddr
*)mhdr
->msg_name
;
1627 fromlen
= mhdr
->msg_namelen
;
1628 if (cc
< sizeof(struct icmp6_hdr
)) {
1629 if (options
& F_VERBOSE
)
1630 warnx("packet too short (%d bytes) from %s", cc
,
1631 pr_addr(from
, fromlen
));
1634 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1635 (options
& F_VERBOSE
) != 0)
1636 warnx("some control data discarded, insufficient buffer size");
1637 icp
= (struct icmp6_hdr
*)buf
;
1638 ni
= (struct icmp6_nodeinfo
*)buf
;
1641 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1642 warnx("failed to get receiving hop limit");
1645 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1646 warnx("failed to get receiving packet information");
1649 if (rcvtclass
&& (tclass
= get_tclass(mhdr
)) == -1) {
1650 warnx("failed to get receiving traffic class");
1654 if (how_so_traffic_class
> 0)
1655 sotc
= get_so_traffic_class(mhdr
);
1657 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1658 seq
= ntohs(icp
->icmp6_seq
);
1661 tpp
= (struct tv32
*)(icp
+ 1);
1662 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1663 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1665 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1666 ((double)tv
.tv_usec
) / 1000.0;
1668 tsumsq
+= triptime
* triptime
;
1669 if (triptime
< tmin
)
1671 if (triptime
> tmax
)
1675 if (TST(seq
% mx_dup_ck
)) {
1680 SET(seq
% mx_dup_ck
);
1684 if (options
& F_QUIET
)
1687 if (options
& F_FLOOD
)
1688 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1690 if (options
& F_AUDIBLE
)
1691 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1692 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1693 pr_addr(from
, fromlen
), seq
);
1694 (void)printf(" hlim=%d", hoplim
);
1695 if ((options
& F_VERBOSE
) != 0) {
1696 struct sockaddr_in6 dstsa
;
1698 memset(&dstsa
, 0, sizeof(dstsa
));
1699 dstsa
.sin6_family
= AF_INET6
;
1700 dstsa
.sin6_len
= sizeof(dstsa
);
1701 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1702 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1703 (void)printf(" dst=%s",
1704 pr_addr((struct sockaddr
*)&dstsa
,
1708 (void)printf(" time=%.3f ms", triptime
);
1710 if (!IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
1711 (void)printf("(DUP!)");
1714 (void)printf(" tclass=%d", tclass
);
1716 (void)printf(" sotc=%d", sotc
);
1717 /* check the data */
1718 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1719 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1720 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1722 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1727 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1728 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1730 if (TST(seq
% mx_dup_ck
)) {
1735 SET(seq
% mx_dup_ck
);
1739 if (options
& F_QUIET
)
1742 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1744 switch (ntohs(ni
->ni_code
)) {
1745 case ICMP6_NI_SUCCESS
:
1747 case ICMP6_NI_REFUSED
:
1748 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1750 case ICMP6_NI_UNKNOWN
:
1751 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1754 printf("unknown code 0x%x, type 0x%x",
1755 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1759 switch (ntohs(ni
->ni_qtype
)) {
1761 printf("NodeInfo NOOP");
1763 case NI_QTYPE_SUPTYPES
:
1764 pr_suptypes(ni
, end
- (u_char
*)ni
);
1766 case NI_QTYPE_NODEADDR
:
1767 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1770 default: /* XXX: for backward compatibility */
1771 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1772 if (buf
[off
+ ICMP6_NIRLEN
] ==
1773 cc
- off
- ICMP6_NIRLEN
- 1)
1778 cp
++; /* skip length */
1780 safeputc(*cp
& 0xff);
1786 if (dnsdecode((const u_char
**)&cp
, end
,
1787 (const u_char
*)(ni
+ 1), dnsname
,
1788 sizeof(dnsname
)) == NULL
) {
1793 * name-lookup special handling for
1796 if (cp
+ 1 <= end
&& !*cp
&&
1797 strlen(dnsname
) > 0) {
1798 dnsname
[strlen(dnsname
) - 1] = '\0';
1801 printf("%s%s", i
> 0 ? "," : "",
1805 if (options
& F_VERBOSE
) {
1809 (void)printf(" ("); /*)*/
1811 switch (ni
->ni_code
) {
1812 case ICMP6_NI_REFUSED
:
1813 (void)printf("refused");
1816 case ICMP6_NI_UNKNOWN
:
1817 (void)printf("unknown qtype");
1822 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1823 /* case of refusion, unknown */
1828 ttl
= (int32_t)ntohl(*(u_int32_t
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1831 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1832 (void)printf("TTL=%d:meaningless",
1836 (void)printf("TTL=%d:invalid",
1839 (void)printf("TTL=%d", ttl
);
1849 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1858 if (buf
[off
+ ICMP6_NIRLEN
] !=
1859 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1862 (void)printf("invalid namelen:%d/%lu",
1863 buf
[off
+ ICMP6_NIRLEN
],
1864 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1874 /* We've got something other than an ECHOREPLY */
1875 if (!(options
& F_VERBOSE
))
1877 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1881 if (!(options
& F_FLOOD
)) {
1882 (void)putchar('\n');
1883 if (options
& F_VERBOSE
)
1885 (void)fflush(stdout
);
1892 struct msghdr
*mhdr
;
1899 bufp
= mhdr
->msg_control
;
1900 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1901 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1902 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1905 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1908 switch (cm
->cmsg_type
) {
1910 printf(" HbH Options: ");
1911 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1914 #ifdef IPV6_RTHDRDSTOPTS
1915 case IPV6_RTHDRDSTOPTS
:
1917 printf(" Dst Options: ");
1918 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1921 printf(" Routing: ");
1922 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1928 #ifdef USE_RFC2292BIS
1930 pr_ip6opt(void *extbuf
, size_t bufsize
)
1932 struct ip6_hbh
*ext
;
1935 socklen_t extlen
, len
, origextlen
;
1941 ext
= (struct ip6_hbh
*)extbuf
;
1942 extlen
= (ext
->ip6h_len
+ 1) * 8;
1943 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1944 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1947 * Bounds checking on the ancillary data buffer:
1948 * subtract the size of a cmsg structure from the buffer size.
1950 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
1951 origextlen
= extlen
;
1952 extlen
= bufsize
- CMSG_SPACE(0);
1953 warnx("options truncated, showing only %u (total=%u)",
1954 (unsigned int)(extlen
/ 8 - 1),
1955 (unsigned int)(ext
->ip6h_len
));
1960 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1961 &type
, &len
, &databuf
);
1962 if (currentlen
== -1)
1966 * Note that inet6_opt_next automatically skips any padding
1971 offset
= inet6_opt_get_val(databuf
, offset
,
1972 &value4
, sizeof(value4
));
1973 printf(" Jumbo Payload Opt: Length %u\n",
1974 (u_int32_t
)ntohl(value4
));
1976 case IP6OPT_ROUTER_ALERT
:
1978 offset
= inet6_opt_get_val(databuf
, offset
,
1979 &value2
, sizeof(value2
));
1980 printf(" Router Alert Opt: Type %u\n",
1984 printf(" Received Opt %u len %lu\n",
1985 type
, (unsigned long)len
);
1991 #else /* !USE_RFC2292BIS */
1994 pr_ip6opt(void *extbuf
, size_t bufsize __unused
)
1999 #endif /* USE_RFC2292BIS */
2001 #ifdef USE_RFC2292BIS
2003 pr_rthdr(void *extbuf
, size_t bufsize
)
2005 struct in6_addr
*in6
;
2006 char ntopbuf
[INET6_ADDRSTRLEN
];
2007 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
2008 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
2010 /* print fixed part of the header */
2011 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
2012 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
2013 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
2014 printf("%d segments, ", segments
);
2015 printf("%d left\n", rh
->ip6r_segleft
);
2017 printf("segments unknown, ");
2018 printf("%d left\n", rh
->ip6r_segleft
);
2023 * Bounds checking on the ancillary data buffer. When calculating
2024 * the number of items to show keep in mind:
2025 * - The size of the cmsg structure
2026 * - The size of one segment (the size of a Type 0 routing header)
2027 * - When dividing add a fudge factor of one in case the
2028 * dividend is not evenly divisible by the divisor
2030 rthsize
= (rh
->ip6r_len
+ 1) * 8;
2031 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
2032 origsegs
= segments
;
2033 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
2034 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
2035 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
2036 (size1
- size0
) + 1;
2037 warnx("segments truncated, showing only %d (total=%d)",
2038 segments
, origsegs
);
2041 for (i
= 0; i
< segments
; i
++) {
2042 in6
= inet6_rth_getaddr(extbuf
, i
);
2044 printf(" [%d]<NULL>\n", i
);
2046 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
2048 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
2049 printf(" [%d]%s\n", i
, ntopbuf
);
2057 #else /* !USE_RFC2292BIS */
2060 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
2065 #endif /* USE_RFC2292BIS */
2068 pr_bitrange(v
, soff
, ii
)
2078 /* shift till we have 0x01 */
2079 if ((v
& 0x01) == 0) {
2081 printf("-%u", soff
+ off
- 1);
2092 case 0x04: case 0x0c:
2103 /* we have 0x01 with us */
2104 for (i
= 0; i
< 32 - off
; i
++) {
2105 if ((v
& (0x01 << i
)) == 0)
2109 printf(" %u", soff
+ off
);
2117 pr_suptypes(ni
, nilen
)
2118 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be SUPTYPES */
2123 const u_char
*cp
, *end
;
2126 u_int16_t words
; /*32bit count*/
2129 #define MAXQTYPES (1 << 16)
2133 cp
= (u_char
*)(ni
+ 1);
2134 end
= ((u_char
*)ni
) + nilen
;
2138 printf("NodeInfo Supported Qtypes");
2139 if (options
& F_VERBOSE
) {
2140 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
2141 printf(", compressed bitmap");
2143 printf(", raw bitmap");
2147 clen
= (size_t)(end
- cp
);
2148 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
2149 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
2155 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
2157 memcpy(&cbit
, cp
, sizeof(cbit
));
2158 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
2162 clen
= ntohs(cbit
.words
) * sizeof(v
);
2163 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
2168 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
2169 memcpy(&v
, cp
+ off
, sizeof(v
));
2170 v
= (u_int32_t
)ntohl(v
);
2171 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
2173 /* flush the remaining bits */
2174 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
2178 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
2179 cur
+= ntohs(cbit
.skip
) * 32;
2184 pr_nodeaddr(ni
, nilen
)
2185 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be NODEADDR */
2188 u_char
*cp
= (u_char
*)(ni
+ 1);
2189 char ntop_buf
[INET6_ADDRSTRLEN
];
2192 nilen
-= sizeof(struct icmp6_nodeinfo
);
2194 if (options
& F_VERBOSE
) {
2195 switch (ni
->ni_code
) {
2196 case ICMP6_NI_REFUSED
:
2197 (void)printf("refused");
2199 case ICMP6_NI_UNKNOWN
:
2200 (void)printf("unknown qtype");
2203 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2204 (void)printf(" truncated");
2208 printf(" no address\n");
2211 * In icmp-name-lookups 05 and later, TTL of each returned address
2212 * is contained in the resposne. We try to detect the version
2213 * by the length of the data, but note that the detection algorithm
2214 * is incomplete. We assume the latest draft by default.
2216 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2222 /* XXX: alignment? */
2223 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2224 cp
+= sizeof(u_int32_t
);
2225 nilen
-= sizeof(u_int32_t
);
2228 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2230 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2231 printf(" %s", ntop_buf
);
2233 if (ttl
== 0xffffffff) {
2235 * XXX: can this convention be applied to all
2236 * type of TTL (i.e. non-ND TTL)?
2238 printf("(TTL=infty)");
2241 printf("(TTL=%u)", ttl
);
2245 nilen
-= sizeof(struct in6_addr
);
2246 cp
+= sizeof(struct in6_addr
);
2252 struct msghdr
*mhdr
;
2256 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2257 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2258 if (cm
->cmsg_len
== 0)
2261 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2262 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2263 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2264 return(*(int *)CMSG_DATA(cm
));
2270 struct in6_pktinfo
*
2271 get_rcvpktinfo(mhdr
)
2272 struct msghdr
*mhdr
;
2276 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2277 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2278 if (cm
->cmsg_len
== 0)
2281 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2282 cm
->cmsg_type
== IPV6_PKTINFO
&&
2283 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2284 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2292 struct msghdr
*mhdr
;
2294 #ifdef IPV6_RECVPATHMTU
2296 struct ip6_mtuinfo
*mtuctl
= NULL
;
2298 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2299 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2300 if (cm
->cmsg_len
== 0)
2303 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2304 cm
->cmsg_type
== IPV6_PATHMTU
&&
2305 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2306 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2309 * If the notified destination is different from
2310 * the one we are pinging, just ignore the info.
2311 * We check the scope ID only when both notified value
2312 * and our own value have non-0 values, because we may
2313 * have used the default scope zone ID for sending,
2314 * in which case the scope ID value is 0.
2316 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2318 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2319 dst
.sin6_scope_id
&&
2320 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2321 dst
.sin6_scope_id
)) {
2322 if ((options
& F_VERBOSE
) != 0) {
2323 printf("path MTU for %s is notified. "
2325 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2326 sizeof(mtuctl
->ip6m_addr
)));
2332 * Ignore an invalid MTU. XXX: can we just believe
2335 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2338 /* notification for our destination. return the MTU. */
2339 return((int)mtuctl
->ip6m_mtu
);
2348 struct msghdr
*mhdr
;
2352 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2353 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2354 if (cm
->cmsg_len
== 0)
2357 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2358 cm
->cmsg_type
== IPV6_TCLASS
&&
2359 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2360 return(*(int *)CMSG_DATA(cm
));
2367 get_so_traffic_class(struct msghdr
*mhdr
)
2371 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2372 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2373 if (cm
->cmsg_len
== 0)
2376 if (cm
->cmsg_level
== SOL_SOCKET
&&
2377 cm
->cmsg_type
== SO_TRAFFIC_CLASS
&&
2378 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2379 return(*(int *)CMSG_DATA(cm
));
2388 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2393 struct timeval
*out
, *in
;
2395 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2397 out
->tv_usec
+= 1000000;
2399 out
->tv_sec
-= in
->tv_sec
;
2413 (void)signal(SIGINT
, SIG_DFL
);
2414 (void)kill(getpid(), SIGINT
);
2422 * Print out statistics.
2428 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2429 (void)printf("%ld packets transmitted, ", ntransmitted
);
2430 (void)printf("%ld packets received, ", nreceived
);
2432 (void)printf("+%ld duplicates, ", nrepeats
);
2434 if (nreceived
> ntransmitted
)
2435 (void)printf("-- somebody's duplicating packets!");
2437 (void)printf("%.1f%% packet loss",
2438 ((((double)ntransmitted
- nreceived
) * 100.0) /
2441 (void)putchar('\n');
2442 if (nreceived
&& timing
) {
2443 /* Only display average to microseconds */
2444 double num
= nreceived
+ nrepeats
;
2445 double avg
= tsum
/ num
;
2446 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2448 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2449 tmin
, avg
, tmax
, dev
);
2450 (void)fflush(stdout
);
2452 (void)fflush(stdout
);
2456 static const char *niqcode
[] = {
2458 "DNS label", /*or empty*/
2463 static const char *nircode
[] = {
2464 "Success", "Refused", "Unknown",
2470 * Print a descriptive string about an ICMP header.
2474 struct icmp6_hdr
*icp
;
2477 char ntop_buf
[INET6_ADDRSTRLEN
];
2478 struct nd_redirect
*red
;
2479 struct icmp6_nodeinfo
*ni
;
2480 char dnsname
[NS_MAXDNAME
+ 1];
2484 switch (icp
->icmp6_type
) {
2485 case ICMP6_DST_UNREACH
:
2486 switch (icp
->icmp6_code
) {
2487 case ICMP6_DST_UNREACH_NOROUTE
:
2488 (void)printf("No Route to Destination\n");
2490 case ICMP6_DST_UNREACH_ADMIN
:
2491 (void)printf("Destination Administratively "
2494 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2495 (void)printf("Destination Unreachable Beyond Scope\n");
2497 case ICMP6_DST_UNREACH_ADDR
:
2498 (void)printf("Destination Host Unreachable\n");
2500 case ICMP6_DST_UNREACH_NOPORT
:
2501 (void)printf("Destination Port Unreachable\n");
2504 (void)printf("Destination Unreachable, Bad Code: %d\n",
2508 /* Print returned IP header information */
2509 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2511 case ICMP6_PACKET_TOO_BIG
:
2512 (void)printf("Packet too big mtu = %d\n",
2513 (int)ntohl(icp
->icmp6_mtu
));
2514 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2516 case ICMP6_TIME_EXCEEDED
:
2517 switch (icp
->icmp6_code
) {
2518 case ICMP6_TIME_EXCEED_TRANSIT
:
2519 (void)printf("Time to live exceeded\n");
2521 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2522 (void)printf("Frag reassembly time exceeded\n");
2525 (void)printf("Time exceeded, Bad Code: %d\n",
2529 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2531 case ICMP6_PARAM_PROB
:
2532 (void)printf("Parameter problem: ");
2533 switch (icp
->icmp6_code
) {
2534 case ICMP6_PARAMPROB_HEADER
:
2535 (void)printf("Erroneous Header ");
2537 case ICMP6_PARAMPROB_NEXTHEADER
:
2538 (void)printf("Unknown Nextheader ");
2540 case ICMP6_PARAMPROB_OPTION
:
2541 (void)printf("Unrecognized Option ");
2544 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2547 (void)printf("pointer = 0x%02x\n",
2548 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2549 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2551 case ICMP6_ECHO_REQUEST
:
2552 (void)printf("Echo Request");
2553 /* XXX ID + Seq + Data */
2555 case ICMP6_ECHO_REPLY
:
2556 (void)printf("Echo Reply");
2557 /* XXX ID + Seq + Data */
2559 case ICMP6_MEMBERSHIP_QUERY
:
2560 (void)printf("Listener Query");
2562 case ICMP6_MEMBERSHIP_REPORT
:
2563 (void)printf("Listener Report");
2565 case ICMP6_MEMBERSHIP_REDUCTION
:
2566 (void)printf("Listener Done");
2568 case ND_ROUTER_SOLICIT
:
2569 (void)printf("Router Solicitation");
2571 case ND_ROUTER_ADVERT
:
2572 (void)printf("Router Advertisement");
2574 case ND_NEIGHBOR_SOLICIT
:
2575 (void)printf("Neighbor Solicitation");
2577 case ND_NEIGHBOR_ADVERT
:
2578 (void)printf("Neighbor Advertisement");
2581 red
= (struct nd_redirect
*)icp
;
2582 (void)printf("Redirect\n");
2583 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2585 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2586 (void)printf("Destination: %s", ntop_buf
);
2587 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2589 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2590 (void)printf(" New Target: %s", ntop_buf
);
2592 case ICMP6_NI_QUERY
:
2593 (void)printf("Node Information Query");
2594 /* XXX ID + Seq + Data */
2595 ni
= (struct icmp6_nodeinfo
*)icp
;
2596 l
= end
- (u_char
*)(ni
+ 1);
2598 switch (ntohs(ni
->ni_qtype
)) {
2600 (void)printf("NOOP");
2602 case NI_QTYPE_SUPTYPES
:
2603 (void)printf("Supported qtypes");
2606 (void)printf("DNS name");
2608 case NI_QTYPE_NODEADDR
:
2609 (void)printf("nodeaddr");
2611 case NI_QTYPE_IPV4ADDR
:
2612 (void)printf("IPv4 nodeaddr");
2615 (void)printf("unknown qtype");
2618 if (options
& F_VERBOSE
) {
2619 switch (ni
->ni_code
) {
2620 case ICMP6_NI_SUBJ_IPV6
:
2621 if (l
== sizeof(struct in6_addr
) &&
2622 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2623 sizeof(ntop_buf
)) != NULL
) {
2624 (void)printf(", subject=%s(%s)",
2625 niqcode
[ni
->ni_code
], ntop_buf
);
2628 /* backward compat to -W */
2629 (void)printf(", oldfqdn");
2631 (void)printf(", invalid");
2635 case ICMP6_NI_SUBJ_FQDN
:
2636 if (end
== (u_char
*)(ni
+ 1)) {
2637 (void)printf(", no subject");
2640 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2641 cp
= (const u_char
*)(ni
+ 1);
2642 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2643 sizeof(dnsname
)) != NULL
)
2644 printf("(%s)", dnsname
);
2646 printf("(invalid)");
2648 case ICMP6_NI_SUBJ_IPV4
:
2649 if (l
== sizeof(struct in_addr
) &&
2650 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2651 sizeof(ntop_buf
)) != NULL
) {
2652 (void)printf(", subject=%s(%s)",
2653 niqcode
[ni
->ni_code
], ntop_buf
);
2655 (void)printf(", invalid");
2658 (void)printf(", invalid");
2663 case ICMP6_NI_REPLY
:
2664 (void)printf("Node Information Reply");
2665 /* XXX ID + Seq + Data */
2666 ni
= (struct icmp6_nodeinfo
*)icp
;
2668 switch (ntohs(ni
->ni_qtype
)) {
2670 (void)printf("NOOP");
2672 case NI_QTYPE_SUPTYPES
:
2673 (void)printf("Supported qtypes");
2676 (void)printf("DNS name");
2678 case NI_QTYPE_NODEADDR
:
2679 (void)printf("nodeaddr");
2681 case NI_QTYPE_IPV4ADDR
:
2682 (void)printf("IPv4 nodeaddr");
2685 (void)printf("unknown qtype");
2688 if (options
& F_VERBOSE
) {
2689 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2690 printf(", invalid");
2692 printf(", %s", nircode
[ni
->ni_code
]);
2696 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2702 * Print an IP6 header.
2706 struct ip6_hdr
*ip6
;
2708 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2710 char ntop_buf
[INET6_ADDRSTRLEN
];
2712 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2713 tc
= (tc
>> 4) & 0x0f;
2714 tc
|= (ip6
->ip6_vfc
<< 4);
2716 printf("Vr TC Flow Plen Nxt Hlim\n");
2717 printf(" %1x %02x %05x %04x %02x %02x\n",
2718 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2719 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2720 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2721 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2722 printf("%s->", ntop_buf
);
2723 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2724 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2725 printf("%s\n", ntop_buf
);
2730 * Return an ascii host address as a dotted quad and optionally with
2734 pr_addr(addr
, addrlen
)
2735 struct sockaddr
*addr
;
2738 static char buf
[NI_MAXHOST
];
2741 if ((options
& F_HOSTNAME
) == 0)
2742 flag
|= NI_NUMERICHOST
;
2744 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2752 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2756 struct ip6_hdr
*ip6
;
2759 u_char
*cp
= (u_char
*)ip6
, nh
;
2762 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2767 hlen
= sizeof(*ip6
);
2771 while (end
- cp
>= 8) {
2773 case IPPROTO_HOPOPTS
:
2775 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2776 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2778 case IPPROTO_DSTOPTS
:
2780 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2781 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2783 case IPPROTO_FRAGMENT
:
2785 hlen
= sizeof(struct ip6_frag
);
2786 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2788 case IPPROTO_ROUTING
:
2790 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2791 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2796 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2797 nh
= ((struct ah
*)cp
)->ah_nxt
;
2800 case IPPROTO_ICMPV6
:
2801 printf("ICMP6: type = %d, code = %d\n",
2808 printf("TCP: from port %u, to port %u (decimal)\n",
2809 (*cp
* 256 + *(cp
+ 1)),
2810 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2813 printf("UDP: from port %u, to port %u (decimal)\n",
2814 (*cp
* 256 + *(cp
+ 1)),
2815 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2818 printf("Unknown Header(%d)\n", nh
);
2822 if ((cp
+= hlen
) >= end
)
2844 for (cp
= patp
; *cp
; cp
++)
2846 errx(1, "patterns must be specified as hex digits");
2848 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2849 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2850 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2851 &pat
[13], &pat
[14], &pat
[15]);
2856 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
2858 for (jj
= 0; jj
< ii
; ++jj
)
2859 bp
[jj
+ kk
] = pat
[jj
];
2860 if (!(options
& F_QUIET
)) {
2861 (void)printf("PATTERN: 0x");
2862 for (jj
= 0; jj
< ii
; ++jj
)
2863 (void)printf("%02x", bp
[jj
] & 0xFF);
2869 #ifdef IPSEC_POLICY_IPSEC
2871 setpolicy(so
, policy
)
2878 return 0; /* ignore */
2880 buf
= ipsec_set_policy(policy
, strlen(policy
));
2882 errx(1, "%s", ipsec_strerror());
2883 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2884 ipsec_get_policylen(buf
)) < 0)
2885 warnx("Unable to set IPsec policy");
2900 u_int8_t digest
[16];
2903 char hbuf
[NI_MAXHOST
];
2904 struct in6_addr in6
;
2906 p
= strchr(name
, '.');
2908 p
= name
+ strlen(name
);
2910 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2911 return NULL
; /*label too long*/
2912 strncpy(hbuf
, name
, l
);
2913 hbuf
[(int)l
] = '\0';
2915 for (q
= name
; *q
; q
++) {
2916 if (isupper(*(unsigned char *)q
))
2917 *q
= tolower(*(unsigned char *)q
);
2920 /* generate 8 bytes of pseudo-random value. */
2921 memset(&ctxt
, 0, sizeof(ctxt
));
2924 MD5Update(&ctxt
, &c
, sizeof(c
));
2925 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2926 MD5Final(digest
, &ctxt
);
2928 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2929 return NULL
; /*XXX*/
2930 bcopy(digest
, &in6
.s6_addr
[12], 4);
2932 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2935 return strdup(hbuf
);
2941 (void)fprintf(stderr
,
2942 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2947 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2951 #ifdef IPV6_USE_MIN_MTU
2955 "[-a addrtype] [-b bufsiz] [-B boundif] [-c count]\n"
2956 " [-g gateway] [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2957 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2961 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
2962 "[hops ...] host\n");