3
0

arping.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. static struct in_addr src;
  18. static struct in_addr dst;
  19. static struct sockaddr_ll me;
  20. static struct sockaddr_ll he;
  21. static unsigned last;
  22. enum {
  23. DAD = 1,
  24. UNSOLICITED = 2,
  25. ADVERT = 4,
  26. QUIET = 8,
  27. QUIT_ON_REPLY = 16,
  28. BCAST_ONLY = 32,
  29. UNICASTING = 64
  30. };
  31. static int sock;
  32. static unsigned count = UINT_MAX;
  33. static unsigned timeout_us;
  34. static unsigned sent;
  35. static unsigned brd_sent;
  36. static unsigned received;
  37. static unsigned brd_recv;
  38. static unsigned req_recv;
  39. static int send_pack(struct in_addr *src_addr,
  40. struct in_addr *dst_addr, struct sockaddr_ll *ME,
  41. struct sockaddr_ll *HE)
  42. {
  43. int err;
  44. unsigned now;
  45. unsigned char buf[256];
  46. struct arphdr *ah = (struct arphdr *) buf;
  47. unsigned char *p = (unsigned char *) (ah + 1);
  48. ah->ar_hrd = htons(ME->sll_hatype);
  49. ah->ar_hrd = htons(ARPHRD_ETHER);
  50. ah->ar_pro = htons(ETH_P_IP);
  51. ah->ar_hln = ME->sll_halen;
  52. ah->ar_pln = 4;
  53. ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
  54. memcpy(p, &ME->sll_addr, ah->ar_hln);
  55. p += ME->sll_halen;
  56. memcpy(p, src_addr, 4);
  57. p += 4;
  58. if (option_mask32 & ADVERT)
  59. memcpy(p, &ME->sll_addr, ah->ar_hln);
  60. else
  61. memcpy(p, &HE->sll_addr, ah->ar_hln);
  62. p += ah->ar_hln;
  63. memcpy(p, dst_addr, 4);
  64. p += 4;
  65. now = MONOTONIC_US();
  66. err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
  67. if (err == p - buf) {
  68. last = now;
  69. sent++;
  70. if (!(option_mask32 & UNICASTING))
  71. brd_sent++;
  72. }
  73. return err;
  74. }
  75. static void finish(void) ATTRIBUTE_NORETURN;
  76. static void finish(void)
  77. {
  78. if (!(option_mask32 & QUIET)) {
  79. printf("Sent %u probe(s) (%u broadcast(s))\n"
  80. "Received %u repl%s"
  81. " (%u request(s), %u broadcast(s))\n",
  82. sent, brd_sent,
  83. received, (received == 1) ? "ies" : "y",
  84. req_recv, brd_recv);
  85. }
  86. if (option_mask32 & DAD)
  87. exit(!!received);
  88. if (option_mask32 & UNSOLICITED)
  89. exit(0);
  90. exit(!received);
  91. }
  92. static void catcher(void)
  93. {
  94. static unsigned start;
  95. unsigned now;
  96. now = MONOTONIC_US();
  97. if (start == 0)
  98. start = now;
  99. if (count == 0 || (timeout_us && (now - start) > (timeout_us + 500000)))
  100. finish();
  101. count--;
  102. if (last == 0 || (now - last) > 500000) {
  103. send_pack(&src, &dst, &me, &he);
  104. if (count == 0 && (option_mask32 & UNSOLICITED))
  105. finish();
  106. }
  107. alarm(1);
  108. }
  109. static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
  110. {
  111. struct arphdr *ah = (struct arphdr *) buf;
  112. unsigned char *p = (unsigned char *) (ah + 1);
  113. struct in_addr src_ip, dst_ip;
  114. /* Filter out wild packets */
  115. if (FROM->sll_pkttype != PACKET_HOST
  116. && FROM->sll_pkttype != PACKET_BROADCAST
  117. && FROM->sll_pkttype != PACKET_MULTICAST)
  118. return 0;
  119. /* Only these types are recognised */
  120. if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
  121. return 0;
  122. /* ARPHRD check and this darned FDDI hack here :-( */
  123. if (ah->ar_hrd != htons(FROM->sll_hatype)
  124. && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
  125. return 0;
  126. /* Protocol must be IP. */
  127. if (ah->ar_pro != htons(ETH_P_IP))
  128. return 0;
  129. if (ah->ar_pln != 4)
  130. return 0;
  131. if (ah->ar_hln != me.sll_halen)
  132. return 0;
  133. if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
  134. return 0;
  135. memcpy(&src_ip, p + ah->ar_hln, 4);
  136. memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
  137. if (!(option_mask32 & DAD)) {
  138. if (src_ip.s_addr != dst.s_addr)
  139. return 0;
  140. if (src.s_addr != dst_ip.s_addr)
  141. return 0;
  142. if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
  143. return 0;
  144. } else {
  145. /* DAD packet was:
  146. src_ip = 0 (or some src)
  147. src_hw = ME
  148. dst_ip = tested address
  149. dst_hw = <unspec>
  150. We fail, if receive request/reply with:
  151. src_ip = tested_address
  152. src_hw != ME
  153. if src_ip in request was not zero, check
  154. also that it matches to dst_ip, otherwise
  155. dst_ip/dst_hw do not matter.
  156. */
  157. if (src_ip.s_addr != dst.s_addr)
  158. return 0;
  159. if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
  160. return 0;
  161. if (src.s_addr && src.s_addr != dst_ip.s_addr)
  162. return 0;
  163. }
  164. if (!(option_mask32 & QUIET)) {
  165. int s_printed = 0;
  166. printf("%scast re%s from %s [%s]",
  167. FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
  168. ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
  169. inet_ntoa(src_ip),
  170. ether_ntoa((struct ether_addr *) p));
  171. if (dst_ip.s_addr != src.s_addr) {
  172. printf("for %s ", inet_ntoa(dst_ip));
  173. s_printed = 1;
  174. }
  175. if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
  176. if (!s_printed)
  177. printf("for ");
  178. printf("[%s]",
  179. ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
  180. }
  181. if (last) {
  182. unsigned diff = MONOTONIC_US() - last;
  183. printf(" %u.%03ums\n", diff / 1000, diff % 1000);
  184. } else {
  185. printf(" UNSOLICITED?\n");
  186. }
  187. fflush(stdout);
  188. }
  189. received++;
  190. if (FROM->sll_pkttype != PACKET_HOST)
  191. brd_recv++;
  192. if (ah->ar_op == htons(ARPOP_REQUEST))
  193. req_recv++;
  194. if (option_mask32 & QUIT_ON_REPLY)
  195. finish();
  196. if (!(option_mask32 & BCAST_ONLY)) {
  197. memcpy(he.sll_addr, p, me.sll_halen);
  198. option_mask32 |= UNICASTING;
  199. }
  200. return 1;
  201. }
  202. int arping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  203. int arping_main(int argc, char **argv)
  204. {
  205. const char *device = "eth0";
  206. int ifindex;
  207. char *source = NULL;
  208. char *target;
  209. unsigned char *packet;
  210. sock = xsocket(PF_PACKET, SOCK_DGRAM, 0);
  211. // Drop suid root privileges
  212. xsetuid(getuid());
  213. {
  214. unsigned opt;
  215. char *str_count, *str_timeout;
  216. /* Dad also sets quit_on_reply.
  217. * Advert also sets unsolicited.
  218. */
  219. opt_complementary = "=1:Df:AU";
  220. opt = getopt32(argv, "DUAqfbc:w:I:s:",
  221. &str_count, &str_timeout, &device, &source);
  222. if (opt & 0x40) /* -c: count */
  223. count = xatou(str_count);
  224. if (opt & 0x80) /* -w: timeout */
  225. timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000;
  226. //if (opt & 0x100) /* -I: interface */
  227. if (strlen(device) >= IF_NAMESIZE) {
  228. bb_error_msg_and_die("interface name '%s' is too long",
  229. device);
  230. }
  231. //if (opt & 0x200) /* -s: source */
  232. option_mask32 &= 0x3f; /* set respective flags */
  233. }
  234. target = argv[optind];
  235. xfunc_error_retval = 2;
  236. {
  237. struct ifreq ifr;
  238. memset(&ifr, 0, sizeof(ifr));
  239. strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
  240. ioctl_or_perror_and_die(sock, SIOCGIFINDEX, &ifr, "interface %s not found", device);
  241. ifindex = ifr.ifr_ifindex;
  242. xioctl(sock, SIOCGIFFLAGS, (char *) &ifr);
  243. if (!(ifr.ifr_flags & IFF_UP)) {
  244. bb_error_msg_and_die("interface %s is down", device);
  245. }
  246. if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
  247. bb_error_msg("interface %s is not ARPable", device);
  248. return (option_mask32 & DAD ? 0 : 2);
  249. }
  250. }
  251. if (!inet_aton(target, &dst)) {
  252. len_and_sockaddr *lsa;
  253. lsa = xhost_and_af2sockaddr(target, 0, AF_INET);
  254. memcpy(&dst, &lsa->sin.sin_addr.s_addr, 4);
  255. if (ENABLE_FEATURE_CLEAN_UP)
  256. free(lsa);
  257. }
  258. if (source && !inet_aton(source, &src)) {
  259. bb_error_msg_and_die("invalid source address %s", source);
  260. }
  261. if (!(option_mask32 & DAD) && (option_mask32 & UNSOLICITED) && src.s_addr == 0)
  262. src = dst;
  263. if (!(option_mask32 & DAD) || src.s_addr) {
  264. struct sockaddr_in saddr;
  265. int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  266. if (device) {
  267. if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) == -1)
  268. bb_error_msg("warning: interface %s is ignored", device);
  269. }
  270. memset(&saddr, 0, sizeof(saddr));
  271. saddr.sin_family = AF_INET;
  272. if (src.s_addr) {
  273. saddr.sin_addr = src;
  274. xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  275. } else if (!(option_mask32 & DAD)) {
  276. socklen_t alen = sizeof(saddr);
  277. saddr.sin_port = htons(1025);
  278. saddr.sin_addr = dst;
  279. if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
  280. bb_perror_msg("warning: setsockopt(SO_DONTROUTE)");
  281. xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
  282. if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
  283. bb_error_msg_and_die("getsockname");
  284. }
  285. src = saddr.sin_addr;
  286. }
  287. close(probe_fd);
  288. }
  289. me.sll_family = AF_PACKET;
  290. me.sll_ifindex = ifindex;
  291. me.sll_protocol = htons(ETH_P_ARP);
  292. xbind(sock, (struct sockaddr *) &me, sizeof(me));
  293. {
  294. socklen_t alen = sizeof(me);
  295. if (getsockname(sock, (struct sockaddr *) &me, &alen) == -1) {
  296. bb_error_msg_and_die("getsockname");
  297. }
  298. }
  299. if (me.sll_halen == 0) {
  300. bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
  301. return (option_mask32 & DAD ? 0 : 2);
  302. }
  303. he = me;
  304. memset(he.sll_addr, -1, he.sll_halen);
  305. if (!(option_mask32 & QUIET)) {
  306. printf("ARPING to %s from %s via %s\n",
  307. inet_ntoa(dst), inet_ntoa(src),
  308. device ? device : "unknown");
  309. }
  310. if (!src.s_addr && !(option_mask32 & DAD)) {
  311. bb_error_msg_and_die("no src address in the non-DAD mode");
  312. }
  313. {
  314. struct sigaction sa;
  315. memset(&sa, 0, sizeof(sa));
  316. sa.sa_flags = SA_RESTART;
  317. sa.sa_handler = (void (*)(int)) finish;
  318. sigaction(SIGINT, &sa, NULL);
  319. sa.sa_handler = (void (*)(int)) catcher;
  320. sigaction(SIGALRM, &sa, NULL);
  321. }
  322. catcher();
  323. packet = xmalloc(4096);
  324. while (1) {
  325. sigset_t sset, osset;
  326. struct sockaddr_ll from;
  327. socklen_t alen = sizeof(from);
  328. int cc;
  329. cc = recvfrom(sock, packet, 4096, 0, (struct sockaddr *) &from, &alen);
  330. if (cc < 0) {
  331. bb_perror_msg("recvfrom");
  332. continue;
  333. }
  334. sigemptyset(&sset);
  335. sigaddset(&sset, SIGALRM);
  336. sigaddset(&sset, SIGINT);
  337. sigprocmask(SIG_BLOCK, &sset, &osset);
  338. recv_pack(packet, cc, &from);
  339. sigprocmask(SIG_SETMASK, &osset, NULL);
  340. }
  341. }