dhcp_snoop.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * DHCP snooping for Proxy ARP
  3. * Copyright (c) 2014, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/dhcp.h"
  11. #include "l2_packet/l2_packet.h"
  12. #include "hostapd.h"
  13. #include "sta_info.h"
  14. #include "ap_drv_ops.h"
  15. #include "x_snoop.h"
  16. #include "dhcp_snoop.h"
  17. static const char * ipaddr_str(u32 addr)
  18. {
  19. static char buf[17];
  20. os_snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
  21. (addr >> 24) & 0xff, (addr >> 16) & 0xff,
  22. (addr >> 8) & 0xff, addr & 0xff);
  23. return buf;
  24. }
  25. static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
  26. size_t len)
  27. {
  28. struct hostapd_data *hapd = ctx;
  29. const struct bootp_pkt *b;
  30. struct sta_info *sta;
  31. int exten_len;
  32. const u8 *end, *pos;
  33. int res, msgtype = 0, prefixlen = 32;
  34. u32 subnet_mask = 0;
  35. u16 tot_len;
  36. exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
  37. if (exten_len < 4)
  38. return;
  39. b = (const struct bootp_pkt *) &buf[ETH_HLEN];
  40. tot_len = ntohs(b->iph.tot_len);
  41. if (tot_len > (unsigned int) (len - ETH_HLEN))
  42. return;
  43. if (WPA_GET_BE32(b->exten) != DHCP_MAGIC)
  44. return;
  45. /* Parse DHCP options */
  46. end = (const u8 *) b + tot_len;
  47. pos = &b->exten[4];
  48. while (pos < end && *pos != DHCP_OPT_END) {
  49. const u8 *opt = pos++;
  50. if (*opt == DHCP_OPT_PAD)
  51. continue;
  52. if (pos >= end || 1 + *pos > end - pos)
  53. break;
  54. pos += *pos + 1;
  55. if (pos >= end)
  56. break;
  57. switch (*opt) {
  58. case DHCP_OPT_SUBNET_MASK:
  59. if (opt[1] == 4)
  60. subnet_mask = WPA_GET_BE32(&opt[2]);
  61. if (subnet_mask == 0)
  62. return;
  63. while (!(subnet_mask & 0x1)) {
  64. subnet_mask >>= 1;
  65. prefixlen--;
  66. }
  67. break;
  68. case DHCP_OPT_MSG_TYPE:
  69. if (opt[1])
  70. msgtype = opt[2];
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. if (msgtype == DHCPACK) {
  77. if (b->your_ip == 0)
  78. return;
  79. /* DHCPACK for DHCPREQUEST */
  80. sta = ap_get_sta(hapd, b->hw_addr);
  81. if (!sta)
  82. return;
  83. wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
  84. " @ IPv4 address %s/%d",
  85. MAC2STR(sta->addr),
  86. ipaddr_str(be_to_host32(b->your_ip)),
  87. prefixlen);
  88. if (sta->ipaddr == b->your_ip)
  89. return;
  90. if (sta->ipaddr != 0) {
  91. wpa_printf(MSG_DEBUG,
  92. "dhcp_snoop: Removing IPv4 address %s from the ip neigh table",
  93. ipaddr_str(be_to_host32(sta->ipaddr)));
  94. hostapd_drv_br_delete_ip_neigh(hapd, 4,
  95. (u8 *) &sta->ipaddr);
  96. }
  97. res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
  98. prefixlen, sta->addr);
  99. if (res) {
  100. wpa_printf(MSG_DEBUG,
  101. "dhcp_snoop: Adding ip neigh table failed: %d",
  102. res);
  103. return;
  104. }
  105. sta->ipaddr = b->your_ip;
  106. }
  107. if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
  108. for (sta = hapd->sta_list; sta; sta = sta->next) {
  109. if (!(sta->flags & WLAN_STA_AUTHORIZED))
  110. continue;
  111. x_snoop_mcast_to_ucast_convert_send(hapd, sta,
  112. (u8 *) buf, len);
  113. }
  114. }
  115. }
  116. int dhcp_snoop_init(struct hostapd_data *hapd)
  117. {
  118. hapd->sock_dhcp = x_snoop_get_l2_packet(hapd, handle_dhcp,
  119. L2_PACKET_FILTER_DHCP);
  120. if (hapd->sock_dhcp == NULL) {
  121. wpa_printf(MSG_DEBUG,
  122. "dhcp_snoop: Failed to initialize L2 packet processing for DHCP packet: %s",
  123. strerror(errno));
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. void dhcp_snoop_deinit(struct hostapd_data *hapd)
  129. {
  130. l2_packet_deinit(hapd->sock_dhcp);
  131. hapd->sock_dhcp = NULL;
  132. }