Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * arping.c - Ping hosts by ARP requests/replies |
| 4 | * |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 6 | * |
| 7 | * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 8 | * Busybox port: Nick Fedchik <nick@fedchik.org.ua> |
| 9 | */ |
| 10 | |
| 11 | #include <sys/ioctl.h> |
Mike Frysinger | 06adf5f | 2006-03-22 00:25:07 +0000 | [diff] [blame] | 12 | #include <signal.h> |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 13 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 14 | #include <arpa/inet.h> |
| 15 | #include <net/if.h> |
| 16 | #include <netinet/ether.h> |
| 17 | #include <netpacket/packet.h> |
| 18 | |
| 19 | #include "busybox.h" |
| 20 | |
Glenn L McGrath | 6b0658f | 2003-09-26 00:33:18 +0000 | [diff] [blame] | 21 | static struct in_addr src; |
| 22 | static struct in_addr dst; |
| 23 | static struct sockaddr_ll me; |
| 24 | static struct sockaddr_ll he; |
| 25 | static struct timeval last; |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 26 | |
| 27 | enum cfg_e { |
| 28 | dad = 1, |
| 29 | unsolicited = 2, |
| 30 | advert = 4, |
| 31 | quiet = 8, |
| 32 | quit_on_reply = 16, |
Bernhard Reutner-Fischer | 6153629 | 2006-04-03 09:46:29 +0000 | [diff] [blame] | 33 | broadcast_only = 32, |
| 34 | unicasting = 64 |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 35 | }; |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 36 | static int cfg; |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 37 | |
| 38 | static int s; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 39 | static unsigned count = UINT_MAX; |
| 40 | static unsigned timeout; |
Glenn L McGrath | 6b0658f | 2003-09-26 00:33:18 +0000 | [diff] [blame] | 41 | static int sent; |
| 42 | static int brd_sent; |
| 43 | static int received; |
| 44 | static int brd_recv; |
| 45 | static int req_recv; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 46 | |
| 47 | #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \ |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 48 | ((tv1).tv_usec-(tv2).tv_usec)/1000 ) |
| 49 | |
Glenn L McGrath | e6ae6e3 | 2003-01-19 13:31:41 +0000 | [diff] [blame] | 50 | static int send_pack(int sock, struct in_addr *src_addr, |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 51 | struct in_addr *dst_addr, struct sockaddr_ll *ME, |
| 52 | struct sockaddr_ll *HE) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 53 | { |
| 54 | int err; |
| 55 | struct timeval now; |
Bernhard Reutner-Fischer | 2766eed | 2006-03-31 18:13:42 +0000 | [diff] [blame] | 56 | RESERVE_CONFIG_UBUFFER(buf, 256); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 57 | struct arphdr *ah = (struct arphdr *) buf; |
| 58 | unsigned char *p = (unsigned char *) (ah + 1); |
| 59 | |
| 60 | ah->ar_hrd = htons(ME->sll_hatype); |
| 61 | ah->ar_hrd = htons(ARPHRD_ETHER); |
| 62 | ah->ar_pro = htons(ETH_P_IP); |
| 63 | ah->ar_hln = ME->sll_halen; |
| 64 | ah->ar_pln = 4; |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 65 | ah->ar_op = cfg & advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 66 | |
| 67 | memcpy(p, &ME->sll_addr, ah->ar_hln); |
| 68 | p += ME->sll_halen; |
| 69 | |
| 70 | memcpy(p, src_addr, 4); |
| 71 | p += 4; |
| 72 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 73 | if (cfg & advert) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 74 | memcpy(p, &ME->sll_addr, ah->ar_hln); |
| 75 | else |
| 76 | memcpy(p, &HE->sll_addr, ah->ar_hln); |
| 77 | p += ah->ar_hln; |
| 78 | |
| 79 | memcpy(p, dst_addr, 4); |
| 80 | p += 4; |
| 81 | |
| 82 | gettimeofday(&now, NULL); |
| 83 | err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE)); |
| 84 | if (err == p - buf) { |
| 85 | last = now; |
| 86 | sent++; |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 87 | if (!(cfg & unicasting)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 88 | brd_sent++; |
| 89 | } |
Bernhard Reutner-Fischer | 2766eed | 2006-03-31 18:13:42 +0000 | [diff] [blame] | 90 | RELEASE_CONFIG_BUFFER(buf); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 94 | static void finish(void) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 95 | { |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 96 | if (!(cfg & quiet)) { |
| 97 | printf("Sent %d probe(s) (%d broadcast(s))\n" |
| 98 | "Received %d repl%s" |
| 99 | " (%d request(s), %d broadcast(s))\n", |
Bernhard Reutner-Fischer | ebd1355 | 2006-04-03 12:29:12 +0000 | [diff] [blame] | 100 | sent, brd_sent, |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 101 | received, (received == 1) ? "ies" : "y", |
| 102 | req_recv, brd_recv); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 103 | } |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 104 | if (cfg & dad) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 105 | exit(!!received); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 106 | if (cfg & unsolicited) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 107 | exit(0); |
| 108 | exit(!received); |
| 109 | } |
| 110 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 111 | static void catcher(void) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 112 | { |
| 113 | struct timeval tv; |
Glenn L McGrath | 8b96b71 | 2003-08-28 19:54:16 +0000 | [diff] [blame] | 114 | static struct timeval start; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 115 | |
| 116 | gettimeofday(&tv, NULL); |
| 117 | |
| 118 | if (start.tv_sec == 0) |
| 119 | start = tv; |
| 120 | |
| 121 | if (count-- == 0 |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 122 | || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 123 | finish(); |
| 124 | |
| 125 | if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) { |
| 126 | send_pack(s, &src, &dst, &me, &he); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 127 | if (count == 0 && (cfg & unsolicited)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 128 | finish(); |
| 129 | } |
| 130 | alarm(1); |
| 131 | } |
| 132 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 133 | static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 134 | { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 135 | struct arphdr *ah = (struct arphdr *) buf; |
| 136 | unsigned char *p = (unsigned char *) (ah + 1); |
| 137 | struct in_addr src_ip, dst_ip; |
| 138 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 139 | /* Filter out wild packets */ |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 140 | if (FROM->sll_pkttype != PACKET_HOST |
| 141 | && FROM->sll_pkttype != PACKET_BROADCAST |
| 142 | && FROM->sll_pkttype != PACKET_MULTICAST) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | |
| 145 | /* Only these types are recognised */ |
| 146 | if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY)) |
| 147 | return 0; |
| 148 | |
| 149 | /* ARPHRD check and this darned FDDI hack here :-( */ |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 150 | if (ah->ar_hrd != htons(FROM->sll_hatype) |
| 151 | && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER))) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | |
| 154 | /* Protocol must be IP. */ |
| 155 | if (ah->ar_pro != htons(ETH_P_IP)) |
| 156 | return 0; |
| 157 | if (ah->ar_pln != 4) |
| 158 | return 0; |
| 159 | if (ah->ar_hln != me.sll_halen) |
| 160 | return 0; |
| 161 | if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln)) |
| 162 | return 0; |
| 163 | memcpy(&src_ip, p + ah->ar_hln, 4); |
| 164 | memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 165 | if (!(cfg & dad)) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 166 | if (src_ip.s_addr != dst.s_addr) |
| 167 | return 0; |
| 168 | if (src.s_addr != dst_ip.s_addr) |
| 169 | return 0; |
| 170 | if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)) |
| 171 | return 0; |
| 172 | } else { |
| 173 | /* DAD packet was: |
| 174 | src_ip = 0 (or some src) |
| 175 | src_hw = ME |
| 176 | dst_ip = tested address |
| 177 | dst_hw = <unspec> |
| 178 | |
| 179 | We fail, if receive request/reply with: |
| 180 | src_ip = tested_address |
| 181 | src_hw != ME |
| 182 | if src_ip in request was not zero, check |
| 183 | also that it matches to dst_ip, otherwise |
| 184 | dst_ip/dst_hw do not matter. |
| 185 | */ |
| 186 | if (src_ip.s_addr != dst.s_addr) |
| 187 | return 0; |
| 188 | if (memcmp(p, &me.sll_addr, me.sll_halen) == 0) |
| 189 | return 0; |
| 190 | if (src.s_addr && src.s_addr != dst_ip.s_addr) |
| 191 | return 0; |
| 192 | } |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 193 | if (!(cfg & quiet)) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 194 | int s_printed = 0; |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 195 | struct timeval tv; |
| 196 | |
| 197 | gettimeofday(&tv, NULL); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 199 | printf("%scast re%s from %s [%s]", |
| 200 | FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad", |
| 201 | ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest", |
Bernhard Reutner-Fischer | ebd1355 | 2006-04-03 12:29:12 +0000 | [diff] [blame] | 202 | inet_ntoa(src_ip), |
| 203 | ether_ntoa((struct ether_addr *) p)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 204 | if (dst_ip.s_addr != src.s_addr) { |
| 205 | printf("for %s ", inet_ntoa(dst_ip)); |
| 206 | s_printed = 1; |
| 207 | } |
| 208 | if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) { |
| 209 | if (!s_printed) |
| 210 | printf("for "); |
| 211 | printf("[%s]", |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 212 | ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 213 | } |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 214 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 215 | if (last.tv_sec) { |
| 216 | long usecs = (tv.tv_sec - last.tv_sec) * 1000000 + |
| 217 | tv.tv_usec - last.tv_usec; |
| 218 | long msecs = (usecs + 500) / 1000; |
| 219 | |
| 220 | usecs -= msecs * 1000 - 500; |
| 221 | printf(" %ld.%03ldms\n", msecs, usecs); |
| 222 | } else { |
| 223 | printf(" UNSOLICITED?\n"); |
| 224 | } |
| 225 | fflush(stdout); |
| 226 | } |
| 227 | received++; |
| 228 | if (FROM->sll_pkttype != PACKET_HOST) |
| 229 | brd_recv++; |
| 230 | if (ah->ar_op == htons(ARPOP_REQUEST)) |
| 231 | req_recv++; |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 232 | if (cfg & quit_on_reply) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 233 | finish(); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 234 | if (!(cfg & broadcast_only)) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 235 | memcpy(he.sll_addr, p, me.sll_halen); |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 236 | cfg |= unicasting; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 237 | } |
| 238 | return 1; |
| 239 | } |
| 240 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 241 | int arping_main(int argc, char **argv); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 242 | int arping_main(int argc, char **argv) |
| 243 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 244 | const char *device = "eth0"; |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 245 | int ifindex; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 246 | char *source = NULL; |
| 247 | char *target; |
| 248 | |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 249 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 250 | |
Rob Landley | afb94ec | 2006-07-16 08:06:34 +0000 | [diff] [blame] | 251 | // Drop suid root privileges |
| 252 | xsetuid(getuid()); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 253 | |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 254 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 255 | unsigned opt; |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 256 | char *_count, *_timeout; |
Bernhard Reutner-Fischer | a0f75e2 | 2006-04-03 11:52:01 +0000 | [diff] [blame] | 257 | |
| 258 | /* Dad also sets quit_on_reply. |
| 259 | * Advert also sets unsolicited. |
| 260 | */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 261 | opt_complementary = "Df:AU"; |
| 262 | opt = getopt32(argc, argv, "DUAqfbc:w:i:s:", |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 263 | &_count, &_timeout, &device, &source); |
| 264 | cfg |= opt & 0x3f; /* set respective flags */ |
| 265 | if (opt & 0x40) /* -c: count */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 266 | count = xatou(_count); |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 267 | if (opt & 0x80) /* -w: timeout */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 268 | timeout = xatoul_range(_timeout, 0, INT_MAX/2000); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 269 | //if (opt & 0x100) /* -i: interface */ |
| 270 | if (strlen(device) > IF_NAMESIZE) { |
| 271 | bb_error_msg_and_die("interface name '%s' is too long", |
| 272 | device); |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 273 | } |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 274 | //if (opt & 0x200) /* -s: source */ |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 275 | } |
| 276 | argc -= optind; |
| 277 | argv += optind; |
| 278 | |
| 279 | if (argc != 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 280 | bb_show_usage(); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 281 | |
| 282 | target = *argv; |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 284 | xfunc_error_retval = 2; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 285 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 286 | { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 287 | struct ifreq ifr; |
| 288 | |
| 289 | memset(&ifr, 0, sizeof(ifr)); |
| 290 | strncpy(ifr.ifr_name, device, IFNAMSIZ - 1); |
| 291 | if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 292 | bb_error_msg_and_die("interface %s not found", device); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 293 | } |
| 294 | ifindex = ifr.ifr_ifindex; |
| 295 | |
| 296 | if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 297 | bb_error_msg_and_die("SIOCGIFFLAGS"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 298 | } |
| 299 | if (!(ifr.ifr_flags & IFF_UP)) { |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 300 | bb_error_msg_and_die("interface %s is down", device); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 301 | } |
| 302 | if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 303 | bb_error_msg("interface %s is not ARPable", device); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 304 | return (cfg & dad ? 0 : 2); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 308 | if (!inet_aton(target, &dst)) { |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 309 | len_and_sockaddr *lsa; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 310 | lsa = xhost_and_af2sockaddr(target, 0, AF_INET); |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 311 | memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4); |
| 312 | if (ENABLE_FEATURE_CLEAN_UP) |
| 313 | free(lsa); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 316 | if (source && !inet_aton(source, &src)) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 317 | bb_error_msg_and_die("invalid source address %s", source); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 320 | if (!(cfg & dad) && (cfg & unsolicited) && src.s_addr == 0) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 321 | src = dst; |
| 322 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 323 | if (!(cfg & dad) || src.s_addr) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 324 | struct sockaddr_in saddr; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 325 | int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 326 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 327 | if (device) { |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 328 | if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1) |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 329 | bb_error_msg("warning: interface %s is ignored", device); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 330 | } |
| 331 | memset(&saddr, 0, sizeof(saddr)); |
| 332 | saddr.sin_family = AF_INET; |
| 333 | if (src.s_addr) { |
| 334 | saddr.sin_addr = src; |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 335 | xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); |
| 336 | } else if (!(cfg & dad)) { |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 337 | socklen_t alen = sizeof(saddr); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 338 | |
| 339 | saddr.sin_port = htons(1025); |
| 340 | saddr.sin_addr = dst; |
| 341 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 342 | if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1) |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 343 | bb_perror_msg("warning: setsockopt(SO_DONTROUTE)"); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 344 | xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); |
| 345 | if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 346 | bb_error_msg_and_die("getsockname"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 347 | } |
| 348 | src = saddr.sin_addr; |
| 349 | } |
| 350 | close(probe_fd); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 351 | } |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 352 | |
| 353 | me.sll_family = AF_PACKET; |
| 354 | me.sll_ifindex = ifindex; |
| 355 | me.sll_protocol = htons(ETH_P_ARP); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 356 | xbind(s, (struct sockaddr *) &me, sizeof(me)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 357 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 358 | { |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 359 | socklen_t alen = sizeof(me); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 360 | |
| 361 | if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 362 | bb_error_msg_and_die("getsockname"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | if (me.sll_halen == 0) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 366 | bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 367 | return (cfg & dad ? 0 : 2); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 368 | } |
| 369 | he = me; |
| 370 | memset(he.sll_addr, -1, he.sll_halen); |
| 371 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 372 | if (!(cfg & quiet)) { |
Bernhard Reutner-Fischer | ebd1355 | 2006-04-03 12:29:12 +0000 | [diff] [blame] | 373 | printf("ARPING to %s from %s via %s\n", |
| 374 | inet_ntoa(dst), inet_ntoa(src), |
| 375 | device ? device : "unknown"); |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 376 | } |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 377 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 378 | if (!src.s_addr && !(cfg & dad)) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 379 | bb_error_msg_and_die("no src address in the non-DAD mode"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | { |
| 383 | struct sigaction sa; |
Glenn L McGrath | e6ae6e3 | 2003-01-19 13:31:41 +0000 | [diff] [blame] | 384 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 385 | memset(&sa, 0, sizeof(sa)); |
| 386 | sa.sa_flags = SA_RESTART; |
| 387 | |
| 388 | sa.sa_handler = (void (*)(int)) finish; |
| 389 | sigaction(SIGINT, &sa, NULL); |
| 390 | |
| 391 | sa.sa_handler = (void (*)(int)) catcher; |
| 392 | sigaction(SIGALRM, &sa, NULL); |
| 393 | } |
| 394 | |
| 395 | catcher(); |
| 396 | |
| 397 | while (1) { |
| 398 | sigset_t sset, osset; |
Bernhard Reutner-Fischer | 2766eed | 2006-03-31 18:13:42 +0000 | [diff] [blame] | 399 | RESERVE_CONFIG_UBUFFER(packet, 4096); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 400 | struct sockaddr_ll from; |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 401 | socklen_t alen = sizeof(from); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 402 | int cc; |
| 403 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 404 | cc = recvfrom(s, packet, 4096, 0, (struct sockaddr *) &from, &alen); |
| 405 | if (cc < 0) { |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 406 | bb_perror_msg("recvfrom"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 407 | continue; |
| 408 | } |
| 409 | sigemptyset(&sset); |
| 410 | sigaddset(&sset, SIGALRM); |
| 411 | sigaddset(&sset, SIGINT); |
| 412 | sigprocmask(SIG_BLOCK, &sset, &osset); |
| 413 | recv_pack(packet, cc, &from); |
| 414 | sigprocmask(SIG_SETMASK, &osset, NULL); |
Bernhard Reutner-Fischer | 2766eed | 2006-03-31 18:13:42 +0000 | [diff] [blame] | 415 | RELEASE_CONFIG_BUFFER(packet); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 416 | } |
| 417 | } |