traceroute6.c revision 55658
1/* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/usr.sbin/traceroute6/traceroute6.c 55658 2000-01-09 12:46:35Z shin $ 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 75static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93"; 76#endif /* not lint */ 77 78/* 79 * traceroute host - trace the route ip packets follow going to "host". 80 * 81 * Attempt to trace the route an ip packet would follow to some 82 * internet host. We find out intermediate hops by launching probe 83 * packets with a small ttl (time to live) then listening for an 84 * icmp "time exceeded" reply from a gateway. We start our probes 85 * with a ttl of one and increase by one until we get an icmp "port 86 * unreachable" (which means we got to "host") or hit a max (which 87 * defaults to 30 hops & can be changed with the -m flag). Three 88 * probes (change with -q flag) are sent at each ttl setting and a 89 * line is printed showing the ttl, address of the gateway and 90 * round trip time of each probe. If the probe answers come from 91 * different gateways, the address of each responding system will 92 * be printed. If there is no response within a 5 sec. timeout 93 * interval (changed with the -w flag), a "*" is printed for that 94 * probe. 95 * 96 * Probe packets are UDP format. We don't want the destination 97 * host to process them so the destination port is set to an 98 * unlikely value (if some clod on the destination is using that 99 * value, it can be changed with the -p flag). 100 * 101 * A sample use might be: 102 * 103 * [yak 71]% traceroute nis.nsf.net. 104 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet 105 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms 106 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms 107 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms 108 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms 109 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms 110 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms 111 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms 112 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms 113 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms 114 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms 115 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms 116 * 117 * Note that lines 2 & 3 are the same. This is due to a buggy 118 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards 119 * packets with a zero ttl. 120 * 121 * A more interesting example is: 122 * 123 * [yak 72]% traceroute allspice.lcs.mit.edu. 124 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max 125 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms 126 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms 127 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms 128 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms 129 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms 130 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms 131 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms 132 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms 133 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms 134 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms 135 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms 136 * 12 * * * 137 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms 138 * 14 * * * 139 * 15 * * * 140 * 16 * * * 141 * 17 * * * 142 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms 143 * 144 * (I start to see why I'm having so much trouble with mail to 145 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away 146 * either don't send ICMP "time exceeded" messages or send them 147 * with a ttl too small to reach us. 14 - 17 are running the 148 * MIT C Gateway code that doesn't send "time exceeded"s. God 149 * only knows what's going on with 12. 150 * 151 * The silent gateway 12 in the above may be the result of a bug in 152 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3) 153 * sends an unreachable message using whatever ttl remains in the 154 * original datagram. Since, for gateways, the remaining ttl is 155 * zero, the icmp "time exceeded" is guaranteed to not make it back 156 * to us. The behavior of this bug is slightly more interesting 157 * when it appears on the destination system: 158 * 159 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms 160 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms 161 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms 162 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms 163 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms 164 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms 165 * 7 * * * 166 * 8 * * * 167 * 9 * * * 168 * 10 * * * 169 * 11 * * * 170 * 12 * * * 171 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms ! 172 * 173 * Notice that there are 12 "gateways" (13 is the final 174 * destination) and exactly the last half of them are "missing". 175 * What's really happening is that rip (a Sun-3 running Sun OS3.5) 176 * is using the ttl from our arriving datagram as the ttl in its 177 * icmp reply. So, the reply will time out on the return path 178 * (with no notice sent to anyone since icmp's aren't sent for 179 * icmp's) until we probe with a ttl that's at least twice the path 180 * length. I.e., rip is really only 7 hops away. A reply that 181 * returns with a ttl of 1 is a clue this problem exists. 182 * Traceroute prints a "!" after the time if the ttl is <= 1. 183 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or 184 * non-standard (HPUX) software, expect to see this problem 185 * frequently and/or take care picking the target host of your 186 * probes. 187 * 188 * Other possible annotations after the time are !H, !N, !P (got a host, 189 * network or protocol unreachable, respectively), !S or !F (source 190 * route failed or fragmentation needed -- neither of these should 191 * ever occur and the associated gateway is busted if you see one). If 192 * almost all the probes result in some kind of unreachable, traceroute 193 * will give up and exit. 194 * 195 * Notes 196 * ----- 197 * This program must be run by root or be setuid. (I suggest that 198 * you *don't* make it setuid -- casual use could result in a lot 199 * of unnecessary traffic on our poor, congested nets.) 200 * 201 * This program requires a kernel mod that does not appear in any 202 * system available from Berkeley: A raw ip socket using proto 203 * IPPROTO_RAW must interpret the data sent as an ip datagram (as 204 * opposed to data to be wrapped in a ip datagram). See the README 205 * file that came with the source to this program for a description 206 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may 207 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE 208 * MODIFIED TO RUN THIS PROGRAM. 209 * 210 * The udp port usage may appear bizarre (well, ok, it is bizarre). 211 * The problem is that an icmp message only contains 8 bytes of 212 * data from the original datagram. 8 bytes is the size of a udp 213 * header so, if we want to associate replies with the original 214 * datagram, the necessary information must be encoded into the 215 * udp header (the ip id could be used but there's no way to 216 * interlock with the kernel's assignment of ip id's and, anyway, 217 * it would have taken a lot more kernel hacking to allow this 218 * code to set the ip id). So, to allow two or more users to 219 * use traceroute simultaneously, we use this task's pid as the 220 * source port (the high bit is set to move the port number out 221 * of the "likely" range). To keep track of which probe is being 222 * replied to (so times and/or hop counts don't get confused by a 223 * reply that was delayed in transit), we increment the destination 224 * port number before each probe. 225 * 226 * Don't use this as a coding example. I was trying to find a 227 * routing problem and this code sort-of popped out after 48 hours 228 * without sleep. I was amazed it ever compiled, much less ran. 229 * 230 * I stole the idea for this program from Steve Deering. Since 231 * the first release, I've learned that had I attended the right 232 * IETF working group meetings, I also could have stolen it from Guy 233 * Almes or Matt Mathis. I don't know (or care) who came up with 234 * the idea first. I envy the originators' perspicacity and I'm 235 * glad they didn't keep the idea a secret. 236 * 237 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or 238 * enhancements to the original distribution. 239 * 240 * I've hacked up a round-trip-route version of this that works by 241 * sending a loose-source-routed udp datagram through the destination 242 * back to yourself. Unfortunately, SO many gateways botch source 243 * routing, the thing is almost worthless. Maybe one day... 244 * 245 * -- Van Jacobson (van@helios.ee.lbl.gov) 246 * Tue Dec 20 03:50:13 PST 1988 247 */ 248 249#include <sys/param.h> 250#include <sys/time.h> 251#include <sys/socket.h> 252#include <sys/uio.h> 253#include <sys/file.h> 254#include <sys/ioctl.h> 255 256#include <netinet/in.h> 257 258#include <arpa/inet.h> 259 260#include <netdb.h> 261#include <stdio.h> 262#include <err.h> 263#include <errno.h> 264#include <stdlib.h> 265#include <string.h> 266#include <unistd.h> 267 268#include <netinet/ip6.h> 269#include <netinet/icmp6.h> 270#include <netinet/udp.h> 271 272#ifdef IPSEC 273#include <net/route.h> 274#include <netinet6/ipsec.h> 275#endif 276 277#define freehostent(hp) 278#define DUMMY_PORT 10010 279 280#define MAXPACKET 65535 /* max ip packet size */ 281#ifndef MAXHOSTNAMELEN 282#define MAXHOSTNAMELEN 64 283#endif 284 285#ifndef FD_SET 286#define NFDBITS (8*sizeof(fd_set)) 287#define FD_SETSIZE NFDBITS 288#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) 289#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) 290#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) 291#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) 292#endif 293 294#define Fprintf (void)fprintf 295#define Sprintf (void)sprintf 296#define Printf (void)printf 297 298/* 299 * format of a (udp) probe packet. 300 */ 301struct opacket { 302 u_char seq; /* sequence number of this packet */ 303 u_char hops; /* hop limit of the packet */ 304 struct timeval tv; /* time packet left */ 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 *)); 312int setpolicy __P((int so, char *policy)); 313void send_probe __P((int, int)); 314struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *)); 315int get_hoplim __P((struct msghdr *)); 316double deltaT __P((struct timeval *, struct timeval *)); 317char *pr_type __P((int)); 318int packet_ok __P((struct msghdr *, int, int)); 319void print __P((struct msghdr *, int)); 320void tvsub __P((struct timeval *, struct timeval *)); 321char *inetname __P((struct in6_addr *)); 322void usage __P((void)); 323 324int rcvsock; /* receive (icmp) socket file descriptor */ 325int sndsock; /* send (udp) socket file descriptor */ 326struct timezone tz; /* leftover */ 327 328struct msghdr rcvmhdr; 329struct iovec rcviov[2]; 330int rcvhlim; 331struct in6_pktinfo *rcvpktinfo; 332 333struct sockaddr_in6 Src, Dst, Rcv; 334struct sockaddr_in6 *src = &Src, *dst = &Dst, *rcv = &Rcv; 335int datalen; /* How much data */ 336char rtbuf[1024]; /*XXX*/ 337struct cmsghdr *cmsg; 338 339char *source = 0; 340char *hostname; 341 342int nprobes = 3; 343int max_hops = 30; 344u_short ident; 345u_short port = 32768+666; /* start udp dest port # for probe packets */ 346int options; /* socket options */ 347int verbose; 348int waittime = 5; /* time to wait for response (in seconds) */ 349int nflag; /* print addresses numerically */ 350int lflag; /* print both numerical address & hostname */ 351 352char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */ 353 354int 355main(argc, argv) 356 int argc; 357 char *argv[]; 358{ 359 extern char *optarg; 360 extern int optind; 361 struct hostent *hp; 362 struct addrinfo hints, *res; 363 int error; 364 int ch, i, on, probe, seq, hops; 365 static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) 366 + CMSG_SPACE(sizeof(int))]; 367 368 on = 1; 369 seq = 0; 370 371 while ((ch = getopt(argc, argv, "dlm:np:q:rs:w:vg:")) != EOF) 372 switch(ch) { 373 case 'd': 374 options |= SO_DEBUG; 375 break; 376 case 'l': 377 lflag++; 378 break; 379 case 'g': 380 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno); 381 if (hp == NULL) { 382 Fprintf(stderr, 383 "traceroute6: unknown host %s\n", optarg); 384 exit(1); 385 } 386 if (cmsg == NULL) 387 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0); 388 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, IPV6_RTHDR_LOOSE); 389 break; 390 case 'm': 391 max_hops = atoi(optarg); 392 if (max_hops <= 1) { 393 Fprintf(stderr, 394 "traceroute6: max hoplimit must be >1.\n"); 395 exit(1); 396 } 397 break; 398 case 'n': 399 nflag++; 400 break; 401 case 'p': 402 port = atoi(optarg); 403 if (port < 1) { 404 Fprintf(stderr, 405 "traceroute6: port must be >0.\n"); 406 exit(1); 407 } 408 break; 409 case 'q': 410 nprobes = atoi(optarg); 411 if (nprobes < 1) { 412 Fprintf(stderr, 413 "traceroute6: nprobes must be >0.\n"); 414 exit(1); 415 } 416 break; 417 case 'r': 418 options |= SO_DONTROUTE; 419 break; 420 case 's': 421 /* 422 * set the ip source address of the outbound 423 * probe (e.g., on a multi-homed host). 424 */ 425 source = optarg; 426 break; 427 case 'v': 428 verbose++; 429 break; 430 case 'w': 431 waittime = atoi(optarg); 432 if (waittime <= 1) { 433 Fprintf(stderr, 434 "traceroute6: wait must be >1 sec.\n"); 435 exit(1); 436 } 437 break; 438 default: 439 usage(); 440 } 441 argc -= optind; 442 argv += optind; 443 444 if (argc < 1) 445 usage(); 446 447 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 448 449 memset(&hints, 0, sizeof(hints)); 450 hints.ai_family = PF_INET6; 451 hints.ai_socktype = SOCK_RAW; 452 hints.ai_protocol = IPPROTO_ICMPV6; 453 hints.ai_flags = AI_CANONNAME; 454 error = getaddrinfo(*argv, NULL, &hints, &res); 455 if (error) { 456 (void)fprintf(stderr, 457 "traceroute6: %s\n", gai_strerror(error)); 458 if (error == EAI_SYSTEM) 459 (void)fprintf(stderr, 460 "traceroute6: %s\n", strerror(errno)); 461 exit(1); 462 } 463 if (!res->ai_addr) 464 errx(1, "getaddrinfo failed"); 465 memcpy(&Dst, res->ai_addr, res->ai_addrlen); 466 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv; 467 freeaddrinfo(res); 468 469 if (*++argv) 470 datalen = atoi(*argv); 471 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) { 472 Fprintf(stderr, 473 "traceroute6: packet size must be 0 <= s < %ld.\n", 474 (long)(MAXPACKET - sizeof(struct opacket))); 475 exit(1); 476 } 477 datalen += sizeof(struct opacket); 478 outpacket = (struct opacket *)malloc((unsigned)datalen); 479 if (! outpacket) { 480 perror("traceroute6: malloc"); 481 exit(1); 482 } 483 (void) bzero((char *)outpacket, datalen); 484 485 /* 486 * Receive ICMP 487 */ 488 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 489 perror("traceroute6: icmp socket"); 490 exit(5); 491 } 492 /* initialize msghdr for receiving packets */ 493 rcviov[0].iov_base = (caddr_t)packet; 494 rcviov[0].iov_len = sizeof(packet); 495 rcvmhdr.msg_name = (caddr_t)rcv; 496 rcvmhdr.msg_namelen = sizeof(*rcv); 497 rcvmhdr.msg_iov = rcviov; 498 rcvmhdr.msg_iovlen = 1; 499 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 500 rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf); 501 502 /* specify to tell receiving interface */ 503 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 504 sizeof(on)) < 0) 505 err(1, "setsockopt(IPV6_PKTINFO)"); 506 507 /* specify to tell value of hoplimit field of received IP6 hdr */ 508 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 509 sizeof(on)) < 0) 510 err(1, "setsockopt(IPV6_HOPLIMIT)"); 511 512 if (options & SO_DEBUG) 513 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, 514 (char *)&on, sizeof(on)); 515 if (options & SO_DONTROUTE) 516 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, 517 (char *)&on, sizeof(on)); 518#ifdef IPSEC 519#ifdef IPSEC_POLICY_IPSEC 520 /* 521 * do not raise error even if setsockopt fails, kernel may have ipsec 522 * turned off. 523 */ 524 if (setpolicy(rcvsock, "in bypass") < 0) 525 errx(1, ipsec_strerror()); 526 if (setpolicy(rcvsock, "out bypass") < 0) 527 errx(1, ipsec_strerror()); 528#endif /*IPSEC_POLICY_IPSEC*/ 529#endif /*IPSEC*/ 530 531 /* 532 * Send UDP 533 */ 534 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 535 perror("traceroute6: udp socket"); 536 exit(5); 537 } 538 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen, 539 sizeof(datalen)) < 0) { 540 perror("traceroute6: SO_SNDBUF"); 541 exit(6); 542 } 543 if (options & SO_DEBUG) 544 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, 545 (char *)&on, sizeof(on)); 546 if (options & SO_DONTROUTE) 547 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, 548 (char *)&on, sizeof(on)); 549 if (cmsg != NULL) { 550 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE); 551 (void) setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS, 552 rtbuf, cmsg->cmsg_len); 553 } 554#ifdef IPSEC 555#ifdef IPSEC_POLICY_IPSEC 556 /* 557 * do not raise error even if setsockopt fails, kernel may have ipsec 558 * turned off. 559 */ 560 if (setpolicy(sndsock, "in bypass") < 0) 561 errx(1, ipsec_strerror()); 562 if (setpolicy(sndsock, "out bypass") < 0) 563 errx(1, ipsec_strerror()); 564#endif /*IPSEC_POLICY_IPSEC*/ 565#endif /*IPSEC*/ 566 567 /* 568 * Source selection 569 */ 570 bzero((char *)src, sizeof(Src)); 571 if (source) { 572 if (inet_pton(AF_INET6, source, &Src.sin6_addr) != 1) { 573 Printf("traceroute6: unknown host %s\n", source); 574 exit(1); 575 } 576 } else { 577 struct sockaddr_in6 Nxt; 578 int dummy, len; 579 580 len = sizeof(Src); 581 Nxt = Dst; 582 Nxt.sin6_port = htons(DUMMY_PORT); 583 if (cmsg != NULL) 584 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr, 585 sizeof(Nxt.sin6_addr)); 586 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 587 perror("socket") ; 588 } 589 if(-1 == connect(dummy, (struct sockaddr *)&Nxt, sizeof(Nxt))) 590 perror("connect"); 591 if(-1 == getsockname(dummy, (struct sockaddr *)src, &len)) { 592 perror("getsockname"); 593 printf("%d\n", errno); 594 } 595 close(dummy) ; 596 } 597 ident = (getpid() & 0xffff) | 0x8000; 598 Src.sin6_family = AF_INET6; 599 Src.sin6_port = htons(ident); 600 if (bind(sndsock, (struct sockaddr *)src, sizeof(Src)) < 0){ 601 perror ("traceroute6: bind:"); 602 exit (1); 603 } 604 605 /* 606 * Message to users 607 */ 608 Fprintf(stderr, "traceroute to %s (%s)", hostname, 609 inet_ntop(AF_INET6, &Dst.sin6_addr, 610 ntop_buf, sizeof(ntop_buf))); 611 if (source) 612 Fprintf(stderr, " from %s", source); 613 Fprintf(stderr, ", %d hops max, %d byte packets\n", max_hops, datalen); 614 (void) fflush(stderr); 615 616 /* 617 * Main loop 618 */ 619 for (hops = 1; hops <= max_hops; ++hops) { 620 struct in6_addr lastaddr; 621 int got_there = 0; 622 int unreachable = 0; 623 624 Printf("%2d ", hops); 625 bzero(&lastaddr, sizeof(lastaddr)); 626 for (probe = 0; probe < nprobes; ++probe) { 627 int cc; 628 struct timeval t1, t2; 629 struct timezone tz; 630 631 (void) gettimeofday(&t1, &tz); 632 send_probe(++seq, hops); 633 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { 634 (void) gettimeofday(&t2, &tz); 635 if ((i = packet_ok(&rcvmhdr, cc, seq))) { 636 if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr, 637 &lastaddr)) { 638 print(&rcvmhdr, cc); 639 lastaddr = Rcv.sin6_addr; 640 } 641 Printf(" %g ms", deltaT(&t1, &t2)); 642 switch(i - 1) { 643 case ICMP6_DST_UNREACH_NOROUTE: 644 ++unreachable; 645 Printf(" !N"); 646 break; 647 case ICMP6_DST_UNREACH_ADMIN: 648 ++unreachable; 649 Printf(" !P"); 650 break; 651 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 652 ++unreachable; 653 Printf(" !S"); 654 break; 655 case ICMP6_DST_UNREACH_ADDR: 656 ++unreachable; 657 Printf(" !A"); 658 break; 659 case ICMP6_DST_UNREACH_NOPORT: 660 if (rcvhlim >= 0 && 661 rcvhlim <= 1) 662 Printf(" !"); 663 ++got_there; 664 break; 665 } 666 break; 667 } 668 } 669 if (cc == 0) 670 Printf(" *"); 671 (void) fflush(stdout); 672 } 673 putchar('\n'); 674 if (got_there || 675 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) { 676 exit(0); 677 } 678 } 679 680 exit(0); 681} 682 683int 684wait_for_reply(sock, mhdr) 685 int sock; 686 struct msghdr *mhdr; 687{ 688 fd_set fds; 689 struct timeval wait; 690 int cc = 0; 691 692 FD_ZERO(&fds); 693 FD_SET(sock, &fds); 694 wait.tv_sec = waittime; wait.tv_usec = 0; 695 696 if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0) 697 cc = recvmsg(rcvsock, mhdr, 0); 698 699 return(cc); 700} 701 702#ifdef IPSEC 703#ifdef IPSEC_POLICY_IPSEC 704int 705setpolicy(so, policy) 706 int so; 707 char *policy; 708{ 709 char *buf; 710 711 buf = ipsec_set_policy(policy, strlen(policy)); 712 if (buf == NULL) { 713 warnx(ipsec_strerror()); 714 return -1; 715 } 716 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY, 717 buf, ipsec_get_policylen(buf)); 718 719 free(buf); 720 721 return 0; 722} 723#endif 724#endif 725 726void 727send_probe(seq, hops) 728 int seq, hops; 729{ 730 struct opacket *op = outpacket; 731 int i; 732 733 if(setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 734 (char *)&hops, sizeof(hops)) < 0) { 735 perror("setsockopt IPV6_UNICAST_HOPS"); 736 } 737 738 Dst.sin6_port = htons(port + seq); 739 740 op->seq = seq; 741 op->hops = hops; 742 (void) gettimeofday(&op->tv, &tz); 743 744 i = sendto(sndsock, (char *)outpacket, datalen , 0, 745 (struct sockaddr *)dst, sizeof(Dst)); 746 if (i < 0 || i != datalen) { 747 if (i<0) 748 perror("sendto"); 749 Printf("traceroute6: wrote %s %d chars, ret=%d\n", hostname, 750 datalen, i); 751 (void) fflush(stdout); 752 } 753} 754 755int 756get_hoplim(mhdr) 757 struct msghdr *mhdr; 758{ 759 struct cmsghdr *cm; 760 761 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 762 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 763 if (cm->cmsg_level == IPPROTO_IPV6 && 764 cm->cmsg_type == IPV6_HOPLIMIT && 765 cm->cmsg_len == CMSG_LEN(sizeof(int))) 766 return(*(int *)CMSG_DATA(cm)); 767 } 768 769 return(-1); 770} 771 772double 773deltaT(t1p, t2p) 774 struct timeval *t1p, *t2p; 775{ 776 register double dt; 777 778 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + 779 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0; 780 return (dt); 781} 782 783 784/* 785 * Convert an ICMP "type" field to a printable string. 786 */ 787char * 788pr_type(t0) 789 int t0; 790{ 791 u_char t = t0 & 0xff; 792 char *cp; 793 794 switch (t) { 795 case ICMP6_DST_UNREACH: 796 cp = "Destination Unreachable"; 797 break; 798 case ICMP6_PACKET_TOO_BIG: 799 cp = "Pakcet Too Big"; 800 break; 801 case ICMP6_TIME_EXCEEDED: 802 cp = "Time Exceeded"; 803 break; 804 case ICMP6_PARAM_PROB: 805 cp = "Parameter Problem"; 806 break; 807 case ICMP6_ECHO_REQUEST: 808 cp = "Echo Request"; 809 break; 810 case ICMP6_ECHO_REPLY: 811 cp = "Echo Reply"; 812 break; 813 case ICMP6_MEMBERSHIP_QUERY: 814 cp = "Group Membership Query"; 815 break; 816 case ICMP6_MEMBERSHIP_REPORT: 817 cp = "Group Membership Report"; 818 break; 819 case ICMP6_MEMBERSHIP_REDUCTION: 820 cp = "Group Membership Reduction"; 821 break; 822 case ND_ROUTER_SOLICIT: 823 cp = "Router Solicitation"; 824 break; 825 case ND_ROUTER_ADVERT: 826 cp = "Router Advertisement"; 827 break; 828 case ND_NEIGHBOR_SOLICIT: 829 cp = "Neighbor Solicitation"; 830 break; 831 case ND_NEIGHBOR_ADVERT: 832 cp = "Neighbor Advertisement"; 833 break; 834 case ND_REDIRECT: 835 cp = "Ridirect"; 836 break; 837 default: 838 cp = "Unknown"; 839 break; 840 } 841 return cp; 842} 843 844 845int 846packet_ok(mhdr, cc, seq) 847 struct msghdr *mhdr; 848 int cc; 849 int seq; 850{ 851 register struct icmp6_hdr *icp; 852 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 853 u_char type, code; 854 char *buf = (char *)mhdr->msg_iov[0].iov_base; 855 struct cmsghdr *cm; 856 int *hlimp; 857 858 if (cc < sizeof(struct icmp6_hdr)) { 859 if (verbose) 860 Printf("data too short (%d bytes) from %s\n", cc, 861 inet_ntop(AF_INET6, &from->sin6_addr, 862 ntop_buf, sizeof(ntop_buf))); 863 return(0); 864 } 865 icp = (struct icmp6_hdr *)buf; 866 /* get optional information via advanced API */ 867 rcvpktinfo = NULL; 868 hlimp = NULL; 869 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 870 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 871 if (cm->cmsg_level == IPPROTO_IPV6 && 872 cm->cmsg_type == IPV6_PKTINFO && 873 cm->cmsg_len == 874 CMSG_LEN(sizeof(struct in6_pktinfo))) 875 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm)); 876 877 if (cm->cmsg_level == IPPROTO_IPV6 && 878 cm->cmsg_type == IPV6_HOPLIMIT && 879 cm->cmsg_len == CMSG_LEN(sizeof(int))) 880 hlimp = (int *)CMSG_DATA(cm); 881 } 882 if (rcvpktinfo == NULL || hlimp == NULL) { 883 warnx("failed to get received hop limit or packet info"); 884 return(0); 885 } 886 rcvhlim = *hlimp; 887 888 type = icp->icmp6_type; 889 code = icp->icmp6_code; 890 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) 891 || type == ICMP6_DST_UNREACH) { 892 struct ip6_hdr *hip; 893 struct udphdr *up; 894 895 hip = (struct ip6_hdr *)(icp + 1); 896 if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) { 897 if (verbose) 898 warnx("failed to get upper layer header"); 899 return(0); 900 } 901 if (up->uh_sport == htons(ident) && 902 up->uh_dport == htons(port+seq)) 903 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1); 904 } 905 if (verbose) { 906 int i; 907 u_long *lp = (u_long *)(icp + 1); 908 char sbuf[INET6_ADDRSTRLEN+1], dbuf[INET6_ADDRSTRLEN]; 909 910 Printf("\n%d bytes from %s to %s", cc, 911 inet_ntop(AF_INET6, &from->sin6_addr, 912 sbuf, sizeof(sbuf)), 913 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 914 dbuf, sizeof(dbuf))); 915 Printf(": icmp type %d (%s) code %d\n", type, pr_type(type), 916 icp->icmp6_code); 917 for (i = 4; i < cc ; i += sizeof(long)) 918 Printf("%2d: %8.8x\n", i, (u_int32_t)ntohl(*lp++)); 919 } 920 return(0); 921} 922 923/* 924 * Increment pointer until find the UDP header. 925 */ 926struct udphdr * 927get_udphdr(ip6, lim) 928 struct ip6_hdr *ip6; 929 u_char *lim; 930{ 931 u_char *cp = (u_char *)ip6, nh; 932 int hlen; 933 934 if (cp + sizeof(*ip6) >= lim) 935 return(NULL); 936 937 nh = ip6->ip6_nxt; 938 cp += sizeof(struct ip6_hdr); 939 940 while(lim - cp >= 8) { 941 switch(nh) { 942 case IPPROTO_ESP: 943 case IPPROTO_TCP: 944 case IPPROTO_ICMPV6: 945 return(NULL); 946 case IPPROTO_UDP: 947 return((struct udphdr *)cp); 948 case IPPROTO_FRAGMENT: 949 hlen = sizeof(struct ip6_frag); 950 nh = ((struct ip6_frag *)cp)->ip6f_nxt; 951 break; 952 case IPPROTO_AH: 953 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2; 954 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 955 break; 956 default: 957 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3; 958 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 959 break; 960 } 961 962 cp += hlen; 963 } 964 965 return(NULL); 966} 967 968void 969print(mhdr, cc) 970 struct msghdr *mhdr; 971 int cc; 972{ 973 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 974 975 if (nflag) { 976 Printf(" %s", inet_ntop(AF_INET6, &from->sin6_addr, 977 ntop_buf, sizeof(ntop_buf))); 978 } 979 else if (lflag) { 980 Printf(" %s (%s)", inetname(&from->sin6_addr), 981 inet_ntop(AF_INET6, &from->sin6_addr, 982 ntop_buf, sizeof(ntop_buf))); 983 } 984 else { 985 Printf(" %s", inetname(&from->sin6_addr)); 986 } 987 988 if (verbose) { 989 Printf(" %d bytes of data to %s", cc, 990 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 991 ntop_buf, sizeof(ntop_buf))); 992 } 993} 994 995/* 996 * Subtract 2 timeval structs: out = out - in. 997 * Out is assumed to be >= in. 998 */ 999void 1000tvsub(out, in) 1001 register struct timeval *out, *in; 1002{ 1003 if ((out->tv_usec -= in->tv_usec) < 0) { 1004 out->tv_sec--; 1005 out->tv_usec += 1000000; 1006 } 1007 out->tv_sec -= in->tv_sec; 1008} 1009 1010 1011/* 1012 * Construct an Internet address representation. 1013 * If the nflag has been supplied, give 1014 * numeric value, otherwise try for symbolic name. 1015 */ 1016char * 1017inetname(in) 1018 struct in6_addr *in; 1019{ 1020 register char *cp; 1021 static char line[50]; 1022 struct hostent *hp; 1023 static char domain[MAXHOSTNAMELEN + 1]; 1024 static int first = 1; 1025 int herr; 1026 1027 if (first && !nflag) { 1028 first = 0; 1029 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 1030 (cp = index(domain, '.'))) 1031 (void) strcpy(domain, cp + 1); 1032 else 1033 domain[0] = 0; 1034 } 1035 cp = 0; 1036 if (!nflag) { 1037 /* hp = (struct hostent *)addr2hostname(in, sizeof(*in), AF_INET6, &herr); */ 1038 hp = (struct hostent *)getipnodebyaddr((const char *)in, sizeof(*in), 1039 AF_INET6, &herr); 1040 if (hp) { 1041 if ((cp = index(hp->h_name, '.')) && 1042 !strcmp(cp + 1, domain)) 1043 *cp = 0; 1044 cp = hp->h_name; 1045#undef freehostent(hp) 1046 freehostent(hp); 1047#define freehostent(hp) 1048 } 1049 } 1050 if (cp) 1051 (void) strcpy(line, cp); 1052 else { 1053 (void)inet_ntop(AF_INET6, in, line, sizeof(line)); 1054 } 1055 return (line); 1056} 1057 1058void 1059usage() 1060{ 1061 (void)fprintf(stderr, 1062"usage: traceroute6 [-dlnrv] [-m max_hops] [-p port#] [-q nqueries]\n\t\ 1063[-s src_addr] [-g gateway] [-w wait] host [data size]\n"); 1064 exit(1); 1065} 1066