traceroute6.c revision 1.1 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun /*-
31 1.1 itojun * Copyright (c) 1990, 1993
32 1.1 itojun * The Regents of the University of California. All rights reserved.
33 1.1 itojun *
34 1.1 itojun * This code is derived from software contributed to Berkeley by
35 1.1 itojun * Van Jacobson.
36 1.1 itojun *
37 1.1 itojun * Redistribution and use in source and binary forms, with or without
38 1.1 itojun * modification, are permitted provided that the following conditions
39 1.1 itojun * are met:
40 1.1 itojun * 1. Redistributions of source code must retain the above copyright
41 1.1 itojun * notice, this list of conditions and the following disclaimer.
42 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 itojun * notice, this list of conditions and the following disclaimer in the
44 1.1 itojun * documentation and/or other materials provided with the distribution.
45 1.1 itojun * 3. All advertising materials mentioning features or use of this software
46 1.1 itojun * must display the following acknowledgement:
47 1.1 itojun * This product includes software developed by the University of
48 1.1 itojun * California, Berkeley and its contributors.
49 1.1 itojun * 4. Neither the name of the University nor the names of its contributors
50 1.1 itojun * may be used to endorse or promote products derived from this software
51 1.1 itojun * without specific prior written permission.
52 1.1 itojun *
53 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.1 itojun * SUCH DAMAGE.
64 1.1 itojun */
65 1.1 itojun
66 1.1 itojun #include <sys/cdefs.h>
67 1.1 itojun #ifndef lint
68 1.1 itojun #if 0
69 1.1 itojun static char copyright[] =
70 1.1 itojun "@(#) Copyright (c) 1990, 1993\n\
71 1.1 itojun The Regents of the University of California. All rights reserved.\n";
72 1.1 itojun #endif
73 1.1 itojun #endif /* not lint */
74 1.1 itojun
75 1.1 itojun #ifndef lint
76 1.1 itojun #if 0
77 1.1 itojun static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93";
78 1.1 itojun #else
79 1.1 itojun __RCSID("@(#)traceroute.c 8.1 (Berkeley) 6/6/93");
80 1.1 itojun #endif
81 1.1 itojun #endif /* not lint */
82 1.1 itojun
83 1.1 itojun /*
84 1.1 itojun * traceroute host - trace the route ip packets follow going to "host".
85 1.1 itojun *
86 1.1 itojun * Attempt to trace the route an ip packet would follow to some
87 1.1 itojun * internet host. We find out intermediate hops by launching probe
88 1.1 itojun * packets with a small ttl (time to live) then listening for an
89 1.1 itojun * icmp "time exceeded" reply from a gateway. We start our probes
90 1.1 itojun * with a ttl of one and increase by one until we get an icmp "port
91 1.1 itojun * unreachable" (which means we got to "host") or hit a max (which
92 1.1 itojun * defaults to 30 hops & can be changed with the -m flag). Three
93 1.1 itojun * probes (change with -q flag) are sent at each ttl setting and a
94 1.1 itojun * line is printed showing the ttl, address of the gateway and
95 1.1 itojun * round trip time of each probe. If the probe answers come from
96 1.1 itojun * different gateways, the address of each responding system will
97 1.1 itojun * be printed. If there is no response within a 5 sec. timeout
98 1.1 itojun * interval (changed with the -w flag), a "*" is printed for that
99 1.1 itojun * probe.
100 1.1 itojun *
101 1.1 itojun * Probe packets are UDP format. We don't want the destination
102 1.1 itojun * host to process them so the destination port is set to an
103 1.1 itojun * unlikely value (if some clod on the destination is using that
104 1.1 itojun * value, it can be changed with the -p flag).
105 1.1 itojun *
106 1.1 itojun * A sample use might be:
107 1.1 itojun *
108 1.1 itojun * [yak 71]% traceroute nis.nsf.net.
109 1.1 itojun * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
110 1.1 itojun * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
111 1.1 itojun * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
112 1.1 itojun * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
113 1.1 itojun * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
114 1.1 itojun * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
115 1.1 itojun * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
116 1.1 itojun * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
117 1.1 itojun * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
118 1.1 itojun * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
119 1.1 itojun * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
120 1.1 itojun * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
121 1.1 itojun *
122 1.1 itojun * Note that lines 2 & 3 are the same. This is due to a buggy
123 1.1 itojun * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
124 1.1 itojun * packets with a zero ttl.
125 1.1 itojun *
126 1.1 itojun * A more interesting example is:
127 1.1 itojun *
128 1.1 itojun * [yak 72]% traceroute allspice.lcs.mit.edu.
129 1.1 itojun * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
130 1.1 itojun * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
131 1.1 itojun * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
132 1.1 itojun * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
133 1.1 itojun * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
134 1.1 itojun * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
135 1.1 itojun * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
136 1.1 itojun * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
137 1.1 itojun * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
138 1.1 itojun * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
139 1.1 itojun * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
140 1.1 itojun * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
141 1.1 itojun * 12 * * *
142 1.1 itojun * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
143 1.1 itojun * 14 * * *
144 1.1 itojun * 15 * * *
145 1.1 itojun * 16 * * *
146 1.1 itojun * 17 * * *
147 1.1 itojun * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
148 1.1 itojun *
149 1.1 itojun * (I start to see why I'm having so much trouble with mail to
150 1.1 itojun * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
151 1.1 itojun * either don't send ICMP "time exceeded" messages or send them
152 1.1 itojun * with a ttl too small to reach us. 14 - 17 are running the
153 1.1 itojun * MIT C Gateway code that doesn't send "time exceeded"s. God
154 1.1 itojun * only knows what's going on with 12.
155 1.1 itojun *
156 1.1 itojun * The silent gateway 12 in the above may be the result of a bug in
157 1.1 itojun * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
158 1.1 itojun * sends an unreachable message using whatever ttl remains in the
159 1.1 itojun * original datagram. Since, for gateways, the remaining ttl is
160 1.1 itojun * zero, the icmp "time exceeded" is guaranteed to not make it back
161 1.1 itojun * to us. The behavior of this bug is slightly more interesting
162 1.1 itojun * when it appears on the destination system:
163 1.1 itojun *
164 1.1 itojun * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
165 1.1 itojun * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
166 1.1 itojun * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
167 1.1 itojun * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
168 1.1 itojun * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
169 1.1 itojun * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
170 1.1 itojun * 7 * * *
171 1.1 itojun * 8 * * *
172 1.1 itojun * 9 * * *
173 1.1 itojun * 10 * * *
174 1.1 itojun * 11 * * *
175 1.1 itojun * 12 * * *
176 1.1 itojun * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
177 1.1 itojun *
178 1.1 itojun * Notice that there are 12 "gateways" (13 is the final
179 1.1 itojun * destination) and exactly the last half of them are "missing".
180 1.1 itojun * What's really happening is that rip (a Sun-3 running Sun OS3.5)
181 1.1 itojun * is using the ttl from our arriving datagram as the ttl in its
182 1.1 itojun * icmp reply. So, the reply will time out on the return path
183 1.1 itojun * (with no notice sent to anyone since icmp's aren't sent for
184 1.1 itojun * icmp's) until we probe with a ttl that's at least twice the path
185 1.1 itojun * length. I.e., rip is really only 7 hops away. A reply that
186 1.1 itojun * returns with a ttl of 1 is a clue this problem exists.
187 1.1 itojun * Traceroute prints a "!" after the time if the ttl is <= 1.
188 1.1 itojun * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
189 1.1 itojun * non-standard (HPUX) software, expect to see this problem
190 1.1 itojun * frequently and/or take care picking the target host of your
191 1.1 itojun * probes.
192 1.1 itojun *
193 1.1 itojun * Other possible annotations after the time are !H, !N, !P (got a host,
194 1.1 itojun * network or protocol unreachable, respectively), !S or !F (source
195 1.1 itojun * route failed or fragmentation needed -- neither of these should
196 1.1 itojun * ever occur and the associated gateway is busted if you see one). If
197 1.1 itojun * almost all the probes result in some kind of unreachable, traceroute
198 1.1 itojun * will give up and exit.
199 1.1 itojun *
200 1.1 itojun * Notes
201 1.1 itojun * -----
202 1.1 itojun * This program must be run by root or be setuid. (I suggest that
203 1.1 itojun * you *don't* make it setuid -- casual use could result in a lot
204 1.1 itojun * of unnecessary traffic on our poor, congested nets.)
205 1.1 itojun *
206 1.1 itojun * This program requires a kernel mod that does not appear in any
207 1.1 itojun * system available from Berkeley: A raw ip socket using proto
208 1.1 itojun * IPPROTO_RAW must interpret the data sent as an ip datagram (as
209 1.1 itojun * opposed to data to be wrapped in a ip datagram). See the README
210 1.1 itojun * file that came with the source to this program for a description
211 1.1 itojun * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
212 1.1 itojun * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
213 1.1 itojun * MODIFIED TO RUN THIS PROGRAM.
214 1.1 itojun *
215 1.1 itojun * The udp port usage may appear bizarre (well, ok, it is bizarre).
216 1.1 itojun * The problem is that an icmp message only contains 8 bytes of
217 1.1 itojun * data from the original datagram. 8 bytes is the size of a udp
218 1.1 itojun * header so, if we want to associate replies with the original
219 1.1 itojun * datagram, the necessary information must be encoded into the
220 1.1 itojun * udp header (the ip id could be used but there's no way to
221 1.1 itojun * interlock with the kernel's assignment of ip id's and, anyway,
222 1.1 itojun * it would have taken a lot more kernel hacking to allow this
223 1.1 itojun * code to set the ip id). So, to allow two or more users to
224 1.1 itojun * use traceroute simultaneously, we use this task's pid as the
225 1.1 itojun * source port (the high bit is set to move the port number out
226 1.1 itojun * of the "likely" range). To keep track of which probe is being
227 1.1 itojun * replied to (so times and/or hop counts don't get confused by a
228 1.1 itojun * reply that was delayed in transit), we increment the destination
229 1.1 itojun * port number before each probe.
230 1.1 itojun *
231 1.1 itojun * Don't use this as a coding example. I was trying to find a
232 1.1 itojun * routing problem and this code sort-of popped out after 48 hours
233 1.1 itojun * without sleep. I was amazed it ever compiled, much less ran.
234 1.1 itojun *
235 1.1 itojun * I stole the idea for this program from Steve Deering. Since
236 1.1 itojun * the first release, I've learned that had I attended the right
237 1.1 itojun * IETF working group meetings, I also could have stolen it from Guy
238 1.1 itojun * Almes or Matt Mathis. I don't know (or care) who came up with
239 1.1 itojun * the idea first. I envy the originators' perspicacity and I'm
240 1.1 itojun * glad they didn't keep the idea a secret.
241 1.1 itojun *
242 1.1 itojun * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
243 1.1 itojun * enhancements to the original distribution.
244 1.1 itojun *
245 1.1 itojun * I've hacked up a round-trip-route version of this that works by
246 1.1 itojun * sending a loose-source-routed udp datagram through the destination
247 1.1 itojun * back to yourself. Unfortunately, SO many gateways botch source
248 1.1 itojun * routing, the thing is almost worthless. Maybe one day...
249 1.1 itojun *
250 1.1 itojun * -- Van Jacobson (van (at) helios.ee.lbl.gov)
251 1.1 itojun * Tue Dec 20 03:50:13 PST 1988
252 1.1 itojun */
253 1.1 itojun
254 1.1 itojun #include <sys/param.h>
255 1.1 itojun #include <sys/time.h>
256 1.1 itojun #include <sys/socket.h>
257 1.1 itojun #include <sys/uio.h>
258 1.1 itojun #include <sys/file.h>
259 1.1 itojun #include <sys/ioctl.h>
260 1.1 itojun
261 1.1 itojun #include <netinet/in.h>
262 1.1 itojun
263 1.1 itojun #include <arpa/inet.h>
264 1.1 itojun
265 1.1 itojun #include <netdb.h>
266 1.1 itojun #include <stdio.h>
267 1.1 itojun #include <err.h>
268 1.1 itojun #include <errno.h>
269 1.1 itojun #include <stdlib.h>
270 1.1 itojun #include <string.h>
271 1.1 itojun #include <unistd.h>
272 1.1 itojun
273 1.1 itojun #include <netinet/ip6.h>
274 1.1 itojun #include <netinet/icmp6.h>
275 1.1 itojun #include <netinet/udp.h>
276 1.1 itojun
277 1.1 itojun #ifdef IPSEC
278 1.1 itojun #include <net/route.h>
279 1.1 itojun #include <netinet6/ipsec.h>
280 1.1 itojun #endif
281 1.1 itojun
282 1.1 itojun #define freehostent(hp)
283 1.1 itojun #define DUMMY_PORT 10010
284 1.1 itojun
285 1.1 itojun #define MAXPACKET 65535 /* max ip packet size */
286 1.1 itojun #ifndef MAXHOSTNAMELEN
287 1.1 itojun #define MAXHOSTNAMELEN 64
288 1.1 itojun #endif
289 1.1 itojun
290 1.1 itojun #ifndef FD_SET
291 1.1 itojun #define NFDBITS (8*sizeof(fd_set))
292 1.1 itojun #define FD_SETSIZE NFDBITS
293 1.1 itojun #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
294 1.1 itojun #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
295 1.1 itojun #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
296 1.1 itojun #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
297 1.1 itojun #endif
298 1.1 itojun
299 1.1 itojun #define Fprintf (void)fprintf
300 1.1 itojun #define Sprintf (void)sprintf
301 1.1 itojun #define Printf (void)printf
302 1.1 itojun
303 1.1 itojun /*
304 1.1 itojun * format of a (udp) probe packet.
305 1.1 itojun */
306 1.1 itojun struct opacket {
307 1.1 itojun u_char seq; /* sequence number of this packet */
308 1.1 itojun u_char hops; /* hop limit of the packet */
309 1.1 itojun struct timeval tv; /* time packet left */
310 1.1 itojun };
311 1.1 itojun
312 1.1 itojun u_char packet[512]; /* last inbound (icmp) packet */
313 1.1 itojun struct opacket *outpacket; /* last output (udp) packet */
314 1.1 itojun
315 1.1 itojun int main __P((int, char *[]));
316 1.1 itojun int wait_for_reply __P((int, struct msghdr *));
317 1.1 itojun void send_probe __P((int, int));
318 1.1 itojun struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
319 1.1 itojun int get_hoplim __P((struct msghdr *));
320 1.1 itojun double deltaT __P((struct timeval *, struct timeval *));
321 1.1 itojun char *pr_type __P((int));
322 1.1 itojun int packet_ok __P((struct msghdr *, int, int));
323 1.1 itojun void print __P((struct msghdr *, int));
324 1.1 itojun void tvsub __P((struct timeval *, struct timeval *));
325 1.1 itojun char *inetname __P((struct in6_addr *));
326 1.1 itojun void usage __P((void));
327 1.1 itojun
328 1.1 itojun int rcvsock; /* receive (icmp) socket file descriptor */
329 1.1 itojun int sndsock; /* send (udp) socket file descriptor */
330 1.1 itojun struct timezone tz; /* leftover */
331 1.1 itojun
332 1.1 itojun struct msghdr rcvmhdr;
333 1.1 itojun struct iovec rcviov[2];
334 1.1 itojun int rcvhlim;
335 1.1 itojun struct in6_pktinfo *rcvpktinfo;
336 1.1 itojun
337 1.1 itojun struct sockaddr_in6 Src, Dst, Rcv;
338 1.1 itojun struct sockaddr_in6 *src = &Src, *dst = &Dst, *rcv = &Rcv;
339 1.1 itojun int datalen; /* How much data */
340 1.1 itojun char rtbuf[1024]; /*XXX*/
341 1.1 itojun struct cmsghdr *cmsg;
342 1.1 itojun
343 1.1 itojun char *source = 0;
344 1.1 itojun char *hostname;
345 1.1 itojun
346 1.1 itojun int nprobes = 3;
347 1.1 itojun int max_hops = 30;
348 1.1 itojun u_short ident;
349 1.1 itojun u_short port = 32768+666; /* start udp dest port # for probe packets */
350 1.1 itojun int options; /* socket options */
351 1.1 itojun int verbose;
352 1.1 itojun int waittime = 5; /* time to wait for response (in seconds) */
353 1.1 itojun int nflag; /* print addresses numerically */
354 1.1 itojun int lflag; /* print both numerical address & hostname */
355 1.1 itojun
356 1.1 itojun char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
357 1.1 itojun
358 1.1 itojun int
359 1.1 itojun main(argc, argv)
360 1.1 itojun int argc;
361 1.1 itojun char *argv[];
362 1.1 itojun {
363 1.1 itojun extern char *optarg;
364 1.1 itojun extern int optind;
365 1.1 itojun struct hostent *hp;
366 1.1 itojun int ch, i, on, probe, seq, hops;
367 1.1 itojun static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))
368 1.1 itojun + CMSG_SPACE(sizeof(int))];
369 1.1 itojun
370 1.1 itojun on = 1;
371 1.1 itojun seq = 0;
372 1.1 itojun
373 1.1 itojun while ((ch = getopt(argc, argv, "dlm:np:q:rs:w:vg:")) != EOF)
374 1.1 itojun switch(ch) {
375 1.1 itojun case 'd':
376 1.1 itojun options |= SO_DEBUG;
377 1.1 itojun break;
378 1.1 itojun case 'l':
379 1.1 itojun lflag++;
380 1.1 itojun break;
381 1.1 itojun case 'g':
382 1.1 itojun hp = gethostbyname2(optarg, AF_INET6);
383 1.1 itojun if (hp == NULL) {
384 1.1 itojun Fprintf(stderr,
385 1.1 itojun "traceroute6: unknown host %s\n", optarg);
386 1.1 itojun exit(1);
387 1.1 itojun }
388 1.1 itojun if (cmsg == NULL)
389 1.1 itojun cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
390 1.1 itojun inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, IPV6_RTHDR_LOOSE);
391 1.1 itojun break;
392 1.1 itojun case 'm':
393 1.1 itojun max_hops = atoi(optarg);
394 1.1 itojun if (max_hops <= 1) {
395 1.1 itojun Fprintf(stderr,
396 1.1 itojun "traceroute6: max hoplimit must be >1.\n");
397 1.1 itojun exit(1);
398 1.1 itojun }
399 1.1 itojun break;
400 1.1 itojun case 'n':
401 1.1 itojun nflag++;
402 1.1 itojun break;
403 1.1 itojun case 'p':
404 1.1 itojun port = atoi(optarg);
405 1.1 itojun if (port < 1) {
406 1.1 itojun Fprintf(stderr,
407 1.1 itojun "traceroute6: port must be >0.\n");
408 1.1 itojun exit(1);
409 1.1 itojun }
410 1.1 itojun break;
411 1.1 itojun case 'q':
412 1.1 itojun nprobes = atoi(optarg);
413 1.1 itojun if (nprobes < 1) {
414 1.1 itojun Fprintf(stderr,
415 1.1 itojun "traceroute6: nprobes must be >0.\n");
416 1.1 itojun exit(1);
417 1.1 itojun }
418 1.1 itojun break;
419 1.1 itojun case 'r':
420 1.1 itojun options |= SO_DONTROUTE;
421 1.1 itojun break;
422 1.1 itojun case 's':
423 1.1 itojun /*
424 1.1 itojun * set the ip source address of the outbound
425 1.1 itojun * probe (e.g., on a multi-homed host).
426 1.1 itojun */
427 1.1 itojun source = optarg;
428 1.1 itojun break;
429 1.1 itojun case 'v':
430 1.1 itojun verbose++;
431 1.1 itojun break;
432 1.1 itojun case 'w':
433 1.1 itojun waittime = atoi(optarg);
434 1.1 itojun if (waittime <= 1) {
435 1.1 itojun Fprintf(stderr,
436 1.1 itojun "traceroute6: wait must be >1 sec.\n");
437 1.1 itojun exit(1);
438 1.1 itojun }
439 1.1 itojun break;
440 1.1 itojun default:
441 1.1 itojun usage();
442 1.1 itojun }
443 1.1 itojun argc -= optind;
444 1.1 itojun argv += optind;
445 1.1 itojun
446 1.1 itojun if (argc < 1)
447 1.1 itojun usage();
448 1.1 itojun
449 1.1 itojun #if 1
450 1.1 itojun setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
451 1.1 itojun #else
452 1.1 itojun setlinebuf (stdout);
453 1.1 itojun #endif
454 1.1 itojun
455 1.1 itojun (void) bzero((char *)dst, sizeof(Dst));
456 1.1 itojun Dst.sin6_family = AF_INET6;
457 1.1 itojun
458 1.1 itojun hp = (struct hostent *)gethostbyname2(*argv, AF_INET6);
459 1.1 itojun if (hp == NULL) {
460 1.1 itojun if (inet_pton(AF_INET6, *argv, &Dst.sin6_addr) != 1) {
461 1.1 itojun (void)fprintf(stderr,
462 1.1 itojun "traceroute6: unknown host %s\n", *argv);
463 1.1 itojun exit(1);
464 1.1 itojun }
465 1.1 itojun hostname = *argv;
466 1.1 itojun } else {
467 1.1 itojun bcopy(hp->h_addr, (caddr_t)&Dst.sin6_addr, hp->h_length);
468 1.1 itojun hostname = strdup(hp->h_name);
469 1.1 itojun }
470 1.1 itojun freehostent(hp);
471 1.1 itojun
472 1.1 itojun if (*++argv)
473 1.1 itojun datalen = atoi(*argv);
474 1.1 itojun if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) {
475 1.1 itojun Fprintf(stderr,
476 1.1 itojun "traceroute6: packet size must be 0 <= s < %ld.\n",
477 1.1 itojun (long)(MAXPACKET - sizeof(struct opacket)));
478 1.1 itojun exit(1);
479 1.1 itojun }
480 1.1 itojun datalen += sizeof(struct opacket);
481 1.1 itojun outpacket = (struct opacket *)malloc((unsigned)datalen);
482 1.1 itojun if (! outpacket) {
483 1.1 itojun perror("traceroute6: malloc");
484 1.1 itojun exit(1);
485 1.1 itojun }
486 1.1 itojun (void) bzero((char *)outpacket, datalen);
487 1.1 itojun
488 1.1 itojun /*
489 1.1 itojun * Receive ICMP
490 1.1 itojun */
491 1.1 itojun if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
492 1.1 itojun perror("traceroute6: icmp socket");
493 1.1 itojun exit(5);
494 1.1 itojun }
495 1.1 itojun /* initialize msghdr for receiving packets */
496 1.1 itojun rcviov[0].iov_base = (caddr_t)packet;
497 1.1 itojun rcviov[0].iov_len = sizeof(packet);
498 1.1 itojun rcvmhdr.msg_name = (caddr_t)rcv;
499 1.1 itojun rcvmhdr.msg_namelen = sizeof(*rcv);
500 1.1 itojun rcvmhdr.msg_iov = rcviov;
501 1.1 itojun rcvmhdr.msg_iovlen = 1;
502 1.1 itojun rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
503 1.1 itojun rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
504 1.1 itojun
505 1.1 itojun /* specify to tell receiving interface */
506 1.1 itojun if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
507 1.1 itojun sizeof(on)) < 0)
508 1.1 itojun err(1, "setsockopt(IPV6_PKTINFO)");
509 1.1 itojun
510 1.1 itojun /* specify to tell value of hoplimit field of received IP6 hdr */
511 1.1 itojun if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
512 1.1 itojun sizeof(on)) < 0)
513 1.1 itojun err(1, "setsockopt(IPV6_HOPLIMIT)");
514 1.1 itojun
515 1.1 itojun if (options & SO_DEBUG)
516 1.1 itojun (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
517 1.1 itojun (char *)&on, sizeof(on));
518 1.1 itojun if (options & SO_DONTROUTE)
519 1.1 itojun (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
520 1.1 itojun (char *)&on, sizeof(on));
521 1.1 itojun #ifdef IPSEC
522 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
523 1.1 itojun {
524 1.1 itojun int len;
525 1.1 itojun char buf[16];
526 1.1 itojun if ((len = ipsec_set_policy(buf, sizeof(buf), "bypass")) < 0)
527 1.1 itojun errx(1, ipsec_strerror());
528 1.1 itojun if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
529 1.1 itojun buf, len) < 0)
530 1.1 itojun err(1, NULL);
531 1.1 itojun }
532 1.1 itojun #else
533 1.1 itojun {
534 1.1 itojun int level = IPSEC_LEVEL_NONE;
535 1.1 itojun
536 1.1 itojun (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
537 1.1 itojun sizeof(level));
538 1.1 itojun (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
539 1.1 itojun sizeof(level));
540 1.1 itojun #ifdef IP_AUTH_TRANS_LEVEL
541 1.1 itojun (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
542 1.1 itojun sizeof(level));
543 1.1 itojun #else
544 1.1 itojun (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
545 1.1 itojun sizeof(level));
546 1.1 itojun #endif
547 1.1 itojun #ifdef IP_AUTH_NETWORK_LEVEL
548 1.1 itojun (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
549 1.1 itojun sizeof(level));
550 1.1 itojun #endif
551 1.1 itojun }
552 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
553 1.1 itojun #endif /*IPSEC*/
554 1.1 itojun
555 1.1 itojun /*
556 1.1 itojun * Send UDP
557 1.1 itojun */
558 1.1 itojun if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
559 1.1 itojun perror("traceroute6: udp socket");
560 1.1 itojun exit(5);
561 1.1 itojun }
562 1.1 itojun #ifdef SO_SNDBUF
563 1.1 itojun if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
564 1.1 itojun sizeof(datalen)) < 0) {
565 1.1 itojun perror("traceroute6: SO_SNDBUF");
566 1.1 itojun exit(6);
567 1.1 itojun }
568 1.1 itojun #endif /* SO_SNDBUF */
569 1.1 itojun if (options & SO_DEBUG)
570 1.1 itojun (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
571 1.1 itojun (char *)&on, sizeof(on));
572 1.1 itojun if (options & SO_DONTROUTE)
573 1.1 itojun (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
574 1.1 itojun (char *)&on, sizeof(on));
575 1.1 itojun if (cmsg != NULL) {
576 1.1 itojun inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
577 1.1 itojun (void) setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
578 1.1 itojun rtbuf, cmsg->cmsg_len);
579 1.1 itojun }
580 1.1 itojun #ifdef IPSEC
581 1.1 itojun #ifdef IPSEC_POLICY_IPSEC
582 1.1 itojun {
583 1.1 itojun int len;
584 1.1 itojun char buf[16];
585 1.1 itojun if ((len = ipsec_set_policy(buf, sizeof(buf), "bypass")) < 0)
586 1.1 itojun errx(1, ipsec_strerror());
587 1.1 itojun if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
588 1.1 itojun buf, len) < 0)
589 1.1 itojun err(1, NULL);
590 1.1 itojun }
591 1.1 itojun #else
592 1.1 itojun {
593 1.1 itojun int level = IPSEC_LEVEL_BYPASS;
594 1.1 itojun
595 1.1 itojun (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
596 1.1 itojun sizeof(level));
597 1.1 itojun (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
598 1.1 itojun sizeof(level));
599 1.1 itojun #ifdef IP_AUTH_TRANS_LEVEL
600 1.1 itojun (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
601 1.1 itojun sizeof(level));
602 1.1 itojun #else
603 1.1 itojun (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
604 1.1 itojun sizeof(level));
605 1.1 itojun #endif
606 1.1 itojun #ifdef IP_AUTH_NETWORK_LEVEL
607 1.1 itojun (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
608 1.1 itojun sizeof(level));
609 1.1 itojun #endif
610 1.1 itojun }
611 1.1 itojun #endif /*IPSEC_POLICY_IPSEC*/
612 1.1 itojun #endif /*IPSEC*/
613 1.1 itojun
614 1.1 itojun /*
615 1.1 itojun * Source selection
616 1.1 itojun */
617 1.1 itojun bzero((char *)src, sizeof(Src));
618 1.1 itojun if (source) {
619 1.1 itojun if (inet_pton(AF_INET6, source, &Src.sin6_addr) != 1) {
620 1.1 itojun Printf("traceroute6: unknown host %s\n", source);
621 1.1 itojun exit(1);
622 1.1 itojun }
623 1.1 itojun } else {
624 1.1 itojun struct sockaddr_in6 Nxt;
625 1.1 itojun int dummy, len;
626 1.1 itojun
627 1.1 itojun len = sizeof(Src);
628 1.1 itojun Nxt = Dst;
629 1.1 itojun Nxt.sin6_port = htons(DUMMY_PORT);
630 1.1 itojun if (cmsg != NULL)
631 1.1 itojun bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
632 1.1 itojun sizeof(Nxt.sin6_addr));
633 1.1 itojun if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
634 1.1 itojun perror("socket") ;
635 1.1 itojun }
636 1.1 itojun if(-1 == connect(dummy, (struct sockaddr *)&Nxt, sizeof(Nxt)))
637 1.1 itojun perror("connect");
638 1.1 itojun if(-1 == getsockname(dummy, (struct sockaddr *)src, &len)) {
639 1.1 itojun perror("getsockname");
640 1.1 itojun printf("%d\n", errno);
641 1.1 itojun }
642 1.1 itojun close(dummy) ;
643 1.1 itojun }
644 1.1 itojun ident = (getpid() & 0xffff) | 0x8000;
645 1.1 itojun Src.sin6_family = AF_INET6;
646 1.1 itojun Src.sin6_port = htons(ident);
647 1.1 itojun if (bind(sndsock, (struct sockaddr *)src, sizeof(Src)) < 0){
648 1.1 itojun perror ("traceroute6: bind:");
649 1.1 itojun exit (1);
650 1.1 itojun }
651 1.1 itojun
652 1.1 itojun /*
653 1.1 itojun * Message to users
654 1.1 itojun */
655 1.1 itojun Fprintf(stderr, "traceroute to %s (%s)", hostname,
656 1.1 itojun inet_ntop(AF_INET6, &Dst.sin6_addr,
657 1.1 itojun ntop_buf, sizeof(ntop_buf)));
658 1.1 itojun if (source)
659 1.1 itojun Fprintf(stderr, " from %s", source);
660 1.1 itojun Fprintf(stderr, ", %d hops max, %d byte packets\n", max_hops, datalen);
661 1.1 itojun (void) fflush(stderr);
662 1.1 itojun
663 1.1 itojun /*
664 1.1 itojun * Main loop
665 1.1 itojun */
666 1.1 itojun for (hops = 1; hops <= max_hops; ++hops) {
667 1.1 itojun struct in6_addr lastaddr;
668 1.1 itojun int got_there = 0;
669 1.1 itojun int unreachable = 0;
670 1.1 itojun
671 1.1 itojun Printf("%2d ", hops);
672 1.1 itojun bzero(&lastaddr, sizeof(lastaddr));
673 1.1 itojun for (probe = 0; probe < nprobes; ++probe) {
674 1.1 itojun int cc;
675 1.1 itojun struct timeval t1, t2;
676 1.1 itojun struct timezone tz;
677 1.1 itojun
678 1.1 itojun (void) gettimeofday(&t1, &tz);
679 1.1 itojun send_probe(++seq, hops);
680 1.1 itojun while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
681 1.1 itojun (void) gettimeofday(&t2, &tz);
682 1.1 itojun if ((i = packet_ok(&rcvmhdr, cc, seq))) {
683 1.1 itojun if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
684 1.1 itojun &lastaddr)) {
685 1.1 itojun print(&rcvmhdr, cc);
686 1.1 itojun lastaddr = Rcv.sin6_addr;
687 1.1 itojun }
688 1.1 itojun Printf(" %g ms", deltaT(&t1, &t2));
689 1.1 itojun switch(i - 1) {
690 1.1 itojun case ICMP6_DST_UNREACH_NOROUTE:
691 1.1 itojun ++unreachable;
692 1.1 itojun Printf(" !N");
693 1.1 itojun break;
694 1.1 itojun case ICMP6_DST_UNREACH_ADMIN:
695 1.1 itojun ++unreachable;
696 1.1 itojun Printf(" !P");
697 1.1 itojun break;
698 1.1 itojun case ICMP6_DST_UNREACH_NOTNEIGHBOR:
699 1.1 itojun ++unreachable;
700 1.1 itojun Printf(" !S");
701 1.1 itojun break;
702 1.1 itojun case ICMP6_DST_UNREACH_ADDR:
703 1.1 itojun ++unreachable;
704 1.1 itojun Printf(" !A");
705 1.1 itojun break;
706 1.1 itojun case ICMP6_DST_UNREACH_NOPORT:
707 1.1 itojun if (rcvhlim >= 0 &&
708 1.1 itojun rcvhlim <= 1)
709 1.1 itojun Printf(" !");
710 1.1 itojun ++got_there;
711 1.1 itojun break;
712 1.1 itojun }
713 1.1 itojun break;
714 1.1 itojun }
715 1.1 itojun }
716 1.1 itojun if (cc == 0)
717 1.1 itojun Printf(" *");
718 1.1 itojun (void) fflush(stdout);
719 1.1 itojun }
720 1.1 itojun putchar('\n');
721 1.1 itojun if (got_there || unreachable >= nprobes-1)
722 1.1 itojun exit(0);
723 1.1 itojun }
724 1.1 itojun
725 1.1 itojun exit(0);
726 1.1 itojun }
727 1.1 itojun
728 1.1 itojun int
729 1.1 itojun wait_for_reply(sock, mhdr)
730 1.1 itojun int sock;
731 1.1 itojun struct msghdr *mhdr;
732 1.1 itojun {
733 1.1 itojun fd_set fds;
734 1.1 itojun struct timeval wait;
735 1.1 itojun int cc = 0;
736 1.1 itojun
737 1.1 itojun FD_ZERO(&fds);
738 1.1 itojun FD_SET(sock, &fds);
739 1.1 itojun wait.tv_sec = waittime; wait.tv_usec = 0;
740 1.1 itojun
741 1.1 itojun if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0)
742 1.1 itojun cc = recvmsg(rcvsock, mhdr, 0);
743 1.1 itojun
744 1.1 itojun return(cc);
745 1.1 itojun }
746 1.1 itojun
747 1.1 itojun
748 1.1 itojun void
749 1.1 itojun send_probe(seq, hops)
750 1.1 itojun int seq, hops;
751 1.1 itojun {
752 1.1 itojun struct opacket *op = outpacket;
753 1.1 itojun int i;
754 1.1 itojun
755 1.1 itojun if(setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
756 1.1 itojun (char *)&hops, sizeof(hops)) < 0) {
757 1.1 itojun perror("setsockopt IPV6_UNICAST_HOPS");
758 1.1 itojun }
759 1.1 itojun
760 1.1 itojun Dst.sin6_port = htons(port + seq);
761 1.1 itojun
762 1.1 itojun op->seq = seq;
763 1.1 itojun op->hops = hops;
764 1.1 itojun (void) gettimeofday(&op->tv, &tz);
765 1.1 itojun
766 1.1 itojun i = sendto(sndsock, (char *)outpacket, datalen , 0,
767 1.1 itojun (struct sockaddr *)dst, sizeof(Dst));
768 1.1 itojun if (i < 0 || i != datalen) {
769 1.1 itojun if (i<0)
770 1.1 itojun perror("sendto");
771 1.1 itojun Printf("traceroute6: wrote %s %d chars, ret=%d\n", hostname,
772 1.1 itojun datalen, i);
773 1.1 itojun (void) fflush(stdout);
774 1.1 itojun }
775 1.1 itojun }
776 1.1 itojun
777 1.1 itojun int
778 1.1 itojun get_hoplim(mhdr)
779 1.1 itojun struct msghdr *mhdr;
780 1.1 itojun {
781 1.1 itojun struct cmsghdr *cm;
782 1.1 itojun
783 1.1 itojun for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
784 1.1 itojun cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
785 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
786 1.1 itojun cm->cmsg_type == IPV6_HOPLIMIT &&
787 1.1 itojun cm->cmsg_len == CMSG_LEN(sizeof(int)))
788 1.1 itojun return(*(int *)CMSG_DATA(cm));
789 1.1 itojun }
790 1.1 itojun
791 1.1 itojun return(-1);
792 1.1 itojun }
793 1.1 itojun
794 1.1 itojun double
795 1.1 itojun deltaT(t1p, t2p)
796 1.1 itojun struct timeval *t1p, *t2p;
797 1.1 itojun {
798 1.1 itojun register double dt;
799 1.1 itojun
800 1.1 itojun dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
801 1.1 itojun (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
802 1.1 itojun return (dt);
803 1.1 itojun }
804 1.1 itojun
805 1.1 itojun
806 1.1 itojun /*
807 1.1 itojun * Convert an ICMP "type" field to a printable string.
808 1.1 itojun */
809 1.1 itojun char *
810 1.1 itojun pr_type(t0)
811 1.1 itojun int t0;
812 1.1 itojun {
813 1.1 itojun u_char t = t0 & 0xff;
814 1.1 itojun char *cp;
815 1.1 itojun
816 1.1 itojun switch (t) {
817 1.1 itojun case ICMP6_DST_UNREACH:
818 1.1 itojun cp = "Destination Unreachable";
819 1.1 itojun break;
820 1.1 itojun case ICMP6_PACKET_TOO_BIG:
821 1.1 itojun cp = "Pakcet Too Big";
822 1.1 itojun break;
823 1.1 itojun case ICMP6_TIME_EXCEEDED:
824 1.1 itojun cp = "Time Exceeded";
825 1.1 itojun break;
826 1.1 itojun case ICMP6_PARAM_PROB:
827 1.1 itojun cp = "Parameter Problem";
828 1.1 itojun break;
829 1.1 itojun case ICMP6_ECHO_REQUEST:
830 1.1 itojun cp = "Echo Request";
831 1.1 itojun break;
832 1.1 itojun case ICMP6_ECHO_REPLY:
833 1.1 itojun cp = "Echo Reply";
834 1.1 itojun break;
835 1.1 itojun case ICMP6_MEMBERSHIP_QUERY:
836 1.1 itojun cp = "Group Membership Query";
837 1.1 itojun break;
838 1.1 itojun case ICMP6_MEMBERSHIP_REPORT:
839 1.1 itojun cp = "Group Membership Report";
840 1.1 itojun break;
841 1.1 itojun case ICMP6_MEMBERSHIP_REDUCTION:
842 1.1 itojun cp = "Group Membership Reduction";
843 1.1 itojun break;
844 1.1 itojun case ND_ROUTER_SOLICIT:
845 1.1 itojun cp = "Router Solicitation";
846 1.1 itojun break;
847 1.1 itojun case ND_ROUTER_ADVERT:
848 1.1 itojun cp = "Router Advertisement";
849 1.1 itojun break;
850 1.1 itojun case ND_NEIGHBOR_SOLICIT:
851 1.1 itojun cp = "Neighbor Solicitation";
852 1.1 itojun break;
853 1.1 itojun case ND_NEIGHBOR_ADVERT:
854 1.1 itojun cp = "Neighbor Advertisement";
855 1.1 itojun break;
856 1.1 itojun case ND_REDIRECT:
857 1.1 itojun cp = "Ridirect";
858 1.1 itojun break;
859 1.1 itojun default:
860 1.1 itojun cp = "Unknown";
861 1.1 itojun break;
862 1.1 itojun }
863 1.1 itojun return cp;
864 1.1 itojun }
865 1.1 itojun
866 1.1 itojun
867 1.1 itojun int
868 1.1 itojun packet_ok(mhdr, cc, seq)
869 1.1 itojun struct msghdr *mhdr;
870 1.1 itojun int cc;
871 1.1 itojun int seq;
872 1.1 itojun {
873 1.1 itojun register struct icmp6_hdr *icp;
874 1.1 itojun struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
875 1.1 itojun u_char type, code;
876 1.1 itojun char *buf = (char *)mhdr->msg_iov[0].iov_base;
877 1.1 itojun struct cmsghdr *cm;
878 1.1 itojun int *hlimp;
879 1.1 itojun
880 1.1 itojun #ifdef OLDRAWSOCKET
881 1.1 itojun int hlen;
882 1.1 itojun struct ip6_hdr *ip;
883 1.1 itojun #endif
884 1.1 itojun
885 1.1 itojun #ifdef OLDRAWSOCKET
886 1.1 itojun ip = (struct ip6_hdr *) buf;
887 1.1 itojun hlen = sizeof(struct ip6_hdr);
888 1.1 itojun if (cc < hlen + sizeof(struct icmp6_hdr)) {
889 1.1 itojun if (verbose)
890 1.1 itojun Printf("packet too short (%d bytes) from %s\n", cc,
891 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
892 1.1 itojun ntop_buf, sizeof(ntop_buf)));
893 1.1 itojun return (0);
894 1.1 itojun }
895 1.1 itojun cc -= hlen;
896 1.1 itojun icp = (struct icmp6_hdr *)(buf + hlen);
897 1.1 itojun #else
898 1.1 itojun if (cc < sizeof(struct icmp6_hdr)) {
899 1.1 itojun if (verbose)
900 1.1 itojun Printf("data too short (%d bytes) from %s\n", cc,
901 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
902 1.1 itojun ntop_buf, sizeof(ntop_buf)));
903 1.1 itojun return(0);
904 1.1 itojun }
905 1.1 itojun icp = (struct icmp6_hdr *)buf;
906 1.1 itojun #endif
907 1.1 itojun /* get optional information via advanced API */
908 1.1 itojun rcvpktinfo = NULL;
909 1.1 itojun hlimp = NULL;
910 1.1 itojun for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
911 1.1 itojun cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
912 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
913 1.1 itojun cm->cmsg_type == IPV6_PKTINFO &&
914 1.1 itojun cm->cmsg_len ==
915 1.1 itojun CMSG_LEN(sizeof(struct in6_pktinfo)))
916 1.1 itojun rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
917 1.1 itojun
918 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
919 1.1 itojun cm->cmsg_type == IPV6_HOPLIMIT &&
920 1.1 itojun cm->cmsg_len == CMSG_LEN(sizeof(int)))
921 1.1 itojun hlimp = (int *)CMSG_DATA(cm);
922 1.1 itojun }
923 1.1 itojun if (rcvpktinfo == NULL || hlimp == NULL) {
924 1.1 itojun warnx("failed to get received hop limit or packet info");
925 1.1 itojun return(0);
926 1.1 itojun }
927 1.1 itojun rcvhlim = *hlimp;
928 1.1 itojun
929 1.1 itojun type = icp->icmp6_type;
930 1.1 itojun code = icp->icmp6_code;
931 1.1 itojun if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
932 1.1 itojun || type == ICMP6_DST_UNREACH) {
933 1.1 itojun struct ip6_hdr *hip;
934 1.1 itojun struct udphdr *up;
935 1.1 itojun
936 1.1 itojun hip = (struct ip6_hdr *)(icp + 1);
937 1.1 itojun if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) {
938 1.1 itojun if (verbose)
939 1.1 itojun warnx("failed to get upper layer header");
940 1.1 itojun return(0);
941 1.1 itojun }
942 1.1 itojun if (up->uh_sport == htons(ident) &&
943 1.1 itojun up->uh_dport == htons(port+seq))
944 1.1 itojun return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
945 1.1 itojun }
946 1.1 itojun if (verbose) {
947 1.1 itojun int i;
948 1.1 itojun u_long *lp = (u_long *)(icp + 1);
949 1.1 itojun char sbuf[INET6_ADDRSTRLEN+1], dbuf[INET6_ADDRSTRLEN];
950 1.1 itojun
951 1.1 itojun Printf("\n%d bytes from %s to %s", cc,
952 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
953 1.1 itojun sbuf, sizeof(sbuf)),
954 1.1 itojun inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
955 1.1 itojun dbuf, sizeof(dbuf)));
956 1.1 itojun Printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
957 1.1 itojun icp->icmp6_code);
958 1.1 itojun for (i = 4; i < cc ; i += sizeof(long))
959 1.1 itojun Printf("%2d: %8.8x\n", i, (u_int32_t)ntohl(*lp++));
960 1.1 itojun }
961 1.1 itojun return(0);
962 1.1 itojun }
963 1.1 itojun
964 1.1 itojun /*
965 1.1 itojun * Increment pointer until find the UDP header.
966 1.1 itojun */
967 1.1 itojun struct udphdr *
968 1.1 itojun get_udphdr(ip6, lim)
969 1.1 itojun struct ip6_hdr *ip6;
970 1.1 itojun u_char *lim;
971 1.1 itojun {
972 1.1 itojun u_char *cp = (u_char *)ip6, nh;
973 1.1 itojun int hlen;
974 1.1 itojun
975 1.1 itojun if (cp + sizeof(*ip6) >= lim)
976 1.1 itojun return(NULL);
977 1.1 itojun
978 1.1 itojun nh = ip6->ip6_nxt;
979 1.1 itojun cp += sizeof(struct ip6_hdr);
980 1.1 itojun
981 1.1 itojun while(lim - cp >= 8) {
982 1.1 itojun switch(nh) {
983 1.1 itojun case IPPROTO_ESP:
984 1.1 itojun case IPPROTO_TCP:
985 1.1 itojun case IPPROTO_ICMPV6:
986 1.1 itojun return(NULL);
987 1.1 itojun case IPPROTO_UDP:
988 1.1 itojun return((struct udphdr *)cp);
989 1.1 itojun case IPPROTO_FRAGMENT:
990 1.1 itojun hlen = sizeof(struct ip6_frag);
991 1.1 itojun nh = ((struct ip6_frag *)cp)->ip6f_nxt;
992 1.1 itojun break;
993 1.1 itojun case IPPROTO_AH:
994 1.1 itojun hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
995 1.1 itojun nh = ((struct ip6_ext *)cp)->ip6e_nxt;
996 1.1 itojun break;
997 1.1 itojun default:
998 1.1 itojun hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
999 1.1 itojun nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1000 1.1 itojun break;
1001 1.1 itojun }
1002 1.1 itojun
1003 1.1 itojun cp += hlen;
1004 1.1 itojun }
1005 1.1 itojun
1006 1.1 itojun return(NULL);
1007 1.1 itojun }
1008 1.1 itojun
1009 1.1 itojun void
1010 1.1 itojun print(mhdr, cc)
1011 1.1 itojun struct msghdr *mhdr;
1012 1.1 itojun int cc;
1013 1.1 itojun {
1014 1.1 itojun struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1015 1.1 itojun
1016 1.1 itojun if (nflag) {
1017 1.1 itojun Printf(" %s", inet_ntop(AF_INET6, &from->sin6_addr,
1018 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1019 1.1 itojun }
1020 1.1 itojun else if (lflag) {
1021 1.1 itojun Printf(" %s (%s)", inetname(&from->sin6_addr),
1022 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
1023 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1024 1.1 itojun }
1025 1.1 itojun else {
1026 1.1 itojun Printf(" %s", inetname(&from->sin6_addr));
1027 1.1 itojun }
1028 1.1 itojun
1029 1.1 itojun if (verbose) {
1030 1.1 itojun #ifdef OLDRAWSOCKET
1031 1.1 itojun Printf(" %d bytes to %s", cc,
1032 1.1 itojun inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1033 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1034 1.1 itojun #else
1035 1.1 itojun Printf(" %d bytes of data to %s", cc,
1036 1.1 itojun inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1037 1.1 itojun ntop_buf, sizeof(ntop_buf)));
1038 1.1 itojun #endif
1039 1.1 itojun }
1040 1.1 itojun }
1041 1.1 itojun
1042 1.1 itojun /*
1043 1.1 itojun * Subtract 2 timeval structs: out = out - in.
1044 1.1 itojun * Out is assumed to be >= in.
1045 1.1 itojun */
1046 1.1 itojun void
1047 1.1 itojun tvsub(out, in)
1048 1.1 itojun register struct timeval *out, *in;
1049 1.1 itojun {
1050 1.1 itojun if ((out->tv_usec -= in->tv_usec) < 0) {
1051 1.1 itojun out->tv_sec--;
1052 1.1 itojun out->tv_usec += 1000000;
1053 1.1 itojun }
1054 1.1 itojun out->tv_sec -= in->tv_sec;
1055 1.1 itojun }
1056 1.1 itojun
1057 1.1 itojun
1058 1.1 itojun /*
1059 1.1 itojun * Construct an Internet address representation.
1060 1.1 itojun * If the nflag has been supplied, give
1061 1.1 itojun * numeric value, otherwise try for symbolic name.
1062 1.1 itojun */
1063 1.1 itojun char *
1064 1.1 itojun inetname(in)
1065 1.1 itojun struct in6_addr *in;
1066 1.1 itojun {
1067 1.1 itojun register char *cp;
1068 1.1 itojun static char line[50];
1069 1.1 itojun struct hostent *hp;
1070 1.1 itojun static char domain[MAXHOSTNAMELEN + 1];
1071 1.1 itojun static int first = 1;
1072 1.1 itojun
1073 1.1 itojun if (first && !nflag) {
1074 1.1 itojun first = 0;
1075 1.1 itojun if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1076 1.1 itojun (cp = index(domain, '.')))
1077 1.1 itojun (void) strcpy(domain, cp + 1);
1078 1.1 itojun else
1079 1.1 itojun domain[0] = 0;
1080 1.1 itojun }
1081 1.1 itojun cp = 0;
1082 1.1 itojun if (!nflag) {
1083 1.1 itojun /* hp = (struct hostent *)addr2hostname(in, sizeof(*in), AF_INET6, &herr); */
1084 1.1 itojun hp = (struct hostent *)gethostbyaddr((const char *)in, sizeof(*in), AF_INET6);
1085 1.1 itojun if (hp) {
1086 1.1 itojun if ((cp = index(hp->h_name, '.')) &&
1087 1.1 itojun !strcmp(cp + 1, domain))
1088 1.1 itojun *cp = 0;
1089 1.1 itojun cp = hp->h_name;
1090 1.1 itojun freehostent(hp);
1091 1.1 itojun }
1092 1.1 itojun }
1093 1.1 itojun if (cp)
1094 1.1 itojun (void) strcpy(line, cp);
1095 1.1 itojun else {
1096 1.1 itojun (void)inet_ntop(AF_INET6, in, line, sizeof(line));
1097 1.1 itojun }
1098 1.1 itojun return (line);
1099 1.1 itojun }
1100 1.1 itojun
1101 1.1 itojun void
1102 1.1 itojun usage()
1103 1.1 itojun {
1104 1.1 itojun (void)fprintf(stderr,
1105 1.1 itojun "usage: traceroute6 [-dlnrv] [-m max_hops] [-p port#] [-q nqueries]\n\t\
1106 1.1 itojun [-s src_addr] [-g gateway] [-w wait] host [data size]\n");
1107 1.1 itojun exit(1);
1108 1.1 itojun }
1109