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>
268 #include <net/route.h>
269 #include <netinet6/ipsec.h>
272 #define DUMMY_PORT 10010
274 #define MAXPACKET 65535 /* max ip packet size */
276 #ifndef HAVE_GETIPNODEBYNAME
277 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
278 #define freehostent(x)
282 * format of a (udp) probe packet.
290 u_char seq
; /* sequence number of this packet */
291 u_char hops
; /* hop limit of the packet */
293 struct tv32 tv
; /* time packet left */
294 } __attribute__((__packed__
));
296 u_char packet
[512]; /* last inbound (icmp) packet */
297 struct opacket
*outpacket
; /* last output (udp) packet */
299 int wait_for_reply(int, struct msghdr
*);
301 #ifdef IPSEC_POLICY_IPSEC
302 int setpolicy(int so
, char *policy
);
305 void send_probe(int, u_long
);
306 void *get_uphdr(struct ip6_hdr
*, u_char
*);
307 int get_hoplim(struct msghdr
*);
308 double deltaT(struct timeval
*, struct timeval
*);
310 int packet_ok(struct msghdr
*, int, int);
311 void print(struct msghdr
*, int);
312 const char *inetname(struct sockaddr
*);
315 int rcvsock
; /* receive (icmp) socket file descriptor */
316 int sndsock
; /* send (udp) socket file descriptor */
318 struct msghdr rcvmhdr
;
319 struct iovec rcviov
[2];
321 struct in6_pktinfo
*rcvpktinfo
;
323 struct sockaddr_in6 Src
, Dst
, Rcv
;
324 u_long datalen
; /* How much data */
325 #define ICMP6ECHOLEN 8
326 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
333 u_long first_hop
= 1;
334 u_long max_hops
= 30;
336 u_int16_t port
= 32768+666; /* start udp dest port # for probe packets */
338 int options
; /* socket options */
340 int waittime
= 5; /* time to wait for response (in seconds) */
341 int nflag
; /* print addresses numerically */
342 int useproto
= IPPROTO_UDP
; /* protocol to use to send packet */
343 int lflag
; /* print both numerical address & hostname */
346 main(int argc
, char **argv
)
348 int mib
[4] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, IPV6CTL_DEFHLIM
};
349 char hbuf
[NI_MAXHOST
], src0
[NI_MAXHOST
], *ep
;
350 int ch
, i
, on
= 1, seq
, rcvcmsglen
, error
, minlen
;
351 struct addrinfo hints
, *res
;
352 static u_char
*rcvcmsgbuf
;
353 u_long probe
, hops
, lport
;
361 if ((rcvsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
362 perror("socket(ICMPv6)");
367 (void) sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &i
, &size
, NULL
, 0);
370 /* specify to tell receiving interface */
371 #ifdef IPV6_RECVPKTINFO
372 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
374 err(1, "setsockopt(IPV6_RECVPKTINFO)");
375 #else /* old adv. API */
376 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
378 err(1, "setsockopt(IPV6_PKTINFO)");
381 /* specify to tell value of hoplimit field of received IP6 hdr */
382 #ifdef IPV6_RECVHOPLIMIT
383 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
385 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
386 #else /* old adv. API */
387 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
389 err(1, "setsockopt(IPV6_HOPLIMIT)");
394 while ((ch
= getopt(argc
, argv
, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1)
402 first_hop
= strtoul(optarg
, &ep
, 0);
403 if (errno
|| !*optarg
|| *ep
|| first_hop
> 255) {
405 "traceroute6: invalid min hoplimit.\n");
410 hp
= getipnodebyname(optarg
, AF_INET6
, 0, &h_errno
);
413 "traceroute6: unknown host %s\n", optarg
);
419 useproto
= IPPROTO_ICMPV6
;
420 ident
= htons(getpid() & 0xffff); /* same as ping6 */
428 max_hops
= strtoul(optarg
, &ep
, 0);
429 if (errno
|| !*optarg
|| *ep
|| max_hops
> 255) {
431 "traceroute6: invalid max hoplimit.\n");
439 useproto
= IPPROTO_NONE
;
444 lport
= strtoul(optarg
, &ep
, 0);
445 if (errno
|| !*optarg
|| *ep
) {
446 fprintf(stderr
, "traceroute6: invalid port.\n");
449 if (lport
== 0 || lport
!= (lport
& 0xffff)) {
451 "traceroute6: port out of range.\n");
454 port
= lport
& 0xffff;
459 nprobes
= strtoul(optarg
, &ep
, 0);
460 if (errno
|| !*optarg
|| *ep
) {
462 "traceroute6: invalid nprobes.\n");
467 "traceroute6: nprobes must be >0.\n");
472 options
|= SO_DONTROUTE
;
476 * set the ip source address of the outbound
477 * probe (e.g., on a multi-homed host).
485 useproto
= IPPROTO_UDP
;
490 waittime
= strtoul(optarg
, &ep
, 0);
491 if (errno
|| !*optarg
|| *ep
) {
493 "traceroute6: invalid wait time.\n");
498 "traceroute6: wait must be >= 1 sec.\n");
509 * Open socket to send probe packets.
516 if ((sndsock
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
517 perror("socket(SOCK_DGRAM)");
522 if ((sndsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_NONE
)) < 0) {
523 perror("socket(SOCK_RAW)");
528 fprintf(stderr
, "traceroute6: unknown probe protocol %d",
532 if (max_hops
< first_hop
) {
534 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
540 if (setresuid(uid
, uid
, uid
) == -1) {
546 if (argc
< 1 || argc
> 2)
550 setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
);
555 memset(&hints
, 0, sizeof(hints
));
556 hints
.ai_family
= PF_INET6
;
557 hints
.ai_socktype
= SOCK_RAW
;
558 hints
.ai_protocol
= IPPROTO_ICMPV6
;
559 hints
.ai_flags
= AI_CANONNAME
;
560 error
= getaddrinfo(*argv
, NULL
, &hints
, &res
);
563 "traceroute6: %s\n", gai_strerror(error
));
566 if (res
->ai_addrlen
!= sizeof(Dst
)) {
568 "traceroute6: size of sockaddr mismatch\n");
571 memcpy(&Dst
, res
->ai_addr
, res
->ai_addrlen
);
572 hostname
= res
->ai_canonname
? strdup(res
->ai_canonname
) : *argv
;
574 fprintf(stderr
, "traceroute6: not enough core\n");
578 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
,
579 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
580 strlcpy(hbuf
, "?", sizeof(hbuf
));
581 fprintf(stderr
, "traceroute6: Warning: %s has multiple "
582 "addresses; using %s\n", hostname
, hbuf
);
588 datalen
= strtoul(*argv
, &ep
, 0);
589 if (errno
|| !*argv
|| *ep
) {
591 "traceroute6: invalid packet length.\n");
597 minlen
= ICMP6ECHOLEN
+ sizeof(struct tv32
);
600 minlen
= sizeof(struct opacket
);
607 fprintf(stderr
, "traceroute6: unknown probe protocol %d.\n",
611 if (datalen
< minlen
)
613 else if (datalen
>= MAXPACKET
) {
615 "traceroute6: packet size must be %d <= s < %ld.\n",
616 minlen
, (long)MAXPACKET
);
619 outpacket
= (struct opacket
*)malloc((unsigned)datalen
);
624 (void) bzero((char *)outpacket
, datalen
);
626 /* initialize msghdr for receiving packets */
627 rcviov
[0].iov_base
= (caddr_t
)packet
;
628 rcviov
[0].iov_len
= sizeof(packet
);
629 rcvmhdr
.msg_name
= (caddr_t
)&Rcv
;
630 rcvmhdr
.msg_namelen
= sizeof(Rcv
);
631 rcvmhdr
.msg_iov
= rcviov
;
632 rcvmhdr
.msg_iovlen
= 1;
633 rcvcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
634 CMSG_SPACE(sizeof(int));
635 if ((rcvcmsgbuf
= malloc(rcvcmsglen
)) == NULL
) {
636 fprintf(stderr
, "traceroute6: malloc failed\n");
639 rcvmhdr
.msg_control
= (caddr_t
) rcvcmsgbuf
;
640 rcvmhdr
.msg_controllen
= rcvcmsglen
;
642 if (options
& SO_DEBUG
)
643 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DEBUG
,
644 (char *)&on
, sizeof(on
));
645 if (options
& SO_DONTROUTE
)
646 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DONTROUTE
,
647 (char *)&on
, sizeof(on
));
649 #ifdef IPSEC_POLICY_IPSEC
651 * do not raise error even if setsockopt fails, kernel may have ipsec
654 if (setpolicy(rcvsock
, "in bypass") < 0)
655 errx(1, "%s", ipsec_strerror());
656 if (setpolicy(rcvsock
, "out bypass") < 0)
657 errx(1, "%s", ipsec_strerror());
660 int level
= IPSEC_LEVEL_NONE
;
662 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
, &level
,
664 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_ESP_NETWORK_LEVEL
, &level
,
666 #ifdef IP_AUTH_TRANS_LEVEL
667 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
, &level
,
670 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
, &level
,
673 #ifdef IP_AUTH_NETWORK_LEVEL
674 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_NETWORK_LEVEL
, &level
,
678 #endif /*IPSEC_POLICY_IPSEC*/
683 if (setsockopt(sndsock
, SOL_SOCKET
, SO_SNDBUF
, (char *)&i
,
684 sizeof(i
)) < 0 && useproto
!= IPPROTO_NONE
) {
685 perror("setsockopt(SO_SNDBUF)");
688 #endif /* SO_SNDBUF */
689 if (options
& SO_DEBUG
)
690 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DEBUG
,
691 (char *)&on
, sizeof(on
));
692 if (options
& SO_DONTROUTE
)
693 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DONTROUTE
,
694 (char *)&on
, sizeof(on
));
696 #ifdef IPSEC_POLICY_IPSEC
698 * do not raise error even if setsockopt fails, kernel may have ipsec
701 if (setpolicy(sndsock
, "in bypass") < 0)
702 errx(1, "%s", ipsec_strerror());
703 if (setpolicy(sndsock
, "out bypass") < 0)
704 errx(1, "%s", ipsec_strerror());
707 int level
= IPSEC_LEVEL_BYPASS
;
709 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
, &level
,
711 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_ESP_NETWORK_LEVEL
, &level
,
713 #ifdef IP_AUTH_TRANS_LEVEL
714 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
, &level
,
717 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
, &level
,
720 #ifdef IP_AUTH_NETWORK_LEVEL
721 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_NETWORK_LEVEL
, &level
,
725 #endif /*IPSEC_POLICY_IPSEC*/
731 bzero(&Src
, sizeof(Src
));
733 struct addrinfo hints
, *res
;
736 memset(&hints
, 0, sizeof(hints
));
737 hints
.ai_family
= AF_INET6
;
738 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
739 hints
.ai_flags
= AI_NUMERICHOST
;
740 error
= getaddrinfo(source
, "0", &hints
, &res
);
742 printf("traceroute6: %s: %s\n", source
,
743 gai_strerror(error
));
746 if (res
->ai_addrlen
> sizeof(Src
)) {
747 printf("traceroute6: %s: %s\n", source
,
748 gai_strerror(error
));
751 memcpy(&Src
, res
->ai_addr
, res
->ai_addrlen
);
754 struct sockaddr_in6 Nxt
;
759 Nxt
.sin6_port
= htons(DUMMY_PORT
);
760 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
764 if (connect(dummy
, (struct sockaddr
*)&Nxt
, Nxt
.sin6_len
) < 0) {
769 if (getsockname(dummy
, (struct sockaddr
*)&Src
, &len
) < 0) {
770 perror("getsockname");
773 if (getnameinfo((struct sockaddr
*)&Src
, Src
.sin6_len
,
774 src0
, sizeof(src0
), NULL
, 0, NI_NUMERICHOST
)) {
775 fprintf(stderr
, "getnameinfo failed for source\n");
782 Src
.sin6_port
= htons(0);
783 if (bind(sndsock
, (struct sockaddr
*)&Src
, Src
.sin6_len
) < 0) {
792 if (getsockname(sndsock
, (struct sockaddr
*)&Src
, &len
) < 0) {
793 perror("getsockname");
796 srcport
= ntohs(Src
.sin6_port
);
802 if (getnameinfo((struct sockaddr
*)&Dst
, Dst
.sin6_len
, hbuf
,
803 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
))
804 strlcpy(hbuf
, "(invalid)", sizeof(hbuf
));
805 fprintf(stderr
, "traceroute6");
806 fprintf(stderr
, " to %s (%s)", hostname
, hbuf
);
808 fprintf(stderr
, " from %s", source
);
809 fprintf(stderr
, ", %lu hops max, %lu byte packets\n",
811 (void) fflush(stderr
);
814 printf("Skipping %lu intermediate hops\n", first_hop
- 1);
819 for (hops
= first_hop
; hops
<= max_hops
; ++hops
) {
820 struct in6_addr lastaddr
;
824 printf("%2lu ", hops
);
825 bzero(&lastaddr
, sizeof(lastaddr
));
826 for (probe
= 0; probe
< nprobes
; ++probe
) {
828 struct timeval t1
, t2
;
830 (void) gettimeofday(&t1
, NULL
);
831 send_probe(++seq
, hops
);
832 while ((cc
= wait_for_reply(rcvsock
, &rcvmhdr
))) {
833 (void) gettimeofday(&t2
, NULL
);
834 if ((i
= packet_ok(&rcvmhdr
, cc
, seq
))) {
835 if (!IN6_ARE_ADDR_EQUAL(&Rcv
.sin6_addr
,
838 fputs("\n ", stdout
);
840 lastaddr
= Rcv
.sin6_addr
;
842 printf(" %.3f ms", deltaT(&t1
, &t2
));
844 case ICMP6_DST_UNREACH_NOROUTE
:
848 case ICMP6_DST_UNREACH_ADMIN
:
852 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
856 case ICMP6_DST_UNREACH_ADDR
:
860 case ICMP6_DST_UNREACH_NOPORT
:
872 (void) fflush(stdout
);
876 (unreachable
> 0 && unreachable
>= ((nprobes
+ 1) / 2))) {
885 wait_for_reply(int sock
, struct msghdr
*mhdr
)
888 struct pollfd pfd
[1];
892 pfd
[0].events
= POLLIN
;
895 if (poll(pfd
, 1, waittime
* 1000) > 0)
896 cc
= recvmsg(rcvsock
, mhdr
, 0);
904 fdsn
= howmany(sock
+ 1, NFDBITS
) * sizeof(fd_mask
);
905 if ((fdsp
= (fd_set
*)malloc(fdsn
)) == NULL
)
907 memset(fdsp
, 0, fdsn
);
909 wait
.tv_sec
= waittime
; wait
.tv_usec
= 0;
911 if (select(sock
+1, fdsp
, NULL
, NULL
, &wait
) > 0)
912 cc
= recvmsg(rcvsock
, mhdr
, 0);
920 #ifdef IPSEC_POLICY_IPSEC
922 setpolicy(int so
, char *policy
)
926 buf
= ipsec_set_policy(policy
, strlen(policy
));
928 warnx("%s", ipsec_strerror());
931 (void)setsockopt(so
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
,
932 buf
, ipsec_get_policylen(buf
));
942 send_probe(int seq
, u_long hops
)
944 struct icmp6_hdr
*icp
;
951 if (setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
952 (char *)&i
, sizeof(i
)) < 0) {
953 perror("setsockopt IPV6_UNICAST_HOPS");
956 Dst
.sin6_port
= htons(port
+ seq
);
957 (void) gettimeofday(&tv
, NULL
);
958 tv32
.tv32_sec
= htonl(tv
.tv_sec
);
959 tv32
.tv32_usec
= htonl(tv
.tv_usec
);
963 icp
= (struct icmp6_hdr
*)outpacket
;
965 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
967 icp
->icmp6_cksum
= 0;
968 icp
->icmp6_id
= ident
;
969 icp
->icmp6_seq
= htons(seq
);
970 bcopy(&tv32
, ((u_int8_t
*)outpacket
+ ICMP6ECHOLEN
),
978 bcopy(&tv32
, &op
->tv
, sizeof tv32
);
981 /* No space for anything. No harm as seq/tv32 are decorative. */
984 fprintf(stderr
, "Unknown probe protocol %d.\n", useproto
);
988 i
= sendto(sndsock
, (char *)outpacket
, datalen
, 0,
989 (struct sockaddr
*)&Dst
, Dst
.sin6_len
);
990 if (i
< 0 || i
!= datalen
) {
993 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
994 hostname
, datalen
, i
);
995 (void) fflush(stdout
);
1000 get_hoplim(struct msghdr
*mhdr
)
1004 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1005 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1006 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1007 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1008 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1009 return(*(int *)CMSG_DATA(cm
));
1016 deltaT(struct timeval
*t1p
, struct timeval
*t2p
)
1020 dt
= (double)(t2p
->tv_sec
- t1p
->tv_sec
) * 1000.0 +
1021 (double)(t2p
->tv_usec
- t1p
->tv_usec
) / 1000.0;
1026 * Convert an ICMP "type" field to a printable string.
1031 u_char t
= t0
& 0xff;
1035 case ICMP6_DST_UNREACH
:
1036 cp
= "Destination Unreachable";
1038 case ICMP6_PACKET_TOO_BIG
:
1039 cp
= "Packet Too Big";
1041 case ICMP6_TIME_EXCEEDED
:
1042 cp
= "Time Exceeded";
1044 case ICMP6_PARAM_PROB
:
1045 cp
= "Parameter Problem";
1047 case ICMP6_ECHO_REQUEST
:
1048 cp
= "Echo Request";
1050 case ICMP6_ECHO_REPLY
:
1053 case ICMP6_MEMBERSHIP_QUERY
:
1054 cp
= "Group Membership Query";
1056 case ICMP6_MEMBERSHIP_REPORT
:
1057 cp
= "Group Membership Report";
1059 case ICMP6_MEMBERSHIP_REDUCTION
:
1060 cp
= "Group Membership Reduction";
1062 case ND_ROUTER_SOLICIT
:
1063 cp
= "Router Solicitation";
1065 case ND_ROUTER_ADVERT
:
1066 cp
= "Router Advertisement";
1068 case ND_NEIGHBOR_SOLICIT
:
1069 cp
= "Neighbor Solicitation";
1071 case ND_NEIGHBOR_ADVERT
:
1072 cp
= "Neighbor Advertisement";
1085 packet_ok(struct msghdr
*mhdr
, int cc
, int seq
)
1087 struct icmp6_hdr
*icp
;
1088 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
1090 char *buf
= (char *)mhdr
->msg_iov
[0].iov_base
;
1093 char hbuf
[NI_MAXHOST
];
1101 ip
= (struct ip6_hdr
*) buf
;
1102 hlen
= sizeof(struct ip6_hdr
);
1103 if (cc
< hlen
+ sizeof(struct icmp6_hdr
)) {
1105 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1106 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1107 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1108 printf("packet too short (%d bytes) from %s\n", cc
,
1114 icp
= (struct icmp6_hdr
*)(buf
+ hlen
);
1116 if (cc
< sizeof(struct icmp6_hdr
)) {
1118 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1119 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1120 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1121 printf("data too short (%d bytes) from %s\n", cc
, hbuf
);
1125 icp
= (struct icmp6_hdr
*)buf
;
1127 /* get optional information via advanced API */
1130 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1131 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1132 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1133 cm
->cmsg_type
== IPV6_PKTINFO
&&
1135 CMSG_LEN(sizeof(struct in6_pktinfo
)))
1136 rcvpktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
1138 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1139 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1140 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1141 hlimp
= (int *)CMSG_DATA(cm
);
1143 if (rcvpktinfo
== NULL
|| hlimp
== NULL
) {
1144 warnx("failed to get received hop limit or packet info");
1148 rcvhlim
= 0; /*XXX*/
1154 type
= icp
->icmp6_type
;
1155 code
= icp
->icmp6_code
;
1156 if ((type
== ICMP6_TIME_EXCEEDED
&& code
== ICMP6_TIME_EXCEED_TRANSIT
)
1157 || type
== ICMP6_DST_UNREACH
) {
1158 struct ip6_hdr
*hip
;
1161 hip
= (struct ip6_hdr
*)(icp
+ 1);
1162 if ((up
= get_uphdr(hip
, (u_char
*)(buf
+ cc
))) == NULL
) {
1164 warnx("failed to get upper layer header");
1168 case IPPROTO_ICMPV6
:
1169 if (((struct icmp6_hdr
*)up
)->icmp6_id
== ident
&&
1170 ((struct icmp6_hdr
*)up
)->icmp6_seq
== htons(seq
))
1171 return (type
== ICMP6_TIME_EXCEEDED
?
1175 if (((struct udphdr
*)up
)->uh_sport
== htons(srcport
) &&
1176 ((struct udphdr
*)up
)->uh_dport
== htons(port
+ seq
))
1177 return (type
== ICMP6_TIME_EXCEEDED
?
1181 return (type
== ICMP6_TIME_EXCEEDED
? -1 : code
+ 1);
1183 fprintf(stderr
, "Unknown probe proto %d.\n", useproto
);
1186 } else if (useproto
== IPPROTO_ICMPV6
&& type
== ICMP6_ECHO_REPLY
) {
1187 if (icp
->icmp6_id
== ident
&&
1188 icp
->icmp6_seq
== htons(seq
))
1189 return (ICMP6_DST_UNREACH_NOPORT
+ 1);
1192 char sbuf
[NI_MAXHOST
+1], dbuf
[INET6_ADDRSTRLEN
];
1196 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1197 sbuf
, sizeof(sbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1198 strlcpy(sbuf
, "invalid", sizeof(sbuf
));
1199 printf("\n%d bytes from %s to %s", cc
, sbuf
,
1200 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1201 dbuf
, sizeof(dbuf
)) : "?");
1202 printf(": icmp type %d (%s) code %d\n", type
, pr_type(type
),
1204 p
= (u_int8_t
*)(icp
+ 1);
1206 for (i
= 0; i
< cc
; i
++) {
1211 printf("%02x", p
[i
]);
1212 if (i
% WIDTH
== WIDTH
- 1)
1215 if (cc
% WIDTH
!= 0)
1222 * Increment pointer until find the UDP or ICMP header.
1225 get_uphdr(struct ip6_hdr
*ip6
, u_char
*lim
)
1227 u_char
*cp
= (u_char
*)ip6
, nh
;
1229 static u_char none_hdr
[1]; /* Fake pointer for IPPROTO_NONE. */
1231 if (cp
+ sizeof(*ip6
) > lim
)
1235 cp
+= sizeof(struct ip6_hdr
);
1237 while (lim
- cp
>= (nh
== IPPROTO_NONE
? 0 : 8)) {
1242 case IPPROTO_ICMPV6
:
1243 return(useproto
== nh
? cp
: NULL
);
1245 return(useproto
== nh
? cp
: NULL
);
1247 return(useproto
== nh
? none_hdr
: NULL
);
1248 case IPPROTO_FRAGMENT
:
1249 hlen
= sizeof(struct ip6_frag
);
1250 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
1253 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 2) << 2;
1254 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1257 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 1) << 3;
1258 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1269 print(struct msghdr
*mhdr
, int cc
)
1271 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
1272 char hbuf
[NI_MAXHOST
];
1274 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1275 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1276 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1278 printf(" %s", hbuf
);
1280 printf(" %s (%s)", inetname((struct sockaddr
*)from
), hbuf
);
1282 printf(" %s", inetname((struct sockaddr
*)from
));
1286 printf(" %d bytes to %s", cc
,
1287 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1288 hbuf
, sizeof(hbuf
)) : "?");
1290 printf(" %d bytes of data to %s", cc
,
1291 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1292 hbuf
, sizeof(hbuf
)) : "?");
1298 * Construct an Internet address representation.
1299 * If the nflag has been supplied, give
1300 * numeric value, otherwise try for symbolic name.
1303 inetname(struct sockaddr
*sa
)
1305 static char line
[NI_MAXHOST
], domain
[MAXHOSTNAMELEN
+ 1];
1306 static int first
= 1;
1309 if (first
&& !nflag
) {
1311 if (gethostname(domain
, sizeof(domain
)) == 0 &&
1312 (cp
= strchr(domain
, '.')))
1313 (void) strlcpy(domain
, cp
+ 1, sizeof(domain
));
1319 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1320 NI_NAMEREQD
) == 0) {
1321 if ((cp
= strchr(line
, '.')) &&
1322 !strcmp(cp
+ 1, domain
))
1330 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1331 NI_NUMERICHOST
) != 0)
1332 strlcpy(line
, "invalid", sizeof(line
));
1341 "usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1342 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");