blob: 71672957e4eed94a18a5dad3f63207e448403ba8 [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 */
Denys Vlasenko47367e12016-11-23 09:05:14 +01008//config:config ARPING
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "arping (9.3 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010010//config: default y
11//config: select PLATFORM_LINUX
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Ping hosts by ARP packets.
Denys Vlasenko47367e12016-11-23 09:05:14 +010014//config:
15
16//applet:IF_ARPING(APPLET(arping, BB_DIR_USR_SBIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_ARPING) += arping.o
Glenn L McGrath9e598412003-01-09 10:06:01 +000019
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define arping_trivial_usage
21//usage: "[-fqbDUA] [-c CNT] [-w TIMEOUT] [-I IFACE] [-s SRC_IP] DST_IP"
22//usage:#define arping_full_usage "\n\n"
23//usage: "Send ARP requests/replies\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n -f Quit on first ARP reply"
25//usage: "\n -q Quiet"
26//usage: "\n -b Keep broadcasting, don't go unicast"
Denys Vlasenkofb527692015-08-16 19:56:16 +020027//usage: "\n -D Exit with 1 if DST_IP replies"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -U Unsolicited ARP mode, update your neighbors"
29//usage: "\n -A ARP answer mode, update your neighbors"
30//usage: "\n -c N Stop after sending N ARP requests"
Denys Vlasenkofb527692015-08-16 19:56:16 +020031//usage: "\n -w TIMEOUT Seconds to wait for ARP reply"
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: "\n -I IFACE Interface to use (default eth0)"
33//usage: "\n -s SRC_IP Sender IP address"
34//usage: "\n DST_IP Target IP address"
35
Glenn L McGrath9e598412003-01-09 10:06:01 +000036#include <arpa/inet.h>
37#include <net/if.h>
38#include <netinet/ether.h>
39#include <netpacket/packet.h>
40
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020042#include "common_bufsiz.h"
Glenn L McGrath9e598412003-01-09 10:06:01 +000043
Denis Vlasenko459be352007-06-17 19:09:05 +000044/* We don't expect to see 1000+ seconds delay, unsigned is enough */
45#define MONOTONIC_US() ((unsigned)monotonic_us())
46
Denis Vlasenko459be352007-06-17 19:09:05 +000047enum {
48 DAD = 1,
49 UNSOLICITED = 2,
50 ADVERT = 4,
51 QUIET = 8,
52 QUIT_ON_REPLY = 16,
53 BCAST_ONLY = 32,
54 UNICASTING = 64
Rob Landley8ea52052006-03-30 21:50:57 +000055};
Rob Landley8ea52052006-03-30 21:50:57 +000056
Denis Vlasenkofff9b692007-11-23 09:15:26 +000057struct globals {
58 struct in_addr src;
59 struct in_addr dst;
60 struct sockaddr_ll me;
61 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000062 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000063
64 int count; // = -1;
65 unsigned last;
66 unsigned timeout_us;
67 unsigned start;
68
69 unsigned sent;
70 unsigned brd_sent;
71 unsigned received;
72 unsigned brd_recv;
73 unsigned req_recv;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010074} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020075#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +000076#define src (G.src )
77#define dst (G.dst )
78#define me (G.me )
79#define he (G.he )
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000080#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000081#define count (G.count )
82#define last (G.last )
83#define timeout_us (G.timeout_us)
84#define start (G.start )
85#define sent (G.sent )
86#define brd_sent (G.brd_sent )
87#define received (G.received )
88#define brd_recv (G.brd_recv )
89#define req_recv (G.req_recv )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000090#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020091 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +000092 count = -1; \
93} while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000094
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000095// If GNUisms are not available...
96//static void *mempcpy(void *_dst, const void *_src, int n)
97//{
98// memcpy(_dst, _src, n);
99// return (char*)_dst + n;
100//}
101
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000102static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000103 struct in_addr *dst_addr, struct sockaddr_ll *ME,
104 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000105{
106 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +0000107 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +0000108 struct arphdr *ah = (struct arphdr *) buf;
109 unsigned char *p = (unsigned char *) (ah + 1);
110
Glenn L McGrath9e598412003-01-09 10:06:01 +0000111 ah->ar_hrd = htons(ARPHRD_ETHER);
112 ah->ar_pro = htons(ETH_P_IP);
113 ah->ar_hln = ME->sll_halen;
114 ah->ar_pln = 4;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000115 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000116
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000117 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
118 p = mempcpy(p, src_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000119
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000120 if (option_mask32 & ADVERT)
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000121 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000122 else
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000123 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000124
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000125 p = mempcpy(p, dst_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000126
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000127 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000128 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000129 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000130 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000131 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000132 brd_sent++;
133 }
134 return err;
135}
136
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000137static void finish(void) NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000138static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000139{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000140 if (!(option_mask32 & QUIET)) {
141 printf("Sent %u probe(s) (%u broadcast(s))\n"
142 "Received %u repl%s"
143 " (%u request(s), %u broadcast(s))\n",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000144 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000145 received, (received == 1) ? "ies" : "y",
146 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000147 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000148 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000149 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000150 if (option_mask32 & UNSOLICITED)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000151 exit(EXIT_SUCCESS);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000152 exit(!received);
153}
154
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000155static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000156{
Denis Vlasenko459be352007-06-17 19:09:05 +0000157 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000158
Denis Vlasenko459be352007-06-17 19:09:05 +0000159 now = MONOTONIC_US();
160 if (start == 0)
161 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000162
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000163 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000164 finish();
165
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000166 /* count < 0 means "infinite count" */
167 if (count > 0)
168 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000169
170 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000171 send_pack(&src, &dst, &me, &he);
172 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000173 finish();
174 }
175 alarm(1);
176}
177
Denys Vlasenkofb527692015-08-16 19:56:16 +0200178static void recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000179{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000180 struct arphdr *ah = (struct arphdr *) buf;
181 unsigned char *p = (unsigned char *) (ah + 1);
182 struct in_addr src_ip, dst_ip;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000183 /* moves below assume in_addr is 4 bytes big, ensure that */
184 struct BUG_in_addr_must_be_4 {
185 char BUG_in_addr_must_be_4[
186 sizeof(struct in_addr) == 4 ? 1 : -1
187 ];
188 char BUG_s_addr_must_be_4[
189 sizeof(src_ip.s_addr) == 4 ? 1 : -1
190 ];
191 };
Glenn L McGrath9e598412003-01-09 10:06:01 +0000192
Glenn L McGrath9e598412003-01-09 10:06:01 +0000193 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000194 if (FROM->sll_pkttype != PACKET_HOST
195 && FROM->sll_pkttype != PACKET_BROADCAST
196 && FROM->sll_pkttype != PACKET_MULTICAST)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200197 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000198
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200199 /* Only these types are recognized */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000200 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200201 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000202
203 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000204 if (ah->ar_hrd != htons(FROM->sll_hatype)
205 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200206 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000207
208 /* Protocol must be IP. */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000209 if (ah->ar_pro != htons(ETH_P_IP)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000210 || (ah->ar_pln != 4)
211 || (ah->ar_hln != me.sll_halen)
212 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200213 return;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000214
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000215 move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
216 move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000217
218 if (dst.s_addr != src_ip.s_addr)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200219 return;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000220 if (!(option_mask32 & DAD)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000221 if ((src.s_addr != dst_ip.s_addr)
Denys Vlasenkofb527692015-08-16 19:56:16 +0200222 || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
223 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000224 } else {
225 /* DAD packet was:
226 src_ip = 0 (or some src)
227 src_hw = ME
228 dst_ip = tested address
229 dst_hw = <unspec>
230
231 We fail, if receive request/reply with:
232 src_ip = tested_address
233 src_hw != ME
234 if src_ip in request was not zero, check
235 also that it matches to dst_ip, otherwise
236 dst_ip/dst_hw do not matter.
237 */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000238 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000239 || (src.s_addr && src.s_addr != dst_ip.s_addr))
Denys Vlasenkofb527692015-08-16 19:56:16 +0200240 return;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000241 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000242 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000243 int s_printed = 0;
244
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200245 printf("%scast re%s from %s [%02x:%02x:%02x:%02x:%02x:%02x]",
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000246 FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
247 ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
Bernhard Reutner-Fischerebd13552006-04-03 12:29:12 +0000248 inet_ntoa(src_ip),
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200249 p[0], p[1], p[2], p[3], p[4], p[5]
250 );
Glenn L McGrath9e598412003-01-09 10:06:01 +0000251 if (dst_ip.s_addr != src.s_addr) {
252 printf("for %s ", inet_ntoa(dst_ip));
253 s_printed = 1;
254 }
255 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200256 unsigned char *pp = p + ah->ar_hln + 4;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000257 if (!s_printed)
258 printf("for ");
Denys Vlasenko852e8dd2016-05-26 21:35:46 +0200259 printf("[%02x:%02x:%02x:%02x:%02x:%02x]",
260 pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]
261 );
Glenn L McGrath9e598412003-01-09 10:06:01 +0000262 }
Rob Landley8ea52052006-03-30 21:50:57 +0000263
Denis Vlasenko459be352007-06-17 19:09:05 +0000264 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000265 unsigned diff = MONOTONIC_US() - last;
266 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000267 } else {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200268 puts(" UNSOLICITED?");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000269 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100270 fflush_all();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000271 }
272 received++;
273 if (FROM->sll_pkttype != PACKET_HOST)
274 brd_recv++;
275 if (ah->ar_op == htons(ARPOP_REQUEST))
276 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000277 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000278 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000279 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000280 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000281 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000282 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000283}
284
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000285int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000286int arping_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000287{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000288 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000289 char *source = NULL;
290 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000291 unsigned char *packet;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000292 char *err_str;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000293
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000294 INIT_G();
295
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000296 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000297
Rob Landleyafb94ec2006-07-16 08:06:34 +0000298 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000299 // Need to remove SUID_NEVER from applets.h for this to work
300 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000301
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000302 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000303 unsigned opt;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000304 char *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000305
306 /* Dad also sets quit_on_reply.
307 * Advert also sets unsolicited.
308 */
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200309 opt_complementary = "=1:Df:AU";
310 opt = getopt32(argv, "DUAqfbc:+w:I:s:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000311 &count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000312 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000313 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000314 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000315 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000316 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000317
Denis Vlasenko459be352007-06-17 19:09:05 +0000318 target = argv[optind];
Alexander Korolkov748fb602015-03-12 13:05:33 +0100319 err_str = xasprintf("interface %s %%s", device);
Denis Vlasenko40920822006-10-03 20:28:06 +0000320 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000321
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000322 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000323 struct ifreq ifr;
324
325 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000326 strncpy_IFNAMSIZ(ifr.ifr_name, device);
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000327 /* We use ifr.ifr_name in error msg so that problem
328 * with truncated name will be visible */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000329 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000330 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000331
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000332 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000333
Glenn L McGrath9e598412003-01-09 10:06:01 +0000334 if (!(ifr.ifr_flags & IFF_UP)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000335 bb_error_msg_and_die(err_str, "is down");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000336 }
337 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000338 bb_error_msg(err_str, "is not ARPable");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000339 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000340 }
341 }
342
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000343 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000344 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000345 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000346 dst = lsa->u.sin.sin_addr;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000347 if (ENABLE_FEATURE_CLEAN_UP)
348 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000349 }
350
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000351 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000352 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000353 }
354
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000355 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000356 src = dst;
357
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000358 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000359 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000360 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000361
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000362 setsockopt_bindtodevice(probe_fd, device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000363 memset(&saddr, 0, sizeof(saddr));
364 saddr.sin_family = AF_INET;
365 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000366 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000367 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000368 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000369 } else { /* !(option_mask32 & DAD) case */
370 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000371 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000372
373 saddr.sin_port = htons(1025);
374 saddr.sin_addr = dst;
375
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +0200376 if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0)
Denys Vlasenko2db782b2015-08-24 19:08:14 +0200377 bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000378 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000379 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
380 //never happens:
381 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
382 // bb_perror_msg_and_die("getsockname");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000383 if (saddr.sin_family != AF_INET)
384 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000385 src = saddr.sin_addr;
386 }
387 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000388 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000389
390 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000391 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000392 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000393 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000394
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000395 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000396 socklen_t alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000397 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
398 //never happens:
399 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
400 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000401 }
402 if (me.sll_halen == 0) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000403 bb_error_msg(err_str, "is not ARPable (no ll address)");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000404 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000405 }
406 he = me;
407 memset(he.sll_addr, -1, he.sll_halen);
408
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000409 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000410 /* inet_ntoa uses static storage, can't use in same printf */
411 printf("ARPING to %s", inet_ntoa(dst));
412 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000413 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000414
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000415 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
416 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000417
418 catcher();
419
Denis Vlasenko459be352007-06-17 19:09:05 +0000420 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000421 while (1) {
422 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000423 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000424 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000425 int cc;
426
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000427 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000428 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000429 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000430 continue;
431 }
432 sigemptyset(&sset);
433 sigaddset(&sset, SIGALRM);
434 sigaddset(&sset, SIGINT);
435 sigprocmask(SIG_BLOCK, &sset, &osset);
436 recv_pack(packet, cc, &from);
437 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000438 }
439}