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 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 11 | #include <arpa/inet.h> |
| 12 | #include <net/if.h> |
| 13 | #include <netinet/ether.h> |
| 14 | #include <netpacket/packet.h> |
| 15 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 18 | /* We don't expect to see 1000+ seconds delay, unsigned is enough */ |
| 19 | #define MONOTONIC_US() ((unsigned)monotonic_us()) |
| 20 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 21 | enum { |
| 22 | DAD = 1, |
| 23 | UNSOLICITED = 2, |
| 24 | ADVERT = 4, |
| 25 | QUIET = 8, |
| 26 | QUIT_ON_REPLY = 16, |
| 27 | BCAST_ONLY = 32, |
| 28 | UNICASTING = 64 |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 29 | }; |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 31 | struct globals { |
| 32 | struct in_addr src; |
| 33 | struct in_addr dst; |
| 34 | struct sockaddr_ll me; |
| 35 | struct sockaddr_ll he; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 36 | int sock_fd; |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 37 | |
| 38 | int count; // = -1; |
| 39 | unsigned last; |
| 40 | unsigned timeout_us; |
| 41 | unsigned start; |
| 42 | |
| 43 | unsigned sent; |
| 44 | unsigned brd_sent; |
| 45 | unsigned received; |
| 46 | unsigned brd_recv; |
| 47 | unsigned req_recv; |
| 48 | }; |
| 49 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 50 | #define src (G.src ) |
| 51 | #define dst (G.dst ) |
| 52 | #define me (G.me ) |
| 53 | #define he (G.he ) |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 54 | #define sock_fd (G.sock_fd ) |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 55 | #define count (G.count ) |
| 56 | #define last (G.last ) |
| 57 | #define timeout_us (G.timeout_us) |
| 58 | #define start (G.start ) |
| 59 | #define sent (G.sent ) |
| 60 | #define brd_sent (G.brd_sent ) |
| 61 | #define received (G.received ) |
| 62 | #define brd_recv (G.brd_recv ) |
| 63 | #define req_recv (G.req_recv ) |
| 64 | #define INIT_G() \ |
| 65 | do { \ |
| 66 | count = -1; \ |
| 67 | } while (0) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | a6b3a1f | 2008-04-25 08:13:36 +0000 | [diff] [blame] | 69 | // If GNUisms are not available... |
| 70 | //static void *mempcpy(void *_dst, const void *_src, int n) |
| 71 | //{ |
| 72 | // memcpy(_dst, _src, n); |
| 73 | // return (char*)_dst + n; |
| 74 | //} |
| 75 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 76 | static int send_pack(struct in_addr *src_addr, |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 77 | struct in_addr *dst_addr, struct sockaddr_ll *ME, |
| 78 | struct sockaddr_ll *HE) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 79 | { |
| 80 | int err; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 81 | unsigned char buf[256]; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 82 | struct arphdr *ah = (struct arphdr *) buf; |
| 83 | unsigned char *p = (unsigned char *) (ah + 1); |
| 84 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 85 | ah->ar_hrd = htons(ARPHRD_ETHER); |
| 86 | ah->ar_pro = htons(ETH_P_IP); |
| 87 | ah->ar_hln = ME->sll_halen; |
| 88 | ah->ar_pln = 4; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 89 | ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | a6b3a1f | 2008-04-25 08:13:36 +0000 | [diff] [blame] | 91 | p = mempcpy(p, &ME->sll_addr, ah->ar_hln); |
| 92 | p = mempcpy(p, src_addr, 4); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 94 | if (option_mask32 & ADVERT) |
Denis Vlasenko | a6b3a1f | 2008-04-25 08:13:36 +0000 | [diff] [blame] | 95 | p = mempcpy(p, &ME->sll_addr, ah->ar_hln); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 96 | else |
Denis Vlasenko | a6b3a1f | 2008-04-25 08:13:36 +0000 | [diff] [blame] | 97 | p = mempcpy(p, &HE->sll_addr, ah->ar_hln); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | a6b3a1f | 2008-04-25 08:13:36 +0000 | [diff] [blame] | 99 | p = mempcpy(p, dst_addr, 4); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 101 | err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 102 | if (err == p - buf) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 103 | last = MONOTONIC_US(); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 104 | sent++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 105 | if (!(option_mask32 & UNICASTING)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 106 | brd_sent++; |
| 107 | } |
| 108 | return err; |
| 109 | } |
| 110 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 111 | static void finish(void) ATTRIBUTE_NORETURN; |
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 | { |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 114 | if (!(option_mask32 & QUIET)) { |
| 115 | printf("Sent %u probe(s) (%u broadcast(s))\n" |
| 116 | "Received %u repl%s" |
| 117 | " (%u request(s), %u broadcast(s))\n", |
Bernhard Reutner-Fischer | ebd1355 | 2006-04-03 12:29:12 +0000 | [diff] [blame] | 118 | sent, brd_sent, |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 119 | received, (received == 1) ? "ies" : "y", |
| 120 | req_recv, brd_recv); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 121 | } |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 122 | if (option_mask32 & DAD) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 123 | exit(!!received); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 124 | if (option_mask32 & UNSOLICITED) |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 125 | exit(EXIT_SUCCESS); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 126 | exit(!received); |
| 127 | } |
| 128 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 129 | static void catcher(void) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 130 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 131 | unsigned now; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 133 | now = MONOTONIC_US(); |
| 134 | if (start == 0) |
| 135 | start = now; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 137 | if (count == 0 || (timeout_us && (now - start) > timeout_us)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 138 | finish(); |
| 139 | |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 140 | /* count < 0 means "infinite count" */ |
| 141 | if (count > 0) |
| 142 | count--; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 143 | |
| 144 | if (last == 0 || (now - last) > 500000) { |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 145 | send_pack(&src, &dst, &me, &he); |
| 146 | if (count == 0 && (option_mask32 & UNSOLICITED)) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 147 | finish(); |
| 148 | } |
| 149 | alarm(1); |
| 150 | } |
| 151 | |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 152 | static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 153 | { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 154 | struct arphdr *ah = (struct arphdr *) buf; |
| 155 | unsigned char *p = (unsigned char *) (ah + 1); |
| 156 | struct in_addr src_ip, dst_ip; |
| 157 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 158 | /* Filter out wild packets */ |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 159 | if (FROM->sll_pkttype != PACKET_HOST |
| 160 | && FROM->sll_pkttype != PACKET_BROADCAST |
| 161 | && FROM->sll_pkttype != PACKET_MULTICAST) |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 162 | return false; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 163 | |
| 164 | /* Only these types are recognised */ |
| 165 | if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY)) |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 166 | return false; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 167 | |
| 168 | /* ARPHRD check and this darned FDDI hack here :-( */ |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 169 | if (ah->ar_hrd != htons(FROM->sll_hatype) |
| 170 | && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER))) |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 171 | return false; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 172 | |
| 173 | /* Protocol must be IP. */ |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 174 | if (ah->ar_pro != htons(ETH_P_IP) |
| 175 | || (ah->ar_pln != 4) |
| 176 | || (ah->ar_hln != me.sll_halen) |
| 177 | || (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))) |
| 178 | return false; |
| 179 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 180 | memcpy(&src_ip, p + ah->ar_hln, 4); |
| 181 | memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4); |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 182 | |
| 183 | if (dst.s_addr != src_ip.s_addr) |
| 184 | return false; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 185 | if (!(option_mask32 & DAD)) { |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 186 | if ((src.s_addr != dst_ip.s_addr) |
| 187 | || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))) |
| 188 | return false; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 189 | } else { |
| 190 | /* DAD packet was: |
| 191 | src_ip = 0 (or some src) |
| 192 | src_hw = ME |
| 193 | dst_ip = tested address |
| 194 | dst_hw = <unspec> |
| 195 | |
| 196 | We fail, if receive request/reply with: |
| 197 | src_ip = tested_address |
| 198 | src_hw != ME |
| 199 | if src_ip in request was not zero, check |
| 200 | also that it matches to dst_ip, otherwise |
| 201 | dst_ip/dst_hw do not matter. |
| 202 | */ |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 203 | if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0) |
| 204 | || (src.s_addr && src.s_addr != dst_ip.s_addr)) |
| 205 | return false; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 206 | } |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 207 | if (!(option_mask32 & QUIET)) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 208 | int s_printed = 0; |
| 209 | |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 210 | printf("%scast re%s from %s [%s]", |
| 211 | FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad", |
| 212 | ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest", |
Bernhard Reutner-Fischer | ebd1355 | 2006-04-03 12:29:12 +0000 | [diff] [blame] | 213 | inet_ntoa(src_ip), |
| 214 | ether_ntoa((struct ether_addr *) p)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 215 | if (dst_ip.s_addr != src.s_addr) { |
| 216 | printf("for %s ", inet_ntoa(dst_ip)); |
| 217 | s_printed = 1; |
| 218 | } |
| 219 | if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) { |
| 220 | if (!s_printed) |
| 221 | printf("for "); |
| 222 | printf("[%s]", |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 223 | ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 224 | } |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 225 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 226 | if (last) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 227 | unsigned diff = MONOTONIC_US() - last; |
| 228 | printf(" %u.%03ums\n", diff / 1000, diff % 1000); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 229 | } else { |
| 230 | printf(" UNSOLICITED?\n"); |
| 231 | } |
| 232 | fflush(stdout); |
| 233 | } |
| 234 | received++; |
| 235 | if (FROM->sll_pkttype != PACKET_HOST) |
| 236 | brd_recv++; |
| 237 | if (ah->ar_op == htons(ARPOP_REQUEST)) |
| 238 | req_recv++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 239 | if (option_mask32 & QUIT_ON_REPLY) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 240 | finish(); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 241 | if (!(option_mask32 & BCAST_ONLY)) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 242 | memcpy(he.sll_addr, p, me.sll_halen); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 243 | option_mask32 |= UNICASTING; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 244 | } |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 245 | return true; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 248 | int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 249 | int arping_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 250 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 251 | const char *device = "eth0"; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 252 | char *source = NULL; |
| 253 | char *target; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 254 | unsigned char *packet; |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 255 | char *err_str; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 257 | INIT_G(); |
| 258 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 259 | sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 260 | |
Rob Landley | afb94ec | 2006-07-16 08:06:34 +0000 | [diff] [blame] | 261 | // Drop suid root privileges |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 262 | // Need to remove SUID_NEVER from applets.h for this to work |
| 263 | //xsetuid(getuid()); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 264 | |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 265 | err_str = xasprintf("interface %s %%s", device); |
Bernhard Reutner-Fischer | 1376694 | 2006-03-31 18:02:46 +0000 | [diff] [blame] | 266 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 267 | unsigned opt; |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 268 | char *str_timeout; |
Bernhard Reutner-Fischer | a0f75e2 | 2006-04-03 11:52:01 +0000 | [diff] [blame] | 269 | |
| 270 | /* Dad also sets quit_on_reply. |
| 271 | * Advert also sets unsolicited. |
| 272 | */ |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 273 | opt_complementary = "=1:Df:AU:c+"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 274 | opt = getopt32(argv, "DUAqfbc:w:I:s:", |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 275 | &count, &str_timeout, &device, &source); |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 276 | if (opt & 0x80) /* -w: timeout */ |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 277 | timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000; |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 278 | //if (opt & 0x200) /* -s: source */ |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 279 | option_mask32 &= 0x3f; /* set respective flags */ |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 280 | } |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 281 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 282 | target = argv[optind]; |
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)); |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 290 | strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1); |
| 291 | /* We use ifr.ifr_name in error msg so that problem |
| 292 | * with truncated name will be visible */ |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 293 | ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found"); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 294 | me.sll_ifindex = ifr.ifr_ifindex; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 295 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 296 | xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 297 | |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 298 | if (!(ifr.ifr_flags & IFF_UP)) { |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 299 | bb_error_msg_and_die(err_str, "is down"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 300 | } |
| 301 | if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 302 | bb_error_msg(err_str, "is not ARPable"); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 303 | return (option_mask32 & DAD ? 0 : 2); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 307 | /* if (!inet_aton(target, &dst)) - not needed */ { |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 308 | len_and_sockaddr *lsa; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 309 | lsa = xhost_and_af2sockaddr(target, 0, AF_INET); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 310 | memcpy(&dst, &lsa->u.sin.sin_addr.s_addr, 4); |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 311 | if (ENABLE_FEATURE_CLEAN_UP) |
| 312 | free(lsa); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 315 | if (source && !inet_aton(source, &src)) { |
Rob Landley | 8ea5205 | 2006-03-30 21:50:57 +0000 | [diff] [blame] | 316 | bb_error_msg_and_die("invalid source address %s", source); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 319 | if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0) |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 320 | src = dst; |
| 321 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 322 | if (!(option_mask32 & DAD) || src.s_addr) { |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 323 | struct sockaddr_in saddr; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 324 | int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 325 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 326 | if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1) |
| 327 | bb_perror_msg("cannot bind to device %s", device); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 328 | memset(&saddr, 0, sizeof(saddr)); |
| 329 | saddr.sin_family = AF_INET; |
| 330 | if (src.s_addr) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 331 | /* Check that this is indeed our IP */ |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 332 | saddr.sin_addr = src; |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 333 | xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 334 | } else { /* !(option_mask32 & DAD) case */ |
| 335 | /* Find IP address on this iface */ |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 336 | socklen_t alen = sizeof(saddr); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 337 | |
| 338 | saddr.sin_port = htons(1025); |
| 339 | saddr.sin_addr = dst; |
| 340 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 341 | if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1) |
Denis Vlasenko | fff9b69 | 2007-11-23 09:15:26 +0000 | [diff] [blame] | 342 | bb_perror_msg("setsockopt(SO_DONTROUTE)"); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 343 | xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); |
| 344 | if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 345 | bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 346 | } |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 347 | if (saddr.sin_family != AF_INET) |
| 348 | bb_error_msg_and_die("no IP address configured"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 349 | src = saddr.sin_addr; |
| 350 | } |
| 351 | close(probe_fd); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 352 | } |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 353 | |
| 354 | me.sll_family = AF_PACKET; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 355 | //me.sll_ifindex = ifindex; - done before |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 356 | me.sll_protocol = htons(ETH_P_ARP); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 357 | xbind(sock_fd, (struct sockaddr *) &me, sizeof(me)); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 358 | |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 359 | { |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 360 | socklen_t alen = sizeof(me); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 362 | if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) { |
| 363 | bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | if (me.sll_halen == 0) { |
Bernhard Reutner-Fischer | f536b99 | 2008-02-11 13:26:54 +0000 | [diff] [blame] | 367 | bb_error_msg(err_str, "is not ARPable (no ll address)"); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 368 | return (option_mask32 & DAD ? 0 : 2); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 369 | } |
| 370 | he = me; |
| 371 | memset(he.sll_addr, -1, he.sll_halen); |
| 372 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 373 | if (!(option_mask32 & QUIET)) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 374 | /* inet_ntoa uses static storage, can't use in same printf */ |
| 375 | printf("ARPING to %s", inet_ntoa(dst)); |
| 376 | printf(" from %s via %s\n", inet_ntoa(src), device); |
Glenn L McGrath | a837e2d | 2003-02-09 07:01:33 +0000 | [diff] [blame] | 377 | } |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 379 | signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish); |
| 380 | signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 381 | |
| 382 | catcher(); |
| 383 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 384 | packet = xmalloc(4096); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 385 | while (1) { |
| 386 | sigset_t sset, osset; |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 387 | struct sockaddr_ll from; |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 388 | socklen_t alen = sizeof(from); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 389 | int cc; |
| 390 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 391 | cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen); |
Denis Vlasenko | 1dc1b37 | 2006-12-23 02:48:44 +0000 | [diff] [blame] | 392 | if (cc < 0) { |
Denis Vlasenko | a65a177 | 2006-09-23 12:46:30 +0000 | [diff] [blame] | 393 | bb_perror_msg("recvfrom"); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 394 | continue; |
| 395 | } |
| 396 | sigemptyset(&sset); |
| 397 | sigaddset(&sset, SIGALRM); |
| 398 | sigaddset(&sset, SIGINT); |
| 399 | sigprocmask(SIG_BLOCK, &sset, &osset); |
| 400 | recv_pack(packet, cc, &from); |
| 401 | sigprocmask(SIG_SETMASK, &osset, NULL); |
Glenn L McGrath | 9e59841 | 2003-01-09 10:06:01 +0000 | [diff] [blame] | 402 | } |
| 403 | } |