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