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