arping.c 12 KB

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