arping.c 11 KB

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