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