]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/drv_callbacks.c
Move rest of the generic AP mode functionality into src/ap
[thirdparty/hostap.git] / src / ap / drv_callbacks.c
1 /*
2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "radius/radius.h"
19 #include "ap/hostapd.h"
20 #include "ap/ieee802_11.h"
21 #include "ap/sta_info.h"
22 #include "ap/accounting.h"
23 #include "ap/tkip_countermeasures.h"
24 #include "ap/iapp.h"
25 #include "ap/ieee802_1x.h"
26 #include "ap/wpa.h"
27 #include "ap/wmm.h"
28 #include "ap/wps_hostapd.h"
29 #include "driver_i.h"
30
31
32 int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr)
33 {
34 struct sta_info *sta = ap_get_sta(hapd, addr);
35 if (sta)
36 return 0;
37
38 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
39 " - adding a new STA", MAC2STR(addr));
40 sta = ap_sta_add(hapd, addr);
41 if (sta) {
42 hostapd_new_assoc_sta(hapd, sta, 0);
43 } else {
44 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
45 MAC2STR(addr));
46 return -1;
47 }
48
49 return 0;
50 }
51
52
53 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
54 const u8 *ie, size_t ielen)
55 {
56 struct sta_info *sta;
57 int new_assoc, res;
58
59 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
60 HOSTAPD_LEVEL_INFO, "associated");
61
62 sta = ap_get_sta(hapd, addr);
63 if (sta) {
64 accounting_sta_stop(hapd, sta);
65 } else {
66 sta = ap_sta_add(hapd, addr);
67 if (sta == NULL)
68 return -1;
69 }
70 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
71
72 if (hapd->conf->wpa) {
73 if (ie == NULL || ielen == 0) {
74 if (hapd->conf->wps_state) {
75 wpa_printf(MSG_DEBUG, "STA did not include "
76 "WPA/RSN IE in (Re)Association "
77 "Request - possible WPS use");
78 sta->flags |= WLAN_STA_MAYBE_WPS;
79 goto skip_wpa_check;
80 }
81
82 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
83 return -1;
84 }
85 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
86 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
87 sta->flags |= WLAN_STA_WPS;
88 goto skip_wpa_check;
89 }
90
91 if (sta->wpa_sm == NULL)
92 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
93 sta->addr);
94 if (sta->wpa_sm == NULL) {
95 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
96 "machine");
97 return -1;
98 }
99 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
100 ie, ielen, NULL, 0);
101 if (res != WPA_IE_OK) {
102 int resp;
103 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
104 "rejected? (res %u)", res);
105 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
106 if (res == WPA_INVALID_GROUP)
107 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
108 else if (res == WPA_INVALID_PAIRWISE)
109 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
110 else if (res == WPA_INVALID_AKMP)
111 resp = WLAN_REASON_AKMP_NOT_VALID;
112 #ifdef CONFIG_IEEE80211W
113 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
114 resp = WLAN_REASON_INVALID_IE;
115 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
116 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
117 #endif /* CONFIG_IEEE80211W */
118 else
119 resp = WLAN_REASON_INVALID_IE;
120 hapd->drv.sta_disassoc(hapd, sta->addr, resp);
121 ap_free_sta(hapd, sta);
122 return -1;
123 }
124 } else if (hapd->conf->wps_state) {
125 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
126 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
127 sta->flags |= WLAN_STA_WPS;
128 } else
129 sta->flags |= WLAN_STA_MAYBE_WPS;
130 }
131 skip_wpa_check:
132
133 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
134 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
135 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
136
137 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
138
139 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
140
141 return 0;
142 }
143
144
145 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
146 {
147 struct sta_info *sta;
148
149 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
150 HOSTAPD_LEVEL_INFO, "disassociated");
151
152 sta = ap_get_sta(hapd, addr);
153 if (sta == NULL) {
154 wpa_printf(MSG_DEBUG, "Disassociation notification for "
155 "unknown STA " MACSTR, MAC2STR(addr));
156 return;
157 }
158
159 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
160 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
161 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
162 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
163 ap_free_sta(hapd, sta);
164 }
165
166
167 void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
168 const u8 *buf, size_t len)
169 {
170 ieee802_1x_receive(hapd, sa, buf, len);
171 }
172
173
174 struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
175 const u8 *addr)
176 {
177 struct hostapd_iface *iface = hapd->iface;
178 size_t j;
179
180 for (j = 0; j < iface->num_bss; j++) {
181 hapd = iface->bss[j];
182 if (ap_get_sta(hapd, addr))
183 return hapd;
184 }
185
186 return NULL;
187 }
188
189
190 #ifdef HOSTAPD
191
192 #ifdef NEED_AP_MLME
193
194 static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
195 {
196 u16 fc, type, stype;
197
198 /*
199 * PS-Poll frames are 16 bytes. All other frames are
200 * 24 bytes or longer.
201 */
202 if (len < 16)
203 return NULL;
204
205 fc = le_to_host16(hdr->frame_control);
206 type = WLAN_FC_GET_TYPE(fc);
207 stype = WLAN_FC_GET_STYPE(fc);
208
209 switch (type) {
210 case WLAN_FC_TYPE_DATA:
211 if (len < 24)
212 return NULL;
213 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
214 case WLAN_FC_FROMDS | WLAN_FC_TODS:
215 case WLAN_FC_TODS:
216 return hdr->addr1;
217 case WLAN_FC_FROMDS:
218 return hdr->addr2;
219 default:
220 return NULL;
221 }
222 case WLAN_FC_TYPE_CTRL:
223 if (stype != WLAN_FC_STYPE_PSPOLL)
224 return NULL;
225 return hdr->addr1;
226 case WLAN_FC_TYPE_MGMT:
227 return hdr->addr3;
228 default:
229 return NULL;
230 }
231 }
232
233
234 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
235
236 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
237 const u8 *bssid)
238 {
239 size_t i;
240
241 if (bssid == NULL)
242 return NULL;
243 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
244 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
245 return HAPD_BROADCAST;
246
247 for (i = 0; i < iface->num_bss; i++) {
248 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
249 return iface->bss[i];
250 }
251
252 return NULL;
253 }
254
255
256 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
257 const struct ieee80211_hdr *hdr,
258 size_t len)
259 {
260 u16 fc = le_to_host16(hdr->frame_control);
261 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
262 if (hapd == NULL || hapd == HAPD_BROADCAST)
263 return;
264
265 ieee802_11_rx_from_unknown(hapd, hdr->addr2,
266 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
267 (WLAN_FC_TODS | WLAN_FC_FROMDS));
268 }
269
270
271 static void hostapd_mgmt_rx(struct hostapd_data *hapd, const u8 *buf,
272 size_t len, struct hostapd_frame_info *fi)
273 {
274 struct hostapd_iface *iface = hapd->iface;
275 const struct ieee80211_hdr *hdr;
276 const u8 *bssid;
277
278 hdr = (const struct ieee80211_hdr *) buf;
279 bssid = get_hdr_bssid(hdr, len);
280 if (bssid == NULL)
281 return;
282
283 hapd = get_hapd_bssid(iface, bssid);
284 if (hapd == NULL) {
285 u16 fc;
286 fc = le_to_host16(hdr->frame_control);
287
288 /*
289 * Drop frames to unknown BSSIDs except for Beacon frames which
290 * could be used to update neighbor information.
291 */
292 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
293 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
294 hapd = iface->bss[0];
295 else
296 return;
297 }
298
299 if (hapd == HAPD_BROADCAST) {
300 size_t i;
301 for (i = 0; i < iface->num_bss; i++)
302 ieee802_11_mgmt(iface->bss[i], buf, len, fi);
303 } else
304 ieee802_11_mgmt(hapd, buf, len, fi);
305 }
306
307
308 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
309 size_t len, u16 stype, int ok)
310 {
311 struct ieee80211_hdr *hdr;
312 hdr = (struct ieee80211_hdr *) buf;
313 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
314 if (hapd == NULL || hapd == HAPD_BROADCAST)
315 return;
316 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
317 }
318
319 #endif /* NEED_AP_MLME */
320
321
322 void wpa_supplicant_event(void *ctx, wpa_event_type event,
323 union wpa_event_data *data)
324 {
325 struct hostapd_data *hapd = ctx;
326
327 switch (event) {
328 case EVENT_MICHAEL_MIC_FAILURE:
329 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
330 break;
331 case EVENT_SCAN_RESULTS:
332 if (hapd->iface->scan_cb)
333 hapd->iface->scan_cb(hapd->iface);
334 break;
335 #ifdef CONFIG_IEEE80211R
336 case EVENT_FT_RRB_RX:
337 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
338 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
339 break;
340 #endif /* CONFIG_IEEE80211R */
341 case EVENT_WPS_BUTTON_PUSHED:
342 hostapd_wps_button_pushed(hapd);
343 break;
344 #ifdef NEED_AP_MLME
345 case EVENT_TX_STATUS:
346 switch (data->tx_status.type) {
347 case WLAN_FC_TYPE_MGMT:
348 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
349 data->tx_status.data_len,
350 data->tx_status.stype,
351 data->tx_status.ack);
352 break;
353 case WLAN_FC_TYPE_DATA:
354 hostapd_tx_status(hapd, data->tx_status.dst,
355 data->tx_status.data,
356 data->tx_status.data_len,
357 data->tx_status.ack);
358 break;
359 }
360 break;
361 case EVENT_RX_FROM_UNKNOWN:
362 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.hdr,
363 data->rx_from_unknown.len);
364 break;
365 case EVENT_RX_MGMT:
366 hostapd_mgmt_rx(hapd, data->rx_mgmt.frame,
367 data->rx_mgmt.frame_len, data->rx_mgmt.fi);
368 break;
369 #endif /* NEED_AP_MLME */
370 default:
371 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
372 break;
373 }
374 }
375
376 #endif /* HOSTAPD */
377
378
379 void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
380 const u8 *ie, size_t ie_len)
381 {
382 size_t i;
383
384 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
385 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
386 sa, ie, ie_len);
387 }