traceroute6.c revision 1.36 1 /* $NetBSD: traceroute6.c,v 1.36 2006/05/05 00:03:22 rpaulo Exp $ */
2 /* $KAME: traceroute6.c,v 1.67 2004/01/25 03:24:39 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * Van Jacobson.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #if 0
66 #ifndef lint
67 static char copyright[] =
68 "@(#) Copyright (c) 1990, 1993\n\
69 The Regents of the University of California. All rights reserved.\n";
70 #endif /* not lint */
71
72 #ifndef lint
73 static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93";
74 #endif /* not lint */
75 #else
76 #include <sys/cdefs.h>
77 #ifndef lint
78 __RCSID("$NetBSD: traceroute6.c,v 1.36 2006/05/05 00:03:22 rpaulo Exp $");
79 #endif
80 #endif
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 a 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 (at) 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 <netinet6/ipsec.h>
283 #endif
284
285 #define DUMMY_PORT 10010
286
287 #define MAXPACKET 65535 /* max ip packet size */
288
289 #ifndef HAVE_GETIPNODEBYNAME
290 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
291 #define freehostent(x)
292 #endif
293
294 /*
295 * format of a (udp) probe packet.
296 */
297 struct tv32 {
298 u_int32_t tv32_sec;
299 u_int32_t tv32_usec;
300 };
301
302 struct opacket {
303 u_char seq; /* sequence number of this packet */
304 u_char hops; /* hop limit of the packet */
305 u_char pad[2];
306 struct tv32 tv; /* time packet left */
307 } __packed;
308
309 u_char packet[512]; /* last inbound (icmp) packet */
310 struct opacket *outpacket; /* last output (udp) packet */
311
312 int main __P((int, char *[]));
313 int wait_for_reply __P((int, struct msghdr *));
314 #ifdef IPSEC
315 #ifdef IPSEC_POLICY_IPSEC
316 int setpolicy __P((int so, char *policy));
317 #endif
318 #endif
319 void send_probe __P((int, u_long));
320 struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
321 int get_hoplim __P((struct msghdr *));
322 double deltaT __P((struct timeval *, struct timeval *));
323 char *pr_type __P((int));
324 int packet_ok __P((struct msghdr *, int, int));
325 void print __P((struct msghdr *, int));
326 const char *inetname __P((struct sockaddr *));
327 void usage __P((void));
328
329 int rcvsock; /* receive (icmp) socket file descriptor */
330 int sndsock; /* send (udp) socket file descriptor */
331
332 struct msghdr rcvmhdr;
333 struct iovec rcviov[2];
334 int rcvhlim;
335 struct in6_pktinfo *rcvpktinfo;
336
337 struct sockaddr_in6 Src, Dst, Rcv;
338 u_long datalen; /* How much data */
339 #define ICMP6ECHOLEN 8
340 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
341 char rtbuf[2064];
342 #ifdef USE_RFC3542
343 struct ip6_rthdr *rth;
344 #endif
345 struct cmsghdr *cmsg;
346
347 char *source = 0;
348 char *hostname;
349
350 u_long nprobes = 3;
351 u_long first_hop = 1;
352 u_long max_hops = 30;
353 u_int16_t srcport;
354 u_int16_t port = 32768+666; /* start udp dest port # for probe packets */
355 u_int16_t ident;
356 int options; /* socket options */
357 int verbose;
358 int waittime = 5; /* time to wait for response (in seconds) */
359 int nflag; /* print addresses numerically */
360 int useicmp;
361 int lflag; /* print both numerical address & hostname */
362
363 int
364 main(argc, argv)
365 int argc;
366 char *argv[];
367 {
368 int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM };
369 char hbuf[NI_MAXHOST], src0[NI_MAXHOST], *ep;
370 int ch, i, on = 1, seq, rcvcmsglen, error, minlen;
371 struct addrinfo hints, *res;
372 static u_char *rcvcmsgbuf;
373 u_long probe, hops, lport;
374 struct hostent *hp;
375 size_t size;
376
377 /*
378 * Receive ICMP
379 */
380 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
381 perror("socket(ICMPv6)");
382 exit(5);
383 }
384
385 /* revoke privs */
386 seteuid(getuid());
387 setuid(getuid());
388
389 size = sizeof(i);
390 (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &i, &size, NULL, 0);
391 max_hops = i;
392
393 /* specify to tell receiving interface */
394 #ifdef IPV6_RECVPKTINFO
395 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
396 sizeof(on)) < 0)
397 err(1, "setsockopt(IPV6_RECVPKTINFO)");
398 #else /* old adv. API */
399 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
400 sizeof(on)) < 0)
401 err(1, "setsockopt(IPV6_PKTINFO)");
402 #endif
403
404 /* specify to tell value of hoplimit field of received IP6 hdr */
405 #ifdef IPV6_RECVHOPLIMIT
406 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
407 sizeof(on)) < 0)
408 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
409 #else /* old adv. API */
410 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
411 sizeof(on)) < 0)
412 err(1, "setsockopt(IPV6_HOPLIMIT)");
413 #endif
414
415 seq = 0;
416
417 while ((ch = getopt(argc, argv, "df:g:Ilm:np:q:rs:w:v")) != -1)
418 switch (ch) {
419 case 'd':
420 options |= SO_DEBUG;
421 break;
422 case 'f':
423 ep = NULL;
424 errno = 0;
425 first_hop = strtoul(optarg, &ep, 0);
426 if (errno || !*optarg || *ep|| first_hop > 255) {
427 fprintf(stderr,
428 "traceroute6: invalid min hoplimit.\n");
429 exit(1);
430 }
431 break;
432 case 'g':
433 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
434 if (hp == NULL) {
435 fprintf(stderr,
436 "traceroute6: unknown host %s\n", optarg);
437 exit(1);
438 }
439 #ifdef USE_RFC3542
440 if (rth == NULL) {
441 /*
442 * XXX: We can't detect the number of
443 * intermediate nodes yet.
444 */
445 if ((rth = inet6_rth_init((void *)rtbuf,
446 sizeof(rtbuf), IPV6_RTHDR_TYPE_0,
447 0)) == NULL) {
448 fprintf(stderr,
449 "inet6_rth_init failed.\n");
450 exit(1);
451 }
452 }
453 if (inet6_rth_add((void *)rth,
454 (struct in6_addr *)hp->h_addr)) {
455 fprintf(stderr,
456 "inet6_rth_add failed for %s\n",
457 optarg);
458 exit(1);
459 }
460 #else /* old advanced API */
461 if (cmsg == NULL)
462 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
463 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr,
464 IPV6_RTHDR_LOOSE);
465 #endif
466 freehostent(hp);
467 break;
468 case 'I':
469 useicmp++;
470 /* same as ping6 */
471 ident = htons(arc4random() & 0xffff);
472 break;
473 case 'l':
474 lflag++;
475 break;
476 case 'm':
477 ep = NULL;
478 errno = 0;
479 max_hops = strtoul(optarg, &ep, 0);
480 if (errno || !*optarg || *ep || max_hops > 255) {
481 fprintf(stderr,
482 "traceroute6: invalid max hoplimit.\n");
483 exit(1);
484 }
485 break;
486 case 'n':
487 nflag++;
488 break;
489 case 'p':
490 ep = NULL;
491 errno = 0;
492 lport = strtoul(optarg, &ep, 0);
493 if (errno || !*optarg || *ep) {
494 fprintf(stderr, "traceroute6: invalid port.\n");
495 exit(1);
496 }
497 if (lport == 0 || lport != (lport & 0xffff)) {
498 fprintf(stderr,
499 "traceroute6: port out of range.\n");
500 exit(1);
501 }
502 port = lport & 0xffff;
503 break;
504 case 'q':
505 ep = NULL;
506 errno = 0;
507 nprobes = strtoul(optarg, &ep, 0);
508 if (errno || !*optarg || *ep) {
509 fprintf(stderr,
510 "traceroute6: invalid nprobes.\n");
511 exit(1);
512 }
513 if (nprobes < 1) {
514 fprintf(stderr,
515 "traceroute6: nprobes must be >0.\n");
516 exit(1);
517 }
518 break;
519 case 'r':
520 options |= SO_DONTROUTE;
521 break;
522 case 's':
523 /*
524 * set the ip source address of the outbound
525 * probe (e.g., on a multi-homed host).
526 */
527 source = optarg;
528 break;
529 case 'v':
530 verbose++;
531 break;
532 case 'w':
533 ep = NULL;
534 errno = 0;
535 waittime = strtoul(optarg, &ep, 0);
536 if (errno || !*optarg || *ep) {
537 fprintf(stderr,
538 "traceroute6: invalid wait time.\n");
539 exit(1);
540 }
541 if (waittime <= 1) {
542 fprintf(stderr,
543 "traceroute6: wait must be >1 sec.\n");
544 exit(1);
545 }
546 break;
547 default:
548 usage();
549 }
550 argc -= optind;
551 argv += optind;
552
553 if (max_hops < first_hop) {
554 fprintf(stderr,
555 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
556 exit(1);
557 }
558
559 if (argc < 1 || argc > 2)
560 usage();
561
562 #if 1
563 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
564 #else
565 setlinebuf(stdout);
566 #endif
567
568 memset(&hints, 0, sizeof(hints));
569 hints.ai_family = PF_INET6;
570 hints.ai_socktype = SOCK_RAW;
571 hints.ai_protocol = IPPROTO_ICMPV6;
572 hints.ai_flags = AI_CANONNAME;
573 error = getaddrinfo(*argv, NULL, &hints, &res);
574 if (error) {
575 fprintf(stderr,
576 "traceroute6: %s\n", gai_strerror(error));
577 exit(1);
578 }
579 if (res->ai_addrlen != sizeof(Dst)) {
580 fprintf(stderr,
581 "traceroute6: size of sockaddr mismatch\n");
582 exit(1);
583 }
584 memcpy(&Dst, res->ai_addr, res->ai_addrlen);
585 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
586 if (!hostname) {
587 fprintf(stderr, "traceroute6: not enough core\n");
588 exit(1);
589 }
590 if (res->ai_next) {
591 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
592 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
593 strlcpy(hbuf, "?", sizeof(hbuf));
594 fprintf(stderr, "traceroute6: Warning: %s has multiple "
595 "addresses; using %s\n", hostname, hbuf);
596 }
597
598 if (*++argv) {
599 ep = NULL;
600 errno = 0;
601 datalen = strtoul(*argv, &ep, 0);
602 if (errno || !*argv || *ep) {
603 fprintf(stderr,
604 "traceroute6: invalid packet length.\n");
605 exit(1);
606 }
607 }
608 if (useicmp)
609 minlen = ICMP6ECHOLEN + sizeof(struct tv32);
610 else
611 minlen = sizeof(struct opacket);
612 if (datalen < minlen)
613 datalen = minlen;
614 else if (datalen >= MAXPACKET) {
615 fprintf(stderr,
616 "traceroute6: packet size must be %d <= s < %ld.\n",
617 minlen, (long)MAXPACKET);
618 exit(1);
619 }
620 outpacket = (struct opacket *)malloc((unsigned)datalen);
621 if (!outpacket) {
622 perror("malloc");
623 exit(1);
624 }
625 (void) bzero((char *)outpacket, datalen);
626
627 /* initialize msghdr for receiving packets */
628 rcviov[0].iov_base = (caddr_t)packet;
629 rcviov[0].iov_len = sizeof(packet);
630 rcvmhdr.msg_name = (caddr_t)&Rcv;
631 rcvmhdr.msg_namelen = sizeof(Rcv);
632 rcvmhdr.msg_iov = rcviov;
633 rcvmhdr.msg_iovlen = 1;
634 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
635 CMSG_SPACE(sizeof(int));
636 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
637 fprintf(stderr, "traceroute6: malloc failed\n");
638 exit(1);
639 }
640 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
641 rcvmhdr.msg_controllen = rcvcmsglen;
642
643 if (options & SO_DEBUG)
644 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
645 (char *)&on, sizeof(on));
646 if (options & SO_DONTROUTE)
647 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
648 (char *)&on, sizeof(on));
649 #ifdef IPSEC
650 #ifdef IPSEC_POLICY_IPSEC
651 /*
652 * do not raise error even if setsockopt fails, kernel may have ipsec
653 * turned off.
654 */
655 if (setpolicy(rcvsock, "in bypass") < 0)
656 errx(1, "%s", ipsec_strerror());
657 if (setpolicy(rcvsock, "out bypass") < 0)
658 errx(1, "%s", ipsec_strerror());
659 #else
660 {
661 int level = IPSEC_LEVEL_NONE;
662
663 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
664 sizeof(level));
665 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
666 sizeof(level));
667 #ifdef IP_AUTH_TRANS_LEVEL
668 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
669 sizeof(level));
670 #else
671 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
672 sizeof(level));
673 #endif
674 #ifdef IP_AUTH_NETWORK_LEVEL
675 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
676 sizeof(level));
677 #endif
678 }
679 #endif /*IPSEC_POLICY_IPSEC*/
680 #endif /*IPSEC*/
681
682 /*
683 * Send UDP or ICMP
684 */
685 if (useicmp) {
686 sndsock = rcvsock;
687 } else {
688 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
689 perror("socket(SOCK_DGRAM)");
690 exit(5);
691 }
692 }
693 #ifdef SO_SNDBUF
694 i = datalen;
695 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&i,
696 sizeof(i)) < 0) {
697 perror("setsockopt(SO_SNDBUF)");
698 exit(6);
699 }
700 #endif /* SO_SNDBUF */
701 if (options & SO_DEBUG)
702 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
703 (char *)&on, sizeof(on));
704 if (options & SO_DONTROUTE)
705 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
706 (char *)&on, sizeof(on));
707 #ifdef USE_RFC3542
708 if (rth) {/* XXX: there is no library to finalize the header... */
709 rth->ip6r_len = rth->ip6r_segleft * 2;
710 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR,
711 (void *)rth, (rth->ip6r_len + 1) << 3)) {
712 fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n",
713 strerror(errno));
714 exit(1);
715 }
716 }
717 #else /* old advanced API */
718 if (cmsg != NULL) {
719 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
720 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
721 rtbuf, cmsg->cmsg_len) < 0) {
722 fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n",
723 strerror(errno));
724 exit(1);
725 }
726 }
727 #endif /* USE_RFC3542 */
728 #ifdef IPSEC
729 #ifdef IPSEC_POLICY_IPSEC
730 /*
731 * do not raise error even if setsockopt fails, kernel may have ipsec
732 * turned off.
733 */
734 if (setpolicy(sndsock, "in bypass") < 0)
735 errx(1, "%s", ipsec_strerror());
736 if (setpolicy(sndsock, "out bypass") < 0)
737 errx(1, "%s", ipsec_strerror());
738 #else
739 {
740 int level = IPSEC_LEVEL_BYPASS;
741
742 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
743 sizeof(level));
744 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
745 sizeof(level));
746 #ifdef IP_AUTH_TRANS_LEVEL
747 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
748 sizeof(level));
749 #else
750 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
751 sizeof(level));
752 #endif
753 #ifdef IP_AUTH_NETWORK_LEVEL
754 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
755 sizeof(level));
756 #endif
757 }
758 #endif /*IPSEC_POLICY_IPSEC*/
759 #endif /*IPSEC*/
760
761 /*
762 * Source selection
763 */
764 bzero(&Src, sizeof(Src));
765 if (source) {
766 struct addrinfo hints, *res;
767 int error;
768
769 memset(&hints, 0, sizeof(hints));
770 hints.ai_family = AF_INET6;
771 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
772 hints.ai_flags = AI_NUMERICHOST;
773 error = getaddrinfo(source, "0", &hints, &res);
774 if (error) {
775 printf("traceroute6: %s: %s\n", source,
776 gai_strerror(error));
777 exit(1);
778 }
779 if (res->ai_addrlen > sizeof(Src)) {
780 printf("traceroute6: %s: %s\n", source,
781 gai_strerror(error));
782 exit(1);
783 }
784 memcpy(&Src, res->ai_addr, res->ai_addrlen);
785 freeaddrinfo(res);
786 } else {
787 struct sockaddr_in6 Nxt;
788 int dummy;
789 socklen_t len;
790
791 Nxt = Dst;
792 Nxt.sin6_port = htons(DUMMY_PORT);
793 if (cmsg != NULL)
794 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
795 sizeof(Nxt.sin6_addr));
796 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
797 perror("socket");
798 exit(1);
799 }
800 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
801 perror("connect");
802 exit(1);
803 }
804 len = sizeof(Src);
805 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
806 perror("getsockname");
807 exit(1);
808 }
809 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
810 src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) {
811 fprintf(stderr, "getnameinfo failed for source\n");
812 exit(1);
813 }
814 source = src0;
815 close(dummy);
816 }
817
818 Src.sin6_port = htons(0);
819 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
820 perror("bind");
821 exit(1);
822 }
823
824 {
825 socklen_t len;
826
827 len = sizeof(Src);
828 if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) {
829 perror("getsockname");
830 exit(1);
831 }
832 srcport = ntohs(Src.sin6_port);
833 }
834
835 /*
836 * Message to users
837 */
838 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf,
839 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
840 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
841 fprintf(stderr, "traceroute6");
842 fprintf(stderr, " to %s (%s)", hostname, hbuf);
843 if (source)
844 fprintf(stderr, " from %s", source);
845 fprintf(stderr, ", %lu hops max, %lu byte packets\n",
846 max_hops, datalen);
847 (void) fflush(stderr);
848
849 if (first_hop > 1)
850 printf("Skipping %lu intermediate hops\n", first_hop - 1);
851
852 /*
853 * Main loop
854 */
855 for (hops = first_hop; hops <= max_hops; ++hops) {
856 struct in6_addr lastaddr;
857 int got_there = 0;
858 int unreachable = 0;
859
860 printf("%2lu ", hops);
861 bzero(&lastaddr, sizeof(lastaddr));
862 for (probe = 0; probe < nprobes; ++probe) {
863 int cc;
864 struct timeval t1, t2;
865
866 (void) gettimeofday(&t1, NULL);
867 send_probe(++seq, hops);
868 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
869 (void) gettimeofday(&t2, NULL);
870 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
871 if (!IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
872 &lastaddr)) {
873 print(&rcvmhdr, cc);
874 lastaddr = Rcv.sin6_addr;
875 }
876 printf(" %g ms", deltaT(&t1, &t2));
877 switch (i - 1) {
878 case ICMP6_DST_UNREACH_NOROUTE:
879 ++unreachable;
880 printf(" !N");
881 break;
882 case ICMP6_DST_UNREACH_ADMIN:
883 ++unreachable;
884 printf(" !P");
885 break;
886 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
887 ++unreachable;
888 printf(" !S");
889 break;
890 case ICMP6_DST_UNREACH_ADDR:
891 ++unreachable;
892 printf(" !A");
893 break;
894 case ICMP6_DST_UNREACH_NOPORT:
895 if (rcvhlim >= 0 &&
896 rcvhlim <= 1)
897 printf(" !");
898 ++got_there;
899 break;
900 }
901 break;
902 }
903 }
904 if (cc == 0)
905 printf(" *");
906 (void) fflush(stdout);
907 }
908 putchar('\n');
909 if (got_there ||
910 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
911 exit(0);
912 }
913 }
914
915 exit(0);
916 }
917
918 int
919 wait_for_reply(sock, mhdr)
920 int sock;
921 struct msghdr *mhdr;
922 {
923 #ifdef HAVE_POLL
924 struct pollfd pfd[1];
925 int cc = 0;
926
927 pfd[0].fd = sock;
928 pfd[0].events = POLLIN;
929 pfd[0].revents = 0;
930
931 if (poll(pfd, 1, waittime * 1000) > 0)
932 cc = recvmsg(rcvsock, mhdr, 0);
933
934 return(cc);
935 #else
936 fd_set *fdsp;
937 struct timeval wait;
938 int cc = 0, fdsn;
939
940 fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
941 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
942 err(1, "malloc");
943 memset(fdsp, 0, fdsn);
944 FD_SET(sock, fdsp);
945 wait.tv_sec = waittime; wait.tv_usec = 0;
946
947 if (select(sock+1, fdsp, (fd_set *)0, (fd_set *)0, &wait) > 0)
948 cc = recvmsg(rcvsock, mhdr, 0);
949
950 free(fdsp);
951 return(cc);
952 #endif
953 }
954
955 #ifdef IPSEC
956 #ifdef IPSEC_POLICY_IPSEC
957 int
958 setpolicy(so, policy)
959 int so;
960 char *policy;
961 {
962 char *buf;
963
964 buf = ipsec_set_policy(policy, strlen(policy));
965 if (buf == NULL) {
966 warnx("%s", ipsec_strerror());
967 return -1;
968 }
969 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
970 buf, ipsec_get_policylen(buf));
971
972 free(buf);
973
974 return 0;
975 }
976 #endif
977 #endif
978
979 void
980 send_probe(seq, hops)
981 int seq;
982 u_long hops;
983 {
984 struct timeval tv;
985 struct tv32 tv32;
986 int i;
987
988 i = hops;
989 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
990 (char *)&i, sizeof(i)) < 0) {
991 perror("setsockopt IPV6_UNICAST_HOPS");
992 }
993
994 Dst.sin6_port = htons(port + seq);
995 (void) gettimeofday(&tv, NULL);
996 tv32.tv32_sec = htonl(tv.tv_sec);
997 tv32.tv32_usec = htonl(tv.tv_usec);
998
999 if (useicmp) {
1000 struct icmp6_hdr *icp = (struct icmp6_hdr *)outpacket;
1001
1002 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1003 icp->icmp6_code = 0;
1004 icp->icmp6_cksum = 0;
1005 icp->icmp6_id = ident;
1006 icp->icmp6_seq = htons(seq);
1007 bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN),
1008 sizeof(tv32));
1009 } else {
1010 struct opacket *op = outpacket;
1011
1012 op->seq = seq;
1013 op->hops = hops;
1014 bcopy(&tv32, &op->tv, sizeof tv32);
1015 }
1016
1017 i = sendto(sndsock, (char *)outpacket, datalen , 0,
1018 (struct sockaddr *)&Dst, Dst.sin6_len);
1019 if (i < 0 || i != datalen) {
1020 if (i < 0)
1021 perror("sendto");
1022 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
1023 hostname, datalen, i);
1024 (void) fflush(stdout);
1025 }
1026 }
1027
1028 int
1029 get_hoplim(mhdr)
1030 struct msghdr *mhdr;
1031 {
1032 struct cmsghdr *cm;
1033
1034 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1035 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1036 if (cm->cmsg_level == IPPROTO_IPV6 &&
1037 cm->cmsg_type == IPV6_HOPLIMIT &&
1038 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1039 return(*(int *)CMSG_DATA(cm));
1040 }
1041
1042 return(-1);
1043 }
1044
1045 double
1046 deltaT(t1p, t2p)
1047 struct timeval *t1p, *t2p;
1048 {
1049 double dt;
1050
1051 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
1052 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
1053 return (dt);
1054 }
1055
1056 /*
1057 * Convert an ICMP "type" field to a printable string.
1058 */
1059 char *
1060 pr_type(t0)
1061 int t0;
1062 {
1063 u_char t = t0 & 0xff;
1064 char *cp;
1065
1066 switch (t) {
1067 case ICMP6_DST_UNREACH:
1068 cp = "Destination Unreachable";
1069 break;
1070 case ICMP6_PACKET_TOO_BIG:
1071 cp = "Packet Too Big";
1072 break;
1073 case ICMP6_TIME_EXCEEDED:
1074 cp = "Time Exceeded";
1075 break;
1076 case ICMP6_PARAM_PROB:
1077 cp = "Parameter Problem";
1078 break;
1079 case ICMP6_ECHO_REQUEST:
1080 cp = "Echo Request";
1081 break;
1082 case ICMP6_ECHO_REPLY:
1083 cp = "Echo Reply";
1084 break;
1085 case ICMP6_MEMBERSHIP_QUERY:
1086 cp = "Group Membership Query";
1087 break;
1088 case ICMP6_MEMBERSHIP_REPORT:
1089 cp = "Group Membership Report";
1090 break;
1091 case ICMP6_MEMBERSHIP_REDUCTION:
1092 cp = "Group Membership Reduction";
1093 break;
1094 case ND_ROUTER_SOLICIT:
1095 cp = "Router Solicitation";
1096 break;
1097 case ND_ROUTER_ADVERT:
1098 cp = "Router Advertisement";
1099 break;
1100 case ND_NEIGHBOR_SOLICIT:
1101 cp = "Neighbor Solicitation";
1102 break;
1103 case ND_NEIGHBOR_ADVERT:
1104 cp = "Neighbor Advertisement";
1105 break;
1106 case ND_REDIRECT:
1107 cp = "Redirect";
1108 break;
1109 default:
1110 cp = "Unknown";
1111 break;
1112 }
1113 return cp;
1114 }
1115
1116 int
1117 packet_ok(mhdr, cc, seq)
1118 struct msghdr *mhdr;
1119 int cc;
1120 int seq;
1121 {
1122 struct icmp6_hdr *icp;
1123 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1124 u_char type, code;
1125 char *buf = (char *)mhdr->msg_iov[0].iov_base;
1126 struct cmsghdr *cm;
1127 int *hlimp;
1128 char hbuf[NI_MAXHOST];
1129
1130 #ifdef OLDRAWSOCKET
1131 int hlen;
1132 struct ip6_hdr *ip;
1133 #endif
1134
1135 #ifdef OLDRAWSOCKET
1136 ip = (struct ip6_hdr *) buf;
1137 hlen = sizeof(struct ip6_hdr);
1138 if (cc < hlen + sizeof(struct icmp6_hdr)) {
1139 if (verbose) {
1140 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1141 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1142 strlcpy(hbuf, "invalid", sizeof(hbuf));
1143 printf("packet too short (%d bytes) from %s\n", cc,
1144 hbuf);
1145 }
1146 return (0);
1147 }
1148 cc -= hlen;
1149 icp = (struct icmp6_hdr *)(buf + hlen);
1150 #else
1151 if (cc < sizeof(struct icmp6_hdr)) {
1152 if (verbose) {
1153 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1154 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1155 strlcpy(hbuf, "invalid", sizeof(hbuf));
1156 printf("data too short (%d bytes) from %s\n", cc, hbuf);
1157 }
1158 return(0);
1159 }
1160 icp = (struct icmp6_hdr *)buf;
1161 #endif
1162 /* get optional information via advanced API */
1163 rcvpktinfo = NULL;
1164 hlimp = NULL;
1165 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1166 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1167 if (cm->cmsg_level == IPPROTO_IPV6 &&
1168 cm->cmsg_type == IPV6_PKTINFO &&
1169 cm->cmsg_len ==
1170 CMSG_LEN(sizeof(struct in6_pktinfo)))
1171 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
1172
1173 if (cm->cmsg_level == IPPROTO_IPV6 &&
1174 cm->cmsg_type == IPV6_HOPLIMIT &&
1175 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1176 hlimp = (int *)CMSG_DATA(cm);
1177 }
1178 if (rcvpktinfo == NULL || hlimp == NULL) {
1179 warnx("failed to get received hop limit or packet info");
1180 #if 0
1181 return(0);
1182 #else
1183 rcvhlim = 0; /*XXX*/
1184 #endif
1185 }
1186 else
1187 rcvhlim = *hlimp;
1188
1189 type = icp->icmp6_type;
1190 code = icp->icmp6_code;
1191 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
1192 || type == ICMP6_DST_UNREACH) {
1193 struct ip6_hdr *hip;
1194 struct udphdr *up;
1195
1196 hip = (struct ip6_hdr *)(icp + 1);
1197 if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) {
1198 if (verbose)
1199 warnx("failed to get upper layer header");
1200 return(0);
1201 }
1202 if (useicmp &&
1203 ((struct icmp6_hdr *)up)->icmp6_id == ident &&
1204 ((struct icmp6_hdr *)up)->icmp6_seq == htons(seq))
1205 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
1206 else if (!useicmp &&
1207 up->uh_sport == htons(srcport) &&
1208 up->uh_dport == htons(port + seq))
1209 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
1210 } else if (useicmp && type == ICMP6_ECHO_REPLY) {
1211 if (icp->icmp6_id == ident &&
1212 icp->icmp6_seq == htons(seq))
1213 return (ICMP6_DST_UNREACH_NOPORT + 1);
1214 }
1215 if (verbose) {
1216 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
1217 u_int8_t *p;
1218 int i;
1219
1220 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1221 sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0)
1222 strlcpy(sbuf, "invalid", sizeof(sbuf));
1223 printf("\n%d bytes from %s to %s", cc, sbuf,
1224 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1225 dbuf, sizeof(dbuf)) : "?");
1226 printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
1227 icp->icmp6_code);
1228 p = (u_int8_t *)(icp + 1);
1229 #define WIDTH 16
1230 for (i = 0; i < cc; i++) {
1231 if (i % WIDTH == 0)
1232 printf("%04x:", i);
1233 if (i % 4 == 0)
1234 printf(" ");
1235 printf("%02x", p[i]);
1236 if (i % WIDTH == WIDTH - 1)
1237 printf("\n");
1238 }
1239 if (cc % WIDTH != 0)
1240 printf("\n");
1241 }
1242 return(0);
1243 }
1244
1245 /*
1246 * Increment pointer until find the UDP or ICMP header.
1247 */
1248 struct udphdr *
1249 get_udphdr(ip6, lim)
1250 struct ip6_hdr *ip6;
1251 u_char *lim;
1252 {
1253 u_char *cp = (u_char *)ip6, nh;
1254 int hlen;
1255
1256 if (cp + sizeof(*ip6) >= lim)
1257 return(NULL);
1258
1259 nh = ip6->ip6_nxt;
1260 cp += sizeof(struct ip6_hdr);
1261
1262 while (lim - cp >= 8) {
1263 switch (nh) {
1264 case IPPROTO_ESP:
1265 case IPPROTO_TCP:
1266 return(NULL);
1267 case IPPROTO_ICMPV6:
1268 return(useicmp ? (struct udphdr *)cp : NULL);
1269 case IPPROTO_UDP:
1270 return(useicmp ? NULL : (struct udphdr *)cp);
1271 case IPPROTO_FRAGMENT:
1272 hlen = sizeof(struct ip6_frag);
1273 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1274 break;
1275 case IPPROTO_AH:
1276 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
1277 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1278 break;
1279 default:
1280 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
1281 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1282 break;
1283 }
1284
1285 cp += hlen;
1286 }
1287
1288 return(NULL);
1289 }
1290
1291 void
1292 print(mhdr, cc)
1293 struct msghdr *mhdr;
1294 int cc;
1295 {
1296 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1297 char hbuf[NI_MAXHOST];
1298
1299 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1300 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1301 strlcpy(hbuf, "invalid", sizeof(hbuf));
1302 if (nflag)
1303 printf(" %s", hbuf);
1304 else if (lflag)
1305 printf(" %s (%s)", inetname((struct sockaddr *)from), hbuf);
1306 else
1307 printf(" %s", inetname((struct sockaddr *)from));
1308
1309 if (verbose) {
1310 #ifdef OLDRAWSOCKET
1311 printf(" %d bytes to %s", cc,
1312 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1313 hbuf, sizeof(hbuf)) : "?");
1314 #else
1315 printf(" %d bytes of data to %s", cc,
1316 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1317 hbuf, sizeof(hbuf)) : "?");
1318 #endif
1319 }
1320 }
1321
1322 /*
1323 * Construct an Internet address representation.
1324 * If the nflag has been supplied, give
1325 * numeric value, otherwise try for symbolic name.
1326 */
1327 const char *
1328 inetname(sa)
1329 struct sockaddr *sa;
1330 {
1331 static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1];
1332 static int first = 1;
1333 char *cp;
1334
1335 if (first && !nflag) {
1336 first = 0;
1337 if (gethostname(domain, sizeof(domain)) == 0 &&
1338 (cp = strchr(domain, '.')))
1339 (void) strlcpy(domain, cp + 1, sizeof(domain));
1340 else
1341 domain[0] = 0;
1342 }
1343 cp = NULL;
1344 if (!nflag) {
1345 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1346 NI_NAMEREQD) == 0) {
1347 if ((cp = strchr(line, '.')) &&
1348 !strcmp(cp + 1, domain))
1349 *cp = 0;
1350 cp = line;
1351 }
1352 }
1353 if (cp)
1354 return cp;
1355
1356 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1357 NI_NUMERICHOST) != 0)
1358 strlcpy(line, "invalid", sizeof(line));
1359 return line;
1360 }
1361
1362 void
1363 usage()
1364 {
1365
1366 fprintf(stderr,
1367 "usage: traceroute6 [-dIlnrv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1368 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");
1369 exit(1);
1370 }
1371