arping.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. *
  5. * Author: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
  7. */
  8. //config:config ARPING
  9. //config: bool "arping (9.3 kb)"
  10. //config: default y
  11. //config: select PLATFORM_LINUX
  12. //config: help
  13. //config: Ping hosts by ARP packets.
  14. //applet:IF_ARPING(APPLET(arping, BB_DIR_USR_SBIN, BB_SUID_DROP))
  15. //kbuild:lib-$(CONFIG_ARPING) += arping.o
  16. //usage:#define arping_trivial_usage
  17. //usage: "[-fqbDUA] [-c CNT] [-w TIMEOUT] [-I IFACE] [-s SRC_IP] DST_IP"
  18. //usage:#define arping_full_usage "\n\n"
  19. //usage: "Send ARP requests/replies\n"
  20. //usage: "\n -f Quit on first ARP reply"
  21. //usage: "\n -q Quiet"
  22. //usage: "\n -b Keep broadcasting, don't go unicast"
  23. //usage: "\n -D Exit with 1 if DST_IP replies"
  24. //usage: "\n -U Unsolicited ARP mode, update your neighbors"
  25. //usage: "\n -A ARP answer mode, update your neighbors"
  26. //usage: "\n -c N Stop after sending N ARP requests"
  27. //usage: "\n -w TIMEOUT Seconds to wait for ARP reply"
  28. //NB: in iputils-s20160308, iface is mandatory, no default
  29. //usage: "\n -I IFACE Interface to use (default eth0)"
  30. //usage: "\n -s SRC_IP Sender IP address"
  31. //usage: "\n DST_IP Target IP address"
  32. #include <arpa/inet.h>
  33. #include <net/if.h>
  34. #include <netinet/ether.h>
  35. #include <netpacket/packet.h>
  36. #include "libbb.h"
  37. #include "common_bufsiz.h"
  38. /* We don't expect to see 1000+ seconds delay, unsigned is enough */
  39. #define MONOTONIC_US() ((unsigned)monotonic_us())
  40. enum {
  41. UNSOLICITED = 1 << 0,
  42. DAD = 1 << 1,
  43. ADVERT = 1 << 2,
  44. QUIET = 1 << 3,
  45. QUIT_ON_REPLY = 1 << 4,
  46. BCAST_ONLY = 1 << 5,
  47. UNICASTING = 1 << 6,
  48. TIMEOUT = 1 << 7,
  49. };
  50. #define GETOPT32(str_timeout, device, source) \
  51. getopt32(argv, "^" \
  52. "UDAqfbc:+w:I:s:" \
  53. /* DAD also sets quit_on_reply, */ \
  54. /* advert also sets unsolicited: */ \
  55. "\0" "=1:Df:AU", \
  56. &count, &str_timeout, &device, &source \
  57. );
  58. struct globals {
  59. struct in_addr src;
  60. struct in_addr dst;
  61. struct sockaddr_ll me;
  62. struct sockaddr_ll he;
  63. int count; // = -1;
  64. unsigned last;
  65. unsigned timeout_us;
  66. unsigned start;
  67. unsigned sent;
  68. unsigned brd_sent;
  69. unsigned received;
  70. unsigned brd_recv;
  71. unsigned req_recv;
  72. /* should be in main(), but are here to reduce stack use: */
  73. struct ifreq ifr;
  74. struct sockaddr_in probe_saddr;
  75. sigset_t sset;
  76. unsigned char packet[4096];
  77. } FIX_ALIASING;
  78. #define src (G.src )
  79. #define dst (G.dst )
  80. #define me (G.me )
  81. #define he (G.he )
  82. #define count (G.count )
  83. #define last (G.last )
  84. #define timeout_us (G.timeout_us)
  85. #define start (G.start )
  86. #define sent (G.sent )
  87. #define brd_sent (G.brd_sent )
  88. #define received (G.received )
  89. #define brd_recv (G.brd_recv )
  90. #define req_recv (G.req_recv )
  91. //#define G (*(struct globals*)bb_common_bufsiz1)
  92. #define G (*ptr_to_globals)
  93. #define INIT_G() do { \
  94. /*setup_common_bufsiz();*/ \
  95. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  96. count = -1; \
  97. } while (0)
  98. #define sock_fd 3
  99. static int send_pack(struct in_addr *src_addr,
  100. struct in_addr *dst_addr,
  101. struct sockaddr_ll *ME,
  102. struct sockaddr_ll *HE)
  103. {
  104. int err;
  105. unsigned char buf[256];
  106. struct arphdr *ah = (struct arphdr *) buf;
  107. unsigned char *p;
  108. ah->ar_hrd = htons(ARPHRD_ETHER);
  109. ah->ar_pro = htons(ETH_P_IP);
  110. ah->ar_hln = ME->sll_halen;
  111. ah->ar_pln = 4;
  112. ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
  113. p = (unsigned char *) (ah + 1);
  114. p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
  115. p = mempcpy(p, src_addr, 4);
  116. if (option_mask32 & ADVERT)
  117. p = mempcpy(p, &ME->sll_addr, ah->ar_hln);
  118. else
  119. p = mempcpy(p, &HE->sll_addr, ah->ar_hln);
  120. p = mempcpy(p, dst_addr, 4);
  121. err = sendto(sock_fd, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
  122. if (err == p - buf) {
  123. last = MONOTONIC_US();
  124. sent++;
  125. if (!(option_mask32 & UNICASTING))
  126. brd_sent++;
  127. }
  128. return err;
  129. }
  130. static void finish(void) NORETURN;
  131. static void finish(void)
  132. {
  133. if (!(option_mask32 & QUIET)) {
  134. printf("Sent %u probe(s) (%u broadcast(s))\n"
  135. "Received %u response(s)"
  136. " (%u request(s), %u broadcast(s))\n",
  137. sent, brd_sent,
  138. received,
  139. req_recv, brd_recv);
  140. }
  141. if (option_mask32 & DAD)
  142. exit(!!received);
  143. if (option_mask32 & UNSOLICITED)
  144. exit(EXIT_SUCCESS);
  145. exit(!received);
  146. }
  147. static void catcher(void)
  148. {
  149. unsigned now;
  150. now = MONOTONIC_US();
  151. if (start == 0)
  152. start = now;
  153. if (count == 0 || (timeout_us && (now - start) > timeout_us))
  154. finish();
  155. /* count < 0 means "infinite count" */
  156. if (count > 0)
  157. count--;
  158. if (last == 0 || (now - last) > 500000) {
  159. send_pack(&src, &dst, &me, &he);
  160. if (count == 0 && (option_mask32 & UNSOLICITED))
  161. finish();
  162. }
  163. alarm(1);
  164. }
  165. static void recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
  166. {
  167. struct arphdr *ah = (struct arphdr *) buf;
  168. unsigned char *p = (unsigned char *) (ah + 1);
  169. struct in_addr src_ip, dst_ip;
  170. /* moves below assume in_addr is 4 bytes big, ensure that */
  171. BUILD_BUG_ON(sizeof(struct in_addr) != 4);
  172. BUILD_BUG_ON(sizeof(src_ip.s_addr) != 4);
  173. /* Filter out wild packets */
  174. if (FROM->sll_pkttype != PACKET_HOST
  175. && FROM->sll_pkttype != PACKET_BROADCAST
  176. && FROM->sll_pkttype != PACKET_MULTICAST)
  177. return;
  178. /* Only these types are recognized */
  179. if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
  180. return;
  181. /* ARPHRD check and this darned FDDI hack here :-( */
  182. if (ah->ar_hrd != htons(FROM->sll_hatype)
  183. && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
  184. return;
  185. /* Protocol must be IP. */
  186. if (ah->ar_pro != htons(ETH_P_IP)
  187. || (ah->ar_pln != 4)
  188. || (ah->ar_hln != me.sll_halen)
  189. || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln)))
  190. ) {
  191. return;
  192. }
  193. move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
  194. move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);
  195. if (dst.s_addr != src_ip.s_addr)
  196. return;
  197. if (!(option_mask32 & DAD)) {
  198. if ((src.s_addr != dst_ip.s_addr)
  199. || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
  200. return;
  201. } else {
  202. /* DAD packet was:
  203. src_ip = 0 (or some src)
  204. src_hw = ME
  205. dst_ip = tested address
  206. dst_hw = <unspec>
  207. We fail, if receive request/reply with:
  208. src_ip = tested_address
  209. src_hw != ME
  210. if src_ip in request was not zero, check
  211. also that it matches to dst_ip, otherwise
  212. dst_ip/dst_hw do not matter.
  213. */
  214. if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
  215. || (src.s_addr && src.s_addr != dst_ip.s_addr))
  216. return;
  217. }
  218. if (!(option_mask32 & QUIET)) {
  219. int s_printed = 0;
  220. //TODO: arping from iputils-s20160308 print upprcase hex in MAC, follow them?
  221. printf("%scast re%s from %s [%02x:%02x:%02x:%02x:%02x:%02x]",
  222. FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
  223. ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
  224. inet_ntoa(src_ip),
  225. p[0], p[1], p[2], p[3], p[4], p[5]
  226. );
  227. if (dst_ip.s_addr != src.s_addr) {
  228. printf("for %s", inet_ntoa(dst_ip));
  229. s_printed = 1;
  230. }
  231. if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
  232. unsigned char *pp = p + ah->ar_hln + 4;
  233. if (!s_printed)
  234. printf(" for");
  235. printf(" [%02x:%02x:%02x:%02x:%02x:%02x]",
  236. pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]
  237. );
  238. }
  239. if (last) {
  240. unsigned diff = MONOTONIC_US() - last;
  241. printf(" %u.%03ums\n", diff / 1000, diff % 1000);
  242. } else {
  243. puts(" UNSOLICITED?");
  244. }
  245. fflush_all();
  246. }
  247. received++;
  248. if (FROM->sll_pkttype != PACKET_HOST)
  249. brd_recv++;
  250. if (ah->ar_op == htons(ARPOP_REQUEST))
  251. req_recv++;
  252. if (option_mask32 & QUIT_ON_REPLY)
  253. finish();
  254. if (!(option_mask32 & BCAST_ONLY)) {
  255. memcpy(he.sll_addr, p, me.sll_halen);
  256. option_mask32 |= UNICASTING;
  257. }
  258. }
  259. int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  260. int arping_main(int argc UNUSED_PARAM, char **argv)
  261. {
  262. const char *device = "eth0";
  263. char *source = NULL;
  264. char *target;
  265. char *err_str;
  266. INIT_G();
  267. xmove_fd(xsocket(AF_PACKET, SOCK_DGRAM, 0), sock_fd);
  268. // If you ever change BB_SUID_DROP to BB_SUID_REQUIRE,
  269. // drop suid root privileges here:
  270. //xsetuid(getuid());
  271. {
  272. unsigned opt;
  273. char *str_timeout;
  274. opt = GETOPT32(str_timeout, device, source);
  275. if (opt & TIMEOUT)
  276. timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
  277. }
  278. target = argv[optind];
  279. err_str = xasprintf("interface %s %%s", device);
  280. xfunc_error_retval = 2;
  281. /*memset(&G.ifr, 0, sizeof(G.ifr)); - zeroed by INIT_G */
  282. strncpy_IFNAMSIZ(G.ifr.ifr_name, device);
  283. ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &G.ifr, err_str, "not found");
  284. me.sll_ifindex = G.ifr.ifr_ifindex;
  285. xioctl(sock_fd, SIOCGIFFLAGS, (char *) &G.ifr);
  286. if (!(G.ifr.ifr_flags & IFF_UP)) {
  287. bb_error_msg_and_die(err_str, "is down");
  288. }
  289. if (G.ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
  290. bb_error_msg(err_str, "is not ARPable");
  291. BUILD_BUG_ON(DAD != 2);
  292. /* exit 0 if DAD, else exit 2 */
  293. return (~option_mask32 & DAD);
  294. }
  295. /* if (!inet_aton(target, &dst)) - not needed */ {
  296. len_and_sockaddr *lsa;
  297. lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
  298. dst = lsa->u.sin.sin_addr;
  299. if (ENABLE_FEATURE_CLEAN_UP)
  300. free(lsa);
  301. }
  302. if (source && !inet_aton(source, &src)) {
  303. bb_error_msg_and_die("invalid source address %s", source);
  304. }
  305. if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
  306. src = dst;
  307. if (!(option_mask32 & DAD) || src.s_addr) {
  308. /*struct sockaddr_in probe_saddr;*/
  309. int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  310. setsockopt_bindtodevice(probe_fd, device);
  311. /*memset(&G.probe_saddr, 0, sizeof(G.probe_saddr)); - zeroed by INIT_G */
  312. G.probe_saddr.sin_family = AF_INET;
  313. if (src.s_addr) {
  314. /* Check that this is indeed our IP */
  315. G.probe_saddr.sin_addr = src;
  316. xbind(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr));
  317. } else { /* !(option_mask32 & DAD) case */
  318. /* Find IP address on this iface */
  319. G.probe_saddr.sin_port = htons(1025);
  320. G.probe_saddr.sin_addr = dst;
  321. if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0)
  322. bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE");
  323. xconnect(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr));
  324. bb_getsockname(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr));
  325. if (G.probe_saddr.sin_family != AF_INET)
  326. bb_error_msg_and_die("no IP address configured");
  327. src = G.probe_saddr.sin_addr;
  328. }
  329. close(probe_fd);
  330. }
  331. me.sll_family = AF_PACKET;
  332. //me.sll_ifindex = ifindex; - done before
  333. me.sll_protocol = htons(ETH_P_ARP);
  334. xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
  335. bb_getsockname(sock_fd, (struct sockaddr *) &me, sizeof(me));
  336. //never happens:
  337. //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
  338. // bb_perror_msg_and_die("getsockname");
  339. if (me.sll_halen == 0) {
  340. bb_error_msg(err_str, "is not ARPable (no ll address)");
  341. BUILD_BUG_ON(DAD != 2);
  342. /* exit 0 if DAD, else exit 2 */
  343. return (~option_mask32 & DAD);
  344. }
  345. he = me;
  346. memset(he.sll_addr, -1, he.sll_halen);
  347. if (!(option_mask32 & QUIET)) {
  348. /* inet_ntoa uses static storage, can't use in same printf */
  349. printf("ARPING %s", inet_ntoa(dst));
  350. printf(" from %s %s\n", inet_ntoa(src), device);
  351. }
  352. /*sigemptyset(&G.sset); - zeroed by INIT_G */
  353. sigaddset(&G.sset, SIGALRM);
  354. sigaddset(&G.sset, SIGINT);
  355. signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
  356. signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
  357. /* Send the first packet, arm ALRM */
  358. catcher();
  359. while (1) {
  360. struct sockaddr_ll from;
  361. socklen_t alen = sizeof(from);
  362. int cc;
  363. /* Unblock SIGALRM so that the previously called alarm()
  364. * can prevent recvfrom from blocking forever in case the
  365. * inherited procmask is blocking SIGALRM.
  366. */
  367. sigprocmask(SIG_UNBLOCK, &G.sset, NULL);
  368. cc = recvfrom(sock_fd, G.packet, sizeof(G.packet), 0, (struct sockaddr *) &from, &alen);
  369. /* Don't allow SIGALRMs while we process the reply */
  370. sigprocmask(SIG_BLOCK, &G.sset, NULL);
  371. if (cc < 0) {
  372. bb_perror_msg("recvfrom");
  373. continue;
  374. }
  375. recv_pack(G.packet, cc, &from);
  376. }
  377. }