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