1 /* $KAME: ping6.c,v 1.126 2001/05/17 03:39:08 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.4.2.10 2002/12/09 03:04:44 suz Exp $
34 * $DragonFly: src/sbin/ping6/ping6.c,v 1.8 2005/03/05 22:27:08 cpressey Exp $
37 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
40 * Copyright (c) 1989, 1993
41 * The Regents of the University of California. All rights reserved.
43 * This code is derived from software contributed to Berkeley by
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
77 * measure round-trip-delays and packet loss across network paths.
81 * U. S. Army Ballistic Research Laboratory
85 * Public Domain. Distribution Unlimited.
87 * More statistics could always be gathered.
88 * This program has to run SUID to ROOT to access the ICMP socket.
92 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
93 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
94 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
95 * network attached to 1 or more interfaces)
98 #include <sys/param.h>
100 #include <sys/socket.h>
101 #include <sys/time.h>
104 #include <net/route.h>
106 #include <netinet/in.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp6.h>
109 #include <arpa/inet.h>
110 #include <arpa/nameser.h>
125 #include <netinet6/ah.h>
126 #include <netinet6/ipsec.h>
131 #define MAXPACKETLEN 131072
133 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
134 #define ICMP6ECHOTMLEN sizeof(struct timeval)
135 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
136 /* FQDN case, 64 bits of nonce + 32 bits ttl */
137 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
138 #define EXTRA 256 /* for AH and various other headers. weird. */
139 #define DEFDATALEN ICMP6ECHOTMLEN
140 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
141 #define NROUTES 9 /* number of record route slots */
143 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
144 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
145 #define SET(bit) (A(bit) |= B(bit))
146 #define CLR(bit) (A(bit) &= (~B(bit)))
147 #define TST(bit) (A(bit) & B(bit))
149 #define F_FLOOD 0x0001
150 #define F_INTERVAL 0x0002
151 #define F_PINGFILLED 0x0008
152 #define F_QUIET 0x0010
153 #define F_RROUTE 0x0020
154 #define F_SO_DEBUG 0x0040
155 #define F_VERBOSE 0x0100
157 #ifdef IPSEC_POLICY_IPSEC
158 #define F_POLICY 0x0400
160 #define F_AUTHHDR 0x0200
161 #define F_ENCRYPT 0x0400
162 #endif /*IPSEC_POLICY_IPSEC*/
164 #define F_NODEADDR 0x0800
165 #define F_FQDN 0x1000
166 #define F_INTERFACE 0x2000
167 #define F_SRCADDR 0x4000
168 #ifdef IPV6_REACHCONF
169 #define F_REACHCONF 0x8000
171 #define F_HOSTNAME 0x10000
172 #define F_FQDNOLD 0x20000
173 #define F_NIGROUP 0x40000
174 #define F_SUPTYPES 0x80000
175 #define F_NOMINMTU 0x100000
176 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
179 #define IN6LEN sizeof(struct in6_addr)
180 #define SA6LEN sizeof(struct sockaddr_in6)
181 #define DUMMY_PORT 10101
183 #define SIN6(s) ((struct sockaddr_in6 *)(s))
186 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
187 * number of received sequence numbers we can keep track of. Change 128
188 * to 8192 for complete accuracy...
190 #define MAX_DUP_CHK (8 * 8192)
191 int mx_dup_ck = MAX_DUP_CHK;
192 char rcvd_tbl[MAX_DUP_CHK / 8];
194 struct addrinfo *res;
195 struct sockaddr_in6 dst; /* who to ping6 */
196 struct sockaddr_in6 src; /* src addr of this packet */
197 int datalen = DEFDATALEN;
198 int s; /* socket file descriptor */
199 u_char outpack[MAXPACKETLEN];
200 char BSPACE = '\b'; /* characters written for flood */
203 int ident; /* process id to identify our packets */
204 u_int8_t nonce[8]; /* nonce field for node information */
205 struct in6_addr srcaddr;
206 int hoplimit = -1; /* hoplimit */
207 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
210 long npackets; /* max packets to transmit */
211 long nreceived; /* # of packets we got back */
212 long nrepeats; /* number of duplicates */
213 long ntransmitted; /* sequence # for outbound packets = #sent */
214 struct timeval interval = {1, 0}; /* interval between packets */
217 int timing; /* flag to do timing */
218 double tmin = 999999999.0; /* minimum round trip time */
219 double tmax = 0.0; /* maximum round trip time */
220 double tsum = 0.0; /* sum of all times, for doing average */
221 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
223 /* for node addresses */
226 /* for ancillary data(advanced API) */
227 struct msghdr smsghdr;
228 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 *);
242 #ifdef IPV6_RECVPATHMTU
243 int get_pathmtu(struct msghdr *);
245 #define get_pathmtu(mhdr) (0)
249 void set_pathmtu(int);
251 #define set_pathmtu(mtu)
254 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
256 void retransmit(void);
258 size_t pingerlen(void);
260 const char *pr_addr(struct sockaddr *, int);
261 void pr_icmph(struct icmp6_hdr *, u_char *);
262 void pr_iph(struct ip6_hdr *);
263 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
264 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
265 int myechoreply(const struct icmp6_hdr *);
266 int mynireply(const struct icmp6_nodeinfo *);
267 char *dnsdecode(const u_char **, const u_char *, const u_char *,
269 void pr_pack(u_char *, int, struct msghdr *);
270 void pr_exthdrs(struct msghdr *);
271 void pr_ip6opt(void *);
272 void pr_rthdr(void *);
273 int pr_bitrange(u_int32_t, int, int);
274 void pr_retip(struct ip6_hdr *, u_char *);
276 void tvsub(struct timeval *, struct timeval *);
277 int setpolicy(int, char *);
278 char *nigroup(char *);
282 main(int argc, char **argv)
284 struct itimerval itimer;
285 struct sockaddr_in6 from;
286 struct timeval timeout, *tv;
287 struct addrinfo hints;
291 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
292 u_char *datap, *packet;
293 char *e, *target, *ifname = NULL;
295 struct cmsghdr *scmsgp = NULL;
298 struct in6_pktinfo *pktinfo = NULL;
299 #ifdef USE_RFC2292BIS
300 struct ip6_rthdr *rthdr = NULL;
302 #ifdef IPSEC_POLICY_IPSEC
303 char *policy_in = NULL;
304 char *policy_out = NULL;
309 /* just to be sure */
310 memset(&smsghdr, 0, sizeof(smsghdr));
311 memset(&smsgiov, 0, sizeof(smsgiov));
314 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
318 #ifdef IPSEC_POLICY_IPSEC
322 #endif /*IPSEC_POLICY_IPSEC*/
324 while ((ch = getopt(argc, argv,
325 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {
332 options &= ~F_NOUSERDATA;
333 options |= F_NODEADDR;
334 for (cp = optarg; *cp != '\0'; cp++) {
337 naflags |= NI_NODEADDR_FLAG_ALL;
341 naflags |= NI_NODEADDR_FLAG_COMPAT;
345 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
349 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
353 naflags |= NI_NODEADDR_FLAG_GLOBAL;
355 case 'A': /* experimental. not in the spec */
356 #ifdef NI_NODEADDR_FLAG_ANYCAST
357 naflags |= NI_NODEADDR_FLAG_ANYCAST;
361 "-a A is not supported on the platform");
372 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
373 sockbufsize = atoi(optarg);
376 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
380 npackets = strtol(optarg, &e, 10);
381 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
383 "illegal number of packets -- %s", optarg);
386 options |= F_SO_DEBUG;
391 errx(1, "Must be superuser to flood ping");
394 setbuf(stdout, (char *)NULL);
397 options |= F_HOSTNAME;
399 case 'h': /* hoplimit */
400 hoplimit = strtol(optarg, &e, 10);
401 if (255 < hoplimit || hoplimit < -1)
403 "illegal hoplimit -- %s", optarg);
407 options |= F_INTERFACE;
408 #ifndef USE_SIN6_SCOPE_ID
412 case 'i': /* wait between sending packets */
413 intval = strtod(optarg, &e);
414 if (*optarg == '\0' || *e != '\0')
415 errx(1, "illegal timing interval %s", optarg);
416 if (intval < 1 && getuid()) {
417 errx(1, "%s: only root may use interval < 1s",
420 interval.tv_sec = (long)intval;
422 (long)((intval - interval.tv_sec) * 1000000);
423 if (interval.tv_sec < 0)
424 errx(1, "illegal timing interval %s", optarg);
425 /* less than 1/hz does not make sense */
426 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
427 warnx("too small interval, raised to 0.01");
428 interval.tv_usec = 10000;
430 options |= F_INTERVAL;
435 errx(1, "Must be superuser to preload");
437 preload = strtol(optarg, &e, 10);
438 if (preload < 0 || *optarg == '\0' || *e != '\0')
439 errx(1, "illegal preload value -- %s", optarg);
442 #ifdef IPV6_USE_MIN_MTU
443 options |= F_NOMINMTU;
446 errx(1, "-%c is not supported on this platform", ch);
450 options &= ~F_HOSTNAME;
453 options |= F_NIGROUP;
455 case 'p': /* fill buffer with user pattern */
456 options |= F_PINGFILLED;
457 fill((char *)datap, optarg);
463 #ifdef IPV6_REACHCONF
464 options |= F_REACHCONF;
467 errx(1, "-R is not supported in this configuration");
470 /* XXX: use getaddrinfo? */
471 if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
472 errx(1, "invalid IPv6 address: %s", optarg);
473 options |= F_SRCADDR;
476 case 's': /* size of packet to send */
477 datalen = strtol(optarg, &e, 10);
478 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
479 errx(1, "illegal datalen value -- %s", optarg);
480 if (datalen > MAXDATALEN) {
482 "datalen value too large, maximum is %d",
487 options &= ~F_NOUSERDATA;
488 options |= F_SUPTYPES;
491 options |= F_VERBOSE;
494 options &= ~F_NOUSERDATA;
498 options &= ~F_NOUSERDATA;
499 options |= F_FQDNOLD;
502 #ifdef IPSEC_POLICY_IPSEC
505 if (!strncmp("in", optarg, 2)) {
506 if ((policy_in = strdup(optarg)) == NULL)
508 } else if (!strncmp("out", optarg, 3)) {
509 if ((policy_out = strdup(optarg)) == NULL)
512 errx(1, "invalid security policy");
516 options |= F_AUTHHDR;
519 options |= F_ENCRYPT;
521 #endif /*IPSEC_POLICY_IPSEC*/
537 #ifdef IPV6_RECVRTHDR /* 2292bis */
538 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
541 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
544 errx(1, "too many intermediate hops");
550 if (options & F_NIGROUP) {
551 target = nigroup(argv[argc - 1]);
552 if (target == NULL) {
557 target = argv[argc - 1];
560 bzero(&hints, sizeof(struct addrinfo));
561 hints.ai_flags = AI_CANONNAME;
562 hints.ai_family = AF_INET6;
563 hints.ai_socktype = SOCK_RAW;
564 hints.ai_protocol = IPPROTO_ICMPV6;
566 ret_ga = getaddrinfo(target, NULL, &hints, &res);
568 fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
571 if (res->ai_canonname)
572 hostname = res->ai_canonname;
577 errx(1, "getaddrinfo failed");
579 memcpy(&dst, res->ai_addr, res->ai_addrlen);
581 if ((s = socket(res->ai_family, res->ai_socktype,
582 res->ai_protocol)) < 0)
586 * let the kerel pass extension headers of incoming packets,
587 * for privileged socket options
589 if ((options & F_VERBOSE) != 0) {
592 #ifdef IPV6_RECVHOPOPTS
593 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
595 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
596 #else /* old adv. API */
597 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
599 err(1, "setsockopt(IPV6_HOPOPTS)");
601 #ifdef IPV6_RECVDSTOPTS
602 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
604 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
605 #else /* old adv. API */
606 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
608 err(1, "setsockopt(IPV6_DSTOPTS)");
610 #ifdef IPV6_RECVRTHDRDSTOPTS
611 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
613 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
617 /* revoke root privilege */
621 if (options & F_FLOOD && options & F_INTERVAL)
622 errx(1, "-f and -i incompatible options");
624 if ((options & F_NOUSERDATA) == 0) {
625 if (datalen >= (int)sizeof(struct timeval)) {
626 /* we can time transfer */
630 /* in F_VERBOSE case, we may get non-echoreply packets*/
631 if (options & F_VERBOSE)
632 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
634 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
636 /* suppress timing for node information query */
639 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
642 if (!(packet = (u_char *)malloc((u_int)packlen)))
643 err(1, "Unable to allocate packet");
644 if (!(options & F_PINGFILLED))
645 for (i = ICMP6ECHOLEN; i < packlen; ++i)
648 ident = getpid() & 0xFFFF;
650 gettimeofday(&timeout, NULL);
651 srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));
652 memset(nonce, 0, sizeof(nonce));
653 for (i = 0; i < (int)sizeof(nonce); i += sizeof(int))
654 *((int *)&nonce[i]) = rand();
656 memset(nonce, 0, sizeof(nonce));
657 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
658 *((u_int32_t *)&nonce[i]) = arc4random();
663 if (options & F_SO_DEBUG)
664 setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
666 optval = IPV6_DEFHLIM;
667 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
668 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
669 &optval, sizeof(optval)) == -1)
670 err(1, "IPV6_MULTICAST_HOPS");
671 #ifdef IPV6_USE_MIN_MTU
672 if ((options & F_NOMINMTU) == 0) {
674 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
675 &optval, sizeof(optval)) == -1)
676 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
678 #ifdef IPV6_RECVPATHMTU
681 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
682 &optval, sizeof(optval)) == -1)
683 err(1, "setsockopt(IPV6_RECVPATHMTU)");
685 #endif /* IPV6_RECVPATHMTU */
686 #endif /* IPV6_USE_MIN_MTU */
689 #ifdef IPSEC_POLICY_IPSEC
690 if (options & F_POLICY) {
691 if (setpolicy(s, policy_in) < 0)
692 errx(1, "%s", ipsec_strerror());
693 if (setpolicy(s, policy_out) < 0)
694 errx(1, "%s", ipsec_strerror());
697 if (options & F_AUTHHDR) {
698 optval = IPSEC_LEVEL_REQUIRE;
699 #ifdef IPV6_AUTH_TRANS_LEVEL
700 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
701 &optval, sizeof(optval)) == -1)
702 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
704 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
705 &optval, sizeof(optval)) == -1)
706 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
709 if (options & F_ENCRYPT) {
710 optval = IPSEC_LEVEL_REQUIRE;
711 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
712 &optval, sizeof(optval)) == -1)
713 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
715 #endif /*IPSEC_POLICY_IPSEC*/
720 struct icmp6_filter filt;
721 if (!(options & F_VERBOSE)) {
722 ICMP6_FILTER_SETBLOCKALL(&filt);
723 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
724 (options & F_NODEADDR) || (options & F_SUPTYPES))
725 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
727 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
729 ICMP6_FILTER_SETPASSALL(&filt);
731 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
733 err(1, "setsockopt(ICMP6_FILTER)");
735 #endif /*ICMP6_FILTER*/
737 /* let the kerel pass extension headers of incoming packets */
738 if ((options & F_VERBOSE) != 0) {
741 #ifdef IPV6_RECVRTHDR
742 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
744 err(1, "setsockopt(IPV6_RECVRTHDR)");
745 #else /* old adv. API */
746 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
748 err(1, "setsockopt(IPV6_RTHDR)");
754 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
755 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
756 &optval, sizeof(optval)) == -1)
757 err(1, "IPV6_MULTICAST_LOOP");
760 /* Specify the outgoing interface and/or the source address */
762 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
765 ip6optlen += CMSG_SPACE(sizeof(int));
767 #ifdef IPV6_REACHCONF
768 if (options & F_REACHCONF)
769 ip6optlen += CMSG_SPACE(0);
772 /* set IP6 packet options */
774 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
775 errx(1, "can't allocate enough memory");
776 smsghdr.msg_control = (caddr_t)scmsg;
777 smsghdr.msg_controllen = ip6optlen;
778 scmsgp = (struct cmsghdr *)scmsg;
781 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
782 memset(pktinfo, 0, sizeof(*pktinfo));
783 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
784 scmsgp->cmsg_level = IPPROTO_IPV6;
785 scmsgp->cmsg_type = IPV6_PKTINFO;
786 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
789 /* set the outgoing interface */
791 #ifndef USE_SIN6_SCOPE_ID
792 /* pktinfo must have already been allocated */
793 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
794 errx(1, "%s: invalid interface name", ifname);
796 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
797 errx(1, "%s: invalid interface name", ifname);
800 /* set the source address */
801 if (options & F_SRCADDR)/* pktinfo must be valid */
802 pktinfo->ipi6_addr = srcaddr;
804 if (hoplimit != -1) {
805 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
806 scmsgp->cmsg_level = IPPROTO_IPV6;
807 scmsgp->cmsg_type = IPV6_HOPLIMIT;
808 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
810 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
812 #ifdef IPV6_REACHCONF
813 if (options & F_REACHCONF) {
814 scmsgp->cmsg_len = CMSG_LEN(0);
815 scmsgp->cmsg_level = IPPROTO_IPV6;
816 scmsgp->cmsg_type = IPV6_REACHCONF;
818 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
822 if (argc > 1) { /* some intermediate addrs are specified */
824 #ifdef USE_RFC2292BIS
828 #ifdef USE_RFC2292BIS
829 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
830 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
831 scmsgp->cmsg_level = IPPROTO_IPV6;
832 scmsgp->cmsg_type = IPV6_RTHDR;
833 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
834 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
835 IPV6_RTHDR_TYPE_0, argc - 1);
837 errx(1, "can't initialize rthdr");
838 #else /* old advanced API */
839 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
840 IPV6_RTHDR_TYPE_0)) == 0)
841 errx(1, "can't initialize rthdr");
842 #endif /* USE_RFC2292BIS */
844 for (hops = 0; hops < argc - 1; hops++) {
845 struct addrinfo *iaip;
847 if ((error = getaddrinfo(argv[hops], NULL, &hints,
849 errx(1, "%s", gai_strerror(error));
850 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
852 "bad addr family of an intermediate addr");
854 #ifdef USE_RFC2292BIS
855 if (inet6_rth_add(rthdr,
856 &(SIN6(iaip->ai_addr))->sin6_addr))
857 errx(1, "can't add an intermediate node");
858 #else /* old advanced API */
859 if (inet6_rthdr_add(scmsgp,
860 &(SIN6(iaip->ai_addr))->sin6_addr,
862 errx(1, "can't add an intermediate node");
863 #endif /* USE_RFC2292BIS */
867 #ifndef USE_RFC2292BIS
868 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
869 errx(1, "can't set the last flag");
872 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
879 int dummy, len = sizeof(src);
881 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
882 err(1, "UDP socket");
884 src.sin6_family = AF_INET6;
885 src.sin6_addr = dst.sin6_addr;
886 src.sin6_port = ntohs(DUMMY_PORT);
887 src.sin6_scope_id = dst.sin6_scope_id;
889 #ifdef USE_RFC2292BIS
891 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
892 (void *)pktinfo, sizeof(*pktinfo)))
893 err(1, "UDP setsockopt(IPV6_PKTINFO)");
895 if (hoplimit != -1 &&
896 setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,
897 (void *)&hoplimit, sizeof(hoplimit)))
898 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
901 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
902 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
903 err(1, "UDP setsockopt(IPV6_RTHDR)");
904 #else /* old advanced API */
905 if (smsghdr.msg_control &&
906 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
907 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
908 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
911 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
912 err(1, "UDP connect");
914 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
915 err(1, "getsockname");
920 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
922 if (datalen > sockbufsize)
923 warnx("you need -b to increase socket buffer size");
924 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
925 sizeof(sockbufsize)) < 0)
926 err(1, "setsockopt(SO_SNDBUF)");
927 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
928 sizeof(sockbufsize)) < 0)
929 err(1, "setsockopt(SO_RCVBUF)");
932 if (datalen > 8 * 1024) /*XXX*/
933 warnx("you need -b to increase socket buffer size");
935 * When pinging the broadcast address, you can get a lot of
936 * answers. Doing something so evil is useful if you are trying
937 * to stress the ethernet, or just want to fill the arp cache
938 * to get some stuff for /etc/ethers.
941 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
947 #ifndef USE_SIN6_SCOPE_ID
948 #ifdef IPV6_RECVPKTINFO
949 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
951 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
952 #else /* old adv. API */
953 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
955 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
957 #endif /* USE_SIN6_SCOPE_ID */
958 #ifdef IPV6_RECVHOPLIMIT
959 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
961 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
962 #else /* old adv. API */
963 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
965 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
968 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
969 (unsigned long)(pingerlen() - 8));
970 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
971 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
973 while (preload--) /* Fire off them quickies. */
976 signal(SIGINT, onsignal);
978 signal(SIGINFO, onsignal);
981 if ((options & F_FLOOD) == 0) {
982 signal(SIGALRM, onsignal);
983 itimer.it_interval = interval;
984 itimer.it_value = interval;
985 setitimer(ITIMER_REAL, &itimer, NULL);
986 if (ntransmitted == 0)
990 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
991 if ((fdmaskp = malloc(fdmasks)) == NULL)
994 signo = seenalrm = seenint = 0;
1003 struct iovec iov[2];
1005 /* signal handling */
1024 if (options & F_FLOOD) {
1027 timeout.tv_usec = 10000;
1031 memset(fdmaskp, 0, fdmasks);
1033 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1035 if (errno != EINTR) {
1043 fromlen = sizeof(from);
1044 m.msg_name = (caddr_t)&from;
1045 m.msg_namelen = sizeof(from);
1046 memset(&iov, 0, sizeof(iov));
1047 iov[0].iov_base = (caddr_t)packet;
1048 iov[0].iov_len = packlen;
1051 cm = (struct cmsghdr *)buf;
1052 m.msg_control = (caddr_t)buf;
1053 m.msg_controllen = sizeof(buf);
1055 cc = recvmsg(s, &m, 0);
1057 if (errno != EINTR) {
1062 } else if (cc == 0) {
1066 * receive control messages only. Process the
1067 * exceptions (currently the only possiblity is
1068 * a path MTU notification.)
1070 if ((mtu = get_pathmtu(&m)) > 0) {
1071 if ((options & F_VERBOSE) != 0) {
1072 printf("new path MTU (%d) is "
1080 * an ICMPv6 message (probably an echoreply) arrived.
1082 pr_pack(packet, cc, &m);
1084 if (npackets && nreceived >= npackets)
1088 exit(nreceived == 0);
1112 * This routine transmits another ping6.
1117 struct itimerval itimer;
1123 * If we're not transmitting any more packets, change the timer
1124 * to wait two round-trip times if we've received any packets or
1125 * ten seconds if we haven't.
1129 itimer.it_value.tv_sec = 2 * tmax / 1000;
1130 if (itimer.it_value.tv_sec == 0)
1131 itimer.it_value.tv_sec = 1;
1133 itimer.it_value.tv_sec = MAXWAIT;
1134 itimer.it_interval.tv_sec = 0;
1135 itimer.it_interval.tv_usec = 0;
1136 itimer.it_value.tv_usec = 0;
1138 signal(SIGALRM, onint);
1139 setitimer(ITIMER_REAL, &itimer, NULL);
1144 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1145 * will be added on by the kernel. The ID field is our UNIX process ID,
1146 * and the sequence number is an ascending integer. The first 8 bytes
1147 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1148 * byte-order, to compute the round-trip time.
1155 if (options & F_FQDN)
1156 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1157 else if (options & F_FQDNOLD)
1159 else if (options & F_NODEADDR)
1160 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1161 else if (options & F_SUPTYPES)
1164 l = ICMP6ECHOLEN + datalen;
1172 struct icmp6_hdr *icp;
1173 struct iovec iov[2];
1175 struct icmp6_nodeinfo *nip;
1178 if (npackets && ntransmitted >= npackets)
1179 return(-1); /* no more transmission */
1181 icp = (struct icmp6_hdr *)outpack;
1182 nip = (struct icmp6_nodeinfo *)outpack;
1183 memset(icp, 0, sizeof(*icp));
1184 icp->icmp6_cksum = 0;
1185 seq = ntransmitted++;
1186 CLR(seq % mx_dup_ck);
1188 if (options & F_FQDN) {
1189 icp->icmp6_type = ICMP6_NI_QUERY;
1190 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1191 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1192 nip->ni_flags = htons(0);
1194 memcpy(nip->icmp6_ni_nonce, nonce,
1195 sizeof(nip->icmp6_ni_nonce));
1196 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1198 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1199 sizeof(dst.sin6_addr));
1200 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1202 } else if (options & F_FQDNOLD) {
1203 /* packet format in 03 draft - no Subject data on queries */
1204 icp->icmp6_type = ICMP6_NI_QUERY;
1205 icp->icmp6_code = 0; /* code field is always 0 */
1206 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1207 nip->ni_flags = htons(0);
1209 memcpy(nip->icmp6_ni_nonce, nonce,
1210 sizeof(nip->icmp6_ni_nonce));
1211 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1215 } else if (options & F_NODEADDR) {
1216 icp->icmp6_type = ICMP6_NI_QUERY;
1217 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1218 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1219 nip->ni_flags = naflags;
1221 memcpy(nip->icmp6_ni_nonce, nonce,
1222 sizeof(nip->icmp6_ni_nonce));
1223 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1225 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1226 sizeof(dst.sin6_addr));
1227 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1229 } else if (options & F_SUPTYPES) {
1230 icp->icmp6_type = ICMP6_NI_QUERY;
1231 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1232 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1233 /* we support compressed bitmap */
1234 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1236 memcpy(nip->icmp6_ni_nonce, nonce,
1237 sizeof(nip->icmp6_ni_nonce));
1238 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1242 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1243 icp->icmp6_code = 0;
1244 icp->icmp6_id = htons(ident);
1245 icp->icmp6_seq = ntohs(seq);
1247 gettimeofday((struct timeval *)
1248 &outpack[ICMP6ECHOLEN], NULL);
1249 cc = ICMP6ECHOLEN + datalen;
1253 if (pingerlen() != cc)
1254 errx(1, "internal error; length mismatch");
1257 smsghdr.msg_name = (caddr_t)&dst;
1258 smsghdr.msg_namelen = sizeof(dst);
1259 memset(&iov, 0, sizeof(iov));
1260 iov[0].iov_base = (caddr_t)outpack;
1261 iov[0].iov_len = cc;
1262 smsghdr.msg_iov = iov;
1263 smsghdr.msg_iovlen = 1;
1265 i = sendmsg(s, &smsghdr, 0);
1267 if (i < 0 || i != cc) {
1270 printf("ping6: wrote %s %d chars, ret=%d\n", hostname, cc, i);
1272 if (!(options & F_QUIET) && options & F_FLOOD)
1273 write(STDOUT_FILENO, &DOT, 1);
1279 myechoreply(const struct icmp6_hdr *icp)
1281 if (ntohs(icp->icmp6_id) == ident)
1288 mynireply(const struct icmp6_nodeinfo *nip)
1290 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1291 nonce + sizeof(u_int16_t),
1292 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1299 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, u_char *buf,
1304 char cresult[MAXDNAME + 1];
1315 if (i == 0 || cp != *sp) {
1316 if (strlcat(buf, ".", bufsiz) >= bufsiz)
1317 return NULL; /*result overrun*/
1323 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1324 /* DNS compression */
1328 comp = base + (i & 0x3f);
1329 if (dnsdecode(&comp, cp, base, cresult,
1330 sizeof(cresult)) == NULL)
1332 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1333 return NULL; /*result overrun*/
1335 } else if ((i & 0x3f) == i) {
1337 return NULL; /*source overrun*/
1338 while (i-- > 0 && cp < ep) {
1339 l = snprintf(cresult, sizeof(cresult),
1340 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1341 if (l >= (int)sizeof(cresult))
1343 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1344 return NULL; /*result overrun*/
1348 return NULL; /*invalid label*/
1351 return NULL; /*not terminated*/
1359 * Print out the packet, if it came from us. This logic is necessary
1360 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1361 * which arrive ('tis only fair). This permits multiple copies of this
1362 * program to be run without having intermingled output (or statistics!).
1365 pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1367 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1368 struct icmp6_hdr *icp;
1369 struct icmp6_nodeinfo *ni;
1372 struct sockaddr *from;
1374 u_char *cp = NULL, *dp, *end = buf + cc;
1375 struct in6_pktinfo *pktinfo = NULL;
1376 struct timeval tv, *tp;
1377 double triptime = 0;
1382 char dnsname[MAXDNAME + 1];
1384 gettimeofday(&tv, NULL);
1386 if (!mhdr || !mhdr->msg_name ||
1387 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1388 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1389 if (options & F_VERBOSE)
1390 warnx("invalid peername\n");
1393 from = (struct sockaddr *)mhdr->msg_name;
1394 fromlen = mhdr->msg_namelen;
1395 if (cc < (int)sizeof(struct icmp6_hdr)) {
1396 if (options & F_VERBOSE)
1397 warnx("packet too short (%d bytes) from %s\n", cc,
1398 pr_addr(from, fromlen));
1401 icp = (struct icmp6_hdr *)buf;
1402 ni = (struct icmp6_nodeinfo *)buf;
1405 if ((hoplim = get_hoplim(mhdr)) == -1) {
1406 warnx("failed to get receiving hop limit");
1409 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1410 warnx("failed to get receiving pakcet information");
1414 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1415 seq = ntohs(icp->icmp6_seq);
1418 tp = (struct timeval *)(icp + 1);
1420 triptime = ((double)tv.tv_sec) * 1000.0 +
1421 ((double)tv.tv_usec) / 1000.0;
1423 tsumsq += triptime * triptime;
1424 if (triptime < tmin)
1426 if (triptime > tmax)
1430 if (TST(seq % mx_dup_ck)) {
1435 SET(seq % mx_dup_ck);
1439 if (options & F_QUIET)
1442 if (options & F_FLOOD)
1443 write(STDOUT_FILENO, &BSPACE, 1);
1445 printf("%d bytes from %s, icmp_seq=%u", cc,
1446 pr_addr(from, fromlen), seq);
1447 printf(" hlim=%d", hoplim);
1448 if ((options & F_VERBOSE) != 0) {
1449 struct sockaddr_in6 dstsa;
1451 memset(&dstsa, 0, sizeof(dstsa));
1452 dstsa.sin6_family = AF_INET6;
1454 dstsa.sin6_len = sizeof(dstsa);
1456 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1457 dstsa.sin6_addr = pktinfo->ipi6_addr;
1459 pr_addr((struct sockaddr *)&dstsa,
1463 printf(" time=%g ms", triptime);
1466 /* check the data */
1467 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1468 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1469 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1471 printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1476 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1477 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1479 if (TST(seq % mx_dup_ck)) {
1484 SET(seq % mx_dup_ck);
1488 if (options & F_QUIET)
1491 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1493 switch (ntohs(ni->ni_code)) {
1494 case ICMP6_NI_SUCCESS:
1496 case ICMP6_NI_REFUSED:
1497 printf("refused, type 0x%x", ntohs(ni->ni_type));
1499 case ICMP6_NI_UNKNOWN:
1500 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1503 printf("unknown code 0x%x, type 0x%x",
1504 ntohs(ni->ni_code), ntohs(ni->ni_type));
1508 switch (ntohs(ni->ni_qtype)) {
1510 printf("NodeInfo NOOP");
1512 case NI_QTYPE_SUPTYPES:
1513 pr_suptypes(ni, end - (u_char *)ni);
1515 case NI_QTYPE_NODEADDR:
1516 pr_nodeaddr(ni, end - (u_char *)ni);
1519 default: /* XXX: for backward compatibility */
1520 cp = (u_char *)ni + ICMP6_NIRLEN;
1521 if (buf[off + ICMP6_NIRLEN] ==
1522 cc - off - ICMP6_NIRLEN - 1)
1527 cp++; /* skip length */
1529 safeputc(*cp & 0xff);
1535 if (dnsdecode((const u_char **)&cp, end,
1536 (const u_char *)(ni + 1), dnsname,
1537 sizeof(dnsname)) == NULL) {
1542 * name-lookup special handling for
1545 if (cp + 1 <= end && !*cp &&
1546 strlen(dnsname) > 0) {
1547 dnsname[strlen(dnsname) - 1] = '\0';
1550 printf("%s%s", i > 0 ? "," : "",
1554 if (options & F_VERBOSE) {
1560 switch (ni->ni_code) {
1561 case ICMP6_NI_REFUSED:
1565 case ICMP6_NI_UNKNOWN:
1566 printf("unknwon qtype");
1571 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1572 /* case of refusion, unknown */
1577 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1580 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1581 printf("TTL=%d:meaningless", (int)ttl);
1584 printf("TTL=%d:invalid", ttl);
1586 printf("TTL=%d", ttl);
1596 cp = (u_char *)ni + ICMP6_NIRLEN;
1605 if (buf[off + ICMP6_NIRLEN] !=
1606 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1609 printf("invalid namelen:%d/%lu",
1610 buf[off + ICMP6_NIRLEN],
1611 (u_long)cc - off - ICMP6_NIRLEN - 1);
1621 /* We've got something other than an ECHOREPLY */
1622 if (!(options & F_VERBOSE))
1624 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1628 if (!(options & F_FLOOD)) {
1630 if (options & F_VERBOSE)
1638 pr_exthdrs(struct msghdr *mhdr)
1642 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1643 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1644 if (cm->cmsg_level != IPPROTO_IPV6)
1647 switch (cm->cmsg_type) {
1649 printf(" HbH Options: ");
1650 pr_ip6opt(CMSG_DATA(cm));
1653 #ifdef IPV6_RTHDRDSTOPTS
1654 case IPV6_RTHDRDSTOPTS:
1656 printf(" Dst Options: ");
1657 pr_ip6opt(CMSG_DATA(cm));
1660 printf(" Routing: ");
1661 pr_rthdr(CMSG_DATA(cm));
1667 #ifdef USE_RFC2292BIS
1669 pr_ip6opt(void *extbuf)
1671 struct ip6_hbh *ext;
1680 ext = (struct ip6_hbh *)extbuf;
1681 extlen = (ext->ip6h_len + 1) * 8;
1682 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1683 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1687 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1688 &type, &len, &databuf);
1689 if (currentlen == -1)
1693 * Note that inet6_opt_next automatically skips any padding
1698 offset = inet6_opt_get_val(databuf, offset,
1699 &value4, sizeof(value4));
1700 printf(" Jumbo Payload Opt: Length %u\n",
1701 (u_int32_t)ntohl(value4));
1703 case IP6OPT_ROUTER_ALERT:
1705 offset = inet6_opt_get_val(databuf, offset,
1706 &value2, sizeof(value2));
1707 printf(" Router Alert Opt: Type %u\n",
1711 printf(" Received Opt %u len %lu\n",
1712 type, (unsigned long)len);
1718 #else /* !USE_RFC2292BIS */
1721 pr_ip6opt(void *extbuf __unused)
1726 #endif /* USE_RFC2292BIS */
1728 #ifdef USE_RFC2292BIS
1730 pr_rthdr(void *extbuf)
1732 struct in6_addr *in6;
1733 char ntopbuf[INET6_ADDRSTRLEN];
1734 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1737 /* print fixed part of the header */
1738 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1739 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1740 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1741 printf("%d segments, ", segments);
1743 printf("segments unknown, ");
1744 printf("%d left\n", rh->ip6r_segleft);
1746 for (i = 0; i < segments; i++) {
1747 in6 = inet6_rth_getaddr(extbuf, i);
1749 printf(" [%d]<NULL>\n", i);
1751 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1753 strncpy(ntopbuf, "?", sizeof(ntopbuf));
1754 printf(" [%d]%s\n", i, ntopbuf);
1762 #else /* !USE_RFC2292BIS */
1765 pr_rthdr(void *extbuf __unused)
1770 #endif /* USE_RFC2292BIS */
1773 pr_bitrange(u_int32_t v, int ss, int ii)
1780 /* shift till we have 0x01 */
1781 if ((v & 0x01) == 0) {
1783 printf("-%u", s + off - 1);
1794 case 0x04: case 0x0c:
1805 /* we have 0x01 with us */
1806 for (i = 0; i < 32 - off; i++) {
1807 if ((v & (0x01 << i)) == 0)
1811 printf(" %u", ss + off);
1818 /* struct icmp6_nodeinfo *ni: ni->qtype must be SUPTYPES */
1820 pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
1824 const u_char *cp, *end;
1827 u_int16_t words; /*32bit count*/
1830 #define MAXQTYPES (1 << 16)
1834 cp = (u_char *)(ni + 1);
1835 end = ((u_char *)ni) + nilen;
1839 printf("NodeInfo Supported Qtypes");
1840 if (options & F_VERBOSE) {
1841 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1842 printf(", compressed bitmap");
1844 printf(", raw bitmap");
1848 clen = (size_t)(end - cp);
1849 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1850 if (clen == 0 || clen > MAXQTYPES / 8 ||
1856 if (clen < sizeof(cbit) || clen % sizeof(v))
1858 memcpy(&cbit, cp, sizeof(cbit));
1859 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1863 clen = ntohs(cbit.words) * sizeof(v);
1864 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1869 for (off = 0; off < clen; off += sizeof(v)) {
1870 memcpy(&v, cp + off, sizeof(v));
1871 v = (u_int32_t)ntohl(v);
1872 b = pr_bitrange(v, (int)(cur + off * 8), b);
1874 /* flush the remaining bits */
1875 b = pr_bitrange(0, (int)(cur + off * 8), b);
1879 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1880 cur += ntohs(cbit.skip) * 32;
1884 /* struct icmp6_nodeinfo *ni: ni->qtype must be NODEADDR */
1886 pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
1888 u_char *cp = (u_char *)(ni + 1);
1889 char ntop_buf[INET6_ADDRSTRLEN];
1892 nilen -= sizeof(struct icmp6_nodeinfo);
1894 if (options & F_VERBOSE) {
1895 switch (ni->ni_code) {
1896 case ICMP6_NI_REFUSED:
1899 case ICMP6_NI_UNKNOWN:
1900 printf("unknown qtype");
1903 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
1904 printf(" truncated");
1908 printf(" no address\n");
1911 * In icmp-name-lookups 05 and later, TTL of each returned address
1912 * is contained in the resposne. We try to detect the version
1913 * by the length of the data, but note that the detection algorithm
1914 * is incomplete. We assume the latest draft by default.
1916 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
1922 /* XXX: alignment? */
1923 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
1924 cp += sizeof(u_int32_t);
1925 nilen -= sizeof(u_int32_t);
1928 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
1930 strncpy(ntop_buf, "?", sizeof(ntop_buf));
1931 printf(" %s", ntop_buf);
1933 if (ttl == 0xffffffff) {
1935 * XXX: can this convention be applied to all
1936 * type of TTL (i.e. non-ND TTL)?
1938 printf("(TTL=infty)");
1941 printf("(TTL=%u)", ttl);
1945 nilen -= sizeof(struct in6_addr);
1946 cp += sizeof(struct in6_addr);
1951 get_hoplim(struct msghdr *mhdr)
1955 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1956 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1957 if (cm->cmsg_len == 0)
1960 if (cm->cmsg_level == IPPROTO_IPV6 &&
1961 cm->cmsg_type == IPV6_HOPLIMIT &&
1962 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1963 return(*(int *)CMSG_DATA(cm));
1969 struct in6_pktinfo *
1970 get_rcvpktinfo(struct msghdr *mhdr)
1974 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1975 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1976 if (cm->cmsg_len == 0)
1979 if (cm->cmsg_level == IPPROTO_IPV6 &&
1980 cm->cmsg_type == IPV6_PKTINFO &&
1981 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1982 return((struct in6_pktinfo *)CMSG_DATA(cm));
1988 #ifdef IPV6_RECVPATHMTU
1990 get_pathmtu(struct msghdr *mhdr)
1993 struct ip6_mtuinfo *mtuctl = NULL;
1995 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1996 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1997 if (cm->cmsg_len == 0)
2000 if (cm->cmsg_level == IPPROTO_IPV6 &&
2001 cm->cmsg_type == IPV6_PATHMTU &&
2002 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2003 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2006 * If the notified destination is different from
2007 * the one we are pinging, just ignore the info.
2008 * We check the scope ID only when both notified value
2009 * and our own value have non-0 values, because we may
2010 * have used the default scope zone ID for sending,
2011 * in which case the scope ID value is 0.
2013 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2015 (mtuctl->ip6m_addr.sin6_scope_id &&
2016 dst.sin6_scope_id &&
2017 mtuctl->ip6m_addr.sin6_scope_id !=
2018 dst.sin6_scope_id)) {
2019 if ((options & F_VERBOSE) != 0) {
2020 printf("path MTU for %s is notified. "
2022 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2023 sizeof(mtuctl->ip6m_addr)));
2029 * Ignore an invalid MTU. XXX: can we just believe
2032 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2035 /* notification for our destination. return the MTU. */
2036 return((int)mtuctl->ip6m_mtu);
2045 set_pathmtu(int mtu)
2047 static int firsttime = 1;
2051 int oldlen = smsghdr.msg_controllen;
2052 char *oldbuf = smsghdr.msg_control;
2054 /* XXX: We need to enlarge control message buffer */
2055 firsttime = 0; /* prevent further enlargement */
2057 smsghdr.msg_controllen = oldlen + CMSG_SPACE(sizeof(int));
2058 if ((smsghdr.msg_control =
2059 (char *)malloc(smsghdr.msg_controllen)) == NULL)
2060 err(1, "set_pathmtu: malloc");
2061 cm = (struct cmsghdr *)CMSG_FIRSTHDR(&smsghdr);
2062 cm->cmsg_len = CMSG_LEN(sizeof(int));
2063 cm->cmsg_level = IPPROTO_IPV6;
2064 cm->cmsg_type = IPV6_USE_MTU;
2066 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm);
2068 memcpy((void *)cm, (void *)oldbuf, oldlen);
2074 * look for a cmsgptr that points MTU structure.
2075 * XXX: this procedure seems redundant at this moment, but we'd better
2076 * keep the code generic enough for future extensions.
2078 for (cm = CMSG_FIRSTHDR(&smsghdr); cm;
2079 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm)) {
2080 if (cm->cmsg_len == 0) /* XXX: paranoid check */
2081 errx(1, "set_pathmtu: internal error");
2083 if (cm->cmsg_level == IPPROTO_IPV6 &&
2084 cm->cmsg_type == IPV6_USE_MTU &&
2085 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2090 errx(1, "set_pathmtu: internal error: no space for path MTU");
2092 *(int *)CMSG_DATA(cm) = mtu;
2098 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2102 tvsub(struct timeval *out, struct timeval *in)
2104 if ((out->tv_usec -= in->tv_usec) < 0) {
2106 out->tv_usec += 1000000;
2108 out->tv_sec -= in->tv_sec;
2117 onint(int sig __unused)
2119 signal(SIGINT, SIG_IGN);
2120 signal(SIGALRM, SIG_IGN);
2124 exit(nreceived == 0);
2129 * Print out statistics.
2135 printf("\n--- %s ping6 statistics ---\n", hostname);
2136 printf("%ld packets transmitted, ", ntransmitted);
2137 printf("%ld packets received, ", nreceived);
2139 printf("+%ld duplicates, ", nrepeats);
2141 if (nreceived > ntransmitted)
2142 printf("-- somebody's printing up packets!");
2144 printf("%d%% packet loss",
2145 (int) (((ntransmitted - nreceived) * 100) /
2149 if (nreceived && timing) {
2150 /* Only display average to microseconds */
2151 double num = nreceived + nrepeats;
2152 double avg = tsum / num;
2153 double dev = sqrt(tsumsq / num - avg * avg);
2155 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2156 tmin, avg, tmax, dev);
2163 static const char *niqcode[] = {
2165 "DNS label", /*or empty*/
2170 static const char *nircode[] = {
2171 "Success", "Refused", "Unknown",
2177 * Print a descriptive string about an ICMP header.
2180 pr_icmph(struct icmp6_hdr *icp, u_char *end)
2182 char ntop_buf[INET6_ADDRSTRLEN];
2183 struct nd_redirect *red;
2184 struct icmp6_nodeinfo *ni;
2185 char dnsname[MAXDNAME + 1];
2189 switch (icp->icmp6_type) {
2190 case ICMP6_DST_UNREACH:
2191 switch (icp->icmp6_code) {
2192 case ICMP6_DST_UNREACH_NOROUTE:
2193 printf("No Route to Destination\n");
2195 case ICMP6_DST_UNREACH_ADMIN:
2196 printf("Destination Administratively Unreachable\n");
2198 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2199 printf("Destination Unreachable Beyond Scope\n");
2201 case ICMP6_DST_UNREACH_ADDR:
2202 printf("Destination Host Unreachable\n");
2204 case ICMP6_DST_UNREACH_NOPORT:
2205 printf("Destination Port Unreachable\n");
2208 printf("Destination Unreachable, Bad Code: %d\n",
2212 /* Print returned IP header information */
2213 pr_retip((struct ip6_hdr *)(icp + 1), end);
2215 case ICMP6_PACKET_TOO_BIG:
2216 printf("Packet too big mtu = %d\n",
2217 (int)ntohl(icp->icmp6_mtu));
2218 pr_retip((struct ip6_hdr *)(icp + 1), end);
2220 case ICMP6_TIME_EXCEEDED:
2221 switch (icp->icmp6_code) {
2222 case ICMP6_TIME_EXCEED_TRANSIT:
2223 printf("Time to live exceeded\n");
2225 case ICMP6_TIME_EXCEED_REASSEMBLY:
2226 printf("Frag reassembly time exceeded\n");
2229 printf("Time exceeded, Bad Code: %d\n",
2233 pr_retip((struct ip6_hdr *)(icp + 1), end);
2235 case ICMP6_PARAM_PROB:
2236 printf("Parameter problem: ");
2237 switch (icp->icmp6_code) {
2238 case ICMP6_PARAMPROB_HEADER:
2239 printf("Erroneous Header ");
2241 case ICMP6_PARAMPROB_NEXTHEADER:
2242 printf("Unknown Nextheader ");
2244 case ICMP6_PARAMPROB_OPTION:
2245 printf("Unrecognized Option ");
2248 printf("Bad code(%d) ", icp->icmp6_code);
2251 printf("pointer = 0x%02x\n", (u_int32_t)ntohl(icp->icmp6_pptr));
2252 pr_retip((struct ip6_hdr *)(icp + 1), end);
2254 case ICMP6_ECHO_REQUEST:
2255 printf("Echo Request");
2256 /* XXX ID + Seq + Data */
2258 case ICMP6_ECHO_REPLY:
2259 printf("Echo Reply");
2260 /* XXX ID + Seq + Data */
2262 case ICMP6_MEMBERSHIP_QUERY:
2263 printf("Listener Query");
2265 case ICMP6_MEMBERSHIP_REPORT:
2266 printf("Listener Report");
2268 case ICMP6_MEMBERSHIP_REDUCTION:
2269 printf("Listener Done");
2271 case ND_ROUTER_SOLICIT:
2272 printf("Router Solicitation");
2274 case ND_ROUTER_ADVERT:
2275 printf("Router Advertisement");
2277 case ND_NEIGHBOR_SOLICIT:
2278 printf("Neighbor Solicitation");
2280 case ND_NEIGHBOR_ADVERT:
2281 printf("Neighbor Advertisement");
2284 red = (struct nd_redirect *)icp;
2285 printf("Redirect\n");
2286 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2288 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2289 printf("Destination: %s", ntop_buf);
2290 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2292 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2293 printf(" New Target: %s", ntop_buf);
2295 case ICMP6_NI_QUERY:
2296 printf("Node Information Query");
2297 /* XXX ID + Seq + Data */
2298 ni = (struct icmp6_nodeinfo *)icp;
2299 l = end - (u_char *)(ni + 1);
2301 switch (ntohs(ni->ni_qtype)) {
2305 case NI_QTYPE_SUPTYPES:
2306 printf("Supported qtypes");
2311 case NI_QTYPE_NODEADDR:
2314 case NI_QTYPE_IPV4ADDR:
2315 printf("IPv4 nodeaddr");
2318 printf("unknown qtype");
2321 if (options & F_VERBOSE) {
2322 switch (ni->ni_code) {
2323 case ICMP6_NI_SUBJ_IPV6:
2324 if (l == sizeof(struct in6_addr) &&
2325 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2326 sizeof(ntop_buf)) != NULL) {
2327 printf(", subject=%s(%s)",
2328 niqcode[ni->ni_code], ntop_buf);
2331 /* backward compat to -W */
2332 printf(", oldfqdn");
2334 printf(", invalid");
2338 case ICMP6_NI_SUBJ_FQDN:
2339 if (end == (u_char *)(ni + 1)) {
2340 printf(", no subject");
2343 printf(", subject=%s", niqcode[ni->ni_code]);
2344 cp = (const u_char *)(ni + 1);
2345 if (dnsdecode(&cp, end, NULL, dnsname,
2346 sizeof(dnsname)) != NULL)
2347 printf("(%s)", dnsname);
2349 printf("(invalid)");
2351 case ICMP6_NI_SUBJ_IPV4:
2352 if (l == sizeof(struct in_addr) &&
2353 inet_ntop(AF_INET, ni + 1, ntop_buf,
2354 sizeof(ntop_buf)) != NULL) {
2355 printf(", subject=%s(%s)",
2356 niqcode[ni->ni_code], ntop_buf);
2358 printf(", invalid");
2361 printf(", invalid");
2366 case ICMP6_NI_REPLY:
2367 printf("Node Information Reply");
2368 /* XXX ID + Seq + Data */
2369 ni = (struct icmp6_nodeinfo *)icp;
2371 switch (ntohs(ni->ni_qtype)) {
2375 case NI_QTYPE_SUPTYPES:
2376 printf("Supported qtypes");
2381 case NI_QTYPE_NODEADDR:
2384 case NI_QTYPE_IPV4ADDR:
2385 printf("IPv4 nodeaddr");
2388 printf("unknown qtype");
2391 if (options & F_VERBOSE) {
2392 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2393 printf(", invalid");
2395 printf(", %s", nircode[ni->ni_code]);
2399 printf("Bad ICMP type: %d", icp->icmp6_type);
2405 * Print an IP6 header.
2408 pr_iph(struct ip6_hdr *ip6)
2410 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2412 char ntop_buf[INET6_ADDRSTRLEN];
2414 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2415 tc = (tc >> 4) & 0x0f;
2416 tc |= (ip6->ip6_vfc << 4);
2418 printf("Vr TC Flow Plen Nxt Hlim\n");
2419 printf(" %1x %02x %05x %04x %02x %02x\n",
2420 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2421 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2422 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2423 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2424 printf("%s->", ntop_buf);
2425 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2426 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2427 printf("%s\n", ntop_buf);
2432 * Return an ascii host address as a dotted quad and optionally with
2436 pr_addr(struct sockaddr *addr, int addrlen)
2438 static char buf[NI_MAXHOST];
2441 #ifdef NI_WITHSCOPEID
2442 flag = NI_WITHSCOPEID;
2446 if ((options & F_HOSTNAME) == 0)
2447 flag |= NI_NUMERICHOST;
2449 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2457 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2460 pr_retip(struct ip6_hdr *ip6, u_char *end)
2462 u_char *cp = (u_char *)ip6, nh;
2465 if (end - (u_char *)ip6 < (int)sizeof(*ip6)) {
2470 hlen = sizeof(*ip6);
2474 while (end - cp >= 8) {
2476 case IPPROTO_HOPOPTS:
2478 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2479 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2481 case IPPROTO_DSTOPTS:
2483 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2484 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2486 case IPPROTO_FRAGMENT:
2488 hlen = sizeof(struct ip6_frag);
2489 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2491 case IPPROTO_ROUTING:
2493 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2494 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2499 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2500 nh = ((struct ah *)cp)->ah_nxt;
2503 case IPPROTO_ICMPV6:
2504 printf("ICMP6: type = %d, code = %d\n",
2511 printf("TCP: from port %u, to port %u (decimal)\n",
2512 (*cp * 256 + *(cp + 1)),
2513 (*(cp + 2) * 256 + *(cp + 3)));
2516 printf("UDP: from port %u, to port %u (decimal)\n",
2517 (*cp * 256 + *(cp + 1)),
2518 (*(cp + 2) * 256 + *(cp + 3)));
2521 printf("Unknown Header(%d)\n", nh);
2525 if ((cp += hlen) >= end)
2540 fill(char *bp, char *patp)
2546 for (cp = patp; *cp; cp++)
2548 errx(1, "patterns must be specified as hex digits");
2550 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2551 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2552 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2553 &pat[13], &pat[14], &pat[15]);
2558 kk <= MAXDATALEN - (8 + (int)sizeof(struct timeval) + ii);
2560 for (jj = 0; jj < ii; ++jj)
2561 bp[jj + kk] = pat[jj];
2562 if (!(options & F_QUIET)) {
2563 printf("PATTERN: 0x");
2564 for (jj = 0; jj < ii; ++jj)
2565 printf("%02x", bp[jj] & 0xFF);
2571 #ifdef IPSEC_POLICY_IPSEC
2573 setpolicy(int so __unused, char *policy)
2578 return 0; /* ignore */
2580 buf = ipsec_set_policy(policy, strlen(policy));
2582 errx(1, "%s", ipsec_strerror());
2583 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2584 ipsec_get_policylen(buf)) < 0)
2585 warnx("Unable to set IPSec policy");
2599 u_int8_t digest[16];
2602 char hbuf[NI_MAXHOST];
2603 struct in6_addr in6;
2605 p = strchr(name, '.');
2607 p = name + strlen(name);
2609 if (l > 63 || l > sizeof(hbuf) - 1)
2610 return NULL; /*label too long*/
2611 strncpy(hbuf, name, l);
2612 hbuf[(int)l] = '\0';
2614 for (q = name; *q; q++) {
2619 /* generate 8 bytes of pseudo-random value. */
2620 bzero(&ctxt, sizeof(ctxt));
2623 MD5Update(&ctxt, &c, sizeof(c));
2624 MD5Update(&ctxt, name, l);
2625 MD5Final(digest, &ctxt);
2627 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2628 return NULL; /*XXX*/
2629 bcopy(digest, &in6.s6_addr[12], 4);
2631 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2634 return strdup(hbuf);
2641 "usage: ping6 [-dfH"
2642 #ifdef IPV6_USE_MIN_MTU
2646 #ifdef IPV6_REACHCONF
2650 #ifdef IPSEC_POLICY_IPSEC
2656 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
2657 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2659 "\t[-s packetsize] [-h hoplimit] [hops...] host\n");