2 * DHCP snooping for Proxy ARP
3 * Copyright (c) 2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include <netinet/ip.h>
11 #include <netinet/udp.h>
13 #include "utils/common.h"
14 #include "l2_packet/l2_packet.h"
17 #include "ap_drv_ops.h"
19 #include "dhcp_snoop.h"
42 static const u8 ic_bootp_cookie[] = { 99, 130, 83, 99 };
45 static const char * ipaddr_str(u32 addr)
49 os_snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
50 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
51 (addr >> 8) & 0xff, addr & 0xff);
56 static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
59 struct hostapd_data *hapd = ctx;
60 const struct bootp_pkt *b;
64 int res, msgtype = 0, prefixlen = 32;
68 exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
72 b = (const struct bootp_pkt *) &buf[ETH_HLEN];
73 tot_len = ntohs(b->iph.tot_len);
74 if (tot_len > (unsigned int) (len - ETH_HLEN))
77 if (os_memcmp(b->exten, ic_bootp_cookie, ARRAY_SIZE(ic_bootp_cookie)))
80 /* Parse DHCP options */
81 end = (const u8 *) b + tot_len;
83 while (pos < end && *pos != 0xff) {
84 const u8 *opt = pos++;
86 if (*opt == 0) /* padding */
94 case 1: /* subnet mask */
96 subnet_mask = WPA_GET_BE32(&opt[2]);
99 while (!(subnet_mask & 0x1)) {
104 case 53: /* message type */
113 if (msgtype == DHCPACK) {
117 /* DHCPACK for DHCPREQUEST */
118 sta = ap_get_sta(hapd, b->hw_addr);
122 wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
123 " @ IPv4 address %s/%d",
124 MAC2STR(sta->addr), ipaddr_str(ntohl(b->your_ip)),
127 if (sta->ipaddr == b->your_ip)
130 if (sta->ipaddr != 0) {
131 wpa_printf(MSG_DEBUG,
132 "dhcp_snoop: Removing IPv4 address %s from the ip neigh table",
133 ipaddr_str(be_to_host32(sta->ipaddr)));
134 hostapd_drv_br_delete_ip_neigh(hapd, 4,
135 (u8 *) &sta->ipaddr);
138 res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
139 prefixlen, sta->addr);
141 wpa_printf(MSG_DEBUG,
142 "dhcp_snoop: Adding ip neigh table failed: %d",
146 sta->ipaddr = b->your_ip;
149 if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
150 for (sta = hapd->sta_list; sta; sta = sta->next) {
151 if (!(sta->flags & WLAN_STA_AUTHORIZED))
153 x_snoop_mcast_to_ucast_convert_send(hapd, sta,
160 int dhcp_snoop_init(struct hostapd_data *hapd)
162 hapd->sock_dhcp = x_snoop_get_l2_packet(hapd, handle_dhcp,
163 L2_PACKET_FILTER_DHCP);
164 if (hapd->sock_dhcp == NULL) {
165 wpa_printf(MSG_DEBUG,
166 "dhcp_snoop: Failed to initialize L2 packet processing for DHCP packet: %s",
175 void dhcp_snoop_deinit(struct hostapd_data *hapd)
177 l2_packet_deinit(hapd->sock_dhcp);