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