arping.c 9.8 KB

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