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$ 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 69 static 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 static 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 */ 301 struct 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 307 u_char packet[512]; /* last inbound (icmp) packet */ 308 struct opacket *outpacket; /* last output (udp) packet */ 309 310 int main __P((int, char *[])); 311 int wait_for_reply __P((int, struct msghdr *)); 312 int setpolicy __P((int so, char *policy)); 313 void send_probe __P((int, int)); 314 struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *)); 315 int get_hoplim __P((struct msghdr *)); 316 double deltaT __P((struct timeval *, struct timeval *)); 317 char *pr_type __P((int)); 318 int packet_ok __P((struct msghdr *, int, int)); 319 void print __P((struct msghdr *, int)); 320 void tvsub __P((struct timeval *, struct timeval *)); 321 char *inetname __P((struct in6_addr *)); 322 void usage __P((void)); 323 324 int rcvsock; /* receive (icmp) socket file descriptor */ 325 int sndsock; /* send (udp) socket file descriptor */ 326 struct timezone tz; /* leftover */ 327 328 struct msghdr rcvmhdr; 329 struct iovec rcviov[2]; 330 int rcvhlim; 331 struct in6_pktinfo *rcvpktinfo; 332 333 struct sockaddr_in6 Src, Dst, Rcv; 334 struct sockaddr_in6 *src = &Src, *dst = &Dst, *rcv = &Rcv; 335 int datalen; /* How much data */ 336 char rtbuf[1024]; /*XXX*/ 337 struct cmsghdr *cmsg; 338 339 char *source = 0; 340 char *hostname; 341 342 int nprobes = 3; 343 int max_hops = 30; 344 u_short ident; 345 u_short port = 32768+666; /* start udp dest port # for probe packets */ 346 int options; /* socket options */ 347 int verbose; 348 int waittime = 5; /* time to wait for response (in seconds) */ 349 int nflag; /* print addresses numerically */ 350 int lflag; /* print both numerical address & hostname */ 351 352 char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */ 353 354 int 355 main(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 /* 369 * Receive ICMP 370 */ 371 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 372 perror("traceroute6: icmp socket"); 373 exit(5); 374 } 375 setuid(getuid()); 376 377 on = 1; 378 seq = 0; 379 380 while ((ch = getopt(argc, argv, "dlm:np:q:rs:w:vg:")) != EOF) 381 switch(ch) { 382 case 'd': 383 options |= SO_DEBUG; 384 break; 385 case 'l': 386 lflag++; 387 break; 388 case 'g': 389 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno); 390 if (hp == NULL) { 391 Fprintf(stderr, 392 "traceroute6: unknown host %s\n", optarg); 393 exit(1); 394 } 395 if (cmsg == NULL) 396 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0); 397 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, IPV6_RTHDR_LOOSE); 398 break; 399 case 'm': 400 max_hops = atoi(optarg); 401 if (max_hops <= 1) { 402 Fprintf(stderr, 403 "traceroute6: max hoplimit must be >1.\n"); 404 exit(1); 405 } 406 break; 407 case 'n': 408 nflag++; 409 break; 410 case 'p': 411 port = atoi(optarg); 412 if (port < 1) { 413 Fprintf(stderr, 414 "traceroute6: port must be >0.\n"); 415 exit(1); 416 } 417 break; 418 case 'q': 419 nprobes = atoi(optarg); 420 if (nprobes < 1) { 421 Fprintf(stderr, 422 "traceroute6: nprobes must be >0.\n"); 423 exit(1); 424 } 425 break; 426 case 'r': 427 options |= SO_DONTROUTE; 428 break; 429 case 's': 430 /* 431 * set the ip source address of the outbound 432 * probe (e.g., on a multi-homed host). 433 */ 434 source = optarg; 435 break; 436 case 'v': 437 verbose++; 438 break; 439 case 'w': 440 waittime = atoi(optarg); 441 if (waittime <= 1) { 442 Fprintf(stderr, 443 "traceroute6: wait must be >1 sec.\n"); 444 exit(1); 445 } 446 break; 447 default: 448 usage(); 449 } 450 argc -= optind; 451 argv += optind; 452 453 if (argc < 1) 454 usage(); 455 456 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 457 458 memset(&hints, 0, sizeof(hints)); 459 hints.ai_family = PF_INET6; 460 hints.ai_socktype = SOCK_RAW; 461 hints.ai_protocol = IPPROTO_ICMPV6; 462 hints.ai_flags = AI_CANONNAME; 463 error = getaddrinfo(*argv, NULL, &hints, &res); 464 if (error) { 465 (void)fprintf(stderr, 466 "traceroute6: %s\n", gai_strerror(error)); 467 if (error == EAI_SYSTEM) 468 (void)fprintf(stderr, 469 "traceroute6: %s\n", strerror(errno)); 470 exit(1); 471 } 472 if (!res->ai_addr) 473 errx(1, "getaddrinfo failed"); 474 memcpy(&Dst, res->ai_addr, res->ai_addrlen); 475 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv; 476 freeaddrinfo(res); 477 478 if (*++argv) 479 datalen = atoi(*argv); 480 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) { 481 Fprintf(stderr, 482 "traceroute6: packet size must be 0 <= s < %ld.\n", 483 (long)(MAXPACKET - sizeof(struct opacket))); 484 exit(1); 485 } 486 datalen += sizeof(struct opacket); 487 outpacket = (struct opacket *)malloc((unsigned)datalen); 488 if (! outpacket) { 489 perror("traceroute6: malloc"); 490 exit(1); 491 } 492 (void) bzero((char *)outpacket, datalen); 493 494 /* initialize msghdr for receiving packets */ 495 rcviov[0].iov_base = (caddr_t)packet; 496 rcviov[0].iov_len = sizeof(packet); 497 rcvmhdr.msg_name = (caddr_t)rcv; 498 rcvmhdr.msg_namelen = sizeof(*rcv); 499 rcvmhdr.msg_iov = rcviov; 500 rcvmhdr.msg_iovlen = 1; 501 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 502 rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf); 503 504 /* specify to tell receiving interface */ 505 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 506 sizeof(on)) < 0) 507 err(1, "setsockopt(IPV6_PKTINFO)"); 508 509 /* specify to tell value of hoplimit field of received IP6 hdr */ 510 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 511 sizeof(on)) < 0) 512 err(1, "setsockopt(IPV6_HOPLIMIT)"); 513 514 if (options & SO_DEBUG) 515 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, 516 (char *)&on, sizeof(on)); 517 if (options & SO_DONTROUTE) 518 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, 519 (char *)&on, sizeof(on)); 520 #ifdef IPSEC 521 #ifdef IPSEC_POLICY_IPSEC 522 /* 523 * do not raise error even if setsockopt fails, kernel may have ipsec 524 * turned off. 525 */ 526 if (setpolicy(rcvsock, "in bypass") < 0) 527 errx(1, ipsec_strerror()); 528 if (setpolicy(rcvsock, "out bypass") < 0) 529 errx(1, ipsec_strerror()); 530 #endif /*IPSEC_POLICY_IPSEC*/ 531 #endif /*IPSEC*/ 532 533 /* 534 * Send UDP 535 */ 536 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 537 perror("traceroute6: udp socket"); 538 exit(5); 539 } 540 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen, 541 sizeof(datalen)) < 0) { 542 perror("traceroute6: SO_SNDBUF"); 543 exit(6); 544 } 545 if (options & SO_DEBUG) 546 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, 547 (char *)&on, sizeof(on)); 548 if (options & SO_DONTROUTE) 549 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, 550 (char *)&on, sizeof(on)); 551 if (cmsg != NULL) { 552 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE); 553 (void) setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS, 554 rtbuf, cmsg->cmsg_len); 555 } 556 #ifdef IPSEC 557 #ifdef IPSEC_POLICY_IPSEC 558 /* 559 * do not raise error even if setsockopt fails, kernel may have ipsec 560 * turned off. 561 */ 562 if (setpolicy(sndsock, "in bypass") < 0) 563 errx(1, ipsec_strerror()); 564 if (setpolicy(sndsock, "out bypass") < 0) 565 errx(1, ipsec_strerror()); 566 #endif /*IPSEC_POLICY_IPSEC*/ 567 #endif /*IPSEC*/ 568 569 /* 570 * Source selection 571 */ 572 bzero((char *)src, sizeof(Src)); 573 if (source) { 574 if (inet_pton(AF_INET6, source, &Src.sin6_addr) != 1) { 575 Printf("traceroute6: unknown host %s\n", source); 576 exit(1); 577 } 578 } else { 579 struct sockaddr_in6 Nxt; 580 int dummy, len; 581 582 len = sizeof(Src); 583 Nxt = Dst; 584 Nxt.sin6_port = htons(DUMMY_PORT); 585 if (cmsg != NULL) 586 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr, 587 sizeof(Nxt.sin6_addr)); 588 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 589 perror("socket") ; 590 } 591 if(-1 == connect(dummy, (struct sockaddr *)&Nxt, sizeof(Nxt))) 592 perror("connect"); 593 if(-1 == getsockname(dummy, (struct sockaddr *)src, &len)) { 594 perror("getsockname"); 595 printf("%d\n", errno); 596 } 597 close(dummy) ; 598 } 599 ident = (getpid() & 0xffff) | 0x8000; 600 Src.sin6_family = AF_INET6; 601 Src.sin6_port = htons(ident); 602 if (bind(sndsock, (struct sockaddr *)src, sizeof(Src)) < 0){ 603 perror ("traceroute6: bind:"); 604 exit (1); 605 } 606 607 /* 608 * Message to users 609 */ 610 Fprintf(stderr, "traceroute to %s (%s)", hostname, 611 inet_ntop(AF_INET6, &Dst.sin6_addr, 612 ntop_buf, sizeof(ntop_buf))); 613 if (source) 614 Fprintf(stderr, " from %s", source); 615 Fprintf(stderr, ", %d hops max, %d byte packets\n", max_hops, datalen); 616 (void) fflush(stderr); 617 618 /* 619 * Main loop 620 */ 621 for (hops = 1; hops <= max_hops; ++hops) { 622 struct in6_addr lastaddr; 623 int got_there = 0; 624 int unreachable = 0; 625 626 Printf("%2d ", hops); 627 bzero(&lastaddr, sizeof(lastaddr)); 628 for (probe = 0; probe < nprobes; ++probe) { 629 int cc; 630 struct timeval t1, t2; 631 struct timezone tz; 632 633 (void) gettimeofday(&t1, &tz); 634 send_probe(++seq, hops); 635 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { 636 (void) gettimeofday(&t2, &tz); 637 if ((i = packet_ok(&rcvmhdr, cc, seq))) { 638 if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr, 639 &lastaddr)) { 640 print(&rcvmhdr, cc); 641 lastaddr = Rcv.sin6_addr; 642 } 643 Printf(" %g ms", deltaT(&t1, &t2)); 644 switch(i - 1) { 645 case ICMP6_DST_UNREACH_NOROUTE: 646 ++unreachable; 647 Printf(" !N"); 648 break; 649 case ICMP6_DST_UNREACH_ADMIN: 650 ++unreachable; 651 Printf(" !P"); 652 break; 653 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 654 ++unreachable; 655 Printf(" !S"); 656 break; 657 case ICMP6_DST_UNREACH_ADDR: 658 ++unreachable; 659 Printf(" !A"); 660 break; 661 case ICMP6_DST_UNREACH_NOPORT: 662 if (rcvhlim >= 0 && 663 rcvhlim <= 1) 664 Printf(" !"); 665 ++got_there; 666 break; 667 } 668 break; 669 } 670 } 671 if (cc == 0) 672 Printf(" *"); 673 (void) fflush(stdout); 674 } 675 putchar('\n'); 676 if (got_there || 677 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) { 678 exit(0); 679 } 680 } 681 682 exit(0); 683 } 684 685 int 686 wait_for_reply(sock, mhdr) 687 int sock; 688 struct msghdr *mhdr; 689 { 690 fd_set fds; 691 struct timeval wait; 692 int cc = 0; 693 694 FD_ZERO(&fds); 695 FD_SET(sock, &fds); 696 wait.tv_sec = waittime; wait.tv_usec = 0; 697 698 if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0) 699 cc = recvmsg(rcvsock, mhdr, 0); 700 701 return(cc); 702 } 703 704 #ifdef IPSEC 705 #ifdef IPSEC_POLICY_IPSEC 706 int 707 setpolicy(so, policy) 708 int so; 709 char *policy; 710 { 711 char *buf; 712 713 buf = ipsec_set_policy(policy, strlen(policy)); 714 if (buf == NULL) { 715 warnx(ipsec_strerror()); 716 return -1; 717 } 718 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY, 719 buf, ipsec_get_policylen(buf)); 720 721 free(buf); 722 723 return 0; 724 } 725 #endif 726 #endif 727 728 void 729 send_probe(seq, hops) 730 int seq, hops; 731 { 732 struct opacket *op = outpacket; 733 int i; 734 735 if(setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 736 (char *)&hops, sizeof(hops)) < 0) { 737 perror("setsockopt IPV6_UNICAST_HOPS"); 738 } 739 740 Dst.sin6_port = htons(port + seq); 741 742 op->seq = seq; 743 op->hops = hops; 744 (void) gettimeofday(&op->tv, &tz); 745 746 i = sendto(sndsock, (char *)outpacket, datalen , 0, 747 (struct sockaddr *)dst, sizeof(Dst)); 748 if (i < 0 || i != datalen) { 749 if (i<0) 750 perror("sendto"); 751 Printf("traceroute6: wrote %s %d chars, ret=%d\n", hostname, 752 datalen, i); 753 (void) fflush(stdout); 754 } 755 } 756 757 int 758 get_hoplim(mhdr) 759 struct msghdr *mhdr; 760 { 761 struct cmsghdr *cm; 762 763 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 764 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 765 if (cm->cmsg_level == IPPROTO_IPV6 && 766 cm->cmsg_type == IPV6_HOPLIMIT && 767 cm->cmsg_len == CMSG_LEN(sizeof(int))) 768 return(*(int *)CMSG_DATA(cm)); 769 } 770 771 return(-1); 772 } 773 774 double 775 deltaT(t1p, t2p) 776 struct timeval *t1p, *t2p; 777 { 778 register double dt; 779 780 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + 781 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0; 782 return (dt); 783 } 784 785 786 /* 787 * Convert an ICMP "type" field to a printable string. 788 */ 789 char * 790 pr_type(t0) 791 int t0; 792 { 793 u_char t = t0 & 0xff; 794 char *cp; 795 796 switch (t) { 797 case ICMP6_DST_UNREACH: 798 cp = "Destination Unreachable"; 799 break; 800 case ICMP6_PACKET_TOO_BIG: 801 cp = "Pakcet Too Big"; 802 break; 803 case ICMP6_TIME_EXCEEDED: 804 cp = "Time Exceeded"; 805 break; 806 case ICMP6_PARAM_PROB: 807 cp = "Parameter Problem"; 808 break; 809 case ICMP6_ECHO_REQUEST: 810 cp = "Echo Request"; 811 break; 812 case ICMP6_ECHO_REPLY: 813 cp = "Echo Reply"; 814 break; 815 case ICMP6_MEMBERSHIP_QUERY: 816 cp = "Group Membership Query"; 817 break; 818 case ICMP6_MEMBERSHIP_REPORT: 819 cp = "Group Membership Report"; 820 break; 821 case ICMP6_MEMBERSHIP_REDUCTION: 822 cp = "Group Membership Reduction"; 823 break; 824 case ND_ROUTER_SOLICIT: 825 cp = "Router Solicitation"; 826 break; 827 case ND_ROUTER_ADVERT: 828 cp = "Router Advertisement"; 829 break; 830 case ND_NEIGHBOR_SOLICIT: 831 cp = "Neighbor Solicitation"; 832 break; 833 case ND_NEIGHBOR_ADVERT: 834 cp = "Neighbor Advertisement"; 835 break; 836 case ND_REDIRECT: 837 cp = "Ridirect"; 838 break; 839 default: 840 cp = "Unknown"; 841 break; 842 } 843 return cp; 844 } 845 846 847 int 848 packet_ok(mhdr, cc, seq) 849 struct msghdr *mhdr; 850 int cc; 851 int seq; 852 { 853 register struct icmp6_hdr *icp; 854 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 855 u_char type, code; 856 char *buf = (char *)mhdr->msg_iov[0].iov_base; 857 struct cmsghdr *cm; 858 int *hlimp; 859 860 if (cc < sizeof(struct icmp6_hdr)) { 861 if (verbose) 862 Printf("data too short (%d bytes) from %s\n", cc, 863 inet_ntop(AF_INET6, &from->sin6_addr, 864 ntop_buf, sizeof(ntop_buf))); 865 return(0); 866 } 867 icp = (struct icmp6_hdr *)buf; 868 /* get optional information via advanced API */ 869 rcvpktinfo = NULL; 870 hlimp = NULL; 871 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 872 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 873 if (cm->cmsg_level == IPPROTO_IPV6 && 874 cm->cmsg_type == IPV6_PKTINFO && 875 cm->cmsg_len == 876 CMSG_LEN(sizeof(struct in6_pktinfo))) 877 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm)); 878 879 if (cm->cmsg_level == IPPROTO_IPV6 && 880 cm->cmsg_type == IPV6_HOPLIMIT && 881 cm->cmsg_len == CMSG_LEN(sizeof(int))) 882 hlimp = (int *)CMSG_DATA(cm); 883 } 884 if (rcvpktinfo == NULL || hlimp == NULL) { 885 warnx("failed to get received hop limit or packet info"); 886 return(0); 887 } 888 rcvhlim = *hlimp; 889 890 type = icp->icmp6_type; 891 code = icp->icmp6_code; 892 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) 893 || type == ICMP6_DST_UNREACH) { 894 struct ip6_hdr *hip; 895 struct udphdr *up; 896 897 hip = (struct ip6_hdr *)(icp + 1); 898 if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) { 899 if (verbose) 900 warnx("failed to get upper layer header"); 901 return(0); 902 } 903 if (up->uh_sport == htons(ident) && 904 up->uh_dport == htons(port+seq)) 905 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1); 906 } 907 if (verbose) { 908 int i; 909 u_long *lp = (u_long *)(icp + 1); 910 char sbuf[INET6_ADDRSTRLEN+1], dbuf[INET6_ADDRSTRLEN]; 911 912 Printf("\n%d bytes from %s to %s", cc, 913 inet_ntop(AF_INET6, &from->sin6_addr, 914 sbuf, sizeof(sbuf)), 915 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 916 dbuf, sizeof(dbuf))); 917 Printf(": icmp type %d (%s) code %d\n", type, pr_type(type), 918 icp->icmp6_code); 919 for (i = 4; i < cc ; i += sizeof(long)) 920 Printf("%2d: %8.8x\n", i, (u_int32_t)ntohl(*lp++)); 921 } 922 return(0); 923 } 924 925 /* 926 * Increment pointer until find the UDP header. 927 */ 928 struct udphdr * 929 get_udphdr(ip6, lim) 930 struct ip6_hdr *ip6; 931 u_char *lim; 932 { 933 u_char *cp = (u_char *)ip6, nh; 934 int hlen; 935 936 if (cp + sizeof(*ip6) >= lim) 937 return(NULL); 938 939 nh = ip6->ip6_nxt; 940 cp += sizeof(struct ip6_hdr); 941 942 while(lim - cp >= 8) { 943 switch(nh) { 944 case IPPROTO_ESP: 945 case IPPROTO_TCP: 946 case IPPROTO_ICMPV6: 947 return(NULL); 948 case IPPROTO_UDP: 949 return((struct udphdr *)cp); 950 case IPPROTO_FRAGMENT: 951 hlen = sizeof(struct ip6_frag); 952 nh = ((struct ip6_frag *)cp)->ip6f_nxt; 953 break; 954 case IPPROTO_AH: 955 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2; 956 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 957 break; 958 default: 959 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3; 960 nh = ((struct ip6_ext *)cp)->ip6e_nxt; 961 break; 962 } 963 964 cp += hlen; 965 } 966 967 return(NULL); 968 } 969 970 void 971 print(mhdr, cc) 972 struct msghdr *mhdr; 973 int cc; 974 { 975 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; 976 977 if (nflag) { 978 Printf(" %s", inet_ntop(AF_INET6, &from->sin6_addr, 979 ntop_buf, sizeof(ntop_buf))); 980 } 981 else if (lflag) { 982 Printf(" %s (%s)", inetname(&from->sin6_addr), 983 inet_ntop(AF_INET6, &from->sin6_addr, 984 ntop_buf, sizeof(ntop_buf))); 985 } 986 else { 987 Printf(" %s", inetname(&from->sin6_addr)); 988 } 989 990 if (verbose) { 991 Printf(" %d bytes of data to %s", cc, 992 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr, 993 ntop_buf, sizeof(ntop_buf))); 994 } 995 } 996 997 /* 998 * Subtract 2 timeval structs: out = out - in. 999 * Out is assumed to be >= in. 1000 */ 1001 void 1002 tvsub(out, in) 1003 register struct timeval *out, *in; 1004 { 1005 if ((out->tv_usec -= in->tv_usec) < 0) { 1006 out->tv_sec--; 1007 out->tv_usec += 1000000; 1008 } 1009 out->tv_sec -= in->tv_sec; 1010 } 1011 1012 1013 /* 1014 * Construct an Internet address representation. 1015 * If the nflag has been supplied, give 1016 * numeric value, otherwise try for symbolic name. 1017 */ 1018 char * 1019 inetname(in) 1020 struct in6_addr *in; 1021 { 1022 register char *cp; 1023 static char line[50]; 1024 struct hostent *hp; 1025 static char domain[MAXHOSTNAMELEN + 1]; 1026 static int first = 1; 1027 int herr; 1028 1029 if (first && !nflag) { 1030 first = 0; 1031 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 1032 (cp = index(domain, '.'))) 1033 (void) strcpy(domain, cp + 1); 1034 else 1035 domain[0] = 0; 1036 } 1037 cp = 0; 1038 if (!nflag) { 1039 /* hp = (struct hostent *)addr2hostname(in, sizeof(*in), AF_INET6, &herr); */ 1040 hp = (struct hostent *)getipnodebyaddr((const char *)in, sizeof(*in), 1041 AF_INET6, &herr); 1042 if (hp) { 1043 if ((cp = index(hp->h_name, '.')) && 1044 !strcmp(cp + 1, domain)) 1045 *cp = 0; 1046 cp = hp->h_name; 1047 #undef freehostent(hp) 1048 freehostent(hp); 1049 #define freehostent(hp) 1050 } 1051 } 1052 if (cp) 1053 (void) strcpy(line, cp); 1054 else { 1055 (void)inet_ntop(AF_INET6, in, line, sizeof(line)); 1056 } 1057 return (line); 1058 } 1059 1060 void 1061 usage() 1062 { 1063 (void)fprintf(stderr, 1064 "usage: traceroute6 [-dlnrv] [-m max_hops] [-p port#] [-q nqueries]\n\t\ 1065 [-s src_addr] [-g gateway] [-w wait] host [data size]\n"); 1066 exit(1); 1067 } 1068