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