2 * Modified for NRL 4.4BSD IPv6 release.
5 * Search for "#ifdef NRL" to find the changes.
9 * Modified for Linux IPv6 by Pedro Roque <roque@di.fc.ul.pt>
12 * As ICMP error messages for IPv6 now include more than 8 bytes
13 * UDP datagrams are now sent via an UDP socket instead of magic
16 * Original copyright and comments left intact. They might not
17 * match the code anymore.
21 * Copyright (c) 1990, 1993
22 * The Regents of the University of California. All rights reserved.
24 * This code is derived from software contributed to Berkeley by
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 "@(#) Copyright (c) 1990, 1993\n\
59 The Regents of the University of California. All rights reserved.\n";
63 * traceroute host - trace the route ip packets follow going to "host".
65 * Attempt to trace the route an ip packet would follow to some
66 * internet host. We find out intermediate hops by launching probe
67 * packets with a small ttl (time to live) then listening for an
68 * icmp "time exceeded" reply from a gateway. We start our probes
69 * with a ttl of one and increase by one until we get an icmp "port
70 * unreachable" (which means we got to "host") or hit a max (which
71 * defaults to 30 hops & can be changed with the -m flag). Three
72 * probes (change with -q flag) are sent at each ttl setting and a
73 * line is printed showing the ttl, address of the gateway and
74 * round trip time of each probe. If the probe answers come from
75 * different gateways, the address of each responding system will
76 * be printed. If there is no response within a 5 sec. timeout
77 * interval (changed with the -w flag), a "*" is printed for that
80 * Probe packets are UDP format. We don't want the destination
81 * host to process them so the destination port is set to an
82 * unlikely value (if some clod on the destination is using that
83 * value, it can be changed with the -p flag).
85 * A sample use might be:
87 * [yak 71]% traceroute nis.nsf.net.
88 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
89 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
90 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
91 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
92 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
93 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
94 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
95 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
96 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
97 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
98 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
99 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
101 * Note that lines 2 & 3 are the same. This is due to a buggy
102 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
103 * packets with a zero ttl.
105 * A more interesting example is:
107 * [yak 72]% traceroute allspice.lcs.mit.edu.
108 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
109 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
110 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
111 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
112 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
113 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
114 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
115 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
116 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
117 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
118 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
119 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
121 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
126 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
128 * (I start to see why I'm having so much trouble with mail to
129 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
130 * either don't send ICMP "time exceeded" messages or send them
131 * with a ttl too small to reach us. 14 - 17 are running the
132 * MIT C Gateway code that doesn't send "time exceeded"s. God
133 * only knows what's going on with 12.
135 * The silent gateway 12 in the above may be the result of a bug in
136 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
137 * sends an unreachable message using whatever ttl remains in the
138 * original datagram. Since, for gateways, the remaining ttl is
139 * zero, the icmp "time exceeded" is guaranteed to not make it back
140 * to us. The behavior of this bug is slightly more interesting
141 * when it appears on the destination system:
143 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
144 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
145 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
146 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
147 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
148 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
155 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
157 * Notice that there are 12 "gateways" (13 is the final
158 * destination) and exactly the last half of them are "missing".
159 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
160 * is using the ttl from our arriving datagram as the ttl in its
161 * icmp reply. So, the reply will time out on the return path
162 * (with no notice sent to anyone since icmp's aren't sent for
163 * icmp's) until we probe with a ttl that's at least twice the path
164 * length. I.e., rip is really only 7 hops away. A reply that
165 * returns with a ttl of 1 is a clue this problem exists.
166 * Traceroute prints a "!" after the time if the ttl is <= 1.
167 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
168 * non-standard (HPUX) software, expect to see this problem
169 * frequently and/or take care picking the target host of your
172 * Other possible annotations after the time are !H, !N, !P (got a host,
173 * network or protocol unreachable, respectively), !S or !F (source
174 * route failed or fragmentation needed -- neither of these should
175 * ever occur and the associated gateway is busted if you see one). If
176 * almost all the probes result in some kind of unreachable, traceroute
177 * will give up and exit.
181 * This program must be run by root or be setuid. (I suggest that
182 * you *don't* make it setuid -- casual use could result in a lot
183 * of unnecessary traffic on our poor, congested nets.)
185 * This program requires a kernel mod that does not appear in any
186 * system available from Berkeley: A raw ip socket using proto
187 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
188 * opposed to data to be wrapped in a ip datagram). See the README
189 * file that came with the source to this program for a description
190 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
191 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
192 * MODIFIED TO RUN THIS PROGRAM.
194 * The udp port usage may appear bizarre (well, ok, it is bizarre).
195 * The problem is that an icmp message only contains 8 bytes of
196 * data from the original datagram. 8 bytes is the size of a udp
197 * header so, if we want to associate replies with the original
198 * datagram, the necessary information must be encoded into the
199 * udp header (the ip id could be used but there's no way to
200 * interlock with the kernel's assignment of ip id's and, anyway,
201 * it would have taken a lot more kernel hacking to allow this
202 * code to set the ip id). So, to allow two or more users to
203 * use traceroute simultaneously, we use this task's pid as the
204 * source port (the high bit is set to move the port number out
205 * of the "likely" range). To keep track of which probe is being
206 * replied to (so times and/or hop counts don't get confused by a
207 * reply that was delayed in transit), we increment the destination
208 * port number before each probe.
210 * Don't use this as a coding example. I was trying to find a
211 * routing problem and this code sort-of popped out after 48 hours
212 * without sleep. I was amazed it ever compiled, much less ran.
214 * I stole the idea for this program from Steve Deering. Since
215 * the first release, I've learned that had I attended the right
216 * IETF working group meetings, I also could have stolen it from Guy
217 * Almes or Matt Mathis. I don't know (or care) who came up with
218 * the idea first. I envy the originators' perspicacity and I'm
219 * glad they didn't keep the idea a secret.
221 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
222 * enhancements to the original distribution.
224 * I've hacked up a round-trip-route version of this that works by
225 * sending a loose-source-routed udp datagram through the destination
226 * back to yourself. Unfortunately, SO many gateways botch source
227 * routing, the thing is almost worthless. Maybe one day...
229 * -- Van Jacobson (van@helios.ee.lbl.gov)
230 * Tue Dec 20 03:50:13 PST 1988
233 #include <sys/param.h>
234 #include <sys/time.h>
235 #include <sys/socket.h>
236 #include <sys/file.h>
237 #include <sys/ioctl.h>
243 #include <netinet/in_systm.h>
244 #include <netinet/in.h>
245 #include <netinet/ip.h>
246 #include <netinet/ip_icmp.h>
247 #include <netinet/udp.h>
249 #include <netinet/ip6.h>
250 #include <netinet/icmp6.h>
251 #include <linux/types.h>
253 #include <arpa/inet.h>
262 #include "SNAPSHOT.h"
265 #define SOL_IPV6 IPPROTO_IPV6
268 #define MAXPACKET 65535
269 #ifndef MAXHOSTNAMELEN
270 #define MAXHOSTNAMELEN 64
274 #define NFDBITS (8*sizeof(fd_set))
275 #define FD_SETSIZE NFDBITS
276 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
277 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
278 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
279 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
282 #define Fprintf (void)fprintf
283 #define Printf (void)printf
285 u_char packet[512]; /* last inbound (icmp) packet */
287 int wait_for_reply(int, struct sockaddr_in6 *, struct in6_addr *, int);
288 int packet_ok(u_char *buf, int cc, struct sockaddr_in6 *from,
289 struct in6_addr *to, int seq, struct timeval *);
290 void send_probe(int seq, int ttl);
291 double deltaT (struct timeval *, struct timeval *);
292 void print(unsigned char *buf, int cc, struct sockaddr_in6 *from);
293 void tvsub (struct timeval *, struct timeval *);
296 int icmp_sock; /* receive (icmp) socket file descriptor */
297 int sndsock; /* send (udp) socket file descriptor */
298 struct timezone tz; /* leftover */
300 struct sockaddr_in6 whereto; /* Who to try to reach */
302 struct sockaddr_in6 saddr;
303 struct sockaddr_in6 firsthop;
311 u_short port = 32768+666; /* start udp dest port # for probe packets */
312 int options; /* socket options */
314 int waittime = 5; /* time to wait for response (in seconds) */
315 int nflag; /* print addresses numerically */
326 int datalen = sizeof(struct pkt_format);
330 int main(int argc, char *argv[])
332 char pa[MAXHOSTNAMELEN];
336 struct sockaddr_in6 from, *to;
337 int ch, i, on, probe, seq, tos, ttl;
340 icmp_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
341 socket_errno = errno;
343 if (setuid(getuid())) {
344 perror("traceroute6: setuid");
350 to = (struct sockaddr_in6 *)&whereto;
351 while ((ch = getopt(argc, argv, "dm:np:q:rs:t:w:vi:g:V")) != EOF) {
357 max_ttl = atoi(optarg);
360 "traceroute: max ttl must be >1.\n");
371 "traceroute: port must be >0.\n");
376 nprobes = atoi(optarg);
379 "traceroute: nprobes must be >0.\n");
384 options |= SO_DONTROUTE;
388 * set the ip source address of the outbound
389 * probe (e.g., on a multi-homed host).
397 Fprintf(stderr, "Sorry, rthdr is not yet supported\n");
403 waittime = atoi(optarg);
406 "traceroute: wait must be >1 sec.\n");
411 printf("traceroute6 utility, iputils-ss%s\n", SNAPSHOT);
425 (void) bzero((char *)&whereto, sizeof(whereto));
427 to->sin6_family = AF_INET6;
428 to->sin6_port = htons(port);
430 if (inet_pton(AF_INET6, *argv, &to->sin6_addr) > 0) {
433 hp = gethostbyname2(*argv, AF_INET6);
435 memmove((caddr_t)&to->sin6_addr, hp->h_addr, sizeof(to->sin6_addr));
436 hostname = (char *)hp->h_name;
438 (void)fprintf(stderr,
439 "traceroute: unknown host %s\n", *argv);
445 datalen = atoi(*argv);
446 /* Message for rpm maintainers: have _shame_. If you want
447 * to fix something send the patch to me for sanity checking.
448 * "datalen" patch is a shit. */
450 datalen = sizeof(struct pkt_format);
451 else if (datalen < (int)sizeof(struct pkt_format) ||
452 datalen >= MAXPACKET) {
454 "traceroute: packet size must be %d <= s < %d.\n",
455 (int)sizeof(struct pkt_format), MAXPACKET);
462 sendbuff = malloc(datalen);
463 if (sendbuff == NULL) {
464 fprintf(stderr, "malloc failed\n");
469 errno = socket_errno;
470 perror("traceroute6: icmp socket");
474 #ifdef IPV6_RECVPKTINFO
475 setsockopt(icmp_sock, SOL_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));
476 setsockopt(icmp_sock, SOL_IPV6, IPV6_2292PKTINFO, &on, sizeof(on));
478 setsockopt(icmp_sock, SOL_IPV6, IPV6_PKTINFO, &on, sizeof(on));
481 if (options & SO_DEBUG)
482 setsockopt(icmp_sock, SOL_SOCKET, SO_DEBUG,
483 (char *)&on, sizeof(on));
484 if (options & SO_DONTROUTE)
485 setsockopt(icmp_sock, SOL_SOCKET, SO_DONTROUTE,
486 (char *)&on, sizeof(on));
488 if (setsockopt(icmp_sock, SOL_RAW, IPV6_CHECKSUM, &on, sizeof(on)) < 0) {
489 perror("setsockopt(RAW_CHECKSUM)");
493 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
494 perror("traceroute: UDP socket");
498 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
499 sizeof(datalen)) < 0) {
500 perror("traceroute: SO_SNDBUF");
503 #endif /* SO_SNDBUF */
505 if (options & SO_DEBUG)
506 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
507 (char *)&on, sizeof(on));
508 if (options & SO_DONTROUTE)
509 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
510 (char *)&on, sizeof(on));
512 if (source == NULL) {
514 int probe_fd = socket(AF_INET6, SOCK_DGRAM, 0);
521 if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1)
522 perror("WARNING: interface is ignored");
524 firsthop.sin6_port = htons(1025);
525 if (connect(probe_fd, (struct sockaddr*)&firsthop, sizeof(firsthop)) == -1) {
529 alen = sizeof(saddr);
530 if (getsockname(probe_fd, (struct sockaddr*)&saddr, &alen) == -1) {
531 perror("getsockname");
537 (void) bzero((char *)&saddr, sizeof(saddr));
538 saddr.sin6_family = AF_INET6;
539 if (inet_pton(AF_INET6, source, &saddr.sin6_addr) <= 0)
541 Printf("traceroute: unknown addr %s\n", source);
546 if (bind(sndsock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
547 perror ("traceroute: bind sending socket");
550 if (bind(icmp_sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
551 perror ("traceroute: bind icmp6 socket");
555 Fprintf(stderr, "traceroute to %s (%s)", hostname,
556 inet_ntop(AF_INET6, &to->sin6_addr, pa, sizeof(pa)));
558 Fprintf(stderr, " from %s",
559 inet_ntop(AF_INET6, &saddr.sin6_addr, pa, sizeof(pa)));
560 Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl, datalen);
561 (void) fflush(stderr);
563 for (ttl = 1; ttl <= max_ttl; ++ttl) {
564 struct in6_addr lastaddr = {{{0,}}};
569 for (probe = 0; probe < nprobes; ++probe) {
571 struct timeval t1, t2;
575 gettimeofday(&t1, &tz);
576 send_probe(++seq, ttl);
579 while ((cc = wait_for_reply(icmp_sock, &from, &to, reset_timer)) != 0) {
580 gettimeofday(&t2, &tz);
581 if ((i = packet_ok(packet, cc, &from, &to, seq, &t1))) {
583 if (memcmp(&from.sin6_addr, &lastaddr, sizeof(from.sin6_addr))) {
584 print(packet, cc, &from);
589 Printf(" %g ms", deltaT(&t1, &t2));
591 case ICMP6_DST_UNREACH_NOPORT:
595 case ICMP6_DST_UNREACH_NOROUTE:
599 case ICMP6_DST_UNREACH_ADDR:
604 case ICMP6_DST_UNREACH_ADMIN:
615 (void) fflush(stdout);
619 (unreachable > 0 && unreachable >= nprobes-1))
627 wait_for_reply(sock, from, to, reset_timer)
629 struct sockaddr_in6 *from;
634 static struct timeval wait;
642 * traceroute could hang if someone else has a ping
643 * running and our ICMP reply gets dropped but we don't
644 * realize it because we keep waking up to handle those
645 * other ICMP packets that keep coming in. To fix this,
646 * "reset_timer" will only be true if the last packet that
647 * came in was for us or if this is the first time we're
648 * waiting for a reply since sending out a probe. Note
649 * that this takes advantage of the select() feature on
650 * Linux where the remaining timeout is written to the
651 * struct timeval area.
653 wait.tv_sec = waittime;
657 if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0) {
660 iov.iov_base = packet;
661 iov.iov_len = sizeof(packet);
662 msg.msg_name = (void *)from;
663 msg.msg_namelen = sizeof(*from);
667 msg.msg_control = cbuf;
668 msg.msg_controllen = sizeof(cbuf);
670 cc = recvmsg(icmp_sock, &msg, 0);
672 struct cmsghdr *cmsg;
673 struct in6_pktinfo *ipi;
675 for (cmsg = CMSG_FIRSTHDR(&msg);
677 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
678 if (cmsg->cmsg_level != SOL_IPV6)
680 switch (cmsg->cmsg_type) {
682 #ifdef IPV6_2292PKTINFO
683 case IPV6_2292PKTINFO:
685 ipi = (struct in6_pktinfo *)CMSG_DATA(cmsg);
686 memcpy(to, ipi, sizeof(*to));
696 void send_probe(int seq, int ttl)
698 struct pkt_format *pkt = (struct pkt_format *) sendbuff;
701 pkt->ident = htonl(ident);
702 pkt->seq = htonl(seq);
703 gettimeofday(&pkt->tv, &tz);
705 i = setsockopt(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
708 perror("setsockopt");
713 i = sendto(sndsock, sendbuff, datalen, 0,
714 (struct sockaddr *)&whereto, sizeof(whereto));
715 } while (i<0 && errno == ECONNREFUSED);
717 if (i < 0 || i != datalen) {
720 Printf("traceroute: wrote %s %d chars, ret=%d\n", hostname,
722 (void) fflush(stdout);
727 double deltaT(struct timeval *t1p, struct timeval *t2p)
731 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
732 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
738 * Convert an ICMP "type" field to a printable string.
740 char * pr_type(unsigned char t)
747 /* ICMP6_DST_UNREACH: */
748 return "Destination Unreachable";
750 /* ICMP6_PACKET_TOO_BIG: */
751 return "Packet Too Big";
753 /* ICMP6_TIME_EXCEEDED */
754 return "Time Exceeded in Transit";
756 /* ICMP6_PARAM_PROB */
757 return "Parameter Problem";
759 /* ICMP6_ECHO_REQUEST */
760 return "Echo Request";
762 /* ICMP6_ECHO_REPLY */
765 /* ICMP6_MEMBERSHIP_QUERY */
766 return "Membership Query";
768 /* ICMP6_MEMBERSHIP_REPORT */
769 return "Membership Report";
771 /* ICMP6_MEMBERSHIP_REDUCTION */
772 return "Membership Reduction";
774 /* ND_ROUTER_SOLICIT */
775 return "Router Solicitation";
777 /* ND_ROUTER_ADVERT */
778 return "Router Advertisement";
780 /* ND_NEIGHBOR_SOLICIT */
781 return "Neighbor Solicitation";
783 /* ND_NEIGHBOR_ADVERT */
784 return "Neighbor Advertisement";
790 return("OUT-OF-RANGE");
794 int packet_ok(u_char *buf, int cc, struct sockaddr_in6 *from,
795 struct in6_addr *to, int seq,
798 struct icmp6_hdr *icp;
801 icp = (struct icmp6_hdr *) buf;
803 type = icp->icmp6_type;
804 code = icp->icmp6_code;
806 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) ||
807 type == ICMP6_DST_UNREACH)
813 hip = (struct ip6_hdr *) (icp + 1);
814 up = (struct udphdr *)(hip+1);
815 nexthdr = hip->ip6_nxt;
818 nexthdr = *(unsigned char*)up;
821 if (nexthdr == IPPROTO_UDP)
823 struct pkt_format *pkt;
825 pkt = (struct pkt_format *) (up + 1);
827 if (ntohl(pkt->ident) == ident &&
828 ntohl(pkt->seq) == seq)
831 return (type == ICMP6_TIME_EXCEEDED ? -1 : code+1);
839 char pa1[MAXHOSTNAMELEN];
840 char pa2[MAXHOSTNAMELEN];
843 p = (unsigned char *) (icp + 1);
845 Printf("\n%d bytes from %s to %s", cc,
846 inet_ntop(AF_INET6, &from->sin6_addr, pa1, sizeof(pa1)),
847 inet_ntop(AF_INET6, to, pa2, sizeof(pa2)));
849 Printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
852 cc -= sizeof(struct icmp6_hdr);
853 for (i = 0; i < cc ; i++) {
858 Printf("%02x", 0xff & (unsigned)p[i]);
859 if (i % 16 == 15 && i + 1 < cc)
869 void print(unsigned char *buf, int cc, struct sockaddr_in6 *from)
871 char pa[MAXHOSTNAMELEN];
874 Printf(" %s", inet_ntop(AF_INET6, &from->sin6_addr,
878 const char *hostname;
881 hostname = inet_ntop(AF_INET6, &from->sin6_addr, pa, sizeof(pa));
883 if ((hp = gethostbyaddr((char *)&from->sin6_addr,
884 sizeof(from->sin6_addr), AF_INET6)))
885 hostname = hp->h_name;
887 Printf(" %s (%s)", hostname, pa);
893 * Subtract 2 timeval structs: out = out - in.
894 * Out is assumed to be >= in.
898 register struct timeval *out, *in;
900 if ((out->tv_usec -= in->tv_usec) < 0) {
902 out->tv_usec += 1000000;
904 out->tv_sec -= in->tv_sec;
910 "Usage: traceroute6 [-dnrvV] [-m max_ttl] [-p port#] [-q nqueries]\n\t\
911 [-s src_addr] [-t tos] [-w wait] host [data size]\n");