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