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
31 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
32 * @(#)ping.c 8.1 (Berkeley) 6/5/93
33 * $FreeBSD: src/sbin/ping6/ping6.c,v 1.30 2007/04/19 15:41:00 mtm Exp $
34 * $DragonFly: src/sbin/ping6/ping6.c,v 1.10 2008/09/04 10:13:10 hasso Exp $
37 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
40 * Copyright (c) 1989, 1993
41 * The Regents of the University of California. All rights reserved.
43 * This code is derived from software contributed to Berkeley by
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
77 * measure round-trip-delays and packet loss across network paths.
81 * U. S. Army Ballistic Research Laboratory
85 * Public Domain. Distribution Unlimited.
87 * More statistics could always be gathered.
88 * This program has to run SUID to ROOT to access the ICMP socket.
92 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
93 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
94 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
95 * network attached to 1 or more interfaces)
98 #include <sys/param.h>
100 #include <sys/socket.h>
101 #include <sys/time.h>
104 #include <net/route.h>
106 #include <netinet/in.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp6.h>
109 #include <arpa/inet.h>
110 #include <arpa/nameser.h>
128 #include <netinet6/ah.h>
129 #include <netinet6/ipsec.h>
139 #define MAXPACKETLEN 131072
141 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
142 #define ICMP6ECHOTMLEN sizeof(struct tv32)
143 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
144 #define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
145 /* FQDN case, 64 bits of nonce + 32 bits ttl */
146 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
147 #define EXTRA 256 /* for AH and various other headers. weird. */
148 #define DEFDATALEN ICMP6ECHOTMLEN
149 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
150 #define NROUTES 9 /* number of record route slots */
152 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
153 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
154 #define SET(bit) (A(bit) |= B(bit))
155 #define CLR(bit) (A(bit) &= (~B(bit)))
156 #define TST(bit) (A(bit) & B(bit))
158 #define F_FLOOD 0x0001
159 #define F_INTERVAL 0x0002
160 #define F_PINGFILLED 0x0008
161 #define F_QUIET 0x0010
162 #define F_RROUTE 0x0020
163 #define F_SO_DEBUG 0x0040
164 #define F_VERBOSE 0x0100
166 #ifdef IPSEC_POLICY_IPSEC
167 #define F_POLICY 0x0400
169 #define F_AUTHHDR 0x0200
170 #define F_ENCRYPT 0x0400
171 #endif /*IPSEC_POLICY_IPSEC*/
173 #define F_NODEADDR 0x0800
174 #define F_FQDN 0x1000
175 #define F_INTERFACE 0x2000
176 #define F_SRCADDR 0x4000
177 #define F_HOSTNAME 0x10000
178 #define F_FQDNOLD 0x20000
179 #define F_NIGROUP 0x40000
180 #define F_SUPTYPES 0x80000
181 #define F_NOMINMTU 0x100000
182 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
185 #define IN6LEN sizeof(struct in6_addr)
186 #define SA6LEN sizeof(struct sockaddr_in6)
187 #define DUMMY_PORT 10101
189 #define SIN6(s) ((struct sockaddr_in6 *)(s))
192 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
193 * number of received sequence numbers we can keep track of. Change 128
194 * to 8192 for complete accuracy...
196 #define MAX_DUP_CHK (8 * 8192)
197 int mx_dup_ck
= MAX_DUP_CHK
;
198 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
200 struct addrinfo
*res
;
201 struct sockaddr_in6 dst
; /* who to ping6 */
202 struct sockaddr_in6 src
; /* src addr of this packet */
204 int datalen
= DEFDATALEN
;
205 int s
; /* socket file descriptor */
206 u_char outpack
[MAXPACKETLEN
];
207 char BSPACE
= '\b'; /* characters written for flood */
210 int ident
; /* process id to identify our packets */
211 u_int8_t nonce
[8]; /* nonce field for node information */
212 int hoplimit
= -1; /* hoplimit */
213 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
216 long npackets
; /* max packets to transmit */
217 long nreceived
; /* # of packets we got back */
218 long nrepeats
; /* number of duplicates */
219 long ntransmitted
; /* sequence # for outbound packets = #sent */
220 struct timeval interval
= {1, 0}; /* interval between packets */
223 int timing
; /* flag to do timing */
224 double tmin
= 999999999.0; /* minimum round trip time */
225 double tmax
= 0.0; /* maximum round trip time */
226 double tsum
= 0.0; /* sum of all times, for doing average */
227 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
229 /* for node addresses */
232 /* for ancillary data(advanced API) */
233 struct msghdr smsghdr
;
234 struct iovec smsgiov
;
237 volatile sig_atomic_t seenalrm
;
238 volatile sig_atomic_t seenint
;
240 volatile sig_atomic_t seeninfo
;
243 int main(int, char *[]);
244 void fill(char *, char *);
245 int get_hoplim(struct msghdr
*);
246 #ifdef IPV6_RECVPATHMTU
247 int get_pathmtu(struct msghdr
*);
249 #define get_pathmtu(mhdr) (0)
251 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
253 void retransmit(void);
255 size_t pingerlen(void);
257 const char *pr_addr(struct sockaddr
*, int);
258 void pr_icmph(struct icmp6_hdr
*, u_char
*);
259 void pr_iph(struct ip6_hdr
*);
260 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
261 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
262 int myechoreply(const struct icmp6_hdr
*);
263 int mynireply(const struct icmp6_nodeinfo
*);
264 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
266 void pr_pack(u_char
*, int, struct msghdr
*);
267 void pr_exthdrs(struct msghdr
*);
268 void pr_ip6opt(void *, size_t);
269 void pr_rthdr(void *, size_t);
270 int pr_bitrange(u_int32_t
, int, int);
271 void pr_retip(struct ip6_hdr
*, u_char
*);
273 void tvsub(struct timeval
*, struct timeval
*);
274 int setpolicy(int, char *);
275 char *nigroup(char *);
279 main(int argc
, char **argv
)
281 struct itimerval itimer
;
282 struct sockaddr_in6 from
;
283 #ifndef HAVE_ARC4RANDOM
289 struct timeval timeout
, *tv
;
291 struct addrinfo hints
;
293 struct pollfd fdmaskp
[1];
299 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
300 u_char
*datap
, *packet
;
301 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
303 struct cmsghdr
*scmsgp
= NULL
;
305 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
310 struct in6_pktinfo
*pktinfo
= NULL
;
312 struct ip6_rthdr
*rthdr
= NULL
;
314 #ifdef IPSEC_POLICY_IPSEC
315 char *policy_in
= NULL
;
316 char *policy_out
= NULL
;
318 #ifdef IPV6_USE_MIN_MTU
323 /* just to be sure */
324 memset(&smsghdr
, 0, sizeof(smsghdr
));
325 memset(&smsgiov
, 0, sizeof(smsgiov
));
328 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
332 #ifdef IPSEC_POLICY_IPSEC
336 #endif /*IPSEC_POLICY_IPSEC*/
338 while ((ch
= getopt(argc
, argv
,
339 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS
)) != -1) {
346 options
&= ~F_NOUSERDATA
;
347 options
|= F_NODEADDR
;
348 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
351 naflags
|= NI_NODEADDR_FLAG_ALL
;
355 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
359 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
363 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
367 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
369 case 'A': /* experimental. not in the spec */
370 #ifdef NI_NODEADDR_FLAG_ANYCAST
371 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
375 "-a A is not supported on the platform");
386 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
389 lsockbufsize
= strtoul(optarg
, &e
, 10);
390 sockbufsize
= lsockbufsize
;
391 if (errno
|| !*optarg
|| *e
||
392 sockbufsize
!= (long)lsockbufsize
)
393 errx(1, "invalid socket buffer size");
396 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
400 npackets
= strtol(optarg
, &e
, 10);
401 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
403 "illegal number of packets -- %s", optarg
);
406 options
|= F_SO_DEBUG
;
411 errx(1, "Must be superuser to flood ping");
414 setbuf(stdout
, NULL
);
420 options
|= F_HOSTNAME
;
422 case 'h': /* hoplimit */
423 hoplimit
= strtol(optarg
, &e
, 10);
424 if (*optarg
== '\0' || *e
!= '\0')
425 errx(1, "illegal hoplimit %s", optarg
);
426 if (255 < hoplimit
|| hoplimit
< -1)
428 "illegal hoplimit -- %s", optarg
);
432 options
|= F_INTERFACE
;
433 #ifndef USE_SIN6_SCOPE_ID
437 case 'i': /* wait between sending packets */
438 intval
= strtod(optarg
, &e
);
439 if (*optarg
== '\0' || *e
!= '\0')
440 errx(1, "illegal timing interval %s", optarg
);
441 if (intval
< 1 && getuid()) {
442 errx(1, "%s: only root may use interval < 1s",
445 interval
.tv_sec
= (long)intval
;
447 (long)((intval
- interval
.tv_sec
) * 1000000);
448 if (interval
.tv_sec
< 0)
449 errx(1, "illegal timing interval %s", optarg
);
450 /* less than 1/hz does not make sense */
451 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
452 warnx("too small interval, raised to 0.01");
453 interval
.tv_usec
= 10000;
455 options
|= F_INTERVAL
;
460 errx(1, "Must be superuser to preload");
462 preload
= strtol(optarg
, &e
, 10);
463 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
464 errx(1, "illegal preload value -- %s", optarg
);
467 #ifdef IPV6_USE_MIN_MTU
471 errx(1, "-%c is not supported on this platform", ch
);
475 options
&= ~F_HOSTNAME
;
478 options
|= F_NIGROUP
;
480 case 'p': /* fill buffer with user pattern */
481 options
|= F_PINGFILLED
;
482 fill((char *)datap
, optarg
);
488 memset(&hints
, 0, sizeof(struct addrinfo
));
489 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
490 hints
.ai_family
= AF_INET6
;
491 hints
.ai_socktype
= SOCK_RAW
;
492 hints
.ai_protocol
= IPPROTO_ICMPV6
;
494 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
496 errx(1, "invalid source address: %s",
497 gai_strerror(ret_ga
));
500 * res->ai_family must be AF_INET6 and res->ai_addrlen
501 * must be sizeof(src).
503 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
504 srclen
= res
->ai_addrlen
;
506 options
|= F_SRCADDR
;
508 case 's': /* size of packet to send */
509 datalen
= strtol(optarg
, &e
, 10);
510 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
511 errx(1, "illegal datalen value -- %s", optarg
);
512 if (datalen
> MAXDATALEN
) {
514 "datalen value too large, maximum is %d",
519 options
&= ~F_NOUSERDATA
;
520 options
|= F_SUPTYPES
;
523 options
|= F_VERBOSE
;
526 options
&= ~F_NOUSERDATA
;
530 options
&= ~F_NOUSERDATA
;
531 options
|= F_FQDNOLD
;
534 #ifdef IPSEC_POLICY_IPSEC
537 if (!strncmp("in", optarg
, 2)) {
538 if ((policy_in
= strdup(optarg
)) == NULL
)
540 } else if (!strncmp("out", optarg
, 3)) {
541 if ((policy_out
= strdup(optarg
)) == NULL
)
544 errx(1, "invalid security policy");
548 options
|= F_AUTHHDR
;
551 options
|= F_ENCRYPT
;
553 #endif /*IPSEC_POLICY_IPSEC*/
569 errx(1, "too many arguments");
573 if (options
& F_NIGROUP
) {
574 target
= nigroup(argv
[argc
- 1]);
575 if (target
== NULL
) {
580 target
= argv
[argc
- 1];
583 memset(&hints
, 0, sizeof(struct addrinfo
));
584 hints
.ai_flags
= AI_CANONNAME
;
585 hints
.ai_family
= AF_INET6
;
586 hints
.ai_socktype
= SOCK_RAW
;
587 hints
.ai_protocol
= IPPROTO_ICMPV6
;
589 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
591 errx(1, "%s", gai_strerror(ret_ga
));
592 if (res
->ai_canonname
)
593 hostname
= res
->ai_canonname
;
598 errx(1, "getaddrinfo failed");
600 memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
602 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
603 res
->ai_protocol
)) < 0)
606 /* set the source address if specified. */
607 if ((options
& F_SRCADDR
) &&
608 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
612 /* set the gateway (next hop) if specified */
614 struct addrinfo ghints
, *gres
;
617 memset(&ghints
, 0, sizeof(ghints
));
618 ghints
.ai_family
= AF_INET6
;
619 ghints
.ai_socktype
= SOCK_RAW
;
620 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
622 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
624 errx(1, "getaddrinfo for the gateway %s: %s",
625 gateway
, gai_strerror(error
));
627 if (gres
->ai_next
&& (options
& F_VERBOSE
))
628 warnx("gateway resolves to multiple addresses");
630 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
631 gres
->ai_addr
, gres
->ai_addrlen
)) {
632 err(1, "setsockopt(IPV6_NEXTHOP)");
639 * let the kerel pass extension headers of incoming packets,
640 * for privileged socket options
642 if ((options
& F_VERBOSE
) != 0) {
645 #ifdef IPV6_RECVHOPOPTS
646 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
648 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
649 #else /* old adv. API */
650 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
652 err(1, "setsockopt(IPV6_HOPOPTS)");
654 #ifdef IPV6_RECVDSTOPTS
655 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
657 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
658 #else /* old adv. API */
659 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
661 err(1, "setsockopt(IPV6_DSTOPTS)");
663 #ifdef IPV6_RECVRTHDRDSTOPTS
664 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
666 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
670 /* revoke root privilege */
674 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
675 errx(1, "-f and -i incompatible options");
677 if ((options
& F_NOUSERDATA
) == 0) {
678 if (datalen
>= (int)sizeof(struct tv32
)) {
679 /* we can time transfer */
683 /* in F_VERBOSE case, we may get non-echoreply packets*/
684 if (options
& F_VERBOSE
)
685 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
687 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
689 /* suppress timing for node information query */
692 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
695 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
696 err(1, "Unable to allocate packet");
697 if (!(options
& F_PINGFILLED
))
698 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
701 ident
= getpid() & 0xFFFF;
702 #ifndef HAVE_ARC4RANDOM
703 gettimeofday(&seed
, NULL
);
704 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
705 memset(nonce
, 0, sizeof(nonce
));
706 for (i
= 0; i
< (int)sizeof(nonce
); i
+= sizeof(int))
707 *((int *)&nonce
[i
]) = rand();
709 memset(nonce
, 0, sizeof(nonce
));
710 for (i
= 0; i
< (int)sizeof(nonce
); i
+= sizeof(u_int32_t
))
711 *((u_int32_t
*)&nonce
[i
]) = arc4random();
716 if (options
& F_SO_DEBUG
)
717 setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
719 optval
= IPV6_DEFHLIM
;
720 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
721 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
722 &optval
, sizeof(optval
)) == -1)
723 err(1, "IPV6_MULTICAST_HOPS");
724 #ifdef IPV6_USE_MIN_MTU
726 optval
= mflag
> 1 ? 0 : 1;
727 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
728 &optval
, sizeof(optval
)) == -1)
729 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
731 #ifdef IPV6_RECVPATHMTU
734 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
735 &optval
, sizeof(optval
)) == -1)
736 err(1, "setsockopt(IPV6_RECVPATHMTU)");
738 #endif /* IPV6_RECVPATHMTU */
739 #endif /* IPV6_USE_MIN_MTU */
742 #ifdef IPSEC_POLICY_IPSEC
743 if (options
& F_POLICY
) {
744 if (setpolicy(s
, policy_in
) < 0)
745 errx(1, "%s", ipsec_strerror());
746 if (setpolicy(s
, policy_out
) < 0)
747 errx(1, "%s", ipsec_strerror());
750 if (options
& F_AUTHHDR
) {
751 optval
= IPSEC_LEVEL_REQUIRE
;
752 #ifdef IPV6_AUTH_TRANS_LEVEL
753 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
754 &optval
, sizeof(optval
)) == -1)
755 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
757 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
758 &optval
, sizeof(optval
)) == -1)
759 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
762 if (options
& F_ENCRYPT
) {
763 optval
= IPSEC_LEVEL_REQUIRE
;
764 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
765 &optval
, sizeof(optval
)) == -1)
766 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
768 #endif /*IPSEC_POLICY_IPSEC*/
773 struct icmp6_filter filt
;
774 if (!(options
& F_VERBOSE
)) {
775 ICMP6_FILTER_SETBLOCKALL(&filt
);
776 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
777 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
778 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
780 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
782 ICMP6_FILTER_SETPASSALL(&filt
);
784 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
786 err(1, "setsockopt(ICMP6_FILTER)");
788 #endif /*ICMP6_FILTER*/
790 /* let the kerel pass extension headers of incoming packets */
791 if ((options
& F_VERBOSE
) != 0) {
794 #ifdef IPV6_RECVRTHDR
795 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
797 err(1, "setsockopt(IPV6_RECVRTHDR)");
798 #else /* old adv. API */
799 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
801 err(1, "setsockopt(IPV6_RTHDR)");
807 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
808 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
809 &optval, sizeof(optval)) == -1)
810 err(1, "IPV6_MULTICAST_LOOP");
813 /* Specify the outgoing interface and/or the source address */
815 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
818 ip6optlen
+= CMSG_SPACE(sizeof(int));
820 /* set IP6 packet options */
822 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
823 errx(1, "can't allocate enough memory");
824 smsghdr
.msg_control
= (caddr_t
)scmsg
;
825 smsghdr
.msg_controllen
= ip6optlen
;
826 scmsgp
= (struct cmsghdr
*)scmsg
;
829 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
830 memset(pktinfo
, 0, sizeof(*pktinfo
));
831 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
832 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
833 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
834 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
837 /* set the outgoing interface */
839 #ifndef USE_SIN6_SCOPE_ID
840 /* pktinfo must have already been allocated */
841 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
842 errx(1, "%s: invalid interface name", ifname
);
844 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
845 errx(1, "%s: invalid interface name", ifname
);
848 if (hoplimit
!= -1) {
849 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
850 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
851 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
852 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
854 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
857 if (!(options
& F_SRCADDR
)) {
859 * get the source address. XXX since we revoked the root
860 * privilege, we cannot use a raw socket for this.
863 socklen_t len
= sizeof(src
);
865 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
866 err(1, "UDP socket");
868 src
.sin6_family
= AF_INET6
;
869 src
.sin6_addr
= dst
.sin6_addr
;
870 src
.sin6_port
= ntohs(DUMMY_PORT
);
871 src
.sin6_scope_id
= dst
.sin6_scope_id
;
875 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
876 (void *)pktinfo
, sizeof(*pktinfo
)))
877 err(1, "UDP setsockopt(IPV6_PKTINFO)");
878 if (hoplimit
!= -1 &&
879 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
880 (void *)&hoplimit
, sizeof(hoplimit
)))
881 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
882 if (hoplimit
!= -1 &&
883 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
884 (void *)&hoplimit
, sizeof(hoplimit
)))
885 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
887 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
888 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
889 err(1, "UDP setsockopt(IPV6_RTHDR)");
890 #else /* old advanced API */
891 if (smsghdr
.msg_control
&&
892 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
893 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
894 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
897 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
898 err(1, "UDP connect");
900 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
901 err(1, "getsockname");
906 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
908 if (datalen
> sockbufsize
)
909 warnx("you need -b to increase socket buffer size");
910 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
911 sizeof(sockbufsize
)) < 0)
912 err(1, "setsockopt(SO_SNDBUF)");
913 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
914 sizeof(sockbufsize
)) < 0)
915 err(1, "setsockopt(SO_RCVBUF)");
918 if (datalen
> 8 * 1024) /*XXX*/
919 warnx("you need -b to increase socket buffer size");
921 * When pinging the broadcast address, you can get a lot of
922 * answers. Doing something so evil is useful if you are trying
923 * to stress the ethernet, or just want to fill the arp cache
924 * to get some stuff for /etc/ethers.
927 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
933 #ifndef USE_SIN6_SCOPE_ID
934 #ifdef IPV6_RECVPKTINFO
935 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
937 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
938 #else /* old adv. API */
939 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
941 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
943 #endif /* USE_SIN6_SCOPE_ID */
944 #ifdef IPV6_RECVHOPLIMIT
945 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
947 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
948 #else /* old adv. API */
949 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
951 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
954 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
955 (unsigned long)(pingerlen() - 8));
956 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
957 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
959 while (preload
--) /* Fire off them quickies. */
962 signal(SIGINT
, onsignal
);
964 signal(SIGINFO
, onsignal
);
967 if ((options
& F_FLOOD
) == 0) {
968 signal(SIGALRM
, onsignal
);
969 itimer
.it_interval
= interval
;
970 itimer
.it_value
= interval
;
971 setitimer(ITIMER_REAL
, &itimer
, NULL
);
972 if (ntransmitted
== 0)
977 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
978 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
982 seenalrm
= seenint
= 0;
987 /* For control (ancillary) data received from recvmsg() */
988 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
996 /* signal handling */
1015 if (options
& F_FLOOD
) {
1021 timeout
.tv_usec
= 10000;
1033 fdmaskp
[0].events
= POLLIN
;
1034 cc
= poll(fdmaskp
, 1, timeout
);
1036 memset(fdmaskp
, 0, fdmasks
);
1038 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1041 if (errno
!= EINTR
) {
1053 m
.msg_name
= (caddr_t
)&from
;
1054 m
.msg_namelen
= sizeof(from
);
1055 memset(&iov
, 0, sizeof(iov
));
1056 iov
[0].iov_base
= (caddr_t
)packet
;
1057 iov
[0].iov_len
= packlen
;
1060 memset(cm
, 0, CONTROLLEN
);
1061 m
.msg_control
= (void *)cm
;
1062 m
.msg_controllen
= CONTROLLEN
;
1064 cc
= recvmsg(s
, &m
, 0);
1066 if (errno
!= EINTR
) {
1071 } else if (cc
== 0) {
1075 * receive control messages only. Process the
1076 * exceptions (currently the only possiblity is
1077 * a path MTU notification.)
1079 if ((mtu
= get_pathmtu(&m
)) > 0) {
1080 if ((options
& F_VERBOSE
) != 0) {
1081 printf("new path MTU (%d) is "
1088 * an ICMPv6 message (probably an echoreply) arrived.
1090 pr_pack(packet
, cc
, &m
);
1092 if (npackets
&& nreceived
>= npackets
)
1096 exit(nreceived
== 0);
1119 * This routine transmits another ping6.
1124 struct itimerval itimer
;
1130 * If we're not transmitting any more packets, change the timer
1131 * to wait two round-trip times if we've received any packets or
1132 * ten seconds if we haven't.
1136 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1137 if (itimer
.it_value
.tv_sec
== 0)
1138 itimer
.it_value
.tv_sec
= 1;
1140 itimer
.it_value
.tv_sec
= MAXWAIT
;
1141 itimer
.it_interval
.tv_sec
= 0;
1142 itimer
.it_interval
.tv_usec
= 0;
1143 itimer
.it_value
.tv_usec
= 0;
1145 signal(SIGALRM
, onint
);
1146 setitimer(ITIMER_REAL
, &itimer
, NULL
);
1151 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1152 * will be added on by the kernel. The ID field is our UNIX process ID,
1153 * and the sequence number is an ascending integer. The first 8 bytes
1154 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1155 * byte-order, to compute the round-trip time.
1162 if (options
& F_FQDN
)
1163 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1164 else if (options
& F_FQDNOLD
)
1166 else if (options
& F_NODEADDR
)
1167 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1168 else if (options
& F_SUPTYPES
)
1171 l
= ICMP6ECHOLEN
+ datalen
;
1179 struct icmp6_hdr
*icp
;
1180 struct iovec iov
[2];
1182 struct icmp6_nodeinfo
*nip
;
1185 if (npackets
&& ntransmitted
>= npackets
)
1186 return(-1); /* no more transmission */
1188 icp
= (struct icmp6_hdr
*)outpack
;
1189 nip
= (struct icmp6_nodeinfo
*)outpack
;
1190 memset(icp
, 0, sizeof(*icp
));
1191 icp
->icmp6_cksum
= 0;
1192 seq
= ntransmitted
++;
1193 CLR(seq
% mx_dup_ck
);
1195 if (options
& F_FQDN
) {
1196 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1197 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1198 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1199 nip
->ni_flags
= htons(0);
1201 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1202 sizeof(nip
->icmp6_ni_nonce
));
1203 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1205 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1206 sizeof(dst
.sin6_addr
));
1207 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1209 } else if (options
& F_FQDNOLD
) {
1210 /* packet format in 03 draft - no Subject data on queries */
1211 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1212 icp
->icmp6_code
= 0; /* code field is always 0 */
1213 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1214 nip
->ni_flags
= htons(0);
1216 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1217 sizeof(nip
->icmp6_ni_nonce
));
1218 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1222 } else if (options
& F_NODEADDR
) {
1223 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1224 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1225 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1226 nip
->ni_flags
= naflags
;
1228 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1229 sizeof(nip
->icmp6_ni_nonce
));
1230 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1232 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1233 sizeof(dst
.sin6_addr
));
1234 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1236 } else if (options
& F_SUPTYPES
) {
1237 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1238 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1239 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1240 /* we support compressed bitmap */
1241 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1243 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1244 sizeof(nip
->icmp6_ni_nonce
));
1245 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1249 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1250 icp
->icmp6_code
= 0;
1251 icp
->icmp6_id
= htons(ident
);
1252 icp
->icmp6_seq
= ntohs(seq
);
1256 gettimeofday(&tv
, NULL
);
1257 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1258 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1259 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1261 cc
= ICMP6ECHOLEN
+ datalen
;
1265 if (pingerlen() != cc
)
1266 errx(1, "internal error; length mismatch");
1269 smsghdr
.msg_name
= (caddr_t
)&dst
;
1270 smsghdr
.msg_namelen
= sizeof(dst
);
1271 memset(&iov
, 0, sizeof(iov
));
1272 iov
[0].iov_base
= (caddr_t
)outpack
;
1273 iov
[0].iov_len
= cc
;
1274 smsghdr
.msg_iov
= iov
;
1275 smsghdr
.msg_iovlen
= 1;
1277 i
= sendmsg(s
, &smsghdr
, 0);
1279 if (i
< 0 || i
!= cc
) {
1282 printf("ping6: wrote %s %d chars, ret=%d\n", hostname
, cc
, i
);
1284 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1285 write(STDOUT_FILENO
, &DOT
, 1);
1291 myechoreply(const struct icmp6_hdr
*icp
)
1293 if (ntohs(icp
->icmp6_id
) == ident
)
1300 mynireply(const struct icmp6_nodeinfo
*nip
)
1302 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1303 nonce
+ sizeof(u_int16_t
),
1304 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1311 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1316 char cresult
[MAXDNAME
+ 1];
1327 if (i
== 0 || cp
!= *sp
) {
1328 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1329 return NULL
; /*result overrun*/
1335 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1336 /* DNS compression */
1340 comp
= base
+ (i
& 0x3f);
1341 if (dnsdecode(&comp
, cp
, base
, cresult
,
1342 sizeof(cresult
)) == NULL
)
1344 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1345 return NULL
; /*result overrun*/
1347 } else if ((i
& 0x3f) == i
) {
1349 return NULL
; /*source overrun*/
1350 while (i
-- > 0 && cp
< ep
) {
1351 l
= snprintf(cresult
, sizeof(cresult
),
1352 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1353 if (l
>= (int)sizeof(cresult
) || l
< 0)
1355 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1356 return NULL
; /*result overrun*/
1360 return NULL
; /*invalid label*/
1363 return NULL
; /*not terminated*/
1371 * Print out the packet, if it came from us. This logic is necessary
1372 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1373 * which arrive ('tis only fair). This permits multiple copies of this
1374 * program to be run without having intermingled output (or statistics!).
1377 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1379 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1380 struct icmp6_hdr
*icp
;
1381 struct icmp6_nodeinfo
*ni
;
1384 struct sockaddr
*from
;
1386 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1387 struct in6_pktinfo
*pktinfo
= NULL
;
1388 struct timeval tv
, tp
;
1390 double triptime
= 0;
1395 char dnsname
[MAXDNAME
+ 1];
1397 gettimeofday(&tv
, NULL
);
1399 if (!mhdr
|| !mhdr
->msg_name
||
1400 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1401 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1402 if (options
& F_VERBOSE
)
1403 warnx("invalid peername");
1406 from
= (struct sockaddr
*)mhdr
->msg_name
;
1407 fromlen
= mhdr
->msg_namelen
;
1408 if (cc
< (int)sizeof(struct icmp6_hdr
)) {
1409 if (options
& F_VERBOSE
)
1410 warnx("packet too short (%d bytes) from %s", cc
,
1411 pr_addr(from
, fromlen
));
1414 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1415 (options
& F_VERBOSE
) != 0)
1416 warnx("some control data discarded, insufficient buffer size");
1417 icp
= (struct icmp6_hdr
*)buf
;
1418 ni
= (struct icmp6_nodeinfo
*)buf
;
1421 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1422 warnx("failed to get receiving hop limit");
1425 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1426 warnx("failed to get receiving packet information");
1430 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1431 seq
= ntohs(icp
->icmp6_seq
);
1434 tpp
= (struct tv32
*)(icp
+ 1);
1435 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1436 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1438 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1439 ((double)tv
.tv_usec
) / 1000.0;
1441 tsumsq
+= triptime
* triptime
;
1442 if (triptime
< tmin
)
1444 if (triptime
> tmax
)
1448 if (TST(seq
% mx_dup_ck
)) {
1453 SET(seq
% mx_dup_ck
);
1457 if (options
& F_QUIET
)
1460 if (options
& F_FLOOD
)
1461 write(STDOUT_FILENO
, &BSPACE
, 1);
1463 printf("%d bytes from %s, icmp_seq=%u", cc
,
1464 pr_addr(from
, fromlen
), seq
);
1465 printf(" hlim=%d", hoplim
);
1466 if ((options
& F_VERBOSE
) != 0) {
1467 struct sockaddr_in6 dstsa
;
1469 memset(&dstsa
, 0, sizeof(dstsa
));
1470 dstsa
.sin6_family
= AF_INET6
;
1471 dstsa
.sin6_len
= sizeof(dstsa
);
1472 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1473 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1475 pr_addr((struct sockaddr
*)&dstsa
,
1479 printf(" time=%.3f ms", triptime
);
1482 /* check the data */
1483 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1484 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1485 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1487 printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1492 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1493 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1495 if (TST(seq
% mx_dup_ck
)) {
1500 SET(seq
% mx_dup_ck
);
1504 if (options
& F_QUIET
)
1507 printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1509 switch (ntohs(ni
->ni_code
)) {
1510 case ICMP6_NI_SUCCESS
:
1512 case ICMP6_NI_REFUSED
:
1513 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1515 case ICMP6_NI_UNKNOWN
:
1516 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1519 printf("unknown code 0x%x, type 0x%x",
1520 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1524 switch (ntohs(ni
->ni_qtype
)) {
1526 printf("NodeInfo NOOP");
1528 case NI_QTYPE_SUPTYPES
:
1529 pr_suptypes(ni
, end
- (u_char
*)ni
);
1531 case NI_QTYPE_NODEADDR
:
1532 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1535 default: /* XXX: for backward compatibility */
1536 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1537 if (buf
[off
+ ICMP6_NIRLEN
] ==
1538 cc
- off
- ICMP6_NIRLEN
- 1)
1543 cp
++; /* skip length */
1545 safeputc(*cp
& 0xff);
1551 if (dnsdecode((const u_char
**)&cp
, end
,
1552 (const u_char
*)(ni
+ 1), dnsname
,
1553 sizeof(dnsname
)) == NULL
) {
1558 * name-lookup special handling for
1561 if (cp
+ 1 <= end
&& !*cp
&&
1562 strlen(dnsname
) > 0) {
1563 dnsname
[strlen(dnsname
) - 1] = '\0';
1566 printf("%s%s", i
> 0 ? "," : "",
1570 if (options
& F_VERBOSE
) {
1576 switch (ni
->ni_code
) {
1577 case ICMP6_NI_REFUSED
:
1581 case ICMP6_NI_UNKNOWN
:
1582 printf("unknown qtype");
1587 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1588 /* case of refusion, unknown */
1593 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1596 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1597 printf("TTL=%d:meaningless",
1601 printf("TTL=%d:invalid", ttl
);
1603 printf("TTL=%d", ttl
);
1613 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1622 if (buf
[off
+ ICMP6_NIRLEN
] !=
1623 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1626 printf("invalid namelen:%d/%lu",
1627 buf
[off
+ ICMP6_NIRLEN
],
1628 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1638 /* We've got something other than an ECHOREPLY */
1639 if (!(options
& F_VERBOSE
))
1641 printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1645 if (!(options
& F_FLOOD
)) {
1647 if (options
& F_VERBOSE
)
1655 pr_exthdrs(struct msghdr
*mhdr
)
1662 bufp
= mhdr
->msg_control
;
1663 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1664 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1665 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1668 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1671 switch (cm
->cmsg_type
) {
1673 printf(" HbH Options: ");
1674 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1677 #ifdef IPV6_RTHDRDSTOPTS
1678 case IPV6_RTHDRDSTOPTS
:
1680 printf(" Dst Options: ");
1681 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1684 printf(" Routing: ");
1685 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1693 pr_ip6opt(void *extbuf
, size_t bufsize
)
1695 struct ip6_hbh
*ext
;
1698 socklen_t extlen
, len
, origextlen
;
1704 ext
= (struct ip6_hbh
*)extbuf
;
1705 extlen
= (ext
->ip6h_len
+ 1) * 8;
1706 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1707 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1710 * Bounds checking on the ancillary data buffer:
1711 * subtract the size of a cmsg structure from the buffer size.
1713 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
1714 origextlen
= extlen
;
1715 extlen
= bufsize
- CMSG_SPACE(0);
1716 warnx("options truncated, showing only %u (total=%u)",
1717 (unsigned int)(extlen
/ 8 - 1),
1718 (unsigned int)(ext
->ip6h_len
));
1723 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1724 &type
, &len
, &databuf
);
1725 if (currentlen
== -1)
1729 * Note that inet6_opt_next automatically skips any padding
1734 offset
= inet6_opt_get_val(databuf
, offset
,
1735 &value4
, sizeof(value4
));
1736 printf(" Jumbo Payload Opt: Length %u\n",
1737 (u_int32_t
)ntohl(value4
));
1739 /* FIXME: RFC3542 option is IP6OPT_ROUTER_ALERT. */
1740 case IP6OPT_RTALERT
:
1742 offset
= inet6_opt_get_val(databuf
, offset
,
1743 &value2
, sizeof(value2
));
1744 printf(" Router Alert Opt: Type %u\n",
1748 printf(" Received Opt %u len %lu\n",
1749 type
, (unsigned long)len
);
1755 #else /* !USE_RFC3542 */
1758 pr_ip6opt(void *extbuf __unused
, size_t bufsize __unused
)
1763 #endif /* USE_RFC3542 */
1767 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
1769 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1771 /* print fixed part of the header */
1772 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1773 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1774 printf("segments unknown, ");
1775 printf("%d left\n", rh
->ip6r_segleft
);
1780 #else /* !USE_RFC3542 */
1783 pr_rthdr(void *extbuf __unused
)
1788 #endif /* USE_RFC3542 */
1791 pr_bitrange(u_int32_t v
, int soff
, int ii
)
1798 /* shift till we have 0x01 */
1799 if ((v
& 0x01) == 0) {
1801 printf("-%u", soff
+ off
- 1);
1812 case 0x04: case 0x0c:
1823 /* we have 0x01 with us */
1824 for (i
= 0; i
< 32 - off
; i
++) {
1825 if ((v
& (0x01 << i
)) == 0)
1829 printf(" %u", soff
+ off
);
1836 /* struct icmp6_nodeinfo *ni: ni->qtype must be SUPTYPES */
1838 pr_suptypes(struct icmp6_nodeinfo
*ni
, size_t nilen
)
1842 const u_char
*cp
, *end
;
1845 u_int16_t words
; /*32bit count*/
1848 #define MAXQTYPES (1 << 16)
1852 cp
= (u_char
*)(ni
+ 1);
1853 end
= ((u_char
*)ni
) + nilen
;
1857 printf("NodeInfo Supported Qtypes");
1858 if (options
& F_VERBOSE
) {
1859 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1860 printf(", compressed bitmap");
1862 printf(", raw bitmap");
1866 clen
= (size_t)(end
- cp
);
1867 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1868 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1874 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1876 memcpy(&cbit
, cp
, sizeof(cbit
));
1877 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1881 clen
= ntohs(cbit
.words
) * sizeof(v
);
1882 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1887 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1888 memcpy(&v
, cp
+ off
, sizeof(v
));
1889 v
= (u_int32_t
)ntohl(v
);
1890 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1892 /* flush the remaining bits */
1893 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1897 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1898 cur
+= ntohs(cbit
.skip
) * 32;
1902 /* struct icmp6_nodeinfo *ni: ni->qtype must be NODEADDR */
1904 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, int nilen
)
1906 u_char
*cp
= (u_char
*)(ni
+ 1);
1907 char ntop_buf
[INET6_ADDRSTRLEN
];
1910 nilen
-= sizeof(struct icmp6_nodeinfo
);
1912 if (options
& F_VERBOSE
) {
1913 switch (ni
->ni_code
) {
1914 case ICMP6_NI_REFUSED
:
1917 case ICMP6_NI_UNKNOWN
:
1918 printf("unknown qtype");
1921 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1922 printf(" truncated");
1926 printf(" no address\n");
1929 * In icmp-name-lookups 05 and later, TTL of each returned address
1930 * is contained in the resposne. We try to detect the version
1931 * by the length of the data, but note that the detection algorithm
1932 * is incomplete. We assume the latest draft by default.
1934 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1940 /* XXX: alignment? */
1941 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1942 cp
+= sizeof(u_int32_t
);
1943 nilen
-= sizeof(u_int32_t
);
1946 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1948 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
1949 printf(" %s", ntop_buf
);
1951 if (ttl
== 0xffffffff) {
1953 * XXX: can this convention be applied to all
1954 * type of TTL (i.e. non-ND TTL)?
1956 printf("(TTL=infty)");
1959 printf("(TTL=%u)", ttl
);
1963 nilen
-= sizeof(struct in6_addr
);
1964 cp
+= sizeof(struct in6_addr
);
1969 get_hoplim(struct msghdr
*mhdr
)
1973 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1974 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1975 if (cm
->cmsg_len
== 0)
1978 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1979 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1980 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1981 return(*(int *)CMSG_DATA(cm
));
1987 struct in6_pktinfo
*
1988 get_rcvpktinfo(struct msghdr
*mhdr
)
1992 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1993 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1994 if (cm
->cmsg_len
== 0)
1997 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1998 cm
->cmsg_type
== IPV6_PKTINFO
&&
1999 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2000 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2006 #ifdef IPV6_RECVPATHMTU
2008 get_pathmtu(struct msghdr
*mhdr
)
2011 struct ip6_mtuinfo
*mtuctl
= NULL
;
2013 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2014 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2015 if (cm
->cmsg_len
== 0)
2018 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2019 cm
->cmsg_type
== IPV6_PATHMTU
&&
2020 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2021 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2024 * If the notified destination is different from
2025 * the one we are pinging, just ignore the info.
2026 * We check the scope ID only when both notified value
2027 * and our own value have non-0 values, because we may
2028 * have used the default scope zone ID for sending,
2029 * in which case the scope ID value is 0.
2031 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2033 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2034 dst
.sin6_scope_id
&&
2035 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2036 dst
.sin6_scope_id
)) {
2037 if ((options
& F_VERBOSE
) != 0) {
2038 printf("path MTU for %s is notified. "
2040 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2041 sizeof(mtuctl
->ip6m_addr
)));
2047 * Ignore an invalid MTU. XXX: can we just believe
2050 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2053 /* notification for our destination. return the MTU. */
2054 return((int)mtuctl
->ip6m_mtu
);
2063 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2067 tvsub(struct timeval
*out
, struct timeval
*in
)
2069 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2071 out
->tv_usec
+= 1000000;
2073 out
->tv_sec
-= in
->tv_sec
;
2082 onint(int sig __unused
)
2086 signal(SIGINT
, SIG_DFL
);
2087 kill(getpid(), SIGINT
);
2095 * Print out statistics.
2101 printf("\n--- %s ping6 statistics ---\n", hostname
);
2102 printf("%ld packets transmitted, ", ntransmitted
);
2103 printf("%ld packets received, ", nreceived
);
2105 printf("+%ld duplicates, ", nrepeats
);
2107 if (nreceived
> ntransmitted
)
2108 printf("-- somebody's duplicating packets!");
2110 printf("%.1f%% packet loss",
2111 ((((double)ntransmitted
- nreceived
) * 100.0) /
2115 if (nreceived
&& timing
) {
2116 /* Only display average to microseconds */
2117 double num
= nreceived
+ nrepeats
;
2118 double avg
= tsum
/ num
;
2119 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2121 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2122 tmin
, avg
, tmax
, dev
);
2129 static const char *niqcode
[] = {
2131 "DNS label", /*or empty*/
2136 static const char *nircode
[] = {
2137 "Success", "Refused", "Unknown",
2143 * Print a descriptive string about an ICMP header.
2146 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2148 char ntop_buf
[INET6_ADDRSTRLEN
];
2149 struct nd_redirect
*red
;
2150 struct icmp6_nodeinfo
*ni
;
2151 char dnsname
[MAXDNAME
+ 1];
2155 switch (icp
->icmp6_type
) {
2156 case ICMP6_DST_UNREACH
:
2157 switch (icp
->icmp6_code
) {
2158 case ICMP6_DST_UNREACH_NOROUTE
:
2159 printf("No Route to Destination\n");
2161 case ICMP6_DST_UNREACH_ADMIN
:
2162 printf("Destination Administratively "
2165 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2166 printf("Destination Unreachable Beyond Scope\n");
2168 case ICMP6_DST_UNREACH_ADDR
:
2169 printf("Destination Host Unreachable\n");
2171 case ICMP6_DST_UNREACH_NOPORT
:
2172 printf("Destination Port Unreachable\n");
2175 printf("Destination Unreachable, Bad Code: %d\n",
2179 /* Print returned IP header information */
2180 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2182 case ICMP6_PACKET_TOO_BIG
:
2183 printf("Packet too big mtu = %d\n",
2184 (int)ntohl(icp
->icmp6_mtu
));
2185 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2187 case ICMP6_TIME_EXCEEDED
:
2188 switch (icp
->icmp6_code
) {
2189 case ICMP6_TIME_EXCEED_TRANSIT
:
2190 printf("Time to live exceeded\n");
2192 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2193 printf("Frag reassembly time exceeded\n");
2196 printf("Time exceeded, Bad Code: %d\n",
2200 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2202 case ICMP6_PARAM_PROB
:
2203 printf("Parameter problem: ");
2204 switch (icp
->icmp6_code
) {
2205 case ICMP6_PARAMPROB_HEADER
:
2206 printf("Erroneous Header ");
2208 case ICMP6_PARAMPROB_NEXTHEADER
:
2209 printf("Unknown Nextheader ");
2211 case ICMP6_PARAMPROB_OPTION
:
2212 printf("Unrecognized Option ");
2215 printf("Bad code(%d) ", icp
->icmp6_code
);
2218 printf("pointer = 0x%02x\n", (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2219 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2221 case ICMP6_ECHO_REQUEST
:
2222 printf("Echo Request");
2223 /* XXX ID + Seq + Data */
2225 case ICMP6_ECHO_REPLY
:
2226 printf("Echo Reply");
2227 /* XXX ID + Seq + Data */
2229 case ICMP6_MEMBERSHIP_QUERY
:
2230 printf("Listener Query");
2232 case ICMP6_MEMBERSHIP_REPORT
:
2233 printf("Listener Report");
2235 case ICMP6_MEMBERSHIP_REDUCTION
:
2236 printf("Listener Done");
2238 case ND_ROUTER_SOLICIT
:
2239 printf("Router Solicitation");
2241 case ND_ROUTER_ADVERT
:
2242 printf("Router Advertisement");
2244 case ND_NEIGHBOR_SOLICIT
:
2245 printf("Neighbor Solicitation");
2247 case ND_NEIGHBOR_ADVERT
:
2248 printf("Neighbor Advertisement");
2251 red
= (struct nd_redirect
*)icp
;
2252 printf("Redirect\n");
2253 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2255 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2256 printf("Destination: %s", ntop_buf
);
2257 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2259 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2260 printf(" New Target: %s", ntop_buf
);
2262 case ICMP6_NI_QUERY
:
2263 printf("Node Information Query");
2264 /* XXX ID + Seq + Data */
2265 ni
= (struct icmp6_nodeinfo
*)icp
;
2266 l
= end
- (u_char
*)(ni
+ 1);
2268 switch (ntohs(ni
->ni_qtype
)) {
2272 case NI_QTYPE_SUPTYPES
:
2273 printf("Supported qtypes");
2278 case NI_QTYPE_NODEADDR
:
2281 case NI_QTYPE_IPV4ADDR
:
2282 printf("IPv4 nodeaddr");
2285 printf("unknown qtype");
2288 if (options
& F_VERBOSE
) {
2289 switch (ni
->ni_code
) {
2290 case ICMP6_NI_SUBJ_IPV6
:
2291 if (l
== sizeof(struct in6_addr
) &&
2292 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2293 sizeof(ntop_buf
)) != NULL
) {
2294 printf(", subject=%s(%s)",
2295 niqcode
[ni
->ni_code
], ntop_buf
);
2298 /* backward compat to -W */
2299 printf(", oldfqdn");
2301 printf(", invalid");
2305 case ICMP6_NI_SUBJ_FQDN
:
2306 if (end
== (u_char
*)(ni
+ 1)) {
2307 printf(", no subject");
2310 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2311 cp
= (const u_char
*)(ni
+ 1);
2312 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2313 sizeof(dnsname
)) != NULL
)
2314 printf("(%s)", dnsname
);
2316 printf("(invalid)");
2318 case ICMP6_NI_SUBJ_IPV4
:
2319 if (l
== sizeof(struct in_addr
) &&
2320 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2321 sizeof(ntop_buf
)) != NULL
) {
2322 printf(", subject=%s(%s)",
2323 niqcode
[ni
->ni_code
], ntop_buf
);
2325 printf(", invalid");
2328 printf(", invalid");
2333 case ICMP6_NI_REPLY
:
2334 printf("Node Information Reply");
2335 /* XXX ID + Seq + Data */
2336 ni
= (struct icmp6_nodeinfo
*)icp
;
2338 switch (ntohs(ni
->ni_qtype
)) {
2342 case NI_QTYPE_SUPTYPES
:
2343 printf("Supported qtypes");
2348 case NI_QTYPE_NODEADDR
:
2351 case NI_QTYPE_IPV4ADDR
:
2352 printf("IPv4 nodeaddr");
2355 printf("unknown qtype");
2358 if (options
& F_VERBOSE
) {
2359 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2360 printf(", invalid");
2362 printf(", %s", nircode
[ni
->ni_code
]);
2366 printf("Bad ICMP type: %d", icp
->icmp6_type
);
2372 * Print an IP6 header.
2375 pr_iph(struct ip6_hdr
*ip6
)
2377 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2379 char ntop_buf
[INET6_ADDRSTRLEN
];
2381 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2382 tc
= (tc
>> 4) & 0x0f;
2383 tc
|= (ip6
->ip6_vfc
<< 4);
2385 printf("Vr TC Flow Plen Nxt Hlim\n");
2386 printf(" %1x %02x %05x %04x %02x %02x\n",
2387 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2388 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2389 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2390 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2391 printf("%s->", ntop_buf
);
2392 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2393 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2394 printf("%s\n", ntop_buf
);
2399 * Return an ascii host address as a dotted quad and optionally with
2403 pr_addr(struct sockaddr
*addr
, int addrlen
)
2405 static char buf
[NI_MAXHOST
];
2408 if ((options
& F_HOSTNAME
) == 0)
2409 flag
|= NI_NUMERICHOST
;
2411 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2419 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2422 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2424 u_char
*cp
= (u_char
*)ip6
, nh
;
2427 if (end
- (u_char
*)ip6
< (int)sizeof(*ip6
)) {
2432 hlen
= sizeof(*ip6
);
2436 while (end
- cp
>= 8) {
2438 case IPPROTO_HOPOPTS
:
2440 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2441 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2443 case IPPROTO_DSTOPTS
:
2445 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2446 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2448 case IPPROTO_FRAGMENT
:
2450 hlen
= sizeof(struct ip6_frag
);
2451 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2453 case IPPROTO_ROUTING
:
2455 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2456 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2461 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2462 nh
= ((struct ah
*)cp
)->ah_nxt
;
2465 case IPPROTO_ICMPV6
:
2466 printf("ICMP6: type = %d, code = %d\n",
2473 printf("TCP: from port %u, to port %u (decimal)\n",
2474 (*cp
* 256 + *(cp
+ 1)),
2475 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2478 printf("UDP: from port %u, to port %u (decimal)\n",
2479 (*cp
* 256 + *(cp
+ 1)),
2480 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2483 printf("Unknown Header(%d)\n", nh
);
2487 if ((cp
+= hlen
) >= end
)
2502 fill(char *bp
, char *patp
)
2508 for (cp
= patp
; *cp
; cp
++)
2510 errx(1, "patterns must be specified as hex digits");
2512 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2513 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2514 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2515 &pat
[13], &pat
[14], &pat
[15]);
2520 kk
<= MAXDATALEN
- (8 + (int)sizeof(struct tv32
) + ii
);
2522 for (jj
= 0; jj
< ii
; ++jj
)
2523 bp
[jj
+ kk
] = pat
[jj
];
2524 if (!(options
& F_QUIET
)) {
2525 printf("PATTERN: 0x");
2526 for (jj
= 0; jj
< ii
; ++jj
)
2527 printf("%02x", bp
[jj
] & 0xFF);
2533 #ifdef IPSEC_POLICY_IPSEC
2535 setpolicy(int so __unused
, char *policy
)
2540 return 0; /* ignore */
2542 buf
= ipsec_set_policy(policy
, strlen(policy
));
2544 errx(1, "%s", ipsec_strerror());
2545 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2546 ipsec_get_policylen(buf
)) < 0)
2547 warnx("Unable to set IPsec policy");
2561 u_int8_t digest
[16];
2564 char hbuf
[NI_MAXHOST
];
2565 struct in6_addr in6
;
2567 p
= strchr(name
, '.');
2569 p
= name
+ strlen(name
);
2571 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2572 return NULL
; /*label too long*/
2573 strncpy(hbuf
, name
, l
);
2574 hbuf
[(int)l
] = '\0';
2576 for (q
= name
; *q
; q
++) {
2577 if (isupper(*(unsigned char *)q
))
2578 *q
= tolower(*(unsigned char *)q
);
2581 /* generate 8 bytes of pseudo-random value. */
2582 memset(&ctxt
, 0, sizeof(ctxt
));
2585 MD5Update(&ctxt
, &c
, sizeof(c
));
2586 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2587 MD5Final(digest
, &ctxt
);
2589 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2590 return NULL
; /*XXX*/
2591 bcopy(digest
, &in6
.s6_addr
[12], 4);
2593 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2596 return strdup(hbuf
);
2603 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2608 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2612 #ifdef IPV6_USE_MIN_MTU
2616 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2617 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2618 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2622 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2623 "[hops ...] host\n");