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