arping.c 12 KB

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