Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000 |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 6 | * Busybox port by Vladimir Oleynik (C) 2005 <dzo@simtreas.ru> |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 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. |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * traceroute host - trace the route ip packets follow going to "host". |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 27 | * |
| 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 Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 142 | * 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 148 | * 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 Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 177 | * 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 192 | * -- Van Jacobson (van@ee.lbl.gov) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 193 | * Tue Dec 20 03:50:13 PST 1988 |
| 194 | */ |
| 195 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 196 | #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 Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 201 | //#define CONFIG_FEATURE_TRACEROUTE_VERBOSE |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 202 | //#undef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 203 | //#define CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 204 | //#undef CONFIG_FEATURE_TRACEROUTE_USE_ICMP |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 205 | //#define CONFIG_FEATURE_TRACEROUTE_USE_ICMP |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 206 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 207 | |
| 208 | #include <net/if.h> |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 209 | #include <arpa/inet.h> |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 210 | #include <netinet/in.h> |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 211 | #include <netinet/udp.h> |
| 212 | #include <netinet/ip.h> |
| 213 | #include <netinet/ip_icmp.h> |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 214 | #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 Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 222 | #include "libbb.h" |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 223 | #include "inet_common.h" |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 225 | #ifndef IPPROTO_ICMP |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 226 | # define IPPROTO_ICMP 1 |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifndef IPPROTO_IP |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 229 | # define IPPROTO_IP 0 |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 230 | #endif |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 231 | |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 232 | |
| 233 | #define OPT_STRING "FIlnrdvxt:i:m:p:q:s:w:z:f:" \ |
| 234 | IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:") |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 235 | enum { |
| 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 253 | }; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 254 | #define verbose (option_mask32 & OPT_VERBOSE) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 256 | enum { |
| 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | /* Data section of the probe packet */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 263 | struct outdata_t { |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 264 | unsigned char seq; /* sequence number of this packet */ |
| 265 | unsigned char ttl; /* ttl packet left with */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 266 | // UNUSED. Retaining to have the same packet size. |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 267 | struct timeval tv_UNUSED PACKED; /* time packet left */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 270 | struct globals { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 271 | struct ip *outip; |
| 272 | struct outdata_t *outdata; |
| 273 | len_and_sockaddr *dest_lsa; |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 274 | int packlen; /* total length of packet */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 275 | int pmtu; /* Path MTU Discovery (RFC1191) */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 276 | uint16_t ident; |
| 277 | uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 278 | int waittime; // 5; /* time to wait for response (in seconds) */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 279 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
| 280 | int optlen; /* length of ip options */ |
| 281 | #else |
| 282 | #define optlen 0 |
| 283 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 284 | unsigned char recv_pkt[512]; /* last inbound (icmp) packet */ |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 285 | #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 Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 294 | #define outip (G.outip ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 295 | #define outdata (G.outdata ) |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 296 | #define dest_lsa (G.dest_lsa ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 297 | #define packlen (G.packlen ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 298 | #define pmtu (G.pmtu ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 299 | #define ident (G.ident ) |
| 300 | #define port (G.port ) |
| 301 | #define waittime (G.waittime ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 302 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 303 | # define optlen (G.optlen ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 304 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 305 | #define recv_pkt (G.recv_pkt ) |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 306 | #define gwlist (G.gwlist ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 307 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 308 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 309 | port = 32768 + 666; \ |
| 310 | waittime = 5; \ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 311 | } while (0) |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 312 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 313 | #define outicmp ((struct icmp *)(outip + 1)) |
| 314 | #define outudp ((struct udphdr *)(outip + 1)) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 315 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 316 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 317 | static int |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 318 | wait_for_reply(struct sockaddr_in *fromp) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 319 | { |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 320 | struct pollfd pfd[1]; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 321 | int read_len = 0; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 322 | socklen_t fromlen = sizeof(*fromp); |
| 323 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 324 | pfd[0].fd = rcvsock; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 325 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 326 | if (safe_poll(pfd, 1, waittime * 1000) > 0) |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 327 | read_len = recvfrom(rcvsock, recv_pkt, sizeof(recv_pkt), 0, |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 328 | (struct sockaddr *)fromp, &fromlen); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 329 | return read_len; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Checksum routine for Internet Protocol family headers (C Version) |
| 334 | */ |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 335 | static uint16_t |
| 336 | in_cksum(uint16_t *addr, int len) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 337 | { |
| 338 | int nleft = len; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 339 | uint16_t *w = addr; |
| 340 | uint16_t answer; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 341 | int sum = 0; |
| 342 | |
| 343 | /* |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 344 | * 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 348 | */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 349 | while (nleft > 1) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 350 | sum += *w++; |
| 351 | nleft -= 2; |
| 352 | } |
| 353 | |
| 354 | /* mop up an odd byte, if necessary */ |
| 355 | if (nleft == 1) |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 356 | sum += *(unsigned char *)w; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 358 | /* add back carry outs from top 16 bits to low 16 bits */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 359 | 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 365 | static void |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 366 | send_probe(int seq, int ttl) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 367 | { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 368 | int len, res; |
| 369 | void *out; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 370 | |
| 371 | /* Payload */ |
| 372 | outdata->seq = seq; |
| 373 | outdata->ttl = ttl; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 374 | // UNUSED: was storing gettimeofday's result there, but never ever checked it |
| 375 | /*memcpy(&outdata->tv, tp, sizeof(outdata->tv));*/ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 376 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 377 | if (option_mask32 & OPT_USE_ICMP) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 378 | outicmp->icmp_seq = htons(seq); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 379 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 380 | /* Always calculate checksum for icmp packets */ |
| 381 | outicmp->icmp_cksum = 0; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 382 | outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp, |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 383 | packlen - (sizeof(*outip) + optlen)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 384 | if (outicmp->icmp_cksum == 0) |
| 385 | outicmp->icmp_cksum = 0xffff; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 388 | //BUG! verbose is (x & OPT_VERBOSE), not a counter! |
| 389 | #if 0 //ENABLE_FEATURE_TRACEROUTE_VERBOSE |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 390 | /* XXX undocumented debugging hack */ |
| 391 | if (verbose > 1) { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 392 | const uint16_t *sp; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 393 | int nshorts, i; |
| 394 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 395 | sp = (uint16_t *)outip; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 396 | nshorts = (unsigned)packlen / sizeof(uint16_t); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 397 | 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 Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 408 | printf(" %02x", *(unsigned char *)sp); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 409 | } |
| 410 | printf("]\n"); |
| 411 | } |
| 412 | #endif |
| 413 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 414 | #if defined(IP_TTL) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 415 | if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 416 | (char *)&ttl, sizeof(ttl)) < 0) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 417 | bb_perror_msg_and_die("setsockopt ttl %d", ttl); |
| 418 | } |
| 419 | #endif |
| 420 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 421 | 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 Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 429 | res = xsendto(sndsock, out, len, &dest_lsa->u.sa, dest_lsa->len); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 430 | if (res != len) { |
| 431 | bb_info_msg("sent %d octets, ret=%d", len, res); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 432 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 435 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 436 | /* |
| 437 | * Convert an ICMP "type" field to a printable string. |
| 438 | */ |
| 439 | static inline const char * |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 440 | pr_type(unsigned char t) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 441 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 442 | static const char *const ttab[] = { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 443 | "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable", |
| 444 | "Source Quench", "Redirect", "ICMP 6", "ICMP 7", |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 445 | "Echo", "Router Advert", "Router Solicit", "Time Exceeded", |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 446 | "Param Problem", "Timestamp", "Timestamp Reply", "Info Request", |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 447 | "Info Reply", "Mask Request", "Mask Reply" |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 450 | if (t >= ARRAY_SIZE(ttab)) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 451 | return "OUT-OF-RANGE"; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 452 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 453 | return ttab[t]; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 454 | } |
| 455 | #endif |
| 456 | |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 457 | #if !ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 458 | #define packet_ok(read_len, from, seq) \ |
| 459 | packet_ok(read_len, seq) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 460 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 461 | static int |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 462 | packet_ok(int read_len, const struct sockaddr_in *from, int seq) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 463 | { |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 464 | const struct icmp *icp; |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 465 | unsigned char type, code; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 466 | int hlen; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 467 | const struct ip *ip; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 468 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 469 | ip = (struct ip *) recv_pkt; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 470 | hlen = ip->ip_hl << 2; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 471 | if (read_len < hlen + ICMP_MINLEN) { |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 472 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 473 | if (verbose) |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 474 | printf("packet too short (%d bytes) from %s\n", read_len, |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 475 | inet_ntoa(from->sin_addr)); |
| 476 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 477 | return 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 478 | } |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 479 | read_len -= hlen; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 480 | icp = (struct icmp *)(recv_pkt + hlen); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 481 | type = icp->icmp_type; |
| 482 | code = icp->icmp_code; |
| 483 | /* Path MTU Discovery (RFC1191) */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 484 | pmtu = 0; |
| 485 | if (code == ICMP_UNREACH_NEEDFRAG) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 486 | pmtu = ntohs(icp->icmp_nextmtu); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 487 | |
| 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 Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 494 | |
| 495 | hip = &icp->icmp_ip; |
| 496 | hlen = hip->ip_hl << 2; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 497 | if (option_mask32 & OPT_USE_ICMP) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 498 | struct icmp *hicmp; |
| 499 | |
| 500 | /* XXX */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 501 | if (type == ICMP_ECHOREPLY |
| 502 | && icp->icmp_id == htons(ident) |
| 503 | && icp->icmp_seq == htons(seq) |
| 504 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 505 | return -2; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 506 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 507 | |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 508 | hicmp = (struct icmp *)((unsigned char *)hip + hlen); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 509 | if (hlen + SIZEOF_ICMP_HDR <= read_len |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 510 | && hip->ip_p == IPPROTO_ICMP |
| 511 | && hicmp->icmp_id == htons(ident) |
| 512 | && hicmp->icmp_seq == htons(seq) |
| 513 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 514 | return (type == ICMP_TIMXCEED ? -1 : code + 1); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 515 | } |
| 516 | } else { |
| 517 | up = (struct udphdr *)((char *)hip + hlen); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 518 | if (hlen + 12 <= read_len |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 519 | && 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 526 | return (type == ICMP_TIMXCEED ? -1 : code + 1); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 527 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 528 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 529 | } |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 530 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 531 | if (verbose) { |
| 532 | int i; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 533 | uint32_t *lp = (uint32_t *)&icp->icmp_ip; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 534 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 535 | printf("\n%d bytes from %s to " |
| 536 | "%s: icmp type %d (%s) code %d\n", |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 537 | read_len, inet_ntoa(from->sin_addr), |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 538 | inet_ntoa(ip->ip_dst), |
| 539 | type, pr_type(type), icp->icmp_code); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 540 | for (i = 4; i < read_len; i += sizeof(*lp)) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 541 | printf("%2d: x%8.8x\n", i, *lp++); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 542 | } |
| 543 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 544 | return 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 545 | } |
| 546 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 547 | /* |
| 548 | * Construct an Internet address representation. |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 549 | * If the -n flag has been supplied, give |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 550 | * numeric value, otherwise try for symbolic name. |
| 551 | */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 552 | static void |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 553 | print_inetname(const struct sockaddr *from) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 554 | { |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 555 | char *ina = xmalloc_sockaddr2dotted_noport(from); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 556 | |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 557 | if (option_mask32 & OPT_ADDR_NUM) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 558 | printf(" %s", ina); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 559 | } else { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 560 | char *n = NULL; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 561 | |
| 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 Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 566 | n = xmalloc_sockaddr2host_noport((struct sockaddr*)from); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 567 | } |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 568 | printf(" %s (%s)", (n ? n : ina), ina); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 569 | free(n); |
| 570 | } |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 571 | free(ina); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 574 | static void |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 575 | print(int read_len, const struct sockaddr_in *from) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 576 | { |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 577 | print_inetname((const struct sockaddr*)from); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 578 | if (verbose) { |
| 579 | const struct ip *ip; |
| 580 | int hlen; |
| 581 | |
| 582 | ip = (struct ip *) recv_pkt; |
| 583 | hlen = ip->ip_hl << 2; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 584 | read_len -= hlen; |
| 585 | printf(" %d bytes to %s", read_len, inet_ntoa(ip->ip_dst)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 586 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static void |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 590 | print_delta_ms(unsigned t1p, unsigned t2p) |
Denis Vlasenko | 1adf681 | 2007-06-14 07:47:52 +0000 | [diff] [blame] | 591 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 592 | unsigned tt = t2p - t1p; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 593 | printf(" %u.%03u ms", tt / 1000, tt % 1000); |
Denis Vlasenko | 1adf681 | 2007-06-14 07:47:52 +0000 | [diff] [blame] | 594 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 595 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 596 | /* |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 597 | * 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 Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 601 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 602 | int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 603 | int traceroute_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 604 | { |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 605 | int i; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 606 | int minpacket; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 607 | int tos = 0; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 608 | int max_ttl = 30; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 609 | 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 Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 616 | char *max_ttl_str; |
| 617 | char *port_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 618 | char *nprobes_str; |
| 619 | char *waittime_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 620 | char *pausemsecs_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 621 | char *first_ttl_str; |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 622 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 623 | llist_t *source_route_list = NULL; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 624 | int lsrr = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 625 | #endif |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 626 | int ttl; |
| 627 | int seq; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 628 | |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 629 | INIT_G(); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 630 | |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 631 | /* minimum 1 arg */ |
| 632 | opt_complementary = "-1:x-x" IF_FEATURE_TRACEROUTE_SOURCE_ROUTE(":g::"); |
| 633 | op = getopt32(argv, OPT_STRING |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 634 | , &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str |
| 635 | , &source, &waittime_str, &pausemsecs_str, &first_ttl_str |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 636 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 637 | , &source_route_list |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 638 | #endif |
| 639 | ); |
| 640 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 641 | #if 0 /* IGNORED */ |
| 642 | if (op & OPT_IP_CHKSUM) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 643 | bb_error_msg("warning: ip checksums disabled"); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 644 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 645 | 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 Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 650 | port = xatou16(port_str); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 651 | if (op & OPT_NPROBES) |
| 652 | nprobes = xatou_range(nprobes_str, 1, INT_MAX); |
| 653 | if (op & OPT_SOURCE) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 654 | /* |
| 655 | * set the ip source address of the outbound |
| 656 | * probe (e.g., on a multi-homed host). |
| 657 | */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 658 | if (getuid() != 0) |
| 659 | bb_error_msg_and_die("you must be root to use -s"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 660 | } |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 661 | if (op & OPT_WAITTIME) |
Denis Vlasenko | bdea2c0 | 2009-01-24 21:23:41 +0000 | [diff] [blame] | 662 | waittime = xatou_range(waittime_str, 1, 24 * 60 * 60); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 663 | if (op & OPT_PAUSE_MS) |
| 664 | pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000); |
| 665 | if (op & OPT_FIRST_TTL) |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 666 | first_ttl = xatou_range(first_ttl_str, 1, max_ttl); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 667 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 668 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 669 | if (source_route_list) { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 670 | while (source_route_list) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 671 | len_and_sockaddr *lsa; |
| 672 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 673 | if (lsrr >= NGATEWAYS) |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 674 | bb_error_msg_and_die("no more than %d gateways", NGATEWAYS); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 675 | 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 678 | ++lsrr; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 679 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 680 | optlen = (lsrr + 1) * sizeof(gwlist[0]); |
| 681 | } |
| 682 | #endif |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 683 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 684 | 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 688 | |
| 689 | /* Process destination and optional packet size */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 690 | argv += optind; |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 691 | if (argv[1]) |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 692 | packlen = xatoul_range(argv[1], minpacket, 32 * 1024); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 693 | dest_lsa = xhost2sockaddr(argv[0], port); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 694 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 695 | /* Ensure the socket fds won't be 0, 1 or 2 */ |
| 696 | bb_sanitize_stdio(); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 697 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 698 | xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), rcvsock); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 699 | #if TRACEROUTE_SO_DEBUG |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 700 | if (op & OPT_DEBUG) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 701 | setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 702 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 703 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 704 | if (op & OPT_BYPASS_ROUTE) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 705 | setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 706 | &const_int_1, sizeof(const_int_1)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 707 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 708 | 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 Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 712 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE && defined IP_OPTIONS |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 713 | if (lsrr > 0) { |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 714 | unsigned char optlist[MAX_IPOPTLEN]; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 715 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 716 | /* final hop */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 717 | gwlist[lsrr] = dest_lsa->u.sin.sin_addr.s_addr; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 718 | ++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 Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 726 | /* pointer to LSRR addresses */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 727 | optlist[3] = IPOPT_MINOFF; |
| 728 | memcpy(optlist + 4, gwlist, i); |
| 729 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 730 | if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS, |
| 731 | (char *)optlist, i + sizeof(gwlist[0])) < 0) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 732 | bb_perror_msg_and_die("IP_OPTIONS"); |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 733 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 734 | } |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 735 | #endif |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 736 | #ifdef SO_SNDBUF |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 737 | if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 738 | bb_perror_msg_and_die("SO_SNDBUF"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 739 | } |
Eric Andersen | ad79c0b | 2002-06-06 12:24:51 +0000 | [diff] [blame] | 740 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 741 | #ifdef IP_TOS |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 742 | if ((op & OPT_TOS) && setsockopt(sndsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 743 | bb_perror_msg_and_die("setsockopt tos %d", tos); |
| 744 | } |
| 745 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 746 | #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 Andersen | ad79c0b | 2002-06-06 12:24:51 +0000 | [diff] [blame] | 750 | #endif |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 751 | #if TRACEROUTE_SO_DEBUG |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 752 | if (op & OPT_DEBUG) |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 753 | setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, |
| 754 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 755 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 756 | if (op & OPT_BYPASS_ROUTE) |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 757 | setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, |
| 758 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 759 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 760 | 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" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 786 | /* Revert to non-privileged user after opening sockets */ |
Rob Landley | afb94ec | 2006-07-16 08:06:34 +0000 | [diff] [blame] | 787 | xsetgid(getgid()); |
| 788 | xsetuid(getuid()); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 789 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 790 | printf("traceroute to %s (%s)", argv[0], |
| 791 | xmalloc_sockaddr2dotted_noport(&dest_lsa->u.sa)); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 792 | if (op & OPT_SOURCE) |
| 793 | printf(" from %s", source); |
| 794 | printf(", %d hops max, %d byte packets\n", max_ttl, packlen); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 795 | |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 796 | seq = 0; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 797 | for (ttl = first_ttl; ttl <= max_ttl; ++ttl) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 798 | //TODO: make it protocol agnostic (get rid of sockaddr_in) |
| 799 | struct sockaddr_in from; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 800 | uint32_t lastaddr = 0; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 801 | int probe; |
| 802 | int unreachable = 0; /* counter */ |
| 803 | int gotlastaddr = 0; /* flags */ |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 804 | int got_there = 0; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 805 | int first = 1; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 806 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 807 | printf("%2d", ttl); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 808 | for (probe = 0; probe < nprobes; ++probe) { |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 809 | int read_len; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 810 | unsigned t1; |
| 811 | unsigned t2; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 812 | struct ip *ip; |
| 813 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 814 | if (!first && pausemsecs > 0) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 815 | usleep(pausemsecs * 1000); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 816 | fflush_all(); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 817 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 818 | t1 = monotonic_us(); |
| 819 | send_probe(++seq, ttl); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 820 | |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 821 | first = 0; |
| 822 | while ((read_len = wait_for_reply(&from)) != 0) { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 823 | t2 = monotonic_us(); |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 824 | i = packet_ok(read_len, &from, seq); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 825 | /* Skip short packet */ |
| 826 | if (i == 0) |
| 827 | continue; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 828 | if (!gotlastaddr |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 829 | || from.sin_addr.s_addr != lastaddr |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 830 | ) { |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 831 | print(read_len, &from); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 832 | lastaddr = from.sin_addr.s_addr; |
| 833 | gotlastaddr = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 834 | } |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 835 | print_delta_ms(t1, t2); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame] | 836 | ip = (struct ip *)recv_pkt; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 837 | if (op & OPT_TTL_FLAG) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 838 | printf(" (%d)", ip->ip_ttl); |
| 839 | if (i == -2) { |
| 840 | if (ip->ip_ttl <= 1) |
| 841 | printf(" !"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 842 | got_there = 1; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 843 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 844 | } |
| 845 | /* time exceeded in transit */ |
| 846 | if (i == -1) |
| 847 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 848 | i--; |
| 849 | switch (i) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 850 | case ICMP_UNREACH_PORT: |
| 851 | if (ip->ip_ttl <= 1) |
| 852 | printf(" !"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 853 | got_there = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 854 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 855 | case ICMP_UNREACH_NET: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 856 | printf(" !N"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 857 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 858 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 859 | case ICMP_UNREACH_HOST: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 860 | printf(" !H"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 861 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 862 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 863 | case ICMP_UNREACH_PROTOCOL: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 864 | printf(" !P"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 865 | got_there = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 866 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 867 | case ICMP_UNREACH_NEEDFRAG: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 868 | printf(" !F-%d", pmtu); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 869 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 870 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 871 | case ICMP_UNREACH_SRCFAIL: |
| 872 | printf(" !S"); |
| 873 | ++unreachable; |
| 874 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 875 | case ICMP_UNREACH_FILTER_PROHIB: |
| 876 | case ICMP_UNREACH_NET_PROHIB: /* misuse */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 877 | printf(" !A"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 878 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 879 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 880 | case ICMP_UNREACH_HOST_PROHIB: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 881 | printf(" !C"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 882 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 883 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 884 | case ICMP_UNREACH_HOST_PRECEDENCE: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 885 | printf(" !V"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 886 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 887 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 888 | case ICMP_UNREACH_PRECEDENCE_CUTOFF: |
| 889 | printf(" !C"); |
| 890 | ++unreachable; |
| 891 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 892 | case ICMP_UNREACH_NET_UNKNOWN: |
| 893 | case ICMP_UNREACH_HOST_UNKNOWN: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 894 | printf(" !U"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 895 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 896 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 897 | case ICMP_UNREACH_ISOLATED: |
| 898 | printf(" !I"); |
| 899 | ++unreachable; |
| 900 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 901 | case ICMP_UNREACH_TOSNET: |
| 902 | case ICMP_UNREACH_TOSHOST: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 903 | printf(" !T"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 904 | ++unreachable; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 905 | break; |
| 906 | default: |
| 907 | printf(" !<%d>", i); |
| 908 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 909 | break; |
| 910 | } |
| 911 | break; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 912 | } |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 913 | /* there was no packet at all? */ |
| 914 | if (read_len == 0) |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 915 | printf(" *"); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 916 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 917 | bb_putchar('\n'); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 918 | if (got_there |
| 919 | || (unreachable > 0 && unreachable >= nprobes - 1) |
| 920 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 921 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 922 | } |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 923 | } |
Denys Vlasenko | 0d56568 | 2009-11-23 06:15:38 +0100 | [diff] [blame^] | 924 | |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 925 | return 0; |
| 926 | } |