1 /* $KAME: traceroute6.c,v 1.68 2004/01/25 11:16:12 suz Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * "@(#) Copyright (c) 1990, 1993
64 * The Regents of the University of California. All rights reserved.
66 * $FreeBSD: src/usr.sbin/traceroute6/traceroute6.c,v 1.22 2008/02/10 21:06:38 dwmalone Exp $
70 * traceroute host - trace the route ip packets follow going to "host".
72 * Attempt to trace the route an ip packet would follow to some
73 * internet host. We find out intermediate hops by launching probe
74 * packets with a small ttl (time to live) then listening for an
75 * icmp "time exceeded" reply from a gateway. We start our probes
76 * with a ttl of one and increase by one until we get an icmp "port
77 * unreachable" (which means we got to "host") or hit a max (which
78 * defaults to 30 hops & can be changed with the -m flag). Three
79 * probes (change with -q flag) are sent at each ttl setting and a
80 * line is printed showing the ttl, address of the gateway and
81 * round trip time of each probe. If the probe answers come from
82 * different gateways, the address of each responding system will
83 * be printed. If there is no response within a 5 sec. timeout
84 * interval (changed with the -w flag), a "*" is printed for that
87 * Probe packets are UDP format. We don't want the destination
88 * host to process them so the destination port is set to an
89 * unlikely value (if some clod on the destination is using that
90 * value, it can be changed with the -p flag).
92 * A sample use might be:
94 * [yak 71]% traceroute nis.nsf.net.
95 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
96 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
97 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
98 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
99 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
100 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
101 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
102 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
103 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
104 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
105 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
106 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
108 * Note that lines 2 & 3 are the same. This is due to a buggy
109 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
110 * packets with a zero ttl.
112 * A more interesting example is:
114 * [yak 72]% traceroute allspice.lcs.mit.edu.
115 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
116 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
117 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
118 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
119 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
120 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
121 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
122 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
123 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
124 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
125 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
126 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
128 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
133 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
135 * (I start to see why I'm having so much trouble with mail to
136 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
137 * either don't send ICMP "time exceeded" messages or send them
138 * with a ttl too small to reach us. 14 - 17 are running the
139 * MIT C Gateway code that doesn't send "time exceeded"s. God
140 * only knows what's going on with 12.
142 * The silent gateway 12 in the above may be the result of a bug in
143 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
144 * sends an unreachable message using whatever ttl remains in the
145 * original datagram. Since, for gateways, the remaining ttl is
146 * zero, the icmp "time exceeded" is guaranteed to not make it back
147 * to us. The behavior of this bug is slightly more interesting
148 * when it appears on the destination system:
150 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
151 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
152 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
153 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
154 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
155 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
162 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
164 * Notice that there are 12 "gateways" (13 is the final
165 * destination) and exactly the last half of them are "missing".
166 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
167 * is using the ttl from our arriving datagram as the ttl in its
168 * icmp reply. So, the reply will time out on the return path
169 * (with no notice sent to anyone since icmp's aren't sent for
170 * icmp's) until we probe with a ttl that's at least twice the path
171 * length. I.e., rip is really only 7 hops away. A reply that
172 * returns with a ttl of 1 is a clue this problem exists.
173 * Traceroute prints a "!" after the time if the ttl is <= 1.
174 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
175 * non-standard (HPUX) software, expect to see this problem
176 * frequently and/or take care picking the target host of your
179 * Other possible annotations after the time are !H, !N, !P (got a host,
180 * network or protocol unreachable, respectively), !S or !F (source
181 * route failed or fragmentation needed -- neither of these should
182 * ever occur and the associated gateway is busted if you see one). If
183 * almost all the probes result in some kind of unreachable, traceroute
184 * will give up and exit.
188 * This program must be run by root or be setuid. (I suggest that
189 * you *don't* make it setuid -- casual use could result in a lot
190 * of unnecessary traffic on our poor, congested nets.)
192 * This program requires a kernel mod that does not appear in any
193 * system available from Berkeley: A raw ip socket using proto
194 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
195 * opposed to data to be wrapped in an ip datagram). See the README
196 * file that came with the source to this program for a description
197 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
198 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
199 * MODIFIED TO RUN THIS PROGRAM.
201 * The udp port usage may appear bizarre (well, ok, it is bizarre).
202 * The problem is that an icmp message only contains 8 bytes of
203 * data from the original datagram. 8 bytes is the size of a udp
204 * header so, if we want to associate replies with the original
205 * datagram, the necessary information must be encoded into the
206 * udp header (the ip id could be used but there's no way to
207 * interlock with the kernel's assignment of ip id's and, anyway,
208 * it would have taken a lot more kernel hacking to allow this
209 * code to set the ip id). So, to allow two or more users to
210 * use traceroute simultaneously, we use this task's pid as the
211 * source port (the high bit is set to move the port number out
212 * of the "likely" range). To keep track of which probe is being
213 * replied to (so times and/or hop counts don't get confused by a
214 * reply that was delayed in transit), we increment the destination
215 * port number before each probe.
217 * Don't use this as a coding example. I was trying to find a
218 * routing problem and this code sort-of popped out after 48 hours
219 * without sleep. I was amazed it ever compiled, much less ran.
221 * I stole the idea for this program from Steve Deering. Since
222 * the first release, I've learned that had I attended the right
223 * IETF working group meetings, I also could have stolen it from Guy
224 * Almes or Matt Mathis. I don't know (or care) who came up with
225 * the idea first. I envy the originators' perspicacity and I'm
226 * glad they didn't keep the idea a secret.
228 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
229 * enhancements to the original distribution.
231 * I've hacked up a round-trip-route version of this that works by
232 * sending a loose-source-routed udp datagram through the destination
233 * back to yourself. Unfortunately, SO many gateways botch source
234 * routing, the thing is almost worthless. Maybe one day...
236 * -- Van Jacobson (van@helios.ee.lbl.gov)
237 * Tue Dec 20 03:50:13 PST 1988
240 #include <sys/param.h>
241 #include <sys/time.h>
242 #include <sys/socket.h>
244 #include <sys/file.h>
245 #include <sys/ioctl.h>
246 #include <sys/sysctl.h>
248 #include <netinet/in.h>
250 #include <arpa/inet.h>
263 #include <netinet/ip6.h>
264 #include <netinet/icmp6.h>
265 #include <netinet/udp.h>
267 #define DUMMY_PORT 10010
269 #define MAXPACKET 65535 /* max ip packet size */
271 #ifndef HAVE_GETIPNODEBYNAME
272 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
273 #define freehostent(x)
277 * format of a (udp) probe packet.
285 u_char seq
; /* sequence number of this packet */
286 u_char hops
; /* hop limit of the packet */
288 struct tv32 tv
; /* time packet left */
289 } __attribute__((__packed__
));
291 static u_char packet
[512]; /* last inbound (icmp) packet */
292 static struct opacket
*outpacket
; /* last output (udp) packet */
294 static int wait_for_reply(int, struct msghdr
*);
295 static void send_probe(int, u_long
);
296 static void *get_uphdr(struct ip6_hdr
*, u_char
*);
297 static double deltaT(struct timeval
*, struct timeval
*);
298 static char *pr_type(int);
299 static int packet_ok(struct msghdr
*, int, int);
300 static void print(struct msghdr
*, int);
301 static const char *inetname(struct sockaddr
*);
302 static void usage(void) __dead2
;
304 static int rcvsock
; /* receive (icmp) socket file descriptor */
305 static int sndsock
; /* send (udp) socket file descriptor */
307 static struct msghdr rcvmhdr
;
308 static struct iovec rcviov
[2];
310 static struct in6_pktinfo
*rcvpktinfo
;
312 static struct sockaddr_in6 Src
, Dst
, Rcv
;
313 static u_long datalen
; /* How much data */
314 #define ICMP6ECHOLEN 8
316 static char *source
= NULL
;
317 static char *hostname
;
319 static u_long nprobes
= 3;
320 static u_long first_hop
= 1;
321 static u_long max_hops
= 30;
322 static u_int16_t srcport
;
323 static u_int16_t port
= 32768+666; /* start udp dest port # for probe packets */
324 static u_int16_t ident
;
325 static int options
; /* socket options */
327 static int waittime
= 5; /* time to wait for response (in seconds) */
328 static int nflag
; /* print addresses numerically */
329 static int useproto
= IPPROTO_UDP
; /* protocol to use to send packet */
330 static int lflag
; /* print both numerical address & hostname */
333 main(int argc
, char **argv
)
335 int mib
[4] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, IPV6CTL_DEFHLIM
};
336 char hbuf
[NI_MAXHOST
], src0
[NI_MAXHOST
], *ep
;
337 int ch
, i
, on
= 1, seq
, rcvcmsglen
, error
, minlen
;
338 struct addrinfo hints
, *res
;
339 static u_char
*rcvcmsgbuf
;
340 u_long probe
, hops
, lport
;
348 if ((rcvsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
349 perror("socket(ICMPv6)");
354 (void) sysctl(mib
, NELEM(mib
), &i
, &size
, NULL
, 0);
357 /* specify to tell receiving interface */
358 #ifdef IPV6_RECVPKTINFO
359 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
361 err(1, "setsockopt(IPV6_RECVPKTINFO)");
362 #else /* old adv. API */
363 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
365 err(1, "setsockopt(IPV6_PKTINFO)");
368 /* specify to tell value of hoplimit field of received IP6 hdr */
369 #ifdef IPV6_RECVHOPLIMIT
370 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
372 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
373 #else /* old adv. API */
374 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
376 err(1, "setsockopt(IPV6_HOPLIMIT)");
381 while ((ch
= getopt(argc
, argv
, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1)
389 first_hop
= strtoul(optarg
, &ep
, 0);
390 if (errno
|| !*optarg
|| *ep
|| first_hop
> 255) {
392 "traceroute6: invalid min hoplimit.\n");
397 hp
= getipnodebyname(optarg
, AF_INET6
, 0, &h_errno
);
400 "traceroute6: unknown host %s\n", optarg
);
406 useproto
= IPPROTO_ICMPV6
;
407 ident
= htons(getpid() & 0xffff); /* same as ping6 */
415 max_hops
= strtoul(optarg
, &ep
, 0);
416 if (errno
|| !*optarg
|| *ep
|| max_hops
> 255) {
418 "traceroute6: invalid max hoplimit.\n");
426 useproto
= IPPROTO_NONE
;
431 lport
= strtoul(optarg
, &ep
, 0);
432 if (errno
|| !*optarg
|| *ep
) {
433 fprintf(stderr
, "traceroute6: invalid port.\n");
436 if (lport
== 0 || lport
!= (lport
& 0xffff)) {
438 "traceroute6: port out of range.\n");
441 port
= lport
& 0xffff;
446 nprobes
= strtoul(optarg
, &ep
, 0);
447 if (errno
|| !*optarg
|| *ep
) {
449 "traceroute6: invalid nprobes.\n");
454 "traceroute6: nprobes must be >0.\n");
459 options
|= SO_DONTROUTE
;
463 * set the ip source address of the outbound
464 * probe (e.g., on a multi-homed host).
472 useproto
= IPPROTO_UDP
;
477 waittime
= strtoul(optarg
, &ep
, 0);
478 if (errno
|| !*optarg
|| *ep
) {
480 "traceroute6: invalid wait time.\n");
485 "traceroute6: wait must be >= 1 sec.\n");
496 * Open socket to send probe packets.
503 if ((sndsock
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
504 perror("socket(SOCK_DGRAM)");
509 if ((sndsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_NONE
)) < 0) {
510 perror("socket(SOCK_RAW)");
515 fprintf(stderr
, "traceroute6: unknown probe protocol %d",
519 if (max_hops
< first_hop
) {
521 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
527 if (setresuid(uid
, uid
, uid
) == -1) {
533 if (argc
< 1 || argc
> 2)
537 setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
);
542 memset(&hints
, 0, sizeof(hints
));
543 hints
.ai_family
= PF_INET6
;
544 hints
.ai_socktype
= SOCK_RAW
;
545 hints
.ai_protocol
= IPPROTO_ICMPV6
;
546 hints
.ai_flags
= AI_CANONNAME
;
547 error
= getaddrinfo(*argv
, NULL
, &hints
, &res
);
550 "traceroute6: %s\n", gai_strerror(error
));
553 if (res
->ai_addrlen
!= sizeof(Dst
)) {
555 "traceroute6: size of sockaddr mismatch\n");
558 memcpy(&Dst
, res
->ai_addr
, res
->ai_addrlen
);
559 hostname
= res
->ai_canonname
? strdup(res
->ai_canonname
) : *argv
;
561 fprintf(stderr
, "traceroute6: not enough core\n");
565 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
,
566 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
567 strlcpy(hbuf
, "?", sizeof(hbuf
));
568 fprintf(stderr
, "traceroute6: Warning: %s has multiple "
569 "addresses; using %s\n", hostname
, hbuf
);
575 datalen
= strtoul(*argv
, &ep
, 0);
576 if (errno
|| !*argv
|| *ep
) {
578 "traceroute6: invalid packet length.\n");
584 minlen
= ICMP6ECHOLEN
+ sizeof(struct tv32
);
587 minlen
= sizeof(struct opacket
);
594 fprintf(stderr
, "traceroute6: unknown probe protocol %d.\n",
598 if (datalen
< minlen
)
600 else if (datalen
>= MAXPACKET
) {
602 "traceroute6: packet size must be %d <= s < %ld.\n",
603 minlen
, (long)MAXPACKET
);
606 outpacket
= (struct opacket
*)malloc((unsigned)datalen
);
611 (void) bzero((char *)outpacket
, datalen
);
613 /* initialize msghdr for receiving packets */
614 rcviov
[0].iov_base
= (caddr_t
)packet
;
615 rcviov
[0].iov_len
= sizeof(packet
);
616 rcvmhdr
.msg_name
= (caddr_t
)&Rcv
;
617 rcvmhdr
.msg_namelen
= sizeof(Rcv
);
618 rcvmhdr
.msg_iov
= rcviov
;
619 rcvmhdr
.msg_iovlen
= 1;
620 rcvcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
621 CMSG_SPACE(sizeof(int));
622 if ((rcvcmsgbuf
= malloc(rcvcmsglen
)) == NULL
) {
623 fprintf(stderr
, "traceroute6: malloc failed\n");
626 rcvmhdr
.msg_control
= (caddr_t
) rcvcmsgbuf
;
627 rcvmhdr
.msg_controllen
= rcvcmsglen
;
629 if (options
& SO_DEBUG
)
630 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DEBUG
,
631 (char *)&on
, sizeof(on
));
632 if (options
& SO_DONTROUTE
)
633 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DONTROUTE
,
634 (char *)&on
, sizeof(on
));
637 if (setsockopt(sndsock
, SOL_SOCKET
, SO_SNDBUF
, (char *)&i
,
638 sizeof(i
)) < 0 && useproto
!= IPPROTO_NONE
) {
639 perror("setsockopt(SO_SNDBUF)");
642 #endif /* SO_SNDBUF */
643 if (options
& SO_DEBUG
)
644 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DEBUG
,
645 (char *)&on
, sizeof(on
));
646 if (options
& SO_DONTROUTE
)
647 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DONTROUTE
,
648 (char *)&on
, sizeof(on
));
652 bzero(&Src
, sizeof(Src
));
654 struct addrinfo hints
, *res
;
657 memset(&hints
, 0, sizeof(hints
));
658 hints
.ai_family
= AF_INET6
;
659 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
660 hints
.ai_flags
= AI_NUMERICHOST
;
661 error
= getaddrinfo(source
, "0", &hints
, &res
);
663 printf("traceroute6: %s: %s\n", source
,
664 gai_strerror(error
));
667 if (res
->ai_addrlen
> sizeof(Src
)) {
668 printf("traceroute6: %s: %s\n", source
,
669 gai_strerror(error
));
672 memcpy(&Src
, res
->ai_addr
, res
->ai_addrlen
);
675 struct sockaddr_in6 Nxt
;
680 Nxt
.sin6_port
= htons(DUMMY_PORT
);
681 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
685 if (connect(dummy
, (struct sockaddr
*)&Nxt
, Nxt
.sin6_len
) < 0) {
690 if (getsockname(dummy
, (struct sockaddr
*)&Src
, &len
) < 0) {
691 perror("getsockname");
694 if (getnameinfo((struct sockaddr
*)&Src
, Src
.sin6_len
,
695 src0
, sizeof(src0
), NULL
, 0, NI_NUMERICHOST
)) {
696 fprintf(stderr
, "getnameinfo failed for source\n");
703 Src
.sin6_port
= htons(0);
704 if (bind(sndsock
, (struct sockaddr
*)&Src
, Src
.sin6_len
) < 0) {
713 if (getsockname(sndsock
, (struct sockaddr
*)&Src
, &len
) < 0) {
714 perror("getsockname");
717 srcport
= ntohs(Src
.sin6_port
);
723 if (getnameinfo((struct sockaddr
*)&Dst
, Dst
.sin6_len
, hbuf
,
724 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
))
725 strlcpy(hbuf
, "(invalid)", sizeof(hbuf
));
726 fprintf(stderr
, "traceroute6");
727 fprintf(stderr
, " to %s (%s)", hostname
, hbuf
);
729 fprintf(stderr
, " from %s", source
);
730 fprintf(stderr
, ", %lu hops max, %lu byte packets\n",
732 (void) fflush(stderr
);
735 printf("Skipping %lu intermediate hops\n", first_hop
- 1);
740 for (hops
= first_hop
; hops
<= max_hops
; ++hops
) {
741 struct in6_addr lastaddr
;
745 printf("%2lu ", hops
);
746 bzero(&lastaddr
, sizeof(lastaddr
));
747 for (probe
= 0; probe
< nprobes
; ++probe
) {
749 struct timeval t1
, t2
;
751 (void) gettimeofday(&t1
, NULL
);
752 send_probe(++seq
, hops
);
753 while ((cc
= wait_for_reply(rcvsock
, &rcvmhdr
))) {
754 (void) gettimeofday(&t2
, NULL
);
755 if ((i
= packet_ok(&rcvmhdr
, cc
, seq
))) {
756 if (!IN6_ARE_ADDR_EQUAL(&Rcv
.sin6_addr
,
759 fputs("\n ", stdout
);
761 lastaddr
= Rcv
.sin6_addr
;
763 printf(" %.3f ms", deltaT(&t1
, &t2
));
765 case ICMP6_DST_UNREACH_NOROUTE
:
769 case ICMP6_DST_UNREACH_ADMIN
:
773 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
777 case ICMP6_DST_UNREACH_ADDR
:
781 case ICMP6_DST_UNREACH_NOPORT
:
793 (void) fflush(stdout
);
797 (unreachable
> 0 && unreachable
>= ((nprobes
+ 1) / 2))) {
806 wait_for_reply(int sock
, struct msghdr
*mhdr
)
809 struct pollfd pfd
[1];
813 pfd
[0].events
= POLLIN
;
816 if (poll(pfd
, 1, waittime
* 1000) > 0)
817 cc
= recvmsg(rcvsock
, mhdr
, 0);
825 fdsn
= howmany(sock
+ 1, NFDBITS
) * sizeof(fd_mask
);
826 if ((fdsp
= (fd_set
*)malloc(fdsn
)) == NULL
)
828 memset(fdsp
, 0, fdsn
);
830 wait
.tv_sec
= waittime
; wait
.tv_usec
= 0;
832 if (select(sock
+1, fdsp
, NULL
, NULL
, &wait
) > 0)
833 cc
= recvmsg(rcvsock
, mhdr
, 0);
841 send_probe(int seq
, u_long hops
)
843 struct icmp6_hdr
*icp
;
850 if (setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
851 (char *)&i
, sizeof(i
)) < 0) {
852 perror("setsockopt IPV6_UNICAST_HOPS");
855 Dst
.sin6_port
= htons(port
+ seq
);
856 (void) gettimeofday(&tv
, NULL
);
857 tv32
.tv32_sec
= htonl(tv
.tv_sec
);
858 tv32
.tv32_usec
= htonl(tv
.tv_usec
);
862 icp
= (struct icmp6_hdr
*)outpacket
;
864 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
866 icp
->icmp6_cksum
= 0;
867 icp
->icmp6_id
= ident
;
868 icp
->icmp6_seq
= htons(seq
);
869 bcopy(&tv32
, ((u_int8_t
*)outpacket
+ ICMP6ECHOLEN
),
877 bcopy(&tv32
, &op
->tv
, sizeof tv32
);
880 /* No space for anything. No harm as seq/tv32 are decorative. */
883 fprintf(stderr
, "Unknown probe protocol %d.\n", useproto
);
887 i
= sendto(sndsock
, (char *)outpacket
, datalen
, 0,
888 (struct sockaddr
*)&Dst
, Dst
.sin6_len
);
889 if (i
< 0 || i
!= datalen
) {
892 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
893 hostname
, datalen
, i
);
894 (void) fflush(stdout
);
899 deltaT(struct timeval
*t1p
, struct timeval
*t2p
)
903 dt
= (double)(t2p
->tv_sec
- t1p
->tv_sec
) * 1000.0 +
904 (double)(t2p
->tv_usec
- t1p
->tv_usec
) / 1000.0;
909 * Convert an ICMP "type" field to a printable string.
914 u_char t
= t0
& 0xff;
918 case ICMP6_DST_UNREACH
:
919 cp
= "Destination Unreachable";
921 case ICMP6_PACKET_TOO_BIG
:
922 cp
= "Packet Too Big";
924 case ICMP6_TIME_EXCEEDED
:
925 cp
= "Time Exceeded";
927 case ICMP6_PARAM_PROB
:
928 cp
= "Parameter Problem";
930 case ICMP6_ECHO_REQUEST
:
933 case ICMP6_ECHO_REPLY
:
936 case ICMP6_MEMBERSHIP_QUERY
:
937 cp
= "Group Membership Query";
939 case ICMP6_MEMBERSHIP_REPORT
:
940 cp
= "Group Membership Report";
942 case ICMP6_MEMBERSHIP_REDUCTION
:
943 cp
= "Group Membership Reduction";
945 case ND_ROUTER_SOLICIT
:
946 cp
= "Router Solicitation";
948 case ND_ROUTER_ADVERT
:
949 cp
= "Router Advertisement";
951 case ND_NEIGHBOR_SOLICIT
:
952 cp
= "Neighbor Solicitation";
954 case ND_NEIGHBOR_ADVERT
:
955 cp
= "Neighbor Advertisement";
968 packet_ok(struct msghdr
*mhdr
, int cc
, int seq
)
970 struct icmp6_hdr
*icp
;
971 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
973 char *buf
= (char *)mhdr
->msg_iov
[0].iov_base
;
976 char hbuf
[NI_MAXHOST
];
984 ip
= (struct ip6_hdr
*) buf
;
985 hlen
= sizeof(struct ip6_hdr
);
986 if (cc
< hlen
+ sizeof(struct icmp6_hdr
)) {
988 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
989 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
990 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
991 printf("packet too short (%d bytes) from %s\n", cc
,
997 icp
= (struct icmp6_hdr
*)(buf
+ hlen
);
999 if (cc
< sizeof(struct icmp6_hdr
)) {
1001 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1002 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1003 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1004 printf("data too short (%d bytes) from %s\n", cc
, hbuf
);
1008 icp
= (struct icmp6_hdr
*)buf
;
1010 /* get optional information via advanced API */
1013 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1014 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1015 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1016 cm
->cmsg_type
== IPV6_PKTINFO
&&
1018 CMSG_LEN(sizeof(struct in6_pktinfo
)))
1019 rcvpktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
1021 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1022 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1023 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1024 hlimp
= (int *)CMSG_DATA(cm
);
1026 if (rcvpktinfo
== NULL
|| hlimp
== NULL
) {
1027 warnx("failed to get received hop limit or packet info");
1031 rcvhlim
= 0; /*XXX*/
1037 type
= icp
->icmp6_type
;
1038 code
= icp
->icmp6_code
;
1039 if ((type
== ICMP6_TIME_EXCEEDED
&& code
== ICMP6_TIME_EXCEED_TRANSIT
)
1040 || type
== ICMP6_DST_UNREACH
) {
1041 struct ip6_hdr
*hip
;
1044 hip
= (struct ip6_hdr
*)(icp
+ 1);
1045 if ((up
= get_uphdr(hip
, (u_char
*)(buf
+ cc
))) == NULL
) {
1047 warnx("failed to get upper layer header");
1051 case IPPROTO_ICMPV6
:
1052 if (((struct icmp6_hdr
*)up
)->icmp6_id
== ident
&&
1053 ((struct icmp6_hdr
*)up
)->icmp6_seq
== htons(seq
))
1054 return (type
== ICMP6_TIME_EXCEEDED
?
1058 if (((struct udphdr
*)up
)->uh_sport
== htons(srcport
) &&
1059 ((struct udphdr
*)up
)->uh_dport
== htons(port
+ seq
))
1060 return (type
== ICMP6_TIME_EXCEEDED
?
1064 return (type
== ICMP6_TIME_EXCEEDED
? -1 : code
+ 1);
1066 fprintf(stderr
, "Unknown probe proto %d.\n", useproto
);
1069 } else if (useproto
== IPPROTO_ICMPV6
&& type
== ICMP6_ECHO_REPLY
) {
1070 if (icp
->icmp6_id
== ident
&&
1071 icp
->icmp6_seq
== htons(seq
))
1072 return (ICMP6_DST_UNREACH_NOPORT
+ 1);
1075 char sbuf
[NI_MAXHOST
+1], dbuf
[INET6_ADDRSTRLEN
];
1079 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1080 sbuf
, sizeof(sbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1081 strlcpy(sbuf
, "invalid", sizeof(sbuf
));
1082 printf("\n%d bytes from %s to %s", cc
, sbuf
,
1083 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1084 dbuf
, sizeof(dbuf
)) : "?");
1085 printf(": icmp type %d (%s) code %d\n", type
, pr_type(type
),
1087 p
= (u_int8_t
*)(icp
+ 1);
1089 for (i
= 0; i
< cc
; i
++) {
1094 printf("%02x", p
[i
]);
1095 if (i
% WIDTH
== WIDTH
- 1)
1098 if (cc
% WIDTH
!= 0)
1105 * Increment pointer until find the UDP or ICMP header.
1108 get_uphdr(struct ip6_hdr
*ip6
, u_char
*lim
)
1110 u_char
*cp
= (u_char
*)ip6
, nh
;
1112 static u_char none_hdr
[1]; /* Fake pointer for IPPROTO_NONE. */
1114 if (cp
+ sizeof(*ip6
) > lim
)
1118 cp
+= sizeof(struct ip6_hdr
);
1120 while (lim
- cp
>= (nh
== IPPROTO_NONE
? 0 : 8)) {
1125 case IPPROTO_ICMPV6
:
1126 return(useproto
== nh
? cp
: NULL
);
1128 return(useproto
== nh
? cp
: NULL
);
1130 return(useproto
== nh
? none_hdr
: NULL
);
1131 case IPPROTO_FRAGMENT
:
1132 hlen
= sizeof(struct ip6_frag
);
1133 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
1136 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 2) << 2;
1137 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1140 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 1) << 3;
1141 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1152 print(struct msghdr
*mhdr
, int cc
)
1154 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
1155 char hbuf
[NI_MAXHOST
];
1157 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1158 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1159 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1161 printf(" %s", hbuf
);
1163 printf(" %s (%s)", inetname((struct sockaddr
*)from
), hbuf
);
1165 printf(" %s", inetname((struct sockaddr
*)from
));
1169 printf(" %d bytes to %s", cc
,
1170 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1171 hbuf
, sizeof(hbuf
)) : "?");
1173 printf(" %d bytes of data to %s", cc
,
1174 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1175 hbuf
, sizeof(hbuf
)) : "?");
1181 * Construct an Internet address representation.
1182 * If the nflag has been supplied, give
1183 * numeric value, otherwise try for symbolic name.
1186 inetname(struct sockaddr
*sa
)
1188 static char line
[NI_MAXHOST
], domain
[MAXHOSTNAMELEN
+ 1];
1189 static int first
= 1;
1192 if (first
&& !nflag
) {
1194 if (gethostname(domain
, sizeof(domain
)) == 0 &&
1195 (cp
= strchr(domain
, '.')))
1196 (void) strlcpy(domain
, cp
+ 1, sizeof(domain
));
1202 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1203 NI_NAMEREQD
) == 0) {
1204 if ((cp
= strchr(line
, '.')) &&
1205 !strcmp(cp
+ 1, domain
))
1213 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1214 NI_NUMERICHOST
) != 0)
1215 strlcpy(line
, "invalid", sizeof(line
));
1224 "usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1225 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");