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 $
36 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
39 * Copyright (c) 1989, 1993
40 * The Regents of the University of California. All rights reserved.
42 * This code is derived from software contributed to Berkeley by
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. 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 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
72 * measure round-trip-delays and packet loss across network paths.
76 * U. S. Army Ballistic Research Laboratory
80 * Public Domain. Distribution Unlimited.
82 * More statistics could always be gathered.
83 * This program has to run SUID to ROOT to access the ICMP socket.
87 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
88 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
89 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
90 * network attached to 1 or more interfaces)
93 #include <sys/param.h>
95 #include <sys/socket.h>
99 #include <net/route.h>
101 #include <netinet/in.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <arpa/inet.h>
105 #include <arpa/nameser.h>
123 #include <netinet6/ah.h>
124 #include <netinet6/ipsec.h>
134 #define MAXPACKETLEN 131072
136 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
137 #define ICMP6ECHOTMLEN sizeof(struct tv32)
138 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
139 #define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
140 /* FQDN case, 64 bits of nonce + 32 bits ttl */
141 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
142 #define EXTRA 256 /* for AH and various other headers. weird. */
143 #define DEFDATALEN ICMP6ECHOTMLEN
144 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
145 #define NROUTES 9 /* number of record route slots */
147 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
148 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
149 #define SET(bit) (A(bit) |= B(bit))
150 #define CLR(bit) (A(bit) &= (~B(bit)))
151 #define TST(bit) (A(bit) & B(bit))
153 #define F_FLOOD 0x0001
154 #define F_INTERVAL 0x0002
155 #define F_PINGFILLED 0x0008
156 #define F_QUIET 0x0010
157 #define F_RROUTE 0x0020
158 #define F_SO_DEBUG 0x0040
159 #define F_VERBOSE 0x0100
161 #ifdef IPSEC_POLICY_IPSEC
162 #define F_POLICY 0x0400
164 #define F_AUTHHDR 0x0200
165 #define F_ENCRYPT 0x0400
166 #endif /*IPSEC_POLICY_IPSEC*/
168 #define F_NODEADDR 0x0800
169 #define F_FQDN 0x1000
170 #define F_INTERFACE 0x2000
171 #define F_SRCADDR 0x4000
172 #define F_HOSTNAME 0x10000
173 #define F_FQDNOLD 0x20000
174 #define F_NIGROUP 0x40000
175 #define F_SUPTYPES 0x80000
176 #define F_NOMINMTU 0x100000
177 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
180 #define IN6LEN sizeof(struct in6_addr)
181 #define SA6LEN sizeof(struct sockaddr_in6)
182 #define DUMMY_PORT 10101
184 #define SIN6(s) ((struct sockaddr_in6 *)(s))
187 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
188 * number of received sequence numbers we can keep track of. Change 128
189 * to 8192 for complete accuracy...
191 #define MAX_DUP_CHK (8 * 8192)
192 int mx_dup_ck
= MAX_DUP_CHK
;
193 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
195 struct addrinfo
*res
;
196 struct sockaddr_in6 dst
; /* who to ping6 */
197 struct sockaddr_in6 src
; /* src addr of this packet */
199 int datalen
= DEFDATALEN
;
200 int s
; /* socket file descriptor */
201 u_char outpack
[MAXPACKETLEN
];
202 char BSPACE
= '\b'; /* characters written for flood */
205 int ident
; /* process id to identify our packets */
206 u_int8_t nonce
[8]; /* nonce field for node information */
207 int hoplimit
= -1; /* hoplimit */
208 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
211 long npackets
; /* max packets to transmit */
212 long nreceived
; /* # of packets we got back */
213 long nrepeats
; /* number of duplicates */
214 long ntransmitted
; /* sequence # for outbound packets = #sent */
215 struct timeval interval
= {1, 0}; /* interval between packets */
218 int timing
; /* flag to do timing */
219 double tmin
= 999999999.0; /* minimum round trip time */
220 double tmax
= 0.0; /* maximum round trip time */
221 double tsum
= 0.0; /* sum of all times, for doing average */
222 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
224 /* for node addresses */
227 /* for ancillary data(advanced API) */
228 struct msghdr smsghdr
;
229 struct iovec smsgiov
;
232 volatile sig_atomic_t seenalrm
;
233 volatile sig_atomic_t seenint
;
235 volatile sig_atomic_t seeninfo
;
238 void fill(char *, char *);
239 int get_hoplim(struct msghdr
*);
240 #ifdef IPV6_RECVPATHMTU
241 int get_pathmtu(struct msghdr
*);
243 #define get_pathmtu(mhdr) (0)
245 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
247 void retransmit(void);
248 void onint(int) __dead2
;
249 size_t pingerlen(void);
251 const char *pr_addr(struct sockaddr
*, int);
252 void pr_icmph(struct icmp6_hdr
*, u_char
*);
253 void pr_iph(struct ip6_hdr
*);
254 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
255 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
256 int myechoreply(const struct icmp6_hdr
*);
257 int mynireply(const struct icmp6_nodeinfo
*);
258 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
260 void pr_pack(u_char
*, int, struct msghdr
*);
261 void pr_exthdrs(struct msghdr
*);
262 void pr_ip6opt(void *, size_t);
263 void pr_rthdr(void *, size_t);
264 int pr_bitrange(u_int32_t
, int, int);
265 void pr_retip(struct ip6_hdr
*, u_char
*);
267 void tvsub(struct timeval
*, struct timeval
*);
268 int setpolicy(int, char *);
269 char *nigroup(char *);
270 void usage(void) __dead2
;
273 main(int argc
, char **argv
)
275 struct itimerval itimer
;
276 struct sockaddr_in6 from
;
277 #ifndef HAVE_ARC4RANDOM
283 struct timeval timeout
, *tv
;
285 struct addrinfo hints
;
287 struct pollfd fdmaskp
[1];
293 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
294 u_char
*datap
, *packet
;
295 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
297 struct cmsghdr
*scmsgp
= NULL
;
299 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
304 struct in6_pktinfo
*pktinfo
= NULL
;
306 struct ip6_rthdr
*rthdr
= NULL
;
308 #ifdef IPSEC_POLICY_IPSEC
309 char *policy_in
= NULL
;
310 char *policy_out
= NULL
;
312 #ifdef IPV6_USE_MIN_MTU
317 /* just to be sure */
318 memset(&smsghdr
, 0, sizeof(smsghdr
));
319 memset(&smsgiov
, 0, sizeof(smsgiov
));
322 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
326 #ifdef IPSEC_POLICY_IPSEC
330 #endif /*IPSEC_POLICY_IPSEC*/
332 while ((ch
= getopt(argc
, argv
,
333 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS
)) != -1) {
340 options
&= ~F_NOUSERDATA
;
341 options
|= F_NODEADDR
;
342 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
345 naflags
|= NI_NODEADDR_FLAG_ALL
;
349 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
353 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
357 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
361 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
363 case 'A': /* experimental. not in the spec */
364 #ifdef NI_NODEADDR_FLAG_ANYCAST
365 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
369 "-a A is not supported on the platform");
380 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
383 lsockbufsize
= strtoul(optarg
, &e
, 10);
384 sockbufsize
= lsockbufsize
;
385 if (errno
|| !*optarg
|| *e
||
386 sockbufsize
!= (long)lsockbufsize
)
387 errx(1, "invalid socket buffer size");
390 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
394 npackets
= strtol(optarg
, &e
, 10);
395 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
397 "illegal number of packets -- %s", optarg
);
400 options
|= F_SO_DEBUG
;
405 errx(1, "Must be superuser to flood ping");
408 setbuf(stdout
, NULL
);
414 options
|= F_HOSTNAME
;
416 case 'h': /* hoplimit */
417 hoplimit
= strtol(optarg
, &e
, 10);
418 if (*optarg
== '\0' || *e
!= '\0')
419 errx(1, "illegal hoplimit %s", optarg
);
420 if (255 < hoplimit
|| hoplimit
< -1)
422 "illegal hoplimit -- %s", optarg
);
426 options
|= F_INTERFACE
;
427 #ifndef USE_SIN6_SCOPE_ID
431 case 'i': /* wait between sending packets */
432 intval
= strtod(optarg
, &e
);
433 if (*optarg
== '\0' || *e
!= '\0')
434 errx(1, "illegal timing interval %s", optarg
);
435 if (intval
< 1 && getuid()) {
436 errx(1, "%s: only root may use interval < 1s",
439 interval
.tv_sec
= (long)intval
;
441 (long)((intval
- interval
.tv_sec
) * 1000000);
442 if (interval
.tv_sec
< 0)
443 errx(1, "illegal timing interval %s", optarg
);
444 /* less than 1/hz does not make sense */
445 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 10000) {
446 warnx("too small interval, raised to 0.01");
447 interval
.tv_usec
= 10000;
449 options
|= F_INTERVAL
;
454 errx(1, "Must be superuser to preload");
456 preload
= strtol(optarg
, &e
, 10);
457 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
458 errx(1, "illegal preload value -- %s", optarg
);
461 #ifdef IPV6_USE_MIN_MTU
465 errx(1, "-%c is not supported on this platform", ch
);
469 options
&= ~F_HOSTNAME
;
472 options
|= F_NIGROUP
;
474 case 'p': /* fill buffer with user pattern */
475 options
|= F_PINGFILLED
;
476 fill((char *)datap
, optarg
);
482 memset(&hints
, 0, sizeof(struct addrinfo
));
483 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
484 hints
.ai_family
= AF_INET6
;
485 hints
.ai_socktype
= SOCK_RAW
;
486 hints
.ai_protocol
= IPPROTO_ICMPV6
;
488 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
490 errx(1, "invalid source address: %s",
491 gai_strerror(ret_ga
));
494 * res->ai_family must be AF_INET6 and res->ai_addrlen
495 * must be sizeof(src).
497 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
498 srclen
= res
->ai_addrlen
;
500 options
|= F_SRCADDR
;
502 case 's': /* size of packet to send */
503 datalen
= strtol(optarg
, &e
, 10);
504 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
505 errx(1, "illegal datalen value -- %s", optarg
);
506 if (datalen
> MAXDATALEN
) {
508 "datalen value too large, maximum is %d",
513 options
&= ~F_NOUSERDATA
;
514 options
|= F_SUPTYPES
;
517 options
|= F_VERBOSE
;
520 options
&= ~F_NOUSERDATA
;
524 options
&= ~F_NOUSERDATA
;
525 options
|= F_FQDNOLD
;
528 #ifdef IPSEC_POLICY_IPSEC
531 if (!strncmp("in", optarg
, 2)) {
532 if ((policy_in
= strdup(optarg
)) == NULL
)
534 } else if (!strncmp("out", optarg
, 3)) {
535 if ((policy_out
= strdup(optarg
)) == NULL
)
538 errx(1, "invalid security policy");
542 options
|= F_AUTHHDR
;
545 options
|= F_ENCRYPT
;
547 #endif /*IPSEC_POLICY_IPSEC*/
563 errx(1, "too many arguments");
567 if (options
& F_NIGROUP
) {
568 target
= nigroup(argv
[argc
- 1]);
569 if (target
== NULL
) {
574 target
= argv
[argc
- 1];
577 memset(&hints
, 0, sizeof(struct addrinfo
));
578 hints
.ai_flags
= AI_CANONNAME
;
579 hints
.ai_family
= AF_INET6
;
580 hints
.ai_socktype
= SOCK_RAW
;
581 hints
.ai_protocol
= IPPROTO_ICMPV6
;
583 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
585 errx(1, "%s", gai_strerror(ret_ga
));
586 if (res
->ai_canonname
)
587 hostname
= res
->ai_canonname
;
592 errx(1, "getaddrinfo failed");
594 memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
596 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
597 res
->ai_protocol
)) < 0)
600 /* set the source address if specified. */
601 if ((options
& F_SRCADDR
) &&
602 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
606 /* set the gateway (next hop) if specified */
608 struct addrinfo ghints
, *gres
;
611 memset(&ghints
, 0, sizeof(ghints
));
612 ghints
.ai_family
= AF_INET6
;
613 ghints
.ai_socktype
= SOCK_RAW
;
614 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
616 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
618 errx(1, "getaddrinfo for the gateway %s: %s",
619 gateway
, gai_strerror(error
));
621 if (gres
->ai_next
&& (options
& F_VERBOSE
))
622 warnx("gateway resolves to multiple addresses");
624 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
625 gres
->ai_addr
, gres
->ai_addrlen
)) {
626 err(1, "setsockopt(IPV6_NEXTHOP)");
633 * let the kerel pass extension headers of incoming packets,
634 * for privileged socket options
636 if ((options
& F_VERBOSE
) != 0) {
639 #ifdef IPV6_RECVHOPOPTS
640 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
642 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
643 #else /* old adv. API */
644 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
646 err(1, "setsockopt(IPV6_HOPOPTS)");
648 #ifdef IPV6_RECVDSTOPTS
649 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
651 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
652 #else /* old adv. API */
653 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
655 err(1, "setsockopt(IPV6_DSTOPTS)");
657 #ifdef IPV6_RECVRTHDRDSTOPTS
658 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
660 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
664 /* revoke root privilege */
668 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
669 errx(1, "-f and -i incompatible options");
671 if ((options
& F_NOUSERDATA
) == 0) {
672 if (datalen
>= (int)sizeof(struct tv32
)) {
673 /* we can time transfer */
677 /* in F_VERBOSE case, we may get non-echoreply packets*/
678 if (options
& F_VERBOSE
)
679 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
681 packlen
= datalen
+ IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
683 /* suppress timing for node information query */
686 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
689 if (!(packet
= (u_char
*)malloc((u_int
)packlen
)))
690 err(1, "Unable to allocate packet");
691 if (!(options
& F_PINGFILLED
))
692 for (i
= ICMP6ECHOLEN
; i
< packlen
; ++i
)
695 ident
= getpid() & 0xFFFF;
696 #ifndef HAVE_ARC4RANDOM
697 gettimeofday(&seed
, NULL
);
698 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
699 memset(nonce
, 0, sizeof(nonce
));
700 for (i
= 0; i
< (int)sizeof(nonce
); i
+= sizeof(int))
701 *((int *)&nonce
[i
]) = rand();
703 memset(nonce
, 0, sizeof(nonce
));
704 for (i
= 0; i
< (int)sizeof(nonce
); i
+= sizeof(u_int32_t
))
705 *((u_int32_t
*)&nonce
[i
]) = arc4random();
710 if (options
& F_SO_DEBUG
)
711 setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
713 optval
= IPV6_DEFHLIM
;
714 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
715 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
716 &optval
, sizeof(optval
)) == -1)
717 err(1, "IPV6_MULTICAST_HOPS");
718 #ifdef IPV6_USE_MIN_MTU
720 optval
= mflag
> 1 ? 0 : 1;
721 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
722 &optval
, sizeof(optval
)) == -1)
723 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
725 #ifdef IPV6_RECVPATHMTU
728 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
729 &optval
, sizeof(optval
)) == -1)
730 err(1, "setsockopt(IPV6_RECVPATHMTU)");
732 #endif /* IPV6_RECVPATHMTU */
733 #endif /* IPV6_USE_MIN_MTU */
736 #ifdef IPSEC_POLICY_IPSEC
737 if (options
& F_POLICY
) {
738 if (setpolicy(s
, policy_in
) < 0)
739 errx(1, "%s", ipsec_strerror());
740 if (setpolicy(s
, policy_out
) < 0)
741 errx(1, "%s", ipsec_strerror());
744 if (options
& F_AUTHHDR
) {
745 optval
= IPSEC_LEVEL_REQUIRE
;
746 #ifdef IPV6_AUTH_TRANS_LEVEL
747 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
748 &optval
, sizeof(optval
)) == -1)
749 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
751 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
752 &optval
, sizeof(optval
)) == -1)
753 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
756 if (options
& F_ENCRYPT
) {
757 optval
= IPSEC_LEVEL_REQUIRE
;
758 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
759 &optval
, sizeof(optval
)) == -1)
760 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
762 #endif /*IPSEC_POLICY_IPSEC*/
767 struct icmp6_filter filt
;
768 if (!(options
& F_VERBOSE
)) {
769 ICMP6_FILTER_SETBLOCKALL(&filt
);
770 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
771 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
772 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
774 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
776 ICMP6_FILTER_SETPASSALL(&filt
);
778 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
780 err(1, "setsockopt(ICMP6_FILTER)");
782 #endif /*ICMP6_FILTER*/
784 /* let the kerel pass extension headers of incoming packets */
785 if ((options
& F_VERBOSE
) != 0) {
788 #ifdef IPV6_RECVRTHDR
789 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
791 err(1, "setsockopt(IPV6_RECVRTHDR)");
792 #else /* old adv. API */
793 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
795 err(1, "setsockopt(IPV6_RTHDR)");
801 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
802 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
803 &optval, sizeof(optval)) == -1)
804 err(1, "IPV6_MULTICAST_LOOP");
807 /* Specify the outgoing interface and/or the source address */
809 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
812 ip6optlen
+= CMSG_SPACE(sizeof(int));
814 /* set IP6 packet options */
816 if ((scmsg
= (char *)malloc(ip6optlen
)) == NULL
)
817 errx(1, "can't allocate enough memory");
818 smsghdr
.msg_control
= (caddr_t
)scmsg
;
819 smsghdr
.msg_controllen
= ip6optlen
;
820 scmsgp
= (struct cmsghdr
*)scmsg
;
823 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
824 memset(pktinfo
, 0, sizeof(*pktinfo
));
825 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
826 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
827 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
828 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
831 /* set the outgoing interface */
833 #ifndef USE_SIN6_SCOPE_ID
834 /* pktinfo must have already been allocated */
835 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
836 errx(1, "%s: invalid interface name", ifname
);
838 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
839 errx(1, "%s: invalid interface name", ifname
);
842 if (hoplimit
!= -1) {
843 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
844 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
845 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
846 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
848 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
851 if (!(options
& F_SRCADDR
)) {
853 * get the source address. XXX since we revoked the root
854 * privilege, we cannot use a raw socket for this.
857 socklen_t len
= sizeof(src
);
859 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
860 err(1, "UDP socket");
862 src
.sin6_family
= AF_INET6
;
863 src
.sin6_addr
= dst
.sin6_addr
;
864 src
.sin6_port
= ntohs(DUMMY_PORT
);
865 src
.sin6_scope_id
= dst
.sin6_scope_id
;
869 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
870 (void *)pktinfo
, sizeof(*pktinfo
)))
871 err(1, "UDP setsockopt(IPV6_PKTINFO)");
872 if (hoplimit
!= -1 &&
873 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
874 (void *)&hoplimit
, sizeof(hoplimit
)))
875 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
876 if (hoplimit
!= -1 &&
877 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
878 (void *)&hoplimit
, sizeof(hoplimit
)))
879 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
881 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
882 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
883 err(1, "UDP setsockopt(IPV6_RTHDR)");
884 #else /* old advanced API */
885 if (smsghdr
.msg_control
&&
886 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
887 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
888 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
891 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
892 err(1, "UDP connect");
894 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
895 err(1, "getsockname");
900 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
902 if (datalen
> sockbufsize
)
903 warnx("you need -b to increase socket buffer size");
904 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
905 sizeof(sockbufsize
)) < 0)
906 err(1, "setsockopt(SO_SNDBUF)");
907 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
908 sizeof(sockbufsize
)) < 0)
909 err(1, "setsockopt(SO_RCVBUF)");
912 if (datalen
> 8 * 1024) /*XXX*/
913 warnx("you need -b to increase socket buffer size");
915 * When pinging the broadcast address, you can get a lot of
916 * answers. Doing something so evil is useful if you are trying
917 * to stress the ethernet, or just want to fill the arp cache
918 * to get some stuff for /etc/ethers.
921 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
927 #ifndef USE_SIN6_SCOPE_ID
928 #ifdef IPV6_RECVPKTINFO
929 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
931 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
932 #else /* old adv. API */
933 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
935 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
937 #endif /* USE_SIN6_SCOPE_ID */
938 #ifdef IPV6_RECVHOPLIMIT
939 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
941 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
942 #else /* old adv. API */
943 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
945 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
948 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
949 (unsigned long)(pingerlen() - 8));
950 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
951 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
953 while (preload
--) /* Fire off them quickies. */
956 signal(SIGINT
, onsignal
);
958 signal(SIGINFO
, onsignal
);
961 if ((options
& F_FLOOD
) == 0) {
962 signal(SIGALRM
, onsignal
);
963 itimer
.it_interval
= interval
;
964 itimer
.it_value
= interval
;
965 setitimer(ITIMER_REAL
, &itimer
, NULL
);
966 if (ntransmitted
== 0)
971 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
972 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
976 seenalrm
= seenint
= 0;
981 /* For control (ancillary) data received from recvmsg() */
982 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
990 /* signal handling */
1009 if (options
& F_FLOOD
) {
1015 timeout
.tv_usec
= 10000;
1027 fdmaskp
[0].events
= POLLIN
;
1028 cc
= poll(fdmaskp
, 1, timeout
);
1030 memset(fdmaskp
, 0, fdmasks
);
1032 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1035 if (errno
!= EINTR
) {
1047 m
.msg_name
= (caddr_t
)&from
;
1048 m
.msg_namelen
= sizeof(from
);
1049 memset(&iov
, 0, sizeof(iov
));
1050 iov
[0].iov_base
= (caddr_t
)packet
;
1051 iov
[0].iov_len
= packlen
;
1054 memset(cm
, 0, CONTROLLEN
);
1055 m
.msg_control
= (void *)cm
;
1056 m
.msg_controllen
= CONTROLLEN
;
1058 cc
= recvmsg(s
, &m
, 0);
1060 if (errno
!= EINTR
) {
1065 } else if (cc
== 0) {
1069 * receive control messages only. Process the
1070 * exceptions (currently the only possiblity is
1071 * a path MTU notification.)
1073 if ((mtu
= get_pathmtu(&m
)) > 0) {
1074 if ((options
& F_VERBOSE
) != 0) {
1075 printf("new path MTU (%d) is "
1082 * an ICMPv6 message (probably an echoreply) arrived.
1084 pr_pack(packet
, cc
, &m
);
1086 if (npackets
&& nreceived
>= npackets
)
1090 exit(nreceived
== 0);
1113 * This routine transmits another ping6.
1118 struct itimerval itimer
;
1124 * If we're not transmitting any more packets, change the timer
1125 * to wait two round-trip times if we've received any packets or
1126 * ten seconds if we haven't.
1130 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1131 if (itimer
.it_value
.tv_sec
== 0)
1132 itimer
.it_value
.tv_sec
= 1;
1134 itimer
.it_value
.tv_sec
= MAXWAIT
;
1135 itimer
.it_interval
.tv_sec
= 0;
1136 itimer
.it_interval
.tv_usec
= 0;
1137 itimer
.it_value
.tv_usec
= 0;
1139 signal(SIGALRM
, onint
);
1140 setitimer(ITIMER_REAL
, &itimer
, NULL
);
1145 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1146 * will be added on by the kernel. The ID field is our UNIX process ID,
1147 * and the sequence number is an ascending integer. The first 8 bytes
1148 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1149 * byte-order, to compute the round-trip time.
1156 if (options
& F_FQDN
)
1157 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1158 else if (options
& F_FQDNOLD
)
1160 else if (options
& F_NODEADDR
)
1161 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1162 else if (options
& F_SUPTYPES
)
1165 l
= ICMP6ECHOLEN
+ datalen
;
1173 struct icmp6_hdr
*icp
;
1174 struct iovec iov
[2];
1176 struct icmp6_nodeinfo
*nip
;
1179 if (npackets
&& ntransmitted
>= npackets
)
1180 return(-1); /* no more transmission */
1182 icp
= (struct icmp6_hdr
*)outpack
;
1183 nip
= (struct icmp6_nodeinfo
*)outpack
;
1184 memset(icp
, 0, sizeof(*icp
));
1185 icp
->icmp6_cksum
= 0;
1186 seq
= ntransmitted
++;
1187 CLR(seq
% mx_dup_ck
);
1189 if (options
& F_FQDN
) {
1190 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1191 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1192 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1193 nip
->ni_flags
= htons(0);
1195 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1196 sizeof(nip
->icmp6_ni_nonce
));
1197 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1199 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1200 sizeof(dst
.sin6_addr
));
1201 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1203 } else if (options
& F_FQDNOLD
) {
1204 /* packet format in 03 draft - no Subject data on queries */
1205 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1206 icp
->icmp6_code
= 0; /* code field is always 0 */
1207 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1208 nip
->ni_flags
= htons(0);
1210 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1211 sizeof(nip
->icmp6_ni_nonce
));
1212 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1216 } else if (options
& F_NODEADDR
) {
1217 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1218 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1219 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1220 nip
->ni_flags
= naflags
;
1222 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1223 sizeof(nip
->icmp6_ni_nonce
));
1224 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1226 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1227 sizeof(dst
.sin6_addr
));
1228 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1230 } else if (options
& F_SUPTYPES
) {
1231 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1232 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1233 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1234 /* we support compressed bitmap */
1235 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1237 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1238 sizeof(nip
->icmp6_ni_nonce
));
1239 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1243 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1244 icp
->icmp6_code
= 0;
1245 icp
->icmp6_id
= htons(ident
);
1246 icp
->icmp6_seq
= ntohs(seq
);
1250 gettimeofday(&tv
, NULL
);
1251 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1252 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1253 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1255 cc
= ICMP6ECHOLEN
+ datalen
;
1259 if (pingerlen() != cc
)
1260 errx(1, "internal error; length mismatch");
1263 smsghdr
.msg_name
= (caddr_t
)&dst
;
1264 smsghdr
.msg_namelen
= sizeof(dst
);
1265 memset(&iov
, 0, sizeof(iov
));
1266 iov
[0].iov_base
= (caddr_t
)outpack
;
1267 iov
[0].iov_len
= cc
;
1268 smsghdr
.msg_iov
= iov
;
1269 smsghdr
.msg_iovlen
= 1;
1271 i
= sendmsg(s
, &smsghdr
, 0);
1273 if (i
< 0 || i
!= cc
) {
1276 printf("ping6: wrote %s %d chars, ret=%d\n", hostname
, cc
, i
);
1278 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1279 write(STDOUT_FILENO
, &DOT
, 1);
1285 myechoreply(const struct icmp6_hdr
*icp
)
1287 if (ntohs(icp
->icmp6_id
) == ident
)
1294 mynireply(const struct icmp6_nodeinfo
*nip
)
1296 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1297 nonce
+ sizeof(u_int16_t
),
1298 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1305 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1310 char cresult
[MAXDNAME
+ 1];
1321 if (i
== 0 || cp
!= *sp
) {
1322 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1323 return NULL
; /*result overrun*/
1329 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1330 /* DNS compression */
1334 comp
= base
+ (i
& 0x3f);
1335 if (dnsdecode(&comp
, cp
, base
, cresult
,
1336 sizeof(cresult
)) == NULL
)
1338 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1339 return NULL
; /*result overrun*/
1341 } else if ((i
& 0x3f) == i
) {
1343 return NULL
; /*source overrun*/
1344 while (i
-- > 0 && cp
< ep
) {
1345 l
= snprintf(cresult
, sizeof(cresult
),
1346 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1347 if (l
>= (int)sizeof(cresult
) || l
< 0)
1349 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1350 return NULL
; /*result overrun*/
1354 return NULL
; /*invalid label*/
1357 return NULL
; /*not terminated*/
1365 * Print out the packet, if it came from us. This logic is necessary
1366 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1367 * which arrive ('tis only fair). This permits multiple copies of this
1368 * program to be run without having intermingled output (or statistics!).
1371 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1373 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1374 struct icmp6_hdr
*icp
;
1375 struct icmp6_nodeinfo
*ni
;
1378 struct sockaddr
*from
;
1380 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1381 struct in6_pktinfo
*pktinfo
= NULL
;
1382 struct timeval tv
, tp
;
1384 double triptime
= 0;
1389 char dnsname
[MAXDNAME
+ 1];
1391 gettimeofday(&tv
, NULL
);
1393 if (!mhdr
|| !mhdr
->msg_name
||
1394 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1395 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1396 if (options
& F_VERBOSE
)
1397 warnx("invalid peername");
1400 from
= (struct sockaddr
*)mhdr
->msg_name
;
1401 fromlen
= mhdr
->msg_namelen
;
1402 if (cc
< (int)sizeof(struct icmp6_hdr
)) {
1403 if (options
& F_VERBOSE
)
1404 warnx("packet too short (%d bytes) from %s", cc
,
1405 pr_addr(from
, fromlen
));
1408 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1409 (options
& F_VERBOSE
) != 0)
1410 warnx("some control data discarded, insufficient buffer size");
1411 icp
= (struct icmp6_hdr
*)buf
;
1412 ni
= (struct icmp6_nodeinfo
*)buf
;
1415 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1416 warnx("failed to get receiving hop limit");
1419 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1420 warnx("failed to get receiving packet information");
1424 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1425 seq
= ntohs(icp
->icmp6_seq
);
1428 tpp
= (struct tv32
*)(icp
+ 1);
1429 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1430 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1432 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1433 ((double)tv
.tv_usec
) / 1000.0;
1435 tsumsq
+= triptime
* triptime
;
1436 if (triptime
< tmin
)
1438 if (triptime
> tmax
)
1442 if (TST(seq
% mx_dup_ck
)) {
1447 SET(seq
% mx_dup_ck
);
1451 if (options
& F_QUIET
)
1454 if (options
& F_FLOOD
)
1455 write(STDOUT_FILENO
, &BSPACE
, 1);
1457 printf("%d bytes from %s, icmp_seq=%u", cc
,
1458 pr_addr(from
, fromlen
), seq
);
1459 printf(" hlim=%d", hoplim
);
1460 if ((options
& F_VERBOSE
) != 0) {
1461 struct sockaddr_in6 dstsa
;
1463 memset(&dstsa
, 0, sizeof(dstsa
));
1464 dstsa
.sin6_family
= AF_INET6
;
1465 dstsa
.sin6_len
= sizeof(dstsa
);
1466 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1467 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1469 pr_addr((struct sockaddr
*)&dstsa
,
1473 printf(" time=%.3f ms", triptime
);
1476 /* check the data */
1477 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1478 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1479 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1481 printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1486 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1487 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1489 if (TST(seq
% mx_dup_ck
)) {
1494 SET(seq
% mx_dup_ck
);
1498 if (options
& F_QUIET
)
1501 printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1503 switch (ntohs(ni
->ni_code
)) {
1504 case ICMP6_NI_SUCCESS
:
1506 case ICMP6_NI_REFUSED
:
1507 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1509 case ICMP6_NI_UNKNOWN
:
1510 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1513 printf("unknown code 0x%x, type 0x%x",
1514 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1518 switch (ntohs(ni
->ni_qtype
)) {
1520 printf("NodeInfo NOOP");
1522 case NI_QTYPE_SUPTYPES
:
1523 pr_suptypes(ni
, end
- (u_char
*)ni
);
1525 case NI_QTYPE_NODEADDR
:
1526 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1529 default: /* XXX: for backward compatibility */
1530 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1531 if (buf
[off
+ ICMP6_NIRLEN
] ==
1532 cc
- off
- ICMP6_NIRLEN
- 1)
1537 cp
++; /* skip length */
1539 safeputc(*cp
& 0xff);
1545 if (dnsdecode((const u_char
**)(uintptr_t)&cp
, end
,
1546 (const u_char
*)(ni
+ 1), dnsname
,
1547 sizeof(dnsname
)) == NULL
) {
1552 * name-lookup special handling for
1555 if (cp
+ 1 <= end
&& !*cp
&&
1556 strlen(dnsname
) > 0) {
1557 dnsname
[strlen(dnsname
) - 1] = '\0';
1560 printf("%s%s", i
> 0 ? "," : "",
1564 if (options
& F_VERBOSE
) {
1570 switch (ni
->ni_code
) {
1571 case ICMP6_NI_REFUSED
:
1575 case ICMP6_NI_UNKNOWN
:
1576 printf("unknown qtype");
1581 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
1582 /* case of refusion, unknown */
1587 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
1590 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
1591 printf("TTL=%d:meaningless",
1595 printf("TTL=%d:invalid", ttl
);
1597 printf("TTL=%d", ttl
);
1607 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1616 if (buf
[off
+ ICMP6_NIRLEN
] !=
1617 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
1620 printf("invalid namelen:%d/%lu",
1621 buf
[off
+ ICMP6_NIRLEN
],
1622 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
1632 /* We've got something other than an ECHOREPLY */
1633 if (!(options
& F_VERBOSE
))
1635 printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1639 if (!(options
& F_FLOOD
)) {
1641 if (options
& F_VERBOSE
)
1649 pr_exthdrs(struct msghdr
*mhdr
)
1656 bufp
= mhdr
->msg_control
;
1657 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1658 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1659 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
1662 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
1665 switch (cm
->cmsg_type
) {
1667 printf(" HbH Options: ");
1668 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1671 #ifdef IPV6_RTHDRDSTOPTS
1672 case IPV6_RTHDRDSTOPTS
:
1674 printf(" Dst Options: ");
1675 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
1678 printf(" Routing: ");
1679 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
1687 pr_ip6opt(void *extbuf
, size_t bufsize
)
1689 struct ip6_hbh
*ext
;
1692 socklen_t extlen
, len
;
1698 ext
= (struct ip6_hbh
*)extbuf
;
1699 extlen
= (ext
->ip6h_len
+ 1) * 8;
1700 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
1701 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
1704 * Bounds checking on the ancillary data buffer:
1705 * subtract the size of a cmsg structure from the buffer size.
1707 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
1708 extlen
= bufsize
- CMSG_SPACE(0);
1709 warnx("options truncated, showing only %u (total=%u)",
1710 (unsigned int)(extlen
/ 8 - 1),
1711 (unsigned int)(ext
->ip6h_len
));
1716 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
1717 &type
, &len
, &databuf
);
1718 if (currentlen
== -1)
1722 * Note that inet6_opt_next automatically skips any padding
1727 offset
= inet6_opt_get_val(databuf
, offset
,
1728 &value4
, sizeof(value4
));
1729 printf(" Jumbo Payload Opt: Length %u\n",
1730 (u_int32_t
)ntohl(value4
));
1732 /* FIXME: RFC3542 option is IP6OPT_ROUTER_ALERT. */
1733 case IP6OPT_RTALERT
:
1735 offset
= inet6_opt_get_val(databuf
, offset
,
1736 &value2
, sizeof(value2
));
1737 printf(" Router Alert Opt: Type %u\n",
1741 printf(" Received Opt %u len %lu\n",
1742 type
, (unsigned long)len
);
1748 #else /* !USE_RFC3542 */
1751 pr_ip6opt(void *extbuf __unused
, size_t bufsize __unused
)
1756 #endif /* USE_RFC3542 */
1760 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
1762 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
1764 /* print fixed part of the header */
1765 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
1766 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
1767 printf("segments unknown, ");
1768 printf("%d left\n", rh
->ip6r_segleft
);
1773 #else /* !USE_RFC3542 */
1776 pr_rthdr(void *extbuf __unused
)
1781 #endif /* USE_RFC3542 */
1784 pr_bitrange(u_int32_t v
, int soff
, int ii
)
1791 /* shift till we have 0x01 */
1792 if ((v
& 0x01) == 0) {
1794 printf("-%u", soff
+ off
- 1);
1805 case 0x04: case 0x0c:
1816 /* we have 0x01 with us */
1817 for (i
= 0; i
< 32 - off
; i
++) {
1818 if ((v
& (0x01 << i
)) == 0)
1822 printf(" %u", soff
+ off
);
1829 /* struct icmp6_nodeinfo *ni: ni->qtype must be SUPTYPES */
1831 pr_suptypes(struct icmp6_nodeinfo
*ni
, size_t nilen
)
1835 const u_char
*cp
, *end
;
1838 u_int16_t words
; /*32bit count*/
1841 #define MAXQTYPES (1 << 16)
1845 cp
= (u_char
*)(ni
+ 1);
1846 end
= ((u_char
*)ni
) + nilen
;
1850 printf("NodeInfo Supported Qtypes");
1851 if (options
& F_VERBOSE
) {
1852 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
1853 printf(", compressed bitmap");
1855 printf(", raw bitmap");
1859 clen
= (size_t)(end
- cp
);
1860 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
1861 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
1867 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
1869 memcpy(&cbit
, cp
, sizeof(cbit
));
1870 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
1874 clen
= ntohs(cbit
.words
) * sizeof(v
);
1875 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
1880 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
1881 memcpy(&v
, cp
+ off
, sizeof(v
));
1882 v
= (u_int32_t
)ntohl(v
);
1883 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
1885 /* flush the remaining bits */
1886 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
1890 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
1891 cur
+= ntohs(cbit
.skip
) * 32;
1895 /* struct icmp6_nodeinfo *ni: ni->qtype must be NODEADDR */
1897 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, int nilen
)
1899 u_char
*cp
= (u_char
*)(ni
+ 1);
1900 char ntop_buf
[INET6_ADDRSTRLEN
];
1903 nilen
-= sizeof(struct icmp6_nodeinfo
);
1905 if (options
& F_VERBOSE
) {
1906 switch (ni
->ni_code
) {
1907 case ICMP6_NI_REFUSED
:
1910 case ICMP6_NI_UNKNOWN
:
1911 printf("unknown qtype");
1914 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
1915 printf(" truncated");
1919 printf(" no address\n");
1922 * In icmp-name-lookups 05 and later, TTL of each returned address
1923 * is contained in the resposne. We try to detect the version
1924 * by the length of the data, but note that the detection algorithm
1925 * is incomplete. We assume the latest draft by default.
1927 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
1933 /* XXX: alignment? */
1934 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
1935 cp
+= sizeof(u_int32_t
);
1936 nilen
-= sizeof(u_int32_t
);
1939 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
1941 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
1942 printf(" %s", ntop_buf
);
1944 if (ttl
== 0xffffffff) {
1946 * XXX: can this convention be applied to all
1947 * type of TTL (i.e. non-ND TTL)?
1949 printf("(TTL=infty)");
1952 printf("(TTL=%u)", ttl
);
1956 nilen
-= sizeof(struct in6_addr
);
1957 cp
+= sizeof(struct in6_addr
);
1962 get_hoplim(struct msghdr
*mhdr
)
1966 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1967 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1968 if (cm
->cmsg_len
== 0)
1971 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1972 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1973 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1974 return(*(int *)CMSG_DATA(cm
));
1980 struct in6_pktinfo
*
1981 get_rcvpktinfo(struct msghdr
*mhdr
)
1985 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1986 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1987 if (cm
->cmsg_len
== 0)
1990 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1991 cm
->cmsg_type
== IPV6_PKTINFO
&&
1992 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
1993 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
1999 #ifdef IPV6_RECVPATHMTU
2001 get_pathmtu(struct msghdr
*mhdr
)
2004 struct ip6_mtuinfo
*mtuctl
= NULL
;
2006 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2007 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2008 if (cm
->cmsg_len
== 0)
2011 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2012 cm
->cmsg_type
== IPV6_PATHMTU
&&
2013 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2014 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2017 * If the notified destination is different from
2018 * the one we are pinging, just ignore the info.
2019 * We check the scope ID only when both notified value
2020 * and our own value have non-0 values, because we may
2021 * have used the default scope zone ID for sending,
2022 * in which case the scope ID value is 0.
2024 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2026 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2027 dst
.sin6_scope_id
&&
2028 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2029 dst
.sin6_scope_id
)) {
2030 if ((options
& F_VERBOSE
) != 0) {
2031 printf("path MTU for %s is notified. "
2033 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2034 sizeof(mtuctl
->ip6m_addr
)));
2040 * Ignore an invalid MTU. XXX: can we just believe
2043 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2046 /* notification for our destination. return the MTU. */
2047 return((int)mtuctl
->ip6m_mtu
);
2056 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2060 tvsub(struct timeval
*out
, struct timeval
*in
)
2062 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2064 out
->tv_usec
+= 1000000;
2066 out
->tv_sec
-= in
->tv_sec
;
2075 onint(int sig __unused
)
2079 signal(SIGINT
, SIG_DFL
);
2080 kill(getpid(), SIGINT
);
2088 * Print out statistics.
2094 printf("\n--- %s ping6 statistics ---\n", hostname
);
2095 printf("%ld packets transmitted, ", ntransmitted
);
2096 printf("%ld packets received, ", nreceived
);
2098 printf("+%ld duplicates, ", nrepeats
);
2100 if (nreceived
> ntransmitted
)
2101 printf("-- somebody's duplicating packets!");
2103 printf("%.1f%% packet loss",
2104 ((((double)ntransmitted
- nreceived
) * 100.0) /
2108 if (nreceived
&& timing
) {
2109 /* Only display average to microseconds */
2110 double num
= nreceived
+ nrepeats
;
2111 double avg
= tsum
/ num
;
2112 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2114 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2115 tmin
, avg
, tmax
, dev
);
2122 static const char *niqcode
[] = {
2124 "DNS label", /*or empty*/
2129 static const char *nircode
[] = {
2130 "Success", "Refused", "Unknown",
2136 * Print a descriptive string about an ICMP header.
2139 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2141 char ntop_buf
[INET6_ADDRSTRLEN
];
2142 struct nd_redirect
*red
;
2143 struct icmp6_nodeinfo
*ni
;
2144 char dnsname
[MAXDNAME
+ 1];
2148 switch (icp
->icmp6_type
) {
2149 case ICMP6_DST_UNREACH
:
2150 switch (icp
->icmp6_code
) {
2151 case ICMP6_DST_UNREACH_NOROUTE
:
2152 printf("No Route to Destination\n");
2154 case ICMP6_DST_UNREACH_ADMIN
:
2155 printf("Destination Administratively "
2158 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2159 printf("Destination Unreachable Beyond Scope\n");
2161 case ICMP6_DST_UNREACH_ADDR
:
2162 printf("Destination Host Unreachable\n");
2164 case ICMP6_DST_UNREACH_NOPORT
:
2165 printf("Destination Port Unreachable\n");
2168 printf("Destination Unreachable, Bad Code: %d\n",
2172 /* Print returned IP header information */
2173 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2175 case ICMP6_PACKET_TOO_BIG
:
2176 printf("Packet too big mtu = %d\n",
2177 (int)ntohl(icp
->icmp6_mtu
));
2178 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2180 case ICMP6_TIME_EXCEEDED
:
2181 switch (icp
->icmp6_code
) {
2182 case ICMP6_TIME_EXCEED_TRANSIT
:
2183 printf("Time to live exceeded\n");
2185 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2186 printf("Frag reassembly time exceeded\n");
2189 printf("Time exceeded, Bad Code: %d\n",
2193 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2195 case ICMP6_PARAM_PROB
:
2196 printf("Parameter problem: ");
2197 switch (icp
->icmp6_code
) {
2198 case ICMP6_PARAMPROB_HEADER
:
2199 printf("Erroneous Header ");
2201 case ICMP6_PARAMPROB_NEXTHEADER
:
2202 printf("Unknown Nextheader ");
2204 case ICMP6_PARAMPROB_OPTION
:
2205 printf("Unrecognized Option ");
2208 printf("Bad code(%d) ", icp
->icmp6_code
);
2211 printf("pointer = 0x%02x\n", (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2212 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2214 case ICMP6_ECHO_REQUEST
:
2215 printf("Echo Request");
2216 /* XXX ID + Seq + Data */
2218 case ICMP6_ECHO_REPLY
:
2219 printf("Echo Reply");
2220 /* XXX ID + Seq + Data */
2222 case ICMP6_MEMBERSHIP_QUERY
:
2223 printf("Listener Query");
2225 case ICMP6_MEMBERSHIP_REPORT
:
2226 printf("Listener Report");
2228 case ICMP6_MEMBERSHIP_REDUCTION
:
2229 printf("Listener Done");
2231 case ND_ROUTER_SOLICIT
:
2232 printf("Router Solicitation");
2234 case ND_ROUTER_ADVERT
:
2235 printf("Router Advertisement");
2237 case ND_NEIGHBOR_SOLICIT
:
2238 printf("Neighbor Solicitation");
2240 case ND_NEIGHBOR_ADVERT
:
2241 printf("Neighbor Advertisement");
2244 red
= (struct nd_redirect
*)icp
;
2245 printf("Redirect\n");
2246 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2248 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2249 printf("Destination: %s", ntop_buf
);
2250 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2252 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2253 printf(" New Target: %s", ntop_buf
);
2255 case ICMP6_NI_QUERY
:
2256 printf("Node Information Query");
2257 /* XXX ID + Seq + Data */
2258 ni
= (struct icmp6_nodeinfo
*)icp
;
2259 l
= end
- (u_char
*)(ni
+ 1);
2261 switch (ntohs(ni
->ni_qtype
)) {
2265 case NI_QTYPE_SUPTYPES
:
2266 printf("Supported qtypes");
2271 case NI_QTYPE_NODEADDR
:
2274 case NI_QTYPE_IPV4ADDR
:
2275 printf("IPv4 nodeaddr");
2278 printf("unknown qtype");
2281 if (options
& F_VERBOSE
) {
2282 switch (ni
->ni_code
) {
2283 case ICMP6_NI_SUBJ_IPV6
:
2284 if (l
== sizeof(struct in6_addr
) &&
2285 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2286 sizeof(ntop_buf
)) != NULL
) {
2287 printf(", subject=%s(%s)",
2288 niqcode
[ni
->ni_code
], ntop_buf
);
2291 /* backward compat to -W */
2292 printf(", oldfqdn");
2294 printf(", invalid");
2298 case ICMP6_NI_SUBJ_FQDN
:
2299 if (end
== (u_char
*)(ni
+ 1)) {
2300 printf(", no subject");
2303 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2304 cp
= (const u_char
*)(ni
+ 1);
2305 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2306 sizeof(dnsname
)) != NULL
)
2307 printf("(%s)", dnsname
);
2309 printf("(invalid)");
2311 case ICMP6_NI_SUBJ_IPV4
:
2312 if (l
== sizeof(struct in_addr
) &&
2313 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2314 sizeof(ntop_buf
)) != NULL
) {
2315 printf(", subject=%s(%s)",
2316 niqcode
[ni
->ni_code
], ntop_buf
);
2318 printf(", invalid");
2321 printf(", invalid");
2326 case ICMP6_NI_REPLY
:
2327 printf("Node Information Reply");
2328 /* XXX ID + Seq + Data */
2329 ni
= (struct icmp6_nodeinfo
*)icp
;
2331 switch (ntohs(ni
->ni_qtype
)) {
2335 case NI_QTYPE_SUPTYPES
:
2336 printf("Supported qtypes");
2341 case NI_QTYPE_NODEADDR
:
2344 case NI_QTYPE_IPV4ADDR
:
2345 printf("IPv4 nodeaddr");
2348 printf("unknown qtype");
2351 if (options
& F_VERBOSE
) {
2352 if (ni
->ni_code
> NELEM(nircode
))
2353 printf(", invalid");
2355 printf(", %s", nircode
[ni
->ni_code
]);
2359 printf("Bad ICMP type: %d", icp
->icmp6_type
);
2365 * Print an IP6 header.
2368 pr_iph(struct ip6_hdr
*ip6
)
2370 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2372 char ntop_buf
[INET6_ADDRSTRLEN
];
2374 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2375 tc
= (tc
>> 4) & 0x0f;
2376 tc
|= (ip6
->ip6_vfc
<< 4);
2378 printf("Vr TC Flow Plen Nxt Hlim\n");
2379 printf(" %1x %02x %05x %04x %02x %02x\n",
2380 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2381 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2382 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2383 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2384 printf("%s->", ntop_buf
);
2385 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2386 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2387 printf("%s\n", ntop_buf
);
2392 * Return an ascii host address as a dotted quad and optionally with
2396 pr_addr(struct sockaddr
*addr
, int addrlen
)
2398 static char buf
[NI_MAXHOST
];
2401 if ((options
& F_HOSTNAME
) == 0)
2402 flag
|= NI_NUMERICHOST
;
2404 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2412 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2415 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2417 u_char
*cp
= (u_char
*)ip6
, nh
;
2420 if (end
- (u_char
*)ip6
< (int)sizeof(*ip6
)) {
2425 hlen
= sizeof(*ip6
);
2429 while (end
- cp
>= 8) {
2431 case IPPROTO_HOPOPTS
:
2433 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2434 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2436 case IPPROTO_DSTOPTS
:
2438 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2439 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2441 case IPPROTO_FRAGMENT
:
2443 hlen
= sizeof(struct ip6_frag
);
2444 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2446 case IPPROTO_ROUTING
:
2448 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2449 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2454 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2455 nh
= ((struct ah
*)cp
)->ah_nxt
;
2458 case IPPROTO_ICMPV6
:
2459 printf("ICMP6: type = %d, code = %d\n",
2466 printf("TCP: from port %u, to port %u (decimal)\n",
2467 (*cp
* 256 + *(cp
+ 1)),
2468 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2471 printf("UDP: from port %u, to port %u (decimal)\n",
2472 (*cp
* 256 + *(cp
+ 1)),
2473 (*(cp
+ 2) * 256 + *(cp
+ 3)));
2476 printf("Unknown Header(%d)\n", nh
);
2480 if ((cp
+= hlen
) >= end
)
2495 fill(char *bp
, char *patp
)
2501 for (cp
= patp
; *cp
; cp
++)
2503 errx(1, "patterns must be specified as hex digits");
2505 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2506 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
2507 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
2508 &pat
[13], &pat
[14], &pat
[15]);
2513 kk
<= MAXDATALEN
- (8 + (int)sizeof(struct tv32
) + ii
);
2515 for (jj
= 0; jj
< ii
; ++jj
)
2516 bp
[jj
+ kk
] = pat
[jj
];
2517 if (!(options
& F_QUIET
)) {
2518 printf("PATTERN: 0x");
2519 for (jj
= 0; jj
< ii
; ++jj
)
2520 printf("%02x", bp
[jj
] & 0xFF);
2526 #ifdef IPSEC_POLICY_IPSEC
2528 setpolicy(int so __unused
, char *policy
)
2533 return 0; /* ignore */
2535 buf
= ipsec_set_policy(policy
, strlen(policy
));
2537 errx(1, "%s", ipsec_strerror());
2538 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
2539 ipsec_get_policylen(buf
)) < 0)
2540 warnx("Unable to set IPsec policy");
2554 u_int8_t digest
[16];
2557 char hbuf
[NI_MAXHOST
];
2558 struct in6_addr in6
;
2560 p
= strchr(name
, '.');
2562 p
= name
+ strlen(name
);
2564 if (l
> 63 || l
> sizeof(hbuf
) - 1)
2565 return NULL
; /*label too long*/
2566 strncpy(hbuf
, name
, l
);
2567 hbuf
[(int)l
] = '\0';
2569 for (q
= name
; *q
; q
++) {
2570 if (isupper(*(unsigned char *)q
))
2571 *q
= tolower(*(unsigned char *)q
);
2574 /* generate 8 bytes of pseudo-random value. */
2575 memset(&ctxt
, 0, sizeof(ctxt
));
2578 MD5Update(&ctxt
, &c
, sizeof(c
));
2579 MD5Update(&ctxt
, (unsigned char *)name
, l
);
2580 MD5Final(digest
, &ctxt
);
2582 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
2583 return NULL
; /*XXX*/
2584 bcopy(digest
, &in6
.s6_addr
[12], 4);
2586 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
2589 return strdup(hbuf
);
2596 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2601 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2605 #ifdef IPV6_USE_MIN_MTU
2609 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2610 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2611 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2615 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2616 "[hops ...] host\n");