traceroute6.c revision 1.27 1 /* $NetBSD: traceroute6.c,v 1.27 2002/08/30 04:02:44 onoe Exp $ */
2 /* $KAME: traceroute6.c,v 1.58 2002/08/27 00:33: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. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 */
68
69 #if 0
70 #ifndef lint
71 static char copyright[] =
72 "@(#) Copyright (c) 1990, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
74 #endif /* not lint */
75
76 #ifndef lint
77 static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93";
78 #endif /* not lint */
79 #else
80 #include <sys/cdefs.h>
81 #ifndef lint
82 __RCSID("$NetBSD: traceroute6.c,v 1.27 2002/08/30 04:02:44 onoe Exp $");
83 #endif
84 #endif
85
86 /*
87 * traceroute host - trace the route ip packets follow going to "host".
88 *
89 * Attempt to trace the route an ip packet would follow to some
90 * internet host. We find out intermediate hops by launching probe
91 * packets with a small ttl (time to live) then listening for an
92 * icmp "time exceeded" reply from a gateway. We start our probes
93 * with a ttl of one and increase by one until we get an icmp "port
94 * unreachable" (which means we got to "host") or hit a max (which
95 * defaults to 30 hops & can be changed with the -m flag). Three
96 * probes (change with -q flag) are sent at each ttl setting and a
97 * line is printed showing the ttl, address of the gateway and
98 * round trip time of each probe. If the probe answers come from
99 * different gateways, the address of each responding system will
100 * be printed. If there is no response within a 5 sec. timeout
101 * interval (changed with the -w flag), a "*" is printed for that
102 * probe.
103 *
104 * Probe packets are UDP format. We don't want the destination
105 * host to process them so the destination port is set to an
106 * unlikely value (if some clod on the destination is using that
107 * value, it can be changed with the -p flag).
108 *
109 * A sample use might be:
110 *
111 * [yak 71]% traceroute nis.nsf.net.
112 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
113 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
114 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
115 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
116 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
117 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
118 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
119 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
120 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
121 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
122 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
123 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
124 *
125 * Note that lines 2 & 3 are the same. This is due to a buggy
126 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
127 * packets with a zero ttl.
128 *
129 * A more interesting example is:
130 *
131 * [yak 72]% traceroute allspice.lcs.mit.edu.
132 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
133 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
134 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
135 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
136 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
137 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
138 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
139 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
140 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
141 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
142 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
143 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
144 * 12 * * *
145 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
146 * 14 * * *
147 * 15 * * *
148 * 16 * * *
149 * 17 * * *
150 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
151 *
152 * (I start to see why I'm having so much trouble with mail to
153 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
154 * either don't send ICMP "time exceeded" messages or send them
155 * with a ttl too small to reach us. 14 - 17 are running the
156 * MIT C Gateway code that doesn't send "time exceeded"s. God
157 * only knows what's going on with 12.
158 *
159 * The silent gateway 12 in the above may be the result of a bug in
160 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
161 * sends an unreachable message using whatever ttl remains in the
162 * original datagram. Since, for gateways, the remaining ttl is
163 * zero, the icmp "time exceeded" is guaranteed to not make it back
164 * to us. The behavior of this bug is slightly more interesting
165 * when it appears on the destination system:
166 *
167 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
168 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
169 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
170 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
171 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
172 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
173 * 7 * * *
174 * 8 * * *
175 * 9 * * *
176 * 10 * * *
177 * 11 * * *
178 * 12 * * *
179 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
180 *
181 * Notice that there are 12 "gateways" (13 is the final
182 * destination) and exactly the last half of them are "missing".
183 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
184 * is using the ttl from our arriving datagram as the ttl in its
185 * icmp reply. So, the reply will time out on the return path
186 * (with no notice sent to anyone since icmp's aren't sent for
187 * icmp's) until we probe with a ttl that's at least twice the path
188 * length. I.e., rip is really only 7 hops away. A reply that
189 * returns with a ttl of 1 is a clue this problem exists.
190 * Traceroute prints a "!" after the time if the ttl is <= 1.
191 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
192 * non-standard (HPUX) software, expect to see this problem
193 * frequently and/or take care picking the target host of your
194 * probes.
195 *
196 * Other possible annotations after the time are !H, !N, !P (got a host,
197 * network or protocol unreachable, respectively), !S or !F (source
198 * route failed or fragmentation needed -- neither of these should
199 * ever occur and the associated gateway is busted if you see one). If
200 * almost all the probes result in some kind of unreachable, traceroute
201 * will give up and exit.
202 *
203 * Notes
204 * -----
205 * This program must be run by root or be setuid. (I suggest that
206 * you *don't* make it setuid -- casual use could result in a lot
207 * of unnecessary traffic on our poor, congested nets.)
208 *
209 * This program requires a kernel mod that does not appear in any
210 * system available from Berkeley: A raw ip socket using proto
211 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
212 * opposed to data to be wrapped in a ip datagram). See the README
213 * file that came with the source to this program for a description
214 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
215 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
216 * MODIFIED TO RUN THIS PROGRAM.
217 *
218 * The udp port usage may appear bizarre (well, ok, it is bizarre).
219 * The problem is that an icmp message only contains 8 bytes of
220 * data from the original datagram. 8 bytes is the size of a udp
221 * header so, if we want to associate replies with the original
222 * datagram, the necessary information must be encoded into the
223 * udp header (the ip id could be used but there's no way to
224 * interlock with the kernel's assignment of ip id's and, anyway,
225 * it would have taken a lot more kernel hacking to allow this
226 * code to set the ip id). So, to allow two or more users to
227 * use traceroute simultaneously, we use this task's pid as the
228 * source port (the high bit is set to move the port number out
229 * of the "likely" range). To keep track of which probe is being
230 * replied to (so times and/or hop counts don't get confused by a
231 * reply that was delayed in transit), we increment the destination
232 * port number before each probe.
233 *
234 * Don't use this as a coding example. I was trying to find a
235 * routing problem and this code sort-of popped out after 48 hours
236 * without sleep. I was amazed it ever compiled, much less ran.
237 *
238 * I stole the idea for this program from Steve Deering. Since
239 * the first release, I've learned that had I attended the right
240 * IETF working group meetings, I also could have stolen it from Guy
241 * Almes or Matt Mathis. I don't know (or care) who came up with
242 * the idea first. I envy the originators' perspicacity and I'm
243 * glad they didn't keep the idea a secret.
244 *
245 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
246 * enhancements to the original distribution.
247 *
248 * I've hacked up a round-trip-route version of this that works by
249 * sending a loose-source-routed udp datagram through the destination
250 * back to yourself. Unfortunately, SO many gateways botch source
251 * routing, the thing is almost worthless. Maybe one day...
252 *
253 * -- Van Jacobson (van (at) helios.ee.lbl.gov)
254 * Tue Dec 20 03:50:13 PST 1988
255 */
256
257 #include <sys/param.h>
258 #include <sys/time.h>
259 #include <sys/socket.h>
260 #include <sys/uio.h>
261 #include <sys/file.h>
262 #include <sys/ioctl.h>
263 #include <sys/sysctl.h>
264
265 #include <netinet/in.h>
266
267 #include <arpa/inet.h>
268
269 #include <netdb.h>
270 #include <stdio.h>
271 #include <err.h>
272 #ifdef HAVE_POLL
273 #include <poll.h>
274 #endif
275 #include <errno.h>
276 #include <stdlib.h>
277 #include <string.h>
278 #include <unistd.h>
279
280 #include <netinet/ip6.h>
281 #include <netinet/icmp6.h>
282 #include <netinet/udp.h>
283
284 #ifdef IPSEC
285 #include <net/route.h>
286 #include <netinet6/ipsec.h>
287 #endif
288
289 #define DUMMY_PORT 10010
290
291 #define MAXPACKET 65535 /* max ip packet size */
292
293 #ifndef HAVE_GETIPNODEBYNAME
294 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
295 #define freehostent(x)
296 #endif
297
298 /*
299 * format of a (udp) probe packet.
300 */
301 struct opacket {
302 u_char seq; /* sequence number of this packet */
303 u_char hops; /* hop limit of the packet */
304 struct timeval tv; /* time packet left */
305 };
306 struct tv32 {
307 u_int32_t tv32_sec;
308 u_int32_t tv32_usec;
309 };
310
311 u_char packet[512]; /* last inbound (icmp) packet */
312 struct opacket *outpacket; /* last output (udp) packet */
313
314 int main __P((int, char *[]));
315 int wait_for_reply __P((int, struct msghdr *));
316 #ifdef IPSEC
317 #ifdef IPSEC_POLICY_IPSEC
318 int setpolicy __P((int so, char *policy));
319 #endif
320 #endif
321 void send_probe __P((int, int));
322 struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
323 int get_hoplim __P((struct msghdr *));
324 double deltaT __P((struct timeval *, struct timeval *));
325 char *pr_type __P((int));
326 int packet_ok __P((struct msghdr *, int, int));
327 void print __P((struct msghdr *, int));
328 void tvsub __P((struct timeval *, struct timeval *));
329 const char *inetname __P((struct sockaddr *));
330 void usage __P((void));
331
332 int rcvsock; /* receive (icmp) socket file descriptor */
333 int sndsock; /* send (udp) socket file descriptor */
334
335 struct msghdr rcvmhdr;
336 struct iovec rcviov[2];
337 int rcvhlim;
338 struct in6_pktinfo *rcvpktinfo;
339
340 struct sockaddr_in6 Src, Dst, Rcv;
341 int datalen; /* How much data */
342 #define ICMP6ECHOLEN 8
343 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
344 char rtbuf[2064];
345 #ifdef USE_RFC2292BIS
346 struct ip6_rthdr *rth;
347 #endif
348 struct cmsghdr *cmsg;
349
350 char *source = 0;
351 char *hostname;
352
353 int nprobes = 3;
354 int first_hop = 1;
355 int max_hops = 30;
356 u_int16_t srcport;
357 u_int16_t port = 32768+666; /* start udp dest port # for probe packets */
358 u_int16_t ident;
359 int options; /* socket options */
360 int verbose;
361 int waittime = 5; /* time to wait for response (in seconds) */
362 int nflag; /* print addresses numerically */
363 int useicmp;
364 int lflag; /* print both numerical address & hostname */
365
366 int
367 main(argc, argv)
368 int argc;
369 char *argv[];
370 {
371 struct hostent *hp;
372 int error;
373 struct addrinfo hints, *res;
374 int ch, i, on, probe, seq, hops, rcvcmsglen;
375 static u_char *rcvcmsgbuf;
376 char hbuf[NI_MAXHOST], src0[NI_MAXHOST];
377 char *ep;
378 int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM };
379 size_t size = sizeof(max_hops);
380 u_long lport;
381 int minlen;
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 /* revoke privs */
392 seteuid(getuid());
393 setuid(getuid());
394
395 (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &max_hops, &size,
396 NULL, 0);
397
398 /* set a minimum set of socket options */
399 on = 1;
400 /* specify to tell receiving interface */
401 #ifdef IPV6_RECVPKTINFO
402 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
403 sizeof(on)) < 0)
404 err(1, "setsockopt(IPV6_RECVPKTINFO)");
405 #else /* old adv. API */
406 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
407 sizeof(on)) < 0)
408 err(1, "setsockopt(IPV6_PKTINFO)");
409 #endif
410
411 /* specify to tell value of hoplimit field of received IP6 hdr */
412 #ifdef IPV6_RECVHOPLIMIT
413 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
414 sizeof(on)) < 0)
415 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
416 #else /* old adv. API */
417 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
418 sizeof(on)) < 0)
419 err(1, "setsockopt(IPV6_HOPLIMIT)");
420 #endif
421
422 seq = 0;
423
424 while ((ch = getopt(argc, argv, "df:g:Ilm:np:q:rs:w:v")) != -1)
425 switch (ch) {
426 case 'd':
427 options |= SO_DEBUG;
428 break;
429 case 'f':
430 ep = NULL;
431 first_hop = strtoul(optarg, &ep, 0);
432 if (!*argv || *ep) {
433 fprintf(stderr,
434 "traceroute6: invalid min hoplimit.\n");
435 exit(1);
436 }
437 if (first_hop > max_hops) {
438 fprintf(stderr,
439 "traceroute6: min hoplimit must be <= %d.\n",
440 max_hops);
441 exit(1);
442 }
443 break;
444 case 'g':
445 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
446 if (hp == NULL) {
447 fprintf(stderr,
448 "traceroute6: unknown host %s\n", optarg);
449 exit(1);
450 }
451 #ifdef USE_RFC2292BIS
452 if (rth == NULL) {
453 /*
454 * XXX: We can't detect the number of
455 * intermediate nodes yet.
456 */
457 if ((rth = inet6_rth_init((void *)rtbuf,
458 sizeof(rtbuf), IPV6_RTHDR_TYPE_0,
459 0)) == NULL) {
460 fprintf(stderr,
461 "inet6_rth_init failed.\n");
462 exit(1);
463 }
464 }
465 if (inet6_rth_add((void *)rth,
466 (struct in6_addr *)hp->h_addr)) {
467 fprintf(stderr,
468 "inet6_rth_add failed for %s\n",
469 optarg);
470 exit(1);
471 }
472 #else /* old advanced API */
473 if (cmsg == NULL)
474 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
475 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr,
476 IPV6_RTHDR_LOOSE);
477 #endif
478 freehostent(hp);
479 break;
480 case 'I':
481 useicmp++;
482 ident = htons(getpid() & 0xffff); /* same as ping6 */
483 break;
484 case 'l':
485 lflag++;
486 break;
487 case 'm':
488 ep = NULL;
489 max_hops = strtoul(optarg, &ep, 0);
490 if (!*argv || *ep) {
491 fprintf(stderr,
492 "traceroute6: invalid max hoplimit.\n");
493 exit(1);
494 }
495 if (max_hops < first_hop) {
496 fprintf(stderr,
497 "traceroute6: max hoplimit must be >= %d.\n",
498 first_hop);
499 exit(1);
500 }
501 break;
502 case 'n':
503 nflag++;
504 break;
505 case 'p':
506 ep = NULL;
507 lport = strtoul(optarg, &ep, 0);
508 if (!*argv || *ep) {
509 fprintf(stderr, "traceroute6: port.\n");
510 exit(1);
511 }
512 if (lport == 0 || lport != (lport & 0xffff)) {
513 fprintf(stderr,
514 "traceroute6: port out of range.\n");
515 exit(1);
516 }
517 port = lport & 0xffff;
518 break;
519 case 'q':
520 ep = NULL;
521 nprobes = strtoul(optarg, &ep, 0);
522 if (!*argv || *ep) {
523 fprintf(stderr,
524 "traceroute6: invalid nprobes.\n");
525 exit(1);
526 }
527 if (nprobes < 1) {
528 fprintf(stderr,
529 "traceroute6: nprobes must be >0.\n");
530 exit(1);
531 }
532 break;
533 case 'r':
534 options |= SO_DONTROUTE;
535 break;
536 case 's':
537 /*
538 * set the ip source address of the outbound
539 * probe (e.g., on a multi-homed host).
540 */
541 source = optarg;
542 break;
543 case 'v':
544 verbose++;
545 break;
546 case 'w':
547 ep = NULL;
548 waittime = strtoul(optarg, &ep, 0);
549 if (!*argv || *ep) {
550 fprintf(stderr,
551 "traceroute6: invalid wait time.\n");
552 exit(1);
553 }
554 if (waittime <= 1) {
555 fprintf(stderr,
556 "traceroute6: wait must be >1 sec.\n");
557 exit(1);
558 }
559 break;
560 default:
561 usage();
562 }
563 argc -= optind;
564 argv += optind;
565
566 if (argc < 1 || argc > 2)
567 usage();
568
569 #if 1
570 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
571 #else
572 setlinebuf (stdout);
573 #endif
574
575 memset(&hints, 0, sizeof(hints));
576 hints.ai_family = PF_INET6;
577 hints.ai_socktype = SOCK_RAW;
578 hints.ai_protocol = IPPROTO_ICMPV6;
579 hints.ai_flags = AI_CANONNAME;
580 error = getaddrinfo(*argv, NULL, &hints, &res);
581 if (error) {
582 fprintf(stderr,
583 "traceroute6: %s\n", gai_strerror(error));
584 exit(1);
585 }
586 if (res->ai_addrlen != sizeof(Dst)) {
587 fprintf(stderr,
588 "traceroute6: size of sockaddr mismatch\n");
589 exit(1);
590 }
591 memcpy(&Dst, res->ai_addr, res->ai_addrlen);
592 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
593 if (!hostname) {
594 fprintf(stderr, "traceroute6: not enough core\n");
595 exit(1);
596 }
597 if (res->ai_next) {
598 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
599 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
600 strlcpy(hbuf, "?", sizeof(hbuf));
601 fprintf(stderr, "traceroute6: Warning: %s has multiple "
602 "addresses; using %s\n", hostname, hbuf);
603 }
604
605 if (*++argv) {
606 ep = NULL;
607 datalen = strtoul(*argv, &ep, 0);
608 if (!*argv || *ep) {
609 fprintf(stderr,
610 "traceroute6: invalid packet length.\n");
611 exit(1);
612 }
613 }
614 if (useicmp)
615 minlen = ICMP6ECHOLEN + sizeof(struct tv32);
616 else
617 minlen = sizeof(struct opacket);
618 if (datalen < minlen)
619 datalen = minlen;
620 else if (datalen >= MAXPACKET) {
621 fprintf(stderr,
622 "traceroute6: packet size must be %d <= s < %ld.\n",
623 minlen, (long)MAXPACKET);
624 exit(1);
625 }
626 outpacket = (struct opacket *)malloc((unsigned)datalen);
627 if (! outpacket) {
628 perror("malloc");
629 exit(1);
630 }
631 (void) bzero((char *)outpacket, datalen);
632
633 /* initialize msghdr for receiving packets */
634 rcviov[0].iov_base = (caddr_t)packet;
635 rcviov[0].iov_len = sizeof(packet);
636 rcvmhdr.msg_name = (caddr_t)&Rcv;
637 rcvmhdr.msg_namelen = sizeof(Rcv);
638 rcvmhdr.msg_iov = rcviov;
639 rcvmhdr.msg_iovlen = 1;
640 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo))
641 + CMSG_SPACE(sizeof(int));
642 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
643 fprintf(stderr, "traceroute6: malloc failed\n");
644 exit(1);
645 }
646 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
647 rcvmhdr.msg_controllen = rcvcmsglen;
648
649 if (options & SO_DEBUG)
650 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
651 (char *)&on, sizeof(on));
652 if (options & SO_DONTROUTE)
653 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
654 (char *)&on, sizeof(on));
655 #ifdef IPSEC
656 #ifdef IPSEC_POLICY_IPSEC
657 /*
658 * do not raise error even if setsockopt fails, kernel may have ipsec
659 * turned off.
660 */
661 if (setpolicy(rcvsock, "in bypass") < 0)
662 errx(1, "%s", ipsec_strerror());
663 if (setpolicy(rcvsock, "out bypass") < 0)
664 errx(1, "%s", ipsec_strerror());
665 #else
666 {
667 int level = IPSEC_LEVEL_NONE;
668
669 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
670 sizeof(level));
671 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
672 sizeof(level));
673 #ifdef IP_AUTH_TRANS_LEVEL
674 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
675 sizeof(level));
676 #else
677 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
678 sizeof(level));
679 #endif
680 #ifdef IP_AUTH_NETWORK_LEVEL
681 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
682 sizeof(level));
683 #endif
684 }
685 #endif /*IPSEC_POLICY_IPSEC*/
686 #endif /*IPSEC*/
687
688 /*
689 * Send UDP or ICMP
690 */
691 if (useicmp) {
692 sndsock = rcvsock;
693 } else {
694 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
695 perror("socket(SOCK_DGRAM)");
696 exit(5);
697 }
698 }
699 #ifdef SO_SNDBUF
700 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
701 sizeof(datalen)) < 0) {
702 perror("setsockopt(SO_SNDBUF)");
703 exit(6);
704 }
705 #endif /* SO_SNDBUF */
706 if (options & SO_DEBUG)
707 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
708 (char *)&on, sizeof(on));
709 if (options & SO_DONTROUTE)
710 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
711 (char *)&on, sizeof(on));
712 #ifdef USE_RFC2292BIS
713 if (rth) {/* XXX: there is no library to finalize the header... */
714 rth->ip6r_len = rth->ip6r_segleft * 2;
715 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR,
716 (void *)rth, (rth->ip6r_len + 1) << 3)) {
717 fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n",
718 strerror(errno));
719 exit(1);
720 }
721 }
722 #else /* old advanced API */
723 if (cmsg != NULL) {
724 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
725 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
726 rtbuf, cmsg->cmsg_len) < 0) {
727 fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n",
728 strerror(errno));
729 exit(1);
730 }
731 }
732 #endif /* USE_RFC2292BIS */
733 #ifdef IPSEC
734 #ifdef IPSEC_POLICY_IPSEC
735 /*
736 * do not raise error even if setsockopt fails, kernel may have ipsec
737 * turned off.
738 */
739 if (setpolicy(sndsock, "in bypass") < 0)
740 errx(1, "%s", ipsec_strerror());
741 if (setpolicy(sndsock, "out bypass") < 0)
742 errx(1, "%s", ipsec_strerror());
743 #else
744 {
745 int level = IPSEC_LEVEL_BYPASS;
746
747 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
748 sizeof(level));
749 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
750 sizeof(level));
751 #ifdef IP_AUTH_TRANS_LEVEL
752 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
753 sizeof(level));
754 #else
755 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
756 sizeof(level));
757 #endif
758 #ifdef IP_AUTH_NETWORK_LEVEL
759 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
760 sizeof(level));
761 #endif
762 }
763 #endif /*IPSEC_POLICY_IPSEC*/
764 #endif /*IPSEC*/
765
766 /*
767 * Source selection
768 */
769 bzero(&Src, sizeof(Src));
770 if (source) {
771 struct addrinfo hints, *res;
772 int error;
773
774 memset(&hints, 0, sizeof(hints));
775 hints.ai_family = AF_INET6;
776 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
777 hints.ai_flags = AI_NUMERICHOST;
778 error = getaddrinfo(source, "0", &hints, &res);
779 if (error) {
780 printf("traceroute6: %s: %s\n", source,
781 gai_strerror(error));
782 exit(1);
783 }
784 if (res->ai_addrlen > sizeof(Src)) {
785 printf("traceroute6: %s: %s\n", source,
786 gai_strerror(error));
787 exit(1);
788 }
789 memcpy(&Src, res->ai_addr, res->ai_addrlen);
790 freeaddrinfo(res);
791 } else {
792 struct sockaddr_in6 Nxt;
793 int dummy, len;
794
795 Nxt = Dst;
796 Nxt.sin6_port = htons(DUMMY_PORT);
797 if (cmsg != NULL)
798 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
799 sizeof(Nxt.sin6_addr));
800 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
801 perror("socket");
802 exit(1);
803 }
804 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
805 perror("connect");
806 exit(1);
807 }
808 len = sizeof(Src);
809 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
810 perror("getsockname");
811 exit(1);
812 }
813 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
814 src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) {
815 fprintf(stderr, "getnameinfo failed for source\n");
816 exit(1);
817 }
818 source = src0;
819 close(dummy);
820 }
821
822 Src.sin6_port = htons(0);
823 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
824 perror("bind");
825 exit(1);
826 }
827
828 {
829 int len;
830
831 len = sizeof(Src);
832 if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) {
833 perror("getsockname");
834 exit(1);
835 }
836 srcport = ntohs(Src.sin6_port);
837 }
838
839 /*
840 * Message to users
841 */
842 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf,
843 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
844 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
845 fprintf(stderr, "traceroute6");
846 fprintf(stderr, " to %s (%s)", hostname, hbuf);
847 if (source)
848 fprintf(stderr, " from %s", source);
849 fprintf(stderr, ", %d hops max, %d byte packets\n",
850 max_hops, datalen);
851 (void) fflush(stderr);
852
853 if (first_hop > 1)
854 printf("Skipping %d intermediate hops\n", first_hop - 1);
855
856 /*
857 * Main loop
858 */
859 for (hops = first_hop; hops <= max_hops; ++hops) {
860 struct in6_addr lastaddr;
861 int got_there = 0;
862 int unreachable = 0;
863
864 printf("%2d ", hops);
865 bzero(&lastaddr, sizeof(lastaddr));
866 for (probe = 0; probe < nprobes; ++probe) {
867 int cc;
868 struct timeval t1, t2;
869
870 (void) gettimeofday(&t1, NULL);
871 send_probe(++seq, hops);
872 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
873 (void) gettimeofday(&t2, NULL);
874 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
875 if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
876 &lastaddr)) {
877 print(&rcvmhdr, cc);
878 lastaddr = Rcv.sin6_addr;
879 }
880 printf(" %g ms", deltaT(&t1, &t2));
881 switch (i - 1) {
882 case ICMP6_DST_UNREACH_NOROUTE:
883 ++unreachable;
884 printf(" !N");
885 break;
886 case ICMP6_DST_UNREACH_ADMIN:
887 ++unreachable;
888 printf(" !P");
889 break;
890 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
891 ++unreachable;
892 printf(" !S");
893 break;
894 case ICMP6_DST_UNREACH_ADDR:
895 ++unreachable;
896 printf(" !A");
897 break;
898 case ICMP6_DST_UNREACH_NOPORT:
899 if (rcvhlim >= 0 &&
900 rcvhlim <= 1)
901 printf(" !");
902 ++got_there;
903 break;
904 }
905 break;
906 }
907 }
908 if (cc == 0)
909 printf(" *");
910 (void) fflush(stdout);
911 }
912 putchar('\n');
913 if (got_there ||
914 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
915 exit(0);
916 }
917 }
918
919 exit(0);
920 }
921
922 int
923 wait_for_reply(sock, mhdr)
924 int sock;
925 struct msghdr *mhdr;
926 {
927 #ifdef HAVE_POLL
928 struct pollfd pfd[1];
929 int cc = 0;
930
931 pfd[0].fd = sock;
932 pfd[0].events = POLLIN;
933 pfd[0].revents = 0;
934
935 if (poll(pfd, 1, waittime * 1000) > 0)
936 cc = recvmsg(rcvsock, mhdr, 0);
937
938 return(cc);
939 #else
940 fd_set *fdsp;
941 struct timeval wait;
942 int cc = 0, fdsn;
943
944 fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
945 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
946 err(1, "malloc");
947 memset(fdsp, 0, fdsn);
948 FD_SET(sock, fdsp);
949 wait.tv_sec = waittime; wait.tv_usec = 0;
950
951 if (select(sock+1, fdsp, (fd_set *)0, (fd_set *)0, &wait) > 0)
952 cc = recvmsg(rcvsock, mhdr, 0);
953
954 free(fdsp);
955 return(cc);
956 #endif
957 }
958
959 #ifdef IPSEC
960 #ifdef IPSEC_POLICY_IPSEC
961 int
962 setpolicy(so, policy)
963 int so;
964 char *policy;
965 {
966 char *buf;
967
968 buf = ipsec_set_policy(policy, strlen(policy));
969 if (buf == NULL) {
970 warnx("%s", ipsec_strerror());
971 return -1;
972 }
973 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
974 buf, ipsec_get_policylen(buf));
975
976 free(buf);
977
978 return 0;
979 }
980 #endif
981 #endif
982
983 void
984 send_probe(seq, hops)
985 int seq, hops;
986 {
987 int i;
988
989 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
990 (char *)&hops, sizeof(hops)) < 0) {
991 perror("setsockopt IPV6_UNICAST_HOPS");
992 }
993
994 Dst.sin6_port = htons(port + seq);
995
996 if (useicmp) {
997 struct icmp6_hdr *icp = (struct icmp6_hdr *)outpacket;
998 struct timeval tv;
999 struct tv32 *tv32;
1000
1001 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1002 icp->icmp6_code = 0;
1003 icp->icmp6_cksum = 0;
1004 icp->icmp6_id = ident;
1005 icp->icmp6_seq = htons(seq);
1006 (void) gettimeofday(&tv, NULL);
1007 tv32 = (struct tv32 *)((u_int8_t *)outpacket + ICMP6ECHOLEN);
1008 tv32->tv32_sec = htonl(tv.tv_sec);
1009 tv32->tv32_usec = htonl(tv.tv_usec);
1010 } else {
1011 struct opacket *op = outpacket;
1012 op->seq = seq;
1013 op->hops = hops;
1014 (void) gettimeofday(&op->tv, NULL);
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 %d 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 register 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 register 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 int i;
1217 u_int8_t *p;
1218 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
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 * Subtract 2 timeval structs: out = out - in.
1324 * Out is assumed to be >= in.
1325 */
1326 void
1327 tvsub(out, in)
1328 register struct timeval *out, *in;
1329 {
1330
1331 if ((out->tv_usec -= in->tv_usec) < 0) {
1332 out->tv_sec--;
1333 out->tv_usec += 1000000;
1334 }
1335 out->tv_sec -= in->tv_sec;
1336 }
1337
1338 /*
1339 * Construct an Internet address representation.
1340 * If the nflag has been supplied, give
1341 * numeric value, otherwise try for symbolic name.
1342 */
1343 const char *
1344 inetname(sa)
1345 struct sockaddr *sa;
1346 {
1347 register char *cp;
1348 static char line[NI_MAXHOST];
1349 static char domain[MAXHOSTNAMELEN + 1];
1350 static int first = 1;
1351
1352 if (first && !nflag) {
1353 first = 0;
1354 if (gethostname(domain, sizeof(domain)) == 0 &&
1355 (cp = strchr(domain, '.')))
1356 (void) strlcpy(domain, cp + 1, sizeof(domain));
1357 else
1358 domain[0] = 0;
1359 }
1360 cp = NULL;
1361 if (!nflag) {
1362 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1363 NI_NAMEREQD) == 0) {
1364 if ((cp = strchr(line, '.')) &&
1365 !strcmp(cp + 1, domain))
1366 *cp = 0;
1367 cp = line;
1368 }
1369 }
1370 if (cp)
1371 return cp;
1372
1373 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1374 NI_NUMERICHOST) != 0)
1375 strlcpy(line, "invalid", sizeof(line));
1376 return line;
1377 }
1378
1379 void
1380 usage()
1381 {
1382
1383 fprintf(stderr,
1384 "usage: traceroute6 [-dIlnrv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1385 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");
1386 exit(1);
1387 }
1388