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