traceroute6.c revision 1.6 1 /* $NetBSD: traceroute6.c,v 1.6 2000/01/31 14:26:44 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.6 2000/01/31 14:26:44 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 #ifdef IPSEC
320 #ifdef IPSEC_POLICY_IPSEC
321 int setpolicy __P((int so, char *policy));
322 #endif
323 #endif
324 void send_probe __P((int, int));
325 struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
326 int get_hoplim __P((struct msghdr *));
327 double deltaT __P((struct timeval *, struct timeval *));
328 char *pr_type __P((int));
329 int packet_ok __P((struct msghdr *, int, int));
330 void print __P((struct msghdr *, int));
331 void tvsub __P((struct timeval *, struct timeval *));
332 char *inetname __P((struct in6_addr *));
333 void usage __P((void));
334
335 int rcvsock; /* receive (icmp) socket file descriptor */
336 int sndsock; /* send (udp) socket file descriptor */
337 struct timezone tz; /* leftover */
338
339 struct msghdr rcvmhdr;
340 struct iovec rcviov[2];
341 int rcvhlim;
342 struct in6_pktinfo *rcvpktinfo;
343
344 struct sockaddr_in6 Src, Dst, Rcv;
345 struct sockaddr_in6 *src = &Src, *dst = &Dst, *rcv = &Rcv;
346 int datalen; /* How much data */
347 char rtbuf[1024]; /*XXX*/
348 struct cmsghdr *cmsg;
349
350 char *source = 0;
351 char *hostname;
352
353 int nprobes = 3;
354 int max_hops = 30;
355 u_short ident;
356 u_short port = 32768+666; /* start udp dest port # for probe packets */
357 int options; /* socket options */
358 int verbose;
359 int waittime = 5; /* time to wait for response (in seconds) */
360 int nflag; /* print addresses numerically */
361 int lflag; /* print both numerical address & hostname */
362
363 char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
364
365 int
366 main(argc, argv)
367 int argc;
368 char *argv[];
369 {
370 extern char *optarg;
371 extern int optind;
372 struct hostent *hp;
373 int ch, i, on, probe, seq, hops;
374 static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))
375 + CMSG_SPACE(sizeof(int))];
376
377 on = 1;
378 seq = 0;
379
380 while ((ch = getopt(argc, argv, "dlm:np:q:rs:w:vg:")) != EOF)
381 switch(ch) {
382 case 'd':
383 options |= SO_DEBUG;
384 break;
385 case 'l':
386 lflag++;
387 break;
388 case 'g':
389 #if 0
390 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
391 #else
392 hp = gethostbyname2(optarg, AF_INET6);
393 #endif
394 if (hp == NULL) {
395 Fprintf(stderr,
396 "traceroute6: unknown host %s\n", optarg);
397 exit(1);
398 }
399 if (cmsg == NULL)
400 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
401 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, IPV6_RTHDR_LOOSE);
402 break;
403 case 'm':
404 max_hops = atoi(optarg);
405 if (max_hops <= 1) {
406 Fprintf(stderr,
407 "traceroute6: max hoplimit must be >1.\n");
408 exit(1);
409 }
410 break;
411 case 'n':
412 nflag++;
413 break;
414 case 'p':
415 port = atoi(optarg);
416 if (port < 1) {
417 Fprintf(stderr,
418 "traceroute6: port must be >0.\n");
419 exit(1);
420 }
421 break;
422 case 'q':
423 nprobes = atoi(optarg);
424 if (nprobes < 1) {
425 Fprintf(stderr,
426 "traceroute6: nprobes must be >0.\n");
427 exit(1);
428 }
429 break;
430 case 'r':
431 options |= SO_DONTROUTE;
432 break;
433 case 's':
434 /*
435 * set the ip source address of the outbound
436 * probe (e.g., on a multi-homed host).
437 */
438 source = optarg;
439 break;
440 case 'v':
441 verbose++;
442 break;
443 case 'w':
444 waittime = atoi(optarg);
445 if (waittime <= 1) {
446 Fprintf(stderr,
447 "traceroute6: wait must be >1 sec.\n");
448 exit(1);
449 }
450 break;
451 default:
452 usage();
453 }
454 argc -= optind;
455 argv += optind;
456
457 if (argc < 1)
458 usage();
459
460 #if 1
461 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
462 #else
463 setlinebuf (stdout);
464 #endif
465
466 (void) bzero((char *)dst, sizeof(Dst));
467 Dst.sin6_family = AF_INET6;
468
469 hp = (struct hostent *)gethostbyname2(*argv, AF_INET6);
470 if (hp == NULL) {
471 if (inet_pton(AF_INET6, *argv, &Dst.sin6_addr) != 1) {
472 (void)fprintf(stderr,
473 "traceroute6: unknown host %s\n", *argv);
474 exit(1);
475 }
476 hostname = *argv;
477 } else {
478 bcopy(hp->h_addr, (caddr_t)&Dst.sin6_addr, hp->h_length);
479 hostname = strdup(hp->h_name);
480 }
481 freehostent(hp);
482
483 if (*++argv)
484 datalen = atoi(*argv);
485 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) {
486 Fprintf(stderr,
487 "traceroute6: packet size must be 0 <= s < %ld.\n",
488 (long)(MAXPACKET - sizeof(struct opacket)));
489 exit(1);
490 }
491 datalen += sizeof(struct opacket);
492 outpacket = (struct opacket *)malloc((unsigned)datalen);
493 if (! outpacket) {
494 perror("traceroute6: malloc");
495 exit(1);
496 }
497 (void) bzero((char *)outpacket, datalen);
498
499 /*
500 * Receive ICMP
501 */
502 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
503 perror("traceroute6: icmp socket");
504 exit(5);
505 }
506 /* initialize msghdr for receiving packets */
507 rcviov[0].iov_base = (caddr_t)packet;
508 rcviov[0].iov_len = sizeof(packet);
509 rcvmhdr.msg_name = (caddr_t)rcv;
510 rcvmhdr.msg_namelen = sizeof(*rcv);
511 rcvmhdr.msg_iov = rcviov;
512 rcvmhdr.msg_iovlen = 1;
513 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
514 rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
515
516 /* specify to tell receiving interface */
517 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
518 sizeof(on)) < 0)
519 err(1, "setsockopt(IPV6_PKTINFO)");
520
521 /* specify to tell value of hoplimit field of received IP6 hdr */
522 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
523 sizeof(on)) < 0)
524 err(1, "setsockopt(IPV6_HOPLIMIT)");
525
526 if (options & SO_DEBUG)
527 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
528 (char *)&on, sizeof(on));
529 if (options & SO_DONTROUTE)
530 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
531 (char *)&on, sizeof(on));
532 #ifdef IPSEC
533 #ifdef IPSEC_POLICY_IPSEC
534 /*
535 * do not raise error even if setsockopt fails, kernel may have ipsec
536 * turned off.
537 */
538 if (setpolicy(rcvsock, "in bypass") < 0)
539 errx(1, ipsec_strerror());
540 if (setpolicy(rcvsock, "out bypass") < 0)
541 errx(1, ipsec_strerror());
542 #else
543 {
544 int level = IPSEC_LEVEL_NONE;
545
546 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
547 sizeof(level));
548 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
549 sizeof(level));
550 #ifdef IP_AUTH_TRANS_LEVEL
551 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
552 sizeof(level));
553 #else
554 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
555 sizeof(level));
556 #endif
557 #ifdef IP_AUTH_NETWORK_LEVEL
558 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
559 sizeof(level));
560 #endif
561 }
562 #endif /*IPSEC_POLICY_IPSEC*/
563 #endif /*IPSEC*/
564
565 /*
566 * Send UDP
567 */
568 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
569 perror("traceroute6: udp socket");
570 exit(5);
571 }
572 #ifdef SO_SNDBUF
573 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
574 sizeof(datalen)) < 0) {
575 perror("traceroute6: SO_SNDBUF");
576 exit(6);
577 }
578 #endif /* SO_SNDBUF */
579 if (options & SO_DEBUG)
580 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
581 (char *)&on, sizeof(on));
582 if (options & SO_DONTROUTE)
583 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
584 (char *)&on, sizeof(on));
585 if (cmsg != NULL) {
586 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
587 (void) setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
588 rtbuf, cmsg->cmsg_len);
589 }
590 #ifdef IPSEC
591 #ifdef IPSEC_POLICY_IPSEC
592 /*
593 * do not raise error even if setsockopt fails, kernel may have ipsec
594 * turned off.
595 */
596 if (setpolicy(sndsock, "in bypass") < 0)
597 errx(1, ipsec_strerror());
598 if (setpolicy(sndsock, "out bypass") < 0)
599 errx(1, ipsec_strerror());
600 #else
601 {
602 int level = IPSEC_LEVEL_BYPASS;
603
604 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
605 sizeof(level));
606 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
607 sizeof(level));
608 #ifdef IP_AUTH_TRANS_LEVEL
609 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
610 sizeof(level));
611 #else
612 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
613 sizeof(level));
614 #endif
615 #ifdef IP_AUTH_NETWORK_LEVEL
616 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
617 sizeof(level));
618 #endif
619 }
620 #endif /*IPSEC_POLICY_IPSEC*/
621 #endif /*IPSEC*/
622
623 /*
624 * Source selection
625 */
626 bzero((char *)src, sizeof(Src));
627 if (source) {
628 if (inet_pton(AF_INET6, source, &Src.sin6_addr) != 1) {
629 Printf("traceroute6: unknown host %s\n", source);
630 exit(1);
631 }
632 } else {
633 struct sockaddr_in6 Nxt;
634 int dummy, len;
635
636 len = sizeof(Src);
637 Nxt = Dst;
638 Nxt.sin6_port = htons(DUMMY_PORT);
639 if (cmsg != NULL)
640 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
641 sizeof(Nxt.sin6_addr));
642 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
643 perror("socket") ;
644 }
645 if(-1 == connect(dummy, (struct sockaddr *)&Nxt, sizeof(Nxt)))
646 perror("connect");
647 if(-1 == getsockname(dummy, (struct sockaddr *)src, &len)) {
648 perror("getsockname");
649 printf("%d\n", errno);
650 }
651 close(dummy) ;
652 }
653 ident = (getpid() & 0xffff) | 0x8000;
654 Src.sin6_family = AF_INET6;
655 Src.sin6_port = htons(ident);
656 if (bind(sndsock, (struct sockaddr *)src, sizeof(Src)) < 0){
657 perror ("traceroute6: bind:");
658 exit (1);
659 }
660
661 /*
662 * Message to users
663 */
664 Fprintf(stderr, "traceroute to %s (%s)", hostname,
665 inet_ntop(AF_INET6, &Dst.sin6_addr,
666 ntop_buf, sizeof(ntop_buf)));
667 if (source)
668 Fprintf(stderr, " from %s", source);
669 Fprintf(stderr, ", %d hops max, %d byte packets\n", max_hops, datalen);
670 (void) fflush(stderr);
671
672 /*
673 * Main loop
674 */
675 for (hops = 1; hops <= max_hops; ++hops) {
676 struct in6_addr lastaddr;
677 int got_there = 0;
678 int unreachable = 0;
679
680 Printf("%2d ", hops);
681 bzero(&lastaddr, sizeof(lastaddr));
682 for (probe = 0; probe < nprobes; ++probe) {
683 int cc;
684 struct timeval t1, t2;
685 struct timezone tz;
686
687 (void) gettimeofday(&t1, &tz);
688 send_probe(++seq, hops);
689 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
690 (void) gettimeofday(&t2, &tz);
691 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
692 if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
693 &lastaddr)) {
694 print(&rcvmhdr, cc);
695 lastaddr = Rcv.sin6_addr;
696 }
697 Printf(" %g ms", deltaT(&t1, &t2));
698 switch(i - 1) {
699 case ICMP6_DST_UNREACH_NOROUTE:
700 ++unreachable;
701 Printf(" !N");
702 break;
703 case ICMP6_DST_UNREACH_ADMIN:
704 ++unreachable;
705 Printf(" !P");
706 break;
707 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
708 ++unreachable;
709 Printf(" !S");
710 break;
711 case ICMP6_DST_UNREACH_ADDR:
712 ++unreachable;
713 Printf(" !A");
714 break;
715 case ICMP6_DST_UNREACH_NOPORT:
716 if (rcvhlim >= 0 &&
717 rcvhlim <= 1)
718 Printf(" !");
719 ++got_there;
720 break;
721 }
722 break;
723 }
724 }
725 if (cc == 0)
726 Printf(" *");
727 (void) fflush(stdout);
728 }
729 putchar('\n');
730 if (got_there ||
731 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
732 exit(0);
733 }
734 }
735
736 exit(0);
737 }
738
739 int
740 wait_for_reply(sock, mhdr)
741 int sock;
742 struct msghdr *mhdr;
743 {
744 fd_set fds;
745 struct timeval wait;
746 int cc = 0;
747
748 FD_ZERO(&fds);
749 FD_SET(sock, &fds);
750 wait.tv_sec = waittime; wait.tv_usec = 0;
751
752 if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0)
753 cc = recvmsg(rcvsock, mhdr, 0);
754
755 return(cc);
756 }
757
758 #ifdef IPSEC
759 #ifdef IPSEC_POLICY_IPSEC
760 int
761 setpolicy(so, policy)
762 int so;
763 char *policy;
764 {
765 char *buf;
766
767 buf = ipsec_set_policy(policy, strlen(policy));
768 if (buf == NULL) {
769 warnx(ipsec_strerror());
770 return -1;
771 }
772 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
773 buf, ipsec_get_policylen(buf));
774
775 free(buf);
776
777 return 0;
778 }
779 #endif
780 #endif
781
782
783 void
784 send_probe(seq, hops)
785 int seq, hops;
786 {
787 struct opacket *op = outpacket;
788 int i;
789
790 if(setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
791 (char *)&hops, sizeof(hops)) < 0) {
792 perror("setsockopt IPV6_UNICAST_HOPS");
793 }
794
795 Dst.sin6_port = htons(port + seq);
796
797 op->seq = seq;
798 op->hops = hops;
799 (void) gettimeofday(&op->tv, &tz);
800
801 i = sendto(sndsock, (char *)outpacket, datalen , 0,
802 (struct sockaddr *)dst, sizeof(Dst));
803 if (i < 0 || i != datalen) {
804 if (i<0)
805 perror("sendto");
806 Printf("traceroute6: wrote %s %d chars, ret=%d\n", hostname,
807 datalen, i);
808 (void) fflush(stdout);
809 }
810 }
811
812 int
813 get_hoplim(mhdr)
814 struct msghdr *mhdr;
815 {
816 struct cmsghdr *cm;
817
818 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
819 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
820 if (cm->cmsg_level == IPPROTO_IPV6 &&
821 cm->cmsg_type == IPV6_HOPLIMIT &&
822 cm->cmsg_len == CMSG_LEN(sizeof(int)))
823 return(*(int *)CMSG_DATA(cm));
824 }
825
826 return(-1);
827 }
828
829 double
830 deltaT(t1p, t2p)
831 struct timeval *t1p, *t2p;
832 {
833 register double dt;
834
835 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
836 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
837 return (dt);
838 }
839
840
841 /*
842 * Convert an ICMP "type" field to a printable string.
843 */
844 char *
845 pr_type(t0)
846 int t0;
847 {
848 u_char t = t0 & 0xff;
849 char *cp;
850
851 switch (t) {
852 case ICMP6_DST_UNREACH:
853 cp = "Destination Unreachable";
854 break;
855 case ICMP6_PACKET_TOO_BIG:
856 cp = "Pakcet Too Big";
857 break;
858 case ICMP6_TIME_EXCEEDED:
859 cp = "Time Exceeded";
860 break;
861 case ICMP6_PARAM_PROB:
862 cp = "Parameter Problem";
863 break;
864 case ICMP6_ECHO_REQUEST:
865 cp = "Echo Request";
866 break;
867 case ICMP6_ECHO_REPLY:
868 cp = "Echo Reply";
869 break;
870 case ICMP6_MEMBERSHIP_QUERY:
871 cp = "Group Membership Query";
872 break;
873 case ICMP6_MEMBERSHIP_REPORT:
874 cp = "Group Membership Report";
875 break;
876 case ICMP6_MEMBERSHIP_REDUCTION:
877 cp = "Group Membership Reduction";
878 break;
879 case ND_ROUTER_SOLICIT:
880 cp = "Router Solicitation";
881 break;
882 case ND_ROUTER_ADVERT:
883 cp = "Router Advertisement";
884 break;
885 case ND_NEIGHBOR_SOLICIT:
886 cp = "Neighbor Solicitation";
887 break;
888 case ND_NEIGHBOR_ADVERT:
889 cp = "Neighbor Advertisement";
890 break;
891 case ND_REDIRECT:
892 cp = "Ridirect";
893 break;
894 default:
895 cp = "Unknown";
896 break;
897 }
898 return cp;
899 }
900
901
902 int
903 packet_ok(mhdr, cc, seq)
904 struct msghdr *mhdr;
905 int cc;
906 int seq;
907 {
908 register struct icmp6_hdr *icp;
909 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
910 u_char type, code;
911 char *buf = (char *)mhdr->msg_iov[0].iov_base;
912 struct cmsghdr *cm;
913 int *hlimp;
914
915 #ifdef OLDRAWSOCKET
916 int hlen;
917 struct ip6_hdr *ip;
918 #endif
919
920 #ifdef OLDRAWSOCKET
921 ip = (struct ip6_hdr *) buf;
922 hlen = sizeof(struct ip6_hdr);
923 if (cc < hlen + sizeof(struct icmp6_hdr)) {
924 if (verbose)
925 Printf("packet too short (%d bytes) from %s\n", cc,
926 inet_ntop(AF_INET6, &from->sin6_addr,
927 ntop_buf, sizeof(ntop_buf)));
928 return (0);
929 }
930 cc -= hlen;
931 icp = (struct icmp6_hdr *)(buf + hlen);
932 #else
933 if (cc < sizeof(struct icmp6_hdr)) {
934 if (verbose)
935 Printf("data too short (%d bytes) from %s\n", cc,
936 inet_ntop(AF_INET6, &from->sin6_addr,
937 ntop_buf, sizeof(ntop_buf)));
938 return(0);
939 }
940 icp = (struct icmp6_hdr *)buf;
941 #endif
942 /* get optional information via advanced API */
943 rcvpktinfo = NULL;
944 hlimp = NULL;
945 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
946 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
947 if (cm->cmsg_level == IPPROTO_IPV6 &&
948 cm->cmsg_type == IPV6_PKTINFO &&
949 cm->cmsg_len ==
950 CMSG_LEN(sizeof(struct in6_pktinfo)))
951 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
952
953 if (cm->cmsg_level == IPPROTO_IPV6 &&
954 cm->cmsg_type == IPV6_HOPLIMIT &&
955 cm->cmsg_len == CMSG_LEN(sizeof(int)))
956 hlimp = (int *)CMSG_DATA(cm);
957 }
958 if (rcvpktinfo == NULL || hlimp == NULL) {
959 warnx("failed to get received hop limit or packet info");
960 return(0);
961 }
962 rcvhlim = *hlimp;
963
964 type = icp->icmp6_type;
965 code = icp->icmp6_code;
966 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
967 || type == ICMP6_DST_UNREACH) {
968 struct ip6_hdr *hip;
969 struct udphdr *up;
970
971 hip = (struct ip6_hdr *)(icp + 1);
972 if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) {
973 if (verbose)
974 warnx("failed to get upper layer header");
975 return(0);
976 }
977 if (up->uh_sport == htons(ident) &&
978 up->uh_dport == htons(port+seq))
979 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
980 }
981 if (verbose) {
982 int i;
983 u_long *lp = (u_long *)(icp + 1);
984 char sbuf[INET6_ADDRSTRLEN+1], dbuf[INET6_ADDRSTRLEN];
985
986 Printf("\n%d bytes from %s to %s", cc,
987 inet_ntop(AF_INET6, &from->sin6_addr,
988 sbuf, sizeof(sbuf)),
989 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
990 dbuf, sizeof(dbuf)));
991 Printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
992 icp->icmp6_code);
993 for (i = 4; i < cc ; i += sizeof(long))
994 Printf("%2d: %8.8x\n", i, (u_int32_t)ntohl(*lp++));
995 }
996 return(0);
997 }
998
999 /*
1000 * Increment pointer until find the UDP header.
1001 */
1002 struct udphdr *
1003 get_udphdr(ip6, lim)
1004 struct ip6_hdr *ip6;
1005 u_char *lim;
1006 {
1007 u_char *cp = (u_char *)ip6, nh;
1008 int hlen;
1009
1010 if (cp + sizeof(*ip6) >= lim)
1011 return(NULL);
1012
1013 nh = ip6->ip6_nxt;
1014 cp += sizeof(struct ip6_hdr);
1015
1016 while(lim - cp >= 8) {
1017 switch(nh) {
1018 case IPPROTO_ESP:
1019 case IPPROTO_TCP:
1020 case IPPROTO_ICMPV6:
1021 return(NULL);
1022 case IPPROTO_UDP:
1023 return((struct udphdr *)cp);
1024 case IPPROTO_FRAGMENT:
1025 hlen = sizeof(struct ip6_frag);
1026 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1027 break;
1028 case IPPROTO_AH:
1029 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
1030 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1031 break;
1032 default:
1033 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
1034 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1035 break;
1036 }
1037
1038 cp += hlen;
1039 }
1040
1041 return(NULL);
1042 }
1043
1044 void
1045 print(mhdr, cc)
1046 struct msghdr *mhdr;
1047 int cc;
1048 {
1049 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1050
1051 if (nflag) {
1052 Printf(" %s", inet_ntop(AF_INET6, &from->sin6_addr,
1053 ntop_buf, sizeof(ntop_buf)));
1054 }
1055 else if (lflag) {
1056 Printf(" %s (%s)", inetname(&from->sin6_addr),
1057 inet_ntop(AF_INET6, &from->sin6_addr,
1058 ntop_buf, sizeof(ntop_buf)));
1059 }
1060 else {
1061 Printf(" %s", inetname(&from->sin6_addr));
1062 }
1063
1064 if (verbose) {
1065 #ifdef OLDRAWSOCKET
1066 Printf(" %d bytes to %s", cc,
1067 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1068 ntop_buf, sizeof(ntop_buf)));
1069 #else
1070 Printf(" %d bytes of data to %s", cc,
1071 inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1072 ntop_buf, sizeof(ntop_buf)));
1073 #endif
1074 }
1075 }
1076
1077 /*
1078 * Subtract 2 timeval structs: out = out - in.
1079 * Out is assumed to be >= in.
1080 */
1081 void
1082 tvsub(out, in)
1083 register struct timeval *out, *in;
1084 {
1085 if ((out->tv_usec -= in->tv_usec) < 0) {
1086 out->tv_sec--;
1087 out->tv_usec += 1000000;
1088 }
1089 out->tv_sec -= in->tv_sec;
1090 }
1091
1092
1093 /*
1094 * Construct an Internet address representation.
1095 * If the nflag has been supplied, give
1096 * numeric value, otherwise try for symbolic name.
1097 */
1098 char *
1099 inetname(in)
1100 struct in6_addr *in;
1101 {
1102 register char *cp;
1103 static char line[50];
1104 struct hostent *hp;
1105 static char domain[MAXHOSTNAMELEN + 1];
1106 static int first = 1;
1107
1108 if (first && !nflag) {
1109 first = 0;
1110 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1111 (cp = index(domain, '.')))
1112 (void) strcpy(domain, cp + 1);
1113 else
1114 domain[0] = 0;
1115 }
1116 cp = 0;
1117 if (!nflag) {
1118 /* hp = (struct hostent *)addr2hostname(in, sizeof(*in), AF_INET6, &herr); */
1119 hp = (struct hostent *)gethostbyaddr((const char *)in, sizeof(*in), AF_INET6);
1120 if (hp) {
1121 if ((cp = index(hp->h_name, '.')) &&
1122 !strcmp(cp + 1, domain))
1123 *cp = 0;
1124 cp = hp->h_name;
1125 freehostent(hp);
1126 }
1127 }
1128 if (cp)
1129 (void) strcpy(line, cp);
1130 else {
1131 (void)inet_ntop(AF_INET6, in, line, sizeof(line));
1132 }
1133 return (line);
1134 }
1135
1136 void
1137 usage()
1138 {
1139 (void)fprintf(stderr,
1140 "usage: traceroute6 [-dlnrv] [-m max_hops] [-p port#] [-q nqueries]\n\t\
1141 [-s src_addr] [-g gateway] [-w wait] host [data size]\n");
1142 exit(1);
1143 }
1144