xref: /freebsd/sbin/ping/ping6.c (revision 9ce201f2ee3ca340032d9cc71d91a36b3b45a4c3)
13cde9171SAlan Somers /*	$KAME: ping6.c,v 1.169 2003/07/25 06:01:47 itojun Exp $	*/
23cde9171SAlan Somers 
33cde9171SAlan Somers /*-
43cde9171SAlan Somers  * SPDX-License-Identifier: BSD-3-Clause
53cde9171SAlan Somers  *
63cde9171SAlan Somers  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
73cde9171SAlan Somers  * All rights reserved.
83cde9171SAlan Somers  *
93cde9171SAlan Somers  * Redistribution and use in source and binary forms, with or without
103cde9171SAlan Somers  * modification, are permitted provided that the following conditions
113cde9171SAlan Somers  * are met:
123cde9171SAlan Somers  * 1. Redistributions of source code must retain the above copyright
133cde9171SAlan Somers  *    notice, this list of conditions and the following disclaimer.
143cde9171SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
153cde9171SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
163cde9171SAlan Somers  *    documentation and/or other materials provided with the distribution.
173cde9171SAlan Somers  * 3. Neither the name of the project nor the names of its contributors
183cde9171SAlan Somers  *    may be used to endorse or promote products derived from this software
193cde9171SAlan Somers  *    without specific prior written permission.
203cde9171SAlan Somers  *
213cde9171SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
223cde9171SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
233cde9171SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243cde9171SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
253cde9171SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
263cde9171SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
273cde9171SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
283cde9171SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293cde9171SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
303cde9171SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313cde9171SAlan Somers  * SUCH DAMAGE.
323cde9171SAlan Somers  */
333cde9171SAlan Somers 
343cde9171SAlan Somers /*	BSDI	ping.c,v 2.3 1996/01/21 17:56:50 jch Exp	*/
353cde9171SAlan Somers 
363cde9171SAlan Somers /*
373cde9171SAlan Somers  * Copyright (c) 1989, 1993
383cde9171SAlan Somers  *	The Regents of the University of California.  All rights reserved.
393cde9171SAlan Somers  *
403cde9171SAlan Somers  * This code is derived from software contributed to Berkeley by
413cde9171SAlan Somers  * Mike Muuss.
423cde9171SAlan Somers  *
433cde9171SAlan Somers  * Redistribution and use in source and binary forms, with or without
443cde9171SAlan Somers  * modification, are permitted provided that the following conditions
453cde9171SAlan Somers  * are met:
463cde9171SAlan Somers  * 1. Redistributions of source code must retain the above copyright
473cde9171SAlan Somers  *    notice, this list of conditions and the following disclaimer.
483cde9171SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
493cde9171SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
503cde9171SAlan Somers  *    documentation and/or other materials provided with the distribution.
513cde9171SAlan Somers  * 3. Neither the name of the University nor the names of its contributors
523cde9171SAlan Somers  *    may be used to endorse or promote products derived from this software
533cde9171SAlan Somers  *    without specific prior written permission.
543cde9171SAlan Somers  *
553cde9171SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
563cde9171SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
573cde9171SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
583cde9171SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
593cde9171SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
603cde9171SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
613cde9171SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
623cde9171SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
633cde9171SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
643cde9171SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
653cde9171SAlan Somers  * SUCH DAMAGE.
663cde9171SAlan Somers  */
673cde9171SAlan Somers 
683cde9171SAlan Somers #if 0
693cde9171SAlan Somers #ifndef lint
703cde9171SAlan Somers static const char copyright[] =
713cde9171SAlan Somers "@(#) Copyright (c) 1989, 1993\n\
723cde9171SAlan Somers 	The Regents of the University of California.  All rights reserved.\n";
733cde9171SAlan Somers #endif /* not lint */
743cde9171SAlan Somers 
753cde9171SAlan Somers #ifndef lint
763cde9171SAlan Somers static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
773cde9171SAlan Somers #endif /* not lint */
783cde9171SAlan Somers #endif
793cde9171SAlan Somers 
803cde9171SAlan Somers #include <sys/cdefs.h>
813cde9171SAlan Somers __FBSDID("$FreeBSD$");
823cde9171SAlan Somers 
833cde9171SAlan Somers /*
843cde9171SAlan Somers  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
853cde9171SAlan Somers  * measure round-trip-delays and packet loss across network paths.
863cde9171SAlan Somers  *
873cde9171SAlan Somers  * Author -
883cde9171SAlan Somers  *	Mike Muuss
893cde9171SAlan Somers  *	U. S. Army Ballistic Research Laboratory
903cde9171SAlan Somers  *	December, 1983
913cde9171SAlan Somers  *
923cde9171SAlan Somers  * Status -
933cde9171SAlan Somers  *	Public Domain.  Distribution Unlimited.
943cde9171SAlan Somers  * Bugs -
953cde9171SAlan Somers  *	More statistics could always be gathered.
963cde9171SAlan Somers  *	This program has to run SUID to ROOT to access the ICMP socket.
973cde9171SAlan Somers  */
983cde9171SAlan Somers /*
993cde9171SAlan Somers  * NOTE:
1003cde9171SAlan Somers  * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
1013cde9171SAlan Somers  * as IPV6_PKTINFO.  Some people object it (sin6_scope_id specifies *link*
1023cde9171SAlan Somers  * while IPV6_PKTINFO specifies *interface*.  Link is defined as collection of
1033cde9171SAlan Somers  * network attached to 1 or more interfaces)
1043cde9171SAlan Somers  */
1053cde9171SAlan Somers 
1063cde9171SAlan Somers #include <sys/param.h>
1073cde9171SAlan Somers #include <sys/capsicum.h>
1083cde9171SAlan Somers #include <sys/uio.h>
1093cde9171SAlan Somers #include <sys/socket.h>
1103cde9171SAlan Somers 
1113cde9171SAlan Somers #include <net/if.h>
1123cde9171SAlan Somers #include <net/route.h>
1133cde9171SAlan Somers 
1143cde9171SAlan Somers #include <netinet/in.h>
1153cde9171SAlan Somers #include <netinet/ip6.h>
1163cde9171SAlan Somers #include <netinet/icmp6.h>
1173cde9171SAlan Somers #include <arpa/inet.h>
1183cde9171SAlan Somers #include <arpa/nameser.h>
1193cde9171SAlan Somers #include <netdb.h>
1203cde9171SAlan Somers 
1213cde9171SAlan Somers #include <capsicum_helpers.h>
1223cde9171SAlan Somers #include <casper/cap_dns.h>
1233cde9171SAlan Somers #include <libcasper.h>
1243cde9171SAlan Somers 
1253cde9171SAlan Somers #include <ctype.h>
1263cde9171SAlan Somers #include <err.h>
1273cde9171SAlan Somers #include <errno.h>
1283cde9171SAlan Somers #include <fcntl.h>
1293cde9171SAlan Somers #include <math.h>
1303cde9171SAlan Somers #include <signal.h>
1313cde9171SAlan Somers #include <stdio.h>
1323cde9171SAlan Somers #include <stdlib.h>
1333cde9171SAlan Somers #include <string.h>
1343cde9171SAlan Somers #include <sysexits.h>
1353cde9171SAlan Somers #include <time.h>
1363cde9171SAlan Somers #include <unistd.h>
1373cde9171SAlan Somers 
1383cde9171SAlan Somers #ifdef IPSEC
1393cde9171SAlan Somers #include <netipsec/ah.h>
1403cde9171SAlan Somers #include <netipsec/ipsec.h>
1413cde9171SAlan Somers #endif
1423cde9171SAlan Somers 
1433cde9171SAlan Somers #include <md5.h>
1443cde9171SAlan Somers 
1453cde9171SAlan Somers #include "main.h"
1463cde9171SAlan Somers #include "ping6.h"
1473cde9171SAlan Somers 
1483cde9171SAlan Somers struct tv32 {
1493cde9171SAlan Somers 	u_int32_t tv32_sec;
1503cde9171SAlan Somers 	u_int32_t tv32_nsec;
1513cde9171SAlan Somers };
1523cde9171SAlan Somers 
1533cde9171SAlan Somers #define MAXPACKETLEN	131072
1543cde9171SAlan Somers #define	IP6LEN		40
1553cde9171SAlan Somers #define ICMP6ECHOLEN	8	/* icmp echo header len excluding time */
1563cde9171SAlan Somers #define ICMP6ECHOTMLEN sizeof(struct tv32)
1573cde9171SAlan Somers #define ICMP6_NIQLEN	(ICMP6ECHOLEN + 8)
1583cde9171SAlan Somers # define CONTROLLEN	10240	/* ancillary data buffer size RFC3542 20.1 */
1593cde9171SAlan Somers /* FQDN case, 64 bits of nonce + 32 bits ttl */
1603cde9171SAlan Somers #define ICMP6_NIRLEN	(ICMP6ECHOLEN + 12)
1613cde9171SAlan Somers #define	EXTRA		256	/* for AH and various other headers. weird. */
1623cde9171SAlan Somers #define	DEFDATALEN	ICMP6ECHOTMLEN
1633cde9171SAlan Somers #define MAXDATALEN	MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
1643cde9171SAlan Somers #define	NROUTES		9		/* number of record route slots */
1653cde9171SAlan Somers #define	MAXWAIT		10000		/* max ms to wait for response */
1663cde9171SAlan Somers #define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1673cde9171SAlan Somers 
1683cde9171SAlan Somers #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1693cde9171SAlan Somers #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1703cde9171SAlan Somers #define	SET(bit)	(A(bit) |= B(bit))
1713cde9171SAlan Somers #define	CLR(bit)	(A(bit) &= (~B(bit)))
1723cde9171SAlan Somers #define	TST(bit)	(A(bit) & B(bit))
1733cde9171SAlan Somers 
1743cde9171SAlan Somers #define	F_FLOOD		0x0001
1753cde9171SAlan Somers #define	F_INTERVAL	0x0002
1763cde9171SAlan Somers #define	F_PINGFILLED	0x0008
1773cde9171SAlan Somers #define	F_QUIET		0x0010
1783cde9171SAlan Somers #define	F_RROUTE	0x0020
1793cde9171SAlan Somers #define	F_SO_DEBUG	0x0040
1803cde9171SAlan Somers #define	F_VERBOSE	0x0100
1813cde9171SAlan Somers #ifdef IPSEC
1823cde9171SAlan Somers #ifdef IPSEC_POLICY_IPSEC
1833cde9171SAlan Somers #define	F_POLICY	0x0400
1843cde9171SAlan Somers #else
1853cde9171SAlan Somers #define F_AUTHHDR	0x0200
1863cde9171SAlan Somers #define F_ENCRYPT	0x0400
1873cde9171SAlan Somers #endif /*IPSEC_POLICY_IPSEC*/
1883cde9171SAlan Somers #endif /*IPSEC*/
1893cde9171SAlan Somers #define F_NODEADDR	0x0800
1903cde9171SAlan Somers #define F_FQDN		0x1000
1913cde9171SAlan Somers #define F_INTERFACE	0x2000
1923cde9171SAlan Somers #define F_SRCADDR	0x4000
1933cde9171SAlan Somers #define F_HOSTNAME	0x10000
1943cde9171SAlan Somers #define F_FQDNOLD	0x20000
1953cde9171SAlan Somers #define F_NIGROUP	0x40000
1963cde9171SAlan Somers #define F_SUPTYPES	0x80000
1973cde9171SAlan Somers #define F_NOMINMTU	0x100000
1983cde9171SAlan Somers #define F_ONCE		0x200000
1993cde9171SAlan Somers #define F_AUDIBLE	0x400000
2003cde9171SAlan Somers #define F_MISSED	0x800000
2013cde9171SAlan Somers #define F_DONTFRAG	0x1000000
2023cde9171SAlan Somers #define F_NOUSERDATA	(F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
2033cde9171SAlan Somers #define	F_WAITTIME	0x2000000
2043cde9171SAlan Somers static u_int options;
2053cde9171SAlan Somers 
2063cde9171SAlan Somers #define IN6LEN		sizeof(struct in6_addr)
2073cde9171SAlan Somers #define SA6LEN		sizeof(struct sockaddr_in6)
2083cde9171SAlan Somers #define DUMMY_PORT	10101
2093cde9171SAlan Somers 
2103cde9171SAlan Somers #define SIN6(s)	((struct sockaddr_in6 *)(s))
2113cde9171SAlan Somers 
2123cde9171SAlan Somers /*
2133cde9171SAlan Somers  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
2143cde9171SAlan Somers  * number of received sequence numbers we can keep track of.  Change 128
2153cde9171SAlan Somers  * to 8192 for complete accuracy...
2163cde9171SAlan Somers  */
2173cde9171SAlan Somers #define	MAX_DUP_CHK	(8 * 8192)
2183cde9171SAlan Somers static int mx_dup_ck = MAX_DUP_CHK;
2193cde9171SAlan Somers static char rcvd_tbl[MAX_DUP_CHK / 8];
2203cde9171SAlan Somers 
2213cde9171SAlan Somers static struct sockaddr_in6 dst;	/* who to ping6 */
2223cde9171SAlan Somers static struct sockaddr_in6 src;	/* src addr of this packet */
2233cde9171SAlan Somers static socklen_t srclen;
2243cde9171SAlan Somers static size_t datalen = DEFDATALEN;
2253cde9171SAlan Somers static int ssend;		/* send socket file descriptor */
2263cde9171SAlan Somers static int srecv;		/* receive socket file descriptor */
2273cde9171SAlan Somers static u_char outpack[MAXPACKETLEN];
2283cde9171SAlan Somers static char BSPACE = '\b';	/* characters written for flood */
2293cde9171SAlan Somers static char BBELL = '\a';	/* characters written for AUDIBLE */
2303cde9171SAlan Somers static char DOT = '.';
2313cde9171SAlan Somers static char *hostname;
2323cde9171SAlan Somers static int ident;		/* process id to identify our packets */
2333cde9171SAlan Somers static u_int8_t nonce[8];	/* nonce field for node information */
2343cde9171SAlan Somers static int hoplimit = -1;	/* hoplimit */
2353cde9171SAlan Somers static int tclass = -1;		/* traffic class */
2363cde9171SAlan Somers static int pcp = -2;		/* vlan priority code point */
2373cde9171SAlan Somers static u_char *packet = NULL;
2383cde9171SAlan Somers static cap_channel_t *capdns;
2393cde9171SAlan Somers 
2403cde9171SAlan Somers /* counters */
2413cde9171SAlan Somers static long nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
2423cde9171SAlan Somers static long npackets;		/* max packets to transmit */
2433cde9171SAlan Somers static long nreceived;		/* # of packets we got back */
2443cde9171SAlan Somers static long nrepeats;		/* number of duplicates */
2453cde9171SAlan Somers static long ntransmitted;	/* sequence # for outbound packets = #sent */
2463cde9171SAlan Somers static long ntransmitfailures;	/* number of transmit failures */
2473cde9171SAlan Somers static int interval = 1000;	/* interval between packets in ms */
2483cde9171SAlan Somers static int waittime = MAXWAIT;	/* timeout for each packet */
2493cde9171SAlan Somers static long nrcvtimeout = 0;	/* # of packets we got back after waittime */
2503cde9171SAlan Somers 
2513cde9171SAlan Somers /* timing */
2523cde9171SAlan Somers static int timing;		/* flag to do timing */
2533cde9171SAlan Somers static double tmin = 999999999.0;	/* minimum round trip time */
2543cde9171SAlan Somers static double tmax = 0.0;	/* maximum round trip time */
2553cde9171SAlan Somers static double tsum = 0.0;	/* sum of all times, for doing average */
2563cde9171SAlan Somers static double tsumsq = 0.0;	/* sum of all times squared, for std. dev. */
2573cde9171SAlan Somers 
2583cde9171SAlan Somers /* for node addresses */
2593cde9171SAlan Somers static u_short naflags;
2603cde9171SAlan Somers 
2613cde9171SAlan Somers /* for ancillary data(advanced API) */
2623cde9171SAlan Somers static struct msghdr smsghdr;
2633cde9171SAlan Somers static struct iovec smsgiov;
2643cde9171SAlan Somers static char *scmsg = 0;
2653cde9171SAlan Somers 
2663cde9171SAlan Somers static volatile sig_atomic_t seenint;
2673cde9171SAlan Somers #ifdef SIGINFO
2683cde9171SAlan Somers static volatile sig_atomic_t seeninfo;
2693cde9171SAlan Somers #endif
2703cde9171SAlan Somers 
2713cde9171SAlan Somers static cap_channel_t *capdns_setup(void);
2723cde9171SAlan Somers static void	 fill(char *, char *);
2733cde9171SAlan Somers static int	 get_hoplim(struct msghdr *);
2743cde9171SAlan Somers static int	 get_pathmtu(struct msghdr *);
2753cde9171SAlan Somers static struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
2763cde9171SAlan Somers static void	 onsignal(int);
2773cde9171SAlan Somers static void	 onint(int);
2783cde9171SAlan Somers static size_t	 pingerlen(void);
2793cde9171SAlan Somers static int	 pinger(void);
2803cde9171SAlan Somers static const char *pr_addr(struct sockaddr *, int);
2813cde9171SAlan Somers static void	 pr_icmph(struct icmp6_hdr *, u_char *);
2823cde9171SAlan Somers static void	 pr_iph(struct ip6_hdr *);
2833cde9171SAlan Somers static void	 pr_suptypes(struct icmp6_nodeinfo *, size_t);
2843cde9171SAlan Somers static void	 pr_nodeaddr(struct icmp6_nodeinfo *, int);
2853cde9171SAlan Somers static int	 myechoreply(const struct icmp6_hdr *);
2863cde9171SAlan Somers static int	 mynireply(const struct icmp6_nodeinfo *);
2873cde9171SAlan Somers static const char *dnsdecode(const u_char *, const u_char *, const u_char *,
2883cde9171SAlan Somers     char *, size_t);
2893cde9171SAlan Somers static void	 pr_pack(u_char *, int, struct msghdr *);
2903cde9171SAlan Somers static void	 pr_exthdrs(struct msghdr *);
2913cde9171SAlan Somers static void	 pr_ip6opt(void *, size_t);
2923cde9171SAlan Somers static void	 pr_rthdr(void *, size_t);
2933cde9171SAlan Somers static int	 pr_bitrange(u_int32_t, int, int);
2943cde9171SAlan Somers static void	 pr_retip(struct ip6_hdr *, u_char *);
2953cde9171SAlan Somers static void	 summary(void);
296*9ce201f2SAlan Somers #ifdef IPSEC
297*9ce201f2SAlan Somers #ifdef IPSEC_POLICY_IPSEC
2983cde9171SAlan Somers static int	 setpolicy(int, char *);
299*9ce201f2SAlan Somers #endif
300*9ce201f2SAlan Somers #endif
3013cde9171SAlan Somers static char	*nigroup(char *, int);
3023cde9171SAlan Somers 
3033cde9171SAlan Somers int
3043cde9171SAlan Somers ping6(int argc, char *argv[])
3053cde9171SAlan Somers {
3063cde9171SAlan Somers 	struct timespec last, intvl;
3073cde9171SAlan Somers 	struct sockaddr_in6 from, *sin6;
3083cde9171SAlan Somers 	struct addrinfo hints, *res;
3093cde9171SAlan Somers 	struct sigaction si_sa;
3103cde9171SAlan Somers 	int cc, i;
3113cde9171SAlan Somers 	int almost_done, ch, hold, packlen, preload, optval, error;
3123cde9171SAlan Somers 	int nig_oldmcprefix = -1;
3133cde9171SAlan Somers 	u_char *datap;
3143cde9171SAlan Somers 	char *e, *target, *ifname = NULL, *gateway = NULL;
3153cde9171SAlan Somers 	int ip6optlen = 0;
3163cde9171SAlan Somers 	struct cmsghdr *scmsgp = NULL;
3173cde9171SAlan Somers 	/* For control (ancillary) data received from recvmsg() */
3183cde9171SAlan Somers 	u_char cm[CONTROLLEN];
3193cde9171SAlan Somers #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
3203cde9171SAlan Somers 	u_long lsockbufsize;
3213cde9171SAlan Somers 	int sockbufsize = 0;
3223cde9171SAlan Somers #endif
3233cde9171SAlan Somers 	int usepktinfo = 0;
3243cde9171SAlan Somers 	struct in6_pktinfo pktinfo;
3253cde9171SAlan Somers 	char *cmsg_pktinfo = NULL;
3263cde9171SAlan Somers 	struct ip6_rthdr *rthdr = NULL;
3273cde9171SAlan Somers #ifdef IPSEC_POLICY_IPSEC
3283cde9171SAlan Somers 	char *policy_in = NULL;
3293cde9171SAlan Somers 	char *policy_out = NULL;
3303cde9171SAlan Somers #endif
3313cde9171SAlan Somers 	double t;
3323cde9171SAlan Somers 	u_long alarmtimeout;
3333cde9171SAlan Somers 	size_t rthlen;
3343cde9171SAlan Somers #ifdef IPV6_USE_MIN_MTU
3353cde9171SAlan Somers 	int mflag = 0;
3363cde9171SAlan Somers #endif
3373cde9171SAlan Somers 	cap_rights_t rights_srecv;
3383cde9171SAlan Somers 	cap_rights_t rights_ssend;
3393cde9171SAlan Somers 	cap_rights_t rights_stdin;
3403cde9171SAlan Somers 
3413cde9171SAlan Somers 	/* just to be sure */
3423cde9171SAlan Somers 	memset(&smsghdr, 0, sizeof(smsghdr));
3433cde9171SAlan Somers 	memset(&smsgiov, 0, sizeof(smsgiov));
3443cde9171SAlan Somers 	memset(&pktinfo, 0, sizeof(pktinfo));
3453cde9171SAlan Somers 
3463cde9171SAlan Somers 	intvl.tv_sec = interval / 1000;
3473cde9171SAlan Somers 	intvl.tv_nsec = interval % 1000 * 1000000;
3483cde9171SAlan Somers 
3493cde9171SAlan Somers 	alarmtimeout = preload = 0;
3503cde9171SAlan Somers 	datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
3513cde9171SAlan Somers 	capdns = capdns_setup();
352*9ce201f2SAlan Somers 
353*9ce201f2SAlan Somers 	while ((ch = getopt(argc, argv, PING6OPTS)) != -1) {
3543cde9171SAlan Somers 		switch (ch) {
3553cde9171SAlan Somers 		case '6':
3563cde9171SAlan Somers 			/* This option is processed in main(). */
3573cde9171SAlan Somers 			break;
3583cde9171SAlan Somers 		case 'k':
3593cde9171SAlan Somers 		{
3603cde9171SAlan Somers 			char *cp;
3613cde9171SAlan Somers 
3623cde9171SAlan Somers 			options &= ~F_NOUSERDATA;
3633cde9171SAlan Somers 			options |= F_NODEADDR;
3643cde9171SAlan Somers 			for (cp = optarg; *cp != '\0'; cp++) {
3653cde9171SAlan Somers 				switch (*cp) {
3663cde9171SAlan Somers 				case 'a':
3673cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_ALL;
3683cde9171SAlan Somers 					break;
3693cde9171SAlan Somers 				case 'c':
3703cde9171SAlan Somers 				case 'C':
3713cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_COMPAT;
3723cde9171SAlan Somers 					break;
3733cde9171SAlan Somers 				case 'l':
3743cde9171SAlan Somers 				case 'L':
3753cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
3763cde9171SAlan Somers 					break;
3773cde9171SAlan Somers 				case 's':
3783cde9171SAlan Somers 				case 'S':
3793cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_SITELOCAL;
3803cde9171SAlan Somers 					break;
3813cde9171SAlan Somers 				case 'g':
3823cde9171SAlan Somers 				case 'G':
3833cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_GLOBAL;
3843cde9171SAlan Somers 					break;
3853cde9171SAlan Somers 				case 'A': /* experimental. not in the spec */
3863cde9171SAlan Somers #ifdef NI_NODEADDR_FLAG_ANYCAST
3873cde9171SAlan Somers 					naflags |= NI_NODEADDR_FLAG_ANYCAST;
3883cde9171SAlan Somers 					break;
3893cde9171SAlan Somers #else
3903cde9171SAlan Somers 					errx(1,
3913cde9171SAlan Somers "-a A is not supported on the platform");
3923cde9171SAlan Somers 					/*NOTREACHED*/
3933cde9171SAlan Somers #endif
3943cde9171SAlan Somers 				default:
3953cde9171SAlan Somers 					usage();
3963cde9171SAlan Somers 					/*NOTREACHED*/
3973cde9171SAlan Somers 				}
3983cde9171SAlan Somers 			}
3993cde9171SAlan Somers 			break;
4003cde9171SAlan Somers 		}
4013cde9171SAlan Somers 		case 'b':
4023cde9171SAlan Somers #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
4033cde9171SAlan Somers 			errno = 0;
4043cde9171SAlan Somers 			e = NULL;
4053cde9171SAlan Somers 			lsockbufsize = strtoul(optarg, &e, 10);
4063cde9171SAlan Somers 			sockbufsize = (int)lsockbufsize;
4073cde9171SAlan Somers 			if (errno || !*optarg || *e ||
4083cde9171SAlan Somers 			    lsockbufsize > INT_MAX)
4093cde9171SAlan Somers 				errx(1, "invalid socket buffer size");
4103cde9171SAlan Somers #else
4113cde9171SAlan Somers 			errx(1,
4123cde9171SAlan Somers "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
4133cde9171SAlan Somers #endif
4143cde9171SAlan Somers 			break;
4153cde9171SAlan Somers 		case 'C':		/* vlan priority code point */
4163cde9171SAlan Somers 			pcp = strtol(optarg, &e, 10);
4173cde9171SAlan Somers 			if (*optarg == '\0' || *e != '\0')
4183cde9171SAlan Somers 				errx(1, "illegal vlan pcp %s", optarg);
4193cde9171SAlan Somers 			if (7 < pcp || pcp < -1)
4203cde9171SAlan Somers 				errx(1, "illegal vlan pcp -- %s", optarg);
4213cde9171SAlan Somers 			break;
4223cde9171SAlan Somers 		case 'c':
4233cde9171SAlan Somers 			npackets = strtol(optarg, &e, 10);
4243cde9171SAlan Somers 			if (npackets <= 0 || *optarg == '\0' || *e != '\0')
4253cde9171SAlan Somers 				errx(1,
4263cde9171SAlan Somers 				    "illegal number of packets -- %s", optarg);
4273cde9171SAlan Somers 			break;
4283cde9171SAlan Somers 		case 'D':
4293cde9171SAlan Somers 			options |= F_DONTFRAG;
4303cde9171SAlan Somers 			break;
4313cde9171SAlan Somers 		case 'd':
4323cde9171SAlan Somers 			options |= F_SO_DEBUG;
4333cde9171SAlan Somers 			break;
4343cde9171SAlan Somers 		case 'f':
4353cde9171SAlan Somers 			if (getuid()) {
4363cde9171SAlan Somers 				errno = EPERM;
4373cde9171SAlan Somers 				errx(1, "Must be superuser to flood ping");
4383cde9171SAlan Somers 			}
4393cde9171SAlan Somers 			options |= F_FLOOD;
4403cde9171SAlan Somers 			setbuf(stdout, (char *)NULL);
4413cde9171SAlan Somers 			break;
4423cde9171SAlan Somers 		case 'e':
4433cde9171SAlan Somers 			gateway = optarg;
4443cde9171SAlan Somers 			break;
4453cde9171SAlan Somers 		case 'H':
4463cde9171SAlan Somers 			options |= F_HOSTNAME;
4473cde9171SAlan Somers 			break;
4483cde9171SAlan Somers 		case 'm':		/* hoplimit */
4493cde9171SAlan Somers 			hoplimit = strtol(optarg, &e, 10);
4503cde9171SAlan Somers 			if (*optarg == '\0' || *e != '\0')
4513cde9171SAlan Somers 				errx(1, "illegal hoplimit %s", optarg);
4523cde9171SAlan Somers 			if (255 < hoplimit || hoplimit < -1)
4533cde9171SAlan Somers 				errx(1,
4543cde9171SAlan Somers 				    "illegal hoplimit -- %s", optarg);
4553cde9171SAlan Somers 			break;
4563cde9171SAlan Somers 		case 'I':
4573cde9171SAlan Somers 			ifname = optarg;
4583cde9171SAlan Somers 			options |= F_INTERFACE;
4593cde9171SAlan Somers #ifndef USE_SIN6_SCOPE_ID
4603cde9171SAlan Somers 			usepktinfo++;
4613cde9171SAlan Somers #endif
4623cde9171SAlan Somers 			break;
4633cde9171SAlan Somers 		case 'i':		/* wait between sending packets */
4643cde9171SAlan Somers 			t = strtod(optarg, &e);
4653cde9171SAlan Somers 			if (*optarg == '\0' || *e != '\0')
4663cde9171SAlan Somers 				errx(1, "illegal timing interval %s", optarg);
4673cde9171SAlan Somers 			if (t < 1 && getuid()) {
4683cde9171SAlan Somers 				errx(1, "%s: only root may use interval < 1s",
4693cde9171SAlan Somers 				    strerror(EPERM));
4703cde9171SAlan Somers 			}
4713cde9171SAlan Somers 			intvl.tv_sec = (time_t)t;
4723cde9171SAlan Somers 			intvl.tv_nsec =
4733cde9171SAlan Somers 			    (long)((t - intvl.tv_sec) * 1000000000);
4743cde9171SAlan Somers 			if (intvl.tv_sec < 0)
4753cde9171SAlan Somers 				errx(1, "illegal timing interval %s", optarg);
4763cde9171SAlan Somers 			/* less than 1/hz does not make sense */
4773cde9171SAlan Somers 			if (intvl.tv_sec == 0 && intvl.tv_nsec < 1000) {
4783cde9171SAlan Somers 				warnx("too small interval, raised to .000001");
4793cde9171SAlan Somers 				intvl.tv_nsec = 1000;
4803cde9171SAlan Somers 			}
4813cde9171SAlan Somers 			options |= F_INTERVAL;
4823cde9171SAlan Somers 			break;
4833cde9171SAlan Somers 		case 'l':
4843cde9171SAlan Somers 			if (getuid()) {
4853cde9171SAlan Somers 				errno = EPERM;
4863cde9171SAlan Somers 				errx(1, "Must be superuser to preload");
4873cde9171SAlan Somers 			}
4883cde9171SAlan Somers 			preload = strtol(optarg, &e, 10);
4893cde9171SAlan Somers 			if (preload < 0 || *optarg == '\0' || *e != '\0')
4903cde9171SAlan Somers 				errx(1, "illegal preload value -- %s", optarg);
4913cde9171SAlan Somers 			break;
4923cde9171SAlan Somers 		case 'u':
4933cde9171SAlan Somers #ifdef IPV6_USE_MIN_MTU
4943cde9171SAlan Somers 			mflag++;
4953cde9171SAlan Somers 			break;
4963cde9171SAlan Somers #else
4973cde9171SAlan Somers 			errx(1, "-%c is not supported on this platform", ch);
4983cde9171SAlan Somers 			/*NOTREACHED*/
4993cde9171SAlan Somers #endif
5003cde9171SAlan Somers 		case 'n':
5013cde9171SAlan Somers 			options &= ~F_HOSTNAME;
5023cde9171SAlan Somers 			break;
5033cde9171SAlan Somers 		case 'N':
5043cde9171SAlan Somers 			options |= F_NIGROUP;
5053cde9171SAlan Somers 			nig_oldmcprefix++;
5063cde9171SAlan Somers 			break;
5073cde9171SAlan Somers 		case 'o':
5083cde9171SAlan Somers 			options |= F_ONCE;
5093cde9171SAlan Somers 			break;
5103cde9171SAlan Somers 		case 'p':		/* fill buffer with user pattern */
5113cde9171SAlan Somers 			options |= F_PINGFILLED;
5123cde9171SAlan Somers 			fill((char *)datap, optarg);
5133cde9171SAlan Somers 				break;
5143cde9171SAlan Somers 		case 'q':
5153cde9171SAlan Somers 			options |= F_QUIET;
5163cde9171SAlan Somers 			break;
5173cde9171SAlan Somers 		case 'a':
5183cde9171SAlan Somers 			options |= F_AUDIBLE;
5193cde9171SAlan Somers 			break;
5203cde9171SAlan Somers 		case 'A':
5213cde9171SAlan Somers 			options |= F_MISSED;
5223cde9171SAlan Somers 			break;
5233cde9171SAlan Somers 		case 'S':
5243cde9171SAlan Somers 			memset(&hints, 0, sizeof(struct addrinfo));
5253cde9171SAlan Somers 			hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
5263cde9171SAlan Somers 			hints.ai_family = AF_INET6;
5273cde9171SAlan Somers 			hints.ai_socktype = SOCK_RAW;
5283cde9171SAlan Somers 			hints.ai_protocol = IPPROTO_ICMPV6;
5293cde9171SAlan Somers 
5303cde9171SAlan Somers 			error = cap_getaddrinfo(capdns, optarg, NULL, &hints, &res);
5313cde9171SAlan Somers 			if (error) {
5323cde9171SAlan Somers 				errx(1, "invalid source address: %s",
5333cde9171SAlan Somers 				     gai_strerror(error));
5343cde9171SAlan Somers 			}
5353cde9171SAlan Somers 			/*
5363cde9171SAlan Somers 			 * res->ai_family must be AF_INET6 and res->ai_addrlen
5373cde9171SAlan Somers 			 * must be sizeof(src).
5383cde9171SAlan Somers 			 */
5393cde9171SAlan Somers 			memcpy(&src, res->ai_addr, res->ai_addrlen);
5403cde9171SAlan Somers 			srclen = res->ai_addrlen;
5413cde9171SAlan Somers 			freeaddrinfo(res);
5423cde9171SAlan Somers 			options |= F_SRCADDR;
5433cde9171SAlan Somers 			break;
5443cde9171SAlan Somers 		case 's':		/* size of packet to send */
5453cde9171SAlan Somers 			datalen = strtol(optarg, &e, 10);
5463cde9171SAlan Somers 			if (datalen <= 0 || *optarg == '\0' || *e != '\0')
5473cde9171SAlan Somers 				errx(1, "illegal datalen value -- %s", optarg);
5483cde9171SAlan Somers 			if (datalen > MAXDATALEN) {
5493cde9171SAlan Somers 				errx(1,
5503cde9171SAlan Somers 				    "datalen value too large, maximum is %d",
5513cde9171SAlan Somers 				    MAXDATALEN);
5523cde9171SAlan Somers 			}
5533cde9171SAlan Somers 			break;
5543cde9171SAlan Somers 		case 'O':
5553cde9171SAlan Somers 			options &= ~F_NOUSERDATA;
5563cde9171SAlan Somers 			options |= F_SUPTYPES;
5573cde9171SAlan Somers 			break;
5583cde9171SAlan Somers 		case 'v':
5593cde9171SAlan Somers 			options |= F_VERBOSE;
5603cde9171SAlan Somers 			break;
5613cde9171SAlan Somers 		case 'y':
5623cde9171SAlan Somers 			options &= ~F_NOUSERDATA;
5633cde9171SAlan Somers 			options |= F_FQDN;
5643cde9171SAlan Somers 			break;
5653cde9171SAlan Somers 		case 'Y':
5663cde9171SAlan Somers 			options &= ~F_NOUSERDATA;
5673cde9171SAlan Somers 			options |= F_FQDNOLD;
5683cde9171SAlan Somers 			break;
5693cde9171SAlan Somers 		case 'W':
5703cde9171SAlan Somers 			t = strtod(optarg, &e);
5713cde9171SAlan Somers 			if (*e || e == optarg || t > (double)INT_MAX)
5723cde9171SAlan Somers 				err(EX_USAGE, "invalid timing interval: `%s'",
5733cde9171SAlan Somers 				    optarg);
5743cde9171SAlan Somers 			options |= F_WAITTIME;
5753cde9171SAlan Somers 			waittime = (int)t;
5763cde9171SAlan Somers 			break;
5773cde9171SAlan Somers 		case 't':
5783cde9171SAlan Somers 			alarmtimeout = strtoul(optarg, &e, 0);
5793cde9171SAlan Somers 			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
5803cde9171SAlan Somers 				errx(EX_USAGE, "invalid timeout: `%s'",
5813cde9171SAlan Somers 				    optarg);
5823cde9171SAlan Somers 			if (alarmtimeout > MAXALARM)
5833cde9171SAlan Somers 				errx(EX_USAGE, "invalid timeout: `%s' > %d",
5843cde9171SAlan Somers 				    optarg, MAXALARM);
5853cde9171SAlan Somers 			{
5863cde9171SAlan Somers 				struct itimerval itv;
5873cde9171SAlan Somers 
5883cde9171SAlan Somers 				timerclear(&itv.it_interval);
5893cde9171SAlan Somers 				timerclear(&itv.it_value);
5903cde9171SAlan Somers 				itv.it_value.tv_sec = (time_t)alarmtimeout;
5913cde9171SAlan Somers 				if (setitimer(ITIMER_REAL, &itv, NULL) != 0)
5923cde9171SAlan Somers 					err(1, "setitimer");
5933cde9171SAlan Somers 			}
5943cde9171SAlan Somers 			break;
5953cde9171SAlan Somers 		case 'z':		/* traffic class */
5963cde9171SAlan Somers 			tclass = strtol(optarg, &e, 10);
5973cde9171SAlan Somers 			if (*optarg == '\0' || *e != '\0')
5983cde9171SAlan Somers 				errx(1, "illegal traffic class %s", optarg);
5993cde9171SAlan Somers 			if (255 < tclass || tclass < -1)
6003cde9171SAlan Somers 				errx(1,
6013cde9171SAlan Somers 				    "illegal traffic class -- %s", optarg);
6023cde9171SAlan Somers 			break;
6033cde9171SAlan Somers #ifdef IPSEC
6043cde9171SAlan Somers #ifdef IPSEC_POLICY_IPSEC
6053cde9171SAlan Somers 		case 'P':
6063cde9171SAlan Somers 			options |= F_POLICY;
6073cde9171SAlan Somers 			if (!strncmp("in", optarg, 2)) {
6083cde9171SAlan Somers 				if ((policy_in = strdup(optarg)) == NULL)
6093cde9171SAlan Somers 					errx(1, "strdup");
6103cde9171SAlan Somers 			} else if (!strncmp("out", optarg, 3)) {
6113cde9171SAlan Somers 				if ((policy_out = strdup(optarg)) == NULL)
6123cde9171SAlan Somers 					errx(1, "strdup");
6133cde9171SAlan Somers 			} else
6143cde9171SAlan Somers 				errx(1, "invalid security policy");
6153cde9171SAlan Somers 			break;
6163cde9171SAlan Somers #else
6173cde9171SAlan Somers 		case 'Z':
6183cde9171SAlan Somers 			options |= F_AUTHHDR;
6193cde9171SAlan Somers 			break;
6203cde9171SAlan Somers 		case 'E':
6213cde9171SAlan Somers 			options |= F_ENCRYPT;
6223cde9171SAlan Somers 			break;
6233cde9171SAlan Somers #endif /*IPSEC_POLICY_IPSEC*/
6243cde9171SAlan Somers #endif /*IPSEC*/
6253cde9171SAlan Somers 		default:
6263cde9171SAlan Somers 			usage();
6273cde9171SAlan Somers 			/*NOTREACHED*/
6283cde9171SAlan Somers 		}
6293cde9171SAlan Somers 	}
6303cde9171SAlan Somers 
6313cde9171SAlan Somers 	argc -= optind;
6323cde9171SAlan Somers 	argv += optind;
6333cde9171SAlan Somers 
6343cde9171SAlan Somers 	if (argc < 1) {
6353cde9171SAlan Somers 		usage();
6363cde9171SAlan Somers 		/*NOTREACHED*/
6373cde9171SAlan Somers 	}
6383cde9171SAlan Somers 
6393cde9171SAlan Somers 	if (argc > 1) {
6403cde9171SAlan Somers #ifdef IPV6_RECVRTHDR	/* 2292bis */
6413cde9171SAlan Somers 		rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
6423cde9171SAlan Somers 		    argc - 1));
6433cde9171SAlan Somers #else  /* RFC2292 */
6443cde9171SAlan Somers 		rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
6453cde9171SAlan Somers #endif
6463cde9171SAlan Somers 		if (rthlen == 0) {
6473cde9171SAlan Somers 			errx(1, "too many intermediate hops");
6483cde9171SAlan Somers 			/*NOTREACHED*/
6493cde9171SAlan Somers 		}
6503cde9171SAlan Somers 		ip6optlen += rthlen;
6513cde9171SAlan Somers 	}
6523cde9171SAlan Somers 
6533cde9171SAlan Somers 	if (options & F_NIGROUP) {
6543cde9171SAlan Somers 		target = nigroup(argv[argc - 1], nig_oldmcprefix);
6553cde9171SAlan Somers 		if (target == NULL) {
6563cde9171SAlan Somers 			usage();
6573cde9171SAlan Somers 			/*NOTREACHED*/
6583cde9171SAlan Somers 		}
6593cde9171SAlan Somers 	} else
6603cde9171SAlan Somers 		target = argv[argc - 1];
6613cde9171SAlan Somers 
6623cde9171SAlan Somers 	/* cap_getaddrinfo */
6633cde9171SAlan Somers 	memset(&hints, 0, sizeof(struct addrinfo));
6643cde9171SAlan Somers 	hints.ai_flags = AI_CANONNAME;
6653cde9171SAlan Somers 	hints.ai_family = AF_INET6;
6663cde9171SAlan Somers 	hints.ai_socktype = SOCK_RAW;
6673cde9171SAlan Somers 	hints.ai_protocol = IPPROTO_ICMPV6;
6683cde9171SAlan Somers 
6693cde9171SAlan Somers 	error = cap_getaddrinfo(capdns, target, NULL, &hints, &res);
6703cde9171SAlan Somers 	if (error)
6713cde9171SAlan Somers 		errx(1, "%s", gai_strerror(error));
6723cde9171SAlan Somers 	if (res->ai_canonname)
6733cde9171SAlan Somers 		hostname = strdup(res->ai_canonname);
6743cde9171SAlan Somers 	else
6753cde9171SAlan Somers 		hostname = target;
6763cde9171SAlan Somers 
6773cde9171SAlan Somers 	if (!res->ai_addr)
6783cde9171SAlan Somers 		errx(1, "cap_getaddrinfo failed");
6793cde9171SAlan Somers 
6803cde9171SAlan Somers 	(void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
6813cde9171SAlan Somers 
6823cde9171SAlan Somers 	if ((ssend = socket(res->ai_family, res->ai_socktype,
6833cde9171SAlan Somers 	    res->ai_protocol)) < 0)
6843cde9171SAlan Somers 		err(1, "socket ssend");
6853cde9171SAlan Somers 	if ((srecv = socket(res->ai_family, res->ai_socktype,
6863cde9171SAlan Somers 	    res->ai_protocol)) < 0)
6873cde9171SAlan Somers 		err(1, "socket srecv");
6883cde9171SAlan Somers 	freeaddrinfo(res);
6893cde9171SAlan Somers 
6903cde9171SAlan Somers 	/* set the source address if specified. */
6913cde9171SAlan Somers 	if ((options & F_SRCADDR) != 0) {
6923cde9171SAlan Somers 		/* properly fill sin6_scope_id */
6933cde9171SAlan Somers 		if (IN6_IS_ADDR_LINKLOCAL(&src.sin6_addr) && (
6943cde9171SAlan Somers 		    IN6_IS_ADDR_LINKLOCAL(&dst.sin6_addr) ||
6953cde9171SAlan Somers 		    IN6_IS_ADDR_MC_LINKLOCAL(&dst.sin6_addr) ||
6963cde9171SAlan Somers 		    IN6_IS_ADDR_MC_NODELOCAL(&dst.sin6_addr))) {
6973cde9171SAlan Somers 			if (src.sin6_scope_id == 0)
6983cde9171SAlan Somers 				src.sin6_scope_id = dst.sin6_scope_id;
6993cde9171SAlan Somers 			if (dst.sin6_scope_id == 0)
7003cde9171SAlan Somers 				dst.sin6_scope_id = src.sin6_scope_id;
7013cde9171SAlan Somers 		}
7023cde9171SAlan Somers 		if (bind(ssend, (struct sockaddr *)&src, srclen) != 0)
7033cde9171SAlan Somers 			err(1, "bind");
7043cde9171SAlan Somers 	}
7053cde9171SAlan Somers 	/* set the gateway (next hop) if specified */
7063cde9171SAlan Somers 	if (gateway) {
7073cde9171SAlan Somers 		memset(&hints, 0, sizeof(hints));
7083cde9171SAlan Somers 		hints.ai_family = AF_INET6;
7093cde9171SAlan Somers 		hints.ai_socktype = SOCK_RAW;
7103cde9171SAlan Somers 		hints.ai_protocol = IPPROTO_ICMPV6;
7113cde9171SAlan Somers 
7123cde9171SAlan Somers 		error = cap_getaddrinfo(capdns, gateway, NULL, &hints, &res);
7133cde9171SAlan Somers 		if (error) {
7143cde9171SAlan Somers 			errx(1, "cap_getaddrinfo for the gateway %s: %s",
7153cde9171SAlan Somers 			     gateway, gai_strerror(error));
7163cde9171SAlan Somers 		}
7173cde9171SAlan Somers 		if (res->ai_next && (options & F_VERBOSE))
7183cde9171SAlan Somers 			warnx("gateway resolves to multiple addresses");
7193cde9171SAlan Somers 
7203cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_NEXTHOP,
7213cde9171SAlan Somers 		    res->ai_addr, res->ai_addrlen)) {
7223cde9171SAlan Somers 			err(1, "setsockopt(IPV6_NEXTHOP)");
7233cde9171SAlan Somers 		}
7243cde9171SAlan Somers 
7253cde9171SAlan Somers 		freeaddrinfo(res);
7263cde9171SAlan Somers 	}
7273cde9171SAlan Somers 
7283cde9171SAlan Somers 	/*
7293cde9171SAlan Somers 	 * let the kerel pass extension headers of incoming packets,
7303cde9171SAlan Somers 	 * for privileged socket options
7313cde9171SAlan Somers 	 */
7323cde9171SAlan Somers 	if ((options & F_VERBOSE) != 0) {
7333cde9171SAlan Somers 		int opton = 1;
7343cde9171SAlan Somers 
7353cde9171SAlan Somers #ifdef IPV6_RECVHOPOPTS
7363cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
7373cde9171SAlan Somers 		    sizeof(opton)))
7383cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RECVHOPOPTS)");
7393cde9171SAlan Somers #else  /* old adv. API */
7403cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
7413cde9171SAlan Somers 		    sizeof(opton)))
7423cde9171SAlan Somers 			err(1, "setsockopt(IPV6_HOPOPTS)");
7433cde9171SAlan Somers #endif
7443cde9171SAlan Somers #ifdef IPV6_RECVDSTOPTS
7453cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
7463cde9171SAlan Somers 		    sizeof(opton)))
7473cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RECVDSTOPTS)");
7483cde9171SAlan Somers #else  /* old adv. API */
7493cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
7503cde9171SAlan Somers 		    sizeof(opton)))
7513cde9171SAlan Somers 			err(1, "setsockopt(IPV6_DSTOPTS)");
7523cde9171SAlan Somers #endif
7533cde9171SAlan Somers #ifdef IPV6_RECVRTHDRDSTOPTS
7543cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
7553cde9171SAlan Somers 		    sizeof(opton)))
7563cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
7573cde9171SAlan Somers #endif
7583cde9171SAlan Somers 	}
7593cde9171SAlan Somers 
7603cde9171SAlan Somers 	/* revoke root privilege */
7613cde9171SAlan Somers 	if (seteuid(getuid()) != 0)
7623cde9171SAlan Somers 		err(1, "seteuid() failed");
7633cde9171SAlan Somers 	if (setuid(getuid()) != 0)
7643cde9171SAlan Somers 		err(1, "setuid() failed");
7653cde9171SAlan Somers 
7663cde9171SAlan Somers 	if ((options & F_FLOOD) && (options & F_INTERVAL))
7673cde9171SAlan Somers 		errx(1, "-f and -i incompatible options");
7683cde9171SAlan Somers 
7693cde9171SAlan Somers 	if ((options & F_NOUSERDATA) == 0) {
7703cde9171SAlan Somers 		if (datalen >= sizeof(struct tv32)) {
7713cde9171SAlan Somers 			/* we can time transfer */
7723cde9171SAlan Somers 			timing = 1;
7733cde9171SAlan Somers 		} else
7743cde9171SAlan Somers 			timing = 0;
7753cde9171SAlan Somers 		/* in F_VERBOSE case, we may get non-echoreply packets*/
7763cde9171SAlan Somers 		if (options & F_VERBOSE)
7773cde9171SAlan Somers 			packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
7783cde9171SAlan Somers 		else
7793cde9171SAlan Somers 			packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
7803cde9171SAlan Somers 	} else {
7813cde9171SAlan Somers 		/* suppress timing for node information query */
7823cde9171SAlan Somers 		timing = 0;
7833cde9171SAlan Somers 		datalen = 2048;
7843cde9171SAlan Somers 		packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
7853cde9171SAlan Somers 	}
7863cde9171SAlan Somers 
7873cde9171SAlan Somers 	if (!(packet = (u_char *)malloc((u_int)packlen)))
7883cde9171SAlan Somers 		err(1, "Unable to allocate packet");
7893cde9171SAlan Somers 	if (!(options & F_PINGFILLED))
7903cde9171SAlan Somers 		for (i = ICMP6ECHOLEN; i < packlen; ++i)
7913cde9171SAlan Somers 			*datap++ = i;
7923cde9171SAlan Somers 
7933cde9171SAlan Somers 	ident = getpid() & 0xFFFF;
7943cde9171SAlan Somers 	arc4random_buf(nonce, sizeof(nonce));
7953cde9171SAlan Somers 	optval = 1;
7963cde9171SAlan Somers 	if (options & F_DONTFRAG)
7973cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_DONTFRAG,
7983cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
7993cde9171SAlan Somers 			err(1, "IPV6_DONTFRAG");
8003cde9171SAlan Somers 	hold = 1;
8013cde9171SAlan Somers 
8023cde9171SAlan Somers 	if (options & F_SO_DEBUG) {
8033cde9171SAlan Somers 		(void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold,
8043cde9171SAlan Somers 		    sizeof(hold));
8053cde9171SAlan Somers 		(void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold,
8063cde9171SAlan Somers 		    sizeof(hold));
8073cde9171SAlan Somers 	}
8083cde9171SAlan Somers 	optval = IPV6_DEFHLIM;
8093cde9171SAlan Somers 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
8103cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
8113cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8123cde9171SAlan Somers 			err(1, "IPV6_MULTICAST_HOPS");
8133cde9171SAlan Somers #ifdef IPV6_USE_MIN_MTU
8143cde9171SAlan Somers 	if (mflag != 1) {
8153cde9171SAlan Somers 		optval = mflag > 1 ? 0 : 1;
8163cde9171SAlan Somers 
8173cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
8183cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8193cde9171SAlan Somers 			err(1, "setsockopt(IPV6_USE_MIN_MTU)");
8203cde9171SAlan Somers 	}
8213cde9171SAlan Somers #ifdef IPV6_RECVPATHMTU
8223cde9171SAlan Somers 	else {
8233cde9171SAlan Somers 		optval = 1;
8243cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVPATHMTU,
8253cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8263cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RECVPATHMTU)");
8273cde9171SAlan Somers 	}
8283cde9171SAlan Somers #endif /* IPV6_RECVPATHMTU */
8293cde9171SAlan Somers #endif /* IPV6_USE_MIN_MTU */
8303cde9171SAlan Somers 
8313cde9171SAlan Somers #ifdef IPSEC
8323cde9171SAlan Somers #ifdef IPSEC_POLICY_IPSEC
8333cde9171SAlan Somers 	if (options & F_POLICY) {
8343cde9171SAlan Somers 		if (setpolicy(srecv, policy_in) < 0)
8353cde9171SAlan Somers 			errx(1, "%s", ipsec_strerror());
8363cde9171SAlan Somers 		if (setpolicy(ssend, policy_out) < 0)
8373cde9171SAlan Somers 			errx(1, "%s", ipsec_strerror());
8383cde9171SAlan Somers 	}
8393cde9171SAlan Somers #else
8403cde9171SAlan Somers 	if (options & F_AUTHHDR) {
8413cde9171SAlan Somers 		optval = IPSEC_LEVEL_REQUIRE;
8423cde9171SAlan Somers #ifdef IPV6_AUTH_TRANS_LEVEL
8433cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
8443cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8453cde9171SAlan Somers 			err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
8463cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
8473cde9171SAlan Somers 		     &optval, sizeof(optval)) == -1)
8483cde9171SAlan Somers 			err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
8493cde9171SAlan Somers #else /* old def */
8503cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
8513cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8523cde9171SAlan Somers 			err(1, "setsockopt(IPV6_AUTH_LEVEL)");
8533cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
8543cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8553cde9171SAlan Somers 			err(1, "setsockopt(IPV6_AUTH_LEVEL)");
8563cde9171SAlan Somers #endif
8573cde9171SAlan Somers 	}
8583cde9171SAlan Somers 	if (options & F_ENCRYPT) {
8593cde9171SAlan Somers 		optval = IPSEC_LEVEL_REQUIRE;
8603cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
8613cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8623cde9171SAlan Somers 			err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
8633cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
8643cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
8653cde9171SAlan Somers 			err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
8663cde9171SAlan Somers 	}
8673cde9171SAlan Somers #endif /*IPSEC_POLICY_IPSEC*/
8683cde9171SAlan Somers #endif
8693cde9171SAlan Somers 
8703cde9171SAlan Somers #ifdef ICMP6_FILTER
8713cde9171SAlan Somers     {
8723cde9171SAlan Somers 	struct icmp6_filter filt;
8733cde9171SAlan Somers 	if (!(options & F_VERBOSE)) {
8743cde9171SAlan Somers 		ICMP6_FILTER_SETBLOCKALL(&filt);
8753cde9171SAlan Somers 		if ((options & F_FQDN) || (options & F_FQDNOLD) ||
8763cde9171SAlan Somers 		    (options & F_NODEADDR) || (options & F_SUPTYPES))
8773cde9171SAlan Somers 			ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
8783cde9171SAlan Somers 		else
8793cde9171SAlan Somers 			ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
8803cde9171SAlan Somers 	} else {
8813cde9171SAlan Somers 		ICMP6_FILTER_SETPASSALL(&filt);
8823cde9171SAlan Somers 	}
8833cde9171SAlan Somers 	if (setsockopt(srecv, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
8843cde9171SAlan Somers 	    sizeof(filt)) < 0)
8853cde9171SAlan Somers 		err(1, "setsockopt(ICMP6_FILTER)");
8863cde9171SAlan Somers     }
8873cde9171SAlan Somers #endif /*ICMP6_FILTER*/
8883cde9171SAlan Somers 
8893cde9171SAlan Somers 	/* let the kerel pass extension headers of incoming packets */
8903cde9171SAlan Somers 	if ((options & F_VERBOSE) != 0) {
8913cde9171SAlan Somers 		int opton = 1;
8923cde9171SAlan Somers 
8933cde9171SAlan Somers #ifdef IPV6_RECVRTHDR
8943cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
8953cde9171SAlan Somers 		    sizeof(opton)))
8963cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RECVRTHDR)");
8973cde9171SAlan Somers #else  /* old adv. API */
8983cde9171SAlan Somers 		if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RTHDR, &opton,
8993cde9171SAlan Somers 		    sizeof(opton)))
9003cde9171SAlan Somers 			err(1, "setsockopt(IPV6_RTHDR)");
9013cde9171SAlan Somers #endif
9023cde9171SAlan Somers 	}
9033cde9171SAlan Somers 
9043cde9171SAlan Somers /*
9053cde9171SAlan Somers 	optval = 1;
9063cde9171SAlan Somers 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
9073cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
9083cde9171SAlan Somers 		    &optval, sizeof(optval)) == -1)
9093cde9171SAlan Somers 			err(1, "IPV6_MULTICAST_LOOP");
9103cde9171SAlan Somers */
9113cde9171SAlan Somers 
9123cde9171SAlan Somers 	/* Specify the outgoing interface and/or the source address */
9133cde9171SAlan Somers 	if (usepktinfo)
9143cde9171SAlan Somers 		ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
9153cde9171SAlan Somers 
9163cde9171SAlan Somers 	if (hoplimit != -1)
9173cde9171SAlan Somers 		ip6optlen += CMSG_SPACE(sizeof(int));
9183cde9171SAlan Somers 
9193cde9171SAlan Somers 	/* set IP6 packet options */
9203cde9171SAlan Somers 	if (ip6optlen) {
9213cde9171SAlan Somers 		if ((scmsg = (char *)malloc(ip6optlen)) == NULL)
9223cde9171SAlan Somers 			errx(1, "can't allocate enough memory");
9233cde9171SAlan Somers 		smsghdr.msg_control = (caddr_t)scmsg;
9243cde9171SAlan Somers 		smsghdr.msg_controllen = ip6optlen;
9253cde9171SAlan Somers 		scmsgp = CMSG_FIRSTHDR(&smsghdr);
9263cde9171SAlan Somers 	}
9273cde9171SAlan Somers 	if (usepktinfo) {
9283cde9171SAlan Somers 		cmsg_pktinfo = CMSG_DATA(scmsgp);
9293cde9171SAlan Somers 		scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
9303cde9171SAlan Somers 		scmsgp->cmsg_level = IPPROTO_IPV6;
9313cde9171SAlan Somers 		scmsgp->cmsg_type = IPV6_PKTINFO;
9323cde9171SAlan Somers 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
9333cde9171SAlan Somers 	}
9343cde9171SAlan Somers 
9353cde9171SAlan Somers 	/* set the outgoing interface */
9363cde9171SAlan Somers 	if (ifname) {
9373cde9171SAlan Somers #ifndef USE_SIN6_SCOPE_ID
9383cde9171SAlan Somers 		/* pktinfo must have already been allocated */
9393cde9171SAlan Somers 		if ((pktinfo.ipi6_ifindex = if_nametoindex(ifname)) == 0)
9403cde9171SAlan Somers 			errx(1, "%s: invalid interface name", ifname);
9413cde9171SAlan Somers #else
9423cde9171SAlan Somers 		if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
9433cde9171SAlan Somers 			errx(1, "%s: invalid interface name", ifname);
9443cde9171SAlan Somers #endif
9453cde9171SAlan Somers 	}
9463cde9171SAlan Somers 	if (hoplimit != -1) {
9473cde9171SAlan Somers 		scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
9483cde9171SAlan Somers 		scmsgp->cmsg_level = IPPROTO_IPV6;
9493cde9171SAlan Somers 		scmsgp->cmsg_type = IPV6_HOPLIMIT;
9503cde9171SAlan Somers 		memcpy(CMSG_DATA(scmsgp), &hoplimit, sizeof(hoplimit));
9513cde9171SAlan Somers 
9523cde9171SAlan Somers 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
9533cde9171SAlan Somers 	}
9543cde9171SAlan Somers 
9553cde9171SAlan Somers 	if (tclass != -1) {
9563cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_TCLASS,
9573cde9171SAlan Somers 		    &tclass, sizeof(tclass)) == -1)
9583cde9171SAlan Somers 			err(1, "setsockopt(IPV6_TCLASS)");
9593cde9171SAlan Somers 	}
9603cde9171SAlan Somers 
9613cde9171SAlan Somers 	if (pcp != -2) {
9623cde9171SAlan Somers 		if (setsockopt(ssend, IPPROTO_IPV6, IPV6_VLAN_PCP,
9633cde9171SAlan Somers 		    &pcp, sizeof(pcp)) == -1)
9643cde9171SAlan Somers 			err(1, "setsockopt(IPV6_VLAN_PCP)");
9653cde9171SAlan Somers 	}
9663cde9171SAlan Somers 
9673cde9171SAlan Somers 	if (argc > 1) {	/* some intermediate addrs are specified */
9683cde9171SAlan Somers 		int hops;
9693cde9171SAlan Somers 		int rthdrlen;
9703cde9171SAlan Somers 
9713cde9171SAlan Somers 		rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
9723cde9171SAlan Somers 		scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
9733cde9171SAlan Somers 		scmsgp->cmsg_level = IPPROTO_IPV6;
9743cde9171SAlan Somers 		scmsgp->cmsg_type = IPV6_RTHDR;
9753cde9171SAlan Somers 		rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
9763cde9171SAlan Somers 		rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
9773cde9171SAlan Somers 		    IPV6_RTHDR_TYPE_0, argc - 1);
9783cde9171SAlan Somers 		if (rthdr == NULL)
9793cde9171SAlan Somers 			errx(1, "can't initialize rthdr");
9803cde9171SAlan Somers 
9813cde9171SAlan Somers 		for (hops = 0; hops < argc - 1; hops++) {
9823cde9171SAlan Somers 			memset(&hints, 0, sizeof(hints));
9833cde9171SAlan Somers 			hints.ai_family = AF_INET6;
9843cde9171SAlan Somers 
9853cde9171SAlan Somers 			if ((error = cap_getaddrinfo(capdns, argv[hops], NULL, &hints,
9863cde9171SAlan Somers 			    &res)))
9873cde9171SAlan Somers 				errx(1, "%s", gai_strerror(error));
9883cde9171SAlan Somers 			if (res->ai_addr->sa_family != AF_INET6)
9893cde9171SAlan Somers 				errx(1,
9903cde9171SAlan Somers 				    "bad addr family of an intermediate addr");
9913cde9171SAlan Somers 			sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
9923cde9171SAlan Somers 			if (inet6_rth_add(rthdr, &sin6->sin6_addr))
9933cde9171SAlan Somers 				errx(1, "can't add an intermediate node");
9943cde9171SAlan Somers 			freeaddrinfo(res);
9953cde9171SAlan Somers 		}
9963cde9171SAlan Somers 
9973cde9171SAlan Somers 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
9983cde9171SAlan Somers 	}
9993cde9171SAlan Somers 
10003cde9171SAlan Somers 	/* From now on we will use only reverse DNS lookups. */
10013cde9171SAlan Somers #ifdef WITH_CASPER
10023cde9171SAlan Somers 	if (capdns != NULL) {
10033cde9171SAlan Somers 		const char *types[1];
10043cde9171SAlan Somers 
10053cde9171SAlan Somers 		types[0] = "ADDR2NAME";
10063cde9171SAlan Somers 		if (cap_dns_type_limit(capdns, types, nitems(types)) < 0)
10073cde9171SAlan Somers 			err(1, "unable to limit access to system.dns service");
10083cde9171SAlan Somers 	}
10093cde9171SAlan Somers #endif
10103cde9171SAlan Somers 	if (!(options & F_SRCADDR)) {
10113cde9171SAlan Somers 		/*
10123cde9171SAlan Somers 		 * get the source address. XXX since we revoked the root
10133cde9171SAlan Somers 		 * privilege, we cannot use a raw socket for this.
10143cde9171SAlan Somers 		 */
10153cde9171SAlan Somers 		int dummy;
10163cde9171SAlan Somers 		socklen_t len = sizeof(src);
10173cde9171SAlan Somers 
10183cde9171SAlan Somers 		if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
10193cde9171SAlan Somers 			err(1, "UDP socket");
10203cde9171SAlan Somers 
10213cde9171SAlan Somers 		src.sin6_family = AF_INET6;
10223cde9171SAlan Somers 		src.sin6_addr = dst.sin6_addr;
10233cde9171SAlan Somers 		src.sin6_port = ntohs(DUMMY_PORT);
10243cde9171SAlan Somers 		src.sin6_scope_id = dst.sin6_scope_id;
10253cde9171SAlan Somers 
10263cde9171SAlan Somers 		if (usepktinfo &&
10273cde9171SAlan Somers 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
10283cde9171SAlan Somers 		    (void *)&pktinfo, sizeof(pktinfo)))
10293cde9171SAlan Somers 			err(1, "UDP setsockopt(IPV6_PKTINFO)");
10303cde9171SAlan Somers 
10313cde9171SAlan Somers 		if (hoplimit != -1 &&
10323cde9171SAlan Somers 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
10333cde9171SAlan Somers 		    (void *)&hoplimit, sizeof(hoplimit)))
10343cde9171SAlan Somers 			err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
10353cde9171SAlan Somers 
10363cde9171SAlan Somers 		if (hoplimit != -1 &&
10373cde9171SAlan Somers 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
10383cde9171SAlan Somers 		    (void *)&hoplimit, sizeof(hoplimit)))
10393cde9171SAlan Somers 			err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
10403cde9171SAlan Somers 
10413cde9171SAlan Somers 		if (rthdr &&
10423cde9171SAlan Somers 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
10433cde9171SAlan Somers 		    (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
10443cde9171SAlan Somers 			err(1, "UDP setsockopt(IPV6_RTHDR)");
10453cde9171SAlan Somers 
10463cde9171SAlan Somers 		if (connect(dummy, (struct sockaddr *)&src, len) < 0)
10473cde9171SAlan Somers 			err(1, "UDP connect");
10483cde9171SAlan Somers 
10493cde9171SAlan Somers 		if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
10503cde9171SAlan Somers 			err(1, "getsockname");
10513cde9171SAlan Somers 
10523cde9171SAlan Somers 		close(dummy);
10533cde9171SAlan Somers 	}
10543cde9171SAlan Somers 
10553cde9171SAlan Somers 	/* Save pktinfo in the ancillary data. */
10563cde9171SAlan Somers 	if (usepktinfo)
10573cde9171SAlan Somers 		memcpy(cmsg_pktinfo, &pktinfo, sizeof(pktinfo));
10583cde9171SAlan Somers 
10593cde9171SAlan Somers 	if (connect(ssend, (struct sockaddr *)&dst, sizeof(dst)) != 0)
10603cde9171SAlan Somers 		err(1, "connect() ssend");
10613cde9171SAlan Somers 
10623cde9171SAlan Somers 	caph_cache_catpages();
10633cde9171SAlan Somers 	if (caph_enter_casper() < 0)
10643cde9171SAlan Somers 		err(1, "caph_enter_casper");
10653cde9171SAlan Somers 
10663cde9171SAlan Somers 	cap_rights_init(&rights_stdin);
10673cde9171SAlan Somers 	if (caph_rights_limit(STDIN_FILENO, &rights_stdin) < 0)
10683cde9171SAlan Somers 		err(1, "caph_rights_limit stdin");
10693cde9171SAlan Somers 	if (caph_limit_stdout() < 0)
10703cde9171SAlan Somers 		err(1, "caph_limit_stdout");
10713cde9171SAlan Somers 	if (caph_limit_stderr() < 0)
10723cde9171SAlan Somers 		err(1, "caph_limit_stderr");
10733cde9171SAlan Somers 
10743cde9171SAlan Somers 	cap_rights_init(&rights_srecv, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT);
10753cde9171SAlan Somers 	if (caph_rights_limit(srecv, &rights_srecv) < 0)
10763cde9171SAlan Somers 		err(1, "caph_rights_limit srecv");
10773cde9171SAlan Somers 	cap_rights_init(&rights_ssend, CAP_SEND, CAP_SETSOCKOPT);
10783cde9171SAlan Somers 	if (caph_rights_limit(ssend, &rights_ssend) < 0)
10793cde9171SAlan Somers 		err(1, "caph_rights_limit ssend");
10803cde9171SAlan Somers 
10813cde9171SAlan Somers #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
10823cde9171SAlan Somers 	if (sockbufsize) {
10833cde9171SAlan Somers 		if (datalen > (size_t)sockbufsize)
10843cde9171SAlan Somers 			warnx("you need -b to increase socket buffer size");
10853cde9171SAlan Somers 		if (setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
10863cde9171SAlan Somers 		    sizeof(sockbufsize)) < 0)
10873cde9171SAlan Somers 			err(1, "setsockopt(SO_SNDBUF)");
10883cde9171SAlan Somers 		if (setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
10893cde9171SAlan Somers 		    sizeof(sockbufsize)) < 0)
10903cde9171SAlan Somers 			err(1, "setsockopt(SO_RCVBUF)");
10913cde9171SAlan Somers 	}
10923cde9171SAlan Somers 	else {
10933cde9171SAlan Somers 		if (datalen > 8 * 1024)	/*XXX*/
10943cde9171SAlan Somers 			warnx("you need -b to increase socket buffer size");
10953cde9171SAlan Somers 		/*
10963cde9171SAlan Somers 		 * When pinging the broadcast address, you can get a lot of
10973cde9171SAlan Somers 		 * answers. Doing something so evil is useful if you are trying
10983cde9171SAlan Somers 		 * to stress the ethernet, or just want to fill the arp cache
10993cde9171SAlan Somers 		 * to get some stuff for /etc/ethers.
11003cde9171SAlan Somers 		 */
11013cde9171SAlan Somers 		hold = 48 * 1024;
11023cde9171SAlan Somers 		setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
11033cde9171SAlan Somers 		    sizeof(hold));
11043cde9171SAlan Somers 	}
11053cde9171SAlan Somers #endif
11063cde9171SAlan Somers 
11073cde9171SAlan Somers 	optval = 1;
11083cde9171SAlan Somers #ifndef USE_SIN6_SCOPE_ID
11093cde9171SAlan Somers #ifdef IPV6_RECVPKTINFO
11103cde9171SAlan Somers 	if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
11113cde9171SAlan Somers 	    sizeof(optval)) < 0)
11123cde9171SAlan Somers 		warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
11133cde9171SAlan Somers #else  /* old adv. API */
11143cde9171SAlan Somers 	if (setsockopt(srecv, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
11153cde9171SAlan Somers 	    sizeof(optval)) < 0)
11163cde9171SAlan Somers 		warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
11173cde9171SAlan Somers #endif
11183cde9171SAlan Somers #endif /* USE_SIN6_SCOPE_ID */
11193cde9171SAlan Somers #ifdef IPV6_RECVHOPLIMIT
11203cde9171SAlan Somers 	if (setsockopt(srecv, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
11213cde9171SAlan Somers 	    sizeof(optval)) < 0)
11223cde9171SAlan Somers 		warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
11233cde9171SAlan Somers #else  /* old adv. API */
11243cde9171SAlan Somers 	if (setsockopt(srecv, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
11253cde9171SAlan Somers 	    sizeof(optval)) < 0)
11263cde9171SAlan Somers 		warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
11273cde9171SAlan Somers #endif
11283cde9171SAlan Somers 
11293cde9171SAlan Somers 	cap_rights_clear(&rights_srecv, CAP_SETSOCKOPT);
11303cde9171SAlan Somers 	if (caph_rights_limit(srecv, &rights_srecv) < 0)
11313cde9171SAlan Somers 		err(1, "caph_rights_limit srecv setsockopt");
11323cde9171SAlan Somers 	cap_rights_clear(&rights_ssend, CAP_SETSOCKOPT);
11333cde9171SAlan Somers 	if (caph_rights_limit(ssend, &rights_ssend) < 0)
11343cde9171SAlan Somers 		err(1, "caph_rights_limit ssend setsockopt");
11353cde9171SAlan Somers 
11363cde9171SAlan Somers 	printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
11373cde9171SAlan Somers 	    (unsigned long)(pingerlen() - 8));
11383cde9171SAlan Somers 	printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
11393cde9171SAlan Somers 	printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
11403cde9171SAlan Somers 
11413cde9171SAlan Somers 	if (preload == 0)
11423cde9171SAlan Somers 		pinger();
11433cde9171SAlan Somers 	else {
11443cde9171SAlan Somers 		if (npackets != 0 && preload > npackets)
11453cde9171SAlan Somers 			preload = npackets;
11463cde9171SAlan Somers 		while (preload--)
11473cde9171SAlan Somers 			pinger();
11483cde9171SAlan Somers 	}
11493cde9171SAlan Somers 	clock_gettime(CLOCK_MONOTONIC, &last);
11503cde9171SAlan Somers 
11513cde9171SAlan Somers 	sigemptyset(&si_sa.sa_mask);
11523cde9171SAlan Somers 	si_sa.sa_flags = 0;
11533cde9171SAlan Somers 	si_sa.sa_handler = onsignal;
11543cde9171SAlan Somers 	if (sigaction(SIGINT, &si_sa, 0) == -1)
11553cde9171SAlan Somers 		err(EX_OSERR, "sigaction SIGINT");
11563cde9171SAlan Somers 	seenint = 0;
11573cde9171SAlan Somers #ifdef SIGINFO
11583cde9171SAlan Somers 	if (sigaction(SIGINFO, &si_sa, 0) == -1)
11593cde9171SAlan Somers 		err(EX_OSERR, "sigaction SIGINFO");
11603cde9171SAlan Somers 	seeninfo = 0;
11613cde9171SAlan Somers #endif
11623cde9171SAlan Somers 	if (alarmtimeout > 0) {
11633cde9171SAlan Somers 		if (sigaction(SIGALRM, &si_sa, 0) == -1)
11643cde9171SAlan Somers 			err(EX_OSERR, "sigaction SIGALRM");
11653cde9171SAlan Somers 	}
11663cde9171SAlan Somers 	if (options & F_FLOOD) {
11673cde9171SAlan Somers 		intvl.tv_sec = 0;
11683cde9171SAlan Somers 		intvl.tv_nsec = 10000000;
11693cde9171SAlan Somers 	}
11703cde9171SAlan Somers 
11713cde9171SAlan Somers 	almost_done = 0;
11723cde9171SAlan Somers 	while (seenint == 0) {
11733cde9171SAlan Somers 		struct timespec now, timeout;
11743cde9171SAlan Somers 		struct msghdr m;
11753cde9171SAlan Somers 		struct iovec iov[2];
11763cde9171SAlan Somers 		fd_set rfds;
11773cde9171SAlan Somers 		int n;
11783cde9171SAlan Somers 
11793cde9171SAlan Somers 		/* signal handling */
11803cde9171SAlan Somers 		if (seenint)
11813cde9171SAlan Somers 			onint(SIGINT);
11823cde9171SAlan Somers #ifdef SIGINFO
11833cde9171SAlan Somers 		if (seeninfo) {
11843cde9171SAlan Somers 			summary();
11853cde9171SAlan Somers 			seeninfo = 0;
11863cde9171SAlan Somers 			continue;
11873cde9171SAlan Somers 		}
11883cde9171SAlan Somers #endif
11893cde9171SAlan Somers 		FD_ZERO(&rfds);
11903cde9171SAlan Somers 		FD_SET(srecv, &rfds);
11913cde9171SAlan Somers 		clock_gettime(CLOCK_MONOTONIC, &now);
11923cde9171SAlan Somers 		timespecadd(&last, &intvl, &timeout);
11933cde9171SAlan Somers 		timespecsub(&timeout, &now, &timeout);
11943cde9171SAlan Somers 		if (timeout.tv_sec < 0)
11953cde9171SAlan Somers 			timespecclear(&timeout);
11963cde9171SAlan Somers 
11973cde9171SAlan Somers 		n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL);
11983cde9171SAlan Somers 		if (n < 0)
11993cde9171SAlan Somers 			continue;	/* EINTR */
12003cde9171SAlan Somers 		if (n == 1) {
12013cde9171SAlan Somers 			m.msg_name = (caddr_t)&from;
12023cde9171SAlan Somers 			m.msg_namelen = sizeof(from);
12033cde9171SAlan Somers 			memset(&iov, 0, sizeof(iov));
12043cde9171SAlan Somers 			iov[0].iov_base = (caddr_t)packet;
12053cde9171SAlan Somers 			iov[0].iov_len = packlen;
12063cde9171SAlan Somers 			m.msg_iov = iov;
12073cde9171SAlan Somers 			m.msg_iovlen = 1;
12083cde9171SAlan Somers 			memset(cm, 0, CONTROLLEN);
12093cde9171SAlan Somers 			m.msg_control = (void *)cm;
12103cde9171SAlan Somers 			m.msg_controllen = CONTROLLEN;
12113cde9171SAlan Somers 
12123cde9171SAlan Somers 			cc = recvmsg(srecv, &m, 0);
12133cde9171SAlan Somers 			if (cc < 0) {
12143cde9171SAlan Somers 				if (errno != EINTR) {
12153cde9171SAlan Somers 					warn("recvmsg");
12163cde9171SAlan Somers 					sleep(1);
12173cde9171SAlan Somers 				}
12183cde9171SAlan Somers 				continue;
12193cde9171SAlan Somers 			} else if (cc == 0) {
12203cde9171SAlan Somers 				int mtu;
12213cde9171SAlan Somers 
12223cde9171SAlan Somers 				/*
12233cde9171SAlan Somers 				 * receive control messages only. Process the
12243cde9171SAlan Somers 				 * exceptions (currently the only possibility is
12253cde9171SAlan Somers 				 * a path MTU notification.)
12263cde9171SAlan Somers 				 */
12273cde9171SAlan Somers 				if ((mtu = get_pathmtu(&m)) > 0) {
12283cde9171SAlan Somers 					if ((options & F_VERBOSE) != 0) {
12293cde9171SAlan Somers 						printf("new path MTU (%d) is "
12303cde9171SAlan Somers 						    "notified\n", mtu);
12313cde9171SAlan Somers 					}
12323cde9171SAlan Somers 				}
12333cde9171SAlan Somers 				continue;
12343cde9171SAlan Somers 			} else {
12353cde9171SAlan Somers 				/*
12363cde9171SAlan Somers 				 * an ICMPv6 message (probably an echoreply)
12373cde9171SAlan Somers 				 * arrived.
12383cde9171SAlan Somers 				 */
12393cde9171SAlan Somers 				pr_pack(packet, cc, &m);
12403cde9171SAlan Somers 			}
12413cde9171SAlan Somers 			if (((options & F_ONCE) != 0 && nreceived > 0) ||
12423cde9171SAlan Somers 			    (npackets > 0 && nreceived >= npackets))
12433cde9171SAlan Somers 				break;
12443cde9171SAlan Somers 		}
12453cde9171SAlan Somers 		if (n == 0 || (options & F_FLOOD)) {
12463cde9171SAlan Somers 			if (npackets == 0 || ntransmitted < npackets)
12473cde9171SAlan Somers 				pinger();
12483cde9171SAlan Somers 			else {
12493cde9171SAlan Somers 				if (almost_done)
12503cde9171SAlan Somers 					break;
12513cde9171SAlan Somers 				almost_done = 1;
12523cde9171SAlan Somers 			/*
12533cde9171SAlan Somers 			 * If we're not transmitting any more packets,
12543cde9171SAlan Somers 			 * change the timer to wait two round-trip times
12553cde9171SAlan Somers 			 * if we've received any packets or (waittime)
12563cde9171SAlan Somers 			 * milliseconds if we haven't.
12573cde9171SAlan Somers 			 */
12583cde9171SAlan Somers 				intvl.tv_nsec = 0;
12593cde9171SAlan Somers 				if (nreceived) {
12603cde9171SAlan Somers 					intvl.tv_sec = 2 * tmax / 1000;
12613cde9171SAlan Somers 					if (intvl.tv_sec == 0)
12623cde9171SAlan Somers 						intvl.tv_sec = 1;
12633cde9171SAlan Somers 				} else {
12643cde9171SAlan Somers 					intvl.tv_sec = waittime / 1000;
12653cde9171SAlan Somers 					intvl.tv_nsec =
12663cde9171SAlan Somers 						waittime % 1000 * 1000000;
12673cde9171SAlan Somers 				}
12683cde9171SAlan Somers 			}
12693cde9171SAlan Somers 			clock_gettime(CLOCK_MONOTONIC, &last);
12703cde9171SAlan Somers 			if (ntransmitted - nreceived - 1 > nmissedmax) {
12713cde9171SAlan Somers 				nmissedmax = ntransmitted - nreceived - 1;
12723cde9171SAlan Somers 				if (options & F_MISSED)
12733cde9171SAlan Somers 					(void)write(STDOUT_FILENO, &BBELL, 1);
12743cde9171SAlan Somers 			}
12753cde9171SAlan Somers 		}
12763cde9171SAlan Somers 	}
12773cde9171SAlan Somers 	sigemptyset(&si_sa.sa_mask);
12783cde9171SAlan Somers 	si_sa.sa_flags = 0;
12793cde9171SAlan Somers 	si_sa.sa_handler = SIG_IGN;
12803cde9171SAlan Somers 	sigaction(SIGINT, &si_sa, 0);
12813cde9171SAlan Somers 	sigaction(SIGALRM, &si_sa, 0);
12823cde9171SAlan Somers 	summary();
12833cde9171SAlan Somers 
12843cde9171SAlan Somers         if(packet != NULL)
12853cde9171SAlan Somers                 free(packet);
12863cde9171SAlan Somers 
12873cde9171SAlan Somers 	if (nreceived > 0)
12883cde9171SAlan Somers 		exit(0);
12893cde9171SAlan Somers 	else if (ntransmitted > ntransmitfailures)
12903cde9171SAlan Somers 		exit(2);
12913cde9171SAlan Somers 	else
12923cde9171SAlan Somers 		exit(EX_OSERR);
12933cde9171SAlan Somers }
12943cde9171SAlan Somers 
12953cde9171SAlan Somers static void
12963cde9171SAlan Somers onsignal(int sig)
12973cde9171SAlan Somers {
12983cde9171SAlan Somers 
12993cde9171SAlan Somers 	switch (sig) {
13003cde9171SAlan Somers 	case SIGINT:
13013cde9171SAlan Somers 	case SIGALRM:
13023cde9171SAlan Somers 		seenint++;
13033cde9171SAlan Somers 		break;
13043cde9171SAlan Somers #ifdef SIGINFO
13053cde9171SAlan Somers 	case SIGINFO:
13063cde9171SAlan Somers 		seeninfo++;
13073cde9171SAlan Somers 		break;
13083cde9171SAlan Somers #endif
13093cde9171SAlan Somers 	}
13103cde9171SAlan Somers }
13113cde9171SAlan Somers 
13123cde9171SAlan Somers /*
13133cde9171SAlan Somers  * pinger --
13143cde9171SAlan Somers  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
13153cde9171SAlan Somers  * will be added on by the kernel.  The ID field is our UNIX process ID,
13163cde9171SAlan Somers  * and the sequence number is an ascending integer.  The first 8 bytes
13173cde9171SAlan Somers  * of the data portion are used to hold a UNIX "timespec" struct in VAX
13183cde9171SAlan Somers  * byte-order, to compute the round-trip time.
13193cde9171SAlan Somers  */
13203cde9171SAlan Somers static size_t
13213cde9171SAlan Somers pingerlen(void)
13223cde9171SAlan Somers {
13233cde9171SAlan Somers 	size_t l;
13243cde9171SAlan Somers 
13253cde9171SAlan Somers 	if (options & F_FQDN)
13263cde9171SAlan Somers 		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
13273cde9171SAlan Somers 	else if (options & F_FQDNOLD)
13283cde9171SAlan Somers 		l = ICMP6_NIQLEN;
13293cde9171SAlan Somers 	else if (options & F_NODEADDR)
13303cde9171SAlan Somers 		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
13313cde9171SAlan Somers 	else if (options & F_SUPTYPES)
13323cde9171SAlan Somers 		l = ICMP6_NIQLEN;
13333cde9171SAlan Somers 	else
13343cde9171SAlan Somers 		l = ICMP6ECHOLEN + datalen;
13353cde9171SAlan Somers 
13363cde9171SAlan Somers 	return l;
13373cde9171SAlan Somers }
13383cde9171SAlan Somers 
13393cde9171SAlan Somers static int
13403cde9171SAlan Somers pinger(void)
13413cde9171SAlan Somers {
13423cde9171SAlan Somers 	struct icmp6_hdr *icp;
13433cde9171SAlan Somers 	struct iovec iov[2];
13443cde9171SAlan Somers 	int i, cc;
13453cde9171SAlan Somers 	struct icmp6_nodeinfo *nip;
13463cde9171SAlan Somers 	uint16_t seq;
13473cde9171SAlan Somers 
13483cde9171SAlan Somers 	if (npackets && ntransmitted >= npackets)
13493cde9171SAlan Somers 		return(-1);	/* no more transmission */
13503cde9171SAlan Somers 
13513cde9171SAlan Somers 	icp = (struct icmp6_hdr *)outpack;
13523cde9171SAlan Somers 	nip = (struct icmp6_nodeinfo *)outpack;
13533cde9171SAlan Somers 	memset(icp, 0, sizeof(*icp));
13543cde9171SAlan Somers 	icp->icmp6_cksum = 0;
13553cde9171SAlan Somers 	seq = ntransmitted++;
13563cde9171SAlan Somers 	CLR(seq % mx_dup_ck);
13573cde9171SAlan Somers 
13583cde9171SAlan Somers 	if (options & F_FQDN) {
13593cde9171SAlan Somers 		uint16_t s;
13603cde9171SAlan Somers 
13613cde9171SAlan Somers 		icp->icmp6_type = ICMP6_NI_QUERY;
13623cde9171SAlan Somers 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
13633cde9171SAlan Somers 		nip->ni_qtype = htons(NI_QTYPE_FQDN);
13643cde9171SAlan Somers 		nip->ni_flags = htons(0);
13653cde9171SAlan Somers 
13663cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, nonce,
13673cde9171SAlan Somers 		    sizeof(nip->icmp6_ni_nonce));
13683cde9171SAlan Somers 		s = htons(seq);
13693cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, &s, sizeof(s));
13703cde9171SAlan Somers 
13713cde9171SAlan Somers 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
13723cde9171SAlan Somers 		    sizeof(dst.sin6_addr));
13733cde9171SAlan Somers 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
13743cde9171SAlan Somers 		datalen = 0;
13753cde9171SAlan Somers 	} else if (options & F_FQDNOLD) {
13763cde9171SAlan Somers 		uint16_t s;
13773cde9171SAlan Somers 		/* packet format in 03 draft - no Subject data on queries */
13783cde9171SAlan Somers 		icp->icmp6_type = ICMP6_NI_QUERY;
13793cde9171SAlan Somers 		icp->icmp6_code = 0;	/* code field is always 0 */
13803cde9171SAlan Somers 		nip->ni_qtype = htons(NI_QTYPE_FQDN);
13813cde9171SAlan Somers 		nip->ni_flags = htons(0);
13823cde9171SAlan Somers 
13833cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, nonce,
13843cde9171SAlan Somers 		    sizeof(nip->icmp6_ni_nonce));
13853cde9171SAlan Somers 		s = htons(seq);
13863cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, &s, sizeof(s));
13873cde9171SAlan Somers 
13883cde9171SAlan Somers 		cc = ICMP6_NIQLEN;
13893cde9171SAlan Somers 		datalen = 0;
13903cde9171SAlan Somers 	} else if (options & F_NODEADDR) {
13913cde9171SAlan Somers 		uint16_t s;
13923cde9171SAlan Somers 
13933cde9171SAlan Somers 		icp->icmp6_type = ICMP6_NI_QUERY;
13943cde9171SAlan Somers 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
13953cde9171SAlan Somers 		nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
13963cde9171SAlan Somers 		nip->ni_flags = naflags;
13973cde9171SAlan Somers 
13983cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, nonce,
13993cde9171SAlan Somers 		    sizeof(nip->icmp6_ni_nonce));
14003cde9171SAlan Somers 		s = htons(seq);
14013cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, &s, sizeof(s));
14023cde9171SAlan Somers 
14033cde9171SAlan Somers 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
14043cde9171SAlan Somers 		    sizeof(dst.sin6_addr));
14053cde9171SAlan Somers 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
14063cde9171SAlan Somers 		datalen = 0;
14073cde9171SAlan Somers 	} else if (options & F_SUPTYPES) {
14083cde9171SAlan Somers 		uint16_t s;
14093cde9171SAlan Somers 
14103cde9171SAlan Somers 		icp->icmp6_type = ICMP6_NI_QUERY;
14113cde9171SAlan Somers 		icp->icmp6_code = ICMP6_NI_SUBJ_FQDN;	/*empty*/
14123cde9171SAlan Somers 		nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
14133cde9171SAlan Somers 		/* we support compressed bitmap */
14143cde9171SAlan Somers 		nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
14153cde9171SAlan Somers 
14163cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, nonce,
14173cde9171SAlan Somers 		    sizeof(nip->icmp6_ni_nonce));
14183cde9171SAlan Somers 		s = htons(seq);
14193cde9171SAlan Somers 		memcpy(nip->icmp6_ni_nonce, &s, sizeof(s));
14203cde9171SAlan Somers 
14213cde9171SAlan Somers 		cc = ICMP6_NIQLEN;
14223cde9171SAlan Somers 		datalen = 0;
14233cde9171SAlan Somers 	} else {
14243cde9171SAlan Somers 		icp->icmp6_type = ICMP6_ECHO_REQUEST;
14253cde9171SAlan Somers 		icp->icmp6_code = 0;
14263cde9171SAlan Somers 		icp->icmp6_id = htons(ident);
14273cde9171SAlan Somers 		icp->icmp6_seq = htons(seq);
14283cde9171SAlan Somers 		if (timing) {
14293cde9171SAlan Somers 			struct timespec tv;
14303cde9171SAlan Somers 			struct tv32 tv32;
14313cde9171SAlan Somers 			(void)clock_gettime(CLOCK_MONOTONIC, &tv);
14323cde9171SAlan Somers 			/*
14333cde9171SAlan Somers 			 * Truncate seconds down to 32 bits in order
14343cde9171SAlan Somers 			 * to fit the timestamp within 8 bytes of the
14353cde9171SAlan Somers 			 * packet. We're only concerned with
14363cde9171SAlan Somers 			 * durations, not absolute times.
14373cde9171SAlan Somers 			 */
14383cde9171SAlan Somers 			tv32.tv32_sec = (uint32_t)htonl(tv.tv_sec);
14393cde9171SAlan Somers 			tv32.tv32_nsec = (uint32_t)htonl(tv.tv_nsec);
14403cde9171SAlan Somers 			memcpy(&outpack[ICMP6ECHOLEN], &tv32, sizeof(tv32));
14413cde9171SAlan Somers 		}
14423cde9171SAlan Somers 		cc = ICMP6ECHOLEN + datalen;
14433cde9171SAlan Somers 	}
14443cde9171SAlan Somers 
14453cde9171SAlan Somers #ifdef DIAGNOSTIC
14463cde9171SAlan Somers 	if (pingerlen() != cc)
14473cde9171SAlan Somers 		errx(1, "internal error; length mismatch");
14483cde9171SAlan Somers #endif
14493cde9171SAlan Somers 
14503cde9171SAlan Somers 	memset(&iov, 0, sizeof(iov));
14513cde9171SAlan Somers 	iov[0].iov_base = (caddr_t)outpack;
14523cde9171SAlan Somers 	iov[0].iov_len = cc;
14533cde9171SAlan Somers 	smsghdr.msg_iov = iov;
14543cde9171SAlan Somers 	smsghdr.msg_iovlen = 1;
14553cde9171SAlan Somers 
14563cde9171SAlan Somers 	i = sendmsg(ssend, &smsghdr, 0);
14573cde9171SAlan Somers 
14583cde9171SAlan Somers 	if (i < 0 || i != cc)  {
14593cde9171SAlan Somers 		if (i < 0) {
14603cde9171SAlan Somers 			ntransmitfailures++;
14613cde9171SAlan Somers 			warn("sendmsg");
14623cde9171SAlan Somers 		}
14633cde9171SAlan Somers 		(void)printf("ping6: wrote %s %d chars, ret=%d\n",
14643cde9171SAlan Somers 		    hostname, cc, i);
14653cde9171SAlan Somers 	}
14663cde9171SAlan Somers 	if (!(options & F_QUIET) && options & F_FLOOD)
14673cde9171SAlan Somers 		(void)write(STDOUT_FILENO, &DOT, 1);
14683cde9171SAlan Somers 
14693cde9171SAlan Somers 	return(0);
14703cde9171SAlan Somers }
14713cde9171SAlan Somers 
14723cde9171SAlan Somers static int
14733cde9171SAlan Somers myechoreply(const struct icmp6_hdr *icp)
14743cde9171SAlan Somers {
14753cde9171SAlan Somers 	if (ntohs(icp->icmp6_id) == ident)
14763cde9171SAlan Somers 		return 1;
14773cde9171SAlan Somers 	else
14783cde9171SAlan Somers 		return 0;
14793cde9171SAlan Somers }
14803cde9171SAlan Somers 
14813cde9171SAlan Somers static int
14823cde9171SAlan Somers mynireply(const struct icmp6_nodeinfo *nip)
14833cde9171SAlan Somers {
14843cde9171SAlan Somers 	if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
14853cde9171SAlan Somers 	    nonce + sizeof(u_int16_t),
14863cde9171SAlan Somers 	    sizeof(nonce) - sizeof(u_int16_t)) == 0)
14873cde9171SAlan Somers 		return 1;
14883cde9171SAlan Somers 	else
14893cde9171SAlan Somers 		return 0;
14903cde9171SAlan Somers }
14913cde9171SAlan Somers 
14923cde9171SAlan Somers /*
14933cde9171SAlan Somers  * Decode a name from a DNS message.
14943cde9171SAlan Somers  *
14953cde9171SAlan Somers  * Format of the message is described in RFC 1035 subsection 4.1.4.
14963cde9171SAlan Somers  *
14973cde9171SAlan Somers  * Arguments:
14983cde9171SAlan Somers  *   sp     - Pointer to a DNS pointer octet or to the first octet of a label
14993cde9171SAlan Somers  *            in the message.
15003cde9171SAlan Somers  *   ep     - Pointer to the end of the message (one step past the last octet).
15013cde9171SAlan Somers  *   base   - Pointer to the beginning of the message.
15023cde9171SAlan Somers  *   buf    - Buffer into which the decoded name will be saved.
15033cde9171SAlan Somers  *   bufsiz - Size of the buffer 'buf'.
15043cde9171SAlan Somers  *
15053cde9171SAlan Somers  * Return value:
15063cde9171SAlan Somers  *   Pointer to an octet immediately following the ending zero octet
15073cde9171SAlan Somers  *   of the decoded label, or NULL if an error occured.
15083cde9171SAlan Somers  */
15093cde9171SAlan Somers static const char *
15103cde9171SAlan Somers dnsdecode(const u_char *sp, const u_char *ep, const u_char *base, char *buf,
15113cde9171SAlan Somers 	size_t bufsiz)
15123cde9171SAlan Somers {
15133cde9171SAlan Somers 	int i;
15143cde9171SAlan Somers 	const u_char *cp;
15153cde9171SAlan Somers 	char cresult[MAXDNAME + 1];
15163cde9171SAlan Somers 	const u_char *comp;
15173cde9171SAlan Somers 	int l;
15183cde9171SAlan Somers 
15193cde9171SAlan Somers 	cp = sp;
15203cde9171SAlan Somers 	*buf = '\0';
15213cde9171SAlan Somers 
15223cde9171SAlan Somers 	if (cp >= ep)
15233cde9171SAlan Somers 		return NULL;
15243cde9171SAlan Somers 	while (cp < ep) {
15253cde9171SAlan Somers 		i = *cp;
15263cde9171SAlan Somers 		if (i == 0 || cp != sp) {
15273cde9171SAlan Somers 			if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
15283cde9171SAlan Somers 				return NULL;	/*result overrun*/
15293cde9171SAlan Somers 		}
15303cde9171SAlan Somers 		if (i == 0)
15313cde9171SAlan Somers 			break;
15323cde9171SAlan Somers 		cp++;
15333cde9171SAlan Somers 
15343cde9171SAlan Somers 		if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
15353cde9171SAlan Somers 			/* DNS compression */
15363cde9171SAlan Somers 			if (!base)
15373cde9171SAlan Somers 				return NULL;
15383cde9171SAlan Somers 
15393cde9171SAlan Somers 			comp = base + (i & 0x3f);
15403cde9171SAlan Somers 			if (dnsdecode(comp, cp, base, cresult,
15413cde9171SAlan Somers 			    sizeof(cresult)) == NULL)
15423cde9171SAlan Somers 				return NULL;
15433cde9171SAlan Somers 			if (strlcat(buf, cresult, bufsiz) >= bufsiz)
15443cde9171SAlan Somers 				return NULL;	/*result overrun*/
15453cde9171SAlan Somers 			break;
15463cde9171SAlan Somers 		} else if ((i & 0x3f) == i) {
15473cde9171SAlan Somers 			if (i > ep - cp)
15483cde9171SAlan Somers 				return NULL;	/*source overrun*/
15493cde9171SAlan Somers 			while (i-- > 0 && cp < ep) {
15503cde9171SAlan Somers 				l = snprintf(cresult, sizeof(cresult),
15513cde9171SAlan Somers 				    isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
15523cde9171SAlan Somers 				if ((size_t)l >= sizeof(cresult) || l < 0)
15533cde9171SAlan Somers 					return NULL;
15543cde9171SAlan Somers 				if (strlcat(buf, cresult, bufsiz) >= bufsiz)
15553cde9171SAlan Somers 					return NULL;	/*result overrun*/
15563cde9171SAlan Somers 				cp++;
15573cde9171SAlan Somers 			}
15583cde9171SAlan Somers 		} else
15593cde9171SAlan Somers 			return NULL;	/*invalid label*/
15603cde9171SAlan Somers 	}
15613cde9171SAlan Somers 	if (i != 0)
15623cde9171SAlan Somers 		return NULL;	/*not terminated*/
15633cde9171SAlan Somers 	cp++;
15643cde9171SAlan Somers 	return cp;
15653cde9171SAlan Somers }
15663cde9171SAlan Somers 
15673cde9171SAlan Somers /*
15683cde9171SAlan Somers  * pr_pack --
15693cde9171SAlan Somers  *	Print out the packet, if it came from us.  This logic is necessary
15703cde9171SAlan Somers  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
15713cde9171SAlan Somers  * which arrive ('tis only fair).  This permits multiple copies of this
15723cde9171SAlan Somers  * program to be run without having intermingled output (or statistics!).
15733cde9171SAlan Somers  */
15743cde9171SAlan Somers static void
15753cde9171SAlan Somers pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
15763cde9171SAlan Somers {
15773cde9171SAlan Somers #define safeputc(c)	printf((isprint((c)) ? "%c" : "\\%03o"), c)
15783cde9171SAlan Somers 	struct icmp6_hdr *icp;
15793cde9171SAlan Somers 	struct icmp6_nodeinfo *ni;
15803cde9171SAlan Somers 	int i;
15813cde9171SAlan Somers 	int hoplim;
15823cde9171SAlan Somers 	struct sockaddr *from;
15833cde9171SAlan Somers 	int fromlen;
15843cde9171SAlan Somers 	const u_char *cp = NULL;
15853cde9171SAlan Somers 	u_char *dp, *end = buf + cc;
15863cde9171SAlan Somers 	struct in6_pktinfo *pktinfo = NULL;
15873cde9171SAlan Somers 	struct timespec tv, tp;
15883cde9171SAlan Somers 	struct tv32 tpp;
15893cde9171SAlan Somers 	double triptime = 0;
15903cde9171SAlan Somers 	int dupflag;
15913cde9171SAlan Somers 	size_t off;
15923cde9171SAlan Somers 	int oldfqdn;
15933cde9171SAlan Somers 	u_int16_t seq;
15943cde9171SAlan Somers 	char dnsname[MAXDNAME + 1];
15953cde9171SAlan Somers 
15963cde9171SAlan Somers 	(void)clock_gettime(CLOCK_MONOTONIC, &tv);
15973cde9171SAlan Somers 
15983cde9171SAlan Somers 	if (!mhdr || !mhdr->msg_name ||
15993cde9171SAlan Somers 	    mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
16003cde9171SAlan Somers 	    ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
16013cde9171SAlan Somers 		if (options & F_VERBOSE)
16023cde9171SAlan Somers 			warnx("invalid peername");
16033cde9171SAlan Somers 		return;
16043cde9171SAlan Somers 	}
16053cde9171SAlan Somers 	from = (struct sockaddr *)mhdr->msg_name;
16063cde9171SAlan Somers 	fromlen = mhdr->msg_namelen;
16073cde9171SAlan Somers 	if (cc < (int)sizeof(struct icmp6_hdr)) {
16083cde9171SAlan Somers 		if (options & F_VERBOSE)
16093cde9171SAlan Somers 			warnx("packet too short (%d bytes) from %s", cc,
16103cde9171SAlan Somers 			    pr_addr(from, fromlen));
16113cde9171SAlan Somers 		return;
16123cde9171SAlan Somers 	}
16133cde9171SAlan Somers 	if (((mhdr->msg_flags & MSG_CTRUNC) != 0) &&
16143cde9171SAlan Somers 	    (options & F_VERBOSE) != 0)
16153cde9171SAlan Somers 		warnx("some control data discarded, insufficient buffer size");
16163cde9171SAlan Somers 	icp = (struct icmp6_hdr *)buf;
16173cde9171SAlan Somers 	ni = (struct icmp6_nodeinfo *)buf;
16183cde9171SAlan Somers 	off = 0;
16193cde9171SAlan Somers 
16203cde9171SAlan Somers 	if ((hoplim = get_hoplim(mhdr)) == -1) {
16213cde9171SAlan Somers 		warnx("failed to get receiving hop limit");
16223cde9171SAlan Somers 		return;
16233cde9171SAlan Somers 	}
16243cde9171SAlan Somers 	if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
16253cde9171SAlan Somers 		warnx("failed to get receiving packet information");
16263cde9171SAlan Somers 		return;
16273cde9171SAlan Somers 	}
16283cde9171SAlan Somers 
16293cde9171SAlan Somers 	if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
16303cde9171SAlan Somers 		seq = ntohs(icp->icmp6_seq);
16313cde9171SAlan Somers 		++nreceived;
16323cde9171SAlan Somers 		if (timing) {
16333cde9171SAlan Somers 			memcpy(&tpp, icp + 1, sizeof(tpp));
16343cde9171SAlan Somers 			tp.tv_sec = ntohl(tpp.tv32_sec);
16353cde9171SAlan Somers 			tp.tv_nsec = ntohl(tpp.tv32_nsec);
16363cde9171SAlan Somers 			timespecsub(&tv, &tp, &tv);
16373cde9171SAlan Somers 			triptime = ((double)tv.tv_sec) * 1000.0 +
16383cde9171SAlan Somers 			    ((double)tv.tv_nsec) / 1000000.0;
16393cde9171SAlan Somers 			tsum += triptime;
16403cde9171SAlan Somers 			tsumsq += triptime * triptime;
16413cde9171SAlan Somers 			if (triptime < tmin)
16423cde9171SAlan Somers 				tmin = triptime;
16433cde9171SAlan Somers 			if (triptime > tmax)
16443cde9171SAlan Somers 				tmax = triptime;
16453cde9171SAlan Somers 		}
16463cde9171SAlan Somers 
16473cde9171SAlan Somers 		if (TST(seq % mx_dup_ck)) {
16483cde9171SAlan Somers 			++nrepeats;
16493cde9171SAlan Somers 			--nreceived;
16503cde9171SAlan Somers 			dupflag = 1;
16513cde9171SAlan Somers 		} else {
16523cde9171SAlan Somers 			SET(seq % mx_dup_ck);
16533cde9171SAlan Somers 			dupflag = 0;
16543cde9171SAlan Somers 		}
16553cde9171SAlan Somers 
16563cde9171SAlan Somers 		if (options & F_QUIET)
16573cde9171SAlan Somers 			return;
16583cde9171SAlan Somers 
16593cde9171SAlan Somers 		if (options & F_WAITTIME && triptime > waittime) {
16603cde9171SAlan Somers 			++nrcvtimeout;
16613cde9171SAlan Somers 			return;
16623cde9171SAlan Somers 		}
16633cde9171SAlan Somers 
16643cde9171SAlan Somers 		if (options & F_FLOOD)
16653cde9171SAlan Somers 			(void)write(STDOUT_FILENO, &BSPACE, 1);
16663cde9171SAlan Somers 		else {
16673cde9171SAlan Somers 			if (options & F_AUDIBLE)
16683cde9171SAlan Somers 				(void)write(STDOUT_FILENO, &BBELL, 1);
16693cde9171SAlan Somers 			(void)printf("%d bytes from %s, icmp_seq=%u", cc,
16703cde9171SAlan Somers 			    pr_addr(from, fromlen), seq);
16713cde9171SAlan Somers 			(void)printf(" hlim=%d", hoplim);
16723cde9171SAlan Somers 			if ((options & F_VERBOSE) != 0) {
16733cde9171SAlan Somers 				struct sockaddr_in6 dstsa;
16743cde9171SAlan Somers 
16753cde9171SAlan Somers 				memset(&dstsa, 0, sizeof(dstsa));
16763cde9171SAlan Somers 				dstsa.sin6_family = AF_INET6;
16773cde9171SAlan Somers 				dstsa.sin6_len = sizeof(dstsa);
16783cde9171SAlan Somers 				dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
16793cde9171SAlan Somers 				dstsa.sin6_addr = pktinfo->ipi6_addr;
16803cde9171SAlan Somers 				(void)printf(" dst=%s",
16813cde9171SAlan Somers 				    pr_addr((struct sockaddr *)&dstsa,
16823cde9171SAlan Somers 				    sizeof(dstsa)));
16833cde9171SAlan Somers 			}
16843cde9171SAlan Somers 			if (timing)
16853cde9171SAlan Somers 				(void)printf(" time=%.3f ms", triptime);
16863cde9171SAlan Somers 			if (dupflag)
16873cde9171SAlan Somers 				(void)printf("(DUP!)");
16883cde9171SAlan Somers 			/* check the data */
16893cde9171SAlan Somers 			cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
16903cde9171SAlan Somers 			dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
16913cde9171SAlan Somers 			for (i = 8; cp < end; ++i, ++cp, ++dp) {
16923cde9171SAlan Somers 				if (*cp != *dp) {
16933cde9171SAlan Somers 					(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
16943cde9171SAlan Somers 					break;
16953cde9171SAlan Somers 				}
16963cde9171SAlan Somers 			}
16973cde9171SAlan Somers 		}
16983cde9171SAlan Somers 	} else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
16993cde9171SAlan Somers 		memcpy(&seq, ni->icmp6_ni_nonce, sizeof(seq));
17003cde9171SAlan Somers 		seq = ntohs(seq);
17013cde9171SAlan Somers 		++nreceived;
17023cde9171SAlan Somers 		if (TST(seq % mx_dup_ck)) {
17033cde9171SAlan Somers 			++nrepeats;
17043cde9171SAlan Somers 			--nreceived;
17053cde9171SAlan Somers 			dupflag = 1;
17063cde9171SAlan Somers 		} else {
17073cde9171SAlan Somers 			SET(seq % mx_dup_ck);
17083cde9171SAlan Somers 			dupflag = 0;
17093cde9171SAlan Somers 		}
17103cde9171SAlan Somers 
17113cde9171SAlan Somers 		if (options & F_QUIET)
17123cde9171SAlan Somers 			return;
17133cde9171SAlan Somers 
17143cde9171SAlan Somers 		(void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
17153cde9171SAlan Somers 
17163cde9171SAlan Somers 		switch (ntohs(ni->ni_code)) {
17173cde9171SAlan Somers 		case ICMP6_NI_SUCCESS:
17183cde9171SAlan Somers 			break;
17193cde9171SAlan Somers 		case ICMP6_NI_REFUSED:
17203cde9171SAlan Somers 			printf("refused, type 0x%x", ntohs(ni->ni_type));
17213cde9171SAlan Somers 			goto fqdnend;
17223cde9171SAlan Somers 		case ICMP6_NI_UNKNOWN:
17233cde9171SAlan Somers 			printf("unknown, type 0x%x", ntohs(ni->ni_type));
17243cde9171SAlan Somers 			goto fqdnend;
17253cde9171SAlan Somers 		default:
17263cde9171SAlan Somers 			printf("unknown code 0x%x, type 0x%x",
17273cde9171SAlan Somers 			    ntohs(ni->ni_code), ntohs(ni->ni_type));
17283cde9171SAlan Somers 			goto fqdnend;
17293cde9171SAlan Somers 		}
17303cde9171SAlan Somers 
17313cde9171SAlan Somers 		switch (ntohs(ni->ni_qtype)) {
17323cde9171SAlan Somers 		case NI_QTYPE_NOOP:
17333cde9171SAlan Somers 			printf("NodeInfo NOOP");
17343cde9171SAlan Somers 			break;
17353cde9171SAlan Somers 		case NI_QTYPE_SUPTYPES:
17363cde9171SAlan Somers 			pr_suptypes(ni, end - (u_char *)ni);
17373cde9171SAlan Somers 			break;
17383cde9171SAlan Somers 		case NI_QTYPE_NODEADDR:
17393cde9171SAlan Somers 			pr_nodeaddr(ni, end - (u_char *)ni);
17403cde9171SAlan Somers 			break;
17413cde9171SAlan Somers 		case NI_QTYPE_FQDN:
17423cde9171SAlan Somers 		default:	/* XXX: for backward compatibility */
17433cde9171SAlan Somers 			cp = (u_char *)ni + ICMP6_NIRLEN;
17443cde9171SAlan Somers 			if (buf[off + ICMP6_NIRLEN] ==
17453cde9171SAlan Somers 			    cc - off - ICMP6_NIRLEN - 1)
17463cde9171SAlan Somers 				oldfqdn = 1;
17473cde9171SAlan Somers 			else
17483cde9171SAlan Somers 				oldfqdn = 0;
17493cde9171SAlan Somers 			if (oldfqdn) {
17503cde9171SAlan Somers 				cp++;	/* skip length */
17513cde9171SAlan Somers 				while (cp < end) {
17523cde9171SAlan Somers 					safeputc(*cp & 0xff);
17533cde9171SAlan Somers 					cp++;
17543cde9171SAlan Somers 				}
17553cde9171SAlan Somers 			} else {
17563cde9171SAlan Somers 				i = 0;
17573cde9171SAlan Somers 				while (cp < end) {
17583cde9171SAlan Somers 					cp = dnsdecode((const u_char *)cp, end,
17593cde9171SAlan Somers 					    (const u_char *)(ni + 1), dnsname,
17603cde9171SAlan Somers 					    sizeof(dnsname));
17613cde9171SAlan Somers 					if (cp == NULL) {
17623cde9171SAlan Somers 						printf("???");
17633cde9171SAlan Somers 						break;
17643cde9171SAlan Somers 					}
17653cde9171SAlan Somers 					/*
17663cde9171SAlan Somers 					 * name-lookup special handling for
17673cde9171SAlan Somers 					 * truncated name
17683cde9171SAlan Somers 					 */
17693cde9171SAlan Somers 					if (cp + 1 <= end && !*cp &&
17703cde9171SAlan Somers 					    strlen(dnsname) > 0) {
17713cde9171SAlan Somers 						dnsname[strlen(dnsname) - 1] = '\0';
17723cde9171SAlan Somers 						cp++;
17733cde9171SAlan Somers 					}
17743cde9171SAlan Somers 					printf("%s%s", i > 0 ? "," : "",
17753cde9171SAlan Somers 					    dnsname);
17763cde9171SAlan Somers 				}
17773cde9171SAlan Somers 			}
17783cde9171SAlan Somers 			if (options & F_VERBOSE) {
17793cde9171SAlan Somers 				u_long t;
17803cde9171SAlan Somers 				int32_t ttl;
17813cde9171SAlan Somers 				int comma = 0;
17823cde9171SAlan Somers 
17833cde9171SAlan Somers 				(void)printf(" (");	/*)*/
17843cde9171SAlan Somers 
17853cde9171SAlan Somers 				switch (ni->ni_code) {
17863cde9171SAlan Somers 				case ICMP6_NI_REFUSED:
17873cde9171SAlan Somers 					(void)printf("refused");
17883cde9171SAlan Somers 					comma++;
17893cde9171SAlan Somers 					break;
17903cde9171SAlan Somers 				case ICMP6_NI_UNKNOWN:
17913cde9171SAlan Somers 					(void)printf("unknown qtype");
17923cde9171SAlan Somers 					comma++;
17933cde9171SAlan Somers 					break;
17943cde9171SAlan Somers 				}
17953cde9171SAlan Somers 
17963cde9171SAlan Somers 				if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
17973cde9171SAlan Somers 					/* case of refusion, unknown */
17983cde9171SAlan Somers 					/*(*/
17993cde9171SAlan Somers 					putchar(')');
18003cde9171SAlan Somers 					goto fqdnend;
18013cde9171SAlan Somers 				}
18023cde9171SAlan Somers 				memcpy(&t, &buf[off+ICMP6ECHOLEN+8], sizeof(t));
18033cde9171SAlan Somers 				ttl = (int32_t)ntohl(t);
18043cde9171SAlan Somers 				if (comma)
18053cde9171SAlan Somers 					printf(",");
18063cde9171SAlan Somers 				if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
18073cde9171SAlan Somers 					(void)printf("TTL=%d:meaningless",
18083cde9171SAlan Somers 					    (int)ttl);
18093cde9171SAlan Somers 				} else {
18103cde9171SAlan Somers 					if (ttl < 0) {
18113cde9171SAlan Somers 						(void)printf("TTL=%d:invalid",
18123cde9171SAlan Somers 						   ttl);
18133cde9171SAlan Somers 					} else
18143cde9171SAlan Somers 						(void)printf("TTL=%d", ttl);
18153cde9171SAlan Somers 				}
18163cde9171SAlan Somers 				comma++;
18173cde9171SAlan Somers 
18183cde9171SAlan Somers 				if (oldfqdn) {
18193cde9171SAlan Somers 					if (comma)
18203cde9171SAlan Somers 						printf(",");
18213cde9171SAlan Somers 					printf("03 draft");
18223cde9171SAlan Somers 					comma++;
18233cde9171SAlan Somers 				} else {
18243cde9171SAlan Somers 					cp = (u_char *)ni + ICMP6_NIRLEN;
18253cde9171SAlan Somers 					if (cp == end) {
18263cde9171SAlan Somers 						if (comma)
18273cde9171SAlan Somers 							printf(",");
18283cde9171SAlan Somers 						printf("no name");
18293cde9171SAlan Somers 						comma++;
18303cde9171SAlan Somers 					}
18313cde9171SAlan Somers 				}
18323cde9171SAlan Somers 
18333cde9171SAlan Somers 				if (buf[off + ICMP6_NIRLEN] !=
18343cde9171SAlan Somers 				    cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
18353cde9171SAlan Somers 					if (comma)
18363cde9171SAlan Somers 						printf(",");
18373cde9171SAlan Somers 					(void)printf("invalid namelen:%d/%lu",
18383cde9171SAlan Somers 					    buf[off + ICMP6_NIRLEN],
18393cde9171SAlan Somers 					    (u_long)cc - off - ICMP6_NIRLEN - 1);
18403cde9171SAlan Somers 					comma++;
18413cde9171SAlan Somers 				}
18423cde9171SAlan Somers 				/*(*/
18433cde9171SAlan Somers 				putchar(')');
18443cde9171SAlan Somers 			}
18453cde9171SAlan Somers 		fqdnend:
18463cde9171SAlan Somers 			;
18473cde9171SAlan Somers 		}
18483cde9171SAlan Somers 	} else {
18493cde9171SAlan Somers 		/* We've got something other than an ECHOREPLY */
18503cde9171SAlan Somers 		if (!(options & F_VERBOSE))
18513cde9171SAlan Somers 			return;
18523cde9171SAlan Somers 		(void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
18533cde9171SAlan Somers 		pr_icmph(icp, end);
18543cde9171SAlan Somers 	}
18553cde9171SAlan Somers 
18563cde9171SAlan Somers 	if (!(options & F_FLOOD)) {
18573cde9171SAlan Somers 		(void)putchar('\n');
18583cde9171SAlan Somers 		if (options & F_VERBOSE)
18593cde9171SAlan Somers 			pr_exthdrs(mhdr);
18603cde9171SAlan Somers 		(void)fflush(stdout);
18613cde9171SAlan Somers 	}
18623cde9171SAlan Somers #undef safeputc
18633cde9171SAlan Somers }
18643cde9171SAlan Somers 
18653cde9171SAlan Somers static void
18663cde9171SAlan Somers pr_exthdrs(struct msghdr *mhdr)
18673cde9171SAlan Somers {
18683cde9171SAlan Somers 	ssize_t	bufsize;
18693cde9171SAlan Somers 	void	*bufp;
18703cde9171SAlan Somers 	struct cmsghdr *cm;
18713cde9171SAlan Somers 
18723cde9171SAlan Somers 	bufsize = 0;
18733cde9171SAlan Somers 	bufp = mhdr->msg_control;
18743cde9171SAlan Somers 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
18753cde9171SAlan Somers 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
18763cde9171SAlan Somers 		if (cm->cmsg_level != IPPROTO_IPV6)
18773cde9171SAlan Somers 			continue;
18783cde9171SAlan Somers 
18793cde9171SAlan Somers 		bufsize = CONTROLLEN - ((caddr_t)CMSG_DATA(cm) - (caddr_t)bufp);
18803cde9171SAlan Somers 		if (bufsize <= 0)
18813cde9171SAlan Somers 			continue;
18823cde9171SAlan Somers 		switch (cm->cmsg_type) {
18833cde9171SAlan Somers 		case IPV6_HOPOPTS:
18843cde9171SAlan Somers 			printf("  HbH Options: ");
18853cde9171SAlan Somers 			pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
18863cde9171SAlan Somers 			break;
18873cde9171SAlan Somers 		case IPV6_DSTOPTS:
18883cde9171SAlan Somers #ifdef IPV6_RTHDRDSTOPTS
18893cde9171SAlan Somers 		case IPV6_RTHDRDSTOPTS:
18903cde9171SAlan Somers #endif
18913cde9171SAlan Somers 			printf("  Dst Options: ");
18923cde9171SAlan Somers 			pr_ip6opt(CMSG_DATA(cm), (size_t)bufsize);
18933cde9171SAlan Somers 			break;
18943cde9171SAlan Somers 		case IPV6_RTHDR:
18953cde9171SAlan Somers 			printf("  Routing: ");
18963cde9171SAlan Somers 			pr_rthdr(CMSG_DATA(cm), (size_t)bufsize);
18973cde9171SAlan Somers 			break;
18983cde9171SAlan Somers 		}
18993cde9171SAlan Somers 	}
19003cde9171SAlan Somers }
19013cde9171SAlan Somers 
19023cde9171SAlan Somers static void
19033cde9171SAlan Somers pr_ip6opt(void *extbuf, size_t bufsize)
19043cde9171SAlan Somers {
19053cde9171SAlan Somers 	struct ip6_hbh *ext;
19063cde9171SAlan Somers 	int currentlen;
19073cde9171SAlan Somers 	u_int8_t type;
19083cde9171SAlan Somers 	socklen_t extlen, len;
19093cde9171SAlan Somers 	void *databuf;
19103cde9171SAlan Somers 	size_t offset;
19113cde9171SAlan Somers 	u_int16_t value2;
19123cde9171SAlan Somers 	u_int32_t value4;
19133cde9171SAlan Somers 
19143cde9171SAlan Somers 	ext = (struct ip6_hbh *)extbuf;
19153cde9171SAlan Somers 	extlen = (ext->ip6h_len + 1) * 8;
19163cde9171SAlan Somers 	printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
19173cde9171SAlan Somers 	    (unsigned int)ext->ip6h_len, (unsigned long)extlen);
19183cde9171SAlan Somers 
19193cde9171SAlan Somers 	/*
19203cde9171SAlan Somers 	 * Bounds checking on the ancillary data buffer:
19213cde9171SAlan Somers 	 *     subtract the size of a cmsg structure from the buffer size.
19223cde9171SAlan Somers 	 */
19233cde9171SAlan Somers 	if (bufsize < (extlen  + CMSG_SPACE(0))) {
19243cde9171SAlan Somers 		extlen = bufsize - CMSG_SPACE(0);
19253cde9171SAlan Somers 		warnx("options truncated, showing only %u (total=%u)",
19263cde9171SAlan Somers 		    (unsigned int)(extlen / 8 - 1),
19273cde9171SAlan Somers 		    (unsigned int)(ext->ip6h_len));
19283cde9171SAlan Somers 	}
19293cde9171SAlan Somers 
19303cde9171SAlan Somers 	currentlen = 0;
19313cde9171SAlan Somers 	while (1) {
19323cde9171SAlan Somers 		currentlen = inet6_opt_next(extbuf, extlen, currentlen,
19333cde9171SAlan Somers 		    &type, &len, &databuf);
19343cde9171SAlan Somers 		if (currentlen == -1)
19353cde9171SAlan Somers 			break;
19363cde9171SAlan Somers 		switch (type) {
19373cde9171SAlan Somers 		/*
19383cde9171SAlan Somers 		 * Note that inet6_opt_next automatically skips any padding
19393cde9171SAlan Somers 		 * optins.
19403cde9171SAlan Somers 		 */
19413cde9171SAlan Somers 		case IP6OPT_JUMBO:
19423cde9171SAlan Somers 			offset = 0;
19433cde9171SAlan Somers 			offset = inet6_opt_get_val(databuf, offset,
19443cde9171SAlan Somers 			    &value4, sizeof(value4));
19453cde9171SAlan Somers 			printf("    Jumbo Payload Opt: Length %u\n",
19463cde9171SAlan Somers 			    (u_int32_t)ntohl(value4));
19473cde9171SAlan Somers 			break;
19483cde9171SAlan Somers 		case IP6OPT_ROUTER_ALERT:
19493cde9171SAlan Somers 			offset = 0;
19503cde9171SAlan Somers 			offset = inet6_opt_get_val(databuf, offset,
19513cde9171SAlan Somers 						   &value2, sizeof(value2));
19523cde9171SAlan Somers 			printf("    Router Alert Opt: Type %u\n",
19533cde9171SAlan Somers 			    ntohs(value2));
19543cde9171SAlan Somers 			break;
19553cde9171SAlan Somers 		default:
19563cde9171SAlan Somers 			printf("    Received Opt %u len %lu\n",
19573cde9171SAlan Somers 			    type, (unsigned long)len);
19583cde9171SAlan Somers 			break;
19593cde9171SAlan Somers 		}
19603cde9171SAlan Somers 	}
19613cde9171SAlan Somers 	return;
19623cde9171SAlan Somers }
19633cde9171SAlan Somers 
19643cde9171SAlan Somers static void
19653cde9171SAlan Somers pr_rthdr(void *extbuf, size_t bufsize)
19663cde9171SAlan Somers {
19673cde9171SAlan Somers 	struct in6_addr *in6;
19683cde9171SAlan Somers 	char ntopbuf[INET6_ADDRSTRLEN];
19693cde9171SAlan Somers 	struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
19703cde9171SAlan Somers 	int i, segments, origsegs, rthsize, size0, size1;
19713cde9171SAlan Somers 
19723cde9171SAlan Somers 	/* print fixed part of the header */
19733cde9171SAlan Somers 	printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
19743cde9171SAlan Somers 	    rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
19753cde9171SAlan Somers 	if ((segments = inet6_rth_segments(extbuf)) >= 0) {
19763cde9171SAlan Somers 		printf("%d segments, ", segments);
19773cde9171SAlan Somers 		printf("%d left\n", rh->ip6r_segleft);
19783cde9171SAlan Somers 	} else {
19793cde9171SAlan Somers 		printf("segments unknown, ");
19803cde9171SAlan Somers 		printf("%d left\n", rh->ip6r_segleft);
19813cde9171SAlan Somers 		return;
19823cde9171SAlan Somers 	}
19833cde9171SAlan Somers 
19843cde9171SAlan Somers 	/*
19853cde9171SAlan Somers 	 * Bounds checking on the ancillary data buffer. When calculating
19863cde9171SAlan Somers 	 * the number of items to show keep in mind:
19873cde9171SAlan Somers 	 *	- The size of the cmsg structure
19883cde9171SAlan Somers 	 *	- The size of one segment (the size of a Type 0 routing header)
19893cde9171SAlan Somers 	 *	- When dividing add a fudge factor of one in case the
19903cde9171SAlan Somers 	 *	  dividend is not evenly divisible by the divisor
19913cde9171SAlan Somers 	 */
19923cde9171SAlan Somers 	rthsize = (rh->ip6r_len + 1) * 8;
19933cde9171SAlan Somers 	if (bufsize < (rthsize + CMSG_SPACE(0))) {
19943cde9171SAlan Somers 		origsegs = segments;
19953cde9171SAlan Somers 		size0 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0);
19963cde9171SAlan Somers 		size1 = inet6_rth_space(IPV6_RTHDR_TYPE_0, 1);
19973cde9171SAlan Somers 		segments -= (rthsize - (bufsize - CMSG_SPACE(0))) /
19983cde9171SAlan Somers 		    (size1 - size0) + 1;
19993cde9171SAlan Somers 		warnx("segments truncated, showing only %d (total=%d)",
20003cde9171SAlan Somers 		    segments, origsegs);
20013cde9171SAlan Somers 	}
20023cde9171SAlan Somers 
20033cde9171SAlan Somers 	for (i = 0; i < segments; i++) {
20043cde9171SAlan Somers 		in6 = inet6_rth_getaddr(extbuf, i);
20053cde9171SAlan Somers 		if (in6 == NULL)
20063cde9171SAlan Somers 			printf("   [%d]<NULL>\n", i);
20073cde9171SAlan Somers 		else {
20083cde9171SAlan Somers 			if (!inet_ntop(AF_INET6, in6, ntopbuf,
20093cde9171SAlan Somers 			    sizeof(ntopbuf)))
20103cde9171SAlan Somers 				strlcpy(ntopbuf, "?", sizeof(ntopbuf));
20113cde9171SAlan Somers 			printf("   [%d]%s\n", i, ntopbuf);
20123cde9171SAlan Somers 		}
20133cde9171SAlan Somers 	}
20143cde9171SAlan Somers 
20153cde9171SAlan Somers 	return;
20163cde9171SAlan Somers 
20173cde9171SAlan Somers }
20183cde9171SAlan Somers 
20193cde9171SAlan Somers static int
20203cde9171SAlan Somers pr_bitrange(u_int32_t v, int soff, int ii)
20213cde9171SAlan Somers {
20223cde9171SAlan Somers 	int off;
20233cde9171SAlan Somers 	int i;
20243cde9171SAlan Somers 
20253cde9171SAlan Somers 	off = 0;
20263cde9171SAlan Somers 	while (off < 32) {
20273cde9171SAlan Somers 		/* shift till we have 0x01 */
20283cde9171SAlan Somers 		if ((v & 0x01) == 0) {
20293cde9171SAlan Somers 			if (ii > 1)
20303cde9171SAlan Somers 				printf("-%u", soff + off - 1);
20313cde9171SAlan Somers 			ii = 0;
20323cde9171SAlan Somers 			switch (v & 0x0f) {
20333cde9171SAlan Somers 			case 0x00:
20343cde9171SAlan Somers 				v >>= 4;
20353cde9171SAlan Somers 				off += 4;
20363cde9171SAlan Somers 				continue;
20373cde9171SAlan Somers 			case 0x08:
20383cde9171SAlan Somers 				v >>= 3;
20393cde9171SAlan Somers 				off += 3;
20403cde9171SAlan Somers 				continue;
20413cde9171SAlan Somers 			case 0x04: case 0x0c:
20423cde9171SAlan Somers 				v >>= 2;
20433cde9171SAlan Somers 				off += 2;
20443cde9171SAlan Somers 				continue;
20453cde9171SAlan Somers 			default:
20463cde9171SAlan Somers 				v >>= 1;
20473cde9171SAlan Somers 				off += 1;
20483cde9171SAlan Somers 				continue;
20493cde9171SAlan Somers 			}
20503cde9171SAlan Somers 		}
20513cde9171SAlan Somers 
20523cde9171SAlan Somers 		/* we have 0x01 with us */
20533cde9171SAlan Somers 		for (i = 0; i < 32 - off; i++) {
20543cde9171SAlan Somers 			if ((v & (0x01 << i)) == 0)
20553cde9171SAlan Somers 				break;
20563cde9171SAlan Somers 		}
20573cde9171SAlan Somers 		if (!ii)
20583cde9171SAlan Somers 			printf(" %u", soff + off);
20593cde9171SAlan Somers 		ii += i;
20603cde9171SAlan Somers 		v >>= i; off += i;
20613cde9171SAlan Somers 	}
20623cde9171SAlan Somers 	return ii;
20633cde9171SAlan Somers }
20643cde9171SAlan Somers 
20653cde9171SAlan Somers static void
20663cde9171SAlan Somers pr_suptypes(struct icmp6_nodeinfo *ni, size_t nilen)
20673cde9171SAlan Somers 	/* ni->qtype must be SUPTYPES */
20683cde9171SAlan Somers {
20693cde9171SAlan Somers 	size_t clen;
20703cde9171SAlan Somers 	u_int32_t v;
20713cde9171SAlan Somers 	const u_char *cp, *end;
20723cde9171SAlan Somers 	u_int16_t cur;
20733cde9171SAlan Somers 	struct cbit {
20743cde9171SAlan Somers 		u_int16_t words;	/*32bit count*/
20753cde9171SAlan Somers 		u_int16_t skip;
20763cde9171SAlan Somers 	} cbit;
20773cde9171SAlan Somers #define MAXQTYPES	(1 << 16)
20783cde9171SAlan Somers 	size_t off;
20793cde9171SAlan Somers 	int b;
20803cde9171SAlan Somers 
20813cde9171SAlan Somers 	cp = (u_char *)(ni + 1);
20823cde9171SAlan Somers 	end = ((u_char *)ni) + nilen;
20833cde9171SAlan Somers 	cur = 0;
20843cde9171SAlan Somers 	b = 0;
20853cde9171SAlan Somers 
20863cde9171SAlan Somers 	printf("NodeInfo Supported Qtypes");
20873cde9171SAlan Somers 	if (options & F_VERBOSE) {
20883cde9171SAlan Somers 		if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
20893cde9171SAlan Somers 			printf(", compressed bitmap");
20903cde9171SAlan Somers 		else
20913cde9171SAlan Somers 			printf(", raw bitmap");
20923cde9171SAlan Somers 	}
20933cde9171SAlan Somers 
20943cde9171SAlan Somers 	while (cp < end) {
20953cde9171SAlan Somers 		clen = (size_t)(end - cp);
20963cde9171SAlan Somers 		if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
20973cde9171SAlan Somers 			if (clen == 0 || clen > MAXQTYPES / 8 ||
20983cde9171SAlan Somers 			    clen % sizeof(v)) {
20993cde9171SAlan Somers 				printf("???");
21003cde9171SAlan Somers 				return;
21013cde9171SAlan Somers 			}
21023cde9171SAlan Somers 		} else {
21033cde9171SAlan Somers 			if (clen < sizeof(cbit) || clen % sizeof(v))
21043cde9171SAlan Somers 				return;
21053cde9171SAlan Somers 			memcpy(&cbit, cp, sizeof(cbit));
21063cde9171SAlan Somers 			if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
21073cde9171SAlan Somers 			    clen)
21083cde9171SAlan Somers 				return;
21093cde9171SAlan Somers 			cp += sizeof(cbit);
21103cde9171SAlan Somers 			clen = ntohs(cbit.words) * sizeof(v);
21113cde9171SAlan Somers 			if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
21123cde9171SAlan Somers 			    MAXQTYPES)
21133cde9171SAlan Somers 				return;
21143cde9171SAlan Somers 		}
21153cde9171SAlan Somers 
21163cde9171SAlan Somers 		for (off = 0; off < clen; off += sizeof(v)) {
21173cde9171SAlan Somers 			memcpy(&v, cp + off, sizeof(v));
21183cde9171SAlan Somers 			v = (u_int32_t)ntohl(v);
21193cde9171SAlan Somers 			b = pr_bitrange(v, (int)(cur + off * 8), b);
21203cde9171SAlan Somers 		}
21213cde9171SAlan Somers 		/* flush the remaining bits */
21223cde9171SAlan Somers 		b = pr_bitrange(0, (int)(cur + off * 8), b);
21233cde9171SAlan Somers 
21243cde9171SAlan Somers 		cp += clen;
21253cde9171SAlan Somers 		cur += clen * 8;
21263cde9171SAlan Somers 		if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
21273cde9171SAlan Somers 			cur += ntohs(cbit.skip) * 32;
21283cde9171SAlan Somers 	}
21293cde9171SAlan Somers }
21303cde9171SAlan Somers 
21313cde9171SAlan Somers static void
21323cde9171SAlan Somers pr_nodeaddr(struct icmp6_nodeinfo *ni, int nilen)
21333cde9171SAlan Somers 	/* ni->qtype must be NODEADDR */
21343cde9171SAlan Somers {
21353cde9171SAlan Somers 	u_char *cp = (u_char *)(ni + 1);
21363cde9171SAlan Somers 	char ntop_buf[INET6_ADDRSTRLEN];
21373cde9171SAlan Somers 	int withttl = 0;
21383cde9171SAlan Somers 
21393cde9171SAlan Somers 	nilen -= sizeof(struct icmp6_nodeinfo);
21403cde9171SAlan Somers 
21413cde9171SAlan Somers 	if (options & F_VERBOSE) {
21423cde9171SAlan Somers 		switch (ni->ni_code) {
21433cde9171SAlan Somers 		case ICMP6_NI_REFUSED:
21443cde9171SAlan Somers 			(void)printf("refused");
21453cde9171SAlan Somers 			break;
21463cde9171SAlan Somers 		case ICMP6_NI_UNKNOWN:
21473cde9171SAlan Somers 			(void)printf("unknown qtype");
21483cde9171SAlan Somers 			break;
21493cde9171SAlan Somers 		}
21503cde9171SAlan Somers 		if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
21513cde9171SAlan Somers 			(void)printf(" truncated");
21523cde9171SAlan Somers 	}
21533cde9171SAlan Somers 	putchar('\n');
21543cde9171SAlan Somers 	if (nilen <= 0)
21553cde9171SAlan Somers 		printf("  no address\n");
21563cde9171SAlan Somers 
21573cde9171SAlan Somers 	/*
21583cde9171SAlan Somers 	 * In icmp-name-lookups 05 and later, TTL of each returned address
21593cde9171SAlan Somers 	 * is contained in the resposne. We try to detect the version
21603cde9171SAlan Somers 	 * by the length of the data, but note that the detection algorithm
21613cde9171SAlan Somers 	 * is incomplete. We assume the latest draft by default.
21623cde9171SAlan Somers 	 */
21633cde9171SAlan Somers 	if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
21643cde9171SAlan Somers 		withttl = 1;
21653cde9171SAlan Somers 	while (nilen > 0) {
21663cde9171SAlan Somers 		u_int32_t ttl = 0;
21673cde9171SAlan Somers 
21683cde9171SAlan Somers 		if (withttl) {
21693cde9171SAlan Somers 			uint32_t t;
21703cde9171SAlan Somers 
21713cde9171SAlan Somers 			memcpy(&t, cp, sizeof(t));
21723cde9171SAlan Somers 			ttl = (u_int32_t)ntohl(t);
21733cde9171SAlan Somers 			cp += sizeof(u_int32_t);
21743cde9171SAlan Somers 			nilen -= sizeof(u_int32_t);
21753cde9171SAlan Somers 		}
21763cde9171SAlan Somers 
21773cde9171SAlan Somers 		if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
21783cde9171SAlan Somers 		    NULL)
21793cde9171SAlan Somers 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
21803cde9171SAlan Somers 		printf("  %s", ntop_buf);
21813cde9171SAlan Somers 		if (withttl) {
21823cde9171SAlan Somers 			if (ttl == 0xffffffff) {
21833cde9171SAlan Somers 				/*
21843cde9171SAlan Somers 				 * XXX: can this convention be applied to all
21853cde9171SAlan Somers 				 * type of TTL (i.e. non-ND TTL)?
21863cde9171SAlan Somers 				 */
21873cde9171SAlan Somers 				printf("(TTL=infty)");
21883cde9171SAlan Somers 			}
21893cde9171SAlan Somers 			else
21903cde9171SAlan Somers 				printf("(TTL=%u)", ttl);
21913cde9171SAlan Somers 		}
21923cde9171SAlan Somers 		putchar('\n');
21933cde9171SAlan Somers 
21943cde9171SAlan Somers 		nilen -= sizeof(struct in6_addr);
21953cde9171SAlan Somers 		cp += sizeof(struct in6_addr);
21963cde9171SAlan Somers 	}
21973cde9171SAlan Somers }
21983cde9171SAlan Somers 
21993cde9171SAlan Somers static int
22003cde9171SAlan Somers get_hoplim(struct msghdr *mhdr)
22013cde9171SAlan Somers {
22023cde9171SAlan Somers 	struct cmsghdr *cm;
22033cde9171SAlan Somers 
22043cde9171SAlan Somers 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
22053cde9171SAlan Somers 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
22063cde9171SAlan Somers 		if (cm->cmsg_len == 0)
22073cde9171SAlan Somers 			return(-1);
22083cde9171SAlan Somers 
22093cde9171SAlan Somers 		if (cm->cmsg_level == IPPROTO_IPV6 &&
22103cde9171SAlan Somers 		    cm->cmsg_type == IPV6_HOPLIMIT &&
22113cde9171SAlan Somers 		    cm->cmsg_len == CMSG_LEN(sizeof(int))) {
22123cde9171SAlan Somers 			int r;
22133cde9171SAlan Somers 
22143cde9171SAlan Somers 			memcpy(&r, CMSG_DATA(cm), sizeof(r));
22153cde9171SAlan Somers 			return(r);
22163cde9171SAlan Somers 		}
22173cde9171SAlan Somers 	}
22183cde9171SAlan Somers 
22193cde9171SAlan Somers 	return(-1);
22203cde9171SAlan Somers }
22213cde9171SAlan Somers 
22223cde9171SAlan Somers static struct in6_pktinfo *
22233cde9171SAlan Somers get_rcvpktinfo(struct msghdr *mhdr)
22243cde9171SAlan Somers {
22253cde9171SAlan Somers 	static struct in6_pktinfo pi;
22263cde9171SAlan Somers 	struct cmsghdr *cm;
22273cde9171SAlan Somers 
22283cde9171SAlan Somers 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
22293cde9171SAlan Somers 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
22303cde9171SAlan Somers 		if (cm->cmsg_len == 0)
22313cde9171SAlan Somers 			return(NULL);
22323cde9171SAlan Somers 
22333cde9171SAlan Somers 		if (cm->cmsg_level == IPPROTO_IPV6 &&
22343cde9171SAlan Somers 		    cm->cmsg_type == IPV6_PKTINFO &&
22353cde9171SAlan Somers 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
22363cde9171SAlan Somers 			memcpy(&pi, CMSG_DATA(cm), sizeof(pi));
22373cde9171SAlan Somers 			return(&pi);
22383cde9171SAlan Somers 		}
22393cde9171SAlan Somers 	}
22403cde9171SAlan Somers 
22413cde9171SAlan Somers 	return(NULL);
22423cde9171SAlan Somers }
22433cde9171SAlan Somers 
22443cde9171SAlan Somers static int
22453cde9171SAlan Somers get_pathmtu(struct msghdr *mhdr)
22463cde9171SAlan Somers {
22473cde9171SAlan Somers #ifdef IPV6_RECVPATHMTU
22483cde9171SAlan Somers 	struct cmsghdr *cm;
22493cde9171SAlan Somers 	struct ip6_mtuinfo mtuctl;
22503cde9171SAlan Somers 
22513cde9171SAlan Somers 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
22523cde9171SAlan Somers 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
22533cde9171SAlan Somers 		if (cm->cmsg_len == 0)
22543cde9171SAlan Somers 			return(0);
22553cde9171SAlan Somers 
22563cde9171SAlan Somers 		if (cm->cmsg_level == IPPROTO_IPV6 &&
22573cde9171SAlan Somers 		    cm->cmsg_type == IPV6_PATHMTU &&
22583cde9171SAlan Somers 		    cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
22593cde9171SAlan Somers 			memcpy(&mtuctl, CMSG_DATA(cm), sizeof(mtuctl));
22603cde9171SAlan Somers 
22613cde9171SAlan Somers 			/*
22623cde9171SAlan Somers 			 * If the notified destination is different from
22633cde9171SAlan Somers 			 * the one we are pinging, just ignore the info.
22643cde9171SAlan Somers 			 * We check the scope ID only when both notified value
22653cde9171SAlan Somers 			 * and our own value have non-0 values, because we may
22663cde9171SAlan Somers 			 * have used the default scope zone ID for sending,
22673cde9171SAlan Somers 			 * in which case the scope ID value is 0.
22683cde9171SAlan Somers 			 */
22693cde9171SAlan Somers 			if (!IN6_ARE_ADDR_EQUAL(&mtuctl.ip6m_addr.sin6_addr,
22703cde9171SAlan Somers 						&dst.sin6_addr) ||
22713cde9171SAlan Somers 			    (mtuctl.ip6m_addr.sin6_scope_id &&
22723cde9171SAlan Somers 			     dst.sin6_scope_id &&
22733cde9171SAlan Somers 			     mtuctl.ip6m_addr.sin6_scope_id !=
22743cde9171SAlan Somers 			     dst.sin6_scope_id)) {
22753cde9171SAlan Somers 				if ((options & F_VERBOSE) != 0) {
22763cde9171SAlan Somers 					printf("path MTU for %s is notified. "
22773cde9171SAlan Somers 					       "(ignored)\n",
22783cde9171SAlan Somers 					   pr_addr((struct sockaddr *)&mtuctl.ip6m_addr,
22793cde9171SAlan Somers 					   sizeof(mtuctl.ip6m_addr)));
22803cde9171SAlan Somers 				}
22813cde9171SAlan Somers 				return(0);
22823cde9171SAlan Somers 			}
22833cde9171SAlan Somers 
22843cde9171SAlan Somers 			/*
22853cde9171SAlan Somers 			 * Ignore an invalid MTU. XXX: can we just believe
22863cde9171SAlan Somers 			 * the kernel check?
22873cde9171SAlan Somers 			 */
22883cde9171SAlan Somers 			if (mtuctl.ip6m_mtu < IPV6_MMTU)
22893cde9171SAlan Somers 				return(0);
22903cde9171SAlan Somers 
22913cde9171SAlan Somers 			/* notification for our destination. return the MTU. */
22923cde9171SAlan Somers 			return((int)mtuctl.ip6m_mtu);
22933cde9171SAlan Somers 		}
22943cde9171SAlan Somers 	}
22953cde9171SAlan Somers #endif
22963cde9171SAlan Somers 	return(0);
22973cde9171SAlan Somers }
22983cde9171SAlan Somers 
22993cde9171SAlan Somers /*
23003cde9171SAlan Somers  * onint --
23013cde9171SAlan Somers  *	SIGINT handler.
23023cde9171SAlan Somers  */
23033cde9171SAlan Somers /* ARGSUSED */
23043cde9171SAlan Somers static void
23053cde9171SAlan Somers onint(int notused __unused)
23063cde9171SAlan Somers {
23073cde9171SAlan Somers 	/*
23083cde9171SAlan Somers 	 * When doing reverse DNS lookups, the seenint flag might not
23093cde9171SAlan Somers 	 * be noticed for a while.  Just exit if we get a second SIGINT.
23103cde9171SAlan Somers 	 */
23113cde9171SAlan Somers 	if ((options & F_HOSTNAME) && seenint != 0)
23123cde9171SAlan Somers 		_exit(nreceived ? 0 : 2);
23133cde9171SAlan Somers }
23143cde9171SAlan Somers 
23153cde9171SAlan Somers /*
23163cde9171SAlan Somers  * summary --
23173cde9171SAlan Somers  *	Print out statistics.
23183cde9171SAlan Somers  */
23193cde9171SAlan Somers static void
23203cde9171SAlan Somers summary(void)
23213cde9171SAlan Somers {
23223cde9171SAlan Somers 
23233cde9171SAlan Somers 	(void)printf("\n--- %s ping6 statistics ---\n", hostname);
23243cde9171SAlan Somers 	(void)printf("%ld packets transmitted, ", ntransmitted);
23253cde9171SAlan Somers 	(void)printf("%ld packets received, ", nreceived);
23263cde9171SAlan Somers 	if (nrepeats)
23273cde9171SAlan Somers 		(void)printf("+%ld duplicates, ", nrepeats);
23283cde9171SAlan Somers 	if (ntransmitted) {
23293cde9171SAlan Somers 		if (nreceived > ntransmitted)
23303cde9171SAlan Somers 			(void)printf("-- somebody's duplicating packets!");
23313cde9171SAlan Somers 		else
23323cde9171SAlan Somers 			(void)printf("%.1f%% packet loss",
23333cde9171SAlan Somers 			    ((((double)ntransmitted - nreceived) * 100.0) /
23343cde9171SAlan Somers 			    ntransmitted));
23353cde9171SAlan Somers 	}
23363cde9171SAlan Somers 	if (nrcvtimeout)
23373cde9171SAlan Somers 		printf(", %ld packets out of wait time", nrcvtimeout);
23383cde9171SAlan Somers 	(void)putchar('\n');
23393cde9171SAlan Somers 	if (nreceived && timing) {
23403cde9171SAlan Somers 		/* Only display average to microseconds */
23413cde9171SAlan Somers 		double num = nreceived + nrepeats;
23423cde9171SAlan Somers 		double avg = tsum / num;
23433cde9171SAlan Somers 		double dev = sqrt(tsumsq / num - avg * avg);
23443cde9171SAlan Somers 		(void)printf(
23453cde9171SAlan Somers 		    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
23463cde9171SAlan Somers 		    tmin, avg, tmax, dev);
23473cde9171SAlan Somers 		(void)fflush(stdout);
23483cde9171SAlan Somers 	}
23493cde9171SAlan Somers 	(void)fflush(stdout);
23503cde9171SAlan Somers }
23513cde9171SAlan Somers 
23523cde9171SAlan Somers /*subject type*/
23533cde9171SAlan Somers static const char *niqcode[] = {
23543cde9171SAlan Somers 	"IPv6 address",
23553cde9171SAlan Somers 	"DNS label",	/*or empty*/
23563cde9171SAlan Somers 	"IPv4 address",
23573cde9171SAlan Somers };
23583cde9171SAlan Somers 
23593cde9171SAlan Somers /*result code*/
23603cde9171SAlan Somers static const char *nircode[] = {
23613cde9171SAlan Somers 	"Success", "Refused", "Unknown",
23623cde9171SAlan Somers };
23633cde9171SAlan Somers 
23643cde9171SAlan Somers 
23653cde9171SAlan Somers /*
23663cde9171SAlan Somers  * pr_icmph --
23673cde9171SAlan Somers  *	Print a descriptive string about an ICMP header.
23683cde9171SAlan Somers  */
23693cde9171SAlan Somers static void
23703cde9171SAlan Somers pr_icmph(struct icmp6_hdr *icp, u_char *end)
23713cde9171SAlan Somers {
23723cde9171SAlan Somers 	char ntop_buf[INET6_ADDRSTRLEN];
23733cde9171SAlan Somers 	struct nd_redirect *red;
23743cde9171SAlan Somers 	struct icmp6_nodeinfo *ni;
23753cde9171SAlan Somers 	char dnsname[MAXDNAME + 1];
23763cde9171SAlan Somers 	const u_char *cp;
23773cde9171SAlan Somers 	size_t l;
23783cde9171SAlan Somers 
23793cde9171SAlan Somers 	switch (icp->icmp6_type) {
23803cde9171SAlan Somers 	case ICMP6_DST_UNREACH:
23813cde9171SAlan Somers 		switch (icp->icmp6_code) {
23823cde9171SAlan Somers 		case ICMP6_DST_UNREACH_NOROUTE:
23833cde9171SAlan Somers 			(void)printf("No Route to Destination\n");
23843cde9171SAlan Somers 			break;
23853cde9171SAlan Somers 		case ICMP6_DST_UNREACH_ADMIN:
23863cde9171SAlan Somers 			(void)printf("Destination Administratively "
23873cde9171SAlan Somers 			    "Unreachable\n");
23883cde9171SAlan Somers 			break;
23893cde9171SAlan Somers 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
23903cde9171SAlan Somers 			(void)printf("Destination Unreachable Beyond Scope\n");
23913cde9171SAlan Somers 			break;
23923cde9171SAlan Somers 		case ICMP6_DST_UNREACH_ADDR:
23933cde9171SAlan Somers 			(void)printf("Destination Host Unreachable\n");
23943cde9171SAlan Somers 			break;
23953cde9171SAlan Somers 		case ICMP6_DST_UNREACH_NOPORT:
23963cde9171SAlan Somers 			(void)printf("Destination Port Unreachable\n");
23973cde9171SAlan Somers 			break;
23983cde9171SAlan Somers 		default:
23993cde9171SAlan Somers 			(void)printf("Destination Unreachable, Bad Code: %d\n",
24003cde9171SAlan Somers 			    icp->icmp6_code);
24013cde9171SAlan Somers 			break;
24023cde9171SAlan Somers 		}
24033cde9171SAlan Somers 		/* Print returned IP header information */
24043cde9171SAlan Somers 		pr_retip((struct ip6_hdr *)(icp + 1), end);
24053cde9171SAlan Somers 		break;
24063cde9171SAlan Somers 	case ICMP6_PACKET_TOO_BIG:
24073cde9171SAlan Somers 		(void)printf("Packet too big mtu = %d\n",
24083cde9171SAlan Somers 		    (int)ntohl(icp->icmp6_mtu));
24093cde9171SAlan Somers 		pr_retip((struct ip6_hdr *)(icp + 1), end);
24103cde9171SAlan Somers 		break;
24113cde9171SAlan Somers 	case ICMP6_TIME_EXCEEDED:
24123cde9171SAlan Somers 		switch (icp->icmp6_code) {
24133cde9171SAlan Somers 		case ICMP6_TIME_EXCEED_TRANSIT:
24143cde9171SAlan Somers 			(void)printf("Time to live exceeded\n");
24153cde9171SAlan Somers 			break;
24163cde9171SAlan Somers 		case ICMP6_TIME_EXCEED_REASSEMBLY:
24173cde9171SAlan Somers 			(void)printf("Frag reassembly time exceeded\n");
24183cde9171SAlan Somers 			break;
24193cde9171SAlan Somers 		default:
24203cde9171SAlan Somers 			(void)printf("Time exceeded, Bad Code: %d\n",
24213cde9171SAlan Somers 			    icp->icmp6_code);
24223cde9171SAlan Somers 			break;
24233cde9171SAlan Somers 		}
24243cde9171SAlan Somers 		pr_retip((struct ip6_hdr *)(icp + 1), end);
24253cde9171SAlan Somers 		break;
24263cde9171SAlan Somers 	case ICMP6_PARAM_PROB:
24273cde9171SAlan Somers 		(void)printf("Parameter problem: ");
24283cde9171SAlan Somers 		switch (icp->icmp6_code) {
24293cde9171SAlan Somers 		case ICMP6_PARAMPROB_HEADER:
24303cde9171SAlan Somers 			(void)printf("Erroneous Header ");
24313cde9171SAlan Somers 			break;
24323cde9171SAlan Somers 		case ICMP6_PARAMPROB_NEXTHEADER:
24333cde9171SAlan Somers 			(void)printf("Unknown Nextheader ");
24343cde9171SAlan Somers 			break;
24353cde9171SAlan Somers 		case ICMP6_PARAMPROB_OPTION:
24363cde9171SAlan Somers 			(void)printf("Unrecognized Option ");
24373cde9171SAlan Somers 			break;
24383cde9171SAlan Somers 		default:
24393cde9171SAlan Somers 			(void)printf("Bad code(%d) ", icp->icmp6_code);
24403cde9171SAlan Somers 			break;
24413cde9171SAlan Somers 		}
24423cde9171SAlan Somers 		(void)printf("pointer = 0x%02x\n",
24433cde9171SAlan Somers 		    (u_int32_t)ntohl(icp->icmp6_pptr));
24443cde9171SAlan Somers 		pr_retip((struct ip6_hdr *)(icp + 1), end);
24453cde9171SAlan Somers 		break;
24463cde9171SAlan Somers 	case ICMP6_ECHO_REQUEST:
24473cde9171SAlan Somers 		(void)printf("Echo Request");
24483cde9171SAlan Somers 		/* XXX ID + Seq + Data */
24493cde9171SAlan Somers 		break;
24503cde9171SAlan Somers 	case ICMP6_ECHO_REPLY:
24513cde9171SAlan Somers 		(void)printf("Echo Reply");
24523cde9171SAlan Somers 		/* XXX ID + Seq + Data */
24533cde9171SAlan Somers 		break;
24543cde9171SAlan Somers 	case ICMP6_MEMBERSHIP_QUERY:
24553cde9171SAlan Somers 		(void)printf("Listener Query");
24563cde9171SAlan Somers 		break;
24573cde9171SAlan Somers 	case ICMP6_MEMBERSHIP_REPORT:
24583cde9171SAlan Somers 		(void)printf("Listener Report");
24593cde9171SAlan Somers 		break;
24603cde9171SAlan Somers 	case ICMP6_MEMBERSHIP_REDUCTION:
24613cde9171SAlan Somers 		(void)printf("Listener Done");
24623cde9171SAlan Somers 		break;
24633cde9171SAlan Somers 	case ND_ROUTER_SOLICIT:
24643cde9171SAlan Somers 		(void)printf("Router Solicitation");
24653cde9171SAlan Somers 		break;
24663cde9171SAlan Somers 	case ND_ROUTER_ADVERT:
24673cde9171SAlan Somers 		(void)printf("Router Advertisement");
24683cde9171SAlan Somers 		break;
24693cde9171SAlan Somers 	case ND_NEIGHBOR_SOLICIT:
24703cde9171SAlan Somers 		(void)printf("Neighbor Solicitation");
24713cde9171SAlan Somers 		break;
24723cde9171SAlan Somers 	case ND_NEIGHBOR_ADVERT:
24733cde9171SAlan Somers 		(void)printf("Neighbor Advertisement");
24743cde9171SAlan Somers 		break;
24753cde9171SAlan Somers 	case ND_REDIRECT:
24763cde9171SAlan Somers 		red = (struct nd_redirect *)icp;
24773cde9171SAlan Somers 		(void)printf("Redirect\n");
24783cde9171SAlan Somers 		if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
24793cde9171SAlan Somers 		    sizeof(ntop_buf)))
24803cde9171SAlan Somers 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
24813cde9171SAlan Somers 		(void)printf("Destination: %s", ntop_buf);
24823cde9171SAlan Somers 		if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
24833cde9171SAlan Somers 		    sizeof(ntop_buf)))
24843cde9171SAlan Somers 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
24853cde9171SAlan Somers 		(void)printf(" New Target: %s", ntop_buf);
24863cde9171SAlan Somers 		break;
24873cde9171SAlan Somers 	case ICMP6_NI_QUERY:
24883cde9171SAlan Somers 		(void)printf("Node Information Query");
24893cde9171SAlan Somers 		/* XXX ID + Seq + Data */
24903cde9171SAlan Somers 		ni = (struct icmp6_nodeinfo *)icp;
24913cde9171SAlan Somers 		l = end - (u_char *)(ni + 1);
24923cde9171SAlan Somers 		printf(", ");
24933cde9171SAlan Somers 		switch (ntohs(ni->ni_qtype)) {
24943cde9171SAlan Somers 		case NI_QTYPE_NOOP:
24953cde9171SAlan Somers 			(void)printf("NOOP");
24963cde9171SAlan Somers 			break;
24973cde9171SAlan Somers 		case NI_QTYPE_SUPTYPES:
24983cde9171SAlan Somers 			(void)printf("Supported qtypes");
24993cde9171SAlan Somers 			break;
25003cde9171SAlan Somers 		case NI_QTYPE_FQDN:
25013cde9171SAlan Somers 			(void)printf("DNS name");
25023cde9171SAlan Somers 			break;
25033cde9171SAlan Somers 		case NI_QTYPE_NODEADDR:
25043cde9171SAlan Somers 			(void)printf("nodeaddr");
25053cde9171SAlan Somers 			break;
25063cde9171SAlan Somers 		case NI_QTYPE_IPV4ADDR:
25073cde9171SAlan Somers 			(void)printf("IPv4 nodeaddr");
25083cde9171SAlan Somers 			break;
25093cde9171SAlan Somers 		default:
25103cde9171SAlan Somers 			(void)printf("unknown qtype");
25113cde9171SAlan Somers 			break;
25123cde9171SAlan Somers 		}
25133cde9171SAlan Somers 		if (options & F_VERBOSE) {
25143cde9171SAlan Somers 			switch (ni->ni_code) {
25153cde9171SAlan Somers 			case ICMP6_NI_SUBJ_IPV6:
25163cde9171SAlan Somers 				if (l == sizeof(struct in6_addr) &&
25173cde9171SAlan Somers 				    inet_ntop(AF_INET6, ni + 1, ntop_buf,
25183cde9171SAlan Somers 				    sizeof(ntop_buf)) != NULL) {
25193cde9171SAlan Somers 					(void)printf(", subject=%s(%s)",
25203cde9171SAlan Somers 					    niqcode[ni->ni_code], ntop_buf);
25213cde9171SAlan Somers 				} else {
25223cde9171SAlan Somers #if 1
25233cde9171SAlan Somers 					/* backward compat to -W */
25243cde9171SAlan Somers 					(void)printf(", oldfqdn");
25253cde9171SAlan Somers #else
25263cde9171SAlan Somers 					(void)printf(", invalid");
25273cde9171SAlan Somers #endif
25283cde9171SAlan Somers 				}
25293cde9171SAlan Somers 				break;
25303cde9171SAlan Somers 			case ICMP6_NI_SUBJ_FQDN:
25313cde9171SAlan Somers 				if (end == (u_char *)(ni + 1)) {
25323cde9171SAlan Somers 					(void)printf(", no subject");
25333cde9171SAlan Somers 					break;
25343cde9171SAlan Somers 				}
25353cde9171SAlan Somers 				printf(", subject=%s", niqcode[ni->ni_code]);
25363cde9171SAlan Somers 				cp = (const u_char *)(ni + 1);
25373cde9171SAlan Somers 				cp = dnsdecode(cp, end, NULL, dnsname,
25383cde9171SAlan Somers 				    sizeof(dnsname));
25393cde9171SAlan Somers 				if (cp != NULL)
25403cde9171SAlan Somers 					printf("(%s)", dnsname);
25413cde9171SAlan Somers 				else
25423cde9171SAlan Somers 					printf("(invalid)");
25433cde9171SAlan Somers 				break;
25443cde9171SAlan Somers 			case ICMP6_NI_SUBJ_IPV4:
25453cde9171SAlan Somers 				if (l == sizeof(struct in_addr) &&
25463cde9171SAlan Somers 				    inet_ntop(AF_INET, ni + 1, ntop_buf,
25473cde9171SAlan Somers 				    sizeof(ntop_buf)) != NULL) {
25483cde9171SAlan Somers 					(void)printf(", subject=%s(%s)",
25493cde9171SAlan Somers 					    niqcode[ni->ni_code], ntop_buf);
25503cde9171SAlan Somers 				} else
25513cde9171SAlan Somers 					(void)printf(", invalid");
25523cde9171SAlan Somers 				break;
25533cde9171SAlan Somers 			default:
25543cde9171SAlan Somers 				(void)printf(", invalid");
25553cde9171SAlan Somers 				break;
25563cde9171SAlan Somers 			}
25573cde9171SAlan Somers 		}
25583cde9171SAlan Somers 		break;
25593cde9171SAlan Somers 	case ICMP6_NI_REPLY:
25603cde9171SAlan Somers 		(void)printf("Node Information Reply");
25613cde9171SAlan Somers 		/* XXX ID + Seq + Data */
25623cde9171SAlan Somers 		ni = (struct icmp6_nodeinfo *)icp;
25633cde9171SAlan Somers 		printf(", ");
25643cde9171SAlan Somers 		switch (ntohs(ni->ni_qtype)) {
25653cde9171SAlan Somers 		case NI_QTYPE_NOOP:
25663cde9171SAlan Somers 			(void)printf("NOOP");
25673cde9171SAlan Somers 			break;
25683cde9171SAlan Somers 		case NI_QTYPE_SUPTYPES:
25693cde9171SAlan Somers 			(void)printf("Supported qtypes");
25703cde9171SAlan Somers 			break;
25713cde9171SAlan Somers 		case NI_QTYPE_FQDN:
25723cde9171SAlan Somers 			(void)printf("DNS name");
25733cde9171SAlan Somers 			break;
25743cde9171SAlan Somers 		case NI_QTYPE_NODEADDR:
25753cde9171SAlan Somers 			(void)printf("nodeaddr");
25763cde9171SAlan Somers 			break;
25773cde9171SAlan Somers 		case NI_QTYPE_IPV4ADDR:
25783cde9171SAlan Somers 			(void)printf("IPv4 nodeaddr");
25793cde9171SAlan Somers 			break;
25803cde9171SAlan Somers 		default:
25813cde9171SAlan Somers 			(void)printf("unknown qtype");
25823cde9171SAlan Somers 			break;
25833cde9171SAlan Somers 		}
25843cde9171SAlan Somers 		if (options & F_VERBOSE) {
25853cde9171SAlan Somers 			if (ni->ni_code > nitems(nircode))
25863cde9171SAlan Somers 				printf(", invalid");
25873cde9171SAlan Somers 			else
25883cde9171SAlan Somers 				printf(", %s", nircode[ni->ni_code]);
25893cde9171SAlan Somers 		}
25903cde9171SAlan Somers 		break;
25913cde9171SAlan Somers 	default:
25923cde9171SAlan Somers 		(void)printf("Bad ICMP type: %d", icp->icmp6_type);
25933cde9171SAlan Somers 	}
25943cde9171SAlan Somers }
25953cde9171SAlan Somers 
25963cde9171SAlan Somers /*
25973cde9171SAlan Somers  * pr_iph --
25983cde9171SAlan Somers  *	Print an IP6 header.
25993cde9171SAlan Somers  */
26003cde9171SAlan Somers static void
26013cde9171SAlan Somers pr_iph(struct ip6_hdr *ip6)
26023cde9171SAlan Somers {
26033cde9171SAlan Somers 	u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
26043cde9171SAlan Somers 	u_int8_t tc;
26053cde9171SAlan Somers 	char ntop_buf[INET6_ADDRSTRLEN];
26063cde9171SAlan Somers 
26073cde9171SAlan Somers 	tc = *(&ip6->ip6_vfc + 1); /* XXX */
26083cde9171SAlan Somers 	tc = (tc >> 4) & 0x0f;
26093cde9171SAlan Somers 	tc |= (ip6->ip6_vfc << 4);
26103cde9171SAlan Somers 
26113cde9171SAlan Somers 	printf("Vr TC  Flow Plen Nxt Hlim\n");
26123cde9171SAlan Somers 	printf(" %1x %02x %05x %04x  %02x   %02x\n",
26133cde9171SAlan Somers 	    (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
26143cde9171SAlan Somers 	    ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
26153cde9171SAlan Somers 	if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
26163cde9171SAlan Somers 		strlcpy(ntop_buf, "?", sizeof(ntop_buf));
26173cde9171SAlan Somers 	printf("%s->", ntop_buf);
26183cde9171SAlan Somers 	if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
26193cde9171SAlan Somers 		strlcpy(ntop_buf, "?", sizeof(ntop_buf));
26203cde9171SAlan Somers 	printf("%s\n", ntop_buf);
26213cde9171SAlan Somers }
26223cde9171SAlan Somers 
26233cde9171SAlan Somers /*
26243cde9171SAlan Somers  * pr_addr --
26253cde9171SAlan Somers  *	Return an ascii host address as a dotted quad and optionally with
26263cde9171SAlan Somers  * a hostname.
26273cde9171SAlan Somers  */
26283cde9171SAlan Somers static const char *
26293cde9171SAlan Somers pr_addr(struct sockaddr *addr, int addrlen)
26303cde9171SAlan Somers {
26313cde9171SAlan Somers 	static char buf[NI_MAXHOST];
26323cde9171SAlan Somers 	int flag = 0;
26333cde9171SAlan Somers 
26343cde9171SAlan Somers 	if ((options & F_HOSTNAME) == 0)
26353cde9171SAlan Somers 		flag |= NI_NUMERICHOST;
26363cde9171SAlan Somers 
26373cde9171SAlan Somers 	if (cap_getnameinfo(capdns, addr, addrlen, buf, sizeof(buf), NULL, 0,
26383cde9171SAlan Somers 		flag) == 0)
26393cde9171SAlan Somers 		return (buf);
26403cde9171SAlan Somers 	else
26413cde9171SAlan Somers 		return "?";
26423cde9171SAlan Somers }
26433cde9171SAlan Somers 
26443cde9171SAlan Somers /*
26453cde9171SAlan Somers  * pr_retip --
26463cde9171SAlan Somers  *	Dump some info on a returned (via ICMPv6) IPv6 packet.
26473cde9171SAlan Somers  */
26483cde9171SAlan Somers static void
26493cde9171SAlan Somers pr_retip(struct ip6_hdr *ip6, u_char *end)
26503cde9171SAlan Somers {
26513cde9171SAlan Somers 	u_char *cp = (u_char *)ip6, nh;
26523cde9171SAlan Somers 	int hlen;
26533cde9171SAlan Somers 
26543cde9171SAlan Somers 	if ((size_t)(end - (u_char *)ip6) < sizeof(*ip6)) {
26553cde9171SAlan Somers 		printf("IP6");
26563cde9171SAlan Somers 		goto trunc;
26573cde9171SAlan Somers 	}
26583cde9171SAlan Somers 	pr_iph(ip6);
26593cde9171SAlan Somers 	hlen = sizeof(*ip6);
26603cde9171SAlan Somers 
26613cde9171SAlan Somers 	nh = ip6->ip6_nxt;
26623cde9171SAlan Somers 	cp += hlen;
26633cde9171SAlan Somers 	while (end - cp >= 8) {
2664*9ce201f2SAlan Somers #ifdef IPSEC
26653cde9171SAlan Somers 		struct ah ah;
2666*9ce201f2SAlan Somers #endif
26673cde9171SAlan Somers 
26683cde9171SAlan Somers 		switch (nh) {
26693cde9171SAlan Somers 		case IPPROTO_HOPOPTS:
26703cde9171SAlan Somers 			printf("HBH ");
26713cde9171SAlan Somers 			hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
26723cde9171SAlan Somers 			nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
26733cde9171SAlan Somers 			break;
26743cde9171SAlan Somers 		case IPPROTO_DSTOPTS:
26753cde9171SAlan Somers 			printf("DSTOPT ");
26763cde9171SAlan Somers 			hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
26773cde9171SAlan Somers 			nh = ((struct ip6_dest *)cp)->ip6d_nxt;
26783cde9171SAlan Somers 			break;
26793cde9171SAlan Somers 		case IPPROTO_FRAGMENT:
26803cde9171SAlan Somers 			printf("FRAG ");
26813cde9171SAlan Somers 			hlen = sizeof(struct ip6_frag);
26823cde9171SAlan Somers 			nh = ((struct ip6_frag *)cp)->ip6f_nxt;
26833cde9171SAlan Somers 			break;
26843cde9171SAlan Somers 		case IPPROTO_ROUTING:
26853cde9171SAlan Somers 			printf("RTHDR ");
26863cde9171SAlan Somers 			hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
26873cde9171SAlan Somers 			nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
26883cde9171SAlan Somers 			break;
26893cde9171SAlan Somers #ifdef IPSEC
26903cde9171SAlan Somers 		case IPPROTO_AH:
26913cde9171SAlan Somers 			printf("AH ");
26923cde9171SAlan Somers 			memcpy(&ah, cp, sizeof(ah));
26933cde9171SAlan Somers 			hlen = (ah.ah_len+2) << 2;
26943cde9171SAlan Somers 			nh = ah.ah_nxt;
26953cde9171SAlan Somers 			break;
26963cde9171SAlan Somers #endif
26973cde9171SAlan Somers 		case IPPROTO_ICMPV6:
26983cde9171SAlan Somers 			printf("ICMP6: type = %d, code = %d\n",
26993cde9171SAlan Somers 			    *cp, *(cp + 1));
27003cde9171SAlan Somers 			return;
27013cde9171SAlan Somers 		case IPPROTO_ESP:
27023cde9171SAlan Somers 			printf("ESP\n");
27033cde9171SAlan Somers 			return;
27043cde9171SAlan Somers 		case IPPROTO_TCP:
27053cde9171SAlan Somers 			printf("TCP: from port %u, to port %u (decimal)\n",
27063cde9171SAlan Somers 			    (*cp * 256 + *(cp + 1)),
27073cde9171SAlan Somers 			    (*(cp + 2) * 256 + *(cp + 3)));
27083cde9171SAlan Somers 			return;
27093cde9171SAlan Somers 		case IPPROTO_UDP:
27103cde9171SAlan Somers 			printf("UDP: from port %u, to port %u (decimal)\n",
27113cde9171SAlan Somers 			    (*cp * 256 + *(cp + 1)),
27123cde9171SAlan Somers 			    (*(cp + 2) * 256 + *(cp + 3)));
27133cde9171SAlan Somers 			return;
27143cde9171SAlan Somers 		default:
27153cde9171SAlan Somers 			printf("Unknown Header(%d)\n", nh);
27163cde9171SAlan Somers 			return;
27173cde9171SAlan Somers 		}
27183cde9171SAlan Somers 
27193cde9171SAlan Somers 		if ((cp += hlen) >= end)
27203cde9171SAlan Somers 			goto trunc;
27213cde9171SAlan Somers 	}
27223cde9171SAlan Somers 	if (end - cp < 8)
27233cde9171SAlan Somers 		goto trunc;
27243cde9171SAlan Somers 
27253cde9171SAlan Somers 	putchar('\n');
27263cde9171SAlan Somers 	return;
27273cde9171SAlan Somers 
27283cde9171SAlan Somers   trunc:
27293cde9171SAlan Somers 	printf("...\n");
27303cde9171SAlan Somers 	return;
27313cde9171SAlan Somers }
27323cde9171SAlan Somers 
27333cde9171SAlan Somers static void
27343cde9171SAlan Somers fill(char *bp, char *patp)
27353cde9171SAlan Somers {
27363cde9171SAlan Somers 	int ii, jj, kk;
27373cde9171SAlan Somers 	int pat[16];
27383cde9171SAlan Somers 	char *cp;
27393cde9171SAlan Somers 
27403cde9171SAlan Somers 	for (cp = patp; *cp; cp++)
27413cde9171SAlan Somers 		if (!isxdigit(*cp))
27423cde9171SAlan Somers 			errx(1, "patterns must be specified as hex digits");
27433cde9171SAlan Somers 	ii = sscanf(patp,
27443cde9171SAlan Somers 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
27453cde9171SAlan Somers 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
27463cde9171SAlan Somers 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
27473cde9171SAlan Somers 	    &pat[13], &pat[14], &pat[15]);
27483cde9171SAlan Somers 
27493cde9171SAlan Somers /* xxx */
27503cde9171SAlan Somers 	if (ii > 0)
27513cde9171SAlan Somers 		for (kk = 0;
27523cde9171SAlan Somers 		    (size_t)kk <= MAXDATALEN - 8 + sizeof(struct tv32) + ii;
27533cde9171SAlan Somers 		    kk += ii)
27543cde9171SAlan Somers 			for (jj = 0; jj < ii; ++jj)
27553cde9171SAlan Somers 				bp[jj + kk] = pat[jj];
27563cde9171SAlan Somers 	if (!(options & F_QUIET)) {
27573cde9171SAlan Somers 		(void)printf("PATTERN: 0x");
27583cde9171SAlan Somers 		for (jj = 0; jj < ii; ++jj)
27593cde9171SAlan Somers 			(void)printf("%02x", bp[jj] & 0xFF);
27603cde9171SAlan Somers 		(void)printf("\n");
27613cde9171SAlan Somers 	}
27623cde9171SAlan Somers }
27633cde9171SAlan Somers 
27643cde9171SAlan Somers #ifdef IPSEC
27653cde9171SAlan Somers #ifdef IPSEC_POLICY_IPSEC
27663cde9171SAlan Somers static int
27673cde9171SAlan Somers setpolicy(int so __unused, char *policy)
27683cde9171SAlan Somers {
27693cde9171SAlan Somers 	char *buf;
27703cde9171SAlan Somers 
27713cde9171SAlan Somers 	if (policy == NULL)
27723cde9171SAlan Somers 		return 0;	/* ignore */
27733cde9171SAlan Somers 
27743cde9171SAlan Somers 	buf = ipsec_set_policy(policy, strlen(policy));
27753cde9171SAlan Somers 	if (buf == NULL)
27763cde9171SAlan Somers 		errx(1, "%s", ipsec_strerror());
27773cde9171SAlan Somers 	if (setsockopt(ssend, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
27783cde9171SAlan Somers 	    ipsec_get_policylen(buf)) < 0)
27793cde9171SAlan Somers 		warnx("Unable to set IPsec policy");
27803cde9171SAlan Somers 	free(buf);
27813cde9171SAlan Somers 
27823cde9171SAlan Somers 	return 0;
27833cde9171SAlan Somers }
27843cde9171SAlan Somers #endif
27853cde9171SAlan Somers #endif
27863cde9171SAlan Somers 
27873cde9171SAlan Somers static char *
27883cde9171SAlan Somers nigroup(char *name, int nig_oldmcprefix)
27893cde9171SAlan Somers {
27903cde9171SAlan Somers 	char *p;
27913cde9171SAlan Somers 	char *q;
27923cde9171SAlan Somers 	MD5_CTX ctxt;
27933cde9171SAlan Somers 	u_int8_t digest[16];
27943cde9171SAlan Somers 	u_int8_t c;
27953cde9171SAlan Somers 	size_t l;
27963cde9171SAlan Somers 	char hbuf[NI_MAXHOST];
27973cde9171SAlan Somers 	struct in6_addr in6;
27983cde9171SAlan Somers 	int valid;
27993cde9171SAlan Somers 
28003cde9171SAlan Somers 	p = strchr(name, '.');
28013cde9171SAlan Somers 	if (!p)
28023cde9171SAlan Somers 		p = name + strlen(name);
28033cde9171SAlan Somers 	l = p - name;
28043cde9171SAlan Somers 	if (l > 63 || l > sizeof(hbuf) - 1)
28053cde9171SAlan Somers 		return NULL;	/*label too long*/
28063cde9171SAlan Somers 	strncpy(hbuf, name, l);
28073cde9171SAlan Somers 	hbuf[(int)l] = '\0';
28083cde9171SAlan Somers 
28093cde9171SAlan Somers 	for (q = name; *q; q++) {
28103cde9171SAlan Somers 		if (isupper(*(unsigned char *)q))
28113cde9171SAlan Somers 			*q = tolower(*(unsigned char *)q);
28123cde9171SAlan Somers 	}
28133cde9171SAlan Somers 
28143cde9171SAlan Somers 	/* generate 16 bytes of pseudo-random value. */
28153cde9171SAlan Somers 	memset(&ctxt, 0, sizeof(ctxt));
28163cde9171SAlan Somers 	MD5Init(&ctxt);
28173cde9171SAlan Somers 	c = l & 0xff;
28183cde9171SAlan Somers 	MD5Update(&ctxt, &c, sizeof(c));
28193cde9171SAlan Somers 	MD5Update(&ctxt, (unsigned char *)name, l);
28203cde9171SAlan Somers 	MD5Final(digest, &ctxt);
28213cde9171SAlan Somers 
28223cde9171SAlan Somers 	if (nig_oldmcprefix) {
28233cde9171SAlan Somers 		/* draft-ietf-ipngwg-icmp-name-lookup */
28243cde9171SAlan Somers 		valid = inet_pton(AF_INET6, "ff02::2:0000:0000", &in6);
28253cde9171SAlan Somers 	} else {
28263cde9171SAlan Somers 		/* RFC 4620 */
28273cde9171SAlan Somers 		valid = inet_pton(AF_INET6, "ff02::2:ff00:0000", &in6);
28283cde9171SAlan Somers 	}
28293cde9171SAlan Somers 	if (valid != 1)
28303cde9171SAlan Somers 		return NULL;	/*XXX*/
28313cde9171SAlan Somers 
28323cde9171SAlan Somers 	if (nig_oldmcprefix) {
28333cde9171SAlan Somers 		/* draft-ietf-ipngwg-icmp-name-lookup */
28343cde9171SAlan Somers 		bcopy(digest, &in6.s6_addr[12], 4);
28353cde9171SAlan Somers 	} else {
28363cde9171SAlan Somers 		/* RFC 4620 */
28373cde9171SAlan Somers 		bcopy(digest, &in6.s6_addr[13], 3);
28383cde9171SAlan Somers 	}
28393cde9171SAlan Somers 
28403cde9171SAlan Somers 	if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
28413cde9171SAlan Somers 		return NULL;
28423cde9171SAlan Somers 
28433cde9171SAlan Somers 	return strdup(hbuf);
28443cde9171SAlan Somers }
28453cde9171SAlan Somers 
28463cde9171SAlan Somers static cap_channel_t *
28473cde9171SAlan Somers capdns_setup(void)
28483cde9171SAlan Somers {
28493cde9171SAlan Somers 	cap_channel_t *capcas, *capdnsloc;
28503cde9171SAlan Somers #ifdef WITH_CASPER
28513cde9171SAlan Somers 	const char *types[2];
28523cde9171SAlan Somers 	int families[1];
28533cde9171SAlan Somers #endif
28543cde9171SAlan Somers 	capcas = cap_init();
28553cde9171SAlan Somers 	if (capcas == NULL)
28563cde9171SAlan Somers 		err(1, "unable to create casper process");
28573cde9171SAlan Somers 	capdnsloc = cap_service_open(capcas, "system.dns");
28583cde9171SAlan Somers 	/* Casper capability no longer needed. */
28593cde9171SAlan Somers 	cap_close(capcas);
28603cde9171SAlan Somers 	if (capdnsloc == NULL)
28613cde9171SAlan Somers 		err(1, "unable to open system.dns service");
28623cde9171SAlan Somers #ifdef WITH_CASPER
28633cde9171SAlan Somers 	types[0] = "NAME2ADDR";
28643cde9171SAlan Somers 	types[1] = "ADDR2NAME";
28653cde9171SAlan Somers 	if (cap_dns_type_limit(capdnsloc, types, nitems(types)) < 0)
28663cde9171SAlan Somers 		err(1, "unable to limit access to system.dns service");
28673cde9171SAlan Somers 	families[0] = AF_INET6;
28683cde9171SAlan Somers 	if (cap_dns_family_limit(capdnsloc, families, nitems(families)) < 0)
28693cde9171SAlan Somers 		err(1, "unable to limit access to system.dns service");
28703cde9171SAlan Somers #endif
28713cde9171SAlan Somers 	return (capdnsloc);
28723cde9171SAlan Somers }
2873