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