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> 478fae3551SRodney W. Grimes /* 488fae3551SRodney W. Grimes * P I N G . C 498fae3551SRodney W. Grimes * 50e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 518fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 528fae3551SRodney W. Grimes * 538fae3551SRodney W. Grimes * Author - 548fae3551SRodney W. Grimes * Mike Muuss 558fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 568fae3551SRodney W. Grimes * December, 1983 578fae3551SRodney W. Grimes * 588fae3551SRodney W. Grimes * Status - 598fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 608fae3551SRodney W. Grimes * Bugs - 618fae3551SRodney W. Grimes * More statistics could always be gathered. 628fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 638fae3551SRodney W. Grimes */ 648fae3551SRodney W. Grimes 6543470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 66b881b8beSRobert Watson #include <sys/capsicum.h> 678fae3551SRodney W. Grimes #include <sys/socket.h> 68261e59bbSMaxim Konovalov #include <sys/sysctl.h> 698fae3551SRodney W. Grimes #include <sys/time.h> 70039d6aa4SBill Fenner #include <sys/uio.h> 718fae3551SRodney W. Grimes 728fae3551SRodney W. Grimes #include <netinet/in.h> 7343470e3bSGarrett Wollman #include <netinet/in_systm.h> 748fae3551SRodney W. Grimes #include <netinet/ip.h> 758fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 768fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7743470e3bSGarrett Wollman #include <arpa/inet.h> 78c501d73cSMariusz Zaborski 79c501d73cSMariusz Zaborski #include <libcasper.h> 80c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 818fae3551SRodney W. Grimes 829a4365d0SYoshinobu Inoue #ifdef IPSEC 838409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 849a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 859a4365d0SYoshinobu Inoue 867bdc3291SMark Johnston #include <capsicum_helpers.h> 87261e59bbSMaxim Konovalov #include <ctype.h> 88261e59bbSMaxim Konovalov #include <err.h> 89261e59bbSMaxim Konovalov #include <errno.h> 90261e59bbSMaxim Konovalov #include <netdb.h> 91d9cacf60SAlan Somers #include <stddef.h> 92261e59bbSMaxim Konovalov #include <signal.h> 93261e59bbSMaxim Konovalov #include <stdio.h> 94261e59bbSMaxim Konovalov #include <stdlib.h> 95261e59bbSMaxim Konovalov #include <string.h> 96261e59bbSMaxim Konovalov #include <sysexits.h> 971ad76f1bSAlan Somers #include <time.h> 98261e59bbSMaxim Konovalov #include <unistd.h> 99261e59bbSMaxim Konovalov 1003cde9171SAlan Somers #include "main.h" 1013cde9171SAlan Somers #include "ping.h" 102ff77ab83SAlan Somers #include "utils.h" 103ff77ab83SAlan Somers 104301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10513e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 106eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 107eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 108c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1098f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1108f975bb3SBruce Evans /* runs out of buffer space */ 1114fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1124fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 113d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 114bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1150b2f8b3fSMaxim Konovalov #define MAXTOS 255 1168fae3551SRodney W. Grimes 1178fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1188fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1198fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1208fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1218fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1228fae3551SRodney W. Grimes 12313e3f0b7SMaxim Konovalov struct tv32 { 12413e3f0b7SMaxim Konovalov int32_t tv32_sec; 1251ad76f1bSAlan Somers int32_t tv32_nsec; 12613e3f0b7SMaxim Konovalov }; 12713e3f0b7SMaxim Konovalov 1288fae3551SRodney W. Grimes /* various options */ 12985456935SBill Fenner #define F_FLOOD 0x0001 13085456935SBill Fenner #define F_INTERVAL 0x0002 13185456935SBill Fenner #define F_PINGFILLED 0x0008 13285456935SBill Fenner #define F_QUIET 0x0010 13385456935SBill Fenner #define F_RROUTE 0x0020 13485456935SBill Fenner #define F_SO_DEBUG 0x0040 13585456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13685456935SBill Fenner #define F_VERBOSE 0x0100 13785456935SBill Fenner #define F_QUIET2 0x0200 13885456935SBill Fenner #define F_NOLOOP 0x0400 13985456935SBill Fenner #define F_MTTL 0x0800 14085456935SBill Fenner #define F_MIF 0x1000 141772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1429a4365d0SYoshinobu Inoue #ifdef IPSEC 1439a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1449a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1459a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1469a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 147211bfbd2SRuslan Ermilov #define F_TTL 0x8000 148ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1498025c44bSDima Dorfman #define F_ONCE 0x20000 1500b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 151143008a1SMatthew N. Dodd #define F_MASK 0x80000 152eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1539ff95228SGleb Smirnoff #define F_SWEEP 0x200000 154d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 15581a6f4c7SRichard Scheffenegger #define F_IP_VLAN_PCP 0x800000 156d399eb3eSPiotr Pawel Stefaniak #define F_DOT 0x1000000 1578fae3551SRodney W. Grimes 1588fae3551SRodney W. Grimes /* 1598fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1608fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1618fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1628fae3551SRodney W. Grimes */ 1638fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1647e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1657e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1668fae3551SRodney W. Grimes 1677e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1687e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1697e9489e0SHiroki Sato static int maxpayload; 1707e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1717e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1727e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1737e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1747e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 175d399eb3eSPiotr Pawel Stefaniak static const char *DOT = "."; 176d399eb3eSPiotr Pawel Stefaniak static size_t DOTlen = 1; 177d399eb3eSPiotr Pawel Stefaniak static size_t DOTidx = 0; 1787e9489e0SHiroki Sato static char *shostname; 1797e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1807e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1817e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1827e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1837e9489e0SHiroki Sato static int phdr_len = 0; 1847e9489e0SHiroki Sato static int send_len; 1858fae3551SRodney W. Grimes 1868fae3551SRodney W. Grimes /* counters */ 1877e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1887e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1897e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1907e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1917e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1927e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1937e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1947e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1957e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 196badd8138SSean Eric Fagan 19749133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 19849133c6dSPawel Jakub Dawidek 19943470e3bSGarrett Wollman static void fill(char *, char *); 20049133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 20143470e3bSGarrett Wollman static void pinger(void); 20243470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 203eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 204d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 20520b41303SJose Luis Duran static void pr_iph(struct ip *, const u_char *); 206d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 2078fae3551SRodney W. Grimes 20843470e3bSGarrett Wollman int 2093cde9171SAlan Somers ping(int argc, char *const *argv) 2108fae3551SRodney W. Grimes { 21131eac03bSSean Chittenden struct sockaddr_in from, sock_in; 212efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2131ad76f1bSAlan Somers struct timespec last, intvl; 214efc8588dSDavid E. O'Brien struct iovec iov; 215efc8588dSDavid E. O'Brien struct msghdr msg; 216efc8588dSDavid E. O'Brien struct sigaction si_sa; 2170b2f8b3fSMaxim Konovalov size_t sz; 218e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 21978e1f68eSMark Johnston const char *errstr; 220d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 221261e59bbSMaxim Konovalov struct hostent *hp; 222efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 223efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 224efc8588dSDavid E. O'Brien #endif 225261e59bbSMaxim Konovalov struct sockaddr_in *to; 226261e59bbSMaxim Konovalov double t; 227c0a3773aSEugene Grosbein u_long alarmtimeout; 22878e1f68eSMark Johnston long long ltmp; 22949133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 23081a6f4c7SRichard Scheffenegger int ssend_errno, srecv_errno, tos, ttl, pcp; 2311ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 232efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2338fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2344fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2358fae3551SRodney W. Grimes #endif 236261e59bbSMaxim Konovalov unsigned char loop, mttl; 237efc8588dSDavid E. O'Brien 23831eac03bSSean Chittenden payload = source = NULL; 2399a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 240efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2419a4365d0SYoshinobu Inoue #endif 24249133c6dSPawel Jakub Dawidek cap_rights_t rights; 2438fae3551SRodney W. Grimes 244f1284d7aSBill Fenner /* 245f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 246f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 247f1284d7aSBill Fenner * after arg parsing. 24849133c6dSPawel Jakub Dawidek * 24949133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 25049133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 25149133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 25249133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 25349133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 25449133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 25549133c6dSPawel Jakub Dawidek * packets. 256f1284d7aSBill Fenner */ 25749133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 25849133c6dSPawel Jakub Dawidek ssend_errno = errno; 25949133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 26049133c6dSPawel Jakub Dawidek srecv_errno = errno; 261f1284d7aSBill Fenner 2621d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2631d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 264ee2bf734SWarner Losh uid = getuid(); 265f1284d7aSBill Fenner 266eeb63943SDon Lewis if (ssend < 0) { 267eeb63943SDon Lewis errno = ssend_errno; 268eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 269eeb63943SDon Lewis } 270eeb63943SDon Lewis 271eeb63943SDon Lewis if (srecv < 0) { 272eeb63943SDon Lewis errno = srecv_errno; 273eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 274eeb63943SDon Lewis } 275eeb63943SDon Lewis 27681a6f4c7SRichard Scheffenegger alarmtimeout = df = preload = tos = pcp = 0; 277badd8138SSean Eric Fagan 2780b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 2799ce201f2SAlan Somers while ((ch = getopt(argc, argv, PING4OPTS)) != -1) { 2808fae3551SRodney W. Grimes switch(ch) { 281d399eb3eSPiotr Pawel Stefaniak case '.': 282d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 283d399eb3eSPiotr Pawel Stefaniak if (optarg != NULL) { 284d399eb3eSPiotr Pawel Stefaniak DOT = optarg; 285d399eb3eSPiotr Pawel Stefaniak DOTlen = strlen(optarg); 286d399eb3eSPiotr Pawel Stefaniak } 287d399eb3eSPiotr Pawel Stefaniak break; 2883cde9171SAlan Somers case '4': 2893cde9171SAlan Somers /* This option is processed in main(). */ 2903cde9171SAlan Somers break; 291ca517ad8SPoul-Henning Kamp case 'A': 292ca517ad8SPoul-Henning Kamp options |= F_MISSED; 293ca517ad8SPoul-Henning Kamp break; 294772dfa72SDaniel O'Callaghan case 'a': 295772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 296772dfa72SDaniel O'Callaghan break; 29781a6f4c7SRichard Scheffenegger case 'C': 29881a6f4c7SRichard Scheffenegger options |= F_IP_VLAN_PCP; 29978e1f68eSMark Johnston ltmp = strtonum(optarg, -1, 7, &errstr); 30078e1f68eSMark Johnston if (errstr != NULL) 30181a6f4c7SRichard Scheffenegger errx(EX_USAGE, "invalid PCP: `%s'", optarg); 30281a6f4c7SRichard Scheffenegger pcp = ltmp; 30381a6f4c7SRichard Scheffenegger break; 3048fae3551SRodney W. Grimes case 'c': 30578e1f68eSMark Johnston ltmp = strtonum(optarg, 1, LONG_MAX, &errstr); 30678e1f68eSMark Johnston if (errstr != NULL) 30743470e3bSGarrett Wollman errx(EX_USAGE, 30843470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 30943470e3bSGarrett Wollman optarg); 31078e1f68eSMark Johnston npackets = (long)ltmp; 3118fae3551SRodney W. Grimes break; 3120b2f8b3fSMaxim Konovalov case 'D': 3130b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3140b2f8b3fSMaxim Konovalov df = 1; 3150b2f8b3fSMaxim Konovalov break; 3168fae3551SRodney W. Grimes case 'd': 3178fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3188fae3551SRodney W. Grimes break; 3198fae3551SRodney W. Grimes case 'f': 320526f06b2SMatthew Dillon if (uid) { 32143470e3bSGarrett Wollman errno = EPERM; 32243470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3238fae3551SRodney W. Grimes } 3248fae3551SRodney W. Grimes options |= F_FLOOD; 325d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 3268fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3278fae3551SRodney W. Grimes break; 3289ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 32978e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 33078e1f68eSMark Johnston if (errstr != NULL) { 3319ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3329ff95228SGleb Smirnoff optarg); 33378e1f68eSMark Johnston } 33478e1f68eSMark Johnston sweepmax = (int)ltmp; 33578e1f68eSMark Johnston if (uid != 0 && sweepmax > DEFDATALEN) { 33678e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 33778e1f68eSMark Johnston "packet size too large: %d > %u", 33878e1f68eSMark Johnston sweepmax, DEFDATALEN); 3399ff95228SGleb Smirnoff } 3409ff95228SGleb Smirnoff options |= F_SWEEP; 3419ff95228SGleb Smirnoff break; 3429ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 34378e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 34478e1f68eSMark Johnston if (errstr != NULL) { 3459ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3469ff95228SGleb Smirnoff optarg); 34778e1f68eSMark Johnston } 34878e1f68eSMark Johnston sweepmin = (int)ltmp; 34978e1f68eSMark Johnston if (uid != 0 && sweepmin > DEFDATALEN) { 35078e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 35178e1f68eSMark Johnston "packet size too large: %d > %u", 35278e1f68eSMark Johnston sweepmin, DEFDATALEN); 3539ff95228SGleb Smirnoff } 3549ff95228SGleb Smirnoff options |= F_SWEEP; 3559ff95228SGleb Smirnoff break; 35699f13ae1SAlan Somers case 'H': 35703d4d1c7SJose Luis Duran options |= F_HOSTNAME; 35899f13ae1SAlan Somers break; 3599ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 36078e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 36178e1f68eSMark Johnston if (errstr != NULL) { 36278e1f68eSMark Johnston errx(EX_USAGE, "invalid packet size: `%s'", 3639ff95228SGleb Smirnoff optarg); 36478e1f68eSMark Johnston } 36578e1f68eSMark Johnston sweepincr = (int)ltmp; 36678e1f68eSMark Johnston if (uid != 0 && sweepincr > DEFDATALEN) { 36778e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 36878e1f68eSMark Johnston "packet size too large: %d > %u", 36978e1f68eSMark Johnston sweepincr, DEFDATALEN); 3709ff95228SGleb Smirnoff } 3719ff95228SGleb Smirnoff options |= F_SWEEP; 3729ff95228SGleb Smirnoff break; 3731f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3741f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 3751f6a4631SRuslan Ermilov errx(EX_USAGE, 3761f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3771f6a4631SRuslan Ermilov optarg); 3781f6a4631SRuslan Ermilov options |= F_MIF; 3791f6a4631SRuslan Ermilov break; 3808fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3811ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3821ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3831ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3841ad0b1beSMaxim Konovalov optarg); 3858fae3551SRodney W. Grimes options |= F_INTERVAL; 386526f06b2SMatthew Dillon interval = (int)t; 387526f06b2SMatthew Dillon if (uid && interval < 1000) { 388526f06b2SMatthew Dillon errno = EPERM; 389526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 390526f06b2SMatthew Dillon } 3918fae3551SRodney W. Grimes break; 3921f6a4631SRuslan Ermilov case 'L': 3931f6a4631SRuslan Ermilov options |= F_NOLOOP; 3941f6a4631SRuslan Ermilov loop = 0; 39585456935SBill Fenner break; 3968fae3551SRodney W. Grimes case 'l': 39778e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 39878e1f68eSMark Johnston if (errstr != NULL) 39943470e3bSGarrett Wollman errx(EX_USAGE, 40043470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4015e2cc0f4SStephen McKay if (uid) { 402f78ac61bSWarner Losh errno = EPERM; 403f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 404f78ac61bSWarner Losh } 40578e1f68eSMark Johnston preload = (int)ltmp; 4068fae3551SRodney W. Grimes break; 4071f6a4631SRuslan Ermilov case 'M': 408eb1543c6SMatthew N. Dodd switch(optarg[0]) { 409eb1543c6SMatthew N. Dodd case 'M': 410eb1543c6SMatthew N. Dodd case 'm': 4111f6a4631SRuslan Ermilov options |= F_MASK; 41285456935SBill Fenner break; 413eb1543c6SMatthew N. Dodd case 'T': 414eb1543c6SMatthew N. Dodd case 't': 415eb1543c6SMatthew N. Dodd options |= F_TIME; 416eb1543c6SMatthew N. Dodd break; 417eb1543c6SMatthew N. Dodd default: 418eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 419eb1543c6SMatthew N. Dodd break; 420eb1543c6SMatthew N. Dodd } 421eb1543c6SMatthew N. Dodd break; 422211bfbd2SRuslan Ermilov case 'm': /* TTL */ 42378e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 42478e1f68eSMark Johnston if (errstr != NULL) 4251ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 42678e1f68eSMark Johnston ttl = (int)ltmp; 427211bfbd2SRuslan Ermilov options |= F_TTL; 428211bfbd2SRuslan Ermilov break; 4298fae3551SRodney W. Grimes case 'n': 43003d4d1c7SJose Luis Duran options &= ~F_HOSTNAME; 4318fae3551SRodney W. Grimes break; 4328025c44bSDima Dorfman case 'o': 4338025c44bSDima Dorfman options |= F_ONCE; 4348025c44bSDima Dorfman break; 4351f6a4631SRuslan Ermilov #ifdef IPSEC 4361f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4371f6a4631SRuslan Ermilov case 'P': 4381f6a4631SRuslan Ermilov options |= F_POLICY; 4391f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4401f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4411f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4421f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4431f6a4631SRuslan Ermilov else 4441f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4451f6a4631SRuslan Ermilov break; 4461f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4471f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4488fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4498fae3551SRodney W. Grimes options |= F_PINGFILLED; 450d074d39fSMatthew N. Dodd payload = optarg; 4518fae3551SRodney W. Grimes break; 452ef9e6dc7SBill Fenner case 'Q': 453ef9e6dc7SBill Fenner options |= F_QUIET2; 454ef9e6dc7SBill Fenner break; 4558fae3551SRodney W. Grimes case 'q': 4568fae3551SRodney W. Grimes options |= F_QUIET; 4578fae3551SRodney W. Grimes break; 4588fae3551SRodney W. Grimes case 'R': 4598fae3551SRodney W. Grimes options |= F_RROUTE; 4608fae3551SRodney W. Grimes break; 4618fae3551SRodney W. Grimes case 'r': 4628fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4638fae3551SRodney W. Grimes break; 4641f6a4631SRuslan Ermilov case 'S': 4651f6a4631SRuslan Ermilov source = optarg; 4661f6a4631SRuslan Ermilov break; 4678fae3551SRodney W. Grimes case 's': /* size of packet to send */ 46878e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 46978e1f68eSMark Johnston if (errstr != NULL) 47043470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 47143470e3bSGarrett Wollman optarg); 47278e1f68eSMark Johnston datalen = (int)ltmp; 47378e1f68eSMark Johnston if (uid != 0 && datalen > DEFDATALEN) { 474fb7d32c7SMaxim Konovalov errno = EPERM; 475fb7d32c7SMaxim Konovalov err(EX_NOPERM, 47678e1f68eSMark Johnston "packet size too large: %d > %u", 47778e1f68eSMark Johnston datalen, DEFDATALEN); 478fb7d32c7SMaxim Konovalov } 4798fae3551SRodney W. Grimes break; 4801f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 48178e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 48278e1f68eSMark Johnston if (errstr != NULL) 4831f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4841f6a4631SRuslan Ermilov optarg); 48578e1f68eSMark Johnston mttl = (unsigned char)ltmp; 4861f6a4631SRuslan Ermilov options |= F_MTTL; 48799490edeSWarner Losh break; 4887237fd94SBill Fumerola case 't': 489bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 490bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 4917237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 4927237fd94SBill Fumerola optarg); 493bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 494bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 495bf113f1bSBill Fumerola optarg, MAXALARM); 4962eb6acc2SAlan Somers { 4972eb6acc2SAlan Somers struct itimerval itv; 4982eb6acc2SAlan Somers 4992eb6acc2SAlan Somers timerclear(&itv.it_interval); 5002eb6acc2SAlan Somers timerclear(&itv.it_value); 5012eb6acc2SAlan Somers itv.it_value.tv_sec = (time_t)alarmtimeout; 5022eb6acc2SAlan Somers if (setitimer(ITIMER_REAL, &itv, NULL) != 0) 5032eb6acc2SAlan Somers err(1, "setitimer"); 5042eb6acc2SAlan Somers } 5057237fd94SBill Fumerola break; 5068fae3551SRodney W. Grimes case 'v': 5078fae3551SRodney W. Grimes options |= F_VERBOSE; 5088fae3551SRodney W. Grimes break; 509d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 510d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 511d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 512d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 513d6cd1497SGleb Smirnoff optarg); 514d6cd1497SGleb Smirnoff options |= F_WAITTIME; 515d6cd1497SGleb Smirnoff waittime = (int)t; 516d6cd1497SGleb Smirnoff break; 5170b2f8b3fSMaxim Konovalov case 'z': 5180b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 519c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 520c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5210b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 522c0a3773aSEugene Grosbein tos = ltmp; 5230b2f8b3fSMaxim Konovalov break; 5248fae3551SRodney W. Grimes default: 525e345a80dSPhilippe Charnier usage(); 52643470e3bSGarrett Wollman } 52743470e3bSGarrett Wollman } 52843470e3bSGarrett Wollman 52943470e3bSGarrett Wollman if (argc - optind != 1) 530e345a80dSPhilippe Charnier usage(); 53143470e3bSGarrett Wollman target = argv[optind]; 5328fae3551SRodney W. Grimes 533d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 534d829c3dfSMatthew N. Dodd case 0: break; 535d829c3dfSMatthew N. Dodd case F_MASK: 536eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 537eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 538d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 539eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 540eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 541d829c3dfSMatthew N. Dodd break; 542d829c3dfSMatthew N. Dodd case F_TIME: 543eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 544eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 545d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 546eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 547eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 548d829c3dfSMatthew N. Dodd break; 549d829c3dfSMatthew N. Dodd default: 550d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 551d829c3dfSMatthew N. Dodd break; 552eb1543c6SMatthew N. Dodd } 5530fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 554fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 555e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 556e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 557fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5581104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 559fb7d32c7SMaxim Konovalov maxpayload); 560e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5610fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 562d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 563d074d39fSMatthew N. Dodd fill((char *)datap, payload); 564d074d39fSMatthew N. Dodd } 56549133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 56699490edeSWarner Losh if (source) { 56731eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 56831eac03bSSean Chittenden sock_in.sin_family = AF_INET; 56931eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 57099490edeSWarner Losh shostname = source; 57199490edeSWarner Losh } else { 572d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 57399490edeSWarner Losh if (!hp) 57499490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 57599490edeSWarner Losh source, hstrerror(h_errno)); 57699490edeSWarner Losh 57731eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 57831eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 5794fba6582SMaxim Konovalov hp->h_length < 0) 58099490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 58131eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 58231eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 58399490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 58499490edeSWarner Losh sizeof(snamebuf) - 1); 58599490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 58699490edeSWarner Losh shostname = snamebuf; 58799490edeSWarner Losh } 58849133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 58949133c6dSPawel Jakub Dawidek -1) 59099490edeSWarner Losh err(1, "bind"); 59199490edeSWarner Losh } 59299490edeSWarner Losh 593d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 594d389e86aSMatt Jacob to = &whereto; 5958fae3551SRodney W. Grimes to->sin_family = AF_INET; 596d389e86aSMatt Jacob to->sin_len = sizeof *to; 59743470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 5988fae3551SRodney W. Grimes hostname = target; 59943470e3bSGarrett Wollman } else { 60049133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 60143470e3bSGarrett Wollman if (!hp) 60243470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 60343470e3bSGarrett Wollman target, hstrerror(h_errno)); 60443470e3bSGarrett Wollman 60531eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6061ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 60743470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6088fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 609b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6108fae3551SRodney W. Grimes hostname = hnamebuf; 6118fae3551SRodney W. Grimes } 6128fae3551SRodney W. Grimes 61349133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 614a3ce7698SAlan Somers #ifdef WITH_CASPER 61549133c6dSPawel Jakub Dawidek if (capdns != NULL) { 61649133c6dSPawel Jakub Dawidek const char *types[1]; 61749133c6dSPawel Jakub Dawidek 618752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 61949133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 62049133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 62149133c6dSPawel Jakub Dawidek } 622a3ce7698SAlan Somers #endif 62349133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 62449133c6dSPawel Jakub Dawidek err(1, "connect"); 62549133c6dSPawel Jakub Dawidek 62643470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 62743470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 62843470e3bSGarrett Wollman 62943470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63043470e3bSGarrett Wollman errx(EX_USAGE, 63143470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 63243470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 63343470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63443470e3bSGarrett Wollman errx(EX_USAGE, 63543470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6368fae3551SRodney W. Grimes 637d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6388fae3551SRodney W. Grimes timing = 1; 63943470e3bSGarrett Wollman 64078e1f68eSMark Johnston if ((options & (F_PINGFILLED | F_SWEEP)) == 0) 641d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6428fae3551SRodney W. Grimes *datap++ = i; 6438fae3551SRodney W. Grimes 6448fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6458fae3551SRodney W. Grimes 6468fae3551SRodney W. Grimes hold = 1; 64749133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 64849133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6498fae3551SRodney W. Grimes sizeof(hold)); 65049133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 65149133c6dSPawel Jakub Dawidek sizeof(hold)); 65249133c6dSPawel Jakub Dawidek } 6538fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 65449133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6558fae3551SRodney W. Grimes sizeof(hold)); 65681a6f4c7SRichard Scheffenegger if (options & F_IP_VLAN_PCP) { 65781a6f4c7SRichard Scheffenegger (void)setsockopt(ssend, IPPROTO_IP, IP_VLAN_PCP, (char *)&pcp, 65881a6f4c7SRichard Scheffenegger sizeof(pcp)); 65981a6f4c7SRichard Scheffenegger } 6609a4365d0SYoshinobu Inoue #ifdef IPSEC 6619a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6629a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6639a4365d0SYoshinobu Inoue char *buf; 6649a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6659a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6669a4365d0SYoshinobu Inoue if (buf == NULL) 667ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 66849133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6699a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6701ad0b1beSMaxim Konovalov err(EX_CONFIG, 6711ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6729a4365d0SYoshinobu Inoue free(buf); 6739a4365d0SYoshinobu Inoue } 6749a4365d0SYoshinobu Inoue 6759a4365d0SYoshinobu Inoue if (policy_out != NULL) { 6769a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 6779a4365d0SYoshinobu Inoue if (buf == NULL) 678ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 67949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 6809a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6811ad0b1beSMaxim Konovalov err(EX_CONFIG, 6821ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6839a4365d0SYoshinobu Inoue free(buf); 6849a4365d0SYoshinobu Inoue } 6859a4365d0SYoshinobu Inoue } 6869a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 6879a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 6888fae3551SRodney W. Grimes 6890b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 690d9cacf60SAlan Somers struct ip ip; 691d9cacf60SAlan Somers 692d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 6930b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 6940b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 6950b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 6960b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 6970b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 6980b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 6990b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7000b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7010b2f8b3fSMaxim Konovalov } 70249133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 703d9cacf60SAlan Somers ip.ip_v = IPVERSION; 704d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 705d9cacf60SAlan Somers ip.ip_tos = tos; 706d9cacf60SAlan Somers ip.ip_id = 0; 707d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 708d9cacf60SAlan Somers ip.ip_ttl = ttl; 709d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 710d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 711d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 712d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7130b2f8b3fSMaxim Konovalov } 71449133c6dSPawel Jakub Dawidek 71549133c6dSPawel Jakub Dawidek /* 71649133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 71749133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 71849133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 71949133c6dSPawel Jakub Dawidek */ 7207bdc3291SMark Johnston caph_cache_catpages(); 72118fcfaa4SMark Johnston if (caph_enter_casper() < 0) 722301bc9f9SAlan Somers err(1, "caph_enter_casper"); 72349133c6dSPawel Jakub Dawidek 72449133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7257bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 72649133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 72749133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7287bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 72949133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 73049133c6dSPawel Jakub Dawidek 7318fae3551SRodney W. Grimes /* record route option */ 7328fae3551SRodney W. Grimes if (options & F_RROUTE) { 7338fae3551SRodney W. Grimes #ifdef IP_OPTIONS 734039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7358fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7368fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7378fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 738039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 73949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 74043470e3bSGarrett Wollman sizeof(rspace)) < 0) 74143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7428fae3551SRodney W. Grimes #else 74343470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 74443470e3bSGarrett Wollman "record route not available in this implementation"); 7458fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7468fae3551SRodney W. Grimes } 7478fae3551SRodney W. Grimes 748211bfbd2SRuslan Ermilov if (options & F_TTL) { 74949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 750211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 751211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 752211bfbd2SRuslan Ermilov } 753211bfbd2SRuslan Ermilov } 75485456935SBill Fenner if (options & F_NOLOOP) { 75549133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 75685456935SBill Fenner sizeof(loop)) < 0) { 75743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 75885456935SBill Fenner } 75985456935SBill Fenner } 76085456935SBill Fenner if (options & F_MTTL) { 76149133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 762211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 76343470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 76485456935SBill Fenner } 76585456935SBill Fenner } 76685456935SBill Fenner if (options & F_MIF) { 76749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 76885456935SBill Fenner sizeof(ifaddr)) < 0) { 76943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 77085456935SBill Fenner } 77185456935SBill Fenner } 772039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 77384633ef1SAlan Somers { 77484633ef1SAlan Somers int on = 1; 77584633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 77684633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 77784633ef1SAlan Somers sizeof(on)) < 0) 778039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 77984633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 78084633ef1SAlan Somers sizeof(ts_clock)) < 0) 78184633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 782039d6aa4SBill Fenner } 783039d6aa4SBill Fenner #endif 7849ff95228SGleb Smirnoff if (sweepmax) { 785bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 78678e1f68eSMark Johnston errx(EX_USAGE, 78778e1f68eSMark Johnston "Maximum packet size must be no less than the minimum packet size"); 78878e1f68eSMark Johnston 78978e1f68eSMark Johnston if (sweepmax > maxpayload - TIMEVAL_LEN) 79078e1f68eSMark Johnston errx(EX_USAGE, "Invalid sweep maximum"); 7919ff95228SGleb Smirnoff 7929ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 79378e1f68eSMark Johnston errx(EX_USAGE, 79478e1f68eSMark Johnston "Packet size and ping sweep are mutually exclusive"); 7959ff95228SGleb Smirnoff 7969ff95228SGleb Smirnoff if (npackets > 0) { 7979ff95228SGleb Smirnoff snpackets = npackets; 7989ff95228SGleb Smirnoff npackets = 0; 7999ff95228SGleb Smirnoff } else 8009ff95228SGleb Smirnoff snpackets = 1; 8019ff95228SGleb Smirnoff datalen = sweepmin; 8029ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 8039ff95228SGleb Smirnoff } 8049ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8059ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 80685456935SBill Fenner 8078fae3551SRodney W. Grimes /* 8088fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8098fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8108fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 81143470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 81243470e3bSGarrett Wollman * or multicast pings if they wish. 8138fae3551SRodney W. Grimes */ 8144fba6582SMaxim Konovalov 8154fba6582SMaxim Konovalov /* 8164fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8174fba6582SMaxim Konovalov * as well. 8184fba6582SMaxim Konovalov */ 8194fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 82049133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8218fae3551SRodney W. Grimes sizeof(hold)); 82249133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 82349133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8247bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 82549133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 826261e59bbSMaxim Konovalov if (uid == 0) 82749133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 828e8bd25ceSRobert Watson sizeof(hold)); 82949133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 83049133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8317bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 83249133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 833e8bd25ceSRobert Watson 83499490edeSWarner Losh if (to->sin_family == AF_INET) { 83599490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 83699490edeSWarner Losh inet_ntoa(to->sin_addr)); 83799490edeSWarner Losh if (source) 83899490edeSWarner Losh (void)printf(" from %s", shostname); 8399ff95228SGleb Smirnoff if (sweepmax) 8409ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8419ff95228SGleb Smirnoff sweepmin, sweepmax); 8429ff95228SGleb Smirnoff else 84399490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8449ff95228SGleb Smirnoff 8459ff95228SGleb Smirnoff } else { 8469ff95228SGleb Smirnoff if (sweepmax) 8479ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8489ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8499ff95228SGleb Smirnoff else 8508fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8519ff95228SGleb Smirnoff } 8528fae3551SRodney W. Grimes 8537d81b35cSBruce Evans /* 854a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 855a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8567d81b35cSBruce Evans */ 857a2a00888SSean Eric Fagan 858f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 85937e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 86003d4d1c7SJose Luis Duran si_sa.sa_handler = onsignal; 86103d4d1c7SJose Luis Duran if (sigaction(SIGINT, &si_sa, 0) == -1) 862a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 86303d4d1c7SJose Luis Duran seenint = 0; 86403d4d1c7SJose Luis Duran if (sigaction(SIGINFO, &si_sa, 0) == -1) 86572d3e667SJose Luis Duran err(EX_OSERR, "sigaction SIGINFO"); 86603d4d1c7SJose Luis Duran seeninfo = 0; 867bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8687237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8697237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 870bf113f1bSBill Fumerola } 87137e5b2c6SSean Eric Fagan 872039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 873039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 874039d6aa4SBill Fenner msg.msg_iov = &iov; 875039d6aa4SBill Fenner msg.msg_iovlen = 1; 876039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 877039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 87867511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 879039d6aa4SBill Fenner #endif 880039d6aa4SBill Fenner iov.iov_base = packet; 881e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 882039d6aa4SBill Fenner 88332af342fSRuslan Ermilov if (preload == 0) 88432af342fSRuslan Ermilov pinger(); /* send the first ping */ 88532af342fSRuslan Ermilov else { 88632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 88732af342fSRuslan Ermilov preload = npackets; 8888fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8898fae3551SRodney W. Grimes pinger(); 89032af342fSRuslan Ermilov } 8911ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 8928fae3551SRodney W. Grimes 893039d6aa4SBill Fenner if (options & F_FLOOD) { 894039d6aa4SBill Fenner intvl.tv_sec = 0; 8951ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 896039d6aa4SBill Fenner } else { 897526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 8981ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 899039d6aa4SBill Fenner } 900039d6aa4SBill Fenner 901261e59bbSMaxim Konovalov almost_done = 0; 90203d4d1c7SJose Luis Duran while (seenint == 0) { 9031ad76f1bSAlan Somers struct timespec now, timeout; 904039d6aa4SBill Fenner fd_set rfds; 905d9cacf60SAlan Somers int n; 906d9cacf60SAlan Somers ssize_t cc; 9078fae3551SRodney W. Grimes 90803d4d1c7SJose Luis Duran /* signal handling */ 90903d4d1c7SJose Luis Duran if (seeninfo) { 91003d4d1c7SJose Luis Duran pr_summary(stderr); 91103d4d1c7SJose Luis Duran seeninfo = 0; 91203d4d1c7SJose Luis Duran continue; 91303d4d1c7SJose Luis Duran } 91449133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9157e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 916039d6aa4SBill Fenner FD_ZERO(&rfds); 91749133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9181ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9191ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9201ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 921039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9221ad76f1bSAlan Somers timespecclear(&timeout); 92303d4d1c7SJose Luis Duran 9241ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9255e2cc0f4SStephen McKay if (n < 0) 92603d4d1c7SJose Luis Duran continue; /* EINTR */ 927039d6aa4SBill Fenner if (n == 1) { 9281ad76f1bSAlan Somers struct timespec *tv = NULL; 929039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 93067511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 931039d6aa4SBill Fenner #endif 932039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 93349133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9348fae3551SRodney W. Grimes if (errno == EINTR) 9358fae3551SRodney W. Grimes continue; 936e345a80dSPhilippe Charnier warn("recvmsg"); 9378fae3551SRodney W. Grimes continue; 9388fae3551SRodney W. Grimes } 93946d7b45aSTom Jones /* If we have a 0 byte read from recvfrom continue */ 94046d7b45aSTom Jones if (cc == 0) 94146d7b45aSTom Jones continue; 942039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 943b17fb992SAlan Somers if (cmsg != NULL && 944b17fb992SAlan Somers cmsg->cmsg_level == SOL_SOCKET && 945039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 94631eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 947fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 948fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 94931eac03bSSean Chittenden tv = &now; 950fa05a94cSJohn Birrell } 951039d6aa4SBill Fenner #endif 95231eac03bSSean Chittenden if (tv == NULL) { 9531ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 95431eac03bSSean Chittenden tv = &now; 955039d6aa4SBill Fenner } 95631eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 95731eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 95831eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9598fae3551SRodney W. Grimes break; 9608fae3551SRodney W. Grimes } 96103d4d1c7SJose Luis Duran if (n == 0 || (options & F_FLOOD)) { 9629ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 96378e1f68eSMark Johnston if (datalen + sweepincr > sweepmax) 96478e1f68eSMark Johnston break; 96578e1f68eSMark Johnston for (i = 0; i < sweepincr; i++) 9669ff95228SGleb Smirnoff *datap++ = i; 9679ff95228SGleb Smirnoff datalen += sweepincr; 9689ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9699ff95228SGleb Smirnoff sntransmitted = 0; 9709ff95228SGleb Smirnoff } 971039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 972039d6aa4SBill Fenner pinger(); 973039d6aa4SBill Fenner else { 974039d6aa4SBill Fenner if (almost_done) 975039d6aa4SBill Fenner break; 976039d6aa4SBill Fenner almost_done = 1; 97703d4d1c7SJose Luis Duran /* 97803d4d1c7SJose Luis Duran * If we're not transmitting any more packets, 97903d4d1c7SJose Luis Duran * change the timer to wait two round-trip times 98003d4d1c7SJose Luis Duran * if we've received any packets or (waittime) 98103d4d1c7SJose Luis Duran * milliseconds if we haven't. 98203d4d1c7SJose Luis Duran */ 9831ad76f1bSAlan Somers intvl.tv_nsec = 0; 984039d6aa4SBill Fenner if (nreceived) { 985039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 98603d4d1c7SJose Luis Duran if (intvl.tv_sec == 0) 987039d6aa4SBill Fenner intvl.tv_sec = 1; 988d6cd1497SGleb Smirnoff } else { 989d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 99003d4d1c7SJose Luis Duran intvl.tv_nsec = 99103d4d1c7SJose Luis Duran waittime % 1000 * 1000000; 992d6cd1497SGleb Smirnoff } 993039d6aa4SBill Fenner } 9941ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 99525107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 99625107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 99725107197SIan Dowse if (options & F_MISSED) 998ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 999039d6aa4SBill Fenner } 1000039d6aa4SBill Fenner } 100125107197SIan Dowse } 100203d4d1c7SJose Luis Duran pr_summary(stdout); 10038fae3551SRodney W. Grimes 100403d4d1c7SJose Luis Duran exit(nreceived ? 0 : 2); 1005515dd2faSJulian Elischer } 1006515dd2faSJulian Elischer 1007515dd2faSJulian Elischer /* 10088fae3551SRodney W. Grimes * pinger -- 10098fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10108fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1011eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10121ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10134fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10148fae3551SRodney W. Grimes */ 101543470e3bSGarrett Wollman static void 101643470e3bSGarrett Wollman pinger(void) 10178fae3551SRodney W. Grimes { 10181ad76f1bSAlan Somers struct timespec now; 101913e3f0b7SMaxim Konovalov struct tv32 tv32; 1020d9cacf60SAlan Somers struct icmp icp; 1021efc8588dSDavid E. O'Brien int cc, i; 10220b2f8b3fSMaxim Konovalov u_char *packet; 10238fae3551SRodney W. Grimes 10240b2f8b3fSMaxim Konovalov packet = outpack; 1025d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1026d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1027d9cacf60SAlan Somers icp.icmp_code = 0; 1028d9cacf60SAlan Somers icp.icmp_cksum = 0; 1029d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1030d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10318fae3551SRodney W. Grimes 10325db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10338fae3551SRodney W. Grimes 1034eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10351ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10361ad76f1bSAlan Somers /* 10371ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10381ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10391ad76f1bSAlan Somers * packet. We're only concerned with 10401ad76f1bSAlan Somers * durations, not absolute times. 10411ad76f1bSAlan Somers */ 10421ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10431ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1044eb1543c6SMatthew N. Dodd if (options & F_TIME) 1045d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10461ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1047eb1543c6SMatthew N. Dodd if (timing) 104813e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10490fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 105013e3f0b7SMaxim Konovalov sizeof(tv32)); 1051eb1543c6SMatthew N. Dodd } 1052eb1543c6SMatthew N. Dodd 1053d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1054d9cacf60SAlan Somers 10550fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10568fae3551SRodney W. Grimes 10578fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1058d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1059d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1060d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1061d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 10628fae3551SRodney W. Grimes 10630b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1064d9cacf60SAlan Somers struct ip ip; 1065d9cacf60SAlan Somers 10660b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1067d9cacf60SAlan Somers ip.ip_len = htons(cc); 1068d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1069d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1070d9cacf60SAlan Somers sizeof(ip.ip_len)); 1071d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1072d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1073d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1074d9cacf60SAlan Somers sizeof(ip.ip_sum)); 10750b2f8b3fSMaxim Konovalov packet = outpackhdr; 10760b2f8b3fSMaxim Konovalov } 107749133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10788fae3551SRodney W. Grimes if (i < 0 || i != cc) { 107943470e3bSGarrett Wollman if (i < 0) { 10808f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1081515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1082515dd2faSJulian Elischer return; 1083515dd2faSJulian Elischer } 108443470e3bSGarrett Wollman warn("sendto"); 108543470e3bSGarrett Wollman } else { 108643470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1087416aa49bSPoul-Henning Kamp hostname, i, cc); 10888fae3551SRodney W. Grimes } 1089363d7bbeSJulian Elischer } 1090363d7bbeSJulian Elischer ntransmitted++; 10919ff95228SGleb Smirnoff sntransmitted++; 1092d399eb3eSPiotr Pawel Stefaniak if (!(options & F_QUIET) && options & F_DOT) 1093d399eb3eSPiotr Pawel Stefaniak (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1); 10948fae3551SRodney W. Grimes } 10958fae3551SRodney W. Grimes 10968fae3551SRodney W. Grimes /* 10978fae3551SRodney W. Grimes * pr_pack -- 10988fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 10998fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11008fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11018fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11028fae3551SRodney W. Grimes */ 110343470e3bSGarrett Wollman static void 1104d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11058fae3551SRodney W. Grimes { 11069b219646SPeter Wemm struct in_addr ina; 1107d9cacf60SAlan Somers u_char *cp, *dp, l; 1108d9cacf60SAlan Somers struct icmp icp; 1109d9cacf60SAlan Somers struct ip ip; 1110d9cacf60SAlan Somers const u_char *icmp_data_raw; 111146d7b45aSTom Jones ssize_t icmp_data_raw_len; 11128f975bb3SBruce Evans double triptime; 111346d7b45aSTom Jones int dupflag, i, j, recv_len; 111470960bb8SCy Schubert int8_t hlen; 11152c29d74cSAlan Somers uint16_t seq; 1116efc8588dSDavid E. O'Brien static int old_rrlen; 1117efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1118d9cacf60SAlan Somers struct ip oip; 1119d9cacf60SAlan Somers u_char oip_header_len; 1120d9cacf60SAlan Somers struct icmp oicmp; 11214630a325SJose Luis Duran const u_char *oicmp_raw; 1122d9cacf60SAlan Somers 1123d9cacf60SAlan Somers /* 112446d7b45aSTom Jones * Get size of IP header of the received packet. 112546d7b45aSTom Jones * The header length is contained in the lower four bits of the first 112646d7b45aSTom Jones * byte and represents the number of 4 byte octets the header takes up. 112746d7b45aSTom Jones * 112846d7b45aSTom Jones * The IHL minimum value is 5 (20 bytes) and its maximum value is 15 112946d7b45aSTom Jones * (60 bytes). 1130d9cacf60SAlan Somers */ 1131d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1132d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 11338fae3551SRodney W. Grimes 113446d7b45aSTom Jones /* Reject IP packets with a short header */ 113570960bb8SCy Schubert if (hlen < (int8_t) sizeof(struct ip)) { 113646d7b45aSTom Jones if (options & F_VERBOSE) 113746d7b45aSTom Jones warn("IHL too short (%d bytes) from %s", hlen, 113846d7b45aSTom Jones inet_ntoa(from->sin_addr)); 113946d7b45aSTom Jones return; 114046d7b45aSTom Jones } 114146d7b45aSTom Jones 114246d7b45aSTom Jones memcpy(&ip, buf, sizeof(struct ip)); 114346d7b45aSTom Jones 114446d7b45aSTom Jones /* Check packet has enough data to carry a valid ICMP header */ 1145e88178ddSMaxim Konovalov recv_len = cc; 11468fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11478fae3551SRodney W. Grimes if (options & F_VERBOSE) 1148d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 114943470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11508fae3551SRodney W. Grimes return; 11518fae3551SRodney W. Grimes } 11528fae3551SRodney W. Grimes 115346d7b45aSTom Jones icmp_data_raw_len = cc - (hlen + offsetof(struct icmp, icmp_data)); 1154d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1155d9cacf60SAlan Somers 11568fae3551SRodney W. Grimes /* Now the ICMP part */ 11578fae3551SRodney W. Grimes cc -= hlen; 1158d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1159d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1160d9cacf60SAlan Somers if (icp.icmp_id != ident) 11618fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11628fae3551SRodney W. Grimes ++nreceived; 11638f975bb3SBruce Evans triptime = 0.0; 11648fae3551SRodney W. Grimes if (timing) { 11651ad76f1bSAlan Somers struct timespec tv1; 116613e3f0b7SMaxim Konovalov struct tv32 tv32; 1167d9cacf60SAlan Somers const u_char *tp; 1168d9cacf60SAlan Somers 1169d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1170143008a1SMatthew N. Dodd 11717e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11727e9489e0SHiroki Sato sizeof(tv1)) { 11739d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 117413e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 117513e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 11761ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 11771ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1178039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 11791ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 1180ea6d1692SJose Luis Duran if (triptime < 0) { 1181ea6d1692SJose Luis Duran warnx("time of day goes back (%.3f ms)," 1182ea6d1692SJose Luis Duran " clamping time to 0", 1183ea6d1692SJose Luis Duran triptime); 1184ea6d1692SJose Luis Duran triptime = 0; 1185ea6d1692SJose Luis Duran } 11868fae3551SRodney W. Grimes tsum += triptime; 11873109a910SGarrett Wollman tsumsq += triptime * triptime; 11888fae3551SRodney W. Grimes if (triptime < tmin) 11898fae3551SRodney W. Grimes tmin = triptime; 11908fae3551SRodney W. Grimes if (triptime > tmax) 11918fae3551SRodney W. Grimes tmax = triptime; 119247e9b3eaSMatthew N. Dodd } else 119347e9b3eaSMatthew N. Dodd timing = 0; 11948fae3551SRodney W. Grimes } 11958fae3551SRodney W. Grimes 1196d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 11975db89bc7SBill Fenner 11985db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11998fae3551SRodney W. Grimes ++nrepeats; 12008fae3551SRodney W. Grimes --nreceived; 12018fae3551SRodney W. Grimes dupflag = 1; 12028fae3551SRodney W. Grimes } else { 12035db89bc7SBill Fenner SET(seq % mx_dup_ck); 12048fae3551SRodney W. Grimes dupflag = 0; 12058fae3551SRodney W. Grimes } 12068fae3551SRodney W. Grimes 12078fae3551SRodney W. Grimes if (options & F_QUIET) 12088fae3551SRodney W. Grimes return; 12098fae3551SRodney W. Grimes 1210d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1211d6cd1497SGleb Smirnoff ++nrcvtimeout; 1212d6cd1497SGleb Smirnoff return; 1213d6cd1497SGleb Smirnoff } 1214d6cd1497SGleb Smirnoff 1215d399eb3eSPiotr Pawel Stefaniak if (options & F_DOT) 12168fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12178fae3551SRodney W. Grimes else { 1218d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1219229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1220d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12218fae3551SRodney W. Grimes if (timing) 1222d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12238fae3551SRodney W. Grimes if (dupflag) 12248fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1225772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1226ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1227143008a1SMatthew N. Dodd if (options & F_MASK) { 1228143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1229143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1230d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1231143008a1SMatthew N. Dodd } 1232eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1233d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1234d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1235d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1236eb1543c6SMatthew N. Dodd } 1237e88178ddSMaxim Konovalov if (recv_len != send_len) { 1238e88178ddSMaxim Konovalov (void)printf( 1239e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1240e88178ddSMaxim Konovalov recv_len, send_len); 1241e88178ddSMaxim Konovalov } 12428fae3551SRodney W. Grimes /* check the data */ 1243d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1244d9cacf60SAlan Somers icmp_data) + phdr_len); 12450fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 124647e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 124729dccd6aSMaxim Konovalov i = 0; 124829dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 124929dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 125029dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 125129dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 125229dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 125329dccd6aSMaxim Konovalov } 125429dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12558fae3551SRodney W. Grimes if (*cp != *dp) { 12568fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12578fae3551SRodney W. Grimes i, *dp, *cp); 12581ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1259d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1260d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1261d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1262e88178ddSMaxim Konovalov if ((i % 16) == 8) 1263d32ff037SJohn Birrell (void)printf("\n\t"); 1264e88178ddSMaxim Konovalov (void)printf(" %2x", *cp); 1265d32ff037SJohn Birrell } 12661ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12670fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1268d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1269e88178ddSMaxim Konovalov if ((i % 16) == 8) 12708fae3551SRodney W. Grimes (void)printf("\n\t"); 1271e88178ddSMaxim Konovalov (void)printf(" %2x", *cp); 12728fae3551SRodney W. Grimes } 12738fae3551SRodney W. Grimes break; 12748fae3551SRodney W. Grimes } 12758fae3551SRodney W. Grimes } 12768fae3551SRodney W. Grimes } 12778fae3551SRodney W. Grimes } else { 1278ef9e6dc7SBill Fenner /* 1279ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1280ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1281ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1282ef9e6dc7SBill Fenner * and ICMP type and ID. 1283f78ac61bSWarner Losh * 1284f78ac61bSWarner Losh * Only print all the error messages if we are running 1285f78ac61bSWarner Losh * as root to avoid leaking information not normally 1286f78ac61bSWarner Losh * available to those not running as root. 1287ef9e6dc7SBill Fenner */ 128846d7b45aSTom Jones 128946d7b45aSTom Jones /* 129046d7b45aSTom Jones * If we don't have enough bytes for a quoted IP header and an 129146d7b45aSTom Jones * ICMP header then stop. 129246d7b45aSTom Jones */ 129346d7b45aSTom Jones if (icmp_data_raw_len < 129446d7b45aSTom Jones (ssize_t)(sizeof(struct ip) + sizeof(struct icmp))) { 129546d7b45aSTom Jones if (options & F_VERBOSE) 129646d7b45aSTom Jones warnx("quoted data too short (%zd bytes) from %s", 129746d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 129846d7b45aSTom Jones return; 129946d7b45aSTom Jones } 130046d7b45aSTom Jones 1301d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1302d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 130346d7b45aSTom Jones 130446d7b45aSTom Jones /* Reject IP packets with a short header */ 130546d7b45aSTom Jones if (oip_header_len < sizeof(struct ip)) { 130646d7b45aSTom Jones if (options & F_VERBOSE) 130746d7b45aSTom Jones warnx("inner IHL too short (%d bytes) from %s", 130846d7b45aSTom Jones oip_header_len, inet_ntoa(from->sin_addr)); 130946d7b45aSTom Jones return; 131046d7b45aSTom Jones } 131146d7b45aSTom Jones 131246d7b45aSTom Jones /* 131346d7b45aSTom Jones * Check against the actual IHL length, to protect against 131446d7b45aSTom Jones * quoated packets carrying IP options. 131546d7b45aSTom Jones */ 131646d7b45aSTom Jones if (icmp_data_raw_len < 131746d7b45aSTom Jones (ssize_t)(oip_header_len + sizeof(struct icmp))) { 131846d7b45aSTom Jones if (options & F_VERBOSE) 131946d7b45aSTom Jones warnx("inner packet too short (%zd bytes) from %s", 132046d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 132146d7b45aSTom Jones return; 132246d7b45aSTom Jones } 132346d7b45aSTom Jones 132446d7b45aSTom Jones memcpy(&oip, icmp_data_raw, sizeof(struct ip)); 13254630a325SJose Luis Duran oicmp_raw = icmp_data_raw + oip_header_len; 13264630a325SJose Luis Duran memcpy(&oicmp, oicmp_raw, sizeof(struct icmp)); 1327ef9e6dc7SBill Fenner 1328ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1329ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1330d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1331d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1332d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1333d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1334d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 133543470e3bSGarrett Wollman pr_addr(from->sin_addr)); 133620b41303SJose Luis Duran pr_icmph(&icp, &oip, icmp_data_raw); 1337ef9e6dc7SBill Fenner } else 1338ef9e6dc7SBill Fenner return; 13398fae3551SRodney W. Grimes } 13408fae3551SRodney W. Grimes 13418fae3551SRodney W. Grimes /* Display any IP options */ 13428fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 13438fae3551SRodney W. Grimes 13448fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 13458fae3551SRodney W. Grimes switch (*cp) { 13468fae3551SRodney W. Grimes case IPOPT_EOL: 13478fae3551SRodney W. Grimes hlen = 0; 13488fae3551SRodney W. Grimes break; 13498fae3551SRodney W. Grimes case IPOPT_LSRR: 1350cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1351cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1352cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1353301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13548fae3551SRodney W. Grimes hlen -= 2; 1355301207dfSMaxim Konovalov cp += 2; 13563c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13573c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13588fae3551SRodney W. Grimes for (;;) { 1359301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1360301207dfSMaxim Konovalov if (ina.s_addr == 0) 13611ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1362301207dfSMaxim Konovalov else 13631ad0b1beSMaxim Konovalov (void)printf("\t%s", 13641ad0b1beSMaxim Konovalov pr_addr(ina)); 1365301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1366301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1367301207dfSMaxim Konovalov j -= INADDR_LEN; 1368301207dfSMaxim Konovalov if (j < INADDR_LEN) 13698fae3551SRodney W. Grimes break; 13708fae3551SRodney W. Grimes (void)putchar('\n'); 13718fae3551SRodney W. Grimes } 1372301207dfSMaxim Konovalov } else 1373d2c9a140SJose Luis Duran (void)printf("\t(truncated route)"); 13748fae3551SRodney W. Grimes break; 13758fae3551SRodney W. Grimes case IPOPT_RR: 1376301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1377301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 13788fae3551SRodney W. Grimes hlen -= 2; 1379301207dfSMaxim Konovalov cp += 2; 13808fae3551SRodney W. Grimes if (i > j) 13818fae3551SRodney W. Grimes i = j; 1382301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1383301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1384301207dfSMaxim Konovalov old_rrlen = 0; 13858fae3551SRodney W. Grimes continue; 1386301207dfSMaxim Konovalov } 13878fae3551SRodney W. Grimes if (i == old_rrlen 13888fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 1389d399eb3eSPiotr Pawel Stefaniak && !(options & F_DOT)) { 13908fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13918fae3551SRodney W. Grimes hlen -= i; 13928fae3551SRodney W. Grimes cp += i; 13938fae3551SRodney W. Grimes break; 13948fae3551SRodney W. Grimes } 13958fae3551SRodney W. Grimes old_rrlen = i; 13968fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13978fae3551SRodney W. Grimes (void)printf("\nRR: "); 1398301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1399301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 14008fae3551SRodney W. Grimes for (;;) { 1401301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1402301207dfSMaxim Konovalov if (ina.s_addr == 0) 14031ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1404301207dfSMaxim Konovalov else 1405301207dfSMaxim Konovalov (void)printf("\t%s", 1406301207dfSMaxim Konovalov pr_addr(ina)); 1407301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1408301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1409301207dfSMaxim Konovalov i -= INADDR_LEN; 1410301207dfSMaxim Konovalov if (i < INADDR_LEN) 14118fae3551SRodney W. Grimes break; 14128fae3551SRodney W. Grimes (void)putchar('\n'); 14138fae3551SRodney W. Grimes } 1414301207dfSMaxim Konovalov } else 1415301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 14168fae3551SRodney W. Grimes break; 14178fae3551SRodney W. Grimes case IPOPT_NOP: 14188fae3551SRodney W. Grimes (void)printf("\nNOP"); 14198fae3551SRodney W. Grimes break; 14208fae3551SRodney W. Grimes default: 14218fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 14228fae3551SRodney W. Grimes break; 14238fae3551SRodney W. Grimes } 1424d399eb3eSPiotr Pawel Stefaniak if (!(options & F_DOT)) { 14258fae3551SRodney W. Grimes (void)putchar('\n'); 14268fae3551SRodney W. Grimes (void)fflush(stdout); 14278fae3551SRodney W. Grimes } 14288fae3551SRodney W. Grimes } 14298fae3551SRodney W. Grimes 14308fae3551SRodney W. Grimes /* 14318fae3551SRodney W. Grimes * pr_icmph -- 14328fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 14338fae3551SRodney W. Grimes */ 143443470e3bSGarrett Wollman static void 1435d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 14368fae3551SRodney W. Grimes { 1437efc8588dSDavid E. O'Brien 14388fae3551SRodney W. Grimes switch(icp->icmp_type) { 14398fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 14408fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 14418fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 14428fae3551SRodney W. Grimes break; 14438fae3551SRodney W. Grimes case ICMP_UNREACH: 14448fae3551SRodney W. Grimes switch(icp->icmp_code) { 14458fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 14468fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 14478fae3551SRodney W. Grimes break; 14488fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 14498fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 14508fae3551SRodney W. Grimes break; 14518fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 14528fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 14538fae3551SRodney W. Grimes break; 14548fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 14558fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 14568fae3551SRodney W. Grimes break; 14578fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1458ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1459ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 14608fae3551SRodney W. Grimes break; 14618fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 14628fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 14638fae3551SRodney W. Grimes break; 1464ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1465ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1466ef9e6dc7SBill Fenner break; 14678fae3551SRodney W. Grimes default: 14688fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 14698fae3551SRodney W. Grimes icp->icmp_code); 14708fae3551SRodney W. Grimes break; 14718fae3551SRodney W. Grimes } 14728fae3551SRodney W. Grimes /* Print returned IP header information */ 14731dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 14748fae3551SRodney W. Grimes break; 14758fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 14768fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 14771dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 14788fae3551SRodney W. Grimes break; 14798fae3551SRodney W. Grimes case ICMP_REDIRECT: 14808fae3551SRodney W. Grimes switch(icp->icmp_code) { 14818fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 14828fae3551SRodney W. Grimes (void)printf("Redirect Network"); 14838fae3551SRodney W. Grimes break; 14848fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 14858fae3551SRodney W. Grimes (void)printf("Redirect Host"); 14868fae3551SRodney W. Grimes break; 14878fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 14888fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 14898fae3551SRodney W. Grimes break; 14908fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 14918fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 14928fae3551SRodney W. Grimes break; 14938fae3551SRodney W. Grimes default: 14948fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 14958fae3551SRodney W. Grimes break; 14968fae3551SRodney W. Grimes } 1497ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 14981dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 14998fae3551SRodney W. Grimes break; 15008fae3551SRodney W. Grimes case ICMP_ECHO: 15018fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15028fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15038fae3551SRodney W. Grimes break; 15048fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15058fae3551SRodney W. Grimes switch(icp->icmp_code) { 15068fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15078fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15088fae3551SRodney W. Grimes break; 15098fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15108fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15118fae3551SRodney W. Grimes break; 15128fae3551SRodney W. Grimes default: 15138fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15148fae3551SRodney W. Grimes icp->icmp_code); 15158fae3551SRodney W. Grimes break; 15168fae3551SRodney W. Grimes } 15171dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15188fae3551SRodney W. Grimes break; 15198fae3551SRodney W. Grimes case ICMP_PARAMPROB: 15208fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 15218fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 15221dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15238fae3551SRodney W. Grimes break; 15248fae3551SRodney W. Grimes case ICMP_TSTAMP: 15258fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 15268fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15278fae3551SRodney W. Grimes break; 15288fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 15298fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 15308fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15318fae3551SRodney W. Grimes break; 15328fae3551SRodney W. Grimes case ICMP_IREQ: 15338fae3551SRodney W. Grimes (void)printf("Information Request\n"); 15348fae3551SRodney W. Grimes /* XXX ID + Seq */ 15358fae3551SRodney W. Grimes break; 15368fae3551SRodney W. Grimes case ICMP_IREQREPLY: 15378fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 15388fae3551SRodney W. Grimes /* XXX ID + Seq */ 15398fae3551SRodney W. Grimes break; 15408fae3551SRodney W. Grimes case ICMP_MASKREQ: 15418fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 15428fae3551SRodney W. Grimes break; 15438fae3551SRodney W. Grimes case ICMP_MASKREPLY: 15448fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 15458fae3551SRodney W. Grimes break; 1546ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1547ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1548ef9e6dc7SBill Fenner break; 1549ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1550ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1551ef9e6dc7SBill Fenner break; 15528fae3551SRodney W. Grimes default: 15538fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 15548fae3551SRodney W. Grimes } 15558fae3551SRodney W. Grimes } 15568fae3551SRodney W. Grimes 15578fae3551SRodney W. Grimes /* 15588fae3551SRodney W. Grimes * pr_iph -- 15598fae3551SRodney W. Grimes * Print an IP header with options. 15608fae3551SRodney W. Grimes */ 156143470e3bSGarrett Wollman static void 156220b41303SJose Luis Duran pr_iph(struct ip *ip, const u_char *cp) 15638fae3551SRodney W. Grimes { 1564*b86e4812SJose Luis Duran struct in_addr dst_ina, src_ina; 1565efc8588dSDavid E. O'Brien int hlen; 15668fae3551SRodney W. Grimes 15678fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 156820b41303SJose Luis Duran cp = cp + sizeof(struct ip); /* point to options */ 15698fae3551SRodney W. Grimes 1570*b86e4812SJose Luis Duran memcpy(&src_ina, &ip->ip_src.s_addr, sizeof(src_ina)); 1571*b86e4812SJose Luis Duran memcpy(&dst_ina, &ip->ip_dst.s_addr, sizeof(dst_ina)); 1572*b86e4812SJose Luis Duran 1573*b86e4812SJose Luis Duran (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks %*s %*s", 1574*b86e4812SJose Luis Duran (int)strlen(inet_ntoa(src_ina)), "Src", 1575*b86e4812SJose Luis Duran (int)strlen(inet_ntoa(dst_ina)), "Dst"); 1576*b86e4812SJose Luis Duran if (hlen > (int)sizeof(struct ip)) 1577*b86e4812SJose Luis Duran (void)printf(" Opts"); 1578*b86e4812SJose Luis Duran (void)putchar('\n'); 15798fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1580ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1581ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 15829185854dSJose Luis Duran (void)printf(" %1x %04x", 15839185854dSJose Luis Duran (ntohs(ip->ip_off) & 0xe000) >> 13, 15849185854dSJose Luis Duran ntohs(ip->ip_off) & 0x1fff); 1585ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1586ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1587*b86e4812SJose Luis Duran (void)printf(" %s", inet_ntoa(src_ina)); 1588*b86e4812SJose Luis Duran (void)printf(" %s", inet_ntoa(dst_ina)); 1589ef9e6dc7SBill Fenner /* dump any option bytes */ 1590*b86e4812SJose Luis Duran if (hlen > (int)sizeof(struct ip)) { 1591*b86e4812SJose Luis Duran (void)printf(" "); 1592491263d7SJose Luis Duran while (hlen-- > (int)sizeof(struct ip)) { 15938fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 15948fae3551SRodney W. Grimes } 1595*b86e4812SJose Luis Duran } 15968fae3551SRodney W. Grimes (void)putchar('\n'); 15978fae3551SRodney W. Grimes } 15988fae3551SRodney W. Grimes 15998fae3551SRodney W. Grimes /* 16008fae3551SRodney W. Grimes * pr_addr -- 16018fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16028fae3551SRodney W. Grimes * a hostname. 16038fae3551SRodney W. Grimes */ 160443470e3bSGarrett Wollman static char * 1605fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16068fae3551SRodney W. Grimes { 16078fae3551SRodney W. Grimes struct hostent *hp; 1608f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16098fae3551SRodney W. Grimes 161003d4d1c7SJose Luis Duran if (!(options & F_HOSTNAME)) 161143470e3bSGarrett Wollman return inet_ntoa(ina); 161249133c6dSPawel Jakub Dawidek 1613491263d7SJose Luis Duran hp = cap_gethostbyaddr(capdns, (char *)&ina, sizeof(ina), AF_INET); 161449133c6dSPawel Jakub Dawidek 161549133c6dSPawel Jakub Dawidek if (hp == NULL) 161649133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 161749133c6dSPawel Jakub Dawidek 1618efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 161943470e3bSGarrett Wollman inet_ntoa(ina)); 16208fae3551SRodney W. Grimes return(buf); 16218fae3551SRodney W. Grimes } 16228fae3551SRodney W. Grimes 1623eb1543c6SMatthew N. Dodd static char * 1624007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1625eb1543c6SMatthew N. Dodd { 16267898770aSAlan Somers static char buf[11]; 1627007fe4e3SMaxim Konovalov int hour, min, sec; 1628eb1543c6SMatthew N. Dodd 1629007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1630007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1631007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1632007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1633eb1543c6SMatthew N. Dodd 1634007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1635eb1543c6SMatthew N. Dodd 1636eb1543c6SMatthew N. Dodd return (buf); 1637eb1543c6SMatthew N. Dodd } 1638eb1543c6SMatthew N. Dodd 163943470e3bSGarrett Wollman static void 1640fafb8f11SEd Schouten fill(char *bp, char *patp) 16418fae3551SRodney W. Grimes { 16428fae3551SRodney W. Grimes char *cp; 1643efc8588dSDavid E. O'Brien int pat[16]; 16444fba6582SMaxim Konovalov u_int ii, jj, kk; 16458fae3551SRodney W. Grimes 164643470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 164743470e3bSGarrett Wollman if (!isxdigit(*cp)) 164843470e3bSGarrett Wollman errx(EX_USAGE, 164943470e3bSGarrett Wollman "patterns must be specified as hex digits"); 165043470e3bSGarrett Wollman 16518fae3551SRodney W. Grimes } 16528fae3551SRodney W. Grimes ii = sscanf(patp, 16538fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 16548fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 16558fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 16568fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 16578fae3551SRodney W. Grimes 16588fae3551SRodney W. Grimes if (ii > 0) 1659d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 16608fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 16618fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 16628fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 16638fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 16648fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 16658fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 16668fae3551SRodney W. Grimes (void)printf("\n"); 16678fae3551SRodney W. Grimes } 16688fae3551SRodney W. Grimes } 16698fae3551SRodney W. Grimes 167049133c6dSPawel Jakub Dawidek static cap_channel_t * 167149133c6dSPawel Jakub Dawidek capdns_setup(void) 167249133c6dSPawel Jakub Dawidek { 167349133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1674a3ce7698SAlan Somers #ifdef WITH_CASPER 167549133c6dSPawel Jakub Dawidek const char *types[2]; 167649133c6dSPawel Jakub Dawidek int families[1]; 1677a3ce7698SAlan Somers #endif 167849133c6dSPawel Jakub Dawidek capcas = cap_init(); 1679c501d73cSMariusz Zaborski if (capcas == NULL) 1680c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 168149133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 168249133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 168349133c6dSPawel Jakub Dawidek cap_close(capcas); 168449133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 168549133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1686a3ce7698SAlan Somers #ifdef WITH_CASPER 1687752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1688752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 168949133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 169049133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 169149133c6dSPawel Jakub Dawidek families[0] = AF_INET; 169249133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 169349133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1694a3ce7698SAlan Somers #endif 169549133c6dSPawel Jakub Dawidek return (capdnsloc); 169649133c6dSPawel Jakub Dawidek } 1697