arping.c 10 KB

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