blob: 7284f0022d51facd417c4cd71db3ed07d2b603f8 [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +00002/*
3 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000
Eric Andersen5c58d282001-07-10 16:29:00 +00004 * The Regents of the University of California. All rights reserved.
5 *
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +00006 * Busybox port by Vladimir Oleynik (C) 2005 <dzo@simtreas.ru>
Eric Andersen5c58d282001-07-10 16:29:00 +00007 *
8 * Redistribution and use in source and binary forms, with or without
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +00009 * modification, are permitted provided that: (1) source code distributions
10 * retain the above copyright notice and this paragraph in its entirety, (2)
11 * distributions including binary code include the above copyright notice and
12 * this paragraph in its entirety in the documentation or other materials
13 * provided with the distribution, and (3) all advertising materials mentioning
14 * features or use of this software display the following acknowledgement:
15 * ``This product includes software developed by the University of California,
16 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17 * the University nor the names of its contributors may be used to endorse
18 * or promote products derived from this software without specific prior
19 * written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Eric Andersen5c58d282001-07-10 16:29:00 +000023 */
24
25/*
26 * traceroute host - trace the route ip packets follow going to "host".
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +000027 *
28 * Attempt to trace the route an ip packet would follow to some
29 * internet host. We find out intermediate hops by launching probe
30 * packets with a small ttl (time to live) then listening for an
31 * icmp "time exceeded" reply from a gateway. We start our probes
32 * with a ttl of one and increase by one until we get an icmp "port
33 * unreachable" (which means we got to "host") or hit a max (which
34 * defaults to 30 hops & can be changed with the -m flag). Three
35 * probes (change with -q flag) are sent at each ttl setting and a
36 * line is printed showing the ttl, address of the gateway and
37 * round trip time of each probe. If the probe answers come from
38 * different gateways, the address of each responding system will
39 * be printed. If there is no response within a 5 sec. timeout
40 * interval (changed with the -w flag), a "*" is printed for that
41 * probe.
42 *
43 * Probe packets are UDP format. We don't want the destination
44 * host to process them so the destination port is set to an
45 * unlikely value (if some clod on the destination is using that
46 * value, it can be changed with the -p flag).
47 *
48 * A sample use might be:
49 *
50 * [yak 71]% traceroute nis.nsf.net.
51 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
52 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
53 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
54 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
55 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
56 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
57 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
58 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
59 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
60 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
61 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
62 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
63 *
64 * Note that lines 2 & 3 are the same. This is due to a buggy
65 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
66 * packets with a zero ttl.
67 *
68 * A more interesting example is:
69 *
70 * [yak 72]% traceroute allspice.lcs.mit.edu.
71 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
72 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
73 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
74 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
75 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
76 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
77 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
78 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
79 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
80 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
81 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
82 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
83 * 12 * * *
84 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
85 * 14 * * *
86 * 15 * * *
87 * 16 * * *
88 * 17 * * *
89 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
90 *
91 * (I start to see why I'm having so much trouble with mail to
92 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
93 * either don't send ICMP "time exceeded" messages or send them
94 * with a ttl too small to reach us. 14 - 17 are running the
95 * MIT C Gateway code that doesn't send "time exceeded"s. God
96 * only knows what's going on with 12.
97 *
98 * The silent gateway 12 in the above may be the result of a bug in
99 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
100 * sends an unreachable message using whatever ttl remains in the
101 * original datagram. Since, for gateways, the remaining ttl is
102 * zero, the icmp "time exceeded" is guaranteed to not make it back
103 * to us. The behavior of this bug is slightly more interesting
104 * when it appears on the destination system:
105 *
106 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
107 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
108 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
109 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
110 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
111 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
112 * 7 * * *
113 * 8 * * *
114 * 9 * * *
115 * 10 * * *
116 * 11 * * *
117 * 12 * * *
118 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
119 *
120 * Notice that there are 12 "gateways" (13 is the final
121 * destination) and exactly the last half of them are "missing".
122 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
123 * is using the ttl from our arriving datagram as the ttl in its
124 * icmp reply. So, the reply will time out on the return path
125 * (with no notice sent to anyone since icmp's aren't sent for
126 * icmp's) until we probe with a ttl that's at least twice the path
127 * length. I.e., rip is really only 7 hops away. A reply that
128 * returns with a ttl of 1 is a clue this problem exists.
129 * Traceroute prints a "!" after the time if the ttl is <= 1.
130 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
131 * non-standard (HPUX) software, expect to see this problem
132 * frequently and/or take care picking the target host of your
133 * probes.
134 *
135 * Other possible annotations after the time are !H, !N, !P (got a host,
136 * network or protocol unreachable, respectively), !S or !F (source
137 * route failed or fragmentation needed -- neither of these should
138 * ever occur and the associated gateway is busted if you see one). If
139 * almost all the probes result in some kind of unreachable, traceroute
140 * will give up and exit.
141 *
Eric Andersen5c58d282001-07-10 16:29:00 +0000142 * Notes
143 * -----
144 * This program must be run by root or be setuid. (I suggest that
145 * you *don't* make it setuid -- casual use could result in a lot
146 * of unnecessary traffic on our poor, congested nets.)
147 *
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000148 * This program requires a kernel mod that does not appear in any
149 * system available from Berkeley: A raw ip socket using proto
150 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
151 * opposed to data to be wrapped in a ip datagram). See the README
152 * file that came with the source to this program for a description
153 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
154 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
155 * MODIFIED TO RUN THIS PROGRAM.
156 *
157 * The udp port usage may appear bizarre (well, ok, it is bizarre).
158 * The problem is that an icmp message only contains 8 bytes of
159 * data from the original datagram. 8 bytes is the size of a udp
160 * header so, if we want to associate replies with the original
161 * datagram, the necessary information must be encoded into the
162 * udp header (the ip id could be used but there's no way to
163 * interlock with the kernel's assignment of ip id's and, anyway,
164 * it would have taken a lot more kernel hacking to allow this
165 * code to set the ip id). So, to allow two or more users to
166 * use traceroute simultaneously, we use this task's pid as the
167 * source port (the high bit is set to move the port number out
168 * of the "likely" range). To keep track of which probe is being
169 * replied to (so times and/or hop counts don't get confused by a
170 * reply that was delayed in transit), we increment the destination
171 * port number before each probe.
172 *
173 * Don't use this as a coding example. I was trying to find a
174 * routing problem and this code sort-of popped out after 48 hours
175 * without sleep. I was amazed it ever compiled, much less ran.
176 *
Eric Andersen5c58d282001-07-10 16:29:00 +0000177 * I stole the idea for this program from Steve Deering. Since
178 * the first release, I've learned that had I attended the right
179 * IETF working group meetings, I also could have stolen it from Guy
180 * Almes or Matt Mathis. I don't know (or care) who came up with
181 * the idea first. I envy the originators' perspicacity and I'm
182 * glad they didn't keep the idea a secret.
183 *
184 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
185 * enhancements to the original distribution.
186 *
187 * I've hacked up a round-trip-route version of this that works by
188 * sending a loose-source-routed udp datagram through the destination
189 * back to yourself. Unfortunately, SO many gateways botch source
190 * routing, the thing is almost worthless. Maybe one day...
191 *
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000192 * -- Van Jacobson (van@ee.lbl.gov)
Eric Andersen5c58d282001-07-10 16:29:00 +0000193 * Tue Dec 20 03:50:13 PST 1988
194 */
195
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000196#define TRACEROUTE_SO_DEBUG 0
197
198/* TODO: undefs were uncommented - ??! we have config system for that! */
199/* probably ok to remove altogether */
200//#undef CONFIG_FEATURE_TRACEROUTE_VERBOSE
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000201//#define CONFIG_FEATURE_TRACEROUTE_VERBOSE
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000202//#undef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000203//#define CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000204//#undef CONFIG_FEATURE_TRACEROUTE_USE_ICMP
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000205//#define CONFIG_FEATURE_TRACEROUTE_USE_ICMP
Eric Andersen7467c8d2001-07-12 20:26:32 +0000206
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000207
208#include <net/if.h>
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000209#include <arpa/inet.h>
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000210#include <netinet/in.h>
Eric Andersen5c58d282001-07-10 16:29:00 +0000211#include <netinet/udp.h>
212#include <netinet/ip.h>
213#include <netinet/ip_icmp.h>
214
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000215#include "libbb.h"
Denis Vlasenko703e2022007-01-22 14:12:08 +0000216#include "inet_common.h"
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000217
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000218#ifndef IPPROTO_ICMP
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000219# define IPPROTO_ICMP 1
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000220#endif
221#ifndef IPPROTO_IP
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000222# define IPPROTO_IP 0
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000223#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000224
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000225/* Keep in sync with getopt32 call! */
226enum {
227 OPT_DONT_FRAGMNT = (1 << 0), /* F */
228 OPT_USE_ICMP = (1 << 1) * ENABLE_FEATURE_TRACEROUTE_USE_ICMP, /* I */
229 OPT_TTL_FLAG = (1 << 2), /* l */
230 OPT_ADDR_NUM = (1 << 3), /* n */
231 OPT_BYPASS_ROUTE = (1 << 4), /* r */
232 OPT_DEBUG = (1 << 5), /* d */
233 OPT_VERBOSE = (1 << 6) * ENABLE_FEATURE_TRACEROUTE_VERBOSE, /* v */
234 OPT_IP_CHKSUM = (1 << 7), /* x */
235 OPT_TOS = (1 << 8), /* t */
236 OPT_DEVICE = (1 << 9), /* i */
237 OPT_MAX_TTL = (1 << 10), /* m */
238 OPT_PORT = (1 << 11), /* p */
239 OPT_NPROBES = (1 << 12), /* q */
240 OPT_SOURCE = (1 << 13), /* s */
241 OPT_WAITTIME = (1 << 14), /* w */
242 OPT_PAUSE_MS = (1 << 15), /* z */
243 OPT_FIRST_TTL = (1 << 16), /* f */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000244};
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000245#define verbose (option_mask32 & OPT_VERBOSE)
Eric Andersen5c58d282001-07-10 16:29:00 +0000246
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000247enum {
248 SIZEOF_ICMP_HDR = 8,
249 rcvsock = 3, /* receive (icmp) socket file descriptor */
250 sndsock = 4, /* send (udp/icmp) socket file descriptor */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000251};
252
253/* Data section of the probe packet */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000254struct outdata_t {
Mike Frysinger294254c2006-02-19 22:59:12 +0000255 unsigned char seq; /* sequence number of this packet */
256 unsigned char ttl; /* ttl packet left with */
Denis Vlasenko459be352007-06-17 19:09:05 +0000257// UNUSED. Retaining to have the same packet size.
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000258 struct timeval tv_UNUSED PACKED; /* time packet left */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000259};
260
Denis Vlasenko2e723232007-03-29 21:55:22 +0000261struct globals {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000262 struct ip *outip;
263 struct outdata_t *outdata;
264 len_and_sockaddr *dest_lsa;
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000265 int packlen; /* total length of packet */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000266 int pmtu; /* Path MTU Discovery (RFC1191) */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000267 uint16_t ident;
268 uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000269 int waittime; // 5; /* time to wait for response (in seconds) */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000270#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
271 int optlen; /* length of ip options */
272#else
273#define optlen 0
274#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000275 unsigned char recv_pkt[512]; /* last inbound (icmp) packet */
Denis Vlasenko2e723232007-03-29 21:55:22 +0000276#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
277 /* Maximum number of gateways (include room for one noop) */
278#define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t)))
279 /* loose source route gateway list (including room for final destination) */
280 uint32_t gwlist[NGATEWAYS + 1];
281#endif
282};
283
284#define G (*ptr_to_globals)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000285#define outip (G.outip )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000286#define outdata (G.outdata )
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000287#define dest_lsa (G.dest_lsa )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000288#define packlen (G.packlen )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000289#define pmtu (G.pmtu )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000290#define ident (G.ident )
291#define port (G.port )
292#define waittime (G.waittime )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000293#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000294# define optlen (G.optlen )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000295#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000296#define recv_pkt (G.recv_pkt )
Denis Vlasenko2e723232007-03-29 21:55:22 +0000297#define gwlist (G.gwlist )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000298#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000299 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000300 port = 32768 + 666; \
301 waittime = 5; \
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000302} while (0)
Denis Vlasenko2e723232007-03-29 21:55:22 +0000303
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000304#define outicmp ((struct icmp *)(outip + 1))
305#define outudp ((struct udphdr *)(outip + 1))
Eric Andersen5c58d282001-07-10 16:29:00 +0000306
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000307
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000308static int
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000309wait_for_reply(struct sockaddr_in *fromp)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000310{
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000311 struct pollfd pfd[1];
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000312 int cc = 0;
313 socklen_t fromlen = sizeof(*fromp);
314
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000315 pfd[0].fd = rcvsock;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000316 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000317 if (safe_poll(pfd, 1, waittime * 1000) > 0)
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000318 cc = recvfrom(rcvsock, recv_pkt, sizeof(recv_pkt), 0,
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000319 (struct sockaddr *)fromp, &fromlen);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000320 return cc;
321}
322
323/*
324 * Checksum routine for Internet Protocol family headers (C Version)
325 */
Denis Vlasenko28703012006-12-19 20:32:02 +0000326static uint16_t
327in_cksum(uint16_t *addr, int len)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000328{
329 int nleft = len;
Denis Vlasenko28703012006-12-19 20:32:02 +0000330 uint16_t *w = addr;
331 uint16_t answer;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000332 int sum = 0;
333
334 /*
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000335 * Our algorithm is simple, using a 32 bit accumulator (sum),
336 * we add sequential 16 bit words to it, and at the end, fold
337 * back all the carry bits from the top 16 bits into the lower
338 * 16 bits.
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000339 */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000340 while (nleft > 1) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000341 sum += *w++;
342 nleft -= 2;
343 }
344
345 /* mop up an odd byte, if necessary */
346 if (nleft == 1)
Mike Frysinger294254c2006-02-19 22:59:12 +0000347 sum += *(unsigned char *)w;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000348
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000349 /* add back carry outs from top 16 bits to low 16 bits */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000350 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
351 sum += (sum >> 16); /* add carry */
352 answer = ~sum; /* truncate to 16 bits */
353 return answer;
354}
355
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000356static void
Denis Vlasenko459be352007-06-17 19:09:05 +0000357send_probe(int seq, int ttl)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000358{
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000359 int len, res;
360 void *out;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000361
362 /* Payload */
363 outdata->seq = seq;
364 outdata->ttl = ttl;
Denis Vlasenko459be352007-06-17 19:09:05 +0000365// UNUSED: was storing gettimeofday's result there, but never ever checked it
366 /*memcpy(&outdata->tv, tp, sizeof(outdata->tv));*/
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000367
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000368 if (option_mask32 & OPT_USE_ICMP) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000369 outicmp->icmp_seq = htons(seq);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000370
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000371 /* Always calculate checksum for icmp packets */
372 outicmp->icmp_cksum = 0;
Denis Vlasenko28703012006-12-19 20:32:02 +0000373 outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp,
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000374 packlen - (sizeof(*outip) + optlen));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000375 if (outicmp->icmp_cksum == 0)
376 outicmp->icmp_cksum = 0xffff;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000377 }
378
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000379//BUG! verbose is (x & OPT_VERBOSE), not a counter!
380#if 0 //ENABLE_FEATURE_TRACEROUTE_VERBOSE
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000381 /* XXX undocumented debugging hack */
382 if (verbose > 1) {
Denis Vlasenko28703012006-12-19 20:32:02 +0000383 const uint16_t *sp;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000384 int nshorts, i;
385
Denis Vlasenko28703012006-12-19 20:32:02 +0000386 sp = (uint16_t *)outip;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000387 nshorts = (unsigned)packlen / sizeof(uint16_t);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000388 i = 0;
389 printf("[ %d bytes", packlen);
390 while (--nshorts >= 0) {
391 if ((i++ % 8) == 0)
392 printf("\n\t");
393 printf(" %04x", ntohs(*sp));
394 sp++;
395 }
396 if (packlen & 1) {
397 if ((i % 8) == 0)
398 printf("\n\t");
Mike Frysinger294254c2006-02-19 22:59:12 +0000399 printf(" %02x", *(unsigned char *)sp);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000400 }
401 printf("]\n");
402 }
403#endif
404
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000405#if defined(IP_TTL)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000406 if (setsockopt(sndsock, IPPROTO_IP, IP_TTL,
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000407 (char *)&ttl, sizeof(ttl)) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000408 bb_perror_msg_and_die("setsockopt ttl %d", ttl);
409 }
410#endif
411
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000412 len = packlen - sizeof(*outip);
413 if (option_mask32 & OPT_USE_ICMP)
414 out = outicmp;
415 else {
416 out = outdata;
417 len -= sizeof(*outudp);
418 set_nport(dest_lsa, htons(port + seq));
419 }
420 res = xsendto(sndsock, out, len,
421 (struct sockaddr *)&dest_lsa->u.sa, dest_lsa->len);
422 if (res != len) {
423 bb_info_msg("sent %d octets, ret=%d", len, res);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000424 }
Eric Andersen7467c8d2001-07-12 20:26:32 +0000425}
426
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000427#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000428/*
429 * Convert an ICMP "type" field to a printable string.
430 */
431static inline const char *
Mike Frysinger294254c2006-02-19 22:59:12 +0000432pr_type(unsigned char t)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000433{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000434 static const char *const ttab[] = {
Eric Andersen7467c8d2001-07-12 20:26:32 +0000435 "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable",
436 "Source Quench", "Redirect", "ICMP 6", "ICMP 7",
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000437 "Echo", "Router Advert", "Router Solicit", "Time Exceeded",
Eric Andersen7467c8d2001-07-12 20:26:32 +0000438 "Param Problem", "Timestamp", "Timestamp Reply", "Info Request",
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000439 "Info Reply", "Mask Request", "Mask Reply"
Eric Andersen7467c8d2001-07-12 20:26:32 +0000440 };
441
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000442 if (t >= ARRAY_SIZE(ttab))
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000443 return "OUT-OF-RANGE";
Eric Andersen7467c8d2001-07-12 20:26:32 +0000444
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000445 return ttab[t];
Eric Andersen7467c8d2001-07-12 20:26:32 +0000446}
447#endif
448
Denis Vlasenko85c24712008-03-17 09:04:04 +0000449#if !ENABLE_FEATURE_TRACEROUTE_VERBOSE
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000450#define packet_ok(cc, from, seq) \
451 packet_ok(cc, seq)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000452#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000453static int
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000454packet_ok(int cc, const struct sockaddr_in *from, int seq)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000455{
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000456 const struct icmp *icp;
Mike Frysinger294254c2006-02-19 22:59:12 +0000457 unsigned char type, code;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000458 int hlen;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000459 const struct ip *ip;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000460
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000461 ip = (struct ip *) recv_pkt;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000462 hlen = ip->ip_hl << 2;
463 if (cc < hlen + ICMP_MINLEN) {
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000464#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000465 if (verbose)
466 printf("packet too short (%d bytes) from %s\n", cc,
467 inet_ntoa(from->sin_addr));
468#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000469 return 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000470 }
471 cc -= hlen;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000472 icp = (struct icmp *)(recv_pkt + hlen);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000473 type = icp->icmp_type;
474 code = icp->icmp_code;
475 /* Path MTU Discovery (RFC1191) */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000476 pmtu = 0;
477 if (code == ICMP_UNREACH_NEEDFRAG)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000478 pmtu = ntohs(icp->icmp_nextmtu);
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000479
480 if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS)
481 || type == ICMP_UNREACH
482 || type == ICMP_ECHOREPLY
483 ) {
484 const struct ip *hip;
485 const struct udphdr *up;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000486
487 hip = &icp->icmp_ip;
488 hlen = hip->ip_hl << 2;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000489 if (option_mask32 & OPT_USE_ICMP) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000490 struct icmp *hicmp;
491
492 /* XXX */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000493 if (type == ICMP_ECHOREPLY
494 && icp->icmp_id == htons(ident)
495 && icp->icmp_seq == htons(seq)
496 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000497 return -2;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000498 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000499
Mike Frysinger294254c2006-02-19 22:59:12 +0000500 hicmp = (struct icmp *)((unsigned char *)hip + hlen);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000501 if (hlen + SIZEOF_ICMP_HDR <= cc
502 && hip->ip_p == IPPROTO_ICMP
503 && hicmp->icmp_id == htons(ident)
504 && hicmp->icmp_seq == htons(seq)
505 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000506 return (type == ICMP_TIMXCEED ? -1 : code + 1);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000507 }
508 } else {
509 up = (struct udphdr *)((char *)hip + hlen);
510 if (hlen + 12 <= cc
511 && hip->ip_p == IPPROTO_UDP
512// Off: since we do not form the entire IP packet,
513// but defer it to kernel, we can't set source port,
514// and thus can't check it here in the reply
515 /* && up->source == htons(ident) */
516 && up->dest == htons(port + seq)
517 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000518 return (type == ICMP_TIMXCEED ? -1 : code + 1);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000519 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000520 }
Eric Andersen7467c8d2001-07-12 20:26:32 +0000521 }
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000522#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000523 if (verbose) {
524 int i;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000525 uint32_t *lp = (uint32_t *)&icp->icmp_ip;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000526
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000527 printf("\n%d bytes from %s to "
528 "%s: icmp type %d (%s) code %d\n",
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000529 cc, inet_ntoa(from->sin_addr),
530 inet_ntoa(ip->ip_dst),
531 type, pr_type(type), icp->icmp_code);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000532 for (i = 4; i < cc; i += sizeof(*lp))
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000533 printf("%2d: x%8.8x\n", i, *lp++);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000534 }
535#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000536 return 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000537}
538
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000539/*
540 * Construct an Internet address representation.
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000541 * If the -n flag has been supplied, give
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000542 * numeric value, otherwise try for symbolic name.
543 */
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000544static void
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000545print_inetname(const struct sockaddr_in *from)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000546{
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000547 const char *ina;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000548
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000549 ina = inet_ntoa(from->sin_addr);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000550 if (option_mask32 & OPT_ADDR_NUM)
551 printf(" %s", ina);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000552 else {
553 char *n = NULL;
554 if (from->sin_addr.s_addr != INADDR_ANY)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000555 n = xmalloc_sockaddr2host_noport((struct sockaddr*)from);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000556 printf(" %s (%s)", (n ? n : ina), ina);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000557 free(n);
558 }
Eric Andersen7467c8d2001-07-12 20:26:32 +0000559}
560
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000561static void
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000562print(int cc, const struct sockaddr_in *from)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000563{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000564 print_inetname(from);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000565 if (verbose) {
566 const struct ip *ip;
567 int hlen;
568
569 ip = (struct ip *) recv_pkt;
570 hlen = ip->ip_hl << 2;
571 cc -= hlen;
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000572 printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000573 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000574}
575
576static void
Denis Vlasenko459be352007-06-17 19:09:05 +0000577print_delta_ms(unsigned t1p, unsigned t2p)
Denis Vlasenko1adf6812007-06-14 07:47:52 +0000578{
Denis Vlasenko459be352007-06-17 19:09:05 +0000579 unsigned tt = t2p - t1p;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000580 printf(" %u.%03u ms", tt / 1000, tt % 1000);
Denis Vlasenko1adf6812007-06-14 07:47:52 +0000581}
Eric Andersen7467c8d2001-07-12 20:26:32 +0000582
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000583/*
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000584Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl]
585[-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos]
586[-w waittime] [-z pausemsecs] host [packetlen]"
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000587*/
588
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000589int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000590int traceroute_main(int argc, char **argv)
Eric Andersen5c58d282001-07-10 16:29:00 +0000591{
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000592 int minpacket;
593 int ttl, i;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000594 int seq = 0;
595 int tos = 0;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000596 int max_ttl = 30;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000597 int nprobes = 3;
598 int first_ttl = 1;
599 unsigned pausemsecs = 0;
600 unsigned op;
601 char *source;
602 char *device;
603 char *tos_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000604 char *max_ttl_str;
605 char *port_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000606 char *nprobes_str;
607 char *waittime_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000608 char *pausemsecs_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000609 char *first_ttl_str;
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000610#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000611 llist_t *source_route_list = NULL;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000612 int lsrr = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000613#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000614
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000615 INIT_G();
Denis Vlasenko2e723232007-03-29 21:55:22 +0000616
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000617#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000618 opt_complementary = "x-x:g::";
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000619#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000620 opt_complementary = "x-x";
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000621#endif
622
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000623 op = getopt32(argv, "FIlnrdvxt:i:m:p:q:s:w:z:f:"
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000624#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000625 "g:"
626#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000627 , &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str
628 , &source, &waittime_str, &pausemsecs_str, &first_ttl_str
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000629#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000630 , &source_route_list
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000631#endif
632 );
633
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000634#if 0 /* IGNORED */
635 if (op & OPT_IP_CHKSUM)
Denis Vlasenko13858992006-10-08 12:49:22 +0000636 bb_error_msg("warning: ip checksums disabled");
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000637#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000638 if (op & OPT_TOS)
639 tos = xatou_range(tos_str, 0, 255);
640 if (op & OPT_MAX_TTL)
641 max_ttl = xatou_range(max_ttl_str, 1, 255);
642 if (op & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +0000643 port = xatou16(port_str);
Denis Vlasenko2e723232007-03-29 21:55:22 +0000644 if (op & OPT_NPROBES)
645 nprobes = xatou_range(nprobes_str, 1, INT_MAX);
646 if (op & OPT_SOURCE) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000647 /*
648 * set the ip source address of the outbound
649 * probe (e.g., on a multi-homed host).
650 */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000651 if (getuid() != 0)
652 bb_error_msg_and_die("you must be root to use -s");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000653 }
Denis Vlasenko2e723232007-03-29 21:55:22 +0000654 if (op & OPT_WAITTIME)
Denis Vlasenkobdea2c02009-01-24 21:23:41 +0000655 waittime = xatou_range(waittime_str, 1, 24 * 60 * 60);
Denis Vlasenko2e723232007-03-29 21:55:22 +0000656 if (op & OPT_PAUSE_MS)
657 pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000);
658 if (op & OPT_FIRST_TTL)
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000659 first_ttl = xatou_range(first_ttl_str, 1, max_ttl);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000660
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000661#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000662 if (source_route_list) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000663 while (source_route_list) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000664 len_and_sockaddr *lsa;
665
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000666 if (lsrr >= NGATEWAYS)
Denis Vlasenko92758142006-10-03 19:56:34 +0000667 bb_error_msg_and_die("no more than %d gateways", NGATEWAYS);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000668 lsa = xhost_and_af2sockaddr(llist_pop(&source_route_list), 0, AF_INET);
669 gwlist[lsrr] = lsa->u.sin.sin_addr.s_addr;
670 free(lsa);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000671 ++lsrr;
Eric Andersen5c58d282001-07-10 16:29:00 +0000672 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000673 optlen = (lsrr + 1) * sizeof(gwlist[0]);
674 }
675#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000676
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000677 minpacket = sizeof(*outip) + SIZEOF_ICMP_HDR + sizeof(*outdata) + optlen;
678 if (!(op & OPT_USE_ICMP))
679 minpacket += sizeof(*outudp) - SIZEOF_ICMP_HDR;
680 packlen = minpacket;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000681
682 /* Process destination and optional packet size */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000683 argv += optind;
684 argc -= optind;
685 switch (argc) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000686 case 2:
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000687 packlen = xatoul_range(argv[1], minpacket, 32 * 1024);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000688 /* Fall through */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000689 case 1:
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000690 dest_lsa = xhost2sockaddr(argv[0], port);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000691 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000692 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000693 bb_show_usage();
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000694 }
Eric Andersen5c58d282001-07-10 16:29:00 +0000695
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000696 /* Ensure the socket fds won't be 0, 1 or 2 */
697 bb_sanitize_stdio();
Eric Andersen5c58d282001-07-10 16:29:00 +0000698
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000699 xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), rcvsock);
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000700#if TRACEROUTE_SO_DEBUG
Denis Vlasenko2e723232007-03-29 21:55:22 +0000701 if (op & OPT_DEBUG)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000702 setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000703 &const_int_1, sizeof(const_int_1));
Eric Andersen7467c8d2001-07-12 20:26:32 +0000704#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000705 if (op & OPT_BYPASS_ROUTE)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000706 setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000707 &const_int_1, sizeof(const_int_1));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000708
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000709 if (op & OPT_USE_ICMP)
710 xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), sndsock);
711 else
712 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sndsock);
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000713#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000714#if defined(IP_OPTIONS)
715 if (lsrr > 0) {
Mike Frysinger294254c2006-02-19 22:59:12 +0000716 unsigned char optlist[MAX_IPOPTLEN];
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000717
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000718 /* final hop */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000719 gwlist[lsrr] = dest_lsa->u.sin.sin_addr.s_addr;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000720 ++lsrr;
721
722 /* force 4 byte alignment */
723 optlist[0] = IPOPT_NOP;
724 /* loose source route option */
725 optlist[1] = IPOPT_LSRR;
726 i = lsrr * sizeof(gwlist[0]);
727 optlist[2] = i + 3;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000728 /* pointer to LSRR addresses */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000729 optlist[3] = IPOPT_MINOFF;
730 memcpy(optlist + 4, gwlist, i);
731
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000732 if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS,
733 (char *)optlist, i + sizeof(gwlist[0])) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000734 bb_perror_msg_and_die("IP_OPTIONS");
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000735 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000736 }
737#endif /* IP_OPTIONS */
738#endif /* CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE */
Eric Andersen5c58d282001-07-10 16:29:00 +0000739#ifdef SO_SNDBUF
Denis Vlasenko703e2022007-01-22 14:12:08 +0000740 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000741 bb_perror_msg_and_die("SO_SNDBUF");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000742 }
Eric Andersenad79c0b2002-06-06 12:24:51 +0000743#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000744#ifdef IP_TOS
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000745 if ((op & OPT_TOS) && setsockopt(sndsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000746 bb_perror_msg_and_die("setsockopt tos %d", tos);
747 }
748#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000749#ifdef IP_DONTFRAG
750 if (op & OPT_DONT_FRAGMNT)
751 setsockopt(sndsock, IPPROTO_IP, IP_DONTFRAG,
752 &const_int_1, sizeof(const_int_1));
Eric Andersenad79c0b2002-06-06 12:24:51 +0000753#endif
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000754#if TRACEROUTE_SO_DEBUG
Denis Vlasenko2e723232007-03-29 21:55:22 +0000755 if (op & OPT_DEBUG)
Denis Vlasenko703e2022007-01-22 14:12:08 +0000756 setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
757 &const_int_1, sizeof(const_int_1));
Eric Andersen7467c8d2001-07-12 20:26:32 +0000758#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000759 if (op & OPT_BYPASS_ROUTE)
Denis Vlasenko703e2022007-01-22 14:12:08 +0000760 setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
761 &const_int_1, sizeof(const_int_1));
Eric Andersen5c58d282001-07-10 16:29:00 +0000762
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000763 outip = xzalloc(packlen);
764
765 if (op & OPT_USE_ICMP) {
766 ident = getpid() | 0x8000;
767 outicmp->icmp_type = ICMP_ECHO;
768 outicmp->icmp_id = htons(ident);
769 outdata = (struct outdata_t *)((char *)outicmp + SIZEOF_ICMP_HDR);
770 } else {
771 outdata = (struct outdata_t *)(outudp + 1);
772 }
773
774 if (op & OPT_DEVICE) /* hmm, do we need error check? */
775 setsockopt_bindtodevice(sndsock, device);
776
777 if (op & OPT_SOURCE) {
778 len_and_sockaddr *source_lsa = xdotted2sockaddr(source, 0);
779 /* Ping does this (why?) */
780 if (setsockopt(sndsock, IPPROTO_IP, IP_MULTICAST_IF,
781 &source_lsa->u.sa, source_lsa->len))
782 bb_error_msg_and_die("can't set multicast source interface");
783//TODO: we can query source port we bound to,
784// and check it in replies... if we care enough
785 xbind(sndsock, &source_lsa->u.sa, source_lsa->len);
786 free(source_lsa);
787 }
788
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000789 /* Revert to non-privileged user after opening sockets */
Rob Landleyafb94ec2006-07-16 08:06:34 +0000790 xsetgid(getgid());
791 xsetuid(getuid());
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000792
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000793 printf("traceroute to %s (%s)", argv[0],
794 xmalloc_sockaddr2dotted_noport(&dest_lsa->u.sa));
Denis Vlasenko2e723232007-03-29 21:55:22 +0000795 if (op & OPT_SOURCE)
796 printf(" from %s", source);
797 printf(", %d hops max, %d byte packets\n", max_ttl, packlen);
Eric Andersen5c58d282001-07-10 16:29:00 +0000798
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000799 for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000800//TODO: make it protocol agnostic (get rid of sockaddr_in)
801 struct sockaddr_in from;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000802 uint32_t lastaddr = 0;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000803 int probe;
804 int unreachable = 0; /* counter */
805 int gotlastaddr = 0; /* flags */
Eric Andersen5c58d282001-07-10 16:29:00 +0000806 int got_there = 0;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000807 int first = 1;
Eric Andersen5c58d282001-07-10 16:29:00 +0000808
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000809 printf("%2d", ttl);
Eric Andersen5c58d282001-07-10 16:29:00 +0000810 for (probe = 0; probe < nprobes; ++probe) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000811 int cc;
Denis Vlasenko459be352007-06-17 19:09:05 +0000812 unsigned t1;
813 unsigned t2;
Eric Andersen5c58d282001-07-10 16:29:00 +0000814 struct ip *ip;
815
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000816 if (!first && pausemsecs > 0)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000817 usleep(pausemsecs * 1000);
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000818 fflush(stdout);
819
Denis Vlasenko459be352007-06-17 19:09:05 +0000820 t1 = monotonic_us();
821 send_probe(++seq, ttl);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000822 first = 0;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000823
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000824 while ((cc = wait_for_reply(&from)) != 0) {
Denis Vlasenko459be352007-06-17 19:09:05 +0000825 t2 = monotonic_us();
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000826 i = packet_ok(cc, &from, seq);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000827 /* Skip short packet */
828 if (i == 0)
829 continue;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000830 if (!gotlastaddr
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000831 || from.sin_addr.s_addr != lastaddr
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000832 ) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000833 print(cc, &from);
834 lastaddr = from.sin_addr.s_addr;
835 gotlastaddr = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000836 }
Denis Vlasenko459be352007-06-17 19:09:05 +0000837 print_delta_ms(t1, t2);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000838 ip = (struct ip *)recv_pkt;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000839 if (op & OPT_TTL_FLAG)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000840 printf(" (%d)", ip->ip_ttl);
841 if (i == -2) {
842 if (ip->ip_ttl <= 1)
843 printf(" !");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000844 got_there = 1;
Eric Andersen5c58d282001-07-10 16:29:00 +0000845 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000846 }
847 /* time exceeded in transit */
848 if (i == -1)
849 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000850 i--;
851 switch (i) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000852 case ICMP_UNREACH_PORT:
853 if (ip->ip_ttl <= 1)
854 printf(" !");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000855 got_there = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000856 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000857 case ICMP_UNREACH_NET:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000858 printf(" !N");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000859 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000860 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000861 case ICMP_UNREACH_HOST:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000862 printf(" !H");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000863 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000864 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000865 case ICMP_UNREACH_PROTOCOL:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000866 printf(" !P");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000867 got_there = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000868 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000869 case ICMP_UNREACH_NEEDFRAG:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000870 printf(" !F-%d", pmtu);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000871 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000872 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000873 case ICMP_UNREACH_SRCFAIL:
874 printf(" !S");
875 ++unreachable;
876 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000877 case ICMP_UNREACH_FILTER_PROHIB:
878 case ICMP_UNREACH_NET_PROHIB: /* misuse */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000879 printf(" !A");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000880 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000881 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000882 case ICMP_UNREACH_HOST_PROHIB:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000883 printf(" !C");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000884 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000885 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000886 case ICMP_UNREACH_HOST_PRECEDENCE:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000887 printf(" !V");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000888 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000889 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000890 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
891 printf(" !C");
892 ++unreachable;
893 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000894 case ICMP_UNREACH_NET_UNKNOWN:
895 case ICMP_UNREACH_HOST_UNKNOWN:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000896 printf(" !U");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000897 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000898 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000899 case ICMP_UNREACH_ISOLATED:
900 printf(" !I");
901 ++unreachable;
902 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000903 case ICMP_UNREACH_TOSNET:
904 case ICMP_UNREACH_TOSHOST:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000905 printf(" !T");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000906 ++unreachable;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000907 break;
908 default:
909 printf(" !<%d>", i);
910 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000911 break;
912 }
913 break;
Eric Andersen5c58d282001-07-10 16:29:00 +0000914 }
915 if (cc == 0)
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000916 printf(" *");
Eric Andersen5c58d282001-07-10 16:29:00 +0000917 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000918 bb_putchar('\n');
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000919 if (got_there
920 || (unreachable > 0 && unreachable >= nprobes - 1)
921 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000922 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000923 }
Eric Andersen5c58d282001-07-10 16:29:00 +0000924 }
Eric Andersen5c58d282001-07-10 16:29:00 +0000925 return 0;
926}