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