blob: 46bd65e3610207e9c0ed8fa89c454780f0bdcf9e [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
Pere Orga5bc8c002011-04-11 03:29:49 +02009//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 Orga5bc8c002011-04-11 03:29:49 +020013//usage: "\n -f Quit on first ARP reply"
14//usage: "\n -q Quiet"
15//usage: "\n -b Keep broadcasting, don't go unicast"
Denys Vlasenkofb527692015-08-16 19:56:16 +020016//usage: "\n -D Exit with 1 if DST_IP replies"
Pere Orga5bc8c002011-04-11 03:29:49 +020017//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"
Denys Vlasenkofb527692015-08-16 19:56:16 +020020//usage: "\n -w TIMEOUT Seconds to wait for ARP reply"
Pere Orga5bc8c002011-04-11 03:29:49 +020021//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 McGrath9e598412003-01-09 10:06:01 +000025#include <arpa/inet.h>
26#include <net/if.h>
27#include <netinet/ether.h>
28#include <netpacket/packet.h>
29
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020031#include "common_bufsiz.h"
Glenn L McGrath9e598412003-01-09 10:06:01 +000032
Denis Vlasenko459be352007-06-17 19:09:05 +000033/* We don't expect to see 1000+ seconds delay, unsigned is enough */
34#define MONOTONIC_US() ((unsigned)monotonic_us())
35
Denis Vlasenko459be352007-06-17 19:09:05 +000036enum {
37 DAD = 1,
38 UNSOLICITED = 2,
39 ADVERT = 4,
40 QUIET = 8,
41 QUIT_ON_REPLY = 16,
42 BCAST_ONLY = 32,
43 UNICASTING = 64
Rob Landley8ea52052006-03-30 21:50:57 +000044};
Rob Landley8ea52052006-03-30 21:50:57 +000045
Denis Vlasenkofff9b692007-11-23 09:15:26 +000046struct globals {
47 struct in_addr src;
48 struct in_addr dst;
49 struct sockaddr_ll me;
50 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000051 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000052
53 int count; // = -1;
54 unsigned last;
55 unsigned timeout_us;
56 unsigned start;
57
58 unsigned sent;
59 unsigned brd_sent;
60 unsigned received;
61 unsigned brd_recv;
62 unsigned req_recv;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010063} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020064#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +000065#define src (G.src )
66#define dst (G.dst )
67#define me (G.me )
68#define he (G.he )
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000069#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000070#define count (G.count )
71#define last (G.last )
72#define timeout_us (G.timeout_us)
73#define start (G.start )
74#define sent (G.sent )
75#define brd_sent (G.brd_sent )
76#define received (G.received )
77#define brd_recv (G.brd_recv )
78#define req_recv (G.req_recv )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000079#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020080 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +000081 count = -1; \
82} while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000083
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000084// If GNUisms are not available...
85//static void *mempcpy(void *_dst, const void *_src, int n)
86//{
87// memcpy(_dst, _src, n);
88// return (char*)_dst + n;
89//}
90
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000091static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000092 struct in_addr *dst_addr, struct sockaddr_ll *ME,
93 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000094{
95 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000096 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000097 struct arphdr *ah = (struct arphdr *) buf;
98 unsigned char *p = (unsigned char *) (ah + 1);
99
Glenn L McGrath9e598412003-01-09 10:06:01 +0000100 ah->ar_hrd = htons(ARPHRD_ETHER);
101 ah->ar_pro = htons(ETH_P_IP);
102 ah->ar_hln = ME->sll_halen;
103 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000104 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000106 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
107 p = mempcpy(p, src_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000108
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000109 if (option_mask32 & ADVERT)
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000110 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000111 else
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000112 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000113
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000114 p = mempcpy(p, dst_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000115
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000116 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000117 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000118 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000119 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000120 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000121 brd_sent++;
122 }
123 return err;
124}
125
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000126static void finish(void) NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000127static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000128{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000129 if (!(option_mask32 & QUIET)) {
130 printf("Sent %u probe(s) (%u broadcast(s))\n"
131 "Received %u repl%s"
132 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000133 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000134 received, (received == 1) ? "ies" : "y",
135 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000136 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000137 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000138 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000139 if (option_mask32 & UNSOLICITED)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000140 exit(EXIT_SUCCESS);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000141 exit(!received);
142}
143
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000144static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000145{
Denis Vlasenko459be352007-06-17 19:09:05 +0000146 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000147
Denis Vlasenko459be352007-06-17 19:09:05 +0000148 now = MONOTONIC_US();
149 if (start == 0)
150 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000151
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000152 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000153 finish();
154
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000155 /* count < 0 means "infinite count" */
156 if (count > 0)
157 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000158
159 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000160 send_pack(&src, &dst, &me, &he);
161 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000162 finish();
163 }
164 alarm(1);
165}
166
Denys Vlasenkofb527692015-08-16 19:56:16 +0200167static void recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000168{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000169 struct arphdr *ah = (struct arphdr *) buf;
170 unsigned char *p = (unsigned char *) (ah + 1);
171 struct in_addr src_ip, dst_ip;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000172 /* moves below assume in_addr is 4 bytes big, ensure that */
173 struct BUG_in_addr_must_be_4 {
174 char BUG_in_addr_must_be_4[
175 sizeof(struct in_addr) == 4 ? 1 : -1
176 ];
177 char BUG_s_addr_must_be_4[
178 sizeof(src_ip.s_addr) == 4 ? 1 : -1
179 ];
180 };
Glenn L McGrath9e598412003-01-09 10:06:01 +0000181
Glenn L McGrath9e598412003-01-09 10:06:01 +0000182 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000183 if (FROM->sll_pkttype != PACKET_HOST
184 && FROM->sll_pkttype != PACKET_BROADCAST
185 && FROM->sll_pkttype != PACKET_MULTICAST)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200186 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000187
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200188 /* Only these types are recognized */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000189 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200190 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000191
192 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000193 if (ah->ar_hrd != htons(FROM->sll_hatype)
194 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200195 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000196
197 /* Protocol must be IP. */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000198 if (ah->ar_pro != htons(ETH_P_IP)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000199 || (ah->ar_pln != 4)
200 || (ah->ar_hln != me.sll_halen)
201 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200202 return;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000203
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000204 move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
205 move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000206
207 if (dst.s_addr != src_ip.s_addr)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200208 return;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000209 if (!(option_mask32 & DAD)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000210 if ((src.s_addr != dst_ip.s_addr)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200211 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
212 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000213 } else {
214 /* DAD packet was:
215 src_ip = 0 (or some src)
216 src_hw = ME
217 dst_ip = tested address
218 dst_hw = <unspec>
219
220 We fail, if receive request/reply with:
221 src_ip = tested_address
222 src_hw != ME
223 if src_ip in request was not zero, check
224 also that it matches to dst_ip, otherwise
225 dst_ip/dst_hw do not matter.
226 */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000227 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000228 || (src.s_addr && src.s_addr != dst_ip.s_addr))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200229 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000230 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000231 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000232 int s_printed = 0;
233
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200234 printf("%scast re%s from %s [%02x:%02x:%02x:%02x:%02x:%02x]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000235 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
236 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000237 inet_ntoa(src_ip),
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200238 p[0], p[1], p[2], p[3], p[4], p[5]
239 );
Glenn L McGrath9e598412003-01-09 10:06:01 +0000240 if (dst_ip.s_addr != src.s_addr) {
241 printf("for %s ", inet_ntoa(dst_ip));
242 s_printed = 1;
243 }
244 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200245 unsigned char *pp = p + ah->ar_hln + 4;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000246 if (!s_printed)
247 printf("for ");
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200248 printf("[%02x:%02x:%02x:%02x:%02x:%02x]",
249 pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]
250 );
Glenn L McGrath9e598412003-01-09 10:06:01 +0000251 }
Rob Landley8ea52052006-03-30 21:50:57 +0000252
Denis Vlasenko459be352007-06-17 19:09:05 +0000253 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000254 unsigned diff = MONOTONIC_US() - last;
255 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000256 } else {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200257 puts(" UNSOLICITED?");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000258 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100259 fflush_all();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000260 }
261 received++;
262 if (FROM->sll_pkttype != PACKET_HOST)
263 brd_recv++;
264 if (ah->ar_op == htons(ARPOP_REQUEST))
265 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000266 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000267 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000268 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000269 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000270 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000271 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000272}
273
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000274int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000275int arping_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000276{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000277 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000278 char *source = NULL;
279 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000280 unsigned char *packet;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000281 char *err_str;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000282
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000283 INIT_G();
284
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000285 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000286
Rob Landleyafb94ec2006-07-16 08:06:34 +0000287 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000288 // Need to remove SUID_NEVER from applets.h for this to work
289 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000290
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000291 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000292 unsigned opt;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000293 char *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000294
295 /* Dad also sets quit_on_reply.
296 * Advert also sets unsolicited.
297 */
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200298 opt_complementary = "=1:Df:AU";
299 opt = getopt32(argv, "DUAqfbc:+w:I:s:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000300 &count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000301 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000302 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000303 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000304 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000305 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000306
Denis Vlasenko459be352007-06-17 19:09:05 +0000307 target = argv[optind];
Alexander Korolkov748fb602015-03-12 13:05:33 +0100308 err_str = xasprintf("interface %s %%s", device);
Denis Vlasenko40920822006-10-03 20:28:06 +0000309 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000310
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000311 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000312 struct ifreq ifr;
313
314 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000315 strncpy_IFNAMSIZ(ifr.ifr_name, device);
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000316 /* We use ifr.ifr_name in error msg so that problem
317 * with truncated name will be visible */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000318 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000319 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000320
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000321 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000322
Glenn L McGrath9e598412003-01-09 10:06:01 +0000323 if (!(ifr.ifr_flags & IFF_UP)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000324 bb_error_msg_and_die(err_str, "is down");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000325 }
326 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000327 bb_error_msg(err_str, "is not ARPable");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000328 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000329 }
330 }
331
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000332 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000333 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000334 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000335 dst = lsa->u.sin.sin_addr;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000336 if (ENABLE_FEATURE_CLEAN_UP)
337 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000338 }
339
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000340 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000341 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000342 }
343
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000344 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000345 src = dst;
346
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000347 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000348 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000349 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000350
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000351 setsockopt_bindtodevice(probe_fd, device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000352 memset(&saddr, 0, sizeof(saddr));
353 saddr.sin_family = AF_INET;
354 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000355 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000356 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000357 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000358 } else { /* !(option_mask32 & DAD) case */
359 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000360 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000361
362 saddr.sin_port = htons(1025);
363 saddr.sin_addr = dst;
364
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +0200365 if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0)
Denys Vlasenko2db782b2015-08-24 19:08:14 +0200366 bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000367 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000368 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
369 //never happens:
370 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
371 // bb_perror_msg_and_die("getsockname");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000372 if (saddr.sin_family != AF_INET)
373 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000374 src = saddr.sin_addr;
375 }
376 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000377 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000378
379 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000380 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000381 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000382 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000383
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000384 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000385 socklen_t alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000386 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
387 //never happens:
388 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
389 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000390 }
391 if (me.sll_halen == 0) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000392 bb_error_msg(err_str, "is not ARPable (no ll address)");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000393 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000394 }
395 he = me;
396 memset(he.sll_addr, -1, he.sll_halen);
397
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000398 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000399 /* inet_ntoa uses static storage, can't use in same printf */
400 printf("ARPING to %s", inet_ntoa(dst));
401 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000402 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000403
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000404 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
405 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000406
407 catcher();
408
Denis Vlasenko459be352007-06-17 19:09:05 +0000409 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000410 while (1) {
411 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000412 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000413 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000414 int cc;
415
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000416 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000417 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000418 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000419 continue;
420 }
421 sigemptyset(&sset);
422 sigaddset(&sset, SIGALRM);
423 sigaddset(&sset, SIGINT);
424 sigprocmask(SIG_BLOCK, &sset, &osset);
425 recv_pack(packet, cc, &from);
426 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000427 }
428}