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