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 * 4. 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 main(int, char *[]);
300 int wait_for_reply(int, struct msghdr *);
302 #ifdef IPSEC_POLICY_IPSEC
303 int setpolicy(int so, char *policy);
306 void send_probe(int, u_long);
307 void *get_uphdr(struct ip6_hdr *, u_char *);
308 int get_hoplim(struct msghdr *);
309 double deltaT(struct timeval *, struct timeval *);
311 int packet_ok(struct msghdr *, int, int);
312 void print(struct msghdr *, int);
313 const char *inetname(struct sockaddr *);
316 int rcvsock; /* receive (icmp) socket file descriptor */
317 int sndsock; /* send (udp) socket file descriptor */
319 struct msghdr rcvmhdr;
320 struct iovec rcviov[2];
322 struct in6_pktinfo *rcvpktinfo;
324 struct sockaddr_in6 Src, Dst, Rcv;
325 u_long datalen; /* How much data */
326 #define ICMP6ECHOLEN 8
327 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
334 u_long first_hop = 1;
335 u_long max_hops = 30;
337 u_int16_t port = 32768+666; /* start udp dest port # for probe packets */
339 int options; /* socket options */
341 int waittime = 5; /* time to wait for response (in seconds) */
342 int nflag; /* print addresses numerically */
343 int useproto = IPPROTO_UDP; /* protocol to use to send packet */
344 int lflag; /* print both numerical address & hostname */
347 main(int argc, char **argv)
349 int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM };
350 char hbuf[NI_MAXHOST], src0[NI_MAXHOST], *ep;
351 int ch, i, on = 1, seq, rcvcmsglen, error, minlen;
352 struct addrinfo hints, *res;
353 static u_char *rcvcmsgbuf;
354 u_long probe, hops, lport;
362 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
363 perror("socket(ICMPv6)");
368 (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &i, &size, NULL, 0);
371 /* specify to tell receiving interface */
372 #ifdef IPV6_RECVPKTINFO
373 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
375 err(1, "setsockopt(IPV6_RECVPKTINFO)");
376 #else /* old adv. API */
377 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
379 err(1, "setsockopt(IPV6_PKTINFO)");
382 /* specify to tell value of hoplimit field of received IP6 hdr */
383 #ifdef IPV6_RECVHOPLIMIT
384 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
386 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
387 #else /* old adv. API */
388 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
390 err(1, "setsockopt(IPV6_HOPLIMIT)");
395 while ((ch = getopt(argc, argv, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1)
403 first_hop = strtoul(optarg, &ep, 0);
404 if (errno || !*optarg || *ep || first_hop > 255) {
406 "traceroute6: invalid min hoplimit.\n");
411 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
414 "traceroute6: unknown host %s\n", optarg);
420 useproto = IPPROTO_ICMPV6;
421 ident = htons(getpid() & 0xffff); /* same as ping6 */
429 max_hops = strtoul(optarg, &ep, 0);
430 if (errno || !*optarg || *ep || max_hops > 255) {
432 "traceroute6: invalid max hoplimit.\n");
440 useproto = IPPROTO_NONE;
445 lport = strtoul(optarg, &ep, 0);
446 if (errno || !*optarg || *ep) {
447 fprintf(stderr, "traceroute6: invalid port.\n");
450 if (lport == 0 || lport != (lport & 0xffff)) {
452 "traceroute6: port out of range.\n");
455 port = lport & 0xffff;
460 nprobes = strtoul(optarg, &ep, 0);
461 if (errno || !*optarg || *ep) {
463 "traceroute6: invalid nprobes.\n");
468 "traceroute6: nprobes must be >0.\n");
473 options |= SO_DONTROUTE;
477 * set the ip source address of the outbound
478 * probe (e.g., on a multi-homed host).
486 useproto = IPPROTO_UDP;
491 waittime = strtoul(optarg, &ep, 0);
492 if (errno || !*optarg || *ep) {
494 "traceroute6: invalid wait time.\n");
499 "traceroute6: wait must be >= 1 sec.\n");
510 * Open socket to send probe packets.
517 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
518 perror("socket(SOCK_DGRAM)");
523 if ((sndsock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
524 perror("socket(SOCK_RAW)");
529 fprintf(stderr, "traceroute6: unknown probe protocol %d",
533 if (max_hops < first_hop) {
535 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
541 if (setresuid(uid, uid, uid) == -1) {
547 if (argc < 1 || argc > 2)
551 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
556 memset(&hints, 0, sizeof(hints));
557 hints.ai_family = PF_INET6;
558 hints.ai_socktype = SOCK_RAW;
559 hints.ai_protocol = IPPROTO_ICMPV6;
560 hints.ai_flags = AI_CANONNAME;
561 error = getaddrinfo(*argv, NULL, &hints, &res);
564 "traceroute6: %s\n", gai_strerror(error));
567 if (res->ai_addrlen != sizeof(Dst)) {
569 "traceroute6: size of sockaddr mismatch\n");
572 memcpy(&Dst, res->ai_addr, res->ai_addrlen);
573 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
575 fprintf(stderr, "traceroute6: not enough core\n");
579 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
580 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
581 strlcpy(hbuf, "?", sizeof(hbuf));
582 fprintf(stderr, "traceroute6: Warning: %s has multiple "
583 "addresses; using %s\n", hostname, hbuf);
589 datalen = strtoul(*argv, &ep, 0);
590 if (errno || !*argv || *ep) {
592 "traceroute6: invalid packet length.\n");
598 minlen = ICMP6ECHOLEN + sizeof(struct tv32);
601 minlen = sizeof(struct opacket);
608 fprintf(stderr, "traceroute6: unknown probe protocol %d.\n",
612 if (datalen < minlen)
614 else if (datalen >= MAXPACKET) {
616 "traceroute6: packet size must be %d <= s < %ld.\n",
617 minlen, (long)MAXPACKET);
620 outpacket = (struct opacket *)malloc((unsigned)datalen);
625 (void) bzero((char *)outpacket, datalen);
627 /* initialize msghdr for receiving packets */
628 rcviov[0].iov_base = (caddr_t)packet;
629 rcviov[0].iov_len = sizeof(packet);
630 rcvmhdr.msg_name = (caddr_t)&Rcv;
631 rcvmhdr.msg_namelen = sizeof(Rcv);
632 rcvmhdr.msg_iov = rcviov;
633 rcvmhdr.msg_iovlen = 1;
634 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
635 CMSG_SPACE(sizeof(int));
636 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
637 fprintf(stderr, "traceroute6: malloc failed\n");
640 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
641 rcvmhdr.msg_controllen = rcvcmsglen;
643 if (options & SO_DEBUG)
644 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
645 (char *)&on, sizeof(on));
646 if (options & SO_DONTROUTE)
647 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
648 (char *)&on, sizeof(on));
650 #ifdef IPSEC_POLICY_IPSEC
652 * do not raise error even if setsockopt fails, kernel may have ipsec
655 if (setpolicy(rcvsock, "in bypass") < 0)
656 errx(1, "%s", ipsec_strerror());
657 if (setpolicy(rcvsock, "out bypass") < 0)
658 errx(1, "%s", ipsec_strerror());
661 int level = IPSEC_LEVEL_NONE;
663 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
665 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
667 #ifdef IP_AUTH_TRANS_LEVEL
668 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
671 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
674 #ifdef IP_AUTH_NETWORK_LEVEL
675 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
679 #endif /*IPSEC_POLICY_IPSEC*/
684 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&i,
685 sizeof(i)) < 0 && useproto != IPPROTO_NONE) {
686 perror("setsockopt(SO_SNDBUF)");
689 #endif /* SO_SNDBUF */
690 if (options & SO_DEBUG)
691 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
692 (char *)&on, sizeof(on));
693 if (options & SO_DONTROUTE)
694 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
695 (char *)&on, sizeof(on));
697 #ifdef IPSEC_POLICY_IPSEC
699 * do not raise error even if setsockopt fails, kernel may have ipsec
702 if (setpolicy(sndsock, "in bypass") < 0)
703 errx(1, "%s", ipsec_strerror());
704 if (setpolicy(sndsock, "out bypass") < 0)
705 errx(1, "%s", ipsec_strerror());
708 int level = IPSEC_LEVEL_BYPASS;
710 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
712 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
714 #ifdef IP_AUTH_TRANS_LEVEL
715 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
718 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
721 #ifdef IP_AUTH_NETWORK_LEVEL
722 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
726 #endif /*IPSEC_POLICY_IPSEC*/
732 bzero(&Src, sizeof(Src));
734 struct addrinfo hints, *res;
737 memset(&hints, 0, sizeof(hints));
738 hints.ai_family = AF_INET6;
739 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
740 hints.ai_flags = AI_NUMERICHOST;
741 error = getaddrinfo(source, "0", &hints, &res);
743 printf("traceroute6: %s: %s\n", source,
744 gai_strerror(error));
747 if (res->ai_addrlen > sizeof(Src)) {
748 printf("traceroute6: %s: %s\n", source,
749 gai_strerror(error));
752 memcpy(&Src, res->ai_addr, res->ai_addrlen);
755 struct sockaddr_in6 Nxt;
760 Nxt.sin6_port = htons(DUMMY_PORT);
761 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
765 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
770 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
771 perror("getsockname");
774 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
775 src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) {
776 fprintf(stderr, "getnameinfo failed for source\n");
783 Src.sin6_port = htons(0);
784 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
793 if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) {
794 perror("getsockname");
797 srcport = ntohs(Src.sin6_port);
803 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf,
804 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
805 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
806 fprintf(stderr, "traceroute6");
807 fprintf(stderr, " to %s (%s)", hostname, hbuf);
809 fprintf(stderr, " from %s", source);
810 fprintf(stderr, ", %lu hops max, %lu byte packets\n",
812 (void) fflush(stderr);
815 printf("Skipping %lu intermediate hops\n", first_hop - 1);
820 for (hops = first_hop; hops <= max_hops; ++hops) {
821 struct in6_addr lastaddr;
825 printf("%2lu ", hops);
826 bzero(&lastaddr, sizeof(lastaddr));
827 for (probe = 0; probe < nprobes; ++probe) {
829 struct timeval t1, t2;
831 (void) gettimeofday(&t1, NULL);
832 send_probe(++seq, hops);
833 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
834 (void) gettimeofday(&t2, NULL);
835 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
836 if (!IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
839 fputs("\n ", stdout);
841 lastaddr = Rcv.sin6_addr;
843 printf(" %.3f ms", deltaT(&t1, &t2));
845 case ICMP6_DST_UNREACH_NOROUTE:
849 case ICMP6_DST_UNREACH_ADMIN:
853 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
857 case ICMP6_DST_UNREACH_ADDR:
861 case ICMP6_DST_UNREACH_NOPORT:
873 (void) fflush(stdout);
877 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
886 wait_for_reply(int sock, struct msghdr *mhdr)
889 struct pollfd pfd[1];
893 pfd[0].events = POLLIN;
896 if (poll(pfd, 1, waittime * 1000) > 0)
897 cc = recvmsg(rcvsock, mhdr, 0);
905 fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
906 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
908 memset(fdsp, 0, fdsn);
910 wait.tv_sec = waittime; wait.tv_usec = 0;
912 if (select(sock+1, fdsp, NULL, NULL, &wait) > 0)
913 cc = recvmsg(rcvsock, mhdr, 0);
921 #ifdef IPSEC_POLICY_IPSEC
923 setpolicy(int so, char *policy)
927 buf = ipsec_set_policy(policy, strlen(policy));
929 warnx("%s", ipsec_strerror());
932 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
933 buf, ipsec_get_policylen(buf));
943 send_probe(int seq, u_long hops)
945 struct icmp6_hdr *icp;
952 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
953 (char *)&i, sizeof(i)) < 0) {
954 perror("setsockopt IPV6_UNICAST_HOPS");
957 Dst.sin6_port = htons(port + seq);
958 (void) gettimeofday(&tv, NULL);
959 tv32.tv32_sec = htonl(tv.tv_sec);
960 tv32.tv32_usec = htonl(tv.tv_usec);
964 icp = (struct icmp6_hdr *)outpacket;
966 icp->icmp6_type = ICMP6_ECHO_REQUEST;
968 icp->icmp6_cksum = 0;
969 icp->icmp6_id = ident;
970 icp->icmp6_seq = htons(seq);
971 bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN),
979 bcopy(&tv32, &op->tv, sizeof tv32);
982 /* No space for anything. No harm as seq/tv32 are decorative. */
985 fprintf(stderr, "Unknown probe protocol %d.\n", useproto);
989 i = sendto(sndsock, (char *)outpacket, datalen, 0,
990 (struct sockaddr *)&Dst, Dst.sin6_len);
991 if (i < 0 || i != datalen) {
994 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
995 hostname, datalen, i);
996 (void) fflush(stdout);
1001 get_hoplim(struct msghdr *mhdr)
1005 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1006 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1007 if (cm->cmsg_level == IPPROTO_IPV6 &&
1008 cm->cmsg_type == IPV6_HOPLIMIT &&
1009 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1010 return(*(int *)CMSG_DATA(cm));
1017 deltaT(struct timeval *t1p, struct timeval *t2p)
1021 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
1022 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
1027 * Convert an ICMP "type" field to a printable string.
1032 u_char t = t0 & 0xff;
1036 case ICMP6_DST_UNREACH:
1037 cp = "Destination Unreachable";
1039 case ICMP6_PACKET_TOO_BIG:
1040 cp = "Packet Too Big";
1042 case ICMP6_TIME_EXCEEDED:
1043 cp = "Time Exceeded";
1045 case ICMP6_PARAM_PROB:
1046 cp = "Parameter Problem";
1048 case ICMP6_ECHO_REQUEST:
1049 cp = "Echo Request";
1051 case ICMP6_ECHO_REPLY:
1054 case ICMP6_MEMBERSHIP_QUERY:
1055 cp = "Group Membership Query";
1057 case ICMP6_MEMBERSHIP_REPORT:
1058 cp = "Group Membership Report";
1060 case ICMP6_MEMBERSHIP_REDUCTION:
1061 cp = "Group Membership Reduction";
1063 case ND_ROUTER_SOLICIT:
1064 cp = "Router Solicitation";
1066 case ND_ROUTER_ADVERT:
1067 cp = "Router Advertisement";
1069 case ND_NEIGHBOR_SOLICIT:
1070 cp = "Neighbor Solicitation";
1072 case ND_NEIGHBOR_ADVERT:
1073 cp = "Neighbor Advertisement";
1086 packet_ok(struct msghdr *mhdr, int cc, int seq)
1088 struct icmp6_hdr *icp;
1089 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1091 char *buf = (char *)mhdr->msg_iov[0].iov_base;
1094 char hbuf[NI_MAXHOST];
1102 ip = (struct ip6_hdr *) buf;
1103 hlen = sizeof(struct ip6_hdr);
1104 if (cc < hlen + sizeof(struct icmp6_hdr)) {
1106 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1107 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1108 strlcpy(hbuf, "invalid", sizeof(hbuf));
1109 printf("packet too short (%d bytes) from %s\n", cc,
1115 icp = (struct icmp6_hdr *)(buf + hlen);
1117 if (cc < sizeof(struct icmp6_hdr)) {
1119 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1120 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1121 strlcpy(hbuf, "invalid", sizeof(hbuf));
1122 printf("data too short (%d bytes) from %s\n", cc, hbuf);
1126 icp = (struct icmp6_hdr *)buf;
1128 /* get optional information via advanced API */
1131 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1132 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1133 if (cm->cmsg_level == IPPROTO_IPV6 &&
1134 cm->cmsg_type == IPV6_PKTINFO &&
1136 CMSG_LEN(sizeof(struct in6_pktinfo)))
1137 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
1139 if (cm->cmsg_level == IPPROTO_IPV6 &&
1140 cm->cmsg_type == IPV6_HOPLIMIT &&
1141 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1142 hlimp = (int *)CMSG_DATA(cm);
1144 if (rcvpktinfo == NULL || hlimp == NULL) {
1145 warnx("failed to get received hop limit or packet info");
1149 rcvhlim = 0; /*XXX*/
1155 type = icp->icmp6_type;
1156 code = icp->icmp6_code;
1157 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
1158 || type == ICMP6_DST_UNREACH) {
1159 struct ip6_hdr *hip;
1162 hip = (struct ip6_hdr *)(icp + 1);
1163 if ((up = get_uphdr(hip, (u_char *)(buf + cc))) == NULL) {
1165 warnx("failed to get upper layer header");
1169 case IPPROTO_ICMPV6:
1170 if (((struct icmp6_hdr *)up)->icmp6_id == ident &&
1171 ((struct icmp6_hdr *)up)->icmp6_seq == htons(seq))
1172 return (type == ICMP6_TIME_EXCEEDED ?
1176 if (((struct udphdr *)up)->uh_sport == htons(srcport) &&
1177 ((struct udphdr *)up)->uh_dport == htons(port + seq))
1178 return (type == ICMP6_TIME_EXCEEDED ?
1182 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
1184 fprintf(stderr, "Unknown probe proto %d.\n", useproto);
1187 } else if (useproto == IPPROTO_ICMPV6 && type == ICMP6_ECHO_REPLY) {
1188 if (icp->icmp6_id == ident &&
1189 icp->icmp6_seq == htons(seq))
1190 return (ICMP6_DST_UNREACH_NOPORT + 1);
1193 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
1197 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1198 sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0)
1199 strlcpy(sbuf, "invalid", sizeof(sbuf));
1200 printf("\n%d bytes from %s to %s", cc, sbuf,
1201 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1202 dbuf, sizeof(dbuf)) : "?");
1203 printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
1205 p = (u_int8_t *)(icp + 1);
1207 for (i = 0; i < cc; i++) {
1212 printf("%02x", p[i]);
1213 if (i % WIDTH == WIDTH - 1)
1216 if (cc % WIDTH != 0)
1223 * Increment pointer until find the UDP or ICMP header.
1226 get_uphdr(struct ip6_hdr *ip6, u_char *lim)
1228 u_char *cp = (u_char *)ip6, nh;
1230 static u_char none_hdr[1]; /* Fake pointer for IPPROTO_NONE. */
1232 if (cp + sizeof(*ip6) > lim)
1236 cp += sizeof(struct ip6_hdr);
1238 while (lim - cp >= (nh == IPPROTO_NONE ? 0 : 8)) {
1243 case IPPROTO_ICMPV6:
1244 return(useproto == nh ? cp : NULL);
1246 return(useproto == nh ? cp : NULL);
1248 return(useproto == nh ? none_hdr : NULL);
1249 case IPPROTO_FRAGMENT:
1250 hlen = sizeof(struct ip6_frag);
1251 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1254 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
1255 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1258 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
1259 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1270 print(struct msghdr *mhdr, int cc)
1272 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1273 char hbuf[NI_MAXHOST];
1275 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1276 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1277 strlcpy(hbuf, "invalid", sizeof(hbuf));
1279 printf(" %s", hbuf);
1281 printf(" %s (%s)", inetname((struct sockaddr *)from), hbuf);
1283 printf(" %s", inetname((struct sockaddr *)from));
1287 printf(" %d bytes to %s", cc,
1288 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1289 hbuf, sizeof(hbuf)) : "?");
1291 printf(" %d bytes of data to %s", cc,
1292 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1293 hbuf, sizeof(hbuf)) : "?");
1299 * Construct an Internet address representation.
1300 * If the nflag has been supplied, give
1301 * numeric value, otherwise try for symbolic name.
1304 inetname(struct sockaddr *sa)
1306 static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1];
1307 static int first = 1;
1310 if (first && !nflag) {
1312 if (gethostname(domain, sizeof(domain)) == 0 &&
1313 (cp = strchr(domain, '.')))
1314 (void) strlcpy(domain, cp + 1, sizeof(domain));
1320 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1321 NI_NAMEREQD) == 0) {
1322 if ((cp = strchr(line, '.')) &&
1323 !strcmp(cp + 1, domain))
1331 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1332 NI_NUMERICHOST) != 0)
1333 strlcpy(line, "invalid", sizeof(line));
1342 "usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1343 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");