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