Home | History | Annotate | Line # | Download | only in ping6
ping6.c revision 1.82
      1  1.82  christos /*	$NetBSD: ping6.c,v 1.82 2013/10/19 01:09:16 christos Exp $	*/
      2  1.52    itojun /*	$KAME: ping6.c,v 1.164 2002/11/16 14:05:37 itojun Exp $	*/
      3   1.6    itojun 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1    itojun  * All rights reserved.
      7  1.15    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19  1.15    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  */
     32   1.1    itojun 
     33   1.1    itojun /*	BSDI	ping.c,v 2.3 1996/01/21 17:56:50 jch Exp	*/
     34   1.1    itojun 
     35   1.1    itojun /*
     36   1.1    itojun  * Copyright (c) 1989, 1993
     37   1.1    itojun  *	The Regents of the University of California.  All rights reserved.
     38   1.1    itojun  *
     39   1.1    itojun  * This code is derived from software contributed to Berkeley by
     40   1.1    itojun  * Mike Muuss.
     41   1.1    itojun  *
     42   1.1    itojun  * Redistribution and use in source and binary forms, with or without
     43   1.1    itojun  * modification, are permitted provided that the following conditions
     44   1.1    itojun  * are met:
     45   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     46   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     47   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     49   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     50  1.57       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.1    itojun  *    may be used to endorse or promote products derived from this software
     52   1.1    itojun  *    without specific prior written permission.
     53   1.1    itojun  *
     54   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1    itojun  * SUCH DAMAGE.
     65   1.1    itojun  */
     66   1.1    itojun 
     67   1.4    itojun #if 0
     68   1.1    itojun #ifndef lint
     69   1.6    itojun static char copyright[] =
     70   1.1    itojun "@(#) Copyright (c) 1989, 1993\n\
     71   1.6    itojun 	The Regents of the University of California.  All rights reserved.\n";
     72   1.6    itojun #endif /* not lint */
     73   1.6    itojun 
     74   1.6    itojun #ifndef lint
     75   1.6    itojun static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
     76   1.1    itojun #endif /* not lint */
     77   1.6    itojun #else
     78   1.6    itojun #include <sys/cdefs.h>
     79   1.6    itojun #ifndef lint
     80  1.82  christos __RCSID("$NetBSD: ping6.c,v 1.82 2013/10/19 01:09:16 christos Exp $");
     81   1.4    itojun #endif
     82   1.1    itojun #endif
     83   1.1    itojun 
     84   1.1    itojun /*
     85   1.1    itojun  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
     86   1.1    itojun  * measure round-trip-delays and packet loss across network paths.
     87   1.1    itojun  *
     88   1.1    itojun  * Author -
     89   1.1    itojun  *	Mike Muuss
     90   1.1    itojun  *	U. S. Army Ballistic Research Laboratory
     91   1.1    itojun  *	December, 1983
     92   1.1    itojun  *
     93   1.1    itojun  * Status -
     94   1.1    itojun  *	Public Domain.  Distribution Unlimited.
     95   1.1    itojun  * Bugs -
     96   1.1    itojun  *	More statistics could always be gathered.
     97   1.1    itojun  *	This program has to run SUID to ROOT to access the ICMP socket.
     98   1.1    itojun  */
     99   1.9    itojun /*
    100   1.9    itojun  * NOTE:
    101   1.9    itojun  * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
    102  1.28    itojun  * as IPV6_PKTINFO.  Some people object it (sin6_scope_id specifies *link*
    103  1.28    itojun  * while IPV6_PKTINFO specifies *interface*.  Link is defined as collection of
    104   1.9    itojun  * network attached to 1 or more interfaces)
    105   1.9    itojun  */
    106   1.1    itojun 
    107   1.1    itojun #include <sys/param.h>
    108   1.1    itojun #include <sys/uio.h>
    109   1.1    itojun #include <sys/socket.h>
    110   1.1    itojun #include <sys/time.h>
    111   1.1    itojun 
    112   1.1    itojun #include <net/if.h>
    113   1.1    itojun #include <net/route.h>
    114   1.1    itojun 
    115   1.1    itojun #include <netinet/in.h>
    116   1.1    itojun #include <netinet/ip6.h>
    117   1.1    itojun #include <netinet/icmp6.h>
    118   1.1    itojun #include <arpa/inet.h>
    119  1.17    itojun #include <arpa/nameser.h>
    120   1.1    itojun #include <netdb.h>
    121   1.1    itojun 
    122   1.1    itojun #include <ctype.h>
    123   1.1    itojun #include <err.h>
    124   1.1    itojun #include <errno.h>
    125   1.1    itojun #include <fcntl.h>
    126  1.18    itojun #include <math.h>
    127   1.1    itojun #include <signal.h>
    128   1.1    itojun #include <stdio.h>
    129   1.1    itojun #include <stdlib.h>
    130   1.1    itojun #include <string.h>
    131   1.1    itojun #include <unistd.h>
    132  1.48   mycroft #include <poll.h>
    133   1.1    itojun 
    134   1.1    itojun #ifdef IPSEC
    135  1.79  drochner #include <netinet/ip6.h>
    136  1.80  drochner #include <netipsec/ipsec.h>
    137   1.1    itojun #endif
    138   1.1    itojun 
    139  1.15    itojun #include <md5.h>
    140  1.15    itojun 
    141  1.41    itojun struct tv32 {
    142  1.41    itojun 	u_int32_t tv32_sec;
    143  1.41    itojun 	u_int32_t tv32_usec;
    144  1.41    itojun };
    145  1.37    itojun 
    146   1.1    itojun #define MAXPACKETLEN	131072
    147   1.1    itojun #define	IP6LEN		40
    148   1.1    itojun #define ICMP6ECHOLEN	8	/* icmp echo header len excluding time */
    149  1.41    itojun #define ICMP6ECHOTMLEN sizeof(struct tv32)
    150   1.1    itojun #define ICMP6_NIQLEN	(ICMP6ECHOLEN + 8)
    151  1.17    itojun /* FQDN case, 64 bits of nonce + 32 bits ttl */
    152  1.17    itojun #define ICMP6_NIRLEN	(ICMP6ECHOLEN + 12)
    153   1.1    itojun #define	EXTRA		256	/* for AH and various other headers. weird. */
    154   1.1    itojun #define	DEFDATALEN	ICMP6ECHOTMLEN
    155   1.1    itojun #define MAXDATALEN	MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
    156   1.1    itojun #define	NROUTES		9		/* number of record route slots */
    157   1.1    itojun 
    158   1.1    itojun #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
    159   1.1    itojun #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
    160   1.1    itojun #define	SET(bit)	(A(bit) |= B(bit))
    161   1.1    itojun #define	CLR(bit)	(A(bit) &= (~B(bit)))
    162   1.1    itojun #define	TST(bit)	(A(bit) & B(bit))
    163   1.1    itojun 
    164   1.1    itojun #define	F_FLOOD		0x0001
    165   1.1    itojun #define	F_INTERVAL	0x0002
    166   1.1    itojun #define	F_PINGFILLED	0x0008
    167   1.1    itojun #define	F_QUIET		0x0010
    168   1.1    itojun #define	F_RROUTE	0x0020
    169   1.1    itojun #define	F_SO_DEBUG	0x0040
    170   1.1    itojun #define	F_VERBOSE	0x0100
    171   1.1    itojun #ifdef IPSEC
    172   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
    173   1.9    itojun #define	F_POLICY	0x0400
    174   1.1    itojun #else
    175   1.1    itojun #define F_AUTHHDR	0x0200
    176   1.1    itojun #define F_ENCRYPT	0x0400
    177   1.1    itojun #endif /*IPSEC_POLICY_IPSEC*/
    178   1.1    itojun #endif /*IPSEC*/
    179   1.1    itojun #define F_NODEADDR	0x0800
    180   1.1    itojun #define F_FQDN		0x1000
    181   1.1    itojun #define F_INTERFACE	0x2000
    182   1.9    itojun #define F_SRCADDR	0x4000
    183  1.11    itojun #ifdef IPV6_REACHCONF
    184  1.11    itojun #define F_REACHCONF	0x8000
    185  1.11    itojun #endif
    186  1.15    itojun #define F_HOSTNAME	0x10000
    187  1.15    itojun #define F_FQDNOLD	0x20000
    188  1.15    itojun #define F_NIGROUP	0x40000
    189  1.17    itojun #define F_SUPTYPES	0x80000
    190  1.36    itojun #define F_NOMINMTU	0x100000
    191  1.17    itojun #define F_NOUSERDATA	(F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
    192  1.75     joerg static u_int options;
    193   1.1    itojun 
    194   1.1    itojun #define IN6LEN		sizeof(struct in6_addr)
    195   1.1    itojun #define SA6LEN		sizeof(struct sockaddr_in6)
    196  1.28    itojun #define DUMMY_PORT	10101
    197   1.1    itojun 
    198  1.28    itojun #define SIN6(s)	((struct sockaddr_in6 *)(s))
    199   1.1    itojun 
    200   1.1    itojun /*
    201   1.1    itojun  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
    202   1.1    itojun  * number of received sequence numbers we can keep track of.  Change 128
    203   1.1    itojun  * to 8192 for complete accuracy...
    204   1.1    itojun  */
    205   1.1    itojun #define	MAX_DUP_CHK	(8 * 8192)
    206  1.75     joerg static int mx_dup_ck = MAX_DUP_CHK;
    207  1.75     joerg static char rcvd_tbl[MAX_DUP_CHK / 8];
    208   1.1    itojun 
    209  1.75     joerg static struct addrinfo *res;
    210  1.75     joerg static struct sockaddr_in6 dst;	/* who to ping6 */
    211  1.75     joerg static struct sockaddr_in6 src;	/* src addr of this packet */
    212  1.75     joerg static socklen_t srclen;
    213  1.75     joerg static int datalen = DEFDATALEN;
    214  1.75     joerg static int s;				/* socket file descriptor */
    215  1.75     joerg static u_char outpack[MAXPACKETLEN];
    216  1.75     joerg static char BSPACE = '\b';		/* characters written for flood */
    217  1.75     joerg static char DOT = '.';
    218  1.75     joerg static char *hostname;
    219  1.75     joerg static int ident;			/* process id to identify our packets */
    220  1.75     joerg static u_int8_t nonce[8];		/* nonce field for node information */
    221  1.75     joerg static int hoplimit = -1;		/* hoplimit */
    222   1.1    itojun 
    223   1.1    itojun /* counters */
    224  1.75     joerg static long npackets;			/* max packets to transmit */
    225  1.75     joerg static long nreceived;			/* # of packets we got back */
    226  1.75     joerg static long nrepeats;			/* number of duplicates */
    227  1.75     joerg static long ntransmitted;		/* sequence # for outbound packets = #sent */
    228  1.75     joerg static struct timeval interval = {1, 0}; /* interval between packets */
    229   1.1    itojun 
    230   1.1    itojun /* timing */
    231  1.75     joerg static int timing;			/* flag to do timing */
    232  1.75     joerg static double tmin = 999999999.0;	/* minimum round trip time */
    233  1.75     joerg static double tmax = 0.0;		/* maximum round trip time */
    234  1.75     joerg static double tsum = 0.0;		/* sum of all times, for doing average */
    235  1.75     joerg static double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
    236   1.1    itojun 
    237   1.1    itojun /* for node addresses */
    238  1.75     joerg static u_short naflags;
    239   1.1    itojun 
    240   1.1    itojun /* for ancillary data(advanced API) */
    241  1.75     joerg static struct msghdr smsghdr;
    242  1.75     joerg static struct iovec smsgiov;
    243  1.75     joerg static char *scmsg = 0;
    244   1.1    itojun 
    245  1.75     joerg static volatile sig_atomic_t seenalrm;
    246  1.75     joerg static volatile sig_atomic_t seenint;
    247  1.31    itojun #ifdef SIGINFO
    248  1.75     joerg static volatile sig_atomic_t seeninfo;
    249  1.31    itojun #endif
    250  1.30    itojun 
    251  1.75     joerg static void	 fill(char *, char *);
    252  1.75     joerg static int	 get_hoplim(struct msghdr *);
    253  1.75     joerg static int	 get_pathmtu(struct msghdr *);
    254  1.75     joerg static struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
    255  1.75     joerg static void	 onsignal(int);
    256  1.75     joerg static void	 retransmit(void);
    257  1.75     joerg __dead static void	 onint(int);
    258  1.75     joerg static size_t	 pingerlen(void);
    259  1.75     joerg static int	 pinger(void);
    260  1.75     joerg static const char *pr_addr(struct sockaddr *, int);
    261  1.75     joerg static void	 pr_icmph(struct icmp6_hdr *, u_char *);
    262  1.75     joerg static void	 pr_iph(struct ip6_hdr *);
    263  1.75     joerg static void	 pr_suptypes(struct icmp6_nodeinfo *, size_t);
    264  1.75     joerg static void	 pr_nodeaddr(struct icmp6_nodeinfo *, int);
    265  1.75     joerg static int	 myechoreply(const struct icmp6_hdr *);
    266  1.75     joerg static int	 mynireply(const struct icmp6_nodeinfo *);
    267  1.75     joerg static char *dnsdecode(const u_char **, const u_char *, const u_char *,
    268  1.62   xtraeme 	char *, size_t);
    269  1.75     joerg static void	 pr_pack(u_char *, int, struct msghdr *);
    270  1.75     joerg static void	 pr_exthdrs(struct msghdr *);
    271  1.75     joerg static void	 pr_ip6opt(void *);
    272  1.75     joerg static void	 pr_rthdr(void *);
    273  1.75     joerg static int	 pr_bitrange(u_int32_t, int, int);
    274  1.75     joerg static void	 pr_retip(struct ip6_hdr *, u_char *);
    275  1.75     joerg static void	 summary(void);
    276  1.75     joerg static void	 tvsub(struct timeval *, struct timeval *);
    277  1.75     joerg static int	 setpolicy(int, char *);
    278  1.75     joerg static char	*nigroup(char *);
    279  1.75     joerg __dead static void	 usage(void);
    280   1.1    itojun 
    281   1.1    itojun int
    282  1.62   xtraeme main(int argc, char *argv[])
    283   1.1    itojun {
    284   1.1    itojun 	struct itimerval itimer;
    285   1.1    itojun 	struct sockaddr_in6 from;
    286  1.48   mycroft 	int timeout;
    287   1.1    itojun 	struct addrinfo hints;
    288  1.48   mycroft 	struct pollfd fdmaskp[1];
    289  1.72     lukem 	int cc;
    290  1.72     lukem 	u_int i, packlen;
    291  1.72     lukem 	int ch, hold, preload, optval, ret_ga;
    292   1.1    itojun 	u_char *datap, *packet;
    293  1.41    itojun 	char *e, *target, *ifname = NULL, *gateway = NULL;
    294   1.1    itojun 	int ip6optlen = 0;
    295   1.1    itojun 	struct cmsghdr *scmsgp = NULL;
    296  1.51    itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
    297  1.51    itojun 	u_long lsockbufsize;
    298   1.1    itojun 	int sockbufsize = 0;
    299  1.51    itojun #endif
    300   1.9    itojun 	int usepktinfo = 0;
    301   1.9    itojun 	struct in6_pktinfo *pktinfo = NULL;
    302  1.11    itojun 	struct ip6_rthdr *rthdr = NULL;
    303   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
    304  1.11    itojun 	char *policy_in = NULL;
    305  1.11    itojun 	char *policy_out = NULL;
    306   1.1    itojun #endif
    307  1.18    itojun 	double intval;
    308  1.22    itojun 	size_t rthlen;
    309  1.41    itojun #ifdef IPV6_USE_MIN_MTU
    310  1.41    itojun 	int mflag = 0;
    311  1.41    itojun #endif
    312   1.1    itojun 
    313   1.1    itojun 	/* just to be sure */
    314  1.53    itojun 	memset(&smsghdr, 0, sizeof(smsghdr));
    315  1.53    itojun 	memset(&smsgiov, 0, sizeof(smsgiov));
    316   1.1    itojun 
    317   1.1    itojun 	preload = 0;
    318   1.1    itojun 	datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
    319   1.1    itojun #ifndef IPSEC
    320  1.24    itojun #define ADDOPTS
    321   1.1    itojun #else
    322   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
    323  1.24    itojun #define ADDOPTS	"P:"
    324   1.1    itojun #else
    325  1.24    itojun #define ADDOPTS	"AE"
    326   1.1    itojun #endif /*IPSEC_POLICY_IPSEC*/
    327   1.1    itojun #endif
    328  1.28    itojun 	while ((ch = getopt(argc, argv,
    329  1.41    itojun 	    "a:b:c:dfHg:h:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {
    330  1.24    itojun #undef ADDOPTS
    331  1.17    itojun 		switch (ch) {
    332  1.17    itojun 		case 'a':
    333  1.17    itojun 		{
    334  1.17    itojun 			char *cp;
    335  1.17    itojun 
    336  1.17    itojun 			options &= ~F_NOUSERDATA;
    337  1.17    itojun 			options |= F_NODEADDR;
    338  1.17    itojun 			for (cp = optarg; *cp != '\0'; cp++) {
    339  1.17    itojun 				switch (*cp) {
    340  1.17    itojun 				case 'a':
    341  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_ALL;
    342  1.17    itojun 					break;
    343  1.17    itojun 				case 'c':
    344  1.17    itojun 				case 'C':
    345  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_COMPAT;
    346  1.17    itojun 					break;
    347  1.17    itojun 				case 'l':
    348  1.17    itojun 				case 'L':
    349  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
    350  1.17    itojun 					break;
    351  1.17    itojun 				case 's':
    352  1.17    itojun 				case 'S':
    353  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_SITELOCAL;
    354  1.17    itojun 					break;
    355  1.17    itojun 				case 'g':
    356  1.17    itojun 				case 'G':
    357  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_GLOBAL;
    358  1.17    itojun 					break;
    359  1.17    itojun 				case 'A': /* experimental. not in the spec */
    360  1.31    itojun #ifdef NI_NODEADDR_FLAG_ANYCAST
    361  1.17    itojun 					naflags |= NI_NODEADDR_FLAG_ANYCAST;
    362  1.17    itojun 					break;
    363  1.31    itojun #else
    364  1.31    itojun 					errx(1,
    365  1.31    itojun "-a A is not supported on the platform");
    366  1.31    itojun 					/*NOTREACHED*/
    367  1.31    itojun #endif
    368  1.17    itojun 				default:
    369  1.17    itojun 					usage();
    370  1.17    itojun 					/*NOTREACHED*/
    371  1.17    itojun 				}
    372  1.17    itojun 			}
    373  1.17    itojun 			break;
    374  1.17    itojun 		}
    375  1.17    itojun 		case 'b':
    376   1.1    itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
    377  1.51    itojun 			errno = 0;
    378  1.51    itojun 			e = NULL;
    379  1.51    itojun 			lsockbufsize = strtoul(optarg, &e, 10);
    380  1.51    itojun 			sockbufsize = lsockbufsize;
    381  1.51    itojun 			if (errno || !*optarg || *e ||
    382  1.72     lukem 			    (u_long)sockbufsize != lsockbufsize)
    383  1.51    itojun 				errx(1, "invalid socket buffer size");
    384   1.1    itojun #else
    385  1.41    itojun 			errx(1,
    386   1.1    itojun "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
    387   1.1    itojun #endif
    388   1.1    itojun 			break;
    389   1.1    itojun 		case 'c':
    390   1.1    itojun 			npackets = strtol(optarg, &e, 10);
    391   1.1    itojun 			if (npackets <= 0 || *optarg == '\0' || *e != '\0')
    392   1.1    itojun 				errx(1,
    393   1.1    itojun 				    "illegal number of packets -- %s", optarg);
    394   1.1    itojun 			break;
    395   1.1    itojun 		case 'd':
    396   1.1    itojun 			options |= F_SO_DEBUG;
    397   1.1    itojun 			break;
    398   1.1    itojun 		case 'f':
    399   1.1    itojun 			if (getuid()) {
    400   1.1    itojun 				errno = EPERM;
    401   1.3   thorpej 				errx(1, "Must be superuser to flood ping");
    402   1.1    itojun 			}
    403   1.1    itojun 			options |= F_FLOOD;
    404  1.78    plunky 			setbuf(stdout, NULL);
    405   1.1    itojun 			break;
    406  1.41    itojun 		case 'g':
    407  1.41    itojun 			gateway = optarg;
    408  1.41    itojun 			break;
    409  1.15    itojun 		case 'H':
    410  1.15    itojun 			options |= F_HOSTNAME;
    411  1.15    itojun 			break;
    412   1.1    itojun 		case 'h':		/* hoplimit */
    413   1.1    itojun 			hoplimit = strtol(optarg, &e, 10);
    414  1.41    itojun 			if (*optarg == '\0' || *e != '\0')
    415  1.41    itojun 				errx(1, "illegal hoplimit %s", optarg);
    416   1.1    itojun 			if (255 < hoplimit || hoplimit < -1)
    417   1.1    itojun 				errx(1,
    418   1.1    itojun 				    "illegal hoplimit -- %s", optarg);
    419   1.1    itojun 			break;
    420   1.1    itojun 		case 'I':
    421   1.1    itojun 			ifname = optarg;
    422   1.1    itojun 			options |= F_INTERFACE;
    423   1.9    itojun #ifndef USE_SIN6_SCOPE_ID
    424   1.9    itojun 			usepktinfo++;
    425   1.9    itojun #endif
    426   1.1    itojun 			break;
    427   1.1    itojun 		case 'i':		/* wait between sending packets */
    428  1.18    itojun 			intval = strtod(optarg, &e);
    429  1.18    itojun 			if (*optarg == '\0' || *e != '\0')
    430  1.18    itojun 				errx(1, "illegal timing interval %s", optarg);
    431  1.18    itojun 			if (intval < 1 && getuid()) {
    432  1.18    itojun 				errx(1, "%s: only root may use interval < 1s",
    433  1.18    itojun 				    strerror(EPERM));
    434  1.18    itojun 			}
    435  1.18    itojun 			interval.tv_sec = (long)intval;
    436  1.18    itojun 			interval.tv_usec =
    437  1.18    itojun 			    (long)((intval - interval.tv_sec) * 1000000);
    438  1.18    itojun 			if (interval.tv_sec < 0)
    439  1.18    itojun 				errx(1, "illegal timing interval %s", optarg);
    440  1.18    itojun 			/* less than 1/hz does not make sense */
    441  1.18    itojun 			if (interval.tv_sec == 0 && interval.tv_usec < 10000) {
    442  1.18    itojun 				warnx("too small interval, raised to 0.01");
    443  1.18    itojun 				interval.tv_usec = 10000;
    444  1.18    itojun 			}
    445   1.1    itojun 			options |= F_INTERVAL;
    446   1.1    itojun 			break;
    447   1.1    itojun 		case 'l':
    448  1.11    itojun 			if (getuid()) {
    449  1.11    itojun 				errno = EPERM;
    450  1.11    itojun 				errx(1, "Must be superuser to preload");
    451  1.11    itojun 			}
    452   1.1    itojun 			preload = strtol(optarg, &e, 10);
    453   1.1    itojun 			if (preload < 0 || *optarg == '\0' || *e != '\0')
    454   1.1    itojun 				errx(1, "illegal preload value -- %s", optarg);
    455  1.35    itojun 			break;
    456  1.36    itojun 		case 'm':
    457  1.36    itojun #ifdef IPV6_USE_MIN_MTU
    458  1.41    itojun 			mflag++;
    459  1.36    itojun 			break;
    460  1.36    itojun #else
    461  1.36    itojun 			errx(1, "-%c is not supported on this platform", ch);
    462  1.36    itojun 			/*NOTREACHED*/
    463  1.36    itojun #endif
    464  1.35    itojun 		case 'n':
    465  1.35    itojun 			options &= ~F_HOSTNAME;
    466   1.1    itojun 			break;
    467  1.15    itojun 		case 'N':
    468  1.15    itojun 			options |= F_NIGROUP;
    469  1.15    itojun 			break;
    470   1.1    itojun 		case 'p':		/* fill buffer with user pattern */
    471   1.1    itojun 			options |= F_PINGFILLED;
    472   1.1    itojun 			fill((char *)datap, optarg);
    473   1.1    itojun 				break;
    474   1.1    itojun 		case 'q':
    475   1.1    itojun 			options |= F_QUIET;
    476   1.1    itojun 			break;
    477  1.12    itojun 		case 'R':
    478  1.11    itojun #ifdef IPV6_REACHCONF
    479  1.11    itojun 			options |= F_REACHCONF;
    480   1.1    itojun 			break;
    481  1.12    itojun #else
    482  1.12    itojun 			errx(1, "-R is not supported in this configuration");
    483  1.11    itojun #endif
    484   1.9    itojun 		case 'S':
    485  1.41    itojun 			memset(&hints, 0, sizeof(struct addrinfo));
    486  1.37    itojun 			hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */
    487  1.37    itojun 			hints.ai_family = AF_INET6;
    488  1.37    itojun 			hints.ai_socktype = SOCK_RAW;
    489  1.37    itojun 			hints.ai_protocol = IPPROTO_ICMPV6;
    490  1.37    itojun 
    491  1.37    itojun 			ret_ga = getaddrinfo(optarg, NULL, &hints, &res);
    492  1.37    itojun 			if (ret_ga) {
    493  1.37    itojun 				errx(1, "invalid source address: %s",
    494  1.37    itojun 				     gai_strerror(ret_ga));
    495  1.37    itojun 			}
    496  1.37    itojun 			/*
    497  1.37    itojun 			 * res->ai_family must be AF_INET6 and res->ai_addrlen
    498  1.37    itojun 			 * must be sizeof(src).
    499  1.37    itojun 			 */
    500  1.37    itojun 			memcpy(&src, res->ai_addr, res->ai_addrlen);
    501  1.37    itojun 			srclen = res->ai_addrlen;
    502  1.37    itojun 			freeaddrinfo(res);
    503   1.9    itojun 			options |= F_SRCADDR;
    504   1.9    itojun 			break;
    505   1.1    itojun 		case 's':		/* size of packet to send */
    506   1.1    itojun 			datalen = strtol(optarg, &e, 10);
    507  1.81   msaitoh 			if (datalen < 0 || *optarg == '\0' || *e != '\0')
    508   1.1    itojun 				errx(1, "illegal datalen value -- %s", optarg);
    509  1.17    itojun 			if (datalen > MAXDATALEN) {
    510   1.1    itojun 				errx(1,
    511   1.1    itojun 				    "datalen value too large, maximum is %d",
    512   1.1    itojun 				    MAXDATALEN);
    513  1.17    itojun 			}
    514  1.17    itojun 			break;
    515  1.17    itojun 		case 't':
    516  1.17    itojun 			options &= ~F_NOUSERDATA;
    517  1.17    itojun 			options |= F_SUPTYPES;
    518   1.1    itojun 			break;
    519   1.1    itojun 		case 'v':
    520   1.1    itojun 			options |= F_VERBOSE;
    521   1.1    itojun 			break;
    522   1.1    itojun 		case 'w':
    523  1.17    itojun 			options &= ~F_NOUSERDATA;
    524  1.15    itojun 			options |= F_FQDN;
    525  1.15    itojun 			break;
    526   1.1    itojun 		case 'W':
    527  1.17    itojun 			options &= ~F_NOUSERDATA;
    528  1.15    itojun 			options |= F_FQDNOLD;
    529   1.1    itojun 			break;
    530   1.1    itojun #ifdef IPSEC
    531   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
    532   1.1    itojun 		case 'P':
    533   1.1    itojun 			options |= F_POLICY;
    534  1.25    itojun 			if (!strncmp("in", optarg, 2)) {
    535  1.25    itojun 				if ((policy_in = strdup(optarg)) == NULL)
    536  1.25    itojun 					errx(1, "strdup");
    537  1.25    itojun 			} else if (!strncmp("out", optarg, 3)) {
    538  1.25    itojun 				if ((policy_out = strdup(optarg)) == NULL)
    539  1.25    itojun 					errx(1, "strdup");
    540  1.25    itojun 			} else
    541  1.11    itojun 				errx(1, "invalid security policy");
    542   1.1    itojun 			break;
    543   1.1    itojun #else
    544   1.1    itojun 		case 'A':
    545   1.1    itojun 			options |= F_AUTHHDR;
    546   1.1    itojun 			break;
    547   1.1    itojun 		case 'E':
    548   1.1    itojun 			options |= F_ENCRYPT;
    549   1.1    itojun 			break;
    550   1.1    itojun #endif /*IPSEC_POLICY_IPSEC*/
    551   1.1    itojun #endif /*IPSEC*/
    552   1.1    itojun 		default:
    553   1.1    itojun 			usage();
    554  1.15    itojun 			/*NOTREACHED*/
    555   1.1    itojun 		}
    556  1.15    itojun 	}
    557  1.41    itojun 
    558   1.1    itojun 	argc -= optind;
    559   1.1    itojun 	argv += optind;
    560   1.1    itojun 
    561  1.15    itojun 	if (argc < 1) {
    562   1.1    itojun 		usage();
    563  1.15    itojun 		/*NOTREACHED*/
    564  1.15    itojun 	}
    565   1.1    itojun 
    566  1.11    itojun 	if (argc > 1) {
    567  1.22    itojun 		rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,
    568  1.17    itojun 		    argc - 1));
    569  1.22    itojun 		if (rthlen == 0) {
    570  1.22    itojun 			errx(1, "too many intermediate hops");
    571  1.22    itojun 			/*NOTREACHED*/
    572  1.22    itojun 		}
    573  1.22    itojun 		ip6optlen += rthlen;
    574  1.11    itojun 	}
    575   1.1    itojun 
    576  1.15    itojun 	if (options & F_NIGROUP) {
    577  1.15    itojun 		target = nigroup(argv[argc - 1]);
    578  1.15    itojun 		if (target == NULL) {
    579  1.15    itojun 			usage();
    580  1.15    itojun 			/*NOTREACHED*/
    581  1.15    itojun 		}
    582  1.15    itojun 	} else
    583  1.15    itojun 		target = argv[argc - 1];
    584   1.1    itojun 
    585   1.1    itojun 	/* getaddrinfo */
    586  1.41    itojun 	memset(&hints, 0, sizeof(struct addrinfo));
    587  1.34    itojun 	hints.ai_flags = AI_CANONNAME;
    588   1.1    itojun 	hints.ai_family = AF_INET6;
    589   1.1    itojun 	hints.ai_socktype = SOCK_RAW;
    590   1.1    itojun 	hints.ai_protocol = IPPROTO_ICMPV6;
    591   1.1    itojun 
    592   1.1    itojun 	ret_ga = getaddrinfo(target, NULL, &hints, &res);
    593  1.37    itojun 	if (ret_ga)
    594  1.37    itojun 		errx(1, "%s", gai_strerror(ret_ga));
    595   1.1    itojun 	if (res->ai_canonname)
    596   1.1    itojun 		hostname = res->ai_canonname;
    597   1.1    itojun 	else
    598   1.1    itojun 		hostname = target;
    599  1.28    itojun 
    600   1.1    itojun 	if (!res->ai_addr)
    601   1.1    itojun 		errx(1, "getaddrinfo failed");
    602   1.1    itojun 
    603   1.1    itojun 	(void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
    604   1.1    itojun 
    605  1.28    itojun 	if ((s = socket(res->ai_family, res->ai_socktype,
    606  1.28    itojun 	    res->ai_protocol)) < 0)
    607  1.26    itojun 		err(1, "socket");
    608  1.26    itojun 
    609  1.37    itojun 	/* set the source address if specified. */
    610  1.37    itojun 	if ((options & F_SRCADDR) &&
    611  1.37    itojun 	    bind(s, (struct sockaddr *)&src, srclen) != 0) {
    612  1.37    itojun 		err(1, "bind");
    613  1.37    itojun 	}
    614  1.37    itojun 
    615  1.41    itojun 	/* set the gateway (next hop) if specified */
    616  1.41    itojun 	if (gateway) {
    617  1.41    itojun 		struct addrinfo ghints, *gres;
    618  1.41    itojun 		int error;
    619  1.41    itojun 
    620  1.41    itojun 		memset(&ghints, 0, sizeof(ghints));
    621  1.41    itojun 		ghints.ai_family = AF_INET6;
    622  1.41    itojun 		ghints.ai_socktype = SOCK_RAW;
    623  1.41    itojun 		ghints.ai_protocol = IPPROTO_ICMPV6;
    624  1.41    itojun 
    625  1.41    itojun 		error = getaddrinfo(gateway, NULL, &hints, &gres);
    626  1.41    itojun 		if (error) {
    627  1.44   thorpej 			errx(1, "getaddrinfo for the gateway %s: %s",
    628  1.41    itojun 			     gateway, gai_strerror(error));
    629  1.41    itojun 		}
    630  1.41    itojun 		if (gres->ai_next && (options & F_VERBOSE))
    631  1.41    itojun 			warnx("gateway resolves to multiple addresses");
    632  1.41    itojun 
    633  1.41    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP,
    634  1.41    itojun 			       gres->ai_addr, gres->ai_addrlen)) {
    635  1.41    itojun 			err(1, "setsockopt(IPV6_NEXTHOP)");
    636  1.41    itojun 		}
    637  1.41    itojun 
    638  1.41    itojun 		freeaddrinfo(gres);
    639  1.41    itojun 	}
    640  1.41    itojun 
    641  1.26    itojun 	/*
    642  1.26    itojun 	 * let the kerel pass extension headers of incoming packets,
    643  1.26    itojun 	 * for privileged socket options
    644  1.26    itojun 	 */
    645  1.26    itojun 	if ((options & F_VERBOSE) != 0) {
    646  1.26    itojun 		int opton = 1;
    647  1.26    itojun 
    648  1.26    itojun #ifdef IPV6_RECVHOPOPTS
    649  1.26    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
    650  1.28    itojun 		    sizeof(opton)))
    651  1.26    itojun 			err(1, "setsockopt(IPV6_RECVHOPOPTS)");
    652  1.26    itojun #else  /* old adv. API */
    653  1.26    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
    654  1.28    itojun 		    sizeof(opton)))
    655  1.26    itojun 			err(1, "setsockopt(IPV6_HOPOPTS)");
    656  1.26    itojun #endif
    657  1.26    itojun #ifdef IPV6_RECVDSTOPTS
    658  1.26    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
    659  1.28    itojun 		    sizeof(opton)))
    660  1.26    itojun 			err(1, "setsockopt(IPV6_RECVDSTOPTS)");
    661  1.26    itojun #else  /* old adv. API */
    662  1.26    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
    663  1.28    itojun 		    sizeof(opton)))
    664  1.26    itojun 			err(1, "setsockopt(IPV6_DSTOPTS)");
    665  1.26    itojun #endif
    666  1.26    itojun #ifdef IPV6_RECVRTHDRDSTOPTS
    667  1.26    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
    668  1.28    itojun 		    sizeof(opton)))
    669  1.26    itojun 			err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
    670  1.26    itojun #endif
    671  1.26    itojun 	}
    672  1.26    itojun 
    673  1.26    itojun 	/* revoke root privilege */
    674  1.27    itojun 	seteuid(getuid());
    675  1.26    itojun 	setuid(getuid());
    676  1.26    itojun 
    677  1.41    itojun 	if ((options & F_FLOOD) && (options & F_INTERVAL))
    678   1.1    itojun 		errx(1, "-f and -i incompatible options");
    679   1.1    itojun 
    680  1.23    itojun 	if ((options & F_NOUSERDATA) == 0) {
    681  1.72     lukem 		if (datalen >= (int)sizeof(struct tv32)) {
    682  1.23    itojun 			/* we can time transfer */
    683  1.23    itojun 			timing = 1;
    684  1.23    itojun 		} else
    685  1.23    itojun 			timing = 0;
    686  1.36    itojun 		/* in F_VERBOSE case, we may get non-echoreply packets*/
    687  1.36    itojun 		if (options & F_VERBOSE)
    688  1.36    itojun 			packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
    689  1.36    itojun 		else
    690  1.36    itojun 			packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
    691  1.17    itojun 	} else {
    692  1.17    itojun 		/* suppress timing for node information query */
    693  1.17    itojun 		timing = 0;
    694  1.17    itojun 		datalen = 2048;
    695  1.36    itojun 		packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;
    696  1.17    itojun 	}
    697  1.28    itojun 
    698  1.72     lukem 	if (!(packet = (u_char *)malloc(packlen)))
    699   1.3   thorpej 		err(1, "Unable to allocate packet");
    700   1.1    itojun 	if (!(options & F_PINGFILLED))
    701  1.17    itojun 		for (i = ICMP6ECHOLEN; i < packlen; ++i)
    702   1.1    itojun 			*datap++ = i;
    703   1.1    itojun 
    704  1.59    kleink 	ident = arc4random() & 0xFFFF;
    705  1.18    itojun 	memset(nonce, 0, sizeof(nonce));
    706  1.82  christos 	for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t)) {
    707  1.82  christos 		uint32_t r = arc4random();
    708  1.82  christos 		memcpy(&nonce[i], &r, sizeof(r));
    709  1.82  christos 	}
    710   1.1    itojun 
    711   1.1    itojun 	hold = 1;
    712   1.1    itojun 
    713   1.1    itojun 	if (options & F_SO_DEBUG)
    714   1.1    itojun 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
    715   1.1    itojun 		    sizeof(hold));
    716   1.1    itojun 	optval = IPV6_DEFHLIM;
    717   1.1    itojun 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
    718   1.1    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    719  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    720   1.1    itojun 			err(1, "IPV6_MULTICAST_HOPS");
    721  1.28    itojun #ifdef IPV6_USE_MIN_MTU
    722  1.41    itojun 	if (mflag != 1) {
    723  1.41    itojun 		optval = mflag > 1 ? 0 : 1;
    724  1.41    itojun 
    725  1.28    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
    726  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    727  1.28    itojun 			err(1, "setsockopt(IPV6_USE_MIN_MTU)");
    728  1.28    itojun 	}
    729  1.28    itojun #ifdef IPV6_RECVPATHMTU
    730  1.28    itojun 	else {
    731  1.28    itojun 		optval = 1;
    732  1.28    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
    733  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    734  1.28    itojun 			err(1, "setsockopt(IPV6_RECVPATHMTU)");
    735  1.28    itojun 	}
    736  1.28    itojun #endif /* IPV6_RECVPATHMTU */
    737  1.28    itojun #endif /* IPV6_USE_MIN_MTU */
    738   1.1    itojun 
    739   1.1    itojun #ifdef IPSEC
    740   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
    741   1.1    itojun 	if (options & F_POLICY) {
    742  1.11    itojun 		if (setpolicy(s, policy_in) < 0)
    743  1.16    itojun 			errx(1, "%s", ipsec_strerror());
    744  1.11    itojun 		if (setpolicy(s, policy_out) < 0)
    745  1.16    itojun 			errx(1, "%s", ipsec_strerror());
    746   1.1    itojun 	}
    747   1.1    itojun #else
    748   1.1    itojun 	if (options & F_AUTHHDR) {
    749   1.1    itojun 		optval = IPSEC_LEVEL_REQUIRE;
    750   1.1    itojun #ifdef IPV6_AUTH_TRANS_LEVEL
    751   1.1    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
    752  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    753   1.1    itojun 			err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
    754   1.1    itojun #else /* old def */
    755   1.1    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
    756  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    757   1.1    itojun 			err(1, "setsockopt(IPV6_AUTH_LEVEL)");
    758   1.1    itojun #endif
    759   1.1    itojun 	}
    760   1.1    itojun 	if (options & F_ENCRYPT) {
    761   1.1    itojun 		optval = IPSEC_LEVEL_REQUIRE;
    762   1.1    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
    763  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    764   1.1    itojun 			err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
    765   1.1    itojun 	}
    766   1.1    itojun #endif /*IPSEC_POLICY_IPSEC*/
    767   1.1    itojun #endif
    768   1.1    itojun 
    769   1.1    itojun #ifdef ICMP6_FILTER
    770   1.1    itojun     {
    771   1.1    itojun 	struct icmp6_filter filt;
    772   1.1    itojun 	if (!(options & F_VERBOSE)) {
    773   1.1    itojun 		ICMP6_FILTER_SETBLOCKALL(&filt);
    774  1.15    itojun 		if ((options & F_FQDN) || (options & F_FQDNOLD) ||
    775  1.17    itojun 		    (options & F_NODEADDR) || (options & F_SUPTYPES))
    776   1.1    itojun 			ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
    777   1.1    itojun 		else
    778   1.1    itojun 			ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
    779   1.1    itojun 	} else {
    780   1.1    itojun 		ICMP6_FILTER_SETPASSALL(&filt);
    781   1.1    itojun 	}
    782   1.1    itojun 	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
    783  1.28    itojun 	    sizeof(filt)) < 0)
    784   1.1    itojun 		err(1, "setsockopt(ICMP6_FILTER)");
    785   1.1    itojun     }
    786   1.1    itojun #endif /*ICMP6_FILTER*/
    787   1.1    itojun 
    788  1.11    itojun 	/* let the kerel pass extension headers of incoming packets */
    789  1.11    itojun 	if ((options & F_VERBOSE) != 0) {
    790  1.11    itojun 		int opton = 1;
    791  1.11    itojun 
    792  1.11    itojun #ifdef IPV6_RECVRTHDR
    793  1.11    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
    794  1.28    itojun 		    sizeof(opton)))
    795  1.11    itojun 			err(1, "setsockopt(IPV6_RECVRTHDR)");
    796  1.11    itojun #else  /* old adv. API */
    797  1.11    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
    798  1.28    itojun 		    sizeof(opton)))
    799  1.11    itojun 			err(1, "setsockopt(IPV6_RTHDR)");
    800  1.15    itojun #endif
    801  1.11    itojun 	}
    802  1.11    itojun 
    803   1.1    itojun /*
    804  1.15    itojun 	optval = 1;
    805   1.1    itojun 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
    806   1.1    itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    807  1.28    itojun 		    &optval, sizeof(optval)) == -1)
    808   1.1    itojun 			err(1, "IPV6_MULTICAST_LOOP");
    809   1.1    itojun */
    810   1.1    itojun 
    811   1.9    itojun 	/* Specify the outgoing interface and/or the source address */
    812   1.9    itojun 	if (usepktinfo)
    813   1.1    itojun 		ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
    814   1.1    itojun 
    815   1.1    itojun 	if (hoplimit != -1)
    816   1.1    itojun 		ip6optlen += CMSG_SPACE(sizeof(int));
    817   1.1    itojun 
    818  1.11    itojun #ifdef IPV6_REACHCONF
    819  1.11    itojun 	if (options & F_REACHCONF)
    820  1.11    itojun 		ip6optlen += CMSG_SPACE(0);
    821  1.11    itojun #endif
    822  1.11    itojun 
    823   1.1    itojun 	/* set IP6 packet options */
    824   1.1    itojun 	if (ip6optlen) {
    825   1.1    itojun 		if ((scmsg = (char *)malloc(ip6optlen)) == 0)
    826   1.1    itojun 			errx(1, "can't allocate enough memory");
    827   1.1    itojun 		smsghdr.msg_control = (caddr_t)scmsg;
    828   1.1    itojun 		smsghdr.msg_controllen = ip6optlen;
    829   1.1    itojun 		scmsgp = (struct cmsghdr *)scmsg;
    830   1.1    itojun 	}
    831   1.9    itojun 	if (usepktinfo) {
    832   1.9    itojun 		pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
    833   1.9    itojun 		memset(pktinfo, 0, sizeof(*pktinfo));
    834   1.1    itojun 		scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
    835   1.1    itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    836   1.1    itojun 		scmsgp->cmsg_type = IPV6_PKTINFO;
    837   1.9    itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    838   1.9    itojun 	}
    839   1.1    itojun 
    840   1.9    itojun 	/* set the outgoing interface */
    841   1.9    itojun 	if (ifname) {
    842   1.9    itojun #ifndef USE_SIN6_SCOPE_ID
    843   1.9    itojun 		/* pktinfo must have already been allocated */
    844   1.9    itojun 		if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
    845  1.28    itojun 			errx(1, "%s: invalid interface name", ifname);
    846   1.1    itojun #else
    847   1.9    itojun 		if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
    848   1.1    itojun 			errx(1, "%s: invalid interface name", ifname);
    849   1.1    itojun #endif
    850   1.1    itojun 	}
    851   1.1    itojun 	if (hoplimit != -1) {
    852   1.1    itojun 		scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
    853   1.1    itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    854   1.1    itojun 		scmsgp->cmsg_type = IPV6_HOPLIMIT;
    855   1.1    itojun 		*(int *)(CMSG_DATA(scmsgp)) = hoplimit;
    856   1.1    itojun 
    857   1.1    itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    858   1.1    itojun 	}
    859  1.11    itojun #ifdef IPV6_REACHCONF
    860  1.11    itojun 	if (options & F_REACHCONF) {
    861  1.11    itojun 		scmsgp->cmsg_len = CMSG_LEN(0);
    862  1.11    itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    863  1.11    itojun 		scmsgp->cmsg_type = IPV6_REACHCONF;
    864  1.11    itojun 
    865  1.11    itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    866  1.11    itojun 	}
    867  1.11    itojun #endif
    868  1.11    itojun 
    869   1.1    itojun 	if (argc > 1) {	/* some intermediate addrs are specified */
    870   1.1    itojun 		int hops, error;
    871  1.11    itojun 		int rthdrlen;
    872  1.11    itojun 
    873  1.11    itojun 		rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
    874  1.11    itojun 		scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
    875  1.11    itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    876  1.11    itojun 		scmsgp->cmsg_type = IPV6_RTHDR;
    877  1.11    itojun 		rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
    878  1.11    itojun 		rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
    879  1.28    itojun 		    IPV6_RTHDR_TYPE_0, argc - 1);
    880  1.11    itojun 		if (rthdr == NULL)
    881  1.11    itojun 			errx(1, "can't initialize rthdr");
    882   1.1    itojun 
    883   1.1    itojun 		for (hops = 0; hops < argc - 1; hops++) {
    884   1.1    itojun 			struct addrinfo *iaip;
    885   1.1    itojun 
    886  1.28    itojun 			if ((error = getaddrinfo(argv[hops], NULL, &hints,
    887  1.28    itojun 			    &iaip)))
    888  1.21        is 				errx(1, "%s", gai_strerror(error));
    889  1.19    itojun 			if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)
    890   1.1    itojun 				errx(1,
    891  1.28    itojun 				    "bad addr family of an intermediate addr");
    892   1.1    itojun 
    893  1.11    itojun 			if (inet6_rth_add(rthdr,
    894  1.28    itojun 			    &(SIN6(iaip->ai_addr))->sin6_addr))
    895  1.11    itojun 				errx(1, "can't add an intermediate node");
    896  1.19    itojun 			freeaddrinfo(iaip);
    897   1.1    itojun 		}
    898   1.1    itojun 
    899   1.1    itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    900   1.1    itojun 	}
    901   1.1    itojun 
    902  1.37    itojun 	if (!(options & F_SRCADDR)) {
    903  1.15    itojun 		/*
    904  1.37    itojun 		 * get the source address. XXX since we revoked the root
    905  1.37    itojun 		 * privilege, we cannot use a raw socket for this.
    906   1.1    itojun 		 */
    907  1.47    itojun 		int dummy;
    908  1.47    itojun 		socklen_t len = sizeof(src);
    909  1.28    itojun 
    910   1.1    itojun 		if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    911   1.1    itojun 			err(1, "UDP socket");
    912   1.1    itojun 
    913   1.1    itojun 		src.sin6_family = AF_INET6;
    914   1.1    itojun 		src.sin6_addr = dst.sin6_addr;
    915   1.1    itojun 		src.sin6_port = ntohs(DUMMY_PORT);
    916   1.9    itojun 		src.sin6_scope_id = dst.sin6_scope_id;
    917   1.1    itojun 
    918  1.11    itojun 		if (pktinfo &&
    919  1.11    itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
    920  1.28    itojun 		    (void *)pktinfo, sizeof(*pktinfo)))
    921  1.11    itojun 			err(1, "UDP setsockopt(IPV6_PKTINFO)");
    922  1.11    itojun 
    923  1.11    itojun 		if (hoplimit != -1 &&
    924  1.41    itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
    925  1.28    itojun 		    (void *)&hoplimit, sizeof(hoplimit)))
    926  1.41    itojun 			err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
    927  1.41    itojun 
    928  1.41    itojun 		if (hoplimit != -1 &&
    929  1.41    itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    930  1.41    itojun 		    (void *)&hoplimit, sizeof(hoplimit)))
    931  1.41    itojun 			err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
    932  1.11    itojun 
    933  1.11    itojun 		if (rthdr &&
    934  1.11    itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
    935  1.28    itojun 		    (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
    936  1.11    itojun 			err(1, "UDP setsockopt(IPV6_RTHDR)");
    937  1.28    itojun 
    938   1.1    itojun 		if (connect(dummy, (struct sockaddr *)&src, len) < 0)
    939   1.1    itojun 			err(1, "UDP connect");
    940  1.28    itojun 
    941   1.1    itojun 		if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
    942   1.1    itojun 			err(1, "getsockname");
    943   1.1    itojun 
    944   1.1    itojun 		close(dummy);
    945   1.1    itojun 	}
    946   1.1    itojun 
    947   1.1    itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
    948   1.1    itojun 	if (sockbufsize) {
    949   1.1    itojun 		if (datalen > sockbufsize)
    950  1.15    itojun 			warnx("you need -b to increase socket buffer size");
    951   1.1    itojun 		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
    952  1.28    itojun 		    sizeof(sockbufsize)) < 0)
    953   1.1    itojun 			err(1, "setsockopt(SO_SNDBUF)");
    954   1.1    itojun 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
    955  1.28    itojun 		    sizeof(sockbufsize)) < 0)
    956   1.1    itojun 			err(1, "setsockopt(SO_RCVBUF)");
    957   1.1    itojun 	}
    958   1.1    itojun 	else {
    959   1.1    itojun 		if (datalen > 8 * 1024)	/*XXX*/
    960   1.1    itojun 			warnx("you need -b to increase socket buffer size");
    961   1.1    itojun 		/*
    962   1.1    itojun 		 * When pinging the broadcast address, you can get a lot of
    963   1.1    itojun 		 * answers. Doing something so evil is useful if you are trying
    964   1.1    itojun 		 * to stress the ethernet, or just want to fill the arp cache
    965   1.1    itojun 		 * to get some stuff for /etc/ethers.
    966   1.1    itojun 		 */
    967   1.1    itojun 		hold = 48 * 1024;
    968  1.28    itojun 		setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
    969  1.28    itojun 		    sizeof(hold));
    970   1.1    itojun 	}
    971   1.1    itojun #endif
    972   1.1    itojun 
    973   1.1    itojun 	optval = 1;
    974   1.9    itojun #ifndef USE_SIN6_SCOPE_ID
    975  1.11    itojun #ifdef IPV6_RECVPKTINFO
    976  1.11    itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
    977  1.28    itojun 	    sizeof(optval)) < 0)
    978  1.11    itojun 		warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
    979  1.11    itojun #else  /* old adv. API */
    980  1.11    itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
    981  1.28    itojun 	    sizeof(optval)) < 0)
    982  1.11    itojun 		warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
    983  1.15    itojun #endif
    984  1.11    itojun #endif /* USE_SIN6_SCOPE_ID */
    985  1.11    itojun #ifdef IPV6_RECVHOPLIMIT
    986  1.11    itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
    987  1.28    itojun 	    sizeof(optval)) < 0)
    988  1.11    itojun 		warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
    989  1.11    itojun #else  /* old adv. API */
    990  1.11    itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
    991  1.28    itojun 	    sizeof(optval)) < 0)
    992  1.11    itojun 		warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
    993  1.15    itojun #endif
    994   1.1    itojun 
    995  1.36    itojun 	printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
    996  1.36    itojun 	    (unsigned long)(pingerlen() - 8));
    997  1.31    itojun 	printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
    998  1.31    itojun 	printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
    999   1.1    itojun 
   1000   1.1    itojun 	while (preload--)		/* Fire off them quickies. */
   1001  1.36    itojun 		(void)pinger();
   1002   1.1    itojun 
   1003  1.30    itojun 	(void)signal(SIGINT, onsignal);
   1004  1.19    itojun #ifdef SIGINFO
   1005  1.30    itojun 	(void)signal(SIGINFO, onsignal);
   1006  1.19    itojun #endif
   1007   1.1    itojun 
   1008   1.1    itojun 	if ((options & F_FLOOD) == 0) {
   1009  1.30    itojun 		(void)signal(SIGALRM, onsignal);
   1010  1.18    itojun 		itimer.it_interval = interval;
   1011  1.30    itojun 		itimer.it_value = interval;
   1012   1.1    itojun 		(void)setitimer(ITIMER_REAL, &itimer, NULL);
   1013  1.71  dholland 		if (ntransmitted == 0)
   1014  1.41    itojun 			retransmit();
   1015   1.1    itojun 	}
   1016   1.1    itojun 
   1017  1.43    itojun 	seenalrm = seenint = 0;
   1018  1.31    itojun #ifdef SIGINFO
   1019  1.31    itojun 	seeninfo = 0;
   1020  1.31    itojun #endif
   1021  1.30    itojun 
   1022   1.1    itojun 	for (;;) {
   1023   1.1    itojun 		struct msghdr m;
   1024  1.11    itojun 		u_char buf[1024];
   1025   1.1    itojun 		struct iovec iov[2];
   1026   1.1    itojun 
   1027  1.31    itojun 		/* signal handling */
   1028  1.31    itojun 		if (seenalrm) {
   1029  1.31    itojun 			retransmit();
   1030  1.31    itojun 			seenalrm = 0;
   1031  1.31    itojun 			continue;
   1032  1.31    itojun 		}
   1033  1.31    itojun 		if (seenint) {
   1034  1.31    itojun 			onint(SIGINT);
   1035  1.31    itojun 			seenint = 0;
   1036  1.31    itojun 			continue;
   1037  1.31    itojun 		}
   1038  1.31    itojun #ifdef SIGINFO
   1039  1.31    itojun 		if (seeninfo) {
   1040  1.31    itojun 			summary();
   1041  1.31    itojun 			seeninfo = 0;
   1042  1.31    itojun 			continue;
   1043  1.31    itojun 		}
   1044  1.31    itojun #endif
   1045  1.31    itojun 
   1046   1.1    itojun 		if (options & F_FLOOD) {
   1047  1.36    itojun 			(void)pinger();
   1048  1.48   mycroft 			timeout = 10;
   1049  1.50    itojun 		} else {
   1050  1.48   mycroft 			timeout = INFTIM;
   1051  1.50    itojun 		}
   1052  1.48   mycroft 		fdmaskp[0].fd = s;
   1053  1.48   mycroft 		fdmaskp[0].events = POLLIN;
   1054  1.48   mycroft 		cc = poll(fdmaskp, 1, timeout);
   1055  1.31    itojun 		if (cc < 0) {
   1056  1.31    itojun 			if (errno != EINTR) {
   1057  1.49   mycroft 				warn("poll");
   1058  1.31    itojun 				sleep(1);
   1059  1.30    itojun 			}
   1060  1.30    itojun 			continue;
   1061  1.34    itojun 		} else if (cc == 0)
   1062  1.34    itojun 			continue;
   1063  1.30    itojun 
   1064   1.1    itojun 		m.msg_name = (caddr_t)&from;
   1065   1.1    itojun 		m.msg_namelen = sizeof(from);
   1066   1.1    itojun 		memset(&iov, 0, sizeof(iov));
   1067   1.1    itojun 		iov[0].iov_base = (caddr_t)packet;
   1068   1.1    itojun 		iov[0].iov_len = packlen;
   1069   1.1    itojun 		m.msg_iov = iov;
   1070   1.1    itojun 		m.msg_iovlen = 1;
   1071   1.1    itojun 		m.msg_control = (caddr_t)buf;
   1072   1.1    itojun 		m.msg_controllen = sizeof(buf);
   1073   1.1    itojun 
   1074  1.30    itojun 		cc = recvmsg(s, &m, 0);
   1075  1.30    itojun 		if (cc < 0) {
   1076  1.30    itojun 			if (errno != EINTR) {
   1077  1.30    itojun 				warn("recvmsg");
   1078  1.31    itojun 				sleep(1);
   1079  1.30    itojun 			}
   1080   1.1    itojun 			continue;
   1081  1.36    itojun 		} else if (cc == 0) {
   1082  1.36    itojun 			int mtu;
   1083  1.36    itojun 
   1084  1.36    itojun 			/*
   1085  1.36    itojun 			 * receive control messages only. Process the
   1086  1.36    itojun 			 * exceptions (currently the only possiblity is
   1087  1.36    itojun 			 * a path MTU notification.)
   1088  1.36    itojun 			 */
   1089  1.36    itojun 			if ((mtu = get_pathmtu(&m)) > 0) {
   1090  1.36    itojun 				if ((options & F_VERBOSE) != 0) {
   1091  1.36    itojun 					printf("new path MTU (%d) is "
   1092  1.36    itojun 					    "notified\n", mtu);
   1093  1.36    itojun 				}
   1094  1.36    itojun 			}
   1095  1.36    itojun 			continue;
   1096  1.30    itojun 		} else {
   1097  1.30    itojun 			/*
   1098  1.30    itojun 			 * an ICMPv6 message (probably an echoreply) arrived.
   1099  1.30    itojun 			 */
   1100  1.30    itojun 			pr_pack(packet, cc, &m);
   1101   1.1    itojun 		}
   1102   1.1    itojun 		if (npackets && nreceived >= npackets)
   1103   1.1    itojun 			break;
   1104   1.1    itojun 	}
   1105   1.1    itojun 	summary();
   1106   1.1    itojun 	exit(nreceived == 0);
   1107   1.1    itojun }
   1108   1.1    itojun 
   1109  1.75     joerg static void
   1110  1.62   xtraeme onsignal(int sig)
   1111  1.30    itojun {
   1112  1.43    itojun 
   1113  1.30    itojun 	switch (sig) {
   1114  1.30    itojun 	case SIGALRM:
   1115  1.30    itojun 		seenalrm++;
   1116  1.30    itojun 		break;
   1117  1.30    itojun 	case SIGINT:
   1118  1.30    itojun 		seenint++;
   1119  1.30    itojun 		break;
   1120  1.31    itojun #ifdef SIGINFO
   1121  1.30    itojun 	case SIGINFO:
   1122  1.30    itojun 		seeninfo++;
   1123  1.30    itojun 		break;
   1124  1.31    itojun #endif
   1125  1.30    itojun 	}
   1126  1.30    itojun }
   1127  1.30    itojun 
   1128   1.1    itojun /*
   1129  1.30    itojun  * retransmit --
   1130   1.1    itojun  *	This routine transmits another ping6.
   1131   1.1    itojun  */
   1132  1.75     joerg static void
   1133  1.62   xtraeme retransmit(void)
   1134   1.1    itojun {
   1135   1.1    itojun 	struct itimerval itimer;
   1136   1.1    itojun 
   1137  1.36    itojun 	if (pinger() == 0)
   1138   1.1    itojun 		return;
   1139   1.1    itojun 
   1140   1.1    itojun 	/*
   1141   1.1    itojun 	 * If we're not transmitting any more packets, change the timer
   1142   1.1    itojun 	 * to wait two round-trip times if we've received any packets or
   1143   1.1    itojun 	 * ten seconds if we haven't.
   1144   1.1    itojun 	 */
   1145   1.1    itojun #define	MAXWAIT		10
   1146   1.1    itojun 	if (nreceived) {
   1147   1.1    itojun 		itimer.it_value.tv_sec =  2 * tmax / 1000;
   1148   1.1    itojun 		if (itimer.it_value.tv_sec == 0)
   1149   1.1    itojun 			itimer.it_value.tv_sec = 1;
   1150   1.1    itojun 	} else
   1151   1.1    itojun 		itimer.it_value.tv_sec = MAXWAIT;
   1152   1.1    itojun 	itimer.it_interval.tv_sec = 0;
   1153   1.1    itojun 	itimer.it_interval.tv_usec = 0;
   1154   1.1    itojun 	itimer.it_value.tv_usec = 0;
   1155   1.1    itojun 
   1156   1.1    itojun 	(void)signal(SIGALRM, onint);
   1157   1.1    itojun 	(void)setitimer(ITIMER_REAL, &itimer, NULL);
   1158   1.1    itojun }
   1159   1.1    itojun 
   1160   1.1    itojun /*
   1161   1.1    itojun  * pinger --
   1162   1.1    itojun  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
   1163   1.1    itojun  * will be added on by the kernel.  The ID field is our UNIX process ID,
   1164   1.1    itojun  * and the sequence number is an ascending integer.  The first 8 bytes
   1165   1.1    itojun  * of the data portion are used to hold a UNIX "timeval" struct in VAX
   1166   1.1    itojun  * byte-order, to compute the round-trip time.
   1167   1.1    itojun  */
   1168  1.75     joerg static size_t
   1169  1.62   xtraeme pingerlen(void)
   1170  1.36    itojun {
   1171  1.36    itojun 	size_t l;
   1172  1.36    itojun 
   1173  1.36    itojun 	if (options & F_FQDN)
   1174  1.36    itojun 		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
   1175  1.36    itojun 	else if (options & F_FQDNOLD)
   1176  1.36    itojun 		l = ICMP6_NIQLEN;
   1177  1.36    itojun 	else if (options & F_NODEADDR)
   1178  1.36    itojun 		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
   1179  1.36    itojun 	else if (options & F_SUPTYPES)
   1180  1.36    itojun 		l = ICMP6_NIQLEN;
   1181  1.36    itojun 	else
   1182  1.36    itojun 		l = ICMP6ECHOLEN + datalen;
   1183  1.36    itojun 
   1184  1.36    itojun 	return l;
   1185  1.36    itojun }
   1186  1.36    itojun 
   1187  1.75     joerg static int
   1188  1.62   xtraeme pinger(void)
   1189   1.1    itojun {
   1190   1.1    itojun 	struct icmp6_hdr *icp;
   1191   1.1    itojun 	struct iovec iov[2];
   1192   1.1    itojun 	int i, cc;
   1193  1.17    itojun 	struct icmp6_nodeinfo *nip;
   1194  1.74  christos 	uint16_t seq;
   1195   1.1    itojun 
   1196  1.36    itojun 	if (npackets && ntransmitted >= npackets)
   1197  1.36    itojun 		return(-1);	/* no more transmission */
   1198  1.36    itojun 
   1199   1.1    itojun 	icp = (struct icmp6_hdr *)outpack;
   1200  1.17    itojun 	nip = (struct icmp6_nodeinfo *)outpack;
   1201  1.14    itojun 	memset(icp, 0, sizeof(*icp));
   1202   1.1    itojun 	icp->icmp6_cksum = 0;
   1203  1.17    itojun 	seq = ntransmitted++;
   1204  1.17    itojun 	CLR(seq % mx_dup_ck);
   1205  1.77  christos 	seq = ntohs(seq);
   1206   1.1    itojun 
   1207   1.1    itojun 	if (options & F_FQDN) {
   1208  1.15    itojun 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
   1209  1.17    itojun 		nip->ni_qtype = htons(NI_QTYPE_FQDN);
   1210  1.17    itojun 		nip->ni_flags = htons(0);
   1211  1.17    itojun 
   1212  1.15    itojun 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
   1213  1.15    itojun 		    sizeof(dst.sin6_addr));
   1214  1.15    itojun 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
   1215  1.15    itojun 	} else if (options & F_FQDNOLD) {
   1216  1.15    itojun 		/* packet format in 03 draft - no Subject data on queries */
   1217  1.17    itojun 		icp->icmp6_code = 0;	/* code field is always 0 */
   1218  1.17    itojun 		nip->ni_qtype = htons(NI_QTYPE_FQDN);
   1219  1.17    itojun 		nip->ni_flags = htons(0);
   1220  1.17    itojun 
   1221   1.1    itojun 		cc = ICMP6_NIQLEN;
   1222   1.1    itojun 	} else if (options & F_NODEADDR) {
   1223  1.15    itojun 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
   1224  1.17    itojun 		nip->ni_qtype = htons(NI_QTYPE_NODEADDR);
   1225  1.17    itojun 		nip->ni_flags = naflags;
   1226  1.17    itojun 
   1227  1.15    itojun 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
   1228  1.15    itojun 		    sizeof(dst.sin6_addr));
   1229  1.15    itojun 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
   1230  1.17    itojun 	} else if (options & F_SUPTYPES) {
   1231  1.17    itojun 		icp->icmp6_code = ICMP6_NI_SUBJ_FQDN;	/*empty*/
   1232  1.17    itojun 		nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);
   1233  1.24    itojun 		/* we support compressed bitmap */
   1234  1.24    itojun 		nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;
   1235  1.17    itojun 
   1236  1.74  christos 		cc = ICMP6_NIQLEN;
   1237  1.74  christos 	} else
   1238  1.74  christos 		cc = 0;	/* XXX: gcc */
   1239  1.74  christos 
   1240  1.74  christos 	if (options & (F_FQDN|F_FQDNOLD|F_NODEADDR|F_SUPTYPES)) {
   1241  1.74  christos 		icp->icmp6_type = ICMP6_NI_QUERY;
   1242  1.28    itojun 		memcpy(nip->icmp6_ni_nonce, nonce,
   1243  1.28    itojun 		    sizeof(nip->icmp6_ni_nonce));
   1244  1.74  christos 		memcpy(nip->icmp6_ni_nonce, &seq, sizeof(seq));
   1245  1.17    itojun 		datalen = 0;
   1246  1.17    itojun 	} else {
   1247   1.1    itojun 		icp->icmp6_type = ICMP6_ECHO_REQUEST;
   1248  1.17    itojun 		icp->icmp6_code = 0;
   1249  1.17    itojun 		icp->icmp6_id = htons(ident);
   1250  1.77  christos 		icp->icmp6_seq = seq;
   1251  1.41    itojun 		if (timing) {
   1252  1.41    itojun 			struct timeval tv;
   1253  1.41    itojun 			struct tv32 *tv32;
   1254  1.41    itojun 			(void)gettimeofday(&tv, NULL);
   1255  1.41    itojun 			tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN];
   1256  1.41    itojun 			tv32->tv32_sec = htonl(tv.tv_sec);
   1257  1.41    itojun 			tv32->tv32_usec = htonl(tv.tv_usec);
   1258  1.41    itojun 		}
   1259   1.1    itojun 		cc = ICMP6ECHOLEN + datalen;
   1260   1.1    itojun 	}
   1261   1.1    itojun 
   1262  1.36    itojun #ifdef DIAGNOSTIC
   1263  1.36    itojun 	if (pingerlen() != cc)
   1264  1.36    itojun 		errx(1, "internal error; length mismatch");
   1265  1.36    itojun #endif
   1266  1.36    itojun 
   1267   1.1    itojun 	smsghdr.msg_name = (caddr_t)&dst;
   1268   1.1    itojun 	smsghdr.msg_namelen = sizeof(dst);
   1269   1.1    itojun 	memset(&iov, 0, sizeof(iov));
   1270   1.1    itojun 	iov[0].iov_base = (caddr_t)outpack;
   1271   1.1    itojun 	iov[0].iov_len = cc;
   1272   1.1    itojun 	smsghdr.msg_iov = iov;
   1273   1.1    itojun 	smsghdr.msg_iovlen = 1;
   1274   1.1    itojun 
   1275   1.1    itojun 	i = sendmsg(s, &smsghdr, 0);
   1276   1.1    itojun 
   1277   1.1    itojun 	if (i < 0 || i != cc)  {
   1278   1.1    itojun 		if (i < 0)
   1279   1.1    itojun 			warn("sendmsg");
   1280   1.1    itojun 		(void)printf("ping6: wrote %s %d chars, ret=%d\n",
   1281   1.1    itojun 		    hostname, cc, i);
   1282   1.1    itojun 	}
   1283   1.1    itojun 	if (!(options & F_QUIET) && options & F_FLOOD)
   1284   1.1    itojun 		(void)write(STDOUT_FILENO, &DOT, 1);
   1285  1.36    itojun 
   1286  1.36    itojun 	return(0);
   1287   1.1    itojun }
   1288   1.1    itojun 
   1289  1.75     joerg static int
   1290  1.62   xtraeme myechoreply(const struct icmp6_hdr *icp)
   1291  1.17    itojun {
   1292  1.17    itojun 	if (ntohs(icp->icmp6_id) == ident)
   1293  1.17    itojun 		return 1;
   1294  1.17    itojun 	else
   1295  1.17    itojun 		return 0;
   1296  1.17    itojun }
   1297  1.17    itojun 
   1298  1.75     joerg static int
   1299  1.62   xtraeme mynireply(const struct icmp6_nodeinfo *nip)
   1300  1.17    itojun {
   1301  1.17    itojun 	if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),
   1302  1.17    itojun 	    nonce + sizeof(u_int16_t),
   1303  1.17    itojun 	    sizeof(nonce) - sizeof(u_int16_t)) == 0)
   1304  1.17    itojun 		return 1;
   1305  1.17    itojun 	else
   1306  1.17    itojun 		return 0;
   1307  1.17    itojun }
   1308  1.17    itojun 
   1309  1.75     joerg static char *
   1310  1.62   xtraeme dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf,
   1311  1.62   xtraeme 	  size_t bufsiz)
   1312  1.17    itojun {
   1313  1.25    itojun 	int i;
   1314  1.17    itojun 	const u_char *cp;
   1315  1.17    itojun 	char cresult[MAXDNAME + 1];
   1316  1.17    itojun 	const u_char *comp;
   1317  1.28    itojun 	int l;
   1318  1.17    itojun 
   1319  1.64        he 	i = 0;		/* XXXGCC -Wuninitialized [sun2] */
   1320  1.64        he 
   1321  1.17    itojun 	cp = *sp;
   1322  1.25    itojun 	*buf = '\0';
   1323  1.17    itojun 
   1324  1.17    itojun 	if (cp >= ep)
   1325  1.17    itojun 		return NULL;
   1326  1.17    itojun 	while (cp < ep) {
   1327  1.17    itojun 		i = *cp;
   1328  1.17    itojun 		if (i == 0 || cp != *sp) {
   1329  1.47    itojun 			if (strlcat((char *)buf, ".", bufsiz) >= bufsiz)
   1330  1.17    itojun 				return NULL;	/*result overrun*/
   1331  1.17    itojun 		}
   1332  1.17    itojun 		if (i == 0)
   1333  1.17    itojun 			break;
   1334  1.17    itojun 		cp++;
   1335  1.17    itojun 
   1336  1.17    itojun 		if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {
   1337  1.17    itojun 			/* DNS compression */
   1338  1.17    itojun 			if (!base)
   1339  1.17    itojun 				return NULL;
   1340  1.17    itojun 
   1341  1.17    itojun 			comp = base + (i & 0x3f);
   1342  1.17    itojun 			if (dnsdecode(&comp, cp, base, cresult,
   1343  1.17    itojun 			    sizeof(cresult)) == NULL)
   1344  1.17    itojun 				return NULL;
   1345  1.25    itojun 			if (strlcat(buf, cresult, bufsiz) >= bufsiz)
   1346  1.17    itojun 				return NULL;	/*result overrun*/
   1347  1.17    itojun 			break;
   1348  1.17    itojun 		} else if ((i & 0x3f) == i) {
   1349  1.17    itojun 			if (i > ep - cp)
   1350  1.17    itojun 				return NULL;	/*source overrun*/
   1351  1.17    itojun 			while (i-- > 0 && cp < ep) {
   1352  1.28    itojun 				l = snprintf(cresult, sizeof(cresult),
   1353  1.25    itojun 				    isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);
   1354  1.72     lukem 				if (l >= (int)sizeof(cresult) || l < 0)
   1355  1.28    itojun 					return NULL;
   1356  1.25    itojun 				if (strlcat(buf, cresult, bufsiz) >= bufsiz)
   1357  1.17    itojun 					return NULL;	/*result overrun*/
   1358  1.17    itojun 				cp++;
   1359  1.17    itojun 			}
   1360  1.17    itojun 		} else
   1361  1.17    itojun 			return NULL;	/*invalid label*/
   1362  1.17    itojun 	}
   1363  1.17    itojun 	if (i != 0)
   1364  1.17    itojun 		return NULL;	/*not terminated*/
   1365  1.17    itojun 	cp++;
   1366  1.17    itojun 	*sp = cp;
   1367  1.17    itojun 	return buf;
   1368  1.17    itojun }
   1369  1.17    itojun 
   1370   1.1    itojun /*
   1371   1.1    itojun  * pr_pack --
   1372   1.1    itojun  *	Print out the packet, if it came from us.  This logic is necessary
   1373   1.1    itojun  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
   1374   1.1    itojun  * which arrive ('tis only fair).  This permits multiple copies of this
   1375   1.1    itojun  * program to be run without having intermingled output (or statistics!).
   1376   1.1    itojun  */
   1377  1.75     joerg static void
   1378  1.62   xtraeme pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
   1379   1.1    itojun {
   1380  1.15    itojun #define safeputc(c)	printf((isprint((c)) ? "%c" : "\\%03o"), c)
   1381   1.1    itojun 	struct icmp6_hdr *icp;
   1382  1.17    itojun 	struct icmp6_nodeinfo *ni;
   1383   1.1    itojun 	int i;
   1384   1.1    itojun 	int hoplim;
   1385  1.31    itojun 	struct sockaddr *from;
   1386  1.31    itojun 	int fromlen;
   1387   1.1    itojun 	u_char *cp = NULL, *dp, *end = buf + cc;
   1388  1.11    itojun 	struct in6_pktinfo *pktinfo = NULL;
   1389  1.41    itojun 	struct timeval tv, tp;
   1390  1.41    itojun 	struct tv32 *tpp;
   1391   1.1    itojun 	double triptime = 0;
   1392   1.1    itojun 	int dupflag;
   1393   1.1    itojun 	size_t off;
   1394  1.15    itojun 	int oldfqdn;
   1395  1.17    itojun 	u_int16_t seq;
   1396  1.17    itojun 	char dnsname[MAXDNAME + 1];
   1397   1.1    itojun 
   1398   1.1    itojun 	(void)gettimeofday(&tv, NULL);
   1399   1.1    itojun 
   1400  1.32    itojun 	if (!mhdr || !mhdr->msg_name ||
   1401  1.32    itojun 	    mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
   1402  1.31    itojun 	    ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) {
   1403  1.31    itojun 		if (options & F_VERBOSE)
   1404  1.40    itojun 			warnx("invalid peername");
   1405  1.31    itojun 		return;
   1406  1.31    itojun 	}
   1407  1.31    itojun 	from = (struct sockaddr *)mhdr->msg_name;
   1408  1.31    itojun 	fromlen = mhdr->msg_namelen;
   1409  1.72     lukem 	if (cc < (int)sizeof(struct icmp6_hdr)) {
   1410   1.1    itojun 		if (options & F_VERBOSE)
   1411  1.40    itojun 			warnx("packet too short (%d bytes) from %s", cc,
   1412  1.31    itojun 			    pr_addr(from, fromlen));
   1413   1.1    itojun 		return;
   1414   1.1    itojun 	}
   1415   1.1    itojun 	icp = (struct icmp6_hdr *)buf;
   1416  1.17    itojun 	ni = (struct icmp6_nodeinfo *)buf;
   1417   1.1    itojun 	off = 0;
   1418   1.1    itojun 
   1419   1.1    itojun 	if ((hoplim = get_hoplim(mhdr)) == -1) {
   1420   1.1    itojun 		warnx("failed to get receiving hop limit");
   1421   1.1    itojun 		return;
   1422   1.1    itojun 	}
   1423  1.11    itojun 	if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
   1424  1.55      cjep 		warnx("failed to get receiving packet information");
   1425  1.11    itojun 		return;
   1426  1.11    itojun 	}
   1427   1.1    itojun 
   1428  1.17    itojun 	if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) {
   1429  1.17    itojun 		seq = ntohs(icp->icmp6_seq);
   1430   1.1    itojun 		++nreceived;
   1431   1.1    itojun 		if (timing) {
   1432  1.41    itojun 			tpp = (struct tv32 *)(icp + 1);
   1433  1.41    itojun 			tp.tv_sec = ntohl(tpp->tv32_sec);
   1434  1.41    itojun 			tp.tv_usec = ntohl(tpp->tv32_usec);
   1435  1.41    itojun 			tvsub(&tv, &tp);
   1436   1.1    itojun 			triptime = ((double)tv.tv_sec) * 1000.0 +
   1437   1.1    itojun 			    ((double)tv.tv_usec) / 1000.0;
   1438   1.1    itojun 			tsum += triptime;
   1439  1.18    itojun 			tsumsq += triptime * triptime;
   1440   1.1    itojun 			if (triptime < tmin)
   1441   1.1    itojun 				tmin = triptime;
   1442   1.1    itojun 			if (triptime > tmax)
   1443   1.1    itojun 				tmax = triptime;
   1444   1.1    itojun 		}
   1445   1.1    itojun 
   1446  1.17    itojun 		if (TST(seq % mx_dup_ck)) {
   1447   1.1    itojun 			++nrepeats;
   1448   1.1    itojun 			--nreceived;
   1449   1.1    itojun 			dupflag = 1;
   1450   1.1    itojun 		} else {
   1451  1.17    itojun 			SET(seq % mx_dup_ck);
   1452   1.1    itojun 			dupflag = 0;
   1453   1.1    itojun 		}
   1454   1.1    itojun 
   1455   1.1    itojun 		if (options & F_QUIET)
   1456   1.1    itojun 			return;
   1457   1.1    itojun 
   1458   1.1    itojun 		if (options & F_FLOOD)
   1459   1.1    itojun 			(void)write(STDOUT_FILENO, &BSPACE, 1);
   1460   1.1    itojun 		else {
   1461   1.1    itojun 			(void)printf("%d bytes from %s, icmp_seq=%u", cc,
   1462  1.31    itojun 			    pr_addr(from, fromlen), seq);
   1463   1.1    itojun 			(void)printf(" hlim=%d", hoplim);
   1464  1.11    itojun 			if ((options & F_VERBOSE) != 0) {
   1465  1.11    itojun 				struct sockaddr_in6 dstsa;
   1466  1.11    itojun 
   1467  1.11    itojun 				memset(&dstsa, 0, sizeof(dstsa));
   1468  1.11    itojun 				dstsa.sin6_family = AF_INET6;
   1469  1.31    itojun #ifdef SIN6_LEN
   1470  1.11    itojun 				dstsa.sin6_len = sizeof(dstsa);
   1471  1.31    itojun #endif
   1472  1.11    itojun 				dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
   1473  1.11    itojun 				dstsa.sin6_addr = pktinfo->ipi6_addr;
   1474  1.31    itojun 				(void)printf(" dst=%s",
   1475  1.31    itojun 				    pr_addr((struct sockaddr *)&dstsa,
   1476  1.31    itojun 				    sizeof(dstsa)));
   1477  1.11    itojun 			}
   1478   1.1    itojun 			if (timing)
   1479  1.70      elad 				(void)printf(" time=%.3f ms", triptime);
   1480   1.1    itojun 			if (dupflag)
   1481   1.1    itojun 				(void)printf("(DUP!)");
   1482   1.1    itojun 			/* check the data */
   1483   1.1    itojun 			cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
   1484   1.1    itojun 			dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
   1485   1.1    itojun 			for (i = 8; cp < end; ++i, ++cp, ++dp) {
   1486   1.1    itojun 				if (*cp != *dp) {
   1487   1.1    itojun 					(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
   1488   1.1    itojun 					break;
   1489   1.1    itojun 				}
   1490   1.1    itojun 			}
   1491   1.1    itojun 		}
   1492  1.17    itojun 	} else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) {
   1493  1.74  christos 		memcpy(&seq, ni->icmp6_ni_nonce, sizeof(seq));
   1494  1.74  christos 		seq = ntohs(seq);
   1495  1.17    itojun 		++nreceived;
   1496  1.17    itojun 		if (TST(seq % mx_dup_ck)) {
   1497  1.17    itojun 			++nrepeats;
   1498  1.17    itojun 			--nreceived;
   1499  1.17    itojun 			dupflag = 1;
   1500  1.17    itojun 		} else {
   1501  1.17    itojun 			SET(seq % mx_dup_ck);
   1502  1.17    itojun 			dupflag = 0;
   1503  1.17    itojun 		}
   1504  1.17    itojun 
   1505  1.17    itojun 		if (options & F_QUIET)
   1506  1.17    itojun 			return;
   1507   1.1    itojun 
   1508  1.31    itojun 		(void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
   1509   1.1    itojun 
   1510  1.17    itojun 		switch (ntohs(ni->ni_code)) {
   1511  1.17    itojun 		case ICMP6_NI_SUCCESS:
   1512  1.17    itojun 			break;
   1513  1.17    itojun 		case ICMP6_NI_REFUSED:
   1514  1.17    itojun 			printf("refused, type 0x%x", ntohs(ni->ni_type));
   1515  1.17    itojun 			goto fqdnend;
   1516  1.17    itojun 		case ICMP6_NI_UNKNOWN:
   1517  1.17    itojun 			printf("unknown, type 0x%x", ntohs(ni->ni_type));
   1518  1.17    itojun 			goto fqdnend;
   1519  1.17    itojun 		default:
   1520  1.17    itojun 			printf("unknown code 0x%x, type 0x%x",
   1521  1.17    itojun 			    ntohs(ni->ni_code), ntohs(ni->ni_type));
   1522  1.17    itojun 			goto fqdnend;
   1523  1.17    itojun 		}
   1524  1.17    itojun 
   1525  1.17    itojun 		switch (ntohs(ni->ni_qtype)) {
   1526  1.17    itojun 		case NI_QTYPE_NOOP:
   1527  1.17    itojun 			printf("NodeInfo NOOP");
   1528  1.17    itojun 			break;
   1529  1.17    itojun 		case NI_QTYPE_SUPTYPES:
   1530  1.24    itojun 			pr_suptypes(ni, end - (u_char *)ni);
   1531  1.17    itojun 			break;
   1532  1.17    itojun 		case NI_QTYPE_NODEADDR:
   1533  1.17    itojun 			pr_nodeaddr(ni, end - (u_char *)ni);
   1534  1.17    itojun 			break;
   1535  1.17    itojun 		case NI_QTYPE_FQDN:
   1536  1.17    itojun 		default:	/* XXX: for backward compatibility */
   1537  1.15    itojun 			cp = (u_char *)ni + ICMP6_NIRLEN;
   1538  1.15    itojun 			if (buf[off + ICMP6_NIRLEN] ==
   1539  1.15    itojun 			    cc - off - ICMP6_NIRLEN - 1)
   1540  1.15    itojun 				oldfqdn = 1;
   1541  1.15    itojun 			else
   1542  1.15    itojun 				oldfqdn = 0;
   1543  1.15    itojun 			if (oldfqdn) {
   1544  1.17    itojun 				cp++;	/* skip length */
   1545  1.15    itojun 				while (cp < end) {
   1546  1.15    itojun 					safeputc(*cp & 0xff);
   1547  1.15    itojun 					cp++;
   1548  1.15    itojun 				}
   1549  1.15    itojun 			} else {
   1550  1.17    itojun 				i = 0;
   1551  1.15    itojun 				while (cp < end) {
   1552  1.74  christos 					if (dnsdecode((void *)&cp, end,
   1553  1.43    itojun 					    (const u_char *)(ni + 1), dnsname,
   1554  1.43    itojun 					    sizeof(dnsname)) == NULL) {
   1555  1.17    itojun 						printf("???");
   1556  1.15    itojun 						break;
   1557  1.15    itojun 					}
   1558  1.17    itojun 					/*
   1559  1.17    itojun 					 * name-lookup special handling for
   1560  1.17    itojun 					 * truncated name
   1561  1.17    itojun 					 */
   1562  1.18    itojun 					if (cp + 1 <= end && !*cp &&
   1563  1.17    itojun 					    strlen(dnsname) > 0) {
   1564  1.17    itojun 						dnsname[strlen(dnsname) - 1] = '\0';
   1565  1.17    itojun 						cp++;
   1566  1.17    itojun 					}
   1567  1.17    itojun 					printf("%s%s", i > 0 ? "," : "",
   1568  1.17    itojun 					    dnsname);
   1569  1.15    itojun 				}
   1570  1.15    itojun 			}
   1571  1.15    itojun 			if (options & F_VERBOSE) {
   1572  1.15    itojun 				int32_t ttl;
   1573  1.15    itojun 				int comma = 0;
   1574  1.15    itojun 
   1575  1.15    itojun 				(void)printf(" (");	/*)*/
   1576  1.15    itojun 
   1577  1.17    itojun 				switch (ni->ni_code) {
   1578  1.15    itojun 				case ICMP6_NI_REFUSED:
   1579  1.15    itojun 					(void)printf("refused");
   1580  1.15    itojun 					comma++;
   1581  1.15    itojun 					break;
   1582  1.15    itojun 				case ICMP6_NI_UNKNOWN:
   1583  1.45    itojun 					(void)printf("unknown qtype");
   1584  1.15    itojun 					comma++;
   1585  1.15    itojun 					break;
   1586  1.15    itojun 				}
   1587   1.1    itojun 
   1588  1.15    itojun 				if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
   1589  1.19    itojun 					/* case of refusion, unknown */
   1590  1.19    itojun 					/*(*/
   1591  1.19    itojun 					putchar(')');
   1592  1.15    itojun 					goto fqdnend;
   1593  1.15    itojun 				}
   1594  1.15    itojun 				ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
   1595  1.15    itojun 				if (comma)
   1596  1.15    itojun 					printf(",");
   1597  1.17    itojun 				if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) {
   1598  1.15    itojun 					(void)printf("TTL=%d:meaningless",
   1599  1.17    itojun 					    (int)ttl);
   1600  1.17    itojun 				} else {
   1601  1.15    itojun 					if (ttl < 0) {
   1602  1.15    itojun 						(void)printf("TTL=%d:invalid",
   1603  1.17    itojun 						   ttl);
   1604  1.15    itojun 					} else
   1605  1.15    itojun 						(void)printf("TTL=%d", ttl);
   1606  1.15    itojun 				}
   1607  1.15    itojun 				comma++;
   1608   1.1    itojun 
   1609  1.15    itojun 				if (oldfqdn) {
   1610  1.15    itojun 					if (comma)
   1611  1.15    itojun 						printf(",");
   1612  1.15    itojun 					printf("03 draft");
   1613  1.15    itojun 					comma++;
   1614  1.15    itojun 				} else {
   1615  1.15    itojun 					cp = (u_char *)ni + ICMP6_NIRLEN;
   1616  1.15    itojun 					if (cp == end) {
   1617  1.15    itojun 						if (comma)
   1618  1.15    itojun 							printf(",");
   1619  1.15    itojun 						printf("no name");
   1620  1.15    itojun 						comma++;
   1621  1.15    itojun 					}
   1622  1.15    itojun 				}
   1623   1.1    itojun 
   1624  1.15    itojun 				if (buf[off + ICMP6_NIRLEN] !=
   1625  1.15    itojun 				    cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
   1626  1.15    itojun 					if (comma)
   1627  1.15    itojun 						printf(",");
   1628  1.15    itojun 					(void)printf("invalid namelen:%d/%lu",
   1629  1.17    itojun 					    buf[off + ICMP6_NIRLEN],
   1630  1.17    itojun 					    (u_long)cc - off - ICMP6_NIRLEN - 1);
   1631  1.15    itojun 					comma++;
   1632  1.15    itojun 				}
   1633  1.15    itojun 				/*(*/
   1634  1.15    itojun 				putchar(')');
   1635  1.15    itojun 			}
   1636  1.17    itojun 		fqdnend:
   1637  1.15    itojun 			;
   1638   1.1    itojun 		}
   1639   1.1    itojun 	} else {
   1640   1.1    itojun 		/* We've got something other than an ECHOREPLY */
   1641   1.1    itojun 		if (!(options & F_VERBOSE))
   1642   1.1    itojun 			return;
   1643  1.31    itojun 		(void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
   1644   1.1    itojun 		pr_icmph(icp, end);
   1645   1.1    itojun 	}
   1646   1.1    itojun 
   1647   1.1    itojun 	if (!(options & F_FLOOD)) {
   1648   1.1    itojun 		(void)putchar('\n');
   1649  1.11    itojun 		if (options & F_VERBOSE)
   1650  1.11    itojun 			pr_exthdrs(mhdr);
   1651   1.1    itojun 		(void)fflush(stdout);
   1652   1.1    itojun 	}
   1653  1.15    itojun #undef safeputc
   1654   1.1    itojun }
   1655   1.1    itojun 
   1656  1.75     joerg static void
   1657  1.62   xtraeme pr_exthdrs(struct msghdr *mhdr)
   1658  1.11    itojun {
   1659  1.11    itojun 	struct cmsghdr *cm;
   1660  1.11    itojun 
   1661  1.11    itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1662  1.11    itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1663  1.11    itojun 		if (cm->cmsg_level != IPPROTO_IPV6)
   1664  1.11    itojun 			continue;
   1665  1.11    itojun 
   1666  1.17    itojun 		switch (cm->cmsg_type) {
   1667  1.11    itojun 		case IPV6_HOPOPTS:
   1668  1.11    itojun 			printf("  HbH Options: ");
   1669  1.11    itojun 			pr_ip6opt(CMSG_DATA(cm));
   1670  1.11    itojun 			break;
   1671  1.11    itojun 		case IPV6_DSTOPTS:
   1672  1.11    itojun #ifdef IPV6_RTHDRDSTOPTS
   1673  1.11    itojun 		case IPV6_RTHDRDSTOPTS:
   1674  1.11    itojun #endif
   1675  1.11    itojun 			printf("  Dst Options: ");
   1676  1.11    itojun 			pr_ip6opt(CMSG_DATA(cm));
   1677  1.11    itojun 			break;
   1678  1.11    itojun 		case IPV6_RTHDR:
   1679  1.11    itojun 			printf("  Routing: ");
   1680  1.11    itojun 			pr_rthdr(CMSG_DATA(cm));
   1681  1.11    itojun 			break;
   1682  1.11    itojun 		}
   1683  1.11    itojun 	}
   1684  1.11    itojun }
   1685  1.11    itojun 
   1686  1.75     joerg static void
   1687  1.11    itojun pr_ip6opt(void *extbuf)
   1688  1.11    itojun {
   1689  1.11    itojun 	struct ip6_hbh *ext;
   1690  1.11    itojun 	int currentlen;
   1691  1.11    itojun 	u_int8_t type;
   1692  1.67    rpaulo 	size_t extlen;
   1693  1.67    rpaulo 	socklen_t len;
   1694  1.11    itojun 	void *databuf;
   1695  1.11    itojun 	size_t offset;
   1696  1.11    itojun 	u_int16_t value2;
   1697  1.11    itojun 	u_int32_t value4;
   1698  1.11    itojun 
   1699  1.11    itojun 	ext = (struct ip6_hbh *)extbuf;
   1700  1.11    itojun 	extlen = (ext->ip6h_len + 1) * 8;
   1701  1.23    itojun 	printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
   1702  1.28    itojun 	    (unsigned int)ext->ip6h_len, (unsigned long)extlen);
   1703  1.11    itojun 
   1704  1.11    itojun 	currentlen = 0;
   1705  1.11    itojun 	while (1) {
   1706  1.11    itojun 		currentlen = inet6_opt_next(extbuf, extlen, currentlen,
   1707  1.28    itojun 		    &type, &len, &databuf);
   1708  1.11    itojun 		if (currentlen == -1)
   1709  1.11    itojun 			break;
   1710  1.11    itojun 		switch (type) {
   1711  1.11    itojun 		/*
   1712  1.11    itojun 		 * Note that inet6_opt_next automatically skips any padding
   1713  1.11    itojun 		 * optins.
   1714  1.11    itojun 		 */
   1715  1.11    itojun 		case IP6OPT_JUMBO:
   1716  1.11    itojun 			offset = 0;
   1717  1.11    itojun 			offset = inet6_opt_get_val(databuf, offset,
   1718  1.28    itojun 			    &value4, sizeof(value4));
   1719  1.11    itojun 			printf("    Jumbo Payload Opt: Length %u\n",
   1720  1.28    itojun 			    (u_int32_t)ntohl(value4));
   1721  1.11    itojun 			break;
   1722  1.11    itojun 		case IP6OPT_ROUTER_ALERT:
   1723  1.11    itojun 			offset = 0;
   1724  1.11    itojun 			offset = inet6_opt_get_val(databuf, offset,
   1725  1.11    itojun 						   &value2, sizeof(value2));
   1726  1.11    itojun 			printf("    Router Alert Opt: Type %u\n",
   1727  1.28    itojun 			    ntohs(value2));
   1728  1.11    itojun 			break;
   1729  1.11    itojun 		default:
   1730  1.23    itojun 			printf("    Received Opt %u len %lu\n",
   1731  1.28    itojun 			    type, (unsigned long)len);
   1732  1.11    itojun 			break;
   1733  1.11    itojun 		}
   1734  1.11    itojun 	}
   1735  1.11    itojun 	return;
   1736  1.11    itojun }
   1737  1.11    itojun 
   1738  1.75     joerg static void
   1739  1.11    itojun pr_rthdr(void *extbuf)
   1740  1.11    itojun {
   1741  1.11    itojun 	struct in6_addr *in6;
   1742  1.11    itojun 	char ntopbuf[INET6_ADDRSTRLEN];
   1743  1.11    itojun 	struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
   1744  1.11    itojun 	int i, segments;
   1745  1.11    itojun 
   1746  1.11    itojun 	/* print fixed part of the header */
   1747  1.11    itojun 	printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
   1748  1.28    itojun 	    rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
   1749  1.11    itojun 	if ((segments = inet6_rth_segments(extbuf)) >= 0)
   1750  1.11    itojun 		printf("%d segments, ", segments);
   1751  1.11    itojun 	else
   1752  1.11    itojun 		printf("segments unknown, ");
   1753  1.11    itojun 	printf("%d left\n", rh->ip6r_segleft);
   1754  1.11    itojun 
   1755  1.11    itojun 	for (i = 0; i < segments; i++) {
   1756  1.11    itojun 		in6 = inet6_rth_getaddr(extbuf, i);
   1757  1.11    itojun 		if (in6 == NULL)
   1758  1.11    itojun 			printf("   [%d]<NULL>\n", i);
   1759  1.19    itojun 		else {
   1760  1.28    itojun 			if (!inet_ntop(AF_INET6, in6, ntopbuf,
   1761  1.28    itojun 			    sizeof(ntopbuf)))
   1762  1.52    itojun 				strlcpy(ntopbuf, "?", sizeof(ntopbuf));
   1763  1.19    itojun 			printf("   [%d]%s\n", i, ntopbuf);
   1764  1.19    itojun 		}
   1765  1.11    itojun 	}
   1766  1.11    itojun 
   1767  1.11    itojun 	return;
   1768  1.28    itojun 
   1769  1.11    itojun }
   1770  1.28    itojun 
   1771  1.75     joerg static int
   1772  1.62   xtraeme pr_bitrange(u_int32_t v, int soff, int ii)
   1773  1.24    itojun {
   1774  1.24    itojun 	int off;
   1775  1.24    itojun 	int i;
   1776  1.24    itojun 
   1777  1.24    itojun 	off = 0;
   1778  1.24    itojun 	while (off < 32) {
   1779  1.24    itojun 		/* shift till we have 0x01 */
   1780  1.24    itojun 		if ((v & 0x01) == 0) {
   1781  1.24    itojun 			if (ii > 1)
   1782  1.38     lukem 				printf("-%u", soff + off - 1);
   1783  1.24    itojun 			ii = 0;
   1784  1.24    itojun 			switch (v & 0x0f) {
   1785  1.24    itojun 			case 0x00:
   1786  1.28    itojun 				v >>= 4;
   1787  1.28    itojun 				off += 4;
   1788  1.28    itojun 				continue;
   1789  1.24    itojun 			case 0x08:
   1790  1.28    itojun 				v >>= 3;
   1791  1.28    itojun 				off += 3;
   1792  1.28    itojun 				continue;
   1793  1.24    itojun 			case 0x04: case 0x0c:
   1794  1.28    itojun 				v >>= 2;
   1795  1.28    itojun 				off += 2;
   1796  1.28    itojun 				continue;
   1797  1.24    itojun 			default:
   1798  1.28    itojun 				v >>= 1;
   1799  1.28    itojun 				off += 1;
   1800  1.28    itojun 				continue;
   1801  1.24    itojun 			}
   1802  1.24    itojun 		}
   1803  1.24    itojun 
   1804  1.24    itojun 		/* we have 0x01 with us */
   1805  1.24    itojun 		for (i = 0; i < 32 - off; i++) {
   1806  1.24    itojun 			if ((v & (0x01 << i)) == 0)
   1807  1.24    itojun 				break;
   1808  1.24    itojun 		}
   1809  1.24    itojun 		if (!ii)
   1810  1.38     lukem 			printf(" %u", soff + off);
   1811  1.24    itojun 		ii += i;
   1812  1.24    itojun 		v >>= i; off += i;
   1813  1.24    itojun 	}
   1814  1.24    itojun 	return ii;
   1815  1.24    itojun }
   1816  1.24    itojun 
   1817  1.75     joerg static void
   1818  1.62   xtraeme pr_suptypes(struct icmp6_nodeinfo *ni /* ni->qtype must be SUPTYPES */,
   1819  1.62   xtraeme 	    size_t nilen)
   1820  1.24    itojun {
   1821  1.24    itojun 	size_t clen;
   1822  1.24    itojun 	u_int32_t v;
   1823  1.24    itojun 	const u_char *cp, *end;
   1824  1.24    itojun 	u_int16_t cur;
   1825  1.24    itojun 	struct cbit {
   1826  1.24    itojun 		u_int16_t words;	/*32bit count*/
   1827  1.24    itojun 		u_int16_t skip;
   1828  1.24    itojun 	} cbit;
   1829  1.24    itojun #define MAXQTYPES	(1 << 16)
   1830  1.24    itojun 	size_t off;
   1831  1.24    itojun 	int b;
   1832  1.24    itojun 
   1833  1.24    itojun 	cp = (u_char *)(ni + 1);
   1834  1.24    itojun 	end = ((u_char *)ni) + nilen;
   1835  1.24    itojun 	cur = 0;
   1836  1.24    itojun 	b = 0;
   1837  1.24    itojun 
   1838  1.24    itojun 	printf("NodeInfo Supported Qtypes");
   1839  1.24    itojun 	if (options & F_VERBOSE) {
   1840  1.24    itojun 		if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS)
   1841  1.24    itojun 			printf(", compressed bitmap");
   1842  1.24    itojun 		else
   1843  1.24    itojun 			printf(", raw bitmap");
   1844  1.24    itojun 	}
   1845  1.24    itojun 
   1846  1.24    itojun 	while (cp < end) {
   1847  1.24    itojun 		clen = (size_t)(end - cp);
   1848  1.24    itojun 		if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) {
   1849  1.24    itojun 			if (clen == 0 || clen > MAXQTYPES / 8 ||
   1850  1.24    itojun 			    clen % sizeof(v)) {
   1851  1.24    itojun 				printf("???");
   1852  1.24    itojun 				return;
   1853  1.24    itojun 			}
   1854  1.24    itojun 		} else {
   1855  1.24    itojun 			if (clen < sizeof(cbit) || clen % sizeof(v))
   1856  1.24    itojun 				return;
   1857  1.24    itojun 			memcpy(&cbit, cp, sizeof(cbit));
   1858  1.28    itojun 			if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) >
   1859  1.28    itojun 			    clen)
   1860  1.24    itojun 				return;
   1861  1.24    itojun 			cp += sizeof(cbit);
   1862  1.24    itojun 			clen = ntohs(cbit.words) * sizeof(v);
   1863  1.28    itojun 			if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 >
   1864  1.28    itojun 			    MAXQTYPES)
   1865  1.24    itojun 				return;
   1866  1.24    itojun 		}
   1867  1.24    itojun 
   1868  1.24    itojun 		for (off = 0; off < clen; off += sizeof(v)) {
   1869  1.24    itojun 			memcpy(&v, cp + off, sizeof(v));
   1870  1.24    itojun 			v = (u_int32_t)ntohl(v);
   1871  1.24    itojun 			b = pr_bitrange(v, (int)(cur + off * 8), b);
   1872  1.24    itojun 		}
   1873  1.24    itojun 		/* flush the remaining bits */
   1874  1.24    itojun 		b = pr_bitrange(0, (int)(cur + off * 8), b);
   1875  1.24    itojun 
   1876  1.24    itojun 		cp += clen;
   1877  1.24    itojun 		cur += clen * 8;
   1878  1.24    itojun 		if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0)
   1879  1.24    itojun 			cur += ntohs(cbit.skip) * 32;
   1880  1.24    itojun 	}
   1881  1.24    itojun }
   1882  1.11    itojun 
   1883  1.75     joerg static void
   1884  1.62   xtraeme pr_nodeaddr(struct icmp6_nodeinfo *ni, /* ni->qtype must be NODEADDR */
   1885  1.62   xtraeme 	    int nilen)
   1886   1.1    itojun {
   1887  1.24    itojun 	u_char *cp = (u_char *)(ni + 1);
   1888  1.12    itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   1889  1.24    itojun 	int withttl = 0;
   1890   1.1    itojun 
   1891   1.1    itojun 	nilen -= sizeof(struct icmp6_nodeinfo);
   1892   1.1    itojun 
   1893   1.1    itojun 	if (options & F_VERBOSE) {
   1894  1.17    itojun 		switch (ni->ni_code) {
   1895  1.17    itojun 		case ICMP6_NI_REFUSED:
   1896  1.17    itojun 			(void)printf("refused");
   1897  1.17    itojun 			break;
   1898  1.17    itojun 		case ICMP6_NI_UNKNOWN:
   1899  1.17    itojun 			(void)printf("unknown qtype");
   1900  1.17    itojun 			break;
   1901   1.1    itojun 		}
   1902  1.20    itojun 		if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE)
   1903   1.1    itojun 			(void)printf(" truncated");
   1904   1.1    itojun 	}
   1905   1.1    itojun 	putchar('\n');
   1906   1.1    itojun 	if (nilen <= 0)
   1907   1.1    itojun 		printf("  no address\n");
   1908  1.24    itojun 
   1909  1.24    itojun 	/*
   1910  1.24    itojun 	 * In icmp-name-lookups 05 and later, TTL of each returned address
   1911  1.24    itojun 	 * is contained in the resposne. We try to detect the version
   1912  1.24    itojun 	 * by the length of the data, but note that the detection algorithm
   1913  1.24    itojun 	 * is incomplete. We assume the latest draft by default.
   1914  1.24    itojun 	 */
   1915  1.24    itojun 	if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0)
   1916  1.24    itojun 		withttl = 1;
   1917  1.24    itojun 	while (nilen > 0) {
   1918  1.63     lukem 		u_int32_t ttl = 0;
   1919  1.24    itojun 
   1920  1.24    itojun 		if (withttl) {
   1921  1.28    itojun 			/* XXX: alignment? */
   1922  1.28    itojun 			ttl = (u_int32_t)ntohl(*(u_int32_t *)cp);
   1923  1.24    itojun 			cp += sizeof(u_int32_t);
   1924  1.24    itojun 			nilen -= sizeof(u_int32_t);
   1925  1.24    itojun 		}
   1926  1.24    itojun 
   1927  1.24    itojun 		if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) ==
   1928  1.24    itojun 		    NULL)
   1929  1.52    itojun 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
   1930  1.24    itojun 		printf("  %s", ntop_buf);
   1931  1.24    itojun 		if (withttl) {
   1932  1.24    itojun 			if (ttl == 0xffffffff) {
   1933  1.24    itojun 				/*
   1934  1.24    itojun 				 * XXX: can this convention be applied to all
   1935  1.24    itojun 				 * type of TTL (i.e. non-ND TTL)?
   1936  1.24    itojun 				 */
   1937  1.24    itojun 				printf("(TTL=infty)");
   1938  1.24    itojun 			}
   1939  1.24    itojun 			else
   1940  1.24    itojun 				printf("(TTL=%u)", ttl);
   1941  1.24    itojun 		}
   1942  1.24    itojun 		putchar('\n');
   1943  1.24    itojun 
   1944  1.24    itojun 		nilen -= sizeof(struct in6_addr);
   1945  1.24    itojun 		cp += sizeof(struct in6_addr);
   1946   1.1    itojun 	}
   1947   1.1    itojun }
   1948   1.1    itojun 
   1949  1.75     joerg static int
   1950  1.62   xtraeme get_hoplim(struct msghdr *mhdr)
   1951   1.1    itojun {
   1952   1.1    itojun 	struct cmsghdr *cm;
   1953   1.1    itojun 
   1954   1.1    itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1955   1.1    itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1956  1.25    itojun 		if (cm->cmsg_len == 0)
   1957  1.25    itojun 			return(-1);
   1958  1.25    itojun 
   1959   1.1    itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1960   1.1    itojun 		    cm->cmsg_type == IPV6_HOPLIMIT &&
   1961   1.1    itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
   1962   1.1    itojun 			return(*(int *)CMSG_DATA(cm));
   1963   1.1    itojun 	}
   1964   1.1    itojun 
   1965   1.1    itojun 	return(-1);
   1966   1.1    itojun }
   1967   1.1    itojun 
   1968  1.75     joerg static struct in6_pktinfo *
   1969  1.62   xtraeme get_rcvpktinfo(struct msghdr *mhdr)
   1970  1.11    itojun {
   1971  1.11    itojun 	struct cmsghdr *cm;
   1972  1.11    itojun 
   1973  1.11    itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1974  1.11    itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1975  1.25    itojun 		if (cm->cmsg_len == 0)
   1976  1.25    itojun 			return(NULL);
   1977  1.25    itojun 
   1978  1.11    itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1979  1.11    itojun 		    cm->cmsg_type == IPV6_PKTINFO &&
   1980  1.11    itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
   1981  1.11    itojun 			return((struct in6_pktinfo *)CMSG_DATA(cm));
   1982  1.11    itojun 	}
   1983  1.11    itojun 
   1984  1.11    itojun 	return(NULL);
   1985  1.11    itojun }
   1986  1.11    itojun 
   1987  1.75     joerg static int
   1988  1.62   xtraeme get_pathmtu(struct msghdr *mhdr)
   1989  1.36    itojun {
   1990  1.36    itojun #ifdef IPV6_RECVPATHMTU
   1991  1.36    itojun 	struct cmsghdr *cm;
   1992  1.36    itojun 	struct ip6_mtuinfo *mtuctl = NULL;
   1993  1.36    itojun 
   1994  1.36    itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1995  1.36    itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1996  1.36    itojun 		if (cm->cmsg_len == 0)
   1997  1.36    itojun 			return(0);
   1998  1.36    itojun 
   1999  1.36    itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   2000  1.36    itojun 		    cm->cmsg_type == IPV6_PATHMTU &&
   2001  1.36    itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) {
   2002  1.36    itojun 			mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm);
   2003  1.36    itojun 
   2004  1.36    itojun 			/*
   2005  1.36    itojun 			 * If the notified destination is different from
   2006  1.36    itojun 			 * the one we are pinging, just ignore the info.
   2007  1.36    itojun 			 * We check the scope ID only when both notified value
   2008  1.36    itojun 			 * and our own value have non-0 values, because we may
   2009  1.36    itojun 			 * have used the default scope zone ID for sending,
   2010  1.36    itojun 			 * in which case the scope ID value is 0.
   2011  1.36    itojun 			 */
   2012  1.36    itojun 			if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,
   2013  1.36    itojun 						&dst.sin6_addr) ||
   2014  1.36    itojun 			    (mtuctl->ip6m_addr.sin6_scope_id &&
   2015  1.36    itojun 			     dst.sin6_scope_id &&
   2016  1.36    itojun 			     mtuctl->ip6m_addr.sin6_scope_id !=
   2017  1.36    itojun 			     dst.sin6_scope_id)) {
   2018  1.36    itojun 				if ((options & F_VERBOSE) != 0) {
   2019  1.36    itojun 					printf("path MTU for %s is notified. "
   2020  1.36    itojun 					       "(ignored)\n",
   2021  1.36    itojun 					   pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,
   2022  1.36    itojun 					   sizeof(mtuctl->ip6m_addr)));
   2023  1.36    itojun 				}
   2024  1.36    itojun 				return(0);
   2025  1.36    itojun 			}
   2026  1.36    itojun 
   2027  1.36    itojun 			/*
   2028  1.36    itojun 			 * Ignore an invalid MTU. XXX: can we just believe
   2029  1.36    itojun 			 * the kernel check?
   2030  1.36    itojun 			 */
   2031  1.36    itojun 			if (mtuctl->ip6m_mtu < IPV6_MMTU)
   2032  1.36    itojun 				return(0);
   2033  1.36    itojun 
   2034  1.36    itojun 			/* notification for our destination. return the MTU. */
   2035  1.36    itojun 			return((int)mtuctl->ip6m_mtu);
   2036  1.36    itojun 		}
   2037  1.36    itojun 	}
   2038  1.36    itojun #endif
   2039  1.36    itojun 	return(0);
   2040  1.36    itojun }
   2041  1.36    itojun 
   2042   1.1    itojun /*
   2043   1.1    itojun  * tvsub --
   2044   1.1    itojun  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
   2045   1.1    itojun  * be >= in.
   2046   1.1    itojun  */
   2047  1.75     joerg static void
   2048  1.62   xtraeme tvsub(struct timeval *out, struct timeval *in)
   2049   1.1    itojun {
   2050   1.1    itojun 	if ((out->tv_usec -= in->tv_usec) < 0) {
   2051   1.1    itojun 		--out->tv_sec;
   2052   1.1    itojun 		out->tv_usec += 1000000;
   2053   1.1    itojun 	}
   2054   1.1    itojun 	out->tv_sec -= in->tv_sec;
   2055   1.1    itojun }
   2056   1.1    itojun 
   2057   1.1    itojun /*
   2058   1.1    itojun  * onint --
   2059   1.1    itojun  *	SIGINT handler.
   2060   1.1    itojun  */
   2061  1.11    itojun /* ARGSUSED */
   2062  1.75     joerg static void
   2063  1.62   xtraeme onint(int notused)
   2064   1.1    itojun {
   2065   1.1    itojun 	summary();
   2066   1.1    itojun 
   2067   1.1    itojun 	(void)signal(SIGINT, SIG_DFL);
   2068   1.1    itojun 	(void)kill(getpid(), SIGINT);
   2069   1.1    itojun 
   2070   1.1    itojun 	/* NOTREACHED */
   2071   1.1    itojun 	exit(1);
   2072   1.1    itojun }
   2073   1.1    itojun 
   2074   1.1    itojun /*
   2075   1.1    itojun  * summary --
   2076   1.1    itojun  *	Print out statistics.
   2077   1.1    itojun  */
   2078  1.75     joerg static void
   2079  1.62   xtraeme summary(void)
   2080   1.1    itojun {
   2081   1.1    itojun 
   2082   1.1    itojun 	(void)printf("\n--- %s ping6 statistics ---\n", hostname);
   2083   1.1    itojun 	(void)printf("%ld packets transmitted, ", ntransmitted);
   2084   1.1    itojun 	(void)printf("%ld packets received, ", nreceived);
   2085   1.1    itojun 	if (nrepeats)
   2086   1.1    itojun 		(void)printf("+%ld duplicates, ", nrepeats);
   2087   1.1    itojun 	if (ntransmitted) {
   2088   1.1    itojun 		if (nreceived > ntransmitted)
   2089  1.46    itojun 			(void)printf("-- somebody's duplicating packets!");
   2090   1.1    itojun 		else
   2091  1.56    itojun 			(void)printf("%.1f%% packet loss",
   2092  1.56    itojun 			    ((((double)ntransmitted - nreceived) * 100.0) /
   2093   1.1    itojun 			    ntransmitted));
   2094   1.1    itojun 	}
   2095   1.1    itojun 	(void)putchar('\n');
   2096   1.1    itojun 	if (nreceived && timing) {
   2097   1.1    itojun 		/* Only display average to microseconds */
   2098  1.18    itojun 		double num = nreceived + nrepeats;
   2099  1.73     ahoka 		double dev, avg;
   2100  1.73     ahoka 		if (num > 1) {
   2101  1.73     ahoka 			avg = tsum / num;
   2102  1.73     ahoka 			dev = sqrt((tsumsq - num * avg * avg) / (num - 1));
   2103  1.73     ahoka 		} else {
   2104  1.73     ahoka 			avg = tsum;
   2105  1.73     ahoka 			dev = 0.0;
   2106  1.73     ahoka 		}
   2107  1.18    itojun 		(void)printf(
   2108  1.18    itojun 		    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
   2109  1.18    itojun 		    tmin, avg, tmax, dev);
   2110   1.1    itojun 		(void)fflush(stdout);
   2111   1.1    itojun 	}
   2112  1.23    itojun 	(void)fflush(stdout);
   2113   1.1    itojun }
   2114   1.1    itojun 
   2115  1.17    itojun /*subject type*/
   2116  1.37    itojun static const char *niqcode[] = {
   2117  1.17    itojun 	"IPv6 address",
   2118  1.17    itojun 	"DNS label",	/*or empty*/
   2119  1.17    itojun 	"IPv4 address",
   2120   1.1    itojun };
   2121  1.17    itojun 
   2122  1.17    itojun /*result code*/
   2123  1.37    itojun static const char *nircode[] = {
   2124  1.17    itojun 	"Success", "Refused", "Unknown",
   2125  1.17    itojun };
   2126  1.17    itojun 
   2127   1.1    itojun 
   2128   1.1    itojun /*
   2129   1.1    itojun  * pr_icmph --
   2130   1.1    itojun  *	Print a descriptive string about an ICMP header.
   2131   1.1    itojun  */
   2132  1.75     joerg static void
   2133  1.62   xtraeme pr_icmph(struct icmp6_hdr *icp, u_char *end)
   2134   1.1    itojun {
   2135  1.12    itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   2136  1.17    itojun 	struct nd_redirect *red;
   2137  1.17    itojun 	struct icmp6_nodeinfo *ni;
   2138  1.17    itojun 	char dnsname[MAXDNAME + 1];
   2139  1.17    itojun 	const u_char *cp;
   2140  1.17    itojun 	size_t l;
   2141  1.12    itojun 
   2142  1.17    itojun 	switch (icp->icmp6_type) {
   2143   1.1    itojun 	case ICMP6_DST_UNREACH:
   2144  1.17    itojun 		switch (icp->icmp6_code) {
   2145   1.1    itojun 		case ICMP6_DST_UNREACH_NOROUTE:
   2146   1.1    itojun 			(void)printf("No Route to Destination\n");
   2147   1.1    itojun 			break;
   2148   1.1    itojun 		case ICMP6_DST_UNREACH_ADMIN:
   2149   1.1    itojun 			(void)printf("Destination Administratively "
   2150  1.28    itojun 			    "Unreachable\n");
   2151   1.1    itojun 			break;
   2152   1.9    itojun 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
   2153   1.9    itojun 			(void)printf("Destination Unreachable Beyond Scope\n");
   2154   1.1    itojun 			break;
   2155   1.1    itojun 		case ICMP6_DST_UNREACH_ADDR:
   2156   1.1    itojun 			(void)printf("Destination Host Unreachable\n");
   2157   1.1    itojun 			break;
   2158   1.1    itojun 		case ICMP6_DST_UNREACH_NOPORT:
   2159   1.1    itojun 			(void)printf("Destination Port Unreachable\n");
   2160   1.1    itojun 			break;
   2161   1.1    itojun 		default:
   2162   1.1    itojun 			(void)printf("Destination Unreachable, Bad Code: %d\n",
   2163   1.1    itojun 			    icp->icmp6_code);
   2164   1.1    itojun 			break;
   2165   1.1    itojun 		}
   2166   1.1    itojun 		/* Print returned IP header information */
   2167   1.1    itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   2168   1.1    itojun 		break;
   2169   1.1    itojun 	case ICMP6_PACKET_TOO_BIG:
   2170   1.1    itojun 		(void)printf("Packet too big mtu = %d\n",
   2171  1.28    itojun 		    (int)ntohl(icp->icmp6_mtu));
   2172  1.11    itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   2173   1.1    itojun 		break;
   2174   1.1    itojun 	case ICMP6_TIME_EXCEEDED:
   2175  1.17    itojun 		switch (icp->icmp6_code) {
   2176   1.1    itojun 		case ICMP6_TIME_EXCEED_TRANSIT:
   2177   1.1    itojun 			(void)printf("Time to live exceeded\n");
   2178   1.1    itojun 			break;
   2179   1.1    itojun 		case ICMP6_TIME_EXCEED_REASSEMBLY:
   2180   1.1    itojun 			(void)printf("Frag reassembly time exceeded\n");
   2181   1.1    itojun 			break;
   2182   1.1    itojun 		default:
   2183   1.1    itojun 			(void)printf("Time exceeded, Bad Code: %d\n",
   2184   1.1    itojun 			    icp->icmp6_code);
   2185   1.1    itojun 			break;
   2186   1.1    itojun 		}
   2187   1.1    itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   2188   1.1    itojun 		break;
   2189   1.1    itojun 	case ICMP6_PARAM_PROB:
   2190   1.1    itojun 		(void)printf("Parameter problem: ");
   2191  1.17    itojun 		switch (icp->icmp6_code) {
   2192  1.17    itojun 		case ICMP6_PARAMPROB_HEADER:
   2193  1.17    itojun 			(void)printf("Erroneous Header ");
   2194  1.17    itojun 			break;
   2195  1.17    itojun 		case ICMP6_PARAMPROB_NEXTHEADER:
   2196  1.17    itojun 			(void)printf("Unknown Nextheader ");
   2197  1.17    itojun 			break;
   2198  1.17    itojun 		case ICMP6_PARAMPROB_OPTION:
   2199  1.17    itojun 			(void)printf("Unrecognized Option ");
   2200  1.17    itojun 			break;
   2201  1.17    itojun 		default:
   2202  1.17    itojun 			(void)printf("Bad code(%d) ", icp->icmp6_code);
   2203  1.17    itojun 			break;
   2204   1.1    itojun 		}
   2205   1.1    itojun 		(void)printf("pointer = 0x%02x\n",
   2206  1.28    itojun 		    (u_int32_t)ntohl(icp->icmp6_pptr));
   2207   1.1    itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   2208   1.1    itojun 		break;
   2209   1.1    itojun 	case ICMP6_ECHO_REQUEST:
   2210  1.11    itojun 		(void)printf("Echo Request");
   2211   1.1    itojun 		/* XXX ID + Seq + Data */
   2212   1.1    itojun 		break;
   2213   1.1    itojun 	case ICMP6_ECHO_REPLY:
   2214  1.11    itojun 		(void)printf("Echo Reply");
   2215   1.1    itojun 		/* XXX ID + Seq + Data */
   2216   1.1    itojun 		break;
   2217   1.1    itojun 	case ICMP6_MEMBERSHIP_QUERY:
   2218  1.11    itojun 		(void)printf("Listener Query");
   2219   1.1    itojun 		break;
   2220   1.1    itojun 	case ICMP6_MEMBERSHIP_REPORT:
   2221  1.11    itojun 		(void)printf("Listener Report");
   2222   1.1    itojun 		break;
   2223   1.1    itojun 	case ICMP6_MEMBERSHIP_REDUCTION:
   2224  1.11    itojun 		(void)printf("Listener Done");
   2225   1.1    itojun 		break;
   2226   1.1    itojun 	case ND_ROUTER_SOLICIT:
   2227  1.11    itojun 		(void)printf("Router Solicitation");
   2228   1.1    itojun 		break;
   2229   1.1    itojun 	case ND_ROUTER_ADVERT:
   2230  1.11    itojun 		(void)printf("Router Advertisement");
   2231   1.1    itojun 		break;
   2232   1.1    itojun 	case ND_NEIGHBOR_SOLICIT:
   2233  1.11    itojun 		(void)printf("Neighbor Solicitation");
   2234   1.1    itojun 		break;
   2235   1.1    itojun 	case ND_NEIGHBOR_ADVERT:
   2236  1.11    itojun 		(void)printf("Neighbor Advertisement");
   2237   1.1    itojun 		break;
   2238   1.1    itojun 	case ND_REDIRECT:
   2239  1.17    itojun 		red = (struct nd_redirect *)icp;
   2240   1.1    itojun 		(void)printf("Redirect\n");
   2241  1.19    itojun 		if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,
   2242  1.19    itojun 		    sizeof(ntop_buf)))
   2243  1.52    itojun 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
   2244  1.19    itojun 		(void)printf("Destination: %s", ntop_buf);
   2245  1.19    itojun 		if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,
   2246  1.19    itojun 		    sizeof(ntop_buf)))
   2247  1.52    itojun 			strlcpy(ntop_buf, "?", sizeof(ntop_buf));
   2248  1.25    itojun 		(void)printf(" New Target: %s", ntop_buf);
   2249   1.1    itojun 		break;
   2250   1.1    itojun 	case ICMP6_NI_QUERY:
   2251  1.11    itojun 		(void)printf("Node Information Query");
   2252   1.1    itojun 		/* XXX ID + Seq + Data */
   2253  1.17    itojun 		ni = (struct icmp6_nodeinfo *)icp;
   2254  1.17    itojun 		l = end - (u_char *)(ni + 1);
   2255  1.17    itojun 		printf(", ");
   2256  1.17    itojun 		switch (ntohs(ni->ni_qtype)) {
   2257  1.17    itojun 		case NI_QTYPE_NOOP:
   2258  1.17    itojun 			(void)printf("NOOP");
   2259  1.17    itojun 			break;
   2260  1.17    itojun 		case NI_QTYPE_SUPTYPES:
   2261  1.17    itojun 			(void)printf("Supported qtypes");
   2262  1.17    itojun 			break;
   2263  1.17    itojun 		case NI_QTYPE_FQDN:
   2264  1.17    itojun 			(void)printf("DNS name");
   2265  1.17    itojun 			break;
   2266  1.17    itojun 		case NI_QTYPE_NODEADDR:
   2267  1.17    itojun 			(void)printf("nodeaddr");
   2268  1.17    itojun 			break;
   2269  1.17    itojun 		case NI_QTYPE_IPV4ADDR:
   2270  1.17    itojun 			(void)printf("IPv4 nodeaddr");
   2271  1.17    itojun 			break;
   2272  1.17    itojun 		default:
   2273  1.17    itojun 			(void)printf("unknown qtype");
   2274  1.17    itojun 			break;
   2275  1.17    itojun 		}
   2276  1.17    itojun 		if (options & F_VERBOSE) {
   2277  1.17    itojun 			switch (ni->ni_code) {
   2278  1.17    itojun 			case ICMP6_NI_SUBJ_IPV6:
   2279  1.17    itojun 				if (l == sizeof(struct in6_addr) &&
   2280  1.28    itojun 				    inet_ntop(AF_INET6, ni + 1, ntop_buf,
   2281  1.28    itojun 				    sizeof(ntop_buf)) != NULL) {
   2282  1.17    itojun 					(void)printf(", subject=%s(%s)",
   2283  1.17    itojun 					    niqcode[ni->ni_code], ntop_buf);
   2284  1.17    itojun 				} else {
   2285  1.17    itojun #if 1
   2286  1.17    itojun 					/* backward compat to -W */
   2287  1.17    itojun 					(void)printf(", oldfqdn");
   2288  1.17    itojun #else
   2289  1.17    itojun 					(void)printf(", invalid");
   2290  1.17    itojun #endif
   2291  1.17    itojun 				}
   2292  1.17    itojun 				break;
   2293  1.17    itojun 			case ICMP6_NI_SUBJ_FQDN:
   2294  1.17    itojun 				if (end == (u_char *)(ni + 1)) {
   2295  1.17    itojun 					(void)printf(", no subject");
   2296  1.17    itojun 					break;
   2297  1.17    itojun 				}
   2298  1.17    itojun 				printf(", subject=%s", niqcode[ni->ni_code]);
   2299  1.17    itojun 				cp = (const u_char *)(ni + 1);
   2300  1.28    itojun 				if (dnsdecode(&cp, end, NULL, dnsname,
   2301  1.28    itojun 				    sizeof(dnsname)) != NULL)
   2302  1.17    itojun 					printf("(%s)", dnsname);
   2303  1.17    itojun 				else
   2304  1.17    itojun 					printf("(invalid)");
   2305  1.17    itojun 				break;
   2306  1.17    itojun 			case ICMP6_NI_SUBJ_IPV4:
   2307  1.17    itojun 				if (l == sizeof(struct in_addr) &&
   2308  1.28    itojun 				    inet_ntop(AF_INET, ni + 1, ntop_buf,
   2309  1.28    itojun 				    sizeof(ntop_buf)) != NULL) {
   2310  1.17    itojun 					(void)printf(", subject=%s(%s)",
   2311  1.17    itojun 					    niqcode[ni->ni_code], ntop_buf);
   2312  1.17    itojun 				} else
   2313  1.17    itojun 					(void)printf(", invalid");
   2314  1.17    itojun 				break;
   2315  1.17    itojun 			default:
   2316  1.17    itojun 				(void)printf(", invalid");
   2317  1.17    itojun 				break;
   2318  1.17    itojun 			}
   2319  1.17    itojun 		}
   2320   1.1    itojun 		break;
   2321   1.1    itojun 	case ICMP6_NI_REPLY:
   2322  1.11    itojun 		(void)printf("Node Information Reply");
   2323   1.1    itojun 		/* XXX ID + Seq + Data */
   2324  1.17    itojun 		ni = (struct icmp6_nodeinfo *)icp;
   2325  1.17    itojun 		printf(", ");
   2326  1.17    itojun 		switch (ntohs(ni->ni_qtype)) {
   2327  1.17    itojun 		case NI_QTYPE_NOOP:
   2328  1.17    itojun 			(void)printf("NOOP");
   2329  1.17    itojun 			break;
   2330  1.17    itojun 		case NI_QTYPE_SUPTYPES:
   2331  1.17    itojun 			(void)printf("Supported qtypes");
   2332  1.17    itojun 			break;
   2333  1.17    itojun 		case NI_QTYPE_FQDN:
   2334  1.17    itojun 			(void)printf("DNS name");
   2335  1.17    itojun 			break;
   2336  1.17    itojun 		case NI_QTYPE_NODEADDR:
   2337  1.17    itojun 			(void)printf("nodeaddr");
   2338  1.17    itojun 			break;
   2339  1.17    itojun 		case NI_QTYPE_IPV4ADDR:
   2340  1.17    itojun 			(void)printf("IPv4 nodeaddr");
   2341  1.17    itojun 			break;
   2342  1.17    itojun 		default:
   2343  1.17    itojun 			(void)printf("unknown qtype");
   2344  1.17    itojun 			break;
   2345  1.17    itojun 		}
   2346  1.17    itojun 		if (options & F_VERBOSE) {
   2347  1.65      elad 			if (ni->ni_code >= sizeof(nircode) / sizeof(nircode[0]))
   2348  1.17    itojun 				printf(", invalid");
   2349  1.17    itojun 			else
   2350  1.17    itojun 				printf(", %s", nircode[ni->ni_code]);
   2351  1.17    itojun 		}
   2352   1.1    itojun 		break;
   2353   1.1    itojun 	default:
   2354  1.11    itojun 		(void)printf("Bad ICMP type: %d", icp->icmp6_type);
   2355   1.1    itojun 	}
   2356   1.1    itojun }
   2357   1.1    itojun 
   2358   1.1    itojun /*
   2359   1.1    itojun  * pr_iph --
   2360   1.1    itojun  *	Print an IP6 header.
   2361   1.1    itojun  */
   2362  1.75     joerg static void
   2363  1.62   xtraeme pr_iph(struct ip6_hdr *ip6)
   2364   1.1    itojun {
   2365   1.1    itojun 	u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
   2366   1.1    itojun 	u_int8_t tc;
   2367  1.12    itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   2368   1.1    itojun 
   2369   1.1    itojun 	tc = *(&ip6->ip6_vfc + 1); /* XXX */
   2370   1.1    itojun 	tc = (tc >> 4) & 0x0f;
   2371   1.1    itojun 	tc |= (ip6->ip6_vfc << 4);
   2372   1.1    itojun 
   2373   1.1    itojun 	printf("Vr TC  Flow Plen Nxt Hlim\n");
   2374   1.1    itojun 	printf(" %1x %02x %05x %04x  %02x   %02x\n",
   2375  1.28    itojun 	    (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),
   2376  1.28    itojun 	    ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);
   2377  1.19    itojun 	if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
   2378  1.52    itojun 		strlcpy(ntop_buf, "?", sizeof(ntop_buf));
   2379  1.19    itojun 	printf("%s->", ntop_buf);
   2380  1.19    itojun 	if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
   2381  1.52    itojun 		strlcpy(ntop_buf, "?", sizeof(ntop_buf));
   2382  1.19    itojun 	printf("%s\n", ntop_buf);
   2383   1.1    itojun }
   2384   1.1    itojun 
   2385   1.1    itojun /*
   2386   1.1    itojun  * pr_addr --
   2387   1.1    itojun  *	Return an ascii host address as a dotted quad and optionally with
   2388   1.1    itojun  * a hostname.
   2389   1.1    itojun  */
   2390  1.75     joerg static const char *
   2391  1.62   xtraeme pr_addr(struct sockaddr *addr, int addrlen)
   2392   1.1    itojun {
   2393  1.31    itojun 	static char buf[NI_MAXHOST];
   2394  1.41    itojun 	int flag = 0;
   2395   1.1    itojun 
   2396  1.15    itojun 	if ((options & F_HOSTNAME) == 0)
   2397   1.1    itojun 		flag |= NI_NUMERICHOST;
   2398   1.1    itojun 
   2399  1.31    itojun 	if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)
   2400  1.17    itojun 		return (buf);
   2401  1.17    itojun 	else
   2402  1.17    itojun 		return "?";
   2403   1.1    itojun }
   2404   1.1    itojun 
   2405   1.1    itojun /*
   2406   1.1    itojun  * pr_retip --
   2407   1.1    itojun  *	Dump some info on a returned (via ICMPv6) IPv6 packet.
   2408   1.1    itojun  */
   2409  1.75     joerg static void
   2410  1.62   xtraeme pr_retip(struct ip6_hdr *ip6, u_char *end)
   2411   1.1    itojun {
   2412   1.1    itojun 	u_char *cp = (u_char *)ip6, nh;
   2413   1.1    itojun 	int hlen;
   2414   1.1    itojun 
   2415  1.72     lukem 	if (end - (u_char *)ip6 < (intptr_t)sizeof(*ip6)) {
   2416   1.1    itojun 		printf("IP6");
   2417   1.1    itojun 		goto trunc;
   2418   1.1    itojun 	}
   2419   1.1    itojun 	pr_iph(ip6);
   2420   1.1    itojun 	hlen = sizeof(*ip6);
   2421   1.1    itojun 
   2422   1.1    itojun 	nh = ip6->ip6_nxt;
   2423   1.1    itojun 	cp += hlen;
   2424   1.9    itojun 	while (end - cp >= 8) {
   2425   1.9    itojun 		switch (nh) {
   2426  1.17    itojun 		case IPPROTO_HOPOPTS:
   2427  1.17    itojun 			printf("HBH ");
   2428  1.17    itojun 			hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
   2429  1.17    itojun 			nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
   2430  1.17    itojun 			break;
   2431  1.17    itojun 		case IPPROTO_DSTOPTS:
   2432  1.17    itojun 			printf("DSTOPT ");
   2433  1.17    itojun 			hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
   2434  1.17    itojun 			nh = ((struct ip6_dest *)cp)->ip6d_nxt;
   2435  1.17    itojun 			break;
   2436  1.17    itojun 		case IPPROTO_FRAGMENT:
   2437  1.17    itojun 			printf("FRAG ");
   2438  1.17    itojun 			hlen = sizeof(struct ip6_frag);
   2439  1.17    itojun 			nh = ((struct ip6_frag *)cp)->ip6f_nxt;
   2440  1.17    itojun 			break;
   2441  1.17    itojun 		case IPPROTO_ROUTING:
   2442  1.17    itojun 			printf("RTHDR ");
   2443  1.17    itojun 			hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
   2444  1.17    itojun 			nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
   2445  1.17    itojun 			break;
   2446   1.9    itojun #ifdef IPSEC
   2447  1.17    itojun 		case IPPROTO_AH:
   2448  1.17    itojun 			printf("AH ");
   2449  1.79  drochner 			hlen = (((struct ip6_ext *)cp)->ip6e_len+2) << 2;
   2450  1.79  drochner 			nh = ((struct ip6_ext *)cp)->ip6e_nxt;
   2451  1.17    itojun 			break;
   2452  1.17    itojun #endif
   2453  1.17    itojun 		case IPPROTO_ICMPV6:
   2454  1.28    itojun 			printf("ICMP6: type = %d, code = %d\n",
   2455  1.28    itojun 			    *cp, *(cp + 1));
   2456  1.17    itojun 			return;
   2457  1.17    itojun 		case IPPROTO_ESP:
   2458  1.17    itojun 			printf("ESP\n");
   2459  1.17    itojun 			return;
   2460  1.17    itojun 		case IPPROTO_TCP:
   2461  1.17    itojun 			printf("TCP: from port %u, to port %u (decimal)\n",
   2462  1.17    itojun 			    (*cp * 256 + *(cp + 1)),
   2463  1.17    itojun 			    (*(cp + 2) * 256 + *(cp + 3)));
   2464  1.17    itojun 			return;
   2465  1.17    itojun 		case IPPROTO_UDP:
   2466  1.17    itojun 			printf("UDP: from port %u, to port %u (decimal)\n",
   2467  1.17    itojun 			    (*cp * 256 + *(cp + 1)),
   2468  1.17    itojun 			    (*(cp + 2) * 256 + *(cp + 3)));
   2469  1.17    itojun 			return;
   2470  1.17    itojun 		default:
   2471  1.17    itojun 			printf("Unknown Header(%d)\n", nh);
   2472  1.17    itojun 			return;
   2473   1.1    itojun 		}
   2474   1.1    itojun 
   2475   1.1    itojun 		if ((cp += hlen) >= end)
   2476   1.1    itojun 			goto trunc;
   2477   1.1    itojun 	}
   2478   1.1    itojun 	if (end - cp < 8)
   2479   1.1    itojun 		goto trunc;
   2480   1.1    itojun 
   2481   1.1    itojun 	putchar('\n');
   2482   1.1    itojun 	return;
   2483   1.1    itojun 
   2484   1.1    itojun   trunc:
   2485   1.1    itojun 	printf("...\n");
   2486   1.1    itojun 	return;
   2487   1.1    itojun }
   2488   1.1    itojun 
   2489  1.75     joerg static void
   2490  1.62   xtraeme fill(char *bp, char *patp)
   2491   1.1    itojun {
   2492  1.41    itojun 	int ii, jj, kk;
   2493   1.1    itojun 	int pat[16];
   2494   1.1    itojun 	char *cp;
   2495   1.1    itojun 
   2496   1.1    itojun 	for (cp = patp; *cp; cp++)
   2497  1.61       dsl 		if (!isxdigit((unsigned char)*cp))
   2498   1.1    itojun 			errx(1, "patterns must be specified as hex digits");
   2499   1.1    itojun 	ii = sscanf(patp,
   2500   1.1    itojun 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
   2501   1.1    itojun 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
   2502   1.1    itojun 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
   2503   1.1    itojun 	    &pat[13], &pat[14], &pat[15]);
   2504   1.1    itojun 
   2505  1.28    itojun /* xxx */
   2506   1.1    itojun 	if (ii > 0)
   2507   1.1    itojun 		for (kk = 0;
   2508  1.72     lukem 		    kk <= (int)(MAXDATALEN - (8 + sizeof(struct tv32) + ii));
   2509   1.1    itojun 		    kk += ii)
   2510   1.1    itojun 			for (jj = 0; jj < ii; ++jj)
   2511   1.1    itojun 				bp[jj + kk] = pat[jj];
   2512   1.1    itojun 	if (!(options & F_QUIET)) {
   2513   1.1    itojun 		(void)printf("PATTERN: 0x");
   2514   1.1    itojun 		for (jj = 0; jj < ii; ++jj)
   2515   1.1    itojun 			(void)printf("%02x", bp[jj] & 0xFF);
   2516   1.1    itojun 		(void)printf("\n");
   2517   1.1    itojun 	}
   2518   1.1    itojun }
   2519   1.1    itojun 
   2520  1.11    itojun #ifdef IPSEC
   2521  1.11    itojun #ifdef IPSEC_POLICY_IPSEC
   2522  1.75     joerg static int
   2523  1.62   xtraeme setpolicy(int so, char *policy)
   2524  1.11    itojun {
   2525  1.11    itojun 	char *buf;
   2526  1.11    itojun 
   2527  1.11    itojun 	if (policy == NULL)
   2528  1.11    itojun 		return 0;	/* ignore */
   2529  1.11    itojun 
   2530  1.11    itojun 	buf = ipsec_set_policy(policy, strlen(policy));
   2531  1.11    itojun 	if (buf == NULL)
   2532  1.16    itojun 		errx(1, "%s", ipsec_strerror());
   2533  1.28    itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf,
   2534  1.28    itojun 	    ipsec_get_policylen(buf)) < 0)
   2535  1.42    itojun 		warnx("Unable to set IPsec policy");
   2536  1.11    itojun 	free(buf);
   2537  1.11    itojun 
   2538  1.11    itojun 	return 0;
   2539  1.11    itojun }
   2540  1.11    itojun #endif
   2541  1.11    itojun #endif
   2542  1.11    itojun 
   2543  1.75     joerg static char *
   2544  1.62   xtraeme nigroup(char *name)
   2545  1.15    itojun {
   2546  1.15    itojun 	char *p;
   2547  1.47    itojun 	char *q;
   2548  1.15    itojun 	MD5_CTX ctxt;
   2549  1.15    itojun 	u_int8_t digest[16];
   2550  1.28    itojun 	u_int8_t c;
   2551  1.28    itojun 	size_t l;
   2552  1.15    itojun 	char hbuf[NI_MAXHOST];
   2553  1.15    itojun 	struct in6_addr in6;
   2554  1.15    itojun 
   2555  1.19    itojun 	p = strchr(name, '.');
   2556  1.19    itojun 	if (!p)
   2557  1.19    itojun 		p = name + strlen(name);
   2558  1.19    itojun 	l = p - name;
   2559  1.28    itojun 	if (l > 63 || l > sizeof(hbuf) - 1)
   2560  1.15    itojun 		return NULL;	/*label too long*/
   2561  1.19    itojun 	strncpy(hbuf, name, l);
   2562  1.19    itojun 	hbuf[(int)l] = '\0';
   2563  1.19    itojun 
   2564  1.19    itojun 	for (q = name; *q; q++) {
   2565  1.47    itojun 		if (isupper(*(unsigned char *)q))
   2566  1.47    itojun 			*q = tolower(*(unsigned char *)q);
   2567  1.19    itojun 	}
   2568  1.15    itojun 
   2569  1.15    itojun 	/* generate 8 bytes of pseudo-random value. */
   2570  1.41    itojun 	memset(&ctxt, 0, sizeof(ctxt));
   2571  1.15    itojun 	MD5Init(&ctxt);
   2572  1.28    itojun 	c = l & 0xff;
   2573  1.28    itojun 	MD5Update(&ctxt, &c, sizeof(c));
   2574  1.47    itojun 	MD5Update(&ctxt, (unsigned char *)name, l);
   2575  1.15    itojun 	MD5Final(digest, &ctxt);
   2576  1.15    itojun 
   2577  1.17    itojun 	if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1)
   2578  1.17    itojun 		return NULL;	/*XXX*/
   2579  1.15    itojun 	bcopy(digest, &in6.s6_addr[12], 4);
   2580  1.15    itojun 
   2581  1.15    itojun 	if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
   2582  1.15    itojun 		return NULL;
   2583  1.15    itojun 
   2584  1.15    itojun 	return strdup(hbuf);
   2585  1.15    itojun }
   2586  1.15    itojun 
   2587  1.75     joerg static void
   2588  1.62   xtraeme usage(void)
   2589   1.1    itojun {
   2590   1.1    itojun 	(void)fprintf(stderr,
   2591  1.36    itojun 	    "usage: ping6 [-dfH"
   2592  1.36    itojun #ifdef IPV6_USE_MIN_MTU
   2593  1.36    itojun 	    "m"
   2594  1.36    itojun #endif
   2595  1.36    itojun 	    "nNqtvwW"
   2596  1.15    itojun #ifdef IPV6_REACHCONF
   2597  1.28    itojun 	    "R"
   2598  1.15    itojun #endif
   2599   1.1    itojun #ifdef IPSEC
   2600   1.1    itojun #ifdef IPSEC_POLICY_IPSEC
   2601  1.28    itojun 	    "] [-P policy"
   2602   1.1    itojun #else
   2603  1.28    itojun 	    "AE"
   2604  1.28    itojun #endif
   2605   1.1    itojun #endif
   2606  1.28    itojun 	    "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n"
   2607  1.28    itojun             "\t[-I interface] [-i wait] [-l preload] [-p pattern] "
   2608  1.28    itojun 	    "[-S sourceaddr]\n"
   2609  1.60    itojun             "\t[-s packetsize] [-h hoplimit] [-g gateway] [hops...] host\n");
   2610   1.1    itojun 	exit(1);
   2611   1.1    itojun }
   2612