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>
151 #include <sysexits.h>
155 #include <netinet6/ah.h>
156 #include <netinet6/ipsec.h>
166 #define MAXPACKETLEN 131072
168 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
169 #define ICMP6ECHOTMLEN sizeof(struct tv32)
170 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
171 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
172 /* FQDN case, 64 bits of nonce + 32 bits ttl */
173 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
174 #define EXTRA 256 /* for AH and various other headers. weird. */
175 #define DEFDATALEN ICMP6ECHOTMLEN
176 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
177 #define NROUTES 9 /* number of record route slots */
178 #define MAXWAIT 10000 /* max ms to wait for response */
179 #define MAXALARM (60 * 60) /* max seconds for alarm timeout */
181 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
182 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
183 #define SET(bit) (A(bit) |= B(bit))
184 #define CLR(bit) (A(bit) &= (~B(bit)))
185 #define TST(bit) (A(bit) & B(bit))
187 #define F_FLOOD 0x0001
188 #define F_INTERVAL 0x0002
189 #define F_PINGFILLED 0x0008
190 #define F_QUIET 0x0010
191 #define F_RROUTE 0x0020
192 #define F_SO_DEBUG 0x0040
193 #define F_PRTIME 0x0080
194 #define F_VERBOSE 0x0100
196 #ifdef IPSEC_POLICY_IPSEC
197 #define F_POLICY 0x0400
199 #define F_AUTHHDR 0x0200
200 #define F_ENCRYPT 0x0400
201 #endif /*IPSEC_POLICY_IPSEC*/
203 #define F_NODEADDR 0x0800
204 #define F_FQDN 0x1000
205 #define F_INTERFACE 0x2000
206 #define F_SRCADDR 0x4000
207 #define F_HOSTNAME 0x10000
208 #define F_FQDNOLD 0x20000
209 #define F_NIGROUP 0x40000
210 #define F_SUPTYPES 0x80000
211 #define F_NOMINMTU 0x100000
212 #define F_ONCE 0x200000
213 #define F_AUDIBLE 0x400000
214 #define F_MISSED 0x800000
215 #define F_DONTFRAG 0x1000000
216 #define F_SWEEP 0x2000000
217 #define F_CONNECT 0x4000000
218 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
219 #define F_WAITTIME 0x8000000
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
;
272 long nmissedmax
; /* max value of ntransmitted - nreceived - 1 */
273 long npackets
; /* max packets to transmit */
274 long nreceived
; /* # of packets we got back */
275 long nrepeats
; /* number of duplicates */
276 long ntransmitted
; /* sequence # for outbound packets = #sent */
277 static int interval
= 1000; /* interval between packets in ms */
278 static int waittime
= MAXWAIT
; /* timeout for each packet */
279 static long nrcvtimeout
= 0; /* # of packets we got back after waittime */
281 long snpackets
= 0; /* max packets to transmit in one sweep */
282 long sntransmitted
= 0; /* # of packets we sent in this sweep */
283 int sweepmax
= 0; /* max value of payload in sweep */
284 int sweepmin
= 0; /* start value of payload in sweep */
285 int sweepincr
= 1; /* payload increment in sweep */
288 int timing
; /* flag to do timing */
289 double tmin
= 999999999.0; /* minimum round trip time */
290 double tmax
= 0.0; /* maximum round trip time */
291 double tsum
= 0.0; /* sum of all times, for doing average */
292 double tsumsq
= 0.0; /* sum of all times squared, for std. dev. */
294 /* for node addresses */
297 /* for ancillary data(advanced API) */
298 struct msghdr smsghdr
;
299 struct iovec smsgiov
[2];
302 volatile sig_atomic_t seenalrm
;
303 volatile sig_atomic_t seenint
;
305 volatile sig_atomic_t seeninfo
;
312 int so_traffic_class
= SO_TC_CTL
; /* use control class, by default */
313 int net_service_type
= -1;
315 int32_t thiszone
; /* seconds offset from gmt to local time */
316 extern int32_t gmt2local(time_t);
317 static void pr_currenttime(void);
319 int main(int, char *[]);
320 void fill(char *, char *);
321 int get_hoplim(struct msghdr
*);
322 int get_pathmtu(struct msghdr
*);
323 int get_tclass(struct msghdr
*);
324 int get_so_traffic_class(struct msghdr
*);
325 struct in6_pktinfo
*get_rcvpktinfo(struct msghdr
*);
328 size_t pingerlen(void);
330 const char *pr_addr(struct sockaddr
*, int);
331 void pr_icmph(struct icmp6_hdr
*, u_char
*);
332 void pr_iph(struct ip6_hdr
*);
333 void pr_suptypes(struct icmp6_nodeinfo
*, size_t);
334 void pr_nodeaddr(struct icmp6_nodeinfo
*, int);
335 int myechoreply(const struct icmp6_hdr
*);
336 int mynireply(const struct icmp6_nodeinfo
*);
337 char *dnsdecode(const u_char
**, const u_char
*, const u_char
*,
339 void pr_pack(u_char
*, int, struct msghdr
*);
340 void pr_exthdrs(struct msghdr
*);
341 void pr_ip6opt(void *, size_t);
342 void pr_rthdr(void *, size_t);
343 int pr_bitrange(u_int32_t
, int, int);
344 void pr_retip(struct ip6_hdr
*, u_char
*);
346 void tvsub(struct timeval
*, struct timeval
*);
347 int setpolicy(int, char *);
348 char *nigroup(char *, int);
349 static int str2sotc(const char *, bool *);
350 static int str2netservicetype(const char *, bool *);
351 static u_int8_t
str2tclass(const char *, bool *);
355 main(int argc
, char *argv
[])
357 struct timeval last
, intvl
;
358 struct sockaddr_in6 from
;
359 struct addrinfo hints
;
361 int almost_done
, ch
, hold
, packlen
, preload
, optval
, ret_ga
;
362 int nig_oldmcprefix
= -1;
364 char *e
, *target
, *ifname
= NULL
, *gateway
= NULL
;
366 struct cmsghdr
*scmsgp
= NULL
;
367 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
372 struct in6_pktinfo
*pktinfo
= NULL
;
373 #ifdef USE_RFC2292BIS
374 struct ip6_rthdr
*rthdr
= NULL
;
376 #ifdef IPSEC_POLICY_IPSEC
377 char *policy_in
= NULL
;
378 char *policy_out
= NULL
;
383 #ifdef IPV6_USE_MIN_MTU
386 /* T_CLASS value -1 means default, so -2 means do not bother */
390 /* just to be sure */
391 memset(&smsghdr
, 0, sizeof(smsghdr
));
392 memset(&smsgiov
, 0, sizeof(smsgiov
));
395 datap
= &outpack
[ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
];
399 #ifdef IPSEC_POLICY_IPSEC
403 #endif /*IPSEC_POLICY_IPSEC*/
405 while ((ch
= getopt_long(argc
, argv
,
406 "a:b:B:Cc:DdfHG:g:h:I:i:k:K:l:mnNop:qrRS:s:tvwWz:" ADDOPTS
,
407 longopts
, NULL
)) != -1) {
414 options
&= ~F_NOUSERDATA
;
415 options
|= F_NODEADDR
;
416 for (cp
= optarg
; *cp
!= '\0'; cp
++) {
419 naflags
|= NI_NODEADDR_FLAG_ALL
;
423 naflags
|= NI_NODEADDR_FLAG_COMPAT
;
427 naflags
|= NI_NODEADDR_FLAG_LINKLOCAL
;
431 naflags
|= NI_NODEADDR_FLAG_SITELOCAL
;
435 naflags
|= NI_NODEADDR_FLAG_GLOBAL
;
437 case 'A': /* experimental. not in the spec */
438 #ifdef NI_NODEADDR_FLAG_ANYCAST
439 naflags
|= NI_NODEADDR_FLAG_ANYCAST
;
442 errx(1, "-a A is not supported on "
454 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
457 lsockbufsize
= strtoul(optarg
, &e
, 10);
458 sockbufsize
= lsockbufsize
;
459 if (errno
|| !*optarg
|| *e
||
460 sockbufsize
!= lsockbufsize
)
461 errx(1, "invalid socket buffer size");
463 errx(1, "-b option ignored: SO_SNDBUF/SO_RCVBUF "
464 "socket options not supported");
474 npackets
= strtol(optarg
, &e
, 10);
475 if (npackets
<= 0 || *optarg
== '\0' || *e
!= '\0')
477 "illegal number of packets -- %s", optarg
);
480 options
|= F_DONTFRAG
;
483 options
|= F_SO_DEBUG
;
488 errx(1, "Must be superuser to flood ping");
491 setbuf(stdout
, (char *)NULL
);
501 tofree
= strdup(optarg
);
503 errx(1, "### strdup() failed");
509 if ((str
= strsep(&ptr
, ",")) == NULL
)
510 errx(1, "-G requires maximum packet size");
511 ultmp
= strtoul(str
, &ep
, 0);
512 if (*ep
|| ep
== optarg
)
513 errx(EX_USAGE
, "option -G invalid maximum packet size: `%s'",
517 if (sweepmax
< 1 || sweepmax
> MAXDATALEN
) {
519 "-G invalid maximum packet size, needs to be between 1 and %d",
523 if ((str
= strsep(&ptr
, ",")) == NULL
)
526 ultmp
= strtoul(str
, &ep
, 0);
527 if (*ep
|| ep
== optarg
)
528 errx(EX_USAGE
, "option -G invalid minimum packet size: `%s'",
531 if (sweepmin
< 0 || sweepmin
> MAXDATALEN
) {
533 "-G invalid minimum packet size, needs to be between 0 and %d",
538 if ((str
= strsep(&ptr
, ",")) == NULL
)
542 ultmp
= strtoul(str
, &ep
, 0);
543 if (*ep
|| ep
== optarg
)
544 errx(EX_USAGE
, "option -G invalid sweep increment size: `%s'",
547 if (sweepincr
< 1 || sweepincr
> MAXDATALEN
) {
549 "-G invalid sweep increment size, needs to be between 1 and %d",
557 options
|= F_HOSTNAME
;
559 case 'h': /* hoplimit */
560 hoplimit
= strtol(optarg
, &e
, 10);
561 if (*optarg
== '\0' || *e
!= '\0')
562 errx(1, "illegal hoplimit %s", optarg
);
563 if (255 < hoplimit
|| hoplimit
< -1)
565 "illegal hoplimit -- %s", optarg
);
569 options
|= F_INTERFACE
;
570 #ifndef USE_SIN6_SCOPE_ID
574 case 'i': /* wait between sending packets */
575 t
= strtod(optarg
, &e
);
576 if (*optarg
== '\0' || *e
!= '\0')
577 errx(1, "illegal timing interval %s", optarg
);
578 if (t
< 1 && getuid()) {
579 errx(1, "%s: only root may use interval < 1s",
582 intvl
.tv_sec
= (long)t
;
584 (long)((t
- intvl
.tv_sec
) * 1000000);
585 if (intvl
.tv_sec
< 0)
586 errx(1, "illegal timing interval %s", optarg
);
587 /* less than 1/hz does not make sense */
588 if (intvl
.tv_sec
== 0 && intvl
.tv_usec
< 1) {
589 warnx("too small interval, raised to .000001");
592 options
|= F_INTERVAL
;
595 if (strcasecmp(optarg
, "sendmsg") == 0) {
599 if (strcasecmp(optarg
, "recvmsg") == 0) {
603 so_traffic_class
= str2sotc(optarg
, &valid
);
605 errx(EX_USAGE
, "bad traffic class: `%s'",
609 if (strcasecmp(optarg
, "sendmsg") == 0) {
613 net_service_type
= str2netservicetype(optarg
, &valid
);
615 errx(EX_USAGE
, "bad network service type: `%s'",
617 /* suppress default traffic class (-k can still be specified after -K) */
618 so_traffic_class
= -1;
623 errx(1, "Must be superuser to preload");
625 preload
= strtol(optarg
, &e
, 10);
626 if (preload
< 0 || *optarg
== '\0' || *e
!= '\0')
627 errx(1, "illegal preload value -- %s", optarg
);
630 #ifdef IPV6_USE_MIN_MTU
634 errx(1, "-%c is not supported on this platform", ch
);
638 options
&= ~F_HOSTNAME
;
641 options
|= F_NIGROUP
;
647 case 'p': /* fill buffer with user pattern */
648 options
|= F_PINGFILLED
;
649 fill((char *)datap
, optarg
);
655 options
|= F_AUDIBLE
;
661 memset(&hints
, 0, sizeof(struct addrinfo
));
662 hints
.ai_flags
= AI_NUMERICHOST
; /* allow hostname? */
663 hints
.ai_family
= AF_INET6
;
664 hints
.ai_socktype
= SOCK_RAW
;
665 hints
.ai_protocol
= IPPROTO_ICMPV6
;
667 ret_ga
= getaddrinfo(optarg
, NULL
, &hints
, &res
);
669 errx(1, "invalid source address: %s",
670 gai_strerror(ret_ga
));
673 * res->ai_family must be AF_INET6 and res->ai_addrlen
674 * must be sizeof(src).
676 memcpy(&src
, res
->ai_addr
, res
->ai_addrlen
);
677 srclen
= res
->ai_addrlen
;
680 options
|= F_SRCADDR
;
682 case 's': /* size of packet to send */
683 datalen
= strtol(optarg
, &e
, 10);
684 if (datalen
<= 0 || *optarg
== '\0' || *e
!= '\0')
685 errx(1, "illegal datalen value -- %s", optarg
);
686 if (datalen
> MAXDATALEN
) {
688 "datalen value too large, maximum is %d",
693 options
&= ~F_NOUSERDATA
;
694 options
|= F_SUPTYPES
;
697 options
|= F_VERBOSE
;
700 options
&= ~F_NOUSERDATA
;
704 options
&= ~F_NOUSERDATA
;
705 options
|= F_FQDNOLD
;
708 tclass
= str2tclass(optarg
, &valid
);
710 errx(1, "illegal TOS value -- %s", optarg
);
714 alarmtimeout
= strtoul(optarg
, &e
, 0);
715 if (alarmtimeout
< 1 || alarmtimeout
== ULONG_MAX
)
716 errx(EX_USAGE
, "invalid timeout: `%s'",
718 if (alarmtimeout
> MAXALARM
)
719 errx(EX_USAGE
, "invalid timeout: `%s' > %d",
721 alarm((int)alarmtimeout
);
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], nig_oldmcprefix
);
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
= strdup(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 if (datalen
>= sizeof(struct tv32
)) {
939 /* we can time transfer */
943 /* in F_VERBOSE case, we may get non-echoreply packets*/
944 if (options
& F_VERBOSE
)
945 packlen
= MAX(2048, sweepmax
) + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
947 packlen
= MAX(datalen
, sweepmax
) + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
949 /* suppress timing for node information query */
952 packlen
= 2048 + IP6LEN
+ ICMP6ECHOLEN
+ EXTRA
;
955 if (!(packet
= (u_char
*)malloc(packlen
)))
956 err(1, "Unable to allocate packet");
957 if (!(options
& F_PINGFILLED
))
958 for (i
= ICMP6ECHOLEN
; i
< MAX(datalen
, sweepmax
); ++i
)
961 ident
= getpid() & 0xFFFF;
962 arc4random_buf(nonce
, sizeof(nonce
));
964 if (options
& F_DONTFRAG
)
965 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_DONTFRAG
,
966 &optval
, sizeof(optval
)) == -1)
967 err(1, "IPV6_DONTFRAG");
970 if (options
& F_SO_DEBUG
)
971 (void)setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, (char *)&hold
,
975 (void) setsockopt(s
, SOL_SOCKET
, SO_RECV_ANYIF
, (char *)&hold
,
978 optval
= IPV6_DEFHLIM
;
979 if (IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
980 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
981 &optval
, sizeof(optval
)) == -1)
982 err(1, "IPV6_MULTICAST_HOPS");
983 #ifdef IPV6_USE_MIN_MTU
985 optval
= mflag
> 1 ? 0 : 1;
987 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
988 &optval
, sizeof(optval
)) == -1)
989 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
991 #ifdef IPV6_RECVPATHMTU
994 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPATHMTU
,
995 &optval
, sizeof(optval
)) == -1)
996 err(1, "setsockopt(IPV6_RECVPATHMTU)");
998 #endif /* IPV6_RECVPATHMTU */
999 #endif /* IPV6_USE_MIN_MTU */
1002 #ifdef IPSEC_POLICY_IPSEC
1003 if (options
& F_POLICY
) {
1004 if (setpolicy(s
, policy_in
) < 0)
1005 errx(1, "%s", ipsec_strerror());
1006 if (setpolicy(s
, policy_out
) < 0)
1007 errx(1, "%s", ipsec_strerror());
1010 if (options
& F_AUTHHDR
) {
1011 optval
= IPSEC_LEVEL_REQUIRE
;
1012 #ifdef IPV6_AUTH_TRANS_LEVEL
1013 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
1014 &optval
, sizeof(optval
)) == -1)
1015 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
1017 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
,
1018 &optval
, sizeof(optval
)) == -1)
1019 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
1022 if (options
& F_ENCRYPT
) {
1023 optval
= IPSEC_LEVEL_REQUIRE
;
1024 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
1025 &optval
, sizeof(optval
)) == -1)
1026 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
1028 #endif /*IPSEC_POLICY_IPSEC*/
1033 struct icmp6_filter filt
;
1034 if (!(options
& F_VERBOSE
)) {
1035 ICMP6_FILTER_SETBLOCKALL(&filt
);
1036 if ((options
& F_FQDN
) || (options
& F_FQDNOLD
) ||
1037 (options
& F_NODEADDR
) || (options
& F_SUPTYPES
))
1038 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY
, &filt
);
1040 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY
, &filt
);
1042 ICMP6_FILTER_SETPASSALL(&filt
);
1044 if (setsockopt(s
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
1046 err(1, "setsockopt(ICMP6_FILTER)");
1048 #endif /*ICMP6_FILTER*/
1050 /* let the kerel pass extension headers of incoming packets */
1051 if ((options
& F_VERBOSE
) != 0) {
1054 #ifdef IPV6_RECVRTHDR
1055 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVRTHDR
, &opton
,
1057 err(1, "setsockopt(IPV6_RECVRTHDR)");
1058 #else /* old adv. API */
1059 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RTHDR
, &opton
,
1061 err(1, "setsockopt(IPV6_RTHDR)");
1068 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVTCLASS
, &on
,
1070 err(1, "setsockopt(IPV6_RECVTCLASS)");
1072 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
1074 err(1, "setsockopt(IPV6_TCLASS)");
1079 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
1080 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1081 &optval, sizeof(optval)) == -1)
1082 err(1, "IPV6_MULTICAST_LOOP");
1085 /* Specify the outgoing interface and/or the source address */
1087 ip6optlen
+= CMSG_SPACE(sizeof(struct in6_pktinfo
));
1090 ip6optlen
+= CMSG_SPACE(sizeof(int));
1092 #ifdef IPV6_USE_MIN_MTU
1094 ip6optlen
+= CMSG_SPACE(sizeof(int));
1095 #endif /* IPV6_USE_MIN_MTU */
1098 ip6optlen
+= CMSG_SPACE(sizeof(int));
1100 if (use_sendmsg
== 0) {
1101 if (net_service_type
!= -1)
1102 if (setsockopt(s
, SOL_SOCKET
, SO_NET_SERVICE_TYPE
,
1103 (void *)&net_service_type
, sizeof (net_service_type
)) != 0)
1104 warn("setsockopt(SO_NET_SERVICE_TYPE");
1105 if (so_traffic_class
!= -1) {
1106 if (setsockopt(s
, SOL_SOCKET
, SO_TRAFFIC_CLASS
,
1107 (void *)&so_traffic_class
, sizeof (so_traffic_class
)) != 0)
1108 warn("setsockopt(SO_TRAFFIC_CLASS");
1112 if (net_service_type
!= -1)
1113 ip6optlen
+= CMSG_SPACE(sizeof(int));
1114 if (so_traffic_class
!= -1)
1115 ip6optlen
+= CMSG_SPACE(sizeof(int));
1117 if (use_recvmsg
> 0) {
1119 if (setsockopt(s
, SOL_SOCKET
, SO_RECV_TRAFFIC_CLASS
,
1120 (void *)&on
, sizeof (on
)) != 0)
1121 warn("setsockopt(SO_RECV_TRAFFIC_CLASS");
1124 /* set IP6 packet options */
1126 if ((scmsg
= (char *)malloc(ip6optlen
)) == 0)
1127 errx(1, "can't allocate enough memory");
1128 smsghdr
.msg_control
= (caddr_t
)scmsg
;
1129 smsghdr
.msg_controllen
= ip6optlen
;
1130 scmsgp
= (struct cmsghdr
*)scmsg
;
1133 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
1134 memset(pktinfo
, 0, sizeof(*pktinfo
));
1135 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1136 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1137 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
1138 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1141 /* set the outgoing interface */
1143 #ifndef USE_SIN6_SCOPE_ID
1144 /* pktinfo must have already been allocated */
1145 if ((pktinfo
->ipi6_ifindex
= if_nametoindex(ifname
)) == 0)
1146 errx(1, "%s: invalid interface name", ifname
);
1148 if ((dst
.sin6_scope_id
= if_nametoindex(ifname
)) == 0)
1149 errx(1, "%s: invalid interface name", ifname
);
1152 if (hoplimit
!= -1) {
1153 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1154 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1155 scmsgp
->cmsg_type
= IPV6_HOPLIMIT
;
1156 *(int *)(CMSG_DATA(scmsgp
)) = hoplimit
;
1158 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1161 #ifdef IPV6_USE_MIN_MTU
1163 optval
= mflag
> 1 ? 0 : 1;
1165 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1166 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1167 scmsgp
->cmsg_type
= IPV6_USE_MIN_MTU
;
1168 *(int *)(CMSG_DATA(scmsgp
)) = optval
;
1170 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1172 #endif /* IPV6_USE_MIN_MTU */
1174 if (argc
> 1) { /* some intermediate addrs are specified */
1176 #ifdef USE_RFC2292BIS
1180 #ifdef USE_RFC2292BIS
1181 rthdrlen
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, argc
- 1);
1182 scmsgp
->cmsg_len
= CMSG_LEN(rthdrlen
);
1183 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1184 scmsgp
->cmsg_type
= IPV6_RTHDR
;
1185 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(scmsgp
);
1186 rthdr
= inet6_rth_init((void *)rthdr
, rthdrlen
,
1187 IPV6_RTHDR_TYPE_0
, argc
- 1);
1189 errx(1, "can't initialize rthdr");
1190 #else /* old advanced API */
1191 if ((scmsgp
= (struct cmsghdr
*)inet6_rthdr_init(scmsgp
,
1192 IPV6_RTHDR_TYPE_0
)) == 0)
1193 errx(1, "can't initialize rthdr");
1194 #endif /* USE_RFC2292BIS */
1196 for (hops
= 0; hops
< argc
- 1; hops
++) {
1197 struct addrinfo
*iaip
;
1199 if ((error
= getaddrinfo(argv
[hops
], NULL
, &hints
,
1201 errx(1, "%s", gai_strerror(error
));
1202 if (SIN6(iaip
->ai_addr
)->sin6_family
!= AF_INET6
)
1204 "bad addr family of an intermediate addr");
1206 #ifdef USE_RFC2292BIS
1207 if (inet6_rth_add(rthdr
,
1208 &(SIN6(iaip
->ai_addr
))->sin6_addr
))
1209 errx(1, "can't add an intermediate node");
1210 #else /* old advanced API */
1211 if (inet6_rthdr_add(scmsgp
,
1212 &(SIN6(iaip
->ai_addr
))->sin6_addr
,
1214 errx(1, "can't add an intermediate node");
1215 #endif /* USE_RFC2292BIS */
1219 #ifndef USE_RFC2292BIS
1220 if (inet6_rthdr_lasthop(scmsgp
, IPV6_RTHDR_LOOSE
))
1221 errx(1, "can't set the last flag");
1224 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1228 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1229 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
1230 scmsgp
->cmsg_type
= IPV6_TCLASS
;
1231 *(int *)(CMSG_DATA(scmsgp
)) = tclass
;
1233 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1235 if (use_sendmsg
!= 0) {
1236 if (so_traffic_class
!= -1) {
1237 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1238 scmsgp
->cmsg_level
= SOL_SOCKET
;
1239 scmsgp
->cmsg_type
= SO_TRAFFIC_CLASS
;
1240 *(int *)(CMSG_DATA(scmsgp
)) = so_traffic_class
;
1242 scmsgp
= CMSG_NXTHDR(&smsghdr
, scmsgp
);
1244 if (net_service_type
!= -1) {
1245 scmsgp
->cmsg_len
= CMSG_LEN(sizeof(int));
1246 scmsgp
->cmsg_level
= SOL_SOCKET
;
1247 scmsgp
->cmsg_type
= SO_NET_SERVICE_TYPE
;
1248 *(int *)(CMSG_DATA(scmsgp
)) = net_service_type
;
1251 if (!(options
& F_SRCADDR
)) {
1253 * get the source address. XXX since we revoked the root
1254 * privilege, we cannot use a raw socket for this.
1257 socklen_t len
= sizeof(src
);
1259 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1260 err(1, "UDP socket");
1263 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_BOUND_IF
,
1264 (char *)&ifscope
, sizeof (ifscope
)) != 0)
1265 err(1, "setsockopt(IPV6_BOUND_IF)");
1269 if (setsockopt(dummy
, IPPROTO_IPV6
, IPV6_NO_IFT_CELLULAR
,
1270 (char *)&nocell
, sizeof (nocell
)) != 0)
1271 err(1, "setsockopt(IPV6_NO_IFT_CELLULAR)");
1274 src
.sin6_family
= AF_INET6
;
1275 src
.sin6_addr
= dst
.sin6_addr
;
1276 src
.sin6_port
= ntohs(DUMMY_PORT
);
1277 src
.sin6_scope_id
= dst
.sin6_scope_id
;
1279 #ifdef USE_RFC2292BIS
1281 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTINFO
,
1282 (void *)pktinfo
, sizeof(*pktinfo
)))
1283 err(1, "UDP setsockopt(IPV6_PKTINFO)");
1285 if (hoplimit
!= -1 &&
1286 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1287 (void *)&hoplimit
, sizeof(hoplimit
)))
1288 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
1290 if (hoplimit
!= -1 &&
1291 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1292 (void *)&hoplimit
, sizeof(hoplimit
)))
1293 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
1296 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_RTHDR
,
1297 (void *)rthdr
, (rthdr
->ip6r_len
+ 1) << 3))
1298 err(1, "UDP setsockopt(IPV6_RTHDR)");
1299 #else /* old advanced API */
1300 if (smsghdr
.msg_control
&&
1301 setsockopt(dummy
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
1302 (void *)smsghdr
.msg_control
, smsghdr
.msg_controllen
))
1303 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
1306 if (connect(dummy
, (struct sockaddr
*)&src
, len
) < 0)
1307 err(1, "UDP connect");
1309 if (getsockname(dummy
, (struct sockaddr
*)&src
, &len
) < 0)
1310 err(1, "getsockname");
1315 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
1317 if (MAX(datalen
, sweepmax
) > sockbufsize
)
1318 warnx("you need -b to increase socket buffer size");
1319 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
, &sockbufsize
,
1320 sizeof(sockbufsize
)) < 0)
1321 err(1, "setsockopt(SO_SNDBUF)");
1322 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, &sockbufsize
,
1323 sizeof(sockbufsize
)) < 0)
1324 err(1, "setsockopt(SO_RCVBUF)");
1327 if (MAX(datalen
, sweepmax
) > 8 * 1024) /*XXX*/
1328 warnx("you need -b to increase socket buffer size");
1330 * When pinging the broadcast address, you can get a lot of
1331 * answers. Doing something so evil is useful if you are trying
1332 * to stress the ethernet, or just want to fill the arp cache
1333 * to get some stuff for /etc/ethers.
1336 setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
, (char *)&hold
,
1342 #ifndef USE_SIN6_SCOPE_ID
1343 #ifdef IPV6_RECVPKTINFO
1344 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &optval
,
1345 sizeof(optval
)) < 0)
1346 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1347 #else /* old adv. API */
1348 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_PKTINFO
, &optval
,
1349 sizeof(optval
)) < 0)
1350 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1352 #endif /* USE_SIN6_SCOPE_ID */
1353 #ifdef IPV6_RECVHOPLIMIT
1354 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &optval
,
1355 sizeof(optval
)) < 0)
1356 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1357 #else /* old adv. API */
1358 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &optval
,
1359 sizeof(optval
)) < 0)
1360 warn("setsockopt(IPV6_HOPLIMIT, %d, %lu)",
1361 optval
, sizeof(optval
)); /* XXX err? */
1365 printf("PING6(40+8+[%lu...%lu] bytes) ",
1366 (unsigned long)(sweepmin
),
1367 (unsigned long)(sweepmax
));
1369 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1370 (unsigned long)(pingerlen() - 8));
1371 printf("%s --> ", pr_addr((struct sockaddr
*)&src
, sizeof(src
)));
1372 printf("%s\n", pr_addr((struct sockaddr
*)&dst
, sizeof(dst
)));
1377 if (npackets
!= 0 && preload
> npackets
)
1382 gettimeofday(&last
, NULL
);
1387 * Clear blocked signals inherited from the parent
1390 sigemptyset(&newset
);
1391 if (sigprocmask(SIG_SETMASK
, &newset
, NULL
) != 0)
1392 err(EX_OSERR
, "sigprocmask(newset)");
1394 seenalrm
= seenint
= 0;
1399 (void)signal(SIGINT
, onsignal
);
1401 (void)signal(SIGINFO
, onsignal
);
1403 if (alarmtimeout
> 0) {
1404 (void)signal(SIGALRM
, onsignal
);
1407 if (options
& F_FLOOD
) {
1409 intvl
.tv_usec
= 10000;
1410 } else if ((options
& F_INTERVAL
) == 0) {
1411 intvl
.tv_sec
= interval
/ 1000;
1412 intvl
.tv_usec
= interval
% 1000 * 1000;
1415 /* For control (ancillary) data received from recvmsg() */
1416 cm
= (struct cmsghdr
*)malloc(CONTROLLEN
);
1421 while (seenint
== 0) {
1422 struct timeval now
, timeout
;
1424 struct iovec iov
[2];
1428 /* signal handling */
1440 gettimeofday(&now
, NULL
);
1441 timeout
.tv_sec
= last
.tv_sec
+ intvl
.tv_sec
- now
.tv_sec
;
1442 timeout
.tv_usec
= last
.tv_usec
+ intvl
.tv_usec
- now
.tv_usec
;
1443 while (timeout
.tv_usec
< 0) {
1444 timeout
.tv_usec
+= 1000000;
1447 while (timeout
.tv_usec
> 1000000) {
1448 timeout
.tv_usec
-= 1000000;
1451 if (timeout
.tv_sec
< 0)
1452 timeout
.tv_sec
= timeout
.tv_usec
= 0;
1454 n
= select(s
+ 1, &rfds
, NULL
, NULL
, &timeout
);
1456 continue; /* EINTR */
1458 m
.msg_name
= (caddr_t
)&from
;
1459 m
.msg_namelen
= sizeof(from
);
1460 memset(&iov
, 0, sizeof(iov
));
1461 iov
[0].iov_base
= (caddr_t
)packet
;
1462 iov
[0].iov_len
= packlen
;
1465 memset(cm
, 0, CONTROLLEN
);
1466 m
.msg_control
= (void *)cm
;
1467 m
.msg_controllen
= CONTROLLEN
;
1470 cc
= recvmsg(s
, &m
, 0);
1472 if (errno
!= EINTR
) {
1477 } else if (cc
== 0) {
1481 * receive control messages only. Process the
1482 * exceptions (currently the only possibility is
1483 * a path MTU notification.)
1485 if ((mtu
= get_pathmtu(&m
)) > 0) {
1486 if ((options
& F_VERBOSE
) != 0) {
1487 printf("new path MTU (%d) is "
1494 * an ICMPv6 message (probably an echoreply)
1497 pr_pack(packet
, cc
, &m
);
1499 if (((options
& F_ONCE
) != 0 && nreceived
> 0) ||
1500 (npackets
> 0 && nreceived
>= npackets
) ||
1501 (sweepmax
&& datalen
> sweepmax
))
1504 if (n
== 0 || (options
& F_FLOOD
)) {
1505 if (npackets
== 0 || ntransmitted
< npackets
)
1512 * If we're not transmitting any more packets,
1513 * change the timer to wait two round-trip times
1514 * if we've received any packets or (waittime)
1515 * milliseconds if we haven't.
1519 intvl
.tv_sec
= 2 * tmax
/ 1000;
1520 if (intvl
.tv_sec
== 0)
1523 intvl
.tv_sec
= waittime
/ 1000;
1524 intvl
.tv_usec
= waittime
% 1000 * 1000;
1527 gettimeofday(&last
, NULL
);
1528 if (ntransmitted
- nreceived
- 1 > nmissedmax
) {
1529 nmissedmax
= ntransmitted
- nreceived
- 1;
1530 if (options
& F_MISSED
)
1531 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1535 sigemptyset(&newset
);
1536 if (sigprocmask(SIG_SETMASK
, &newset
, NULL
) != 0)
1537 err(EX_OSERR
, "sigprocmask(newset)");
1543 exit(nreceived
== 0 ? 2 : 0);
1566 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1567 * will be added on by the kernel. The ID field is our UNIX process ID,
1568 * and the sequence number is an ascending integer. The first 8 bytes
1569 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1570 * byte-order, to compute the round-trip time.
1577 if (options
& F_FQDN
)
1578 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1579 else if (options
& F_FQDNOLD
)
1581 else if (options
& F_NODEADDR
)
1582 l
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1583 else if (options
& F_SUPTYPES
)
1586 l
= ICMP6ECHOLEN
+ datalen
;
1594 struct icmp6_hdr
*icp
;
1596 struct icmp6_nodeinfo
*nip
;
1599 if (npackets
&& ntransmitted
>= npackets
)
1600 return(-1); /* no more transmission */
1602 if (sweepmax
&& sntransmitted
== snpackets
) {
1603 datalen
+= sweepincr
;
1604 if (datalen
> sweepmax
)
1605 return(-1); /* no more transmission */
1609 icp
= (struct icmp6_hdr
*)outpack
;
1610 nip
= (struct icmp6_nodeinfo
*)outpack
;
1611 memset(icp
, 0, sizeof(*icp
));
1612 icp
->icmp6_cksum
= 0;
1613 seq
= ntransmitted
++;
1614 CLR(seq
% mx_dup_ck
);
1616 if (options
& F_FQDN
) {
1617 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1618 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1619 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1620 nip
->ni_flags
= htons(0);
1622 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1623 sizeof(nip
->icmp6_ni_nonce
));
1624 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1626 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1627 sizeof(dst
.sin6_addr
));
1628 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1630 } else if (options
& F_FQDNOLD
) {
1631 /* packet format in 03 draft - no Subject data on queries */
1632 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1633 icp
->icmp6_code
= 0; /* code field is always 0 */
1634 nip
->ni_qtype
= htons(NI_QTYPE_FQDN
);
1635 nip
->ni_flags
= htons(0);
1637 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1638 sizeof(nip
->icmp6_ni_nonce
));
1639 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1643 } else if (options
& F_NODEADDR
) {
1644 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1645 icp
->icmp6_code
= ICMP6_NI_SUBJ_IPV6
;
1646 nip
->ni_qtype
= htons(NI_QTYPE_NODEADDR
);
1647 nip
->ni_flags
= naflags
;
1649 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1650 sizeof(nip
->icmp6_ni_nonce
));
1651 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1653 memcpy(&outpack
[ICMP6_NIQLEN
], &dst
.sin6_addr
,
1654 sizeof(dst
.sin6_addr
));
1655 cc
= ICMP6_NIQLEN
+ sizeof(dst
.sin6_addr
);
1657 } else if (options
& F_SUPTYPES
) {
1658 icp
->icmp6_type
= ICMP6_NI_QUERY
;
1659 icp
->icmp6_code
= ICMP6_NI_SUBJ_FQDN
; /*empty*/
1660 nip
->ni_qtype
= htons(NI_QTYPE_SUPTYPES
);
1661 /* we support compressed bitmap */
1662 nip
->ni_flags
= NI_SUPTYPE_FLAG_COMPRESS
;
1664 memcpy(nip
->icmp6_ni_nonce
, nonce
,
1665 sizeof(nip
->icmp6_ni_nonce
));
1666 *(u_int16_t
*)nip
->icmp6_ni_nonce
= ntohs(seq
);
1670 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1671 icp
->icmp6_code
= 0;
1672 icp
->icmp6_id
= htons(ident
);
1673 icp
->icmp6_seq
= ntohs(seq
);
1677 (void)gettimeofday(&tv
, NULL
);
1678 tv32
= (struct tv32
*)&outpack
[ICMP6ECHOLEN
];
1679 tv32
->tv32_sec
= htonl(tv
.tv_sec
);
1680 tv32
->tv32_usec
= htonl(tv
.tv_usec
);
1682 cc
= ICMP6ECHOLEN
+ datalen
;
1686 if (pingerlen() != cc
)
1687 errx(1, "internal error; length mismatch");
1690 if ((options
& F_CONNECT
)) {
1691 smsghdr
.msg_name
= NULL
;
1692 smsghdr
.msg_namelen
= 0;
1694 smsghdr
.msg_name
= (caddr_t
)&dst
;
1695 smsghdr
.msg_namelen
= sizeof(dst
);
1697 memset(&smsgiov
, 0, sizeof(smsgiov
));
1698 smsgiov
[0].iov_base
= (caddr_t
)outpack
;
1699 smsgiov
[0].iov_len
= cc
;
1700 smsghdr
.msg_iov
= smsgiov
;
1701 smsghdr
.msg_iovlen
= 1;
1703 i
= sendmsg(s
, &smsghdr
, 0);
1705 if (i
< 0 || i
!= cc
) {
1708 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1712 if (!(options
& F_QUIET
) && options
& F_FLOOD
)
1713 (void)write(STDOUT_FILENO
, &DOT
, 1);
1719 myechoreply(const struct icmp6_hdr
*icp
)
1721 if (ntohs(icp
->icmp6_id
) == ident
)
1728 mynireply(const struct icmp6_nodeinfo
*nip
)
1730 if (memcmp(nip
->icmp6_ni_nonce
+ sizeof(u_int16_t
),
1731 nonce
+ sizeof(u_int16_t
),
1732 sizeof(nonce
) - sizeof(u_int16_t
)) == 0)
1739 dnsdecode(const u_char
**sp
, const u_char
*ep
, const u_char
*base
, char *buf
,
1741 /*base for compressed name*/
1745 char cresult
[NS_MAXDNAME
+ 1];
1756 if (i
== 0 || cp
!= *sp
) {
1757 if (strlcat((char *)buf
, ".", bufsiz
) >= bufsiz
)
1758 return NULL
; /*result overrun*/
1764 if ((i
& 0xc0) == 0xc0 && cp
- base
> (i
& 0x3f)) {
1765 /* DNS compression */
1769 comp
= base
+ (i
& 0x3f);
1770 if (dnsdecode(&comp
, cp
, base
, cresult
,
1771 sizeof(cresult
)) == NULL
)
1773 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1774 return NULL
; /*result overrun*/
1776 } else if ((i
& 0x3f) == i
) {
1778 return NULL
; /*source overrun*/
1779 while (i
-- > 0 && cp
< ep
) {
1780 l
= snprintf(cresult
, sizeof(cresult
),
1781 isprint(*cp
) ? "%c" : "\\%03o", *cp
& 0xff);
1782 if (l
>= sizeof(cresult
) || l
< 0)
1784 if (strlcat(buf
, cresult
, bufsiz
) >= bufsiz
)
1785 return NULL
; /*result overrun*/
1789 return NULL
; /*invalid label*/
1792 return NULL
; /*not terminated*/
1800 * Print out the packet, if it came from us. This logic is necessary
1801 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1802 * which arrive ('tis only fair). This permits multiple copies of this
1803 * program to be run without having intermingled output (or statistics!).
1806 pr_pack(u_char
*buf
, int cc
, struct msghdr
*mhdr
)
1808 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1809 struct icmp6_hdr
*icp
;
1810 struct icmp6_nodeinfo
*ni
;
1813 struct sockaddr
*from
;
1815 u_char
*cp
= NULL
, *dp
, *end
= buf
+ cc
;
1816 struct in6_pktinfo
*pktinfo
= NULL
;
1817 struct timeval tv
, tp
;
1819 double triptime
= 0;
1824 char dnsname
[NS_MAXDNAME
+ 1];
1828 (void)gettimeofday(&tv
, NULL
);
1830 if (!mhdr
|| !mhdr
->msg_name
||
1831 mhdr
->msg_namelen
!= sizeof(struct sockaddr_in6
) ||
1832 ((struct sockaddr
*)mhdr
->msg_name
)->sa_family
!= AF_INET6
) {
1833 if (options
& F_VERBOSE
)
1834 warnx("invalid peername");
1837 from
= (struct sockaddr
*)mhdr
->msg_name
;
1838 fromlen
= mhdr
->msg_namelen
;
1839 if (cc
< sizeof(struct icmp6_hdr
)) {
1840 if (options
& F_VERBOSE
)
1841 warnx("packet too short (%d bytes) from %s", cc
,
1842 pr_addr(from
, fromlen
));
1845 if (((mhdr
->msg_flags
& MSG_CTRUNC
) != 0) &&
1846 (options
& F_VERBOSE
) != 0)
1847 warnx("some control data discarded, insufficient buffer size");
1848 icp
= (struct icmp6_hdr
*)buf
;
1849 ni
= (struct icmp6_nodeinfo
*)buf
;
1852 if ((hoplim
= get_hoplim(mhdr
)) == -1) {
1853 warnx("failed to get receiving hop limit");
1856 if ((pktinfo
= get_rcvpktinfo(mhdr
)) == NULL
) {
1857 warnx("failed to get receiving packet information");
1860 if (rcvtclass
&& (tclass
= get_tclass(mhdr
)) == -1) {
1861 warnx("failed to get receiving traffic class");
1865 if (use_recvmsg
> 0)
1866 sotc
= get_so_traffic_class(mhdr
);
1868 if (icp
->icmp6_type
== ICMP6_ECHO_REPLY
&& myechoreply(icp
)) {
1869 seq
= ntohs(icp
->icmp6_seq
);
1872 tpp
= (struct tv32
*)(icp
+ 1);
1873 tp
.tv_sec
= ntohl(tpp
->tv32_sec
);
1874 tp
.tv_usec
= ntohl(tpp
->tv32_usec
);
1876 triptime
= ((double)tv
.tv_sec
) * 1000.0 +
1877 ((double)tv
.tv_usec
) / 1000.0;
1879 tsumsq
+= triptime
* triptime
;
1880 if (triptime
< tmin
)
1882 if (triptime
> tmax
)
1886 if (TST(seq
% mx_dup_ck
)) {
1891 SET(seq
% mx_dup_ck
);
1895 if (options
& F_QUIET
)
1898 if (options
& F_WAITTIME
&& triptime
> waittime
) {
1903 if (options
& F_FLOOD
)
1904 (void)write(STDOUT_FILENO
, &BSPACE
, 1);
1906 if (options
& F_AUDIBLE
)
1907 (void)write(STDOUT_FILENO
, &BBELL
, 1);
1908 if (options
& F_PRTIME
)
1910 (void)printf("%d bytes from %s, icmp_seq=%u", cc
,
1911 pr_addr(from
, fromlen
), seq
);
1912 (void)printf(" hlim=%d", hoplim
);
1913 if ((options
& F_VERBOSE
) != 0) {
1914 struct sockaddr_in6 dstsa
;
1916 memset(&dstsa
, 0, sizeof(dstsa
));
1917 dstsa
.sin6_family
= AF_INET6
;
1918 dstsa
.sin6_len
= sizeof(dstsa
);
1919 dstsa
.sin6_scope_id
= pktinfo
->ipi6_ifindex
;
1920 dstsa
.sin6_addr
= pktinfo
->ipi6_addr
;
1921 (void)printf(" dst=%s",
1922 pr_addr((struct sockaddr
*)&dstsa
,
1926 (void)printf(" time=%.3f ms", triptime
);
1928 if (!IN6_IS_ADDR_MULTICAST(&dst
.sin6_addr
))
1929 (void)printf("(DUP!)");
1932 (void)printf(" tclass=%d", tclass
);
1934 (void)printf(" sotc=%d", sotc
);
1935 /* check the data */
1936 cp
= buf
+ off
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1937 dp
= outpack
+ ICMP6ECHOLEN
+ ICMP6ECHOTMLEN
;
1938 for (i
= 8; cp
< end
; ++i
, ++cp
, ++dp
) {
1940 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i
, *dp
, *cp
);
1945 } else if (icp
->icmp6_type
== ICMP6_NI_REPLY
&& mynireply(ni
)) {
1946 seq
= ntohs(*(u_int16_t
*)ni
->icmp6_ni_nonce
);
1948 if (TST(seq
% mx_dup_ck
)) {
1953 SET(seq
% mx_dup_ck
);
1957 if (options
& F_QUIET
)
1960 if (options
& F_PRTIME
)
1962 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
1964 switch (ntohs(ni
->ni_code
)) {
1965 case ICMP6_NI_SUCCESS
:
1967 case ICMP6_NI_REFUSED
:
1968 printf("refused, type 0x%x", ntohs(ni
->ni_type
));
1970 case ICMP6_NI_UNKNOWN
:
1971 printf("unknown, type 0x%x", ntohs(ni
->ni_type
));
1974 printf("unknown code 0x%x, type 0x%x",
1975 ntohs(ni
->ni_code
), ntohs(ni
->ni_type
));
1979 switch (ntohs(ni
->ni_qtype
)) {
1981 printf("NodeInfo NOOP");
1983 case NI_QTYPE_SUPTYPES
:
1984 pr_suptypes(ni
, end
- (u_char
*)ni
);
1986 case NI_QTYPE_NODEADDR
:
1987 pr_nodeaddr(ni
, end
- (u_char
*)ni
);
1990 default: /* XXX: for backward compatibility */
1991 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
1992 if (buf
[off
+ ICMP6_NIRLEN
] ==
1993 cc
- off
- ICMP6_NIRLEN
- 1)
1998 cp
++; /* skip length */
2000 safeputc(*cp
& 0xff);
2006 if (dnsdecode((const u_char
**)&cp
, end
,
2007 (const u_char
*)(ni
+ 1), dnsname
,
2008 sizeof(dnsname
)) == NULL
) {
2013 * name-lookup special handling for
2016 if (cp
+ 1 <= end
&& !*cp
&&
2017 strlen(dnsname
) > 0) {
2018 dnsname
[strlen(dnsname
) - 1] = '\0';
2021 printf("%s%s", i
> 0 ? "," : "",
2025 if (options
& F_VERBOSE
) {
2029 (void)printf(" ("); /*)*/
2031 switch (ni
->ni_code
) {
2032 case ICMP6_NI_REFUSED
:
2033 (void)printf("refused");
2036 case ICMP6_NI_UNKNOWN
:
2037 (void)printf("unknown qtype");
2042 if ((end
- (u_char
*)ni
) < ICMP6_NIRLEN
) {
2043 /* case of refusion, unknown */
2048 ttl
= (int32_t)ntohl(*(u_long
*)&buf
[off
+ICMP6ECHOLEN
+8]);
2051 if (!(ni
->ni_flags
& NI_FQDN_FLAG_VALIDTTL
)) {
2052 (void)printf("TTL=%d:meaningless",
2056 (void)printf("TTL=%d:invalid",
2059 (void)printf("TTL=%d", ttl
);
2069 cp
= (u_char
*)ni
+ ICMP6_NIRLEN
;
2078 if (buf
[off
+ ICMP6_NIRLEN
] !=
2079 cc
- off
- ICMP6_NIRLEN
- 1 && oldfqdn
) {
2082 (void)printf("invalid namelen:%d/%lu",
2083 buf
[off
+ ICMP6_NIRLEN
],
2084 (u_long
)cc
- off
- ICMP6_NIRLEN
- 1);
2094 /* We've got something other than an ECHOREPLY */
2095 if (!(options
& F_VERBOSE
))
2097 if (options
& F_PRTIME
)
2099 (void)printf("%d bytes from %s: ", cc
, pr_addr(from
, fromlen
));
2103 if (!(options
& F_FLOOD
)) {
2104 (void)putchar('\n');
2105 if (options
& F_VERBOSE
)
2107 (void)fflush(stdout
);
2113 pr_exthdrs(struct msghdr
*mhdr
)
2120 bufp
= mhdr
->msg_control
;
2121 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2122 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2123 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2126 bufsize
= CONTROLLEN
- ((caddr_t
)CMSG_DATA(cm
) - (caddr_t
)bufp
);
2129 switch (cm
->cmsg_type
) {
2131 printf(" HbH Options: ");
2132 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
2135 #ifdef IPV6_RTHDRDSTOPTS
2136 case IPV6_RTHDRDSTOPTS
:
2138 printf(" Dst Options: ");
2139 pr_ip6opt(CMSG_DATA(cm
), (size_t)bufsize
);
2142 printf(" Routing: ");
2143 pr_rthdr(CMSG_DATA(cm
), (size_t)bufsize
);
2149 #ifdef USE_RFC2292BIS
2151 pr_ip6opt(void *extbuf
, size_t bufsize
)
2153 struct ip6_hbh
*ext
;
2156 socklen_t extlen
, len
;
2162 ext
= (struct ip6_hbh
*)extbuf
;
2163 extlen
= (ext
->ip6h_len
+ 1) * 8;
2164 printf("nxt %u, len %u (%lu bytes)\n", ext
->ip6h_nxt
,
2165 (unsigned int)ext
->ip6h_len
, (unsigned long)extlen
);
2168 * Bounds checking on the ancillary data buffer:
2169 * subtract the size of a cmsg structure from the buffer size.
2171 if (bufsize
< (extlen
+ CMSG_SPACE(0))) {
2172 extlen
= bufsize
- CMSG_SPACE(0);
2173 warnx("options truncated, showing only %u (total=%u)",
2174 (unsigned int)(extlen
/ 8 - 1),
2175 (unsigned int)(ext
->ip6h_len
));
2180 currentlen
= inet6_opt_next(extbuf
, extlen
, currentlen
,
2181 &type
, &len
, &databuf
);
2182 if (currentlen
== -1)
2186 * Note that inet6_opt_next automatically skips any padding
2191 (void) inet6_opt_get_val(databuf
, offset
,
2192 &value4
, sizeof(value4
));
2193 printf(" Jumbo Payload Opt: Length %u\n",
2194 (u_int32_t
)ntohl(value4
));
2196 case IP6OPT_ROUTER_ALERT
:
2198 (void)inet6_opt_get_val(databuf
, offset
,
2199 &value2
, sizeof(value2
));
2200 printf(" Router Alert Opt: Type %u\n",
2204 printf(" Received Opt %u len %lu\n",
2205 type
, (unsigned long)len
);
2211 #else /* !USE_RFC2292BIS */
2214 pr_ip6opt(void *extbuf
, size_t bufsize __unused
)
2219 #endif /* USE_RFC2292BIS */
2221 #ifdef USE_RFC2292BIS
2223 pr_rthdr(void *extbuf
, size_t bufsize
)
2225 struct in6_addr
*in6
;
2226 char ntopbuf
[INET6_ADDRSTRLEN
];
2227 struct ip6_rthdr
*rh
= (struct ip6_rthdr
*)extbuf
;
2228 int i
, segments
, origsegs
, rthsize
, size0
, size1
;
2230 /* print fixed part of the header */
2231 printf("nxt %u, len %u (%d bytes), type %u, ", rh
->ip6r_nxt
,
2232 rh
->ip6r_len
, (rh
->ip6r_len
+ 1) << 3, rh
->ip6r_type
);
2233 if ((segments
= inet6_rth_segments(extbuf
)) >= 0) {
2234 printf("%d segments, ", segments
);
2235 printf("%d left\n", rh
->ip6r_segleft
);
2237 printf("segments unknown, ");
2238 printf("%d left\n", rh
->ip6r_segleft
);
2243 * Bounds checking on the ancillary data buffer. When calculating
2244 * the number of items to show keep in mind:
2245 * - The size of the cmsg structure
2246 * - The size of one segment (the size of a Type 0 routing header)
2247 * - When dividing add a fudge factor of one in case the
2248 * dividend is not evenly divisible by the divisor
2250 rthsize
= (rh
->ip6r_len
+ 1) * 8;
2251 if (bufsize
< (rthsize
+ CMSG_SPACE(0))) {
2252 origsegs
= segments
;
2253 size0
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 0);
2254 size1
= inet6_rth_space(IPV6_RTHDR_TYPE_0
, 1);
2255 segments
-= (rthsize
- (bufsize
- CMSG_SPACE(0))) /
2256 (size1
- size0
) + 1;
2257 warnx("segments truncated, showing only %d (total=%d)",
2258 segments
, origsegs
);
2261 for (i
= 0; i
< segments
; i
++) {
2262 in6
= inet6_rth_getaddr(extbuf
, i
);
2264 printf(" [%d]<NULL>\n", i
);
2266 if (!inet_ntop(AF_INET6
, in6
, ntopbuf
,
2268 strlcpy(ntopbuf
, "?", sizeof(ntopbuf
));
2269 printf(" [%d]%s\n", i
, ntopbuf
);
2277 #else /* !USE_RFC2292BIS */
2280 pr_rthdr(void *extbuf
, size_t bufsize __unused
)
2285 #endif /* USE_RFC2292BIS */
2288 pr_bitrange(u_int32_t v
, int soff
, int ii
)
2295 /* shift till we have 0x01 */
2296 if ((v
& 0x01) == 0) {
2298 printf("-%u", soff
+ off
- 1);
2309 case 0x04: case 0x0c:
2320 /* we have 0x01 with us */
2321 for (i
= 0; i
< 32 - off
; i
++) {
2322 if ((v
& (0x01 << i
)) == 0)
2326 printf(" %u", soff
+ off
);
2334 pr_suptypes(struct icmp6_nodeinfo
*ni
, size_t nilen
)
2335 /* ni->qtype must be SUPTYPES */
2339 const u_char
*cp
, *end
;
2342 u_int16_t words
; /*32bit count*/
2345 #define MAXQTYPES (1 << 16)
2349 cp
= (u_char
*)(ni
+ 1);
2350 end
= ((u_char
*)ni
) + nilen
;
2354 printf("NodeInfo Supported Qtypes");
2355 if (options
& F_VERBOSE
) {
2356 if (ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
)
2357 printf(", compressed bitmap");
2359 printf(", raw bitmap");
2363 clen
= (size_t)(end
- cp
);
2364 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) == 0) {
2365 if (clen
== 0 || clen
> MAXQTYPES
/ 8 ||
2371 if (clen
< sizeof(cbit
) || clen
% sizeof(v
))
2373 memcpy(&cbit
, cp
, sizeof(cbit
));
2374 if (sizeof(cbit
) + ntohs(cbit
.words
) * sizeof(v
) >
2378 clen
= ntohs(cbit
.words
) * sizeof(v
);
2379 if (cur
+ clen
* 8 + (u_long
)ntohs(cbit
.skip
) * 32 >
2384 for (off
= 0; off
< clen
; off
+= sizeof(v
)) {
2385 memcpy(&v
, cp
+ off
, sizeof(v
));
2386 v
= (u_int32_t
)ntohl(v
);
2387 b
= pr_bitrange(v
, (int)(cur
+ off
* 8), b
);
2389 /* flush the remaining bits */
2390 b
= pr_bitrange(0, (int)(cur
+ off
* 8), b
);
2394 if ((ni
->ni_flags
& NI_SUPTYPE_FLAG_COMPRESS
) != 0)
2395 cur
+= ntohs(cbit
.skip
) * 32;
2400 pr_nodeaddr(struct icmp6_nodeinfo
*ni
, int nilen
)
2401 /* ni->qtype must be NODEADDR */
2403 u_char
*cp
= (u_char
*)(ni
+ 1);
2404 char ntop_buf
[INET6_ADDRSTRLEN
];
2407 nilen
-= sizeof(struct icmp6_nodeinfo
);
2409 if (options
& F_VERBOSE
) {
2410 switch (ni
->ni_code
) {
2411 case ICMP6_NI_REFUSED
:
2412 (void)printf("refused");
2414 case ICMP6_NI_UNKNOWN
:
2415 (void)printf("unknown qtype");
2418 if (ni
->ni_flags
& NI_NODEADDR_FLAG_TRUNCATE
)
2419 (void)printf(" truncated");
2423 printf(" no address\n");
2426 * In icmp-name-lookups 05 and later, TTL of each returned address
2427 * is contained in the resposne. We try to detect the version
2428 * by the length of the data, but note that the detection algorithm
2429 * is incomplete. We assume the latest draft by default.
2431 if (nilen
% (sizeof(u_int32_t
) + sizeof(struct in6_addr
)) == 0)
2437 /* XXX: alignment? */
2438 ttl
= (u_int32_t
)ntohl(*(u_int32_t
*)cp
);
2439 cp
+= sizeof(u_int32_t
);
2440 nilen
-= sizeof(u_int32_t
);
2443 if (inet_ntop(AF_INET6
, cp
, ntop_buf
, sizeof(ntop_buf
)) ==
2445 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2446 printf(" %s", ntop_buf
);
2448 if (ttl
== 0xffffffff) {
2450 * XXX: can this convention be applied to all
2451 * type of TTL (i.e. non-ND TTL)?
2453 printf("(TTL=infty)");
2456 printf("(TTL=%u)", ttl
);
2460 nilen
-= sizeof(struct in6_addr
);
2461 cp
+= sizeof(struct in6_addr
);
2466 get_hoplim(struct msghdr
*mhdr
)
2470 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2471 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2472 if (cm
->cmsg_len
== 0)
2475 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2476 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
2477 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2478 return(*(int *)CMSG_DATA(cm
));
2484 struct in6_pktinfo
*
2485 get_rcvpktinfo(struct msghdr
*mhdr
)
2489 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2490 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2491 if (cm
->cmsg_len
== 0)
2494 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2495 cm
->cmsg_type
== IPV6_PKTINFO
&&
2496 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
)))
2497 return((struct in6_pktinfo
*)CMSG_DATA(cm
));
2504 get_pathmtu(struct msghdr
*mhdr
)
2506 #ifdef IPV6_RECVPATHMTU
2508 struct ip6_mtuinfo
*mtuctl
= NULL
;
2510 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2511 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2512 if (cm
->cmsg_len
== 0)
2515 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2516 cm
->cmsg_type
== IPV6_PATHMTU
&&
2517 cm
->cmsg_len
== CMSG_LEN(sizeof(struct ip6_mtuinfo
))) {
2518 mtuctl
= (struct ip6_mtuinfo
*)CMSG_DATA(cm
);
2521 * If the notified destination is different from
2522 * the one we are pinging, just ignore the info.
2523 * We check the scope ID only when both notified value
2524 * and our own value have non-0 values, because we may
2525 * have used the default scope zone ID for sending,
2526 * in which case the scope ID value is 0.
2528 if (!IN6_ARE_ADDR_EQUAL(&mtuctl
->ip6m_addr
.sin6_addr
,
2530 (mtuctl
->ip6m_addr
.sin6_scope_id
&&
2531 dst
.sin6_scope_id
&&
2532 mtuctl
->ip6m_addr
.sin6_scope_id
!=
2533 dst
.sin6_scope_id
)) {
2534 if ((options
& F_VERBOSE
) != 0) {
2535 printf("path MTU for %s is notified. "
2537 pr_addr((struct sockaddr
*)&mtuctl
->ip6m_addr
,
2538 sizeof(mtuctl
->ip6m_addr
)));
2544 * Ignore an invalid MTU. XXX: can we just believe
2547 if (mtuctl
->ip6m_mtu
< IPV6_MMTU
)
2550 /* notification for our destination. return the MTU. */
2551 return((int)mtuctl
->ip6m_mtu
);
2560 struct msghdr
*mhdr
;
2564 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2565 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2566 if (cm
->cmsg_len
== 0)
2569 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
2570 cm
->cmsg_type
== IPV6_TCLASS
&&
2571 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2572 return(*(int *)CMSG_DATA(cm
));
2579 get_so_traffic_class(struct msghdr
*mhdr
)
2583 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
2584 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
2585 if (cm
->cmsg_len
== 0)
2588 if (cm
->cmsg_level
== SOL_SOCKET
&&
2589 cm
->cmsg_type
== SO_TRAFFIC_CLASS
&&
2590 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
2591 return(*(int *)CMSG_DATA(cm
));
2599 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2603 tvsub(struct timeval
*out
, struct timeval
*in
)
2605 if ((out
->tv_usec
-= in
->tv_usec
) < 0) {
2607 out
->tv_usec
+= 1000000;
2609 out
->tv_sec
-= in
->tv_sec
;
2618 onint(int notused __unused
)
2621 * When doing reverse DNS lookups, the seenint flag might not
2622 * be noticed for a while. Just exit if we get a second SIGINT.
2624 if ((options
& F_HOSTNAME
) && seenint
!= 0)
2625 _exit(nreceived
? 0 : 2);
2630 * Print out statistics.
2635 (void)printf("\n--- %s ping6 statistics ---\n", hostname
);
2636 (void)printf("%ld packets transmitted, ", ntransmitted
);
2637 (void)printf("%ld packets received, ", nreceived
);
2639 (void)printf("+%ld duplicates, ", nrepeats
);
2641 if (nreceived
> ntransmitted
)
2642 (void)printf("-- somebody's duplicating packets!");
2644 (void)printf("%.1f%% packet loss",
2645 ((((double)ntransmitted
- nreceived
) * 100.0) /
2649 printf(", %ld packets out of wait time", nrcvtimeout
);
2650 (void)putchar('\n');
2651 if (nreceived
&& timing
) {
2652 /* Only display average to microseconds */
2653 double num
= nreceived
+ nrepeats
;
2654 double avg
= tsum
/ num
;
2655 double dev
= sqrt(tsumsq
/ num
- avg
* avg
);
2657 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2658 tmin
, avg
, tmax
, dev
);
2659 (void)fflush(stdout
);
2661 (void)fflush(stdout
);
2665 static const char *niqcode
[] = {
2667 "DNS label", /*or empty*/
2672 static const char *nircode
[] = {
2673 "Success", "Refused", "Unknown",
2679 * Print a descriptive string about an ICMP header.
2682 pr_icmph(struct icmp6_hdr
*icp
, u_char
*end
)
2684 char ntop_buf
[INET6_ADDRSTRLEN
];
2685 struct nd_redirect
*red
;
2686 struct icmp6_nodeinfo
*ni
;
2687 char dnsname
[NS_MAXDNAME
+ 1];
2691 switch (icp
->icmp6_type
) {
2692 case ICMP6_DST_UNREACH
:
2693 switch (icp
->icmp6_code
) {
2694 case ICMP6_DST_UNREACH_NOROUTE
:
2695 (void)printf("No Route to Destination\n");
2697 case ICMP6_DST_UNREACH_ADMIN
:
2698 (void)printf("Destination Administratively "
2701 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
2702 (void)printf("Destination Unreachable Beyond Scope\n");
2704 case ICMP6_DST_UNREACH_ADDR
:
2705 (void)printf("Destination Host Unreachable\n");
2707 case ICMP6_DST_UNREACH_NOPORT
:
2708 (void)printf("Destination Port Unreachable\n");
2711 (void)printf("Destination Unreachable, Bad Code: %d\n",
2715 /* Print returned IP header information */
2716 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2718 case ICMP6_PACKET_TOO_BIG
:
2719 (void)printf("Packet too big mtu = %d\n",
2720 (int)ntohl(icp
->icmp6_mtu
));
2721 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2723 case ICMP6_TIME_EXCEEDED
:
2724 switch (icp
->icmp6_code
) {
2725 case ICMP6_TIME_EXCEED_TRANSIT
:
2726 (void)printf("Time to live exceeded\n");
2728 case ICMP6_TIME_EXCEED_REASSEMBLY
:
2729 (void)printf("Frag reassembly time exceeded\n");
2732 (void)printf("Time exceeded, Bad Code: %d\n",
2736 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2738 case ICMP6_PARAM_PROB
:
2739 (void)printf("Parameter problem: ");
2740 switch (icp
->icmp6_code
) {
2741 case ICMP6_PARAMPROB_HEADER
:
2742 (void)printf("Erroneous Header ");
2744 case ICMP6_PARAMPROB_NEXTHEADER
:
2745 (void)printf("Unknown Nextheader ");
2747 case ICMP6_PARAMPROB_OPTION
:
2748 (void)printf("Unrecognized Option ");
2751 (void)printf("Bad code(%d) ", icp
->icmp6_code
);
2754 (void)printf("pointer = 0x%02x\n",
2755 (u_int32_t
)ntohl(icp
->icmp6_pptr
));
2756 pr_retip((struct ip6_hdr
*)(icp
+ 1), end
);
2758 case ICMP6_ECHO_REQUEST
:
2759 (void)printf("Echo Request");
2760 /* XXX ID + Seq + Data */
2762 case ICMP6_ECHO_REPLY
:
2763 (void)printf("Echo Reply");
2764 /* XXX ID + Seq + Data */
2766 case ICMP6_MEMBERSHIP_QUERY
:
2767 (void)printf("Listener Query");
2769 case ICMP6_MEMBERSHIP_REPORT
:
2770 (void)printf("Listener Report");
2772 case ICMP6_MEMBERSHIP_REDUCTION
:
2773 (void)printf("Listener Done");
2775 case ND_ROUTER_SOLICIT
:
2776 (void)printf("Router Solicitation");
2778 case ND_ROUTER_ADVERT
:
2779 (void)printf("Router Advertisement");
2781 case ND_NEIGHBOR_SOLICIT
:
2782 (void)printf("Neighbor Solicitation");
2784 case ND_NEIGHBOR_ADVERT
:
2785 (void)printf("Neighbor Advertisement");
2788 red
= (struct nd_redirect
*)icp
;
2789 (void)printf("Redirect\n");
2790 if (!inet_ntop(AF_INET6
, &red
->nd_rd_dst
, ntop_buf
,
2792 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2793 (void)printf("Destination: %s", ntop_buf
);
2794 if (!inet_ntop(AF_INET6
, &red
->nd_rd_target
, ntop_buf
,
2796 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2797 (void)printf(" New Target: %s", ntop_buf
);
2799 case ICMP6_NI_QUERY
:
2800 (void)printf("Node Information Query");
2801 /* XXX ID + Seq + Data */
2802 ni
= (struct icmp6_nodeinfo
*)icp
;
2803 l
= end
- (u_char
*)(ni
+ 1);
2805 switch (ntohs(ni
->ni_qtype
)) {
2807 (void)printf("NOOP");
2809 case NI_QTYPE_SUPTYPES
:
2810 (void)printf("Supported qtypes");
2813 (void)printf("DNS name");
2815 case NI_QTYPE_NODEADDR
:
2816 (void)printf("nodeaddr");
2818 case NI_QTYPE_IPV4ADDR
:
2819 (void)printf("IPv4 nodeaddr");
2822 (void)printf("unknown qtype");
2825 if (options
& F_VERBOSE
) {
2826 switch (ni
->ni_code
) {
2827 case ICMP6_NI_SUBJ_IPV6
:
2828 if (l
== sizeof(struct in6_addr
) &&
2829 inet_ntop(AF_INET6
, ni
+ 1, ntop_buf
,
2830 sizeof(ntop_buf
)) != NULL
) {
2831 (void)printf(", subject=%s(%s)",
2832 niqcode
[ni
->ni_code
], ntop_buf
);
2835 /* backward compat to -W */
2836 (void)printf(", oldfqdn");
2838 (void)printf(", invalid");
2842 case ICMP6_NI_SUBJ_FQDN
:
2843 if (end
== (u_char
*)(ni
+ 1)) {
2844 (void)printf(", no subject");
2847 printf(", subject=%s", niqcode
[ni
->ni_code
]);
2848 cp
= (const u_char
*)(ni
+ 1);
2849 if (dnsdecode(&cp
, end
, NULL
, dnsname
,
2850 sizeof(dnsname
)) != NULL
)
2851 printf("(%s)", dnsname
);
2853 printf("(invalid)");
2855 case ICMP6_NI_SUBJ_IPV4
:
2856 if (l
== sizeof(struct in_addr
) &&
2857 inet_ntop(AF_INET
, ni
+ 1, ntop_buf
,
2858 sizeof(ntop_buf
)) != NULL
) {
2859 (void)printf(", subject=%s(%s)",
2860 niqcode
[ni
->ni_code
], ntop_buf
);
2862 (void)printf(", invalid");
2865 (void)printf(", invalid");
2870 case ICMP6_NI_REPLY
:
2871 (void)printf("Node Information Reply");
2872 /* XXX ID + Seq + Data */
2873 ni
= (struct icmp6_nodeinfo
*)icp
;
2875 switch (ntohs(ni
->ni_qtype
)) {
2877 (void)printf("NOOP");
2879 case NI_QTYPE_SUPTYPES
:
2880 (void)printf("Supported qtypes");
2883 (void)printf("DNS name");
2885 case NI_QTYPE_NODEADDR
:
2886 (void)printf("nodeaddr");
2888 case NI_QTYPE_IPV4ADDR
:
2889 (void)printf("IPv4 nodeaddr");
2892 (void)printf("unknown qtype");
2895 if (options
& F_VERBOSE
) {
2896 if (ni
->ni_code
> sizeof(nircode
) / sizeof(nircode
[0]))
2897 printf(", invalid");
2899 printf(", %s", nircode
[ni
->ni_code
]);
2903 (void)printf("Bad ICMP type: %d", icp
->icmp6_type
);
2909 * Print an IP6 header.
2912 pr_iph(struct ip6_hdr
*ip6
)
2914 u_int32_t flow
= ip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
;
2916 char ntop_buf
[INET6_ADDRSTRLEN
];
2918 tc
= *(&ip6
->ip6_vfc
+ 1); /* XXX */
2919 tc
= (tc
>> 4) & 0x0f;
2920 tc
|= (ip6
->ip6_vfc
<< 4);
2922 printf("Vr TC Flow Plen Nxt Hlim\n");
2923 printf(" %1x %02x %05x %04x %02x %02x\n",
2924 (ip6
->ip6_vfc
& IPV6_VERSION_MASK
) >> 4, tc
, (u_int32_t
)ntohl(flow
),
2925 ntohs(ip6
->ip6_plen
), ip6
->ip6_nxt
, ip6
->ip6_hlim
);
2926 if (!inet_ntop(AF_INET6
, &ip6
->ip6_src
, ntop_buf
, sizeof(ntop_buf
)))
2927 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2928 printf("%s->", ntop_buf
);
2929 if (!inet_ntop(AF_INET6
, &ip6
->ip6_dst
, ntop_buf
, sizeof(ntop_buf
)))
2930 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2931 printf("%s\n", ntop_buf
);
2936 * Return an ascii host address as a dotted quad and optionally with
2940 pr_addr(struct sockaddr
*addr
, int addrlen
)
2942 static char buf
[NI_MAXHOST
];
2945 if ((options
& F_HOSTNAME
) == 0)
2946 flag
|= NI_NUMERICHOST
;
2948 if (getnameinfo(addr
, addrlen
, buf
, sizeof(buf
), NULL
, 0, flag
) == 0)
2956 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2959 pr_retip(struct ip6_hdr
*ip6
, u_char
*end
)
2961 u_char
*cp
= (u_char
*)ip6
, nh
;
2964 if (end
- (u_char
*)ip6
< sizeof(*ip6
)) {
2969 hlen
= sizeof(*ip6
);
2973 while (end
- cp
>= 8) {
2975 case IPPROTO_HOPOPTS
:
2977 hlen
= (((struct ip6_hbh
*)cp
)->ip6h_len
+1) << 3;
2978 nh
= ((struct ip6_hbh
*)cp
)->ip6h_nxt
;
2980 case IPPROTO_DSTOPTS
:
2982 hlen
= (((struct ip6_dest
*)cp
)->ip6d_len
+1) << 3;
2983 nh
= ((struct ip6_dest
*)cp
)->ip6d_nxt
;
2985 case IPPROTO_FRAGMENT
:
2987 hlen
= sizeof(struct ip6_frag
);
2988 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
2990 case IPPROTO_ROUTING
:
2992 hlen
= (((struct ip6_rthdr
*)cp
)->ip6r_len
+1) << 3;
2993 nh
= ((struct ip6_rthdr
*)cp
)->ip6r_nxt
;
2998 hlen
= (((struct ah
*)cp
)->ah_len
+2) << 2;
2999 nh
= ((struct ah
*)cp
)->ah_nxt
;
3002 case IPPROTO_ICMPV6
:
3003 printf("ICMP6: type = %d, code = %d\n",
3010 printf("TCP: from port %u, to port %u (decimal)\n",
3011 (*cp
* 256 + *(cp
+ 1)),
3012 (*(cp
+ 2) * 256 + *(cp
+ 3)));
3015 printf("UDP: from port %u, to port %u (decimal)\n",
3016 (*cp
* 256 + *(cp
+ 1)),
3017 (*(cp
+ 2) * 256 + *(cp
+ 3)));
3020 printf("Unknown Header(%d)\n", nh
);
3024 if ((cp
+= hlen
) >= end
)
3039 fill(char *bp
, char *patp
)
3045 for (cp
= patp
; *cp
; cp
++)
3047 errx(1, "patterns must be specified as hex digits");
3049 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
3050 &pat
[0], &pat
[1], &pat
[2], &pat
[3], &pat
[4], &pat
[5], &pat
[6],
3051 &pat
[7], &pat
[8], &pat
[9], &pat
[10], &pat
[11], &pat
[12],
3052 &pat
[13], &pat
[14], &pat
[15]);
3057 kk
<= MAXDATALEN
- (8 + sizeof(struct tv32
) + ii
);
3059 for (jj
= 0; jj
< ii
; ++jj
)
3060 bp
[jj
+ kk
] = pat
[jj
];
3061 if (!(options
& F_QUIET
)) {
3062 (void)printf("PATTERN: 0x");
3063 for (jj
= 0; jj
< ii
; ++jj
)
3064 (void)printf("%02x", bp
[jj
] & 0xFF);
3070 #ifdef IPSEC_POLICY_IPSEC
3072 setpolicy(int so __unused
, char *policy
)
3077 return 0; /* ignore */
3079 buf
= ipsec_set_policy(policy
, strlen(policy
));
3081 errx(1, "%s", ipsec_strerror());
3082 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
, buf
,
3083 ipsec_get_policylen(buf
)) < 0)
3084 warnx("Unable to set IPsec policy");
3093 nigroup(char *name
, int nig_oldmcprefix
)
3098 u_int8_t digest
[16];
3101 char hbuf
[NI_MAXHOST
];
3102 struct in6_addr in6
;
3105 p
= strchr(name
, '.');
3107 p
= name
+ strlen(name
);
3109 if (l
> 63 || l
> sizeof(hbuf
) - 1)
3110 return NULL
; /*label too long*/
3111 strncpy(hbuf
, name
, l
);
3112 hbuf
[(int)l
] = '\0';
3114 for (q
= name
; *q
; q
++) {
3115 if (isupper(*(unsigned char *)q
))
3116 *q
= tolower(*(unsigned char *)q
);
3119 /* generate 16 bytes of pseudo-random value. */
3120 memset(&ctxt
, 0, sizeof(ctxt
));
3123 MD5Update(&ctxt
, &c
, sizeof(c
));
3124 MD5Update(&ctxt
, (unsigned char *)name
, l
);
3125 MD5Final(digest
, &ctxt
);
3127 if (nig_oldmcprefix
) {
3128 /* draft-ietf-ipngwg-icmp-name-lookup */
3129 valid
= inet_pton(AF_INET6
, "ff02::2:0000:0000", &in6
);
3132 valid
= inet_pton(AF_INET6
, "ff02::2:ff00:0000", &in6
);
3135 return NULL
; /*XXX*/
3137 if (nig_oldmcprefix
) {
3138 /* draft-ietf-ipngwg-icmp-name-lookup */
3139 bcopy(digest
, &in6
.s6_addr
[12], 4);
3142 bcopy(digest
, &in6
.s6_addr
[13], 3);
3145 if (inet_ntop(AF_INET6
, &in6
, hbuf
, sizeof(hbuf
)) == NULL
)
3148 return strdup(hbuf
);
3152 str2sotc(const char *str
, bool *valid
)
3159 if (str
== NULL
|| *str
== '\0')
3161 else if (strcasecmp(str
, "BK_SYS") == 0)
3162 return SO_TC_BK_SYS
;
3163 else if (strcasecmp(str
, "BK") == 0)
3165 else if (strcasecmp(str
, "BE") == 0)
3167 else if (strcasecmp(str
, "RD") == 0)
3169 else if (strcasecmp(str
, "OAM") == 0)
3171 else if (strcasecmp(str
, "AV") == 0)
3173 else if (strcasecmp(str
, "RV") == 0)
3175 else if (strcasecmp(str
, "VI") == 0)
3177 else if (strcasecmp(str
, "VO") == 0)
3179 else if (strcasecmp(str
, "CTL") == 0)
3182 sotc
= (int)strtol(str
, &endptr
, 0);
3183 if (*endptr
!= '\0')
3190 str2netservicetype(const char *str
, bool *valid
)
3197 if (str
== NULL
|| *str
== '\0')
3199 else if (strcasecmp(str
, "BK") == 0)
3200 return NET_SERVICE_TYPE_BK
;
3201 else if (strcasecmp(str
, "BE") == 0)
3202 return NET_SERVICE_TYPE_BE
;
3203 else if (strcasecmp(str
, "VI") == 0)
3204 return NET_SERVICE_TYPE_VI
;
3205 else if (strcasecmp(str
, "SIG") == 0)
3206 return NET_SERVICE_TYPE_SIG
;
3207 else if (strcasecmp(str
, "VO") == 0)
3208 return NET_SERVICE_TYPE_VO
;
3209 else if (strcasecmp(str
, "RV") == 0)
3210 return NET_SERVICE_TYPE_RV
;
3211 else if (strcasecmp(str
, "AV") == 0)
3212 return NET_SERVICE_TYPE_AV
;
3213 else if (strcasecmp(str
, "OAM") == 0)
3214 return NET_SERVICE_TYPE_OAM
;
3215 else if (strcasecmp(str
, "RD") == 0)
3216 return NET_SERVICE_TYPE_RD
;
3218 svc
= (int)strtol(str
, &endptr
, 0);
3219 if (*endptr
!= '\0')
3226 str2tclass(const char *str
, bool *valid
)
3233 if (str
== NULL
|| *str
== '\0')
3235 else if (strcasecmp(str
, "DF") == 0)
3237 else if (strcasecmp(str
, "EF") == 0)
3239 else if (strcasecmp(str
, "VA") == 0)
3242 else if (strcasecmp(str
, "CS0") == 0)
3244 else if (strcasecmp(str
, "CS1") == 0)
3246 else if (strcasecmp(str
, "CS2") == 0)
3248 else if (strcasecmp(str
, "CS3") == 0)
3250 else if (strcasecmp(str
, "CS4") == 0)
3252 else if (strcasecmp(str
, "CS5") == 0)
3254 else if (strcasecmp(str
, "CS6") == 0)
3256 else if (strcasecmp(str
, "CS7") == 0)
3259 else if (strcasecmp(str
, "AF11") == 0)
3261 else if (strcasecmp(str
, "AF12") == 0)
3263 else if (strcasecmp(str
, "AF13") == 0)
3265 else if (strcasecmp(str
, "AF21") == 0)
3267 else if (strcasecmp(str
, "AF22") == 0)
3269 else if (strcasecmp(str
, "AF23") == 0)
3271 else if (strcasecmp(str
, "AF31") == 0)
3273 else if (strcasecmp(str
, "AF32") == 0)
3275 else if (strcasecmp(str
, "AF33") == 0)
3277 else if (strcasecmp(str
, "AF41") == 0)
3279 else if (strcasecmp(str
, "AF42") == 0)
3281 else if (strcasecmp(str
, "AF43") == 0)
3285 unsigned long val
= strtoul(str
, &endptr
, 0);
3286 if (*endptr
!= '\0' || val
> 255)
3289 return ((u_int8_t
)val
);
3291 /* DSCP occupies the 6 upper bits of the traffic class field */
3296 pr_currenttime(void)
3301 gettimeofday(&tv
, NULL
);
3303 s
= (tv
.tv_sec
+ thiszone
) % 86400;
3304 printf("%02d:%02d:%02d.%06u ", s
/ 3600, (s
% 3600) / 60, s
% 60,
3305 (u_int32_t
)tv
.tv_usec
);
3311 (void)fprintf(stderr
,
3312 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3317 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
3321 #ifdef IPV6_USE_MIN_MTU
3325 "[-a addrtype] [-b bufsiz] [-c count]\n"
3326 " [-g gateway] [-h hoplimit] [-I interface] [-i wait] [-l preload]"
3327 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
3331 " [-p pattern] [-S sourceaddr] [-s packetsize] [-z tclass] "
3332 "[-k traffic_class] [-K net_service_type] "
3333 "[hops ...] host\n");
3334 (void)fprintf(stderr
, "Apple specific options (to be specified before hops or host like all options)\n");
3335 (void)fprintf(stderr
, " -b boundif # bind the socket to the interface\n");
3336 (void)fprintf(stderr
, " -k traffic_class # set traffic class socket option\n");
3337 (void)fprintf(stderr
, " -K net_service_type # set traffic class socket options\n");
3338 (void)fprintf(stderr
, " -apple-connect # call connect(2) in the socket\n");
3339 (void)fprintf(stderr
, " -apple-time # display current time\n");
3340 (void)fprintf(stderr
, " -apple-progress # show progress for debugging\n");