1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
32 * @(#)ping.c 8.1 (Berkeley) 6/5/93
33 * $FreeBSD: src/sbin/ping6/ping6.c,v 1.30 2007/04/19 15:41:00 mtm Exp $
36 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
39 * Copyright (c) 1989, 1993
40 * The Regents of the University of California. All rights reserved.
42 * This code is derived from software contributed to Berkeley by
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
72 * measure round-trip-delays and packet loss across network paths.
76 * U. S. Army Ballistic Research Laboratory
80 * Public Domain. Distribution Unlimited.
82 * More statistics could always be gathered.
83 * This program has to run SUID to ROOT to access the ICMP socket.
87 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
88 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
89 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
90 * network attached to 1 or more interfaces)
93 #include <sys/param.h>
95 #include <sys/socket.h>
99 #include <net/route.h>
101 #include <netinet/in.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <arpa/inet.h>
105 #include <arpa/nameser.h>
123 #include <netinet6/ah.h>
124 #include <netinet6/ipsec.h>
134 #define MAXPACKETLEN 131072
136 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
137 #define ICMP6ECHOTMLEN sizeof(struct tv32)
138 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
139 #define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
140 /* FQDN case, 64 bits of nonce + 32 bits ttl */
141 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
142 #define EXTRA 256 /* for AH and various other headers. weird. */
143 #define DEFDATALEN ICMP6ECHOTMLEN
144 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
145 #define NROUTES 9 /* number of record route slots */
147 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
148 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
149 #define SET(bit) (A(bit) |= B(bit))
150 #define CLR(bit) (A(bit) &= (~B(bit)))
151 #define TST(bit) (A(bit) & B(bit))
153 #define F_FLOOD 0x0001
154 #define F_INTERVAL 0x0002
155 #define F_PINGFILLED 0x0008
156 #define F_QUIET 0x0010
157 #define F_RROUTE 0x0020
158 #define F_SO_DEBUG 0x0040
159 #define F_VERBOSE 0x0100
161 #ifdef IPSEC_POLICY_IPSEC
162 #define F_POLICY 0x0400
164 #define F_AUTHHDR 0x0200
165 #define F_ENCRYPT 0x0400
166 #endif /*IPSEC_POLICY_IPSEC*/
168 #define F_NODEADDR 0x0800
169 #define F_FQDN 0x1000
170 #define F_INTERFACE 0x2000
171 #define F_SRCADDR 0x4000
172 #define F_HOSTNAME 0x10000
173 #define F_FQDNOLD 0x20000
174 #define F_NIGROUP 0x40000
175 #define F_SUPTYPES 0x80000
176 #define F_NOMINMTU 0x100000
177 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
180 #define IN6LEN sizeof(struct in6_addr)
181 #define SA6LEN sizeof(struct sockaddr_in6)
182 #define DUMMY_PORT 10101
184 #define SIN6(s) ((struct sockaddr_in6 *)(s))
187 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
188 * number of received sequence numbers we can keep track of. Change 128
189 * to 8192 for complete accuracy...
191 #define MAX_DUP_CHK (8 * 8192)
192 int mx_dup_ck = MAX_DUP_CHK;
193 char rcvd_tbl[MAX_DUP_CHK / 8];
195 struct addrinfo *res;
196 struct sockaddr_in6 dst; /* who to ping6 */
197 struct sockaddr_in6 src; /* src addr of this packet */
199 int datalen = DEFDATALEN;
200 int s; /* socket file descriptor */
201 u_char outpack[MAXPACKETLEN];
202 char BSPACE = '\b'; /* characters written for flood */
205 int ident; /* process id to identify our packets */
206 u_int8_t nonce[8]; /* nonce field for node information */
207 int hoplimit = -1; /* hoplimit */
208 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
211 long npackets; /* max packets to transmit */
212 long nreceived; /* # of packets we got back */
213 long nrepeats; /* number of duplicates */
214 long ntransmitted; /* sequence # for outbound packets = #sent */
215 struct timeval interval = {1, 0}; /* interval between packets */
218 int timing; /* flag to do timing */
219 double tmin = 999999999.0; /* minimum round trip time */
220 double tmax = 0.0; /* maximum round trip time */
221 double tsum = 0.0; /* sum of all times, for doing average */
222 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
224 /* for node addresses */
227 /* for ancillary data(advanced API) */
228 struct msghdr smsghdr;
229 struct iovec smsgiov;
232 volatile sig_atomic_t seenalrm;
233 volatile sig_atomic_t seenint;
235 volatile sig_atomic_t seeninfo;
238 int main(int, char *[]);
239 void fill(char *, char *);
240 int get_hoplim(struct msghdr *);
241 #ifdef IPV6_RECVPATHMTU
242 int get_pathmtu(struct msghdr *);
244 #define get_pathmtu(mhdr) (0)
246 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
248 void retransmit(void);
250 size_t pingerlen(void);
252 const char *pr_addr(struct sockaddr *, int);
253 void pr_icmph(struct icmp6_hdr *, u_char *);
254 void pr_iph(struct ip6_hdr *);
255 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
256 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
257 int myechoreply(const struct icmp6_hdr *);
258 int mynireply(const struct icmp6_nodeinfo *);
259 char *dnsdecode(const u_char **, const u_char *, const u_char *,
261 void pr_pack(u_char *, int, struct msghdr *);
262 void pr_exthdrs(struct msghdr *);
263 void pr_ip6opt(void *, size_t);
264 void pr_rthdr(void *, size_t);
265 int pr_bitrange(u_int32_t, int, int);
266 void pr_retip(struct ip6_hdr *, u_char *);
268 void tvsub(struct timeval *, struct timeval *);
269 int setpolicy(int, char *);
270 char *nigroup(char *);
274 main(int argc, char **argv)
276 struct itimerval itimer;
277 struct sockaddr_in6 from;
278 #ifndef HAVE_ARC4RANDOM
284 struct timeval timeout, *tv;
286 struct addrinfo hints;
288 struct pollfd fdmaskp[1];
294 int ch, hold, packlen, preload, optval, ret_ga;
295 u_char *datap, *packet;
296 char *e, *target, *ifname = NULL, *gateway = NULL;
298 struct cmsghdr *scmsgp = NULL;
300 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
305 struct in6_pktinfo *pktinfo = NULL;
307 struct ip6_rthdr *rthdr = NULL;
309 #ifdef IPSEC_POLICY_IPSEC
310 char *policy_in = NULL;
311 char *policy_out = NULL;
313 #ifdef IPV6_USE_MIN_MTU
318 /* just to be sure */
319 memset(&smsghdr, 0, sizeof(smsghdr));
320 memset(&smsgiov, 0, sizeof(smsgiov));
323 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
327 #ifdef IPSEC_POLICY_IPSEC
331 #endif /*IPSEC_POLICY_IPSEC*/
333 while ((ch = getopt(argc, argv,
334 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS)) != -1) {
341 options &= ~F_NOUSERDATA;
342 options |= F_NODEADDR;
343 for (cp = optarg; *cp != '\0'; cp++) {
346 naflags |= NI_NODEADDR_FLAG_ALL;
350 naflags |= NI_NODEADDR_FLAG_COMPAT;
354 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
358 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
362 naflags |= NI_NODEADDR_FLAG_GLOBAL;
364 case 'A': /* experimental. not in the spec */
365 #ifdef NI_NODEADDR_FLAG_ANYCAST
366 naflags |= NI_NODEADDR_FLAG_ANYCAST;
370 "-a A is not supported on the platform");
381 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
384 lsockbufsize = strtoul(optarg, &e, 10);
385 sockbufsize = lsockbufsize;
386 if (errno || !*optarg || *e ||
387 sockbufsize != (long)lsockbufsize)
388 errx(1, "invalid socket buffer size");
391 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
395 npackets = strtol(optarg, &e, 10);
396 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
398 "illegal number of packets -- %s", optarg);
401 options |= F_SO_DEBUG;
406 errx(1, "Must be superuser to flood ping");
409 setbuf(stdout, NULL);
415 options |= F_HOSTNAME;
417 case 'h': /* hoplimit */
418 hoplimit = strtol(optarg, &e, 10);
419 if (*optarg == '\0' || *e != '\0')
420 errx(1, "illegal hoplimit %s", optarg);
421 if (255 < hoplimit || hoplimit < -1)
423 "illegal hoplimit -- %s", optarg);
427 options |= F_INTERFACE;
428 #ifndef USE_SIN6_SCOPE_ID
432 case 'i': /* wait between sending packets */
433 intval = strtod(optarg, &e);
434 if (*optarg == '\0' || *e != '\0')
435 errx(1, "illegal timing interval %s", optarg);
436 if (intval < 1 && getuid()) {
437 errx(1, "%s: only root may use interval < 1s",
440 interval.tv_sec = (long)intval;
442 (long)((intval - interval.tv_sec) * 1000000);
443 if (interval.tv_sec < 0)
444 errx(1, "illegal timing interval %s", optarg);
445 /* less than 1/hz does not make sense */
446 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
447 warnx("too small interval, raised to 0.01");
448 interval.tv_usec = 10000;
450 options |= F_INTERVAL;
455 errx(1, "Must be superuser to preload");
457 preload = strtol(optarg, &e, 10);
458 if (preload < 0 || *optarg == '\0' || *e != '\0')
459 errx(1, "illegal preload value -- %s", optarg);
462 #ifdef IPV6_USE_MIN_MTU
466 errx(1, "-%c is not supported on this platform", ch);
470 options &= ~F_HOSTNAME;
473 options |= F_NIGROUP;
475 case 'p': /* fill buffer with user pattern */
476 options |= F_PINGFILLED;
477 fill((char *)datap, optarg);
483 memset(&hints, 0, sizeof(struct addrinfo));
484 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
485 hints.ai_family = AF_INET6;
486 hints.ai_socktype = SOCK_RAW;
487 hints.ai_protocol = IPPROTO_ICMPV6;
489 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
491 errx(1, "invalid source address: %s",
492 gai_strerror(ret_ga));
495 * res->ai_family must be AF_INET6 and res->ai_addrlen
496 * must be sizeof(src).
498 memcpy(&src, res->ai_addr, res->ai_addrlen);
499 srclen = res->ai_addrlen;
501 options |= F_SRCADDR;
503 case 's': /* size of packet to send */
504 datalen = strtol(optarg, &e, 10);
505 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
506 errx(1, "illegal datalen value -- %s", optarg);
507 if (datalen > MAXDATALEN) {
509 "datalen value too large, maximum is %d",
514 options &= ~F_NOUSERDATA;
515 options |= F_SUPTYPES;
518 options |= F_VERBOSE;
521 options &= ~F_NOUSERDATA;
525 options &= ~F_NOUSERDATA;
526 options |= F_FQDNOLD;
529 #ifdef IPSEC_POLICY_IPSEC
532 if (!strncmp("in", optarg, 2)) {
533 if ((policy_in = strdup(optarg)) == NULL)
535 } else if (!strncmp("out", optarg, 3)) {
536 if ((policy_out = strdup(optarg)) == NULL)
539 errx(1, "invalid security policy");
543 options |= F_AUTHHDR;
546 options |= F_ENCRYPT;
548 #endif /*IPSEC_POLICY_IPSEC*/
564 errx(1, "too many arguments");
568 if (options & F_NIGROUP) {
569 target = nigroup(argv[argc - 1]);
570 if (target == NULL) {
575 target = argv[argc - 1];
578 memset(&hints, 0, sizeof(struct addrinfo));
579 hints.ai_flags = AI_CANONNAME;
580 hints.ai_family = AF_INET6;
581 hints.ai_socktype = SOCK_RAW;
582 hints.ai_protocol = IPPROTO_ICMPV6;
584 ret_ga = getaddrinfo(target, NULL, &hints, &res);
586 errx(1, "%s", gai_strerror(ret_ga));
587 if (res->ai_canonname)
588 hostname = res->ai_canonname;
593 errx(1, "getaddrinfo failed");
595 memcpy(&dst, res->ai_addr, res->ai_addrlen);
597 if ((s = socket(res->ai_family, res->ai_socktype,
598 res->ai_protocol)) < 0)
601 /* set the source address if specified. */
602 if ((options & F_SRCADDR) &&
603 bind(s, (struct sockaddr *)&src, srclen) != 0) {
607 /* set the gateway (next hop) if specified */
609 struct addrinfo ghints, *gres;
612 memset(&ghints, 0, sizeof(ghints));
613 ghints.ai_family = AF_INET6;
614 ghints.ai_socktype = SOCK_RAW;
615 ghints.ai_protocol = IPPROTO_ICMPV6;
617 error = getaddrinfo(gateway, NULL, &hints, &gres);
619 errx(1, "getaddrinfo for the gateway %s: %s",
620 gateway, gai_strerror(error));
622 if (gres->ai_next && (options & F_VERBOSE))
623 warnx("gateway resolves to multiple addresses");
625 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
626 gres->ai_addr, gres->ai_addrlen)) {
627 err(1, "setsockopt(IPV6_NEXTHOP)");
634 * let the kerel pass extension headers of incoming packets,
635 * for privileged socket options
637 if ((options & F_VERBOSE) != 0) {
640 #ifdef IPV6_RECVHOPOPTS
641 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
643 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
644 #else /* old adv. API */
645 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
647 err(1, "setsockopt(IPV6_HOPOPTS)");
649 #ifdef IPV6_RECVDSTOPTS
650 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
652 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
653 #else /* old adv. API */
654 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
656 err(1, "setsockopt(IPV6_DSTOPTS)");
658 #ifdef IPV6_RECVRTHDRDSTOPTS
659 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
661 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
665 /* revoke root privilege */
669 if ((options & F_FLOOD) && (options & F_INTERVAL))
670 errx(1, "-f and -i incompatible options");
672 if ((options & F_NOUSERDATA) == 0) {
673 if (datalen >= (int)sizeof(struct tv32)) {
674 /* we can time transfer */
678 /* in F_VERBOSE case, we may get non-echoreply packets*/
679 if (options & F_VERBOSE)
680 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
682 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
684 /* suppress timing for node information query */
687 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
690 if (!(packet = (u_char *)malloc((u_int)packlen)))
691 err(1, "Unable to allocate packet");
692 if (!(options & F_PINGFILLED))
693 for (i = ICMP6ECHOLEN; i < packlen; ++i)
696 ident = getpid() & 0xFFFF;
697 #ifndef HAVE_ARC4RANDOM
698 gettimeofday(&seed, NULL);
699 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
700 memset(nonce, 0, sizeof(nonce));
701 for (i = 0; i < (int)sizeof(nonce); i += sizeof(int))
702 *((int *)&nonce[i]) = rand();
704 memset(nonce, 0, sizeof(nonce));
705 for (i = 0; i < (int)sizeof(nonce); i += sizeof(u_int32_t))
706 *((u_int32_t *)&nonce[i]) = arc4random();
711 if (options & F_SO_DEBUG)
712 setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
714 optval = IPV6_DEFHLIM;
715 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
716 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
717 &optval, sizeof(optval)) == -1)
718 err(1, "IPV6_MULTICAST_HOPS");
719 #ifdef IPV6_USE_MIN_MTU
721 optval = mflag > 1 ? 0 : 1;
722 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
723 &optval, sizeof(optval)) == -1)
724 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
726 #ifdef IPV6_RECVPATHMTU
729 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
730 &optval, sizeof(optval)) == -1)
731 err(1, "setsockopt(IPV6_RECVPATHMTU)");
733 #endif /* IPV6_RECVPATHMTU */
734 #endif /* IPV6_USE_MIN_MTU */
737 #ifdef IPSEC_POLICY_IPSEC
738 if (options & F_POLICY) {
739 if (setpolicy(s, policy_in) < 0)
740 errx(1, "%s", ipsec_strerror());
741 if (setpolicy(s, policy_out) < 0)
742 errx(1, "%s", ipsec_strerror());
745 if (options & F_AUTHHDR) {
746 optval = IPSEC_LEVEL_REQUIRE;
747 #ifdef IPV6_AUTH_TRANS_LEVEL
748 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
749 &optval, sizeof(optval)) == -1)
750 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
752 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
753 &optval, sizeof(optval)) == -1)
754 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
757 if (options & F_ENCRYPT) {
758 optval = IPSEC_LEVEL_REQUIRE;
759 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
760 &optval, sizeof(optval)) == -1)
761 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
763 #endif /*IPSEC_POLICY_IPSEC*/
768 struct icmp6_filter filt;
769 if (!(options & F_VERBOSE)) {
770 ICMP6_FILTER_SETBLOCKALL(&filt);
771 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
772 (options & F_NODEADDR) || (options & F_SUPTYPES))
773 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
775 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
777 ICMP6_FILTER_SETPASSALL(&filt);
779 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
781 err(1, "setsockopt(ICMP6_FILTER)");
783 #endif /*ICMP6_FILTER*/
785 /* let the kerel pass extension headers of incoming packets */
786 if ((options & F_VERBOSE) != 0) {
789 #ifdef IPV6_RECVRTHDR
790 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
792 err(1, "setsockopt(IPV6_RECVRTHDR)");
793 #else /* old adv. API */
794 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
796 err(1, "setsockopt(IPV6_RTHDR)");
802 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
803 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
804 &optval, sizeof(optval)) == -1)
805 err(1, "IPV6_MULTICAST_LOOP");
808 /* Specify the outgoing interface and/or the source address */
810 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
813 ip6optlen += CMSG_SPACE(sizeof(int));
815 /* set IP6 packet options */
817 if ((scmsg = (char *)malloc(ip6optlen)) == NULL)
818 errx(1, "can't allocate enough memory");
819 smsghdr.msg_control = (caddr_t)scmsg;
820 smsghdr.msg_controllen = ip6optlen;
821 scmsgp = (struct cmsghdr *)scmsg;
824 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
825 memset(pktinfo, 0, sizeof(*pktinfo));
826 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
827 scmsgp->cmsg_level = IPPROTO_IPV6;
828 scmsgp->cmsg_type = IPV6_PKTINFO;
829 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
832 /* set the outgoing interface */
834 #ifndef USE_SIN6_SCOPE_ID
835 /* pktinfo must have already been allocated */
836 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
837 errx(1, "%s: invalid interface name", ifname);
839 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
840 errx(1, "%s: invalid interface name", ifname);
843 if (hoplimit != -1) {
844 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
845 scmsgp->cmsg_level = IPPROTO_IPV6;
846 scmsgp->cmsg_type = IPV6_HOPLIMIT;
847 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
849 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
852 if (!(options & F_SRCADDR)) {
854 * get the source address. XXX since we revoked the root
855 * privilege, we cannot use a raw socket for this.
858 socklen_t len = sizeof(src);
860 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
861 err(1, "UDP socket");
863 src.sin6_family = AF_INET6;
864 src.sin6_addr = dst.sin6_addr;
865 src.sin6_port = ntohs(DUMMY_PORT);
866 src.sin6_scope_id = dst.sin6_scope_id;
870 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
871 (void *)pktinfo, sizeof(*pktinfo)))
872 err(1, "UDP setsockopt(IPV6_PKTINFO)");
873 if (hoplimit != -1 &&
874 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
875 (void *)&hoplimit, sizeof(hoplimit)))
876 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
877 if (hoplimit != -1 &&
878 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
879 (void *)&hoplimit, sizeof(hoplimit)))
880 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
882 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
883 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
884 err(1, "UDP setsockopt(IPV6_RTHDR)");
885 #else /* old advanced API */
886 if (smsghdr.msg_control &&
887 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
888 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
889 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
892 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
893 err(1, "UDP connect");
895 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
896 err(1, "getsockname");
901 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
903 if (datalen > sockbufsize)
904 warnx("you need -b to increase socket buffer size");
905 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
906 sizeof(sockbufsize)) < 0)
907 err(1, "setsockopt(SO_SNDBUF)");
908 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
909 sizeof(sockbufsize)) < 0)
910 err(1, "setsockopt(SO_RCVBUF)");
913 if (datalen > 8 * 1024) /*XXX*/
914 warnx("you need -b to increase socket buffer size");
916 * When pinging the broadcast address, you can get a lot of
917 * answers. Doing something so evil is useful if you are trying
918 * to stress the ethernet, or just want to fill the arp cache
919 * to get some stuff for /etc/ethers.
922 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
928 #ifndef USE_SIN6_SCOPE_ID
929 #ifdef IPV6_RECVPKTINFO
930 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
932 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
933 #else /* old adv. API */
934 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
936 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
938 #endif /* USE_SIN6_SCOPE_ID */
939 #ifdef IPV6_RECVHOPLIMIT
940 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
942 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
943 #else /* old adv. API */
944 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
946 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
949 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
950 (unsigned long)(pingerlen() - 8));
951 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
952 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
954 while (preload--) /* Fire off them quickies. */
957 signal(SIGINT, onsignal);
959 signal(SIGINFO, onsignal);
962 if ((options & F_FLOOD) == 0) {
963 signal(SIGALRM, onsignal);
964 itimer.it_interval = interval;
965 itimer.it_value = interval;
966 setitimer(ITIMER_REAL, &itimer, NULL);
967 if (ntransmitted == 0)
972 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
973 if ((fdmaskp = malloc(fdmasks)) == NULL)
977 seenalrm = seenint = 0;
982 /* For control (ancillary) data received from recvmsg() */
983 cm = (struct cmsghdr *)malloc(CONTROLLEN);
991 /* signal handling */
1010 if (options & F_FLOOD) {
1016 timeout.tv_usec = 10000;
1028 fdmaskp[0].events = POLLIN;
1029 cc = poll(fdmaskp, 1, timeout);
1031 memset(fdmaskp, 0, fdmasks);
1033 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1036 if (errno != EINTR) {
1048 m.msg_name = (caddr_t)&from;
1049 m.msg_namelen = sizeof(from);
1050 memset(&iov, 0, sizeof(iov));
1051 iov[0].iov_base = (caddr_t)packet;
1052 iov[0].iov_len = packlen;
1055 memset(cm, 0, CONTROLLEN);
1056 m.msg_control = (void *)cm;
1057 m.msg_controllen = CONTROLLEN;
1059 cc = recvmsg(s, &m, 0);
1061 if (errno != EINTR) {
1066 } else if (cc == 0) {
1070 * receive control messages only. Process the
1071 * exceptions (currently the only possiblity is
1072 * a path MTU notification.)
1074 if ((mtu = get_pathmtu(&m)) > 0) {
1075 if ((options & F_VERBOSE) != 0) {
1076 printf("new path MTU (%d) is "
1083 * an ICMPv6 message (probably an echoreply) arrived.
1085 pr_pack(packet, cc, &m);
1087 if (npackets && nreceived >= npackets)
1091 exit(nreceived == 0);
1114 * This routine transmits another ping6.
1119 struct itimerval itimer;
1125 * If we're not transmitting any more packets, change the timer
1126 * to wait two round-trip times if we've received any packets or
1127 * ten seconds if we haven't.
1131 itimer.it_value.tv_sec = 2 * tmax / 1000;
1132 if (itimer.it_value.tv_sec == 0)
1133 itimer.it_value.tv_sec = 1;
1135 itimer.it_value.tv_sec = MAXWAIT;
1136 itimer.it_interval.tv_sec = 0;
1137 itimer.it_interval.tv_usec = 0;
1138 itimer.it_value.tv_usec = 0;
1140 signal(SIGALRM, onint);
1141 setitimer(ITIMER_REAL, &itimer, NULL);
1146 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1147 * will be added on by the kernel. The ID field is our UNIX process ID,
1148 * and the sequence number is an ascending integer. The first 8 bytes
1149 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1150 * byte-order, to compute the round-trip time.
1157 if (options & F_FQDN)
1158 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1159 else if (options & F_FQDNOLD)
1161 else if (options & F_NODEADDR)
1162 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1163 else if (options & F_SUPTYPES)
1166 l = ICMP6ECHOLEN + datalen;
1174 struct icmp6_hdr *icp;
1175 struct iovec iov[2];
1177 struct icmp6_nodeinfo *nip;
1180 if (npackets && ntransmitted >= npackets)
1181 return(-1); /* no more transmission */
1183 icp = (struct icmp6_hdr *)outpack;
1184 nip = (struct icmp6_nodeinfo *)outpack;
1185 memset(icp, 0, sizeof(*icp));
1186 icp->icmp6_cksum = 0;
1187 seq = ntransmitted++;
1188 CLR(seq % mx_dup_ck);
1190 if (options & F_FQDN) {
1191 icp->icmp6_type = ICMP6_NI_QUERY;
1192 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1193 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1194 nip->ni_flags = htons(0);
1196 memcpy(nip->icmp6_ni_nonce, nonce,
1197 sizeof(nip->icmp6_ni_nonce));
1198 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1200 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1201 sizeof(dst.sin6_addr));
1202 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1204 } else if (options & F_FQDNOLD) {
1205 /* packet format in 03 draft - no Subject data on queries */
1206 icp->icmp6_type = ICMP6_NI_QUERY;
1207 icp->icmp6_code = 0; /* code field is always 0 */
1208 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1209 nip->ni_flags = htons(0);
1211 memcpy(nip->icmp6_ni_nonce, nonce,
1212 sizeof(nip->icmp6_ni_nonce));
1213 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1217 } else if (options & F_NODEADDR) {
1218 icp->icmp6_type = ICMP6_NI_QUERY;
1219 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1220 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1221 nip->ni_flags = naflags;
1223 memcpy(nip->icmp6_ni_nonce, nonce,
1224 sizeof(nip->icmp6_ni_nonce));
1225 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1227 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1228 sizeof(dst.sin6_addr));
1229 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1231 } else if (options & F_SUPTYPES) {
1232 icp->icmp6_type = ICMP6_NI_QUERY;
1233 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1234 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1235 /* we support compressed bitmap */
1236 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1238 memcpy(nip->icmp6_ni_nonce, nonce,
1239 sizeof(nip->icmp6_ni_nonce));
1240 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1244 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1245 icp->icmp6_code = 0;
1246 icp->icmp6_id = htons(ident);
1247 icp->icmp6_seq = ntohs(seq);
1251 gettimeofday(&tv, NULL);
1252 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1253 tv32->tv32_sec = htonl(tv.tv_sec);
1254 tv32->tv32_usec = htonl(tv.tv_usec);
1256 cc = ICMP6ECHOLEN + datalen;
1260 if (pingerlen() != cc)
1261 errx(1, "internal error; length mismatch");
1264 smsghdr.msg_name = (caddr_t)&dst;
1265 smsghdr.msg_namelen = sizeof(dst);
1266 memset(&iov, 0, sizeof(iov));
1267 iov[0].iov_base = (caddr_t)outpack;
1268 iov[0].iov_len = cc;
1269 smsghdr.msg_iov = iov;
1270 smsghdr.msg_iovlen = 1;
1272 i = sendmsg(s, &smsghdr, 0);
1274 if (i < 0 || i != cc) {
1277 printf("ping6: wrote %s %d chars, ret=%d\n", hostname, cc, i);
1279 if (!(options & F_QUIET) && options & F_FLOOD)
1280 write(STDOUT_FILENO, &DOT, 1);
1286 myechoreply(const struct icmp6_hdr *icp)
1288 if (ntohs(icp->icmp6_id) == ident)
1295 mynireply(const struct icmp6_nodeinfo *nip)
1297 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1298 nonce + sizeof(u_int16_t),
1299 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1306 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1311 char cresult[MAXDNAME + 1];
1322 if (i == 0 || cp != *sp) {
1323 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1324 return NULL; /*result overrun*/
1330 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1331 /* DNS compression */
1335 comp = base + (i & 0x3f);
1336 if (dnsdecode(&comp, cp, base, cresult,
1337 sizeof(cresult)) == NULL)
1339 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1340 return NULL; /*result overrun*/
1342 } else if ((i & 0x3f) == i) {
1344 return NULL; /*source overrun*/
1345 while (i-- > 0 && cp < ep) {
1346 l = snprintf(cresult, sizeof(cresult),
1347 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1348 if (l >= (int)sizeof(cresult) || l < 0)
1350 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1351 return NULL; /*result overrun*/
1355 return NULL; /*invalid label*/
1358 return NULL; /*not terminated*/
1366 * Print out the packet, if it came from us. This logic is necessary
1367 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1368 * which arrive ('tis only fair). This permits multiple copies of this
1369 * program to be run without having intermingled output (or statistics!).
1372 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1374 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1375 struct icmp6_hdr *icp;
1376 struct icmp6_nodeinfo *ni;
1379 struct sockaddr *from;
1381 u_char *cp = NULL, *dp, *end = buf + cc;
1382 struct in6_pktinfo *pktinfo = NULL;
1383 struct timeval tv, tp;
1385 double triptime = 0;
1390 char dnsname[MAXDNAME + 1];
1392 gettimeofday(&tv, NULL);
1394 if (!mhdr || !mhdr->msg_name ||
1395 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1396 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1397 if (options & F_VERBOSE)
1398 warnx("invalid peername");
1401 from = (struct sockaddr *)mhdr->msg_name;
1402 fromlen = mhdr->msg_namelen;
1403 if (cc < (int)sizeof(struct icmp6_hdr)) {
1404 if (options & F_VERBOSE)
1405 warnx("packet too short (%d bytes) from %s", cc,
1406 pr_addr(from, fromlen));
1409 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1410 (options & F_VERBOSE) != 0)
1411 warnx("some control data discarded, insufficient buffer size");
1412 icp = (struct icmp6_hdr *)buf;
1413 ni = (struct icmp6_nodeinfo *)buf;
1416 if ((hoplim = get_hoplim(mhdr)) == -1) {
1417 warnx("failed to get receiving hop limit");
1420 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1421 warnx("failed to get receiving packet information");
1425 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1426 seq = ntohs(icp->icmp6_seq);
1429 tpp = (struct tv32 *)(icp + 1);
1430 tp.tv_sec = ntohl(tpp->tv32_sec);
1431 tp.tv_usec = ntohl(tpp->tv32_usec);
1433 triptime = ((double)tv.tv_sec) * 1000.0 +
1434 ((double)tv.tv_usec) / 1000.0;
1436 tsumsq += triptime * triptime;
1437 if (triptime < tmin)
1439 if (triptime > tmax)
1443 if (TST(seq % mx_dup_ck)) {
1448 SET(seq % mx_dup_ck);
1452 if (options & F_QUIET)
1455 if (options & F_FLOOD)
1456 write(STDOUT_FILENO, &BSPACE, 1);
1458 printf("%d bytes from %s, icmp_seq=%u", cc,
1459 pr_addr(from, fromlen), seq);
1460 printf(" hlim=%d", hoplim);
1461 if ((options & F_VERBOSE) != 0) {
1462 struct sockaddr_in6 dstsa;
1464 memset(&dstsa, 0, sizeof(dstsa));
1465 dstsa.sin6_family = AF_INET6;
1466 dstsa.sin6_len = sizeof(dstsa);
1467 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1468 dstsa.sin6_addr = pktinfo->ipi6_addr;
1470 pr_addr((struct sockaddr *)&dstsa,
1474 printf(" time=%.3f ms", triptime);
1477 /* check the data */
1478 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1479 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1480 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1482 printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1487 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1488 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1490 if (TST(seq % mx_dup_ck)) {
1495 SET(seq % mx_dup_ck);
1499 if (options & F_QUIET)
1502 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1504 switch (ntohs(ni->ni_code)) {
1505 case ICMP6_NI_SUCCESS:
1507 case ICMP6_NI_REFUSED:
1508 printf("refused, type 0x%x", ntohs(ni->ni_type));
1510 case ICMP6_NI_UNKNOWN:
1511 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1514 printf("unknown code 0x%x, type 0x%x",
1515 ntohs(ni->ni_code), ntohs(ni->ni_type));
1519 switch (ntohs(ni->ni_qtype)) {
1521 printf("NodeInfo NOOP");
1523 case NI_QTYPE_SUPTYPES:
1524 pr_suptypes(ni, end - (u_char *)ni);
1526 case NI_QTYPE_NODEADDR:
1527 pr_nodeaddr(ni, end - (u_char *)ni);
1530 default: /* XXX: for backward compatibility */
1531 cp = (u_char *)ni + ICMP6_NIRLEN;
1532 if (buf[off + ICMP6_NIRLEN] ==
1533 cc - off - ICMP6_NIRLEN - 1)
1538 cp++; /* skip length */
1540 safeputc(*cp & 0xff);
1546 if (dnsdecode((const u_char **)&cp, end,
1547 (const u_char *)(ni + 1), dnsname,
1548 sizeof(dnsname)) == NULL) {
1553 * name-lookup special handling for
1556 if (cp + 1 <= end && !*cp &&
1557 strlen(dnsname) > 0) {
1558 dnsname[strlen(dnsname) - 1] = '\0';
1561 printf("%s%s", i > 0 ? "," : "",
1565 if (options & F_VERBOSE) {
1571 switch (ni->ni_code) {
1572 case ICMP6_NI_REFUSED:
1576 case ICMP6_NI_UNKNOWN:
1577 printf("unknown qtype");
1582 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1583 /* case of refusion, unknown */
1588 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1591 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1592 printf("TTL=%d:meaningless",
1596 printf("TTL=%d:invalid", ttl);
1598 printf("TTL=%d", ttl);
1608 cp = (u_char *)ni + ICMP6_NIRLEN;
1617 if (buf[off + ICMP6_NIRLEN] !=
1618 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1621 printf("invalid namelen:%d/%lu",
1622 buf[off + ICMP6_NIRLEN],
1623 (u_long)cc - off - ICMP6_NIRLEN - 1);
1633 /* We've got something other than an ECHOREPLY */
1634 if (!(options & F_VERBOSE))
1636 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1640 if (!(options & F_FLOOD)) {
1642 if (options & F_VERBOSE)
1650 pr_exthdrs(struct msghdr *mhdr)
1657 bufp = mhdr->msg_control;
1658 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1659 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1660 if (cm->cmsg_level != IPPROTO_IPV6)
1663 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1666 switch (cm->cmsg_type) {
1668 printf(" HbH Options: ");
1669 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1672 #ifdef IPV6_RTHDRDSTOPTS
1673 case IPV6_RTHDRDSTOPTS:
1675 printf(" Dst Options: ");
1676 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1679 printf(" Routing: ");
1680 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1688 pr_ip6opt(void *extbuf, size_t bufsize)
1690 struct ip6_hbh *ext;
1693 socklen_t extlen, len;
1699 ext = (struct ip6_hbh *)extbuf;
1700 extlen = (ext->ip6h_len + 1) * 8;
1701 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1702 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1705 * Bounds checking on the ancillary data buffer:
1706 * subtract the size of a cmsg structure from the buffer size.
1708 if (bufsize < (extlen + CMSG_SPACE(0))) {
1709 extlen = bufsize - CMSG_SPACE(0);
1710 warnx("options truncated, showing only %u (total=%u)",
1711 (unsigned int)(extlen / 8 - 1),
1712 (unsigned int)(ext->ip6h_len));
1717 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1718 &type, &len, &databuf);
1719 if (currentlen == -1)
1723 * Note that inet6_opt_next automatically skips any padding
1728 offset = inet6_opt_get_val(databuf, offset,
1729 &value4, sizeof(value4));
1730 printf(" Jumbo Payload Opt: Length %u\n",
1731 (u_int32_t)ntohl(value4));
1733 /* FIXME: RFC3542 option is IP6OPT_ROUTER_ALERT. */
1734 case IP6OPT_RTALERT:
1736 offset = inet6_opt_get_val(databuf, offset,
1737 &value2, sizeof(value2));
1738 printf(" Router Alert Opt: Type %u\n",
1742 printf(" Received Opt %u len %lu\n",
1743 type, (unsigned long)len);
1749 #else /* !USE_RFC3542 */
1752 pr_ip6opt(void *extbuf __unused, size_t bufsize __unused)
1757 #endif /* USE_RFC3542 */
1761 pr_rthdr(void *extbuf, size_t bufsize __unused)
1763 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1765 /* print fixed part of the header */
1766 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1767 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1768 printf("segments unknown, ");
1769 printf("%d left\n", rh->ip6r_segleft);
1774 #else /* !USE_RFC3542 */
1777 pr_rthdr(void *extbuf __unused)
1782 #endif /* USE_RFC3542 */
1785 pr_bitrange(u_int32_t v, int soff, int ii)
1792 /* shift till we have 0x01 */
1793 if ((v & 0x01) == 0) {
1795 printf("-%u", soff + off - 1);
1806 case 0x04: case 0x0c:
1817 /* we have 0x01 with us */
1818 for (i = 0; i < 32 - off; i++) {
1819 if ((v & (0x01 << i)) == 0)
1823 printf(" %u", soff + off);
1830 /* struct icmp6_nodeinfo *ni: ni->qtype must be SUPTYPES */
1832 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1836 const u_char *cp, *end;
1839 u_int16_t words; /*32bit count*/
1842 #define MAXQTYPES (1 << 16)
1846 cp = (u_char *)(ni + 1);
1847 end = ((u_char *)ni) + nilen;
1851 printf("NodeInfo Supported Qtypes");
1852 if (options & F_VERBOSE) {
1853 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1854 printf(", compressed bitmap");
1856 printf(", raw bitmap");
1860 clen = (size_t)(end - cp);
1861 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1862 if (clen == 0 || clen > MAXQTYPES / 8 ||
1868 if (clen < sizeof(cbit) || clen % sizeof(v))
1870 memcpy(&cbit, cp, sizeof(cbit));
1871 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1875 clen = ntohs(cbit.words) * sizeof(v);
1876 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1881 for (off = 0; off < clen; off += sizeof(v)) {
1882 memcpy(&v, cp + off, sizeof(v));
1883 v = (u_int32_t)ntohl(v);
1884 b = pr_bitrange(v, (int)(cur + off * 8), b);
1886 /* flush the remaining bits */
1887 b = pr_bitrange(0, (int)(cur + off * 8), b);
1891 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1892 cur += ntohs(cbit.skip) * 32;
1896 /* struct icmp6_nodeinfo *ni: ni->qtype must be NODEADDR */
1898 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
1900 u_char *cp = (u_char *)(ni + 1);
1901 char ntop_buf[INET6_ADDRSTRLEN];
1904 nilen -= sizeof(struct icmp6_nodeinfo);
1906 if (options & F_VERBOSE) {
1907 switch (ni->ni_code) {
1908 case ICMP6_NI_REFUSED:
1911 case ICMP6_NI_UNKNOWN:
1912 printf("unknown qtype");
1915 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
1916 printf(" truncated");
1920 printf(" no address\n");
1923 * In icmp-name-lookups 05 and later, TTL of each returned address
1924 * is contained in the resposne. We try to detect the version
1925 * by the length of the data, but note that the detection algorithm
1926 * is incomplete. We assume the latest draft by default.
1928 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
1934 /* XXX: alignment? */
1935 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
1936 cp += sizeof(u_int32_t);
1937 nilen -= sizeof(u_int32_t);
1940 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
1942 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
1943 printf(" %s", ntop_buf);
1945 if (ttl == 0xffffffff) {
1947 * XXX: can this convention be applied to all
1948 * type of TTL (i.e. non-ND TTL)?
1950 printf("(TTL=infty)");
1953 printf("(TTL=%u)", ttl);
1957 nilen -= sizeof(struct in6_addr);
1958 cp += sizeof(struct in6_addr);
1963 get_hoplim(struct msghdr *mhdr)
1967 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1968 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1969 if (cm->cmsg_len == 0)
1972 if (cm->cmsg_level == IPPROTO_IPV6 &&
1973 cm->cmsg_type == IPV6_HOPLIMIT &&
1974 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1975 return(*(int *)CMSG_DATA(cm));
1981 struct in6_pktinfo *
1982 get_rcvpktinfo(struct msghdr *mhdr)
1986 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1987 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1988 if (cm->cmsg_len == 0)
1991 if (cm->cmsg_level == IPPROTO_IPV6 &&
1992 cm->cmsg_type == IPV6_PKTINFO &&
1993 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1994 return((struct in6_pktinfo *)CMSG_DATA(cm));
2000 #ifdef IPV6_RECVPATHMTU
2002 get_pathmtu(struct msghdr *mhdr)
2005 struct ip6_mtuinfo *mtuctl = NULL;
2007 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2008 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2009 if (cm->cmsg_len == 0)
2012 if (cm->cmsg_level == IPPROTO_IPV6 &&
2013 cm->cmsg_type == IPV6_PATHMTU &&
2014 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2015 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2018 * If the notified destination is different from
2019 * the one we are pinging, just ignore the info.
2020 * We check the scope ID only when both notified value
2021 * and our own value have non-0 values, because we may
2022 * have used the default scope zone ID for sending,
2023 * in which case the scope ID value is 0.
2025 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2027 (mtuctl->ip6m_addr.sin6_scope_id &&
2028 dst.sin6_scope_id &&
2029 mtuctl->ip6m_addr.sin6_scope_id !=
2030 dst.sin6_scope_id)) {
2031 if ((options & F_VERBOSE) != 0) {
2032 printf("path MTU for %s is notified. "
2034 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2035 sizeof(mtuctl->ip6m_addr)));
2041 * Ignore an invalid MTU. XXX: can we just believe
2044 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2047 /* notification for our destination. return the MTU. */
2048 return((int)mtuctl->ip6m_mtu);
2057 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2061 tvsub(struct timeval *out, struct timeval *in)
2063 if ((out->tv_usec -= in->tv_usec) < 0) {
2065 out->tv_usec += 1000000;
2067 out->tv_sec -= in->tv_sec;
2076 onint(int sig __unused)
2080 signal(SIGINT, SIG_DFL);
2081 kill(getpid(), SIGINT);
2089 * Print out statistics.
2095 printf("\n--- %s ping6 statistics ---\n", hostname);
2096 printf("%ld packets transmitted, ", ntransmitted);
2097 printf("%ld packets received, ", nreceived);
2099 printf("+%ld duplicates, ", nrepeats);
2101 if (nreceived > ntransmitted)
2102 printf("-- somebody's duplicating packets!");
2104 printf("%.1f%% packet loss",
2105 ((((double)ntransmitted - nreceived) * 100.0) /
2109 if (nreceived && timing) {
2110 /* Only display average to microseconds */
2111 double num = nreceived + nrepeats;
2112 double avg = tsum / num;
2113 double dev = sqrt(tsumsq / num - avg * avg);
2115 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2116 tmin, avg, tmax, dev);
2123 static const char *niqcode[] = {
2125 "DNS label", /*or empty*/
2130 static const char *nircode[] = {
2131 "Success", "Refused", "Unknown",
2137 * Print a descriptive string about an ICMP header.
2140 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2142 char ntop_buf[INET6_ADDRSTRLEN];
2143 struct nd_redirect *red;
2144 struct icmp6_nodeinfo *ni;
2145 char dnsname[MAXDNAME + 1];
2149 switch (icp->icmp6_type) {
2150 case ICMP6_DST_UNREACH:
2151 switch (icp->icmp6_code) {
2152 case ICMP6_DST_UNREACH_NOROUTE:
2153 printf("No Route to Destination\n");
2155 case ICMP6_DST_UNREACH_ADMIN:
2156 printf("Destination Administratively "
2159 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2160 printf("Destination Unreachable Beyond Scope\n");
2162 case ICMP6_DST_UNREACH_ADDR:
2163 printf("Destination Host Unreachable\n");
2165 case ICMP6_DST_UNREACH_NOPORT:
2166 printf("Destination Port Unreachable\n");
2169 printf("Destination Unreachable, Bad Code: %d\n",
2173 /* Print returned IP header information */
2174 pr_retip((struct ip6_hdr *)(icp + 1), end);
2176 case ICMP6_PACKET_TOO_BIG:
2177 printf("Packet too big mtu = %d\n",
2178 (int)ntohl(icp->icmp6_mtu));
2179 pr_retip((struct ip6_hdr *)(icp + 1), end);
2181 case ICMP6_TIME_EXCEEDED:
2182 switch (icp->icmp6_code) {
2183 case ICMP6_TIME_EXCEED_TRANSIT:
2184 printf("Time to live exceeded\n");
2186 case ICMP6_TIME_EXCEED_REASSEMBLY:
2187 printf("Frag reassembly time exceeded\n");
2190 printf("Time exceeded, Bad Code: %d\n",
2194 pr_retip((struct ip6_hdr *)(icp + 1), end);
2196 case ICMP6_PARAM_PROB:
2197 printf("Parameter problem: ");
2198 switch (icp->icmp6_code) {
2199 case ICMP6_PARAMPROB_HEADER:
2200 printf("Erroneous Header ");
2202 case ICMP6_PARAMPROB_NEXTHEADER:
2203 printf("Unknown Nextheader ");
2205 case ICMP6_PARAMPROB_OPTION:
2206 printf("Unrecognized Option ");
2209 printf("Bad code(%d) ", icp->icmp6_code);
2212 printf("pointer = 0x%02x\n", (u_int32_t)ntohl(icp->icmp6_pptr));
2213 pr_retip((struct ip6_hdr *)(icp + 1), end);
2215 case ICMP6_ECHO_REQUEST:
2216 printf("Echo Request");
2217 /* XXX ID + Seq + Data */
2219 case ICMP6_ECHO_REPLY:
2220 printf("Echo Reply");
2221 /* XXX ID + Seq + Data */
2223 case ICMP6_MEMBERSHIP_QUERY:
2224 printf("Listener Query");
2226 case ICMP6_MEMBERSHIP_REPORT:
2227 printf("Listener Report");
2229 case ICMP6_MEMBERSHIP_REDUCTION:
2230 printf("Listener Done");
2232 case ND_ROUTER_SOLICIT:
2233 printf("Router Solicitation");
2235 case ND_ROUTER_ADVERT:
2236 printf("Router Advertisement");
2238 case ND_NEIGHBOR_SOLICIT:
2239 printf("Neighbor Solicitation");
2241 case ND_NEIGHBOR_ADVERT:
2242 printf("Neighbor Advertisement");
2245 red = (struct nd_redirect *)icp;
2246 printf("Redirect\n");
2247 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2249 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2250 printf("Destination: %s", ntop_buf);
2251 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2253 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2254 printf(" New Target: %s", ntop_buf);
2256 case ICMP6_NI_QUERY:
2257 printf("Node Information Query");
2258 /* XXX ID + Seq + Data */
2259 ni = (struct icmp6_nodeinfo *)icp;
2260 l = end - (u_char *)(ni + 1);
2262 switch (ntohs(ni->ni_qtype)) {
2266 case NI_QTYPE_SUPTYPES:
2267 printf("Supported qtypes");
2272 case NI_QTYPE_NODEADDR:
2275 case NI_QTYPE_IPV4ADDR:
2276 printf("IPv4 nodeaddr");
2279 printf("unknown qtype");
2282 if (options & F_VERBOSE) {
2283 switch (ni->ni_code) {
2284 case ICMP6_NI_SUBJ_IPV6:
2285 if (l == sizeof(struct in6_addr) &&
2286 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2287 sizeof(ntop_buf)) != NULL) {
2288 printf(", subject=%s(%s)",
2289 niqcode[ni->ni_code], ntop_buf);
2292 /* backward compat to -W */
2293 printf(", oldfqdn");
2295 printf(", invalid");
2299 case ICMP6_NI_SUBJ_FQDN:
2300 if (end == (u_char *)(ni + 1)) {
2301 printf(", no subject");
2304 printf(", subject=%s", niqcode[ni->ni_code]);
2305 cp = (const u_char *)(ni + 1);
2306 if (dnsdecode(&cp, end, NULL, dnsname,
2307 sizeof(dnsname)) != NULL)
2308 printf("(%s)", dnsname);
2310 printf("(invalid)");
2312 case ICMP6_NI_SUBJ_IPV4:
2313 if (l == sizeof(struct in_addr) &&
2314 inet_ntop(AF_INET, ni + 1, ntop_buf,
2315 sizeof(ntop_buf)) != NULL) {
2316 printf(", subject=%s(%s)",
2317 niqcode[ni->ni_code], ntop_buf);
2319 printf(", invalid");
2322 printf(", invalid");
2327 case ICMP6_NI_REPLY:
2328 printf("Node Information Reply");
2329 /* XXX ID + Seq + Data */
2330 ni = (struct icmp6_nodeinfo *)icp;
2332 switch (ntohs(ni->ni_qtype)) {
2336 case NI_QTYPE_SUPTYPES:
2337 printf("Supported qtypes");
2342 case NI_QTYPE_NODEADDR:
2345 case NI_QTYPE_IPV4ADDR:
2346 printf("IPv4 nodeaddr");
2349 printf("unknown qtype");
2352 if (options & F_VERBOSE) {
2353 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2354 printf(", invalid");
2356 printf(", %s", nircode[ni->ni_code]);
2360 printf("Bad ICMP type: %d", icp->icmp6_type);
2366 * Print an IP6 header.
2369 pr_iph(struct ip6_hdr *ip6)
2371 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2373 char ntop_buf[INET6_ADDRSTRLEN];
2375 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2376 tc = (tc >> 4) & 0x0f;
2377 tc |= (ip6->ip6_vfc << 4);
2379 printf("Vr TC Flow Plen Nxt Hlim\n");
2380 printf(" %1x %02x %05x %04x %02x %02x\n",
2381 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2382 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2383 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2384 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2385 printf("%s->", ntop_buf);
2386 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2387 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2388 printf("%s\n", ntop_buf);
2393 * Return an ascii host address as a dotted quad and optionally with
2397 pr_addr(struct sockaddr *addr, int addrlen)
2399 static char buf[NI_MAXHOST];
2402 if ((options & F_HOSTNAME) == 0)
2403 flag |= NI_NUMERICHOST;
2405 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2413 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2416 pr_retip(struct ip6_hdr *ip6, u_char *end)
2418 u_char *cp = (u_char *)ip6, nh;
2421 if (end - (u_char *)ip6 < (int)sizeof(*ip6)) {
2426 hlen = sizeof(*ip6);
2430 while (end - cp >= 8) {
2432 case IPPROTO_HOPOPTS:
2434 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2435 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2437 case IPPROTO_DSTOPTS:
2439 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2440 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2442 case IPPROTO_FRAGMENT:
2444 hlen = sizeof(struct ip6_frag);
2445 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2447 case IPPROTO_ROUTING:
2449 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2450 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2455 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2456 nh = ((struct ah *)cp)->ah_nxt;
2459 case IPPROTO_ICMPV6:
2460 printf("ICMP6: type = %d, code = %d\n",
2467 printf("TCP: from port %u, to port %u (decimal)\n",
2468 (*cp * 256 + *(cp + 1)),
2469 (*(cp + 2) * 256 + *(cp + 3)));
2472 printf("UDP: from port %u, to port %u (decimal)\n",
2473 (*cp * 256 + *(cp + 1)),
2474 (*(cp + 2) * 256 + *(cp + 3)));
2477 printf("Unknown Header(%d)\n", nh);
2481 if ((cp += hlen) >= end)
2496 fill(char *bp, char *patp)
2502 for (cp = patp; *cp; cp++)
2504 errx(1, "patterns must be specified as hex digits");
2506 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2507 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2508 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2509 &pat[13], &pat[14], &pat[15]);
2514 kk <= MAXDATALEN - (8 + (int)sizeof(struct tv32) + ii);
2516 for (jj = 0; jj < ii; ++jj)
2517 bp[jj + kk] = pat[jj];
2518 if (!(options & F_QUIET)) {
2519 printf("PATTERN: 0x");
2520 for (jj = 0; jj < ii; ++jj)
2521 printf("%02x", bp[jj] & 0xFF);
2527 #ifdef IPSEC_POLICY_IPSEC
2529 setpolicy(int so __unused, char *policy)
2534 return 0; /* ignore */
2536 buf = ipsec_set_policy(policy, strlen(policy));
2538 errx(1, "%s", ipsec_strerror());
2539 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2540 ipsec_get_policylen(buf)) < 0)
2541 warnx("Unable to set IPsec policy");
2555 u_int8_t digest[16];
2558 char hbuf[NI_MAXHOST];
2559 struct in6_addr in6;
2561 p = strchr(name, '.');
2563 p = name + strlen(name);
2565 if (l > 63 || l > sizeof(hbuf) - 1)
2566 return NULL; /*label too long*/
2567 strncpy(hbuf, name, l);
2568 hbuf[(int)l] = '\0';
2570 for (q = name; *q; q++) {
2571 if (isupper(*(unsigned char *)q))
2572 *q = tolower(*(unsigned char *)q);
2575 /* generate 8 bytes of pseudo-random value. */
2576 memset(&ctxt, 0, sizeof(ctxt));
2579 MD5Update(&ctxt, &c, sizeof(c));
2580 MD5Update(&ctxt, (unsigned char *)name, l);
2581 MD5Final(digest, &ctxt);
2583 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2584 return NULL; /*XXX*/
2585 bcopy(digest, &in6.s6_addr[12], 4);
2587 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2590 return strdup(hbuf);
2597 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2602 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2606 #ifdef IPV6_USE_MIN_MTU
2610 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2611 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2612 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2616 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2617 "[hops ...] host\n");