arpping.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * arpping.c
  4. *
  5. * Mostly stolen from: dhcpcd - DHCP client daemon
  6. * by Yoichi Hariguchi <yoichi@fore.com>
  7. */
  8. #include <netinet/if_ether.h>
  9. #include <net/if_arp.h>
  10. #include "common.h"
  11. #include "dhcpd.h"
  12. struct arpMsg {
  13. /* Ethernet header */
  14. uint8_t h_dest[6]; /* destination ether addr */
  15. uint8_t h_source[6]; /* source ether addr */
  16. uint16_t h_proto; /* packet type ID field */
  17. /* ARP packet */
  18. uint16_t htype; /* hardware type (must be ARPHRD_ETHER) */
  19. uint16_t ptype; /* protocol type (must be ETH_P_IP) */
  20. uint8_t hlen; /* hardware address length (must be 6) */
  21. uint8_t plen; /* protocol address length (must be 4) */
  22. uint16_t operation; /* ARP opcode */
  23. uint8_t sHaddr[6]; /* sender's hardware address */
  24. uint8_t sInaddr[4]; /* sender's IP address */
  25. uint8_t tHaddr[6]; /* target's hardware address */
  26. uint8_t tInaddr[4]; /* target's IP address */
  27. uint8_t pad[18]; /* pad for min. Ethernet payload (60 bytes) */
  28. } ATTRIBUTE_PACKED;
  29. /* args: yiaddr - what IP to ping
  30. * ip - our ip
  31. * mac - our arp address
  32. * interface - interface to use
  33. * retn: 1 addr free
  34. * 0 addr used
  35. * -1 error
  36. */
  37. /* FIXME: match response against chaddr */
  38. int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
  39. {
  40. int timeout = 2;
  41. int s; /* socket */
  42. int rv = 1; /* return value */
  43. struct sockaddr addr; /* for interface name */
  44. struct arpMsg arp;
  45. fd_set fdset;
  46. struct timeval tm;
  47. time_t prevTime;
  48. s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
  49. if (s == -1) {
  50. bb_perror_msg(bb_msg_can_not_create_raw_socket);
  51. return -1;
  52. }
  53. if (setsockopt_broadcast(s) == -1) {
  54. bb_perror_msg("cannot setsocketopt on raw socket");
  55. close(s);
  56. return -1;
  57. }
  58. /* send arp request */
  59. memset(&arp, 0, sizeof(arp));
  60. memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */
  61. memcpy(arp.h_source, mac, 6); /* MAC SA */
  62. arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
  63. arp.htype = htons(ARPHRD_ETHER); /* hardware type */
  64. arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
  65. arp.hlen = 6; /* hardware address length */
  66. arp.plen = 4; /* protocol address length */
  67. arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
  68. memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */
  69. memcpy(arp.sHaddr, mac, 6); /* source hardware address */
  70. memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */
  71. memset(&addr, 0, sizeof(addr));
  72. strcpy(addr.sa_data, interface);
  73. if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
  74. rv = 0;
  75. /* wait arp reply, and check it */
  76. tm.tv_usec = 0;
  77. prevTime = uptime();
  78. while (timeout > 0) {
  79. FD_ZERO(&fdset);
  80. FD_SET(s, &fdset);
  81. tm.tv_sec = timeout;
  82. if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
  83. bb_perror_msg("error on ARPING request");
  84. if (errno != EINTR) rv = 0;
  85. } else if (FD_ISSET(s, &fdset)) {
  86. if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
  87. if (arp.operation == htons(ARPOP_REPLY) &&
  88. memcmp(arp.tHaddr, mac, 6) == 0 &&
  89. *((uint32_t *) arp.sInaddr) == yiaddr) {
  90. DEBUG("Valid arp reply received for this address");
  91. rv = 0;
  92. break;
  93. }
  94. }
  95. timeout -= uptime() - prevTime;
  96. prevTime = uptime();
  97. }
  98. close(s);
  99. DEBUG("%salid arp replies for this address", rv ? "No v" : "V");
  100. return rv;
  101. }