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> |
| 214 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 215 | #include "libbb.h" |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 216 | #include "inet_common.h" |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 217 | |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 218 | #ifndef IPPROTO_ICMP |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 219 | # define IPPROTO_ICMP 1 |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 220 | #endif |
| 221 | #ifndef IPPROTO_IP |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 222 | # define IPPROTO_IP 0 |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 223 | #endif |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 225 | /* Keep in sync with getopt32 call! */ |
| 226 | enum { |
| 227 | OPT_DONT_FRAGMNT = (1 << 0), /* F */ |
| 228 | OPT_USE_ICMP = (1 << 1) * ENABLE_FEATURE_TRACEROUTE_USE_ICMP, /* I */ |
| 229 | OPT_TTL_FLAG = (1 << 2), /* l */ |
| 230 | OPT_ADDR_NUM = (1 << 3), /* n */ |
| 231 | OPT_BYPASS_ROUTE = (1 << 4), /* r */ |
| 232 | OPT_DEBUG = (1 << 5), /* d */ |
| 233 | OPT_VERBOSE = (1 << 6) * ENABLE_FEATURE_TRACEROUTE_VERBOSE, /* v */ |
| 234 | OPT_IP_CHKSUM = (1 << 7), /* x */ |
| 235 | OPT_TOS = (1 << 8), /* t */ |
| 236 | OPT_DEVICE = (1 << 9), /* i */ |
| 237 | OPT_MAX_TTL = (1 << 10), /* m */ |
| 238 | OPT_PORT = (1 << 11), /* p */ |
| 239 | OPT_NPROBES = (1 << 12), /* q */ |
| 240 | OPT_SOURCE = (1 << 13), /* s */ |
| 241 | OPT_WAITTIME = (1 << 14), /* w */ |
| 242 | OPT_PAUSE_MS = (1 << 15), /* z */ |
| 243 | OPT_FIRST_TTL = (1 << 16), /* f */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 244 | }; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 245 | #define verbose (option_mask32 & OPT_VERBOSE) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 247 | enum { |
| 248 | SIZEOF_ICMP_HDR = 8, |
| 249 | rcvsock = 3, /* receive (icmp) socket file descriptor */ |
| 250 | sndsock = 4, /* send (udp/icmp) socket file descriptor */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /* Data section of the probe packet */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 254 | struct outdata_t { |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 255 | unsigned char seq; /* sequence number of this packet */ |
| 256 | unsigned char ttl; /* ttl packet left with */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 257 | // UNUSED. Retaining to have the same packet size. |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 258 | struct timeval tv_UNUSED PACKED; /* time packet left */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 261 | struct globals { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 262 | struct ip *outip; |
| 263 | struct outdata_t *outdata; |
| 264 | len_and_sockaddr *dest_lsa; |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 265 | int packlen; /* total length of packet */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 266 | int pmtu; /* Path MTU Discovery (RFC1191) */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 267 | uint16_t ident; |
| 268 | uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 269 | int waittime; // 5; /* time to wait for response (in seconds) */ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 270 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
| 271 | int optlen; /* length of ip options */ |
| 272 | #else |
| 273 | #define optlen 0 |
| 274 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 275 | unsigned char recv_pkt[512]; /* last inbound (icmp) packet */ |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 276 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
| 277 | /* Maximum number of gateways (include room for one noop) */ |
| 278 | #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t))) |
| 279 | /* loose source route gateway list (including room for final destination) */ |
| 280 | uint32_t gwlist[NGATEWAYS + 1]; |
| 281 | #endif |
| 282 | }; |
| 283 | |
| 284 | #define G (*ptr_to_globals) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 285 | #define outip (G.outip ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 286 | #define outdata (G.outdata ) |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 287 | #define dest_lsa (G.dest_lsa ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 288 | #define packlen (G.packlen ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 289 | #define pmtu (G.pmtu ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 290 | #define ident (G.ident ) |
| 291 | #define port (G.port ) |
| 292 | #define waittime (G.waittime ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 293 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 294 | # define optlen (G.optlen ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 295 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 296 | #define recv_pkt (G.recv_pkt ) |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 297 | #define gwlist (G.gwlist ) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 298 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 299 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 300 | port = 32768 + 666; \ |
| 301 | waittime = 5; \ |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 302 | } while (0) |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 304 | #define outicmp ((struct icmp *)(outip + 1)) |
| 305 | #define outudp ((struct udphdr *)(outip + 1)) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 306 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 307 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 308 | static int |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 309 | wait_for_reply(struct sockaddr_in *fromp) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 310 | { |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 311 | struct pollfd pfd[1]; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 312 | int cc = 0; |
| 313 | socklen_t fromlen = sizeof(*fromp); |
| 314 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 315 | pfd[0].fd = rcvsock; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 316 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 317 | if (safe_poll(pfd, 1, waittime * 1000) > 0) |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 318 | cc = recvfrom(rcvsock, recv_pkt, sizeof(recv_pkt), 0, |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 319 | (struct sockaddr *)fromp, &fromlen); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 320 | return cc; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Checksum routine for Internet Protocol family headers (C Version) |
| 325 | */ |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 326 | static uint16_t |
| 327 | in_cksum(uint16_t *addr, int len) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 328 | { |
| 329 | int nleft = len; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 330 | uint16_t *w = addr; |
| 331 | uint16_t answer; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 332 | int sum = 0; |
| 333 | |
| 334 | /* |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 335 | * Our algorithm is simple, using a 32 bit accumulator (sum), |
| 336 | * we add sequential 16 bit words to it, and at the end, fold |
| 337 | * back all the carry bits from the top 16 bits into the lower |
| 338 | * 16 bits. |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 339 | */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 340 | while (nleft > 1) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 341 | sum += *w++; |
| 342 | nleft -= 2; |
| 343 | } |
| 344 | |
| 345 | /* mop up an odd byte, if necessary */ |
| 346 | if (nleft == 1) |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 347 | sum += *(unsigned char *)w; |
"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 | /* 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] | 350 | sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ |
| 351 | sum += (sum >> 16); /* add carry */ |
| 352 | answer = ~sum; /* truncate to 16 bits */ |
| 353 | return answer; |
| 354 | } |
| 355 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 356 | static void |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 357 | send_probe(int seq, int ttl) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 358 | { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 359 | int len, res; |
| 360 | void *out; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 361 | |
| 362 | /* Payload */ |
| 363 | outdata->seq = seq; |
| 364 | outdata->ttl = ttl; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 365 | // UNUSED: was storing gettimeofday's result there, but never ever checked it |
| 366 | /*memcpy(&outdata->tv, tp, sizeof(outdata->tv));*/ |
"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 | if (option_mask32 & OPT_USE_ICMP) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 369 | outicmp->icmp_seq = htons(seq); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 370 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 371 | /* Always calculate checksum for icmp packets */ |
| 372 | outicmp->icmp_cksum = 0; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 373 | outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp, |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 374 | packlen - (sizeof(*outip) + optlen)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 375 | if (outicmp->icmp_cksum == 0) |
| 376 | outicmp->icmp_cksum = 0xffff; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 379 | //BUG! verbose is (x & OPT_VERBOSE), not a counter! |
| 380 | #if 0 //ENABLE_FEATURE_TRACEROUTE_VERBOSE |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 381 | /* XXX undocumented debugging hack */ |
| 382 | if (verbose > 1) { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 383 | const uint16_t *sp; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 384 | int nshorts, i; |
| 385 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 386 | sp = (uint16_t *)outip; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 387 | nshorts = (unsigned)packlen / sizeof(uint16_t); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 388 | i = 0; |
| 389 | printf("[ %d bytes", packlen); |
| 390 | while (--nshorts >= 0) { |
| 391 | if ((i++ % 8) == 0) |
| 392 | printf("\n\t"); |
| 393 | printf(" %04x", ntohs(*sp)); |
| 394 | sp++; |
| 395 | } |
| 396 | if (packlen & 1) { |
| 397 | if ((i % 8) == 0) |
| 398 | printf("\n\t"); |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 399 | printf(" %02x", *(unsigned char *)sp); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 400 | } |
| 401 | printf("]\n"); |
| 402 | } |
| 403 | #endif |
| 404 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 405 | #if defined(IP_TTL) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 406 | if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 407 | (char *)&ttl, sizeof(ttl)) < 0) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 408 | bb_perror_msg_and_die("setsockopt ttl %d", ttl); |
| 409 | } |
| 410 | #endif |
| 411 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 412 | len = packlen - sizeof(*outip); |
| 413 | if (option_mask32 & OPT_USE_ICMP) |
| 414 | out = outicmp; |
| 415 | else { |
| 416 | out = outdata; |
| 417 | len -= sizeof(*outudp); |
| 418 | set_nport(dest_lsa, htons(port + seq)); |
| 419 | } |
| 420 | res = xsendto(sndsock, out, len, |
| 421 | (struct sockaddr *)&dest_lsa->u.sa, dest_lsa->len); |
| 422 | if (res != len) { |
| 423 | bb_info_msg("sent %d octets, ret=%d", len, res); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 424 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 427 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 428 | /* |
| 429 | * Convert an ICMP "type" field to a printable string. |
| 430 | */ |
| 431 | static inline const char * |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 432 | pr_type(unsigned char t) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 433 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 434 | static const char *const ttab[] = { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 435 | "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable", |
| 436 | "Source Quench", "Redirect", "ICMP 6", "ICMP 7", |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 437 | "Echo", "Router Advert", "Router Solicit", "Time Exceeded", |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 438 | "Param Problem", "Timestamp", "Timestamp Reply", "Info Request", |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 439 | "Info Reply", "Mask Request", "Mask Reply" |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 442 | if (t >= ARRAY_SIZE(ttab)) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 443 | return "OUT-OF-RANGE"; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 444 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 445 | return ttab[t]; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 446 | } |
| 447 | #endif |
| 448 | |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 449 | #if !ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 450 | #define packet_ok(cc, from, seq) \ |
| 451 | packet_ok(cc, seq) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 452 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 453 | static int |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 454 | packet_ok(int cc, const struct sockaddr_in *from, int seq) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 455 | { |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 456 | const struct icmp *icp; |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 457 | unsigned char type, code; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 458 | int hlen; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 459 | const struct ip *ip; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 460 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 461 | ip = (struct ip *) recv_pkt; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 462 | hlen = ip->ip_hl << 2; |
| 463 | if (cc < hlen + ICMP_MINLEN) { |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 464 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 465 | if (verbose) |
| 466 | printf("packet too short (%d bytes) from %s\n", cc, |
| 467 | inet_ntoa(from->sin_addr)); |
| 468 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 469 | return 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 470 | } |
| 471 | cc -= hlen; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 472 | icp = (struct icmp *)(recv_pkt + hlen); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 473 | type = icp->icmp_type; |
| 474 | code = icp->icmp_code; |
| 475 | /* Path MTU Discovery (RFC1191) */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 476 | pmtu = 0; |
| 477 | if (code == ICMP_UNREACH_NEEDFRAG) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 478 | pmtu = ntohs(icp->icmp_nextmtu); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 479 | |
| 480 | if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS) |
| 481 | || type == ICMP_UNREACH |
| 482 | || type == ICMP_ECHOREPLY |
| 483 | ) { |
| 484 | const struct ip *hip; |
| 485 | const struct udphdr *up; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 486 | |
| 487 | hip = &icp->icmp_ip; |
| 488 | hlen = hip->ip_hl << 2; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 489 | if (option_mask32 & OPT_USE_ICMP) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 490 | struct icmp *hicmp; |
| 491 | |
| 492 | /* XXX */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 493 | if (type == ICMP_ECHOREPLY |
| 494 | && icp->icmp_id == htons(ident) |
| 495 | && icp->icmp_seq == htons(seq) |
| 496 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 497 | return -2; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 498 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 499 | |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 500 | hicmp = (struct icmp *)((unsigned char *)hip + hlen); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 501 | if (hlen + SIZEOF_ICMP_HDR <= cc |
| 502 | && hip->ip_p == IPPROTO_ICMP |
| 503 | && hicmp->icmp_id == htons(ident) |
| 504 | && hicmp->icmp_seq == htons(seq) |
| 505 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 506 | return (type == ICMP_TIMXCEED ? -1 : code + 1); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 507 | } |
| 508 | } else { |
| 509 | up = (struct udphdr *)((char *)hip + hlen); |
| 510 | if (hlen + 12 <= cc |
| 511 | && hip->ip_p == IPPROTO_UDP |
| 512 | // Off: since we do not form the entire IP packet, |
| 513 | // but defer it to kernel, we can't set source port, |
| 514 | // and thus can't check it here in the reply |
| 515 | /* && up->source == htons(ident) */ |
| 516 | && up->dest == htons(port + seq) |
| 517 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 518 | return (type == ICMP_TIMXCEED ? -1 : code + 1); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 519 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 520 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 521 | } |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 522 | #if ENABLE_FEATURE_TRACEROUTE_VERBOSE |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 523 | if (verbose) { |
| 524 | int i; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 525 | uint32_t *lp = (uint32_t *)&icp->icmp_ip; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 526 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 527 | printf("\n%d bytes from %s to " |
| 528 | "%s: icmp type %d (%s) code %d\n", |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 529 | cc, inet_ntoa(from->sin_addr), |
| 530 | inet_ntoa(ip->ip_dst), |
| 531 | type, pr_type(type), icp->icmp_code); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 532 | for (i = 4; i < cc; i += sizeof(*lp)) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 533 | printf("%2d: x%8.8x\n", i, *lp++); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 534 | } |
| 535 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 536 | return 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 537 | } |
| 538 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 539 | /* |
| 540 | * Construct an Internet address representation. |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 541 | * If the -n flag has been supplied, give |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 542 | * numeric value, otherwise try for symbolic name. |
| 543 | */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 544 | static void |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 545 | print_inetname(const struct sockaddr_in *from) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 546 | { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 547 | const char *ina; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 548 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 549 | ina = inet_ntoa(from->sin_addr); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 550 | if (option_mask32 & OPT_ADDR_NUM) |
| 551 | printf(" %s", ina); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 552 | else { |
| 553 | char *n = NULL; |
| 554 | if (from->sin_addr.s_addr != INADDR_ANY) |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 555 | n = xmalloc_sockaddr2host_noport((struct sockaddr*)from); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 556 | printf(" %s (%s)", (n ? n : ina), ina); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 557 | free(n); |
| 558 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 561 | static void |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 562 | print(int cc, const struct sockaddr_in *from) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 563 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 564 | print_inetname(from); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 565 | if (verbose) { |
| 566 | const struct ip *ip; |
| 567 | int hlen; |
| 568 | |
| 569 | ip = (struct ip *) recv_pkt; |
| 570 | hlen = ip->ip_hl << 2; |
| 571 | cc -= hlen; |
Denis Vlasenko | 6398cf4 | 2007-04-11 17:04:29 +0000 | [diff] [blame] | 572 | printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 573 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 577 | print_delta_ms(unsigned t1p, unsigned t2p) |
Denis Vlasenko | 1adf681 | 2007-06-14 07:47:52 +0000 | [diff] [blame] | 578 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 579 | unsigned tt = t2p - t1p; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 580 | printf(" %u.%03u ms", tt / 1000, tt % 1000); |
Denis Vlasenko | 1adf681 | 2007-06-14 07:47:52 +0000 | [diff] [blame] | 581 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 582 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 583 | /* |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 584 | Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl] |
| 585 | [-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos] |
| 586 | [-w waittime] [-z pausemsecs] host [packetlen]" |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 587 | */ |
| 588 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 589 | int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 590 | int traceroute_main(int argc, char **argv) |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 591 | { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 592 | int minpacket; |
| 593 | int ttl, i; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 594 | int seq = 0; |
| 595 | int tos = 0; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 596 | int max_ttl = 30; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 597 | int nprobes = 3; |
| 598 | int first_ttl = 1; |
| 599 | unsigned pausemsecs = 0; |
| 600 | unsigned op; |
| 601 | char *source; |
| 602 | char *device; |
| 603 | char *tos_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 604 | char *max_ttl_str; |
| 605 | char *port_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 606 | char *nprobes_str; |
| 607 | char *waittime_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 608 | char *pausemsecs_str; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 609 | char *first_ttl_str; |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 610 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 611 | llist_t *source_route_list = NULL; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 612 | int lsrr = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 613 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 614 | |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 615 | INIT_G(); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 616 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 617 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 618 | opt_complementary = "x-x:g::"; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 619 | #else |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 620 | opt_complementary = "x-x"; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 621 | #endif |
| 622 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 623 | op = getopt32(argv, "FIlnrdvxt:i:m:p:q:s:w:z:f:" |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 624 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 625 | "g:" |
| 626 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 627 | , &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str |
| 628 | , &source, &waittime_str, &pausemsecs_str, &first_ttl_str |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 629 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 630 | , &source_route_list |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 631 | #endif |
| 632 | ); |
| 633 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 634 | #if 0 /* IGNORED */ |
| 635 | if (op & OPT_IP_CHKSUM) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 636 | bb_error_msg("warning: ip checksums disabled"); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 637 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 638 | if (op & OPT_TOS) |
| 639 | tos = xatou_range(tos_str, 0, 255); |
| 640 | if (op & OPT_MAX_TTL) |
| 641 | max_ttl = xatou_range(max_ttl_str, 1, 255); |
| 642 | if (op & OPT_PORT) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 643 | port = xatou16(port_str); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 644 | if (op & OPT_NPROBES) |
| 645 | nprobes = xatou_range(nprobes_str, 1, INT_MAX); |
| 646 | if (op & OPT_SOURCE) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 647 | /* |
| 648 | * set the ip source address of the outbound |
| 649 | * probe (e.g., on a multi-homed host). |
| 650 | */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 651 | if (getuid() != 0) |
| 652 | 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] | 653 | } |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 654 | if (op & OPT_WAITTIME) |
| 655 | waittime = xatou_range(waittime_str, 2, 24 * 60 * 60); |
| 656 | if (op & OPT_PAUSE_MS) |
| 657 | pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000); |
| 658 | if (op & OPT_FIRST_TTL) |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 659 | first_ttl = xatou_range(first_ttl_str, 1, max_ttl); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 660 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 661 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
Denis Vlasenko | f7fcca4 | 2007-04-13 22:07:21 +0000 | [diff] [blame] | 662 | if (source_route_list) { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 663 | while (source_route_list) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 664 | len_and_sockaddr *lsa; |
| 665 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 666 | if (lsrr >= NGATEWAYS) |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 667 | bb_error_msg_and_die("no more than %d gateways", NGATEWAYS); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 668 | lsa = xhost_and_af2sockaddr(llist_pop(&source_route_list), 0, AF_INET); |
| 669 | gwlist[lsrr] = lsa->u.sin.sin_addr.s_addr; |
| 670 | free(lsa); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 671 | ++lsrr; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 672 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 673 | optlen = (lsrr + 1) * sizeof(gwlist[0]); |
| 674 | } |
| 675 | #endif |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 676 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 677 | minpacket = sizeof(*outip) + SIZEOF_ICMP_HDR + sizeof(*outdata) + optlen; |
| 678 | if (!(op & OPT_USE_ICMP)) |
| 679 | minpacket += sizeof(*outudp) - SIZEOF_ICMP_HDR; |
| 680 | packlen = minpacket; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 681 | |
| 682 | /* Process destination and optional packet size */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 683 | argv += optind; |
| 684 | argc -= optind; |
| 685 | switch (argc) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 686 | case 2: |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 687 | packlen = xatoul_range(argv[1], minpacket, 32 * 1024); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 688 | /* Fall through */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 689 | case 1: |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 690 | dest_lsa = xhost2sockaddr(argv[0], port); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 691 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 692 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 693 | bb_show_usage(); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 694 | } |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 695 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 696 | /* Ensure the socket fds won't be 0, 1 or 2 */ |
| 697 | bb_sanitize_stdio(); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 698 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 699 | xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), rcvsock); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 700 | #if TRACEROUTE_SO_DEBUG |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 701 | if (op & OPT_DEBUG) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 702 | setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 703 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 704 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 705 | if (op & OPT_BYPASS_ROUTE) |
Denis Vlasenko | 9ad2cb3 | 2007-12-16 17:19:40 +0000 | [diff] [blame] | 706 | setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 707 | &const_int_1, sizeof(const_int_1)); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 708 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 709 | if (op & OPT_USE_ICMP) |
| 710 | xmove_fd(xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP), sndsock); |
| 711 | else |
| 712 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sndsock); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 713 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 714 | #if defined(IP_OPTIONS) |
| 715 | if (lsrr > 0) { |
Mike Frysinger | 294254c | 2006-02-19 22:59:12 +0000 | [diff] [blame] | 716 | unsigned char optlist[MAX_IPOPTLEN]; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 717 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 718 | /* final hop */ |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 719 | gwlist[lsrr] = dest_lsa->u.sin.sin_addr.s_addr; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 720 | ++lsrr; |
| 721 | |
| 722 | /* force 4 byte alignment */ |
| 723 | optlist[0] = IPOPT_NOP; |
| 724 | /* loose source route option */ |
| 725 | optlist[1] = IPOPT_LSRR; |
| 726 | i = lsrr * sizeof(gwlist[0]); |
| 727 | optlist[2] = i + 3; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 728 | /* pointer to LSRR addresses */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 729 | optlist[3] = IPOPT_MINOFF; |
| 730 | memcpy(optlist + 4, gwlist, i); |
| 731 | |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 732 | if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS, |
| 733 | (char *)optlist, i + sizeof(gwlist[0])) < 0) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 734 | bb_perror_msg_and_die("IP_OPTIONS"); |
Denis Vlasenko | 6edadde | 2006-10-03 18:19:02 +0000 | [diff] [blame] | 735 | } |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 736 | } |
| 737 | #endif /* IP_OPTIONS */ |
| 738 | #endif /* CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE */ |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 739 | #ifdef SO_SNDBUF |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 740 | if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 741 | bb_perror_msg_and_die("SO_SNDBUF"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 742 | } |
Eric Andersen | ad79c0b | 2002-06-06 12:24:51 +0000 | [diff] [blame] | 743 | #endif |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 744 | #ifdef IP_TOS |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 745 | 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] | 746 | bb_perror_msg_and_die("setsockopt tos %d", tos); |
| 747 | } |
| 748 | #endif |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 749 | #ifdef IP_DONTFRAG |
| 750 | if (op & OPT_DONT_FRAGMNT) |
| 751 | setsockopt(sndsock, IPPROTO_IP, IP_DONTFRAG, |
| 752 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | ad79c0b | 2002-06-06 12:24:51 +0000 | [diff] [blame] | 753 | #endif |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 754 | #if TRACEROUTE_SO_DEBUG |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 755 | if (op & OPT_DEBUG) |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 756 | setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, |
| 757 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 758 | #endif |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 759 | if (op & OPT_BYPASS_ROUTE) |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 760 | setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, |
| 761 | &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 762 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 763 | outip = xzalloc(packlen); |
| 764 | |
| 765 | if (op & OPT_USE_ICMP) { |
| 766 | ident = getpid() | 0x8000; |
| 767 | outicmp->icmp_type = ICMP_ECHO; |
| 768 | outicmp->icmp_id = htons(ident); |
| 769 | outdata = (struct outdata_t *)((char *)outicmp + SIZEOF_ICMP_HDR); |
| 770 | } else { |
| 771 | outdata = (struct outdata_t *)(outudp + 1); |
| 772 | } |
| 773 | |
| 774 | if (op & OPT_DEVICE) /* hmm, do we need error check? */ |
| 775 | setsockopt_bindtodevice(sndsock, device); |
| 776 | |
| 777 | if (op & OPT_SOURCE) { |
| 778 | len_and_sockaddr *source_lsa = xdotted2sockaddr(source, 0); |
| 779 | /* Ping does this (why?) */ |
| 780 | if (setsockopt(sndsock, IPPROTO_IP, IP_MULTICAST_IF, |
| 781 | &source_lsa->u.sa, source_lsa->len)) |
| 782 | bb_error_msg_and_die("can't set multicast source interface"); |
| 783 | //TODO: we can query source port we bound to, |
| 784 | // and check it in replies... if we care enough |
| 785 | xbind(sndsock, &source_lsa->u.sa, source_lsa->len); |
| 786 | free(source_lsa); |
| 787 | } |
| 788 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 789 | /* Revert to non-privileged user after opening sockets */ |
Rob Landley | afb94ec | 2006-07-16 08:06:34 +0000 | [diff] [blame] | 790 | xsetgid(getgid()); |
| 791 | xsetuid(getuid()); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 792 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 793 | printf("traceroute to %s (%s)", argv[0], |
| 794 | xmalloc_sockaddr2dotted_noport(&dest_lsa->u.sa)); |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 795 | if (op & OPT_SOURCE) |
| 796 | printf(" from %s", source); |
| 797 | printf(", %d hops max, %d byte packets\n", max_ttl, packlen); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 798 | |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 799 | for (ttl = first_ttl; ttl <= max_ttl; ++ttl) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 800 | //TODO: make it protocol agnostic (get rid of sockaddr_in) |
| 801 | struct sockaddr_in from; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 802 | uint32_t lastaddr = 0; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 803 | int probe; |
| 804 | int unreachable = 0; /* counter */ |
| 805 | int gotlastaddr = 0; /* flags */ |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 806 | int got_there = 0; |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 807 | int first = 1; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 808 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 809 | printf("%2d", ttl); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 810 | for (probe = 0; probe < nprobes; ++probe) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 811 | int cc; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 812 | unsigned t1; |
| 813 | unsigned t2; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 814 | struct ip *ip; |
| 815 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 816 | if (!first && pausemsecs > 0) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 817 | usleep(pausemsecs * 1000); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 818 | fflush(stdout); |
| 819 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 820 | t1 = monotonic_us(); |
| 821 | send_probe(++seq, ttl); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 822 | first = 0; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 823 | |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 824 | while ((cc = wait_for_reply(&from)) != 0) { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 825 | t2 = monotonic_us(); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 826 | i = packet_ok(cc, &from, seq); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 827 | /* Skip short packet */ |
| 828 | if (i == 0) |
| 829 | continue; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 830 | if (!gotlastaddr |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 831 | || from.sin_addr.s_addr != lastaddr |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 832 | ) { |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 833 | print(cc, &from); |
| 834 | lastaddr = from.sin_addr.s_addr; |
| 835 | gotlastaddr = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 836 | } |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 837 | print_delta_ms(t1, t2); |
Denis Vlasenko | 766c10c | 2009-01-24 20:14:24 +0000 | [diff] [blame^] | 838 | ip = (struct ip *)recv_pkt; |
Denis Vlasenko | 2e72323 | 2007-03-29 21:55:22 +0000 | [diff] [blame] | 839 | if (op & OPT_TTL_FLAG) |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 840 | printf(" (%d)", ip->ip_ttl); |
| 841 | if (i == -2) { |
| 842 | if (ip->ip_ttl <= 1) |
| 843 | printf(" !"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 844 | got_there = 1; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 845 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 846 | } |
| 847 | /* time exceeded in transit */ |
| 848 | if (i == -1) |
| 849 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 850 | i--; |
| 851 | switch (i) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 852 | case ICMP_UNREACH_PORT: |
| 853 | if (ip->ip_ttl <= 1) |
| 854 | printf(" !"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 855 | got_there = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 856 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 857 | case ICMP_UNREACH_NET: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 858 | printf(" !N"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 859 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 860 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 861 | case ICMP_UNREACH_HOST: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 862 | printf(" !H"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 863 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 864 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 865 | case ICMP_UNREACH_PROTOCOL: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 866 | printf(" !P"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 867 | got_there = 1; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 868 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 869 | case ICMP_UNREACH_NEEDFRAG: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 870 | printf(" !F-%d", pmtu); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 871 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 872 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 873 | case ICMP_UNREACH_SRCFAIL: |
| 874 | printf(" !S"); |
| 875 | ++unreachable; |
| 876 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 877 | case ICMP_UNREACH_FILTER_PROHIB: |
| 878 | case ICMP_UNREACH_NET_PROHIB: /* misuse */ |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 879 | printf(" !A"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 880 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 881 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 882 | case ICMP_UNREACH_HOST_PROHIB: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 883 | printf(" !C"); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 884 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 885 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 886 | case ICMP_UNREACH_HOST_PRECEDENCE: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 887 | printf(" !V"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 888 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 889 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 890 | case ICMP_UNREACH_PRECEDENCE_CUTOFF: |
| 891 | printf(" !C"); |
| 892 | ++unreachable; |
| 893 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 894 | case ICMP_UNREACH_NET_UNKNOWN: |
| 895 | case ICMP_UNREACH_HOST_UNKNOWN: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 896 | printf(" !U"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 897 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 898 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 899 | case ICMP_UNREACH_ISOLATED: |
| 900 | printf(" !I"); |
| 901 | ++unreachable; |
| 902 | break; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 903 | case ICMP_UNREACH_TOSNET: |
| 904 | case ICMP_UNREACH_TOSHOST: |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 905 | printf(" !T"); |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 906 | ++unreachable; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 907 | break; |
| 908 | default: |
| 909 | printf(" !<%d>", i); |
| 910 | ++unreachable; |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | break; |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 914 | } |
| 915 | if (cc == 0) |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 916 | printf(" *"); |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 917 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 918 | bb_putchar('\n'); |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 919 | if (got_there |
| 920 | || (unreachable > 0 && unreachable >= nprobes - 1) |
| 921 | ) { |
"Vladimir N. Oleynik" | 45a8ed8 | 2005-09-06 16:08:33 +0000 | [diff] [blame] | 922 | break; |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 923 | } |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 924 | } |
Eric Andersen | 5c58d28 | 2001-07-10 16:29:00 +0000 | [diff] [blame] | 925 | return 0; |
| 926 | } |