15b9c547cSRui Paulo /*
25b9c547cSRui Paulo * DHCP snooping for Proxy ARP
35b9c547cSRui Paulo * Copyright (c) 2014, Qualcomm Atheros, Inc.
45b9c547cSRui Paulo *
55b9c547cSRui Paulo * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo * See README for more details.
75b9c547cSRui Paulo */
85b9c547cSRui Paulo
95b9c547cSRui Paulo #include "utils/includes.h"
105b9c547cSRui Paulo
115b9c547cSRui Paulo #include "utils/common.h"
1285732ac8SCy Schubert #include "common/dhcp.h"
135b9c547cSRui Paulo #include "l2_packet/l2_packet.h"
145b9c547cSRui Paulo #include "hostapd.h"
155b9c547cSRui Paulo #include "sta_info.h"
165b9c547cSRui Paulo #include "ap_drv_ops.h"
175b9c547cSRui Paulo #include "x_snoop.h"
185b9c547cSRui Paulo #include "dhcp_snoop.h"
195b9c547cSRui Paulo
205b9c547cSRui Paulo
ipaddr_str(u32 addr)215b9c547cSRui Paulo static const char * ipaddr_str(u32 addr)
225b9c547cSRui Paulo {
235b9c547cSRui Paulo static char buf[17];
245b9c547cSRui Paulo
255b9c547cSRui Paulo os_snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
265b9c547cSRui Paulo (addr >> 24) & 0xff, (addr >> 16) & 0xff,
275b9c547cSRui Paulo (addr >> 8) & 0xff, addr & 0xff);
285b9c547cSRui Paulo return buf;
295b9c547cSRui Paulo }
305b9c547cSRui Paulo
315b9c547cSRui Paulo
handle_dhcp(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)325b9c547cSRui Paulo static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
335b9c547cSRui Paulo size_t len)
345b9c547cSRui Paulo {
355b9c547cSRui Paulo struct hostapd_data *hapd = ctx;
365b9c547cSRui Paulo const struct bootp_pkt *b;
375b9c547cSRui Paulo struct sta_info *sta;
385b9c547cSRui Paulo int exten_len;
395b9c547cSRui Paulo const u8 *end, *pos;
405b9c547cSRui Paulo int res, msgtype = 0, prefixlen = 32;
415b9c547cSRui Paulo u32 subnet_mask = 0;
42c1d255d3SCy Schubert u16 ip_len;
435b9c547cSRui Paulo
445b9c547cSRui Paulo exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
455b9c547cSRui Paulo if (exten_len < 4)
465b9c547cSRui Paulo return;
475b9c547cSRui Paulo
485b9c547cSRui Paulo b = (const struct bootp_pkt *) &buf[ETH_HLEN];
49c1d255d3SCy Schubert ip_len = ntohs(b->iph.ip_len);
50c1d255d3SCy Schubert if (ip_len > (unsigned int) (len - ETH_HLEN))
515b9c547cSRui Paulo return;
525b9c547cSRui Paulo
5385732ac8SCy Schubert if (WPA_GET_BE32(b->exten) != DHCP_MAGIC)
545b9c547cSRui Paulo return;
555b9c547cSRui Paulo
565b9c547cSRui Paulo /* Parse DHCP options */
57c1d255d3SCy Schubert end = (const u8 *) b + ip_len;
585b9c547cSRui Paulo pos = &b->exten[4];
5985732ac8SCy Schubert while (pos < end && *pos != DHCP_OPT_END) {
605b9c547cSRui Paulo const u8 *opt = pos++;
615b9c547cSRui Paulo
6285732ac8SCy Schubert if (*opt == DHCP_OPT_PAD)
635b9c547cSRui Paulo continue;
645b9c547cSRui Paulo
6585732ac8SCy Schubert if (pos >= end || 1 + *pos > end - pos)
6685732ac8SCy Schubert break;
675b9c547cSRui Paulo pos += *pos + 1;
685b9c547cSRui Paulo if (pos >= end)
695b9c547cSRui Paulo break;
705b9c547cSRui Paulo
715b9c547cSRui Paulo switch (*opt) {
7285732ac8SCy Schubert case DHCP_OPT_SUBNET_MASK:
735b9c547cSRui Paulo if (opt[1] == 4)
745b9c547cSRui Paulo subnet_mask = WPA_GET_BE32(&opt[2]);
755b9c547cSRui Paulo if (subnet_mask == 0)
765b9c547cSRui Paulo return;
775b9c547cSRui Paulo while (!(subnet_mask & 0x1)) {
785b9c547cSRui Paulo subnet_mask >>= 1;
795b9c547cSRui Paulo prefixlen--;
805b9c547cSRui Paulo }
815b9c547cSRui Paulo break;
8285732ac8SCy Schubert case DHCP_OPT_MSG_TYPE:
835b9c547cSRui Paulo if (opt[1])
845b9c547cSRui Paulo msgtype = opt[2];
855b9c547cSRui Paulo break;
865b9c547cSRui Paulo default:
875b9c547cSRui Paulo break;
885b9c547cSRui Paulo }
895b9c547cSRui Paulo }
905b9c547cSRui Paulo
914b72b91aSCy Schubert #ifdef CONFIG_HS20
924bc52338SCy Schubert if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
934bc52338SCy Schubert for (sta = hapd->sta_list; sta; sta = sta->next) {
944bc52338SCy Schubert if (!(sta->flags & WLAN_STA_AUTHORIZED))
954bc52338SCy Schubert continue;
964bc52338SCy Schubert x_snoop_mcast_to_ucast_convert_send(hapd, sta,
974bc52338SCy Schubert (u8 *) buf, len);
984bc52338SCy Schubert }
994bc52338SCy Schubert }
1004b72b91aSCy Schubert #endif /* CONFIG_HS20 */
1014bc52338SCy Schubert
1025b9c547cSRui Paulo if (msgtype == DHCPACK) {
1035b9c547cSRui Paulo if (b->your_ip == 0)
1045b9c547cSRui Paulo return;
1055b9c547cSRui Paulo
1065b9c547cSRui Paulo /* DHCPACK for DHCPREQUEST */
1075b9c547cSRui Paulo sta = ap_get_sta(hapd, b->hw_addr);
1085b9c547cSRui Paulo if (!sta)
1095b9c547cSRui Paulo return;
1105b9c547cSRui Paulo
1115b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
1125b9c547cSRui Paulo " @ IPv4 address %s/%d",
113780fb4a2SCy Schubert MAC2STR(sta->addr),
114780fb4a2SCy Schubert ipaddr_str(be_to_host32(b->your_ip)),
1155b9c547cSRui Paulo prefixlen);
1165b9c547cSRui Paulo
1175b9c547cSRui Paulo if (sta->ipaddr == b->your_ip)
1185b9c547cSRui Paulo return;
1195b9c547cSRui Paulo
1205b9c547cSRui Paulo if (sta->ipaddr != 0) {
1215b9c547cSRui Paulo wpa_printf(MSG_DEBUG,
1225b9c547cSRui Paulo "dhcp_snoop: Removing IPv4 address %s from the ip neigh table",
1235b9c547cSRui Paulo ipaddr_str(be_to_host32(sta->ipaddr)));
1245b9c547cSRui Paulo hostapd_drv_br_delete_ip_neigh(hapd, 4,
1255b9c547cSRui Paulo (u8 *) &sta->ipaddr);
1265b9c547cSRui Paulo }
1275b9c547cSRui Paulo
1285b9c547cSRui Paulo res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
1295b9c547cSRui Paulo prefixlen, sta->addr);
1305b9c547cSRui Paulo if (res) {
1315b9c547cSRui Paulo wpa_printf(MSG_DEBUG,
1325b9c547cSRui Paulo "dhcp_snoop: Adding ip neigh table failed: %d",
1335b9c547cSRui Paulo res);
1345b9c547cSRui Paulo return;
1355b9c547cSRui Paulo }
1365b9c547cSRui Paulo sta->ipaddr = b->your_ip;
1375b9c547cSRui Paulo }
1385b9c547cSRui Paulo }
1395b9c547cSRui Paulo
1405b9c547cSRui Paulo
dhcp_snoop_init(struct hostapd_data * hapd)1415b9c547cSRui Paulo int dhcp_snoop_init(struct hostapd_data *hapd)
1425b9c547cSRui Paulo {
1435b9c547cSRui Paulo hapd->sock_dhcp = x_snoop_get_l2_packet(hapd, handle_dhcp,
1445b9c547cSRui Paulo L2_PACKET_FILTER_DHCP);
1455b9c547cSRui Paulo if (hapd->sock_dhcp == NULL) {
1465b9c547cSRui Paulo wpa_printf(MSG_DEBUG,
1475b9c547cSRui Paulo "dhcp_snoop: Failed to initialize L2 packet processing for DHCP packet: %s",
1485b9c547cSRui Paulo strerror(errno));
1495b9c547cSRui Paulo return -1;
1505b9c547cSRui Paulo }
1515b9c547cSRui Paulo
1525b9c547cSRui Paulo return 0;
1535b9c547cSRui Paulo }
1545b9c547cSRui Paulo
1555b9c547cSRui Paulo
dhcp_snoop_deinit(struct hostapd_data * hapd)1565b9c547cSRui Paulo void dhcp_snoop_deinit(struct hostapd_data *hapd)
1575b9c547cSRui Paulo {
1585b9c547cSRui Paulo l2_packet_deinit(hapd->sock_dhcp);
15985732ac8SCy Schubert hapd->sock_dhcp = NULL;
1605b9c547cSRui Paulo }
161