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
[] =
81 "$FreeBSD: src/sbin/ping6/ping6.c,v 1.29.2.1 2007/05/22 22:01:44 mtm 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>
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_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
199 #define IN6LEN sizeof(struct in6_addr)
200 #define SA6LEN sizeof(struct sockaddr_in6)
201 #define DUMMY_PORT 10101
203 #define SIN6(s) ((struct sockaddr_in6 *)(s))
208 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
209 * number of received sequence numbers we can keep track of. Change 128
210 * to 8192 for complete accuracy...
212 #define MAX_DUP_CHK (8 * 8192)
213 int mx_dup_ck
= MAX_DUP_CHK
;
214 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
216 struct addrinfo
*res
;
217 struct sockaddr_in6 dst
; /* who to ping6 */
218 struct sockaddr_in6 src
; /* src addr of this packet */
220 int datalen
= DEFDATALEN
;
221 int s
; /* socket file descriptor */
222 u_char outpack
[MAXPACKETLEN
];
223 char BSPACE
= '\b'; /* characters written for flood */
226 int ident
; /* process id to identify our packets */
227 u_int8_t nonce
[8]; /* nonce field for node information */
228 int hoplimit
= -1; /* hoplimit */
229 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
232 long npackets
; /* max packets to transmit */
233 long nreceived
; /* # of packets we got back */
234 long nrepeats
; /* number of duplicates */
235 long ntransmitted
; /* sequence # for outbound packets = #sent */
236 long nmissed
; /* # of packet missed */
237 struct timeval interval
= {1, 0}; /* interval between packets */
240 int timing
; /* flag to do timing */
241 double tmin
= 999999999.0; /* minimum round trip time */
242 double tmax
= 0.0; /* maximum round trip time */
243 double tsum
= 0.0; /* sum of all times, for doing average */
244 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
246 /* for node addresses */
249 /* for ancillary data(advanced API) */
250 struct msghdr smsghdr
;
251 struct iovec smsgiov
;
254 volatile sig_atomic_t seenalrm
;
255 volatile sig_atomic_t seenint
;
257 volatile sig_atomic_t seeninfo
;
262 int main(int, char *[]);
263 void fill(char *, char *);
264 int get_hoplim(struct msghdr
*);
265 int get_pathmtu(struct msghdr
*);
266 int get_tclass(struct msghdr
*);
267 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
269 void retransmit(void);
271 size_t pingerlen(void);
273 const char *pr_addr(struct sockaddr
*, int);
274 void pr_icmph(struct icmp6_hdr
*, u_char
*);
275 void pr_iph(struct ip6_hdr
*);
276 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
277 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
278 int myechoreply(const struct icmp6_hdr
*);
279 int mynireply(const struct icmp6_nodeinfo
*);
280 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
282 void pr_pack(u_char
*, int, struct msghdr
*);
283 void pr_exthdrs(struct msghdr
*);
284 void pr_ip6opt(void *, size_t);
285 void pr_rthdr(void *, size_t);
286 int pr_bitrange(u_int32_t
, int, int);
287 void pr_retip(struct ip6_hdr
*, u_char
*);
289 void tvsub(struct timeval
*, struct timeval
*);
290 int setpolicy(int, char *);
291 char *nigroup(char *);
299 struct itimerval itimer
;
300 struct sockaddr_in6 from
;
301 #ifndef HAVE_ARC4RANDOM
307 struct timeval timeout
, *tv
;
309 struct addrinfo hints
;
311 struct pollfd fdmaskp
[1];
317 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
318 u_char
*datap
, *packet
;
319 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
321 struct cmsghdr
*scmsgp
= NULL
;
323 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
328 struct in6_pktinfo
*pktinfo
= NULL
;
329 #ifdef USE_RFC2292BIS
330 struct ip6_rthdr
*rthdr
= NULL
;
332 #ifdef IPSEC_POLICY_IPSEC
333 char *policy_in
= NULL
;
334 char *policy_out
= NULL
;
338 #ifdef IPV6_USE_MIN_MTU
341 int tclass
= -2; /* T_CLASS value -1 means default, so -2 means do not bother */
343 /* just to be sure */
344 memset(&smsghdr
, 0, sizeof(smsghdr
));
345 memset(&smsgiov
, 0, sizeof(smsgiov
));
348 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
352 #ifdef IPSEC_POLICY_IPSEC
356 #endif /*IPSEC_POLICY_IPSEC*/
358 while ((ch
= getopt(argc
, argv
,
359 "a:b:c:dfHg:h:I:i:l:mnNop:qS:s:tvwWz:" ADDOPTS
)) != -1) {
366 options
&= ~F_NOUSERDATA
;
367 options
|= F_NODEADDR
;
368 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
371 naflags
|= NI_NODEADDR_FLAG_ALL
;
375 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
379 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
383 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
387 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
389 case 'A': /* experimental. not in the spec */
390 #ifdef NI_NODEADDR_FLAG_ANYCAST
391 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
395 "-a A is not supported on the platform");
406 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
409 lsockbufsize
= strtoul(optarg
, &e
, 10);
410 sockbufsize
= lsockbufsize
;
411 if (errno
|| !*optarg
|| *e
||
412 sockbufsize
!= lsockbufsize
)
413 errx(1, "invalid socket buffer size");
416 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
420 npackets
= strtol(optarg
, &e
, 10);
421 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
423 "illegal number of packets -- %s", optarg
);
426 options
|= F_SO_DEBUG
;
431 errx(1, "Must be superuser to flood ping");
434 setbuf(stdout
, (char *)NULL
);
440 options
|= F_HOSTNAME
;
442 case 'h': /* hoplimit */
443 hoplimit
= strtol(optarg
, &e
, 10);
444 if (*optarg
== '\0' || *e
!= '\0')
445 errx(1, "illegal hoplimit %s", optarg
);
446 if (255 < hoplimit
|| hoplimit
< -1)
448 "illegal hoplimit -- %s", optarg
);
452 options
|= F_INTERFACE
;
453 #ifndef USE_SIN6_SCOPE_ID
457 case 'i': /* wait between sending packets */
458 intval
= strtod(optarg
, &e
);
459 if (*optarg
== '\0' || *e
!= '\0')
460 errx(1, "illegal timing interval %s", optarg
);
461 if (intval
< 1 && getuid()) {
462 errx(1, "%s: only root may use interval < 1s",
465 interval
.tv_sec
= (long)intval
;
467 (long)((intval
- interval
.tv_sec
) * 1000000);
468 if (interval
.tv_sec
< 0)
469 errx(1, "illegal timing interval %s", optarg
);
470 /* less than 1/hz does not make sense */
471 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 1) {
472 warnx("too small interval, raised to .000001");
473 interval
.tv_usec
= 1;
475 options
|= F_INTERVAL
;
480 errx(1, "Must be superuser to preload");
482 preload
= strtol(optarg
, &e
, 10);
483 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
484 errx(1, "illegal preload value -- %s", optarg
);
487 #ifdef IPV6_USE_MIN_MTU
491 errx(1, "-%c is not supported on this platform", ch
);
495 options
&= ~F_HOSTNAME
;
498 options
|= F_NIGROUP
;
503 case 'p': /* fill buffer with user pattern */
504 options
|= F_PINGFILLED
;
505 fill((char *)datap
, optarg
);
511 memset(&hints
, 0, sizeof(struct addrinfo
));
512 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
513 hints
.ai_family
= AF_INET6
;
514 hints
.ai_socktype
= SOCK_RAW
;
515 hints
.ai_protocol
= IPPROTO_ICMPV6
;
517 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
519 errx(1, "invalid source address: %s",
520 gai_strerror(ret_ga
));
523 * res->ai_family must be AF_INET6 and res->ai_addrlen
524 * must be sizeof(src).
526 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
527 srclen
= res
->ai_addrlen
;
529 options
|= F_SRCADDR
;
531 case 's': /* size of packet to send */
532 datalen
= strtol(optarg
, &e
, 10);
533 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
534 errx(1, "illegal datalen value -- %s", optarg
);
535 if (datalen
> MAXDATALEN
) {
537 "datalen value too large, maximum is %d",
542 options
&= ~F_NOUSERDATA
;
543 options
|= F_SUPTYPES
;
546 options
|= F_VERBOSE
;
549 options
&= ~F_NOUSERDATA
;
553 options
&= ~F_NOUSERDATA
;
554 options
|= F_FQDNOLD
;
557 tclass
= (int)strtol(optarg
, &e
, 10);
558 if (tclass
< -1 || *optarg
== '\0' || *e
!= '\0')
559 errx(1, "illegal TOS value -- %s", optarg
);
562 "TOS value too large, maximum is %d",
567 #ifdef IPSEC_POLICY_IPSEC
570 if (!strncmp("in", optarg
, 2)) {
571 if ((policy_in
= strdup(optarg
)) == NULL
)
573 } else if (!strncmp("out", optarg
, 3)) {
574 if ((policy_out
= strdup(optarg
)) == NULL
)
577 errx(1, "invalid security policy");
581 options
|= F_AUTHHDR
;
584 options
|= F_ENCRYPT
;
586 #endif /*IPSEC_POLICY_IPSEC*/
603 #ifdef IPV6_RECVRTHDR /* 2292bis */
604 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
607 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
610 errx(1, "too many intermediate hops");
616 if (options
& F_NIGROUP
) {
617 target
= nigroup(argv
[argc
- 1]);
618 if (target
== NULL
) {
623 target
= argv
[argc
- 1];
626 memset(&hints
, 0, sizeof(struct addrinfo
));
627 hints
.ai_flags
= AI_CANONNAME
;
628 hints
.ai_family
= AF_INET6
;
629 /* XXX getaddrinfo does like SOCK_DGRAM for IPPROTO_ICMPV6 */
630 hints
.ai_socktype
= SOCK_RAW
;
631 hints
.ai_protocol
= IPPROTO_ICMPV6
;
633 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
635 errx(1, "getaddrinfo -- %s", gai_strerror(ret_ga
));
636 if (res
->ai_canonname
)
637 hostname
= res
->ai_canonname
;
642 errx(1, "getaddrinfo failed");
644 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
646 res
->ai_socktype
= getuid() ? SOCK_DGRAM
: SOCK_RAW
;
647 res
->ai_protocol
= IPPROTO_ICMPV6
;
649 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
650 res
->ai_protocol
)) < 0)
653 /* set the source address if specified. */
654 if ((options
& F_SRCADDR
) &&
655 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
659 /* set the gateway (next hop) if specified */
661 struct addrinfo ghints
, *gres
;
664 memset(&ghints
, 0, sizeof(ghints
));
665 ghints
.ai_family
= AF_INET6
;
666 ghints
.ai_socktype
= SOCK_RAW
;
667 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
669 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
671 errx(1, "getaddrinfo for the gateway %s: %s",
672 gateway
, gai_strerror(error
));
674 if (gres
->ai_next
&& (options
& F_VERBOSE
))
675 warnx("gateway resolves to multiple addresses");
677 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
678 gres
->ai_addr
, gres
->ai_addrlen
)) {
679 err(1, "setsockopt(IPV6_NEXTHOP)");
686 * let the kerel pass extension headers of incoming packets,
687 * for privileged socket options
689 if ((options
& F_VERBOSE
) != 0) {
692 #ifdef IPV6_RECVHOPOPTS
693 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
695 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
696 #else /* old adv. API */
697 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
699 err(1, "setsockopt(IPV6_HOPOPTS)");
701 #ifdef IPV6_RECVDSTOPTS
702 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
704 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
705 #else /* old adv. API */
706 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
708 err(1, "setsockopt(IPV6_DSTOPTS)");
710 #ifdef IPV6_RECVRTHDRDSTOPTS
711 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
713 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
717 /* revoke root privilege */
721 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
722 errx(1, "-f and -i incompatible options");
724 if ((options
& F_NOUSERDATA
) == 0) {
725 if (datalen
>= sizeof(struct tv32
)) {
726 /* we can time transfer */
730 /* in F_VERBOSE case, we may get non-echoreply packets*/
731 if (options
& F_VERBOSE
)
732 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
734 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
736 /* suppress timing for node information query */
739 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
742 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
743 err(1, "Unable to allocate packet");
744 if (!(options
& F_PINGFILLED
))
745 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
748 ident
= getpid() & 0xFFFF;
749 #ifndef HAVE_ARC4RANDOM
750 gettimeofday(&seed
, NULL
);
751 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
752 memset(nonce
, 0, sizeof(nonce
));
753 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
754 *((int *)&nonce
[i
]) = rand();
756 memset(nonce
, 0, sizeof(nonce
));
757 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
758 *((u_int32_t
*)&nonce
[i
]) = arc4random();
763 if (options
& F_SO_DEBUG
)
764 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
766 optval
= IPV6_DEFHLIM
;
767 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
768 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
769 &optval
, sizeof(optval
)) == -1)
770 err(1, "IPV6_MULTICAST_HOPS");
771 #ifdef IPV6_USE_MIN_MTU
773 optval
= mflag
> 1 ? 0 : 1;
775 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
776 &optval
, sizeof(optval
)) == -1)
777 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
779 #ifdef IPV6_RECVPATHMTU
782 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
783 &optval
, sizeof(optval
)) == -1)
784 err(1, "setsockopt(IPV6_RECVPATHMTU)");
786 #endif /* IPV6_RECVPATHMTU */
787 #endif /* IPV6_USE_MIN_MTU */
790 #ifdef IPSEC_POLICY_IPSEC
791 if (options
& F_POLICY
) {
792 if (setpolicy(s
, policy_in
) < 0)
793 errx(1, "%s", ipsec_strerror());
794 if (setpolicy(s
, policy_out
) < 0)
795 errx(1, "%s", ipsec_strerror());
798 if (options
& F_AUTHHDR
) {
799 optval
= IPSEC_LEVEL_REQUIRE
;
800 #ifdef IPV6_AUTH_TRANS_LEVEL
801 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
802 &optval
, sizeof(optval
)) == -1)
803 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
805 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
806 &optval
, sizeof(optval
)) == -1)
807 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
810 if (options
& F_ENCRYPT
) {
811 optval
= IPSEC_LEVEL_REQUIRE
;
812 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
813 &optval
, sizeof(optval
)) == -1)
814 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
816 #endif /*IPSEC_POLICY_IPSEC*/
821 struct icmp6_filter filt
;
822 if (!(options
& F_VERBOSE
)) {
823 ICMP6_FILTER_SETBLOCKALL(&filt
);
824 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
825 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
826 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
828 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
830 ICMP6_FILTER_SETPASSALL(&filt
);
832 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
834 err(1, "setsockopt(ICMP6_FILTER)");
836 #endif /*ICMP6_FILTER*/
838 /* let the kerel pass extension headers of incoming packets */
839 if ((options
& F_VERBOSE
) != 0) {
842 #ifdef IPV6_RECVRTHDR
843 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
845 err(1, "setsockopt(IPV6_RECVRTHDR)");
846 #else /* old adv. API */
847 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
849 err(1, "setsockopt(IPV6_RTHDR)");
856 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVTCLASS
, &on
, sizeof(on
)))
857 err(1, "setsockopt(IPV6_RECVTCLASS)");
859 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
, sizeof(tclass
)))
860 err(1, "setsockopt(IPV6_TCLASS)");
865 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
866 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
867 &optval, sizeof(optval)) == -1)
868 err(1, "IPV6_MULTICAST_LOOP");
871 /* Specify the outgoing interface and/or the source address */
873 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
876 ip6optlen
+= CMSG_SPACE(sizeof(int));
879 ip6optlen
+= CMSG_SPACE(sizeof(int));
881 /* set IP6 packet options */
883 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
884 errx(1, "can't allocate enough memory");
885 smsghdr
.msg_control
= (caddr_t
)scmsg
;
886 smsghdr
.msg_controllen
= ip6optlen
;
887 scmsgp
= (struct cmsghdr
*)scmsg
;
890 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
891 memset(pktinfo
, 0, sizeof(*pktinfo
));
892 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
893 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
894 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
895 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
898 /* set the outgoing interface */
900 #ifndef USE_SIN6_SCOPE_ID
901 /* pktinfo must have already been allocated */
902 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
903 errx(1, "%s: invalid interface name", ifname
);
905 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
906 errx(1, "%s: invalid interface name", ifname
);
909 if (hoplimit
!= -1) {
910 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
911 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
912 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
913 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
915 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
918 if (argc
> 1) { /* some intermediate addrs are specified */
920 #ifdef USE_RFC2292BIS
924 #ifdef USE_RFC2292BIS
925 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
926 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
927 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
928 scmsgp
->cmsg_type
= IPV6_RTHDR
;
929 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
930 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
931 IPV6_RTHDR_TYPE_0
, argc
- 1);
933 errx(1, "can't initialize rthdr");
934 #else /* old advanced API */
935 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
936 IPV6_RTHDR_TYPE_0
)) == 0)
937 errx(1, "can't initialize rthdr");
938 #endif /* USE_RFC2292BIS */
940 for (hops
= 0; hops
< argc
- 1; hops
++) {
941 struct addrinfo
*iaip
;
943 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
945 errx(1, "%s", gai_strerror(error
));
946 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
948 "bad addr family of an intermediate addr");
950 #ifdef USE_RFC2292BIS
951 if (inet6_rth_add(rthdr
,
952 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
953 errx(1, "can't add an intermediate node");
954 #else /* old advanced API */
955 if (inet6_rthdr_add(scmsgp
,
956 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
958 errx(1, "can't add an intermediate node");
959 #endif /* USE_RFC2292BIS */
963 #ifndef USE_RFC2292BIS
964 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
965 errx(1, "can't set the last flag");
968 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
972 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
973 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
974 scmsgp
->cmsg_type
= IPV6_TCLASS
;
975 *(int *)(CMSG_DATA(scmsgp
)) = tclass
;
977 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
980 if (!(options
& F_SRCADDR
)) {
982 * get the source address. XXX since we revoked the root
983 * privilege, we cannot use a raw socket for this.
986 socklen_t len
= sizeof(src
);
988 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
989 err(1, "UDP socket");
991 src
.sin6_family
= AF_INET6
;
992 src
.sin6_addr
= dst
.sin6_addr
;
993 src
.sin6_port
= ntohs(DUMMY_PORT
);
994 src
.sin6_scope_id
= dst
.sin6_scope_id
;
996 #ifdef USE_RFC2292BIS
998 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
999 (void *)pktinfo
, sizeof(*pktinfo
)))
1000 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1002 if (hoplimit
!= -1 &&
1003 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1004 (void *)&hoplimit
, sizeof(hoplimit
)))
1005 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1007 if (hoplimit
!= -1 &&
1008 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1009 (void *)&hoplimit
, sizeof(hoplimit
)))
1010 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1013 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
1014 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
1015 err(1, "UDP setsockopt(IPV6_RTHDR)");
1016 #else /* old advanced API */
1017 if (smsghdr
.msg_control
&&
1018 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
1019 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
1020 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1023 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
1024 err(1, "UDP connect");
1026 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
1027 err(1, "getsockname");
1032 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1034 if (datalen
> sockbufsize
)
1035 warnx("you need -b to increase socket buffer size");
1036 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
1037 sizeof(sockbufsize
)) < 0)
1038 err(1, "setsockopt(SO_SNDBUF)");
1039 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
1040 sizeof(sockbufsize
)) < 0)
1041 err(1, "setsockopt(SO_RCVBUF)");
1044 if (datalen
> 8 * 1024) /*XXX*/
1045 warnx("you need -b to increase socket buffer size");
1047 * When pinging the broadcast address, you can get a lot of
1048 * answers. Doing something so evil is useful if you are trying
1049 * to stress the ethernet, or just want to fill the arp cache
1050 * to get some stuff for /etc/ethers.
1053 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1059 #ifndef USE_SIN6_SCOPE_ID
1060 #ifdef IPV6_RECVPKTINFO
1061 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1062 sizeof(optval
)) < 0)
1063 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1064 #else /* old adv. API */
1065 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1066 sizeof(optval
)) < 0)
1067 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1069 #endif /* USE_SIN6_SCOPE_ID */
1070 #ifdef IPV6_RECVHOPLIMIT
1071 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1072 sizeof(optval
)) < 0)
1073 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1074 #else /* old adv. API */
1075 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1076 sizeof(optval
)) < 0)
1077 warn("setsockopt(IPV6_HOPLIMIT, %d, %u)", optval
, sizeof(optval
)); /* XXX err? */
1080 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1081 (unsigned long)(pingerlen() - 8));
1082 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1083 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1085 while (preload
--) /* Fire off them quickies. */
1088 (void)signal(SIGINT
, onsignal
);
1090 (void)signal(SIGINFO
, onsignal
);
1093 if ((options
& F_FLOOD
) == 0) {
1094 (void)signal(SIGALRM
, onsignal
);
1095 itimer
.it_interval
= interval
;
1096 itimer
.it_value
= interval
;
1097 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1098 if (ntransmitted
== 0)
1103 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1104 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1108 seenalrm
= seenint
= 0;
1113 /* For control (ancillary) data received from recvmsg() */
1114 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1120 struct iovec iov
[2];
1122 /* signal handling */
1124 if (ntransmitted
- nreceived
> nmissed
) {
1126 if (!(options
& F_QUIET
))
1127 printf("Request timeout for icmp_seq=%ld\n", ntransmitted
- 1);
1146 if (options
& F_FLOOD
) {
1152 timeout
.tv_usec
= 10000;
1164 fdmaskp
[0].events
= POLLIN
;
1165 cc
= poll(fdmaskp
, 1, timeout
);
1167 memset(fdmaskp
, 0, fdmasks
);
1169 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1172 if (errno
!= EINTR
) {
1184 m
.msg_name
= (caddr_t
)&from
;
1185 m
.msg_namelen
= sizeof(from
);
1186 memset(&iov
, 0, sizeof(iov
));
1187 iov
[0].iov_base
= (caddr_t
)packet
;
1188 iov
[0].iov_len
= packlen
;
1191 memset(cm
, 0, CONTROLLEN
);
1192 m
.msg_control
= (void *)cm
;
1193 m
.msg_controllen
= CONTROLLEN
;
1195 cc
= recvmsg(s
, &m
, 0);
1197 if (errno
!= EINTR
) {
1202 } else if (cc
== 0) {
1206 * receive control messages only. Process the
1207 * exceptions (currently the only possiblity is
1208 * a path MTU notification.)
1210 if ((mtu
= get_pathmtu(&m
)) > 0) {
1211 if ((options
& F_VERBOSE
) != 0) {
1212 printf("new path MTU (%d) is "
1219 * an ICMPv6 message (probably an echoreply) arrived.
1221 pr_pack(packet
, cc
, &m
);
1223 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1224 (npackets
> 0 && nreceived
>= npackets
))
1228 exit(nreceived
== 0);
1253 * This routine transmits another ping6.
1258 struct itimerval itimer
;
1264 * If we're not transmitting any more packets, change the timer
1265 * to wait two round-trip times if we've received any packets or
1266 * ten seconds if we haven't.
1270 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1271 if (itimer
.it_value
.tv_sec
== 0)
1272 itimer
.it_value
.tv_sec
= 1;
1274 itimer
.it_value
.tv_sec
= MAXWAIT
;
1275 itimer
.it_interval
.tv_sec
= 0;
1276 itimer
.it_interval
.tv_usec
= 0;
1277 itimer
.it_value
.tv_usec
= 0;
1279 (void)signal(SIGALRM
, onint
);
1280 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1285 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1286 * will be added on by the kernel. The ID field is our UNIX process ID,
1287 * and the sequence number is an ascending integer. The first 8 bytes
1288 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1289 * byte-order, to compute the round-trip time.
1296 if (options
& F_FQDN
)
1297 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1298 else if (options
& F_FQDNOLD
)
1300 else if (options
& F_NODEADDR
)
1301 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1302 else if (options
& F_SUPTYPES
)
1305 l
= ICMP6ECHOLEN
+ datalen
;
1313 struct icmp6_hdr
*icp
;
1314 struct iovec iov
[2];
1316 struct icmp6_nodeinfo
*nip
;
1319 if (npackets
&& ntransmitted
>= npackets
)
1320 return(-1); /* no more transmission */
1322 icp
= (struct icmp6_hdr
*)outpack
;
1323 nip
= (struct icmp6_nodeinfo
*)outpack
;
1324 memset(icp
, 0, sizeof(*icp
));
1325 icp
->icmp6_cksum
= 0;
1326 seq
= ntransmitted
++;
1327 CLR(seq
% mx_dup_ck
);
1329 if (options
& F_FQDN
) {
1330 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1331 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1332 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1333 nip
->ni_flags
= htons(0);
1335 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1336 sizeof(nip
->icmp6_ni_nonce
));
1337 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1339 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1340 sizeof(dst
.sin6_addr
));
1341 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1343 } else if (options
& F_FQDNOLD
) {
1344 /* packet format in 03 draft - no Subject data on queries */
1345 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1346 icp
->icmp6_code
= 0; /* code field is always 0 */
1347 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1348 nip
->ni_flags
= htons(0);
1350 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1351 sizeof(nip
->icmp6_ni_nonce
));
1352 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1356 } else if (options
& F_NODEADDR
) {
1357 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1358 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1359 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1360 nip
->ni_flags
= naflags
;
1362 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1363 sizeof(nip
->icmp6_ni_nonce
));
1364 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1366 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1367 sizeof(dst
.sin6_addr
));
1368 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1370 } else if (options
& F_SUPTYPES
) {
1371 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1372 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1373 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1374 /* we support compressed bitmap */
1375 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1377 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1378 sizeof(nip
->icmp6_ni_nonce
));
1379 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1383 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1384 icp
->icmp6_code
= 0;
1385 icp
->icmp6_id
= htons(ident
);
1386 icp
->icmp6_seq
= ntohs(seq
);
1390 (void)gettimeofday(&tv
, NULL
);
1391 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1392 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1393 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1395 cc
= ICMP6ECHOLEN
+ datalen
;
1399 if (pingerlen() != cc
)
1400 errx(1, "internal error; length mismatch");
1403 smsghdr
.msg_name
= (caddr_t
)&dst
;
1404 smsghdr
.msg_namelen
= sizeof(dst
);
1405 memset(&iov
, 0, sizeof(iov
));
1406 iov
[0].iov_base
= (caddr_t
)outpack
;
1407 iov
[0].iov_len
= cc
;
1408 smsghdr
.msg_iov
= iov
;
1409 smsghdr
.msg_iovlen
= 1;
1411 i
= sendmsg(s
, &smsghdr
, 0);
1413 if (i
< 0 || i
!= cc
) {
1416 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1419 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1420 (void)write(STDOUT_FILENO
, &DOT
, 1);
1427 const struct icmp6_hdr
*icp
;
1429 if (ntohs(icp
->icmp6_id
) == ident
)
1437 const struct icmp6_nodeinfo
*nip
;
1439 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1440 nonce
+ sizeof(u_int16_t
),
1441 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1448 dnsdecode(sp
, ep
, base
, buf
, bufsiz
)
1451 const u_char
*base
; /*base for compressed name*/
1457 char cresult
[NS_MAXDNAME
+ 1];
1468 if (i
== 0 || cp
!= *sp
) {
1469 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1470 return NULL
; /*result overrun*/
1476 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1477 /* DNS compression */
1481 comp
= base
+ (i
& 0x3f);
1482 if (dnsdecode(&comp
, cp
, base
, cresult
,
1483 sizeof(cresult
)) == NULL
)
1485 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1486 return NULL
; /*result overrun*/
1488 } else if ((i
& 0x3f) == i
) {
1490 return NULL
; /*source overrun*/
1491 while (i
-- > 0 && cp
< ep
) {
1492 l
= snprintf(cresult
, sizeof(cresult
),
1493 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1494 if (l
>= sizeof(cresult
) || l
< 0)
1496 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1497 return NULL
; /*result overrun*/
1501 return NULL
; /*invalid label*/
1504 return NULL
; /*not terminated*/
1512 * Print out the packet, if it came from us. This logic is necessary
1513 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1514 * which arrive ('tis only fair). This permits multiple copies of this
1515 * program to be run without having intermingled output (or statistics!).
1518 pr_pack(buf
, cc
, mhdr
)
1521 struct msghdr
*mhdr
;
1523 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1524 struct icmp6_hdr
*icp
;
1525 struct icmp6_nodeinfo
*ni
;
1528 struct sockaddr
*from
;
1530 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1531 struct in6_pktinfo
*pktinfo
= NULL
;
1532 struct timeval tv
, tp
;
1534 double triptime
= 0;
1539 char dnsname
[NS_MAXDNAME
+ 1];
1542 (void)gettimeofday(&tv
, NULL
);
1544 if (!mhdr
|| !mhdr
->msg_name
||
1545 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1546 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1547 if (options
& F_VERBOSE
)
1548 warnx("invalid peername");
1551 from
= (struct sockaddr
*)mhdr
->msg_name
;
1552 fromlen
= mhdr
->msg_namelen
;
1553 if (cc
< sizeof(struct icmp6_hdr
)) {
1554 if (options
& F_VERBOSE
)
1555 warnx("packet too short (%d bytes) from %s", cc
,
1556 pr_addr(from
, fromlen
));
1559 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1560 (options
& F_VERBOSE
) != 0)
1561 warnx("some control data discarded, insufficient buffer size");
1562 icp
= (struct icmp6_hdr
*)buf
;
1563 ni
= (struct icmp6_nodeinfo
*)buf
;
1566 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1567 warnx("failed to get receiving hop limit");
1570 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1571 warnx("failed to get receiving packet information");
1574 if (rcvtclass
&& (tclass
= get_tclass(mhdr
)) == -1) {
1575 warnx("failed to get receiving traffic class");
1579 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1580 seq
= ntohs(icp
->icmp6_seq
);
1583 tpp
= (struct tv32
*)(icp
+ 1);
1584 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1585 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1587 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1588 ((double)tv
.tv_usec
) / 1000.0;
1590 tsumsq
+= triptime
* triptime
;
1591 if (triptime
< tmin
)
1593 if (triptime
> tmax
)
1597 if (TST(seq
% mx_dup_ck
)) {
1602 SET(seq
% mx_dup_ck
);
1606 if (options
& F_QUIET
)
1609 if (options
& F_FLOOD
)
1610 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1612 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1613 pr_addr(from
, fromlen
), seq
);
1614 (void)printf(" hlim=%d", hoplim
);
1615 if ((options
& F_VERBOSE
) != 0) {
1616 struct sockaddr_in6 dstsa
;
1618 memset(&dstsa
, 0, sizeof(dstsa
));
1619 dstsa
.sin6_family
= AF_INET6
;
1620 dstsa
.sin6_len
= sizeof(dstsa
);
1621 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1622 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1623 (void)printf(" dst=%s",
1624 pr_addr((struct sockaddr
*)&dstsa
,
1628 (void)printf(" time=%.3f ms", triptime
);
1630 if (!IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
1631 (void)printf("(DUP!)");
1633 /* check the data */
1634 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1635 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1636 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1638 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1643 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1644 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1646 if (TST(seq
% mx_dup_ck
)) {
1651 SET(seq
% mx_dup_ck
);
1655 if (options
& F_QUIET
)
1658 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1660 switch (ntohs(ni
->ni_code
)) {
1661 case ICMP6_NI_SUCCESS
:
1663 case ICMP6_NI_REFUSED
:
1664 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1666 case ICMP6_NI_UNKNOWN
:
1667 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1670 printf("unknown code 0x%x, type 0x%x",
1671 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1675 switch (ntohs(ni
->ni_qtype
)) {
1677 printf("NodeInfo NOOP");
1679 case NI_QTYPE_SUPTYPES
:
1680 pr_suptypes(ni
, end
- (u_char
*)ni
);
1682 case NI_QTYPE_NODEADDR
:
1683 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1686 default: /* XXX: for backward compatibility */
1687 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1688 if (buf
[off
+ ICMP6_NIRLEN
] ==
1689 cc
- off
- ICMP6_NIRLEN
- 1)
1694 cp
++; /* skip length */
1696 safeputc(*cp
& 0xff);
1702 if (dnsdecode((const u_char
**)&cp
, end
,
1703 (const u_char
*)(ni
+ 1), dnsname
,
1704 sizeof(dnsname
)) == NULL
) {
1709 * name-lookup special handling for
1712 if (cp
+ 1 <= end
&& !*cp
&&
1713 strlen(dnsname
) > 0) {
1714 dnsname
[strlen(dnsname
) - 1] = '\0';
1717 printf("%s%s", i
> 0 ? "," : "",
1721 if (options
& F_VERBOSE
) {
1725 (void)printf(" ("); /*)*/
1727 switch (ni
->ni_code
) {
1728 case ICMP6_NI_REFUSED
:
1729 (void)printf("refused");
1732 case ICMP6_NI_UNKNOWN
:
1733 (void)printf("unknown qtype");
1738 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1739 /* case of refusion, unknown */
1744 ttl
= (int32_t)ntohl(*(u_int32_t
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1747 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1748 (void)printf("TTL=%d:meaningless",
1752 (void)printf("TTL=%d:invalid",
1755 (void)printf("TTL=%d", ttl
);
1765 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1774 if (buf
[off
+ ICMP6_NIRLEN
] !=
1775 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1778 (void)printf("invalid namelen:%d/%lu",
1779 buf
[off
+ ICMP6_NIRLEN
],
1780 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1790 /* We've got something other than an ECHOREPLY */
1791 if (!(options
& F_VERBOSE
))
1793 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1797 if (!(options
& F_FLOOD
)) {
1798 (void)putchar('\n');
1799 if (options
& F_VERBOSE
)
1801 (void)fflush(stdout
);
1808 struct msghdr
*mhdr
;
1815 bufp
= mhdr
->msg_control
;
1816 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1817 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1818 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1821 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1824 switch (cm
->cmsg_type
) {
1826 printf(" HbH Options: ");
1827 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1830 #ifdef IPV6_RTHDRDSTOPTS
1831 case IPV6_RTHDRDSTOPTS
:
1833 printf(" Dst Options: ");
1834 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1837 printf(" Routing: ");
1838 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1844 #ifdef USE_RFC2292BIS
1846 pr_ip6opt(void *extbuf
, size_t bufsize
)
1848 struct ip6_hbh
*ext
;
1851 socklen_t extlen
, len
, origextlen
;
1857 ext
= (struct ip6_hbh
*)extbuf
;
1858 extlen
= (ext
->ip6h_len
+ 1) * 8;
1859 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1860 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1863 * Bounds checking on the ancillary data buffer:
1864 * subtract the size of a cmsg structure from the buffer size.
1866 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
1867 origextlen
= extlen
;
1868 extlen
= bufsize
- CMSG_SPACE(0);
1869 warnx("options truncated, showing only %u (total=%u)",
1870 (unsigned int)(extlen
/ 8 - 1),
1871 (unsigned int)(ext
->ip6h_len
));
1876 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1877 &type
, &len
, &databuf
);
1878 if (currentlen
== -1)
1882 * Note that inet6_opt_next automatically skips any padding
1887 offset
= inet6_opt_get_val(databuf
, offset
,
1888 &value4
, sizeof(value4
));
1889 printf(" Jumbo Payload Opt: Length %u\n",
1890 (u_int32_t
)ntohl(value4
));
1892 case IP6OPT_ROUTER_ALERT
:
1894 offset
= inet6_opt_get_val(databuf
, offset
,
1895 &value2
, sizeof(value2
));
1896 printf(" Router Alert Opt: Type %u\n",
1900 printf(" Received Opt %u len %lu\n",
1901 type
, (unsigned long)len
);
1907 #else /* !USE_RFC2292BIS */
1910 pr_ip6opt(void *extbuf
, size_t bufsize __unused
)
1915 #endif /* USE_RFC2292BIS */
1917 #ifdef USE_RFC2292BIS
1919 pr_rthdr(void *extbuf
, size_t bufsize
)
1921 struct in6_addr
*in6
;
1922 char ntopbuf
[INET6_ADDRSTRLEN
];
1923 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1924 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
1926 /* print fixed part of the header */
1927 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1928 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1929 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
1930 printf("%d segments, ", segments
);
1931 printf("%d left\n", rh
->ip6r_segleft
);
1933 printf("segments unknown, ");
1934 printf("%d left\n", rh
->ip6r_segleft
);
1939 * Bounds checking on the ancillary data buffer. When calculating
1940 * the number of items to show keep in mind:
1941 * - The size of the cmsg structure
1942 * - The size of one segment (the size of a Type 0 routing header)
1943 * - When dividing add a fudge factor of one in case the
1944 * dividend is not evenly divisible by the divisor
1946 rthsize
= (rh
->ip6r_len
+ 1) * 8;
1947 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
1948 origsegs
= segments
;
1949 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
1950 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
1951 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
1952 (size1
- size0
) + 1;
1953 warnx("segments truncated, showing only %d (total=%d)",
1954 segments
, origsegs
);
1957 for (i
= 0; i
< segments
; i
++) {
1958 in6
= inet6_rth_getaddr(extbuf
, i
);
1960 printf(" [%d]<NULL>\n", i
);
1962 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
1964 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
1965 printf(" [%d]%s\n", i
, ntopbuf
);
1973 #else /* !USE_RFC2292BIS */
1976 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
1981 #endif /* USE_RFC2292BIS */
1984 pr_bitrange(v
, soff
, ii
)
1994 /* shift till we have 0x01 */
1995 if ((v
& 0x01) == 0) {
1997 printf("-%u", soff
+ off
- 1);
2008 case 0x04: case 0x0c:
2019 /* we have 0x01 with us */
2020 for (i
= 0; i
< 32 - off
; i
++) {
2021 if ((v
& (0x01 << i
)) == 0)
2025 printf(" %u", soff
+ off
);
2033 pr_suptypes(ni
, nilen
)
2034 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be SUPTYPES */
2039 const u_char
*cp
, *end
;
2042 u_int16_t words
; /*32bit count*/
2045 #define MAXQTYPES (1 << 16)
2049 cp
= (u_char
*)(ni
+ 1);
2050 end
= ((u_char
*)ni
) + nilen
;
2054 printf("NodeInfo Supported Qtypes");
2055 if (options
& F_VERBOSE
) {
2056 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
2057 printf(", compressed bitmap");
2059 printf(", raw bitmap");
2063 clen
= (size_t)(end
- cp
);
2064 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
2065 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
2071 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
2073 memcpy(&cbit
, cp
, sizeof(cbit
));
2074 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
2078 clen
= ntohs(cbit
.words
) * sizeof(v
);
2079 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
2084 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
2085 memcpy(&v
, cp
+ off
, sizeof(v
));
2086 v
= (u_int32_t
)ntohl(v
);
2087 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
2089 /* flush the remaining bits */
2090 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
2094 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
2095 cur
+= ntohs(cbit
.skip
) * 32;
2100 pr_nodeaddr(ni
, nilen
)
2101 struct icmp6_nodeinfo
*ni
; /* ni->qtype must be NODEADDR */
2104 u_char
*cp
= (u_char
*)(ni
+ 1);
2105 char ntop_buf
[INET6_ADDRSTRLEN
];
2108 nilen
-= sizeof(struct icmp6_nodeinfo
);
2110 if (options
& F_VERBOSE
) {
2111 switch (ni
->ni_code
) {
2112 case ICMP6_NI_REFUSED
:
2113 (void)printf("refused");
2115 case ICMP6_NI_UNKNOWN
:
2116 (void)printf("unknown qtype");
2119 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2120 (void)printf(" truncated");
2124 printf(" no address\n");
2127 * In icmp-name-lookups 05 and later, TTL of each returned address
2128 * is contained in the resposne. We try to detect the version
2129 * by the length of the data, but note that the detection algorithm
2130 * is incomplete. We assume the latest draft by default.
2132 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2138 /* XXX: alignment? */
2139 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2140 cp
+= sizeof(u_int32_t
);
2141 nilen
-= sizeof(u_int32_t
);
2144 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2146 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2147 printf(" %s", ntop_buf
);
2149 if (ttl
== 0xffffffff) {
2151 * XXX: can this convention be applied to all
2152 * type of TTL (i.e. non-ND TTL)?
2154 printf("(TTL=infty)");
2157 printf("(TTL=%u)", ttl
);
2161 nilen
-= sizeof(struct in6_addr
);
2162 cp
+= sizeof(struct in6_addr
);
2168 struct msghdr
*mhdr
;
2172 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2173 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2174 if (cm
->cmsg_len
== 0)
2177 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2178 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2179 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2180 return(*(int *)CMSG_DATA(cm
));
2186 struct in6_pktinfo
*
2187 get_rcvpktinfo(mhdr
)
2188 struct msghdr
*mhdr
;
2192 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2193 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2194 if (cm
->cmsg_len
== 0)
2197 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2198 cm
->cmsg_type
== IPV6_PKTINFO
&&
2199 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2200 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2208 struct msghdr
*mhdr
;
2210 #ifdef IPV6_RECVPATHMTU
2212 struct ip6_mtuinfo
*mtuctl
= NULL
;
2214 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2215 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2216 if (cm
->cmsg_len
== 0)
2219 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2220 cm
->cmsg_type
== IPV6_PATHMTU
&&
2221 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2222 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2225 * If the notified destination is different from
2226 * the one we are pinging, just ignore the info.
2227 * We check the scope ID only when both notified value
2228 * and our own value have non-0 values, because we may
2229 * have used the default scope zone ID for sending,
2230 * in which case the scope ID value is 0.
2232 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2234 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2235 dst
.sin6_scope_id
&&
2236 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2237 dst
.sin6_scope_id
)) {
2238 if ((options
& F_VERBOSE
) != 0) {
2239 printf("path MTU for %s is notified. "
2241 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2242 sizeof(mtuctl
->ip6m_addr
)));
2248 * Ignore an invalid MTU. XXX: can we just believe
2251 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2254 /* notification for our destination. return the MTU. */
2255 return((int)mtuctl
->ip6m_mtu
);
2264 struct msghdr
*mhdr
;
2268 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2269 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2270 if (cm
->cmsg_len
== 0)
2273 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2274 cm
->cmsg_type
== IPV6_TCLASS
&&
2275 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2276 return(*(int *)CMSG_DATA(cm
));
2286 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2291 struct timeval
*out
, *in
;
2293 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2295 out
->tv_usec
+= 1000000;
2297 out
->tv_sec
-= in
->tv_sec
;
2311 (void)signal(SIGINT
, SIG_DFL
);
2312 (void)kill(getpid(), SIGINT
);
2320 * Print out statistics.
2326 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2327 (void)printf("%ld packets transmitted, ", ntransmitted
);
2328 (void)printf("%ld packets received, ", nreceived
);
2330 (void)printf("+%ld duplicates, ", nrepeats
);
2332 if (nreceived
> ntransmitted
)
2333 (void)printf("-- somebody's duplicating packets!");
2335 (void)printf("%.1f%% packet loss",
2336 ((((double)ntransmitted
- nreceived
) * 100.0) /
2339 (void)putchar('\n');
2340 if (nreceived
&& timing
) {
2341 /* Only display average to microseconds */
2342 double num
= nreceived
+ nrepeats
;
2343 double avg
= tsum
/ num
;
2344 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2346 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2347 tmin
, avg
, tmax
, dev
);
2348 (void)fflush(stdout
);
2350 (void)fflush(stdout
);
2354 static const char *niqcode
[] = {
2356 "DNS label", /*or empty*/
2361 static const char *nircode
[] = {
2362 "Success", "Refused", "Unknown",
2368 * Print a descriptive string about an ICMP header.
2372 struct icmp6_hdr
*icp
;
2375 char ntop_buf
[INET6_ADDRSTRLEN
];
2376 struct nd_redirect
*red
;
2377 struct icmp6_nodeinfo
*ni
;
2378 char dnsname
[NS_MAXDNAME
+ 1];
2382 switch (icp
->icmp6_type
) {
2383 case ICMP6_DST_UNREACH
:
2384 switch (icp
->icmp6_code
) {
2385 case ICMP6_DST_UNREACH_NOROUTE
:
2386 (void)printf("No Route to Destination\n");
2388 case ICMP6_DST_UNREACH_ADMIN
:
2389 (void)printf("Destination Administratively "
2392 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2393 (void)printf("Destination Unreachable Beyond Scope\n");
2395 case ICMP6_DST_UNREACH_ADDR
:
2396 (void)printf("Destination Host Unreachable\n");
2398 case ICMP6_DST_UNREACH_NOPORT
:
2399 (void)printf("Destination Port Unreachable\n");
2402 (void)printf("Destination Unreachable, Bad Code: %d\n",
2406 /* Print returned IP header information */
2407 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2409 case ICMP6_PACKET_TOO_BIG
:
2410 (void)printf("Packet too big mtu = %d\n",
2411 (int)ntohl(icp
->icmp6_mtu
));
2412 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2414 case ICMP6_TIME_EXCEEDED
:
2415 switch (icp
->icmp6_code
) {
2416 case ICMP6_TIME_EXCEED_TRANSIT
:
2417 (void)printf("Time to live exceeded\n");
2419 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2420 (void)printf("Frag reassembly time exceeded\n");
2423 (void)printf("Time exceeded, Bad Code: %d\n",
2427 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2429 case ICMP6_PARAM_PROB
:
2430 (void)printf("Parameter problem: ");
2431 switch (icp
->icmp6_code
) {
2432 case ICMP6_PARAMPROB_HEADER
:
2433 (void)printf("Erroneous Header ");
2435 case ICMP6_PARAMPROB_NEXTHEADER
:
2436 (void)printf("Unknown Nextheader ");
2438 case ICMP6_PARAMPROB_OPTION
:
2439 (void)printf("Unrecognized Option ");
2442 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2445 (void)printf("pointer = 0x%02x\n",
2446 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2447 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2449 case ICMP6_ECHO_REQUEST
:
2450 (void)printf("Echo Request");
2451 /* XXX ID + Seq + Data */
2453 case ICMP6_ECHO_REPLY
:
2454 (void)printf("Echo Reply");
2455 /* XXX ID + Seq + Data */
2457 case ICMP6_MEMBERSHIP_QUERY
:
2458 (void)printf("Listener Query");
2460 case ICMP6_MEMBERSHIP_REPORT
:
2461 (void)printf("Listener Report");
2463 case ICMP6_MEMBERSHIP_REDUCTION
:
2464 (void)printf("Listener Done");
2466 case ND_ROUTER_SOLICIT
:
2467 (void)printf("Router Solicitation");
2469 case ND_ROUTER_ADVERT
:
2470 (void)printf("Router Advertisement");
2472 case ND_NEIGHBOR_SOLICIT
:
2473 (void)printf("Neighbor Solicitation");
2475 case ND_NEIGHBOR_ADVERT
:
2476 (void)printf("Neighbor Advertisement");
2479 red
= (struct nd_redirect
*)icp
;
2480 (void)printf("Redirect\n");
2481 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2483 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2484 (void)printf("Destination: %s", ntop_buf
);
2485 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2487 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2488 (void)printf(" New Target: %s", ntop_buf
);
2490 case ICMP6_NI_QUERY
:
2491 (void)printf("Node Information Query");
2492 /* XXX ID + Seq + Data */
2493 ni
= (struct icmp6_nodeinfo
*)icp
;
2494 l
= end
- (u_char
*)(ni
+ 1);
2496 switch (ntohs(ni
->ni_qtype
)) {
2498 (void)printf("NOOP");
2500 case NI_QTYPE_SUPTYPES
:
2501 (void)printf("Supported qtypes");
2504 (void)printf("DNS name");
2506 case NI_QTYPE_NODEADDR
:
2507 (void)printf("nodeaddr");
2509 case NI_QTYPE_IPV4ADDR
:
2510 (void)printf("IPv4 nodeaddr");
2513 (void)printf("unknown qtype");
2516 if (options
& F_VERBOSE
) {
2517 switch (ni
->ni_code
) {
2518 case ICMP6_NI_SUBJ_IPV6
:
2519 if (l
== sizeof(struct in6_addr
) &&
2520 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2521 sizeof(ntop_buf
)) != NULL
) {
2522 (void)printf(", subject=%s(%s)",
2523 niqcode
[ni
->ni_code
], ntop_buf
);
2526 /* backward compat to -W */
2527 (void)printf(", oldfqdn");
2529 (void)printf(", invalid");
2533 case ICMP6_NI_SUBJ_FQDN
:
2534 if (end
== (u_char
*)(ni
+ 1)) {
2535 (void)printf(", no subject");
2538 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2539 cp
= (const u_char
*)(ni
+ 1);
2540 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2541 sizeof(dnsname
)) != NULL
)
2542 printf("(%s)", dnsname
);
2544 printf("(invalid)");
2546 case ICMP6_NI_SUBJ_IPV4
:
2547 if (l
== sizeof(struct in_addr
) &&
2548 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2549 sizeof(ntop_buf
)) != NULL
) {
2550 (void)printf(", subject=%s(%s)",
2551 niqcode
[ni
->ni_code
], ntop_buf
);
2553 (void)printf(", invalid");
2556 (void)printf(", invalid");
2561 case ICMP6_NI_REPLY
:
2562 (void)printf("Node Information Reply");
2563 /* XXX ID + Seq + Data */
2564 ni
= (struct icmp6_nodeinfo
*)icp
;
2566 switch (ntohs(ni
->ni_qtype
)) {
2568 (void)printf("NOOP");
2570 case NI_QTYPE_SUPTYPES
:
2571 (void)printf("Supported qtypes");
2574 (void)printf("DNS name");
2576 case NI_QTYPE_NODEADDR
:
2577 (void)printf("nodeaddr");
2579 case NI_QTYPE_IPV4ADDR
:
2580 (void)printf("IPv4 nodeaddr");
2583 (void)printf("unknown qtype");
2586 if (options
& F_VERBOSE
) {
2587 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2588 printf(", invalid");
2590 printf(", %s", nircode
[ni
->ni_code
]);
2594 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2600 * Print an IP6 header.
2604 struct ip6_hdr
*ip6
;
2606 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2608 char ntop_buf
[INET6_ADDRSTRLEN
];
2610 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2611 tc
= (tc
>> 4) & 0x0f;
2612 tc
|= (ip6
->ip6_vfc
<< 4);
2614 printf("Vr TC Flow Plen Nxt Hlim\n");
2615 printf(" %1x %02x %05x %04x %02x %02x\n",
2616 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2617 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2618 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2619 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2620 printf("%s->", ntop_buf
);
2621 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2622 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2623 printf("%s\n", ntop_buf
);
2628 * Return an ascii host address as a dotted quad and optionally with
2632 pr_addr(addr
, addrlen
)
2633 struct sockaddr
*addr
;
2636 static char buf
[NI_MAXHOST
];
2639 if ((options
& F_HOSTNAME
) == 0)
2640 flag
|= NI_NUMERICHOST
;
2642 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2650 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2654 struct ip6_hdr
*ip6
;
2657 u_char
*cp
= (u_char
*)ip6
, nh
;
2660 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2665 hlen
= sizeof(*ip6
);
2669 while (end
- cp
>= 8) {
2671 case IPPROTO_HOPOPTS
:
2673 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2674 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2676 case IPPROTO_DSTOPTS
:
2678 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2679 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2681 case IPPROTO_FRAGMENT
:
2683 hlen
= sizeof(struct ip6_frag
);
2684 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2686 case IPPROTO_ROUTING
:
2688 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2689 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2694 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2695 nh
= ((struct ah
*)cp
)->ah_nxt
;
2698 case IPPROTO_ICMPV6
:
2699 printf("ICMP6: type = %d, code = %d\n",
2706 printf("TCP: from port %u, to port %u (decimal)\n",
2707 (*cp
* 256 + *(cp
+ 1)),
2708 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2711 printf("UDP: from port %u, to port %u (decimal)\n",
2712 (*cp
* 256 + *(cp
+ 1)),
2713 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2716 printf("Unknown Header(%d)\n", nh
);
2720 if ((cp
+= hlen
) >= end
)
2742 for (cp
= patp
; *cp
; cp
++)
2744 errx(1, "patterns must be specified as hex digits");
2746 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2747 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2748 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2749 &pat
[13], &pat
[14], &pat
[15]);
2754 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
2756 for (jj
= 0; jj
< ii
; ++jj
)
2757 bp
[jj
+ kk
] = pat
[jj
];
2758 if (!(options
& F_QUIET
)) {
2759 (void)printf("PATTERN: 0x");
2760 for (jj
= 0; jj
< ii
; ++jj
)
2761 (void)printf("%02x", bp
[jj
] & 0xFF);
2767 #ifdef IPSEC_POLICY_IPSEC
2769 setpolicy(so
, policy
)
2776 return 0; /* ignore */
2778 buf
= ipsec_set_policy(policy
, strlen(policy
));
2780 errx(1, "%s", ipsec_strerror());
2781 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2782 ipsec_get_policylen(buf
)) < 0)
2783 warnx("Unable to set IPsec policy");
2798 u_int8_t digest
[16];
2801 char hbuf
[NI_MAXHOST
];
2802 struct in6_addr in6
;
2804 p
= strchr(name
, '.');
2806 p
= name
+ strlen(name
);
2808 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2809 return NULL
; /*label too long*/
2810 strncpy(hbuf
, name
, l
);
2811 hbuf
[(int)l
] = '\0';
2813 for (q
= name
; *q
; q
++) {
2814 if (isupper(*(unsigned char *)q
))
2815 *q
= tolower(*(unsigned char *)q
);
2818 /* generate 8 bytes of pseudo-random value. */
2819 memset(&ctxt
, 0, sizeof(ctxt
));
2822 MD5Update(&ctxt
, &c
, sizeof(c
));
2823 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2824 MD5Final(digest
, &ctxt
);
2826 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2827 return NULL
; /*XXX*/
2828 bcopy(digest
, &in6
.s6_addr
[12], 4);
2830 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2833 return strdup(hbuf
);
2839 (void)fprintf(stderr
,
2840 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2845 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2849 #ifdef IPV6_USE_MIN_MTU
2853 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2854 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2855 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2859 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
2860 "[hops ...] host\n");