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