2 * Copyright (c) 2002-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1989, 1993
60 * The Regents of the University of California. All rights reserved.
62 * This code is derived from software contributed to Berkeley by
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 #include <sys/cdefs.h>
97 __unused
static const char copyright
[] =
98 "@(#) Copyright (c) 1989, 1993\n\
99 The Regents of the University of California. All rights reserved.\n";
100 #endif /* not lint */
103 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
104 * measure round-trip-delays and packet loss across network paths.
108 * U. S. Army Ballistic Research Laboratory
112 * Public Domain. Distribution Unlimited.
114 * More statistics could always be gathered.
115 * This program has to run SUID to ROOT to access the ICMP socket.
119 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
120 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
121 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
122 * network attached to 1 or more interfaces)
125 #include <sys/param.h>
127 #include <sys/socket.h>
128 #include <sys/time.h>
131 #include <net/route.h>
133 #include <netinet/in.h>
134 #include <netinet/ip6.h>
135 #include <netinet/icmp6.h>
136 #include <arpa/inet.h>
137 #include <arpa/nameser.h>
154 #include <sysexits.h>
158 #include <netinet6/ah.h>
159 #include <netinet6/ipsec.h>
169 #define MAXPACKETLEN 131072
171 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
172 #define ICMP6ECHOTMLEN sizeof(struct tv32)
173 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
174 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
175 /* FQDN case, 64 bits of nonce + 32 bits ttl */
176 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
177 #define EXTRA 256 /* for AH and various other headers. weird. */
178 #define DEFDATALEN ICMP6ECHOTMLEN
179 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
180 #define NROUTES 9 /* number of record route slots */
182 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
183 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
184 #define SET(bit) (A(bit) |= B(bit))
185 #define CLR(bit) (A(bit) &= (~B(bit)))
186 #define TST(bit) (A(bit) & B(bit))
188 #define F_FLOOD 0x0001
189 #define F_INTERVAL 0x0002
190 #define F_PINGFILLED 0x0008
191 #define F_QUIET 0x0010
192 #define F_RROUTE 0x0020
193 #define F_SO_DEBUG 0x0040
194 #define F_PRTIME 0x0080
195 #define F_VERBOSE 0x0100
197 #ifdef IPSEC_POLICY_IPSEC
198 #define F_POLICY 0x0400
200 #define F_AUTHHDR 0x0200
201 #define F_ENCRYPT 0x0400
202 #endif /*IPSEC_POLICY_IPSEC*/
204 #define F_NODEADDR 0x0800
205 #define F_FQDN 0x1000
206 #define F_INTERFACE 0x2000
207 #define F_SRCADDR 0x4000
208 #define F_HOSTNAME 0x10000
209 #define F_FQDNOLD 0x20000
210 #define F_NIGROUP 0x40000
211 #define F_SUPTYPES 0x80000
212 #define F_NOMINMTU 0x100000
213 #define F_ONCE 0x200000
214 #define F_AUDIBLE 0x400000
215 #define F_MISSED 0x800000
216 #define F_DONTFRAG 0x1000000
217 #define F_SWEEP 0x2000000
218 #define F_CONNECT 0x4000000
219 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
222 static int longopt_flag
= 0;
224 #define LOF_CONNECT 0x01
225 #define LOF_PRTIME 0x02
227 static const struct option longopts
[] = {
228 { "apple-connect", no_argument
, &longopt_flag
, LOF_CONNECT
},
229 { "apple-time", no_argument
, &longopt_flag
, LOF_PRTIME
},
234 #define IN6LEN sizeof(struct in6_addr)
235 #define SA6LEN sizeof(struct sockaddr_in6)
236 #define DUMMY_PORT 10101
238 #define SIN6(s) ((struct sockaddr_in6 *)(s))
242 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
243 * number of received sequence numbers we can keep track of. Change 128
244 * to 8192 for complete accuracy...
246 #define MAX_DUP_CHK (8 * 8192)
247 int mx_dup_ck
= MAX_DUP_CHK
;
248 char rcvd_tbl
[MAX_DUP_CHK
/ 8];
250 struct addrinfo
*res
;
251 struct sockaddr_in6 dst
; /* who to ping6 */
252 struct sockaddr_in6 src
; /* src addr of this packet */
254 int datalen
= DEFDATALEN
;
255 int s
; /* socket file descriptor */
256 u_char outpack
[MAXPACKETLEN
];
257 char BSPACE
= '\b'; /* characters written for flood */
258 char BBELL
= '\a'; /* characters written for AUDIBLE */
261 int ident
; /* process id to identify our packets */
262 u_int8_t nonce
[8]; /* nonce field for node information */
263 int hoplimit
= -1; /* hoplimit */
264 int pathmtu
= 0; /* path MTU for the destination. 0 = unspec. */
265 u_char
*packet
= NULL
;
266 struct cmsghdr
*cm
= NULL
;
268 unsigned int ifscope
;
271 struct pollfd fdmaskp
[1];
273 fd_set
*fdmaskp
= NULL
;
278 long nmissedmax
; /* max value of ntransmitted - nreceived - 1 */
279 long npackets
; /* max packets to transmit */
280 long nreceived
; /* # of packets we got back */
281 long nrepeats
; /* number of duplicates */
282 long ntransmitted
; /* sequence # for outbound packets = #sent */
283 struct timeval interval
= {1, 0}; /* interval between packets */
284 long snpackets
= 0; /* max packets to transmit in one sweep */
285 long sntransmitted
= 0; /* # of packets we sent in this sweep */
286 int sweepmax
= 0; /* max value of payload in sweep */
287 int sweepmin
= 0; /* start value of payload in sweep */
288 int sweepincr
= 1; /* payload increment in sweep */
291 int timing
; /* flag to do timing */
292 double tmin
= 999999999.0; /* minimum round trip time */
293 double tmax
= 0.0; /* maximum round trip time */
294 double tsum
= 0.0; /* sum of all times, for doing average */
295 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
297 /* for node addresses */
300 /* for ancillary data(advanced API) */
301 struct msghdr smsghdr
;
302 struct iovec smsgiov
[2];
305 volatile sig_atomic_t seenalrm
;
306 volatile sig_atomic_t seenint
;
308 volatile sig_atomic_t seeninfo
;
315 int so_traffic_class
= SO_TC_CTL
; /* use control class, by default */
316 int net_service_type
= -1;
318 int32_t thiszone
; /* seconds offset from gmt to local time */
319 extern int32_t gmt2local(time_t);
320 static void pr_currenttime(void);
322 int main(int, char *[]);
323 void fill(char *, char *);
324 int get_hoplim(struct msghdr
*);
325 int get_pathmtu(struct msghdr
*);
326 int get_tclass(struct msghdr
*);
327 int get_so_traffic_class(struct msghdr
*);
328 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
330 void retransmit(void);
332 size_t pingerlen(void);
334 const char *pr_addr(struct sockaddr
*, int);
335 void pr_icmph(struct icmp6_hdr
*, u_char
*);
336 void pr_iph(struct ip6_hdr
*);
337 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
338 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
339 int myechoreply(const struct icmp6_hdr
*);
340 int mynireply(const struct icmp6_nodeinfo
*);
341 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
343 void pr_pack(u_char
*, int, struct msghdr
*);
344 void pr_exthdrs(struct msghdr
*);
345 void pr_ip6opt(void *, size_t);
346 void pr_rthdr(void *, size_t);
347 int pr_bitrange(u_int32_t
, int, int);
348 void pr_retip(struct ip6_hdr
*, u_char
*);
350 void tvsub(struct timeval
*, struct timeval
*);
351 int setpolicy(int, char *);
352 char *nigroup(char *);
353 static int str2sotc(const char *, bool *);
354 static int str2netservicetype(const char *, bool *);
355 static u_int8_t
str2tclass(const char *, bool *);
359 main(int argc
, char *argv
[])
361 struct itimerval itimer
;
362 struct sockaddr_in6 from
;
363 #ifndef HAVE_ARC4RANDOM
369 struct timeval timeout
, *tv
;
371 struct addrinfo hints
;
373 int ch
, hold
, packlen
, preload
, optval
, ret_ga
;
375 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
377 struct cmsghdr
*scmsgp
= NULL
;
378 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
383 struct in6_pktinfo
*pktinfo
= NULL
;
384 #ifdef USE_RFC2292BIS
385 struct ip6_rthdr
*rthdr
= NULL
;
387 #ifdef IPSEC_POLICY_IPSEC
388 char *policy_in
= NULL
;
389 char *policy_out
= NULL
;
393 #ifdef IPV6_USE_MIN_MTU
396 /* T_CLASS value -1 means default, so -2 means do not bother */
399 struct timeval intvl
;
401 /* just to be sure */
402 memset(&smsghdr
, 0, sizeof(smsghdr
));
403 memset(&smsgiov
, 0, sizeof(smsgiov
));
406 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
410 #ifdef IPSEC_POLICY_IPSEC
414 #endif /*IPSEC_POLICY_IPSEC*/
416 while ((ch
= getopt_long(argc
, argv
,
417 "a:b:B:Cc:DdfHG:g:h:I:i:k:K:l:mnNop:qrRS:s:tvwWz:" ADDOPTS
,
418 longopts
, NULL
)) != -1) {
425 options
&= ~F_NOUSERDATA
;
426 options
|= F_NODEADDR
;
427 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
430 naflags
|= NI_NODEADDR_FLAG_ALL
;
434 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
438 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
442 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
446 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
448 case 'A': /* experimental. not in the spec */
449 #ifdef NI_NODEADDR_FLAG_ANYCAST
450 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
453 errx(1, "-a A is not supported on "
465 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
468 lsockbufsize
= strtoul(optarg
, &e
, 10);
469 sockbufsize
= lsockbufsize
;
470 if (errno
|| !*optarg
|| *e
||
471 sockbufsize
!= lsockbufsize
)
472 errx(1, "invalid socket buffer size");
474 errx(1, "-b option ignored: SO_SNDBUF/SO_RCVBUF "
475 "socket options not supported");
485 npackets
= strtol(optarg
, &e
, 10);
486 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
488 "illegal number of packets -- %s", optarg
);
491 options
|= F_DONTFRAG
;
494 options
|= F_SO_DEBUG
;
499 errx(1, "Must be superuser to flood ping");
502 setbuf(stdout
, (char *)NULL
);
512 tofree
= strdup(optarg
);
514 errx(1, "### strdup() failed");
520 if ((str
= strsep(&ptr
, ",")) == NULL
)
521 errx(1, "-G requires maximum packet size");
522 ultmp
= strtoul(str
, &ep
, 0);
523 if (*ep
|| ep
== optarg
)
524 errx(EX_USAGE
, "option -G invalid maximum packet size: `%s'",
528 if (sweepmax
< 1 || sweepmax
> MAXDATALEN
) {
530 "-G invalid maximum packet size, needs to be between 1 and %d",
534 if ((str
= strsep(&ptr
, ",")) == NULL
)
537 ultmp
= strtoul(str
, &ep
, 0);
538 if (*ep
|| ep
== optarg
)
539 errx(EX_USAGE
, "option -G invalid minimum packet size: `%s'",
542 if (sweepmin
< 0 || sweepmin
> MAXDATALEN
) {
544 "-G invalid minimum packet size, needs to be between 0 and %d",
549 if ((str
= strsep(&ptr
, ",")) == NULL
)
553 ultmp
= strtoul(str
, &ep
, 0);
554 if (*ep
|| ep
== optarg
)
555 errx(EX_USAGE
, "option -G invalid sweep increment size: `%s'",
558 if (sweepincr
< 1 || sweepincr
> MAXDATALEN
) {
560 "-G invalid sweep increment size, needs to be between 1 and %d",
568 options
|= F_HOSTNAME
;
570 case 'h': /* hoplimit */
571 hoplimit
= strtol(optarg
, &e
, 10);
572 if (*optarg
== '\0' || *e
!= '\0')
573 errx(1, "illegal hoplimit %s", optarg
);
574 if (255 < hoplimit
|| hoplimit
< -1)
576 "illegal hoplimit -- %s", optarg
);
580 options
|= F_INTERFACE
;
581 #ifndef USE_SIN6_SCOPE_ID
585 case 'i': /* wait between sending packets */
586 intval
= strtod(optarg
, &e
);
587 if (*optarg
== '\0' || *e
!= '\0')
588 errx(1, "illegal timing interval %s", optarg
);
589 if (intval
< 0.1 && getuid()) {
590 errx(1, "%s: only root may use interval < 0.1s",
593 interval
.tv_sec
= (long)intval
;
595 (long)((intval
- interval
.tv_sec
) * 1000000);
596 if (interval
.tv_sec
< 0)
597 errx(1, "illegal timing interval %s", optarg
);
598 /* less than 1/hz does not make sense */
599 if (interval
.tv_sec
== 0 && interval
.tv_usec
< 1) {
600 warnx("too small interval, raised to .000001");
601 interval
.tv_usec
= 1;
603 options
|= F_INTERVAL
;
606 if (strcasecmp(optarg
, "sendmsg") == 0) {
610 if (strcasecmp(optarg
, "recvmsg") == 0) {
614 so_traffic_class
= str2sotc(optarg
, &valid
);
616 errx(EX_USAGE
, "bad traffic class: `%s'",
620 if (strcasecmp(optarg
, "sendmsg") == 0) {
624 net_service_type
= str2netservicetype(optarg
, &valid
);
626 errx(EX_USAGE
, "bad network service type: `%s'",
628 /* suppress default traffic class (-k can still be specified after -K) */
629 so_traffic_class
= -1;
634 errx(1, "Must be superuser to preload");
636 preload
= strtol(optarg
, &e
, 10);
637 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
638 errx(1, "illegal preload value -- %s", optarg
);
641 #ifdef IPV6_USE_MIN_MTU
645 errx(1, "-%c is not supported on this platform", ch
);
649 options
&= ~F_HOSTNAME
;
652 options
|= F_NIGROUP
;
657 case 'p': /* fill buffer with user pattern */
658 options
|= F_PINGFILLED
;
659 fill((char *)datap
, optarg
);
665 options
|= F_AUDIBLE
;
671 memset(&hints
, 0, sizeof(struct addrinfo
));
672 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
673 hints
.ai_family
= AF_INET6
;
674 hints
.ai_socktype
= SOCK_RAW
;
675 hints
.ai_protocol
= IPPROTO_ICMPV6
;
677 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
679 errx(1, "invalid source address: %s",
680 gai_strerror(ret_ga
));
683 * res->ai_family must be AF_INET6 and res->ai_addrlen
684 * must be sizeof(src).
686 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
687 srclen
= res
->ai_addrlen
;
690 options
|= F_SRCADDR
;
692 case 's': /* size of packet to send */
693 datalen
= strtol(optarg
, &e
, 10);
694 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
695 errx(1, "illegal datalen value -- %s", optarg
);
696 if (datalen
> MAXDATALEN
) {
698 "datalen value too large, maximum is %d",
703 options
&= ~F_NOUSERDATA
;
704 options
|= F_SUPTYPES
;
707 options
|= F_VERBOSE
;
710 options
&= ~F_NOUSERDATA
;
714 options
&= ~F_NOUSERDATA
;
715 options
|= F_FQDNOLD
;
718 tclass
= str2tclass(optarg
, &valid
);
720 errx(1, "illegal TOS value -- %s", optarg
);
724 #ifdef IPSEC_POLICY_IPSEC
727 if (!strncmp("in", optarg
, 2)) {
728 if ((policy_in
= strdup(optarg
)) == NULL
)
730 } else if (!strncmp("out", optarg
, 3)) {
731 if ((policy_out
= strdup(optarg
)) == NULL
)
734 errx(1, "invalid security policy");
738 options
|= F_AUTHHDR
;
741 options
|= F_ENCRYPT
;
743 #endif /*IPSEC_POLICY_IPSEC*/
746 switch (longopt_flag
) {
748 options
|= F_CONNECT
;
752 thiszone
= gmt2local(0);
765 if (boundif
!= NULL
&& (ifscope
= if_nametoindex(boundif
)) == 0)
766 errx(1, "bad interface name");
768 if ((options
& F_SWEEP
) && !sweepmax
)
769 errx(EX_USAGE
, "Maximum sweep size must be specified");
771 if ((options
& F_SWEEP
) && (options
& F_NOUSERDATA
))
772 errx(EX_USAGE
, "Option -G incompatible with -t, -w and -W");
783 #ifdef IPV6_RECVRTHDR /* 2292bis */
784 rthlen
= CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0
,
787 rthlen
= inet6_rthdr_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
790 errx(1, "too many intermediate hops");
796 if (options
& F_NIGROUP
) {
797 target
= nigroup(argv
[argc
- 1]);
798 if (target
== NULL
) {
803 target
= argv
[argc
- 1];
806 memset(&hints
, 0, sizeof(struct addrinfo
));
807 hints
.ai_flags
= AI_CANONNAME
;
808 hints
.ai_family
= AF_INET6
;
809 hints
.ai_socktype
= SOCK_RAW
;
810 hints
.ai_protocol
= IPPROTO_ICMPV6
;
812 ret_ga
= getaddrinfo(target
, NULL
, &hints
, &res
);
814 errx(1, "getaddrinfo -- %s", gai_strerror(ret_ga
));
815 if (res
->ai_canonname
)
816 hostname
= res
->ai_canonname
;
821 errx(1, "getaddrinfo failed");
823 (void)memcpy(&dst
, res
->ai_addr
, res
->ai_addrlen
);
825 res
->ai_socktype
= getuid() ? SOCK_DGRAM
: SOCK_RAW
;
826 res
->ai_protocol
= IPPROTO_ICMPV6
;
828 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
829 res
->ai_protocol
)) < 0)
833 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
834 (char *)&ifscope
, sizeof (ifscope
)) != 0)
835 err(1, "setsockopt(IPV6_BOUND_IF)");
839 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
840 (char *)&nocell
, sizeof (nocell
)) != 0)
841 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
844 /* set the source address if specified. */
845 if ((options
& F_SRCADDR
) &&
846 bind(s
, (struct sockaddr
*)&src
, srclen
) != 0) {
850 /* set the gateway (next hop) if specified */
852 struct addrinfo ghints
, *gres
;
855 memset(&ghints
, 0, sizeof(ghints
));
856 ghints
.ai_family
= AF_INET6
;
857 ghints
.ai_socktype
= SOCK_RAW
;
858 ghints
.ai_protocol
= IPPROTO_ICMPV6
;
860 error
= getaddrinfo(gateway
, NULL
, &hints
, &gres
);
862 errx(1, "getaddrinfo for the gateway %s: %s",
863 gateway
, gai_strerror(error
));
865 if (gres
->ai_next
&& (options
& F_VERBOSE
))
866 warnx("gateway resolves to multiple addresses");
868 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_NEXTHOP
,
869 gres
->ai_addr
, gres
->ai_addrlen
)) {
870 err(1, "setsockopt(IPV6_NEXTHOP)");
877 * let the kerel pass extension headers of incoming packets,
878 * for privileged socket options
880 if ((options
& F_VERBOSE
) != 0) {
883 #ifdef IPV6_RECVHOPOPTS
884 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPOPTS
, &opton
,
886 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
887 #else /* old adv. API */
888 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPOPTS
, &opton
,
890 err(1, "setsockopt(IPV6_HOPOPTS)");
892 #ifdef IPV6_RECVDSTOPTS
893 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVDSTOPTS
, &opton
,
895 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
896 #else /* old adv. API */
897 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DSTOPTS
, &opton
,
899 err(1, "setsockopt(IPV6_DSTOPTS)");
901 #ifdef IPV6_RECVRTHDRDSTOPTS
902 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDRDSTOPTS
, &opton
,
904 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
908 /* revoke root privilege */
909 if (seteuid(getuid()) != 0)
910 err(1, "seteuid() failed");
911 if (setuid(getuid()) != 0)
912 err(1, "setuid() failed");
914 if ((options
& F_FLOOD
) && (options
& F_INTERVAL
))
915 errx(1, "-f and -i incompatible options");
917 if ((options
& F_CONNECT
)) {
918 if (connect(s
, (struct sockaddr
*)&dst
, sizeof(dst
)) == -1)
919 err(EX_OSERR
, "connect");
923 if (sweepmin
>= sweepmax
)
924 errx(EX_USAGE
, "Maximum packet size must be greater than the minimum packet size");
926 if (datalen
!= DEFDATALEN
)
927 errx(EX_USAGE
, "Packet size and ping sweep are mutually exclusive");
930 snpackets
= npackets
;
937 if ((options
& F_NOUSERDATA
) == 0) {
938 /* in F_VERBOSE case, we may get non-echoreply packets*/
939 if (options
& F_VERBOSE
)
940 packlen
= MAX(2048, sweepmax
) + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
942 packlen
= MAX(datalen
, sweepmax
) + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
944 /* suppress timing for node information query */
947 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
950 if (!(packet
= (u_char
*)malloc((u_int
)MAX(datalen
, sweepmax
))))
951 err(1, "Unable to allocate packet");
952 if (!(options
& F_PINGFILLED
))
953 for (i
= ICMP6ECHOLEN
; i
< MAX(datalen
, sweepmax
); ++i
)
956 ident
= getpid() & 0xFFFF;
957 #ifndef HAVE_ARC4RANDOM
958 gettimeofday(&seed
, NULL
);
959 srand((unsigned int)(seed
.tv_sec
^ seed
.tv_usec
^ (long)ident
));
960 memset(nonce
, 0, sizeof(nonce
));
961 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(int))
962 *((int *)&nonce
[i
]) = rand();
964 memset(nonce
, 0, sizeof(nonce
));
965 for (i
= 0; i
< sizeof(nonce
); i
+= sizeof(u_int32_t
))
966 *((u_int32_t
*)&nonce
[i
]) = arc4random();
969 if (options
& F_DONTFRAG
)
970 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
971 &optval
, sizeof(optval
)) == -1)
972 err(1, "IPV6_DONTFRAG");
974 if (options
& F_SO_DEBUG
)
975 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
979 (void) setsockopt(s
, SOL_SOCKET
, SO_RECV_ANYIF
, (char *)&hold
,
982 optval
= IPV6_DEFHLIM
;
983 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
984 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
985 &optval
, sizeof(optval
)) == -1)
986 err(1, "IPV6_MULTICAST_HOPS");
987 #ifdef IPV6_USE_MIN_MTU
989 optval
= mflag
> 1 ? 0 : 1;
991 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
992 &optval
, sizeof(optval
)) == -1)
993 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
995 #ifdef IPV6_RECVPATHMTU
998 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
999 &optval
, sizeof(optval
)) == -1)
1000 err(1, "setsockopt(IPV6_RECVPATHMTU)");
1002 #endif /* IPV6_RECVPATHMTU */
1003 #endif /* IPV6_USE_MIN_MTU */
1006 #ifdef IPSEC_POLICY_IPSEC
1007 if (options
& F_POLICY
) {
1008 if (setpolicy(s
, policy_in
) < 0)
1009 errx(1, "%s", ipsec_strerror());
1010 if (setpolicy(s
, policy_out
) < 0)
1011 errx(1, "%s", ipsec_strerror());
1014 if (options
& F_AUTHHDR
) {
1015 optval
= IPSEC_LEVEL_REQUIRE
;
1016 #ifdef IPV6_AUTH_TRANS_LEVEL
1017 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
1018 &optval
, sizeof(optval
)) == -1)
1019 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
1021 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
1022 &optval
, sizeof(optval
)) == -1)
1023 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
1026 if (options
& F_ENCRYPT
) {
1027 optval
= IPSEC_LEVEL_REQUIRE
;
1028 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
1029 &optval
, sizeof(optval
)) == -1)
1030 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
1032 #endif /*IPSEC_POLICY_IPSEC*/
1037 struct icmp6_filter filt
;
1038 if (!(options
& F_VERBOSE
)) {
1039 ICMP6_FILTER_SETBLOCKALL(&filt
);
1040 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
1041 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
1042 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
1044 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
1046 ICMP6_FILTER_SETPASSALL(&filt
);
1048 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
1050 err(1, "setsockopt(ICMP6_FILTER)");
1052 #endif /*ICMP6_FILTER*/
1054 /* let the kerel pass extension headers of incoming packets */
1055 if ((options
& F_VERBOSE
) != 0) {
1058 #ifdef IPV6_RECVRTHDR
1059 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
1061 err(1, "setsockopt(IPV6_RECVRTHDR)");
1062 #else /* old adv. API */
1063 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
1065 err(1, "setsockopt(IPV6_RTHDR)");
1072 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVTCLASS
, &on
,
1074 err(1, "setsockopt(IPV6_RECVTCLASS)");
1076 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
1078 err(1, "setsockopt(IPV6_TCLASS)");
1083 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
1084 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1085 &optval, sizeof(optval)) == -1)
1086 err(1, "IPV6_MULTICAST_LOOP");
1089 /* Specify the outgoing interface and/or the source address */
1091 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
1094 ip6optlen
+= CMSG_SPACE(sizeof(int));
1096 #ifdef IPV6_USE_MIN_MTU
1098 ip6optlen
+= CMSG_SPACE(sizeof(int));
1099 #endif /* IPV6_USE_MIN_MTU */
1102 ip6optlen
+= CMSG_SPACE(sizeof(int));
1104 if (use_sendmsg
== 0) {
1105 if (net_service_type
!= -1)
1106 if (setsockopt(s
, SOL_SOCKET
, SO_NET_SERVICE_TYPE
,
1107 (void *)&net_service_type
, sizeof (net_service_type
)) != 0)
1108 warn("setsockopt(SO_NET_SERVICE_TYPE");
1109 if (so_traffic_class
!= -1) {
1110 if (setsockopt(s
, SOL_SOCKET
, SO_TRAFFIC_CLASS
,
1111 (void *)&so_traffic_class
, sizeof (so_traffic_class
)) != 0)
1112 warn("setsockopt(SO_TRAFFIC_CLASS");
1116 if (net_service_type
!= -1)
1117 ip6optlen
+= CMSG_SPACE(sizeof(int));
1118 if (so_traffic_class
!= -1)
1119 ip6optlen
+= CMSG_SPACE(sizeof(int));
1121 if (use_recvmsg
> 0) {
1123 if (setsockopt(s
, SOL_SOCKET
, SO_RECV_TRAFFIC_CLASS
,
1124 (void *)&on
, sizeof (on
)) != 0)
1125 warn("setsockopt(SO_RECV_TRAFFIC_CLASS");
1128 /* set IP6 packet options */
1130 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
1131 errx(1, "can't allocate enough memory");
1132 smsghdr
.msg_control
= (caddr_t
)scmsg
;
1133 smsghdr
.msg_controllen
= ip6optlen
;
1134 scmsgp
= (struct cmsghdr
*)scmsg
;
1137 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
1138 memset(pktinfo
, 0, sizeof(*pktinfo
));
1139 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1140 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1141 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
1142 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1145 /* set the outgoing interface */
1147 #ifndef USE_SIN6_SCOPE_ID
1148 /* pktinfo must have already been allocated */
1149 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
1150 errx(1, "%s: invalid interface name", ifname
);
1152 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
1153 errx(1, "%s: invalid interface name", ifname
);
1156 if (hoplimit
!= -1) {
1157 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1158 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1159 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
1160 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
1162 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1165 #ifdef IPV6_USE_MIN_MTU
1167 optval
= mflag
> 1 ? 0 : 1;
1169 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1170 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1171 scmsgp
->cmsg_type
= IPV6_USE_MIN_MTU
;
1172 *(int *)(CMSG_DATA(scmsgp
)) = optval
;
1174 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1176 #endif /* IPV6_USE_MIN_MTU */
1178 if (argc
> 1) { /* some intermediate addrs are specified */
1180 #ifdef USE_RFC2292BIS
1184 #ifdef USE_RFC2292BIS
1185 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
1186 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
1187 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1188 scmsgp
->cmsg_type
= IPV6_RTHDR
;
1189 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
1190 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
1191 IPV6_RTHDR_TYPE_0
, argc
- 1);
1193 errx(1, "can't initialize rthdr");
1194 #else /* old advanced API */
1195 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
1196 IPV6_RTHDR_TYPE_0
)) == 0)
1197 errx(1, "can't initialize rthdr");
1198 #endif /* USE_RFC2292BIS */
1200 for (hops
= 0; hops
< argc
- 1; hops
++) {
1201 struct addrinfo
*iaip
;
1203 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
1205 errx(1, "%s", gai_strerror(error
));
1206 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
1208 "bad addr family of an intermediate addr");
1210 #ifdef USE_RFC2292BIS
1211 if (inet6_rth_add(rthdr
,
1212 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
1213 errx(1, "can't add an intermediate node");
1214 #else /* old advanced API */
1215 if (inet6_rthdr_add(scmsgp
,
1216 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
1218 errx(1, "can't add an intermediate node");
1219 #endif /* USE_RFC2292BIS */
1223 #ifndef USE_RFC2292BIS
1224 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
1225 errx(1, "can't set the last flag");
1228 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1232 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1233 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1234 scmsgp
->cmsg_type
= IPV6_TCLASS
;
1235 *(int *)(CMSG_DATA(scmsgp
)) = tclass
;
1237 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1239 if (use_sendmsg
!= 0) {
1240 if (so_traffic_class
!= -1) {
1241 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1242 scmsgp
->cmsg_level
= SOL_SOCKET
;
1243 scmsgp
->cmsg_type
= SO_TRAFFIC_CLASS
;
1244 *(int *)(CMSG_DATA(scmsgp
)) = so_traffic_class
;
1246 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1248 if (net_service_type
!= -1) {
1249 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1250 scmsgp
->cmsg_level
= SOL_SOCKET
;
1251 scmsgp
->cmsg_type
= SO_NET_SERVICE_TYPE
;
1252 *(int *)(CMSG_DATA(scmsgp
)) = net_service_type
;
1255 if (!(options
& F_SRCADDR
)) {
1257 * get the source address. XXX since we revoked the root
1258 * privilege, we cannot use a raw socket for this.
1261 socklen_t len
= sizeof(src
);
1263 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1264 err(1, "UDP socket");
1267 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
1268 (char *)&ifscope
, sizeof (ifscope
)) != 0)
1269 err(1, "setsockopt(IPV6_BOUND_IF)");
1273 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
1274 (char *)&nocell
, sizeof (nocell
)) != 0)
1275 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
1278 src
.sin6_family
= AF_INET6
;
1279 src
.sin6_addr
= dst
.sin6_addr
;
1280 src
.sin6_port
= ntohs(DUMMY_PORT
);
1281 src
.sin6_scope_id
= dst
.sin6_scope_id
;
1283 #ifdef USE_RFC2292BIS
1285 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
1286 (void *)pktinfo
, sizeof(*pktinfo
)))
1287 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1289 if (hoplimit
!= -1 &&
1290 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1291 (void *)&hoplimit
, sizeof(hoplimit
)))
1292 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1294 if (hoplimit
!= -1 &&
1295 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1296 (void *)&hoplimit
, sizeof(hoplimit
)))
1297 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1300 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
1301 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
1302 err(1, "UDP setsockopt(IPV6_RTHDR)");
1303 #else /* old advanced API */
1304 if (smsghdr
.msg_control
&&
1305 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
1306 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
1307 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1310 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
1311 err(1, "UDP connect");
1313 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
1314 err(1, "getsockname");
1319 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1321 if (MAX(datalen
, sweepmax
) > sockbufsize
)
1322 warnx("you need -b to increase socket buffer size");
1323 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
1324 sizeof(sockbufsize
)) < 0)
1325 err(1, "setsockopt(SO_SNDBUF)");
1326 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
1327 sizeof(sockbufsize
)) < 0)
1328 err(1, "setsockopt(SO_RCVBUF)");
1331 if (MAX(datalen
, sweepmax
) > 8 * 1024) /*XXX*/
1332 warnx("you need -b to increase socket buffer size");
1334 * When pinging the broadcast address, you can get a lot of
1335 * answers. Doing something so evil is useful if you are trying
1336 * to stress the ethernet, or just want to fill the arp cache
1337 * to get some stuff for /etc/ethers.
1340 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1346 #ifndef USE_SIN6_SCOPE_ID
1347 #ifdef IPV6_RECVPKTINFO
1348 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1349 sizeof(optval
)) < 0)
1350 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1351 #else /* old adv. API */
1352 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1353 sizeof(optval
)) < 0)
1354 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1356 #endif /* USE_SIN6_SCOPE_ID */
1357 #ifdef IPV6_RECVHOPLIMIT
1358 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1359 sizeof(optval
)) < 0)
1360 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1361 #else /* old adv. API */
1362 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1363 sizeof(optval
)) < 0)
1364 warn("setsockopt(IPV6_HOPLIMIT, %d, %lu)",
1365 optval
, sizeof(optval
)); /* XXX err? */
1369 printf("PING6(40+8+[%lu...%lu] bytes) ",
1370 (unsigned long)(sweepmin
),
1371 (unsigned long)(sweepmax
));
1373 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1374 (unsigned long)(pingerlen() - 8));
1375 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1376 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1378 while (preload
--) /* Fire off them quickies. */
1384 * Clear blocked signals inherited from the parent
1387 sigemptyset(&newset
);
1388 if (sigprocmask(SIG_SETMASK
, &newset
, NULL
) != 0)
1389 err(EX_OSERR
, "sigprocmask(newset)");
1391 seenalrm
= seenint
= 0;
1396 (void)signal(SIGINT
, onsignal
);
1398 (void)signal(SIGINFO
, onsignal
);
1401 if ((options
& F_FLOOD
) == 0) {
1402 if (signal(SIGALRM
, onsignal
) == SIG_ERR
)
1403 warn("signal(SIGALRM)");
1404 itimer
.it_interval
= interval
;
1405 itimer
.it_value
= interval
;
1406 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1407 if (ntransmitted
== 0)
1411 if (options
& F_FLOOD
) {
1413 intvl
.tv_usec
= 10000;
1415 intvl
.tv_sec
= interval
.tv_sec
;
1416 intvl
.tv_usec
= interval
.tv_usec
;
1420 fdmasks
= howmany(s
+ 1, NFDBITS
) * sizeof(fd_mask
);
1421 if ((fdmaskp
= malloc(fdmasks
)) == NULL
)
1425 /* For control (ancillary) data received from recvmsg() */
1426 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1432 struct iovec iov
[2];
1434 /* signal handling */
1437 /* last packet sent, timeout reached? */
1438 if (npackets
&& ntransmitted
>= npackets
)
1441 if (sweepmax
&& datalen
> sweepmax
)
1459 if (options
& F_FLOOD
) {
1466 timeout
.tv_usec
= 10000;
1478 fdmaskp
[0].events
= POLLIN
;
1479 cc
= poll(fdmaskp
, 1, timeout
);
1481 memset(fdmaskp
, 0, fdmasks
);
1483 cc
= select(s
+ 1, fdmaskp
, NULL
, NULL
, tv
);
1486 if (errno
!= EINTR
) {
1495 } else if (cc
== 0) {
1498 m
.msg_name
= (caddr_t
)&from
;
1499 m
.msg_namelen
= sizeof(from
);
1500 memset(&iov
, 0, sizeof(iov
));
1501 iov
[0].iov_base
= (caddr_t
)packet
;
1502 iov
[0].iov_len
= packlen
;
1505 memset(cm
, 0, CONTROLLEN
);
1506 m
.msg_control
= (void *)cm
;
1507 m
.msg_controllen
= CONTROLLEN
;
1509 cc
= recvmsg(s
, &m
, 0);
1511 if (errno
!= EINTR
) {
1516 } else if (cc
== 0) {
1520 * receive control messages only. Process the
1521 * exceptions (currently the only possibility is
1522 * a path MTU notification.)
1524 if ((mtu
= get_pathmtu(&m
)) > 0) {
1525 if ((options
& F_VERBOSE
) != 0) {
1526 printf("new path MTU (%d) is "
1533 * an ICMPv6 message (probably an echoreply) arrived.
1535 pr_pack(packet
, cc
, &m
);
1537 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1538 (npackets
> 0 && nreceived
>= npackets
))
1540 if (ntransmitted
- nreceived
- 1 > nmissedmax
) {
1541 nmissedmax
= ntransmitted
- nreceived
- 1;
1542 if (options
& F_MISSED
)
1543 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1561 if (fdmaskp
!= NULL
)
1565 exit(nreceived
== 0 ? 2 : 0);
1590 * This routine transmits another ping6.
1595 struct itimerval itimer
;
1597 if (pinger() == 0) {
1601 * If we're not transmitting any more packets, change the timer
1602 * to wait two round-trip times if we've received any packets or
1603 * ten seconds if we haven't.
1607 itimer
.it_value
.tv_sec
= 2 * tmax
/ 1000;
1608 if (itimer
.it_value
.tv_sec
== 0)
1609 itimer
.it_value
.tv_sec
= 1;
1611 itimer
.it_value
.tv_sec
= MAXWAIT
;
1612 itimer
.it_interval
.tv_sec
= 0;
1613 itimer
.it_interval
.tv_usec
= 0;
1614 itimer
.it_value
.tv_usec
= 0;
1616 (void)signal(SIGALRM
, onsignal
);
1617 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
1622 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1623 * will be added on by the kernel. The ID field is our UNIX process ID,
1624 * and the sequence number is an ascending integer. The first 8 bytes
1625 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1626 * byte-order, to compute the round-trip time.
1633 if (options
& F_FQDN
)
1634 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1635 else if (options
& F_FQDNOLD
)
1637 else if (options
& F_NODEADDR
)
1638 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1639 else if (options
& F_SUPTYPES
)
1642 l
= ICMP6ECHOLEN
+ datalen
;
1650 struct icmp6_hdr
*icp
;
1652 struct icmp6_nodeinfo
*nip
;
1655 if (npackets
&& ntransmitted
>= npackets
)
1656 return(-1); /* no more transmission */
1658 if (sweepmax
&& sntransmitted
== snpackets
) {
1659 datalen
+= sweepincr
;
1660 if (datalen
> sweepmax
)
1661 return(-1); /* no more transmission */
1665 icp
= (struct icmp6_hdr
*)outpack
;
1666 nip
= (struct icmp6_nodeinfo
*)outpack
;
1667 memset(icp
, 0, sizeof(*icp
));
1668 icp
->icmp6_cksum
= 0;
1669 seq
= ntransmitted
++;
1670 CLR(seq
% mx_dup_ck
);
1672 if (options
& F_FQDN
) {
1673 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1674 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1675 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1676 nip
->ni_flags
= htons(0);
1678 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1679 sizeof(nip
->icmp6_ni_nonce
));
1680 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1682 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1683 sizeof(dst
.sin6_addr
));
1684 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1686 } else if (options
& F_FQDNOLD
) {
1687 /* packet format in 03 draft - no Subject data on queries */
1688 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1689 icp
->icmp6_code
= 0; /* code field is always 0 */
1690 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1691 nip
->ni_flags
= htons(0);
1693 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1694 sizeof(nip
->icmp6_ni_nonce
));
1695 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1699 } else if (options
& F_NODEADDR
) {
1700 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1701 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1702 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1703 nip
->ni_flags
= naflags
;
1705 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1706 sizeof(nip
->icmp6_ni_nonce
));
1707 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1709 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1710 sizeof(dst
.sin6_addr
));
1711 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1713 } else if (options
& F_SUPTYPES
) {
1714 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1715 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1716 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1717 /* we support compressed bitmap */
1718 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1720 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1721 sizeof(nip
->icmp6_ni_nonce
));
1722 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1726 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1727 icp
->icmp6_code
= 0;
1728 icp
->icmp6_id
= htons(ident
);
1729 icp
->icmp6_seq
= ntohs(seq
);
1730 if (datalen
>= sizeof(struct tv32
)) {
1731 /* we can time transfer */
1738 (void)gettimeofday(&tv
, NULL
);
1739 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1740 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1741 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1743 cc
= ICMP6ECHOLEN
+ datalen
;
1747 if (pingerlen() != cc
)
1748 errx(1, "internal error; length mismatch");
1751 if ((options
& F_CONNECT
)) {
1752 smsghdr
.msg_name
= NULL
;
1753 smsghdr
.msg_namelen
= 0;
1755 smsghdr
.msg_name
= (caddr_t
)&dst
;
1756 smsghdr
.msg_namelen
= sizeof(dst
);
1758 memset(&smsgiov
, 0, sizeof(smsgiov
));
1759 smsgiov
[0].iov_base
= (caddr_t
)outpack
;
1760 smsgiov
[0].iov_len
= cc
;
1761 smsghdr
.msg_iov
= smsgiov
;
1762 smsghdr
.msg_iovlen
= 1;
1764 i
= sendmsg(s
, &smsghdr
, 0);
1766 if (i
< 0 || i
!= cc
) {
1769 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1773 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1774 (void)write(STDOUT_FILENO
, &DOT
, 1);
1780 myechoreply(const struct icmp6_hdr
*icp
)
1782 if (ntohs(icp
->icmp6_id
) == ident
)
1789 mynireply(const struct icmp6_nodeinfo
*nip
)
1791 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1792 nonce
+ sizeof(u_int16_t
),
1793 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1800 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1802 /*base for compressed name*/
1806 char cresult
[NS_MAXDNAME
+ 1];
1817 if (i
== 0 || cp
!= *sp
) {
1818 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1819 return NULL
; /*result overrun*/
1825 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1826 /* DNS compression */
1830 comp
= base
+ (i
& 0x3f);
1831 if (dnsdecode(&comp
, cp
, base
, cresult
,
1832 sizeof(cresult
)) == NULL
)
1834 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1835 return NULL
; /*result overrun*/
1837 } else if ((i
& 0x3f) == i
) {
1839 return NULL
; /*source overrun*/
1840 while (i
-- > 0 && cp
< ep
) {
1841 l
= snprintf(cresult
, sizeof(cresult
),
1842 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1843 if (l
>= sizeof(cresult
) || l
< 0)
1845 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1846 return NULL
; /*result overrun*/
1850 return NULL
; /*invalid label*/
1853 return NULL
; /*not terminated*/
1861 * Print out the packet, if it came from us. This logic is necessary
1862 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1863 * which arrive ('tis only fair). This permits multiple copies of this
1864 * program to be run without having intermingled output (or statistics!).
1867 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1869 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1870 struct icmp6_hdr
*icp
;
1871 struct icmp6_nodeinfo
*ni
;
1874 struct sockaddr
*from
;
1876 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1877 struct in6_pktinfo
*pktinfo
= NULL
;
1878 struct timeval tv
, tp
;
1880 double triptime
= 0;
1885 char dnsname
[NS_MAXDNAME
+ 1];
1889 (void)gettimeofday(&tv
, NULL
);
1891 if (!mhdr
|| !mhdr
->msg_name
||
1892 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1893 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1894 if (options
& F_VERBOSE
)
1895 warnx("invalid peername");
1898 from
= (struct sockaddr
*)mhdr
->msg_name
;
1899 fromlen
= mhdr
->msg_namelen
;
1900 if (cc
< sizeof(struct icmp6_hdr
)) {
1901 if (options
& F_VERBOSE
)
1902 warnx("packet too short (%d bytes) from %s", cc
,
1903 pr_addr(from
, fromlen
));
1906 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1907 (options
& F_VERBOSE
) != 0)
1908 warnx("some control data discarded, insufficient buffer size");
1909 icp
= (struct icmp6_hdr
*)buf
;
1910 ni
= (struct icmp6_nodeinfo
*)buf
;
1913 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1914 warnx("failed to get receiving hop limit");
1917 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1918 warnx("failed to get receiving packet information");
1921 if (rcvtclass
&& (tclass
= get_tclass(mhdr
)) == -1) {
1922 warnx("failed to get receiving traffic class");
1926 if (use_recvmsg
> 0)
1927 sotc
= get_so_traffic_class(mhdr
);
1929 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1930 seq
= ntohs(icp
->icmp6_seq
);
1933 tpp
= (struct tv32
*)(icp
+ 1);
1934 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1935 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1937 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1938 ((double)tv
.tv_usec
) / 1000.0;
1940 tsumsq
+= triptime
* triptime
;
1941 if (triptime
< tmin
)
1943 if (triptime
> tmax
)
1947 if (TST(seq
% mx_dup_ck
)) {
1952 SET(seq
% mx_dup_ck
);
1956 if (options
& F_QUIET
)
1959 if (options
& F_FLOOD
)
1960 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1962 if (options
& F_AUDIBLE
)
1963 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1964 if (options
& F_PRTIME
)
1966 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1967 pr_addr(from
, fromlen
), seq
);
1968 (void)printf(" hlim=%d", hoplim
);
1969 if ((options
& F_VERBOSE
) != 0) {
1970 struct sockaddr_in6 dstsa
;
1972 memset(&dstsa
, 0, sizeof(dstsa
));
1973 dstsa
.sin6_family
= AF_INET6
;
1974 dstsa
.sin6_len
= sizeof(dstsa
);
1975 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1976 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1977 (void)printf(" dst=%s",
1978 pr_addr((struct sockaddr
*)&dstsa
,
1982 (void)printf(" time=%.3f ms", triptime
);
1984 if (!IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
1985 (void)printf("(DUP!)");
1988 (void)printf(" tclass=%d", tclass
);
1990 (void)printf(" sotc=%d", sotc
);
1991 /* check the data */
1992 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1993 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1994 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1996 (void)printf("\nwrong data byte #%d "
1997 "should be 0x%x but was 0x%x",
2003 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
2004 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
2006 if (TST(seq
% mx_dup_ck
)) {
2010 SET(seq
% mx_dup_ck
);
2013 if (options
& F_QUIET
)
2016 if (options
& F_PRTIME
)
2018 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
2020 switch (ntohs(ni
->ni_code
)) {
2021 case ICMP6_NI_SUCCESS
:
2023 case ICMP6_NI_REFUSED
:
2024 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
2026 case ICMP6_NI_UNKNOWN
:
2027 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
2030 printf("unknown code 0x%x, type 0x%x",
2031 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
2035 switch (ntohs(ni
->ni_qtype
)) {
2037 printf("NodeInfo NOOP");
2039 case NI_QTYPE_SUPTYPES
:
2040 pr_suptypes(ni
, end
- (u_char
*)ni
);
2042 case NI_QTYPE_NODEADDR
:
2043 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
2046 default: /* XXX: for backward compatibility */
2047 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
2048 if (buf
[off
+ ICMP6_NIRLEN
] ==
2049 cc
- off
- ICMP6_NIRLEN
- 1)
2054 cp
++; /* skip length */
2056 safeputc(*cp
& 0xff);
2062 if (dnsdecode((const u_char
**)&cp
, end
,
2063 (const u_char
*)(ni
+ 1), dnsname
,
2064 sizeof(dnsname
)) == NULL
) {
2069 * name-lookup special handling for
2072 if (cp
+ 1 <= end
&& !*cp
&&
2073 strlen(dnsname
) > 0) {
2074 dnsname
[strlen(dnsname
) - 1] = '\0';
2077 printf("%s%s", i
> 0 ? "," : "",
2081 if (options
& F_VERBOSE
) {
2085 (void)printf(" ("); /*)*/
2087 switch (ni
->ni_code
) {
2088 case ICMP6_NI_REFUSED
:
2089 (void)printf("refused");
2092 case ICMP6_NI_UNKNOWN
:
2093 (void)printf("unknown qtype");
2098 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
2099 /* case of refusion, unknown */
2104 ttl
= (int32_t)ntohl(*(u_int32_t
*)
2105 &buf
[off
+ICMP6ECHOLEN
+8]);
2108 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
2109 (void)printf("TTL=%d:meaningless",
2113 (void)printf("TTL=%d:invalid",
2116 (void)printf("TTL=%d", ttl
);
2126 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
2135 if (buf
[off
+ ICMP6_NIRLEN
] !=
2136 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
2139 (void)printf("invalid namelen:%d/%lu",
2140 buf
[off
+ ICMP6_NIRLEN
],
2141 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
2151 /* We've got something other than an ECHOREPLY */
2152 if (!(options
& F_VERBOSE
))
2154 if (options
& F_PRTIME
)
2156 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
2160 if (!(options
& F_FLOOD
)) {
2161 (void)putchar('\n');
2162 if (options
& F_VERBOSE
)
2164 (void)fflush(stdout
);
2170 pr_exthdrs(struct msghdr
*mhdr
)
2176 bufp
= mhdr
->msg_control
;
2177 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2178 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2179 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2182 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
2185 switch (cm
->cmsg_type
) {
2187 printf(" HbH Options: ");
2188 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
2191 #ifdef IPV6_RTHDRDSTOPTS
2192 case IPV6_RTHDRDSTOPTS
:
2194 printf(" Dst Options: ");
2195 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
2198 printf(" Routing: ");
2199 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
2205 #ifdef USE_RFC2292BIS
2207 pr_ip6opt(void *extbuf
, size_t bufsize
)
2209 struct ip6_hbh
*ext
;
2212 socklen_t extlen
, len
;
2218 ext
= (struct ip6_hbh
*)extbuf
;
2219 extlen
= (ext
->ip6h_len
+ 1) * 8;
2220 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
2221 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
2224 * Bounds checking on the ancillary data buffer:
2225 * subtract the size of a cmsg structure from the buffer size.
2227 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
2228 extlen
= bufsize
- CMSG_SPACE(0);
2229 warnx("options truncated, showing only %u (total=%u)",
2230 (unsigned int)(extlen
/ 8 - 1),
2231 (unsigned int)(ext
->ip6h_len
));
2236 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
2237 &type
, &len
, &databuf
);
2238 if (currentlen
== -1)
2242 * Note that inet6_opt_next automatically skips any padding
2247 (void) inet6_opt_get_val(databuf
, offset
,
2248 &value4
, sizeof(value4
));
2249 printf(" Jumbo Payload Opt: Length %u\n",
2250 (u_int32_t
)ntohl(value4
));
2252 case IP6OPT_ROUTER_ALERT
:
2254 (void)inet6_opt_get_val(databuf
, offset
,
2255 &value2
, sizeof(value2
));
2256 printf(" Router Alert Opt: Type %u\n",
2260 printf(" Received Opt %u len %lu\n",
2261 type
, (unsigned long)len
);
2267 #else /* !USE_RFC2292BIS */
2270 pr_ip6opt(void *extbuf
, size_t bufsize __unused
)
2275 #endif /* USE_RFC2292BIS */
2277 #ifdef USE_RFC2292BIS
2279 pr_rthdr(void *extbuf
, size_t bufsize
)
2281 struct in6_addr
*in6
;
2282 char ntopbuf
[INET6_ADDRSTRLEN
];
2283 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
2284 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
2286 /* print fixed part of the header */
2287 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
2288 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
2289 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
2290 printf("%d segments, ", segments
);
2291 printf("%d left\n", rh
->ip6r_segleft
);
2293 printf("segments unknown, ");
2294 printf("%d left\n", rh
->ip6r_segleft
);
2299 * Bounds checking on the ancillary data buffer. When calculating
2300 * the number of items to show keep in mind:
2301 * - The size of the cmsg structure
2302 * - The size of one segment (the size of a Type 0 routing header)
2303 * - When dividing add a fudge factor of one in case the
2304 * dividend is not evenly divisible by the divisor
2306 rthsize
= (rh
->ip6r_len
+ 1) * 8;
2307 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
2308 origsegs
= segments
;
2309 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
2310 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
2311 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
2312 (size1
- size0
) + 1;
2313 warnx("segments truncated, showing only %d (total=%d)",
2314 segments
, origsegs
);
2317 for (i
= 0; i
< segments
; i
++) {
2318 in6
= inet6_rth_getaddr(extbuf
, i
);
2320 printf(" [%d]<NULL>\n", i
);
2322 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
2324 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
2325 printf(" [%d]%s\n", i
, ntopbuf
);
2333 #else /* !USE_RFC2292BIS */
2336 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
2341 #endif /* USE_RFC2292BIS */
2344 pr_bitrange(u_int32_t v
, int soff
, int ii
)
2351 /* shift till we have 0x01 */
2352 if ((v
& 0x01) == 0) {
2354 printf("-%u", soff
+ off
- 1);
2365 case 0x04: case 0x0c:
2376 /* we have 0x01 with us */
2377 for (i
= 0; i
< 32 - off
; i
++) {
2378 if ((v
& (0x01 << i
)) == 0)
2382 printf(" %u", soff
+ off
);
2390 pr_suptypes(struct icmp6_nodeinfo
*ni
, size_t nilen
)
2391 /* ni->qtype must be SUPTYPES */
2395 const u_char
*cp
, *end
;
2398 u_int16_t words
; /*32bit count*/
2401 #define MAXQTYPES (1 << 16)
2405 cp
= (u_char
*)(ni
+ 1);
2406 end
= ((u_char
*)ni
) + nilen
;
2410 printf("NodeInfo Supported Qtypes");
2411 if (options
& F_VERBOSE
) {
2412 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
2413 printf(", compressed bitmap");
2415 printf(", raw bitmap");
2419 clen
= (size_t)(end
- cp
);
2420 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
2421 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
2427 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
2429 memcpy(&cbit
, cp
, sizeof(cbit
));
2430 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
2434 clen
= ntohs(cbit
.words
) * sizeof(v
);
2435 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
2440 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
2441 memcpy(&v
, cp
+ off
, sizeof(v
));
2442 v
= (u_int32_t
)ntohl(v
);
2443 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
2445 /* flush the remaining bits */
2446 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
2450 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
2451 cur
+= ntohs(cbit
.skip
) * 32;
2456 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, int nilen
)
2457 /* ni->qtype must be NODEADDR */
2459 u_char
*cp
= (u_char
*)(ni
+ 1);
2460 char ntop_buf
[INET6_ADDRSTRLEN
];
2463 nilen
-= sizeof(struct icmp6_nodeinfo
);
2465 if (options
& F_VERBOSE
) {
2466 switch (ni
->ni_code
) {
2467 case ICMP6_NI_REFUSED
:
2468 (void)printf("refused");
2470 case ICMP6_NI_UNKNOWN
:
2471 (void)printf("unknown qtype");
2474 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2475 (void)printf(" truncated");
2479 printf(" no address\n");
2482 * In icmp-name-lookups 05 and later, TTL of each returned address
2483 * is contained in the resposne. We try to detect the version
2484 * by the length of the data, but note that the detection algorithm
2485 * is incomplete. We assume the latest draft by default.
2487 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2493 /* XXX: alignment? */
2494 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2495 cp
+= sizeof(u_int32_t
);
2496 nilen
-= sizeof(u_int32_t
);
2499 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2501 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2502 printf(" %s", ntop_buf
);
2504 if (ttl
== 0xffffffff) {
2506 * XXX: can this convention be applied to all
2507 * type of TTL (i.e. non-ND TTL)?
2509 printf("(TTL=infty)");
2512 printf("(TTL=%u)", ttl
);
2516 nilen
-= sizeof(struct in6_addr
);
2517 cp
+= sizeof(struct in6_addr
);
2522 get_hoplim(struct msghdr
*mhdr
)
2526 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2527 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2528 if (cm
->cmsg_len
== 0)
2531 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2532 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2533 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2534 return(*(int *)CMSG_DATA(cm
));
2540 struct in6_pktinfo
*
2541 get_rcvpktinfo(struct msghdr
*mhdr
)
2545 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2546 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2547 if (cm
->cmsg_len
== 0)
2550 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2551 cm
->cmsg_type
== IPV6_PKTINFO
&&
2552 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2553 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2560 get_pathmtu(struct msghdr
*mhdr
)
2562 #ifdef IPV6_RECVPATHMTU
2564 struct ip6_mtuinfo
*mtuctl
= NULL
;
2566 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2567 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2568 if (cm
->cmsg_len
== 0)
2571 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2572 cm
->cmsg_type
== IPV6_PATHMTU
&&
2573 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2574 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2577 * If the notified destination is different from
2578 * the one we are pinging, just ignore the info.
2579 * We check the scope ID only when both notified value
2580 * and our own value have non-0 values, because we may
2581 * have used the default scope zone ID for sending,
2582 * in which case the scope ID value is 0.
2584 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2586 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2587 dst
.sin6_scope_id
&&
2588 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2589 dst
.sin6_scope_id
)) {
2590 if ((options
& F_VERBOSE
) != 0) {
2591 printf("path MTU for %s is notified. "
2593 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2594 sizeof(mtuctl
->ip6m_addr
)));
2600 * Ignore an invalid MTU. XXX: can we just believe
2603 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2606 /* notification for our destination. return the MTU. */
2607 return((int)mtuctl
->ip6m_mtu
);
2616 struct msghdr
*mhdr
;
2620 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2621 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2622 if (cm
->cmsg_len
== 0)
2625 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2626 cm
->cmsg_type
== IPV6_TCLASS
&&
2627 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2628 return(*(int *)CMSG_DATA(cm
));
2635 get_so_traffic_class(struct msghdr
*mhdr
)
2639 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2640 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2641 if (cm
->cmsg_len
== 0)
2644 if (cm
->cmsg_level
== SOL_SOCKET
&&
2645 cm
->cmsg_type
== SO_TRAFFIC_CLASS
&&
2646 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2647 return(*(int *)CMSG_DATA(cm
));
2655 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2659 tvsub(struct timeval
*out
, struct timeval
*in
)
2661 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2663 out
->tv_usec
+= 1000000;
2665 out
->tv_sec
-= in
->tv_sec
;
2674 onint(int notused __unused
)
2691 if (fdmaskp
!= NULL
)
2695 (void)signal(SIGINT
, SIG_DFL
);
2696 (void)kill(getpid(), SIGINT
);
2704 * Print out statistics.
2709 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2710 (void)printf("%ld packets transmitted, ", ntransmitted
);
2711 (void)printf("%ld packets received, ", nreceived
);
2713 (void)printf("+%ld duplicates, ", nrepeats
);
2715 if (nreceived
> ntransmitted
)
2716 (void)printf("-- somebody's duplicating packets!");
2718 (void)printf("%.1f%% packet loss",
2719 ((((double)ntransmitted
- nreceived
) * 100.0) /
2722 (void)putchar('\n');
2723 if (nreceived
&& timing
) {
2724 /* Only display average to microseconds */
2725 double num
= nreceived
+ nrepeats
;
2726 double avg
= tsum
/ num
;
2727 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2729 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2730 tmin
, avg
, tmax
, dev
);
2731 (void)fflush(stdout
);
2733 (void)fflush(stdout
);
2737 static const char *niqcode
[] = {
2739 "DNS label", /*or empty*/
2744 static const char *nircode
[] = {
2745 "Success", "Refused", "Unknown",
2751 * Print a descriptive string about an ICMP header.
2754 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2756 char ntop_buf
[INET6_ADDRSTRLEN
];
2757 struct nd_redirect
*red
;
2758 struct icmp6_nodeinfo
*ni
;
2759 char dnsname
[NS_MAXDNAME
+ 1];
2763 switch (icp
->icmp6_type
) {
2764 case ICMP6_DST_UNREACH
:
2765 switch (icp
->icmp6_code
) {
2766 case ICMP6_DST_UNREACH_NOROUTE
:
2767 (void)printf("No Route to Destination\n");
2769 case ICMP6_DST_UNREACH_ADMIN
:
2770 (void)printf("Destination Administratively "
2773 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2774 (void)printf("Destination Unreachable Beyond Scope\n");
2776 case ICMP6_DST_UNREACH_ADDR
:
2777 (void)printf("Destination Host Unreachable\n");
2779 case ICMP6_DST_UNREACH_NOPORT
:
2780 (void)printf("Destination Port Unreachable\n");
2783 (void)printf("Destination Unreachable, Bad Code: %d\n",
2787 /* Print returned IP header information */
2788 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2790 case ICMP6_PACKET_TOO_BIG
:
2791 (void)printf("Packet too big mtu = %d\n",
2792 (int)ntohl(icp
->icmp6_mtu
));
2793 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2795 case ICMP6_TIME_EXCEEDED
:
2796 switch (icp
->icmp6_code
) {
2797 case ICMP6_TIME_EXCEED_TRANSIT
:
2798 (void)printf("Time to live exceeded\n");
2800 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2801 (void)printf("Frag reassembly time exceeded\n");
2804 (void)printf("Time exceeded, Bad Code: %d\n",
2808 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2810 case ICMP6_PARAM_PROB
:
2811 (void)printf("Parameter problem: ");
2812 switch (icp
->icmp6_code
) {
2813 case ICMP6_PARAMPROB_HEADER
:
2814 (void)printf("Erroneous Header ");
2816 case ICMP6_PARAMPROB_NEXTHEADER
:
2817 (void)printf("Unknown Nextheader ");
2819 case ICMP6_PARAMPROB_OPTION
:
2820 (void)printf("Unrecognized Option ");
2823 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2826 (void)printf("pointer = 0x%02x\n",
2827 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2828 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2830 case ICMP6_ECHO_REQUEST
:
2831 (void)printf("Echo Request");
2832 /* XXX ID + Seq + Data */
2834 case ICMP6_ECHO_REPLY
:
2835 (void)printf("Echo Reply");
2836 /* XXX ID + Seq + Data */
2838 case ICMP6_MEMBERSHIP_QUERY
:
2839 (void)printf("Listener Query");
2841 case ICMP6_MEMBERSHIP_REPORT
:
2842 (void)printf("Listener Report");
2844 case ICMP6_MEMBERSHIP_REDUCTION
:
2845 (void)printf("Listener Done");
2847 case ND_ROUTER_SOLICIT
:
2848 (void)printf("Router Solicitation");
2850 case ND_ROUTER_ADVERT
:
2851 (void)printf("Router Advertisement");
2853 case ND_NEIGHBOR_SOLICIT
:
2854 (void)printf("Neighbor Solicitation");
2856 case ND_NEIGHBOR_ADVERT
:
2857 (void)printf("Neighbor Advertisement");
2860 red
= (struct nd_redirect
*)icp
;
2861 (void)printf("Redirect\n");
2862 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2864 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2865 (void)printf("Destination: %s", ntop_buf
);
2866 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2868 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2869 (void)printf(" New Target: %s", ntop_buf
);
2871 case ICMP6_NI_QUERY
:
2872 (void)printf("Node Information Query");
2873 /* XXX ID + Seq + Data */
2874 ni
= (struct icmp6_nodeinfo
*)icp
;
2875 l
= end
- (u_char
*)(ni
+ 1);
2877 switch (ntohs(ni
->ni_qtype
)) {
2879 (void)printf("NOOP");
2881 case NI_QTYPE_SUPTYPES
:
2882 (void)printf("Supported qtypes");
2885 (void)printf("DNS name");
2887 case NI_QTYPE_NODEADDR
:
2888 (void)printf("nodeaddr");
2890 case NI_QTYPE_IPV4ADDR
:
2891 (void)printf("IPv4 nodeaddr");
2894 (void)printf("unknown qtype");
2897 if (options
& F_VERBOSE
) {
2898 switch (ni
->ni_code
) {
2899 case ICMP6_NI_SUBJ_IPV6
:
2900 if (l
== sizeof(struct in6_addr
) &&
2901 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2902 sizeof(ntop_buf
)) != NULL
) {
2903 (void)printf(", subject=%s(%s)",
2904 niqcode
[ni
->ni_code
], ntop_buf
);
2907 /* backward compat to -W */
2908 (void)printf(", oldfqdn");
2910 (void)printf(", invalid");
2914 case ICMP6_NI_SUBJ_FQDN
:
2915 if (end
== (u_char
*)(ni
+ 1)) {
2916 (void)printf(", no subject");
2919 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2920 cp
= (const u_char
*)(ni
+ 1);
2921 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2922 sizeof(dnsname
)) != NULL
)
2923 printf("(%s)", dnsname
);
2925 printf("(invalid)");
2927 case ICMP6_NI_SUBJ_IPV4
:
2928 if (l
== sizeof(struct in_addr
) &&
2929 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2930 sizeof(ntop_buf
)) != NULL
) {
2931 (void)printf(", subject=%s(%s)",
2932 niqcode
[ni
->ni_code
], ntop_buf
);
2934 (void)printf(", invalid");
2937 (void)printf(", invalid");
2942 case ICMP6_NI_REPLY
:
2943 (void)printf("Node Information Reply");
2944 /* XXX ID + Seq + Data */
2945 ni
= (struct icmp6_nodeinfo
*)icp
;
2947 switch (ntohs(ni
->ni_qtype
)) {
2949 (void)printf("NOOP");
2951 case NI_QTYPE_SUPTYPES
:
2952 (void)printf("Supported qtypes");
2955 (void)printf("DNS name");
2957 case NI_QTYPE_NODEADDR
:
2958 (void)printf("nodeaddr");
2960 case NI_QTYPE_IPV4ADDR
:
2961 (void)printf("IPv4 nodeaddr");
2964 (void)printf("unknown qtype");
2967 if (options
& F_VERBOSE
) {
2968 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2969 printf(", invalid");
2971 printf(", %s", nircode
[ni
->ni_code
]);
2975 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2981 * Print an IP6 header.
2984 pr_iph(struct ip6_hdr
*ip6
)
2986 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2988 char ntop_buf
[INET6_ADDRSTRLEN
];
2990 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2991 tc
= (tc
>> 4) & 0x0f;
2992 tc
|= (ip6
->ip6_vfc
<< 4);
2994 printf("Vr TC Flow Plen Nxt Hlim\n");
2995 printf(" %1x %02x %05x %04x %02x %02x\n",
2996 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2997 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2998 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2999 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
3000 printf("%s->", ntop_buf
);
3001 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
3002 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
3003 printf("%s\n", ntop_buf
);
3008 * Return an ascii host address as a dotted quad and optionally with
3012 pr_addr(struct sockaddr
*addr
, int addrlen
)
3014 static char buf
[NI_MAXHOST
];
3017 if ((options
& F_HOSTNAME
) == 0)
3018 flag
|= NI_NUMERICHOST
;
3020 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
3028 * Dump some info on a returned (via ICMPv6) IPv6 packet.
3031 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
3033 u_char
*cp
= (u_char
*)ip6
, nh
;
3036 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
3041 hlen
= sizeof(*ip6
);
3045 while (end
- cp
>= 8) {
3047 case IPPROTO_HOPOPTS
:
3049 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
3050 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
3052 case IPPROTO_DSTOPTS
:
3054 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
3055 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
3057 case IPPROTO_FRAGMENT
:
3059 hlen
= sizeof(struct ip6_frag
);
3060 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
3062 case IPPROTO_ROUTING
:
3064 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
3065 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
3070 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
3071 nh
= ((struct ah
*)cp
)->ah_nxt
;
3074 case IPPROTO_ICMPV6
:
3075 printf("ICMP6: type = %d, code = %d\n",
3082 printf("TCP: from port %u, to port %u (decimal)\n",
3083 (*cp
* 256 + *(cp
+ 1)),
3084 (*(cp
+ 2) * 256 + *(cp
+ 3)));
3087 printf("UDP: from port %u, to port %u (decimal)\n",
3088 (*cp
* 256 + *(cp
+ 1)),
3089 (*(cp
+ 2) * 256 + *(cp
+ 3)));
3092 printf("Unknown Header(%d)\n", nh
);
3096 if ((cp
+= hlen
) >= end
)
3111 fill(char *bp
, char *patp
)
3117 for (cp
= patp
; *cp
; cp
++)
3119 errx(1, "patterns must be specified as hex digits");
3121 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
3122 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
3123 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
3124 &pat
[13], &pat
[14], &pat
[15]);
3129 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
3131 for (jj
= 0; jj
< ii
; ++jj
)
3132 bp
[jj
+ kk
] = pat
[jj
];
3133 if (!(options
& F_QUIET
)) {
3134 (void)printf("PATTERN: 0x");
3135 for (jj
= 0; jj
< ii
; ++jj
)
3136 (void)printf("%02x", bp
[jj
] & 0xFF);
3142 #ifdef IPSEC_POLICY_IPSEC
3144 setpolicy(int so __unused
, char *policy
)
3149 return 0; /* ignore */
3151 buf
= ipsec_set_policy(policy
, strlen(policy
));
3153 errx(1, "%s", ipsec_strerror());
3154 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
3155 ipsec_get_policylen(buf
)) < 0)
3156 warnx("Unable to set IPsec policy");
3170 u_int8_t digest
[16];
3173 char hbuf
[NI_MAXHOST
];
3174 struct in6_addr in6
;
3176 p
= strchr(name
, '.');
3178 p
= name
+ strlen(name
);
3180 if (l
> 63 || l
> sizeof(hbuf
) - 1)
3181 return NULL
; /*label too long*/
3182 strncpy(hbuf
, name
, l
);
3183 hbuf
[(int)l
] = '\0';
3185 for (q
= name
; *q
; q
++) {
3186 if (isupper(*(unsigned char *)q
))
3187 *q
= tolower(*(unsigned char *)q
);
3190 /* generate 8 bytes of pseudo-random value. */
3191 memset(&ctxt
, 0, sizeof(ctxt
));
3194 MD5Update(&ctxt
, &c
, sizeof(c
));
3195 MD5Update(&ctxt
, (unsigned char *)name
, l
);
3196 MD5Final(digest
, &ctxt
);
3198 if (inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
) != 1)
3199 return NULL
; /*XXX*/
3200 bcopy(digest
, &in6
.s6_addr
[12], 4);
3202 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
3205 return strdup(hbuf
);
3209 str2sotc(const char *str
, bool *valid
)
3216 if (str
== NULL
|| *str
== '\0')
3218 else if (strcasecmp(str
, "BK_SYS") == 0)
3219 return SO_TC_BK_SYS
;
3220 else if (strcasecmp(str
, "BK") == 0)
3222 else if (strcasecmp(str
, "BE") == 0)
3224 else if (strcasecmp(str
, "RD") == 0)
3226 else if (strcasecmp(str
, "OAM") == 0)
3228 else if (strcasecmp(str
, "AV") == 0)
3230 else if (strcasecmp(str
, "RV") == 0)
3232 else if (strcasecmp(str
, "VI") == 0)
3234 else if (strcasecmp(str
, "VO") == 0)
3236 else if (strcasecmp(str
, "CTL") == 0)
3239 sotc
= (int)strtol(str
, &endptr
, 0);
3240 if (*endptr
!= '\0')
3247 str2netservicetype(const char *str
, bool *valid
)
3254 if (str
== NULL
|| *str
== '\0')
3256 else if (strcasecmp(str
, "BK") == 0)
3257 return NET_SERVICE_TYPE_BK
;
3258 else if (strcasecmp(str
, "BE") == 0)
3259 return NET_SERVICE_TYPE_BE
;
3260 else if (strcasecmp(str
, "VI") == 0)
3261 return NET_SERVICE_TYPE_VI
;
3262 else if (strcasecmp(str
, "SIG") == 0)
3263 return NET_SERVICE_TYPE_SIG
;
3264 else if (strcasecmp(str
, "VO") == 0)
3265 return NET_SERVICE_TYPE_VO
;
3266 else if (strcasecmp(str
, "RV") == 0)
3267 return NET_SERVICE_TYPE_RV
;
3268 else if (strcasecmp(str
, "AV") == 0)
3269 return NET_SERVICE_TYPE_AV
;
3270 else if (strcasecmp(str
, "OAM") == 0)
3271 return NET_SERVICE_TYPE_OAM
;
3272 else if (strcasecmp(str
, "RD") == 0)
3273 return NET_SERVICE_TYPE_RD
;
3275 svc
= (int)strtol(str
, &endptr
, 0);
3276 if (*endptr
!= '\0')
3283 str2tclass(const char *str
, bool *valid
)
3290 if (str
== NULL
|| *str
== '\0')
3292 else if (strcasecmp(str
, "DF") == 0)
3294 else if (strcasecmp(str
, "EF") == 0)
3296 else if (strcasecmp(str
, "VA") == 0)
3299 else if (strcasecmp(str
, "CS0") == 0)
3301 else if (strcasecmp(str
, "CS1") == 0)
3303 else if (strcasecmp(str
, "CS2") == 0)
3305 else if (strcasecmp(str
, "CS3") == 0)
3307 else if (strcasecmp(str
, "CS4") == 0)
3309 else if (strcasecmp(str
, "CS5") == 0)
3311 else if (strcasecmp(str
, "CS6") == 0)
3313 else if (strcasecmp(str
, "CS7") == 0)
3316 else if (strcasecmp(str
, "AF11") == 0)
3318 else if (strcasecmp(str
, "AF12") == 0)
3320 else if (strcasecmp(str
, "AF13") == 0)
3322 else if (strcasecmp(str
, "AF21") == 0)
3324 else if (strcasecmp(str
, "AF22") == 0)
3326 else if (strcasecmp(str
, "AF23") == 0)
3328 else if (strcasecmp(str
, "AF31") == 0)
3330 else if (strcasecmp(str
, "AF32") == 0)
3332 else if (strcasecmp(str
, "AF33") == 0)
3334 else if (strcasecmp(str
, "AF41") == 0)
3336 else if (strcasecmp(str
, "AF42") == 0)
3338 else if (strcasecmp(str
, "AF43") == 0)
3342 unsigned long val
= strtoul(str
, &endptr
, 0);
3343 if (*endptr
!= '\0' || val
> 255)
3346 return ((u_int8_t
)val
);
3348 /* DSCP occupies the 6 upper bits of the traffic class field */
3353 pr_currenttime(void)
3358 gettimeofday(&tv
, NULL
);
3360 s
= (tv
.tv_sec
+ thiszone
) % 86400;
3361 printf("%02d:%02d:%02d.%06u ", s
/ 3600, (s
% 3600) / 60, s
% 60,
3362 (u_int32_t
)tv
.tv_usec
);
3368 (void)fprintf(stderr
,
3369 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3374 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3378 #ifdef IPV6_USE_MIN_MTU
3382 "[-a addrtype] [-b bufsiz] [-c count]\n"
3383 " [-g gateway] [-h hoplimit] [-I interface] [-i wait] [-l preload]"
3384 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
3388 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
3389 "[-k traffic_class] [-K net_service_type] "
3390 "[hops ...] host\n");
3391 (void)fprintf(stderr
, "Apple specific options (to be specified before hops or host like all options)\n");
3392 (void)fprintf(stderr
, " -b boundif # bind the socket to the interface\n");
3393 (void)fprintf(stderr
, " -k traffic_class # set traffic class socket option\n");
3394 (void)fprintf(stderr
, " -K net_service_type # set traffic class socket options\n");
3395 (void)fprintf(stderr
, " -apple-connect # call connect(2) in the socket\n");
3396 (void)fprintf(stderr
, " -apple-time # display current time\n");
3397 (void)fprintf(stderr
, " -apple-progress # show progress for debugging\n");