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