3
0

arping.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 Duplicated address detection mode"
  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 Time to wait for ARP reply, seconds"
  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 bool 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 false;
  162. /* Only these types are recognized */
  163. if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
  164. return false;
  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 false;
  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 false;
  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 false;
  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 false;
  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 false;
  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. printf(" UNSOLICITED?\n");
  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. return true;
  237. }
  238. int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  239. int arping_main(int argc UNUSED_PARAM, char **argv)
  240. {
  241. const char *device = "eth0";
  242. char *source = NULL;
  243. char *target;
  244. unsigned char *packet;
  245. char *err_str;
  246. INIT_G();
  247. sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
  248. // Drop suid root privileges
  249. // Need to remove SUID_NEVER from applets.h for this to work
  250. //xsetuid(getuid());
  251. err_str = xasprintf("interface %s %%s", device);
  252. {
  253. unsigned opt;
  254. char *str_timeout;
  255. /* Dad also sets quit_on_reply.
  256. * Advert also sets unsolicited.
  257. */
  258. opt_complementary = "=1:Df:AU:c+";
  259. opt = getopt32(argv, "DUAqfbc:w:I:s:",
  260. &count, &str_timeout, &device, &source);
  261. if (opt & 0x80) /* -w: timeout */
  262. timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
  263. //if (opt & 0x200) /* -s: source */
  264. option_mask32 &= 0x3f; /* set respective flags */
  265. }
  266. target = argv[optind];
  267. xfunc_error_retval = 2;
  268. {
  269. struct ifreq ifr;
  270. memset(&ifr, 0, sizeof(ifr));
  271. strncpy_IFNAMSIZ(ifr.ifr_name, device);
  272. /* We use ifr.ifr_name in error msg so that problem
  273. * with truncated name will be visible */
  274. ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
  275. me.sll_ifindex = ifr.ifr_ifindex;
  276. xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
  277. if (!(ifr.ifr_flags & IFF_UP)) {
  278. bb_error_msg_and_die(err_str, "is down");
  279. }
  280. if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
  281. bb_error_msg(err_str, "is not ARPable");
  282. return (option_mask32 & DAD ? 0 : 2);
  283. }
  284. }
  285. /* if (!inet_aton(target, &dst)) - not needed */ {
  286. len_and_sockaddr *lsa;
  287. lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
  288. dst = lsa->u.sin.sin_addr;
  289. if (ENABLE_FEATURE_CLEAN_UP)
  290. free(lsa);
  291. }
  292. if (source && !inet_aton(source, &src)) {
  293. bb_error_msg_and_die("invalid source address %s", source);
  294. }
  295. if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
  296. src = dst;
  297. if (!(option_mask32 & DAD) || src.s_addr) {
  298. struct sockaddr_in saddr;
  299. int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  300. setsockopt_bindtodevice(probe_fd, device);
  301. memset(&saddr, 0, sizeof(saddr));
  302. saddr.sin_family = AF_INET;
  303. if (src.s_addr) {
  304. /* Check that this is indeed our IP */
  305. saddr.sin_addr = src;
  306. xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  307. } else { /* !(option_mask32 & DAD) case */
  308. /* Find IP address on this iface */
  309. socklen_t alen = sizeof(saddr);
  310. saddr.sin_port = htons(1025);
  311. saddr.sin_addr = dst;
  312. if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
  313. bb_perror_msg("setsockopt(SO_DONTROUTE)");
  314. xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  315. getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
  316. //never happens:
  317. //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
  318. // bb_perror_msg_and_die("getsockname");
  319. if (saddr.sin_family != AF_INET)
  320. bb_error_msg_and_die("no IP address configured");
  321. src = saddr.sin_addr;
  322. }
  323. close(probe_fd);
  324. }
  325. me.sll_family = AF_PACKET;
  326. //me.sll_ifindex = ifindex; - done before
  327. me.sll_protocol = htons(ETH_P_ARP);
  328. xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
  329. {
  330. socklen_t alen = sizeof(me);
  331. getsockname(sock_fd, (struct sockaddr *) &me, &alen);
  332. //never happens:
  333. //if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
  334. // bb_perror_msg_and_die("getsockname");
  335. }
  336. if (me.sll_halen == 0) {
  337. bb_error_msg(err_str, "is not ARPable (no ll address)");
  338. return (option_mask32 & DAD ? 0 : 2);
  339. }
  340. he = me;
  341. memset(he.sll_addr, -1, he.sll_halen);
  342. if (!(option_mask32 & QUIET)) {
  343. /* inet_ntoa uses static storage, can't use in same printf */
  344. printf("ARPING to %s", inet_ntoa(dst));
  345. printf(" from %s via %s\n", inet_ntoa(src), device);
  346. }
  347. signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
  348. signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
  349. catcher();
  350. packet = xmalloc(4096);
  351. while (1) {
  352. sigset_t sset, osset;
  353. struct sockaddr_ll from;
  354. socklen_t alen = sizeof(from);
  355. int cc;
  356. cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
  357. if (cc < 0) {
  358. bb_perror_msg("recvfrom");
  359. continue;
  360. }
  361. sigemptyset(&sset);
  362. sigaddset(&sset, SIGALRM);
  363. sigaddset(&sset, SIGINT);
  364. sigprocmask(SIG_BLOCK, &sset, &osset);
  365. recv_pack(packet, cc, &from);
  366. sigprocmask(SIG_SETMASK, &osset, NULL);
  367. }
  368. }