blob: d36ddee1492c903954f5dc84d2ecda86fd517bca [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>
Denys Vlasenko0d565682009-11-23 06:15:38 +0100214#if ENABLE_FEATURE_IPV6
215# include <netinet/ip6.h>
216# include <netinet/icmp6.h>
217# ifndef SOL_IPV6
218# define SOL_IPV6 IPPROTO_IPV6
219# endif
220#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000221
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000222#include "libbb.h"
Denis Vlasenko703e2022007-01-22 14:12:08 +0000223#include "inet_common.h"
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000224
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000225#ifndef IPPROTO_ICMP
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000226# define IPPROTO_ICMP 1
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000227#endif
228#ifndef IPPROTO_IP
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000229# define IPPROTO_IP 0
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000230#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000231
Denys Vlasenko0d565682009-11-23 06:15:38 +0100232
233#define OPT_STRING "FIlnrdvxt:i:m:p:q:s:w:z:f:" \
234 IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:")
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000235enum {
236 OPT_DONT_FRAGMNT = (1 << 0), /* F */
237 OPT_USE_ICMP = (1 << 1) * ENABLE_FEATURE_TRACEROUTE_USE_ICMP, /* I */
238 OPT_TTL_FLAG = (1 << 2), /* l */
239 OPT_ADDR_NUM = (1 << 3), /* n */
240 OPT_BYPASS_ROUTE = (1 << 4), /* r */
241 OPT_DEBUG = (1 << 5), /* d */
242 OPT_VERBOSE = (1 << 6) * ENABLE_FEATURE_TRACEROUTE_VERBOSE, /* v */
243 OPT_IP_CHKSUM = (1 << 7), /* x */
244 OPT_TOS = (1 << 8), /* t */
245 OPT_DEVICE = (1 << 9), /* i */
246 OPT_MAX_TTL = (1 << 10), /* m */
247 OPT_PORT = (1 << 11), /* p */
248 OPT_NPROBES = (1 << 12), /* q */
249 OPT_SOURCE = (1 << 13), /* s */
250 OPT_WAITTIME = (1 << 14), /* w */
251 OPT_PAUSE_MS = (1 << 15), /* z */
252 OPT_FIRST_TTL = (1 << 16), /* f */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000253};
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000254#define verbose (option_mask32 & OPT_VERBOSE)
Eric Andersen5c58d282001-07-10 16:29:00 +0000255
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000256enum {
257 SIZEOF_ICMP_HDR = 8,
258 rcvsock = 3, /* receive (icmp) socket file descriptor */
259 sndsock = 4, /* send (udp/icmp) socket file descriptor */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000260};
261
262/* Data section of the probe packet */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000263struct outdata_t {
Mike Frysinger294254c2006-02-19 22:59:12 +0000264 unsigned char seq; /* sequence number of this packet */
265 unsigned char ttl; /* ttl packet left with */
Denis Vlasenko459be352007-06-17 19:09:05 +0000266// UNUSED. Retaining to have the same packet size.
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000267 struct timeval tv_UNUSED PACKED; /* time packet left */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000268};
269
Denis Vlasenko2e723232007-03-29 21:55:22 +0000270struct globals {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000271 struct ip *outip;
272 struct outdata_t *outdata;
273 len_and_sockaddr *dest_lsa;
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000274 int packlen; /* total length of packet */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000275 int pmtu; /* Path MTU Discovery (RFC1191) */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000276 uint16_t ident;
277 uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000278 int waittime; // 5; /* time to wait for response (in seconds) */
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000279#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
280 int optlen; /* length of ip options */
281#else
282#define optlen 0
283#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000284 unsigned char recv_pkt[512]; /* last inbound (icmp) packet */
Denis Vlasenko2e723232007-03-29 21:55:22 +0000285#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
286 /* Maximum number of gateways (include room for one noop) */
287#define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t)))
288 /* loose source route gateway list (including room for final destination) */
289 uint32_t gwlist[NGATEWAYS + 1];
290#endif
291};
292
293#define G (*ptr_to_globals)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000294#define outip (G.outip )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000295#define outdata (G.outdata )
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000296#define dest_lsa (G.dest_lsa )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000297#define packlen (G.packlen )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000298#define pmtu (G.pmtu )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000299#define ident (G.ident )
300#define port (G.port )
301#define waittime (G.waittime )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000302#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000303# define optlen (G.optlen )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000304#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000305#define recv_pkt (G.recv_pkt )
Denis Vlasenko2e723232007-03-29 21:55:22 +0000306#define gwlist (G.gwlist )
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000307#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000308 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000309 port = 32768 + 666; \
310 waittime = 5; \
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000311} while (0)
Denis Vlasenko2e723232007-03-29 21:55:22 +0000312
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000313#define outicmp ((struct icmp *)(outip + 1))
314#define outudp ((struct udphdr *)(outip + 1))
Eric Andersen5c58d282001-07-10 16:29:00 +0000315
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000316
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000317static int
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000318wait_for_reply(struct sockaddr_in *fromp)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000319{
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000320 struct pollfd pfd[1];
Denys Vlasenko0d565682009-11-23 06:15:38 +0100321 int read_len = 0;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000322 socklen_t fromlen = sizeof(*fromp);
323
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000324 pfd[0].fd = rcvsock;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000325 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000326 if (safe_poll(pfd, 1, waittime * 1000) > 0)
Denys Vlasenko0d565682009-11-23 06:15:38 +0100327 read_len = recvfrom(rcvsock, recv_pkt, sizeof(recv_pkt), 0,
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000328 (struct sockaddr *)fromp, &fromlen);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100329 return read_len;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000330}
331
332/*
333 * Checksum routine for Internet Protocol family headers (C Version)
334 */
Denis Vlasenko28703012006-12-19 20:32:02 +0000335static uint16_t
336in_cksum(uint16_t *addr, int len)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000337{
338 int nleft = len;
Denis Vlasenko28703012006-12-19 20:32:02 +0000339 uint16_t *w = addr;
340 uint16_t answer;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000341 int sum = 0;
342
343 /*
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000344 * Our algorithm is simple, using a 32 bit accumulator (sum),
345 * we add sequential 16 bit words to it, and at the end, fold
346 * back all the carry bits from the top 16 bits into the lower
347 * 16 bits.
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000348 */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000349 while (nleft > 1) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000350 sum += *w++;
351 nleft -= 2;
352 }
353
354 /* mop up an odd byte, if necessary */
355 if (nleft == 1)
Mike Frysinger294254c2006-02-19 22:59:12 +0000356 sum += *(unsigned char *)w;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000357
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000358 /* add back carry outs from top 16 bits to low 16 bits */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000359 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
360 sum += (sum >> 16); /* add carry */
361 answer = ~sum; /* truncate to 16 bits */
362 return answer;
363}
364
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000365static void
Denis Vlasenko459be352007-06-17 19:09:05 +0000366send_probe(int seq, int ttl)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000367{
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000368 int len, res;
369 void *out;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000370
371 /* Payload */
372 outdata->seq = seq;
373 outdata->ttl = ttl;
Denis Vlasenko459be352007-06-17 19:09:05 +0000374// UNUSED: was storing gettimeofday's result there, but never ever checked it
375 /*memcpy(&outdata->tv, tp, sizeof(outdata->tv));*/
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000376
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000377 if (option_mask32 & OPT_USE_ICMP) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000378 outicmp->icmp_seq = htons(seq);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000379
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000380 /* Always calculate checksum for icmp packets */
381 outicmp->icmp_cksum = 0;
Denis Vlasenko28703012006-12-19 20:32:02 +0000382 outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp,
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000383 packlen - (sizeof(*outip) + optlen));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000384 if (outicmp->icmp_cksum == 0)
385 outicmp->icmp_cksum = 0xffff;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000386 }
387
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000388//BUG! verbose is (x & OPT_VERBOSE), not a counter!
389#if 0 //ENABLE_FEATURE_TRACEROUTE_VERBOSE
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000390 /* XXX undocumented debugging hack */
391 if (verbose > 1) {
Denis Vlasenko28703012006-12-19 20:32:02 +0000392 const uint16_t *sp;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000393 int nshorts, i;
394
Denis Vlasenko28703012006-12-19 20:32:02 +0000395 sp = (uint16_t *)outip;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000396 nshorts = (unsigned)packlen / sizeof(uint16_t);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000397 i = 0;
398 printf("[ %d bytes", packlen);
399 while (--nshorts >= 0) {
400 if ((i++ % 8) == 0)
401 printf("\n\t");
402 printf(" %04x", ntohs(*sp));
403 sp++;
404 }
405 if (packlen & 1) {
406 if ((i % 8) == 0)
407 printf("\n\t");
Mike Frysinger294254c2006-02-19 22:59:12 +0000408 printf(" %02x", *(unsigned char *)sp);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000409 }
410 printf("]\n");
411 }
412#endif
413
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000414#if defined(IP_TTL)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000415 if (setsockopt(sndsock, IPPROTO_IP, IP_TTL,
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000416 (char *)&ttl, sizeof(ttl)) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000417 bb_perror_msg_and_die("setsockopt ttl %d", ttl);
418 }
419#endif
420
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000421 len = packlen - sizeof(*outip);
422 if (option_mask32 & OPT_USE_ICMP)
423 out = outicmp;
424 else {
425 out = outdata;
426 len -= sizeof(*outudp);
427 set_nport(dest_lsa, htons(port + seq));
428 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100429 res = xsendto(sndsock, out, len, &dest_lsa->u.sa, dest_lsa->len);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000430 if (res != len) {
431 bb_info_msg("sent %d octets, ret=%d", len, res);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000432 }
Eric Andersen7467c8d2001-07-12 20:26:32 +0000433}
434
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000435#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000436/*
437 * Convert an ICMP "type" field to a printable string.
438 */
439static inline const char *
Mike Frysinger294254c2006-02-19 22:59:12 +0000440pr_type(unsigned char t)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000441{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000442 static const char *const ttab[] = {
Eric Andersen7467c8d2001-07-12 20:26:32 +0000443 "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable",
444 "Source Quench", "Redirect", "ICMP 6", "ICMP 7",
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000445 "Echo", "Router Advert", "Router Solicit", "Time Exceeded",
Eric Andersen7467c8d2001-07-12 20:26:32 +0000446 "Param Problem", "Timestamp", "Timestamp Reply", "Info Request",
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000447 "Info Reply", "Mask Request", "Mask Reply"
Eric Andersen7467c8d2001-07-12 20:26:32 +0000448 };
449
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000450 if (t >= ARRAY_SIZE(ttab))
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000451 return "OUT-OF-RANGE";
Eric Andersen7467c8d2001-07-12 20:26:32 +0000452
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000453 return ttab[t];
Eric Andersen7467c8d2001-07-12 20:26:32 +0000454}
455#endif
456
Denis Vlasenko85c24712008-03-17 09:04:04 +0000457#if !ENABLE_FEATURE_TRACEROUTE_VERBOSE
Denys Vlasenko0d565682009-11-23 06:15:38 +0100458#define packet_ok(read_len, from, seq) \
459 packet_ok(read_len, seq)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000460#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000461static int
Denys Vlasenko0d565682009-11-23 06:15:38 +0100462packet_ok(int read_len, const struct sockaddr_in *from, int seq)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000463{
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000464 const struct icmp *icp;
Mike Frysinger294254c2006-02-19 22:59:12 +0000465 unsigned char type, code;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000466 int hlen;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000467 const struct ip *ip;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000468
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000469 ip = (struct ip *) recv_pkt;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000470 hlen = ip->ip_hl << 2;
Denys Vlasenko0d565682009-11-23 06:15:38 +0100471 if (read_len < hlen + ICMP_MINLEN) {
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000472#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000473 if (verbose)
Denys Vlasenko0d565682009-11-23 06:15:38 +0100474 printf("packet too short (%d bytes) from %s\n", read_len,
Eric Andersen7467c8d2001-07-12 20:26:32 +0000475 inet_ntoa(from->sin_addr));
476#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000477 return 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000478 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100479 read_len -= hlen;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000480 icp = (struct icmp *)(recv_pkt + hlen);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000481 type = icp->icmp_type;
482 code = icp->icmp_code;
483 /* Path MTU Discovery (RFC1191) */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000484 pmtu = 0;
485 if (code == ICMP_UNREACH_NEEDFRAG)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000486 pmtu = ntohs(icp->icmp_nextmtu);
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000487
488 if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS)
489 || type == ICMP_UNREACH
490 || type == ICMP_ECHOREPLY
491 ) {
492 const struct ip *hip;
493 const struct udphdr *up;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000494
495 hip = &icp->icmp_ip;
496 hlen = hip->ip_hl << 2;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000497 if (option_mask32 & OPT_USE_ICMP) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000498 struct icmp *hicmp;
499
500 /* XXX */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000501 if (type == ICMP_ECHOREPLY
502 && icp->icmp_id == htons(ident)
503 && icp->icmp_seq == htons(seq)
504 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000505 return -2;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000506 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000507
Mike Frysinger294254c2006-02-19 22:59:12 +0000508 hicmp = (struct icmp *)((unsigned char *)hip + hlen);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100509 if (hlen + SIZEOF_ICMP_HDR <= read_len
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000510 && hip->ip_p == IPPROTO_ICMP
511 && hicmp->icmp_id == htons(ident)
512 && hicmp->icmp_seq == htons(seq)
513 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000514 return (type == ICMP_TIMXCEED ? -1 : code + 1);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000515 }
516 } else {
517 up = (struct udphdr *)((char *)hip + hlen);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100518 if (hlen + 12 <= read_len
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000519 && hip->ip_p == IPPROTO_UDP
520// Off: since we do not form the entire IP packet,
521// but defer it to kernel, we can't set source port,
522// and thus can't check it here in the reply
523 /* && up->source == htons(ident) */
524 && up->dest == htons(port + seq)
525 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000526 return (type == ICMP_TIMXCEED ? -1 : code + 1);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000527 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000528 }
Eric Andersen7467c8d2001-07-12 20:26:32 +0000529 }
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000530#if ENABLE_FEATURE_TRACEROUTE_VERBOSE
Eric Andersen7467c8d2001-07-12 20:26:32 +0000531 if (verbose) {
532 int i;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000533 uint32_t *lp = (uint32_t *)&icp->icmp_ip;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000534
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000535 printf("\n%d bytes from %s to "
536 "%s: icmp type %d (%s) code %d\n",
Denys Vlasenko0d565682009-11-23 06:15:38 +0100537 read_len, inet_ntoa(from->sin_addr),
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000538 inet_ntoa(ip->ip_dst),
539 type, pr_type(type), icp->icmp_code);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100540 for (i = 4; i < read_len; i += sizeof(*lp))
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000541 printf("%2d: x%8.8x\n", i, *lp++);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000542 }
543#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000544 return 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000545}
546
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000547/*
548 * Construct an Internet address representation.
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000549 * If the -n flag has been supplied, give
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000550 * numeric value, otherwise try for symbolic name.
551 */
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000552static void
Denys Vlasenko0d565682009-11-23 06:15:38 +0100553print_inetname(const struct sockaddr *from)
Eric Andersen7467c8d2001-07-12 20:26:32 +0000554{
Denys Vlasenko0d565682009-11-23 06:15:38 +0100555 char *ina = xmalloc_sockaddr2dotted_noport(from);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000556
Denys Vlasenko0d565682009-11-23 06:15:38 +0100557 if (option_mask32 & OPT_ADDR_NUM) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000558 printf(" %s", ina);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100559 } else {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000560 char *n = NULL;
Denys Vlasenko0d565682009-11-23 06:15:38 +0100561
562 if (from->sa_family != AF_INET
563 || ((struct sockaddr_in*)from)->sin_addr.s_addr != INADDR_ANY
564 ) {
565 /* Try to reverse resolve if it is not 0.0.0.0 */
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000566 n = xmalloc_sockaddr2host_noport((struct sockaddr*)from);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100567 }
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000568 printf(" %s (%s)", (n ? n : ina), ina);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000569 free(n);
570 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100571 free(ina);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000572}
573
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000574static void
Denys Vlasenko0d565682009-11-23 06:15:38 +0100575print(int read_len, const struct sockaddr_in *from)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000576{
Denys Vlasenko0d565682009-11-23 06:15:38 +0100577 print_inetname((const struct sockaddr*)from);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000578 if (verbose) {
579 const struct ip *ip;
580 int hlen;
581
582 ip = (struct ip *) recv_pkt;
583 hlen = ip->ip_hl << 2;
Denys Vlasenko0d565682009-11-23 06:15:38 +0100584 read_len -= hlen;
585 printf(" %d bytes to %s", read_len, inet_ntoa(ip->ip_dst));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000586 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000587}
588
589static void
Denis Vlasenko459be352007-06-17 19:09:05 +0000590print_delta_ms(unsigned t1p, unsigned t2p)
Denis Vlasenko1adf6812007-06-14 07:47:52 +0000591{
Denis Vlasenko459be352007-06-17 19:09:05 +0000592 unsigned tt = t2p - t1p;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000593 printf(" %u.%03u ms", tt / 1000, tt % 1000);
Denis Vlasenko1adf6812007-06-14 07:47:52 +0000594}
Eric Andersen7467c8d2001-07-12 20:26:32 +0000595
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000596/*
Denys Vlasenko0d565682009-11-23 06:15:38 +0100597 * Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl]
598 * [-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos]
599 * [-w waittime] [-z pausemsecs] host [packetlen]"
600 */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000601
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000602int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko0d565682009-11-23 06:15:38 +0100603int traceroute_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen5c58d282001-07-10 16:29:00 +0000604{
Denys Vlasenko0d565682009-11-23 06:15:38 +0100605 int i;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000606 int minpacket;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000607 int tos = 0;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000608 int max_ttl = 30;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000609 int nprobes = 3;
610 int first_ttl = 1;
611 unsigned pausemsecs = 0;
612 unsigned op;
613 char *source;
614 char *device;
615 char *tos_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000616 char *max_ttl_str;
617 char *port_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000618 char *nprobes_str;
619 char *waittime_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000620 char *pausemsecs_str;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000621 char *first_ttl_str;
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000622#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000623 llist_t *source_route_list = NULL;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000624 int lsrr = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000625#endif
Denys Vlasenko0d565682009-11-23 06:15:38 +0100626 int ttl;
627 int seq;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000628
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000629 INIT_G();
Denis Vlasenko2e723232007-03-29 21:55:22 +0000630
Denys Vlasenko0d565682009-11-23 06:15:38 +0100631 /* minimum 1 arg */
632 opt_complementary = "-1:x-x" IF_FEATURE_TRACEROUTE_SOURCE_ROUTE(":g::");
633 op = getopt32(argv, OPT_STRING
Denis Vlasenko2e723232007-03-29 21:55:22 +0000634 , &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str
635 , &source, &waittime_str, &pausemsecs_str, &first_ttl_str
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000636#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000637 , &source_route_list
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000638#endif
639 );
640
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000641#if 0 /* IGNORED */
642 if (op & OPT_IP_CHKSUM)
Denis Vlasenko13858992006-10-08 12:49:22 +0000643 bb_error_msg("warning: ip checksums disabled");
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000644#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000645 if (op & OPT_TOS)
646 tos = xatou_range(tos_str, 0, 255);
647 if (op & OPT_MAX_TTL)
648 max_ttl = xatou_range(max_ttl_str, 1, 255);
649 if (op & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +0000650 port = xatou16(port_str);
Denis Vlasenko2e723232007-03-29 21:55:22 +0000651 if (op & OPT_NPROBES)
652 nprobes = xatou_range(nprobes_str, 1, INT_MAX);
653 if (op & OPT_SOURCE) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000654 /*
655 * set the ip source address of the outbound
656 * probe (e.g., on a multi-homed host).
657 */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000658 if (getuid() != 0)
659 bb_error_msg_and_die("you must be root to use -s");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000660 }
Denis Vlasenko2e723232007-03-29 21:55:22 +0000661 if (op & OPT_WAITTIME)
Denis Vlasenkobdea2c02009-01-24 21:23:41 +0000662 waittime = xatou_range(waittime_str, 1, 24 * 60 * 60);
Denis Vlasenko2e723232007-03-29 21:55:22 +0000663 if (op & OPT_PAUSE_MS)
664 pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000);
665 if (op & OPT_FIRST_TTL)
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000666 first_ttl = xatou_range(first_ttl_str, 1, max_ttl);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000667
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000668#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
Denis Vlasenkof7fcca42007-04-13 22:07:21 +0000669 if (source_route_list) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000670 while (source_route_list) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000671 len_and_sockaddr *lsa;
672
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000673 if (lsrr >= NGATEWAYS)
Denis Vlasenko92758142006-10-03 19:56:34 +0000674 bb_error_msg_and_die("no more than %d gateways", NGATEWAYS);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000675 lsa = xhost_and_af2sockaddr(llist_pop(&source_route_list), 0, AF_INET);
676 gwlist[lsrr] = lsa->u.sin.sin_addr.s_addr;
677 free(lsa);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000678 ++lsrr;
Eric Andersen5c58d282001-07-10 16:29:00 +0000679 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000680 optlen = (lsrr + 1) * sizeof(gwlist[0]);
681 }
682#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000683
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000684 minpacket = sizeof(*outip) + SIZEOF_ICMP_HDR + sizeof(*outdata) + optlen;
685 if (!(op & OPT_USE_ICMP))
686 minpacket += sizeof(*outudp) - SIZEOF_ICMP_HDR;
687 packlen = minpacket;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000688
689 /* Process destination and optional packet size */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000690 argv += optind;
Denys Vlasenko0d565682009-11-23 06:15:38 +0100691 if (argv[1])
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000692 packlen = xatoul_range(argv[1], minpacket, 32 * 1024);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100693 dest_lsa = xhost2sockaddr(argv[0], port);
Eric Andersen5c58d282001-07-10 16:29:00 +0000694
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000695 /* Ensure the socket fds won't be 0, 1 or 2 */
696 bb_sanitize_stdio();
Eric Andersen5c58d282001-07-10 16:29:00 +0000697
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000698 xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), rcvsock);
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000699#if TRACEROUTE_SO_DEBUG
Denis Vlasenko2e723232007-03-29 21:55:22 +0000700 if (op & OPT_DEBUG)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000701 setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000702 &const_int_1, sizeof(const_int_1));
Eric Andersen7467c8d2001-07-12 20:26:32 +0000703#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000704 if (op & OPT_BYPASS_ROUTE)
Denis Vlasenko9ad2cb32007-12-16 17:19:40 +0000705 setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000706 &const_int_1, sizeof(const_int_1));
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000707
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000708 if (op & OPT_USE_ICMP)
709 xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), sndsock);
710 else
711 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sndsock);
Denys Vlasenko0d565682009-11-23 06:15:38 +0100712#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE && defined IP_OPTIONS
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000713 if (lsrr > 0) {
Mike Frysinger294254c2006-02-19 22:59:12 +0000714 unsigned char optlist[MAX_IPOPTLEN];
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000715
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000716 /* final hop */
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000717 gwlist[lsrr] = dest_lsa->u.sin.sin_addr.s_addr;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000718 ++lsrr;
719
720 /* force 4 byte alignment */
721 optlist[0] = IPOPT_NOP;
722 /* loose source route option */
723 optlist[1] = IPOPT_LSRR;
724 i = lsrr * sizeof(gwlist[0]);
725 optlist[2] = i + 3;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000726 /* pointer to LSRR addresses */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000727 optlist[3] = IPOPT_MINOFF;
728 memcpy(optlist + 4, gwlist, i);
729
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000730 if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS,
731 (char *)optlist, i + sizeof(gwlist[0])) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000732 bb_perror_msg_and_die("IP_OPTIONS");
Denis Vlasenko6edadde2006-10-03 18:19:02 +0000733 }
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000734 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100735#endif
Eric Andersen5c58d282001-07-10 16:29:00 +0000736#ifdef SO_SNDBUF
Denis Vlasenko703e2022007-01-22 14:12:08 +0000737 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000738 bb_perror_msg_and_die("SO_SNDBUF");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000739 }
Eric Andersenad79c0b2002-06-06 12:24:51 +0000740#endif
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000741#ifdef IP_TOS
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000742 if ((op & OPT_TOS) && setsockopt(sndsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000743 bb_perror_msg_and_die("setsockopt tos %d", tos);
744 }
745#endif
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000746#ifdef IP_DONTFRAG
747 if (op & OPT_DONT_FRAGMNT)
748 setsockopt(sndsock, IPPROTO_IP, IP_DONTFRAG,
749 &const_int_1, sizeof(const_int_1));
Eric Andersenad79c0b2002-06-06 12:24:51 +0000750#endif
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000751#if TRACEROUTE_SO_DEBUG
Denis Vlasenko2e723232007-03-29 21:55:22 +0000752 if (op & OPT_DEBUG)
Denis Vlasenko703e2022007-01-22 14:12:08 +0000753 setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
754 &const_int_1, sizeof(const_int_1));
Eric Andersen7467c8d2001-07-12 20:26:32 +0000755#endif
Denis Vlasenko2e723232007-03-29 21:55:22 +0000756 if (op & OPT_BYPASS_ROUTE)
Denis Vlasenko703e2022007-01-22 14:12:08 +0000757 setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
758 &const_int_1, sizeof(const_int_1));
Eric Andersen5c58d282001-07-10 16:29:00 +0000759
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000760 outip = xzalloc(packlen);
761
762 if (op & OPT_USE_ICMP) {
763 ident = getpid() | 0x8000;
764 outicmp->icmp_type = ICMP_ECHO;
765 outicmp->icmp_id = htons(ident);
766 outdata = (struct outdata_t *)((char *)outicmp + SIZEOF_ICMP_HDR);
767 } else {
768 outdata = (struct outdata_t *)(outudp + 1);
769 }
770
771 if (op & OPT_DEVICE) /* hmm, do we need error check? */
772 setsockopt_bindtodevice(sndsock, device);
773
774 if (op & OPT_SOURCE) {
775 len_and_sockaddr *source_lsa = xdotted2sockaddr(source, 0);
776 /* Ping does this (why?) */
777 if (setsockopt(sndsock, IPPROTO_IP, IP_MULTICAST_IF,
778 &source_lsa->u.sa, source_lsa->len))
779 bb_error_msg_and_die("can't set multicast source interface");
780//TODO: we can query source port we bound to,
781// and check it in replies... if we care enough
782 xbind(sndsock, &source_lsa->u.sa, source_lsa->len);
783 free(source_lsa);
784 }
785
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000786 /* Revert to non-privileged user after opening sockets */
Rob Landleyafb94ec2006-07-16 08:06:34 +0000787 xsetgid(getgid());
788 xsetuid(getuid());
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000789
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000790 printf("traceroute to %s (%s)", argv[0],
791 xmalloc_sockaddr2dotted_noport(&dest_lsa->u.sa));
Denis Vlasenko2e723232007-03-29 21:55:22 +0000792 if (op & OPT_SOURCE)
793 printf(" from %s", source);
794 printf(", %d hops max, %d byte packets\n", max_ttl, packlen);
Eric Andersen5c58d282001-07-10 16:29:00 +0000795
Denys Vlasenko0d565682009-11-23 06:15:38 +0100796 seq = 0;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000797 for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000798//TODO: make it protocol agnostic (get rid of sockaddr_in)
799 struct sockaddr_in from;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000800 uint32_t lastaddr = 0;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000801 int probe;
802 int unreachable = 0; /* counter */
803 int gotlastaddr = 0; /* flags */
Eric Andersen5c58d282001-07-10 16:29:00 +0000804 int got_there = 0;
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000805 int first = 1;
Eric Andersen5c58d282001-07-10 16:29:00 +0000806
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000807 printf("%2d", ttl);
Eric Andersen5c58d282001-07-10 16:29:00 +0000808 for (probe = 0; probe < nprobes; ++probe) {
Denys Vlasenko0d565682009-11-23 06:15:38 +0100809 int read_len;
Denis Vlasenko459be352007-06-17 19:09:05 +0000810 unsigned t1;
811 unsigned t2;
Eric Andersen5c58d282001-07-10 16:29:00 +0000812 struct ip *ip;
813
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000814 if (!first && pausemsecs > 0)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000815 usleep(pausemsecs * 1000);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100816 fflush_all();
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000817
Denis Vlasenko459be352007-06-17 19:09:05 +0000818 t1 = monotonic_us();
819 send_probe(++seq, ttl);
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000820
Denys Vlasenko0d565682009-11-23 06:15:38 +0100821 first = 0;
822 while ((read_len = wait_for_reply(&from)) != 0) {
Denis Vlasenko459be352007-06-17 19:09:05 +0000823 t2 = monotonic_us();
Denys Vlasenko0d565682009-11-23 06:15:38 +0100824 i = packet_ok(read_len, &from, seq);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000825 /* Skip short packet */
826 if (i == 0)
827 continue;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000828 if (!gotlastaddr
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000829 || from.sin_addr.s_addr != lastaddr
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000830 ) {
Denys Vlasenko0d565682009-11-23 06:15:38 +0100831 print(read_len, &from);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000832 lastaddr = from.sin_addr.s_addr;
833 gotlastaddr = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000834 }
Denis Vlasenko459be352007-06-17 19:09:05 +0000835 print_delta_ms(t1, t2);
Denis Vlasenko766c10c2009-01-24 20:14:24 +0000836 ip = (struct ip *)recv_pkt;
Denis Vlasenko2e723232007-03-29 21:55:22 +0000837 if (op & OPT_TTL_FLAG)
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000838 printf(" (%d)", ip->ip_ttl);
839 if (i == -2) {
840 if (ip->ip_ttl <= 1)
841 printf(" !");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000842 got_there = 1;
Eric Andersen5c58d282001-07-10 16:29:00 +0000843 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000844 }
845 /* time exceeded in transit */
846 if (i == -1)
847 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000848 i--;
849 switch (i) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000850 case ICMP_UNREACH_PORT:
851 if (ip->ip_ttl <= 1)
852 printf(" !");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000853 got_there = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000854 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000855 case ICMP_UNREACH_NET:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000856 printf(" !N");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000857 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000858 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000859 case ICMP_UNREACH_HOST:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000860 printf(" !H");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000861 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000862 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000863 case ICMP_UNREACH_PROTOCOL:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000864 printf(" !P");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000865 got_there = 1;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000866 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000867 case ICMP_UNREACH_NEEDFRAG:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000868 printf(" !F-%d", pmtu);
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000869 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000870 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000871 case ICMP_UNREACH_SRCFAIL:
872 printf(" !S");
873 ++unreachable;
874 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000875 case ICMP_UNREACH_FILTER_PROHIB:
876 case ICMP_UNREACH_NET_PROHIB: /* misuse */
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000877 printf(" !A");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000878 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000879 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000880 case ICMP_UNREACH_HOST_PROHIB:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000881 printf(" !C");
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000882 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000883 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000884 case ICMP_UNREACH_HOST_PRECEDENCE:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000885 printf(" !V");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000886 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000887 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000888 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
889 printf(" !C");
890 ++unreachable;
891 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000892 case ICMP_UNREACH_NET_UNKNOWN:
893 case ICMP_UNREACH_HOST_UNKNOWN:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000894 printf(" !U");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000895 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000896 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000897 case ICMP_UNREACH_ISOLATED:
898 printf(" !I");
899 ++unreachable;
900 break;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000901 case ICMP_UNREACH_TOSNET:
902 case ICMP_UNREACH_TOSHOST:
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000903 printf(" !T");
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000904 ++unreachable;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000905 break;
906 default:
907 printf(" !<%d>", i);
908 ++unreachable;
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000909 break;
910 }
911 break;
Eric Andersen5c58d282001-07-10 16:29:00 +0000912 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100913 /* there was no packet at all? */
914 if (read_len == 0)
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000915 printf(" *");
Eric Andersen5c58d282001-07-10 16:29:00 +0000916 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000917 bb_putchar('\n');
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000918 if (got_there
919 || (unreachable > 0 && unreachable >= nprobes - 1)
920 ) {
"Vladimir N. Oleynik"45a8ed82005-09-06 16:08:33 +0000921 break;
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000922 }
Eric Andersen5c58d282001-07-10 16:29:00 +0000923 }
Denys Vlasenko0d565682009-11-23 06:15:38 +0100924
Eric Andersen5c58d282001-07-10 16:29:00 +0000925 return 0;
926}