Home | History | Annotate | Line # | Download | only in ping6
ping6.c revision 1.16
      1  1.16   itojun /*	$NetBSD: ping6.c,v 1.16 2000/07/07 12:29:10 itojun Exp $	*/
      2  1.16   itojun /*	$KAME: ping6.c,v 1.56 2000/07/07 12:17:15 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.1   itojun  * 3. All advertising materials mentioning features or use of this software
     51   1.1   itojun  *    must display the following acknowledgement:
     52   1.1   itojun  *	This product includes software developed by the University of
     53   1.1   itojun  *	California, Berkeley and its contributors.
     54   1.1   itojun  * 4. Neither the name of the University nor the names of its contributors
     55   1.1   itojun  *    may be used to endorse or promote products derived from this software
     56   1.1   itojun  *    without specific prior written permission.
     57   1.1   itojun  *
     58   1.1   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1   itojun  * SUCH DAMAGE.
     69   1.1   itojun  */
     70   1.1   itojun 
     71   1.4   itojun #if 0
     72   1.1   itojun #ifndef lint
     73   1.6   itojun static char copyright[] =
     74   1.1   itojun "@(#) Copyright (c) 1989, 1993\n\
     75   1.6   itojun 	The Regents of the University of California.  All rights reserved.\n";
     76   1.6   itojun #endif /* not lint */
     77   1.6   itojun 
     78   1.6   itojun #ifndef lint
     79   1.6   itojun static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
     80   1.1   itojun #endif /* not lint */
     81   1.6   itojun #else
     82   1.6   itojun #include <sys/cdefs.h>
     83   1.6   itojun #ifndef lint
     84  1.16   itojun __RCSID("$NetBSD: ping6.c,v 1.16 2000/07/07 12:29:10 itojun Exp $");
     85   1.4   itojun #endif
     86   1.1   itojun #endif
     87   1.1   itojun 
     88   1.1   itojun /*
     89   1.1   itojun  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
     90   1.1   itojun  * measure round-trip-delays and packet loss across network paths.
     91   1.1   itojun  *
     92   1.1   itojun  * Author -
     93   1.1   itojun  *	Mike Muuss
     94   1.1   itojun  *	U. S. Army Ballistic Research Laboratory
     95   1.1   itojun  *	December, 1983
     96   1.1   itojun  *
     97   1.1   itojun  * Status -
     98   1.1   itojun  *	Public Domain.  Distribution Unlimited.
     99   1.1   itojun  * Bugs -
    100   1.1   itojun  *	More statistics could always be gathered.
    101   1.1   itojun  *	This program has to run SUID to ROOT to access the ICMP socket.
    102   1.1   itojun  */
    103   1.9   itojun /*
    104   1.9   itojun  * NOTE:
    105   1.9   itojun  * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics
    106  1.11   itojun  * as IPV6_PKTINFO.  Some people object it (sin6_scope_id specifies *link* while
    107   1.9   itojun  * IPV6_PKTINFO specifies *interface*.  Link is defined as collection of
    108   1.9   itojun  * network attached to 1 or more interfaces)
    109   1.9   itojun  */
    110   1.1   itojun 
    111   1.1   itojun #include <sys/param.h>
    112   1.1   itojun #include <sys/uio.h>
    113   1.1   itojun #include <sys/socket.h>
    114   1.1   itojun #include <sys/time.h>
    115   1.1   itojun 
    116   1.1   itojun #include <net/if.h>
    117   1.1   itojun #include <net/route.h>
    118   1.1   itojun 
    119   1.1   itojun #include <netinet/in_systm.h>
    120   1.1   itojun #include <netinet/in.h>
    121   1.1   itojun #include <netinet/ip.h>
    122   1.1   itojun #include <netinet/ip_var.h>
    123   1.1   itojun #include <netinet/ip6.h>
    124   1.1   itojun #include <netinet/icmp6.h>
    125   1.1   itojun #include <arpa/inet.h>
    126   1.1   itojun #include <netdb.h>
    127   1.1   itojun 
    128   1.1   itojun #include <ctype.h>
    129   1.1   itojun #include <err.h>
    130   1.1   itojun #include <errno.h>
    131   1.1   itojun #include <fcntl.h>
    132   1.1   itojun #include <signal.h>
    133   1.1   itojun #include <stdio.h>
    134   1.1   itojun #include <stdlib.h>
    135   1.1   itojun #include <string.h>
    136   1.1   itojun #include <unistd.h>
    137   1.1   itojun 
    138   1.1   itojun #ifdef IPSEC
    139   1.9   itojun #include <netinet6/ah.h>
    140   1.1   itojun #include <netinet6/ipsec.h>
    141   1.1   itojun #endif
    142   1.1   itojun 
    143  1.15   itojun #include <md5.h>
    144  1.15   itojun 
    145   1.1   itojun #define MAXPACKETLEN	131072
    146   1.1   itojun #define	IP6LEN		40
    147   1.1   itojun #define ICMP6ECHOLEN	8	/* icmp echo header len excluding time */
    148   1.1   itojun #define ICMP6ECHOTMLEN sizeof(struct timeval)
    149   1.1   itojun #define ICMP6_NIQLEN	(ICMP6ECHOLEN + 8)
    150   1.1   itojun #define ICMP6_NIRLEN	(ICMP6ECHOLEN + 12) /* 64 bits of nonce + 32 bits ttl */
    151   1.1   itojun #define	EXTRA		256	/* for AH and various other headers. weird. */
    152   1.1   itojun #define	DEFDATALEN	ICMP6ECHOTMLEN
    153   1.1   itojun #define MAXDATALEN	MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN
    154   1.1   itojun #define	NROUTES		9		/* number of record route slots */
    155   1.1   itojun 
    156   1.1   itojun #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
    157   1.1   itojun #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
    158   1.1   itojun #define	SET(bit)	(A(bit) |= B(bit))
    159   1.1   itojun #define	CLR(bit)	(A(bit) &= (~B(bit)))
    160   1.1   itojun #define	TST(bit)	(A(bit) & B(bit))
    161   1.1   itojun 
    162   1.1   itojun #define	F_FLOOD		0x0001
    163   1.1   itojun #define	F_INTERVAL	0x0002
    164   1.1   itojun #define	F_NUMERIC	0x0004
    165   1.1   itojun #define	F_PINGFILLED	0x0008
    166   1.1   itojun #define	F_QUIET		0x0010
    167   1.1   itojun #define	F_RROUTE	0x0020
    168   1.1   itojun #define	F_SO_DEBUG	0x0040
    169   1.1   itojun #define	F_VERBOSE	0x0100
    170   1.1   itojun #ifdef IPSEC
    171   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
    172   1.9   itojun #define	F_POLICY	0x0400
    173   1.1   itojun #else
    174   1.1   itojun #define F_AUTHHDR	0x0200
    175   1.1   itojun #define F_ENCRYPT	0x0400
    176   1.1   itojun #endif /*IPSEC_POLICY_IPSEC*/
    177   1.1   itojun #endif /*IPSEC*/
    178   1.1   itojun #define F_NODEADDR	0x0800
    179   1.1   itojun #define F_FQDN		0x1000
    180   1.1   itojun #define F_INTERFACE	0x2000
    181   1.9   itojun #define F_SRCADDR	0x4000
    182  1.11   itojun #ifdef IPV6_REACHCONF
    183  1.11   itojun #define F_REACHCONF	0x8000
    184  1.11   itojun #endif
    185  1.15   itojun #define F_HOSTNAME	0x10000
    186  1.15   itojun #define F_FQDNOLD	0x20000
    187  1.15   itojun #define F_NIGROUP	0x40000
    188   1.1   itojun u_int options;
    189   1.1   itojun 
    190   1.1   itojun #define IN6LEN		sizeof(struct in6_addr)
    191   1.1   itojun #define SA6LEN		sizeof(struct sockaddr_in6)
    192   1.1   itojun #define DUMMY_PORT      10101
    193   1.1   itojun 
    194   1.1   itojun #define SIN6(s) ((struct sockaddr_in6 *)(s))
    195   1.1   itojun 
    196   1.1   itojun /*
    197   1.1   itojun  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
    198   1.1   itojun  * number of received sequence numbers we can keep track of.  Change 128
    199   1.1   itojun  * to 8192 for complete accuracy...
    200   1.1   itojun  */
    201   1.1   itojun #define	MAX_DUP_CHK	(8 * 8192)
    202   1.1   itojun int mx_dup_ck = MAX_DUP_CHK;
    203   1.1   itojun char rcvd_tbl[MAX_DUP_CHK / 8];
    204   1.1   itojun 
    205  1.15   itojun struct addrinfo *res;
    206   1.1   itojun struct sockaddr_in6 dst;        /* who to ping6 */
    207   1.1   itojun struct sockaddr_in6 src;        /* src addr of this packet */
    208   1.1   itojun int datalen = DEFDATALEN;
    209   1.1   itojun int s;				/* socket file descriptor */
    210   1.1   itojun u_char outpack[MAXPACKETLEN];
    211   1.1   itojun char BSPACE = '\b';		/* characters written for flood */
    212   1.1   itojun char DOT = '.';
    213   1.1   itojun char *hostname;
    214   1.1   itojun int ident;			/* process id to identify our packets */
    215   1.9   itojun struct in6_addr srcaddr;
    216   1.1   itojun 
    217   1.1   itojun /* counters */
    218   1.1   itojun long npackets;			/* max packets to transmit */
    219   1.1   itojun long nreceived;			/* # of packets we got back */
    220   1.1   itojun long nrepeats;			/* number of duplicates */
    221   1.1   itojun long ntransmitted;		/* sequence # for outbound packets = #sent */
    222   1.1   itojun int interval = 1;		/* interval between packets */
    223   1.1   itojun int hoplimit = -1;		/* hoplimit */
    224   1.1   itojun 
    225   1.1   itojun /* timing */
    226   1.1   itojun int timing;			/* flag to do timing */
    227   1.1   itojun double tmin = 999999999.0;	/* minimum round trip time */
    228   1.1   itojun double tmax = 0.0;		/* maximum round trip time */
    229   1.1   itojun double tsum = 0.0;		/* sum of all times, for doing average */
    230   1.1   itojun 
    231   1.1   itojun /* for node addresses */
    232   1.1   itojun u_short naflags;
    233   1.1   itojun 
    234   1.1   itojun /* for ancillary data(advanced API) */
    235   1.1   itojun struct msghdr smsghdr;
    236   1.1   itojun struct iovec smsgiov;
    237   1.1   itojun char *scmsg = 0;
    238   1.1   itojun 
    239   1.1   itojun int	 main __P((int, char *[]));
    240   1.1   itojun void	 fill __P((char *, char *));
    241   1.1   itojun int	 get_hoplim __P((struct msghdr *));
    242  1.11   itojun struct in6_pktinfo *get_rcvpktinfo __P((struct msghdr *));
    243   1.1   itojun void	 onalrm __P((int));
    244   1.1   itojun void	 oninfo __P((int));
    245   1.1   itojun void	 onint __P((int));
    246   1.1   itojun void	 pinger __P((void));
    247  1.12   itojun const char *pr_addr __P((struct sockaddr_in6 *));
    248   1.1   itojun void	 pr_icmph __P((struct icmp6_hdr *, u_char *));
    249   1.1   itojun void	 pr_iph __P((struct ip6_hdr *));
    250   1.1   itojun void	 pr_nodeaddr __P((struct icmp6_nodeinfo *, int));
    251   1.1   itojun void	 pr_pack __P((u_char *, int, struct msghdr *));
    252  1.11   itojun void	 pr_exthdrs __P((struct msghdr *));
    253  1.11   itojun void	 pr_ip6opt __P((void *));
    254  1.11   itojun void	 pr_rthdr __P((void *));
    255   1.1   itojun void	 pr_retip __P((struct ip6_hdr *, u_char *));
    256   1.1   itojun void	 summary __P((void));
    257   1.1   itojun void	 tvsub __P((struct timeval *, struct timeval *));
    258  1.11   itojun int	 setpolicy __P((int, char *));
    259  1.15   itojun char	*nigroup __P((char *));
    260   1.1   itojun void	 usage __P((void));
    261   1.1   itojun 
    262   1.1   itojun int
    263   1.1   itojun main(argc, argv)
    264   1.1   itojun 	int argc;
    265   1.1   itojun 	char *argv[];
    266   1.1   itojun {
    267   1.1   itojun 	struct itimerval itimer;
    268   1.1   itojun 	struct sockaddr_in6 from;
    269   1.1   itojun 	struct timeval timeout;
    270   1.1   itojun 	struct addrinfo hints;
    271   1.1   itojun 	fd_set fdset;
    272   1.1   itojun 	register int cc, i;
    273   1.1   itojun 	int ch, fromlen, hold, packlen, preload, optval, ret_ga;
    274   1.1   itojun 	u_char *datap, *packet;
    275   1.1   itojun 	char *e, *target, *ifname = NULL;
    276   1.1   itojun 	int ip6optlen = 0;
    277   1.1   itojun 	struct cmsghdr *scmsgp = NULL;
    278   1.1   itojun 	int sockbufsize = 0;
    279   1.9   itojun 	int usepktinfo = 0;
    280   1.9   itojun 	struct in6_pktinfo *pktinfo = NULL;
    281  1.11   itojun #ifdef USE_RFC2292BIS
    282  1.11   itojun 	struct ip6_rthdr *rthdr = NULL;
    283  1.15   itojun #endif
    284   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
    285  1.11   itojun 	char *policy_in = NULL;
    286  1.11   itojun 	char *policy_out = NULL;
    287   1.1   itojun #endif
    288   1.1   itojun 
    289   1.1   itojun 	/* just to be sure */
    290   1.1   itojun 	memset(&smsghdr, 0, sizeof(&smsghdr));
    291   1.1   itojun 	memset(&smsgiov, 0, sizeof(&smsgiov));
    292   1.1   itojun 
    293   1.1   itojun 	preload = 0;
    294   1.1   itojun 	datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];
    295   1.1   itojun #ifndef IPSEC
    296  1.15   itojun 	while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwW")) != EOF)
    297   1.1   itojun #else
    298   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
    299  1.15   itojun 	while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWP:")) != EOF)
    300   1.1   itojun #else
    301  1.15   itojun 	while ((ch = getopt(argc, argv, "a:b:c:dfHh:I:i:l:nNp:qRS:s:vwWAE")) != EOF)
    302   1.1   itojun #endif /*IPSEC_POLICY_IPSEC*/
    303   1.1   itojun #endif
    304  1.15   itojun 	{
    305   1.1   itojun 		switch(ch) {
    306   1.1   itojun 		 case 'a':
    307   1.1   itojun 		 {
    308   1.9   itojun 			 char *cp;
    309   1.1   itojun 
    310   1.1   itojun 			 options |= F_NODEADDR;
    311   1.1   itojun 			 datalen = 2048; /* XXX: enough? */
    312   1.1   itojun 			 for (cp = optarg; *cp != '\0'; cp++) {
    313   1.1   itojun 				 switch(*cp) {
    314   1.9   itojun 				 case 'a':
    315   1.9   itojun 					 naflags |= NI_NODEADDR_FLAG_ALL;
    316   1.9   itojun 					 break;
    317  1.13   itojun 				 case 'c':
    318  1.13   itojun 				 case 'C':
    319  1.13   itojun 					 naflags |= NI_NODEADDR_FLAG_COMPAT;
    320  1.13   itojun 					 break;
    321   1.9   itojun 				 case 'l':
    322   1.9   itojun 				 case 'L':
    323   1.9   itojun 					 naflags |= NI_NODEADDR_FLAG_LINKLOCAL;
    324   1.9   itojun 					 break;
    325   1.9   itojun 				 case 's':
    326   1.9   itojun 				 case 'S':
    327   1.9   itojun 					 naflags |= NI_NODEADDR_FLAG_SITELOCAL;
    328   1.9   itojun 					 break;
    329   1.9   itojun 				 case 'g':
    330   1.9   itojun 				 case 'G':
    331   1.9   itojun 					 naflags |= NI_NODEADDR_FLAG_GLOBAL;
    332   1.9   itojun 					 break;
    333   1.9   itojun 				 case 'A': /* experimental. not in the spec */
    334   1.9   itojun 					 naflags |= NI_NODEADDR_FLAG_ANYCAST;
    335   1.9   itojun 					 break;
    336   1.9   itojun 				 default:
    337   1.9   itojun 					 usage();
    338  1.15   itojun 					 /*NOTREACHED*/
    339   1.1   itojun 				 }
    340   1.1   itojun 			 }
    341   1.1   itojun 			 break;
    342   1.1   itojun 		 }
    343   1.1   itojun 		 case 'b':
    344   1.1   itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
    345   1.1   itojun 			sockbufsize = atoi(optarg);
    346   1.1   itojun #else
    347   1.1   itojun 			err(1,
    348   1.1   itojun "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");
    349   1.1   itojun #endif
    350   1.1   itojun 			break;
    351   1.1   itojun 		case 'c':
    352   1.1   itojun 			npackets = strtol(optarg, &e, 10);
    353   1.1   itojun 			if (npackets <= 0 || *optarg == '\0' || *e != '\0')
    354   1.1   itojun 				errx(1,
    355   1.1   itojun 				    "illegal number of packets -- %s", optarg);
    356   1.1   itojun 			break;
    357   1.1   itojun 		case 'd':
    358   1.1   itojun 			options |= F_SO_DEBUG;
    359   1.1   itojun 			break;
    360   1.1   itojun 		case 'f':
    361   1.1   itojun 			if (getuid()) {
    362   1.1   itojun 				errno = EPERM;
    363   1.3  thorpej 				errx(1, "Must be superuser to flood ping");
    364   1.1   itojun 			}
    365   1.1   itojun 			options |= F_FLOOD;
    366   1.1   itojun 			setbuf(stdout, (char *)NULL);
    367   1.1   itojun 			break;
    368  1.15   itojun 		case 'H':
    369  1.15   itojun 			options |= F_HOSTNAME;
    370  1.15   itojun 			break;
    371   1.1   itojun 		case 'h':		/* hoplimit */
    372   1.1   itojun 			hoplimit = strtol(optarg, &e, 10);
    373   1.1   itojun 			if (255 < hoplimit || hoplimit < -1)
    374   1.1   itojun 				errx(1,
    375   1.1   itojun 				    "illegal hoplimit -- %s", optarg);
    376   1.1   itojun 			break;
    377   1.1   itojun 		case 'I':
    378   1.1   itojun 			ifname = optarg;
    379   1.1   itojun 			options |= F_INTERFACE;
    380   1.9   itojun #ifndef USE_SIN6_SCOPE_ID
    381   1.9   itojun 			usepktinfo++;
    382   1.9   itojun #endif
    383   1.1   itojun 			break;
    384   1.1   itojun 		case 'i':		/* wait between sending packets */
    385   1.1   itojun 			interval = strtol(optarg, &e, 10);
    386   1.1   itojun 			if (interval <= 0 || *optarg == '\0' || *e != '\0')
    387   1.1   itojun 				errx(1,
    388   1.1   itojun 				    "illegal timing interval -- %s", optarg);
    389   1.1   itojun 			options |= F_INTERVAL;
    390   1.1   itojun 			break;
    391   1.1   itojun 		case 'l':
    392  1.11   itojun 			if (getuid()) {
    393  1.11   itojun 				errno = EPERM;
    394  1.11   itojun 				errx(1, "Must be superuser to preload");
    395  1.11   itojun 			}
    396   1.1   itojun 			preload = strtol(optarg, &e, 10);
    397   1.1   itojun 			if (preload < 0 || *optarg == '\0' || *e != '\0')
    398   1.1   itojun 				errx(1, "illegal preload value -- %s", optarg);
    399   1.1   itojun 			break;
    400   1.1   itojun 		case 'n':
    401   1.1   itojun 			options |= F_NUMERIC;
    402   1.1   itojun 			break;
    403  1.15   itojun 		case 'N':
    404  1.15   itojun 			options |= F_NIGROUP;
    405  1.15   itojun 			break;
    406   1.1   itojun 		case 'p':		/* fill buffer with user pattern */
    407   1.1   itojun 			options |= F_PINGFILLED;
    408   1.1   itojun 			fill((char *)datap, optarg);
    409   1.1   itojun 				break;
    410   1.1   itojun 		case 'q':
    411   1.1   itojun 			options |= F_QUIET;
    412   1.1   itojun 			break;
    413  1.12   itojun 		case 'R':
    414  1.11   itojun #ifdef IPV6_REACHCONF
    415  1.11   itojun 			options |= F_REACHCONF;
    416   1.1   itojun 			break;
    417  1.12   itojun #else
    418  1.12   itojun 			errx(1, "-R is not supported in this configuration");
    419  1.11   itojun #endif
    420   1.9   itojun 		case 'S':
    421   1.9   itojun 			/* XXX: use getaddrinfo? */
    422   1.9   itojun 			if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)
    423   1.9   itojun 				errx(1, "invalid IPv6 address: %s", optarg);
    424   1.9   itojun 			options |= F_SRCADDR;
    425   1.9   itojun 			usepktinfo++;
    426   1.9   itojun 			break;
    427   1.1   itojun 		case 's':		/* size of packet to send */
    428   1.1   itojun 			datalen = strtol(optarg, &e, 10);
    429   1.1   itojun 			if (datalen <= 0 || *optarg == '\0' || *e != '\0')
    430   1.1   itojun 				errx(1, "illegal datalen value -- %s", optarg);
    431   1.1   itojun 			if (datalen > MAXDATALEN)
    432   1.1   itojun 				errx(1,
    433   1.1   itojun 				    "datalen value too large, maximum is %d",
    434   1.1   itojun 				    MAXDATALEN);
    435   1.1   itojun 			break;
    436   1.1   itojun 		case 'v':
    437   1.1   itojun 			options |= F_VERBOSE;
    438   1.1   itojun 			break;
    439   1.1   itojun 		case 'w':
    440  1.15   itojun 			options |= F_FQDN;
    441  1.15   itojun 			break;
    442   1.1   itojun 		case 'W':
    443  1.15   itojun 			options |= F_FQDNOLD;
    444   1.1   itojun 			break;
    445   1.1   itojun #ifdef IPSEC
    446   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
    447   1.1   itojun 		case 'P':
    448   1.1   itojun 			options |= F_POLICY;
    449  1.11   itojun 			if (!strncmp("in", optarg, 2))
    450  1.11   itojun 				policy_in = strdup(optarg);
    451  1.11   itojun 			else if (!strncmp("out", optarg, 3))
    452  1.11   itojun 				policy_out = strdup(optarg);
    453  1.11   itojun 			else
    454  1.11   itojun 				errx(1, "invalid security policy");
    455   1.1   itojun 			break;
    456   1.1   itojun #else
    457   1.1   itojun 		case 'A':
    458   1.1   itojun 			options |= F_AUTHHDR;
    459   1.1   itojun 			break;
    460   1.1   itojun 		case 'E':
    461   1.1   itojun 			options |= F_ENCRYPT;
    462   1.1   itojun 			break;
    463   1.1   itojun #endif /*IPSEC_POLICY_IPSEC*/
    464   1.1   itojun #endif /*IPSEC*/
    465   1.1   itojun 		default:
    466   1.1   itojun 			usage();
    467  1.15   itojun 			/*NOTREACHED*/
    468   1.1   itojun 		}
    469  1.15   itojun 	}
    470   1.1   itojun 	argc -= optind;
    471   1.1   itojun 	argv += optind;
    472   1.1   itojun 
    473  1.15   itojun 	if (argc < 1) {
    474   1.1   itojun 		usage();
    475  1.15   itojun 		/*NOTREACHED*/
    476  1.15   itojun 	}
    477   1.1   itojun 
    478  1.11   itojun 	if (argc > 1) {
    479  1.11   itojun #ifdef USE_SIN6_SCOPE_ID
    480  1.11   itojun 		ip6optlen += CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1));
    481  1.11   itojun #else  /* old advanced API */
    482   1.1   itojun 		ip6optlen += inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);
    483  1.11   itojun #endif
    484  1.11   itojun 	}
    485   1.1   itojun 
    486  1.15   itojun 	if (options & F_NIGROUP) {
    487  1.15   itojun 		target = nigroup(argv[argc - 1]);
    488  1.15   itojun 		if (target == NULL) {
    489  1.15   itojun 			usage();
    490  1.15   itojun 			/*NOTREACHED*/
    491  1.15   itojun 		}
    492  1.15   itojun 	} else
    493  1.15   itojun 		target = argv[argc - 1];
    494   1.1   itojun 
    495   1.1   itojun 	/* getaddrinfo */
    496   1.1   itojun 	bzero(&hints, sizeof(struct addrinfo));
    497  1.15   itojun 	if ((options & F_NUMERIC) != 0)
    498   1.1   itojun 		hints.ai_flags = AI_CANONNAME;
    499   1.1   itojun 	hints.ai_family = AF_INET6;
    500   1.1   itojun 	hints.ai_socktype = SOCK_RAW;
    501   1.1   itojun 	hints.ai_protocol = IPPROTO_ICMPV6;
    502   1.1   itojun 
    503   1.1   itojun 	ret_ga = getaddrinfo(target, NULL, &hints, &res);
    504   1.1   itojun 	if (ret_ga) {
    505   1.1   itojun 		fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));
    506   1.1   itojun 		exit(1);
    507   1.1   itojun 	}
    508   1.1   itojun 	if (res->ai_canonname)
    509   1.1   itojun 		hostname = res->ai_canonname;
    510   1.1   itojun 	else
    511   1.1   itojun 		hostname = target;
    512   1.1   itojun 
    513   1.1   itojun 	if (!res->ai_addr)
    514   1.1   itojun 		errx(1, "getaddrinfo failed");
    515   1.1   itojun 
    516   1.1   itojun 	(void)memcpy(&dst, res->ai_addr, res->ai_addrlen);
    517   1.1   itojun 
    518   1.1   itojun 	if (options & F_FLOOD && options & F_INTERVAL)
    519   1.1   itojun 		errx(1, "-f and -i incompatible options");
    520   1.1   itojun 
    521   1.1   itojun 	if (datalen >= sizeof(struct timeval))	/* can we time transfer */
    522   1.1   itojun 		timing = 1;
    523   1.1   itojun 	packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;
    524   1.1   itojun 	if (!(packet = (u_char *)malloc((u_int)packlen)))
    525   1.3  thorpej 		err(1, "Unable to allocate packet");
    526   1.1   itojun 	if (!(options & F_PINGFILLED))
    527   1.1   itojun 		for (i = 8; i < datalen; ++i)
    528   1.1   itojun 			*datap++ = i;
    529   1.1   itojun 
    530   1.1   itojun 	ident = getpid() & 0xFFFF;
    531   1.1   itojun 
    532  1.11   itojun 	if ((s = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
    533  1.11   itojun 	  err(1, "socket");
    534   1.1   itojun 
    535   1.1   itojun 	hold = 1;
    536   1.1   itojun 
    537   1.1   itojun 	if (options & F_SO_DEBUG)
    538   1.1   itojun 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
    539   1.1   itojun 		    sizeof(hold));
    540   1.1   itojun 	optval = IPV6_DEFHLIM;
    541   1.1   itojun 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
    542   1.1   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
    543   1.1   itojun 			       &optval, sizeof(optval)) == -1)
    544   1.1   itojun 			err(1, "IPV6_MULTICAST_HOPS");
    545   1.1   itojun 
    546   1.1   itojun #ifdef IPSEC
    547   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
    548   1.1   itojun 	if (options & F_POLICY) {
    549  1.11   itojun 		if (setpolicy(s, policy_in) < 0)
    550  1.16   itojun 			errx(1, "%s", ipsec_strerror());
    551  1.11   itojun 		if (setpolicy(s, policy_out) < 0)
    552  1.16   itojun 			errx(1, "%s", ipsec_strerror());
    553   1.1   itojun 	}
    554   1.1   itojun #else
    555   1.1   itojun 	if (options & F_AUTHHDR) {
    556   1.1   itojun 		optval = IPSEC_LEVEL_REQUIRE;
    557   1.1   itojun #ifdef IPV6_AUTH_TRANS_LEVEL
    558   1.1   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
    559   1.1   itojun 				&optval, sizeof(optval)) == -1)
    560   1.1   itojun 			err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");
    561   1.1   itojun #else /* old def */
    562   1.1   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,
    563   1.1   itojun 				&optval, sizeof(optval)) == -1)
    564   1.1   itojun 			err(1, "setsockopt(IPV6_AUTH_LEVEL)");
    565   1.1   itojun #endif
    566   1.1   itojun 	}
    567   1.1   itojun 	if (options & F_ENCRYPT) {
    568   1.1   itojun 		optval = IPSEC_LEVEL_REQUIRE;
    569   1.1   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
    570   1.1   itojun 				&optval, sizeof(optval)) == -1)
    571   1.1   itojun 			err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");
    572   1.1   itojun 	}
    573   1.1   itojun #endif /*IPSEC_POLICY_IPSEC*/
    574   1.1   itojun #endif
    575   1.1   itojun 
    576   1.1   itojun #ifdef ICMP6_FILTER
    577   1.1   itojun     {
    578   1.1   itojun 	struct icmp6_filter filt;
    579   1.1   itojun 	if (!(options & F_VERBOSE)) {
    580   1.1   itojun 		ICMP6_FILTER_SETBLOCKALL(&filt);
    581  1.15   itojun 		if ((options & F_FQDN) || (options & F_FQDNOLD) ||
    582  1.15   itojun 		    (options & F_NODEADDR))
    583   1.1   itojun 			ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
    584   1.1   itojun 		else
    585   1.1   itojun 			ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
    586   1.1   itojun 	} else {
    587   1.1   itojun 		ICMP6_FILTER_SETPASSALL(&filt);
    588   1.1   itojun 	}
    589   1.1   itojun 	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
    590   1.1   itojun 			sizeof(filt)) < 0)
    591   1.1   itojun 		err(1, "setsockopt(ICMP6_FILTER)");
    592   1.1   itojun     }
    593   1.1   itojun #endif /*ICMP6_FILTER*/
    594   1.1   itojun 
    595  1.11   itojun 	/* let the kerel pass extension headers of incoming packets */
    596  1.11   itojun 	/* TODO: implement parsing routine */
    597  1.11   itojun 	if ((options & F_VERBOSE) != 0) {
    598  1.11   itojun 		int opton = 1;
    599  1.11   itojun 
    600  1.11   itojun #ifdef IPV6_RECVRTHDR
    601  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,
    602  1.11   itojun 			       sizeof(opton)))
    603  1.11   itojun 			err(1, "setsockopt(IPV6_RECVRTHDR)");
    604  1.11   itojun #else  /* old adv. API */
    605  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,
    606  1.11   itojun 			       sizeof(opton)))
    607  1.11   itojun 			err(1, "setsockopt(IPV6_RTHDR)");
    608  1.15   itojun #endif
    609  1.11   itojun #ifdef IPV6_RECVHOPOPTS
    610  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,
    611  1.11   itojun 			       sizeof(opton)))
    612  1.11   itojun 			err(1, "setsockopt(IPV6_RECVHOPOPTS)");
    613  1.11   itojun #else  /* old adv. API */
    614  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,
    615  1.11   itojun 			       sizeof(opton)))
    616  1.11   itojun 			err(1, "setsockopt(IPV6_HOPOPTS)");
    617  1.15   itojun #endif
    618  1.11   itojun #ifdef IPV6_RECVDSTOPTS
    619  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,
    620  1.11   itojun 			       sizeof(opton)))
    621  1.11   itojun 			err(1, "setsockopt(IPV6_RECVDSTOPTS)");
    622  1.11   itojun #else  /* olad adv. API */
    623  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,
    624  1.11   itojun 			       sizeof(opton)))
    625  1.11   itojun 			err(1, "setsockopt(IPV6_DSTOPTS)");
    626  1.15   itojun #endif
    627  1.11   itojun #ifdef IPV6_RECVRTHDRDSTOPTS
    628  1.11   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,
    629  1.11   itojun 			       sizeof(opton)))
    630  1.11   itojun 			err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");
    631  1.15   itojun #endif
    632  1.11   itojun 	}
    633  1.11   itojun 
    634   1.1   itojun /*
    635  1.15   itojun 	optval = 1;
    636   1.1   itojun 	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))
    637   1.1   itojun 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
    638   1.1   itojun 			       &optval, sizeof(optval)) == -1)
    639   1.1   itojun 			err(1, "IPV6_MULTICAST_LOOP");
    640   1.1   itojun */
    641   1.1   itojun 
    642   1.9   itojun 	/* Specify the outgoing interface and/or the source address */
    643   1.9   itojun 	if (usepktinfo)
    644   1.1   itojun 		ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));
    645   1.1   itojun 
    646   1.1   itojun 	if (hoplimit != -1)
    647   1.1   itojun 		ip6optlen += CMSG_SPACE(sizeof(int));
    648   1.1   itojun 
    649  1.11   itojun #ifdef IPV6_REACHCONF
    650  1.11   itojun 	if (options & F_REACHCONF)
    651  1.11   itojun 		ip6optlen += CMSG_SPACE(0);
    652  1.11   itojun #endif
    653  1.11   itojun 
    654   1.1   itojun 	/* set IP6 packet options */
    655   1.1   itojun 	if (ip6optlen) {
    656   1.1   itojun 		if ((scmsg = (char *)malloc(ip6optlen)) == 0)
    657   1.1   itojun 			errx(1, "can't allocate enough memory");
    658   1.1   itojun 		smsghdr.msg_control = (caddr_t)scmsg;
    659   1.1   itojun 		smsghdr.msg_controllen = ip6optlen;
    660   1.1   itojun 		scmsgp = (struct cmsghdr *)scmsg;
    661   1.1   itojun 	}
    662   1.9   itojun 	if (usepktinfo) {
    663   1.9   itojun 		pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
    664   1.9   itojun 		memset(pktinfo, 0, sizeof(*pktinfo));
    665   1.1   itojun 		scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
    666   1.1   itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    667   1.1   itojun 		scmsgp->cmsg_type = IPV6_PKTINFO;
    668   1.9   itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    669   1.9   itojun 	}
    670   1.1   itojun 
    671   1.9   itojun 	/* set the outgoing interface */
    672   1.9   itojun 	if (ifname) {
    673   1.9   itojun #ifndef USE_SIN6_SCOPE_ID
    674   1.9   itojun 		/* pktinfo must have already been allocated */
    675   1.9   itojun 		if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)
    676   1.9   itojun 			    errx(1, "%s: invalid interface name", ifname);
    677   1.1   itojun #else
    678   1.9   itojun 		if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)
    679   1.1   itojun 			errx(1, "%s: invalid interface name", ifname);
    680   1.1   itojun #endif
    681   1.1   itojun 	}
    682   1.9   itojun 	/* set the source address */
    683   1.9   itojun 	if (options & F_SRCADDR)/* pktinfo must be valid */
    684   1.9   itojun 		pktinfo->ipi6_addr = srcaddr;
    685   1.9   itojun 
    686   1.1   itojun 	if (hoplimit != -1) {
    687   1.1   itojun 		scmsgp->cmsg_len = CMSG_LEN(sizeof(int));
    688   1.1   itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    689   1.1   itojun 		scmsgp->cmsg_type = IPV6_HOPLIMIT;
    690   1.1   itojun 		*(int *)(CMSG_DATA(scmsgp)) = hoplimit;
    691   1.1   itojun 
    692   1.1   itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    693   1.1   itojun 	}
    694  1.11   itojun #ifdef IPV6_REACHCONF
    695  1.11   itojun 	if (options & F_REACHCONF) {
    696  1.11   itojun 		scmsgp->cmsg_len = CMSG_LEN(0);
    697  1.11   itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    698  1.11   itojun 		scmsgp->cmsg_type = IPV6_REACHCONF;
    699  1.11   itojun 
    700  1.11   itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    701  1.11   itojun 	}
    702  1.11   itojun #endif
    703  1.11   itojun 
    704   1.1   itojun 	if (argc > 1) {	/* some intermediate addrs are specified */
    705   1.1   itojun 		int hops, error;
    706  1.11   itojun #ifdef USE_RFC2292BIS
    707  1.11   itojun 		int rthdrlen;
    708  1.15   itojun #endif
    709  1.11   itojun 
    710  1.11   itojun #ifdef USE_RFC2292BIS
    711  1.11   itojun 		rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
    712  1.11   itojun 		scmsgp->cmsg_len = CMSG_LEN(rthdrlen);
    713  1.11   itojun 		scmsgp->cmsg_level = IPPROTO_IPV6;
    714  1.11   itojun 		scmsgp->cmsg_type = IPV6_RTHDR;
    715  1.11   itojun 		rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);
    716  1.11   itojun 		rthdr = inet6_rth_init((void *)rthdr, rthdrlen,
    717  1.11   itojun 				       IPV6_RTHDR_TYPE_0, argc - 1);
    718  1.11   itojun 		if (rthdr == NULL)
    719  1.11   itojun 			errx(1, "can't initialize rthdr");
    720  1.11   itojun #else  /* old advanced API */
    721   1.1   itojun 		if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,
    722   1.1   itojun 								 IPV6_RTHDR_TYPE_0)) == 0)
    723   1.1   itojun 			errx(1, "can't initialize rthdr");
    724  1.11   itojun #endif /* USE_RFC2292BIS */
    725   1.1   itojun 
    726   1.1   itojun 		for (hops = 0; hops < argc - 1; hops++) {
    727   1.1   itojun 			struct addrinfo *iaip;
    728   1.1   itojun 
    729   1.1   itojun 			if ((error = getaddrinfo(argv[hops], NULL, &hints, &iaip)))
    730   1.1   itojun 				errx(1, gai_strerror(error));
    731   1.1   itojun 			if (SIN6(res->ai_addr)->sin6_family != AF_INET6)
    732   1.1   itojun 				errx(1,
    733   1.1   itojun 				     "bad addr family of an intermediate addr");
    734   1.1   itojun 
    735  1.11   itojun #ifdef USE_RFC2292BIS
    736  1.11   itojun 			if (inet6_rth_add(rthdr,
    737  1.11   itojun 					  &(SIN6(iaip->ai_addr))->sin6_addr))
    738  1.11   itojun 				errx(1, "can't add an intermediate node");
    739  1.11   itojun #else  /* old advanced API */
    740   1.1   itojun 			if (inet6_rthdr_add(scmsgp,
    741   1.1   itojun 					    &(SIN6(iaip->ai_addr))->sin6_addr,
    742   1.1   itojun 					    IPV6_RTHDR_LOOSE))
    743   1.1   itojun 				errx(1, "can't add an intermediate node");
    744  1.11   itojun #endif /* USE_RFC2292BIS */
    745   1.1   itojun 		}
    746   1.1   itojun 
    747  1.11   itojun #ifndef USE_RFC2292BIS
    748   1.1   itojun 		if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))
    749   1.1   itojun 			errx(1, "can't set the last flag");
    750  1.15   itojun #endif
    751   1.1   itojun 
    752   1.1   itojun 		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);
    753   1.1   itojun 	}
    754   1.1   itojun 
    755   1.1   itojun 	{
    756  1.15   itojun 		/*
    757   1.1   itojun 		 * source selection
    758   1.1   itojun 		 */
    759   1.1   itojun 		int dummy, len = sizeof(src);
    760   1.1   itojun 
    761   1.1   itojun 		if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    762   1.1   itojun 			err(1, "UDP socket");
    763   1.1   itojun 
    764   1.1   itojun 		src.sin6_family = AF_INET6;
    765   1.1   itojun 		src.sin6_addr = dst.sin6_addr;
    766   1.1   itojun 		src.sin6_port = ntohs(DUMMY_PORT);
    767   1.9   itojun 		src.sin6_scope_id = dst.sin6_scope_id;
    768   1.1   itojun 
    769  1.11   itojun 
    770  1.11   itojun #ifdef USE_SIN6_SCOPE_ID
    771  1.11   itojun 		src.sin6_scope_id = dst.sin6_scope_id;
    772  1.11   itojun #endif
    773  1.11   itojun 
    774  1.11   itojun #ifdef USE_RFC2292BIS
    775  1.11   itojun 		if (pktinfo &&
    776  1.11   itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
    777  1.11   itojun 			       (void *)pktinfo, sizeof(*pktinfo)))
    778  1.11   itojun 			err(1, "UDP setsockopt(IPV6_PKTINFO)");
    779  1.11   itojun 
    780  1.11   itojun 		if (hoplimit != -1 &&
    781  1.11   itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,
    782  1.11   itojun 			       (void *)&hoplimit, sizeof(hoplimit)))
    783  1.11   itojun 			err(1, "UDP setsockopt(IPV6_HOPLIMIT)");
    784  1.11   itojun 
    785  1.11   itojun 		if (rthdr &&
    786  1.11   itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,
    787  1.11   itojun 			       (void *)rthdr, (rthdr->ip6r_len + 1) << 3))
    788  1.11   itojun 			err(1, "UDP setsockopt(IPV6_RTHDR)");
    789  1.11   itojun #else  /* old advanced API */
    790  1.11   itojun 		if (smsghdr.msg_control &&
    791  1.11   itojun 		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,
    792   1.1   itojun 			       (void *)smsghdr.msg_control,
    793   1.1   itojun 			       smsghdr.msg_controllen)) {
    794   1.9   itojun 			err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");
    795   1.1   itojun 		}
    796  1.15   itojun #endif
    797   1.1   itojun 
    798   1.1   itojun 		if (connect(dummy, (struct sockaddr *)&src, len) < 0)
    799   1.1   itojun 			err(1, "UDP connect");
    800   1.1   itojun 
    801   1.1   itojun 		if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)
    802   1.1   itojun 			err(1, "getsockname");
    803   1.1   itojun 
    804   1.1   itojun 		close(dummy);
    805   1.1   itojun 	}
    806   1.1   itojun 
    807   1.1   itojun #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
    808   1.1   itojun 	if (sockbufsize) {
    809   1.1   itojun 		if (datalen > sockbufsize)
    810  1.15   itojun 			warnx("you need -b to increase socket buffer size");
    811   1.1   itojun 		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
    812   1.1   itojun 			       sizeof(sockbufsize)) < 0)
    813   1.1   itojun 			err(1, "setsockopt(SO_SNDBUF)");
    814   1.1   itojun 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
    815   1.1   itojun 			       sizeof(sockbufsize)) < 0)
    816   1.1   itojun 			err(1, "setsockopt(SO_RCVBUF)");
    817   1.1   itojun 	}
    818   1.1   itojun 	else {
    819   1.1   itojun 		if (datalen > 8 * 1024)	/*XXX*/
    820   1.1   itojun 			warnx("you need -b to increase socket buffer size");
    821   1.1   itojun 		/*
    822   1.1   itojun 		 * When pinging the broadcast address, you can get a lot of
    823   1.1   itojun 		 * answers. Doing something so evil is useful if you are trying
    824   1.1   itojun 		 * to stress the ethernet, or just want to fill the arp cache
    825   1.1   itojun 		 * to get some stuff for /etc/ethers.
    826   1.1   itojun 		 */
    827   1.1   itojun 		hold = 48 * 1024;
    828   1.1   itojun 		setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, sizeof(hold));
    829   1.1   itojun 	}
    830   1.1   itojun #endif
    831   1.1   itojun 
    832   1.1   itojun 	optval = 1;
    833   1.9   itojun #ifndef USE_SIN6_SCOPE_ID
    834  1.11   itojun #ifdef IPV6_RECVPKTINFO
    835  1.11   itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,
    836  1.11   itojun 		       sizeof(optval)) < 0)
    837  1.11   itojun 		warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */
    838  1.11   itojun #else  /* old adv. API */
    839  1.11   itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,
    840  1.11   itojun 		       sizeof(optval)) < 0)
    841  1.11   itojun 		warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */
    842  1.15   itojun #endif
    843  1.11   itojun #endif /* USE_SIN6_SCOPE_ID */
    844  1.11   itojun #ifdef IPV6_RECVHOPLIMIT
    845  1.11   itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
    846  1.11   itojun 		       sizeof(optval)) < 0)
    847  1.11   itojun 		warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */
    848  1.11   itojun #else  /* old adv. API */
    849  1.11   itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
    850  1.11   itojun 		       sizeof(optval)) < 0)
    851  1.11   itojun 		warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */
    852  1.15   itojun #endif
    853   1.1   itojun 
    854   1.1   itojun 	printf("PING6(%d=40+8+%d bytes) ", datalen + 48, datalen);
    855  1.12   itojun 	printf("%s --> ", pr_addr(&src));
    856  1.12   itojun 	printf("%s\n", pr_addr(&dst));
    857   1.1   itojun 
    858   1.1   itojun 	while (preload--)		/* Fire off them quickies. */
    859   1.1   itojun 		pinger();
    860   1.1   itojun 
    861   1.1   itojun 	(void)signal(SIGINT, onint);
    862   1.1   itojun 	(void)signal(SIGINFO, oninfo);
    863   1.1   itojun 
    864   1.1   itojun 	if ((options & F_FLOOD) == 0) {
    865   1.1   itojun 		(void)signal(SIGALRM, onalrm);
    866   1.1   itojun 		itimer.it_interval.tv_sec = interval;
    867   1.1   itojun 		itimer.it_interval.tv_usec = 0;
    868   1.1   itojun 		itimer.it_value.tv_sec = 0;
    869   1.1   itojun 		itimer.it_value.tv_usec = 1;
    870   1.1   itojun 		(void)setitimer(ITIMER_REAL, &itimer, NULL);
    871   1.1   itojun 	}
    872   1.1   itojun 
    873   1.1   itojun 	FD_ZERO(&fdset);
    874   1.1   itojun 	timeout.tv_sec = 0;
    875   1.1   itojun 	timeout.tv_usec = 10000;
    876   1.1   itojun 	for (;;) {
    877   1.1   itojun 		struct msghdr m;
    878   1.1   itojun 		struct cmsghdr *cm;
    879  1.11   itojun 		u_char buf[1024];
    880   1.1   itojun 		struct iovec iov[2];
    881   1.1   itojun 
    882   1.1   itojun 		if (options & F_FLOOD) {
    883   1.1   itojun 			pinger();
    884   1.1   itojun 			FD_SET(s, &fdset);
    885   1.1   itojun 			if (select(s + 1, &fdset, NULL, NULL, &timeout) < 1)
    886   1.1   itojun 				continue;
    887   1.1   itojun 		}
    888   1.1   itojun 		fromlen = sizeof(from);
    889   1.1   itojun 
    890   1.1   itojun 		m.msg_name = (caddr_t)&from;
    891   1.1   itojun 		m.msg_namelen = sizeof(from);
    892   1.1   itojun 		memset(&iov, 0, sizeof(iov));
    893   1.1   itojun 		iov[0].iov_base = (caddr_t)packet;
    894   1.1   itojun 		iov[0].iov_len = packlen;
    895   1.1   itojun 		m.msg_iov = iov;
    896   1.1   itojun 		m.msg_iovlen = 1;
    897   1.1   itojun 		cm = (struct cmsghdr *)buf;
    898   1.1   itojun 		m.msg_control = (caddr_t)buf;
    899   1.1   itojun 		m.msg_controllen = sizeof(buf);
    900   1.1   itojun 
    901   1.1   itojun 		if ((cc = recvmsg(s, &m, 0)) < 0) {
    902   1.1   itojun 			if (errno == EINTR)
    903   1.1   itojun 				continue;
    904   1.1   itojun 			warn("recvfrom");
    905   1.1   itojun 			continue;
    906   1.1   itojun 		}
    907   1.1   itojun 
    908   1.1   itojun 		pr_pack(packet, cc, &m);
    909   1.1   itojun 		if (npackets && nreceived >= npackets)
    910   1.1   itojun 			break;
    911   1.1   itojun 	}
    912   1.1   itojun 	summary();
    913   1.1   itojun 	exit(nreceived == 0);
    914   1.1   itojun }
    915   1.1   itojun 
    916   1.1   itojun /*
    917   1.1   itojun  * onalrm --
    918   1.1   itojun  *	This routine transmits another ping6.
    919   1.1   itojun  */
    920  1.11   itojun /* ARGSUSED */
    921   1.1   itojun void
    922   1.1   itojun onalrm(signo)
    923   1.1   itojun 	int signo;
    924   1.1   itojun {
    925   1.1   itojun 	struct itimerval itimer;
    926   1.1   itojun 
    927   1.1   itojun 	if (!npackets || ntransmitted < npackets) {
    928   1.1   itojun 		pinger();
    929   1.1   itojun 		return;
    930   1.1   itojun 	}
    931   1.1   itojun 
    932   1.1   itojun 	/*
    933   1.1   itojun 	 * If we're not transmitting any more packets, change the timer
    934   1.1   itojun 	 * to wait two round-trip times if we've received any packets or
    935   1.1   itojun 	 * ten seconds if we haven't.
    936   1.1   itojun 	 */
    937   1.1   itojun #define	MAXWAIT		10
    938   1.1   itojun 	if (nreceived) {
    939   1.1   itojun 		itimer.it_value.tv_sec =  2 * tmax / 1000;
    940   1.1   itojun 		if (itimer.it_value.tv_sec == 0)
    941   1.1   itojun 			itimer.it_value.tv_sec = 1;
    942   1.1   itojun 	} else
    943   1.1   itojun 		itimer.it_value.tv_sec = MAXWAIT;
    944   1.1   itojun 	itimer.it_interval.tv_sec = 0;
    945   1.1   itojun 	itimer.it_interval.tv_usec = 0;
    946   1.1   itojun 	itimer.it_value.tv_usec = 0;
    947   1.1   itojun 
    948   1.1   itojun 	(void)signal(SIGALRM, onint);
    949   1.1   itojun 	(void)setitimer(ITIMER_REAL, &itimer, NULL);
    950   1.1   itojun }
    951   1.1   itojun 
    952   1.1   itojun /*
    953   1.1   itojun  * pinger --
    954   1.1   itojun  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
    955   1.1   itojun  * will be added on by the kernel.  The ID field is our UNIX process ID,
    956   1.1   itojun  * and the sequence number is an ascending integer.  The first 8 bytes
    957   1.1   itojun  * of the data portion are used to hold a UNIX "timeval" struct in VAX
    958   1.1   itojun  * byte-order, to compute the round-trip time.
    959   1.1   itojun  */
    960   1.1   itojun void
    961   1.1   itojun pinger()
    962   1.1   itojun {
    963   1.1   itojun 	struct icmp6_hdr *icp;
    964   1.1   itojun 	struct iovec iov[2];
    965   1.1   itojun 	int i, cc;
    966   1.1   itojun 
    967   1.1   itojun 	icp = (struct icmp6_hdr *)outpack;
    968  1.14   itojun 	memset(icp, 0, sizeof(*icp));
    969   1.1   itojun 	icp->icmp6_code = 0;
    970   1.1   itojun 	icp->icmp6_cksum = 0;
    971   1.9   itojun 	icp->icmp6_seq = ntransmitted++;		/* htons later */
    972   1.9   itojun 	icp->icmp6_id = htons(ident);			/* ID */
    973   1.1   itojun 
    974   1.1   itojun 	CLR(icp->icmp6_seq % mx_dup_ck);
    975   1.9   itojun 	icp->icmp6_seq = htons(icp->icmp6_seq);
    976   1.1   itojun 
    977   1.1   itojun 	if (options & F_FQDN) {
    978   1.1   itojun 		icp->icmp6_type = ICMP6_NI_QUERY;
    979  1.15   itojun 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
    980  1.15   itojun 		/* XXX: overwrite icmp6_id */
    981  1.15   itojun 		((struct icmp6_nodeinfo *)icp)->ni_qtype = htons(NI_QTYPE_FQDN);
    982  1.15   itojun 		((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
    983  1.15   itojun 		if (timing)
    984  1.15   itojun 			(void)gettimeofday((struct timeval *)
    985  1.15   itojun 					   &outpack[ICMP6ECHOLEN], NULL);
    986  1.15   itojun 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
    987  1.15   itojun 		    sizeof(dst.sin6_addr));
    988  1.15   itojun 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
    989  1.15   itojun 		datalen = 0;
    990  1.15   itojun 	} else if (options & F_FQDNOLD) {
    991  1.15   itojun 		/* packet format in 03 draft - no Subject data on queries */
    992  1.15   itojun 		icp->icmp6_type = ICMP6_NI_QUERY;
    993   1.1   itojun 		/* code field is always 0 */
    994   1.1   itojun 		/* XXX: overwrite icmp6_id */
    995  1.14   itojun 		((struct icmp6_nodeinfo *)icp)->ni_qtype = htons(NI_QTYPE_FQDN);
    996  1.14   itojun 		((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
    997   1.1   itojun 		if (timing)
    998   1.1   itojun 			(void)gettimeofday((struct timeval *)
    999   1.1   itojun 					   &outpack[ICMP6ECHOLEN], NULL);
   1000   1.1   itojun 		cc = ICMP6_NIQLEN;
   1001   1.1   itojun 		datalen = 0;
   1002   1.1   itojun 	} else if (options & F_NODEADDR) {
   1003   1.1   itojun 		icp->icmp6_type = ICMP6_NI_QUERY;
   1004  1.15   itojun 		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;
   1005   1.1   itojun 		/* XXX: overwrite icmp6_id */
   1006  1.14   itojun 		((struct icmp6_nodeinfo *)icp)->ni_qtype =
   1007  1.14   itojun 		    htons(NI_QTYPE_NODEADDR);
   1008  1.14   itojun 		((struct icmp6_nodeinfo *)icp)->ni_flags = htons(0);
   1009   1.1   itojun 		if (timing)
   1010   1.1   itojun 			(void)gettimeofday((struct timeval *)
   1011   1.1   itojun 					   &outpack[ICMP6ECHOLEN], NULL);
   1012  1.15   itojun 		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,
   1013  1.15   itojun 		    sizeof(dst.sin6_addr));
   1014  1.15   itojun 		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);
   1015   1.1   itojun 		datalen = 0;
   1016   1.1   itojun 		((struct icmp6_nodeinfo *)icp)->ni_flags = naflags;
   1017   1.1   itojun 	}
   1018   1.1   itojun 	else {
   1019   1.1   itojun 		icp->icmp6_type = ICMP6_ECHO_REQUEST;
   1020   1.1   itojun 		if (timing)
   1021   1.1   itojun 			(void)gettimeofday((struct timeval *)
   1022   1.1   itojun 					   &outpack[ICMP6ECHOLEN], NULL);
   1023   1.1   itojun 		cc = ICMP6ECHOLEN + datalen;
   1024   1.1   itojun 	}
   1025   1.1   itojun 
   1026   1.1   itojun 	smsghdr.msg_name = (caddr_t)&dst;
   1027   1.1   itojun 	smsghdr.msg_namelen = sizeof(dst);
   1028   1.1   itojun 	memset(&iov, 0, sizeof(iov));
   1029   1.1   itojun 	iov[0].iov_base = (caddr_t)outpack;
   1030   1.1   itojun 	iov[0].iov_len = cc;
   1031   1.1   itojun 	smsghdr.msg_iov = iov;
   1032   1.1   itojun 	smsghdr.msg_iovlen = 1;
   1033   1.1   itojun 
   1034   1.1   itojun 	i = sendmsg(s, &smsghdr, 0);
   1035   1.1   itojun 
   1036   1.1   itojun 	if (i < 0 || i != cc)  {
   1037   1.1   itojun 		if (i < 0)
   1038   1.1   itojun 			warn("sendmsg");
   1039   1.1   itojun 		(void)printf("ping6: wrote %s %d chars, ret=%d\n",
   1040   1.1   itojun 		    hostname, cc, i);
   1041   1.1   itojun 	}
   1042   1.1   itojun 	if (!(options & F_QUIET) && options & F_FLOOD)
   1043   1.1   itojun 		(void)write(STDOUT_FILENO, &DOT, 1);
   1044   1.1   itojun }
   1045   1.1   itojun 
   1046   1.1   itojun /*
   1047   1.1   itojun  * pr_pack --
   1048   1.1   itojun  *	Print out the packet, if it came from us.  This logic is necessary
   1049   1.1   itojun  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
   1050   1.1   itojun  * which arrive ('tis only fair).  This permits multiple copies of this
   1051   1.1   itojun  * program to be run without having intermingled output (or statistics!).
   1052   1.1   itojun  */
   1053   1.1   itojun void
   1054   1.1   itojun pr_pack(buf, cc, mhdr)
   1055   1.1   itojun 	u_char *buf;
   1056   1.1   itojun 	int cc;
   1057   1.1   itojun 	struct msghdr *mhdr;
   1058   1.1   itojun {
   1059  1.15   itojun #define safeputc(c)	printf((isprint((c)) ? "%c" : "\\%03o"), c)
   1060   1.1   itojun 	struct icmp6_hdr *icp;
   1061   1.1   itojun 	int i;
   1062   1.1   itojun 	int hoplim;
   1063   1.1   itojun 	struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
   1064   1.1   itojun 	u_char *cp = NULL, *dp, *end = buf + cc;
   1065  1.11   itojun 	struct in6_pktinfo *pktinfo = NULL;
   1066   1.1   itojun 	struct timeval tv, *tp;
   1067   1.1   itojun 	double triptime = 0;
   1068   1.1   itojun 	int dupflag;
   1069   1.1   itojun 	size_t off;
   1070  1.15   itojun 	int oldfqdn;
   1071   1.1   itojun 
   1072   1.1   itojun 	(void)gettimeofday(&tv, NULL);
   1073   1.1   itojun 
   1074   1.1   itojun 	if (cc < sizeof(struct icmp6_hdr)) {
   1075   1.1   itojun 		if (options & F_VERBOSE)
   1076   1.1   itojun 			warnx("packet too short (%d bytes) from %s\n", cc,
   1077  1.12   itojun 			    pr_addr(from));
   1078   1.1   itojun 		return;
   1079   1.1   itojun 	}
   1080   1.1   itojun 	icp = (struct icmp6_hdr *)buf;
   1081   1.1   itojun 	off = 0;
   1082   1.1   itojun 
   1083   1.1   itojun 	if ((hoplim = get_hoplim(mhdr)) == -1) {
   1084   1.1   itojun 		warnx("failed to get receiving hop limit");
   1085   1.1   itojun 		return;
   1086   1.1   itojun 	}
   1087  1.11   itojun 	if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) {
   1088  1.11   itojun 		warnx("failed to get receiving pakcet information");
   1089  1.11   itojun 		return;
   1090  1.11   itojun 	}
   1091   1.1   itojun 
   1092   1.1   itojun 	if (icp->icmp6_type == ICMP6_ECHO_REPLY) {
   1093   1.9   itojun 		/* XXX the following line overwrites the original packet */
   1094   1.9   itojun 		icp->icmp6_seq = ntohs(icp->icmp6_seq);
   1095   1.9   itojun 		if (ntohs(icp->icmp6_id) != ident)
   1096   1.1   itojun 			return;			/* It was not our ECHO */
   1097   1.1   itojun 		++nreceived;
   1098   1.1   itojun 		if (timing) {
   1099   1.1   itojun 			tp = (struct timeval *)(icp + 1);
   1100   1.1   itojun 			tvsub(&tv, tp);
   1101   1.1   itojun 			triptime = ((double)tv.tv_sec) * 1000.0 +
   1102   1.1   itojun 			    ((double)tv.tv_usec) / 1000.0;
   1103   1.1   itojun 			tsum += triptime;
   1104   1.1   itojun 			if (triptime < tmin)
   1105   1.1   itojun 				tmin = triptime;
   1106   1.1   itojun 			if (triptime > tmax)
   1107   1.1   itojun 				tmax = triptime;
   1108   1.1   itojun 		}
   1109   1.1   itojun 
   1110   1.1   itojun 		if (TST(icp->icmp6_seq % mx_dup_ck)) {
   1111   1.1   itojun 			++nrepeats;
   1112   1.1   itojun 			--nreceived;
   1113   1.1   itojun 			dupflag = 1;
   1114   1.1   itojun 		} else {
   1115   1.1   itojun 			SET(icp->icmp6_seq % mx_dup_ck);
   1116   1.1   itojun 			dupflag = 0;
   1117   1.1   itojun 		}
   1118   1.1   itojun 
   1119   1.1   itojun 		if (options & F_QUIET)
   1120   1.1   itojun 			return;
   1121   1.1   itojun 
   1122   1.1   itojun 		if (options & F_FLOOD)
   1123   1.1   itojun 			(void)write(STDOUT_FILENO, &BSPACE, 1);
   1124   1.1   itojun 		else {
   1125   1.1   itojun 			(void)printf("%d bytes from %s, icmp_seq=%u", cc,
   1126   1.9   itojun 				     pr_addr(from),
   1127   1.1   itojun 				     icp->icmp6_seq);
   1128   1.1   itojun 			(void)printf(" hlim=%d", hoplim);
   1129  1.11   itojun 			if ((options & F_VERBOSE) != 0) {
   1130  1.11   itojun 				struct sockaddr_in6 dstsa;
   1131  1.11   itojun 
   1132  1.11   itojun 				memset(&dstsa, 0, sizeof(dstsa));
   1133  1.11   itojun 				dstsa.sin6_family = AF_INET6;
   1134  1.11   itojun 				dstsa.sin6_len = sizeof(dstsa);
   1135  1.11   itojun 				dstsa.sin6_scope_id = pktinfo->ipi6_ifindex;
   1136  1.11   itojun 				dstsa.sin6_addr = pktinfo->ipi6_addr;
   1137  1.11   itojun 				(void)printf(" dst=%s", pr_addr(&dstsa));
   1138  1.11   itojun 			}
   1139   1.1   itojun 			if (timing)
   1140   1.1   itojun 				(void)printf(" time=%g ms", triptime);
   1141   1.1   itojun 			if (dupflag)
   1142   1.1   itojun 				(void)printf("(DUP!)");
   1143   1.1   itojun 			/* check the data */
   1144   1.1   itojun 			cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
   1145   1.1   itojun 			dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN;
   1146   1.1   itojun 			for (i = 8; cp < end; ++i, ++cp, ++dp) {
   1147   1.1   itojun 				if (*cp != *dp) {
   1148   1.1   itojun 					(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp);
   1149   1.1   itojun 					break;
   1150   1.1   itojun 				}
   1151   1.1   itojun 			}
   1152   1.1   itojun 		}
   1153   1.1   itojun 	} else if (icp->icmp6_type == ICMP6_NI_REPLY) { /* ICMP6_NI_REPLY */
   1154   1.1   itojun 		struct icmp6_nodeinfo *ni = (struct icmp6_nodeinfo *)(buf + off);
   1155   1.1   itojun 
   1156   1.1   itojun 		(void)printf("%d bytes from %s: ", cc,
   1157   1.9   itojun 			     pr_addr(from));
   1158   1.1   itojun 
   1159   1.1   itojun 		switch(ntohs(ni->ni_qtype)) {
   1160   1.1   itojun 		 case NI_QTYPE_NOOP:
   1161   1.1   itojun 			 printf("NodeInfo NOOP");
   1162   1.1   itojun 			 break;
   1163   1.1   itojun 		 case NI_QTYPE_SUPTYPES:
   1164   1.1   itojun 			 printf("NodeInfo Supported Qtypes");
   1165   1.1   itojun 			 break;
   1166   1.1   itojun 		 case NI_QTYPE_NODEADDR:
   1167   1.1   itojun 			 pr_nodeaddr(ni, end - (u_char *)ni);
   1168   1.1   itojun 			 break;
   1169   1.1   itojun 		 case NI_QTYPE_FQDN:
   1170   1.1   itojun 		 default:	/* XXX: for backward compatibility */
   1171  1.15   itojun 			cp = (u_char *)ni + ICMP6_NIRLEN;
   1172  1.15   itojun 			if (buf[off + ICMP6_NIRLEN] ==
   1173  1.15   itojun 			    cc - off - ICMP6_NIRLEN - 1)
   1174  1.15   itojun 				oldfqdn = 1;
   1175  1.15   itojun 			else
   1176  1.15   itojun 				oldfqdn = 0;
   1177  1.15   itojun 			if (oldfqdn) {
   1178  1.15   itojun 				cp++;
   1179  1.15   itojun 				while (cp < end) {
   1180  1.15   itojun 					safeputc(*cp & 0xff);
   1181  1.15   itojun 					cp++;
   1182  1.15   itojun 				}
   1183  1.15   itojun 			} else {
   1184  1.15   itojun 				while (cp < end) {
   1185  1.15   itojun 					i = *cp++;
   1186  1.15   itojun 					if (i) {
   1187  1.15   itojun 						if (i > end - cp) {
   1188  1.15   itojun 							printf("???");
   1189  1.15   itojun 							break;
   1190  1.15   itojun 						}
   1191  1.15   itojun 						while (i-- && cp < end) {
   1192  1.15   itojun 							safeputc(*cp & 0xff);
   1193  1.15   itojun 							cp++;
   1194  1.15   itojun 						}
   1195  1.15   itojun 						if (cp + 1 < end && *cp)
   1196  1.15   itojun 							printf(".");
   1197  1.15   itojun 					} else {
   1198  1.15   itojun 						if (cp == end) {
   1199  1.15   itojun 							/* FQDN */
   1200  1.15   itojun 							printf(".");
   1201  1.15   itojun 						} else if (cp + 1 == end &&
   1202  1.15   itojun 							   *cp == '\0') {
   1203  1.15   itojun 							/* truncated */
   1204  1.15   itojun 						} else {
   1205  1.15   itojun 							/* invalid */
   1206  1.15   itojun 							printf("???");
   1207  1.15   itojun 						}
   1208  1.15   itojun 						break;
   1209  1.15   itojun 					}
   1210  1.15   itojun 				}
   1211  1.15   itojun 			}
   1212  1.15   itojun 			if (options & F_VERBOSE) {
   1213  1.15   itojun 				int32_t ttl;
   1214  1.15   itojun 				int comma = 0;
   1215  1.15   itojun 
   1216  1.15   itojun 				(void)printf(" (");	/*)*/
   1217  1.15   itojun 
   1218  1.15   itojun 				switch(ni->ni_code) {
   1219  1.15   itojun 				case ICMP6_NI_REFUSED:
   1220  1.15   itojun 					(void)printf("refused");
   1221  1.15   itojun 					comma++;
   1222  1.15   itojun 					break;
   1223  1.15   itojun 				case ICMP6_NI_UNKNOWN:
   1224  1.15   itojun 					(void)printf("unknwon qtype");
   1225  1.15   itojun 					comma++;
   1226  1.15   itojun 					break;
   1227  1.15   itojun 				}
   1228   1.1   itojun 
   1229  1.15   itojun 				if ((end - (u_char *)ni) < ICMP6_NIRLEN) {
   1230  1.15   itojun 					/* case of refusion, unkown */
   1231  1.15   itojun 					goto fqdnend;
   1232  1.15   itojun 				}
   1233  1.15   itojun 				ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]);
   1234  1.15   itojun 				if (comma)
   1235  1.15   itojun 					printf(",");
   1236  1.15   itojun 				if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL))
   1237  1.15   itojun 					(void)printf("TTL=%d:meaningless",
   1238  1.15   itojun 						     (int)ttl);
   1239  1.15   itojun 				else {
   1240  1.15   itojun 					if (ttl < 0) {
   1241  1.15   itojun 						(void)printf("TTL=%d:invalid",
   1242  1.15   itojun 						    ttl);
   1243  1.15   itojun 					} else
   1244  1.15   itojun 						(void)printf("TTL=%d", ttl);
   1245  1.15   itojun 				}
   1246  1.15   itojun 				comma++;
   1247   1.1   itojun 
   1248  1.15   itojun 				if (oldfqdn) {
   1249  1.15   itojun 					if (comma)
   1250  1.15   itojun 						printf(",");
   1251  1.15   itojun 					printf("03 draft");
   1252  1.15   itojun 					comma++;
   1253  1.15   itojun 				} else {
   1254  1.15   itojun 					cp = (u_char *)ni + ICMP6_NIRLEN;
   1255  1.15   itojun 					if (cp == end) {
   1256  1.15   itojun 						if (comma)
   1257  1.15   itojun 							printf(",");
   1258  1.15   itojun 						printf("no name");
   1259  1.15   itojun 						comma++;
   1260  1.15   itojun 					}
   1261  1.15   itojun 				}
   1262   1.1   itojun 
   1263  1.15   itojun 				if (buf[off + ICMP6_NIRLEN] !=
   1264  1.15   itojun 				    cc - off - ICMP6_NIRLEN - 1 && oldfqdn) {
   1265  1.15   itojun 					if (comma)
   1266  1.15   itojun 						printf(",");
   1267  1.15   itojun 					(void)printf("invalid namelen:%d/%lu",
   1268  1.15   itojun 						     buf[off + ICMP6_NIRLEN],
   1269  1.15   itojun 						     (u_long)cc - off - ICMP6_NIRLEN - 1);
   1270  1.15   itojun 					comma++;
   1271  1.15   itojun 				}
   1272  1.15   itojun 				/*(*/
   1273  1.15   itojun 				putchar(')');
   1274  1.15   itojun 			}
   1275  1.15   itojun 		 fqdnend:
   1276  1.15   itojun 			;
   1277   1.1   itojun 		}
   1278   1.1   itojun 	} else {
   1279   1.1   itojun 		/* We've got something other than an ECHOREPLY */
   1280   1.1   itojun 		if (!(options & F_VERBOSE))
   1281   1.1   itojun 			return;
   1282   1.1   itojun 		(void)printf("%d bytes from %s: ", cc,
   1283   1.9   itojun 			     pr_addr(from));
   1284   1.1   itojun 		pr_icmph(icp, end);
   1285   1.1   itojun 	}
   1286   1.1   itojun 
   1287   1.1   itojun 	if (!(options & F_FLOOD)) {
   1288   1.1   itojun 		(void)putchar('\n');
   1289  1.11   itojun 		if (options & F_VERBOSE)
   1290  1.11   itojun 			pr_exthdrs(mhdr);
   1291   1.1   itojun 		(void)fflush(stdout);
   1292   1.1   itojun 	}
   1293  1.15   itojun #undef safeputc
   1294   1.1   itojun }
   1295   1.1   itojun 
   1296   1.1   itojun void
   1297  1.11   itojun pr_exthdrs(mhdr)
   1298  1.11   itojun 	struct msghdr *mhdr;
   1299  1.11   itojun {
   1300  1.11   itojun 	struct cmsghdr *cm;
   1301  1.11   itojun 
   1302  1.11   itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1303  1.11   itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1304  1.11   itojun 		if (cm->cmsg_level != IPPROTO_IPV6)
   1305  1.11   itojun 			continue;
   1306  1.11   itojun 
   1307  1.11   itojun 		switch(cm->cmsg_type) {
   1308  1.11   itojun 		case IPV6_HOPOPTS:
   1309  1.11   itojun 			printf("  HbH Options: ");
   1310  1.11   itojun 			pr_ip6opt(CMSG_DATA(cm));
   1311  1.11   itojun 			break;
   1312  1.11   itojun 		case IPV6_DSTOPTS:
   1313  1.11   itojun #ifdef IPV6_RTHDRDSTOPTS
   1314  1.11   itojun 		case IPV6_RTHDRDSTOPTS:
   1315  1.11   itojun #endif
   1316  1.11   itojun 			printf("  Dst Options: ");
   1317  1.11   itojun 			pr_ip6opt(CMSG_DATA(cm));
   1318  1.11   itojun 			break;
   1319  1.11   itojun 		case IPV6_RTHDR:
   1320  1.11   itojun 			printf("  Routing: ");
   1321  1.11   itojun 			pr_rthdr(CMSG_DATA(cm));
   1322  1.11   itojun 			break;
   1323  1.11   itojun 		}
   1324  1.11   itojun 	}
   1325  1.11   itojun }
   1326  1.11   itojun 
   1327  1.11   itojun #ifdef USE_RFC2292BIS
   1328  1.11   itojun void
   1329  1.11   itojun pr_ip6opt(void *extbuf)
   1330  1.11   itojun {
   1331  1.11   itojun 	struct ip6_hbh *ext;
   1332  1.11   itojun 	int currentlen;
   1333  1.11   itojun 	u_int8_t type;
   1334  1.11   itojun 	size_t extlen, len;
   1335  1.11   itojun 	void *databuf;
   1336  1.11   itojun 	size_t offset;
   1337  1.11   itojun 	u_int16_t value2;
   1338  1.11   itojun 	u_int32_t value4;
   1339  1.11   itojun 
   1340  1.11   itojun 	ext = (struct ip6_hbh *)extbuf;
   1341  1.11   itojun 	extlen = (ext->ip6h_len + 1) * 8;
   1342  1.11   itojun 	printf("nxt %u, len %u (%d bytes)\n", ext->ip6h_nxt,
   1343  1.11   itojun 	       ext->ip6h_len, extlen);
   1344  1.11   itojun 
   1345  1.11   itojun 	currentlen = 0;
   1346  1.11   itojun 	while (1) {
   1347  1.11   itojun 		currentlen = inet6_opt_next(extbuf, extlen, currentlen,
   1348  1.11   itojun 					    &type, &len, &databuf);
   1349  1.11   itojun 		if (currentlen == -1)
   1350  1.11   itojun 			break;
   1351  1.11   itojun 		switch (type) {
   1352  1.11   itojun 		/*
   1353  1.11   itojun 		 * Note that inet6_opt_next automatically skips any padding
   1354  1.11   itojun 		 * optins.
   1355  1.11   itojun 		 */
   1356  1.11   itojun 		case IP6OPT_JUMBO:
   1357  1.11   itojun 			offset = 0;
   1358  1.11   itojun 			offset = inet6_opt_get_val(databuf, offset,
   1359  1.11   itojun 						   &value4, sizeof(value4));
   1360  1.11   itojun 			printf("    Jumbo Payload Opt: Length %u\n",
   1361  1.11   itojun 			       (unsigned int)ntohl(value4));
   1362  1.11   itojun 			break;
   1363  1.11   itojun 		case IP6OPT_ROUTER_ALERT:
   1364  1.11   itojun 			offset = 0;
   1365  1.11   itojun 			offset = inet6_opt_get_val(databuf, offset,
   1366  1.11   itojun 						   &value2, sizeof(value2));
   1367  1.11   itojun 			printf("    Router Alert Opt: Type %u\n",
   1368  1.11   itojun 			       ntohs(value2));
   1369  1.11   itojun 			break;
   1370  1.11   itojun 		default:
   1371  1.11   itojun 			printf("    Received Opt %u len %u\n", type, len);
   1372  1.11   itojun 			break;
   1373  1.11   itojun 		}
   1374  1.11   itojun 	}
   1375  1.11   itojun 	return;
   1376  1.11   itojun }
   1377  1.11   itojun #else  /* !USE_RFC2292BIS */
   1378  1.11   itojun /* ARGSUSED */
   1379  1.11   itojun void
   1380  1.11   itojun pr_ip6opt(void *extbuf)
   1381  1.11   itojun {
   1382  1.11   itojun 	putchar('\n');
   1383  1.11   itojun 	return;
   1384  1.11   itojun }
   1385  1.11   itojun #endif /* USE_RFC2292BIS */
   1386  1.11   itojun 
   1387  1.11   itojun #ifdef USE_RFC2292BIS
   1388  1.11   itojun void
   1389  1.11   itojun pr_rthdr(void *extbuf)
   1390  1.11   itojun {
   1391  1.11   itojun 	struct in6_addr *in6;
   1392  1.11   itojun 	char ntopbuf[INET6_ADDRSTRLEN];
   1393  1.11   itojun 	struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
   1394  1.11   itojun 	int i, segments;
   1395  1.11   itojun 
   1396  1.11   itojun 	/* print fixed part of the header */
   1397  1.11   itojun 	printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
   1398  1.11   itojun 	       rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
   1399  1.11   itojun 	if ((segments = inet6_rth_segments(extbuf)) >= 0)
   1400  1.11   itojun 		printf("%d segments, ", segments);
   1401  1.11   itojun 	else
   1402  1.11   itojun 		printf("segments unknown, ");
   1403  1.11   itojun 	printf("%d left\n", rh->ip6r_segleft);
   1404  1.11   itojun 
   1405  1.11   itojun 	for (i = 0; i < segments; i++) {
   1406  1.11   itojun 		in6 = inet6_rth_getaddr(extbuf, i);
   1407  1.11   itojun 		if (in6 == NULL)
   1408  1.11   itojun 			printf("   [%d]<NULL>\n", i);
   1409  1.11   itojun 		else
   1410  1.11   itojun 			printf("   [%d]%s\n", i,
   1411  1.12   itojun 			       inet_ntop(AF_INET6, in6,
   1412  1.11   itojun 					 ntopbuf, sizeof(ntopbuf)));
   1413  1.11   itojun 	}
   1414  1.11   itojun 
   1415  1.11   itojun 	return;
   1416  1.11   itojun 
   1417  1.11   itojun }
   1418  1.11   itojun #else  /* !USE_RFC2292BIS */
   1419  1.11   itojun /* ARGSUSED */
   1420  1.11   itojun void
   1421  1.11   itojun pr_rthdr(void *extbuf)
   1422  1.11   itojun {
   1423  1.11   itojun 	putchar('\n');
   1424  1.11   itojun 	return;
   1425  1.11   itojun }
   1426  1.11   itojun #endif /* USE_RFC2292BIS */
   1427  1.11   itojun 
   1428  1.11   itojun 
   1429  1.11   itojun void
   1430   1.1   itojun pr_nodeaddr(ni, nilen)
   1431   1.1   itojun 	struct icmp6_nodeinfo *ni; /* ni->qtype must be NODEADDR */
   1432   1.1   itojun 	int nilen;
   1433   1.1   itojun {
   1434   1.1   itojun 	struct in6_addr *ia6 = (struct in6_addr *)(ni + 1);
   1435  1.12   itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   1436   1.1   itojun 
   1437   1.1   itojun 	nilen -= sizeof(struct icmp6_nodeinfo);
   1438   1.1   itojun 
   1439   1.1   itojun 	if (options & F_VERBOSE) {
   1440   1.1   itojun 		switch(ni->ni_code) {
   1441   1.1   itojun 		 case ICMP6_NI_REFUSED:
   1442   1.1   itojun 			 (void)printf("refused");
   1443   1.1   itojun 			 break;
   1444   1.1   itojun 		 case ICMP6_NI_UNKNOWN:
   1445   1.1   itojun 			 (void)printf("unknown qtype");
   1446   1.1   itojun 			 break;
   1447   1.1   itojun 		}
   1448   1.1   itojun 		if (ni->ni_flags & NI_NODEADDR_FLAG_ALL)
   1449   1.1   itojun 			(void)printf(" truncated");
   1450   1.1   itojun 	}
   1451   1.1   itojun 	putchar('\n');
   1452   1.1   itojun 	if (nilen <= 0)
   1453   1.1   itojun 		printf("  no address\n");
   1454   1.1   itojun 	for (; nilen > 0; nilen -= sizeof(*ia6), ia6 += 1) {
   1455   1.1   itojun 		printf("  %s\n",
   1456   1.1   itojun 		       inet_ntop(AF_INET6, ia6, ntop_buf, sizeof(ntop_buf)));
   1457   1.1   itojun 	}
   1458   1.1   itojun }
   1459   1.1   itojun 
   1460   1.1   itojun int
   1461   1.1   itojun get_hoplim(mhdr)
   1462   1.1   itojun 	struct msghdr *mhdr;
   1463   1.1   itojun {
   1464   1.1   itojun 	struct cmsghdr *cm;
   1465   1.1   itojun 
   1466   1.1   itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1467   1.1   itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1468   1.1   itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1469   1.1   itojun 		    cm->cmsg_type == IPV6_HOPLIMIT &&
   1470   1.1   itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
   1471   1.1   itojun 			return(*(int *)CMSG_DATA(cm));
   1472   1.1   itojun 	}
   1473   1.1   itojun 
   1474   1.1   itojun 	return(-1);
   1475   1.1   itojun }
   1476   1.1   itojun 
   1477  1.11   itojun struct in6_pktinfo *
   1478  1.11   itojun get_rcvpktinfo(mhdr)
   1479  1.11   itojun 	struct msghdr *mhdr;
   1480  1.11   itojun {
   1481  1.11   itojun 	struct cmsghdr *cm;
   1482  1.11   itojun 
   1483  1.11   itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
   1484  1.11   itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
   1485  1.11   itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
   1486  1.11   itojun 		    cm->cmsg_type == IPV6_PKTINFO &&
   1487  1.11   itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
   1488  1.11   itojun 			return((struct in6_pktinfo *)CMSG_DATA(cm));
   1489  1.11   itojun 	}
   1490  1.11   itojun 
   1491  1.11   itojun 	return(NULL);
   1492  1.11   itojun }
   1493  1.11   itojun 
   1494   1.1   itojun /*
   1495   1.1   itojun  * tvsub --
   1496   1.1   itojun  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
   1497   1.1   itojun  * be >= in.
   1498   1.1   itojun  */
   1499   1.1   itojun void
   1500   1.1   itojun tvsub(out, in)
   1501   1.1   itojun 	register struct timeval *out, *in;
   1502   1.1   itojun {
   1503   1.1   itojun 	if ((out->tv_usec -= in->tv_usec) < 0) {
   1504   1.1   itojun 		--out->tv_sec;
   1505   1.1   itojun 		out->tv_usec += 1000000;
   1506   1.1   itojun 	}
   1507   1.1   itojun 	out->tv_sec -= in->tv_sec;
   1508   1.1   itojun }
   1509   1.1   itojun 
   1510   1.1   itojun /*
   1511   1.1   itojun  * oninfo --
   1512   1.1   itojun  *	SIGINFO handler.
   1513   1.1   itojun  */
   1514  1.11   itojun /* ARGSUSED */
   1515   1.1   itojun void
   1516   1.1   itojun oninfo(notused)
   1517   1.1   itojun 	int notused;
   1518   1.1   itojun {
   1519   1.1   itojun 	summary();
   1520   1.1   itojun }
   1521   1.1   itojun 
   1522   1.1   itojun /*
   1523   1.1   itojun  * onint --
   1524   1.1   itojun  *	SIGINT handler.
   1525   1.1   itojun  */
   1526  1.11   itojun /* ARGSUSED */
   1527   1.1   itojun void
   1528   1.1   itojun onint(notused)
   1529   1.1   itojun 	int notused;
   1530   1.1   itojun {
   1531   1.1   itojun 	summary();
   1532   1.1   itojun 
   1533   1.1   itojun 	(void)signal(SIGINT, SIG_DFL);
   1534   1.1   itojun 	(void)kill(getpid(), SIGINT);
   1535   1.1   itojun 
   1536   1.1   itojun 	/* NOTREACHED */
   1537   1.1   itojun 	exit(1);
   1538   1.1   itojun }
   1539   1.1   itojun 
   1540   1.1   itojun /*
   1541   1.1   itojun  * summary --
   1542   1.1   itojun  *	Print out statistics.
   1543   1.1   itojun  */
   1544   1.1   itojun void
   1545   1.1   itojun summary()
   1546   1.1   itojun {
   1547   1.1   itojun 	register int i;
   1548   1.1   itojun 
   1549   1.1   itojun 	(void)printf("\n--- %s ping6 statistics ---\n", hostname);
   1550   1.1   itojun 	(void)printf("%ld packets transmitted, ", ntransmitted);
   1551   1.1   itojun 	(void)printf("%ld packets received, ", nreceived);
   1552   1.1   itojun 	if (nrepeats)
   1553   1.1   itojun 		(void)printf("+%ld duplicates, ", nrepeats);
   1554   1.1   itojun 	if (ntransmitted) {
   1555   1.1   itojun 		if (nreceived > ntransmitted)
   1556   1.1   itojun 			(void)printf("-- somebody's printing up packets!");
   1557   1.1   itojun 		else
   1558   1.1   itojun 			(void)printf("%d%% packet loss",
   1559   1.1   itojun 			    (int) (((ntransmitted - nreceived) * 100) /
   1560   1.1   itojun 			    ntransmitted));
   1561   1.1   itojun 	}
   1562   1.1   itojun 	(void)putchar('\n');
   1563   1.1   itojun 	if (nreceived && timing) {
   1564   1.1   itojun 		/* Only display average to microseconds */
   1565   1.1   itojun 		i = 1000.0 * tsum / (nreceived + nrepeats);
   1566   1.1   itojun 		(void)printf("round-trip min/avg/max = %g/%g/%g ms\n",
   1567   1.1   itojun 		    tmin, ((double)i) / 1000.0, tmax);
   1568   1.1   itojun 		(void)fflush(stdout);
   1569   1.1   itojun 	}
   1570   1.1   itojun }
   1571   1.1   itojun 
   1572   1.1   itojun #ifdef notdef
   1573   1.1   itojun static char *ttab[] = {
   1574   1.1   itojun 	"Echo Reply",		/* ip + seq + udata */
   1575   1.1   itojun 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
   1576   1.1   itojun 	"Source Quench",	/* IP */
   1577   1.1   itojun 	"Redirect",		/* redirect type, gateway, + IP  */
   1578   1.1   itojun 	"Echo",
   1579   1.1   itojun 	"Time Exceeded",	/* transit, frag reassem + IP */
   1580   1.1   itojun 	"Parameter Problem",	/* pointer + IP */
   1581   1.1   itojun 	"Timestamp",		/* id + seq + three timestamps */
   1582   1.1   itojun 	"Timestamp Reply",	/* " */
   1583   1.1   itojun 	"Info Request",		/* id + sq */
   1584   1.1   itojun 	"Info Reply"		/* " */
   1585   1.1   itojun };
   1586   1.1   itojun #endif
   1587   1.1   itojun 
   1588   1.1   itojun /*
   1589   1.1   itojun  * pr_icmph --
   1590   1.1   itojun  *	Print a descriptive string about an ICMP header.
   1591   1.1   itojun  */
   1592   1.1   itojun void
   1593   1.1   itojun pr_icmph(icp, end)
   1594   1.1   itojun 	struct icmp6_hdr *icp;
   1595   1.1   itojun 	u_char *end;
   1596   1.1   itojun {
   1597  1.12   itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   1598  1.12   itojun 
   1599   1.1   itojun 	switch(icp->icmp6_type) {
   1600   1.1   itojun 	case ICMP6_DST_UNREACH:
   1601   1.1   itojun 		switch(icp->icmp6_code) {
   1602   1.1   itojun 		case ICMP6_DST_UNREACH_NOROUTE:
   1603   1.1   itojun 			(void)printf("No Route to Destination\n");
   1604   1.1   itojun 			break;
   1605   1.1   itojun 		case ICMP6_DST_UNREACH_ADMIN:
   1606   1.1   itojun 			(void)printf("Destination Administratively "
   1607   1.1   itojun 				     "Unreachable\n");
   1608   1.1   itojun 			break;
   1609   1.9   itojun 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
   1610   1.9   itojun 			(void)printf("Destination Unreachable Beyond Scope\n");
   1611   1.1   itojun 			break;
   1612   1.1   itojun 		case ICMP6_DST_UNREACH_ADDR:
   1613   1.1   itojun 			(void)printf("Destination Host Unreachable\n");
   1614   1.1   itojun 			break;
   1615   1.1   itojun 		case ICMP6_DST_UNREACH_NOPORT:
   1616   1.1   itojun 			(void)printf("Destination Port Unreachable\n");
   1617   1.1   itojun 			break;
   1618   1.1   itojun 		default:
   1619   1.1   itojun 			(void)printf("Destination Unreachable, Bad Code: %d\n",
   1620   1.1   itojun 			    icp->icmp6_code);
   1621   1.1   itojun 			break;
   1622   1.1   itojun 		}
   1623   1.1   itojun 		/* Print returned IP header information */
   1624   1.1   itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   1625   1.1   itojun 		break;
   1626   1.1   itojun 	case ICMP6_PACKET_TOO_BIG:
   1627   1.1   itojun 		(void)printf("Packet too big mtu = %d\n",
   1628   1.1   itojun 			     (int)ntohl(icp->icmp6_mtu));
   1629  1.11   itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   1630   1.1   itojun 		break;
   1631   1.1   itojun 	case ICMP6_TIME_EXCEEDED:
   1632   1.1   itojun 		switch(icp->icmp6_code) {
   1633   1.1   itojun 		case ICMP6_TIME_EXCEED_TRANSIT:
   1634   1.1   itojun 			(void)printf("Time to live exceeded\n");
   1635   1.1   itojun 			break;
   1636   1.1   itojun 		case ICMP6_TIME_EXCEED_REASSEMBLY:
   1637   1.1   itojun 			(void)printf("Frag reassembly time exceeded\n");
   1638   1.1   itojun 			break;
   1639   1.1   itojun 		default:
   1640   1.1   itojun 			(void)printf("Time exceeded, Bad Code: %d\n",
   1641   1.1   itojun 			    icp->icmp6_code);
   1642   1.1   itojun 			break;
   1643   1.1   itojun 		}
   1644   1.1   itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   1645   1.1   itojun 		break;
   1646   1.1   itojun 	case ICMP6_PARAM_PROB:
   1647   1.1   itojun 		(void)printf("Parameter problem: ");
   1648   1.1   itojun 		switch(icp->icmp6_code) {
   1649   1.1   itojun 		 case ICMP6_PARAMPROB_HEADER:
   1650   1.1   itojun 			 (void)printf("Erroneous Header ");
   1651   1.1   itojun 			 break;
   1652   1.1   itojun 		 case ICMP6_PARAMPROB_NEXTHEADER:
   1653   1.1   itojun 			 (void)printf("Unknown Nextheader ");
   1654   1.1   itojun 			 break;
   1655   1.1   itojun 		 case ICMP6_PARAMPROB_OPTION:
   1656   1.1   itojun 			 (void)printf("Unrecognized Option ");
   1657   1.1   itojun 			 break;
   1658   1.1   itojun 		 default:
   1659   1.1   itojun 			 (void)printf("Bad code(%d) ", icp->icmp6_code);
   1660   1.1   itojun 			 break;
   1661   1.1   itojun 		}
   1662   1.1   itojun 		(void)printf("pointer = 0x%02x\n",
   1663   1.1   itojun 			     (int)ntohl(icp->icmp6_pptr));
   1664   1.1   itojun 		pr_retip((struct ip6_hdr *)(icp + 1), end);
   1665   1.1   itojun 		break;
   1666   1.1   itojun 	case ICMP6_ECHO_REQUEST:
   1667  1.11   itojun 		(void)printf("Echo Request");
   1668   1.1   itojun 		/* XXX ID + Seq + Data */
   1669   1.1   itojun 		break;
   1670   1.1   itojun 	case ICMP6_ECHO_REPLY:
   1671  1.11   itojun 		(void)printf("Echo Reply");
   1672   1.1   itojun 		/* XXX ID + Seq + Data */
   1673   1.1   itojun 		break;
   1674   1.1   itojun 	case ICMP6_MEMBERSHIP_QUERY:
   1675  1.11   itojun 		(void)printf("Listener Query");
   1676   1.1   itojun 		break;
   1677   1.1   itojun 	case ICMP6_MEMBERSHIP_REPORT:
   1678  1.11   itojun 		(void)printf("Listener Report");
   1679   1.1   itojun 		break;
   1680   1.1   itojun 	case ICMP6_MEMBERSHIP_REDUCTION:
   1681  1.11   itojun 		(void)printf("Listener Done");
   1682   1.1   itojun 		break;
   1683   1.1   itojun 	case ND_ROUTER_SOLICIT:
   1684  1.11   itojun 		(void)printf("Router Solicitation");
   1685   1.1   itojun 		break;
   1686   1.1   itojun 	case ND_ROUTER_ADVERT:
   1687  1.11   itojun 		(void)printf("Router Advertisement");
   1688   1.1   itojun 		break;
   1689   1.1   itojun 	case ND_NEIGHBOR_SOLICIT:
   1690  1.11   itojun 		(void)printf("Neighbor Solicitation");
   1691   1.1   itojun 		break;
   1692   1.1   itojun 	case ND_NEIGHBOR_ADVERT:
   1693  1.11   itojun 		(void)printf("Neighbor Advertisement");
   1694   1.1   itojun 		break;
   1695   1.1   itojun 	case ND_REDIRECT:
   1696   1.1   itojun 	{
   1697   1.1   itojun 		struct nd_redirect *red = (struct nd_redirect *)icp;
   1698   1.1   itojun 
   1699   1.1   itojun 		(void)printf("Redirect\n");
   1700  1.11   itojun 		(void)printf("Destination: %s",
   1701   1.1   itojun 			     inet_ntop(AF_INET6, &red->nd_rd_dst,
   1702   1.1   itojun 				       ntop_buf, sizeof(ntop_buf)));
   1703  1.11   itojun 		(void)printf("New Target: %s",
   1704   1.1   itojun 			     inet_ntop(AF_INET6, &red->nd_rd_target,
   1705   1.1   itojun 				       ntop_buf, sizeof(ntop_buf)));
   1706   1.1   itojun 		break;
   1707   1.1   itojun 	}
   1708   1.1   itojun 	case ICMP6_NI_QUERY:
   1709  1.11   itojun 		(void)printf("Node Information Query");
   1710   1.1   itojun 		/* XXX ID + Seq + Data */
   1711   1.1   itojun 		break;
   1712   1.1   itojun 	case ICMP6_NI_REPLY:
   1713  1.11   itojun 		(void)printf("Node Information Reply");
   1714   1.1   itojun 		/* XXX ID + Seq + Data */
   1715   1.1   itojun 		break;
   1716   1.1   itojun 	default:
   1717  1.11   itojun 		(void)printf("Bad ICMP type: %d", icp->icmp6_type);
   1718   1.1   itojun 	}
   1719   1.1   itojun }
   1720   1.1   itojun 
   1721   1.1   itojun /*
   1722   1.1   itojun  * pr_iph --
   1723   1.1   itojun  *	Print an IP6 header.
   1724   1.1   itojun  */
   1725   1.1   itojun void
   1726   1.1   itojun pr_iph(ip6)
   1727   1.1   itojun 	struct ip6_hdr *ip6;
   1728   1.1   itojun {
   1729   1.1   itojun 	u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
   1730   1.1   itojun 	u_int8_t tc;
   1731  1.12   itojun 	char ntop_buf[INET6_ADDRSTRLEN];
   1732   1.1   itojun 
   1733   1.1   itojun 	tc = *(&ip6->ip6_vfc + 1); /* XXX */
   1734   1.1   itojun 	tc = (tc >> 4) & 0x0f;
   1735   1.1   itojun 	tc |= (ip6->ip6_vfc << 4);
   1736   1.1   itojun 
   1737   1.1   itojun 	printf("Vr TC  Flow Plen Nxt Hlim\n");
   1738   1.1   itojun 	printf(" %1x %02x %05x %04x  %02x   %02x\n",
   1739   1.1   itojun 	       (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (int)ntohl(flow),
   1740   1.1   itojun 	       ntohs(ip6->ip6_plen),
   1741   1.1   itojun 	       ip6->ip6_nxt, ip6->ip6_hlim);
   1742   1.1   itojun 	printf("%s->", inet_ntop(AF_INET6, &ip6->ip6_src,
   1743  1.12   itojun 				  ntop_buf, sizeof(ntop_buf)));
   1744   1.1   itojun 	printf("%s\n", inet_ntop(AF_INET6, &ip6->ip6_dst,
   1745  1.12   itojun 				 ntop_buf, sizeof(ntop_buf)));
   1746   1.1   itojun }
   1747   1.1   itojun 
   1748   1.1   itojun /*
   1749   1.1   itojun  * pr_addr --
   1750   1.1   itojun  *	Return an ascii host address as a dotted quad and optionally with
   1751   1.1   itojun  * a hostname.
   1752   1.1   itojun  */
   1753  1.12   itojun const char *
   1754   1.1   itojun pr_addr(addr)
   1755   1.9   itojun 	struct sockaddr_in6 *addr;
   1756   1.1   itojun {
   1757   1.1   itojun 	static char buf[MAXHOSTNAMELEN];
   1758   1.1   itojun 	int flag = 0;
   1759   1.1   itojun 
   1760  1.15   itojun 	if ((options & F_HOSTNAME) == 0)
   1761   1.1   itojun 		flag |= NI_NUMERICHOST;
   1762   1.9   itojun #ifdef KAME_SCOPEID
   1763   1.9   itojun 	flag |= NI_WITHSCOPEID;
   1764   1.9   itojun #endif
   1765   1.1   itojun 
   1766   1.9   itojun 	getnameinfo((struct sockaddr *)addr, addr->sin6_len, buf, sizeof(buf),
   1767   1.9   itojun 		NULL, 0, flag);
   1768   1.1   itojun 
   1769   1.1   itojun 	return (buf);
   1770   1.1   itojun }
   1771   1.1   itojun 
   1772   1.1   itojun /*
   1773   1.1   itojun  * pr_retip --
   1774   1.1   itojun  *	Dump some info on a returned (via ICMPv6) IPv6 packet.
   1775   1.1   itojun  */
   1776   1.1   itojun void
   1777   1.1   itojun pr_retip(ip6, end)
   1778   1.1   itojun 	struct ip6_hdr *ip6;
   1779   1.1   itojun 	u_char *end;
   1780   1.1   itojun {
   1781   1.1   itojun 	u_char *cp = (u_char *)ip6, nh;
   1782   1.1   itojun 	int hlen;
   1783   1.1   itojun 
   1784   1.1   itojun 	if (end - (u_char *)ip6 < sizeof(*ip6)) {
   1785   1.1   itojun 		printf("IP6");
   1786   1.1   itojun 		goto trunc;
   1787   1.1   itojun 	}
   1788   1.1   itojun 	pr_iph(ip6);
   1789   1.1   itojun 	hlen = sizeof(*ip6);
   1790   1.1   itojun 
   1791   1.1   itojun 	nh = ip6->ip6_nxt;
   1792   1.1   itojun 	cp += hlen;
   1793   1.9   itojun 	while (end - cp >= 8) {
   1794   1.9   itojun 		switch (nh) {
   1795   1.1   itojun 		 case IPPROTO_HOPOPTS:
   1796   1.1   itojun 			 printf("HBH ");
   1797   1.1   itojun 			 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
   1798   1.1   itojun 			 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
   1799   1.1   itojun 			 break;
   1800   1.1   itojun 		 case IPPROTO_DSTOPTS:
   1801   1.1   itojun 			 printf("DSTOPT ");
   1802   1.1   itojun 			 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
   1803   1.1   itojun 			 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
   1804   1.1   itojun 			 break;
   1805   1.1   itojun 		 case IPPROTO_FRAGMENT:
   1806   1.1   itojun 			 printf("FRAG ");
   1807   1.1   itojun 			 hlen = sizeof(struct ip6_frag);
   1808   1.1   itojun 			 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
   1809   1.1   itojun 			 break;
   1810   1.1   itojun 		 case IPPROTO_ROUTING:
   1811   1.1   itojun 			 printf("RTHDR ");
   1812   1.1   itojun 			 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
   1813   1.1   itojun 			 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
   1814   1.1   itojun 			 break;
   1815   1.9   itojun #ifdef IPSEC
   1816   1.1   itojun 		 case IPPROTO_AH:
   1817   1.1   itojun 			 printf("AH ");
   1818   1.1   itojun 			 hlen = (((struct ah *)cp)->ah_len+2) << 2;
   1819   1.1   itojun 			 nh = ((struct ah *)cp)->ah_nxt;
   1820   1.1   itojun 			 break;
   1821   1.9   itojun #endif
   1822   1.1   itojun 		 case IPPROTO_ICMPV6:
   1823   1.1   itojun 			 printf("ICMP6: type = %d, code = %d\n",
   1824   1.1   itojun 				*cp, *(cp + 1));
   1825   1.1   itojun 			 return;
   1826   1.1   itojun 		 case IPPROTO_ESP:
   1827   1.1   itojun 			 printf("ESP\n");
   1828   1.1   itojun 			 return;
   1829   1.1   itojun 		 case IPPROTO_TCP:
   1830   1.1   itojun 			 printf("TCP: from port %u, to port %u (decimal)\n",
   1831   1.1   itojun 				(*cp * 256 + *(cp + 1)),
   1832   1.1   itojun 				(*(cp + 2) * 256 + *(cp + 3)));
   1833   1.1   itojun 			 return;
   1834   1.1   itojun 		 case IPPROTO_UDP:
   1835   1.1   itojun 			 printf("UDP: from port %u, to port %u (decimal)\n",
   1836   1.1   itojun 				(*cp * 256 + *(cp + 1)),
   1837   1.1   itojun 				(*(cp + 2) * 256 + *(cp + 3)));
   1838   1.1   itojun 			 return;
   1839   1.1   itojun 		 default:
   1840   1.1   itojun 			 printf("Unknown Header(%d)\n", nh);
   1841   1.1   itojun 			 return;
   1842   1.1   itojun 		}
   1843   1.1   itojun 
   1844   1.1   itojun 		if ((cp += hlen) >= end)
   1845   1.1   itojun 			goto trunc;
   1846   1.1   itojun 	}
   1847   1.1   itojun 	if (end - cp < 8)
   1848   1.1   itojun 		goto trunc;
   1849   1.1   itojun 
   1850   1.1   itojun 	putchar('\n');
   1851   1.1   itojun 	return;
   1852   1.1   itojun 
   1853   1.1   itojun   trunc:
   1854   1.1   itojun 	printf("...\n");
   1855   1.1   itojun 	return;
   1856   1.1   itojun }
   1857   1.1   itojun 
   1858   1.1   itojun void
   1859   1.1   itojun fill(bp, patp)
   1860   1.1   itojun 	char *bp, *patp;
   1861   1.1   itojun {
   1862   1.1   itojun 	register int ii, jj, kk;
   1863   1.1   itojun 	int pat[16];
   1864   1.1   itojun 	char *cp;
   1865   1.1   itojun 
   1866   1.1   itojun 	for (cp = patp; *cp; cp++)
   1867   1.1   itojun 		if (!isxdigit(*cp))
   1868   1.1   itojun 			errx(1, "patterns must be specified as hex digits");
   1869   1.1   itojun 	ii = sscanf(patp,
   1870   1.1   itojun 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
   1871   1.1   itojun 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
   1872   1.1   itojun 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
   1873   1.1   itojun 	    &pat[13], &pat[14], &pat[15]);
   1874   1.1   itojun 
   1875   1.1   itojun /* xxx */
   1876   1.1   itojun 	if (ii > 0)
   1877   1.1   itojun 		for (kk = 0;
   1878   1.1   itojun 		    kk <= MAXDATALEN - (8 + sizeof(struct timeval) + ii);
   1879   1.1   itojun 		    kk += ii)
   1880   1.1   itojun 			for (jj = 0; jj < ii; ++jj)
   1881   1.1   itojun 				bp[jj + kk] = pat[jj];
   1882   1.1   itojun 	if (!(options & F_QUIET)) {
   1883   1.1   itojun 		(void)printf("PATTERN: 0x");
   1884   1.1   itojun 		for (jj = 0; jj < ii; ++jj)
   1885   1.1   itojun 			(void)printf("%02x", bp[jj] & 0xFF);
   1886   1.1   itojun 		(void)printf("\n");
   1887   1.1   itojun 	}
   1888   1.1   itojun }
   1889   1.1   itojun 
   1890  1.11   itojun #ifdef IPSEC
   1891  1.11   itojun #ifdef IPSEC_POLICY_IPSEC
   1892  1.11   itojun int
   1893  1.11   itojun setpolicy(so, policy)
   1894  1.11   itojun 	int so;
   1895  1.11   itojun 	char *policy;
   1896  1.11   itojun {
   1897  1.11   itojun 	char *buf;
   1898  1.11   itojun 
   1899  1.11   itojun 	if (policy == NULL)
   1900  1.11   itojun 		return 0;	/* ignore */
   1901  1.11   itojun 
   1902  1.11   itojun 	buf = ipsec_set_policy(policy, strlen(policy));
   1903  1.11   itojun 	if (buf == NULL)
   1904  1.16   itojun 		errx(1, "%s", ipsec_strerror());
   1905  1.11   itojun 	if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
   1906  1.11   itojun 			buf, ipsec_get_policylen(buf)) < 0)
   1907  1.11   itojun 		warnx("Unable to set IPSec policy");
   1908  1.11   itojun 	free(buf);
   1909  1.11   itojun 
   1910  1.11   itojun 	return 0;
   1911  1.11   itojun }
   1912  1.11   itojun #endif
   1913  1.11   itojun #endif
   1914  1.11   itojun 
   1915  1.15   itojun char *
   1916  1.15   itojun nigroup(name)
   1917  1.15   itojun 	char *name;
   1918  1.15   itojun {
   1919  1.15   itojun 	char *p;
   1920  1.15   itojun 	MD5_CTX ctxt;
   1921  1.15   itojun 	u_int8_t digest[16];
   1922  1.15   itojun 	char l;
   1923  1.15   itojun 	char hbuf[NI_MAXHOST];
   1924  1.15   itojun 	struct in6_addr in6;
   1925  1.15   itojun 
   1926  1.15   itojun 	p = name;
   1927  1.15   itojun 	while (p && *p && *p != '.')
   1928  1.15   itojun 		p++;
   1929  1.15   itojun 	if (p - name > 63)
   1930  1.15   itojun 		return NULL;	/*label too long*/
   1931  1.15   itojun 	l = p - name;
   1932  1.15   itojun 
   1933  1.15   itojun 	/* generate 8 bytes of pseudo-random value. */
   1934  1.15   itojun 	bzero(&ctxt, sizeof(ctxt));
   1935  1.15   itojun 	MD5Init(&ctxt);
   1936  1.15   itojun 	MD5Update(&ctxt, &l, sizeof(l));
   1937  1.15   itojun 	MD5Update(&ctxt, name, p - name);
   1938  1.15   itojun 	MD5Final(digest, &ctxt);
   1939  1.15   itojun 
   1940  1.15   itojun 	bzero(&in6, sizeof(in6));
   1941  1.15   itojun 	in6.s6_addr[0] = 0xff;
   1942  1.15   itojun 	in6.s6_addr[1] = 0x02;
   1943  1.15   itojun 	in6.s6_addr[11] = 0x02;
   1944  1.15   itojun 	bcopy(digest, &in6.s6_addr[12], 4);
   1945  1.15   itojun 
   1946  1.15   itojun 	if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL)
   1947  1.15   itojun 		return NULL;
   1948  1.15   itojun 
   1949  1.15   itojun 	return strdup(hbuf);
   1950  1.15   itojun }
   1951  1.15   itojun 
   1952   1.1   itojun void
   1953   1.1   itojun usage()
   1954   1.1   itojun {
   1955   1.1   itojun 	(void)fprintf(stderr,
   1956  1.15   itojun "usage: ping6 [-dfHnNqvwW"
   1957  1.15   itojun #ifdef IPV6_REACHCONF
   1958  1.15   itojun 		      "R"
   1959  1.15   itojun #endif
   1960   1.1   itojun #ifdef IPSEC
   1961   1.1   itojun #ifdef IPSEC_POLICY_IPSEC
   1962   1.1   itojun 		      "] [-P policy"
   1963   1.1   itojun #else
   1964   1.1   itojun 		      "AE"
   1965   1.1   itojun #endif
   1966  1.15   itojun #endif
   1967  1.13   itojun 		      "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n\
   1968  1.13   itojun              [-I interface] [-i wait] [-l preload] [-p pattern] [-S sourceaddr]\n\
   1969  1.11   itojun              [-s packetsize] [-h hoplimit] [hops...] host\n");
   1970   1.1   itojun 	exit(1);
   1971   1.1   itojun }
   1972