blob: dbfd75ef52da712393cca1c81c13d8cb31a84560 [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"
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 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"
Glenn L McGrath9e598412003-01-09 10:06:01 +000031
Denis Vlasenko459be352007-06-17 19:09:05 +000032/* We don't expect to see 1000+ seconds delay, unsigned is enough */
33#define MONOTONIC_US() ((unsigned)monotonic_us())
34
Denis Vlasenko459be352007-06-17 19:09:05 +000035enum {
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 Landley8ea52052006-03-30 21:50:57 +000043};
Rob Landley8ea52052006-03-30 21:50:57 +000044
Denis Vlasenkofff9b692007-11-23 09:15:26 +000045struct globals {
46 struct in_addr src;
47 struct in_addr dst;
48 struct sockaddr_ll me;
49 struct sockaddr_ll he;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000050 int sock_fd;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000051
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 Vlasenko98a4c7c2010-02-04 15:00:15 +010062} FIX_ALIASING;
Denis Vlasenkofff9b692007-11-23 09:15:26 +000063#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 Vlasenkoa09300a2007-11-25 12:40:56 +000068#define sock_fd (G.sock_fd )
Denis Vlasenkofff9b692007-11-23 09:15:26 +000069#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 Vlasenko7049ff82008-06-25 09:53:17 +000078#define INIT_G() do { \
79 count = -1; \
80} while (0)
Glenn L McGrath9e598412003-01-09 10:06:01 +000081
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +000082// 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 Vlasenkobd7bb292007-06-17 23:40:26 +000089static int send_pack(struct in_addr *src_addr,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +000090 struct in_addr *dst_addr, struct sockaddr_ll *ME,
91 struct sockaddr_ll *HE)
Glenn L McGrath9e598412003-01-09 10:06:01 +000092{
93 int err;
Denis Vlasenko459be352007-06-17 19:09:05 +000094 unsigned char buf[256];
Glenn L McGrath9e598412003-01-09 10:06:01 +000095 struct arphdr *ah = (struct arphdr *) buf;
96 unsigned char *p = (unsigned char *) (ah + 1);
97
Glenn L McGrath9e598412003-01-09 10:06:01 +000098 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 Vlasenkobd7bb292007-06-17 23:40:26 +0000102 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000103
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000104 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
105 p = mempcpy(p, src_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000106
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000107 if (option_mask32 & ADVERT)
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000108 p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000109 else
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000110 p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000111
Denis Vlasenkoa6b3a1f2008-04-25 08:13:36 +0000112 p = mempcpy(p, dst_addr, 4);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000113
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000114 err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000115 if (err == p - buf) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000116 last = MONOTONIC_US();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000117 sent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000118 if (!(option_mask32 & UNICASTING))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000119 brd_sent++;
120 }
121 return err;
122}
123
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000124static void finish(void) NORETURN;
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000125static void finish(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000126{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000127 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-Fischerebd13552006-04-03 12:29:12 +0000131 sent, brd_sent,
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000132 received, (received == 1) ? "ies" : "y",
133 req_recv, brd_recv);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000134 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000135 if (option_mask32 & DAD)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000136 exit(!!received);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000137 if (option_mask32 & UNSOLICITED)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000138 exit(EXIT_SUCCESS);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000139 exit(!received);
140}
141
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000142static void catcher(void)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000143{
Denis Vlasenko459be352007-06-17 19:09:05 +0000144 unsigned now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000145
Denis Vlasenko459be352007-06-17 19:09:05 +0000146 now = MONOTONIC_US();
147 if (start == 0)
148 start = now;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000149
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000150 if (count == 0 || (timeout_us && (now - start) > timeout_us))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000151 finish();
152
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000153 /* count < 0 means "infinite count" */
154 if (count > 0)
155 count--;
Denis Vlasenko459be352007-06-17 19:09:05 +0000156
157 if (last == 0 || (now - last) > 500000) {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000158 send_pack(&src, &dst, &me, &he);
159 if (count == 0 && (option_mask32 & UNSOLICITED))
Glenn L McGrath9e598412003-01-09 10:06:01 +0000160 finish();
161 }
162 alarm(1);
163}
164
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000165static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000166{
Glenn L McGrath9e598412003-01-09 10:06:01 +0000167 struct arphdr *ah = (struct arphdr *) buf;
168 unsigned char *p = (unsigned char *) (ah + 1);
169 struct in_addr src_ip, dst_ip;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000170 /* 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 McGrath9e598412003-01-09 10:06:01 +0000179
Glenn L McGrath9e598412003-01-09 10:06:01 +0000180 /* Filter out wild packets */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000181 if (FROM->sll_pkttype != PACKET_HOST
182 && FROM->sll_pkttype != PACKET_BROADCAST
183 && FROM->sll_pkttype != PACKET_MULTICAST)
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000184 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000185
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200186 /* Only these types are recognized */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000187 if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000188 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000189
190 /* ARPHRD check and this darned FDDI hack here :-( */
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000191 if (ah->ar_hrd != htons(FROM->sll_hatype)
192 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000193 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000194
195 /* Protocol must be IP. */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000196 if (ah->ar_pro != htons(ETH_P_IP)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000197 || (ah->ar_pln != 4)
198 || (ah->ar_hln != me.sll_halen)
199 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000200 return false;
201
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000202 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-Fischerf536b992008-02-11 13:26:54 +0000204
205 if (dst.s_addr != src_ip.s_addr)
206 return false;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000207 if (!(option_mask32 & DAD)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000208 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 McGrath9e598412003-01-09 10:06:01 +0000211 } 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-Fischerf536b992008-02-11 13:26:54 +0000225 if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000226 || (src.s_addr && src.s_addr != dst_ip.s_addr))
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000227 return false;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000228 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000229 if (!(option_mask32 & QUIET)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000230 int s_printed = 0;
231
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000232 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-Fischerebd13552006-04-03 12:29:12 +0000235 inet_ntoa(src_ip),
236 ether_ntoa((struct ether_addr *) p));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000237 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 Vlasenko1dc1b372006-12-23 02:48:44 +0000245 ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000246 }
Rob Landley8ea52052006-03-30 21:50:57 +0000247
Denis Vlasenko459be352007-06-17 19:09:05 +0000248 if (last) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000249 unsigned diff = MONOTONIC_US() - last;
250 printf(" %u.%03ums\n", diff / 1000, diff % 1000);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000251 } else {
252 printf(" UNSOLICITED?\n");
253 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100254 fflush_all();
Glenn L McGrath9e598412003-01-09 10:06:01 +0000255 }
256 received++;
257 if (FROM->sll_pkttype != PACKET_HOST)
258 brd_recv++;
259 if (ah->ar_op == htons(ARPOP_REQUEST))
260 req_recv++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000261 if (option_mask32 & QUIT_ON_REPLY)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000262 finish();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000263 if (!(option_mask32 & BCAST_ONLY)) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000264 memcpy(he.sll_addr, p, me.sll_halen);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000265 option_mask32 |= UNICASTING;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000266 }
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000267 return true;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000268}
269
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000270int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000271int arping_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000272{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000273 const char *device = "eth0";
Glenn L McGrath9e598412003-01-09 10:06:01 +0000274 char *source = NULL;
275 char *target;
Denis Vlasenko459be352007-06-17 19:09:05 +0000276 unsigned char *packet;
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000277 char *err_str;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000278
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000279 INIT_G();
280
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000281 sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000282
Rob Landleyafb94ec2006-07-16 08:06:34 +0000283 // Drop suid root privileges
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000284 // Need to remove SUID_NEVER from applets.h for this to work
285 //xsetuid(getuid());
Glenn L McGrath9e598412003-01-09 10:06:01 +0000286
Bernhard Reutner-Fischer13766942006-03-31 18:02:46 +0000287 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000288 unsigned opt;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000289 char *str_timeout;
Bernhard Reutner-Fischera0f75e22006-04-03 11:52:01 +0000290
291 /* Dad also sets quit_on_reply.
292 * Advert also sets unsolicited.
293 */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000294 opt_complementary = "=1:Df:AU:c+";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000295 opt = getopt32(argv, "DUAqfbc:w:I:s:",
Denis Vlasenko1d426652008-03-17 09:09:09 +0000296 &count, &str_timeout, &device, &source);
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000297 if (opt & 0x80) /* -w: timeout */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000298 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000299 //if (opt & 0x200) /* -s: source */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000300 option_mask32 &= 0x3f; /* set respective flags */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000301 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000302
Denis Vlasenko459be352007-06-17 19:09:05 +0000303 target = argv[optind];
Alexander Korolkov748fb602015-03-12 13:05:33 +0100304 err_str = xasprintf("interface %s %%s", device);
Denis Vlasenko40920822006-10-03 20:28:06 +0000305 xfunc_error_retval = 2;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000306
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000307 {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000308 struct ifreq ifr;
309
310 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000311 strncpy_IFNAMSIZ(ifr.ifr_name, device);
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000312 /* We use ifr.ifr_name in error msg so that problem
313 * with truncated name will be visible */
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000314 ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000315 me.sll_ifindex = ifr.ifr_ifindex;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000316
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000317 xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000318
Glenn L McGrath9e598412003-01-09 10:06:01 +0000319 if (!(ifr.ifr_flags & IFF_UP)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000320 bb_error_msg_and_die(err_str, "is down");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000321 }
322 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000323 bb_error_msg(err_str, "is not ARPable");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000324 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000325 }
326 }
327
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000328 /* if (!inet_aton(target, &dst)) - not needed */ {
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000329 len_and_sockaddr *lsa;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000330 lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000331 dst = lsa->u.sin.sin_addr;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +0000332 if (ENABLE_FEATURE_CLEAN_UP)
333 free(lsa);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000334 }
335
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000336 if (source && !inet_aton(source, &src)) {
Rob Landley8ea52052006-03-30 21:50:57 +0000337 bb_error_msg_and_die("invalid source address %s", source);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000338 }
339
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000340 if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
Glenn L McGrath9e598412003-01-09 10:06:01 +0000341 src = dst;
342
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000343 if (!(option_mask32 & DAD) || src.s_addr) {
Glenn L McGrath9e598412003-01-09 10:06:01 +0000344 struct sockaddr_in saddr;
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000345 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000346
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000347 setsockopt_bindtodevice(probe_fd, device);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000348 memset(&saddr, 0, sizeof(saddr));
349 saddr.sin_family = AF_INET;
350 if (src.s_addr) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000351 /* Check that this is indeed our IP */
Glenn L McGrath9e598412003-01-09 10:06:01 +0000352 saddr.sin_addr = src;
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000353 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000354 } else { /* !(option_mask32 & DAD) case */
355 /* Find IP address on this iface */
Eric Andersen0cb6f352006-01-30 22:30:41 +0000356 socklen_t alen = sizeof(saddr);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000357
358 saddr.sin_port = htons(1025);
359 saddr.sin_addr = dst;
360
Denis Vlasenko703e2022007-01-22 14:12:08 +0000361 if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
Denis Vlasenkofff9b692007-11-23 09:15:26 +0000362 bb_perror_msg("setsockopt(SO_DONTROUTE)");
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000363 xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000364 getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
365 //never happens:
366 //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
367 // bb_perror_msg_and_die("getsockname");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000368 if (saddr.sin_family != AF_INET)
369 bb_error_msg_and_die("no IP address configured");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000370 src = saddr.sin_addr;
371 }
372 close(probe_fd);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000373 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000374
375 me.sll_family = AF_PACKET;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000376 //me.sll_ifindex = ifindex; - done before
Glenn L McGrath9e598412003-01-09 10:06:01 +0000377 me.sll_protocol = htons(ETH_P_ARP);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000378 xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
Glenn L McGrath9e598412003-01-09 10:06:01 +0000379
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000380 {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000381 socklen_t alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000382 getsockname(sock_fd, (struct sockaddr *) &me, &alen);
383 //never happens:
384 //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
385 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000386 }
387 if (me.sll_halen == 0) {
Bernhard Reutner-Fischerf536b992008-02-11 13:26:54 +0000388 bb_error_msg(err_str, "is not ARPable (no ll address)");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000389 return (option_mask32 & DAD ? 0 : 2);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000390 }
391 he = me;
392 memset(he.sll_addr, -1, he.sll_halen);
393
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000394 if (!(option_mask32 & QUIET)) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000395 /* inet_ntoa uses static storage, can't use in same printf */
396 printf("ARPING to %s", inet_ntoa(dst));
397 printf(" from %s via %s\n", inet_ntoa(src), device);
Glenn L McGratha837e2d2003-02-09 07:01:33 +0000398 }
Glenn L McGrath9e598412003-01-09 10:06:01 +0000399
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000400 signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
401 signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000402
403 catcher();
404
Denis Vlasenko459be352007-06-17 19:09:05 +0000405 packet = xmalloc(4096);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000406 while (1) {
407 sigset_t sset, osset;
Glenn L McGrath9e598412003-01-09 10:06:01 +0000408 struct sockaddr_ll from;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000409 socklen_t alen = sizeof(from);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000410 int cc;
411
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000412 cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000413 if (cc < 0) {
Denis Vlasenkoa65a1772006-09-23 12:46:30 +0000414 bb_perror_msg("recvfrom");
Glenn L McGrath9e598412003-01-09 10:06:01 +0000415 continue;
416 }
417 sigemptyset(&sset);
418 sigaddset(&sset, SIGALRM);
419 sigaddset(&sset, SIGINT);
420 sigprocmask(SIG_BLOCK, &sset, &osset);
421 recv_pack(packet, cc, &from);
422 sigprocmask(SIG_SETMASK, &osset, NULL);
Glenn L McGrath9e598412003-01-09 10:06:01 +0000423 }
424}