18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 88fae3551SRodney W. Grimes * Mike Muuss. 98fae3551SRodney W. Grimes * 108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 128fae3551SRodney W. Grimes * are met: 138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 208fae3551SRodney W. Grimes * without specific prior written permission. 218fae3551SRodney W. Grimes * 228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 328fae3551SRodney W. Grimes * SUCH DAMAGE. 338fae3551SRodney W. Grimes */ 348fae3551SRodney W. Grimes 35c69284caSDavid E. O'Brien #if 0 368fae3551SRodney W. Grimes #ifndef lint 3743470e3bSGarrett Wollman static const char copyright[] = 388fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 398fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408fae3551SRodney W. Grimes #endif /* not lint */ 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #ifndef lint 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 448fae3551SRodney W. Grimes #endif /* not lint */ 45c69284caSDavid E. O'Brien #endif 46c69284caSDavid E. O'Brien #include <sys/cdefs.h> 47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes /* 508fae3551SRodney W. Grimes * P I N G . C 518fae3551SRodney W. Grimes * 52e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 538fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 548fae3551SRodney W. Grimes * 558fae3551SRodney W. Grimes * Author - 568fae3551SRodney W. Grimes * Mike Muuss 578fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 588fae3551SRodney W. Grimes * December, 1983 598fae3551SRodney W. Grimes * 608fae3551SRodney W. Grimes * Status - 618fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 628fae3551SRodney W. Grimes * Bugs - 638fae3551SRodney W. Grimes * More statistics could always be gathered. 648fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 658fae3551SRodney W. Grimes */ 668fae3551SRodney W. Grimes 6743470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 68b881b8beSRobert Watson #include <sys/capsicum.h> 698fae3551SRodney W. Grimes #include <sys/socket.h> 70261e59bbSMaxim Konovalov #include <sys/sysctl.h> 718fae3551SRodney W. Grimes #include <sys/time.h> 72039d6aa4SBill Fenner #include <sys/uio.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #include <netinet/in.h> 7543470e3bSGarrett Wollman #include <netinet/in_systm.h> 768fae3551SRodney W. Grimes #include <netinet/ip.h> 778fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 788fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7943470e3bSGarrett Wollman #include <arpa/inet.h> 80c501d73cSMariusz Zaborski 81c501d73cSMariusz Zaborski #include <libcasper.h> 82c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 838fae3551SRodney W. Grimes 849a4365d0SYoshinobu Inoue #ifdef IPSEC 858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 879a4365d0SYoshinobu Inoue 887bdc3291SMark Johnston #include <capsicum_helpers.h> 89261e59bbSMaxim Konovalov #include <ctype.h> 90261e59bbSMaxim Konovalov #include <err.h> 91261e59bbSMaxim Konovalov #include <errno.h> 92261e59bbSMaxim Konovalov #include <math.h> 93261e59bbSMaxim Konovalov #include <netdb.h> 94d9cacf60SAlan Somers #include <stddef.h> 95261e59bbSMaxim Konovalov #include <signal.h> 96261e59bbSMaxim Konovalov #include <stdio.h> 97261e59bbSMaxim Konovalov #include <stdlib.h> 98261e59bbSMaxim Konovalov #include <string.h> 99261e59bbSMaxim Konovalov #include <sysexits.h> 1001ad76f1bSAlan Somers #include <time.h> 101261e59bbSMaxim Konovalov #include <unistd.h> 102261e59bbSMaxim Konovalov 103*3cde9171SAlan Somers #include "main.h" 104*3cde9171SAlan Somers #include "ping.h" 105ff77ab83SAlan Somers #include "utils.h" 106ff77ab83SAlan Somers 107301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10813e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 109eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 110eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 111c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1128f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1138f975bb3SBruce Evans /* runs out of buffer space */ 1144fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1154fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 116d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 117bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1180b2f8b3fSMaxim Konovalov #define MAXTOS 255 1198fae3551SRodney W. Grimes 1208fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1218fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1228fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1238fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1248fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1258fae3551SRodney W. Grimes 12613e3f0b7SMaxim Konovalov struct tv32 { 12713e3f0b7SMaxim Konovalov int32_t tv32_sec; 1281ad76f1bSAlan Somers int32_t tv32_nsec; 12913e3f0b7SMaxim Konovalov }; 13013e3f0b7SMaxim Konovalov 1318fae3551SRodney W. Grimes /* various options */ 1327e9489e0SHiroki Sato static int options; 13385456935SBill Fenner #define F_FLOOD 0x0001 13485456935SBill Fenner #define F_INTERVAL 0x0002 13585456935SBill Fenner #define F_NUMERIC 0x0004 13685456935SBill Fenner #define F_PINGFILLED 0x0008 13785456935SBill Fenner #define F_QUIET 0x0010 13885456935SBill Fenner #define F_RROUTE 0x0020 13985456935SBill Fenner #define F_SO_DEBUG 0x0040 14085456935SBill Fenner #define F_SO_DONTROUTE 0x0080 14185456935SBill Fenner #define F_VERBOSE 0x0100 14285456935SBill Fenner #define F_QUIET2 0x0200 14385456935SBill Fenner #define F_NOLOOP 0x0400 14485456935SBill Fenner #define F_MTTL 0x0800 14585456935SBill Fenner #define F_MIF 0x1000 146772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1479a4365d0SYoshinobu Inoue #ifdef IPSEC 1489a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1499a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1509a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1519a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 152211bfbd2SRuslan Ermilov #define F_TTL 0x8000 153ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1548025c44bSDima Dorfman #define F_ONCE 0x20000 1550b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 156143008a1SMatthew N. Dodd #define F_MASK 0x80000 157eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1589ff95228SGleb Smirnoff #define F_SWEEP 0x200000 159d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 16081a6f4c7SRichard Scheffenegger #define F_IP_VLAN_PCP 0x800000 1618fae3551SRodney W. Grimes 1628fae3551SRodney W. Grimes /* 1638fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1648fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1658fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1668fae3551SRodney W. Grimes */ 1678fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1687e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1697e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1708fae3551SRodney W. Grimes 1717e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1727e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1737e9489e0SHiroki Sato static int maxpayload; 1747e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1757e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1767e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1777e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1787e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1797e9489e0SHiroki Sato static char DOT = '.'; 1807e9489e0SHiroki Sato static char *hostname; 1817e9489e0SHiroki Sato static char *shostname; 1827e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1837e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1847e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1857e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1867e9489e0SHiroki Sato static int phdr_len = 0; 1877e9489e0SHiroki Sato static int send_len; 1888fae3551SRodney W. Grimes 1898fae3551SRodney W. Grimes /* counters */ 1907e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1917e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1927e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1937e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1947e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1957e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1967e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1977e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1987e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1997e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 2007e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 2017e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 2027e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 2038fae3551SRodney W. Grimes 2048fae3551SRodney W. Grimes /* timing */ 2057e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2067e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2077e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2087e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2097e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2108fae3551SRodney W. Grimes 2117e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2127e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2137e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 214badd8138SSean Eric Fagan 21549133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21649133c6dSPawel Jakub Dawidek 21743470e3bSGarrett Wollman static void fill(char *, char *); 21849133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21943470e3bSGarrett Wollman static void check_status(void); 2208f975bb3SBruce Evans static void finish(void) __dead2; 22143470e3bSGarrett Wollman static void pinger(void); 22243470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 223eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 224d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 22543470e3bSGarrett Wollman static void pr_iph(struct ip *); 226d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 227d9cacf60SAlan Somers static void pr_retip(struct ip *, const u_char *); 22843470e3bSGarrett Wollman static void status(int); 2298f975bb3SBruce Evans static void stopit(int); 2308fae3551SRodney W. Grimes 23143470e3bSGarrett Wollman int 232*3cde9171SAlan Somers ping(int argc, char *const *argv) 2338fae3551SRodney W. Grimes { 23431eac03bSSean Chittenden struct sockaddr_in from, sock_in; 235efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2361ad76f1bSAlan Somers struct timespec last, intvl; 237efc8588dSDavid E. O'Brien struct iovec iov; 238efc8588dSDavid E. O'Brien struct msghdr msg; 239efc8588dSDavid E. O'Brien struct sigaction si_sa; 2400b2f8b3fSMaxim Konovalov size_t sz; 241e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 24278e1f68eSMark Johnston const char *errstr; 243d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 244261e59bbSMaxim Konovalov struct hostent *hp; 245efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 246efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 247efc8588dSDavid E. O'Brien #endif 248261e59bbSMaxim Konovalov struct sockaddr_in *to; 249261e59bbSMaxim Konovalov double t; 250c0a3773aSEugene Grosbein u_long alarmtimeout; 25178e1f68eSMark Johnston long long ltmp; 25249133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 25381a6f4c7SRichard Scheffenegger int ssend_errno, srecv_errno, tos, ttl, pcp; 2541ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 255efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2568fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2574fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2588fae3551SRodney W. Grimes #endif 259261e59bbSMaxim Konovalov unsigned char loop, mttl; 260efc8588dSDavid E. O'Brien 26131eac03bSSean Chittenden payload = source = NULL; 2629a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 263efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2649a4365d0SYoshinobu Inoue #endif 26549133c6dSPawel Jakub Dawidek cap_rights_t rights; 2668fae3551SRodney W. Grimes 267299e2c58SAlan Somers options |= F_NUMERIC; 268299e2c58SAlan Somers 269f1284d7aSBill Fenner /* 270f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 271f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 272f1284d7aSBill Fenner * after arg parsing. 27349133c6dSPawel Jakub Dawidek * 27449133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27549133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27649133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27749133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27849133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27949133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 28049133c6dSPawel Jakub Dawidek * packets. 281f1284d7aSBill Fenner */ 28249133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28349133c6dSPawel Jakub Dawidek ssend_errno = errno; 28449133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28549133c6dSPawel Jakub Dawidek srecv_errno = errno; 286f1284d7aSBill Fenner 2871d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2881d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 289ee2bf734SWarner Losh uid = getuid(); 290f1284d7aSBill Fenner 291eeb63943SDon Lewis if (ssend < 0) { 292eeb63943SDon Lewis errno = ssend_errno; 293eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 294eeb63943SDon Lewis } 295eeb63943SDon Lewis 296eeb63943SDon Lewis if (srecv < 0) { 297eeb63943SDon Lewis errno = srecv_errno; 298eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 299eeb63943SDon Lewis } 300eeb63943SDon Lewis 30181a6f4c7SRichard Scheffenegger alarmtimeout = df = preload = tos = pcp = 0; 302badd8138SSean Eric Fagan 3030b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 304211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 305*3cde9171SAlan Somers "4AaC:c:DdfG:g:Hh:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 306211bfbd2SRuslan Ermilov #ifdef IPSEC 3079a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 308211bfbd2SRuslan Ermilov "P:" 3099a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 310211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 311211bfbd2SRuslan Ermilov )) != -1) 3129a4365d0SYoshinobu Inoue { 3138fae3551SRodney W. Grimes switch(ch) { 314*3cde9171SAlan Somers case '4': 315*3cde9171SAlan Somers /* This option is processed in main(). */ 316*3cde9171SAlan Somers break; 317ca517ad8SPoul-Henning Kamp case 'A': 318ca517ad8SPoul-Henning Kamp options |= F_MISSED; 319ca517ad8SPoul-Henning Kamp break; 320772dfa72SDaniel O'Callaghan case 'a': 321772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 322772dfa72SDaniel O'Callaghan break; 32381a6f4c7SRichard Scheffenegger case 'C': 32481a6f4c7SRichard Scheffenegger options |= F_IP_VLAN_PCP; 32578e1f68eSMark Johnston ltmp = strtonum(optarg, -1, 7, &errstr); 32678e1f68eSMark Johnston if (errstr != NULL) 32781a6f4c7SRichard Scheffenegger errx(EX_USAGE, "invalid PCP: `%s'", optarg); 32881a6f4c7SRichard Scheffenegger pcp = ltmp; 32981a6f4c7SRichard Scheffenegger break; 3308fae3551SRodney W. Grimes case 'c': 33178e1f68eSMark Johnston ltmp = strtonum(optarg, 1, LONG_MAX, &errstr); 33278e1f68eSMark Johnston if (errstr != NULL) 33343470e3bSGarrett Wollman errx(EX_USAGE, 33443470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 33543470e3bSGarrett Wollman optarg); 33678e1f68eSMark Johnston npackets = (long)ltmp; 3378fae3551SRodney W. Grimes break; 3380b2f8b3fSMaxim Konovalov case 'D': 3390b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3400b2f8b3fSMaxim Konovalov df = 1; 3410b2f8b3fSMaxim Konovalov break; 3428fae3551SRodney W. Grimes case 'd': 3438fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3448fae3551SRodney W. Grimes break; 3458fae3551SRodney W. Grimes case 'f': 346526f06b2SMatthew Dillon if (uid) { 34743470e3bSGarrett Wollman errno = EPERM; 34843470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3498fae3551SRodney W. Grimes } 3508fae3551SRodney W. Grimes options |= F_FLOOD; 3518fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3528fae3551SRodney W. Grimes break; 3539ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 35478e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 35578e1f68eSMark Johnston if (errstr != NULL) { 3569ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3579ff95228SGleb Smirnoff optarg); 35878e1f68eSMark Johnston } 35978e1f68eSMark Johnston sweepmax = (int)ltmp; 36078e1f68eSMark Johnston if (uid != 0 && sweepmax > DEFDATALEN) { 36178e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 36278e1f68eSMark Johnston "packet size too large: %d > %u", 36378e1f68eSMark Johnston sweepmax, DEFDATALEN); 3649ff95228SGleb Smirnoff } 3659ff95228SGleb Smirnoff options |= F_SWEEP; 3669ff95228SGleb Smirnoff break; 3679ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 36878e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 36978e1f68eSMark Johnston if (errstr != NULL) { 3709ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3719ff95228SGleb Smirnoff optarg); 37278e1f68eSMark Johnston } 37378e1f68eSMark Johnston sweepmin = (int)ltmp; 37478e1f68eSMark Johnston if (uid != 0 && sweepmin > DEFDATALEN) { 37578e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 37678e1f68eSMark Johnston "packet size too large: %d > %u", 37778e1f68eSMark Johnston sweepmin, DEFDATALEN); 3789ff95228SGleb Smirnoff } 3799ff95228SGleb Smirnoff options |= F_SWEEP; 3809ff95228SGleb Smirnoff break; 38199f13ae1SAlan Somers case 'H': 38299f13ae1SAlan Somers options &= ~F_NUMERIC; 38399f13ae1SAlan Somers break; 3849ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 38578e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 38678e1f68eSMark Johnston if (errstr != NULL) { 38778e1f68eSMark Johnston errx(EX_USAGE, "invalid packet size: `%s'", 3889ff95228SGleb Smirnoff optarg); 38978e1f68eSMark Johnston } 39078e1f68eSMark Johnston sweepincr = (int)ltmp; 39178e1f68eSMark Johnston if (uid != 0 && sweepincr > DEFDATALEN) { 39278e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 39378e1f68eSMark Johnston "packet size too large: %d > %u", 39478e1f68eSMark Johnston sweepincr, DEFDATALEN); 3959ff95228SGleb Smirnoff } 3969ff95228SGleb Smirnoff options |= F_SWEEP; 3979ff95228SGleb Smirnoff break; 3981f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3991f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 4001f6a4631SRuslan Ermilov errx(EX_USAGE, 4011f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 4021f6a4631SRuslan Ermilov optarg); 4031f6a4631SRuslan Ermilov options |= F_MIF; 4041f6a4631SRuslan Ermilov break; 4058fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 4061ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 4071ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 4081ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 4091ad0b1beSMaxim Konovalov optarg); 4108fae3551SRodney W. Grimes options |= F_INTERVAL; 411526f06b2SMatthew Dillon interval = (int)t; 412526f06b2SMatthew Dillon if (uid && interval < 1000) { 413526f06b2SMatthew Dillon errno = EPERM; 414526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 415526f06b2SMatthew Dillon } 4168fae3551SRodney W. Grimes break; 4171f6a4631SRuslan Ermilov case 'L': 4181f6a4631SRuslan Ermilov options |= F_NOLOOP; 4191f6a4631SRuslan Ermilov loop = 0; 42085456935SBill Fenner break; 4218fae3551SRodney W. Grimes case 'l': 42278e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 42378e1f68eSMark Johnston if (errstr != NULL) 42443470e3bSGarrett Wollman errx(EX_USAGE, 42543470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4265e2cc0f4SStephen McKay if (uid) { 427f78ac61bSWarner Losh errno = EPERM; 428f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 429f78ac61bSWarner Losh } 43078e1f68eSMark Johnston preload = (int)ltmp; 4318fae3551SRodney W. Grimes break; 4321f6a4631SRuslan Ermilov case 'M': 433eb1543c6SMatthew N. Dodd switch(optarg[0]) { 434eb1543c6SMatthew N. Dodd case 'M': 435eb1543c6SMatthew N. Dodd case 'm': 4361f6a4631SRuslan Ermilov options |= F_MASK; 43785456935SBill Fenner break; 438eb1543c6SMatthew N. Dodd case 'T': 439eb1543c6SMatthew N. Dodd case 't': 440eb1543c6SMatthew N. Dodd options |= F_TIME; 441eb1543c6SMatthew N. Dodd break; 442eb1543c6SMatthew N. Dodd default: 443eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 444eb1543c6SMatthew N. Dodd break; 445eb1543c6SMatthew N. Dodd } 446eb1543c6SMatthew N. Dodd break; 447211bfbd2SRuslan Ermilov case 'm': /* TTL */ 44878e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 44978e1f68eSMark Johnston if (errstr != NULL) 4501ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 45178e1f68eSMark Johnston ttl = (int)ltmp; 452211bfbd2SRuslan Ermilov options |= F_TTL; 453211bfbd2SRuslan Ermilov break; 4548fae3551SRodney W. Grimes case 'n': 4558fae3551SRodney W. Grimes options |= F_NUMERIC; 4568fae3551SRodney W. Grimes break; 4578025c44bSDima Dorfman case 'o': 4588025c44bSDima Dorfman options |= F_ONCE; 4598025c44bSDima Dorfman break; 4601f6a4631SRuslan Ermilov #ifdef IPSEC 4611f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4621f6a4631SRuslan Ermilov case 'P': 4631f6a4631SRuslan Ermilov options |= F_POLICY; 4641f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4651f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4661f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4671f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4681f6a4631SRuslan Ermilov else 4691f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4701f6a4631SRuslan Ermilov break; 4711f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4721f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4738fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4748fae3551SRodney W. Grimes options |= F_PINGFILLED; 475d074d39fSMatthew N. Dodd payload = optarg; 4768fae3551SRodney W. Grimes break; 477ef9e6dc7SBill Fenner case 'Q': 478ef9e6dc7SBill Fenner options |= F_QUIET2; 479ef9e6dc7SBill Fenner break; 4808fae3551SRodney W. Grimes case 'q': 4818fae3551SRodney W. Grimes options |= F_QUIET; 4828fae3551SRodney W. Grimes break; 4838fae3551SRodney W. Grimes case 'R': 4848fae3551SRodney W. Grimes options |= F_RROUTE; 4858fae3551SRodney W. Grimes break; 4868fae3551SRodney W. Grimes case 'r': 4878fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4888fae3551SRodney W. Grimes break; 4891f6a4631SRuslan Ermilov case 'S': 4901f6a4631SRuslan Ermilov source = optarg; 4911f6a4631SRuslan Ermilov break; 4928fae3551SRodney W. Grimes case 's': /* size of packet to send */ 49378e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 49478e1f68eSMark Johnston if (errstr != NULL) 49543470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 49643470e3bSGarrett Wollman optarg); 49778e1f68eSMark Johnston datalen = (int)ltmp; 49878e1f68eSMark Johnston if (uid != 0 && datalen > DEFDATALEN) { 499fb7d32c7SMaxim Konovalov errno = EPERM; 500fb7d32c7SMaxim Konovalov err(EX_NOPERM, 50178e1f68eSMark Johnston "packet size too large: %d > %u", 50278e1f68eSMark Johnston datalen, DEFDATALEN); 503fb7d32c7SMaxim Konovalov } 5048fae3551SRodney W. Grimes break; 5051f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 50678e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 50778e1f68eSMark Johnston if (errstr != NULL) 5081f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 5091f6a4631SRuslan Ermilov optarg); 51078e1f68eSMark Johnston mttl = (unsigned char)ltmp; 5111f6a4631SRuslan Ermilov options |= F_MTTL; 51299490edeSWarner Losh break; 5137237fd94SBill Fumerola case 't': 514bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 515bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 5167237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 5177237fd94SBill Fumerola optarg); 518bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 519bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 520bf113f1bSBill Fumerola optarg, MAXALARM); 5212eb6acc2SAlan Somers { 5222eb6acc2SAlan Somers struct itimerval itv; 5232eb6acc2SAlan Somers 5242eb6acc2SAlan Somers timerclear(&itv.it_interval); 5252eb6acc2SAlan Somers timerclear(&itv.it_value); 5262eb6acc2SAlan Somers itv.it_value.tv_sec = (time_t)alarmtimeout; 5272eb6acc2SAlan Somers if (setitimer(ITIMER_REAL, &itv, NULL) != 0) 5282eb6acc2SAlan Somers err(1, "setitimer"); 5292eb6acc2SAlan Somers } 5307237fd94SBill Fumerola break; 5318fae3551SRodney W. Grimes case 'v': 5328fae3551SRodney W. Grimes options |= F_VERBOSE; 5338fae3551SRodney W. Grimes break; 534d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 535d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 536d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 537d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 538d6cd1497SGleb Smirnoff optarg); 539d6cd1497SGleb Smirnoff options |= F_WAITTIME; 540d6cd1497SGleb Smirnoff waittime = (int)t; 541d6cd1497SGleb Smirnoff break; 5420b2f8b3fSMaxim Konovalov case 'z': 5430b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 544c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 545c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5460b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 547c0a3773aSEugene Grosbein tos = ltmp; 5480b2f8b3fSMaxim Konovalov break; 5498fae3551SRodney W. Grimes default: 550e345a80dSPhilippe Charnier usage(); 55143470e3bSGarrett Wollman } 55243470e3bSGarrett Wollman } 55343470e3bSGarrett Wollman 55443470e3bSGarrett Wollman if (argc - optind != 1) 555e345a80dSPhilippe Charnier usage(); 55643470e3bSGarrett Wollman target = argv[optind]; 5578fae3551SRodney W. Grimes 558d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 559d829c3dfSMatthew N. Dodd case 0: break; 560d829c3dfSMatthew N. Dodd case F_MASK: 561eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 562eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 563d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 564eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 565eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 566d829c3dfSMatthew N. Dodd break; 567d829c3dfSMatthew N. Dodd case F_TIME: 568eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 569eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 570d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 571eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 572eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 573d829c3dfSMatthew N. Dodd break; 574d829c3dfSMatthew N. Dodd default: 575d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 576d829c3dfSMatthew N. Dodd break; 577eb1543c6SMatthew N. Dodd } 5780fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 579fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 580e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 581e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 582fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5831104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 584fb7d32c7SMaxim Konovalov maxpayload); 585e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5860fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 587d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 588d074d39fSMatthew N. Dodd fill((char *)datap, payload); 589d074d39fSMatthew N. Dodd } 59049133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 59199490edeSWarner Losh if (source) { 59231eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 59331eac03bSSean Chittenden sock_in.sin_family = AF_INET; 59431eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 59599490edeSWarner Losh shostname = source; 59699490edeSWarner Losh } else { 597d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 59899490edeSWarner Losh if (!hp) 59999490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 60099490edeSWarner Losh source, hstrerror(h_errno)); 60199490edeSWarner Losh 60231eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 60331eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 6044fba6582SMaxim Konovalov hp->h_length < 0) 60599490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 60631eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 60731eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 60899490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 60999490edeSWarner Losh sizeof(snamebuf) - 1); 61099490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 61199490edeSWarner Losh shostname = snamebuf; 61299490edeSWarner Losh } 61349133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 61449133c6dSPawel Jakub Dawidek -1) 61599490edeSWarner Losh err(1, "bind"); 61699490edeSWarner Losh } 61799490edeSWarner Losh 618d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 619d389e86aSMatt Jacob to = &whereto; 6208fae3551SRodney W. Grimes to->sin_family = AF_INET; 621d389e86aSMatt Jacob to->sin_len = sizeof *to; 62243470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 6238fae3551SRodney W. Grimes hostname = target; 62443470e3bSGarrett Wollman } else { 62549133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 62643470e3bSGarrett Wollman if (!hp) 62743470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 62843470e3bSGarrett Wollman target, hstrerror(h_errno)); 62943470e3bSGarrett Wollman 63031eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6311ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 63243470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6338fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 634b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6358fae3551SRodney W. Grimes hostname = hnamebuf; 6368fae3551SRodney W. Grimes } 6378fae3551SRodney W. Grimes 63849133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 639a3ce7698SAlan Somers #ifdef WITH_CASPER 64049133c6dSPawel Jakub Dawidek if (capdns != NULL) { 64149133c6dSPawel Jakub Dawidek const char *types[1]; 64249133c6dSPawel Jakub Dawidek 643752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 64449133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 64549133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 64649133c6dSPawel Jakub Dawidek } 647a3ce7698SAlan Somers #endif 64849133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 64949133c6dSPawel Jakub Dawidek err(1, "connect"); 65049133c6dSPawel Jakub Dawidek 65143470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 65243470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 65343470e3bSGarrett Wollman 65443470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65543470e3bSGarrett Wollman errx(EX_USAGE, 65643470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 65743470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 65843470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65943470e3bSGarrett Wollman errx(EX_USAGE, 66043470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6618fae3551SRodney W. Grimes 662d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6638fae3551SRodney W. Grimes timing = 1; 66443470e3bSGarrett Wollman 66578e1f68eSMark Johnston if ((options & (F_PINGFILLED | F_SWEEP)) == 0) 666d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6678fae3551SRodney W. Grimes *datap++ = i; 6688fae3551SRodney W. Grimes 6698fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6708fae3551SRodney W. Grimes 6718fae3551SRodney W. Grimes hold = 1; 67249133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 67349133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6748fae3551SRodney W. Grimes sizeof(hold)); 67549133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 67649133c6dSPawel Jakub Dawidek sizeof(hold)); 67749133c6dSPawel Jakub Dawidek } 6788fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 67949133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6808fae3551SRodney W. Grimes sizeof(hold)); 68181a6f4c7SRichard Scheffenegger if (options & F_IP_VLAN_PCP) { 68281a6f4c7SRichard Scheffenegger (void)setsockopt(ssend, IPPROTO_IP, IP_VLAN_PCP, (char *)&pcp, 68381a6f4c7SRichard Scheffenegger sizeof(pcp)); 68481a6f4c7SRichard Scheffenegger } 6859a4365d0SYoshinobu Inoue #ifdef IPSEC 6869a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6879a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6889a4365d0SYoshinobu Inoue char *buf; 6899a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6909a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6919a4365d0SYoshinobu Inoue if (buf == NULL) 692ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 69349133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6949a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6951ad0b1beSMaxim Konovalov err(EX_CONFIG, 6961ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6979a4365d0SYoshinobu Inoue free(buf); 6989a4365d0SYoshinobu Inoue } 6999a4365d0SYoshinobu Inoue 7009a4365d0SYoshinobu Inoue if (policy_out != NULL) { 7019a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 7029a4365d0SYoshinobu Inoue if (buf == NULL) 703ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 70449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 7059a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 7061ad0b1beSMaxim Konovalov err(EX_CONFIG, 7071ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 7089a4365d0SYoshinobu Inoue free(buf); 7099a4365d0SYoshinobu Inoue } 7109a4365d0SYoshinobu Inoue } 7119a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 7129a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 7138fae3551SRodney W. Grimes 7140b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 715d9cacf60SAlan Somers struct ip ip; 716d9cacf60SAlan Somers 717d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 7180b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 7190b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 7200b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 7210b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 7220b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 7230b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 7240b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7250b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7260b2f8b3fSMaxim Konovalov } 72749133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 728d9cacf60SAlan Somers ip.ip_v = IPVERSION; 729d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 730d9cacf60SAlan Somers ip.ip_tos = tos; 731d9cacf60SAlan Somers ip.ip_id = 0; 732d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 733d9cacf60SAlan Somers ip.ip_ttl = ttl; 734d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 735d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 736d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 737d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7380b2f8b3fSMaxim Konovalov } 73949133c6dSPawel Jakub Dawidek 74049133c6dSPawel Jakub Dawidek /* 74149133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 74249133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 74349133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 74449133c6dSPawel Jakub Dawidek */ 7457bdc3291SMark Johnston caph_cache_catpages(); 74618fcfaa4SMark Johnston if (caph_enter_casper() < 0) 747301bc9f9SAlan Somers err(1, "caph_enter_casper"); 74849133c6dSPawel Jakub Dawidek 74949133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7507bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 75149133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 75249133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7537bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 75449133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 75549133c6dSPawel Jakub Dawidek 7568fae3551SRodney W. Grimes /* record route option */ 7578fae3551SRodney W. Grimes if (options & F_RROUTE) { 7588fae3551SRodney W. Grimes #ifdef IP_OPTIONS 759039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7608fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7618fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7628fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 763039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 76449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 76543470e3bSGarrett Wollman sizeof(rspace)) < 0) 76643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7678fae3551SRodney W. Grimes #else 76843470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 76943470e3bSGarrett Wollman "record route not available in this implementation"); 7708fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7718fae3551SRodney W. Grimes } 7728fae3551SRodney W. Grimes 773211bfbd2SRuslan Ermilov if (options & F_TTL) { 77449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 775211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 776211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 777211bfbd2SRuslan Ermilov } 778211bfbd2SRuslan Ermilov } 77985456935SBill Fenner if (options & F_NOLOOP) { 78049133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 78185456935SBill Fenner sizeof(loop)) < 0) { 78243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 78385456935SBill Fenner } 78485456935SBill Fenner } 78585456935SBill Fenner if (options & F_MTTL) { 78649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 787211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 78843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 78985456935SBill Fenner } 79085456935SBill Fenner } 79185456935SBill Fenner if (options & F_MIF) { 79249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 79385456935SBill Fenner sizeof(ifaddr)) < 0) { 79443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 79585456935SBill Fenner } 79685456935SBill Fenner } 797039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 79884633ef1SAlan Somers { 79984633ef1SAlan Somers int on = 1; 80084633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 80184633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 80284633ef1SAlan Somers sizeof(on)) < 0) 803039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 80484633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 80584633ef1SAlan Somers sizeof(ts_clock)) < 0) 80684633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 807039d6aa4SBill Fenner } 808039d6aa4SBill Fenner #endif 8099ff95228SGleb Smirnoff if (sweepmax) { 810bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 81178e1f68eSMark Johnston errx(EX_USAGE, 81278e1f68eSMark Johnston "Maximum packet size must be no less than the minimum packet size"); 81378e1f68eSMark Johnston 81478e1f68eSMark Johnston if (sweepmax > maxpayload - TIMEVAL_LEN) 81578e1f68eSMark Johnston errx(EX_USAGE, "Invalid sweep maximum"); 8169ff95228SGleb Smirnoff 8179ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 81878e1f68eSMark Johnston errx(EX_USAGE, 81978e1f68eSMark Johnston "Packet size and ping sweep are mutually exclusive"); 8209ff95228SGleb Smirnoff 8219ff95228SGleb Smirnoff if (npackets > 0) { 8229ff95228SGleb Smirnoff snpackets = npackets; 8239ff95228SGleb Smirnoff npackets = 0; 8249ff95228SGleb Smirnoff } else 8259ff95228SGleb Smirnoff snpackets = 1; 8269ff95228SGleb Smirnoff datalen = sweepmin; 8279ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 8289ff95228SGleb Smirnoff } 8299ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8309ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 83185456935SBill Fenner 8328fae3551SRodney W. Grimes /* 8338fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8348fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8358fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 83643470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 83743470e3bSGarrett Wollman * or multicast pings if they wish. 8388fae3551SRodney W. Grimes */ 8394fba6582SMaxim Konovalov 8404fba6582SMaxim Konovalov /* 8414fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8424fba6582SMaxim Konovalov * as well. 8434fba6582SMaxim Konovalov */ 8444fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 84549133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8468fae3551SRodney W. Grimes sizeof(hold)); 84749133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 84849133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8497bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 85049133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 851261e59bbSMaxim Konovalov if (uid == 0) 85249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 853e8bd25ceSRobert Watson sizeof(hold)); 85449133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8567bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 85749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 858e8bd25ceSRobert Watson 85999490edeSWarner Losh if (to->sin_family == AF_INET) { 86099490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 86199490edeSWarner Losh inet_ntoa(to->sin_addr)); 86299490edeSWarner Losh if (source) 86399490edeSWarner Losh (void)printf(" from %s", shostname); 8649ff95228SGleb Smirnoff if (sweepmax) 8659ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8669ff95228SGleb Smirnoff sweepmin, sweepmax); 8679ff95228SGleb Smirnoff else 86899490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8699ff95228SGleb Smirnoff 8709ff95228SGleb Smirnoff } else { 8719ff95228SGleb Smirnoff if (sweepmax) 8729ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8739ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8749ff95228SGleb Smirnoff else 8758fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8769ff95228SGleb Smirnoff } 8778fae3551SRodney W. Grimes 8787d81b35cSBruce Evans /* 879a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 880a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8817d81b35cSBruce Evans */ 882a2a00888SSean Eric Fagan 883f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 88437e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 885a2a00888SSean Eric Fagan 886a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 887a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 888a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 889a2a00888SSean Eric Fagan } 890a2a00888SSean Eric Fagan 891a2a00888SSean Eric Fagan si_sa.sa_handler = status; 89237e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 893624ff938SWarner Losh err(EX_OSERR, "sigaction"); 89437e5b2c6SSean Eric Fagan } 895bf113f1bSBill Fumerola 896bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8977237fd94SBill Fumerola si_sa.sa_handler = stopit; 8987237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8997237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 900bf113f1bSBill Fumerola } 90137e5b2c6SSean Eric Fagan 902039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 903039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 904039d6aa4SBill Fenner msg.msg_iov = &iov; 905039d6aa4SBill Fenner msg.msg_iovlen = 1; 906039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 907039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 90867511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 909039d6aa4SBill Fenner #endif 910039d6aa4SBill Fenner iov.iov_base = packet; 911e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 912039d6aa4SBill Fenner 91332af342fSRuslan Ermilov if (preload == 0) 91432af342fSRuslan Ermilov pinger(); /* send the first ping */ 91532af342fSRuslan Ermilov else { 91632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 91732af342fSRuslan Ermilov preload = npackets; 9188fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 9198fae3551SRodney W. Grimes pinger(); 92032af342fSRuslan Ermilov } 9211ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 9228fae3551SRodney W. Grimes 923039d6aa4SBill Fenner if (options & F_FLOOD) { 924039d6aa4SBill Fenner intvl.tv_sec = 0; 9251ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 926039d6aa4SBill Fenner } else { 927526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 9281ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 929039d6aa4SBill Fenner } 930039d6aa4SBill Fenner 931261e59bbSMaxim Konovalov almost_done = 0; 9328f975bb3SBruce Evans while (!finish_up) { 9331ad76f1bSAlan Somers struct timespec now, timeout; 934039d6aa4SBill Fenner fd_set rfds; 935d9cacf60SAlan Somers int n; 936d9cacf60SAlan Somers ssize_t cc; 9378fae3551SRodney W. Grimes 9387d81b35cSBruce Evans check_status(); 93949133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9407e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 941039d6aa4SBill Fenner FD_ZERO(&rfds); 94249133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9431ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9441ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9451ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 946039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9471ad76f1bSAlan Somers timespecclear(&timeout); 9481ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9495e2cc0f4SStephen McKay if (n < 0) 9505e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 951039d6aa4SBill Fenner if (n == 1) { 9521ad76f1bSAlan Somers struct timespec *tv = NULL; 953039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 95467511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 955039d6aa4SBill Fenner #endif 956039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 95749133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9588fae3551SRodney W. Grimes if (errno == EINTR) 9598fae3551SRodney W. Grimes continue; 960e345a80dSPhilippe Charnier warn("recvmsg"); 9618fae3551SRodney W. Grimes continue; 9628fae3551SRodney W. Grimes } 963039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 964b17fb992SAlan Somers if (cmsg != NULL && 965b17fb992SAlan Somers cmsg->cmsg_level == SOL_SOCKET && 966039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 96731eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 968fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 969fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 97031eac03bSSean Chittenden tv = &now; 971fa05a94cSJohn Birrell } 972039d6aa4SBill Fenner #endif 97331eac03bSSean Chittenden if (tv == NULL) { 9741ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 97531eac03bSSean Chittenden tv = &now; 976039d6aa4SBill Fenner } 97731eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 97831eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 97931eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9808fae3551SRodney W. Grimes break; 9818fae3551SRodney W. Grimes } 9825e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9839ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 98478e1f68eSMark Johnston if (datalen + sweepincr > sweepmax) 98578e1f68eSMark Johnston break; 98678e1f68eSMark Johnston for (i = 0; i < sweepincr; i++) 9879ff95228SGleb Smirnoff *datap++ = i; 9889ff95228SGleb Smirnoff datalen += sweepincr; 9899ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9909ff95228SGleb Smirnoff sntransmitted = 0; 9919ff95228SGleb Smirnoff } 992039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 993039d6aa4SBill Fenner pinger(); 994039d6aa4SBill Fenner else { 995039d6aa4SBill Fenner if (almost_done) 996039d6aa4SBill Fenner break; 997039d6aa4SBill Fenner almost_done = 1; 9981ad76f1bSAlan Somers intvl.tv_nsec = 0; 999039d6aa4SBill Fenner if (nreceived) { 1000039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 1001039d6aa4SBill Fenner if (!intvl.tv_sec) 1002039d6aa4SBill Fenner intvl.tv_sec = 1; 1003d6cd1497SGleb Smirnoff } else { 1004d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 10051ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 1006d6cd1497SGleb Smirnoff } 1007039d6aa4SBill Fenner } 10081ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 100925107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 101025107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 101125107197SIan Dowse if (options & F_MISSED) 1012ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1013039d6aa4SBill Fenner } 1014039d6aa4SBill Fenner } 101525107197SIan Dowse } 10168f975bb3SBruce Evans finish(); 10178fae3551SRodney W. Grimes /* NOTREACHED */ 1018f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 10198fae3551SRodney W. Grimes } 10208fae3551SRodney W. Grimes 10218fae3551SRodney W. Grimes /* 10228f975bb3SBruce Evans * stopit -- 10238f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 10248f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 10258f975bb3SBruce Evans * to be called from a signal handler. 1026515dd2faSJulian Elischer */ 1027515dd2faSJulian Elischer void 1028fafb8f11SEd Schouten stopit(int sig __unused) 1029515dd2faSJulian Elischer { 1030efc8588dSDavid E. O'Brien 1031c8bb99e5SIan Dowse /* 1032c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1033c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1034c8bb99e5SIan Dowse */ 1035c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1036c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1037515dd2faSJulian Elischer finish_up = 1; 1038515dd2faSJulian Elischer } 1039515dd2faSJulian Elischer 1040515dd2faSJulian Elischer /* 10418fae3551SRodney W. Grimes * pinger -- 10428fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10438fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1044eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10451ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10464fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10478fae3551SRodney W. Grimes */ 104843470e3bSGarrett Wollman static void 104943470e3bSGarrett Wollman pinger(void) 10508fae3551SRodney W. Grimes { 10511ad76f1bSAlan Somers struct timespec now; 105213e3f0b7SMaxim Konovalov struct tv32 tv32; 1053d9cacf60SAlan Somers struct icmp icp; 1054efc8588dSDavid E. O'Brien int cc, i; 10550b2f8b3fSMaxim Konovalov u_char *packet; 10568fae3551SRodney W. Grimes 10570b2f8b3fSMaxim Konovalov packet = outpack; 1058d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1059d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1060d9cacf60SAlan Somers icp.icmp_code = 0; 1061d9cacf60SAlan Somers icp.icmp_cksum = 0; 1062d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1063d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10648fae3551SRodney W. Grimes 10655db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10668fae3551SRodney W. Grimes 1067eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10681ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10691ad76f1bSAlan Somers /* 10701ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10711ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10721ad76f1bSAlan Somers * packet. We're only concerned with 10731ad76f1bSAlan Somers * durations, not absolute times. 10741ad76f1bSAlan Somers */ 10751ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10761ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1077eb1543c6SMatthew N. Dodd if (options & F_TIME) 1078d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10791ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1080eb1543c6SMatthew N. Dodd if (timing) 108113e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10820fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 108313e3f0b7SMaxim Konovalov sizeof(tv32)); 1084eb1543c6SMatthew N. Dodd } 1085eb1543c6SMatthew N. Dodd 1086d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1087d9cacf60SAlan Somers 10880fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10898fae3551SRodney W. Grimes 10908fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1091d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1092d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1093d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1094d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 10958fae3551SRodney W. Grimes 10960b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1097d9cacf60SAlan Somers struct ip ip; 1098d9cacf60SAlan Somers 10990b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1100d9cacf60SAlan Somers ip.ip_len = htons(cc); 1101d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1102d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1103d9cacf60SAlan Somers sizeof(ip.ip_len)); 1104d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1105d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1106d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1107d9cacf60SAlan Somers sizeof(ip.ip_sum)); 11080b2f8b3fSMaxim Konovalov packet = outpackhdr; 11090b2f8b3fSMaxim Konovalov } 111049133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 11118fae3551SRodney W. Grimes if (i < 0 || i != cc) { 111243470e3bSGarrett Wollman if (i < 0) { 11138f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1114515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1115515dd2faSJulian Elischer return; 1116515dd2faSJulian Elischer } 111743470e3bSGarrett Wollman warn("sendto"); 111843470e3bSGarrett Wollman } else { 111943470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1120416aa49bSPoul-Henning Kamp hostname, i, cc); 11218fae3551SRodney W. Grimes } 1122363d7bbeSJulian Elischer } 1123363d7bbeSJulian Elischer ntransmitted++; 11249ff95228SGleb Smirnoff sntransmitted++; 11258fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 11268fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 11278fae3551SRodney W. Grimes } 11288fae3551SRodney W. Grimes 11298fae3551SRodney W. Grimes /* 11308fae3551SRodney W. Grimes * pr_pack -- 11318fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 11328fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11338fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11348fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11358fae3551SRodney W. Grimes */ 113643470e3bSGarrett Wollman static void 1137d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11388fae3551SRodney W. Grimes { 11399b219646SPeter Wemm struct in_addr ina; 1140d9cacf60SAlan Somers u_char *cp, *dp, l; 1141d9cacf60SAlan Somers struct icmp icp; 1142d9cacf60SAlan Somers struct ip ip; 1143d9cacf60SAlan Somers const u_char *icmp_data_raw; 11448f975bb3SBruce Evans double triptime; 11452c29d74cSAlan Somers int dupflag, hlen, i, j, recv_len; 11462c29d74cSAlan Somers uint16_t seq; 1147efc8588dSDavid E. O'Brien static int old_rrlen; 1148efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1149d9cacf60SAlan Somers struct ip oip; 1150d9cacf60SAlan Somers u_char oip_header_len; 1151d9cacf60SAlan Somers struct icmp oicmp; 1152d9cacf60SAlan Somers const u_char *oicmp_raw; 1153d9cacf60SAlan Somers 1154d9cacf60SAlan Somers /* 1155d9cacf60SAlan Somers * Get size of IP header of the received packet. The 1156d9cacf60SAlan Somers * information is contained in the lower four bits of the 1157d9cacf60SAlan Somers * first byte. 1158d9cacf60SAlan Somers */ 1159d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1160d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 1161d9cacf60SAlan Somers memcpy(&ip, buf, hlen); 11628fae3551SRodney W. Grimes 11638fae3551SRodney W. Grimes /* Check the IP header */ 1164e88178ddSMaxim Konovalov recv_len = cc; 11658fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11668fae3551SRodney W. Grimes if (options & F_VERBOSE) 1167d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 116843470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11698fae3551SRodney W. Grimes return; 11708fae3551SRodney W. Grimes } 11718fae3551SRodney W. Grimes 1172d9cacf60SAlan Somers #ifndef icmp_data 1173d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_ip); 1174d9cacf60SAlan Somers #else 1175d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1176d9cacf60SAlan Somers #endif 1177d9cacf60SAlan Somers 11788fae3551SRodney W. Grimes /* Now the ICMP part */ 11798fae3551SRodney W. Grimes cc -= hlen; 1180d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1181d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1182d9cacf60SAlan Somers if (icp.icmp_id != ident) 11838fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11848fae3551SRodney W. Grimes ++nreceived; 11858f975bb3SBruce Evans triptime = 0.0; 11868fae3551SRodney W. Grimes if (timing) { 11871ad76f1bSAlan Somers struct timespec tv1; 118813e3f0b7SMaxim Konovalov struct tv32 tv32; 1189d9cacf60SAlan Somers const u_char *tp; 1190d9cacf60SAlan Somers 1191d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1192143008a1SMatthew N. Dodd 11937e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11947e9489e0SHiroki Sato sizeof(tv1)) { 11959d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 119613e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 119713e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 11981ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 11991ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1200039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 12011ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 12028fae3551SRodney W. Grimes tsum += triptime; 12033109a910SGarrett Wollman tsumsq += triptime * triptime; 12048fae3551SRodney W. Grimes if (triptime < tmin) 12058fae3551SRodney W. Grimes tmin = triptime; 12068fae3551SRodney W. Grimes if (triptime > tmax) 12078fae3551SRodney W. Grimes tmax = triptime; 120847e9b3eaSMatthew N. Dodd } else 120947e9b3eaSMatthew N. Dodd timing = 0; 12108fae3551SRodney W. Grimes } 12118fae3551SRodney W. Grimes 1212d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 12135db89bc7SBill Fenner 12145db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 12158fae3551SRodney W. Grimes ++nrepeats; 12168fae3551SRodney W. Grimes --nreceived; 12178fae3551SRodney W. Grimes dupflag = 1; 12188fae3551SRodney W. Grimes } else { 12195db89bc7SBill Fenner SET(seq % mx_dup_ck); 12208fae3551SRodney W. Grimes dupflag = 0; 12218fae3551SRodney W. Grimes } 12228fae3551SRodney W. Grimes 12238fae3551SRodney W. Grimes if (options & F_QUIET) 12248fae3551SRodney W. Grimes return; 12258fae3551SRodney W. Grimes 1226d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1227d6cd1497SGleb Smirnoff ++nrcvtimeout; 1228d6cd1497SGleb Smirnoff return; 1229d6cd1497SGleb Smirnoff } 1230d6cd1497SGleb Smirnoff 12318fae3551SRodney W. Grimes if (options & F_FLOOD) 12328fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12338fae3551SRodney W. Grimes else { 1234d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1235229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1236d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12378fae3551SRodney W. Grimes if (timing) 1238d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12398fae3551SRodney W. Grimes if (dupflag) 12408fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1241772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1242ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1243143008a1SMatthew N. Dodd if (options & F_MASK) { 1244143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1245143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1246d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1247143008a1SMatthew N. Dodd } 1248eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1249d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1250d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1251d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1252eb1543c6SMatthew N. Dodd } 1253e88178ddSMaxim Konovalov if (recv_len != send_len) { 1254e88178ddSMaxim Konovalov (void)printf( 1255e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1256e88178ddSMaxim Konovalov recv_len, send_len); 1257e88178ddSMaxim Konovalov } 12588fae3551SRodney W. Grimes /* check the data */ 1259d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1260d9cacf60SAlan Somers icmp_data) + phdr_len); 12610fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 126247e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 126329dccd6aSMaxim Konovalov i = 0; 126429dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 126529dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 126629dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 126729dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 126829dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 126929dccd6aSMaxim Konovalov } 127029dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12718fae3551SRodney W. Grimes if (*cp != *dp) { 12728fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12738fae3551SRodney W. Grimes i, *dp, *cp); 12741ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1275d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1276d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1277d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1278e88178ddSMaxim Konovalov if ((i % 16) == 8) 1279d32ff037SJohn Birrell (void)printf("\n\t"); 1280e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1281d32ff037SJohn Birrell } 12821ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12830fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1284d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1285e88178ddSMaxim Konovalov if ((i % 16) == 8) 12868fae3551SRodney W. Grimes (void)printf("\n\t"); 1287e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12888fae3551SRodney W. Grimes } 12898fae3551SRodney W. Grimes break; 12908fae3551SRodney W. Grimes } 12918fae3551SRodney W. Grimes } 12928fae3551SRodney W. Grimes } 12938fae3551SRodney W. Grimes } else { 1294ef9e6dc7SBill Fenner /* 1295ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1296ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1297ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1298ef9e6dc7SBill Fenner * and ICMP type and ID. 1299f78ac61bSWarner Losh * 1300f78ac61bSWarner Losh * Only print all the error messages if we are running 1301f78ac61bSWarner Losh * as root to avoid leaking information not normally 1302f78ac61bSWarner Losh * available to those not running as root. 1303ef9e6dc7SBill Fenner */ 1304d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1305d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 1306d9cacf60SAlan Somers memcpy(&oip, icmp_data_raw, oip_header_len); 1307d9cacf60SAlan Somers oicmp_raw = icmp_data_raw + oip_header_len; 1308d9cacf60SAlan Somers memcpy(&oicmp, oicmp_raw, offsetof(struct icmp, icmp_id) + 1309d9cacf60SAlan Somers sizeof(oicmp.icmp_id)); 1310ef9e6dc7SBill Fenner 1311ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1312ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1313d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1314d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1315d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1316d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1317d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 131843470e3bSGarrett Wollman pr_addr(from->sin_addr)); 1319d9cacf60SAlan Somers pr_icmph(&icp, &oip, oicmp_raw); 1320ef9e6dc7SBill Fenner } else 1321ef9e6dc7SBill Fenner return; 13228fae3551SRodney W. Grimes } 13238fae3551SRodney W. Grimes 13248fae3551SRodney W. Grimes /* Display any IP options */ 13258fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 13268fae3551SRodney W. Grimes 13278fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 13288fae3551SRodney W. Grimes switch (*cp) { 13298fae3551SRodney W. Grimes case IPOPT_EOL: 13308fae3551SRodney W. Grimes hlen = 0; 13318fae3551SRodney W. Grimes break; 13328fae3551SRodney W. Grimes case IPOPT_LSRR: 1333cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1334cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1335cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1336301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13378fae3551SRodney W. Grimes hlen -= 2; 1338301207dfSMaxim Konovalov cp += 2; 13393c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13403c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13418fae3551SRodney W. Grimes for (;;) { 1342301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1343301207dfSMaxim Konovalov if (ina.s_addr == 0) 13441ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1345301207dfSMaxim Konovalov else 13461ad0b1beSMaxim Konovalov (void)printf("\t%s", 13471ad0b1beSMaxim Konovalov pr_addr(ina)); 1348301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1349301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1350301207dfSMaxim Konovalov j -= INADDR_LEN; 1351301207dfSMaxim Konovalov if (j < INADDR_LEN) 13528fae3551SRodney W. Grimes break; 13538fae3551SRodney W. Grimes (void)putchar('\n'); 13548fae3551SRodney W. Grimes } 1355301207dfSMaxim Konovalov } else 1356301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 13578fae3551SRodney W. Grimes break; 13588fae3551SRodney W. Grimes case IPOPT_RR: 1359301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1360301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 13618fae3551SRodney W. Grimes hlen -= 2; 1362301207dfSMaxim Konovalov cp += 2; 13638fae3551SRodney W. Grimes if (i > j) 13648fae3551SRodney W. Grimes i = j; 1365301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1366301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1367301207dfSMaxim Konovalov old_rrlen = 0; 13688fae3551SRodney W. Grimes continue; 1369301207dfSMaxim Konovalov } 13708fae3551SRodney W. Grimes if (i == old_rrlen 13718fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13728fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13738fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13748fae3551SRodney W. Grimes hlen -= i; 13758fae3551SRodney W. Grimes cp += i; 13768fae3551SRodney W. Grimes break; 13778fae3551SRodney W. Grimes } 13788fae3551SRodney W. Grimes old_rrlen = i; 13798fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13808fae3551SRodney W. Grimes (void)printf("\nRR: "); 1381301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1382301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13838fae3551SRodney W. Grimes for (;;) { 1384301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1385301207dfSMaxim Konovalov if (ina.s_addr == 0) 13861ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1387301207dfSMaxim Konovalov else 1388301207dfSMaxim Konovalov (void)printf("\t%s", 1389301207dfSMaxim Konovalov pr_addr(ina)); 1390301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1391301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1392301207dfSMaxim Konovalov i -= INADDR_LEN; 1393301207dfSMaxim Konovalov if (i < INADDR_LEN) 13948fae3551SRodney W. Grimes break; 13958fae3551SRodney W. Grimes (void)putchar('\n'); 13968fae3551SRodney W. Grimes } 1397301207dfSMaxim Konovalov } else 1398301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13998fae3551SRodney W. Grimes break; 14008fae3551SRodney W. Grimes case IPOPT_NOP: 14018fae3551SRodney W. Grimes (void)printf("\nNOP"); 14028fae3551SRodney W. Grimes break; 14038fae3551SRodney W. Grimes default: 14048fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 14058fae3551SRodney W. Grimes break; 14068fae3551SRodney W. Grimes } 14078fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 14088fae3551SRodney W. Grimes (void)putchar('\n'); 14098fae3551SRodney W. Grimes (void)fflush(stdout); 14108fae3551SRodney W. Grimes } 14118fae3551SRodney W. Grimes } 14128fae3551SRodney W. Grimes 14138fae3551SRodney W. Grimes /* 1414badd8138SSean Eric Fagan * status -- 1415badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1416badd8138SSean Eric Fagan */ 1417badd8138SSean Eric Fagan 141843470e3bSGarrett Wollman static void 1419fafb8f11SEd Schouten status(int sig __unused) 14207d81b35cSBruce Evans { 1421efc8588dSDavid E. O'Brien 142237e5b2c6SSean Eric Fagan siginfo_p = 1; 142337e5b2c6SSean Eric Fagan } 142437e5b2c6SSean Eric Fagan 142543470e3bSGarrett Wollman static void 1426fafb8f11SEd Schouten check_status(void) 1427badd8138SSean Eric Fagan { 1428efc8588dSDavid E. O'Brien 142937e5b2c6SSean Eric Fagan if (siginfo_p) { 143037e5b2c6SSean Eric Fagan siginfo_p = 0; 1431aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14327d81b35cSBruce Evans nreceived, ntransmitted, 1433aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1434aed98a27SMaxim Konovalov if (nreceived && timing) 1435aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1436aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1437aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 143837e5b2c6SSean Eric Fagan } 1439badd8138SSean Eric Fagan } 1440badd8138SSean Eric Fagan 1441badd8138SSean Eric Fagan /* 14428fae3551SRodney W. Grimes * finish -- 14438fae3551SRodney W. Grimes * Print out statistics, and give up. 14448fae3551SRodney W. Grimes */ 144543470e3bSGarrett Wollman static void 1446fafb8f11SEd Schouten finish(void) 14478fae3551SRodney W. Grimes { 14488fae3551SRodney W. Grimes 14498fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1450a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14518fae3551SRodney W. Grimes (void)putchar('\n'); 14528fae3551SRodney W. Grimes (void)fflush(stdout); 14538fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 14548fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 14558fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 14568fae3551SRodney W. Grimes if (nrepeats) 14578fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1458ebe70c8fSWarner Losh if (ntransmitted) { 14598fae3551SRodney W. Grimes if (nreceived > ntransmitted) 14608fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 14618fae3551SRodney W. Grimes else 1462aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1463aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1464aa822c39SDima Dorfman ntransmitted); 1465ebe70c8fSWarner Losh } 1466d6cd1497SGleb Smirnoff if (nrcvtimeout) 1467d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14688fae3551SRodney W. Grimes (void)putchar('\n'); 14693109a910SGarrett Wollman if (nreceived && timing) { 14703109a910SGarrett Wollman double n = nreceived + nrepeats; 14713109a910SGarrett Wollman double avg = tsum / n; 14723109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14731ad0b1beSMaxim Konovalov (void)printf( 14741ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14753109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14763109a910SGarrett Wollman } 1477badd8138SSean Eric Fagan 14786e1173dcSDavid Greenman if (nreceived) 14798fae3551SRodney W. Grimes exit(0); 14806e1173dcSDavid Greenman else 14816e1173dcSDavid Greenman exit(2); 14828fae3551SRodney W. Grimes } 14838fae3551SRodney W. Grimes 14848fae3551SRodney W. Grimes #ifdef notdef 14858fae3551SRodney W. Grimes static char *ttab[] = { 14868fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14878fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14888fae3551SRodney W. Grimes "Source Quench", /* IP */ 14898fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14908fae3551SRodney W. Grimes "Echo", 14918fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14928fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14938fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14948fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14958fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14968fae3551SRodney W. Grimes "Info Reply" /* " */ 14978fae3551SRodney W. Grimes }; 14988fae3551SRodney W. Grimes #endif 14998fae3551SRodney W. Grimes 15008fae3551SRodney W. Grimes /* 15018fae3551SRodney W. Grimes * pr_icmph -- 15028fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 15038fae3551SRodney W. Grimes */ 150443470e3bSGarrett Wollman static void 1505d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 15068fae3551SRodney W. Grimes { 1507efc8588dSDavid E. O'Brien 15088fae3551SRodney W. Grimes switch(icp->icmp_type) { 15098fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15108fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15118fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15128fae3551SRodney W. Grimes break; 15138fae3551SRodney W. Grimes case ICMP_UNREACH: 15148fae3551SRodney W. Grimes switch(icp->icmp_code) { 15158fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15168fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15178fae3551SRodney W. Grimes break; 15188fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15198fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15208fae3551SRodney W. Grimes break; 15218fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15228fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15238fae3551SRodney W. Grimes break; 15248fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15258fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15268fae3551SRodney W. Grimes break; 15278fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1528ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1529ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15308fae3551SRodney W. Grimes break; 15318fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15328fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15338fae3551SRodney W. Grimes break; 1534ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1535ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1536ef9e6dc7SBill Fenner break; 15378fae3551SRodney W. Grimes default: 15388fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15398fae3551SRodney W. Grimes icp->icmp_code); 15408fae3551SRodney W. Grimes break; 15418fae3551SRodney W. Grimes } 15428fae3551SRodney W. Grimes /* Print returned IP header information */ 1543d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15448fae3551SRodney W. Grimes break; 15458fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15468fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 1547d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15488fae3551SRodney W. Grimes break; 15498fae3551SRodney W. Grimes case ICMP_REDIRECT: 15508fae3551SRodney W. Grimes switch(icp->icmp_code) { 15518fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15528fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15538fae3551SRodney W. Grimes break; 15548fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15558fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15568fae3551SRodney W. Grimes break; 15578fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15588fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15598fae3551SRodney W. Grimes break; 15608fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15618fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15628fae3551SRodney W. Grimes break; 15638fae3551SRodney W. Grimes default: 15648fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15658fae3551SRodney W. Grimes break; 15668fae3551SRodney W. Grimes } 1567ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 1568d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15698fae3551SRodney W. Grimes break; 15708fae3551SRodney W. Grimes case ICMP_ECHO: 15718fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15728fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15738fae3551SRodney W. Grimes break; 15748fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15758fae3551SRodney W. Grimes switch(icp->icmp_code) { 15768fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15778fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15788fae3551SRodney W. Grimes break; 15798fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15808fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15818fae3551SRodney W. Grimes break; 15828fae3551SRodney W. Grimes default: 15838fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15848fae3551SRodney W. Grimes icp->icmp_code); 15858fae3551SRodney W. Grimes break; 15868fae3551SRodney W. Grimes } 1587d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15888fae3551SRodney W. Grimes break; 15898fae3551SRodney W. Grimes case ICMP_PARAMPROB: 15908fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 15918fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 1592d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15938fae3551SRodney W. Grimes break; 15948fae3551SRodney W. Grimes case ICMP_TSTAMP: 15958fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 15968fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15978fae3551SRodney W. Grimes break; 15988fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 15998fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16008fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16018fae3551SRodney W. Grimes break; 16028fae3551SRodney W. Grimes case ICMP_IREQ: 16038fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16048fae3551SRodney W. Grimes /* XXX ID + Seq */ 16058fae3551SRodney W. Grimes break; 16068fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16078fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16088fae3551SRodney W. Grimes /* XXX ID + Seq */ 16098fae3551SRodney W. Grimes break; 16108fae3551SRodney W. Grimes case ICMP_MASKREQ: 16118fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16128fae3551SRodney W. Grimes break; 16138fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16148fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16158fae3551SRodney W. Grimes break; 1616ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1617ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1618ef9e6dc7SBill Fenner break; 1619ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1620ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1621ef9e6dc7SBill Fenner break; 16228fae3551SRodney W. Grimes default: 16238fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16248fae3551SRodney W. Grimes } 16258fae3551SRodney W. Grimes } 16268fae3551SRodney W. Grimes 16278fae3551SRodney W. Grimes /* 16288fae3551SRodney W. Grimes * pr_iph -- 16298fae3551SRodney W. Grimes * Print an IP header with options. 16308fae3551SRodney W. Grimes */ 163143470e3bSGarrett Wollman static void 1632fafb8f11SEd Schouten pr_iph(struct ip *ip) 16338fae3551SRodney W. Grimes { 1634a94c074dSDimitry Andric struct in_addr ina; 16358fae3551SRodney W. Grimes u_char *cp; 1636efc8588dSDavid E. O'Brien int hlen; 16378fae3551SRodney W. Grimes 16388fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16398fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16408fae3551SRodney W. Grimes 1641ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16428fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1643ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1644ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1645d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1646d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1647d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1648ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1649ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1650a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1651a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1652a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1653a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1654ef9e6dc7SBill Fenner /* dump any option bytes */ 16558fae3551SRodney W. Grimes while (hlen-- > 20) { 16568fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16578fae3551SRodney W. Grimes } 16588fae3551SRodney W. Grimes (void)putchar('\n'); 16598fae3551SRodney W. Grimes } 16608fae3551SRodney W. Grimes 16618fae3551SRodney W. Grimes /* 16628fae3551SRodney W. Grimes * pr_addr -- 16638fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16648fae3551SRodney W. Grimes * a hostname. 16658fae3551SRodney W. Grimes */ 166643470e3bSGarrett Wollman static char * 1667fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16688fae3551SRodney W. Grimes { 16698fae3551SRodney W. Grimes struct hostent *hp; 1670f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16718fae3551SRodney W. Grimes 167249133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 167343470e3bSGarrett Wollman return inet_ntoa(ina); 167449133c6dSPawel Jakub Dawidek 167549133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 167649133c6dSPawel Jakub Dawidek 167749133c6dSPawel Jakub Dawidek if (hp == NULL) 167849133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 167949133c6dSPawel Jakub Dawidek 1680efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 168143470e3bSGarrett Wollman inet_ntoa(ina)); 16828fae3551SRodney W. Grimes return(buf); 16838fae3551SRodney W. Grimes } 16848fae3551SRodney W. Grimes 16858fae3551SRodney W. Grimes /* 16868fae3551SRodney W. Grimes * pr_retip -- 16878fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 16888fae3551SRodney W. Grimes */ 168943470e3bSGarrett Wollman static void 1690d9cacf60SAlan Somers pr_retip(struct ip *ip, const u_char *cp) 16918fae3551SRodney W. Grimes { 16928fae3551SRodney W. Grimes pr_iph(ip); 16938fae3551SRodney W. Grimes 16948fae3551SRodney W. Grimes if (ip->ip_p == 6) 16958fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 16968fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 16978fae3551SRodney W. Grimes else if (ip->ip_p == 17) 16988fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 16998fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17008fae3551SRodney W. Grimes } 17018fae3551SRodney W. Grimes 1702eb1543c6SMatthew N. Dodd static char * 1703007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1704eb1543c6SMatthew N. Dodd { 17057898770aSAlan Somers static char buf[11]; 1706007fe4e3SMaxim Konovalov int hour, min, sec; 1707eb1543c6SMatthew N. Dodd 1708007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1709007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1710007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1711007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1712eb1543c6SMatthew N. Dodd 1713007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1714eb1543c6SMatthew N. Dodd 1715eb1543c6SMatthew N. Dodd return (buf); 1716eb1543c6SMatthew N. Dodd } 1717eb1543c6SMatthew N. Dodd 171843470e3bSGarrett Wollman static void 1719fafb8f11SEd Schouten fill(char *bp, char *patp) 17208fae3551SRodney W. Grimes { 17218fae3551SRodney W. Grimes char *cp; 1722efc8588dSDavid E. O'Brien int pat[16]; 17234fba6582SMaxim Konovalov u_int ii, jj, kk; 17248fae3551SRodney W. Grimes 172543470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 172643470e3bSGarrett Wollman if (!isxdigit(*cp)) 172743470e3bSGarrett Wollman errx(EX_USAGE, 172843470e3bSGarrett Wollman "patterns must be specified as hex digits"); 172943470e3bSGarrett Wollman 17308fae3551SRodney W. Grimes } 17318fae3551SRodney W. Grimes ii = sscanf(patp, 17328fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17338fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17348fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17358fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17368fae3551SRodney W. Grimes 17378fae3551SRodney W. Grimes if (ii > 0) 1738d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17398fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17408fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17418fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17428fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17438fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17448fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17458fae3551SRodney W. Grimes (void)printf("\n"); 17468fae3551SRodney W. Grimes } 17478fae3551SRodney W. Grimes } 17488fae3551SRodney W. Grimes 174949133c6dSPawel Jakub Dawidek static cap_channel_t * 175049133c6dSPawel Jakub Dawidek capdns_setup(void) 175149133c6dSPawel Jakub Dawidek { 175249133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1753a3ce7698SAlan Somers #ifdef WITH_CASPER 175449133c6dSPawel Jakub Dawidek const char *types[2]; 175549133c6dSPawel Jakub Dawidek int families[1]; 1756a3ce7698SAlan Somers #endif 175749133c6dSPawel Jakub Dawidek capcas = cap_init(); 1758c501d73cSMariusz Zaborski if (capcas == NULL) 1759c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 176049133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 176149133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 176249133c6dSPawel Jakub Dawidek cap_close(capcas); 176349133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 176449133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1765a3ce7698SAlan Somers #ifdef WITH_CASPER 1766752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1767752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 176849133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 176949133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 177049133c6dSPawel Jakub Dawidek families[0] = AF_INET; 177149133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 177249133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1773a3ce7698SAlan Somers #endif 177449133c6dSPawel Jakub Dawidek return (capdnsloc); 177549133c6dSPawel Jakub Dawidek } 1776