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