xref: /freebsd/contrib/wpa/src/ap/dhcp_snoop.c (revision f499134dd403eeeba8283e2640e2654c8da62430)
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 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "common/dhcp.h"
13 #include "l2_packet/l2_packet.h"
14 #include "hostapd.h"
15 #include "sta_info.h"
16 #include "ap_drv_ops.h"
17 #include "x_snoop.h"
18 #include "dhcp_snoop.h"
19 
20 
21 static const char * ipaddr_str(u32 addr)
22 {
23 	static char buf[17];
24 
25 	os_snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
26 		    (addr >> 24) & 0xff, (addr >> 16) & 0xff,
27 		    (addr >> 8) & 0xff, addr & 0xff);
28 	return buf;
29 }
30 
31 
32 static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
33 			size_t len)
34 {
35 	struct hostapd_data *hapd = ctx;
36 	const struct bootp_pkt *b;
37 	struct sta_info *sta;
38 	int exten_len;
39 	const u8 *end, *pos;
40 	int res, msgtype = 0, prefixlen = 32;
41 	u32 subnet_mask = 0;
42 	u16 ip_len;
43 
44 	exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
45 	if (exten_len < 4)
46 		return;
47 
48 	b = (const struct bootp_pkt *) &buf[ETH_HLEN];
49 	ip_len = ntohs(b->iph.ip_len);
50 	if (ip_len > (unsigned int) (len - ETH_HLEN))
51 		return;
52 
53 	if (WPA_GET_BE32(b->exten) != DHCP_MAGIC)
54 		return;
55 
56 	/* Parse DHCP options */
57 	end = (const u8 *) b + ip_len;
58 	pos = &b->exten[4];
59 	while (pos < end && *pos != DHCP_OPT_END) {
60 		const u8 *opt = pos++;
61 
62 		if (*opt == DHCP_OPT_PAD)
63 			continue;
64 
65 		if (pos >= end || 1 + *pos > end - pos)
66 			break;
67 		pos += *pos + 1;
68 		if (pos >= end)
69 			break;
70 
71 		switch (*opt) {
72 		case DHCP_OPT_SUBNET_MASK:
73 			if (opt[1] == 4)
74 				subnet_mask = WPA_GET_BE32(&opt[2]);
75 			if (subnet_mask == 0)
76 				return;
77 			while (!(subnet_mask & 0x1)) {
78 				subnet_mask >>= 1;
79 				prefixlen--;
80 			}
81 			break;
82 		case DHCP_OPT_MSG_TYPE:
83 			if (opt[1])
84 				msgtype = opt[2];
85 			break;
86 		default:
87 			break;
88 		}
89 	}
90 
91 #ifdef CONFIG_HS20
92 	if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
93 		for (sta = hapd->sta_list; sta; sta = sta->next) {
94 			if (!(sta->flags & WLAN_STA_AUTHORIZED))
95 				continue;
96 			x_snoop_mcast_to_ucast_convert_send(hapd, sta,
97 							    (u8 *) buf, len);
98 		}
99 	}
100 #endif /* CONFIG_HS20 */
101 
102 	if (msgtype == DHCPACK) {
103 		if (b->your_ip == 0)
104 			return;
105 
106 		/* DHCPACK for DHCPREQUEST */
107 		sta = ap_get_sta(hapd, b->hw_addr);
108 		if (!sta)
109 			return;
110 
111 		wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
112 			   " @ IPv4 address %s/%d",
113 			   MAC2STR(sta->addr),
114 			   ipaddr_str(be_to_host32(b->your_ip)),
115 			   prefixlen);
116 
117 		if (sta->ipaddr == b->your_ip)
118 			return;
119 
120 		if (sta->ipaddr != 0) {
121 			wpa_printf(MSG_DEBUG,
122 				   "dhcp_snoop: Removing IPv4 address %s from the ip neigh table",
123 				   ipaddr_str(be_to_host32(sta->ipaddr)));
124 			hostapd_drv_br_delete_ip_neigh(hapd, 4,
125 						       (u8 *) &sta->ipaddr);
126 		}
127 
128 		res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
129 						  prefixlen, sta->addr);
130 		if (res) {
131 			wpa_printf(MSG_DEBUG,
132 				   "dhcp_snoop: Adding ip neigh table failed: %d",
133 				   res);
134 			return;
135 		}
136 		sta->ipaddr = b->your_ip;
137 	}
138 }
139 
140 
141 int dhcp_snoop_init(struct hostapd_data *hapd)
142 {
143 	hapd->sock_dhcp = x_snoop_get_l2_packet(hapd, handle_dhcp,
144 						L2_PACKET_FILTER_DHCP);
145 	if (hapd->sock_dhcp == NULL) {
146 		wpa_printf(MSG_DEBUG,
147 			   "dhcp_snoop: Failed to initialize L2 packet processing for DHCP packet: %s",
148 			   strerror(errno));
149 		return -1;
150 	}
151 
152 	return 0;
153 }
154 
155 
156 void dhcp_snoop_deinit(struct hostapd_data *hapd)
157 {
158 	l2_packet_deinit(hapd->sock_dhcp);
159 	hapd->sock_dhcp = NULL;
160 }
161