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