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