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.2 2003/06/17 04:27:34 dillon 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>
117 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
127 #include <netinet6/ah.h>
128 #include <netinet6/ipsec.h>
131 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
137 #define MAXPACKETLEN 131072
139 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
140 #define ICMP6ECHOTMLEN sizeof(struct timeval)
141 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
142 /* FQDN case, 64 bits of nonce + 32 bits ttl */
143 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
144 #define EXTRA 256 /* for AH and various other headers. weird. */
145 #define DEFDATALEN ICMP6ECHOTMLEN
146 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
147 #define NROUTES 9 /* number of record route slots */
149 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
150 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
151 #define SET(bit) (A(bit) |= B(bit))
152 #define CLR(bit) (A(bit) &= (~B(bit)))
153 #define TST(bit) (A(bit) & B(bit))
155 #define F_FLOOD 0x0001
156 #define F_INTERVAL 0x0002
157 #define F_PINGFILLED 0x0008
158 #define F_QUIET 0x0010
159 #define F_RROUTE 0x0020
160 #define F_SO_DEBUG 0x0040
161 #define F_VERBOSE 0x0100
163 #ifdef IPSEC_POLICY_IPSEC
164 #define F_POLICY 0x0400
166 #define F_AUTHHDR 0x0200
167 #define F_ENCRYPT 0x0400
168 #endif /*IPSEC_POLICY_IPSEC*/
170 #define F_NODEADDR 0x0800
171 #define F_FQDN 0x1000
172 #define F_INTERFACE 0x2000
173 #define F_SRCADDR 0x4000
174 #ifdef IPV6_REACHCONF
175 #define F_REACHCONF 0x8000
177 #define F_HOSTNAME 0x10000
178 #define F_FQDNOLD 0x20000
179 #define F_NIGROUP 0x40000
180 #define F_SUPTYPES 0x80000
181 #define F_NOMINMTU 0x100000
182 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
185 #define IN6LEN sizeof(struct in6_addr)
186 #define SA6LEN sizeof(struct sockaddr_in6)
187 #define DUMMY_PORT 10101
189 #define SIN6(s) ((struct sockaddr_in6 *)(s))
192 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
193 * number of received sequence numbers we can keep track of. Change 128
194 * to 8192 for complete accuracy...
196 #define MAX_DUP_CHK (8 * 8192)
197 int mx_dup_ck = MAX_DUP_CHK;
198 char rcvd_tbl[MAX_DUP_CHK / 8];
200 struct addrinfo *res;
201 struct sockaddr_in6 dst; /* who to ping6 */
202 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 struct in6_addr srcaddr;
212 int hoplimit = -1; /* hoplimit */
213 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
216 long npackets; /* max packets to transmit */
217 long nreceived; /* # of packets we got back */
218 long nrepeats; /* number of duplicates */
219 long ntransmitted; /* sequence # for outbound packets = #sent */
220 struct timeval interval = {1, 0}; /* interval between packets */
223 int timing; /* flag to do timing */
224 double tmin = 999999999.0; /* minimum round trip time */
225 double tmax = 0.0; /* maximum round trip time */
226 double tsum = 0.0; /* sum of all times, for doing average */
227 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
228 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
231 /* for node addresses */
234 /* for ancillary data(advanced API) */
235 struct msghdr smsghdr;
236 struct iovec smsgiov;
240 volatile sig_atomic_t seenalrm;
241 volatile sig_atomic_t seenint;
243 volatile sig_atomic_t seeninfo;
246 int main __P((int, char *[]));
247 void fill __P((char *, char *));
248 int get_hoplim __P((struct msghdr *));
249 int get_pathmtu __P((struct msghdr *));
250 void set_pathmtu __P((int));
251 struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
252 void onsignal __P((int));
253 void retransmit __P((void));
254 void onint __P((int));
255 size_t pingerlen __P((void));
256 int pinger __P((void));
257 const char *pr_addr __P((struct sockaddr *, int));
258 void pr_icmph __P((struct icmp6_hdr *, u_char *));
259 void pr_iph __P((struct ip6_hdr *));
260 void pr_suptypes __P((struct icmp6_nodeinfo *, size_t));
261 void pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
262 int myechoreply __P((const struct icmp6_hdr *));
263 int mynireply __P((const struct icmp6_nodeinfo *));
264 char *dnsdecode __P((const u_char **, const u_char *, const u_char *,
266 void pr_pack __P((u_char *, int, struct msghdr *));
267 void pr_exthdrs __P((struct msghdr *));
268 void pr_ip6opt __P((void *));
269 void pr_rthdr __P((void *));
270 int pr_bitrange __P((u_int32_t, int, int));
271 void pr_retip __P((struct ip6_hdr *, u_char *));
272 void summary __P((void));
273 void tvsub __P((struct timeval *, struct timeval *));
274 int setpolicy __P((int, char *));
275 char *nigroup __P((char *));
276 void usage __P((void));
283 struct itimerval itimer;
284 struct sockaddr_in6 from;
285 struct timeval timeout, *tv;
286 struct addrinfo hints;
290 int ch, fromlen, hold, packlen, preload, optval, ret_ga;
291 u_char *datap, *packet;
292 char *e, *target, *ifname = NULL;
294 struct cmsghdr *scmsgp = NULL;
297 struct in6_pktinfo *pktinfo = NULL;
298 #ifdef USE_RFC2292BIS
299 struct ip6_rthdr *rthdr = NULL;
301 #ifdef IPSEC_POLICY_IPSEC
302 char *policy_in = NULL;
303 char *policy_out = NULL;
308 /* just to be sure */
309 memset(&smsghdr, 0, sizeof(smsghdr));
310 memset(&smsgiov, 0, sizeof(smsgiov));
313 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
317 #ifdef IPSEC_POLICY_IPSEC
321 #endif /*IPSEC_POLICY_IPSEC*/
323 while ((ch = getopt(argc, argv,
324 "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {
331 options &= ~F_NOUSERDATA;
332 options |= F_NODEADDR;
333 for (cp = optarg; *cp != '\0'; cp++) {
336 naflags |= NI_NODEADDR_FLAG_ALL;
340 naflags |= NI_NODEADDR_FLAG_COMPAT;
344 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
348 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
352 naflags |= NI_NODEADDR_FLAG_GLOBAL;
354 case 'A': /* experimental. not in the spec */
355 #ifdef NI_NODEADDR_FLAG_ANYCAST
356 naflags |= NI_NODEADDR_FLAG_ANYCAST;
360 "-a A is not supported on the platform");
371 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
372 sockbufsize = atoi(optarg);
375 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
379 npackets = strtol(optarg, &e, 10);
380 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
382 "illegal number of packets -- %s", optarg);
385 options |= F_SO_DEBUG;
390 errx(1, "Must be superuser to flood ping");
393 setbuf(stdout, (char *)NULL);
396 options |= F_HOSTNAME;
398 case 'h': /* hoplimit */
399 hoplimit = strtol(optarg, &e, 10);
400 if (255 < hoplimit || hoplimit < -1)
402 "illegal hoplimit -- %s", optarg);
406 options |= F_INTERFACE;
407 #ifndef USE_SIN6_SCOPE_ID
411 case 'i': /* wait between sending packets */
412 intval = strtod(optarg, &e);
413 if (*optarg == '\0' || *e != '\0')
414 errx(1, "illegal timing interval %s", optarg);
415 if (intval < 1 && getuid()) {
416 errx(1, "%s: only root may use interval < 1s",
419 interval.tv_sec = (long)intval;
421 (long)((intval - interval.tv_sec) * 1000000);
422 if (interval.tv_sec < 0)
423 errx(1, "illegal timing interval %s", optarg);
424 /* less than 1/hz does not make sense */
425 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
426 warnx("too small interval, raised to 0.01");
427 interval.tv_usec = 10000;
429 options |= F_INTERVAL;
434 errx(1, "Must be superuser to preload");
436 preload = strtol(optarg, &e, 10);
437 if (preload < 0 || *optarg == '\0' || *e != '\0')
438 errx(1, "illegal preload value -- %s", optarg);
441 #ifdef IPV6_USE_MIN_MTU
442 options |= F_NOMINMTU;
445 errx(1, "-%c is not supported on this platform", ch);
449 options &= ~F_HOSTNAME;
452 options |= F_NIGROUP;
454 case 'p': /* fill buffer with user pattern */
455 options |= F_PINGFILLED;
456 fill((char *)datap, optarg);
462 #ifdef IPV6_REACHCONF
463 options |= F_REACHCONF;
466 errx(1, "-R is not supported in this configuration");
469 /* XXX: use getaddrinfo? */
470 if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
471 errx(1, "invalid IPv6 address: %s", optarg);
472 options |= F_SRCADDR;
475 case 's': /* size of packet to send */
476 datalen = strtol(optarg, &e, 10);
477 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
478 errx(1, "illegal datalen value -- %s", optarg);
479 if (datalen > MAXDATALEN) {
481 "datalen value too large, maximum is %d",
486 options &= ~F_NOUSERDATA;
487 options |= F_SUPTYPES;
490 options |= F_VERBOSE;
493 options &= ~F_NOUSERDATA;
497 options &= ~F_NOUSERDATA;
498 options |= F_FQDNOLD;
501 #ifdef IPSEC_POLICY_IPSEC
504 if (!strncmp("in", optarg, 2)) {
505 if ((policy_in = strdup(optarg)) == NULL)
507 } else if (!strncmp("out", optarg, 3)) {
508 if ((policy_out = strdup(optarg)) == NULL)
511 errx(1, "invalid security policy");
515 options |= F_AUTHHDR;
518 options |= F_ENCRYPT;
520 #endif /*IPSEC_POLICY_IPSEC*/
536 #ifdef IPV6_RECVRTHDR /* 2292bis */
537 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
540 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
543 errx(1, "too many intermediate hops");
549 if (options & F_NIGROUP) {
550 target = nigroup(argv[argc - 1]);
551 if (target == NULL) {
556 target = argv[argc - 1];
559 bzero(&hints, sizeof(struct addrinfo));
560 hints.ai_flags = AI_CANONNAME;
561 hints.ai_family = AF_INET6;
562 hints.ai_socktype = SOCK_RAW;
563 hints.ai_protocol = IPPROTO_ICMPV6;
565 ret_ga = getaddrinfo(target, NULL, &hints, &res);
567 fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
570 if (res->ai_canonname)
571 hostname = res->ai_canonname;
576 errx(1, "getaddrinfo failed");
578 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
580 if ((s = socket(res->ai_family, res->ai_socktype,
581 res->ai_protocol)) < 0)
585 * let the kerel pass extension headers of incoming packets,
586 * for privileged socket options
588 if ((options & F_VERBOSE) != 0) {
591 #ifdef IPV6_RECVHOPOPTS
592 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
594 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
595 #else /* old adv. API */
596 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
598 err(1, "setsockopt(IPV6_HOPOPTS)");
600 #ifdef IPV6_RECVDSTOPTS
601 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
603 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
604 #else /* old adv. API */
605 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
607 err(1, "setsockopt(IPV6_DSTOPTS)");
609 #ifdef IPV6_RECVRTHDRDSTOPTS
610 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
612 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
616 /* revoke root privilege */
620 if (options & F_FLOOD && options & F_INTERVAL)
621 errx(1, "-f and -i incompatible options");
623 if ((options & F_NOUSERDATA) == 0) {
624 if (datalen >= sizeof(struct timeval)) {
625 /* we can time transfer */
629 /* in F_VERBOSE case, we may get non-echoreply packets*/
630 if (options & F_VERBOSE)
631 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
633 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
635 /* suppress timing for node information query */
638 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
641 if (!(packet = (u_char *)malloc((u_int)packlen)))
642 err(1, "Unable to allocate packet");
643 if (!(options & F_PINGFILLED))
644 for (i = ICMP6ECHOLEN; i < packlen; ++i)
647 ident = getpid() & 0xFFFF;
649 gettimeofday(&timeout, NULL);
650 srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));
651 memset(nonce, 0, sizeof(nonce));
652 for (i = 0; i < sizeof(nonce); i += sizeof(int))
653 *((int *)&nonce[i]) = rand();
655 memset(nonce, 0, sizeof(nonce));
656 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
657 *((u_int32_t *)&nonce[i]) = arc4random();
662 if (options & F_SO_DEBUG)
663 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
665 optval = IPV6_DEFHLIM;
666 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
667 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
668 &optval, sizeof(optval)) == -1)
669 err(1, "IPV6_MULTICAST_HOPS");
670 #ifdef IPV6_USE_MIN_MTU
671 if ((options & F_NOMINMTU) == 0) {
673 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
674 &optval, sizeof(optval)) == -1)
675 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
677 #ifdef IPV6_RECVPATHMTU
680 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
681 &optval, sizeof(optval)) == -1)
682 err(1, "setsockopt(IPV6_RECVPATHMTU)");
684 #endif /* IPV6_RECVPATHMTU */
685 #endif /* IPV6_USE_MIN_MTU */
688 #ifdef IPSEC_POLICY_IPSEC
689 if (options & F_POLICY) {
690 if (setpolicy(s, policy_in) < 0)
691 errx(1, "%s", ipsec_strerror());
692 if (setpolicy(s, policy_out) < 0)
693 errx(1, "%s", ipsec_strerror());
696 if (options & F_AUTHHDR) {
697 optval = IPSEC_LEVEL_REQUIRE;
698 #ifdef IPV6_AUTH_TRANS_LEVEL
699 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
700 &optval, sizeof(optval)) == -1)
701 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
703 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
704 &optval, sizeof(optval)) == -1)
705 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
708 if (options & F_ENCRYPT) {
709 optval = IPSEC_LEVEL_REQUIRE;
710 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
711 &optval, sizeof(optval)) == -1)
712 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
714 #endif /*IPSEC_POLICY_IPSEC*/
719 struct icmp6_filter filt;
720 if (!(options & F_VERBOSE)) {
721 ICMP6_FILTER_SETBLOCKALL(&filt);
722 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
723 (options & F_NODEADDR) || (options & F_SUPTYPES))
724 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
726 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
728 ICMP6_FILTER_SETPASSALL(&filt);
730 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
732 err(1, "setsockopt(ICMP6_FILTER)");
734 #endif /*ICMP6_FILTER*/
736 /* let the kerel pass extension headers of incoming packets */
737 if ((options & F_VERBOSE) != 0) {
740 #ifdef IPV6_RECVRTHDR
741 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
743 err(1, "setsockopt(IPV6_RECVRTHDR)");
744 #else /* old adv. API */
745 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
747 err(1, "setsockopt(IPV6_RTHDR)");
753 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
754 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
755 &optval, sizeof(optval)) == -1)
756 err(1, "IPV6_MULTICAST_LOOP");
759 /* Specify the outgoing interface and/or the source address */
761 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
764 ip6optlen += CMSG_SPACE(sizeof(int));
766 #ifdef IPV6_REACHCONF
767 if (options & F_REACHCONF)
768 ip6optlen += CMSG_SPACE(0);
771 /* set IP6 packet options */
773 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
774 errx(1, "can't allocate enough memory");
775 smsghdr.msg_control = (caddr_t)scmsg;
776 smsghdr.msg_controllen = ip6optlen;
777 scmsgp = (struct cmsghdr *)scmsg;
780 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
781 memset(pktinfo, 0, sizeof(*pktinfo));
782 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
783 scmsgp->cmsg_level = IPPROTO_IPV6;
784 scmsgp->cmsg_type = IPV6_PKTINFO;
785 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
788 /* set the outgoing interface */
790 #ifndef USE_SIN6_SCOPE_ID
791 /* pktinfo must have already been allocated */
792 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
793 errx(1, "%s: invalid interface name", ifname);
795 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
796 errx(1, "%s: invalid interface name", ifname);
799 /* set the source address */
800 if (options & F_SRCADDR)/* pktinfo must be valid */
801 pktinfo->ipi6_addr = srcaddr;
803 if (hoplimit != -1) {
804 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
805 scmsgp->cmsg_level = IPPROTO_IPV6;
806 scmsgp->cmsg_type = IPV6_HOPLIMIT;
807 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
809 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
811 #ifdef IPV6_REACHCONF
812 if (options & F_REACHCONF) {
813 scmsgp->cmsg_len = CMSG_LEN(0);
814 scmsgp->cmsg_level = IPPROTO_IPV6;
815 scmsgp->cmsg_type = IPV6_REACHCONF;
817 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
821 if (argc > 1) { /* some intermediate addrs are specified */
823 #ifdef USE_RFC2292BIS
827 #ifdef USE_RFC2292BIS
828 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
829 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
830 scmsgp->cmsg_level = IPPROTO_IPV6;
831 scmsgp->cmsg_type = IPV6_RTHDR;
832 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
833 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
834 IPV6_RTHDR_TYPE_0, argc - 1);
836 errx(1, "can't initialize rthdr");
837 #else /* old advanced API */
838 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
839 IPV6_RTHDR_TYPE_0)) == 0)
840 errx(1, "can't initialize rthdr");
841 #endif /* USE_RFC2292BIS */
843 for (hops = 0; hops < argc - 1; hops++) {
844 struct addrinfo *iaip;
846 if ((error = getaddrinfo(argv[hops], NULL, &hints,
848 errx(1, "%s", gai_strerror(error));
849 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
851 "bad addr family of an intermediate addr");
853 #ifdef USE_RFC2292BIS
854 if (inet6_rth_add(rthdr,
855 &(SIN6(iaip->ai_addr))->sin6_addr))
856 errx(1, "can't add an intermediate node");
857 #else /* old advanced API */
858 if (inet6_rthdr_add(scmsgp,
859 &(SIN6(iaip->ai_addr))->sin6_addr,
861 errx(1, "can't add an intermediate node");
862 #endif /* USE_RFC2292BIS */
866 #ifndef USE_RFC2292BIS
867 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
868 errx(1, "can't set the last flag");
871 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
878 int dummy, len = sizeof(src);
880 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
881 err(1, "UDP socket");
883 src.sin6_family = AF_INET6;
884 src.sin6_addr = dst.sin6_addr;
885 src.sin6_port = ntohs(DUMMY_PORT);
886 src.sin6_scope_id = dst.sin6_scope_id;
888 #ifdef USE_RFC2292BIS
890 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
891 (void *)pktinfo, sizeof(*pktinfo)))
892 err(1, "UDP setsockopt(IPV6_PKTINFO)");
894 if (hoplimit != -1 &&
895 setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,
896 (void *)&hoplimit, sizeof(hoplimit)))
897 err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
900 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
901 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
902 err(1, "UDP setsockopt(IPV6_RTHDR)");
903 #else /* old advanced API */
904 if (smsghdr.msg_control &&
905 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
906 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
907 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
910 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
911 err(1, "UDP connect");
913 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
914 err(1, "getsockname");
919 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
921 if (datalen > sockbufsize)
922 warnx("you need -b to increase socket buffer size");
923 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
924 sizeof(sockbufsize)) < 0)
925 err(1, "setsockopt(SO_SNDBUF)");
926 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
927 sizeof(sockbufsize)) < 0)
928 err(1, "setsockopt(SO_RCVBUF)");
931 if (datalen > 8 * 1024) /*XXX*/
932 warnx("you need -b to increase socket buffer size");
934 * When pinging the broadcast address, you can get a lot of
935 * answers. Doing something so evil is useful if you are trying
936 * to stress the ethernet, or just want to fill the arp cache
937 * to get some stuff for /etc/ethers.
940 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
946 #ifndef USE_SIN6_SCOPE_ID
947 #ifdef IPV6_RECVPKTINFO
948 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
950 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
951 #else /* old adv. API */
952 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
954 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
956 #endif /* USE_SIN6_SCOPE_ID */
957 #ifdef IPV6_RECVHOPLIMIT
958 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
960 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
961 #else /* old adv. API */
962 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
964 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
967 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
968 (unsigned long)(pingerlen() - 8));
969 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
970 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
972 while (preload--) /* Fire off them quickies. */
975 (void)signal(SIGINT, onsignal);
977 (void)signal(SIGINFO, onsignal);
980 if ((options & F_FLOOD) == 0) {
981 (void)signal(SIGALRM, onsignal);
982 itimer.it_interval = interval;
983 itimer.it_value = interval;
984 (void)setitimer(ITIMER_REAL, &itimer, NULL);
985 if (ntransmitted == 0)
989 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
990 if ((fdmaskp = malloc(fdmasks)) == NULL)
993 signo = seenalrm = seenint = 0;
1002 struct iovec iov[2];
1004 /* signal handling */
1023 if (options & F_FLOOD) {
1026 timeout.tv_usec = 10000;
1030 memset(fdmaskp, 0, fdmasks);
1032 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1034 if (errno != EINTR) {
1042 fromlen = sizeof(from);
1043 m.msg_name = (caddr_t)&from;
1044 m.msg_namelen = sizeof(from);
1045 memset(&iov, 0, sizeof(iov));
1046 iov[0].iov_base = (caddr_t)packet;
1047 iov[0].iov_len = packlen;
1050 cm = (struct cmsghdr *)buf;
1051 m.msg_control = (caddr_t)buf;
1052 m.msg_controllen = sizeof(buf);
1054 cc = recvmsg(s, &m, 0);
1056 if (errno != EINTR) {
1061 } else if (cc == 0) {
1065 * receive control messages only. Process the
1066 * exceptions (currently the only possiblity is
1067 * a path MTU notification.)
1069 if ((mtu = get_pathmtu(&m)) > 0) {
1070 if ((options & F_VERBOSE) != 0) {
1071 printf("new path MTU (%d) is "
1079 * an ICMPv6 message (probably an echoreply) arrived.
1081 pr_pack(packet, cc, &m);
1083 if (npackets && nreceived >= npackets)
1087 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 (void)signal(SIGALRM, onint);
1139 (void)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 (void)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 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1273 if (!(options & F_QUIET) && options & F_FLOOD)
1274 (void)write(STDOUT_FILENO, &DOT, 1);
1281 const struct icmp6_hdr *icp;
1283 if (ntohs(icp->icmp6_id) == ident)
1291 const struct icmp6_nodeinfo *nip;
1293 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1294 nonce + sizeof(u_int16_t),
1295 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1302 dnsdecode(sp, ep, base, buf, bufsiz)
1305 const u_char *base; /*base for compressed name*/
1311 char cresult[MAXDNAME + 1];
1322 if (i == 0 || cp != *sp) {
1323 if (strlcat(buf, ".", bufsiz) >= bufsiz)
1324 return NULL; /*result overrun*/
1330 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1331 /* DNS compression */
1335 comp = base + (i & 0x3f);
1336 if (dnsdecode(&comp, cp, base, cresult,
1337 sizeof(cresult)) == NULL)
1339 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1340 return NULL; /*result overrun*/
1342 } else if ((i & 0x3f) == i) {
1344 return NULL; /*source overrun*/
1345 while (i-- > 0 && cp < ep) {
1346 l = snprintf(cresult, sizeof(cresult),
1347 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1348 if (l >= sizeof(cresult))
1350 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1351 return NULL; /*result overrun*/
1355 return NULL; /*invalid label*/
1358 return NULL; /*not terminated*/
1366 * Print out the packet, if it came from us. This logic is necessary
1367 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1368 * which arrive ('tis only fair). This permits multiple copies of this
1369 * program to be run without having intermingled output (or statistics!).
1372 pr_pack(buf, cc, mhdr)
1375 struct msghdr *mhdr;
1377 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1378 struct icmp6_hdr *icp;
1379 struct icmp6_nodeinfo *ni;
1382 struct sockaddr *from;
1384 u_char *cp = NULL, *dp, *end = buf + cc;
1385 struct in6_pktinfo *pktinfo = NULL;
1386 struct timeval tv, *tp;
1387 double triptime = 0;
1392 char dnsname[MAXDNAME + 1];
1394 (void)gettimeofday(&tv, NULL);
1396 if (!mhdr || !mhdr->msg_name ||
1397 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1398 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1399 if (options & F_VERBOSE)
1400 warnx("invalid peername\n");
1403 from = (struct sockaddr *)mhdr->msg_name;
1404 fromlen = mhdr->msg_namelen;
1405 if (cc < sizeof(struct icmp6_hdr)) {
1406 if (options & F_VERBOSE)
1407 warnx("packet too short (%d bytes) from %s\n", cc,
1408 pr_addr(from, fromlen));
1411 icp = (struct icmp6_hdr *)buf;
1412 ni = (struct icmp6_nodeinfo *)buf;
1415 if ((hoplim = get_hoplim(mhdr)) == -1) {
1416 warnx("failed to get receiving hop limit");
1419 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1420 warnx("failed to get receiving pakcet information");
1424 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1425 seq = ntohs(icp->icmp6_seq);
1428 tp = (struct timeval *)(icp + 1);
1430 triptime = ((double)tv.tv_sec) * 1000.0 +
1431 ((double)tv.tv_usec) / 1000.0;
1433 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
1434 tsumsq += triptime * triptime;
1436 if (triptime < tmin)
1438 if (triptime > tmax)
1442 if (TST(seq % mx_dup_ck)) {
1447 SET(seq % mx_dup_ck);
1451 if (options & F_QUIET)
1454 if (options & F_FLOOD)
1455 (void)write(STDOUT_FILENO, &BSPACE, 1);
1457 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1458 pr_addr(from, fromlen), seq);
1459 (void)printf(" hlim=%d", hoplim);
1460 if ((options & F_VERBOSE) != 0) {
1461 struct sockaddr_in6 dstsa;
1463 memset(&dstsa, 0, sizeof(dstsa));
1464 dstsa.sin6_family = AF_INET6;
1466 dstsa.sin6_len = sizeof(dstsa);
1468 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1469 dstsa.sin6_addr = pktinfo->ipi6_addr;
1470 (void)printf(" dst=%s",
1471 pr_addr((struct sockaddr *)&dstsa,
1475 (void)printf(" time=%g ms", triptime);
1477 (void)printf("(DUP!)");
1478 /* check the data */
1479 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1480 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1481 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1483 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1488 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1489 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1491 if (TST(seq % mx_dup_ck)) {
1496 SET(seq % mx_dup_ck);
1500 if (options & F_QUIET)
1503 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1505 switch (ntohs(ni->ni_code)) {
1506 case ICMP6_NI_SUCCESS:
1508 case ICMP6_NI_REFUSED:
1509 printf("refused, type 0x%x", ntohs(ni->ni_type));
1511 case ICMP6_NI_UNKNOWN:
1512 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1515 printf("unknown code 0x%x, type 0x%x",
1516 ntohs(ni->ni_code), ntohs(ni->ni_type));
1520 switch (ntohs(ni->ni_qtype)) {
1522 printf("NodeInfo NOOP");
1524 case NI_QTYPE_SUPTYPES:
1525 pr_suptypes(ni, end - (u_char *)ni);
1527 case NI_QTYPE_NODEADDR:
1528 pr_nodeaddr(ni, end - (u_char *)ni);
1531 default: /* XXX: for backward compatibility */
1532 cp = (u_char *)ni + ICMP6_NIRLEN;
1533 if (buf[off + ICMP6_NIRLEN] ==
1534 cc - off - ICMP6_NIRLEN - 1)
1539 cp++; /* skip length */
1541 safeputc(*cp & 0xff);
1547 if (dnsdecode((const u_char **)&cp, end,
1548 (const u_char *)(ni + 1), dnsname,
1549 sizeof(dnsname)) == NULL) {
1554 * name-lookup special handling for
1557 if (cp + 1 <= end && !*cp &&
1558 strlen(dnsname) > 0) {
1559 dnsname[strlen(dnsname) - 1] = '\0';
1562 printf("%s%s", i > 0 ? "," : "",
1566 if (options & F_VERBOSE) {
1570 (void)printf(" ("); /*)*/
1572 switch (ni->ni_code) {
1573 case ICMP6_NI_REFUSED:
1574 (void)printf("refused");
1577 case ICMP6_NI_UNKNOWN:
1578 (void)printf("unknwon qtype");
1583 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1584 /* case of refusion, unknown */
1589 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1592 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1593 (void)printf("TTL=%d:meaningless",
1597 (void)printf("TTL=%d:invalid",
1600 (void)printf("TTL=%d", ttl);
1610 cp = (u_char *)ni + ICMP6_NIRLEN;
1619 if (buf[off + ICMP6_NIRLEN] !=
1620 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1623 (void)printf("invalid namelen:%d/%lu",
1624 buf[off + ICMP6_NIRLEN],
1625 (u_long)cc - off - ICMP6_NIRLEN - 1);
1635 /* We've got something other than an ECHOREPLY */
1636 if (!(options & F_VERBOSE))
1638 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1642 if (!(options & F_FLOOD)) {
1643 (void)putchar('\n');
1644 if (options & F_VERBOSE)
1646 (void)fflush(stdout);
1653 struct msghdr *mhdr;
1657 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1658 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1659 if (cm->cmsg_level != IPPROTO_IPV6)
1662 switch (cm->cmsg_type) {
1664 printf(" HbH Options: ");
1665 pr_ip6opt(CMSG_DATA(cm));
1668 #ifdef IPV6_RTHDRDSTOPTS
1669 case IPV6_RTHDRDSTOPTS:
1671 printf(" Dst Options: ");
1672 pr_ip6opt(CMSG_DATA(cm));
1675 printf(" Routing: ");
1676 pr_rthdr(CMSG_DATA(cm));
1682 #ifdef USE_RFC2292BIS
1684 pr_ip6opt(void *extbuf)
1686 struct ip6_hbh *ext;
1695 ext = (struct ip6_hbh *)extbuf;
1696 extlen = (ext->ip6h_len + 1) * 8;
1697 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1698 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1702 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1703 &type, &len, &databuf);
1704 if (currentlen == -1)
1708 * Note that inet6_opt_next automatically skips any padding
1713 offset = inet6_opt_get_val(databuf, offset,
1714 &value4, sizeof(value4));
1715 printf(" Jumbo Payload Opt: Length %u\n",
1716 (u_int32_t)ntohl(value4));
1718 case IP6OPT_ROUTER_ALERT:
1720 offset = inet6_opt_get_val(databuf, offset,
1721 &value2, sizeof(value2));
1722 printf(" Router Alert Opt: Type %u\n",
1726 printf(" Received Opt %u len %lu\n",
1727 type, (unsigned long)len);
1733 #else /* !USE_RFC2292BIS */
1736 pr_ip6opt(void *extbuf)
1741 #endif /* USE_RFC2292BIS */
1743 #ifdef USE_RFC2292BIS
1745 pr_rthdr(void *extbuf)
1747 struct in6_addr *in6;
1748 char ntopbuf[INET6_ADDRSTRLEN];
1749 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1752 /* print fixed part of the header */
1753 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1754 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1755 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1756 printf("%d segments, ", segments);
1758 printf("segments unknown, ");
1759 printf("%d left\n", rh->ip6r_segleft);
1761 for (i = 0; i < segments; i++) {
1762 in6 = inet6_rth_getaddr(extbuf, i);
1764 printf(" [%d]<NULL>\n", i);
1766 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1768 strncpy(ntopbuf, "?", sizeof(ntopbuf));
1769 printf(" [%d]%s\n", i, ntopbuf);
1777 #else /* !USE_RFC2292BIS */
1780 pr_rthdr(void *extbuf)
1785 #endif /* USE_RFC2292BIS */
1788 pr_bitrange(v, s, ii)
1798 /* shift till we have 0x01 */
1799 if ((v & 0x01) == 0) {
1801 printf("-%u", s + off - 1);
1812 case 0x04: case 0x0c:
1823 /* we have 0x01 with us */
1824 for (i = 0; i < 32 - off; i++) {
1825 if ((v & (0x01 << i)) == 0)
1829 printf(" %u", s + off);
1837 pr_suptypes(ni, nilen)
1838 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
1843 const u_char *cp, *end;
1846 u_int16_t words; /*32bit count*/
1849 #define MAXQTYPES (1 << 16)
1853 cp = (u_char *)(ni + 1);
1854 end = ((u_char *)ni) + nilen;
1858 printf("NodeInfo Supported Qtypes");
1859 if (options & F_VERBOSE) {
1860 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1861 printf(", compressed bitmap");
1863 printf(", raw bitmap");
1867 clen = (size_t)(end - cp);
1868 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1869 if (clen == 0 || clen > MAXQTYPES / 8 ||
1875 if (clen < sizeof(cbit) || clen % sizeof(v))
1877 memcpy(&cbit, cp, sizeof(cbit));
1878 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1882 clen = ntohs(cbit.words) * sizeof(v);
1883 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1888 for (off = 0; off < clen; off += sizeof(v)) {
1889 memcpy(&v, cp + off, sizeof(v));
1890 v = (u_int32_t)ntohl(v);
1891 b = pr_bitrange(v, (int)(cur + off * 8), b);
1893 /* flush the remaining bits */
1894 b = pr_bitrange(0, (int)(cur + off * 8), b);
1898 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1899 cur += ntohs(cbit.skip) * 32;
1904 pr_nodeaddr(ni, nilen)
1905 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1908 u_char *cp = (u_char *)(ni + 1);
1909 char ntop_buf[INET6_ADDRSTRLEN];
1912 nilen -= sizeof(struct icmp6_nodeinfo);
1914 if (options & F_VERBOSE) {
1915 switch (ni->ni_code) {
1916 case ICMP6_NI_REFUSED:
1917 (void)printf("refused");
1919 case ICMP6_NI_UNKNOWN:
1920 (void)printf("unknown qtype");
1923 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
1924 (void)printf(" truncated");
1928 printf(" no address\n");
1931 * In icmp-name-lookups 05 and later, TTL of each returned address
1932 * is contained in the resposne. We try to detect the version
1933 * by the length of the data, but note that the detection algorithm
1934 * is incomplete. We assume the latest draft by default.
1936 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
1942 /* XXX: alignment? */
1943 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
1944 cp += sizeof(u_int32_t);
1945 nilen -= sizeof(u_int32_t);
1948 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
1950 strncpy(ntop_buf, "?", sizeof(ntop_buf));
1951 printf(" %s", ntop_buf);
1953 if (ttl == 0xffffffff) {
1955 * XXX: can this convention be applied to all
1956 * type of TTL (i.e. non-ND TTL)?
1958 printf("(TTL=infty)");
1961 printf("(TTL=%u)", ttl);
1965 nilen -= sizeof(struct in6_addr);
1966 cp += sizeof(struct in6_addr);
1972 struct msghdr *mhdr;
1976 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1977 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1978 if (cm->cmsg_len == 0)
1981 if (cm->cmsg_level == IPPROTO_IPV6 &&
1982 cm->cmsg_type == IPV6_HOPLIMIT &&
1983 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1984 return(*(int *)CMSG_DATA(cm));
1990 struct in6_pktinfo *
1991 get_rcvpktinfo(mhdr)
1992 struct msghdr *mhdr;
1996 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1997 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1998 if (cm->cmsg_len == 0)
2001 if (cm->cmsg_level == IPPROTO_IPV6 &&
2002 cm->cmsg_type == IPV6_PKTINFO &&
2003 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2004 return((struct in6_pktinfo *)CMSG_DATA(cm));
2012 struct msghdr *mhdr;
2014 #ifdef IPV6_RECVPATHMTU
2016 struct ip6_mtuinfo *mtuctl = NULL;
2018 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2019 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2020 if (cm->cmsg_len == 0)
2023 if (cm->cmsg_level == IPPROTO_IPV6 &&
2024 cm->cmsg_type == IPV6_PATHMTU &&
2025 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2026 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2029 * If the notified destination is different from
2030 * the one we are pinging, just ignore the info.
2031 * We check the scope ID only when both notified value
2032 * and our own value have non-0 values, because we may
2033 * have used the default scope zone ID for sending,
2034 * in which case the scope ID value is 0.
2036 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2038 (mtuctl->ip6m_addr.sin6_scope_id &&
2039 dst.sin6_scope_id &&
2040 mtuctl->ip6m_addr.sin6_scope_id !=
2041 dst.sin6_scope_id)) {
2042 if ((options & F_VERBOSE) != 0) {
2043 printf("path MTU for %s is notified. "
2045 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2046 sizeof(mtuctl->ip6m_addr)));
2052 * Ignore an invalid MTU. XXX: can we just believe
2055 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2058 /* notification for our destination. return the MTU. */
2059 return((int)mtuctl->ip6m_mtu);
2071 static int firsttime = 1;
2075 int oldlen = smsghdr.msg_controllen;
2076 char *oldbuf = smsghdr.msg_control;
2078 /* XXX: We need to enlarge control message buffer */
2079 firsttime = 0; /* prevent further enlargement */
2081 smsghdr.msg_controllen = oldlen + CMSG_SPACE(sizeof(int));
2082 if ((smsghdr.msg_control =
2083 (char *)malloc(smsghdr.msg_controllen)) == NULL)
2084 err(1, "set_pathmtu: malloc");
2085 cm = (struct cmsghdr *)CMSG_FIRSTHDR(&smsghdr);
2086 cm->cmsg_len = CMSG_LEN(sizeof(int));
2087 cm->cmsg_level = IPPROTO_IPV6;
2088 cm->cmsg_type = IPV6_USE_MTU;
2090 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm);
2092 memcpy((void *)cm, (void *)oldbuf, oldlen);
2098 * look for a cmsgptr that points MTU structure.
2099 * XXX: this procedure seems redundant at this moment, but we'd better
2100 * keep the code generic enough for future extensions.
2102 for (cm = CMSG_FIRSTHDR(&smsghdr); cm;
2103 cm = (struct cmsghdr *)CMSG_NXTHDR(&smsghdr, cm)) {
2104 if (cm->cmsg_len == 0) /* XXX: paranoid check */
2105 errx(1, "set_pathmtu: internal error");
2107 if (cm->cmsg_level == IPPROTO_IPV6 &&
2108 cm->cmsg_type == IPV6_USE_MTU &&
2109 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2114 errx(1, "set_pathmtu: internal error: no space for path MTU");
2116 *(int *)CMSG_DATA(cm) = mtu;
2122 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2127 register struct timeval *out, *in;
2129 if ((out->tv_usec -= in->tv_usec) < 0) {
2131 out->tv_usec += 1000000;
2133 out->tv_sec -= in->tv_sec;
2145 (void)signal(SIGINT, SIG_IGN);
2146 (void)signal(SIGALRM, SIG_IGN);
2150 exit(nreceived == 0);
2155 * Print out statistics.
2161 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2162 (void)printf("%ld packets transmitted, ", ntransmitted);
2163 (void)printf("%ld packets received, ", nreceived);
2165 (void)printf("+%ld duplicates, ", nrepeats);
2167 if (nreceived > ntransmitted)
2168 (void)printf("-- somebody's printing up packets!");
2170 (void)printf("%d%% packet loss",
2171 (int) (((ntransmitted - nreceived) * 100) /
2174 (void)putchar('\n');
2175 if (nreceived && timing) {
2176 /* Only display average to microseconds */
2177 double num = nreceived + nrepeats;
2178 double avg = tsum / num;
2179 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
2180 double dev = sqrt(tsumsq / num - avg * avg);
2182 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2183 tmin, avg, tmax, dev);
2186 "round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
2189 (void)fflush(stdout);
2191 (void)fflush(stdout);
2195 static char *niqcode[] = {
2197 "DNS label", /*or empty*/
2202 static char *nircode[] = {
2203 "Success", "Refused", "Unknown",
2209 * Print a descriptive string about an ICMP header.
2213 struct icmp6_hdr *icp;
2216 char ntop_buf[INET6_ADDRSTRLEN];
2217 struct nd_redirect *red;
2218 struct icmp6_nodeinfo *ni;
2219 char dnsname[MAXDNAME + 1];
2223 switch (icp->icmp6_type) {
2224 case ICMP6_DST_UNREACH:
2225 switch (icp->icmp6_code) {
2226 case ICMP6_DST_UNREACH_NOROUTE:
2227 (void)printf("No Route to Destination\n");
2229 case ICMP6_DST_UNREACH_ADMIN:
2230 (void)printf("Destination Administratively "
2233 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2234 (void)printf("Destination Unreachable Beyond Scope\n");
2236 case ICMP6_DST_UNREACH_ADDR:
2237 (void)printf("Destination Host Unreachable\n");
2239 case ICMP6_DST_UNREACH_NOPORT:
2240 (void)printf("Destination Port Unreachable\n");
2243 (void)printf("Destination Unreachable, Bad Code: %d\n",
2247 /* Print returned IP header information */
2248 pr_retip((struct ip6_hdr *)(icp + 1), end);
2250 case ICMP6_PACKET_TOO_BIG:
2251 (void)printf("Packet too big mtu = %d\n",
2252 (int)ntohl(icp->icmp6_mtu));
2253 pr_retip((struct ip6_hdr *)(icp + 1), end);
2255 case ICMP6_TIME_EXCEEDED:
2256 switch (icp->icmp6_code) {
2257 case ICMP6_TIME_EXCEED_TRANSIT:
2258 (void)printf("Time to live exceeded\n");
2260 case ICMP6_TIME_EXCEED_REASSEMBLY:
2261 (void)printf("Frag reassembly time exceeded\n");
2264 (void)printf("Time exceeded, Bad Code: %d\n",
2268 pr_retip((struct ip6_hdr *)(icp + 1), end);
2270 case ICMP6_PARAM_PROB:
2271 (void)printf("Parameter problem: ");
2272 switch (icp->icmp6_code) {
2273 case ICMP6_PARAMPROB_HEADER:
2274 (void)printf("Erroneous Header ");
2276 case ICMP6_PARAMPROB_NEXTHEADER:
2277 (void)printf("Unknown Nextheader ");
2279 case ICMP6_PARAMPROB_OPTION:
2280 (void)printf("Unrecognized Option ");
2283 (void)printf("Bad code(%d) ", icp->icmp6_code);
2286 (void)printf("pointer = 0x%02x\n",
2287 (u_int32_t)ntohl(icp->icmp6_pptr));
2288 pr_retip((struct ip6_hdr *)(icp + 1), end);
2290 case ICMP6_ECHO_REQUEST:
2291 (void)printf("Echo Request");
2292 /* XXX ID + Seq + Data */
2294 case ICMP6_ECHO_REPLY:
2295 (void)printf("Echo Reply");
2296 /* XXX ID + Seq + Data */
2298 case ICMP6_MEMBERSHIP_QUERY:
2299 (void)printf("Listener Query");
2301 case ICMP6_MEMBERSHIP_REPORT:
2302 (void)printf("Listener Report");
2304 case ICMP6_MEMBERSHIP_REDUCTION:
2305 (void)printf("Listener Done");
2307 case ND_ROUTER_SOLICIT:
2308 (void)printf("Router Solicitation");
2310 case ND_ROUTER_ADVERT:
2311 (void)printf("Router Advertisement");
2313 case ND_NEIGHBOR_SOLICIT:
2314 (void)printf("Neighbor Solicitation");
2316 case ND_NEIGHBOR_ADVERT:
2317 (void)printf("Neighbor Advertisement");
2320 red = (struct nd_redirect *)icp;
2321 (void)printf("Redirect\n");
2322 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2324 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2325 (void)printf("Destination: %s", ntop_buf);
2326 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2328 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2329 (void)printf(" New Target: %s", ntop_buf);
2331 case ICMP6_NI_QUERY:
2332 (void)printf("Node Information Query");
2333 /* XXX ID + Seq + Data */
2334 ni = (struct icmp6_nodeinfo *)icp;
2335 l = end - (u_char *)(ni + 1);
2337 switch (ntohs(ni->ni_qtype)) {
2339 (void)printf("NOOP");
2341 case NI_QTYPE_SUPTYPES:
2342 (void)printf("Supported qtypes");
2345 (void)printf("DNS name");
2347 case NI_QTYPE_NODEADDR:
2348 (void)printf("nodeaddr");
2350 case NI_QTYPE_IPV4ADDR:
2351 (void)printf("IPv4 nodeaddr");
2354 (void)printf("unknown qtype");
2357 if (options & F_VERBOSE) {
2358 switch (ni->ni_code) {
2359 case ICMP6_NI_SUBJ_IPV6:
2360 if (l == sizeof(struct in6_addr) &&
2361 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2362 sizeof(ntop_buf)) != NULL) {
2363 (void)printf(", subject=%s(%s)",
2364 niqcode[ni->ni_code], ntop_buf);
2367 /* backward compat to -W */
2368 (void)printf(", oldfqdn");
2370 (void)printf(", invalid");
2374 case ICMP6_NI_SUBJ_FQDN:
2375 if (end == (u_char *)(ni + 1)) {
2376 (void)printf(", no subject");
2379 printf(", subject=%s", niqcode[ni->ni_code]);
2380 cp = (const u_char *)(ni + 1);
2381 if (dnsdecode(&cp, end, NULL, dnsname,
2382 sizeof(dnsname)) != NULL)
2383 printf("(%s)", dnsname);
2385 printf("(invalid)");
2387 case ICMP6_NI_SUBJ_IPV4:
2388 if (l == sizeof(struct in_addr) &&
2389 inet_ntop(AF_INET, ni + 1, ntop_buf,
2390 sizeof(ntop_buf)) != NULL) {
2391 (void)printf(", subject=%s(%s)",
2392 niqcode[ni->ni_code], ntop_buf);
2394 (void)printf(", invalid");
2397 (void)printf(", invalid");
2402 case ICMP6_NI_REPLY:
2403 (void)printf("Node Information Reply");
2404 /* XXX ID + Seq + Data */
2405 ni = (struct icmp6_nodeinfo *)icp;
2407 switch (ntohs(ni->ni_qtype)) {
2409 (void)printf("NOOP");
2411 case NI_QTYPE_SUPTYPES:
2412 (void)printf("Supported qtypes");
2415 (void)printf("DNS name");
2417 case NI_QTYPE_NODEADDR:
2418 (void)printf("nodeaddr");
2420 case NI_QTYPE_IPV4ADDR:
2421 (void)printf("IPv4 nodeaddr");
2424 (void)printf("unknown qtype");
2427 if (options & F_VERBOSE) {
2428 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2429 printf(", invalid");
2431 printf(", %s", nircode[ni->ni_code]);
2435 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2441 * Print an IP6 header.
2445 struct ip6_hdr *ip6;
2447 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2449 char ntop_buf[INET6_ADDRSTRLEN];
2451 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2452 tc = (tc >> 4) & 0x0f;
2453 tc |= (ip6->ip6_vfc << 4);
2455 printf("Vr TC Flow Plen Nxt Hlim\n");
2456 printf(" %1x %02x %05x %04x %02x %02x\n",
2457 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2458 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2459 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2460 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2461 printf("%s->", ntop_buf);
2462 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2463 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2464 printf("%s\n", ntop_buf);
2469 * Return an ascii host address as a dotted quad and optionally with
2473 pr_addr(addr, addrlen)
2474 struct sockaddr *addr;
2477 static char buf[NI_MAXHOST];
2480 #ifdef NI_WITHSCOPEID
2481 flag = NI_WITHSCOPEID;
2485 if ((options & F_HOSTNAME) == 0)
2486 flag |= NI_NUMERICHOST;
2488 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2496 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2500 struct ip6_hdr *ip6;
2503 u_char *cp = (u_char *)ip6, nh;
2506 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2511 hlen = sizeof(*ip6);
2515 while (end - cp >= 8) {
2517 case IPPROTO_HOPOPTS:
2519 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2520 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2522 case IPPROTO_DSTOPTS:
2524 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2525 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2527 case IPPROTO_FRAGMENT:
2529 hlen = sizeof(struct ip6_frag);
2530 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2532 case IPPROTO_ROUTING:
2534 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2535 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2540 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2541 nh = ((struct ah *)cp)->ah_nxt;
2544 case IPPROTO_ICMPV6:
2545 printf("ICMP6: type = %d, code = %d\n",
2552 printf("TCP: from port %u, to port %u (decimal)\n",
2553 (*cp * 256 + *(cp + 1)),
2554 (*(cp + 2) * 256 + *(cp + 3)));
2557 printf("UDP: from port %u, to port %u (decimal)\n",
2558 (*cp * 256 + *(cp + 1)),
2559 (*(cp + 2) * 256 + *(cp + 3)));
2562 printf("Unknown Header(%d)\n", nh);
2566 if ((cp += hlen) >= end)
2584 register int ii, jj, kk;
2588 for (cp = patp; *cp; cp++)
2590 errx(1, "patterns must be specified as hex digits");
2592 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2593 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2594 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2595 &pat[13], &pat[14], &pat[15]);
2600 kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
2602 for (jj = 0; jj < ii; ++jj)
2603 bp[jj + kk] = pat[jj];
2604 if (!(options & F_QUIET)) {
2605 (void)printf("PATTERN: 0x");
2606 for (jj = 0; jj < ii; ++jj)
2607 (void)printf("%02x", bp[jj] & 0xFF);
2613 #ifdef IPSEC_POLICY_IPSEC
2615 setpolicy(so, policy)
2622 return 0; /* ignore */
2624 buf = ipsec_set_policy(policy, strlen(policy));
2626 errx(1, "%s", ipsec_strerror());
2627 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2628 ipsec_get_policylen(buf)) < 0)
2629 warnx("Unable to set IPSec policy");
2644 u_int8_t digest[16];
2647 char hbuf[NI_MAXHOST];
2648 struct in6_addr in6;
2650 p = strchr(name, '.');
2652 p = name + strlen(name);
2654 if (l > 63 || l > sizeof(hbuf) - 1)
2655 return NULL; /*label too long*/
2656 strncpy(hbuf, name, l);
2657 hbuf[(int)l] = '\0';
2659 for (q = name; *q; q++) {
2664 /* generate 8 bytes of pseudo-random value. */
2665 bzero(&ctxt, sizeof(ctxt));
2668 MD5Update(&ctxt, &c, sizeof(c));
2669 MD5Update(&ctxt, name, l);
2670 MD5Final(digest, &ctxt);
2672 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2673 return NULL; /*XXX*/
2674 bcopy(digest, &in6.s6_addr[12], 4);
2676 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2679 return strdup(hbuf);
2685 (void)fprintf(stderr,
2686 "usage: ping6 [-dfH"
2687 #ifdef IPV6_USE_MIN_MTU
2691 #ifdef IPV6_REACHCONF
2695 #ifdef IPSEC_POLICY_IPSEC
2701 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
2702 "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
2704 "\t[-s packetsize] [-h hoplimit] [hops...] host\n");