blob: 6f6b59cfba73be911be31bd9b4459ed1464d61a5 [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9e598412003-01-09 10:06:01 +00004 *
Denys Vlasenko11872ca2010-10-30 02:24:48 +02005 * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9e598412003-01-09 10:06:01 +00006 * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
7 */
8
Glenn L McGrath9e598412003-01-09 10:06:01 +00009#include <arpa/inet.h>
10#include <net/if.h>
11#include <netinet/ether.h>
12#include <netpacket/packet.h>
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Glenn L McGrath9e598412003-01-09 10:06:01 +000015
Denis Vlasenko459be352007-06-17 19:09:05 +000016/* We don't expect to see 1000+ seconds delay, unsigned is enough */
17#define MONOTONIC_US() ((unsigned)monotonic_us())
18
Denis Vlasenko459be352007-06-17 19:09:05 +000019enum {
20 DAD = 1,
21 UNSOLICITED = 2,
22 ADVERT = 4,
23 QUIET = 8,
24 QUIT_ON_REPLY = 16,
25 BCAST_ONLY = 32,
26 UNICASTING = 64
Rob Landley8ea52052006-03-30 21:50:57 +000027};
Rob Landley8ea52052006-03-30 21:50:57 +000028
Denis Vlasenkofff9b692007-11-23 09:15:26 +000029struct globals {
30 struct in_addr src;
31 struct in_addr dst;
32 struct sockaddr_ll me;
33 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000034 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000035
36 int count; // = -1;
37 unsigned last;
38 unsigned timeout_us;
39 unsigned start;
40
41 unsigned sent;
42 unsigned brd_sent;
43 unsigned received;
44 unsigned brd_recv;
45 unsigned req_recv;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010046} FIX_ALIASING;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000047#define G (*(struct globals*)&bb_common_bufsiz1)
48#define src (G.src )
49#define dst (G.dst )
50#define me (G.me )
51#define he (G.he )
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000052#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000053#define count (G.count )
54#define last (G.last )
55#define timeout_us (G.timeout_us)
56#define start (G.start )
57#define sent (G.sent )
58#define brd_sent (G.brd_sent )
59#define received (G.received )
60#define brd_recv (G.brd_recv )
61#define req_recv (G.req_recv )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000062#define INIT_G() do { \
63 count = -1; \
64} while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000065
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000066// If GNUisms are not available...
67//static void *mempcpy(void *_dst, const void *_src, int n)
68//{
69// memcpy(_dst, _src, n);
70// return (char*)_dst + n;
71//}
72
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000073static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000074 struct in_addr *dst_addr, struct sockaddr_ll *ME,
75 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000076{
77 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000078 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000079 struct arphdr *ah = (struct arphdr *) buf;
80 unsigned char *p = (unsigned char *) (ah + 1);
81
Glenn L McGrath9e598412003-01-09 10:06:01 +000082 ah->ar_hrd = htons(ARPHRD_ETHER);
83 ah->ar_pro = htons(ETH_P_IP);
84 ah->ar_hln = ME->sll_halen;
85 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000086 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +000087
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000088 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
89 p = mempcpy(p, src_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +000090
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000091 if (option_mask32 & ADVERT)
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000092 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +000093 else
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000094 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +000095
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000096 p = mempcpy(p, dst_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +000097
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000098 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +000099 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000100 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000101 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000102 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103 brd_sent++;
104 }
105 return err;
106}
107
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000108static void finish(void) NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000109static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000110{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000111 if (!(option_mask32 & QUIET)) {
112 printf("Sent %u probe(s) (%u broadcast(s))\n"
113 "Received %u repl%s"
114 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000115 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000116 received, (received == 1) ? "ies" : "y",
117 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000118 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000119 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000120 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000121 if (option_mask32 & UNSOLICITED)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000122 exit(EXIT_SUCCESS);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000123 exit(!received);
124}
125
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000126static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000127{
Denis Vlasenko459be352007-06-17 19:09:05 +0000128 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000129
Denis Vlasenko459be352007-06-17 19:09:05 +0000130 now = MONOTONIC_US();
131 if (start == 0)
132 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000133
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000134 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000135 finish();
136
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000137 /* count < 0 means "infinite count" */
138 if (count > 0)
139 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000140
141 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000142 send_pack(&src, &dst, &me, &he);
143 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000144 finish();
145 }
146 alarm(1);
147}
148
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000149static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000150{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000151 struct arphdr *ah = (struct arphdr *) buf;
152 unsigned char *p = (unsigned char *) (ah + 1);
153 struct in_addr src_ip, dst_ip;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000154 /* moves below assume in_addr is 4 bytes big, ensure that */
155 struct BUG_in_addr_must_be_4 {
156 char BUG_in_addr_must_be_4[
157 sizeof(struct in_addr) == 4 ? 1 : -1
158 ];
159 char BUG_s_addr_must_be_4[
160 sizeof(src_ip.s_addr) == 4 ? 1 : -1
161 ];
162 };
Glenn L McGrath9e598412003-01-09 10:06:01 +0000163
Glenn L McGrath9e598412003-01-09 10:06:01 +0000164 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000165 if (FROM->sll_pkttype != PACKET_HOST
166 && FROM->sll_pkttype != PACKET_BROADCAST
167 && FROM->sll_pkttype != PACKET_MULTICAST)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000168 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000169
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200170 /* Only these types are recognized */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000171 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000172 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000173
174 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000175 if (ah->ar_hrd != htons(FROM->sll_hatype)
176 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000177 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000178
179 /* Protocol must be IP. */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000180 if (ah->ar_pro != htons(ETH_P_IP)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000181 || (ah->ar_pln != 4)
182 || (ah->ar_hln != me.sll_halen)
183 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000184 return false;
185
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000186 move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
187 move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000188
189 if (dst.s_addr != src_ip.s_addr)
190 return false;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000191 if (!(option_mask32 & DAD)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000192 if ((src.s_addr != dst_ip.s_addr)
193 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
194 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000195 } else {
196 /* DAD packet was:
197 src_ip = 0 (or some src)
198 src_hw = ME
199 dst_ip = tested address
200 dst_hw = <unspec>
201
202 We fail, if receive request/reply with:
203 src_ip = tested_address
204 src_hw != ME
205 if src_ip in request was not zero, check
206 also that it matches to dst_ip, otherwise
207 dst_ip/dst_hw do not matter.
208 */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000209 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000210 || (src.s_addr && src.s_addr != dst_ip.s_addr))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000211 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000212 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000213 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000214 int s_printed = 0;
215
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000216 printf("%scast re%s from %s [%s]",
217 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
218 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000219 inet_ntoa(src_ip),
220 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000221 if (dst_ip.s_addr != src.s_addr) {
222 printf("for %s ", inet_ntoa(dst_ip));
223 s_printed = 1;
224 }
225 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
226 if (!s_printed)
227 printf("for ");
228 printf("[%s]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000229 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000230 }
Rob Landley8ea52052006-03-30 21:50:57 +0000231
Denis Vlasenko459be352007-06-17 19:09:05 +0000232 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000233 unsigned diff = MONOTONIC_US() - last;
234 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000235 } else {
236 printf(" UNSOLICITED?\n");
237 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100238 fflush_all();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000239 }
240 received++;
241 if (FROM->sll_pkttype != PACKET_HOST)
242 brd_recv++;
243 if (ah->ar_op == htons(ARPOP_REQUEST))
244 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000245 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000246 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000247 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000248 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000249 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000250 }
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000251 return true;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000252}
253
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000254int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000255int arping_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000256{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000257 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000258 char *source = NULL;
259 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000260 unsigned char *packet;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000261 char *err_str;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000262
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000263 INIT_G();
264
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000265 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000266
Rob Landleyafb94ec2006-07-16 08:06:34 +0000267 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000268 // Need to remove SUID_NEVER from applets.h for this to work
269 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000270
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000271 err_str = xasprintf("interface %s %%s", device);
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000272 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000273 unsigned opt;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000274 char *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000275
276 /* Dad also sets quit_on_reply.
277 * Advert also sets unsolicited.
278 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000279 opt_complementary = "=1:Df:AU:c+";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000280 opt = getopt32(argv, "DUAqfbc:w:I:s:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000281 &count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000282 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000283 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000284 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000285 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000286 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000287
Denis Vlasenko459be352007-06-17 19:09:05 +0000288 target = argv[optind];
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000289
Denis Vlasenko40920822006-10-03 20:28:06 +0000290 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000291
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000292 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000293 struct ifreq ifr;
294
295 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000296 strncpy_IFNAMSIZ(ifr.ifr_name, device);
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000297 /* We use ifr.ifr_name in error msg so that problem
298 * with truncated name will be visible */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000299 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000300 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000301
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000302 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000303
Glenn L McGrath9e598412003-01-09 10:06:01 +0000304 if (!(ifr.ifr_flags & IFF_UP)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000305 bb_error_msg_and_die(err_str, "is down");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000306 }
307 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000308 bb_error_msg(err_str, "is not ARPable");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000309 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000310 }
311 }
312
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000313 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000314 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000315 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000316 dst = lsa->u.sin.sin_addr;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000317 if (ENABLE_FEATURE_CLEAN_UP)
318 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000319 }
320
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000321 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000322 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000323 }
324
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000325 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000326 src = dst;
327
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000328 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000329 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000330 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000331
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000332 setsockopt_bindtodevice(probe_fd, device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000333 memset(&saddr, 0, sizeof(saddr));
334 saddr.sin_family = AF_INET;
335 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000336 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000337 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000338 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000339 } else { /* !(option_mask32 & DAD) case */
340 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000341 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000342
343 saddr.sin_port = htons(1025);
344 saddr.sin_addr = dst;
345
Denis Vlasenko703e2022007-01-22 14:12:08 +0000346 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000347 bb_perror_msg("setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000348 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000349 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
350 //never happens:
351 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
352 // bb_perror_msg_and_die("getsockname");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000353 if (saddr.sin_family != AF_INET)
354 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000355 src = saddr.sin_addr;
356 }
357 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000358 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359
360 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000361 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000362 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000363 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000364
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000365 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000366 socklen_t alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000367 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
368 //never happens:
369 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
370 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000371 }
372 if (me.sll_halen == 0) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000373 bb_error_msg(err_str, "is not ARPable (no ll address)");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000374 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000375 }
376 he = me;
377 memset(he.sll_addr, -1, he.sll_halen);
378
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000379 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000380 /* inet_ntoa uses static storage, can't use in same printf */
381 printf("ARPING to %s", inet_ntoa(dst));
382 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000383 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000384
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000385 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
386 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000387
388 catcher();
389
Denis Vlasenko459be352007-06-17 19:09:05 +0000390 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000391 while (1) {
392 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000393 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000394 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000395 int cc;
396
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000397 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000398 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000399 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000400 continue;
401 }
402 sigemptyset(&sset);
403 sigaddset(&sset, SIGALRM);
404 sigaddset(&sset, SIGINT);
405 sigprocmask(SIG_BLOCK, &sset, &osset);
406 recv_pack(packet, cc, &from);
407 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000408 }
409}