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. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * "@(#) Copyright (c) 1990, 1993
68 * The Regents of the University of California. All rights reserved.
70 * $FreeBSD: src/usr.sbin/traceroute6/traceroute6.c,v 1.22 2008/02/10 21:06:38 dwmalone Exp $
71 * $DragonFly: src/usr.sbin/traceroute6/traceroute6.c,v 1.8 2008/05/20 12:14:10 hasso Exp $
75 * traceroute host - trace the route ip packets follow going to "host".
77 * Attempt to trace the route an ip packet would follow to some
78 * internet host. We find out intermediate hops by launching probe
79 * packets with a small ttl (time to live) then listening for an
80 * icmp "time exceeded" reply from a gateway. We start our probes
81 * with a ttl of one and increase by one until we get an icmp "port
82 * unreachable" (which means we got to "host") or hit a max (which
83 * defaults to 30 hops & can be changed with the -m flag). Three
84 * probes (change with -q flag) are sent at each ttl setting and a
85 * line is printed showing the ttl, address of the gateway and
86 * round trip time of each probe. If the probe answers come from
87 * different gateways, the address of each responding system will
88 * be printed. If there is no response within a 5 sec. timeout
89 * interval (changed with the -w flag), a "*" is printed for that
92 * Probe packets are UDP format. We don't want the destination
93 * host to process them so the destination port is set to an
94 * unlikely value (if some clod on the destination is using that
95 * value, it can be changed with the -p flag).
97 * A sample use might be:
99 * [yak 71]% traceroute nis.nsf.net.
100 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
101 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
102 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
103 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
104 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
105 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
106 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
107 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
108 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
109 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
110 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
111 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
113 * Note that lines 2 & 3 are the same. This is due to a buggy
114 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
115 * packets with a zero ttl.
117 * A more interesting example is:
119 * [yak 72]% traceroute allspice.lcs.mit.edu.
120 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
121 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
122 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
123 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
124 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
125 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
126 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
127 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
128 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
129 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
130 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
131 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
133 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
138 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
140 * (I start to see why I'm having so much trouble with mail to
141 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
142 * either don't send ICMP "time exceeded" messages or send them
143 * with a ttl too small to reach us. 14 - 17 are running the
144 * MIT C Gateway code that doesn't send "time exceeded"s. God
145 * only knows what's going on with 12.
147 * The silent gateway 12 in the above may be the result of a bug in
148 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
149 * sends an unreachable message using whatever ttl remains in the
150 * original datagram. Since, for gateways, the remaining ttl is
151 * zero, the icmp "time exceeded" is guaranteed to not make it back
152 * to us. The behavior of this bug is slightly more interesting
153 * when it appears on the destination system:
155 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
156 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
157 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
158 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
159 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
160 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
167 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
169 * Notice that there are 12 "gateways" (13 is the final
170 * destination) and exactly the last half of them are "missing".
171 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
172 * is using the ttl from our arriving datagram as the ttl in its
173 * icmp reply. So, the reply will time out on the return path
174 * (with no notice sent to anyone since icmp's aren't sent for
175 * icmp's) until we probe with a ttl that's at least twice the path
176 * length. I.e., rip is really only 7 hops away. A reply that
177 * returns with a ttl of 1 is a clue this problem exists.
178 * Traceroute prints a "!" after the time if the ttl is <= 1.
179 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
180 * non-standard (HPUX) software, expect to see this problem
181 * frequently and/or take care picking the target host of your
184 * Other possible annotations after the time are !H, !N, !P (got a host,
185 * network or protocol unreachable, respectively), !S or !F (source
186 * route failed or fragmentation needed -- neither of these should
187 * ever occur and the associated gateway is busted if you see one). If
188 * almost all the probes result in some kind of unreachable, traceroute
189 * will give up and exit.
193 * This program must be run by root or be setuid. (I suggest that
194 * you *don't* make it setuid -- casual use could result in a lot
195 * of unnecessary traffic on our poor, congested nets.)
197 * This program requires a kernel mod that does not appear in any
198 * system available from Berkeley: A raw ip socket using proto
199 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
200 * opposed to data to be wrapped in an ip datagram). See the README
201 * file that came with the source to this program for a description
202 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
203 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
204 * MODIFIED TO RUN THIS PROGRAM.
206 * The udp port usage may appear bizarre (well, ok, it is bizarre).
207 * The problem is that an icmp message only contains 8 bytes of
208 * data from the original datagram. 8 bytes is the size of a udp
209 * header so, if we want to associate replies with the original
210 * datagram, the necessary information must be encoded into the
211 * udp header (the ip id could be used but there's no way to
212 * interlock with the kernel's assignment of ip id's and, anyway,
213 * it would have taken a lot more kernel hacking to allow this
214 * code to set the ip id). So, to allow two or more users to
215 * use traceroute simultaneously, we use this task's pid as the
216 * source port (the high bit is set to move the port number out
217 * of the "likely" range). To keep track of which probe is being
218 * replied to (so times and/or hop counts don't get confused by a
219 * reply that was delayed in transit), we increment the destination
220 * port number before each probe.
222 * Don't use this as a coding example. I was trying to find a
223 * routing problem and this code sort-of popped out after 48 hours
224 * without sleep. I was amazed it ever compiled, much less ran.
226 * I stole the idea for this program from Steve Deering. Since
227 * the first release, I've learned that had I attended the right
228 * IETF working group meetings, I also could have stolen it from Guy
229 * Almes or Matt Mathis. I don't know (or care) who came up with
230 * the idea first. I envy the originators' perspicacity and I'm
231 * glad they didn't keep the idea a secret.
233 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
234 * enhancements to the original distribution.
236 * I've hacked up a round-trip-route version of this that works by
237 * sending a loose-source-routed udp datagram through the destination
238 * back to yourself. Unfortunately, SO many gateways botch source
239 * routing, the thing is almost worthless. Maybe one day...
241 * -- Van Jacobson (van@helios.ee.lbl.gov)
242 * Tue Dec 20 03:50:13 PST 1988
245 #include <sys/param.h>
246 #include <sys/time.h>
247 #include <sys/socket.h>
249 #include <sys/file.h>
250 #include <sys/ioctl.h>
251 #include <sys/sysctl.h>
253 #include <netinet/in.h>
255 #include <arpa/inet.h>
268 #include <netinet/ip6.h>
269 #include <netinet/icmp6.h>
270 #include <netinet/udp.h>
273 #include <net/route.h>
274 #include <netinet6/ipsec.h>
277 #define DUMMY_PORT 10010
279 #define MAXPACKET 65535 /* max ip packet size */
281 #ifndef HAVE_GETIPNODEBYNAME
282 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
283 #define freehostent(x)
287 * format of a (udp) probe packet.
295 u_char seq
; /* sequence number of this packet */
296 u_char hops
; /* hop limit of the packet */
298 struct tv32 tv
; /* time packet left */
299 } __attribute__((__packed__
));
301 u_char packet
[512]; /* last inbound (icmp) packet */
302 struct opacket
*outpacket
; /* last output (udp) packet */
304 int main(int, char *[]);
305 int wait_for_reply(int, struct msghdr
*);
307 #ifdef IPSEC_POLICY_IPSEC
308 int setpolicy(int so
, char *policy
);
311 void send_probe(int, u_long
);
312 void *get_uphdr(struct ip6_hdr
*, u_char
*);
313 int get_hoplim(struct msghdr
*);
314 double deltaT(struct timeval
*, struct timeval
*);
316 int packet_ok(struct msghdr
*, int, int);
317 void print(struct msghdr
*, int);
318 const char *inetname(struct sockaddr
*);
321 int rcvsock
; /* receive (icmp) socket file descriptor */
322 int sndsock
; /* send (udp) socket file descriptor */
324 struct msghdr rcvmhdr
;
325 struct iovec rcviov
[2];
327 struct in6_pktinfo
*rcvpktinfo
;
329 struct sockaddr_in6 Src
, Dst
, Rcv
;
330 u_long datalen
; /* How much data */
331 #define ICMP6ECHOLEN 8
332 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
334 #ifdef USE_RFC2292BIS
335 struct ip6_rthdr
*rth
;
337 struct cmsghdr
*cmsg
;
343 u_long first_hop
= 1;
344 u_long max_hops
= 30;
346 u_int16_t port
= 32768+666; /* start udp dest port # for probe packets */
348 int options
; /* socket options */
350 int waittime
= 5; /* time to wait for response (in seconds) */
351 int nflag
; /* print addresses numerically */
352 int useproto
= IPPROTO_UDP
; /* protocol to use to send packet */
353 int lflag
; /* print both numerical address & hostname */
356 main(int argc
, char **argv
)
358 int mib
[4] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, IPV6CTL_DEFHLIM
};
359 char hbuf
[NI_MAXHOST
], src0
[NI_MAXHOST
], *ep
;
360 int ch
, i
, on
= 1, seq
, rcvcmsglen
, error
, minlen
;
361 struct addrinfo hints
, *res
;
362 static u_char
*rcvcmsgbuf
;
363 u_long probe
, hops
, lport
;
371 if ((rcvsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
372 perror("socket(ICMPv6)");
377 (void) sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &i
, &size
, NULL
, 0);
380 /* specify to tell receiving interface */
381 #ifdef IPV6_RECVPKTINFO
382 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
384 err(1, "setsockopt(IPV6_RECVPKTINFO)");
385 #else /* old adv. API */
386 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
388 err(1, "setsockopt(IPV6_PKTINFO)");
391 /* specify to tell value of hoplimit field of received IP6 hdr */
392 #ifdef IPV6_RECVHOPLIMIT
393 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
395 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
396 #else /* old adv. API */
397 if (setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
399 err(1, "setsockopt(IPV6_HOPLIMIT)");
404 while ((ch
= getopt(argc
, argv
, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1)
412 first_hop
= strtoul(optarg
, &ep
, 0);
413 if (errno
|| !*optarg
|| *ep
|| first_hop
> 255) {
415 "traceroute6: invalid min hoplimit.\n");
420 hp
= getipnodebyname(optarg
, AF_INET6
, 0, &h_errno
);
423 "traceroute6: unknown host %s\n", optarg
);
426 #ifdef USE_RFC2292BIS
429 * XXX: We can't detect the number of
430 * intermediate nodes yet.
432 if ((rth
= inet6_rth_init((void *)rtbuf
,
433 sizeof(rtbuf
), IPV6_RTHDR_TYPE_0
,
436 "inet6_rth_init failed.\n");
440 if (inet6_rth_add((void *)rth
,
441 (struct in6_addr
*)hp
->h_addr
)) {
443 "inet6_rth_add failed for %s\n",
447 #else /* old advanced API */
449 cmsg
= inet6_rthdr_init(rtbuf
, IPV6_RTHDR_TYPE_0
);
450 inet6_rthdr_add(cmsg
, (struct in6_addr
*)hp
->h_addr
,
456 useproto
= IPPROTO_ICMPV6
;
457 ident
= htons(getpid() & 0xffff); /* same as ping6 */
465 max_hops
= strtoul(optarg
, &ep
, 0);
466 if (errno
|| !*optarg
|| *ep
|| max_hops
> 255) {
468 "traceroute6: invalid max hoplimit.\n");
476 useproto
= IPPROTO_NONE
;
481 lport
= strtoul(optarg
, &ep
, 0);
482 if (errno
|| !*optarg
|| *ep
) {
483 fprintf(stderr
, "traceroute6: invalid port.\n");
486 if (lport
== 0 || lport
!= (lport
& 0xffff)) {
488 "traceroute6: port out of range.\n");
491 port
= lport
& 0xffff;
496 nprobes
= strtoul(optarg
, &ep
, 0);
497 if (errno
|| !*optarg
|| *ep
) {
499 "traceroute6: invalid nprobes.\n");
504 "traceroute6: nprobes must be >0.\n");
509 options
|= SO_DONTROUTE
;
513 * set the ip source address of the outbound
514 * probe (e.g., on a multi-homed host).
522 useproto
= IPPROTO_UDP
;
527 waittime
= strtoul(optarg
, &ep
, 0);
528 if (errno
|| !*optarg
|| *ep
) {
530 "traceroute6: invalid wait time.\n");
535 "traceroute6: wait must be >= 1 sec.\n");
546 * Open socket to send probe packets.
553 if ((sndsock
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
554 perror("socket(SOCK_DGRAM)");
559 if ((sndsock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_NONE
)) < 0) {
560 perror("socket(SOCK_RAW)");
565 fprintf(stderr
, "traceroute6: unknown probe protocol %d",
569 if (max_hops
< first_hop
) {
571 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
577 if (setresuid(uid
, uid
, uid
) == -1) {
583 if (argc
< 1 || argc
> 2)
587 setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
);
592 memset(&hints
, 0, sizeof(hints
));
593 hints
.ai_family
= PF_INET6
;
594 hints
.ai_socktype
= SOCK_RAW
;
595 hints
.ai_protocol
= IPPROTO_ICMPV6
;
596 hints
.ai_flags
= AI_CANONNAME
;
597 error
= getaddrinfo(*argv
, NULL
, &hints
, &res
);
600 "traceroute6: %s\n", gai_strerror(error
));
603 if (res
->ai_addrlen
!= sizeof(Dst
)) {
605 "traceroute6: size of sockaddr mismatch\n");
608 memcpy(&Dst
, res
->ai_addr
, res
->ai_addrlen
);
609 hostname
= res
->ai_canonname
? strdup(res
->ai_canonname
) : *argv
;
611 fprintf(stderr
, "traceroute6: not enough core\n");
615 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
,
616 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
617 strlcpy(hbuf
, "?", sizeof(hbuf
));
618 fprintf(stderr
, "traceroute6: Warning: %s has multiple "
619 "addresses; using %s\n", hostname
, hbuf
);
625 datalen
= strtoul(*argv
, &ep
, 0);
626 if (errno
|| !*argv
|| *ep
) {
628 "traceroute6: invalid packet length.\n");
634 minlen
= ICMP6ECHOLEN
+ sizeof(struct tv32
);
637 minlen
= sizeof(struct opacket
);
644 fprintf(stderr
, "traceroute6: unknown probe protocol %d.\n",
648 if (datalen
< minlen
)
650 else if (datalen
>= MAXPACKET
) {
652 "traceroute6: packet size must be %d <= s < %ld.\n",
653 minlen
, (long)MAXPACKET
);
656 outpacket
= (struct opacket
*)malloc((unsigned)datalen
);
661 (void) bzero((char *)outpacket
, datalen
);
663 /* initialize msghdr for receiving packets */
664 rcviov
[0].iov_base
= (caddr_t
)packet
;
665 rcviov
[0].iov_len
= sizeof(packet
);
666 rcvmhdr
.msg_name
= (caddr_t
)&Rcv
;
667 rcvmhdr
.msg_namelen
= sizeof(Rcv
);
668 rcvmhdr
.msg_iov
= rcviov
;
669 rcvmhdr
.msg_iovlen
= 1;
670 rcvcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
671 CMSG_SPACE(sizeof(int));
672 if ((rcvcmsgbuf
= malloc(rcvcmsglen
)) == NULL
) {
673 fprintf(stderr
, "traceroute6: malloc failed\n");
676 rcvmhdr
.msg_control
= (caddr_t
) rcvcmsgbuf
;
677 rcvmhdr
.msg_controllen
= rcvcmsglen
;
679 if (options
& SO_DEBUG
)
680 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DEBUG
,
681 (char *)&on
, sizeof(on
));
682 if (options
& SO_DONTROUTE
)
683 (void) setsockopt(rcvsock
, SOL_SOCKET
, SO_DONTROUTE
,
684 (char *)&on
, sizeof(on
));
686 #ifdef IPSEC_POLICY_IPSEC
688 * do not raise error even if setsockopt fails, kernel may have ipsec
691 if (setpolicy(rcvsock
, "in bypass") < 0)
692 errx(1, "%s", ipsec_strerror());
693 if (setpolicy(rcvsock
, "out bypass") < 0)
694 errx(1, "%s", ipsec_strerror());
697 int level
= IPSEC_LEVEL_NONE
;
699 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
, &level
,
701 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_ESP_NETWORK_LEVEL
, &level
,
703 #ifdef IP_AUTH_TRANS_LEVEL
704 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
, &level
,
707 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
, &level
,
710 #ifdef IP_AUTH_NETWORK_LEVEL
711 (void)setsockopt(rcvsock
, IPPROTO_IPV6
, IPV6_AUTH_NETWORK_LEVEL
, &level
,
715 #endif /*IPSEC_POLICY_IPSEC*/
720 if (setsockopt(sndsock
, SOL_SOCKET
, SO_SNDBUF
, (char *)&i
,
721 sizeof(i
)) < 0 && useproto
!= IPPROTO_NONE
) {
722 perror("setsockopt(SO_SNDBUF)");
725 #endif /* SO_SNDBUF */
726 if (options
& SO_DEBUG
)
727 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DEBUG
,
728 (char *)&on
, sizeof(on
));
729 if (options
& SO_DONTROUTE
)
730 (void) setsockopt(sndsock
, SOL_SOCKET
, SO_DONTROUTE
,
731 (char *)&on
, sizeof(on
));
732 #ifdef USE_RFC2292BIS
733 if (rth
) {/* XXX: there is no library to finalize the header... */
734 rth
->ip6r_len
= rth
->ip6r_segleft
* 2;
735 if (setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_RTHDR
,
736 (void *)rth
, (rth
->ip6r_len
+ 1) << 3)) {
737 fprintf(stderr
, "setsockopt(IPV6_RTHDR): %s\n",
742 #else /* old advanced API */
744 inet6_rthdr_lasthop(cmsg
, IPV6_RTHDR_LOOSE
);
745 if (setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_PKTOPTIONS
,
746 rtbuf
, cmsg
->cmsg_len
) < 0) {
747 fprintf(stderr
, "setsockopt(IPV6_PKTOPTIONS): %s\n",
752 #endif /* USE_RFC2292BIS */
754 #ifdef IPSEC_POLICY_IPSEC
756 * do not raise error even if setsockopt fails, kernel may have ipsec
759 if (setpolicy(sndsock
, "in bypass") < 0)
760 errx(1, "%s", ipsec_strerror());
761 if (setpolicy(sndsock
, "out bypass") < 0)
762 errx(1, "%s", ipsec_strerror());
765 int level
= IPSEC_LEVEL_BYPASS
;
767 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
, &level
,
769 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_ESP_NETWORK_LEVEL
, &level
,
771 #ifdef IP_AUTH_TRANS_LEVEL
772 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
, &level
,
775 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_LEVEL
, &level
,
778 #ifdef IP_AUTH_NETWORK_LEVEL
779 (void)setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_AUTH_NETWORK_LEVEL
, &level
,
783 #endif /*IPSEC_POLICY_IPSEC*/
789 bzero(&Src
, sizeof(Src
));
791 struct addrinfo hints
, *res
;
794 memset(&hints
, 0, sizeof(hints
));
795 hints
.ai_family
= AF_INET6
;
796 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
797 hints
.ai_flags
= AI_NUMERICHOST
;
798 error
= getaddrinfo(source
, "0", &hints
, &res
);
800 printf("traceroute6: %s: %s\n", source
,
801 gai_strerror(error
));
804 if (res
->ai_addrlen
> sizeof(Src
)) {
805 printf("traceroute6: %s: %s\n", source
,
806 gai_strerror(error
));
809 memcpy(&Src
, res
->ai_addr
, res
->ai_addrlen
);
812 struct sockaddr_in6 Nxt
;
817 Nxt
.sin6_port
= htons(DUMMY_PORT
);
819 bcopy(inet6_rthdr_getaddr(cmsg
, 1), &Nxt
.sin6_addr
,
820 sizeof(Nxt
.sin6_addr
));
821 if ((dummy
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
825 if (connect(dummy
, (struct sockaddr
*)&Nxt
, Nxt
.sin6_len
) < 0) {
830 if (getsockname(dummy
, (struct sockaddr
*)&Src
, &len
) < 0) {
831 perror("getsockname");
834 if (getnameinfo((struct sockaddr
*)&Src
, Src
.sin6_len
,
835 src0
, sizeof(src0
), NULL
, 0, NI_NUMERICHOST
)) {
836 fprintf(stderr
, "getnameinfo failed for source\n");
843 Src
.sin6_port
= htons(0);
844 if (bind(sndsock
, (struct sockaddr
*)&Src
, Src
.sin6_len
) < 0) {
853 if (getsockname(sndsock
, (struct sockaddr
*)&Src
, &len
) < 0) {
854 perror("getsockname");
857 srcport
= ntohs(Src
.sin6_port
);
863 if (getnameinfo((struct sockaddr
*)&Dst
, Dst
.sin6_len
, hbuf
,
864 sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
))
865 strlcpy(hbuf
, "(invalid)", sizeof(hbuf
));
866 fprintf(stderr
, "traceroute6");
867 fprintf(stderr
, " to %s (%s)", hostname
, hbuf
);
869 fprintf(stderr
, " from %s", source
);
870 fprintf(stderr
, ", %lu hops max, %lu byte packets\n",
872 (void) fflush(stderr
);
875 printf("Skipping %lu intermediate hops\n", first_hop
- 1);
880 for (hops
= first_hop
; hops
<= max_hops
; ++hops
) {
881 struct in6_addr lastaddr
;
885 printf("%2lu ", hops
);
886 bzero(&lastaddr
, sizeof(lastaddr
));
887 for (probe
= 0; probe
< nprobes
; ++probe
) {
889 struct timeval t1
, t2
;
891 (void) gettimeofday(&t1
, NULL
);
892 send_probe(++seq
, hops
);
893 while ((cc
= wait_for_reply(rcvsock
, &rcvmhdr
))) {
894 (void) gettimeofday(&t2
, NULL
);
895 if ((i
= packet_ok(&rcvmhdr
, cc
, seq
))) {
896 if (!IN6_ARE_ADDR_EQUAL(&Rcv
.sin6_addr
,
899 fputs("\n ", stdout
);
901 lastaddr
= Rcv
.sin6_addr
;
903 printf(" %.3f ms", deltaT(&t1
, &t2
));
905 case ICMP6_DST_UNREACH_NOROUTE
:
909 case ICMP6_DST_UNREACH_ADMIN
:
913 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
917 case ICMP6_DST_UNREACH_ADDR
:
921 case ICMP6_DST_UNREACH_NOPORT
:
933 (void) fflush(stdout
);
937 (unreachable
> 0 && unreachable
>= ((nprobes
+ 1) / 2))) {
946 wait_for_reply(int sock
, struct msghdr
*mhdr
)
949 struct pollfd pfd
[1];
953 pfd
[0].events
= POLLIN
;
956 if (poll(pfd
, 1, waittime
* 1000) > 0)
957 cc
= recvmsg(rcvsock
, mhdr
, 0);
965 fdsn
= howmany(sock
+ 1, NFDBITS
) * sizeof(fd_mask
);
966 if ((fdsp
= (fd_set
*)malloc(fdsn
)) == NULL
)
968 memset(fdsp
, 0, fdsn
);
970 wait
.tv_sec
= waittime
; wait
.tv_usec
= 0;
972 if (select(sock
+1, fdsp
, (fd_set
*)0, (fd_set
*)0, &wait
) > 0)
973 cc
= recvmsg(rcvsock
, mhdr
, 0);
981 #ifdef IPSEC_POLICY_IPSEC
983 setpolicy(int so
, char *policy
)
987 buf
= ipsec_set_policy(policy
, strlen(policy
));
989 warnx("%s", ipsec_strerror());
992 (void)setsockopt(so
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
,
993 buf
, ipsec_get_policylen(buf
));
1003 send_probe(int seq
, u_long hops
)
1005 struct icmp6_hdr
*icp
;
1012 if (setsockopt(sndsock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
1013 (char *)&i
, sizeof(i
)) < 0) {
1014 perror("setsockopt IPV6_UNICAST_HOPS");
1017 Dst
.sin6_port
= htons(port
+ seq
);
1018 (void) gettimeofday(&tv
, NULL
);
1019 tv32
.tv32_sec
= htonl(tv
.tv_sec
);
1020 tv32
.tv32_usec
= htonl(tv
.tv_usec
);
1023 case IPPROTO_ICMPV6
:
1024 icp
= (struct icmp6_hdr
*)outpacket
;
1026 icp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
1027 icp
->icmp6_code
= 0;
1028 icp
->icmp6_cksum
= 0;
1029 icp
->icmp6_id
= ident
;
1030 icp
->icmp6_seq
= htons(seq
);
1031 bcopy(&tv32
, ((u_int8_t
*)outpacket
+ ICMP6ECHOLEN
),
1039 bcopy(&tv32
, &op
->tv
, sizeof tv32
);
1042 /* No space for anything. No harm as seq/tv32 are decorative. */
1045 fprintf(stderr
, "Unknown probe protocol %d.\n", useproto
);
1049 i
= sendto(sndsock
, (char *)outpacket
, datalen
, 0,
1050 (struct sockaddr
*)&Dst
, Dst
.sin6_len
);
1051 if (i
< 0 || i
!= datalen
) {
1054 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
1055 hostname
, datalen
, i
);
1056 (void) fflush(stdout
);
1061 get_hoplim(struct msghdr
*mhdr
)
1065 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1066 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1067 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1068 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1069 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1070 return(*(int *)CMSG_DATA(cm
));
1077 deltaT(struct timeval
*t1p
, struct timeval
*t2p
)
1081 dt
= (double)(t2p
->tv_sec
- t1p
->tv_sec
) * 1000.0 +
1082 (double)(t2p
->tv_usec
- t1p
->tv_usec
) / 1000.0;
1087 * Convert an ICMP "type" field to a printable string.
1092 u_char t
= t0
& 0xff;
1096 case ICMP6_DST_UNREACH
:
1097 cp
= "Destination Unreachable";
1099 case ICMP6_PACKET_TOO_BIG
:
1100 cp
= "Packet Too Big";
1102 case ICMP6_TIME_EXCEEDED
:
1103 cp
= "Time Exceeded";
1105 case ICMP6_PARAM_PROB
:
1106 cp
= "Parameter Problem";
1108 case ICMP6_ECHO_REQUEST
:
1109 cp
= "Echo Request";
1111 case ICMP6_ECHO_REPLY
:
1114 case ICMP6_MEMBERSHIP_QUERY
:
1115 cp
= "Group Membership Query";
1117 case ICMP6_MEMBERSHIP_REPORT
:
1118 cp
= "Group Membership Report";
1120 case ICMP6_MEMBERSHIP_REDUCTION
:
1121 cp
= "Group Membership Reduction";
1123 case ND_ROUTER_SOLICIT
:
1124 cp
= "Router Solicitation";
1126 case ND_ROUTER_ADVERT
:
1127 cp
= "Router Advertisement";
1129 case ND_NEIGHBOR_SOLICIT
:
1130 cp
= "Neighbor Solicitation";
1132 case ND_NEIGHBOR_ADVERT
:
1133 cp
= "Neighbor Advertisement";
1146 packet_ok(struct msghdr
*mhdr
, int cc
, int seq
)
1148 struct icmp6_hdr
*icp
;
1149 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
1151 char *buf
= (char *)mhdr
->msg_iov
[0].iov_base
;
1154 char hbuf
[NI_MAXHOST
];
1162 ip
= (struct ip6_hdr
*) buf
;
1163 hlen
= sizeof(struct ip6_hdr
);
1164 if (cc
< hlen
+ sizeof(struct icmp6_hdr
)) {
1166 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1167 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1168 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1169 printf("packet too short (%d bytes) from %s\n", cc
,
1175 icp
= (struct icmp6_hdr
*)(buf
+ hlen
);
1177 if (cc
< sizeof(struct icmp6_hdr
)) {
1179 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1180 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1181 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1182 printf("data too short (%d bytes) from %s\n", cc
, hbuf
);
1186 icp
= (struct icmp6_hdr
*)buf
;
1188 /* get optional information via advanced API */
1191 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(mhdr
); cm
;
1192 cm
= (struct cmsghdr
*)CMSG_NXTHDR(mhdr
, cm
)) {
1193 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1194 cm
->cmsg_type
== IPV6_PKTINFO
&&
1196 CMSG_LEN(sizeof(struct in6_pktinfo
)))
1197 rcvpktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
1199 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
1200 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
1201 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
1202 hlimp
= (int *)CMSG_DATA(cm
);
1204 if (rcvpktinfo
== NULL
|| hlimp
== NULL
) {
1205 warnx("failed to get received hop limit or packet info");
1209 rcvhlim
= 0; /*XXX*/
1215 type
= icp
->icmp6_type
;
1216 code
= icp
->icmp6_code
;
1217 if ((type
== ICMP6_TIME_EXCEEDED
&& code
== ICMP6_TIME_EXCEED_TRANSIT
)
1218 || type
== ICMP6_DST_UNREACH
) {
1219 struct ip6_hdr
*hip
;
1222 hip
= (struct ip6_hdr
*)(icp
+ 1);
1223 if ((up
= get_uphdr(hip
, (u_char
*)(buf
+ cc
))) == NULL
) {
1225 warnx("failed to get upper layer header");
1229 case IPPROTO_ICMPV6
:
1230 if (((struct icmp6_hdr
*)up
)->icmp6_id
== ident
&&
1231 ((struct icmp6_hdr
*)up
)->icmp6_seq
== htons(seq
))
1232 return (type
== ICMP6_TIME_EXCEEDED
?
1236 if (((struct udphdr
*)up
)->uh_sport
== htons(srcport
) &&
1237 ((struct udphdr
*)up
)->uh_dport
== htons(port
+ seq
))
1238 return (type
== ICMP6_TIME_EXCEEDED
?
1242 return (type
== ICMP6_TIME_EXCEEDED
? -1 : code
+ 1);
1244 fprintf(stderr
, "Unknown probe proto %d.\n", useproto
);
1247 } else if (useproto
== IPPROTO_ICMPV6
&& type
== ICMP6_ECHO_REPLY
) {
1248 if (icp
->icmp6_id
== ident
&&
1249 icp
->icmp6_seq
== htons(seq
))
1250 return (ICMP6_DST_UNREACH_NOPORT
+ 1);
1253 char sbuf
[NI_MAXHOST
+1], dbuf
[INET6_ADDRSTRLEN
];
1257 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1258 sbuf
, sizeof(sbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1259 strlcpy(sbuf
, "invalid", sizeof(sbuf
));
1260 printf("\n%d bytes from %s to %s", cc
, sbuf
,
1261 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1262 dbuf
, sizeof(dbuf
)) : "?");
1263 printf(": icmp type %d (%s) code %d\n", type
, pr_type(type
),
1265 p
= (u_int8_t
*)(icp
+ 1);
1267 for (i
= 0; i
< cc
; i
++) {
1272 printf("%02x", p
[i
]);
1273 if (i
% WIDTH
== WIDTH
- 1)
1276 if (cc
% WIDTH
!= 0)
1283 * Increment pointer until find the UDP or ICMP header.
1286 get_uphdr(struct ip6_hdr
*ip6
, u_char
*lim
)
1288 u_char
*cp
= (u_char
*)ip6
, nh
;
1290 static u_char none_hdr
[1]; /* Fake pointer for IPPROTO_NONE. */
1292 if (cp
+ sizeof(*ip6
) > lim
)
1296 cp
+= sizeof(struct ip6_hdr
);
1298 while (lim
- cp
>= (nh
== IPPROTO_NONE
? 0 : 8)) {
1303 case IPPROTO_ICMPV6
:
1304 return(useproto
== nh
? cp
: NULL
);
1306 return(useproto
== nh
? cp
: NULL
);
1308 return(useproto
== nh
? none_hdr
: NULL
);
1309 case IPPROTO_FRAGMENT
:
1310 hlen
= sizeof(struct ip6_frag
);
1311 nh
= ((struct ip6_frag
*)cp
)->ip6f_nxt
;
1314 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 2) << 2;
1315 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1318 hlen
= (((struct ip6_ext
*)cp
)->ip6e_len
+ 1) << 3;
1319 nh
= ((struct ip6_ext
*)cp
)->ip6e_nxt
;
1330 print(struct msghdr
*mhdr
, int cc
)
1332 struct sockaddr_in6
*from
= (struct sockaddr_in6
*)mhdr
->msg_name
;
1333 char hbuf
[NI_MAXHOST
];
1335 if (getnameinfo((struct sockaddr
*)from
, from
->sin6_len
,
1336 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
1337 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
1339 printf(" %s", hbuf
);
1341 printf(" %s (%s)", inetname((struct sockaddr
*)from
), hbuf
);
1343 printf(" %s", inetname((struct sockaddr
*)from
));
1347 printf(" %d bytes to %s", cc
,
1348 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1349 hbuf
, sizeof(hbuf
)) : "?");
1351 printf(" %d bytes of data to %s", cc
,
1352 rcvpktinfo
? inet_ntop(AF_INET6
, &rcvpktinfo
->ipi6_addr
,
1353 hbuf
, sizeof(hbuf
)) : "?");
1359 * Construct an Internet address representation.
1360 * If the nflag has been supplied, give
1361 * numeric value, otherwise try for symbolic name.
1364 inetname(struct sockaddr
*sa
)
1366 static char line
[NI_MAXHOST
], domain
[MAXHOSTNAMELEN
+ 1];
1367 static int first
= 1;
1370 if (first
&& !nflag
) {
1372 if (gethostname(domain
, sizeof(domain
)) == 0 &&
1373 (cp
= strchr(domain
, '.')))
1374 (void) strlcpy(domain
, cp
+ 1, sizeof(domain
));
1380 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1381 NI_NAMEREQD
) == 0) {
1382 if ((cp
= strchr(line
, '.')) &&
1383 !strcmp(cp
+ 1, domain
))
1391 if (getnameinfo(sa
, sa
->sa_len
, line
, sizeof(line
), NULL
, 0,
1392 NI_NUMERICHOST
) != 0)
1393 strlcpy(line
, "invalid", sizeof(line
));
1402 "usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1403 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");