arpping.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mostly stolen from: dhcpcd - DHCP client daemon
  4. * by Yoichi Hariguchi <yoichi@fore.com>
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. */
  8. #include <netinet/if_ether.h>
  9. #include <net/if_arp.h>
  10. #include "common.h"
  11. struct arpMsg {
  12. /* Ethernet header */
  13. uint8_t h_dest[6]; /* 00 destination ether addr */
  14. uint8_t h_source[6]; /* 06 source ether addr */
  15. uint16_t h_proto; /* 0c packet type ID field */
  16. /* ARP packet */
  17. uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
  18. uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
  19. uint8_t hlen; /* 12 hardware address length (must be 6) */
  20. uint8_t plen; /* 13 protocol address length (must be 4) */
  21. uint16_t operation; /* 14 ARP opcode */
  22. uint8_t sHaddr[6]; /* 16 sender's hardware address */
  23. uint8_t sInaddr[4]; /* 1c sender's IP address */
  24. uint8_t tHaddr[6]; /* 20 target's hardware address */
  25. uint8_t tInaddr[4]; /* 26 target's IP address */
  26. uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
  27. } PACKED;
  28. enum {
  29. ARP_MSG_SIZE = 0x2a
  30. };
  31. /* Returns 1 if no reply received */
  32. int FAST_FUNC arpping(uint32_t test_nip,
  33. const uint8_t *safe_mac,
  34. uint32_t from_ip,
  35. uint8_t *from_mac,
  36. const char *interface,
  37. unsigned timeo)
  38. {
  39. int timeout_ms;
  40. struct pollfd pfd[1];
  41. #define s (pfd[0].fd) /* socket */
  42. int rv = 1; /* "no reply received" yet */
  43. struct sockaddr addr; /* for interface name */
  44. struct arpMsg arp;
  45. const char *msg;
  46. if (!timeo)
  47. return 1;
  48. s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
  49. if (s == -1) {
  50. bb_simple_perror_msg(bb_msg_can_not_create_raw_socket);
  51. return -1;
  52. }
  53. if (setsockopt_broadcast(s) == -1) {
  54. bb_simple_perror_msg("can't enable bcast on ARP socket");
  55. goto ret;
  56. }
  57. /* send arp request */
  58. memset(&arp, 0, sizeof(arp));
  59. memset(arp.h_dest, 0xff, 6); /* MAC DA */
  60. memcpy(arp.h_source, from_mac, 6); /* MAC SA */
  61. arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
  62. arp.htype = htons(ARPHRD_ETHER); /* hardware type */
  63. arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
  64. arp.hlen = 6; /* hardware address length */
  65. arp.plen = 4; /* protocol address length */
  66. arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
  67. memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
  68. memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
  69. /* tHaddr is zero-filled */ /* target hardware address */
  70. memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
  71. memset(&addr, 0, sizeof(addr));
  72. safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
  73. if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
  74. // TODO: error message? caller didn't expect us to fail,
  75. // just returning 1 "no reply received" misleads it.
  76. goto ret;
  77. }
  78. /* wait for arp reply, and check it */
  79. timeout_ms = (int)timeo;
  80. do {
  81. typedef uint32_t aliased_uint32_t FIX_ALIASING;
  82. int r;
  83. unsigned prevTime = monotonic_ms();
  84. pfd[0].events = POLLIN;
  85. r = safe_poll(pfd, 1, timeout_ms);
  86. if (r < 0)
  87. break;
  88. if (r) {
  89. r = safe_read(s, &arp, sizeof(arp));
  90. if (r < 0)
  91. break;
  92. //log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
  93. // arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
  94. // arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
  95. if (r >= ARP_MSG_SIZE
  96. && arp.operation == htons(ARPOP_REPLY)
  97. /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
  98. /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
  99. && *(aliased_uint32_t*)arp.sInaddr == test_nip
  100. ) {
  101. /* if ARP source MAC matches safe_mac
  102. * (which is client's MAC), then it's not a conflict
  103. * (client simply already has this IP and replies to ARPs!)
  104. */
  105. if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
  106. rv = 0;
  107. //else log2("sHaddr == safe_mac");
  108. break;
  109. }
  110. }
  111. timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1;
  112. /* We used to check "timeout_ms > 0", but
  113. * this is more under/overflow-resistant
  114. * (people did see overflows here when system time jumps):
  115. */
  116. } while ((unsigned)timeout_ms <= timeo);
  117. ret:
  118. close(s);
  119. msg = "no ARP reply received for this address";
  120. if (rv == 0)
  121. msg += 3;
  122. log1s(msg);
  123. return rv;
  124. }