1 /* $KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 static const char copyright[] =
72 "@(#) Copyright (c) 1989, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
78 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
80 static const char rcsid[] =
85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
86 * measure round-trip-delays and packet loss across network paths.
90 * U. S. Army Ballistic Research Laboratory
94 * Public Domain. Distribution Unlimited.
96 * More statistics could always be gathered.
97 * This program has to run SUID to ROOT to access the ICMP socket.
101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link*
103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of
104 * network attached to 1 or more interfaces)
107 #include <sys/param.h>
109 #include <sys/socket.h>
110 #include <sys/time.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/ip6.h>
117 #include <netinet/icmp6.h>
118 #include <arpa/inet.h>
119 #include <arpa/nameser.h>
137 #include <netinet6/ah.h>
138 #include <netinet6/ipsec.h>
148 #define MAXPACKETLEN 131072
150 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */
151 #define ICMP6ECHOTMLEN sizeof(struct tv32)
152 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8)
153 /* FQDN case, 64 bits of nonce + 32 bits ttl */
154 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12)
155 #define EXTRA 256 /* for AH and various other headers. weird. */
156 #define DEFDATALEN ICMP6ECHOTMLEN
157 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
158 #define NROUTES 9 /* number of record route slots */
160 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
161 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
162 #define SET(bit) (A(bit) |= B(bit))
163 #define CLR(bit) (A(bit) &= (~B(bit)))
164 #define TST(bit) (A(bit) & B(bit))
166 #define F_FLOOD 0x0001
167 #define F_INTERVAL 0x0002
168 #define F_PINGFILLED 0x0008
169 #define F_QUIET 0x0010
170 #define F_RROUTE 0x0020
171 #define F_SO_DEBUG 0x0040
172 #define F_VERBOSE 0x0100
174 #ifdef IPSEC_POLICY_IPSEC
175 #define F_POLICY 0x0400
177 #define F_AUTHHDR 0x0200
178 #define F_ENCRYPT 0x0400
179 #endif /*IPSEC_POLICY_IPSEC*/
181 #define F_NODEADDR 0x0800
182 #define F_FQDN 0x1000
183 #define F_INTERFACE 0x2000
184 #define F_SRCADDR 0x4000
185 #define F_HOSTNAME 0x10000
186 #define F_FQDNOLD 0x20000
187 #define F_NIGROUP 0x40000
188 #define F_SUPTYPES 0x80000
189 #define F_NOMINMTU 0x100000
190 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
193 #define IN6LEN sizeof(struct in6_addr)
194 #define SA6LEN sizeof(struct sockaddr_in6)
195 #define DUMMY_PORT 10101
197 #define SIN6(s) ((struct sockaddr_in6 *)(s))
200 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
201 * number of received sequence numbers we can keep track of. Change 128
202 * to 8192 for complete accuracy...
204 #define MAX_DUP_CHK (8 * 8192)
205 int mx_dup_ck = MAX_DUP_CHK;
206 char rcvd_tbl[MAX_DUP_CHK / 8];
208 struct addrinfo *res;
209 struct sockaddr_in6 dst; /* who to ping6 */
210 struct sockaddr_in6 src; /* src addr of this packet */
212 int datalen = DEFDATALEN;
213 int s; /* socket file descriptor */
214 u_char outpack[MAXPACKETLEN];
215 char BSPACE = '\b'; /* characters written for flood */
218 int ident; /* process id to identify our packets */
219 u_int8_t nonce[8]; /* nonce field for node information */
220 int hoplimit = -1; /* hoplimit */
221 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */
224 long npackets; /* max packets to transmit */
225 long nreceived; /* # of packets we got back */
226 long nrepeats; /* number of duplicates */
227 long ntransmitted; /* sequence # for outbound packets = #sent */
228 struct timeval interval = {1, 0}; /* interval between packets */
231 int timing; /* flag to do timing */
232 double tmin = 999999999.0; /* minimum round trip time */
233 double tmax = 0.0; /* maximum round trip time */
234 double tsum = 0.0; /* sum of all times, for doing average */
235 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
237 /* for node addresses */
240 /* for ancillary data(advanced API) */
241 struct msghdr smsghdr;
242 struct iovec smsgiov;
245 volatile sig_atomic_t seenalrm;
246 volatile sig_atomic_t seenint;
248 volatile sig_atomic_t seeninfo;
251 int main(int, char *[]);
252 void fill(char *, char *);
253 int get_hoplim(struct msghdr *);
254 int get_pathmtu(struct msghdr *);
255 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
257 void retransmit(void);
259 size_t pingerlen(void);
261 const char *pr_addr(struct sockaddr *, int);
262 void pr_icmph(struct icmp6_hdr *, u_char *);
263 void pr_iph(struct ip6_hdr *);
264 void pr_suptypes(struct icmp6_nodeinfo *, size_t);
265 void pr_nodeaddr(struct icmp6_nodeinfo *, int);
266 int myechoreply(const struct icmp6_hdr *);
267 int mynireply(const struct icmp6_nodeinfo *);
268 char *dnsdecode(const u_char **, const u_char *, const u_char *,
270 void pr_pack(u_char *, int, struct msghdr *);
271 void pr_exthdrs(struct msghdr *);
272 void pr_ip6opt(void *);
273 void pr_rthdr(void *);
274 int pr_bitrange(u_int32_t, int, int);
275 void pr_retip(struct ip6_hdr *, u_char *);
277 void tvsub(struct timeval *, struct timeval *);
278 int setpolicy(int, char *);
279 char *nigroup(char *);
287 struct itimerval itimer;
288 struct sockaddr_in6 from;
289 #ifndef HAVE_ARC4RANDOM
295 struct timeval timeout, *tv;
297 struct addrinfo hints;
299 struct pollfd fdmaskp[1];
305 int ch, hold, packlen, preload, optval, ret_ga;
306 u_char *datap, *packet;
307 char *e, *target, *ifname = NULL, *gateway = NULL;
309 struct cmsghdr *scmsgp = NULL;
310 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
315 struct in6_pktinfo *pktinfo = NULL;
316 #ifdef USE_RFC2292BIS
317 struct ip6_rthdr *rthdr = NULL;
319 #ifdef IPSEC_POLICY_IPSEC
320 char *policy_in = NULL;
321 char *policy_out = NULL;
325 #ifdef IPV6_USE_MIN_MTU
329 /* just to be sure */
330 memset(&smsghdr, 0, sizeof(smsghdr));
331 memset(&smsgiov, 0, sizeof(smsgiov));
334 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
338 #ifdef IPSEC_POLICY_IPSEC
342 #endif /*IPSEC_POLICY_IPSEC*/
344 while ((ch = getopt(argc, argv,
345 "a:b:c:dfHg:h:I:i:l:mnNp:qS:s:tvwW" ADDOPTS)) != -1) {
352 options &= ~F_NOUSERDATA;
353 options |= F_NODEADDR;
354 for (cp = optarg; *cp != '\0'; cp++) {
357 naflags |= NI_NODEADDR_FLAG_ALL;
361 naflags |= NI_NODEADDR_FLAG_COMPAT;
365 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
369 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
373 naflags |= NI_NODEADDR_FLAG_GLOBAL;
375 case 'A': /* experimental. not in the spec */
376 #ifdef NI_NODEADDR_FLAG_ANYCAST
377 naflags |= NI_NODEADDR_FLAG_ANYCAST;
381 "-a A is not supported on the platform");
392 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
395 lsockbufsize = strtoul(optarg, &e, 10);
396 sockbufsize = lsockbufsize;
397 if (errno || !*optarg || *e ||
398 sockbufsize != lsockbufsize)
399 errx(1, "invalid socket buffer size");
402 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
406 npackets = strtol(optarg, &e, 10);
407 if (npackets <= 0 || *optarg == '\0' || *e != '\0')
409 "illegal number of packets -- %s", optarg);
412 options |= F_SO_DEBUG;
417 errx(1, "Must be superuser to flood ping");
420 setbuf(stdout, (char *)NULL);
426 options |= F_HOSTNAME;
428 case 'h': /* hoplimit */
429 hoplimit = strtol(optarg, &e, 10);
430 if (*optarg == '\0' || *e != '\0')
431 errx(1, "illegal hoplimit %s", optarg);
432 if (255 < hoplimit || hoplimit < -1)
434 "illegal hoplimit -- %s", optarg);
438 options |= F_INTERFACE;
439 #ifndef USE_SIN6_SCOPE_ID
443 case 'i': /* wait between sending packets */
444 intval = strtod(optarg, &e);
445 if (*optarg == '\0' || *e != '\0')
446 errx(1, "illegal timing interval %s", optarg);
447 if (intval < 1 && getuid()) {
448 errx(1, "%s: only root may use interval < 1s",
451 interval.tv_sec = (long)intval;
453 (long)((intval - interval.tv_sec) * 1000000);
454 if (interval.tv_sec < 0)
455 errx(1, "illegal timing interval %s", optarg);
456 /* less than 1/hz does not make sense */
457 if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
458 warnx("too small interval, raised to 0.01");
459 interval.tv_usec = 10000;
461 options |= F_INTERVAL;
466 errx(1, "Must be superuser to preload");
468 preload = strtol(optarg, &e, 10);
469 if (preload < 0 || *optarg == '\0' || *e != '\0')
470 errx(1, "illegal preload value -- %s", optarg);
473 #ifdef IPV6_USE_MIN_MTU
477 errx(1, "-%c is not supported on this platform", ch);
481 options &= ~F_HOSTNAME;
484 options |= F_NIGROUP;
486 case 'p': /* fill buffer with user pattern */
487 options |= F_PINGFILLED;
488 fill((char *)datap, optarg);
494 memset(&hints, 0, sizeof(struct addrinfo));
495 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
496 hints.ai_family = AF_INET6;
497 hints.ai_socktype = SOCK_RAW;
498 hints.ai_protocol = IPPROTO_ICMPV6;
500 ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
502 errx(1, "invalid source address: %s",
503 gai_strerror(ret_ga));
506 * res->ai_family must be AF_INET6 and res->ai_addrlen
507 * must be sizeof(src).
509 memcpy(&src, res->ai_addr, res->ai_addrlen);
510 srclen = res->ai_addrlen;
512 options |= F_SRCADDR;
514 case 's': /* size of packet to send */
515 datalen = strtol(optarg, &e, 10);
516 if (datalen <= 0 || *optarg == '\0' || *e != '\0')
517 errx(1, "illegal datalen value -- %s", optarg);
518 if (datalen > MAXDATALEN) {
520 "datalen value too large, maximum is %d",
525 options &= ~F_NOUSERDATA;
526 options |= F_SUPTYPES;
529 options |= F_VERBOSE;
532 options &= ~F_NOUSERDATA;
536 options &= ~F_NOUSERDATA;
537 options |= F_FQDNOLD;
540 #ifdef IPSEC_POLICY_IPSEC
543 if (!strncmp("in", optarg, 2)) {
544 if ((policy_in = strdup(optarg)) == NULL)
546 } else if (!strncmp("out", optarg, 3)) {
547 if ((policy_out = strdup(optarg)) == NULL)
550 errx(1, "invalid security policy");
554 options |= F_AUTHHDR;
557 options |= F_ENCRYPT;
559 #endif /*IPSEC_POLICY_IPSEC*/
576 #ifdef IPV6_RECVRTHDR /* 2292bis */
577 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
580 rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
583 errx(1, "too many intermediate hops");
589 if (options & F_NIGROUP) {
590 target = nigroup(argv[argc - 1]);
591 if (target == NULL) {
596 target = argv[argc - 1];
599 memset(&hints, 0, sizeof(struct addrinfo));
600 hints.ai_flags = AI_CANONNAME;
601 hints.ai_family = AF_INET6;
602 hints.ai_socktype = SOCK_RAW;
603 hints.ai_protocol = IPPROTO_ICMPV6;
605 ret_ga = getaddrinfo(target, NULL, &hints, &res);
607 errx(1, "%s", gai_strerror(ret_ga));
608 if (res->ai_canonname)
609 hostname = res->ai_canonname;
614 errx(1, "getaddrinfo failed");
616 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
618 if ((s = socket(res->ai_family, res->ai_socktype,
619 res->ai_protocol)) < 0)
622 /* set the source address if specified. */
623 if ((options & F_SRCADDR) &&
624 bind(s, (struct sockaddr *)&src, srclen) != 0) {
628 /* set the gateway (next hop) if specified */
630 struct addrinfo ghints, *gres;
633 memset(&ghints, 0, sizeof(ghints));
634 ghints.ai_family = AF_INET6;
635 ghints.ai_socktype = SOCK_RAW;
636 ghints.ai_protocol = IPPROTO_ICMPV6;
638 error = getaddrinfo(gateway, NULL, &hints, &gres);
640 errx(1, "getaddrinfo for the gateway %s: %s",
641 gateway, gai_strerror(error));
643 if (gres->ai_next && (options & F_VERBOSE))
644 warnx("gateway resolves to multiple addresses");
646 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
647 gres->ai_addr, gres->ai_addrlen)) {
648 err(1, "setsockopt(IPV6_NEXTHOP)");
655 * let the kerel pass extension headers of incoming packets,
656 * for privileged socket options
658 if ((options & F_VERBOSE) != 0) {
661 #ifdef IPV6_RECVHOPOPTS
662 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
664 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
665 #else /* old adv. API */
666 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
668 err(1, "setsockopt(IPV6_HOPOPTS)");
670 #ifdef IPV6_RECVDSTOPTS
671 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
673 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
674 #else /* old adv. API */
675 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
677 err(1, "setsockopt(IPV6_DSTOPTS)");
679 #ifdef IPV6_RECVRTHDRDSTOPTS
680 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
682 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
686 /* revoke root privilege */
690 if ((options & F_FLOOD) && (options & F_INTERVAL))
691 errx(1, "-f and -i incompatible options");
693 if ((options & F_NOUSERDATA) == 0) {
694 if (datalen >= sizeof(struct tv32)) {
695 /* we can time transfer */
699 /* in F_VERBOSE case, we may get non-echoreply packets*/
700 if (options & F_VERBOSE)
701 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
703 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
705 /* suppress timing for node information query */
708 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
711 if (!(packet = (u_char *)malloc((u_int)packlen)))
712 err(1, "Unable to allocate packet");
713 if (!(options & F_PINGFILLED))
714 for (i = ICMP6ECHOLEN; i < packlen; ++i)
717 ident = getpid() & 0xFFFF;
718 #ifndef HAVE_ARC4RANDOM
719 gettimeofday(&seed, NULL);
720 srand((unsigned int)(seed.tv_sec ^ seed.tv_usec ^ (long)ident));
721 memset(nonce, 0, sizeof(nonce));
722 for (i = 0; i < sizeof(nonce); i += sizeof(int))
723 *((int *)&nonce[i]) = rand();
725 memset(nonce, 0, sizeof(nonce));
726 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))
727 *((u_int32_t *)&nonce[i]) = arc4random();
732 if (options & F_SO_DEBUG)
733 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
735 optval = IPV6_DEFHLIM;
736 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
737 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
738 &optval, sizeof(optval)) == -1)
739 err(1, "IPV6_MULTICAST_HOPS");
740 #ifdef IPV6_USE_MIN_MTU
742 optval = mflag > 1 ? 0 : 1;
744 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
745 &optval, sizeof(optval)) == -1)
746 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
748 #ifdef IPV6_RECVPATHMTU
751 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
752 &optval, sizeof(optval)) == -1)
753 err(1, "setsockopt(IPV6_RECVPATHMTU)");
755 #endif /* IPV6_RECVPATHMTU */
756 #endif /* IPV6_USE_MIN_MTU */
759 #ifdef IPSEC_POLICY_IPSEC
760 if (options & F_POLICY) {
761 if (setpolicy(s, policy_in) < 0)
762 errx(1, "%s", ipsec_strerror());
763 if (setpolicy(s, policy_out) < 0)
764 errx(1, "%s", ipsec_strerror());
767 if (options & F_AUTHHDR) {
768 optval = IPSEC_LEVEL_REQUIRE;
769 #ifdef IPV6_AUTH_TRANS_LEVEL
770 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
771 &optval, sizeof(optval)) == -1)
772 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
774 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
775 &optval, sizeof(optval)) == -1)
776 err(1, "setsockopt(IPV6_AUTH_LEVEL)");
779 if (options & F_ENCRYPT) {
780 optval = IPSEC_LEVEL_REQUIRE;
781 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
782 &optval, sizeof(optval)) == -1)
783 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
785 #endif /*IPSEC_POLICY_IPSEC*/
790 struct icmp6_filter filt;
791 if (!(options & F_VERBOSE)) {
792 ICMP6_FILTER_SETBLOCKALL(&filt);
793 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
794 (options & F_NODEADDR) || (options & F_SUPTYPES))
795 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
797 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
799 ICMP6_FILTER_SETPASSALL(&filt);
801 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
803 err(1, "setsockopt(ICMP6_FILTER)");
805 #endif /*ICMP6_FILTER*/
807 /* let the kerel pass extension headers of incoming packets */
808 if ((options & F_VERBOSE) != 0) {
811 #ifdef IPV6_RECVRTHDR
812 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
814 err(1, "setsockopt(IPV6_RECVRTHDR)");
815 #else /* old adv. API */
816 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
818 err(1, "setsockopt(IPV6_RTHDR)");
824 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
825 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
826 &optval, sizeof(optval)) == -1)
827 err(1, "IPV6_MULTICAST_LOOP");
830 /* Specify the outgoing interface and/or the source address */
832 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
835 ip6optlen += CMSG_SPACE(sizeof(int));
837 /* set IP6 packet options */
839 if ((scmsg = (char *)malloc(ip6optlen)) == 0)
840 errx(1, "can't allocate enough memory");
841 smsghdr.msg_control = (caddr_t)scmsg;
842 smsghdr.msg_controllen = ip6optlen;
843 scmsgp = (struct cmsghdr *)scmsg;
846 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
847 memset(pktinfo, 0, sizeof(*pktinfo));
848 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
849 scmsgp->cmsg_level = IPPROTO_IPV6;
850 scmsgp->cmsg_type = IPV6_PKTINFO;
851 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
854 /* set the outgoing interface */
856 #ifndef USE_SIN6_SCOPE_ID
857 /* pktinfo must have already been allocated */
858 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
859 errx(1, "%s: invalid interface name", ifname);
861 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
862 errx(1, "%s: invalid interface name", ifname);
865 if (hoplimit != -1) {
866 scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
867 scmsgp->cmsg_level = IPPROTO_IPV6;
868 scmsgp->cmsg_type = IPV6_HOPLIMIT;
869 *(int *)(CMSG_DATA(scmsgp)) = hoplimit;
871 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
874 if (argc > 1) { /* some intermediate addrs are specified */
876 #ifdef USE_RFC2292BIS
880 #ifdef USE_RFC2292BIS
881 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
882 scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
883 scmsgp->cmsg_level = IPPROTO_IPV6;
884 scmsgp->cmsg_type = IPV6_RTHDR;
885 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
886 rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
887 IPV6_RTHDR_TYPE_0, argc - 1);
889 errx(1, "can't initialize rthdr");
890 #else /* old advanced API */
891 if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
892 IPV6_RTHDR_TYPE_0)) == 0)
893 errx(1, "can't initialize rthdr");
894 #endif /* USE_RFC2292BIS */
896 for (hops = 0; hops < argc - 1; hops++) {
897 struct addrinfo *iaip;
899 if ((error = getaddrinfo(argv[hops], NULL, &hints,
901 errx(1, "%s", gai_strerror(error));
902 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
904 "bad addr family of an intermediate addr");
906 #ifdef USE_RFC2292BIS
907 if (inet6_rth_add(rthdr,
908 &(SIN6(iaip->ai_addr))->sin6_addr))
909 errx(1, "can't add an intermediate node");
910 #else /* old advanced API */
911 if (inet6_rthdr_add(scmsgp,
912 &(SIN6(iaip->ai_addr))->sin6_addr,
914 errx(1, "can't add an intermediate node");
915 #endif /* USE_RFC2292BIS */
919 #ifndef USE_RFC2292BIS
920 if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
921 errx(1, "can't set the last flag");
924 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
927 if (!(options & F_SRCADDR)) {
929 * get the source address. XXX since we revoked the root
930 * privilege, we cannot use a raw socket for this.
933 socklen_t len = sizeof(src);
935 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
936 err(1, "UDP socket");
938 src.sin6_family = AF_INET6;
939 src.sin6_addr = dst.sin6_addr;
940 src.sin6_port = ntohs(DUMMY_PORT);
941 src.sin6_scope_id = dst.sin6_scope_id;
943 #ifdef USE_RFC2292BIS
945 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
946 (void *)pktinfo, sizeof(*pktinfo)))
947 err(1, "UDP setsockopt(IPV6_PKTINFO)");
949 if (hoplimit != -1 &&
950 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
951 (void *)&hoplimit, sizeof(hoplimit)))
952 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
954 if (hoplimit != -1 &&
955 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
956 (void *)&hoplimit, sizeof(hoplimit)))
957 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
960 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
961 (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
962 err(1, "UDP setsockopt(IPV6_RTHDR)");
963 #else /* old advanced API */
964 if (smsghdr.msg_control &&
965 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
966 (void *)smsghdr.msg_control, smsghdr.msg_controllen))
967 err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
970 if (connect(dummy, (struct sockaddr *)&src, len) < 0)
971 err(1, "UDP connect");
973 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
974 err(1, "getsockname");
979 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
981 if (datalen > sockbufsize)
982 warnx("you need -b to increase socket buffer size");
983 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
984 sizeof(sockbufsize)) < 0)
985 err(1, "setsockopt(SO_SNDBUF)");
986 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
987 sizeof(sockbufsize)) < 0)
988 err(1, "setsockopt(SO_RCVBUF)");
991 if (datalen > 8 * 1024) /*XXX*/
992 warnx("you need -b to increase socket buffer size");
994 * When pinging the broadcast address, you can get a lot of
995 * answers. Doing something so evil is useful if you are trying
996 * to stress the ethernet, or just want to fill the arp cache
997 * to get some stuff for /etc/ethers.
1000 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
1006 #ifndef USE_SIN6_SCOPE_ID
1007 #ifdef IPV6_RECVPKTINFO
1008 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
1009 sizeof(optval)) < 0)
1010 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
1011 #else /* old adv. API */
1012 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
1013 sizeof(optval)) < 0)
1014 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
1016 #endif /* USE_SIN6_SCOPE_ID */
1017 #ifdef IPV6_RECVHOPLIMIT
1018 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
1019 sizeof(optval)) < 0)
1020 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
1021 #else /* old adv. API */
1022 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
1023 sizeof(optval)) < 0)
1024 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
1027 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
1028 (unsigned long)(pingerlen() - 8));
1029 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
1030 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
1032 while (preload--) /* Fire off them quickies. */
1035 (void)signal(SIGINT, onsignal);
1037 (void)signal(SIGINFO, onsignal);
1040 if ((options & F_FLOOD) == 0) {
1041 (void)signal(SIGALRM, onsignal);
1042 itimer.it_interval = interval;
1043 itimer.it_value = interval;
1044 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1045 if (ntransmitted == 0)
1050 fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);
1051 if ((fdmaskp = malloc(fdmasks)) == NULL)
1055 seenalrm = seenint = 0;
1064 struct iovec iov[2];
1066 /* signal handling */
1085 if (options & F_FLOOD) {
1091 timeout.tv_usec = 10000;
1103 fdmaskp[0].events = POLLIN;
1104 cc = poll(fdmaskp, 1, timeout);
1106 memset(fdmaskp, 0, fdmasks);
1108 cc = select(s + 1, fdmaskp, NULL, NULL, tv);
1111 if (errno != EINTR) {
1123 m.msg_name = (caddr_t)&from;
1124 m.msg_namelen = sizeof(from);
1125 memset(&iov, 0, sizeof(iov));
1126 iov[0].iov_base = (caddr_t)packet;
1127 iov[0].iov_len = packlen;
1130 cm = (struct cmsghdr *)buf;
1131 m.msg_control = (caddr_t)buf;
1132 m.msg_controllen = sizeof(buf);
1134 cc = recvmsg(s, &m, 0);
1136 if (errno != EINTR) {
1141 } else if (cc == 0) {
1145 * receive control messages only. Process the
1146 * exceptions (currently the only possiblity is
1147 * a path MTU notification.)
1149 if ((mtu = get_pathmtu(&m)) > 0) {
1150 if ((options & F_VERBOSE) != 0) {
1151 printf("new path MTU (%d) is "
1158 * an ICMPv6 message (probably an echoreply) arrived.
1160 pr_pack(packet, cc, &m);
1162 if (npackets && nreceived >= npackets)
1166 exit(nreceived == 0);
1191 * This routine transmits another ping6.
1196 struct itimerval itimer;
1202 * If we're not transmitting any more packets, change the timer
1203 * to wait two round-trip times if we've received any packets or
1204 * ten seconds if we haven't.
1208 itimer.it_value.tv_sec = 2 * tmax / 1000;
1209 if (itimer.it_value.tv_sec == 0)
1210 itimer.it_value.tv_sec = 1;
1212 itimer.it_value.tv_sec = MAXWAIT;
1213 itimer.it_interval.tv_sec = 0;
1214 itimer.it_interval.tv_usec = 0;
1215 itimer.it_value.tv_usec = 0;
1217 (void)signal(SIGALRM, onint);
1218 (void)setitimer(ITIMER_REAL, &itimer, NULL);
1223 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1224 * will be added on by the kernel. The ID field is our UNIX process ID,
1225 * and the sequence number is an ascending integer. The first 8 bytes
1226 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1227 * byte-order, to compute the round-trip time.
1234 if (options & F_FQDN)
1235 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1236 else if (options & F_FQDNOLD)
1238 else if (options & F_NODEADDR)
1239 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1240 else if (options & F_SUPTYPES)
1243 l = ICMP6ECHOLEN + datalen;
1251 struct icmp6_hdr *icp;
1252 struct iovec iov[2];
1254 struct icmp6_nodeinfo *nip;
1257 if (npackets && ntransmitted >= npackets)
1258 return(-1); /* no more transmission */
1260 icp = (struct icmp6_hdr *)outpack;
1261 nip = (struct icmp6_nodeinfo *)outpack;
1262 memset(icp, 0, sizeof(*icp));
1263 icp->icmp6_cksum = 0;
1264 seq = ntransmitted++;
1265 CLR(seq % mx_dup_ck);
1267 if (options & F_FQDN) {
1268 icp->icmp6_type = ICMP6_NI_QUERY;
1269 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1270 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1271 nip->ni_flags = htons(0);
1273 memcpy(nip->icmp6_ni_nonce, nonce,
1274 sizeof(nip->icmp6_ni_nonce));
1275 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1277 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1278 sizeof(dst.sin6_addr));
1279 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1281 } else if (options & F_FQDNOLD) {
1282 /* packet format in 03 draft - no Subject data on queries */
1283 icp->icmp6_type = ICMP6_NI_QUERY;
1284 icp->icmp6_code = 0; /* code field is always 0 */
1285 nip->ni_qtype = htons(NI_QTYPE_FQDN);
1286 nip->ni_flags = htons(0);
1288 memcpy(nip->icmp6_ni_nonce, nonce,
1289 sizeof(nip->icmp6_ni_nonce));
1290 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1294 } else if (options & F_NODEADDR) {
1295 icp->icmp6_type = ICMP6_NI_QUERY;
1296 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
1297 nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
1298 nip->ni_flags = naflags;
1300 memcpy(nip->icmp6_ni_nonce, nonce,
1301 sizeof(nip->icmp6_ni_nonce));
1302 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1304 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
1305 sizeof(dst.sin6_addr));
1306 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
1308 } else if (options & F_SUPTYPES) {
1309 icp->icmp6_type = ICMP6_NI_QUERY;
1310 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/
1311 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
1312 /* we support compressed bitmap */
1313 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
1315 memcpy(nip->icmp6_ni_nonce, nonce,
1316 sizeof(nip->icmp6_ni_nonce));
1317 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);
1321 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1322 icp->icmp6_code = 0;
1323 icp->icmp6_id = htons(ident);
1324 icp->icmp6_seq = ntohs(seq);
1328 (void)gettimeofday(&tv, NULL);
1329 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
1330 tv32->tv32_sec = htonl(tv.tv_sec);
1331 tv32->tv32_usec = htonl(tv.tv_usec);
1333 cc = ICMP6ECHOLEN + datalen;
1337 if (pingerlen() != cc)
1338 errx(1, "internal error; length mismatch");
1341 smsghdr.msg_name = (caddr_t)&dst;
1342 smsghdr.msg_namelen = sizeof(dst);
1343 memset(&iov, 0, sizeof(iov));
1344 iov[0].iov_base = (caddr_t)outpack;
1345 iov[0].iov_len = cc;
1346 smsghdr.msg_iov = iov;
1347 smsghdr.msg_iovlen = 1;
1349 i = sendmsg(s, &smsghdr, 0);
1351 if (i < 0 || i != cc) {
1354 (void)printf("ping6: wrote %s %d chars, ret=%d\n",
1357 if (!(options & F_QUIET) && options & F_FLOOD)
1358 (void)write(STDOUT_FILENO, &DOT, 1);
1365 const struct icmp6_hdr *icp;
1367 if (ntohs(icp->icmp6_id) == ident)
1375 const struct icmp6_nodeinfo *nip;
1377 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
1378 nonce + sizeof(u_int16_t),
1379 sizeof(nonce) - sizeof(u_int16_t)) == 0)
1386 dnsdecode(sp, ep, base, buf, bufsiz)
1389 const u_char *base; /*base for compressed name*/
1395 char cresult[MAXDNAME + 1];
1406 if (i == 0 || cp != *sp) {
1407 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
1408 return NULL; /*result overrun*/
1414 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
1415 /* DNS compression */
1419 comp = base + (i & 0x3f);
1420 if (dnsdecode(&comp, cp, base, cresult,
1421 sizeof(cresult)) == NULL)
1423 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1424 return NULL; /*result overrun*/
1426 } else if ((i & 0x3f) == i) {
1428 return NULL; /*source overrun*/
1429 while (i-- > 0 && cp < ep) {
1430 l = snprintf(cresult, sizeof(cresult),
1431 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
1432 if (l >= sizeof(cresult) || l < 0)
1434 if (strlcat(buf, cresult, bufsiz) >= bufsiz)
1435 return NULL; /*result overrun*/
1439 return NULL; /*invalid label*/
1442 return NULL; /*not terminated*/
1450 * Print out the packet, if it came from us. This logic is necessary
1451 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1452 * which arrive ('tis only fair). This permits multiple copies of this
1453 * program to be run without having intermingled output (or statistics!).
1456 pr_pack(buf, cc, mhdr)
1459 struct msghdr *mhdr;
1461 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c)
1462 struct icmp6_hdr *icp;
1463 struct icmp6_nodeinfo *ni;
1466 struct sockaddr *from;
1468 u_char *cp = NULL, *dp, *end = buf + cc;
1469 struct in6_pktinfo *pktinfo = NULL;
1470 struct timeval tv, tp;
1472 double triptime = 0;
1477 char dnsname[MAXDNAME + 1];
1479 (void)gettimeofday(&tv, NULL);
1481 if (!mhdr || !mhdr->msg_name ||
1482 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1483 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
1484 if (options & F_VERBOSE)
1485 warnx("invalid peername");
1488 from = (struct sockaddr *)mhdr->msg_name;
1489 fromlen = mhdr->msg_namelen;
1490 if (cc < sizeof(struct icmp6_hdr)) {
1491 if (options & F_VERBOSE)
1492 warnx("packet too short (%d bytes) from %s", cc,
1493 pr_addr(from, fromlen));
1496 icp = (struct icmp6_hdr *)buf;
1497 ni = (struct icmp6_nodeinfo *)buf;
1500 if ((hoplim = get_hoplim(mhdr)) == -1) {
1501 warnx("failed to get receiving hop limit");
1504 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
1505 warnx("failed to get receiving packet information");
1509 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
1510 seq = ntohs(icp->icmp6_seq);
1513 tpp = (struct tv32 *)(icp + 1);
1514 tp.tv_sec = ntohl(tpp->tv32_sec);
1515 tp.tv_usec = ntohl(tpp->tv32_usec);
1517 triptime = ((double)tv.tv_sec) * 1000.0 +
1518 ((double)tv.tv_usec) / 1000.0;
1520 tsumsq += triptime * triptime;
1521 if (triptime < tmin)
1523 if (triptime > tmax)
1527 if (TST(seq % mx_dup_ck)) {
1532 SET(seq % mx_dup_ck);
1536 if (options & F_QUIET)
1539 if (options & F_FLOOD)
1540 (void)write(STDOUT_FILENO, &BSPACE, 1);
1542 (void)printf("%d bytes from %s, icmp_seq=%u", cc,
1543 pr_addr(from, fromlen), seq);
1544 (void)printf(" hlim=%d", hoplim);
1545 if ((options & F_VERBOSE) != 0) {
1546 struct sockaddr_in6 dstsa;
1548 memset(&dstsa, 0, sizeof(dstsa));
1549 dstsa.sin6_family = AF_INET6;
1550 dstsa.sin6_len = sizeof(dstsa);
1551 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
1552 dstsa.sin6_addr = pktinfo->ipi6_addr;
1553 (void)printf(" dst=%s",
1554 pr_addr((struct sockaddr *)&dstsa,
1558 (void)printf(" time=%.3f ms", triptime);
1560 (void)printf("(DUP!)");
1561 /* check the data */
1562 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1563 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
1564 for (i = 8; cp < end; ++i, ++cp, ++dp) {
1566 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
1571 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
1572 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce);
1574 if (TST(seq % mx_dup_ck)) {
1579 SET(seq % mx_dup_ck);
1583 if (options & F_QUIET)
1586 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1588 switch (ntohs(ni->ni_code)) {
1589 case ICMP6_NI_SUCCESS:
1591 case ICMP6_NI_REFUSED:
1592 printf("refused, type 0x%x", ntohs(ni->ni_type));
1594 case ICMP6_NI_UNKNOWN:
1595 printf("unknown, type 0x%x", ntohs(ni->ni_type));
1598 printf("unknown code 0x%x, type 0x%x",
1599 ntohs(ni->ni_code), ntohs(ni->ni_type));
1603 switch (ntohs(ni->ni_qtype)) {
1605 printf("NodeInfo NOOP");
1607 case NI_QTYPE_SUPTYPES:
1608 pr_suptypes(ni, end - (u_char *)ni);
1610 case NI_QTYPE_NODEADDR:
1611 pr_nodeaddr(ni, end - (u_char *)ni);
1614 default: /* XXX: for backward compatibility */
1615 cp = (u_char *)ni + ICMP6_NIRLEN;
1616 if (buf[off + ICMP6_NIRLEN] ==
1617 cc - off - ICMP6_NIRLEN - 1)
1622 cp++; /* skip length */
1624 safeputc(*cp & 0xff);
1630 if (dnsdecode((const u_char **)&cp, end,
1631 (const u_char *)(ni + 1), dnsname,
1632 sizeof(dnsname)) == NULL) {
1637 * name-lookup special handling for
1640 if (cp + 1 <= end && !*cp &&
1641 strlen(dnsname) > 0) {
1642 dnsname[strlen(dnsname) - 1] = '\0';
1645 printf("%s%s", i > 0 ? "," : "",
1649 if (options & F_VERBOSE) {
1653 (void)printf(" ("); /*)*/
1655 switch (ni->ni_code) {
1656 case ICMP6_NI_REFUSED:
1657 (void)printf("refused");
1660 case ICMP6_NI_UNKNOWN:
1661 (void)printf("unknown qtype");
1666 if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
1667 /* case of refusion, unknown */
1672 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
1675 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
1676 (void)printf("TTL=%d:meaningless",
1680 (void)printf("TTL=%d:invalid",
1683 (void)printf("TTL=%d", ttl);
1693 cp = (u_char *)ni + ICMP6_NIRLEN;
1702 if (buf[off + ICMP6_NIRLEN] !=
1703 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
1706 (void)printf("invalid namelen:%d/%lu",
1707 buf[off + ICMP6_NIRLEN],
1708 (u_long)cc - off - ICMP6_NIRLEN - 1);
1718 /* We've got something other than an ECHOREPLY */
1719 if (!(options & F_VERBOSE))
1721 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1725 if (!(options & F_FLOOD)) {
1726 (void)putchar('\n');
1727 if (options & F_VERBOSE)
1729 (void)fflush(stdout);
1736 struct msghdr *mhdr;
1740 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1741 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1742 if (cm->cmsg_level != IPPROTO_IPV6)
1745 switch (cm->cmsg_type) {
1747 printf(" HbH Options: ");
1748 pr_ip6opt(CMSG_DATA(cm));
1751 #ifdef IPV6_RTHDRDSTOPTS
1752 case IPV6_RTHDRDSTOPTS:
1754 printf(" Dst Options: ");
1755 pr_ip6opt(CMSG_DATA(cm));
1758 printf(" Routing: ");
1759 pr_rthdr(CMSG_DATA(cm));
1765 #ifdef USE_RFC2292BIS
1767 pr_ip6opt(void *extbuf)
1769 struct ip6_hbh *ext;
1772 socklen_t extlen, len;
1778 ext = (struct ip6_hbh *)extbuf;
1779 extlen = (ext->ip6h_len + 1) * 8;
1780 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1781 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1785 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1786 &type, &len, &databuf);
1787 if (currentlen == -1)
1791 * Note that inet6_opt_next automatically skips any padding
1796 offset = inet6_opt_get_val(databuf, offset,
1797 &value4, sizeof(value4));
1798 printf(" Jumbo Payload Opt: Length %u\n",
1799 (u_int32_t)ntohl(value4));
1801 case IP6OPT_ROUTER_ALERT:
1803 offset = inet6_opt_get_val(databuf, offset,
1804 &value2, sizeof(value2));
1805 printf(" Router Alert Opt: Type %u\n",
1809 printf(" Received Opt %u len %lu\n",
1810 type, (unsigned long)len);
1816 #else /* !USE_RFC2292BIS */
1819 pr_ip6opt(void *extbuf)
1824 #endif /* USE_RFC2292BIS */
1826 #ifdef USE_RFC2292BIS
1828 pr_rthdr(void *extbuf)
1830 struct in6_addr *in6;
1831 char ntopbuf[INET6_ADDRSTRLEN];
1832 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1835 /* print fixed part of the header */
1836 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1837 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1838 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1839 printf("%d segments, ", segments);
1841 printf("segments unknown, ");
1842 printf("%d left\n", rh->ip6r_segleft);
1844 for (i = 0; i < segments; i++) {
1845 in6 = inet6_rth_getaddr(extbuf, i);
1847 printf(" [%d]<NULL>\n", i);
1849 if (!inet_ntop(AF_INET6, in6, ntopbuf,
1851 strlcpy(ntopbuf, "?", sizeof(ntopbuf));
1852 printf(" [%d]%s\n", i, ntopbuf);
1860 #else /* !USE_RFC2292BIS */
1863 pr_rthdr(void *extbuf)
1868 #endif /* USE_RFC2292BIS */
1871 pr_bitrange(v, soff, ii)
1881 /* shift till we have 0x01 */
1882 if ((v & 0x01) == 0) {
1884 printf("-%u", soff + off - 1);
1895 case 0x04: case 0x0c:
1906 /* we have 0x01 with us */
1907 for (i = 0; i < 32 - off; i++) {
1908 if ((v & (0x01 << i)) == 0)
1912 printf(" %u", soff + off);
1920 pr_suptypes(ni, nilen)
1921 struct icmp6_nodeinfo *ni; /* ni->qtype must be SUPTYPES */
1926 const u_char *cp, *end;
1929 u_int16_t words; /*32bit count*/
1932 #define MAXQTYPES (1 << 16)
1936 cp = (u_char *)(ni + 1);
1937 end = ((u_char *)ni) + nilen;
1941 printf("NodeInfo Supported Qtypes");
1942 if (options & F_VERBOSE) {
1943 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
1944 printf(", compressed bitmap");
1946 printf(", raw bitmap");
1950 clen = (size_t)(end - cp);
1951 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
1952 if (clen == 0 || clen > MAXQTYPES / 8 ||
1958 if (clen < sizeof(cbit) || clen % sizeof(v))
1960 memcpy(&cbit, cp, sizeof(cbit));
1961 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
1965 clen = ntohs(cbit.words) * sizeof(v);
1966 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
1971 for (off = 0; off < clen; off += sizeof(v)) {
1972 memcpy(&v, cp + off, sizeof(v));
1973 v = (u_int32_t)ntohl(v);
1974 b = pr_bitrange(v, (int)(cur + off * 8), b);
1976 /* flush the remaining bits */
1977 b = pr_bitrange(0, (int)(cur + off * 8), b);
1981 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
1982 cur += ntohs(cbit.skip) * 32;
1987 pr_nodeaddr(ni, nilen)
1988 struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
1991 u_char *cp = (u_char *)(ni + 1);
1992 char ntop_buf[INET6_ADDRSTRLEN];
1995 nilen -= sizeof(struct icmp6_nodeinfo);
1997 if (options & F_VERBOSE) {
1998 switch (ni->ni_code) {
1999 case ICMP6_NI_REFUSED:
2000 (void)printf("refused");
2002 case ICMP6_NI_UNKNOWN:
2003 (void)printf("unknown qtype");
2006 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
2007 (void)printf(" truncated");
2011 printf(" no address\n");
2014 * In icmp-name-lookups 05 and later, TTL of each returned address
2015 * is contained in the resposne. We try to detect the version
2016 * by the length of the data, but note that the detection algorithm
2017 * is incomplete. We assume the latest draft by default.
2019 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
2025 /* XXX: alignment? */
2026 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
2027 cp += sizeof(u_int32_t);
2028 nilen -= sizeof(u_int32_t);
2031 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
2033 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2034 printf(" %s", ntop_buf);
2036 if (ttl == 0xffffffff) {
2038 * XXX: can this convention be applied to all
2039 * type of TTL (i.e. non-ND TTL)?
2041 printf("(TTL=infty)");
2044 printf("(TTL=%u)", ttl);
2048 nilen -= sizeof(struct in6_addr);
2049 cp += sizeof(struct in6_addr);
2055 struct msghdr *mhdr;
2059 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2060 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2061 if (cm->cmsg_len == 0)
2064 if (cm->cmsg_level == IPPROTO_IPV6 &&
2065 cm->cmsg_type == IPV6_HOPLIMIT &&
2066 cm->cmsg_len == CMSG_LEN(sizeof(int)))
2067 return(*(int *)CMSG_DATA(cm));
2073 struct in6_pktinfo *
2074 get_rcvpktinfo(mhdr)
2075 struct msghdr *mhdr;
2079 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2080 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2081 if (cm->cmsg_len == 0)
2084 if (cm->cmsg_level == IPPROTO_IPV6 &&
2085 cm->cmsg_type == IPV6_PKTINFO &&
2086 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
2087 return((struct in6_pktinfo *)CMSG_DATA(cm));
2095 struct msghdr *mhdr;
2097 #ifdef IPV6_RECVPATHMTU
2099 struct ip6_mtuinfo *mtuctl = NULL;
2101 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
2102 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
2103 if (cm->cmsg_len == 0)
2106 if (cm->cmsg_level == IPPROTO_IPV6 &&
2107 cm->cmsg_type == IPV6_PATHMTU &&
2108 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
2109 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
2112 * If the notified destination is different from
2113 * the one we are pinging, just ignore the info.
2114 * We check the scope ID only when both notified value
2115 * and our own value have non-0 values, because we may
2116 * have used the default scope zone ID for sending,
2117 * in which case the scope ID value is 0.
2119 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
2121 (mtuctl->ip6m_addr.sin6_scope_id &&
2122 dst.sin6_scope_id &&
2123 mtuctl->ip6m_addr.sin6_scope_id !=
2124 dst.sin6_scope_id)) {
2125 if ((options & F_VERBOSE) != 0) {
2126 printf("path MTU for %s is notified. "
2128 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
2129 sizeof(mtuctl->ip6m_addr)));
2135 * Ignore an invalid MTU. XXX: can we just believe
2138 if (mtuctl->ip6m_mtu < IPV6_MMTU)
2141 /* notification for our destination. return the MTU. */
2142 return((int)mtuctl->ip6m_mtu);
2151 * Subtract 2 timeval structs: out = out - in. Out is assumed to
2156 struct timeval *out, *in;
2158 if ((out->tv_usec -= in->tv_usec) < 0) {
2160 out->tv_usec += 1000000;
2162 out->tv_sec -= in->tv_sec;
2176 (void)signal(SIGINT, SIG_DFL);
2177 (void)kill(getpid(), SIGINT);
2185 * Print out statistics.
2191 (void)printf("\n--- %s ping6 statistics ---\n", hostname);
2192 (void)printf("%ld packets transmitted, ", ntransmitted);
2193 (void)printf("%ld packets received, ", nreceived);
2195 (void)printf("+%ld duplicates, ", nrepeats);
2197 if (nreceived > ntransmitted)
2198 (void)printf("-- somebody's duplicating packets!");
2200 (void)printf("%.1f%% packet loss",
2201 ((((double)ntransmitted - nreceived) * 100.0) /
2204 (void)putchar('\n');
2205 if (nreceived && timing) {
2206 /* Only display average to microseconds */
2207 double num = nreceived + nrepeats;
2208 double avg = tsum / num;
2209 double dev = sqrt(tsumsq / num - avg * avg);
2211 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
2212 tmin, avg, tmax, dev);
2213 (void)fflush(stdout);
2215 (void)fflush(stdout);
2219 static const char *niqcode[] = {
2221 "DNS label", /*or empty*/
2226 static const char *nircode[] = {
2227 "Success", "Refused", "Unknown",
2233 * Print a descriptive string about an ICMP header.
2237 struct icmp6_hdr *icp;
2240 char ntop_buf[INET6_ADDRSTRLEN];
2241 struct nd_redirect *red;
2242 struct icmp6_nodeinfo *ni;
2243 char dnsname[MAXDNAME + 1];
2247 switch (icp->icmp6_type) {
2248 case ICMP6_DST_UNREACH:
2249 switch (icp->icmp6_code) {
2250 case ICMP6_DST_UNREACH_NOROUTE:
2251 (void)printf("No Route to Destination\n");
2253 case ICMP6_DST_UNREACH_ADMIN:
2254 (void)printf("Destination Administratively "
2257 case ICMP6_DST_UNREACH_BEYONDSCOPE:
2258 (void)printf("Destination Unreachable Beyond Scope\n");
2260 case ICMP6_DST_UNREACH_ADDR:
2261 (void)printf("Destination Host Unreachable\n");
2263 case ICMP6_DST_UNREACH_NOPORT:
2264 (void)printf("Destination Port Unreachable\n");
2267 (void)printf("Destination Unreachable, Bad Code: %d\n",
2271 /* Print returned IP header information */
2272 pr_retip((struct ip6_hdr *)(icp + 1), end);
2274 case ICMP6_PACKET_TOO_BIG:
2275 (void)printf("Packet too big mtu = %d\n",
2276 (int)ntohl(icp->icmp6_mtu));
2277 pr_retip((struct ip6_hdr *)(icp + 1), end);
2279 case ICMP6_TIME_EXCEEDED:
2280 switch (icp->icmp6_code) {
2281 case ICMP6_TIME_EXCEED_TRANSIT:
2282 (void)printf("Time to live exceeded\n");
2284 case ICMP6_TIME_EXCEED_REASSEMBLY:
2285 (void)printf("Frag reassembly time exceeded\n");
2288 (void)printf("Time exceeded, Bad Code: %d\n",
2292 pr_retip((struct ip6_hdr *)(icp + 1), end);
2294 case ICMP6_PARAM_PROB:
2295 (void)printf("Parameter problem: ");
2296 switch (icp->icmp6_code) {
2297 case ICMP6_PARAMPROB_HEADER:
2298 (void)printf("Erroneous Header ");
2300 case ICMP6_PARAMPROB_NEXTHEADER:
2301 (void)printf("Unknown Nextheader ");
2303 case ICMP6_PARAMPROB_OPTION:
2304 (void)printf("Unrecognized Option ");
2307 (void)printf("Bad code(%d) ", icp->icmp6_code);
2310 (void)printf("pointer = 0x%02x\n",
2311 (u_int32_t)ntohl(icp->icmp6_pptr));
2312 pr_retip((struct ip6_hdr *)(icp + 1), end);
2314 case ICMP6_ECHO_REQUEST:
2315 (void)printf("Echo Request");
2316 /* XXX ID + Seq + Data */
2318 case ICMP6_ECHO_REPLY:
2319 (void)printf("Echo Reply");
2320 /* XXX ID + Seq + Data */
2322 case ICMP6_MEMBERSHIP_QUERY:
2323 (void)printf("Listener Query");
2325 case ICMP6_MEMBERSHIP_REPORT:
2326 (void)printf("Listener Report");
2328 case ICMP6_MEMBERSHIP_REDUCTION:
2329 (void)printf("Listener Done");
2331 case ND_ROUTER_SOLICIT:
2332 (void)printf("Router Solicitation");
2334 case ND_ROUTER_ADVERT:
2335 (void)printf("Router Advertisement");
2337 case ND_NEIGHBOR_SOLICIT:
2338 (void)printf("Neighbor Solicitation");
2340 case ND_NEIGHBOR_ADVERT:
2341 (void)printf("Neighbor Advertisement");
2344 red = (struct nd_redirect *)icp;
2345 (void)printf("Redirect\n");
2346 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
2348 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2349 (void)printf("Destination: %s", ntop_buf);
2350 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
2352 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2353 (void)printf(" New Target: %s", ntop_buf);
2355 case ICMP6_NI_QUERY:
2356 (void)printf("Node Information Query");
2357 /* XXX ID + Seq + Data */
2358 ni = (struct icmp6_nodeinfo *)icp;
2359 l = end - (u_char *)(ni + 1);
2361 switch (ntohs(ni->ni_qtype)) {
2363 (void)printf("NOOP");
2365 case NI_QTYPE_SUPTYPES:
2366 (void)printf("Supported qtypes");
2369 (void)printf("DNS name");
2371 case NI_QTYPE_NODEADDR:
2372 (void)printf("nodeaddr");
2374 case NI_QTYPE_IPV4ADDR:
2375 (void)printf("IPv4 nodeaddr");
2378 (void)printf("unknown qtype");
2381 if (options & F_VERBOSE) {
2382 switch (ni->ni_code) {
2383 case ICMP6_NI_SUBJ_IPV6:
2384 if (l == sizeof(struct in6_addr) &&
2385 inet_ntop(AF_INET6, ni + 1, ntop_buf,
2386 sizeof(ntop_buf)) != NULL) {
2387 (void)printf(", subject=%s(%s)",
2388 niqcode[ni->ni_code], ntop_buf);
2391 /* backward compat to -W */
2392 (void)printf(", oldfqdn");
2394 (void)printf(", invalid");
2398 case ICMP6_NI_SUBJ_FQDN:
2399 if (end == (u_char *)(ni + 1)) {
2400 (void)printf(", no subject");
2403 printf(", subject=%s", niqcode[ni->ni_code]);
2404 cp = (const u_char *)(ni + 1);
2405 if (dnsdecode(&cp, end, NULL, dnsname,
2406 sizeof(dnsname)) != NULL)
2407 printf("(%s)", dnsname);
2409 printf("(invalid)");
2411 case ICMP6_NI_SUBJ_IPV4:
2412 if (l == sizeof(struct in_addr) &&
2413 inet_ntop(AF_INET, ni + 1, ntop_buf,
2414 sizeof(ntop_buf)) != NULL) {
2415 (void)printf(", subject=%s(%s)",
2416 niqcode[ni->ni_code], ntop_buf);
2418 (void)printf(", invalid");
2421 (void)printf(", invalid");
2426 case ICMP6_NI_REPLY:
2427 (void)printf("Node Information Reply");
2428 /* XXX ID + Seq + Data */
2429 ni = (struct icmp6_nodeinfo *)icp;
2431 switch (ntohs(ni->ni_qtype)) {
2433 (void)printf("NOOP");
2435 case NI_QTYPE_SUPTYPES:
2436 (void)printf("Supported qtypes");
2439 (void)printf("DNS name");
2441 case NI_QTYPE_NODEADDR:
2442 (void)printf("nodeaddr");
2444 case NI_QTYPE_IPV4ADDR:
2445 (void)printf("IPv4 nodeaddr");
2448 (void)printf("unknown qtype");
2451 if (options & F_VERBOSE) {
2452 if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))
2453 printf(", invalid");
2455 printf(", %s", nircode[ni->ni_code]);
2459 (void)printf("Bad ICMP type: %d", icp->icmp6_type);
2465 * Print an IP6 header.
2469 struct ip6_hdr *ip6;
2471 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
2473 char ntop_buf[INET6_ADDRSTRLEN];
2475 tc = *(&ip6->ip6_vfc + 1); /* XXX */
2476 tc = (tc >> 4) & 0x0f;
2477 tc |= (ip6->ip6_vfc << 4);
2479 printf("Vr TC Flow Plen Nxt Hlim\n");
2480 printf(" %1x %02x %05x %04x %02x %02x\n",
2481 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
2482 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
2483 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2484 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2485 printf("%s->", ntop_buf);
2486 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2487 strlcpy(ntop_buf, "?", sizeof(ntop_buf));
2488 printf("%s\n", ntop_buf);
2493 * Return an ascii host address as a dotted quad and optionally with
2497 pr_addr(addr, addrlen)
2498 struct sockaddr *addr;
2501 static char buf[NI_MAXHOST];
2504 if ((options & F_HOSTNAME) == 0)
2505 flag |= NI_NUMERICHOST;
2507 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
2515 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2519 struct ip6_hdr *ip6;
2522 u_char *cp = (u_char *)ip6, nh;
2525 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2530 hlen = sizeof(*ip6);
2534 while (end - cp >= 8) {
2536 case IPPROTO_HOPOPTS:
2538 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2539 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2541 case IPPROTO_DSTOPTS:
2543 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2544 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2546 case IPPROTO_FRAGMENT:
2548 hlen = sizeof(struct ip6_frag);
2549 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2551 case IPPROTO_ROUTING:
2553 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2554 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2559 hlen = (((struct ah *)cp)->ah_len+2) << 2;
2560 nh = ((struct ah *)cp)->ah_nxt;
2563 case IPPROTO_ICMPV6:
2564 printf("ICMP6: type = %d, code = %d\n",
2571 printf("TCP: from port %u, to port %u (decimal)\n",
2572 (*cp * 256 + *(cp + 1)),
2573 (*(cp + 2) * 256 + *(cp + 3)));
2576 printf("UDP: from port %u, to port %u (decimal)\n",
2577 (*cp * 256 + *(cp + 1)),
2578 (*(cp + 2) * 256 + *(cp + 3)));
2581 printf("Unknown Header(%d)\n", nh);
2585 if ((cp += hlen) >= end)
2607 for (cp = patp; *cp; cp++)
2609 errx(1, "patterns must be specified as hex digits");
2611 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
2612 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
2613 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
2614 &pat[13], &pat[14], &pat[15]);
2619 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);
2621 for (jj = 0; jj < ii; ++jj)
2622 bp[jj + kk] = pat[jj];
2623 if (!(options & F_QUIET)) {
2624 (void)printf("PATTERN: 0x");
2625 for (jj = 0; jj < ii; ++jj)
2626 (void)printf("%02x", bp[jj] & 0xFF);
2632 #ifdef IPSEC_POLICY_IPSEC
2634 setpolicy(so, policy)
2641 return 0; /* ignore */
2643 buf = ipsec_set_policy(policy, strlen(policy));
2645 errx(1, "%s", ipsec_strerror());
2646 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
2647 ipsec_get_policylen(buf)) < 0)
2648 warnx("Unable to set IPsec policy");
2663 u_int8_t digest[16];
2666 char hbuf[NI_MAXHOST];
2667 struct in6_addr in6;
2669 p = strchr(name, '.');
2671 p = name + strlen(name);
2673 if (l > 63 || l > sizeof(hbuf) - 1)
2674 return NULL; /*label too long*/
2675 strncpy(hbuf, name, l);
2676 hbuf[(int)l] = '\0';
2678 for (q = name; *q; q++) {
2679 if (isupper(*(unsigned char *)q))
2680 *q = tolower(*(unsigned char *)q);
2683 /* generate 8 bytes of pseudo-random value. */
2684 memset(&ctxt, 0, sizeof(ctxt));
2687 MD5Update(&ctxt, &c, sizeof(c));
2688 MD5Update(&ctxt, (unsigned char *)name, l);
2689 MD5Final(digest, &ctxt);
2691 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
2692 return NULL; /*XXX*/
2693 bcopy(digest, &in6.s6_addr[12], 4);
2695 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
2698 return strdup(hbuf);
2704 (void)fprintf(stderr,
2705 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2710 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
2714 #ifdef IPV6_USE_MIN_MTU
2718 "[-a addrtype] [-b bufsiz] [-c count] [-g gateway]\n"
2719 " [-h hoplimit] [-I interface] [-i wait] [-l preload]"
2720 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2724 " [-p pattern] [-S sourceaddr] [-s packetsize] "
2725 "[hops ...] host\n");