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