1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static const char copyright[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid[] =
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
137 #include <netipsec/ah.h>
138 #include <netipsec/ipsec.h>
148 #define MAXPACKETLEN 131072
150 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
151 #define ICMP6ECHOTMLEN sizeof(struct tv32)
152 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
153 # define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
154 /* FQDN case, 64 bits of nonce + 32 bits ttl */
155 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
156 #define EXTRA 256 /* for AH and various other headers. weird. */
157 #define DEFDATALEN ICMP6ECHOTMLEN
158 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
159 #define NROUTES 9 /* number of record route slots */
161 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
162 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
163 #define SET(bit) (A(bit) |= B(bit))
164 #define CLR(bit) (A(bit) &= (~B(bit)))
165 #define TST(bit) (A(bit) & B(bit))
167 #define F_FLOOD 0x0001
168 #define F_INTERVAL 0x0002
169 #define F_PINGFILLED 0x0008
170 #define F_QUIET 0x0010
171 #define F_RROUTE 0x0020
172 #define F_SO_DEBUG 0x0040
173 #define F_VERBOSE 0x0100
175 #ifdef IPSEC_POLICY_IPSEC
176 #define F_POLICY 0x0400
178 #define F_AUTHHDR 0x0200
179 #define F_ENCRYPT 0x0400
180 #endif /*IPSEC_POLICY_IPSEC*/
182 #define F_NODEADDR 0x0800
183 #define F_FQDN 0x1000
184 #define F_INTERFACE 0x2000
185 #define F_SRCADDR 0x4000
186 #define F_HOSTNAME 0x10000
187 #define F_FQDNOLD 0x20000
188 #define F_NIGROUP 0x40000
189 #define F_SUPTYPES 0x80000
190 #define F_NOMINMTU 0x100000
191 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
194 #define IN6LEN sizeof(struct in6_addr)
195 #define SA6LEN sizeof(struct sockaddr_in6)
196 #define DUMMY_PORT 10101
198 #define SIN6(s) ((struct sockaddr_in6 *)(s))
201 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
202 * number of received sequence numbers we can keep track of. Change 128
203 * to 8192 for complete accuracy...
205 #define MAX_DUP_CHK (8 * 8192)
206 int mx_dup_ck = MAX_DUP_CHK;
207 char rcvd_tbl[MAX_DUP_CHK / 8];
209 struct addrinfo *res;
210 struct sockaddr_in6 dst; /* who to ping6 */
211 struct sockaddr_in6 src; /* src addr of this packet */
213 int datalen = DEFDATALEN;
214 int s; /* socket file descriptor */
215 u_char outpack[MAXPACKETLEN];
216 char BSPACE = '\b'; /* characters written for flood */
219 int ident; /* process id to identify our packets */
220 u_int8_t nonce[8]; /* nonce field for node information */
221 int hoplimit = -1; /* hoplimit */
222 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
225 long npackets; /* max packets to transmit */
226 long nreceived; /* # of packets we got back */
227 long nrepeats; /* number of duplicates */
228 long ntransmitted; /* sequence # for outbound packets = #sent */
229 struct timeval interval = {1, 0}; /* interval between packets */
232 int timing; /* flag to do timing */
233 double tmin = 999999999.0; /* minimum round trip time */
234 double tmax = 0.0; /* maximum round trip time */
235 double tsum = 0.0; /* sum of all times, for doing average */
236 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
238 /* for node addresses */
241 /* for ancillary data(advanced API) */
242 struct msghdr smsghdr;
243 struct iovec smsgiov;
246 volatile sig_atomic_t seenalrm;
247 volatile sig_atomic_t seenint;
249 volatile sig_atomic_t seeninfo;
252 int main(int, char *[]);
253 void fill(char *, char *);
254 int get_hoplim(struct msghdr *);
255 int get_pathmtu(struct msghdr *);
256 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
258 void retransmit(void);
260 size_t pingerlen(void);
262 const char *pr_addr(struct sockaddr *, int);
263 void pr_icmph(struct icmp6_hdr *, u_char *);
264 void pr_iph(struct ip6_hdr *);
265 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
266 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
267 int myechoreply(const struct icmp6_hdr *);
268 int mynireply(const struct icmp6_nodeinfo *);
269 char *dnsdecode(const u_char **, const u_char *, const u_char *,
271 void pr_pack(u_char *, int, struct msghdr *);
272 void pr_exthdrs(struct msghdr *);
273 void pr_ip6opt(void *, size_t);
274 void pr_rthdr(void *, size_t);
275 int pr_bitrange(u_int32_t, int, int);
276 void pr_retip(struct ip6_hdr *, u_char *);
278 void tvsub(struct timeval *, struct timeval *);
279 int setpolicy(int, char *);
280 char *nigroup(char *);
288 struct itimerval itimer;
289 struct sockaddr_in6 from;
290 #ifndef HAVE_ARC4RANDOM
296 struct timeval timeout, *tv;
298 struct addrinfo hints;
300 struct pollfd fdmaskp[1];
306 int ch, hold, packlen, preload, optval, ret_ga;
307 u_char *datap, *packet;
308 char *e, *target, *ifname = NULL, *gateway = NULL;
310 struct cmsghdr *scmsgp = NULL;
312 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
317 struct in6_pktinfo *pktinfo = NULL;
318 #ifdef USE_RFC2292BIS
319 struct ip6_rthdr *rthdr = NULL;
321 #ifdef IPSEC_POLICY_IPSEC
322 char *policy_in = NULL;
323 char *policy_out = NULL;
327 #ifdef IPV6_USE_MIN_MTU
331 /* just to be sure */
332 memset(&smsghdr, 0, sizeof(smsghdr));
333 memset(&smsgiov, 0, sizeof(smsgiov));
336 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
340 #ifdef IPSEC_POLICY_IPSEC
344 #endif /*IPSEC_POLICY_IPSEC*/
346 while ((ch = getopt(argc, argv,
347 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS)) != -1) {
354 options &= ~F_NOUSERDATA;
355 options |= F_NODEADDR;
356 for (cp = optarg; *cp != '\0'; cp++) {
359 naflags |= NI_NODEADDR_FLAG_ALL;
363 naflags |= NI_NODEADDR_FLAG_COMPAT;
367 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
371 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
375 naflags |= NI_NODEADDR_FLAG_GLOBAL;
377 case 'A': /* experimental. not in the spec */
378 #ifdef NI_NODEADDR_FLAG_ANYCAST
379 naflags |= NI_NODEADDR_FLAG_ANYCAST;
383 "-a A is not supported on the platform");
394 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
397 lsockbufsize = strtoul(optarg, &e, 10);
398 sockbufsize = lsockbufsize;
399 if (errno || !*optarg || *e ||
400 sockbufsize != lsockbufsize)
401 errx(1, "invalid socket buffer size");
404 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
408 npackets = strtol(optarg, &e, 10);
409 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
411 "illegal number of packets -- %s", optarg);
414 options |= F_SO_DEBUG;
419 errx(1, "Must be superuser to flood ping");
422 setbuf(stdout, (char *)NULL);
428 options |= F_HOSTNAME;
430 case 'h': /* hoplimit */
431 hoplimit = strtol(optarg, &e, 10);
432 if (*optarg == '\0' || *e != '\0')
433 errx(1, "illegal hoplimit %s", optarg);
434 if (255 < hoplimit || hoplimit < -1)
436 "illegal hoplimit -- %s", optarg);
440 options |= F_INTERFACE;
441 #ifndef USE_SIN6_SCOPE_ID
445 case 'i': /* wait between sending packets */
446 intval = strtod(optarg, &e);
447 if (*optarg == '\0' || *e != '\0')
448 errx(1, "illegal timing interval %s", optarg);
449 if (intval < 1 && getuid()) {
450 errx(1, "%s: only root may use interval < 1s",
453 interval.tv_sec = (long)intval;
455 (long)((intval - interval.tv_sec) * 1000000);
456 if (interval.tv_sec < 0)
457 errx(1, "illegal timing interval %s", optarg);
458 /* less than 1/hz does not make sense */
459 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
460 warnx("too small interval, raised to 0.01");
461 interval.tv_usec = 10000;
463 options |= F_INTERVAL;
468 errx(1, "Must be superuser to preload");
470 preload = strtol(optarg, &e, 10);
471 if (preload < 0 || *optarg == '\0' || *e != '\0')
472 errx(1, "illegal preload value -- %s", optarg);
475 #ifdef IPV6_USE_MIN_MTU
479 errx(1, "-%c is not supported on this platform", ch);
483 options &= ~F_HOSTNAME;
486 options |= F_NIGROUP;
488 case 'p': /* fill buffer with user pattern */
489 options |= F_PINGFILLED;
490 fill((char *)datap, optarg);
496 memset(&hints, 0, sizeof(struct addrinfo));
497 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
498 hints.ai_family = AF_INET6;
499 hints.ai_socktype = SOCK_RAW;
500 hints.ai_protocol = IPPROTO_ICMPV6;
502 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
504 errx(1, "invalid source address: %s",
505 gai_strerror(ret_ga));
508 * res->ai_family must be AF_INET6 and res->ai_addrlen
509 * must be sizeof(src).
511 memcpy(&src, res->ai_addr, res->ai_addrlen);
512 srclen = res->ai_addrlen;
514 options |= F_SRCADDR;
516 case 's': /* size of packet to send */
517 datalen = strtol(optarg, &e, 10);
518 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
519 errx(1, "illegal datalen value -- %s", optarg);
520 if (datalen > MAXDATALEN) {
522 "datalen value too large, maximum is %d",
527 options &= ~F_NOUSERDATA;
528 options |= F_SUPTYPES;
531 options |= F_VERBOSE;
534 options &= ~F_NOUSERDATA;
538 options &= ~F_NOUSERDATA;
539 options |= F_FQDNOLD;
542 #ifdef IPSEC_POLICY_IPSEC
545 if (!strncmp("in", optarg, 2)) {
546 if ((policy_in = strdup(optarg)) == NULL)
548 } else if (!strncmp("out", optarg, 3)) {
549 if ((policy_out = strdup(optarg)) == NULL)
552 errx(1, "invalid security policy");
556 options |= F_AUTHHDR;
559 options |= F_ENCRYPT;
561 #endif /*IPSEC_POLICY_IPSEC*/
578 #ifdef IPV6_RECVRTHDR /* 2292bis */
579 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
582 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
585 errx(1, "too many intermediate hops");
591 if (options & F_NIGROUP) {
592 target = nigroup(argv[argc - 1]);
593 if (target == NULL) {
598 target = argv[argc - 1];
601 memset(&hints, 0, sizeof(struct addrinfo));
602 hints.ai_flags = AI_CANONNAME;
603 hints.ai_family = AF_INET6;
604 hints.ai_socktype = SOCK_RAW;
605 hints.ai_protocol = IPPROTO_ICMPV6;
607 ret_ga = getaddrinfo(target, NULL, &hints, &res);
609 errx(1, "%s", gai_strerror(ret_ga));
610 if (res->ai_canonname)
611 hostname = res->ai_canonname;
616 errx(1, "getaddrinfo failed");
618 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
620 if ((s = socket(res->ai_family, res->ai_socktype,
621 res->ai_protocol)) < 0)
624 /* set the source address if specified. */
625 if ((options & F_SRCADDR) &&
626 bind(s, (struct sockaddr *)&src, srclen) != 0) {
630 /* set the gateway (next hop) if specified */
632 struct addrinfo ghints, *gres;
635 memset(&ghints, 0, sizeof(ghints));
636 ghints.ai_family = AF_INET6;
637 ghints.ai_socktype = SOCK_RAW;
638 ghints.ai_protocol = IPPROTO_ICMPV6;
640 error = getaddrinfo(gateway, NULL, &hints, &gres);
642 errx(1, "getaddrinfo for the gateway %s: %s",
643 gateway, gai_strerror(error));
645 if (gres->ai_next && (options & F_VERBOSE))
646 warnx("gateway resolves to multiple addresses");
648 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
649 gres->ai_addr, gres->ai_addrlen)) {
650 err(1, "setsockopt(IPV6_NEXTHOP)");
657 * let the kerel pass extension headers of incoming packets,
658 * for privileged socket options
660 if ((options & F_VERBOSE) != 0) {
663 #ifdef IPV6_RECVHOPOPTS
664 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
666 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
667 #else /* old adv. API */
668 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
670 err(1, "setsockopt(IPV6_HOPOPTS)");
672 #ifdef IPV6_RECVDSTOPTS
673 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
675 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
676 #else /* old adv. API */
677 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
679 err(1, "setsockopt(IPV6_DSTOPTS)");
681 #ifdef IPV6_RECVRTHDRDSTOPTS
682 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
684 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
688 /* revoke root privilege */
692 if ((options & F_FLOOD) && (options & F_INTERVAL))
693 errx(1, "-f and -i incompatible options");
695 if ((options & F_NOUSERDATA) == 0) {
696 if (datalen >= sizeof(struct tv32)) {
697 /* we can time transfer */
701 /* in F_VERBOSE case, we may get non-echoreply packets*/
702 if (options & F_VERBOSE)
703 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
705 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
707 /* suppress timing for node information query */
710 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
713 if (!(packet = (u_char *)malloc((u_int)packlen)))
714 err(1, "Unable to allocate packet");
715 if (!(options & F_PINGFILLED))
716 for (i = ICMP6ECHOLEN; i < packlen; ++i)
719 ident = getpid() & 0xFFFF;
720 #ifndef HAVE_ARC4RANDOM
721 gettimeofday(&seed, NULL);
722 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
723 memset(nonce, 0, sizeof(nonce));
724 for (i = 0; i < sizeof(nonce); i += sizeof(int))
725 *((int *)&nonce[i]) = rand();
727 memset(nonce, 0, sizeof(nonce));
728 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
729 *((u_int32_t *)&nonce[i]) = arc4random();
734 if (options & F_SO_DEBUG)
735 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
737 optval = IPV6_DEFHLIM;
738 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
739 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
740 &optval, sizeof(optval)) == -1)
741 err(1, "IPV6_MULTICAST_HOPS");
742 #ifdef IPV6_USE_MIN_MTU
744 optval = mflag > 1 ? 0 : 1;
746 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
747 &optval, sizeof(optval)) == -1)
748 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
750 #ifdef IPV6_RECVPATHMTU
753 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
754 &optval, sizeof(optval)) == -1)
755 err(1, "setsockopt(IPV6_RECVPATHMTU)");
757 #endif /* IPV6_RECVPATHMTU */
758 #endif /* IPV6_USE_MIN_MTU */
761 #ifdef IPSEC_POLICY_IPSEC
762 if (options & F_POLICY) {
763 if (setpolicy(s, policy_in) < 0)
764 errx(1, "%s", ipsec_strerror());
765 if (setpolicy(s, policy_out) < 0)
766 errx(1, "%s", ipsec_strerror());
769 if (options & F_AUTHHDR) {
770 optval = IPSEC_LEVEL_REQUIRE;
771 #ifdef IPV6_AUTH_TRANS_LEVEL
772 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
773 &optval, sizeof(optval)) == -1)
774 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
776 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
777 &optval, sizeof(optval)) == -1)
778 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
781 if (options & F_ENCRYPT) {
782 optval = IPSEC_LEVEL_REQUIRE;
783 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
784 &optval, sizeof(optval)) == -1)
785 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
787 #endif /*IPSEC_POLICY_IPSEC*/
792 struct icmp6_filter filt;
793 if (!(options & F_VERBOSE)) {
794 ICMP6_FILTER_SETBLOCKALL(&filt);
795 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
796 (options & F_NODEADDR) || (options & F_SUPTYPES))
797 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
799 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
801 ICMP6_FILTER_SETPASSALL(&filt);
803 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
805 err(1, "setsockopt(ICMP6_FILTER)");
807 #endif /*ICMP6_FILTER*/
809 /* let the kerel pass extension headers of incoming packets */
810 if ((options & F_VERBOSE) != 0) {
813 #ifdef IPV6_RECVRTHDR
814 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
816 err(1, "setsockopt(IPV6_RECVRTHDR)");
817 #else /* old adv. API */
818 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
820 err(1, "setsockopt(IPV6_RTHDR)");
826 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
827 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
828 &optval, sizeof(optval)) == -1)
829 err(1, "IPV6_MULTICAST_LOOP");
832 /* Specify the outgoing interface and/or the source address */
834 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
837 ip6optlen += CMSG_SPACE(sizeof(int));
839 /* set IP6 packet options */
841 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
842 errx(1, "can't allocate enough memory");
843 smsghdr.msg_control = (caddr_t)scmsg;
844 smsghdr.msg_controllen = ip6optlen;
845 scmsgp = (struct cmsghdr *)scmsg;
848 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
849 memset(pktinfo, 0, sizeof(*pktinfo));
850 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
851 scmsgp->cmsg_level = IPPROTO_IPV6;
852 scmsgp->cmsg_type = IPV6_PKTINFO;
853 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
856 /* set the outgoing interface */
858 #ifndef USE_SIN6_SCOPE_ID
859 /* pktinfo must have already been allocated */
860 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
861 errx(1, "%s: invalid interface name", ifname);
863 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
864 errx(1, "%s: invalid interface name", ifname);
867 if (hoplimit != -1) {
868 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
869 scmsgp->cmsg_level = IPPROTO_IPV6;
870 scmsgp->cmsg_type = IPV6_HOPLIMIT;
871 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
873 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
876 if (argc > 1) { /* some intermediate addrs are specified */
878 #ifdef USE_RFC2292BIS
882 #ifdef USE_RFC2292BIS
883 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
884 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
885 scmsgp->cmsg_level = IPPROTO_IPV6;
886 scmsgp->cmsg_type = IPV6_RTHDR;
887 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
888 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
889 IPV6_RTHDR_TYPE_0, argc - 1);
891 errx(1, "can't initialize rthdr");
892 #else /* old advanced API */
893 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
894 IPV6_RTHDR_TYPE_0)) == 0)
895 errx(1, "can't initialize rthdr");
896 #endif /* USE_RFC2292BIS */
898 for (hops = 0; hops < argc - 1; hops++) {
899 struct addrinfo *iaip;
901 if ((error = getaddrinfo(argv[hops], NULL, &hints,
903 errx(1, "%s", gai_strerror(error));
904 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
906 "bad addr family of an intermediate addr");
908 #ifdef USE_RFC2292BIS
909 if (inet6_rth_add(rthdr,
910 &(SIN6(iaip->ai_addr))->sin6_addr))
911 errx(1, "can't add an intermediate node");
912 #else /* old advanced API */
913 if (inet6_rthdr_add(scmsgp,
914 &(SIN6(iaip->ai_addr))->sin6_addr,
916 errx(1, "can't add an intermediate node");
917 #endif /* USE_RFC2292BIS */
921 #ifndef USE_RFC2292BIS
922 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
923 errx(1, "can't set the last flag");
926 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
929 if (!(options & F_SRCADDR)) {
931 * get the source address. XXX since we revoked the root
932 * privilege, we cannot use a raw socket for this.
935 socklen_t len = sizeof(src);
937 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
938 err(1, "UDP socket");
940 src.sin6_family = AF_INET6;
941 src.sin6_addr = dst.sin6_addr;
942 src.sin6_port = ntohs(DUMMY_PORT);
943 src.sin6_scope_id = dst.sin6_scope_id;
945 #ifdef USE_RFC2292BIS
947 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
948 (void *)pktinfo, sizeof(*pktinfo)))
949 err(1, "UDP setsockopt(IPV6_PKTINFO)");
951 if (hoplimit != -1 &&
952 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
953 (void *)&hoplimit, sizeof(hoplimit)))
954 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
956 if (hoplimit != -1 &&
957 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
958 (void *)&hoplimit, sizeof(hoplimit)))
959 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
962 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
963 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
964 err(1, "UDP setsockopt(IPV6_RTHDR)");
965 #else /* old advanced API */
966 if (smsghdr.msg_control &&
967 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
968 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
969 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
972 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
973 err(1, "UDP connect");
975 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
976 err(1, "getsockname");
981 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
983 if (datalen > sockbufsize)
984 warnx("you need -b to increase socket buffer size");
985 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
986 sizeof(sockbufsize)) < 0)
987 err(1, "setsockopt(SO_SNDBUF)");
988 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
989 sizeof(sockbufsize)) < 0)
990 err(1, "setsockopt(SO_RCVBUF)");
993 if (datalen > 8 * 1024) /*XXX*/
994 warnx("you need -b to increase socket buffer size");
996 * When pinging the broadcast address, you can get a lot of
997 * answers. Doing something so evil is useful if you are trying
998 * to stress the ethernet, or just want to fill the arp cache
999 * to get some stuff for /etc/ethers.
1002 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1008 #ifndef USE_SIN6_SCOPE_ID
1009 #ifdef IPV6_RECVPKTINFO
1010 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1011 sizeof(optval)) < 0)
1012 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1013 #else /* old adv. API */
1014 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1015 sizeof(optval)) < 0)
1016 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1018 #endif /* USE_SIN6_SCOPE_ID */
1019 #ifdef IPV6_RECVHOPLIMIT
1020 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1021 sizeof(optval)) < 0)
1022 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1023 #else /* old adv. API */
1024 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1025 sizeof(optval)) < 0)
1026 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1029 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1030 (unsigned long)(pingerlen() - 8));
1031 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1032 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1034 while (preload--) /* Fire off them quickies. */
1037 (void)signal(SIGINT, onsignal);
1039 (void)signal(SIGINFO, onsignal);
1042 if ((options & F_FLOOD) == 0) {
1043 (void)signal(SIGALRM, onsignal);
1044 itimer.it_interval = interval;
1045 itimer.it_value = interval;
1046 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1047 if (ntransmitted == 0)
1052 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
1053 if ((fdmaskp = malloc(fdmasks)) == NULL)
1057 seenalrm = seenint = 0;
1062 /* For control (ancillary) data received from recvmsg() */
1063 cm = (struct cmsghdr *)malloc(CONTROLLEN);
1069 struct iovec iov[2];
1071 /* signal handling */
1090 if (options & F_FLOOD) {
1096 timeout.tv_usec = 10000;
1108 fdmaskp[0].events = POLLIN;
1109 cc = poll(fdmaskp, 1, timeout);
1111 memset(fdmaskp, 0, fdmasks);
1113 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1116 if (errno != EINTR) {
1128 m.msg_name = (caddr_t)&from;
1129 m.msg_namelen = sizeof(from);
1130 memset(&iov, 0, sizeof(iov));
1131 iov[0].iov_base = (caddr_t)packet;
1132 iov[0].iov_len = packlen;
1135 memset(cm, 0, CONTROLLEN);
1136 m.msg_control = (void *)cm;
1137 m.msg_controllen = CONTROLLEN;
1139 cc = recvmsg(s, &m, 0);
1141 if (errno != EINTR) {
1146 } else if (cc == 0) {
1150 * receive control messages only. Process the
1151 * exceptions (currently the only possiblity is
1152 * a path MTU notification.)
1154 if ((mtu = get_pathmtu(&m)) > 0) {
1155 if ((options & F_VERBOSE) != 0) {
1156 printf("new path MTU (%d) is "
1163 * an ICMPv6 message (probably an echoreply) arrived.
1165 pr_pack(packet, cc, &m);
1167 if (npackets && nreceived >= npackets)
1171 exit(nreceived == 0);
1196 * This routine transmits another ping6.
1201 struct itimerval itimer;
1207 * If we're not transmitting any more packets, change the timer
1208 * to wait two round-trip times if we've received any packets or
1209 * ten seconds if we haven't.
1213 itimer.it_value.tv_sec = 2 * tmax / 1000;
1214 if (itimer.it_value.tv_sec == 0)
1215 itimer.it_value.tv_sec = 1;
1217 itimer.it_value.tv_sec = MAXWAIT;
1218 itimer.it_interval.tv_sec = 0;
1219 itimer.it_interval.tv_usec = 0;
1220 itimer.it_value.tv_usec = 0;
1222 (void)signal(SIGALRM, onint);
1223 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1228 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1229 * will be added on by the kernel. The ID field is our UNIX process ID,
1230 * and the sequence number is an ascending integer. The first 8 bytes
1231 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1232 * byte-order, to compute the round-trip time.
1239 if (options & F_FQDN)
1240 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1241 else if (options & F_FQDNOLD)
1243 else if (options & F_NODEADDR)
1244 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1245 else if (options & F_SUPTYPES)
1248 l = ICMP6ECHOLEN + datalen;
1256 struct icmp6_hdr *icp;
1257 struct iovec iov[2];
1259 struct icmp6_nodeinfo *nip;
1262 if (npackets && ntransmitted >= npackets)
1263 return(-1); /* no more transmission */
1265 icp = (struct icmp6_hdr *)outpack;
1266 nip = (struct icmp6_nodeinfo *)outpack;
1267 memset(icp, 0, sizeof(*icp));
1268 icp->icmp6_cksum = 0;
1269 seq = ntransmitted++;
1270 CLR(seq % mx_dup_ck);
1272 if (options & F_FQDN) {
1273 icp->icmp6_type = ICMP6_NI_QUERY;
1274 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1275 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1276 nip->ni_flags = htons(0);
1278 memcpy(nip->icmp6_ni_nonce, nonce,
1279 sizeof(nip->icmp6_ni_nonce));
1280 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1282 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1283 sizeof(dst.sin6_addr));
1284 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1286 } else if (options & F_FQDNOLD) {
1287 /* packet format in 03 draft - no Subject data on queries */
1288 icp->icmp6_type = ICMP6_NI_QUERY;
1289 icp->icmp6_code = 0; /* code field is always 0 */
1290 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1291 nip->ni_flags = htons(0);
1293 memcpy(nip->icmp6_ni_nonce, nonce,
1294 sizeof(nip->icmp6_ni_nonce));
1295 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1299 } else if (options & F_NODEADDR) {
1300 icp->icmp6_type = ICMP6_NI_QUERY;
1301 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1302 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1303 nip->ni_flags = naflags;
1305 memcpy(nip->icmp6_ni_nonce, nonce,
1306 sizeof(nip->icmp6_ni_nonce));
1307 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1309 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1310 sizeof(dst.sin6_addr));
1311 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1313 } else if (options & F_SUPTYPES) {
1314 icp->icmp6_type = ICMP6_NI_QUERY;
1315 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1316 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1317 /* we support compressed bitmap */
1318 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1320 memcpy(nip->icmp6_ni_nonce, nonce,
1321 sizeof(nip->icmp6_ni_nonce));
1322 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1326 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1327 icp->icmp6_code = 0;
1328 icp->icmp6_id = htons(ident);
1329 icp->icmp6_seq = ntohs(seq);
1333 (void)gettimeofday(&tv, NULL);
1334 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1335 tv32->tv32_sec = htonl(tv.tv_sec);
1336 tv32->tv32_usec = htonl(tv.tv_usec);
1338 cc = ICMP6ECHOLEN + datalen;
1342 if (pingerlen() != cc)
1343 errx(1, "internal error; length mismatch");
1346 smsghdr.msg_name = (caddr_t)&dst;
1347 smsghdr.msg_namelen = sizeof(dst);
1348 memset(&iov, 0, sizeof(iov));
1349 iov[0].iov_base = (caddr_t)outpack;
1350 iov[0].iov_len = cc;
1351 smsghdr.msg_iov = iov;
1352 smsghdr.msg_iovlen = 1;
1354 i = sendmsg(s, &smsghdr, 0);
1356 if (i < 0 || i != cc) {
1359 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1362 if (!(options & F_QUIET) && options & F_FLOOD)
1363 (void)write(STDOUT_FILENO, &DOT, 1);
1370 const struct icmp6_hdr *icp;
1372 if (ntohs(icp->icmp6_id) == ident)
1380 const struct icmp6_nodeinfo *nip;
1382 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1383 nonce + sizeof(u_int16_t),
1384 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1391 dnsdecode(sp, ep, base, buf, bufsiz)
1394 const u_char *base; /*base for compressed name*/
1400 char cresult[MAXDNAME + 1];
1411 if (i == 0 || cp != *sp) {
1412 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1413 return NULL; /*result overrun*/
1419 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1420 /* DNS compression */
1424 comp = base + (i & 0x3f);
1425 if (dnsdecode(&comp, cp, base, cresult,
1426 sizeof(cresult)) == NULL)
1428 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1429 return NULL; /*result overrun*/
1431 } else if ((i & 0x3f) == i) {
1433 return NULL; /*source overrun*/
1434 while (i-- > 0 && cp < ep) {
1435 l = snprintf(cresult, sizeof(cresult),
1436 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1437 if (l >= sizeof(cresult) || l < 0)
1439 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1440 return NULL; /*result overrun*/
1444 return NULL; /*invalid label*/
1447 return NULL; /*not terminated*/
1455 * Print out the packet, if it came from us. This logic is necessary
1456 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1457 * which arrive ('tis only fair). This permits multiple copies of this
1458 * program to be run without having intermingled output (or statistics!).
1461 pr_pack(buf, cc, mhdr)
1464 struct msghdr *mhdr;
1466 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1467 struct icmp6_hdr *icp;
1468 struct icmp6_nodeinfo *ni;
1471 struct sockaddr *from;
1473 u_char *cp = NULL, *dp, *end = buf + cc;
1474 struct in6_pktinfo *pktinfo = NULL;
1475 struct timeval tv, tp;
1477 double triptime = 0;
1482 char dnsname[MAXDNAME + 1];
1484 (void)gettimeofday(&tv, NULL);
1486 if (!mhdr || !mhdr->msg_name ||
1487 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1488 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1489 if (options & F_VERBOSE)
1490 warnx("invalid peername");
1493 from = (struct sockaddr *)mhdr->msg_name;
1494 fromlen = mhdr->msg_namelen;
1495 if (cc < sizeof(struct icmp6_hdr)) {
1496 if (options & F_VERBOSE)
1497 warnx("packet too short (%d bytes) from %s", cc,
1498 pr_addr(from, fromlen));
1501 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1502 (options & F_VERBOSE) != 0)
1503 warnx("some control data discarded, insufficient buffer size");
1504 icp = (struct icmp6_hdr *)buf;
1505 ni = (struct icmp6_nodeinfo *)buf;
1508 if ((hoplim = get_hoplim(mhdr)) == -1) {
1509 warnx("failed to get receiving hop limit");
1512 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1513 warnx("failed to get receiving packet information");
1517 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1518 seq = ntohs(icp->icmp6_seq);
1521 tpp = (struct tv32 *)(icp + 1);
1522 tp.tv_sec = ntohl(tpp->tv32_sec);
1523 tp.tv_usec = ntohl(tpp->tv32_usec);
1525 triptime = ((double)tv.tv_sec) * 1000.0 +
1526 ((double)tv.tv_usec) / 1000.0;
1528 tsumsq += triptime * triptime;
1529 if (triptime < tmin)
1531 if (triptime > tmax)
1535 if (TST(seq % mx_dup_ck)) {
1540 SET(seq % mx_dup_ck);
1544 if (options & F_QUIET)
1547 if (options & F_FLOOD)
1548 (void)write(STDOUT_FILENO, &BSPACE, 1);
1550 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1551 pr_addr(from, fromlen), seq);
1552 (void)printf(" hlim=%d", hoplim);
1553 if ((options & F_VERBOSE) != 0) {
1554 struct sockaddr_in6 dstsa;
1556 memset(&dstsa, 0, sizeof(dstsa));
1557 dstsa.sin6_family = AF_INET6;
1558 dstsa.sin6_len = sizeof(dstsa);
1559 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1560 dstsa.sin6_addr = pktinfo->ipi6_addr;
1561 (void)printf(" dst=%s",
1562 pr_addr((struct sockaddr *)&dstsa,
1566 (void)printf(" time=%.3f ms", triptime);
1568 (void)printf("(DUP!)");
1569 /* check the data */
1570 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1571 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1572 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1574 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1579 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1580 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1582 if (TST(seq % mx_dup_ck)) {
1587 SET(seq % mx_dup_ck);
1591 if (options & F_QUIET)
1594 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1596 switch (ntohs(ni->ni_code)) {
1597 case ICMP6_NI_SUCCESS:
1599 case ICMP6_NI_REFUSED:
1600 printf("refused, type 0x%x", ntohs(ni->ni_type));
1602 case ICMP6_NI_UNKNOWN:
1603 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1606 printf("unknown code 0x%x, type 0x%x",
1607 ntohs(ni->ni_code), ntohs(ni->ni_type));
1611 switch (ntohs(ni->ni_qtype)) {
1613 printf("NodeInfo NOOP");
1615 case NI_QTYPE_SUPTYPES:
1616 pr_suptypes(ni, end - (u_char *)ni);
1618 case NI_QTYPE_NODEADDR:
1619 pr_nodeaddr(ni, end - (u_char *)ni);
1622 default: /* XXX: for backward compatibility */
1623 cp = (u_char *)ni + ICMP6_NIRLEN;
1624 if (buf[off + ICMP6_NIRLEN] ==
1625 cc - off - ICMP6_NIRLEN - 1)
1630 cp++; /* skip length */
1632 safeputc(*cp & 0xff);
1638 if (dnsdecode((const u_char **)&cp, end,
1639 (const u_char *)(ni + 1), dnsname,
1640 sizeof(dnsname)) == NULL) {
1645 * name-lookup special handling for
1648 if (cp + 1 <= end && !*cp &&
1649 strlen(dnsname) > 0) {
1650 dnsname[strlen(dnsname) - 1] = '\0';
1653 printf("%s%s", i > 0 ? "," : "",
1657 if (options & F_VERBOSE) {
1661 (void)printf(" ("); /*)*/
1663 switch (ni->ni_code) {
1664 case ICMP6_NI_REFUSED:
1665 (void)printf("refused");
1668 case ICMP6_NI_UNKNOWN:
1669 (void)printf("unknown qtype");
1674 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1675 /* case of refusion, unknown */
1680 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1683 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1684 (void)printf("TTL=%d:meaningless",
1688 (void)printf("TTL=%d:invalid",
1691 (void)printf("TTL=%d", ttl);
1701 cp = (u_char *)ni + ICMP6_NIRLEN;
1710 if (buf[off + ICMP6_NIRLEN] !=
1711 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1714 (void)printf("invalid namelen:%d/%lu",
1715 buf[off + ICMP6_NIRLEN],
1716 (u_long)cc - off - ICMP6_NIRLEN - 1);
1726 /* We've got something other than an ECHOREPLY */
1727 if (!(options & F_VERBOSE))
1729 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1733 if (!(options & F_FLOOD)) {
1734 (void)putchar('\n');
1735 if (options & F_VERBOSE)
1737 (void)fflush(stdout);
1744 struct msghdr *mhdr;
1751 bufp = mhdr->msg_control;
1752 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1753 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1754 if (cm->cmsg_level != IPPROTO_IPV6)
1757 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1760 switch (cm->cmsg_type) {
1762 printf(" HbH Options: ");
1763 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1766 #ifdef IPV6_RTHDRDSTOPTS
1767 case IPV6_RTHDRDSTOPTS:
1769 printf(" Dst Options: ");
1770 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1773 printf(" Routing: ");
1774 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1780 #ifdef USE_RFC2292BIS
1782 pr_ip6opt(void *extbuf, size_t bufsize)
1784 struct ip6_hbh *ext;
1787 socklen_t extlen, len, origextlen;
1793 ext = (struct ip6_hbh *)extbuf;
1794 extlen = (ext->ip6h_len + 1) * 8;
1795 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1796 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1799 * Bounds checking on the ancillary data buffer:
1800 * subtract the size of a cmsg structure from the buffer size.
1802 if (bufsize < (extlen + CMSG_SPACE(0))) {
1803 origextlen = extlen;
1804 extlen = bufsize - CMSG_SPACE(0);
1805 warnx("options truncated, showing only %u (total=%u)",
1806 (unsigned int)(extlen / 8 - 1),
1807 (unsigned int)(ext->ip6h_len));
1812 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1813 &type, &len, &databuf);
1814 if (currentlen == -1)
1818 * Note that inet6_opt_next automatically skips any padding
1823 offset = inet6_opt_get_val(databuf, offset,
1824 &value4, sizeof(value4));
1825 printf(" Jumbo Payload Opt: Length %u\n",
1826 (u_int32_t)ntohl(value4));
1828 case IP6OPT_ROUTER_ALERT:
1830 offset = inet6_opt_get_val(databuf, offset,
1831 &value2, sizeof(value2));
1832 printf(" Router Alert Opt: Type %u\n",
1836 printf(" Received Opt %u len %lu\n",
1837 type, (unsigned long)len);
1843 #else /* !USE_RFC2292BIS */
1846 pr_ip6opt(void *extbuf, size_t bufsize __unused)
1851 #endif /* USE_RFC2292BIS */
1853 #ifdef USE_RFC2292BIS
1855 pr_rthdr(void *extbuf, size_t bufsize)
1857 struct in6_addr *in6;
1858 char ntopbuf[INET6_ADDRSTRLEN];
1859 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1860 int i, segments, origsegs, rthsize, size0, size1;
1862 /* print fixed part of the header */
1863 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1864 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1865 if ((segments = inet6_rth_segments(extbuf)) >= 0) {
1866 printf("%d segments, ", segments);
1867 printf("%d left\n", rh->ip6r_segleft);
1869 printf("segments unknown, ");
1870 printf("%d left\n", rh->ip6r_segleft);
1875 * Bounds checking on the ancillary data buffer. When calculating
1876 * the number of items to show keep in mind:
1877 * - The size of the cmsg structure
1878 * - The size of one segment (the size of a Type 0 routing header)
1879 * - When dividing add a fudge factor of one in case the
1880 * dividend is not evenly divisible by the divisor
1882 rthsize = (rh->ip6r_len + 1) * 8;
1883 if (bufsize < (rthsize + CMSG_SPACE(0))) {
1884 origsegs = segments;
1885 size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
1886 size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
1887 segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
1888 (size1 - size0) + 1;
1889 warnx("segments truncated, showing only %d (total=%d)",
1890 segments, origsegs);
1893 for (i = 0; i < segments; i++) {
1894 in6 = inet6_rth_getaddr(extbuf, i);
1896 printf(" [%d]<NULL>\n", i);
1898 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1900 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1901 printf(" [%d]%s\n", i, ntopbuf);
1909 #else /* !USE_RFC2292BIS */
1912 pr_rthdr(void *extbuf, size_t bufsize __unused)
1917 #endif /* USE_RFC2292BIS */
1920 pr_bitrange(v, soff, ii)
1930 /* shift till we have 0x01 */
1931 if ((v & 0x01) == 0) {
1933 printf("-%u", soff + off - 1);
1944 case 0x04: case 0x0c:
1955 /* we have 0x01 with us */
1956 for (i = 0; i < 32 - off; i++) {
1957 if ((v & (0x01 << i)) == 0)
1961 printf(" %u", soff + off);
1969 pr_suptypes(ni, nilen)
1970 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
1975 const u_char *cp, *end;
1978 u_int16_t words; /*32bit count*/
1981 #define MAXQTYPES (1 << 16)
1985 cp = (u_char *)(ni + 1);
1986 end = ((u_char *)ni) + nilen;
1990 printf("NodeInfo Supported Qtypes");
1991 if (options & F_VERBOSE) {
1992 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1993 printf(", compressed bitmap");
1995 printf(", raw bitmap");
1999 clen = (size_t)(end - cp);
2000 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
2001 if (clen == 0 || clen > MAXQTYPES / 8 ||
2007 if (clen < sizeof(cbit) || clen % sizeof(v))
2009 memcpy(&cbit, cp, sizeof(cbit));
2010 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
2014 clen = ntohs(cbit.words) * sizeof(v);
2015 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
2020 for (off = 0; off < clen; off += sizeof(v)) {
2021 memcpy(&v, cp + off, sizeof(v));
2022 v = (u_int32_t)ntohl(v);
2023 b = pr_bitrange(v, (int)(cur + off * 8), b);
2025 /* flush the remaining bits */
2026 b = pr_bitrange(0, (int)(cur + off * 8), b);
2030 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
2031 cur += ntohs(cbit.skip) * 32;
2036 pr_nodeaddr(ni, nilen)
2037 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
2040 u_char *cp = (u_char *)(ni + 1);
2041 char ntop_buf[INET6_ADDRSTRLEN];
2044 nilen -= sizeof(struct icmp6_nodeinfo);
2046 if (options & F_VERBOSE) {
2047 switch (ni->ni_code) {
2048 case ICMP6_NI_REFUSED:
2049 (void)printf("refused");
2051 case ICMP6_NI_UNKNOWN:
2052 (void)printf("unknown qtype");
2055 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2056 (void)printf(" truncated");
2060 printf(" no address\n");
2063 * In icmp-name-lookups 05 and later, TTL of each returned address
2064 * is contained in the resposne. We try to detect the version
2065 * by the length of the data, but note that the detection algorithm
2066 * is incomplete. We assume the latest draft by default.
2068 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2074 /* XXX: alignment? */
2075 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2076 cp += sizeof(u_int32_t);
2077 nilen -= sizeof(u_int32_t);
2080 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2082 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2083 printf(" %s", ntop_buf);
2085 if (ttl == 0xffffffff) {
2087 * XXX: can this convention be applied to all
2088 * type of TTL (i.e. non-ND TTL)?
2090 printf("(TTL=infty)");
2093 printf("(TTL=%u)", ttl);
2097 nilen -= sizeof(struct in6_addr);
2098 cp += sizeof(struct in6_addr);
2104 struct msghdr *mhdr;
2108 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2109 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2110 if (cm->cmsg_len == 0)
2113 if (cm->cmsg_level == IPPROTO_IPV6 &&
2114 cm->cmsg_type == IPV6_HOPLIMIT &&
2115 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2116 return(*(int *)CMSG_DATA(cm));
2122 struct in6_pktinfo *
2123 get_rcvpktinfo(mhdr)
2124 struct msghdr *mhdr;
2128 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2129 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2130 if (cm->cmsg_len == 0)
2133 if (cm->cmsg_level == IPPROTO_IPV6 &&
2134 cm->cmsg_type == IPV6_PKTINFO &&
2135 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2136 return((struct in6_pktinfo *)CMSG_DATA(cm));
2144 struct msghdr *mhdr;
2146 #ifdef IPV6_RECVPATHMTU
2148 struct ip6_mtuinfo *mtuctl = NULL;
2150 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2151 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2152 if (cm->cmsg_len == 0)
2155 if (cm->cmsg_level == IPPROTO_IPV6 &&
2156 cm->cmsg_type == IPV6_PATHMTU &&
2157 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2158 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2161 * If the notified destination is different from
2162 * the one we are pinging, just ignore the info.
2163 * We check the scope ID only when both notified value
2164 * and our own value have non-0 values, because we may
2165 * have used the default scope zone ID for sending,
2166 * in which case the scope ID value is 0.
2168 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2170 (mtuctl->ip6m_addr.sin6_scope_id &&
2171 dst.sin6_scope_id &&
2172 mtuctl->ip6m_addr.sin6_scope_id !=
2173 dst.sin6_scope_id)) {
2174 if ((options & F_VERBOSE) != 0) {
2175 printf("path MTU for %s is notified. "
2177 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2178 sizeof(mtuctl->ip6m_addr)));
2184 * Ignore an invalid MTU. XXX: can we just believe
2187 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2190 /* notification for our destination. return the MTU. */
2191 return((int)mtuctl->ip6m_mtu);
2200 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2205 struct timeval *out, *in;
2207 if ((out->tv_usec -= in->tv_usec) < 0) {
2209 out->tv_usec += 1000000;
2211 out->tv_sec -= in->tv_sec;
2225 (void)signal(SIGINT, SIG_DFL);
2226 (void)kill(getpid(), SIGINT);
2234 * Print out statistics.
2240 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2241 (void)printf("%ld packets transmitted, ", ntransmitted);
2242 (void)printf("%ld packets received, ", nreceived);
2244 (void)printf("+%ld duplicates, ", nrepeats);
2246 if (nreceived > ntransmitted)
2247 (void)printf("-- somebody's duplicating packets!");
2249 (void)printf("%.1f%% packet loss",
2250 ((((double)ntransmitted - nreceived) * 100.0) /
2253 (void)putchar('\n');
2254 if (nreceived && timing) {
2255 /* Only display average to microseconds */
2256 double num = nreceived + nrepeats;
2257 double avg = tsum / num;
2258 double dev = sqrt(tsumsq / num - avg * avg);
2260 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2261 tmin, avg, tmax, dev);
2262 (void)fflush(stdout);
2264 (void)fflush(stdout);
2268 static const char *niqcode[] = {
2270 "DNS label", /*or empty*/
2275 static const char *nircode[] = {
2276 "Success", "Refused", "Unknown",
2282 * Print a descriptive string about an ICMP header.
2286 struct icmp6_hdr *icp;
2289 char ntop_buf[INET6_ADDRSTRLEN];
2290 struct nd_redirect *red;
2291 struct icmp6_nodeinfo *ni;
2292 char dnsname[MAXDNAME + 1];
2296 switch (icp->icmp6_type) {
2297 case ICMP6_DST_UNREACH:
2298 switch (icp->icmp6_code) {
2299 case ICMP6_DST_UNREACH_NOROUTE:
2300 (void)printf("No Route to Destination\n");
2302 case ICMP6_DST_UNREACH_ADMIN:
2303 (void)printf("Destination Administratively "
2306 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2307 (void)printf("Destination Unreachable Beyond Scope\n");
2309 case ICMP6_DST_UNREACH_ADDR:
2310 (void)printf("Destination Host Unreachable\n");
2312 case ICMP6_DST_UNREACH_NOPORT:
2313 (void)printf("Destination Port Unreachable\n");
2316 (void)printf("Destination Unreachable, Bad Code: %d\n",
2320 /* Print returned IP header information */
2321 pr_retip((struct ip6_hdr *)(icp + 1), end);
2323 case ICMP6_PACKET_TOO_BIG:
2324 (void)printf("Packet too big mtu = %d\n",
2325 (int)ntohl(icp->icmp6_mtu));
2326 pr_retip((struct ip6_hdr *)(icp + 1), end);
2328 case ICMP6_TIME_EXCEEDED:
2329 switch (icp->icmp6_code) {
2330 case ICMP6_TIME_EXCEED_TRANSIT:
2331 (void)printf("Time to live exceeded\n");
2333 case ICMP6_TIME_EXCEED_REASSEMBLY:
2334 (void)printf("Frag reassembly time exceeded\n");
2337 (void)printf("Time exceeded, Bad Code: %d\n",
2341 pr_retip((struct ip6_hdr *)(icp + 1), end);
2343 case ICMP6_PARAM_PROB:
2344 (void)printf("Parameter problem: ");
2345 switch (icp->icmp6_code) {
2346 case ICMP6_PARAMPROB_HEADER:
2347 (void)printf("Erroneous Header ");
2349 case ICMP6_PARAMPROB_NEXTHEADER:
2350 (void)printf("Unknown Nextheader ");
2352 case ICMP6_PARAMPROB_OPTION:
2353 (void)printf("Unrecognized Option ");
2356 (void)printf("Bad code(%d) ", icp->icmp6_code);
2359 (void)printf("pointer = 0x%02x\n",
2360 (u_int32_t)ntohl(icp->icmp6_pptr));
2361 pr_retip((struct ip6_hdr *)(icp + 1), end);
2363 case ICMP6_ECHO_REQUEST:
2364 (void)printf("Echo Request");
2365 /* XXX ID + Seq + Data */
2367 case ICMP6_ECHO_REPLY:
2368 (void)printf("Echo Reply");
2369 /* XXX ID + Seq + Data */
2371 case ICMP6_MEMBERSHIP_QUERY:
2372 (void)printf("Listener Query");
2374 case ICMP6_MEMBERSHIP_REPORT:
2375 (void)printf("Listener Report");
2377 case ICMP6_MEMBERSHIP_REDUCTION:
2378 (void)printf("Listener Done");
2380 case ND_ROUTER_SOLICIT:
2381 (void)printf("Router Solicitation");
2383 case ND_ROUTER_ADVERT:
2384 (void)printf("Router Advertisement");
2386 case ND_NEIGHBOR_SOLICIT:
2387 (void)printf("Neighbor Solicitation");
2389 case ND_NEIGHBOR_ADVERT:
2390 (void)printf("Neighbor Advertisement");
2393 red = (struct nd_redirect *)icp;
2394 (void)printf("Redirect\n");
2395 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2397 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2398 (void)printf("Destination: %s", ntop_buf);
2399 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2401 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2402 (void)printf(" New Target: %s", ntop_buf);
2404 case ICMP6_NI_QUERY:
2405 (void)printf("Node Information Query");
2406 /* XXX ID + Seq + Data */
2407 ni = (struct icmp6_nodeinfo *)icp;
2408 l = end - (u_char *)(ni + 1);
2410 switch (ntohs(ni->ni_qtype)) {
2412 (void)printf("NOOP");
2414 case NI_QTYPE_SUPTYPES:
2415 (void)printf("Supported qtypes");
2418 (void)printf("DNS name");
2420 case NI_QTYPE_NODEADDR:
2421 (void)printf("nodeaddr");
2423 case NI_QTYPE_IPV4ADDR:
2424 (void)printf("IPv4 nodeaddr");
2427 (void)printf("unknown qtype");
2430 if (options & F_VERBOSE) {
2431 switch (ni->ni_code) {
2432 case ICMP6_NI_SUBJ_IPV6:
2433 if (l == sizeof(struct in6_addr) &&
2434 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2435 sizeof(ntop_buf)) != NULL) {
2436 (void)printf(", subject=%s(%s)",
2437 niqcode[ni->ni_code], ntop_buf);
2440 /* backward compat to -W */
2441 (void)printf(", oldfqdn");
2443 (void)printf(", invalid");
2447 case ICMP6_NI_SUBJ_FQDN:
2448 if (end == (u_char *)(ni + 1)) {
2449 (void)printf(", no subject");
2452 printf(", subject=%s", niqcode[ni->ni_code]);
2453 cp = (const u_char *)(ni + 1);
2454 if (dnsdecode(&cp, end, NULL, dnsname,
2455 sizeof(dnsname)) != NULL)
2456 printf("(%s)", dnsname);
2458 printf("(invalid)");
2460 case ICMP6_NI_SUBJ_IPV4:
2461 if (l == sizeof(struct in_addr) &&
2462 inet_ntop(AF_INET, ni + 1, ntop_buf,
2463 sizeof(ntop_buf)) != NULL) {
2464 (void)printf(", subject=%s(%s)",
2465 niqcode[ni->ni_code], ntop_buf);
2467 (void)printf(", invalid");
2470 (void)printf(", invalid");
2475 case ICMP6_NI_REPLY:
2476 (void)printf("Node Information Reply");
2477 /* XXX ID + Seq + Data */
2478 ni = (struct icmp6_nodeinfo *)icp;
2480 switch (ntohs(ni->ni_qtype)) {
2482 (void)printf("NOOP");
2484 case NI_QTYPE_SUPTYPES:
2485 (void)printf("Supported qtypes");
2488 (void)printf("DNS name");
2490 case NI_QTYPE_NODEADDR:
2491 (void)printf("nodeaddr");
2493 case NI_QTYPE_IPV4ADDR:
2494 (void)printf("IPv4 nodeaddr");
2497 (void)printf("unknown qtype");
2500 if (options & F_VERBOSE) {
2501 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2502 printf(", invalid");
2504 printf(", %s", nircode[ni->ni_code]);
2508 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2514 * Print an IP6 header.
2518 struct ip6_hdr *ip6;
2520 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2522 char ntop_buf[INET6_ADDRSTRLEN];
2524 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2525 tc = (tc >> 4) & 0x0f;
2526 tc |= (ip6->ip6_vfc << 4);
2528 printf("Vr TC Flow Plen Nxt Hlim\n");
2529 printf(" %1x %02x %05x %04x %02x %02x\n",
2530 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2531 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2532 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2533 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2534 printf("%s->", ntop_buf);
2535 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2536 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2537 printf("%s\n", ntop_buf);
2542 * Return an ascii host address as a dotted quad and optionally with
2546 pr_addr(addr, addrlen)
2547 struct sockaddr *addr;
2550 static char buf[NI_MAXHOST];
2553 if ((options & F_HOSTNAME) == 0)
2554 flag |= NI_NUMERICHOST;
2556 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2564 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2568 struct ip6_hdr *ip6;
2571 u_char *cp = (u_char *)ip6, nh;
2574 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2579 hlen = sizeof(*ip6);
2583 while (end - cp >= 8) {
2585 case IPPROTO_HOPOPTS:
2587 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2588 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2590 case IPPROTO_DSTOPTS:
2592 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2593 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2595 case IPPROTO_FRAGMENT:
2597 hlen = sizeof(struct ip6_frag);
2598 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2600 case IPPROTO_ROUTING:
2602 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2603 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2608 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2609 nh = ((struct ah *)cp)->ah_nxt;
2612 case IPPROTO_ICMPV6:
2613 printf("ICMP6: type = %d, code = %d\n",
2620 printf("TCP: from port %u, to port %u (decimal)\n",
2621 (*cp * 256 + *(cp + 1)),
2622 (*(cp + 2) * 256 + *(cp + 3)));
2625 printf("UDP: from port %u, to port %u (decimal)\n",
2626 (*cp * 256 + *(cp + 1)),
2627 (*(cp + 2) * 256 + *(cp + 3)));
2630 printf("Unknown Header(%d)\n", nh);
2634 if ((cp += hlen) >= end)
2656 for (cp = patp; *cp; cp++)
2658 errx(1, "patterns must be specified as hex digits");
2660 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2661 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2662 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2663 &pat[13], &pat[14], &pat[15]);
2668 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);
2670 for (jj = 0; jj < ii; ++jj)
2671 bp[jj + kk] = pat[jj];
2672 if (!(options & F_QUIET)) {
2673 (void)printf("PATTERN: 0x");
2674 for (jj = 0; jj < ii; ++jj)
2675 (void)printf("%02x", bp[jj] & 0xFF);
2681 #ifdef IPSEC_POLICY_IPSEC
2683 setpolicy(so, policy)
2690 return 0; /* ignore */
2692 buf = ipsec_set_policy(policy, strlen(policy));
2694 errx(1, "%s", ipsec_strerror());
2695 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2696 ipsec_get_policylen(buf)) < 0)
2697 warnx("Unable to set IPsec policy");
2712 u_int8_t digest[16];
2715 char hbuf[NI_MAXHOST];
2716 struct in6_addr in6;
2718 p = strchr(name, '.');
2720 p = name + strlen(name);
2722 if (l > 63 || l > sizeof(hbuf) - 1)
2723 return NULL; /*label too long*/
2724 strncpy(hbuf, name, l);
2725 hbuf[(int)l] = '\0';
2727 for (q = name; *q; q++) {
2728 if (isupper(*(unsigned char *)q))
2729 *q = tolower(*(unsigned char *)q);
2732 /* generate 8 bytes of pseudo-random value. */
2733 memset(&ctxt, 0, sizeof(ctxt));
2736 MD5Update(&ctxt, &c, sizeof(c));
2737 MD5Update(&ctxt, (unsigned char *)name, l);
2738 MD5Final(digest, &ctxt);
2740 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2741 return NULL; /*XXX*/
2742 bcopy(digest, &in6.s6_addr[12], 4);
2744 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2747 return strdup(hbuf);
2753 (void)fprintf(stderr,
2754 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2759 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2763 #ifdef IPV6_USE_MIN_MTU
2767 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2768 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2769 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2773 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2774 "[hops ...] host\n");