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