]> git.saurik.com Git - apple/network_cmds.git/blob - traceroute6.tproj/traceroute6.c
network_cmds-325.tar.gz
[apple/network_cmds.git] / traceroute6.tproj / traceroute6.c
1 /* $KAME: traceroute6.c,v 1.68 2004/01/25 11:16:12 suz 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.22 2008/02/10 21:06:38 dwmalone 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 an 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 #include <sys/sysctl.h>
261
262 #include <netinet/in.h>
263
264 #include <arpa/inet.h>
265
266 #include <netdb.h>
267 #include <stdio.h>
268 #include <err.h>
269 #ifdef HAVE_POLL
270 #include <poll.h>
271 #endif
272 #include <errno.h>
273 #include <stdlib.h>
274 #include <string.h>
275 #include <unistd.h>
276
277 #include <netinet/ip6.h>
278 #include <netinet/icmp6.h>
279 #include <netinet/udp.h>
280
281 #ifdef IPSEC
282 #include <net/route.h>
283 #include <netinet6/ipsec.h>
284 #endif
285
286 #define DUMMY_PORT 10010
287
288 #define MAXPACKET 65535 /* max ip packet size */
289
290 #ifndef HAVE_GETIPNODEBYNAME
291 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
292 #define freehostent(x)
293 #endif
294
295 /*
296 * format of a (udp) probe packet.
297 */
298 struct tv32 {
299 u_int32_t tv32_sec;
300 u_int32_t tv32_usec;
301 };
302
303 struct opacket {
304 u_char seq; /* sequence number of this packet */
305 u_char hops; /* hop limit of the packet */
306 u_char pad[2];
307 struct tv32 tv; /* time packet left */
308 } __attribute__((__packed__));
309
310 u_char packet[512]; /* last inbound (icmp) packet */
311 struct opacket *outpacket; /* last output (udp) packet */
312
313 int main(int, char *[]);
314 int wait_for_reply(int, struct msghdr *);
315 #ifdef IPSEC
316 #ifdef IPSEC_POLICY_IPSEC
317 int setpolicy(int so, char *policy);
318 #endif
319 #endif
320 void send_probe(int, u_long);
321 void *get_uphdr(struct ip6_hdr *, u_char *);
322 int get_hoplim(struct msghdr *);
323 double deltaT(struct timeval *, struct timeval *);
324 char *pr_type(int);
325 int packet_ok(struct msghdr *, int, int);
326 void print(struct msghdr *, int);
327 const char *inetname(struct sockaddr *);
328 void usage(void);
329
330 int rcvsock; /* receive (icmp) socket file descriptor */
331 int sndsock; /* send (udp) socket file descriptor */
332
333 struct msghdr rcvmhdr;
334 struct iovec rcviov[2];
335 int rcvhlim;
336 struct in6_pktinfo *rcvpktinfo;
337
338 struct sockaddr_in6 Src, Dst, Rcv;
339 u_long datalen; /* How much data */
340 #define ICMP6ECHOLEN 8
341 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
342 char rtbuf[2064];
343 #ifdef USE_RFC2292BIS
344 struct ip6_rthdr *rth;
345 #endif
346 struct cmsghdr *cmsg;
347
348 char *source = 0;
349 char *hostname;
350
351 u_long nprobes = 3;
352 u_long first_hop = 1;
353 u_long max_hops = 30;
354 u_int16_t srcport;
355 u_int16_t port = 32768+666; /* start udp dest port # for probe packets */
356 u_int16_t ident;
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 useproto = IPPROTO_UDP; /* protocol to use to send packet */
362 int lflag; /* print both numerical address & hostname */
363
364 int
365 main(argc, argv)
366 int argc;
367 char *argv[];
368 {
369 int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM };
370 char hbuf[NI_MAXHOST], src0[NI_MAXHOST], *ep;
371 int ch, i, on = 1, seq, rcvcmsglen, error, minlen;
372 struct addrinfo hints, *res;
373 static u_char *rcvcmsgbuf;
374 u_long probe, hops, lport;
375 struct hostent *hp;
376 size_t size;
377
378 /*
379 * Receive ICMP
380 */
381 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
382 perror("socket(ICMPv6)");
383 exit(5);
384 }
385
386 size = sizeof(i);
387 (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &i, &size, NULL, 0);
388 max_hops = i;
389
390 /* specify to tell receiving interface */
391 #ifdef IPV6_RECVPKTINFO
392 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
393 sizeof(on)) < 0)
394 err(1, "setsockopt(IPV6_RECVPKTINFO)");
395 #else /* old adv. API */
396 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
397 sizeof(on)) < 0)
398 err(1, "setsockopt(IPV6_PKTINFO)");
399 #endif
400
401 /* specify to tell value of hoplimit field of received IP6 hdr */
402 #ifdef IPV6_RECVHOPLIMIT
403 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
404 sizeof(on)) < 0)
405 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
406 #else /* old adv. API */
407 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
408 sizeof(on)) < 0)
409 err(1, "setsockopt(IPV6_HOPLIMIT)");
410 #endif
411
412 seq = 0;
413
414 while ((ch = getopt(argc, argv, "df:g:Ilm:nNp:q:rs:Uvw:")) != -1)
415 switch (ch) {
416 case 'd':
417 options |= SO_DEBUG;
418 break;
419 case 'f':
420 ep = NULL;
421 errno = 0;
422 first_hop = strtoul(optarg, &ep, 0);
423 if (errno || !*optarg || *ep || first_hop > 255) {
424 fprintf(stderr,
425 "traceroute6: invalid min hoplimit.\n");
426 exit(1);
427 }
428 break;
429 case 'g':
430 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
431 if (hp == NULL) {
432 fprintf(stderr,
433 "traceroute6: unknown host %s\n", optarg);
434 exit(1);
435 }
436 #ifdef USE_RFC2292BIS
437 if (rth == NULL) {
438 /*
439 * XXX: We can't detect the number of
440 * intermediate nodes yet.
441 */
442 if ((rth = inet6_rth_init((void *)rtbuf,
443 sizeof(rtbuf), IPV6_RTHDR_TYPE_0,
444 0)) == NULL) {
445 fprintf(stderr,
446 "inet6_rth_init failed.\n");
447 exit(1);
448 }
449 }
450 if (inet6_rth_add((void *)rth,
451 (struct in6_addr *)hp->h_addr)) {
452 fprintf(stderr,
453 "inet6_rth_add failed for %s\n",
454 optarg);
455 exit(1);
456 }
457 #else /* old advanced API */
458 if (cmsg == NULL)
459 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
460 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr,
461 IPV6_RTHDR_LOOSE);
462 #endif
463 freehostent(hp);
464 break;
465 case 'I':
466 useproto = IPPROTO_ICMPV6;
467 ident = htons(getpid() & 0xffff); /* same as ping6 */
468 break;
469 case 'l':
470 lflag++;
471 break;
472 case 'm':
473 ep = NULL;
474 errno = 0;
475 max_hops = strtoul(optarg, &ep, 0);
476 if (errno || !*optarg || *ep || max_hops > 255) {
477 fprintf(stderr,
478 "traceroute6: invalid max hoplimit.\n");
479 exit(1);
480 }
481 break;
482 case 'n':
483 nflag++;
484 break;
485 case 'N':
486 useproto = IPPROTO_NONE;
487 break;
488 case 'p':
489 ep = NULL;
490 errno = 0;
491 lport = strtoul(optarg, &ep, 0);
492 if (errno || !*optarg || *ep) {
493 fprintf(stderr, "traceroute6: invalid port.\n");
494 exit(1);
495 }
496 if (lport == 0 || lport != (lport & 0xffff)) {
497 fprintf(stderr,
498 "traceroute6: port out of range.\n");
499 exit(1);
500 }
501 port = lport & 0xffff;
502 break;
503 case 'q':
504 ep = NULL;
505 errno = 0;
506 nprobes = strtoul(optarg, &ep, 0);
507 if (errno || !*optarg || *ep) {
508 fprintf(stderr,
509 "traceroute6: invalid nprobes.\n");
510 exit(1);
511 }
512 if (nprobes < 1) {
513 fprintf(stderr,
514 "traceroute6: nprobes must be >0.\n");
515 exit(1);
516 }
517 break;
518 case 'r':
519 options |= SO_DONTROUTE;
520 break;
521 case 's':
522 /*
523 * set the ip source address of the outbound
524 * probe (e.g., on a multi-homed host).
525 */
526 source = optarg;
527 break;
528 case 'v':
529 verbose++;
530 break;
531 case 'U':
532 useproto = IPPROTO_UDP;
533 break;
534 case 'w':
535 ep = NULL;
536 errno = 0;
537 waittime = strtoul(optarg, &ep, 0);
538 if (errno || !*optarg || *ep) {
539 fprintf(stderr,
540 "traceroute6: invalid wait time.\n");
541 exit(1);
542 }
543 if (waittime < 1) {
544 fprintf(stderr,
545 "traceroute6: wait must be >= 1 sec.\n");
546 exit(1);
547 }
548 break;
549 default:
550 usage();
551 }
552 argc -= optind;
553 argv += optind;
554
555 /*
556 * Open socket to send probe packets.
557 */
558 switch (useproto) {
559 case IPPROTO_ICMPV6:
560 sndsock = rcvsock;
561 break;
562 case IPPROTO_UDP:
563 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
564 perror("socket(SOCK_DGRAM)");
565 exit(5);
566 }
567 break;
568 case IPPROTO_NONE:
569 if ((sndsock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
570 perror("socket(SOCK_RAW)");
571 exit(5);
572 }
573 break;
574 default:
575 fprintf(stderr, "traceroute6: unknown probe protocol %d",
576 useproto);
577 exit(5);
578 }
579 if (max_hops < first_hop) {
580 fprintf(stderr,
581 "traceroute6: max hoplimit must be larger than first hoplimit.\n");
582 exit(1);
583 }
584
585 /* revoke privs */
586 seteuid(getuid());
587 setuid(getuid());
588
589 if (argc < 1 || argc > 2)
590 usage();
591
592 #if 1
593 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
594 #else
595 setlinebuf(stdout);
596 #endif
597
598 memset(&hints, 0, sizeof(hints));
599 hints.ai_family = PF_INET6;
600 hints.ai_socktype = SOCK_RAW;
601 hints.ai_protocol = IPPROTO_ICMPV6;
602 hints.ai_flags = AI_CANONNAME;
603 error = getaddrinfo(*argv, NULL, &hints, &res);
604 if (error) {
605 fprintf(stderr,
606 "traceroute6: %s\n", gai_strerror(error));
607 exit(1);
608 }
609 if (res->ai_addrlen != sizeof(Dst)) {
610 fprintf(stderr,
611 "traceroute6: size of sockaddr mismatch\n");
612 exit(1);
613 }
614 memcpy(&Dst, res->ai_addr, res->ai_addrlen);
615 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
616 if (!hostname) {
617 fprintf(stderr, "traceroute6: not enough core\n");
618 exit(1);
619 }
620 if (res->ai_next) {
621 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
622 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
623 strlcpy(hbuf, "?", sizeof(hbuf));
624 fprintf(stderr, "traceroute6: Warning: %s has multiple "
625 "addresses; using %s\n", hostname, hbuf);
626 }
627
628 if (*++argv) {
629 ep = NULL;
630 errno = 0;
631 datalen = strtoul(*argv, &ep, 0);
632 if (errno || !*argv || *ep) {
633 fprintf(stderr,
634 "traceroute6: invalid packet length.\n");
635 exit(1);
636 }
637 }
638 switch (useproto) {
639 case IPPROTO_ICMPV6:
640 minlen = ICMP6ECHOLEN + sizeof(struct tv32);
641 break;
642 case IPPROTO_UDP:
643 minlen = sizeof(struct opacket);
644 break;
645 case IPPROTO_NONE:
646 minlen = 0;
647 datalen = 0;
648 break;
649 default:
650 fprintf(stderr, "traceroute6: unknown probe protocol %d.\n",
651 useproto);
652 exit(1);
653 }
654 if (datalen < minlen)
655 datalen = minlen;
656 else if (datalen >= MAXPACKET) {
657 fprintf(stderr,
658 "traceroute6: packet size must be %d <= s < %ld.\n",
659 minlen, (long)MAXPACKET);
660 exit(1);
661 }
662 outpacket = (struct opacket *)malloc((unsigned)datalen);
663 if (!outpacket) {
664 perror("malloc");
665 exit(1);
666 }
667 (void) bzero((char *)outpacket, datalen);
668
669 /* initialize msghdr for receiving packets */
670 rcviov[0].iov_base = (caddr_t)packet;
671 rcviov[0].iov_len = sizeof(packet);
672 rcvmhdr.msg_name = (caddr_t)&Rcv;
673 rcvmhdr.msg_namelen = sizeof(Rcv);
674 rcvmhdr.msg_iov = rcviov;
675 rcvmhdr.msg_iovlen = 1;
676 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
677 CMSG_SPACE(sizeof(int));
678 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
679 fprintf(stderr, "traceroute6: malloc failed\n");
680 exit(1);
681 }
682 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
683 rcvmhdr.msg_controllen = rcvcmsglen;
684
685 if (options & SO_DEBUG)
686 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
687 (char *)&on, sizeof(on));
688 if (options & SO_DONTROUTE)
689 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
690 (char *)&on, sizeof(on));
691 #ifdef IPSEC
692 #ifdef IPSEC_POLICY_IPSEC
693 /*
694 * do not raise error even if setsockopt fails, kernel may have ipsec
695 * turned off.
696 */
697 if (setpolicy(rcvsock, "in bypass") < 0)
698 errx(1, "%s", ipsec_strerror());
699 if (setpolicy(rcvsock, "out bypass") < 0)
700 errx(1, "%s", ipsec_strerror());
701 #else
702 {
703 int level = IPSEC_LEVEL_NONE;
704
705 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
706 sizeof(level));
707 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
708 sizeof(level));
709 #ifdef IP_AUTH_TRANS_LEVEL
710 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
711 sizeof(level));
712 #else
713 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
714 sizeof(level));
715 #endif
716 #ifdef IP_AUTH_NETWORK_LEVEL
717 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
718 sizeof(level));
719 #endif
720 }
721 #endif /*IPSEC_POLICY_IPSEC*/
722 #endif /*IPSEC*/
723
724 #ifdef SO_SNDBUF
725 i = datalen;
726 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&i,
727 sizeof(i)) < 0 && useproto != IPPROTO_NONE) {
728 perror("setsockopt(SO_SNDBUF)");
729 exit(6);
730 }
731 #endif /* SO_SNDBUF */
732 if (options & SO_DEBUG)
733 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
734 (char *)&on, sizeof(on));
735 if (options & SO_DONTROUTE)
736 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
737 (char *)&on, sizeof(on));
738 #ifdef USE_RFC2292BIS
739 if (rth) {/* XXX: there is no library to finalize the header... */
740 rth->ip6r_len = rth->ip6r_segleft * 2;
741 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR,
742 (void *)rth, (rth->ip6r_len + 1) << 3)) {
743 fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n",
744 strerror(errno));
745 exit(1);
746 }
747 }
748 #else /* old advanced API */
749 if (cmsg != NULL) {
750 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
751 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
752 rtbuf, cmsg->cmsg_len) < 0) {
753 fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n",
754 strerror(errno));
755 exit(1);
756 }
757 }
758 #endif /* USE_RFC2292BIS */
759 #ifdef IPSEC
760 #ifdef IPSEC_POLICY_IPSEC
761 /*
762 * do not raise error even if setsockopt fails, kernel may have ipsec
763 * turned off.
764 */
765 if (setpolicy(sndsock, "in bypass") < 0)
766 errx(1, "%s", ipsec_strerror());
767 if (setpolicy(sndsock, "out bypass") < 0)
768 errx(1, "%s", ipsec_strerror());
769 #else
770 {
771 int level = IPSEC_LEVEL_BYPASS;
772
773 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
774 sizeof(level));
775 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
776 sizeof(level));
777 #ifdef IP_AUTH_TRANS_LEVEL
778 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
779 sizeof(level));
780 #else
781 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
782 sizeof(level));
783 #endif
784 #ifdef IP_AUTH_NETWORK_LEVEL
785 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
786 sizeof(level));
787 #endif
788 }
789 #endif /*IPSEC_POLICY_IPSEC*/
790 #endif /*IPSEC*/
791
792 /*
793 * Source selection
794 */
795 bzero(&Src, sizeof(Src));
796 if (source) {
797 struct addrinfo hints, *res;
798 int error;
799
800 memset(&hints, 0, sizeof(hints));
801 hints.ai_family = AF_INET6;
802 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
803 hints.ai_flags = AI_NUMERICHOST;
804 error = getaddrinfo(source, "0", &hints, &res);
805 if (error) {
806 printf("traceroute6: %s: %s\n", source,
807 gai_strerror(error));
808 exit(1);
809 }
810 if (res->ai_addrlen > sizeof(Src)) {
811 printf("traceroute6: %s: %s\n", source,
812 gai_strerror(error));
813 exit(1);
814 }
815 memcpy(&Src, res->ai_addr, res->ai_addrlen);
816 freeaddrinfo(res);
817 } else {
818 struct sockaddr_in6 Nxt;
819 int dummy;
820 socklen_t len;
821
822 Nxt = Dst;
823 Nxt.sin6_port = htons(DUMMY_PORT);
824 if (cmsg != NULL)
825 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
826 sizeof(Nxt.sin6_addr));
827 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
828 perror("socket");
829 exit(1);
830 }
831 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
832 perror("connect");
833 exit(1);
834 }
835 len = sizeof(Src);
836 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
837 perror("getsockname");
838 exit(1);
839 }
840 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
841 src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) {
842 fprintf(stderr, "getnameinfo failed for source\n");
843 exit(1);
844 }
845 source = src0;
846 close(dummy);
847 }
848
849 Src.sin6_port = htons(0);
850 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
851 perror("bind");
852 exit(1);
853 }
854
855 {
856 socklen_t len;
857
858 len = sizeof(Src);
859 if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) {
860 perror("getsockname");
861 exit(1);
862 }
863 srcport = ntohs(Src.sin6_port);
864 }
865
866 /*
867 * Message to users
868 */
869 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf,
870 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
871 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
872 fprintf(stderr, "traceroute6");
873 fprintf(stderr, " to %s (%s)", hostname, hbuf);
874 if (source)
875 fprintf(stderr, " from %s", source);
876 fprintf(stderr, ", %lu hops max, %lu byte packets\n",
877 max_hops, datalen);
878 (void) fflush(stderr);
879
880 if (first_hop > 1)
881 printf("Skipping %lu intermediate hops\n", first_hop - 1);
882
883 /*
884 * Main loop
885 */
886 for (hops = first_hop; hops <= max_hops; ++hops) {
887 struct in6_addr lastaddr;
888 int got_there = 0;
889 int unreachable = 0;
890
891 printf("%2lu ", hops);
892 bzero(&lastaddr, sizeof(lastaddr));
893 for (probe = 0; probe < nprobes; ++probe) {
894 int cc;
895 struct timeval t1, t2;
896
897 (void) gettimeofday(&t1, NULL);
898 send_probe(++seq, hops);
899 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
900 (void) gettimeofday(&t2, NULL);
901 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
902 if (!IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
903 &lastaddr)) {
904 if (probe > 0)
905 fputs("\n ", stdout);
906 print(&rcvmhdr, cc);
907 lastaddr = Rcv.sin6_addr;
908 }
909 printf(" %.3f ms", deltaT(&t1, &t2));
910 switch (i - 1) {
911 case ICMP6_DST_UNREACH_NOROUTE:
912 ++unreachable;
913 printf(" !N");
914 break;
915 case ICMP6_DST_UNREACH_ADMIN:
916 ++unreachable;
917 printf(" !P");
918 break;
919 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
920 ++unreachable;
921 printf(" !S");
922 break;
923 case ICMP6_DST_UNREACH_ADDR:
924 ++unreachable;
925 printf(" !A");
926 break;
927 case ICMP6_DST_UNREACH_NOPORT:
928 if (rcvhlim >= 0 &&
929 rcvhlim <= 1)
930 printf(" !");
931 ++got_there;
932 break;
933 }
934 break;
935 }
936 }
937 if (cc == 0)
938 printf(" *");
939 (void) fflush(stdout);
940 }
941 putchar('\n');
942 if (got_there ||
943 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
944 exit(0);
945 }
946 }
947
948 exit(0);
949 }
950
951 int
952 wait_for_reply(sock, mhdr)
953 int sock;
954 struct msghdr *mhdr;
955 {
956 #ifdef HAVE_POLL
957 struct pollfd pfd[1];
958 int cc = 0;
959
960 pfd[0].fd = sock;
961 pfd[0].events = POLLIN;
962 pfd[0].revents = 0;
963
964 if (poll(pfd, 1, waittime * 1000) > 0)
965 cc = recvmsg(rcvsock, mhdr, 0);
966
967 return(cc);
968 #else
969 fd_set *fdsp;
970 struct timeval wait;
971 int cc = 0, fdsn;
972
973 fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
974 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
975 err(1, "malloc");
976 memset(fdsp, 0, fdsn);
977 FD_SET(sock, fdsp);
978 wait.tv_sec = waittime; wait.tv_usec = 0;
979
980 if (select(sock+1, fdsp, (fd_set *)0, (fd_set *)0, &wait) > 0)
981 cc = recvmsg(rcvsock, mhdr, 0);
982
983 free(fdsp);
984 return(cc);
985 #endif
986 }
987
988 #ifdef IPSEC
989 #ifdef IPSEC_POLICY_IPSEC
990 int
991 setpolicy(so, policy)
992 int so;
993 char *policy;
994 {
995 char *buf;
996
997 buf = ipsec_set_policy(policy, strlen(policy));
998 if (buf == NULL) {
999 warnx("%s", ipsec_strerror());
1000 return -1;
1001 }
1002 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
1003 buf, ipsec_get_policylen(buf));
1004
1005 free(buf);
1006
1007 return 0;
1008 }
1009 #endif
1010 #endif
1011
1012 void
1013 send_probe(seq, hops)
1014 int seq;
1015 u_long hops;
1016 {
1017 struct icmp6_hdr *icp;
1018 struct opacket *op;
1019 struct timeval tv;
1020 struct tv32 tv32;
1021 int i;
1022
1023 i = hops;
1024 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1025 (char *)&i, sizeof(i)) < 0) {
1026 perror("setsockopt IPV6_UNICAST_HOPS");
1027 }
1028
1029 Dst.sin6_port = htons(port + seq);
1030 (void) gettimeofday(&tv, NULL);
1031 tv32.tv32_sec = htonl(tv.tv_sec);
1032 tv32.tv32_usec = htonl(tv.tv_usec);
1033
1034 switch (useproto) {
1035 case IPPROTO_ICMPV6:
1036 icp = (struct icmp6_hdr *)outpacket;
1037
1038 icp->icmp6_type = ICMP6_ECHO_REQUEST;
1039 icp->icmp6_code = 0;
1040 icp->icmp6_cksum = 0;
1041 icp->icmp6_id = ident;
1042 icp->icmp6_seq = htons(seq);
1043 bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN),
1044 sizeof(tv32));
1045 break;
1046 case IPPROTO_UDP:
1047 op = outpacket;
1048
1049 op->seq = seq;
1050 op->hops = hops;
1051 bcopy(&tv32, &op->tv, sizeof tv32);
1052 break;
1053 case IPPROTO_NONE:
1054 /* No space for anything. No harm as seq/tv32 are decorative. */
1055 break;
1056 default:
1057 fprintf(stderr, "Unknown probe protocol %d.\n", useproto);
1058 exit(1);
1059 }
1060
1061 i = sendto(sndsock, (char *)outpacket, datalen, 0,
1062 (struct sockaddr *)&Dst, Dst.sin6_len);
1063 if (i < 0 || i != datalen) {
1064 if (i < 0)
1065 perror("sendto");
1066 printf("traceroute6: wrote %s %lu chars, ret=%d\n",
1067 hostname, datalen, i);
1068 (void) fflush(stdout);
1069 }
1070 }
1071
1072 int
1073 get_hoplim(mhdr)
1074 struct msghdr *mhdr;
1075 {
1076 struct cmsghdr *cm;
1077
1078 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1079 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1080 if (cm->cmsg_level == IPPROTO_IPV6 &&
1081 cm->cmsg_type == IPV6_HOPLIMIT &&
1082 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1083 return(*(int *)CMSG_DATA(cm));
1084 }
1085
1086 return(-1);
1087 }
1088
1089 double
1090 deltaT(t1p, t2p)
1091 struct timeval *t1p, *t2p;
1092 {
1093 double dt;
1094
1095 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
1096 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
1097 return (dt);
1098 }
1099
1100 /*
1101 * Convert an ICMP "type" field to a printable string.
1102 */
1103 char *
1104 pr_type(t0)
1105 int t0;
1106 {
1107 u_char t = t0 & 0xff;
1108 char *cp;
1109
1110 switch (t) {
1111 case ICMP6_DST_UNREACH:
1112 cp = "Destination Unreachable";
1113 break;
1114 case ICMP6_PACKET_TOO_BIG:
1115 cp = "Packet Too Big";
1116 break;
1117 case ICMP6_TIME_EXCEEDED:
1118 cp = "Time Exceeded";
1119 break;
1120 case ICMP6_PARAM_PROB:
1121 cp = "Parameter Problem";
1122 break;
1123 case ICMP6_ECHO_REQUEST:
1124 cp = "Echo Request";
1125 break;
1126 case ICMP6_ECHO_REPLY:
1127 cp = "Echo Reply";
1128 break;
1129 case ICMP6_MEMBERSHIP_QUERY:
1130 cp = "Group Membership Query";
1131 break;
1132 case ICMP6_MEMBERSHIP_REPORT:
1133 cp = "Group Membership Report";
1134 break;
1135 case ICMP6_MEMBERSHIP_REDUCTION:
1136 cp = "Group Membership Reduction";
1137 break;
1138 case ND_ROUTER_SOLICIT:
1139 cp = "Router Solicitation";
1140 break;
1141 case ND_ROUTER_ADVERT:
1142 cp = "Router Advertisement";
1143 break;
1144 case ND_NEIGHBOR_SOLICIT:
1145 cp = "Neighbor Solicitation";
1146 break;
1147 case ND_NEIGHBOR_ADVERT:
1148 cp = "Neighbor Advertisement";
1149 break;
1150 case ND_REDIRECT:
1151 cp = "Redirect";
1152 break;
1153 default:
1154 cp = "Unknown";
1155 break;
1156 }
1157 return cp;
1158 }
1159
1160 int
1161 packet_ok(mhdr, cc, seq)
1162 struct msghdr *mhdr;
1163 int cc;
1164 int seq;
1165 {
1166 struct icmp6_hdr *icp;
1167 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1168 u_char type, code;
1169 char *buf = (char *)mhdr->msg_iov[0].iov_base;
1170 struct cmsghdr *cm;
1171 int *hlimp;
1172 char hbuf[NI_MAXHOST];
1173
1174 #ifdef OLDRAWSOCKET
1175 int hlen;
1176 struct ip6_hdr *ip;
1177 #endif
1178
1179 #ifdef OLDRAWSOCKET
1180 ip = (struct ip6_hdr *) buf;
1181 hlen = sizeof(struct ip6_hdr);
1182 if (cc < hlen + sizeof(struct icmp6_hdr)) {
1183 if (verbose) {
1184 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1185 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1186 strlcpy(hbuf, "invalid", sizeof(hbuf));
1187 printf("packet too short (%d bytes) from %s\n", cc,
1188 hbuf);
1189 }
1190 return (0);
1191 }
1192 cc -= hlen;
1193 icp = (struct icmp6_hdr *)(buf + hlen);
1194 #else
1195 if (cc < sizeof(struct icmp6_hdr)) {
1196 if (verbose) {
1197 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1198 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1199 strlcpy(hbuf, "invalid", sizeof(hbuf));
1200 printf("data too short (%d bytes) from %s\n", cc, hbuf);
1201 }
1202 return(0);
1203 }
1204 icp = (struct icmp6_hdr *)buf;
1205 #endif
1206 /* get optional information via advanced API */
1207 rcvpktinfo = NULL;
1208 hlimp = NULL;
1209 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1210 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1211 if (cm->cmsg_level == IPPROTO_IPV6 &&
1212 cm->cmsg_type == IPV6_PKTINFO &&
1213 cm->cmsg_len ==
1214 CMSG_LEN(sizeof(struct in6_pktinfo)))
1215 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
1216
1217 if (cm->cmsg_level == IPPROTO_IPV6 &&
1218 cm->cmsg_type == IPV6_HOPLIMIT &&
1219 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1220 hlimp = (int *)CMSG_DATA(cm);
1221 }
1222 if (rcvpktinfo == NULL || hlimp == NULL) {
1223 warnx("failed to get received hop limit or packet info");
1224 #if 0
1225 return(0);
1226 #else
1227 rcvhlim = 0; /*XXX*/
1228 #endif
1229 }
1230 else
1231 rcvhlim = *hlimp;
1232
1233 type = icp->icmp6_type;
1234 code = icp->icmp6_code;
1235 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
1236 || type == ICMP6_DST_UNREACH) {
1237 struct ip6_hdr *hip;
1238 void *up;
1239
1240 hip = (struct ip6_hdr *)(icp + 1);
1241 if ((up = get_uphdr(hip, (u_char *)(buf + cc))) == NULL) {
1242 if (verbose)
1243 warnx("failed to get upper layer header");
1244 return(0);
1245 }
1246 switch (useproto) {
1247 case IPPROTO_ICMPV6:
1248 if (((struct icmp6_hdr *)up)->icmp6_id == ident &&
1249 ((struct icmp6_hdr *)up)->icmp6_seq == htons(seq))
1250 return (type == ICMP6_TIME_EXCEEDED ?
1251 -1 : code + 1);
1252 break;
1253 case IPPROTO_UDP:
1254 if (((struct udphdr *)up)->uh_sport == htons(srcport) &&
1255 ((struct udphdr *)up)->uh_dport == htons(port + seq))
1256 return (type == ICMP6_TIME_EXCEEDED ?
1257 -1 : code + 1);
1258 break;
1259 case IPPROTO_NONE:
1260 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
1261 default:
1262 fprintf(stderr, "Unknown probe proto %d.\n", useproto);
1263 break;
1264 }
1265 } else if (useproto == IPPROTO_ICMPV6 && type == ICMP6_ECHO_REPLY) {
1266 if (icp->icmp6_id == ident &&
1267 icp->icmp6_seq == htons(seq))
1268 return (ICMP6_DST_UNREACH_NOPORT + 1);
1269 }
1270 if (verbose) {
1271 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
1272 u_int8_t *p;
1273 int i;
1274
1275 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1276 sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0)
1277 strlcpy(sbuf, "invalid", sizeof(sbuf));
1278 printf("\n%d bytes from %s to %s", cc, sbuf,
1279 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1280 dbuf, sizeof(dbuf)) : "?");
1281 printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
1282 icp->icmp6_code);
1283 p = (u_int8_t *)(icp + 1);
1284 #define WIDTH 16
1285 for (i = 0; i < cc; i++) {
1286 if (i % WIDTH == 0)
1287 printf("%04x:", i);
1288 if (i % 4 == 0)
1289 printf(" ");
1290 printf("%02x", p[i]);
1291 if (i % WIDTH == WIDTH - 1)
1292 printf("\n");
1293 }
1294 if (cc % WIDTH != 0)
1295 printf("\n");
1296 }
1297 return(0);
1298 }
1299
1300 /*
1301 * Increment pointer until find the UDP or ICMP header.
1302 */
1303 void *
1304 get_uphdr(ip6, lim)
1305 struct ip6_hdr *ip6;
1306 u_char *lim;
1307 {
1308 u_char *cp = (u_char *)ip6, nh;
1309 int hlen;
1310 static u_char none_hdr[1]; /* Fake pointer for IPPROTO_NONE. */
1311
1312 if (cp + sizeof(*ip6) > lim)
1313 return(NULL);
1314
1315 nh = ip6->ip6_nxt;
1316 cp += sizeof(struct ip6_hdr);
1317
1318 while (lim - cp >= (nh == IPPROTO_NONE ? 0 : 8)) {
1319 switch (nh) {
1320 case IPPROTO_ESP:
1321 case IPPROTO_TCP:
1322 return(NULL);
1323 case IPPROTO_ICMPV6:
1324 return(useproto == nh ? cp : NULL);
1325 case IPPROTO_UDP:
1326 return(useproto == nh ? cp : NULL);
1327 case IPPROTO_NONE:
1328 return(useproto == nh ? none_hdr : NULL);
1329 case IPPROTO_FRAGMENT:
1330 hlen = sizeof(struct ip6_frag);
1331 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1332 break;
1333 case IPPROTO_AH:
1334 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
1335 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1336 break;
1337 default:
1338 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
1339 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1340 break;
1341 }
1342
1343 cp += hlen;
1344 }
1345
1346 return(NULL);
1347 }
1348
1349 void
1350 print(mhdr, cc)
1351 struct msghdr *mhdr;
1352 int cc;
1353 {
1354 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1355 char hbuf[NI_MAXHOST];
1356
1357 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1358 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
1359 strlcpy(hbuf, "invalid", sizeof(hbuf));
1360 if (nflag)
1361 printf(" %s", hbuf);
1362 else if (lflag)
1363 printf(" %s (%s)", inetname((struct sockaddr *)from), hbuf);
1364 else
1365 printf(" %s", inetname((struct sockaddr *)from));
1366
1367 if (verbose) {
1368 #ifdef OLDRAWSOCKET
1369 printf(" %d bytes to %s", cc,
1370 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1371 hbuf, sizeof(hbuf)) : "?");
1372 #else
1373 printf(" %d bytes of data to %s", cc,
1374 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1375 hbuf, sizeof(hbuf)) : "?");
1376 #endif
1377 }
1378 }
1379
1380 /*
1381 * Construct an Internet address representation.
1382 * If the nflag has been supplied, give
1383 * numeric value, otherwise try for symbolic name.
1384 */
1385 const char *
1386 inetname(sa)
1387 struct sockaddr *sa;
1388 {
1389 static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1];
1390 static int first = 1;
1391 char *cp;
1392
1393 if (first && !nflag) {
1394 first = 0;
1395 if (gethostname(domain, sizeof(domain)) == 0 &&
1396 (cp = strchr(domain, '.')))
1397 (void) strlcpy(domain, cp + 1, sizeof(domain));
1398 else
1399 domain[0] = 0;
1400 }
1401 cp = NULL;
1402 if (!nflag) {
1403 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1404 NI_NAMEREQD) == 0) {
1405 if ((cp = strchr(line, '.')) &&
1406 !strcmp(cp + 1, domain))
1407 *cp = 0;
1408 cp = line;
1409 }
1410 }
1411 if (cp)
1412 return cp;
1413
1414 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1415 NI_NUMERICHOST) != 0)
1416 strlcpy(line, "invalid", sizeof(line));
1417 return line;
1418 }
1419
1420 void
1421 usage()
1422 {
1423
1424 fprintf(stderr,
1425 "usage: traceroute6 [-dIlnNrUv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
1426 " [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");
1427 exit(1);
1428 }