blob: ab39c717b6c7249a5ff4ff6dd73555fdd20d7ef5 [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9e598412003-01-09 10:06:01 +00002/*
3 * arping.c - Ping hosts by ARP requests/replies
4 *
Rob Landley8ea52052006-03-30 21:50:57 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9e598412003-01-09 10:06:01 +00006 *
7 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
9 */
10
Glenn L McGrath9e598412003-01-09 10:06:01 +000011#include <arpa/inet.h>
12#include <net/if.h>
13#include <netinet/ether.h>
14#include <netpacket/packet.h>
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Glenn L McGrath9e598412003-01-09 10:06:01 +000017
Denis Vlasenko459be352007-06-17 19:09:05 +000018/* We don't expect to see 1000+ seconds delay, unsigned is enough */
19#define MONOTONIC_US() ((unsigned)monotonic_us())
20
Denis Vlasenko459be352007-06-17 19:09:05 +000021enum {
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 Landley8ea52052006-03-30 21:50:57 +000029};
Rob Landley8ea52052006-03-30 21:50:57 +000030
Denis Vlasenkofff9b692007-11-23 09:15:26 +000031struct globals {
32 struct in_addr src;
33 struct in_addr dst;
34 struct sockaddr_ll me;
35 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000036 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000037
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 Vlasenkoa09300a2007-11-25 12:40:56 +000054#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000055#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 )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000064#define INIT_G() do { \
65 count = -1; \
66} while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000067
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000068// If GNUisms are not available...
69//static void *mempcpy(void *_dst, const void *_src, int n)
70//{
71// memcpy(_dst, _src, n);
72// return (char*)_dst + n;
73//}
74
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000075static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000076 struct in_addr *dst_addr, struct sockaddr_ll *ME,
77 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000078{
79 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000080 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000081 struct arphdr *ah = (struct arphdr *) buf;
82 unsigned char *p = (unsigned char *) (ah + 1);
83
Glenn L McGrath9e598412003-01-09 10:06:01 +000084 ah->ar_hrd = htons(ARPHRD_ETHER);
85 ah->ar_pro = htons(ETH_P_IP);
86 ah->ar_hln = ME->sll_halen;
87 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000088 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000089
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000090 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
91 p = mempcpy(p, src_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +000092
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000093 if (option_mask32 & ADVERT)
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000094 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +000095 else
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000096 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +000097
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000098 p = mempcpy(p, dst_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +000099
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000100 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000101 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000102 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000104 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105 brd_sent++;
106 }
107 return err;
108}
109
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000110static void finish(void) NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000111static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000112{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000113 if (!(option_mask32 & QUIET)) {
114 printf("Sent %u probe(s) (%u broadcast(s))\n"
115 "Received %u repl%s"
116 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000117 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000118 received, (received == 1) ? "ies" : "y",
119 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000120 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000121 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000122 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000123 if (option_mask32 & UNSOLICITED)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000124 exit(EXIT_SUCCESS);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000125 exit(!received);
126}
127
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000128static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000129{
Denis Vlasenko459be352007-06-17 19:09:05 +0000130 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000131
Denis Vlasenko459be352007-06-17 19:09:05 +0000132 now = MONOTONIC_US();
133 if (start == 0)
134 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000135
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000136 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000137 finish();
138
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000139 /* count < 0 means "infinite count" */
140 if (count > 0)
141 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000142
143 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000144 send_pack(&src, &dst, &me, &he);
145 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000146 finish();
147 }
148 alarm(1);
149}
150
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000151static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000152{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000153 struct arphdr *ah = (struct arphdr *) buf;
154 unsigned char *p = (unsigned char *) (ah + 1);
155 struct in_addr src_ip, dst_ip;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000156 /* moves below assume in_addr is 4 bytes big, ensure that */
157 struct BUG_in_addr_must_be_4 {
158 char BUG_in_addr_must_be_4[
159 sizeof(struct in_addr) == 4 ? 1 : -1
160 ];
161 char BUG_s_addr_must_be_4[
162 sizeof(src_ip.s_addr) == 4 ? 1 : -1
163 ];
164 };
Glenn L McGrath9e598412003-01-09 10:06:01 +0000165
Glenn L McGrath9e598412003-01-09 10:06:01 +0000166 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000167 if (FROM->sll_pkttype != PACKET_HOST
168 && FROM->sll_pkttype != PACKET_BROADCAST
169 && FROM->sll_pkttype != PACKET_MULTICAST)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000170 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000171
172 /* Only these types are recognised */
173 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000174 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000175
176 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000177 if (ah->ar_hrd != htons(FROM->sll_hatype)
178 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000179 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000180
181 /* Protocol must be IP. */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000182 if (ah->ar_pro != htons(ETH_P_IP)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000183 || (ah->ar_pln != 4)
184 || (ah->ar_hln != me.sll_halen)
185 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000186 return false;
187
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000188 move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
189 move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000190
191 if (dst.s_addr != src_ip.s_addr)
192 return false;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000193 if (!(option_mask32 & DAD)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000194 if ((src.s_addr != dst_ip.s_addr)
195 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
196 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000197 } else {
198 /* DAD packet was:
199 src_ip = 0 (or some src)
200 src_hw = ME
201 dst_ip = tested address
202 dst_hw = <unspec>
203
204 We fail, if receive request/reply with:
205 src_ip = tested_address
206 src_hw != ME
207 if src_ip in request was not zero, check
208 also that it matches to dst_ip, otherwise
209 dst_ip/dst_hw do not matter.
210 */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000211 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000212 || (src.s_addr && src.s_addr != dst_ip.s_addr))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000213 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000214 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000215 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000216 int s_printed = 0;
217
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000218 printf("%scast re%s from %s [%s]",
219 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
220 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000221 inet_ntoa(src_ip),
222 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000223 if (dst_ip.s_addr != src.s_addr) {
224 printf("for %s ", inet_ntoa(dst_ip));
225 s_printed = 1;
226 }
227 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
228 if (!s_printed)
229 printf("for ");
230 printf("[%s]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000231 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000232 }
Rob Landley8ea52052006-03-30 21:50:57 +0000233
Denis Vlasenko459be352007-06-17 19:09:05 +0000234 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000235 unsigned diff = MONOTONIC_US() - last;
236 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000237 } else {
238 printf(" UNSOLICITED?\n");
239 }
240 fflush(stdout);
241 }
242 received++;
243 if (FROM->sll_pkttype != PACKET_HOST)
244 brd_recv++;
245 if (ah->ar_op == htons(ARPOP_REQUEST))
246 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000247 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000248 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000249 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000250 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000251 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000252 }
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000253 return true;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000254}
255
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000256int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000257int arping_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000258{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000259 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000260 char *source = NULL;
261 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000262 unsigned char *packet;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000263 char *err_str;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000264
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000265 INIT_G();
266
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000267 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000268
Rob Landleyafb94ec2006-07-16 08:06:34 +0000269 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000270 // Need to remove SUID_NEVER from applets.h for this to work
271 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000272
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000273 err_str = xasprintf("interface %s %%s", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000274 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000275 unsigned opt;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000276 char *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000277
278 /* Dad also sets quit_on_reply.
279 * Advert also sets unsolicited.
280 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000281 opt_complementary = "=1:Df:AU:c+";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000282 opt = getopt32(argv, "DUAqfbc:w:I:s:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000283 &count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000284 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000285 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000286 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000287 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000288 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000289
Denis Vlasenko459be352007-06-17 19:09:05 +0000290 target = argv[optind];
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000291
Denis Vlasenko40920822006-10-03 20:28:06 +0000292 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000293
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000294 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000295 struct ifreq ifr;
296
297 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000298 strncpy_IFNAMSIZ(ifr.ifr_name, device);
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000299 /* We use ifr.ifr_name in error msg so that problem
300 * with truncated name will be visible */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000301 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000302 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000303
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000304 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000305
Glenn L McGrath9e598412003-01-09 10:06:01 +0000306 if (!(ifr.ifr_flags & IFF_UP)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000307 bb_error_msg_and_die(err_str, "is down");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000308 }
309 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000310 bb_error_msg(err_str, "is not ARPable");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000311 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000312 }
313 }
314
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000315 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000316 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000317 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000318 dst = lsa->u.sin.sin_addr;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000319 if (ENABLE_FEATURE_CLEAN_UP)
320 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000321 }
322
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000323 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000324 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000325 }
326
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000327 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000328 src = dst;
329
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000330 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000331 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000332 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000333
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000334 setsockopt_bindtodevice(probe_fd, device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000335 memset(&saddr, 0, sizeof(saddr));
336 saddr.sin_family = AF_INET;
337 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000338 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000339 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000340 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000341 } else { /* !(option_mask32 & DAD) case */
342 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000343 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000344
345 saddr.sin_port = htons(1025);
346 saddr.sin_addr = dst;
347
Denis Vlasenko703e2022007-01-22 14:12:08 +0000348 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000349 bb_perror_msg("setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000350 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000351 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
352 //never happens:
353 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
354 // bb_perror_msg_and_die("getsockname");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000355 if (saddr.sin_family != AF_INET)
356 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000357 src = saddr.sin_addr;
358 }
359 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000360 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000361
362 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000363 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000364 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000365 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000366
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000367 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000368 socklen_t alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000369 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
370 //never happens:
371 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
372 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000373 }
374 if (me.sll_halen == 0) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000375 bb_error_msg(err_str, "is not ARPable (no ll address)");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000376 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000377 }
378 he = me;
379 memset(he.sll_addr, -1, he.sll_halen);
380
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000381 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000382 /* inet_ntoa uses static storage, can't use in same printf */
383 printf("ARPING to %s", inet_ntoa(dst));
384 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000385 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000386
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000387 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
388 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000389
390 catcher();
391
Denis Vlasenko459be352007-06-17 19:09:05 +0000392 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000393 while (1) {
394 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000395 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000396 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000397 int cc;
398
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000399 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000400 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000401 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000402 continue;
403 }
404 sigemptyset(&sset);
405 sigaddset(&sset, SIGALRM);
406 sigaddset(&sset, SIGINT);
407 sigprocmask(SIG_BLOCK, &sset, &osset);
408 recv_pack(packet, cc, &from);
409 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000410 }
411}