1/* $KAME: traceroute6.c,v 1.68 2004/01/25 11:16:12 suz Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1990, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Van Jacobson. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * 4. 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#include <sys/cdefs.h> 69 70#ifndef lint 71__unused static char copyright[] = 72"@(#) Copyright (c) 1990, 1993\n\ 73 The Regents of the University of California. All rights reserved.\n"; 74#endif /* not lint */ 75 76/* 77 * traceroute host - trace the route ip packets follow going to "host". 78 * 79 * Attempt to trace the route an ip packet would follow to some 80 * internet host. We find out intermediate hops by launching probe 81 * packets with a small ttl (time to live) then listening for an 82 * icmp "time exceeded" reply from a gateway. We start our probes 83 * with a ttl of one and increase by one until we get an icmp "port 84 * unreachable" (which means we got to "host") or hit a max (which 85 * defaults to 30 hops & can be changed with the -m flag). Three 86 * probes (change with -q flag) are sent at each ttl setting and a 87 * line is printed showing the ttl, address of the gateway and 88 * round trip time of each probe. If the probe answers come from 89 * different gateways, the address of each responding system will 90 * be printed. If there is no response within a 5 sec. timeout 91 * interval (changed with the -w flag), a "*" is printed for that 92 * probe. 93 * 94 * Probe packets are UDP format. We don't want the destination 95 * host to process them so the destination port is set to an 96 * unlikely value (if some clod on the destination is using that 97 * value, it can be changed with the -p flag). 98 * 99 * A sample use might be: 100 * 101 * [yak 71]% traceroute nis.nsf.net. 102 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet 103 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms 104 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms 105 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms 106 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms 107 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms 108 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms 109 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms 110 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms 111 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms 112 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms 113 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms 114 * 115 * Note that lines 2 & 3 are the same. This is due to a buggy 116 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards 117 * packets with a zero ttl. 118 * 119 * A more interesting example is: 120 * 121 * [yak 72]% traceroute allspice.lcs.mit.edu. 122 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max 123 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms 124 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms 125 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms 126 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms 127 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms 128 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms 129 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms 130 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms 131 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms 132 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms 133 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms 134 * 12 * * * 135 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms 136 * 14 * * * 137 * 15 * * * 138 * 16 * * * 139 * 17 * * * 140 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms 141 * 142 * (I start to see why I'm having so much trouble with mail to 143 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away 144 * either don't send ICMP "time exceeded" messages or send them 145 * with a ttl too small to reach us. 14 - 17 are running the 146 * MIT C Gateway code that doesn't send "time exceeded"s. God 147 * only knows what's going on with 12. 148 * 149 * The silent gateway 12 in the above may be the result of a bug in 150 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3) 151 * sends an unreachable message using whatever ttl remains in the 152 * original datagram. Since, for gateways, the remaining ttl is 153 * zero, the icmp "time exceeded" is guaranteed to not make it back 154 * to us. The behavior of this bug is slightly more interesting 155 * when it appears on the destination system: 156 * 157 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms 158 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms 159 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms 160 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms 161 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms 162 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms 163 * 7 * * * 164 * 8 * * * 165 * 9 * * * 166 * 10 * * * 167 * 11 * * * 168 * 12 * * * 169 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms ! 170 * 171 * Notice that there are 12 "gateways" (13 is the final 172 * destination) and exactly the last half of them are "missing". 173 * What's really happening is that rip (a Sun-3 running Sun OS3.5) 174 * is using the ttl from our arriving datagram as the ttl in its 175 * icmp reply. So, the reply will time out on the return path 176 * (with no notice sent to anyone since icmp's aren't sent for 177 * icmp's) until we probe with a ttl that's at least twice the path 178 * length. I.e., rip is really only 7 hops away. A reply that 179 * returns with a ttl of 1 is a clue this problem exists. 180 * Traceroute prints a "!" after the time if the ttl is <= 1. 181 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or 182 * non-standard (HPUX) software, expect to see this problem 183 * frequently and/or take care picking the target host of your 184 * probes. 185 * 186 * Other possible annotations after the time are !H, !N, !P (got a host, 187 * network or protocol unreachable, respectively), !S or !F (source 188 * route failed or fragmentation needed -- neither of these should 189 * ever occur and the associated gateway is busted if you see one). If 190 * almost all the probes result in some kind of unreachable, traceroute 191 * will give up and exit. 192 * 193 * Notes 194 * ----- 195 * This program must be run by root or be setuid. (I suggest that 196 * you *don't* make it setuid -- casual use could result in a lot 197 * of unnecessary traffic on our poor, congested nets.) 198 * 199 * This program requires a kernel mod that does not appear in any 200 * system available from Berkeley: A raw ip socket using proto 201 * IPPROTO_RAW must interpret the data sent as an ip datagram (as 202 * opposed to data to be wrapped in an ip datagram). See the README 203 * file that came with the source to this program for a description 204 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may 205 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE 206 * MODIFIED TO RUN THIS PROGRAM. 207 * 208 * The udp port usage may appear bizarre (well, ok, it is bizarre). 209 * The problem is that an icmp message only contains 8 bytes of 210 * data from the original datagram. 8 bytes is the size of a udp 211 * header so, if we want to associate replies with the original 212 * datagram, the necessary information must be encoded into the 213 * udp header (the ip id could be used but there's no way to 214 * interlock with the kernel's assignment of ip id's and, anyway, 215 * it would have taken a lot more kernel hacking to allow this 216 * code to set the ip id). So, to allow two or more users to 217 * use traceroute simultaneously, we use this task's pid as the 218 * source port (the high bit is set to move the port number out 219 * of the "likely" range). To keep track of which probe is being 220 * replied to (so times and/or hop counts don't get confused by a 221 * reply that was delayed in transit), we increment the destination 222 * port number before each probe. 223 * 224 * Don't use this as a coding example. I was trying to find a 225 * routing problem and this code sort-of popped out after 48 hours 226 * without sleep. I was amazed it ever compiled, much less ran. 227 * 228 * I stole the idea for this program from Steve Deering. Since 229 * the first release, I've learned that had I attended the right 230 * IETF working group meetings, I also could have stolen it from Guy 231 * Almes or Matt Mathis. I don't know (or care) who came up with 232 * the idea first. I envy the originators' perspicacity and I'm 233 * glad they didn't keep the idea a secret. 234 * 235 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or 236 * enhancements to the original distribution. 237 * 238 * I've hacked up a round-trip-route version of this that works by 239 * sending a loose-source-routed udp datagram through the destination 240 * back to yourself. Unfortunately, SO many gateways botch source 241 * routing, the thing is almost worthless. Maybe one day... 242 * 243 * -- Van Jacobson (van@helios.ee.lbl.gov) 244 * Tue Dec 20 03:50:13 PST 1988 245 */ 246 247#include <sys/param.h> 248#include <sys/time.h> 249#include <sys/socket.h> 250#include <sys/uio.h> 251#include <sys/file.h> 252#include <sys/ioctl.h> 253#include <sys/sysctl.h> 254 255#include <netinet/in.h> 256 257#include <arpa/inet.h> 258 259#include <netdb.h> 260#include <stdio.h> 261#include <err.h> 262#ifdef HAVE_POLL 263#include <poll.h> 264#endif 265#include <errno.h> 266#include <stdlib.h> 267#include <string.h> 268#include <unistd.h> 269 270#include <netinet/ip6.h> 271#include <netinet/icmp6.h> 272#include <netinet/udp.h> 273 274#ifdef IPSEC 275#include <net/route.h> 276#include <netinet6/ipsec.h> 277#endif 278 279#define DUMMY_PORT 10010 280 281#define MAXPACKET 65535 /* max ip packet size */ 282 283#ifndef HAVE_GETIPNODEBYNAME 284#define getipnodebyname(x, y, z, u) gethostbyname2((x), (y)) 285#define freehostent(x) 286#endif 287 288/* 289 * format of a (udp) probe packet. 290 */ 291struct tv32 { 292 u_int32_t tv32_sec; 293 u_int32_t tv32_usec; 294}; 295 296struct opacket { 297 u_char seq; /* sequence number of this packet */ 298 u_char hops; /* hop limit of the packet */ 299 u_char pad[2]; 300 struct tv32 tv; /* time packet left */ 301} __attribute__((__packed__)); 302 303u_char packet[512]; /* last inbound (icmp) packet */ 304struct opacket *outpacket; /* last output (udp) packet */ 305 306int main(int, char *[]); 307int wait_for_reply(int, struct msghdr *); 308#ifdef IPSEC 309#ifdef IPSEC_POLICY_IPSEC 310int setpolicy(int so, char *policy); 311#endif 312#endif 313void send_probe(int, u_long); 314void *get_uphdr(struct ip6_hdr *, u_char *); 315int get_hoplim(struct msghdr *); 316double deltaT(struct timeval *, struct timeval *); 317char *pr_type(int); 318int packet_ok(struct msghdr *, int, int); 319void print(struct msghdr *, int); 320const char *inetname(struct sockaddr *); 321void usage(void); 322 323int rcvsock; /* receive (icmp) socket file descriptor */ 324int sndsock; /* send (udp) socket file descriptor */ 325 326struct msghdr rcvmhdr; 327struct iovec rcviov[2]; 328int rcvhlim; 329struct in6_pktinfo *rcvpktinfo; 330 331struct sockaddr_in6 Src, Dst, Rcv; 332u_long datalen; /* How much data */ 333#define ICMP6ECHOLEN 8 334/* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */ 335char rtbuf[2064]; 336#ifdef USE_RFC2292BIS 337struct ip6_rthdr *rth; 338#endif 339struct cmsghdr *cmsg; 340 341char *source = 0; 342char *hostname; 343 344u_long nprobes = 3; 345u_long first_hop = 1; 346u_long max_hops = 30; 347u_int16_t srcport; 348u_int16_t port = 32768+666; /* start udp dest port # for probe packets */ 349u_int16_t ident; 350int options; /* socket options */ 351int verbose; 352int waittime = 5; /* time to wait for response (in seconds) */ 353int nflag; /* print addresses numerically */ 354int useproto = IPPROTO_UDP; /* protocol to use to send packet */ 355int lflag; /* print both numerical address & hostname */ 356 357int 358main(argc, argv) 359 int argc; 360 char *argv[]; 361{ 362 int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM }; 363 char hbuf[NI_MAXHOST], src0[NI_MAXHOST], *ep; 364 int ch, i, on = 1, seq, rcvcmsglen, error, minlen; 365 struct addrinfo hints, *res; 366 static u_char *rcvcmsgbuf; 367 u_long probe, hops, lport; 368 struct hostent *hp; 369 size_t size; 370 371 /* 372 * Receive ICMP 373 */ 374 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 375 perror("socket(ICMPv6)"); 376 exit(5); 377 } 378 379 size = sizeof(i); 380 (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &i, &size, NULL, 0); 381 max_hops = i; 382 383 /* specify to tell receiving interface */ 384#ifdef IPV6_RECVPKTINFO 385 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 386 sizeof(on)) < 0) 387 err(1, "setsockopt(IPV6_RECVPKTINFO)"); 388#else /* old adv. API */ 389 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 390 sizeof(on)) < 0) 391 err(1, "setsockopt(IPV6_PKTINFO)"); 392#endif 393 394 /* specify to tell value of hoplimit field of received IP6 hdr */ 395#ifdef IPV6_RECVHOPLIMIT 396 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, 397 sizeof(on)) < 0) 398 err(1, "setsockopt(IPV6_RECVHOPLIMIT)"); 399#else /* old adv. API */ 400 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 401 sizeof(on)) < 0) 402 err(1, "setsockopt(IPV6_HOPLIMIT)"); 403#endif 404 405 seq = 0; 406 407 while ((ch = getopt(argc, argv, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1) 408 switch (ch) { 409 case 'd': 410 options |= SO_DEBUG; 411 break; 412 case 'f': 413 ep = NULL; 414 errno = 0; 415 first_hop = strtoul(optarg, &ep, 0); 416 if (errno || !*optarg || *ep || first_hop > 255) { 417 fprintf(stderr, 418 "traceroute6: invalid min hoplimit.\n"); 419 exit(1); 420 } 421 break; 422 case 'g': 423 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno); 424 if (hp == NULL) { 425 fprintf(stderr, 426 "traceroute6: unknown host %s\n", optarg); 427 exit(1); 428 } 429#ifdef USE_RFC2292BIS 430 if (rth == NULL) { 431 /* 432 * XXX: We can't detect the number of 433 * intermediate nodes yet. 434 */ 435 if ((rth = inet6_rth_init((void *)rtbuf, 436 sizeof(rtbuf), IPV6_RTHDR_TYPE_0, 437 0)) == NULL) { 438 fprintf(stderr, 439 "inet6_rth_init failed.\n"); 440 exit(1); 441 } 442 } 443 if (inet6_rth_add((void *)rth, 444 (struct in6_addr *)hp->h_addr)) { 445 fprintf(stderr, 446 "inet6_rth_add failed for %s\n", 447 optarg); 448 exit(1); 449 } 450#else /* old advanced API */ 451 if (cmsg == NULL) 452 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0); 453 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, 454 IPV6_RTHDR_LOOSE); 455#endif 456 freehostent(hp); 457 break; 458 case 'I': 459 useproto = IPPROTO_ICMPV6; 460 ident = htons(getpid() & 0xffff); /* same as ping6 */ 461 break; 462 case 'l': 463 lflag++; 464 break; 465 case 'm': 466 ep = NULL; 467 errno = 0; 468 max_hops = strtoul(optarg, &ep, 0); 469 if (errno || !*optarg || *ep || max_hops > 255) { 470 fprintf(stderr, 471 "traceroute6: invalid max hoplimit.\n"); 472 exit(1); 473 } 474 break; 475 case 'n': 476 nflag++; 477 break; 478 case 'N': 479 useproto = IPPROTO_NONE; 480 break; 481 case 'p': 482 ep = NULL; 483 errno = 0; 484 lport = strtoul(optarg, &ep, 0); 485 if (errno || !*optarg || *ep) { 486 fprintf(stderr, "traceroute6: invalid port.\n"); 487 exit(1); 488 } 489 if (lport == 0 || lport != (lport & 0xffff)) { 490 fprintf(stderr, 491 "traceroute6: port out of range.\n"); 492 exit(1); 493 } 494 port = lport & 0xffff; 495 break; 496 case 'q': 497 ep = NULL; 498 errno = 0; 499 nprobes = strtoul(optarg, &ep, 0); 500 if (errno || !*optarg || *ep) { 501 fprintf(stderr, 502 "traceroute6: invalid nprobes.\n"); 503 exit(1); 504 } 505 if (nprobes < 1) { 506 fprintf(stderr, 507 "traceroute6: nprobes must be >0.\n"); 508 exit(1); 509 } 510 break; 511 case 'r': 512 options |= SO_DONTROUTE; 513 break; 514 case 's': 515 /* 516 * set the ip source address of the outbound 517 * probe (e.g., on a multi-homed host). 518 */ 519 source = optarg; 520 break; 521 case 'v': 522 verbose++; 523 break; 524 case 'U': 525 useproto = IPPROTO_UDP; 526 break; 527 case 'w': 528 ep = NULL; 529 errno = 0; 530 waittime = strtoul(optarg, &ep, 0); 531 if (errno || !*optarg || *ep) { 532 fprintf(stderr, 533 "traceroute6: invalid wait time.\n"); 534 exit(1); 535 } 536 if (waittime < 1) { 537 fprintf(stderr, 538 "traceroute6: wait must be >= 1 sec.\n"); 539 exit(1); 540 } 541 break; 542 default: 543 usage(); 544 } 545 argc -= optind; 546 argv += optind; 547 548 /* 549 * Open socket to send probe packets. 550 */ 551 switch (useproto) { 552 case IPPROTO_ICMPV6: 553 sndsock = rcvsock; 554 break; 555 case IPPROTO_UDP: 556 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 557 perror("socket(SOCK_DGRAM)"); 558 exit(5); 559 } 560 break; 561 case IPPROTO_NONE: 562 if ((sndsock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) { 563 perror("socket(SOCK_RAW)"); 564 exit(5); 565 } 566 break; 567 default: 568 fprintf(stderr, "traceroute6: unknown probe protocol %d", 569 useproto); 570 exit(5); 571 } 572 if (max_hops < first_hop) { 573 fprintf(stderr, 574 "traceroute6: max hoplimit must be larger than first hoplimit.\n"); 575 exit(1); 576 } 577 578 /* revoke privs */ 579 seteuid(getuid()); 580 setuid(getuid()); 581 582 if (argc < 1 || argc > 2) 583 usage(); 584 585#if 1 586 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 587#else 588 setlinebuf(stdout); 589#endif 590 591 memset(&hints, 0, sizeof(hints)); 592 hints.ai_family = PF_INET6; 593 hints.ai_socktype = SOCK_RAW; 594 hints.ai_protocol = IPPROTO_ICMPV6; 595 hints.ai_flags = AI_CANONNAME; 596 error = getaddrinfo(*argv, NULL, &hints, &res); 597 if (error) { 598 fprintf(stderr, 599 "traceroute6: %s\n", gai_strerror(error)); 600 exit(1); 601 } 602 if (res->ai_addrlen != sizeof(Dst)) { 603 fprintf(stderr, 604 "traceroute6: size of sockaddr mismatch\n"); 605 exit(1); 606 } 607 memcpy(&Dst, res->ai_addr, res->ai_addrlen); 608 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv; 609 if (!hostname) { 610 fprintf(stderr, "traceroute6: not enough core\n"); 611 exit(1); 612 } 613 if (res->ai_next) { 614 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, 615 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 616 strlcpy(hbuf, "?", sizeof(hbuf)); 617 fprintf(stderr, "traceroute6: Warning: %s has multiple " 618 "addresses; using %s\n", hostname, hbuf); 619 } 620 621 if (*++argv) { 622 ep = NULL; 623 errno = 0; 624 datalen = strtoul(*argv, &ep, 0); 625 if (errno || !*argv || *ep) { 626 fprintf(stderr, 627 "traceroute6: invalid packet length.\n"); 628 exit(1); 629 } 630 } 631 switch (useproto) { 632 case IPPROTO_ICMPV6: 633 minlen = ICMP6ECHOLEN + sizeof(struct tv32); 634 break; 635 case IPPROTO_UDP: 636 minlen = sizeof(struct opacket); 637 break; 638 case IPPROTO_NONE: 639 minlen = 0; 640 datalen = 0; 641 break; 642 default: 643 fprintf(stderr, "traceroute6: unknown probe protocol %d.\n", 644 useproto); 645 exit(1); 646 } 647 if (datalen < minlen) 648 datalen = minlen; 649 else if (datalen >= MAXPACKET) { 650 fprintf(stderr, 651 "traceroute6: packet size must be %d <= s < %ld.\n", 652 minlen, (long)MAXPACKET); 653 exit(1); 654 } 655 outpacket = (struct opacket *)malloc((unsigned)datalen); 656 if (!outpacket) { 657 perror("malloc"); 658 exit(1); 659 } 660 (void) bzero((char *)outpacket, datalen); 661 662 /* initialize msghdr for receiving packets */ 663 rcviov[0].iov_base = (caddr_t)packet; 664 rcviov[0].iov_len = sizeof(packet); 665 rcvmhdr.msg_name = (caddr_t)&Rcv; 666 rcvmhdr.msg_namelen = sizeof(Rcv); 667 rcvmhdr.msg_iov = rcviov; 668 rcvmhdr.msg_iovlen = 1; 669 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 670 CMSG_SPACE(sizeof(int)); 671 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) { 672 fprintf(stderr, "traceroute6: malloc failed\n"); 673 exit(1); 674 } 675 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 676 rcvmhdr.msg_controllen = rcvcmsglen; 677 678 (void) setsockopt(rcvsock, SOL_SOCKET, SO_RECV_ANYIF, (char *)&on, 679 sizeof(on)); 680 if (options & SO_DEBUG) 681 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, 682 (char *)&on, sizeof(on)); 683 if (options & SO_DONTROUTE) 684 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, 685 (char *)&on, sizeof(on)); 686#ifdef IPSEC 687#ifdef IPSEC_POLICY_IPSEC 688 /* 689 * do not raise error even if setsockopt fails, kernel may have ipsec 690 * turned off. 691 */ 692 if (setpolicy(rcvsock, "in bypass") < 0) 693 errx(1, "%s", ipsec_strerror()); 694 if (setpolicy(rcvsock, "out bypass") < 0) 695 errx(1, "%s", ipsec_strerror()); 696#else 697 { 698 int level = IPSEC_LEVEL_NONE; 699 700 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level, 701 sizeof(level)); 702 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level, 703 sizeof(level)); 704#ifdef IP_AUTH_TRANS_LEVEL 705 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level, 706 sizeof(level)); 707#else 708 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level, 709 sizeof(level)); 710#endif 711#ifdef IP_AUTH_NETWORK_LEVEL 712 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level, 713 sizeof(level)); 714#endif 715 } 716#endif /*IPSEC_POLICY_IPSEC*/ 717#endif /*IPSEC*/ 718 719#ifdef SO_SNDBUF 720 i = datalen; 721 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&i, 722 sizeof(i)) < 0 && useproto != IPPROTO_NONE) { 723 perror("setsockopt(SO_SNDBUF)"); 724 exit(6); 725 } 726#endif /* SO_SNDBUF */ 727 if (options & SO_DEBUG) 728 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, 729 (char *)&on, sizeof(on)); 730 if (options & SO_DONTROUTE) 731 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, 732 (char *)&on, sizeof(on)); 733#ifdef USE_RFC2292BIS 734 if (rth) {/* XXX: there is no library to finalize the header... */ 735 rth->ip6r_len = rth->ip6r_segleft * 2; 736 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR, 737 (void *)rth, (rth->ip6r_len + 1) << 3)) { 738 fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n", 739 strerror(errno)); 740 exit(1); 741 } 742 } 743#else /* old advanced API */ 744 if (cmsg != NULL) { 745 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE); 746 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS, 747 rtbuf, cmsg->cmsg_len) < 0) { 748 fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n", 749 strerror(errno)); 750 exit(1); 751 } 752 } 753#endif /* USE_RFC2292BIS */ 754#ifdef IPSEC 755#ifdef IPSEC_POLICY_IPSEC 756 /* 757 * do not raise error even if setsockopt fails, kernel may have ipsec 758 * turned off. 759 */ 760 if (setpolicy(sndsock, "in bypass") < 0) 761 errx(1, "%s", ipsec_strerror()); 762 if (setpolicy(sndsock, "out bypass") < 0) 763 errx(1, "%s", ipsec_strerror()); 764#else 765 { 766 int level = IPSEC_LEVEL_BYPASS; 767 768 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level, 769 sizeof(level)); 770 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level, 771 sizeof(level)); 772#ifdef IP_AUTH_TRANS_LEVEL 773 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level, 774 sizeof(level)); 775#else 776 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level, 777 sizeof(level)); 778#endif 779#ifdef IP_AUTH_NETWORK_LEVEL 780 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level, 781 sizeof(level)); 782#endif 783 } 784#endif /*IPSEC_POLICY_IPSEC*/ 785#endif /*IPSEC*/ 786 787 /* 788 * Source selection 789 */ 790 bzero(&Src, sizeof(Src)); 791 if (source) { 792 struct addrinfo hints, *res; 793 int error; 794 795 memset(&hints, 0, sizeof(hints)); 796 hints.ai_family = AF_INET6; 797 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 798 hints.ai_flags = AI_NUMERICHOST; 799 error = getaddrinfo(source, "0", &hints, &res); 800 if (error) { 801 printf("traceroute6: %s: %s\n", source, 802 gai_strerror(error)); 803 exit(1); 804 } 805 if (res->ai_addrlen > sizeof(Src)) { 806 printf("traceroute6: %s: %s\n", source, 807 gai_strerror(error)); 808 exit(1); 809 } 810 memcpy(&Src, res->ai_addr, res->ai_addrlen); 811 freeaddrinfo(res); 812 } else { 813 struct sockaddr_in6 Nxt; 814 int dummy; 815 socklen_t len; 816 817 Nxt = Dst; 818 Nxt.sin6_port = htons(DUMMY_PORT); 819 if (cmsg != NULL) 820 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr, 821 sizeof(Nxt.sin6_addr)); 822 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 823 perror("socket"); 824 exit(1); 825 } 826 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) { 827 perror("connect"); 828 exit(1); 829 } 830 len = sizeof(Src); 831 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) { 832 perror("getsockname"); 833 exit(1); 834 } 835 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len, 836 src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) { 837 fprintf(stderr, "getnameinfo failed for source\n"); 838 exit(1); 839 } 840 source = src0; 841 close(dummy); 842 } 843 844 Src.sin6_port = htons(0); 845 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) { 846 perror("bind"); 847 exit(1); 848 } 849 850 { 851 socklen_t len; 852 853 len = sizeof(Src); 854 if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) { 855 perror("getsockname"); 856 exit(1); 857 } 858 srcport = ntohs(Src.sin6_port); 859 } 860 861 /* 862 * Message to users 863 */ 864 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf, 865 sizeof(hbuf), NULL, 0, NI_NUMERICHOST)) 866 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 867 fprintf(stderr, "traceroute6"); 868 fprintf(stderr, " to %s (%s)", hostname, hbuf); 869 if (source) 870 fprintf(stderr, " from %s", source); 871 fprintf(stderr, ", %lu hops max, %lu byte packets\n", 872 max_hops, datalen); 873 (void) fflush(stderr); 874 875 if (first_hop > 1) 876 printf("Skipping %lu intermediate hops\n", first_hop - 1); 877 878 /* 879 * Main loop 880 */ 881 for (hops = first_hop; hops <= max_hops; ++hops) { 882 struct in6_addr lastaddr; 883 int got_there = 0; 884 int unreachable = 0; 885 886 printf("%2lu ", hops); 887 bzero(&lastaddr, sizeof(lastaddr)); 888 for (probe = 0; probe < nprobes; ++probe) { 889 int cc; 890 struct timeval t1, t2; 891 892 (void) gettimeofday(&t1, NULL); 893 send_probe(++seq, hops); 894 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { 895 (void) gettimeofday(&t2, NULL); 896 if ((i = packet_ok(&rcvmhdr, cc, seq))) { 897 if (!IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr, 898 &lastaddr)) { 899 if (probe > 0) 900 fputs("\n ", stdout); 901 print(&rcvmhdr, cc); 902 lastaddr = Rcv.sin6_addr; 903 } 904 printf(" %.3f ms", deltaT(&t1, &t2)); 905 switch (i - 1) { 906 case ICMP6_DST_UNREACH_NOROUTE: 907 ++unreachable; 908 printf(" !N"); 909 break; 910 case ICMP6_DST_UNREACH_ADMIN: 911 ++unreachable; 912 printf(" !P"); 913 break; 914 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 915 ++unreachable; 916 printf(" !S"); 917 break; 918 case ICMP6_DST_UNREACH_ADDR: 919 ++unreachable; 920 printf(" !A"); 921 break; 922 case ICMP6_DST_UNREACH_NOPORT: 923 if (rcvhlim >= 0 && 924 rcvhlim <= 1) 925 printf(" !"); 926 ++got_there; 927 break; 928 } 929 break; 930 } 931 } 932 if (cc == 0) 933 printf(" *"); 934 (void) fflush(stdout); 935 } 936 putchar('\n'); 937 if (got_there || 938 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) { 939 exit(0); 940 } 941 } 942 943 exit(0); 944} 945 946int 947wait_for_reply(sock, mhdr) 948 int sock; 949 struct msghdr *mhdr; 950{ 951#ifdef HAVE_POLL 952 struct pollfd pfd[1]; 953 int cc = 0; 954 955 pfd[0].fd = sock; 956 pfd[0].events = POLLIN; 957 pfd[0].revents = 0; 958 959 if (poll(pfd, 1, waittime * 1000) > 0) 960 cc = recvmsg(rcvsock, mhdr, 0); 961 962 return(cc); 963#else 964 fd_set *fdsp; 965 struct timeval wait; 966 int cc = 0, fdsn; 967 968 fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask); 969 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL) 970 err(1, "malloc"); 971 memset(fdsp, 0, fdsn); 972 FD_SET(sock, fdsp); 973 wait.tv_sec = waittime; wait.tv_usec = 0; 974 975 if (select(sock+1, fdsp, (fd_set *)0, (fd_set *)0, &wait) > 0) 976 cc = recvmsg(rcvsock, mhdr, 0); 977 978 free(fdsp); 979 return(cc); 980#endif 981} 982 983#ifdef IPSEC 984#ifdef IPSEC_POLICY_IPSEC 985int 986setpolicy(so, policy) 987 int so; 988 char *policy; 989{ 990 char *buf; 991 992 buf = ipsec_set_policy(policy, strlen(policy)); 993 if (buf == NULL) { 994 warnx("%s", ipsec_strerror()); 995 return -1; 996 } 997 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY, 998 buf, ipsec_get_policylen(buf)); 999 1000 free(buf); 1001 1002 return 0; 1003} 1004#endif 1005#endif 1006 1007void 1008send_probe(seq, hops) 1009 int seq; 1010 u_long hops; 1011{ 1012 struct icmp6_hdr *icp; 1013 struct opacket *op; 1014 struct timeval tv; 1015 struct tv32 tv32; 1016 int i; 1017 1018 i = hops; 1019 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 1020 (char *)&i, sizeof(i)) < 0) { 1021 perror("setsockopt IPV6_UNICAST_HOPS"); 1022 } 1023 1024 Dst.sin6_port = htons(port + seq); 1025 (void) gettimeofday(&tv, NULL); 1026 tv32.tv32_sec = htonl(tv.tv_sec); 1027 tv32.tv32_usec = htonl(tv.tv_usec); 1028 1029 switch (useproto) { 1030 case IPPROTO_ICMPV6: 1031 icp = (struct icmp6_hdr *)outpacket; 1032 1033 icp->icmp6_type = ICMP6_ECHO_REQUEST; 1034 icp->icmp6_code = 0; 1035 icp->icmp6_cksum = 0; 1036 icp->icmp6_id = ident; 1037 icp->icmp6_seq = htons(seq); 1038 bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN), 1039 sizeof(tv32)); 1040 break; 1041 case IPPROTO_UDP: 1042 op = outpacket; 1043 1044 op->seq = seq; 1045 op->hops = hops; 1046 bcopy(&tv32, &op->tv, sizeof tv32); 1047 break; 1048 case IPPROTO_NONE: 1049 /* No space for anything. No harm as seq/tv32 are decorative. */ 1050 break; 1051 default: 1052 fprintf(stderr, "Unknown probe protocol %d.\n", useproto); 1053 exit(1); 1054 } 1055 1056 i = sendto(sndsock, (char *)outpacket, datalen, 0, 1057 (struct sockaddr *)&Dst, Dst.sin6_len); 1058 if (i < 0 || i != datalen) { 1059 if (i < 0) 1060 perror("sendto"); 1061 printf("traceroute6: wrote %s %lu chars, ret=%d\n", 1062 hostname, datalen, i); 1063 (void) fflush(stdout); 1064 } 1065} 1066 1067int 1068get_hoplim(mhdr) 1069 struct msghdr *mhdr; 1070{ 1071 struct cmsghdr *cm; 1072 1073 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 1074 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 1075 if (cm->cmsg_level == IPPROTO_IPV6 && 1076 cm->cmsg_type == IPV6_HOPLIMIT && 1077 cm->cmsg_len == CMSG_LEN(sizeof(int))) 1078 return(*(int *)CMSG_DATA(cm)); 1079 } 1080 1081 return(-1); 1082} 1083 1084double 1085deltaT(t1p, t2p) 1086 struct timeval *t1p, *t2p; 1087{ 1088 double dt; 1089 1090 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + 1091 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0; 1092 return (dt); 1093} 1094 1095/* 1096 * Convert an ICMP "type" field to a printable string. 1097 */ 1098char * 1099pr_type(t0) 1100 int t0; 1101{ 1102 u_char t = t0 & 0xff; 1103 char *cp; 1104 1105 switch (t) { 1106 case ICMP6_DST_UNREACH: 1107 cp = "Destination Unreachable"; 1108 break; 1109 case ICMP6_PACKET_TOO_BIG: 1110 cp = "Packet Too Big"; 1111 break; 1112 case ICMP6_TIME_EXCEEDED: 1113 cp = "Time Exceeded"; 1114 break; 1115 case ICMP6_PARAM_PROB: 1116 cp = "Parameter Problem"; 1117 break; 1118 case ICMP6_ECHO_REQUEST: 1119 cp = "Echo Request"; 1120 break; 1121 case ICMP6_ECHO_REPLY: 1122 cp = "Echo Reply"; 1123 break; 1124 case ICMP6_MEMBERSHIP_QUERY: 1125 cp = "Group Membership Query"; 1126 break; 1127 case ICMP6_MEMBERSHIP_REPORT: 1128 cp = "Group Membership Report"; 1129 break; 1130 case ICMP6_MEMBERSHIP_REDUCTION: 1131 cp = "Group Membership Reduction"; 1132 break; 1133 case ND_ROUTER_SOLICIT: 1134 cp = "Router Solicitation"; 1135 break; 1136 case ND_ROUTER_ADVERT: 1137 cp = "Router Advertisement"; 1138 break; 1139 case ND_NEIGHBOR_SOLICIT: 1140 cp = "Neighbor Solicitation"; 1141 break; 1142 case ND_NEIGHBOR_ADVERT: 1143 cp = "Neighbor Advertisement"; 1144 break; 1145 case ND_REDIRECT: 1146 cp = "Redirect"; 1147 break; 1148 default: 1149 cp = "Unknown"; 1150 break; 1151 } 1152 return cp; 1153} 1154 1155int 1156packet_ok(mhdr, cc, seq) 1157 struct msghdr *mhdr; 1158 int cc; 1159 int seq; 1160{ 1161 struct icmp6_hdr *icp; 1162 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 1163 u_char type, code; 1164 char *buf = (char *)mhdr->msg_iov[0].iov_base; 1165 struct cmsghdr *cm; 1166 int *hlimp; 1167 char hbuf[NI_MAXHOST]; 1168 1169#ifdef OLDRAWSOCKET 1170 int hlen; 1171 struct ip6_hdr *ip; 1172#endif 1173 1174#ifdef OLDRAWSOCKET 1175 ip = (struct ip6_hdr *) buf; 1176 hlen = sizeof(struct ip6_hdr); 1177 if (cc < hlen + sizeof(struct icmp6_hdr)) { 1178 if (verbose) { 1179 if (getnameinfo((struct sockaddr *)from, from->sin6_len, 1180 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 1181 strlcpy(hbuf, "invalid", sizeof(hbuf)); 1182 printf("packet too short (%d bytes) from %s\n", cc, 1183 hbuf); 1184 } 1185 return (0); 1186 } 1187 cc -= hlen; 1188 icp = (struct icmp6_hdr *)(buf + hlen); 1189#else 1190 if (cc < sizeof(struct icmp6_hdr)) { 1191 if (verbose) { 1192 if (getnameinfo((struct sockaddr *)from, from->sin6_len, 1193 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 1194 strlcpy(hbuf, "invalid", sizeof(hbuf)); 1195 printf("data too short (%d bytes) from %s\n", cc, hbuf); 1196 } 1197 return(0); 1198 } 1199 icp = (struct icmp6_hdr *)buf; 1200#endif 1201 /* get optional information via advanced API */ 1202 rcvpktinfo = NULL; 1203 hlimp = NULL; 1204 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 1205 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 1206 if (cm->cmsg_level == IPPROTO_IPV6 && 1207 cm->cmsg_type == IPV6_PKTINFO && 1208 cm->cmsg_len == 1209 CMSG_LEN(sizeof(struct in6_pktinfo))) 1210 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm)); 1211 1212 if (cm->cmsg_level == IPPROTO_IPV6 && 1213 cm->cmsg_type == IPV6_HOPLIMIT && 1214 cm->cmsg_len == CMSG_LEN(sizeof(int))) 1215 hlimp = (int *)CMSG_DATA(cm); 1216 } 1217 if (rcvpktinfo == NULL || hlimp == NULL) { 1218 warnx("failed to get received hop limit or packet info"); 1219#if 0 1220 return(0); 1221#else 1222 rcvhlim = 0; /*XXX*/ 1223#endif 1224 } 1225 else 1226 rcvhlim = *hlimp; 1227 1228 type = icp->icmp6_type; 1229 code = icp->icmp6_code; 1230 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) 1231 || type == ICMP6_DST_UNREACH) { 1232 struct ip6_hdr *hip; 1233 void *up; 1234 1235 hip = (struct ip6_hdr *)(icp + 1); 1236 if ((up = get_uphdr(hip, (u_char *)(buf + cc))) == NULL) { 1237 if (verbose) 1238 warnx("failed to get upper layer header"); 1239 return(0); 1240 } 1241 switch (useproto) { 1242 case IPPROTO_ICMPV6: 1243 if (((struct icmp6_hdr *)up)->icmp6_id == ident && 1244 ((struct icmp6_hdr *)up)->icmp6_seq == htons(seq)) 1245 return (type == ICMP6_TIME_EXCEEDED ? 1246 -1 : code + 1); 1247 break; 1248 case IPPROTO_UDP: 1249 if (((struct udphdr *)up)->uh_sport == htons(srcport) && 1250 ((struct udphdr *)up)->uh_dport == htons(port + seq)) 1251 return (type == ICMP6_TIME_EXCEEDED ? 1252 -1 : code + 1); 1253 break; 1254 case IPPROTO_NONE: 1255 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1); 1256 default: 1257 fprintf(stderr, "Unknown probe proto %d.\n", useproto); 1258 break; 1259 } 1260 } else if (useproto == IPPROTO_ICMPV6 && type == ICMP6_ECHO_REPLY) { 1261 if (icp->icmp6_id == ident && 1262 icp->icmp6_seq == htons(seq)) 1263 return (ICMP6_DST_UNREACH_NOPORT + 1); 1264 } 1265 if (verbose) { 1266 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN]; 1267 u_int8_t *p; 1268 int i; 1269 1270 if (getnameinfo((struct sockaddr *)from, from->sin6_len, 1271 sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0) 1272 strlcpy(sbuf, "invalid", sizeof(sbuf)); 1273 printf("\n%d bytes from %s to %s", cc, sbuf, 1274 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 1275 dbuf, sizeof(dbuf)) : "?"); 1276 printf(": icmp type %d (%s) code %d\n", type, pr_type(type), 1277 icp->icmp6_code); 1278 p = (u_int8_t *)(icp + 1); 1279#define WIDTH 16 1280 for (i = 0; i < cc; i++) { 1281 if (i % WIDTH == 0) 1282 printf("%04x:", i); 1283 if (i % 4 == 0) 1284 printf(" "); 1285 printf("%02x", p[i]); 1286 if (i % WIDTH == WIDTH - 1) 1287 printf("\n"); 1288 } 1289 if (cc % WIDTH != 0) 1290 printf("\n"); 1291 } 1292 return(0); 1293} 1294 1295/* 1296 * Increment pointer until find the UDP or ICMP header. 1297 */ 1298void * 1299get_uphdr(ip6, lim) 1300 struct ip6_hdr *ip6; 1301 u_char *lim; 1302{ 1303 u_char *cp = (u_char *)ip6, nh; 1304 int hlen; 1305 static u_char none_hdr[1]; /* Fake pointer for IPPROTO_NONE. */ 1306 1307 if (cp + sizeof(*ip6) > lim) 1308 return(NULL); 1309 1310 nh = ip6->ip6_nxt; 1311 cp += sizeof(struct ip6_hdr); 1312 1313 while (lim - cp >= (nh == IPPROTO_NONE ? 0 : 8)) { 1314 switch (nh) { 1315 case IPPROTO_ESP: 1316 case IPPROTO_TCP: 1317 return(NULL); 1318 case IPPROTO_ICMPV6: 1319 return(useproto == nh ? cp : NULL); 1320 case IPPROTO_UDP: 1321 return(useproto == nh ? cp : NULL); 1322 case IPPROTO_NONE: 1323 return(useproto == nh ? none_hdr : NULL); 1324 case IPPROTO_FRAGMENT: 1325 hlen = sizeof(struct ip6_frag); 1326 nh = ((struct ip6_frag *)cp)->ip6f_nxt; 1327 break; 1328 case IPPROTO_AH: 1329 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2; 1330 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 1331 break; 1332 default: 1333 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3; 1334 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 1335 break; 1336 } 1337 1338 cp += hlen; 1339 } 1340 1341 return(NULL); 1342} 1343 1344void 1345print(mhdr, cc) 1346 struct msghdr *mhdr; 1347 int cc; 1348{ 1349 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 1350 char hbuf[NI_MAXHOST]; 1351 1352 if (getnameinfo((struct sockaddr *)from, from->sin6_len, 1353 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 1354 strlcpy(hbuf, "invalid", sizeof(hbuf)); 1355 if (nflag) 1356 printf(" %s", hbuf); 1357 else if (lflag) 1358 printf(" %s (%s)", inetname((struct sockaddr *)from), hbuf); 1359 else 1360 printf(" %s", inetname((struct sockaddr *)from)); 1361 1362 if (verbose) { 1363#ifdef OLDRAWSOCKET 1364 printf(" %d bytes to %s", cc, 1365 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 1366 hbuf, sizeof(hbuf)) : "?"); 1367#else 1368 printf(" %d bytes of data to %s", cc, 1369 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 1370 hbuf, sizeof(hbuf)) : "?"); 1371#endif 1372 } 1373} 1374 1375/* 1376 * Construct an Internet address representation. 1377 * If the nflag has been supplied, give 1378 * numeric value, otherwise try for symbolic name. 1379 */ 1380const char * 1381inetname(sa) 1382 struct sockaddr *sa; 1383{ 1384 static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1]; 1385 static int first = 1; 1386 char *cp; 1387 1388 if (first && !nflag) { 1389 first = 0; 1390 if (gethostname(domain, sizeof(domain)) == 0 && 1391 (cp = strchr(domain, '.'))) 1392 (void) memmove(domain, cp + 1, strlen(cp + 1) + 1); 1393 else 1394 domain[0] = 0; 1395 } 1396 cp = NULL; 1397 if (!nflag) { 1398 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0, 1399 NI_NAMEREQD) == 0) { 1400 if ((cp = strchr(line, '.')) && 1401 !strcmp(cp + 1, domain)) 1402 *cp = 0; 1403 cp = line; 1404 } 1405 } 1406 if (cp) 1407 return cp; 1408 1409 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0, 1410 NI_NUMERICHOST) != 0) 1411 strlcpy(line, "invalid", sizeof(line)); 1412 return line; 1413} 1414 1415void 1416usage() 1417{ 1418 1419 fprintf(stderr, 1420"usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n" 1421" [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n"); 1422 exit(1); 1423} 1424