arping.c 11 KB

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