arping.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /* Filter out wild packets */
  136. if (FROM->sll_pkttype != PACKET_HOST
  137. && FROM->sll_pkttype != PACKET_BROADCAST
  138. && FROM->sll_pkttype != PACKET_MULTICAST)
  139. return false;
  140. /* Only these types are recognised */
  141. if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
  142. return false;
  143. /* ARPHRD check and this darned FDDI hack here :-( */
  144. if (ah->ar_hrd != htons(FROM->sll_hatype)
  145. && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
  146. return false;
  147. /* Protocol must be IP. */
  148. if (ah->ar_pro != htons(ETH_P_IP)
  149. || (ah->ar_pln != 4)
  150. || (ah->ar_hln != me.sll_halen)
  151. || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
  152. return false;
  153. memcpy(&src_ip, p + ah->ar_hln, 4);
  154. memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
  155. if (dst.s_addr != src_ip.s_addr)
  156. return false;
  157. if (!(option_mask32 & DAD)) {
  158. if ((src.s_addr != dst_ip.s_addr)
  159. || (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
  160. return false;
  161. } else {
  162. /* DAD packet was:
  163. src_ip = 0 (or some src)
  164. src_hw = ME
  165. dst_ip = tested address
  166. dst_hw = <unspec>
  167. We fail, if receive request/reply with:
  168. src_ip = tested_address
  169. src_hw != ME
  170. if src_ip in request was not zero, check
  171. also that it matches to dst_ip, otherwise
  172. dst_ip/dst_hw do not matter.
  173. */
  174. if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
  175. || (src.s_addr && src.s_addr != dst_ip.s_addr))
  176. return false;
  177. }
  178. if (!(option_mask32 & QUIET)) {
  179. int s_printed = 0;
  180. printf("%scast re%s from %s [%s]",
  181. FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
  182. ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
  183. inet_ntoa(src_ip),
  184. ether_ntoa((struct ether_addr *) p));
  185. if (dst_ip.s_addr != src.s_addr) {
  186. printf("for %s ", inet_ntoa(dst_ip));
  187. s_printed = 1;
  188. }
  189. if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
  190. if (!s_printed)
  191. printf("for ");
  192. printf("[%s]",
  193. ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
  194. }
  195. if (last) {
  196. unsigned diff = MONOTONIC_US() - last;
  197. printf(" %u.%03ums\n", diff / 1000, diff % 1000);
  198. } else {
  199. printf(" UNSOLICITED?\n");
  200. }
  201. fflush(stdout);
  202. }
  203. received++;
  204. if (FROM->sll_pkttype != PACKET_HOST)
  205. brd_recv++;
  206. if (ah->ar_op == htons(ARPOP_REQUEST))
  207. req_recv++;
  208. if (option_mask32 & QUIT_ON_REPLY)
  209. finish();
  210. if (!(option_mask32 & BCAST_ONLY)) {
  211. memcpy(he.sll_addr, p, me.sll_halen);
  212. option_mask32 |= UNICASTING;
  213. }
  214. return true;
  215. }
  216. int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  217. int arping_main(int argc UNUSED_PARAM, char **argv)
  218. {
  219. const char *device = "eth0";
  220. char *source = NULL;
  221. char *target;
  222. unsigned char *packet;
  223. char *err_str;
  224. INIT_G();
  225. sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0);
  226. // Drop suid root privileges
  227. // Need to remove SUID_NEVER from applets.h for this to work
  228. //xsetuid(getuid());
  229. err_str = xasprintf("interface %s %%s", device);
  230. {
  231. unsigned opt;
  232. char *str_timeout;
  233. /* Dad also sets quit_on_reply.
  234. * Advert also sets unsolicited.
  235. */
  236. opt_complementary = "=1:Df:AU:c+";
  237. opt = getopt32(argv, "DUAqfbc:w:I:s:",
  238. &count, &str_timeout, &device, &source);
  239. if (opt & 0x80) /* -w: timeout */
  240. timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;
  241. //if (opt & 0x200) /* -s: source */
  242. option_mask32 &= 0x3f; /* set respective flags */
  243. }
  244. target = argv[optind];
  245. xfunc_error_retval = 2;
  246. {
  247. struct ifreq ifr;
  248. memset(&ifr, 0, sizeof(ifr));
  249. strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
  250. /* We use ifr.ifr_name in error msg so that problem
  251. * with truncated name will be visible */
  252. ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
  253. me.sll_ifindex = ifr.ifr_ifindex;
  254. xioctl(sock_fd, SIOCGIFFLAGS, (char *) &ifr);
  255. if (!(ifr.ifr_flags & IFF_UP)) {
  256. bb_error_msg_and_die(err_str, "is down");
  257. }
  258. if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
  259. bb_error_msg(err_str, "is not ARPable");
  260. return (option_mask32 & DAD ? 0 : 2);
  261. }
  262. }
  263. /* if (!inet_aton(target, &dst)) - not needed */ {
  264. len_and_sockaddr *lsa;
  265. lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
  266. memcpy(&dst, &lsa->u.sin.sin_addr.s_addr, 4);
  267. if (ENABLE_FEATURE_CLEAN_UP)
  268. free(lsa);
  269. }
  270. if (source && !inet_aton(source, &src)) {
  271. bb_error_msg_and_die("invalid source address %s", source);
  272. }
  273. if ((option_mask32 & (DAD|UNSOLICITED)) == UNSOLICITED && src.s_addr == 0)
  274. src = dst;
  275. if (!(option_mask32 & DAD) || src.s_addr) {
  276. struct sockaddr_in saddr;
  277. int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  278. if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
  279. bb_perror_msg("cannot bind to device %s", device);
  280. memset(&saddr, 0, sizeof(saddr));
  281. saddr.sin_family = AF_INET;
  282. if (src.s_addr) {
  283. /* Check that this is indeed our IP */
  284. saddr.sin_addr = src;
  285. xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  286. } else { /* !(option_mask32 & DAD) case */
  287. /* Find IP address on this iface */
  288. socklen_t alen = sizeof(saddr);
  289. saddr.sin_port = htons(1025);
  290. saddr.sin_addr = dst;
  291. if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
  292. bb_perror_msg("setsockopt(SO_DONTROUTE)");
  293. xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  294. if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
  295. bb_perror_msg_and_die("getsockname");
  296. }
  297. if (saddr.sin_family != AF_INET)
  298. bb_error_msg_and_die("no IP address configured");
  299. src = saddr.sin_addr;
  300. }
  301. close(probe_fd);
  302. }
  303. me.sll_family = AF_PACKET;
  304. //me.sll_ifindex = ifindex; - done before
  305. me.sll_protocol = htons(ETH_P_ARP);
  306. xbind(sock_fd, (struct sockaddr *) &me, sizeof(me));
  307. {
  308. socklen_t alen = sizeof(me);
  309. if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) {
  310. bb_perror_msg_and_die("getsockname");
  311. }
  312. }
  313. if (me.sll_halen == 0) {
  314. bb_error_msg(err_str, "is not ARPable (no ll address)");
  315. return (option_mask32 & DAD ? 0 : 2);
  316. }
  317. he = me;
  318. memset(he.sll_addr, -1, he.sll_halen);
  319. if (!(option_mask32 & QUIET)) {
  320. /* inet_ntoa uses static storage, can't use in same printf */
  321. printf("ARPING to %s", inet_ntoa(dst));
  322. printf(" from %s via %s\n", inet_ntoa(src), device);
  323. }
  324. signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
  325. signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
  326. catcher();
  327. packet = xmalloc(4096);
  328. while (1) {
  329. sigset_t sset, osset;
  330. struct sockaddr_ll from;
  331. socklen_t alen = sizeof(from);
  332. int cc;
  333. cc = recvfrom(sock_fd, packet, 4096, 0, (struct sockaddr *) &from, &alen);
  334. if (cc < 0) {
  335. bb_perror_msg("recvfrom");
  336. continue;
  337. }
  338. sigemptyset(&sset);
  339. sigaddset(&sset, SIGALRM);
  340. sigaddset(&sset, SIGINT);
  341. sigprocmask(SIG_BLOCK, &sset, &osset);
  342. recv_pack(packet, cc, &from);
  343. sigprocmask(SIG_SETMASK, &osset, NULL);
  344. }
  345. }