traceroute.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Busybox port by Vladimir Oleynik (C) 2005 <dzo@simtreas.ru>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * 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.
  23. */
  24. /*
  25. * traceroute6
  26. *
  27. * Modified for NRL 4.4BSD IPv6 release.
  28. * 07/31/96 bgp
  29. *
  30. * Modified for Linux IPv6 by Pedro Roque <roque@di.fc.ul.pt>
  31. * 31/07/1996
  32. *
  33. * As ICMP error messages for IPv6 now include more than 8 bytes
  34. * UDP datagrams are now sent via an UDP socket instead of magic
  35. * RAW socket tricks.
  36. *
  37. * Converted to busybox applet by Leonid Lisovskiy <lly@sf.net>
  38. * 2009-11-16
  39. */
  40. /*
  41. * traceroute host - trace the route ip packets follow going to "host".
  42. *
  43. * Attempt to trace the route an ip packet would follow to some
  44. * internet host. We find out intermediate hops by launching probe
  45. * packets with a small ttl (time to live) then listening for an
  46. * icmp "time exceeded" reply from a gateway. We start our probes
  47. * with a ttl of one and increase by one until we get an icmp "port
  48. * unreachable" (which means we got to "host") or hit a max (which
  49. * defaults to 30 hops & can be changed with the -m flag). Three
  50. * probes (change with -q flag) are sent at each ttl setting and a
  51. * line is printed showing the ttl, address of the gateway and
  52. * round trip time of each probe. If the probe answers come from
  53. * different gateways, the address of each responding system will
  54. * be printed. If there is no response within a 5 sec. timeout
  55. * interval (changed with the -w flag), a "*" is printed for that
  56. * probe.
  57. *
  58. * Probe packets are UDP format. We don't want the destination
  59. * host to process them so the destination port is set to an
  60. * unlikely value (if some clod on the destination is using that
  61. * value, it can be changed with the -p flag).
  62. *
  63. * A sample use might be:
  64. *
  65. * [yak 71]% traceroute nis.nsf.net.
  66. * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
  67. * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
  68. * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
  69. * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
  70. * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
  71. * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
  72. * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
  73. * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
  74. * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
  75. * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
  76. * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
  77. * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
  78. *
  79. * Note that lines 2 & 3 are the same. This is due to a buggy
  80. * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
  81. * packets with a zero ttl.
  82. *
  83. * A more interesting example is:
  84. *
  85. * [yak 72]% traceroute allspice.lcs.mit.edu.
  86. * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
  87. * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
  88. * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
  89. * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
  90. * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
  91. * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
  92. * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
  93. * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
  94. * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
  95. * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
  96. * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
  97. * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
  98. * 12 * * *
  99. * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
  100. * 14 * * *
  101. * 15 * * *
  102. * 16 * * *
  103. * 17 * * *
  104. * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
  105. *
  106. * (I start to see why I'm having so much trouble with mail to
  107. * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
  108. * either don't send ICMP "time exceeded" messages or send them
  109. * with a ttl too small to reach us. 14 - 17 are running the
  110. * MIT C Gateway code that doesn't send "time exceeded"s. God
  111. * only knows what's going on with 12.
  112. *
  113. * The silent gateway 12 in the above may be the result of a bug in
  114. * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
  115. * sends an unreachable message using whatever ttl remains in the
  116. * original datagram. Since, for gateways, the remaining ttl is
  117. * zero, the icmp "time exceeded" is guaranteed to not make it back
  118. * to us. The behavior of this bug is slightly more interesting
  119. * when it appears on the destination system:
  120. *
  121. * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
  122. * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
  123. * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
  124. * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
  125. * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
  126. * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
  127. * 7 * * *
  128. * 8 * * *
  129. * 9 * * *
  130. * 10 * * *
  131. * 11 * * *
  132. * 12 * * *
  133. * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
  134. *
  135. * Notice that there are 12 "gateways" (13 is the final
  136. * destination) and exactly the last half of them are "missing".
  137. * What's really happening is that rip (a Sun-3 running Sun OS3.5)
  138. * is using the ttl from our arriving datagram as the ttl in its
  139. * icmp reply. So, the reply will time out on the return path
  140. * (with no notice sent to anyone since icmp's aren't sent for
  141. * icmp's) until we probe with a ttl that's at least twice the path
  142. * length. I.e., rip is really only 7 hops away. A reply that
  143. * returns with a ttl of 1 is a clue this problem exists.
  144. * Traceroute prints a "!" after the time if the ttl is <= 1.
  145. * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
  146. * non-standard (HPUX) software, expect to see this problem
  147. * frequently and/or take care picking the target host of your
  148. * probes.
  149. *
  150. * Other possible annotations after the time are !H, !N, !P (got a host,
  151. * network or protocol unreachable, respectively), !S or !F (source
  152. * route failed or fragmentation needed -- neither of these should
  153. * ever occur and the associated gateway is busted if you see one). If
  154. * almost all the probes result in some kind of unreachable, traceroute
  155. * will give up and exit.
  156. *
  157. * Notes
  158. * -----
  159. * This program must be run by root or be setuid. (I suggest that
  160. * you *don't* make it setuid -- casual use could result in a lot
  161. * of unnecessary traffic on our poor, congested nets.)
  162. *
  163. * This program requires a kernel mod that does not appear in any
  164. * system available from Berkeley: A raw ip socket using proto
  165. * IPPROTO_RAW must interpret the data sent as an ip datagram (as
  166. * opposed to data to be wrapped in a ip datagram). See the README
  167. * file that came with the source to this program for a description
  168. * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
  169. * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
  170. * MODIFIED TO RUN THIS PROGRAM.
  171. *
  172. * The udp port usage may appear bizarre (well, ok, it is bizarre).
  173. * The problem is that an icmp message only contains 8 bytes of
  174. * data from the original datagram. 8 bytes is the size of a udp
  175. * header so, if we want to associate replies with the original
  176. * datagram, the necessary information must be encoded into the
  177. * udp header (the ip id could be used but there's no way to
  178. * interlock with the kernel's assignment of ip id's and, anyway,
  179. * it would have taken a lot more kernel hacking to allow this
  180. * code to set the ip id). So, to allow two or more users to
  181. * use traceroute simultaneously, we use this task's pid as the
  182. * source port (the high bit is set to move the port number out
  183. * of the "likely" range). To keep track of which probe is being
  184. * replied to (so times and/or hop counts don't get confused by a
  185. * reply that was delayed in transit), we increment the destination
  186. * port number before each probe.
  187. *
  188. * Don't use this as a coding example. I was trying to find a
  189. * routing problem and this code sort-of popped out after 48 hours
  190. * without sleep. I was amazed it ever compiled, much less ran.
  191. *
  192. * I stole the idea for this program from Steve Deering. Since
  193. * the first release, I've learned that had I attended the right
  194. * IETF working group meetings, I also could have stolen it from Guy
  195. * Almes or Matt Mathis. I don't know (or care) who came up with
  196. * the idea first. I envy the originators' perspicacity and I'm
  197. * glad they didn't keep the idea a secret.
  198. *
  199. * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
  200. * enhancements to the original distribution.
  201. *
  202. * I've hacked up a round-trip-route version of this that works by
  203. * sending a loose-source-routed udp datagram through the destination
  204. * back to yourself. Unfortunately, SO many gateways botch source
  205. * routing, the thing is almost worthless. Maybe one day...
  206. *
  207. * -- Van Jacobson (van@ee.lbl.gov)
  208. * Tue Dec 20 03:50:13 PST 1988
  209. */
  210. #define TRACEROUTE_SO_DEBUG 0
  211. /* TODO: undefs were uncommented - ??! we have config system for that! */
  212. /* probably ok to remove altogether */
  213. //#undef CONFIG_FEATURE_TRACEROUTE_VERBOSE
  214. //#define CONFIG_FEATURE_TRACEROUTE_VERBOSE
  215. //#undef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
  216. //#define CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
  217. //#undef CONFIG_FEATURE_TRACEROUTE_USE_ICMP
  218. //#define CONFIG_FEATURE_TRACEROUTE_USE_ICMP
  219. #include <net/if.h>
  220. #include <arpa/inet.h>
  221. #include <netinet/in.h>
  222. #include <netinet/udp.h>
  223. #include <netinet/ip.h>
  224. #include <netinet/ip_icmp.h>
  225. #if ENABLE_FEATURE_IPV6
  226. # include <netinet/ip6.h>
  227. # include <netinet/icmp6.h>
  228. # ifndef SOL_IPV6
  229. # define SOL_IPV6 IPPROTO_IPV6
  230. # endif
  231. #endif
  232. #include "libbb.h"
  233. #include "inet_common.h"
  234. #ifndef IPPROTO_ICMP
  235. # define IPPROTO_ICMP 1
  236. #endif
  237. #ifndef IPPROTO_IP
  238. # define IPPROTO_IP 0
  239. #endif
  240. #define OPT_STRING "FIlnrdvxt:i:m:p:q:s:w:z:f:" \
  241. IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:") \
  242. "4" IF_TRACEROUTE6("6")
  243. enum {
  244. OPT_DONT_FRAGMNT = (1 << 0), /* F */
  245. OPT_USE_ICMP = (1 << 1) * ENABLE_FEATURE_TRACEROUTE_USE_ICMP, /* I */
  246. OPT_TTL_FLAG = (1 << 2), /* l */
  247. OPT_ADDR_NUM = (1 << 3), /* n */
  248. OPT_BYPASS_ROUTE = (1 << 4), /* r */
  249. OPT_DEBUG = (1 << 5), /* d */
  250. OPT_VERBOSE = (1 << 6) * ENABLE_FEATURE_TRACEROUTE_VERBOSE, /* v */
  251. OPT_IP_CHKSUM = (1 << 7), /* x */
  252. OPT_TOS = (1 << 8), /* t */
  253. OPT_DEVICE = (1 << 9), /* i */
  254. OPT_MAX_TTL = (1 << 10), /* m */
  255. OPT_PORT = (1 << 11), /* p */
  256. OPT_NPROBES = (1 << 12), /* q */
  257. OPT_SOURCE = (1 << 13), /* s */
  258. OPT_WAITTIME = (1 << 14), /* w */
  259. OPT_PAUSE_MS = (1 << 15), /* z */
  260. OPT_FIRST_TTL = (1 << 16), /* f */
  261. OPT_SOURCE_ROUTE = (1 << 17) * ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE, /* g */
  262. OPT_IPV4 = (1 << (17+ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE)), /* 4 */
  263. OPT_IPV6 = (1 << (18+ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE)) * ENABLE_TRACEROUTE6, /* 6 */
  264. };
  265. #define verbose (option_mask32 & OPT_VERBOSE)
  266. enum {
  267. SIZEOF_ICMP_HDR = 8,
  268. rcvsock = 3, /* receive (icmp) socket file descriptor */
  269. sndsock = 4, /* send (udp/icmp) socket file descriptor */
  270. };
  271. /* Data section of the probe packet */
  272. struct outdata_t {
  273. unsigned char seq; /* sequence number of this packet */
  274. unsigned char ttl; /* ttl packet left with */
  275. // UNUSED. Retaining to have the same packet size.
  276. struct timeval tv_UNUSED PACKED; /* time packet left */
  277. };
  278. #if ENABLE_TRACEROUTE6
  279. struct outdata6_t {
  280. uint32_t ident6;
  281. uint32_t seq6;
  282. struct timeval tv_UNUSED PACKED; /* time packet left */
  283. };
  284. #endif
  285. struct globals {
  286. struct ip *outip;
  287. struct outdata_t *outdata;
  288. len_and_sockaddr *dest_lsa;
  289. int packlen; /* total length of packet */
  290. int pmtu; /* Path MTU Discovery (RFC1191) */
  291. uint32_t ident;
  292. uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */
  293. int waittime; // 5; /* time to wait for response (in seconds) */
  294. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  295. int optlen; /* length of ip options */
  296. #else
  297. #define optlen 0
  298. #endif
  299. unsigned char recv_pkt[512]; /* last inbound (icmp) packet */
  300. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  301. /* Maximum number of gateways (include room for one noop) */
  302. #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t)))
  303. /* loose source route gateway list (including room for final destination) */
  304. uint32_t gwlist[NGATEWAYS + 1];
  305. #endif
  306. };
  307. #define G (*ptr_to_globals)
  308. #define outip (G.outip )
  309. #define outdata (G.outdata )
  310. #define dest_lsa (G.dest_lsa )
  311. #define packlen (G.packlen )
  312. #define pmtu (G.pmtu )
  313. #define ident (G.ident )
  314. #define port (G.port )
  315. #define waittime (G.waittime )
  316. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  317. # define optlen (G.optlen )
  318. #endif
  319. #define recv_pkt (G.recv_pkt )
  320. #define gwlist (G.gwlist )
  321. #define INIT_G() do { \
  322. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  323. port = 32768 + 666; \
  324. waittime = 5; \
  325. } while (0)
  326. #define outicmp ((struct icmp *)(outip + 1))
  327. #define outudp ((struct udphdr *)(outip + 1))
  328. /* libbb candidate? tftp uses this idiom too */
  329. static len_and_sockaddr* dup_sockaddr(const len_and_sockaddr *lsa)
  330. {
  331. len_and_sockaddr *new_lsa = xzalloc(LSA_LEN_SIZE + lsa->len);
  332. memcpy(new_lsa, lsa, LSA_LEN_SIZE + lsa->len);
  333. return new_lsa;
  334. }
  335. static int
  336. wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to)
  337. {
  338. struct pollfd pfd[1];
  339. int read_len = 0;
  340. pfd[0].fd = rcvsock;
  341. pfd[0].events = POLLIN;
  342. if (safe_poll(pfd, 1, waittime * 1000) > 0) {
  343. read_len = recv_from_to(rcvsock,
  344. recv_pkt, sizeof(recv_pkt),
  345. /*flags:*/ 0,
  346. &from_lsa->u.sa, to, from_lsa->len);
  347. }
  348. return read_len;
  349. }
  350. /*
  351. * Checksum routine for Internet Protocol family headers (C Version)
  352. */
  353. static uint16_t
  354. in_cksum(uint16_t *addr, int len)
  355. {
  356. int nleft = len;
  357. uint16_t *w = addr;
  358. uint16_t answer;
  359. int sum = 0;
  360. /*
  361. * Our algorithm is simple, using a 32 bit accumulator (sum),
  362. * we add sequential 16 bit words to it, and at the end, fold
  363. * back all the carry bits from the top 16 bits into the lower
  364. * 16 bits.
  365. */
  366. while (nleft > 1) {
  367. sum += *w++;
  368. nleft -= 2;
  369. }
  370. /* mop up an odd byte, if necessary */
  371. if (nleft == 1)
  372. sum += *(unsigned char *)w;
  373. /* add back carry outs from top 16 bits to low 16 bits */
  374. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  375. sum += (sum >> 16); /* add carry */
  376. answer = ~sum; /* truncate to 16 bits */
  377. return answer;
  378. }
  379. static void
  380. send_probe(int seq, int ttl)
  381. {
  382. int len, res;
  383. void *out;
  384. /* Payload */
  385. #if ENABLE_TRACEROUTE6
  386. if (dest_lsa->u.sa.sa_family == AF_INET6) {
  387. struct outdata6_t *pkt = (struct outdata6_t *) outip;
  388. pkt->ident6 = htonl(ident);
  389. pkt->seq6 = htonl(seq);
  390. /*gettimeofday(&pkt->tv, &tz);*/
  391. } else
  392. #endif
  393. {
  394. outdata->seq = seq;
  395. outdata->ttl = ttl;
  396. // UNUSED: was storing gettimeofday's result there, but never ever checked it
  397. /*memcpy(&outdata->tv, tp, sizeof(outdata->tv));*/
  398. if (option_mask32 & OPT_USE_ICMP) {
  399. outicmp->icmp_seq = htons(seq);
  400. /* Always calculate checksum for icmp packets */
  401. outicmp->icmp_cksum = 0;
  402. outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp,
  403. packlen - (sizeof(*outip) + optlen));
  404. if (outicmp->icmp_cksum == 0)
  405. outicmp->icmp_cksum = 0xffff;
  406. }
  407. }
  408. //BUG! verbose is (x & OPT_VERBOSE), not a counter!
  409. #if 0 //ENABLE_FEATURE_TRACEROUTE_VERBOSE
  410. /* XXX undocumented debugging hack */
  411. if (verbose > 1) {
  412. const uint16_t *sp;
  413. int nshorts, i;
  414. sp = (uint16_t *)outip;
  415. nshorts = (unsigned)packlen / sizeof(uint16_t);
  416. i = 0;
  417. printf("[ %d bytes", packlen);
  418. while (--nshorts >= 0) {
  419. if ((i++ % 8) == 0)
  420. printf("\n\t");
  421. printf(" %04x", ntohs(*sp));
  422. sp++;
  423. }
  424. if (packlen & 1) {
  425. if ((i % 8) == 0)
  426. printf("\n\t");
  427. printf(" %02x", *(unsigned char *)sp);
  428. }
  429. printf("]\n");
  430. }
  431. #endif
  432. #if ENABLE_TRACEROUTE6
  433. if (dest_lsa->u.sa.sa_family == AF_INET6) {
  434. res = setsockopt(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
  435. if (res < 0)
  436. bb_perror_msg_and_die("setsockopt UNICAST_HOPS %d", ttl);
  437. out = outip;
  438. len = packlen;
  439. } else
  440. #endif
  441. {
  442. #if defined IP_TTL
  443. res = setsockopt(sndsock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
  444. if (res < 0)
  445. bb_perror_msg_and_die("setsockopt ttl %d", ttl);
  446. #endif
  447. out = outicmp;
  448. len = packlen - sizeof(*outip);
  449. if (!(option_mask32 & OPT_USE_ICMP)) {
  450. out = outdata;
  451. len -= sizeof(*outudp);
  452. set_nport(dest_lsa, htons(port + seq));
  453. }
  454. }
  455. res = xsendto(sndsock, out, len, &dest_lsa->u.sa, dest_lsa->len);
  456. if (res != len)
  457. bb_info_msg("sent %d octets, ret=%d", len, res);
  458. }
  459. #if ENABLE_FEATURE_TRACEROUTE_VERBOSE
  460. /*
  461. * Convert an ICMP "type" field to a printable string.
  462. */
  463. static const char *
  464. pr_type(unsigned char t)
  465. {
  466. static const char *const ttab[] = {
  467. "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable",
  468. "Source Quench", "Redirect", "ICMP 6", "ICMP 7",
  469. "Echo", "Router Advert", "Router Solicit", "Time Exceeded",
  470. "Param Problem", "Timestamp", "Timestamp Reply", "Info Request",
  471. "Info Reply", "Mask Request", "Mask Reply"
  472. };
  473. # if ENABLE_TRACEROUTE6
  474. static const char *const ttab6[] = {
  475. [0] "Error", "Dest Unreachable", "Packet Too Big", "Time Exceeded",
  476. [4] "Param Problem",
  477. [8] "Echo Request", "Echo Reply", "Membership Query", "Membership Report",
  478. [12] "Membership Reduction", "Router Solicit", "Router Advert", "Neighbor Solicit",
  479. [16] "Neighbor Advert", "Redirect",
  480. };
  481. if (dest_lsa->u.sa.sa_family == AF_INET6) {
  482. if (t < 5)
  483. return ttab6[t];
  484. if (t < 128 || t > ND_REDIRECT)
  485. return "OUT-OF-RANGE";
  486. return ttab6[(t & 63) + 8];
  487. }
  488. # endif
  489. if (t >= ARRAY_SIZE(ttab))
  490. return "OUT-OF-RANGE";
  491. return ttab[t];
  492. }
  493. #endif
  494. #if !ENABLE_FEATURE_TRACEROUTE_VERBOSE
  495. #define packet4_ok(read_len, from, seq) \
  496. packet4_ok(read_len, seq)
  497. #endif
  498. static int
  499. packet4_ok(int read_len, const struct sockaddr_in *from, int seq)
  500. {
  501. const struct icmp *icp;
  502. unsigned char type, code;
  503. int hlen;
  504. const struct ip *ip;
  505. ip = (struct ip *) recv_pkt;
  506. hlen = ip->ip_hl << 2;
  507. if (read_len < hlen + ICMP_MINLEN) {
  508. #if ENABLE_FEATURE_TRACEROUTE_VERBOSE
  509. if (verbose)
  510. printf("packet too short (%d bytes) from %s\n", read_len,
  511. inet_ntoa(from->sin_addr));
  512. #endif
  513. return 0;
  514. }
  515. read_len -= hlen;
  516. icp = (struct icmp *)(recv_pkt + hlen);
  517. type = icp->icmp_type;
  518. code = icp->icmp_code;
  519. /* Path MTU Discovery (RFC1191) */
  520. pmtu = 0;
  521. if (code == ICMP_UNREACH_NEEDFRAG)
  522. pmtu = ntohs(icp->icmp_nextmtu);
  523. if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS)
  524. || type == ICMP_UNREACH
  525. || type == ICMP_ECHOREPLY
  526. ) {
  527. const struct ip *hip;
  528. const struct udphdr *up;
  529. hip = &icp->icmp_ip;
  530. hlen = hip->ip_hl << 2;
  531. if (option_mask32 & OPT_USE_ICMP) {
  532. struct icmp *hicmp;
  533. /* XXX */
  534. if (type == ICMP_ECHOREPLY
  535. && icp->icmp_id == htons(ident)
  536. && icp->icmp_seq == htons(seq)
  537. ) {
  538. return ICMP_UNREACH_PORT+1;
  539. }
  540. hicmp = (struct icmp *)((unsigned char *)hip + hlen);
  541. if (hlen + SIZEOF_ICMP_HDR <= read_len
  542. && hip->ip_p == IPPROTO_ICMP
  543. && hicmp->icmp_id == htons(ident)
  544. && hicmp->icmp_seq == htons(seq)
  545. ) {
  546. return (type == ICMP_TIMXCEED ? -1 : code + 1);
  547. }
  548. } else {
  549. up = (struct udphdr *)((char *)hip + hlen);
  550. if (hlen + 12 <= read_len
  551. && hip->ip_p == IPPROTO_UDP
  552. // Off: since we do not form the entire IP packet,
  553. // but defer it to kernel, we can't set source port,
  554. // and thus can't check it here in the reply
  555. /* && up->source == htons(ident) */
  556. && up->dest == htons(port + seq)
  557. ) {
  558. return (type == ICMP_TIMXCEED ? -1 : code + 1);
  559. }
  560. }
  561. }
  562. #if ENABLE_FEATURE_TRACEROUTE_VERBOSE
  563. if (verbose) {
  564. int i;
  565. uint32_t *lp = (uint32_t *)&icp->icmp_ip;
  566. printf("\n%d bytes from %s to "
  567. "%s: icmp type %d (%s) code %d\n",
  568. read_len, inet_ntoa(from->sin_addr),
  569. inet_ntoa(ip->ip_dst),
  570. type, pr_type(type), icp->icmp_code);
  571. for (i = 4; i < read_len; i += sizeof(*lp))
  572. printf("%2d: x%8.8x\n", i, *lp++);
  573. }
  574. #endif
  575. return 0;
  576. }
  577. #if ENABLE_TRACEROUTE6
  578. # if !ENABLE_FEATURE_TRACEROUTE_VERBOSE
  579. #define packet_ok(read_len, from_lsa, to, seq) \
  580. packet_ok(read_len, from_lsa, seq)
  581. # endif
  582. static int
  583. packet_ok(int read_len, len_and_sockaddr *from_lsa,
  584. struct sockaddr *to,
  585. int seq)
  586. {
  587. const struct icmp6_hdr *icp;
  588. unsigned char type, code;
  589. if (from_lsa->u.sa.sa_family == AF_INET)
  590. return packet4_ok(read_len, &from_lsa->u.sin, seq);
  591. icp = (struct icmp6_hdr *) recv_pkt;
  592. type = icp->icmp6_type;
  593. code = icp->icmp6_code;
  594. if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
  595. || type == ICMP6_DST_UNREACH
  596. ) {
  597. struct ip6_hdr *hip;
  598. struct udphdr *up;
  599. int nexthdr;
  600. hip = (struct ip6_hdr *)(icp + 1);
  601. up = (struct udphdr *) (hip + 1);
  602. nexthdr = hip->ip6_nxt;
  603. if (nexthdr == IPPROTO_FRAGMENT) {
  604. nexthdr = *(unsigned char*)up;
  605. up++;
  606. }
  607. if (nexthdr == IPPROTO_UDP) {
  608. struct outdata6_t *pkt;
  609. pkt = (struct outdata6_t *) (up + 1);
  610. if (ntohl(pkt->ident6) == ident
  611. && ntohl(pkt->seq6) == seq
  612. ) {
  613. return (type == ICMP6_TIME_EXCEEDED ? -1 : (code<<8)+1);
  614. }
  615. }
  616. }
  617. # if ENABLE_FEATURE_TRACEROUTE_VERBOSE
  618. if (verbose) {
  619. unsigned char *p;
  620. char pa1[MAXHOSTNAMELEN];
  621. char pa2[MAXHOSTNAMELEN];
  622. int i;
  623. p = (unsigned char *) (icp + 1);
  624. printf("\n%d bytes from %s to "
  625. "%s: icmp type %d (%s) code %d\n",
  626. read_len,
  627. inet_ntop(AF_INET6, &from_lsa->u.sin6.sin6_addr, pa1, sizeof(pa1)),
  628. inet_ntop(AF_INET6, &((struct sockaddr_in6*)to)->sin6_addr, pa2, sizeof(pa2)),
  629. type, pr_type(type), icp->icmp6_code);
  630. read_len -= sizeof(struct icmp6_hdr);
  631. for (i = 0; i < read_len ; i++) {
  632. if (i % 16 == 0)
  633. printf("%04x:", i);
  634. if (i % 4 == 0)
  635. bb_putchar(' ');
  636. printf("%02x", p[i]);
  637. if ((i % 16 == 15) && (i + 1 < read_len))
  638. bb_putchar('\n');
  639. }
  640. bb_putchar('\n');
  641. }
  642. # endif
  643. return 0;
  644. }
  645. #else /* !ENABLE_TRACEROUTE6 */
  646. static ALWAYS_INLINE int
  647. packet_ok(int read_len,
  648. len_and_sockaddr *from_lsa IF_NOT_FEATURE_TRACEROUTE_VERBOSE(UNUSED_PARAM),
  649. struct sockaddr *to UNUSED_PARAM,
  650. int seq)
  651. {
  652. return packet4_ok(read_len, &from_lsa->u.sin, seq);
  653. }
  654. #endif
  655. /*
  656. * Construct an Internet address representation.
  657. * If the -n flag has been supplied, give
  658. * numeric value, otherwise try for symbolic name.
  659. */
  660. static void
  661. print_inetname(const struct sockaddr *from)
  662. {
  663. char *ina = xmalloc_sockaddr2dotted_noport(from);
  664. if (option_mask32 & OPT_ADDR_NUM) {
  665. printf(" %s", ina);
  666. } else {
  667. char *n = NULL;
  668. if (from->sa_family != AF_INET
  669. || ((struct sockaddr_in*)from)->sin_addr.s_addr != INADDR_ANY
  670. ) {
  671. /* Try to reverse resolve if it is not 0.0.0.0 */
  672. n = xmalloc_sockaddr2host_noport((struct sockaddr*)from);
  673. }
  674. printf(" %s (%s)", (n ? n : ina), ina);
  675. free(n);
  676. }
  677. free(ina);
  678. }
  679. static void
  680. print(int read_len, const struct sockaddr *from, const struct sockaddr *to)
  681. {
  682. print_inetname(from);
  683. if (verbose) {
  684. char *ina = xmalloc_sockaddr2dotted_noport(to);
  685. #if ENABLE_TRACEROUTE6
  686. if (to->sa_family == AF_INET6) {
  687. read_len -= sizeof(struct ip6_hdr);
  688. } else
  689. #endif
  690. {
  691. read_len -= ((struct ip*)recv_pkt)->ip_hl << 2;
  692. }
  693. printf(" %d bytes to %s", read_len, ina);
  694. free(ina);
  695. }
  696. }
  697. static void
  698. print_delta_ms(unsigned t1p, unsigned t2p)
  699. {
  700. unsigned tt = t2p - t1p;
  701. printf(" %u.%03u ms", tt / 1000, tt % 1000);
  702. }
  703. /*
  704. * Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl]
  705. * [-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos]
  706. * [-w waittime] [-z pausemsecs] host [packetlen]"
  707. */
  708. static int
  709. common_traceroute_main(int op, char **argv)
  710. {
  711. int i;
  712. int minpacket;
  713. int tos = 0;
  714. int max_ttl = 30;
  715. int nprobes = 3;
  716. int first_ttl = 1;
  717. unsigned pausemsecs = 0;
  718. char *source;
  719. char *device;
  720. char *tos_str;
  721. char *max_ttl_str;
  722. char *port_str;
  723. char *nprobes_str;
  724. char *waittime_str;
  725. char *pausemsecs_str;
  726. char *first_ttl_str;
  727. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  728. llist_t *source_route_list = NULL;
  729. int lsrr = 0;
  730. #endif
  731. #if ENABLE_TRACEROUTE6
  732. sa_family_t af;
  733. #else
  734. enum { af = AF_INET };
  735. #endif
  736. int ttl;
  737. int seq;
  738. len_and_sockaddr *from_lsa;
  739. struct sockaddr *lastaddr;
  740. struct sockaddr *to;
  741. INIT_G();
  742. /* minimum 1 arg */
  743. opt_complementary = "-1:x-x" IF_FEATURE_TRACEROUTE_SOURCE_ROUTE(":g::");
  744. op |= getopt32(argv, OPT_STRING
  745. , &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str
  746. , &source, &waittime_str, &pausemsecs_str, &first_ttl_str
  747. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  748. , &source_route_list
  749. #endif
  750. );
  751. argv += optind;
  752. #if 0 /* IGNORED */
  753. if (op & OPT_IP_CHKSUM)
  754. bb_error_msg("warning: ip checksums disabled");
  755. #endif
  756. if (op & OPT_TOS)
  757. tos = xatou_range(tos_str, 0, 255);
  758. if (op & OPT_MAX_TTL)
  759. max_ttl = xatou_range(max_ttl_str, 1, 255);
  760. if (op & OPT_PORT)
  761. port = xatou16(port_str);
  762. if (op & OPT_NPROBES)
  763. nprobes = xatou_range(nprobes_str, 1, INT_MAX);
  764. if (op & OPT_SOURCE) {
  765. /*
  766. * set the ip source address of the outbound
  767. * probe (e.g., on a multi-homed host).
  768. */
  769. if (getuid() != 0)
  770. bb_error_msg_and_die(bb_msg_you_must_be_root);
  771. }
  772. if (op & OPT_WAITTIME)
  773. waittime = xatou_range(waittime_str, 1, 24 * 60 * 60);
  774. if (op & OPT_PAUSE_MS)
  775. pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000);
  776. if (op & OPT_FIRST_TTL)
  777. first_ttl = xatou_range(first_ttl_str, 1, max_ttl);
  778. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
  779. if (source_route_list) {
  780. while (source_route_list) {
  781. len_and_sockaddr *lsa;
  782. if (lsrr >= NGATEWAYS)
  783. bb_error_msg_and_die("no more than %d gateways", NGATEWAYS);
  784. lsa = xhost_and_af2sockaddr(llist_pop(&source_route_list), 0, AF_INET);
  785. gwlist[lsrr] = lsa->u.sin.sin_addr.s_addr;
  786. free(lsa);
  787. ++lsrr;
  788. }
  789. optlen = (lsrr + 1) * sizeof(gwlist[0]);
  790. }
  791. #endif
  792. /* Process destination and optional packet size */
  793. minpacket = sizeof(*outip) + SIZEOF_ICMP_HDR + sizeof(*outdata) + optlen;
  794. if (!(op & OPT_USE_ICMP))
  795. minpacket += sizeof(*outudp) - SIZEOF_ICMP_HDR;
  796. #if ENABLE_TRACEROUTE6
  797. af = AF_UNSPEC;
  798. if (op & OPT_IPV4)
  799. af = AF_INET;
  800. if (op & OPT_IPV6)
  801. af = AF_INET6;
  802. dest_lsa = xhost_and_af2sockaddr(argv[0], port, af);
  803. af = dest_lsa->u.sa.sa_family;
  804. if (af == AF_INET6)
  805. minpacket = sizeof(struct outdata6_t);
  806. #else
  807. dest_lsa = xhost2sockaddr(argv[0], port);
  808. #endif
  809. packlen = minpacket;
  810. if (argv[1])
  811. packlen = xatoul_range(argv[1], minpacket, 32 * 1024);
  812. /* Ensure the socket fds won't be 0, 1 or 2 */
  813. bb_sanitize_stdio();
  814. #if ENABLE_TRACEROUTE6
  815. if (af == AF_INET6) {
  816. xmove_fd(xsocket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6), rcvsock);
  817. # ifdef IPV6_RECVPKTINFO
  818. setsockopt(rcvsock, SOL_IPV6, IPV6_RECVPKTINFO,
  819. &const_int_1, sizeof(const_int_1));
  820. setsockopt(rcvsock, SOL_IPV6, IPV6_2292PKTINFO,
  821. &const_int_1, sizeof(const_int_1));
  822. # else
  823. setsockopt(rcvsock, SOL_IPV6, IPV6_PKTINFO,
  824. &const_int_1, sizeof(const_int_1));
  825. # endif
  826. } else
  827. #endif
  828. {
  829. xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), rcvsock);
  830. }
  831. #if TRACEROUTE_SO_DEBUG
  832. if (op & OPT_DEBUG)
  833. setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
  834. &const_int_1, sizeof(const_int_1));
  835. #endif
  836. if (op & OPT_BYPASS_ROUTE)
  837. setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
  838. &const_int_1, sizeof(const_int_1));
  839. #if ENABLE_TRACEROUTE6
  840. if (af == AF_INET6) {
  841. static const int two = 2;
  842. if (setsockopt(rcvsock, SOL_RAW, IPV6_CHECKSUM, &two, sizeof(two)) < 0)
  843. bb_perror_msg_and_die("setsockopt RAW_CHECKSUM");
  844. xmove_fd(xsocket(af, SOCK_DGRAM, 0), sndsock);
  845. } else
  846. #endif
  847. {
  848. if (op & OPT_USE_ICMP)
  849. xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), sndsock);
  850. else
  851. xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sndsock);
  852. #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE && defined IP_OPTIONS
  853. if (lsrr > 0) {
  854. unsigned char optlist[MAX_IPOPTLEN];
  855. /* final hop */
  856. gwlist[lsrr] = dest_lsa->u.sin.sin_addr.s_addr;
  857. ++lsrr;
  858. /* force 4 byte alignment */
  859. optlist[0] = IPOPT_NOP;
  860. /* loose source route option */
  861. optlist[1] = IPOPT_LSRR;
  862. i = lsrr * sizeof(gwlist[0]);
  863. optlist[2] = i + 3;
  864. /* pointer to LSRR addresses */
  865. optlist[3] = IPOPT_MINOFF;
  866. memcpy(optlist + 4, gwlist, i);
  867. if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS,
  868. (char *)optlist, i + sizeof(gwlist[0])) < 0) {
  869. bb_perror_msg_and_die("IP_OPTIONS");
  870. }
  871. }
  872. #endif
  873. }
  874. #ifdef SO_SNDBUF
  875. if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) {
  876. bb_perror_msg_and_die("SO_SNDBUF");
  877. }
  878. #endif
  879. #ifdef IP_TOS
  880. if ((op & OPT_TOS) && setsockopt(sndsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
  881. bb_perror_msg_and_die("setsockopt tos %d", tos);
  882. }
  883. #endif
  884. #ifdef IP_DONTFRAG
  885. if (op & OPT_DONT_FRAGMNT)
  886. setsockopt(sndsock, IPPROTO_IP, IP_DONTFRAG,
  887. &const_int_1, sizeof(const_int_1));
  888. #endif
  889. #if TRACEROUTE_SO_DEBUG
  890. if (op & OPT_DEBUG)
  891. setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
  892. &const_int_1, sizeof(const_int_1));
  893. #endif
  894. if (op & OPT_BYPASS_ROUTE)
  895. setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
  896. &const_int_1, sizeof(const_int_1));
  897. outip = xzalloc(packlen);
  898. ident = getpid();
  899. if (af == AF_INET) {
  900. if (op & OPT_USE_ICMP) {
  901. ident |= 0x8000;
  902. outicmp->icmp_type = ICMP_ECHO;
  903. outicmp->icmp_id = htons(ident);
  904. outdata = (struct outdata_t *)((char *)outicmp + SIZEOF_ICMP_HDR);
  905. } else {
  906. outdata = (struct outdata_t *)(outudp + 1);
  907. }
  908. }
  909. if (op & OPT_DEVICE) /* hmm, do we need error check? */
  910. setsockopt_bindtodevice(sndsock, device);
  911. if (op & OPT_SOURCE) {
  912. #if ENABLE_TRACEROUTE6
  913. // TODO: need xdotted_and_af2sockaddr?
  914. len_and_sockaddr *source_lsa = xhost_and_af2sockaddr(source, 0, af);
  915. #else
  916. len_and_sockaddr *source_lsa = xdotted2sockaddr(source, 0);
  917. #endif
  918. /* Ping4 does this (why?) */
  919. if (af == AF_INET)
  920. if (setsockopt(sndsock, IPPROTO_IP, IP_MULTICAST_IF,
  921. &source_lsa->u.sa, source_lsa->len))
  922. bb_error_msg_and_die("can't set multicast source interface");
  923. //TODO: we can query source port we bound to,
  924. // and check it in replies... if we care enough
  925. xbind(sndsock, &source_lsa->u.sa, source_lsa->len);
  926. free(source_lsa);
  927. }
  928. #if ENABLE_TRACEROUTE6
  929. else if (af == AF_INET6) {
  930. //TODO: why we don't do it for IPv4?
  931. len_and_sockaddr *source_lsa;
  932. int probe_fd = xsocket(af, SOCK_DGRAM, 0);
  933. if (op & OPT_DEVICE)
  934. setsockopt_bindtodevice(probe_fd, device);
  935. set_nport(dest_lsa, htons(1025));
  936. /* dummy connect. makes kernel pick source IP (and port) */
  937. xconnect(probe_fd, &dest_lsa->u.sa, dest_lsa->len);
  938. set_nport(dest_lsa, htons(port));
  939. /* read IP and port */
  940. source_lsa = get_sock_lsa(probe_fd);
  941. if (source_lsa == NULL)
  942. bb_error_msg_and_die("can't get probe addr");
  943. close(probe_fd);
  944. /* bind our sockets to this IP (but not port) */
  945. set_nport(source_lsa, 0);
  946. xbind(sndsock, &source_lsa->u.sa, source_lsa->len);
  947. xbind(rcvsock, &source_lsa->u.sa, source_lsa->len);
  948. free(source_lsa);
  949. }
  950. #endif
  951. /* Revert to non-privileged user after opening sockets */
  952. xsetgid(getgid());
  953. xsetuid(getuid());
  954. printf("traceroute to %s (%s)", argv[0],
  955. xmalloc_sockaddr2dotted_noport(&dest_lsa->u.sa));
  956. if (op & OPT_SOURCE)
  957. printf(" from %s", source);
  958. printf(", %d hops max, %d byte packets\n", max_ttl, packlen);
  959. from_lsa = dup_sockaddr(dest_lsa);
  960. lastaddr = xzalloc(dest_lsa->len);
  961. to = xzalloc(dest_lsa->len);
  962. seq = 0;
  963. for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
  964. int probe;
  965. int unreachable = 0; /* counter */
  966. int gotlastaddr = 0; /* flags */
  967. int got_there = 0;
  968. int first = 1;
  969. printf("%2d", ttl);
  970. for (probe = 0; probe < nprobes; ++probe) {
  971. int read_len;
  972. unsigned t1;
  973. unsigned t2;
  974. struct ip *ip;
  975. if (!first && pausemsecs > 0)
  976. usleep(pausemsecs * 1000);
  977. fflush_all();
  978. t1 = monotonic_us();
  979. send_probe(++seq, ttl);
  980. first = 0;
  981. while ((read_len = wait_for_reply(from_lsa, to)) != 0) {
  982. t2 = monotonic_us();
  983. i = packet_ok(read_len, from_lsa, to, seq);
  984. /* Skip short packet */
  985. if (i == 0)
  986. continue;
  987. if (!gotlastaddr
  988. || (memcmp(lastaddr, &from_lsa->u.sa, from_lsa->len) != 0)
  989. ) {
  990. print(read_len, &from_lsa->u.sa, to);
  991. memcpy(lastaddr, &from_lsa->u.sa, from_lsa->len);
  992. gotlastaddr = 1;
  993. }
  994. print_delta_ms(t1, t2);
  995. ip = (struct ip *)recv_pkt;
  996. if (from_lsa->u.sa.sa_family == AF_INET)
  997. if (op & OPT_TTL_FLAG)
  998. printf(" (%d)", ip->ip_ttl);
  999. /* time exceeded in transit */
  1000. if (i == -1)
  1001. break;
  1002. i--;
  1003. switch (i) {
  1004. #if ENABLE_TRACEROUTE6
  1005. case ICMP6_DST_UNREACH_NOPORT << 8:
  1006. got_there = 1;
  1007. break;
  1008. #endif
  1009. case ICMP_UNREACH_PORT:
  1010. if (ip->ip_ttl <= 1)
  1011. printf(" !");
  1012. got_there = 1;
  1013. break;
  1014. case ICMP_UNREACH_NET:
  1015. #if ENABLE_TRACEROUTE6 && (ICMP6_DST_UNREACH_NOROUTE != ICMP_UNREACH_NET)
  1016. case ICMP6_DST_UNREACH_NOROUTE << 8:
  1017. #endif
  1018. printf(" !N");
  1019. ++unreachable;
  1020. break;
  1021. case ICMP_UNREACH_HOST:
  1022. #if ENABLE_TRACEROUTE6
  1023. case ICMP6_DST_UNREACH_ADDR << 8:
  1024. #endif
  1025. printf(" !H");
  1026. ++unreachable;
  1027. break;
  1028. case ICMP_UNREACH_PROTOCOL:
  1029. printf(" !P");
  1030. got_there = 1;
  1031. break;
  1032. case ICMP_UNREACH_NEEDFRAG:
  1033. printf(" !F-%d", pmtu);
  1034. ++unreachable;
  1035. break;
  1036. case ICMP_UNREACH_SRCFAIL:
  1037. #if ENABLE_TRACEROUTE6
  1038. case ICMP6_DST_UNREACH_ADMIN << 8:
  1039. #endif
  1040. printf(" !S");
  1041. ++unreachable;
  1042. break;
  1043. case ICMP_UNREACH_FILTER_PROHIB:
  1044. case ICMP_UNREACH_NET_PROHIB: /* misuse */
  1045. printf(" !A");
  1046. ++unreachable;
  1047. break;
  1048. case ICMP_UNREACH_HOST_PROHIB:
  1049. printf(" !C");
  1050. ++unreachable;
  1051. break;
  1052. case ICMP_UNREACH_HOST_PRECEDENCE:
  1053. printf(" !V");
  1054. ++unreachable;
  1055. break;
  1056. case ICMP_UNREACH_PRECEDENCE_CUTOFF:
  1057. printf(" !C");
  1058. ++unreachable;
  1059. break;
  1060. case ICMP_UNREACH_NET_UNKNOWN:
  1061. case ICMP_UNREACH_HOST_UNKNOWN:
  1062. printf(" !U");
  1063. ++unreachable;
  1064. break;
  1065. case ICMP_UNREACH_ISOLATED:
  1066. printf(" !I");
  1067. ++unreachable;
  1068. break;
  1069. case ICMP_UNREACH_TOSNET:
  1070. case ICMP_UNREACH_TOSHOST:
  1071. printf(" !T");
  1072. ++unreachable;
  1073. break;
  1074. default:
  1075. printf(" !<%d>", i);
  1076. ++unreachable;
  1077. break;
  1078. }
  1079. break;
  1080. }
  1081. /* there was no packet at all? */
  1082. if (read_len == 0)
  1083. printf(" *");
  1084. }
  1085. bb_putchar('\n');
  1086. if (got_there
  1087. || (unreachable > 0 && unreachable >= nprobes - 1)
  1088. ) {
  1089. break;
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1095. int traceroute_main(int argc UNUSED_PARAM, char **argv)
  1096. {
  1097. return common_traceroute_main(0, argv);
  1098. }
  1099. #if ENABLE_TRACEROUTE6
  1100. int traceroute6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1101. int traceroute6_main(int argc UNUSED_PARAM, char **argv)
  1102. {
  1103. return common_traceroute_main(OPT_IPV6, argv);
  1104. }
  1105. #endif