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. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
76 * measure round-trip-delays and packet loss across network paths.
80 * U. S. Army Ballistic Research Laboratory
84 * Public Domain. Distribution Unlimited.
86 * More statistics could always be gathered.
87 * This program has to run SUID to ROOT to access the ICMP socket.
91 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
92 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
93 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
94 * network attached to 1 or more interfaces)
97 #include <sys/param.h>
99 #include <sys/socket.h>
100 #include <sys/time.h>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <netinet/ip6.h>
107 #include <netinet/icmp6.h>
108 #include <arpa/inet.h>
109 #include <arpa/nameser.h>
127 #include <netinet6/ah.h>
128 #include <netinet6/ipsec.h>
138 #define MAXPACKETLEN 131072
140 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
141 #define ICMP6ECHOTMLEN sizeof(struct tv32)
142 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
143 #define CONTROLLEN 10240 /* ancillary data buffer size RFC3542 20.1 */
144 /* FQDN case, 64 bits of nonce + 32 bits ttl */
145 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
146 #define EXTRA 256 /* for AH and various other headers. weird. */
147 #define DEFDATALEN ICMP6ECHOTMLEN
148 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
149 #define NROUTES 9 /* number of record route slots */
151 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
152 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
153 #define SET(bit) (A(bit) |= B(bit))
154 #define CLR(bit) (A(bit) &= (~B(bit)))
155 #define TST(bit) (A(bit) & B(bit))
157 #define F_FLOOD 0x0001
158 #define F_INTERVAL 0x0002
159 #define F_PINGFILLED 0x0008
160 #define F_QUIET 0x0010
161 #define F_RROUTE 0x0020
162 #define F_SO_DEBUG 0x0040
163 #define F_VERBOSE 0x0100
165 #ifdef IPSEC_POLICY_IPSEC
166 #define F_POLICY 0x0400
168 #define F_AUTHHDR 0x0200
169 #define F_ENCRYPT 0x0400
170 #endif /*IPSEC_POLICY_IPSEC*/
172 #define F_NODEADDR 0x0800
173 #define F_FQDN 0x1000
174 #define F_INTERFACE 0x2000
175 #define F_SRCADDR 0x4000
176 #define F_HOSTNAME 0x10000
177 #define F_FQDNOLD 0x20000
178 #define F_NIGROUP 0x40000
179 #define F_SUPTYPES 0x80000
180 #define F_NOMINMTU 0x100000
181 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
184 #define IN6LEN sizeof(struct in6_addr)
185 #define SA6LEN sizeof(struct sockaddr_in6)
186 #define DUMMY_PORT 10101
188 #define SIN6(s) ((struct sockaddr_in6 *)(s))
191 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
192 * number of received sequence numbers we can keep track of. Change 128
193 * to 8192 for complete accuracy...
195 #define MAX_DUP_CHK (8 * 8192)
196 int mx_dup_ck = MAX_DUP_CHK;
197 char rcvd_tbl[MAX_DUP_CHK / 8];
199 struct addrinfo *res;
200 struct sockaddr_in6 dst; /* who to ping6 */
201 struct sockaddr_in6 src; /* src addr of this packet */
203 int datalen = DEFDATALEN;
204 int s; /* socket file descriptor */
205 u_char outpack[MAXPACKETLEN];
206 char BSPACE = '\b'; /* characters written for flood */
209 int ident; /* process id to identify our packets */
210 u_int8_t nonce[8]; /* nonce field for node information */
211 int hoplimit = -1; /* hoplimit */
212 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
215 long npackets; /* max packets to transmit */
216 long nreceived; /* # of packets we got back */
217 long nrepeats; /* number of duplicates */
218 long ntransmitted; /* sequence # for outbound packets = #sent */
219 struct timeval interval = {1, 0}; /* interval between packets */
222 int timing; /* flag to do timing */
223 double tmin = 999999999.0; /* minimum round trip time */
224 double tmax = 0.0; /* maximum round trip time */
225 double tsum = 0.0; /* sum of all times, for doing average */
226 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
228 /* for node addresses */
231 /* for ancillary data(advanced API) */
232 struct msghdr smsghdr;
233 struct iovec smsgiov;
236 volatile sig_atomic_t seenalrm;
237 volatile sig_atomic_t seenint;
239 volatile sig_atomic_t seeninfo;
242 int main(int, char *[]);
243 void fill(char *, char *);
244 int get_hoplim(struct msghdr *);
245 #ifdef IPV6_RECVPATHMTU
246 int get_pathmtu(struct msghdr *);
248 #define get_pathmtu(mhdr) (0)
250 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
252 void retransmit(void);
254 size_t pingerlen(void);
256 const char *pr_addr(struct sockaddr *, int);
257 void pr_icmph(struct icmp6_hdr *, u_char *);
258 void pr_iph(struct ip6_hdr *);
259 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
260 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
261 int myechoreply(const struct icmp6_hdr *);
262 int mynireply(const struct icmp6_nodeinfo *);
263 char *dnsdecode(const u_char **, const u_char *, const u_char *,
265 void pr_pack(u_char *, int, struct msghdr *);
266 void pr_exthdrs(struct msghdr *);
267 void pr_ip6opt(void *, size_t);
268 void pr_rthdr(void *, size_t);
269 int pr_bitrange(u_int32_t, int, int);
270 void pr_retip(struct ip6_hdr *, u_char *);
272 void tvsub(struct timeval *, struct timeval *);
273 int setpolicy(int, char *);
274 char *nigroup(char *);
278 main(int argc, char **argv)
280 struct itimerval itimer;
281 struct sockaddr_in6 from;
282 #ifndef HAVE_ARC4RANDOM
288 struct timeval timeout, *tv;
290 struct addrinfo hints;
292 struct pollfd fdmaskp[1];
298 int ch, hold, packlen, preload, optval, ret_ga;
299 u_char *datap, *packet;
300 char *e, *target, *ifname = NULL, *gateway = NULL;
302 struct cmsghdr *scmsgp = NULL;
304 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
309 struct in6_pktinfo *pktinfo = NULL;
311 struct ip6_rthdr *rthdr = NULL;
313 #ifdef IPSEC_POLICY_IPSEC
314 char *policy_in = NULL;
315 char *policy_out = NULL;
317 #ifdef IPV6_USE_MIN_MTU
322 /* just to be sure */
323 memset(&smsghdr, 0, sizeof(smsghdr));
324 memset(&smsgiov, 0, sizeof(smsgiov));
327 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
331 #ifdef IPSEC_POLICY_IPSEC
335 #endif /*IPSEC_POLICY_IPSEC*/
337 while ((ch = getopt(argc, argv,
338 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS)) != -1) {
345 options &= ~F_NOUSERDATA;
346 options |= F_NODEADDR;
347 for (cp = optarg; *cp != '\0'; cp++) {
350 naflags |= NI_NODEADDR_FLAG_ALL;
354 naflags |= NI_NODEADDR_FLAG_COMPAT;
358 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
362 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
366 naflags |= NI_NODEADDR_FLAG_GLOBAL;
368 case 'A': /* experimental. not in the spec */
369 #ifdef NI_NODEADDR_FLAG_ANYCAST
370 naflags |= NI_NODEADDR_FLAG_ANYCAST;
374 "-a A is not supported on the platform");
385 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
388 lsockbufsize = strtoul(optarg, &e, 10);
389 sockbufsize = lsockbufsize;
390 if (errno || !*optarg || *e ||
391 sockbufsize != (long)lsockbufsize)
392 errx(1, "invalid socket buffer size");
395 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
399 npackets = strtol(optarg, &e, 10);
400 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
402 "illegal number of packets -- %s", optarg);
405 options |= F_SO_DEBUG;
410 errx(1, "Must be superuser to flood ping");
413 setbuf(stdout, NULL);
419 options |= F_HOSTNAME;
421 case 'h': /* hoplimit */
422 hoplimit = strtol(optarg, &e, 10);
423 if (*optarg == '\0' || *e != '\0')
424 errx(1, "illegal hoplimit %s", optarg);
425 if (255 < hoplimit || hoplimit < -1)
427 "illegal hoplimit -- %s", optarg);
431 options |= F_INTERFACE;
432 #ifndef USE_SIN6_SCOPE_ID
436 case 'i': /* wait between sending packets */
437 intval = strtod(optarg, &e);
438 if (*optarg == '\0' || *e != '\0')
439 errx(1, "illegal timing interval %s", optarg);
440 if (intval < 1 && getuid()) {
441 errx(1, "%s: only root may use interval < 1s",
444 interval.tv_sec = (long)intval;
446 (long)((intval - interval.tv_sec) * 1000000);
447 if (interval.tv_sec < 0)
448 errx(1, "illegal timing interval %s", optarg);
449 /* less than 1/hz does not make sense */
450 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
451 warnx("too small interval, raised to 0.01");
452 interval.tv_usec = 10000;
454 options |= F_INTERVAL;
459 errx(1, "Must be superuser to preload");
461 preload = strtol(optarg, &e, 10);
462 if (preload < 0 || *optarg == '\0' || *e != '\0')
463 errx(1, "illegal preload value -- %s", optarg);
466 #ifdef IPV6_USE_MIN_MTU
470 errx(1, "-%c is not supported on this platform", ch);
474 options &= ~F_HOSTNAME;
477 options |= F_NIGROUP;
479 case 'p': /* fill buffer with user pattern */
480 options |= F_PINGFILLED;
481 fill((char *)datap, optarg);
487 memset(&hints, 0, sizeof(struct addrinfo));
488 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
489 hints.ai_family = AF_INET6;
490 hints.ai_socktype = SOCK_RAW;
491 hints.ai_protocol = IPPROTO_ICMPV6;
493 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
495 errx(1, "invalid source address: %s",
496 gai_strerror(ret_ga));
499 * res->ai_family must be AF_INET6 and res->ai_addrlen
500 * must be sizeof(src).
502 memcpy(&src, res->ai_addr, res->ai_addrlen);
503 srclen = res->ai_addrlen;
505 options |= F_SRCADDR;
507 case 's': /* size of packet to send */
508 datalen = strtol(optarg, &e, 10);
509 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
510 errx(1, "illegal datalen value -- %s", optarg);
511 if (datalen > MAXDATALEN) {
513 "datalen value too large, maximum is %d",
518 options &= ~F_NOUSERDATA;
519 options |= F_SUPTYPES;
522 options |= F_VERBOSE;
525 options &= ~F_NOUSERDATA;
529 options &= ~F_NOUSERDATA;
530 options |= F_FQDNOLD;
533 #ifdef IPSEC_POLICY_IPSEC
536 if (!strncmp("in", optarg, 2)) {
537 if ((policy_in = strdup(optarg)) == NULL)
539 } else if (!strncmp("out", optarg, 3)) {
540 if ((policy_out = strdup(optarg)) == NULL)
543 errx(1, "invalid security policy");
547 options |= F_AUTHHDR;
550 options |= F_ENCRYPT;
552 #endif /*IPSEC_POLICY_IPSEC*/
568 errx(1, "too many arguments");
572 if (options & F_NIGROUP) {
573 target = nigroup(argv[argc - 1]);
574 if (target == NULL) {
579 target = argv[argc - 1];
582 memset(&hints, 0, sizeof(struct addrinfo));
583 hints.ai_flags = AI_CANONNAME;
584 hints.ai_family = AF_INET6;
585 hints.ai_socktype = SOCK_RAW;
586 hints.ai_protocol = IPPROTO_ICMPV6;
588 ret_ga = getaddrinfo(target, NULL, &hints, &res);
590 errx(1, "%s", gai_strerror(ret_ga));
591 if (res->ai_canonname)
592 hostname = res->ai_canonname;
597 errx(1, "getaddrinfo failed");
599 memcpy(&dst, res->ai_addr, res->ai_addrlen);
601 if ((s = socket(res->ai_family, res->ai_socktype,
602 res->ai_protocol)) < 0)
605 /* set the source address if specified. */
606 if ((options & F_SRCADDR) &&
607 bind(s, (struct sockaddr *)&src, srclen) != 0) {
611 /* set the gateway (next hop) if specified */
613 struct addrinfo ghints, *gres;
616 memset(&ghints, 0, sizeof(ghints));
617 ghints.ai_family = AF_INET6;
618 ghints.ai_socktype = SOCK_RAW;
619 ghints.ai_protocol = IPPROTO_ICMPV6;
621 error = getaddrinfo(gateway, NULL, &hints, &gres);
623 errx(1, "getaddrinfo for the gateway %s: %s",
624 gateway, gai_strerror(error));
626 if (gres->ai_next && (options & F_VERBOSE))
627 warnx("gateway resolves to multiple addresses");
629 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
630 gres->ai_addr, gres->ai_addrlen)) {
631 err(1, "setsockopt(IPV6_NEXTHOP)");
638 * let the kerel pass extension headers of incoming packets,
639 * for privileged socket options
641 if ((options & F_VERBOSE) != 0) {
644 #ifdef IPV6_RECVHOPOPTS
645 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
647 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
648 #else /* old adv. API */
649 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
651 err(1, "setsockopt(IPV6_HOPOPTS)");
653 #ifdef IPV6_RECVDSTOPTS
654 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
656 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
657 #else /* old adv. API */
658 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
660 err(1, "setsockopt(IPV6_DSTOPTS)");
662 #ifdef IPV6_RECVRTHDRDSTOPTS
663 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
665 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
669 /* revoke root privilege */
673 if ((options & F_FLOOD) && (options & F_INTERVAL))
674 errx(1, "-f and -i incompatible options");
676 if ((options & F_NOUSERDATA) == 0) {
677 if (datalen >= (int)sizeof(struct tv32)) {
678 /* we can time transfer */
682 /* in F_VERBOSE case, we may get non-echoreply packets*/
683 if (options & F_VERBOSE)
684 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
686 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
688 /* suppress timing for node information query */
691 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
694 if (!(packet = (u_char *)malloc((u_int)packlen)))
695 err(1, "Unable to allocate packet");
696 if (!(options & F_PINGFILLED))
697 for (i = ICMP6ECHOLEN; i < packlen; ++i)
700 ident = getpid() & 0xFFFF;
701 #ifndef HAVE_ARC4RANDOM
702 gettimeofday(&seed, NULL);
703 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
704 memset(nonce, 0, sizeof(nonce));
705 for (i = 0; i < (int)sizeof(nonce); i += sizeof(int))
706 *((int *)&nonce[i]) = rand();
708 memset(nonce, 0, sizeof(nonce));
709 for (i = 0; i < (int)sizeof(nonce); i += sizeof(u_int32_t))
710 *((u_int32_t *)&nonce[i]) = arc4random();
715 if (options & F_SO_DEBUG)
716 setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
718 optval = IPV6_DEFHLIM;
719 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
720 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
721 &optval, sizeof(optval)) == -1)
722 err(1, "IPV6_MULTICAST_HOPS");
723 #ifdef IPV6_USE_MIN_MTU
725 optval = mflag > 1 ? 0 : 1;
726 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
727 &optval, sizeof(optval)) == -1)
728 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
730 #ifdef IPV6_RECVPATHMTU
733 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
734 &optval, sizeof(optval)) == -1)
735 err(1, "setsockopt(IPV6_RECVPATHMTU)");
737 #endif /* IPV6_RECVPATHMTU */
738 #endif /* IPV6_USE_MIN_MTU */
741 #ifdef IPSEC_POLICY_IPSEC
742 if (options & F_POLICY) {
743 if (setpolicy(s, policy_in) < 0)
744 errx(1, "%s", ipsec_strerror());
745 if (setpolicy(s, policy_out) < 0)
746 errx(1, "%s", ipsec_strerror());
749 if (options & F_AUTHHDR) {
750 optval = IPSEC_LEVEL_REQUIRE;
751 #ifdef IPV6_AUTH_TRANS_LEVEL
752 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
753 &optval, sizeof(optval)) == -1)
754 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
756 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
757 &optval, sizeof(optval)) == -1)
758 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
761 if (options & F_ENCRYPT) {
762 optval = IPSEC_LEVEL_REQUIRE;
763 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
764 &optval, sizeof(optval)) == -1)
765 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
767 #endif /*IPSEC_POLICY_IPSEC*/
772 struct icmp6_filter filt;
773 if (!(options & F_VERBOSE)) {
774 ICMP6_FILTER_SETBLOCKALL(&filt);
775 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
776 (options & F_NODEADDR) || (options & F_SUPTYPES))
777 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
779 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
781 ICMP6_FILTER_SETPASSALL(&filt);
783 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
785 err(1, "setsockopt(ICMP6_FILTER)");
787 #endif /*ICMP6_FILTER*/
789 /* let the kerel pass extension headers of incoming packets */
790 if ((options & F_VERBOSE) != 0) {
793 #ifdef IPV6_RECVRTHDR
794 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
796 err(1, "setsockopt(IPV6_RECVRTHDR)");
797 #else /* old adv. API */
798 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
800 err(1, "setsockopt(IPV6_RTHDR)");
806 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
807 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
808 &optval, sizeof(optval)) == -1)
809 err(1, "IPV6_MULTICAST_LOOP");
812 /* Specify the outgoing interface and/or the source address */
814 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
817 ip6optlen += CMSG_SPACE(sizeof(int));
819 /* set IP6 packet options */
821 if ((scmsg = (char *)malloc(ip6optlen)) == NULL)
822 errx(1, "can't allocate enough memory");
823 smsghdr.msg_control = (caddr_t)scmsg;
824 smsghdr.msg_controllen = ip6optlen;
825 scmsgp = (struct cmsghdr *)scmsg;
828 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
829 memset(pktinfo, 0, sizeof(*pktinfo));
830 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
831 scmsgp->cmsg_level = IPPROTO_IPV6;
832 scmsgp->cmsg_type = IPV6_PKTINFO;
833 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
836 /* set the outgoing interface */
838 #ifndef USE_SIN6_SCOPE_ID
839 /* pktinfo must have already been allocated */
840 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
841 errx(1, "%s: invalid interface name", ifname);
843 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
844 errx(1, "%s: invalid interface name", ifname);
847 if (hoplimit != -1) {
848 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
849 scmsgp->cmsg_level = IPPROTO_IPV6;
850 scmsgp->cmsg_type = IPV6_HOPLIMIT;
851 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
853 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
856 if (!(options & F_SRCADDR)) {
858 * get the source address. XXX since we revoked the root
859 * privilege, we cannot use a raw socket for this.
862 socklen_t len = sizeof(src);
864 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
865 err(1, "UDP socket");
867 src.sin6_family = AF_INET6;
868 src.sin6_addr = dst.sin6_addr;
869 src.sin6_port = ntohs(DUMMY_PORT);
870 src.sin6_scope_id = dst.sin6_scope_id;
874 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
875 (void *)pktinfo, sizeof(*pktinfo)))
876 err(1, "UDP setsockopt(IPV6_PKTINFO)");
877 if (hoplimit != -1 &&
878 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
879 (void *)&hoplimit, sizeof(hoplimit)))
880 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
881 if (hoplimit != -1 &&
882 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
883 (void *)&hoplimit, sizeof(hoplimit)))
884 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
886 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
887 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
888 err(1, "UDP setsockopt(IPV6_RTHDR)");
889 #else /* old advanced API */
890 if (smsghdr.msg_control &&
891 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
892 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
893 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
896 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
897 err(1, "UDP connect");
899 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
900 err(1, "getsockname");
905 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
907 if (datalen > sockbufsize)
908 warnx("you need -b to increase socket buffer size");
909 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
910 sizeof(sockbufsize)) < 0)
911 err(1, "setsockopt(SO_SNDBUF)");
912 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
913 sizeof(sockbufsize)) < 0)
914 err(1, "setsockopt(SO_RCVBUF)");
917 if (datalen > 8 * 1024) /*XXX*/
918 warnx("you need -b to increase socket buffer size");
920 * When pinging the broadcast address, you can get a lot of
921 * answers. Doing something so evil is useful if you are trying
922 * to stress the ethernet, or just want to fill the arp cache
923 * to get some stuff for /etc/ethers.
926 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
932 #ifndef USE_SIN6_SCOPE_ID
933 #ifdef IPV6_RECVPKTINFO
934 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
936 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
937 #else /* old adv. API */
938 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
940 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
942 #endif /* USE_SIN6_SCOPE_ID */
943 #ifdef IPV6_RECVHOPLIMIT
944 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
946 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
947 #else /* old adv. API */
948 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
950 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
953 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
954 (unsigned long)(pingerlen() - 8));
955 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
956 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
958 while (preload--) /* Fire off them quickies. */
961 signal(SIGINT, onsignal);
963 signal(SIGINFO, onsignal);
966 if ((options & F_FLOOD) == 0) {
967 signal(SIGALRM, onsignal);
968 itimer.it_interval = interval;
969 itimer.it_value = interval;
970 setitimer(ITIMER_REAL, &itimer, NULL);
971 if (ntransmitted == 0)
976 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
977 if ((fdmaskp = malloc(fdmasks)) == NULL)
981 seenalrm = seenint = 0;
986 /* For control (ancillary) data received from recvmsg() */
987 cm = (struct cmsghdr *)malloc(CONTROLLEN);
995 /* signal handling */
1014 if (options & F_FLOOD) {
1020 timeout.tv_usec = 10000;
1032 fdmaskp[0].events = POLLIN;
1033 cc = poll(fdmaskp, 1, timeout);
1035 memset(fdmaskp, 0, fdmasks);
1037 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1040 if (errno != EINTR) {
1052 m.msg_name = (caddr_t)&from;
1053 m.msg_namelen = sizeof(from);
1054 memset(&iov, 0, sizeof(iov));
1055 iov[0].iov_base = (caddr_t)packet;
1056 iov[0].iov_len = packlen;
1059 memset(cm, 0, CONTROLLEN);
1060 m.msg_control = (void *)cm;
1061 m.msg_controllen = CONTROLLEN;
1063 cc = recvmsg(s, &m, 0);
1065 if (errno != EINTR) {
1070 } else if (cc == 0) {
1074 * receive control messages only. Process the
1075 * exceptions (currently the only possiblity is
1076 * a path MTU notification.)
1078 if ((mtu = get_pathmtu(&m)) > 0) {
1079 if ((options & F_VERBOSE) != 0) {
1080 printf("new path MTU (%d) is "
1087 * an ICMPv6 message (probably an echoreply) arrived.
1089 pr_pack(packet, cc, &m);
1091 if (npackets && nreceived >= npackets)
1095 exit(nreceived == 0);
1118 * This routine transmits another ping6.
1123 struct itimerval itimer;
1129 * If we're not transmitting any more packets, change the timer
1130 * to wait two round-trip times if we've received any packets or
1131 * ten seconds if we haven't.
1135 itimer.it_value.tv_sec = 2 * tmax / 1000;
1136 if (itimer.it_value.tv_sec == 0)
1137 itimer.it_value.tv_sec = 1;
1139 itimer.it_value.tv_sec = MAXWAIT;
1140 itimer.it_interval.tv_sec = 0;
1141 itimer.it_interval.tv_usec = 0;
1142 itimer.it_value.tv_usec = 0;
1144 signal(SIGALRM, onint);
1145 setitimer(ITIMER_REAL, &itimer, NULL);
1150 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1151 * will be added on by the kernel. The ID field is our UNIX process ID,
1152 * and the sequence number is an ascending integer. The first 8 bytes
1153 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1154 * byte-order, to compute the round-trip time.
1161 if (options & F_FQDN)
1162 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1163 else if (options & F_FQDNOLD)
1165 else if (options & F_NODEADDR)
1166 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1167 else if (options & F_SUPTYPES)
1170 l = ICMP6ECHOLEN + datalen;
1178 struct icmp6_hdr *icp;
1179 struct iovec iov[2];
1181 struct icmp6_nodeinfo *nip;
1184 if (npackets && ntransmitted >= npackets)
1185 return(-1); /* no more transmission */
1187 icp = (struct icmp6_hdr *)outpack;
1188 nip = (struct icmp6_nodeinfo *)outpack;
1189 memset(icp, 0, sizeof(*icp));
1190 icp->icmp6_cksum = 0;
1191 seq = ntransmitted++;
1192 CLR(seq % mx_dup_ck);
1194 if (options & F_FQDN) {
1195 icp->icmp6_type = ICMP6_NI_QUERY;
1196 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1197 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1198 nip->ni_flags = htons(0);
1200 memcpy(nip->icmp6_ni_nonce, nonce,
1201 sizeof(nip->icmp6_ni_nonce));
1202 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1204 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1205 sizeof(dst.sin6_addr));
1206 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1208 } else if (options & F_FQDNOLD) {
1209 /* packet format in 03 draft - no Subject data on queries */
1210 icp->icmp6_type = ICMP6_NI_QUERY;
1211 icp->icmp6_code = 0; /* code field is always 0 */
1212 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1213 nip->ni_flags = htons(0);
1215 memcpy(nip->icmp6_ni_nonce, nonce,
1216 sizeof(nip->icmp6_ni_nonce));
1217 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1221 } else if (options & F_NODEADDR) {
1222 icp->icmp6_type = ICMP6_NI_QUERY;
1223 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1224 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1225 nip->ni_flags = naflags;
1227 memcpy(nip->icmp6_ni_nonce, nonce,
1228 sizeof(nip->icmp6_ni_nonce));
1229 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1231 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1232 sizeof(dst.sin6_addr));
1233 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1235 } else if (options & F_SUPTYPES) {
1236 icp->icmp6_type = ICMP6_NI_QUERY;
1237 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1238 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1239 /* we support compressed bitmap */
1240 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1242 memcpy(nip->icmp6_ni_nonce, nonce,
1243 sizeof(nip->icmp6_ni_nonce));
1244 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1248 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1249 icp->icmp6_code = 0;
1250 icp->icmp6_id = htons(ident);
1251 icp->icmp6_seq = ntohs(seq);
1255 gettimeofday(&tv, NULL);
1256 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1257 tv32->tv32_sec = htonl(tv.tv_sec);
1258 tv32->tv32_usec = htonl(tv.tv_usec);
1260 cc = ICMP6ECHOLEN + datalen;
1264 if (pingerlen() != cc)
1265 errx(1, "internal error; length mismatch");
1268 smsghdr.msg_name = (caddr_t)&dst;
1269 smsghdr.msg_namelen = sizeof(dst);
1270 memset(&iov, 0, sizeof(iov));
1271 iov[0].iov_base = (caddr_t)outpack;
1272 iov[0].iov_len = cc;
1273 smsghdr.msg_iov = iov;
1274 smsghdr.msg_iovlen = 1;
1276 i = sendmsg(s, &smsghdr, 0);
1278 if (i < 0 || i != cc) {
1281 printf("ping6: wrote %s %d chars, ret=%d\n", hostname, cc, i);
1283 if (!(options & F_QUIET) && options & F_FLOOD)
1284 write(STDOUT_FILENO, &DOT, 1);
1290 myechoreply(const struct icmp6_hdr *icp)
1292 if (ntohs(icp->icmp6_id) == ident)
1299 mynireply(const struct icmp6_nodeinfo *nip)
1301 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1302 nonce + sizeof(u_int16_t),
1303 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1310 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
1315 char cresult[MAXDNAME + 1];
1326 if (i == 0 || cp != *sp) {
1327 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1328 return NULL; /*result overrun*/
1334 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1335 /* DNS compression */
1339 comp = base + (i & 0x3f);
1340 if (dnsdecode(&comp, cp, base, cresult,
1341 sizeof(cresult)) == NULL)
1343 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1344 return NULL; /*result overrun*/
1346 } else if ((i & 0x3f) == i) {
1348 return NULL; /*source overrun*/
1349 while (i-- > 0 && cp < ep) {
1350 l = snprintf(cresult, sizeof(cresult),
1351 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1352 if (l >= (int)sizeof(cresult) || l < 0)
1354 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1355 return NULL; /*result overrun*/
1359 return NULL; /*invalid label*/
1362 return NULL; /*not terminated*/
1370 * Print out the packet, if it came from us. This logic is necessary
1371 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1372 * which arrive ('tis only fair). This permits multiple copies of this
1373 * program to be run without having intermingled output (or statistics!).
1376 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1378 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1379 struct icmp6_hdr *icp;
1380 struct icmp6_nodeinfo *ni;
1383 struct sockaddr *from;
1385 u_char *cp = NULL, *dp, *end = buf + cc;
1386 struct in6_pktinfo *pktinfo = NULL;
1387 struct timeval tv, tp;
1389 double triptime = 0;
1394 char dnsname[MAXDNAME + 1];
1396 gettimeofday(&tv, NULL);
1398 if (!mhdr || !mhdr->msg_name ||
1399 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1400 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1401 if (options & F_VERBOSE)
1402 warnx("invalid peername");
1405 from = (struct sockaddr *)mhdr->msg_name;
1406 fromlen = mhdr->msg_namelen;
1407 if (cc < (int)sizeof(struct icmp6_hdr)) {
1408 if (options & F_VERBOSE)
1409 warnx("packet too short (%d bytes) from %s", cc,
1410 pr_addr(from, fromlen));
1413 if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
1414 (options & F_VERBOSE) != 0)
1415 warnx("some control data discarded, insufficient buffer size");
1416 icp = (struct icmp6_hdr *)buf;
1417 ni = (struct icmp6_nodeinfo *)buf;
1420 if ((hoplim = get_hoplim(mhdr)) == -1) {
1421 warnx("failed to get receiving hop limit");
1424 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1425 warnx("failed to get receiving packet information");
1429 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1430 seq = ntohs(icp->icmp6_seq);
1433 tpp = (struct tv32 *)(icp + 1);
1434 tp.tv_sec = ntohl(tpp->tv32_sec);
1435 tp.tv_usec = ntohl(tpp->tv32_usec);
1437 triptime = ((double)tv.tv_sec) * 1000.0 +
1438 ((double)tv.tv_usec) / 1000.0;
1440 tsumsq += triptime * triptime;
1441 if (triptime < tmin)
1443 if (triptime > tmax)
1447 if (TST(seq % mx_dup_ck)) {
1452 SET(seq % mx_dup_ck);
1456 if (options & F_QUIET)
1459 if (options & F_FLOOD)
1460 write(STDOUT_FILENO, &BSPACE, 1);
1462 printf("%d bytes from %s, icmp_seq=%u", cc,
1463 pr_addr(from, fromlen), seq);
1464 printf(" hlim=%d", hoplim);
1465 if ((options & F_VERBOSE) != 0) {
1466 struct sockaddr_in6 dstsa;
1468 memset(&dstsa, 0, sizeof(dstsa));
1469 dstsa.sin6_family = AF_INET6;
1470 dstsa.sin6_len = sizeof(dstsa);
1471 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1472 dstsa.sin6_addr = pktinfo->ipi6_addr;
1474 pr_addr((struct sockaddr *)&dstsa,
1478 printf(" time=%.3f ms", triptime);
1481 /* check the data */
1482 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1483 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1484 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1486 printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1491 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1492 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1494 if (TST(seq % mx_dup_ck)) {
1499 SET(seq % mx_dup_ck);
1503 if (options & F_QUIET)
1506 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1508 switch (ntohs(ni->ni_code)) {
1509 case ICMP6_NI_SUCCESS:
1511 case ICMP6_NI_REFUSED:
1512 printf("refused, type 0x%x", ntohs(ni->ni_type));
1514 case ICMP6_NI_UNKNOWN:
1515 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1518 printf("unknown code 0x%x, type 0x%x",
1519 ntohs(ni->ni_code), ntohs(ni->ni_type));
1523 switch (ntohs(ni->ni_qtype)) {
1525 printf("NodeInfo NOOP");
1527 case NI_QTYPE_SUPTYPES:
1528 pr_suptypes(ni, end - (u_char *)ni);
1530 case NI_QTYPE_NODEADDR:
1531 pr_nodeaddr(ni, end - (u_char *)ni);
1534 default: /* XXX: for backward compatibility */
1535 cp = (u_char *)ni + ICMP6_NIRLEN;
1536 if (buf[off + ICMP6_NIRLEN] ==
1537 cc - off - ICMP6_NIRLEN - 1)
1542 cp++; /* skip length */
1544 safeputc(*cp & 0xff);
1550 if (dnsdecode((const u_char **)&cp, end,
1551 (const u_char *)(ni + 1), dnsname,
1552 sizeof(dnsname)) == NULL) {
1557 * name-lookup special handling for
1560 if (cp + 1 <= end && !*cp &&
1561 strlen(dnsname) > 0) {
1562 dnsname[strlen(dnsname) - 1] = '\0';
1565 printf("%s%s", i > 0 ? "," : "",
1569 if (options & F_VERBOSE) {
1575 switch (ni->ni_code) {
1576 case ICMP6_NI_REFUSED:
1580 case ICMP6_NI_UNKNOWN:
1581 printf("unknown qtype");
1586 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1587 /* case of refusion, unknown */
1592 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1595 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1596 printf("TTL=%d:meaningless",
1600 printf("TTL=%d:invalid", ttl);
1602 printf("TTL=%d", ttl);
1612 cp = (u_char *)ni + ICMP6_NIRLEN;
1621 if (buf[off + ICMP6_NIRLEN] !=
1622 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1625 printf("invalid namelen:%d/%lu",
1626 buf[off + ICMP6_NIRLEN],
1627 (u_long)cc - off - ICMP6_NIRLEN - 1);
1637 /* We've got something other than an ECHOREPLY */
1638 if (!(options & F_VERBOSE))
1640 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1644 if (!(options & F_FLOOD)) {
1646 if (options & F_VERBOSE)
1654 pr_exthdrs(struct msghdr *mhdr)
1661 bufp = mhdr->msg_control;
1662 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1663 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1664 if (cm->cmsg_level != IPPROTO_IPV6)
1667 bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
1670 switch (cm->cmsg_type) {
1672 printf(" HbH Options: ");
1673 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1676 #ifdef IPV6_RTHDRDSTOPTS
1677 case IPV6_RTHDRDSTOPTS:
1679 printf(" Dst Options: ");
1680 pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
1683 printf(" Routing: ");
1684 pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
1692 pr_ip6opt(void *extbuf, size_t bufsize)
1694 struct ip6_hbh *ext;
1697 socklen_t extlen, len, __unused origextlen;
1703 ext = (struct ip6_hbh *)extbuf;
1704 extlen = (ext->ip6h_len + 1) * 8;
1705 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1706 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1709 * Bounds checking on the ancillary data buffer:
1710 * subtract the size of a cmsg structure from the buffer size.
1712 if (bufsize < (extlen + CMSG_SPACE(0))) {
1713 origextlen = extlen;
1714 extlen = bufsize - CMSG_SPACE(0);
1715 warnx("options truncated, showing only %u (total=%u)",
1716 (unsigned int)(extlen / 8 - 1),
1717 (unsigned int)(ext->ip6h_len));
1722 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1723 &type, &len, &databuf);
1724 if (currentlen == -1)
1728 * Note that inet6_opt_next automatically skips any padding
1733 offset = inet6_opt_get_val(databuf, offset,
1734 &value4, sizeof(value4));
1735 printf(" Jumbo Payload Opt: Length %u\n",
1736 (u_int32_t)ntohl(value4));
1738 /* FIXME: RFC3542 option is IP6OPT_ROUTER_ALERT. */
1739 case IP6OPT_RTALERT:
1741 offset = inet6_opt_get_val(databuf, offset,
1742 &value2, sizeof(value2));
1743 printf(" Router Alert Opt: Type %u\n",
1747 printf(" Received Opt %u len %lu\n",
1748 type, (unsigned long)len);
1754 #else /* !USE_RFC3542 */
1757 pr_ip6opt(void *extbuf __unused, size_t bufsize __unused)
1762 #endif /* USE_RFC3542 */
1766 pr_rthdr(void *extbuf, size_t bufsize __unused)
1768 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1770 /* print fixed part of the header */
1771 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1772 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1773 printf("segments unknown, ");
1774 printf("%d left\n", rh->ip6r_segleft);
1779 #else /* !USE_RFC3542 */
1782 pr_rthdr(void *extbuf __unused)
1787 #endif /* USE_RFC3542 */
1790 pr_bitrange(u_int32_t v, int soff, int ii)
1797 /* shift till we have 0x01 */
1798 if ((v & 0x01) == 0) {
1800 printf("-%u", soff + off - 1);
1811 case 0x04: case 0x0c:
1822 /* we have 0x01 with us */
1823 for (i = 0; i < 32 - off; i++) {
1824 if ((v & (0x01 << i)) == 0)
1828 printf(" %u", soff + off);
1835 /* struct icmp6_nodeinfo *ni: ni->qtype must be SUPTYPES */
1837 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1841 const u_char *cp, *end;
1844 u_int16_t words; /*32bit count*/
1847 #define MAXQTYPES (1 << 16)
1851 cp = (u_char *)(ni + 1);
1852 end = ((u_char *)ni) + nilen;
1856 printf("NodeInfo Supported Qtypes");
1857 if (options & F_VERBOSE) {
1858 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1859 printf(", compressed bitmap");
1861 printf(", raw bitmap");
1865 clen = (size_t)(end - cp);
1866 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1867 if (clen == 0 || clen > MAXQTYPES / 8 ||
1873 if (clen < sizeof(cbit) || clen % sizeof(v))
1875 memcpy(&cbit, cp, sizeof(cbit));
1876 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1880 clen = ntohs(cbit.words) * sizeof(v);
1881 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1886 for (off = 0; off < clen; off += sizeof(v)) {
1887 memcpy(&v, cp + off, sizeof(v));
1888 v = (u_int32_t)ntohl(v);
1889 b = pr_bitrange(v, (int)(cur + off * 8), b);
1891 /* flush the remaining bits */
1892 b = pr_bitrange(0, (int)(cur + off * 8), b);
1896 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1897 cur += ntohs(cbit.skip) * 32;
1901 /* struct icmp6_nodeinfo *ni: ni->qtype must be NODEADDR */
1903 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
1905 u_char *cp = (u_char *)(ni + 1);
1906 char ntop_buf[INET6_ADDRSTRLEN];
1909 nilen -= sizeof(struct icmp6_nodeinfo);
1911 if (options & F_VERBOSE) {
1912 switch (ni->ni_code) {
1913 case ICMP6_NI_REFUSED:
1916 case ICMP6_NI_UNKNOWN:
1917 printf("unknown qtype");
1920 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
1921 printf(" truncated");
1925 printf(" no address\n");
1928 * In icmp-name-lookups 05 and later, TTL of each returned address
1929 * is contained in the resposne. We try to detect the version
1930 * by the length of the data, but note that the detection algorithm
1931 * is incomplete. We assume the latest draft by default.
1933 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
1939 /* XXX: alignment? */
1940 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
1941 cp += sizeof(u_int32_t);
1942 nilen -= sizeof(u_int32_t);
1945 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
1947 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
1948 printf(" %s", ntop_buf);
1950 if (ttl == 0xffffffff) {
1952 * XXX: can this convention be applied to all
1953 * type of TTL (i.e. non-ND TTL)?
1955 printf("(TTL=infty)");
1958 printf("(TTL=%u)", ttl);
1962 nilen -= sizeof(struct in6_addr);
1963 cp += sizeof(struct in6_addr);
1968 get_hoplim(struct msghdr *mhdr)
1972 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1973 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1974 if (cm->cmsg_len == 0)
1977 if (cm->cmsg_level == IPPROTO_IPV6 &&
1978 cm->cmsg_type == IPV6_HOPLIMIT &&
1979 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1980 return(*(int *)CMSG_DATA(cm));
1986 struct in6_pktinfo *
1987 get_rcvpktinfo(struct msghdr *mhdr)
1991 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1992 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1993 if (cm->cmsg_len == 0)
1996 if (cm->cmsg_level == IPPROTO_IPV6 &&
1997 cm->cmsg_type == IPV6_PKTINFO &&
1998 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1999 return((struct in6_pktinfo *)CMSG_DATA(cm));
2005 #ifdef IPV6_RECVPATHMTU
2007 get_pathmtu(struct msghdr *mhdr)
2010 struct ip6_mtuinfo *mtuctl = NULL;
2012 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2013 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2014 if (cm->cmsg_len == 0)
2017 if (cm->cmsg_level == IPPROTO_IPV6 &&
2018 cm->cmsg_type == IPV6_PATHMTU &&
2019 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2020 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2023 * If the notified destination is different from
2024 * the one we are pinging, just ignore the info.
2025 * We check the scope ID only when both notified value
2026 * and our own value have non-0 values, because we may
2027 * have used the default scope zone ID for sending,
2028 * in which case the scope ID value is 0.
2030 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2032 (mtuctl->ip6m_addr.sin6_scope_id &&
2033 dst.sin6_scope_id &&
2034 mtuctl->ip6m_addr.sin6_scope_id !=
2035 dst.sin6_scope_id)) {
2036 if ((options & F_VERBOSE) != 0) {
2037 printf("path MTU for %s is notified. "
2039 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2040 sizeof(mtuctl->ip6m_addr)));
2046 * Ignore an invalid MTU. XXX: can we just believe
2049 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2052 /* notification for our destination. return the MTU. */
2053 return((int)mtuctl->ip6m_mtu);
2062 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2066 tvsub(struct timeval *out, struct timeval *in)
2068 if ((out->tv_usec -= in->tv_usec) < 0) {
2070 out->tv_usec += 1000000;
2072 out->tv_sec -= in->tv_sec;
2081 onint(int sig __unused)
2085 signal(SIGINT, SIG_DFL);
2086 kill(getpid(), SIGINT);
2094 * Print out statistics.
2100 printf("\n--- %s ping6 statistics ---\n", hostname);
2101 printf("%ld packets transmitted, ", ntransmitted);
2102 printf("%ld packets received, ", nreceived);
2104 printf("+%ld duplicates, ", nrepeats);
2106 if (nreceived > ntransmitted)
2107 printf("-- somebody's duplicating packets!");
2109 printf("%.1f%% packet loss",
2110 ((((double)ntransmitted - nreceived) * 100.0) /
2114 if (nreceived && timing) {
2115 /* Only display average to microseconds */
2116 double num = nreceived + nrepeats;
2117 double avg = tsum / num;
2118 double dev = sqrt(tsumsq / num - avg * avg);
2120 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2121 tmin, avg, tmax, dev);
2128 static const char *niqcode[] = {
2130 "DNS label", /*or empty*/
2135 static const char *nircode[] = {
2136 "Success", "Refused", "Unknown",
2142 * Print a descriptive string about an ICMP header.
2145 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2147 char ntop_buf[INET6_ADDRSTRLEN];
2148 struct nd_redirect *red;
2149 struct icmp6_nodeinfo *ni;
2150 char dnsname[MAXDNAME + 1];
2154 switch (icp->icmp6_type) {
2155 case ICMP6_DST_UNREACH:
2156 switch (icp->icmp6_code) {
2157 case ICMP6_DST_UNREACH_NOROUTE:
2158 printf("No Route to Destination\n");
2160 case ICMP6_DST_UNREACH_ADMIN:
2161 printf("Destination Administratively "
2164 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2165 printf("Destination Unreachable Beyond Scope\n");
2167 case ICMP6_DST_UNREACH_ADDR:
2168 printf("Destination Host Unreachable\n");
2170 case ICMP6_DST_UNREACH_NOPORT:
2171 printf("Destination Port Unreachable\n");
2174 printf("Destination Unreachable, Bad Code: %d\n",
2178 /* Print returned IP header information */
2179 pr_retip((struct ip6_hdr *)(icp + 1), end);
2181 case ICMP6_PACKET_TOO_BIG:
2182 printf("Packet too big mtu = %d\n",
2183 (int)ntohl(icp->icmp6_mtu));
2184 pr_retip((struct ip6_hdr *)(icp + 1), end);
2186 case ICMP6_TIME_EXCEEDED:
2187 switch (icp->icmp6_code) {
2188 case ICMP6_TIME_EXCEED_TRANSIT:
2189 printf("Time to live exceeded\n");
2191 case ICMP6_TIME_EXCEED_REASSEMBLY:
2192 printf("Frag reassembly time exceeded\n");
2195 printf("Time exceeded, Bad Code: %d\n",
2199 pr_retip((struct ip6_hdr *)(icp + 1), end);
2201 case ICMP6_PARAM_PROB:
2202 printf("Parameter problem: ");
2203 switch (icp->icmp6_code) {
2204 case ICMP6_PARAMPROB_HEADER:
2205 printf("Erroneous Header ");
2207 case ICMP6_PARAMPROB_NEXTHEADER:
2208 printf("Unknown Nextheader ");
2210 case ICMP6_PARAMPROB_OPTION:
2211 printf("Unrecognized Option ");
2214 printf("Bad code(%d) ", icp->icmp6_code);
2217 printf("pointer = 0x%02x\n", (u_int32_t)ntohl(icp->icmp6_pptr));
2218 pr_retip((struct ip6_hdr *)(icp + 1), end);
2220 case ICMP6_ECHO_REQUEST:
2221 printf("Echo Request");
2222 /* XXX ID + Seq + Data */
2224 case ICMP6_ECHO_REPLY:
2225 printf("Echo Reply");
2226 /* XXX ID + Seq + Data */
2228 case ICMP6_MEMBERSHIP_QUERY:
2229 printf("Listener Query");
2231 case ICMP6_MEMBERSHIP_REPORT:
2232 printf("Listener Report");
2234 case ICMP6_MEMBERSHIP_REDUCTION:
2235 printf("Listener Done");
2237 case ND_ROUTER_SOLICIT:
2238 printf("Router Solicitation");
2240 case ND_ROUTER_ADVERT:
2241 printf("Router Advertisement");
2243 case ND_NEIGHBOR_SOLICIT:
2244 printf("Neighbor Solicitation");
2246 case ND_NEIGHBOR_ADVERT:
2247 printf("Neighbor Advertisement");
2250 red = (struct nd_redirect *)icp;
2251 printf("Redirect\n");
2252 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2254 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2255 printf("Destination: %s", ntop_buf);
2256 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2258 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2259 printf(" New Target: %s", ntop_buf);
2261 case ICMP6_NI_QUERY:
2262 printf("Node Information Query");
2263 /* XXX ID + Seq + Data */
2264 ni = (struct icmp6_nodeinfo *)icp;
2265 l = end - (u_char *)(ni + 1);
2267 switch (ntohs(ni->ni_qtype)) {
2271 case NI_QTYPE_SUPTYPES:
2272 printf("Supported qtypes");
2277 case NI_QTYPE_NODEADDR:
2280 case NI_QTYPE_IPV4ADDR:
2281 printf("IPv4 nodeaddr");
2284 printf("unknown qtype");
2287 if (options & F_VERBOSE) {
2288 switch (ni->ni_code) {
2289 case ICMP6_NI_SUBJ_IPV6:
2290 if (l == sizeof(struct in6_addr) &&
2291 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2292 sizeof(ntop_buf)) != NULL) {
2293 printf(", subject=%s(%s)",
2294 niqcode[ni->ni_code], ntop_buf);
2297 /* backward compat to -W */
2298 printf(", oldfqdn");
2300 printf(", invalid");
2304 case ICMP6_NI_SUBJ_FQDN:
2305 if (end == (u_char *)(ni + 1)) {
2306 printf(", no subject");
2309 printf(", subject=%s", niqcode[ni->ni_code]);
2310 cp = (const u_char *)(ni + 1);
2311 if (dnsdecode(&cp, end, NULL, dnsname,
2312 sizeof(dnsname)) != NULL)
2313 printf("(%s)", dnsname);
2315 printf("(invalid)");
2317 case ICMP6_NI_SUBJ_IPV4:
2318 if (l == sizeof(struct in_addr) &&
2319 inet_ntop(AF_INET, ni + 1, ntop_buf,
2320 sizeof(ntop_buf)) != NULL) {
2321 printf(", subject=%s(%s)",
2322 niqcode[ni->ni_code], ntop_buf);
2324 printf(", invalid");
2327 printf(", invalid");
2332 case ICMP6_NI_REPLY:
2333 printf("Node Information Reply");
2334 /* XXX ID + Seq + Data */
2335 ni = (struct icmp6_nodeinfo *)icp;
2337 switch (ntohs(ni->ni_qtype)) {
2341 case NI_QTYPE_SUPTYPES:
2342 printf("Supported qtypes");
2347 case NI_QTYPE_NODEADDR:
2350 case NI_QTYPE_IPV4ADDR:
2351 printf("IPv4 nodeaddr");
2354 printf("unknown qtype");
2357 if (options & F_VERBOSE) {
2358 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2359 printf(", invalid");
2361 printf(", %s", nircode[ni->ni_code]);
2365 printf("Bad ICMP type: %d", icp->icmp6_type);
2371 * Print an IP6 header.
2374 pr_iph(struct ip6_hdr *ip6)
2376 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2378 char ntop_buf[INET6_ADDRSTRLEN];
2380 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2381 tc = (tc >> 4) & 0x0f;
2382 tc |= (ip6->ip6_vfc << 4);
2384 printf("Vr TC Flow Plen Nxt Hlim\n");
2385 printf(" %1x %02x %05x %04x %02x %02x\n",
2386 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2387 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2388 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2389 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2390 printf("%s->", ntop_buf);
2391 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2392 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2393 printf("%s\n", ntop_buf);
2398 * Return an ascii host address as a dotted quad and optionally with
2402 pr_addr(struct sockaddr *addr, int addrlen)
2404 static char buf[NI_MAXHOST];
2407 if ((options & F_HOSTNAME) == 0)
2408 flag |= NI_NUMERICHOST;
2410 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2418 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2421 pr_retip(struct ip6_hdr *ip6, u_char *end)
2423 u_char *cp = (u_char *)ip6, nh;
2426 if (end - (u_char *)ip6 < (int)sizeof(*ip6)) {
2431 hlen = sizeof(*ip6);
2435 while (end - cp >= 8) {
2437 case IPPROTO_HOPOPTS:
2439 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2440 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2442 case IPPROTO_DSTOPTS:
2444 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2445 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2447 case IPPROTO_FRAGMENT:
2449 hlen = sizeof(struct ip6_frag);
2450 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2452 case IPPROTO_ROUTING:
2454 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2455 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2460 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2461 nh = ((struct ah *)cp)->ah_nxt;
2464 case IPPROTO_ICMPV6:
2465 printf("ICMP6: type = %d, code = %d\n",
2472 printf("TCP: from port %u, to port %u (decimal)\n",
2473 (*cp * 256 + *(cp + 1)),
2474 (*(cp + 2) * 256 + *(cp + 3)));
2477 printf("UDP: from port %u, to port %u (decimal)\n",
2478 (*cp * 256 + *(cp + 1)),
2479 (*(cp + 2) * 256 + *(cp + 3)));
2482 printf("Unknown Header(%d)\n", nh);
2486 if ((cp += hlen) >= end)
2501 fill(char *bp, char *patp)
2507 for (cp = patp; *cp; cp++)
2509 errx(1, "patterns must be specified as hex digits");
2511 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2512 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2513 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2514 &pat[13], &pat[14], &pat[15]);
2519 kk <= MAXDATALEN - (8 + (int)sizeof(struct tv32) + ii);
2521 for (jj = 0; jj < ii; ++jj)
2522 bp[jj + kk] = pat[jj];
2523 if (!(options & F_QUIET)) {
2524 printf("PATTERN: 0x");
2525 for (jj = 0; jj < ii; ++jj)
2526 printf("%02x", bp[jj] & 0xFF);
2532 #ifdef IPSEC_POLICY_IPSEC
2534 setpolicy(int so __unused, char *policy)
2539 return 0; /* ignore */
2541 buf = ipsec_set_policy(policy, strlen(policy));
2543 errx(1, "%s", ipsec_strerror());
2544 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2545 ipsec_get_policylen(buf)) < 0)
2546 warnx("Unable to set IPsec policy");
2560 u_int8_t digest[16];
2563 char hbuf[NI_MAXHOST];
2564 struct in6_addr in6;
2566 p = strchr(name, '.');
2568 p = name + strlen(name);
2570 if (l > 63 || l > sizeof(hbuf) - 1)
2571 return NULL; /*label too long*/
2572 strncpy(hbuf, name, l);
2573 hbuf[(int)l] = '\0';
2575 for (q = name; *q; q++) {
2576 if (isupper(*(unsigned char *)q))
2577 *q = tolower(*(unsigned char *)q);
2580 /* generate 8 bytes of pseudo-random value. */
2581 memset(&ctxt, 0, sizeof(ctxt));
2584 MD5Update(&ctxt, &c, sizeof(c));
2585 MD5Update(&ctxt, (unsigned char *)name, l);
2586 MD5Final(digest, &ctxt);
2588 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2589 return NULL; /*XXX*/
2590 bcopy(digest, &in6.s6_addr[12], 4);
2592 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2595 return strdup(hbuf);
2602 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2607 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2611 #ifdef IPV6_USE_MIN_MTU
2615 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2616 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2617 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2621 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2622 "[hops ...] host\n");