]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
P2P: Allow local configuration to use 5 GHz band 40 MHz channels
[thirdparty/hostap.git] / src / ap / drv_callbacks.c
CommitLineData
b5b969e9
JM
1/*
2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
b5b969e9
JM
7 */
8
6226e38d 9#include "utils/includes.h"
b5b969e9 10
6226e38d 11#include "utils/common.h"
b5b969e9 12#include "radius/radius.h"
6e6e8c31 13#include "drivers/driver.h"
81f4f619 14#include "common/ieee802_11_defs.h"
c41a1095 15#include "common/ieee802_11_common.h"
bbb921da 16#include "crypto/random.h"
ef796391 17#include "p2p/p2p.h"
54f489be 18#include "wps/wps.h"
d32d94db 19#include "wnm_ap.h"
6226e38d
JM
20#include "hostapd.h"
21#include "ieee802_11.h"
22#include "sta_info.h"
23#include "accounting.h"
24#include "tkip_countermeasures.h"
6226e38d
JM
25#include "ieee802_1x.h"
26#include "wpa_auth.h"
6226e38d 27#include "wps_hostapd.h"
51e2a27a 28#include "ap_drv_ops.h"
8b06c1ed 29#include "ap_config.h"
1b487b8b 30#include "hw_features.h"
b5b969e9
JM
31
32
b5b969e9 33int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
2bb20281 34 const u8 *req_ies, size_t req_ies_len, int reassoc)
b5b969e9
JM
35{
36 struct sta_info *sta;
37 int new_assoc, res;
c41a1095 38 struct ieee802_11_elems elems;
2bb20281
JM
39 const u8 *ie;
40 size_t ielen;
88b32a99
SP
41#ifdef CONFIG_IEEE80211R
42 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
43 u8 *p = buf;
44#endif /* CONFIG_IEEE80211R */
08a74e6a 45 u16 reason = WLAN_REASON_UNSPECIFIED;
88b32a99 46 u16 status = WLAN_STATUS_SUCCESS;
b5b969e9 47
68532a9c
JM
48 if (addr == NULL) {
49 /*
50 * This could potentially happen with unexpected event from the
51 * driver wrapper. This was seen at least in one case where the
52 * driver ended up being set to station mode while hostapd was
53 * running, so better make sure we stop processing such an
54 * event here.
55 */
56 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
57 "no address");
58 return -1;
59 }
bbb921da 60 random_add_randomness(addr, ETH_ALEN);
68532a9c 61
b5b969e9
JM
62 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
63 HOSTAPD_LEVEL_INFO, "associated");
64
2bb20281 65 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
c41a1095
JM
66 if (elems.wps_ie) {
67 ie = elems.wps_ie - 2;
68 ielen = elems.wps_ie_len + 2;
69 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
70 } else if (elems.rsn_ie) {
71 ie = elems.rsn_ie - 2;
72 ielen = elems.rsn_ie_len + 2;
73 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
74 } else if (elems.wpa_ie) {
75 ie = elems.wpa_ie - 2;
76 ielen = elems.wpa_ie_len + 2;
77 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
78 } else {
79 ie = NULL;
80 ielen = 0;
81 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
82 "(Re)AssocReq");
83 }
84
b5b969e9
JM
85 sta = ap_get_sta(hapd, addr);
86 if (sta) {
87 accounting_sta_stop(hapd, sta);
c72bd6d4
JM
88
89 /*
90 * Make sure that the previously registered inactivity timer
91 * will not remove the STA immediately.
92 */
93 sta->timeout_next = STA_NULLFUNC;
b5b969e9
JM
94 } else {
95 sta = ap_sta_add(hapd, addr);
8bd0fc0e
JM
96 if (sta == NULL) {
97 hostapd_drv_sta_disassoc(hapd, addr,
98 WLAN_REASON_DISASSOC_AP_BUSY);
b5b969e9 99 return -1;
8bd0fc0e 100 }
b5b969e9 101 }
17f6b900 102 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
b5b969e9 103
b305c684
JM
104#ifdef CONFIG_P2P
105 if (elems.p2p) {
106 wpabuf_free(sta->p2p_ie);
2bb20281 107 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
b305c684
JM
108 P2P_IE_VENDOR_TYPE);
109 }
110#endif /* CONFIG_P2P */
111
f403dcd6
JM
112#ifdef CONFIG_HS20
113 wpabuf_free(sta->hs20_ie);
114 if (elems.hs20 && elems.hs20_len > 4) {
115 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
116 elems.hs20_len - 4);
117 } else
118 sta->hs20_ie = NULL;
119#endif /* CONFIG_HS20 */
120
b5b969e9
JM
121 if (hapd->conf->wpa) {
122 if (ie == NULL || ielen == 0) {
633d4469 123#ifdef CONFIG_WPS
b5b969e9
JM
124 if (hapd->conf->wps_state) {
125 wpa_printf(MSG_DEBUG, "STA did not include "
126 "WPA/RSN IE in (Re)Association "
127 "Request - possible WPS use");
128 sta->flags |= WLAN_STA_MAYBE_WPS;
129 goto skip_wpa_check;
130 }
633d4469 131#endif /* CONFIG_WPS */
b5b969e9
JM
132
133 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
134 return -1;
135 }
633d4469 136#ifdef CONFIG_WPS
b5b969e9
JM
137 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
138 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
17f6b900 139 struct wpabuf *wps;
b5b969e9 140 sta->flags |= WLAN_STA_WPS;
17f6b900
JM
141 wps = ieee802_11_vendor_ie_concat(ie, ielen,
142 WPS_IE_VENDOR_TYPE);
143 if (wps) {
144 if (wps_is_20(wps)) {
145 wpa_printf(MSG_DEBUG, "WPS: STA "
146 "supports WPS 2.0");
147 sta->flags |= WLAN_STA_WPS2;
148 }
149 wpabuf_free(wps);
150 }
b5b969e9
JM
151 goto skip_wpa_check;
152 }
633d4469 153#endif /* CONFIG_WPS */
b5b969e9
JM
154
155 if (sta->wpa_sm == NULL)
156 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
157 sta->addr);
158 if (sta->wpa_sm == NULL) {
159 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
160 "machine");
161 return -1;
162 }
163 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
88b32a99
SP
164 ie, ielen,
165 elems.mdie, elems.mdie_len);
b5b969e9
JM
166 if (res != WPA_IE_OK) {
167 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
168 "rejected? (res %u)", res);
169 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
88b32a99 170 if (res == WPA_INVALID_GROUP) {
08a74e6a 171 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
172 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
173 } else if (res == WPA_INVALID_PAIRWISE) {
08a74e6a 174 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
88b32a99
SP
175 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
176 } else if (res == WPA_INVALID_AKMP) {
08a74e6a 177 reason = WLAN_REASON_AKMP_NOT_VALID;
88b32a99
SP
178 status = WLAN_STATUS_AKMP_NOT_VALID;
179 }
355d36a7 180#ifdef CONFIG_IEEE80211W
88b32a99 181 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
08a74e6a 182 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
183 status = WLAN_STATUS_INVALID_IE;
184 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
08a74e6a 185 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
186 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
187 }
355d36a7 188#endif /* CONFIG_IEEE80211W */
88b32a99 189 else {
08a74e6a 190 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
191 status = WLAN_STATUS_INVALID_IE;
192 }
08a74e6a 193 goto fail;
b5b969e9 194 }
7d9c0cd3
MP
195#ifdef CONFIG_IEEE80211W
196 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
197 sta->sa_query_count > 0)
198 ap_check_sa_query_timeout(hapd, sta);
199 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
200 (sta->auth_alg != WLAN_AUTH_FT)) {
201 /*
202 * STA has already been associated with MFP and SA
203 * Query timeout has not been reached. Reject the
204 * association attempt temporarily and start SA Query,
205 * if one is not pending.
206 */
207
208 if (sta->sa_query_count == 0)
209 ap_sta_start_sa_query(hapd, sta);
210
211#ifdef CONFIG_IEEE80211R
212 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
213
214 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
215
216 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
217 p - buf);
218#endif /* CONFIG_IEEE80211R */
219 return 0;
220 }
221
222 if (wpa_auth_uses_mfp(sta->wpa_sm))
223 sta->flags |= WLAN_STA_MFP;
224 else
225 sta->flags &= ~WLAN_STA_MFP;
226#endif /* CONFIG_IEEE80211W */
227
88b32a99
SP
228#ifdef CONFIG_IEEE80211R
229 if (sta->auth_alg == WLAN_AUTH_FT) {
230 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
231 req_ies_len);
232 if (status != WLAN_STATUS_SUCCESS) {
233 if (status == WLAN_STATUS_INVALID_PMKID)
234 reason = WLAN_REASON_INVALID_IE;
235 if (status == WLAN_STATUS_INVALID_MDIE)
236 reason = WLAN_REASON_INVALID_IE;
237 if (status == WLAN_STATUS_INVALID_FTIE)
238 reason = WLAN_REASON_INVALID_IE;
239 goto fail;
240 }
241 }
242#endif /* CONFIG_IEEE80211R */
a9aca28b 243 } else if (hapd->conf->wps_state) {
633d4469 244#ifdef CONFIG_WPS
17f6b900 245 struct wpabuf *wps;
2bb20281
JM
246 if (req_ies)
247 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
fa15d405
JM
248 WPS_IE_VENDOR_TYPE);
249 else
250 wps = NULL;
54f489be 251#ifdef CONFIG_WPS_STRICT
fa15d405 252 if (wps && wps_validate_assoc_req(wps) < 0) {
08a74e6a 253 reason = WLAN_REASON_INVALID_IE;
88b32a99 254 status = WLAN_STATUS_INVALID_IE;
fa15d405 255 wpabuf_free(wps);
08a74e6a 256 goto fail;
54f489be 257 }
54f489be 258#endif /* CONFIG_WPS_STRICT */
fa15d405 259 if (wps) {
a9aca28b 260 sta->flags |= WLAN_STA_WPS;
fa15d405 261 if (wps_is_20(wps)) {
17f6b900
JM
262 wpa_printf(MSG_DEBUG, "WPS: STA supports "
263 "WPS 2.0");
264 sta->flags |= WLAN_STA_WPS2;
265 }
a9aca28b
JM
266 } else
267 sta->flags |= WLAN_STA_MAYBE_WPS;
17f6b900 268 wpabuf_free(wps);
633d4469 269#endif /* CONFIG_WPS */
b5b969e9 270 }
633d4469 271#ifdef CONFIG_WPS
b5b969e9 272skip_wpa_check:
633d4469 273#endif /* CONFIG_WPS */
b5b969e9 274
88b32a99
SP
275#ifdef CONFIG_IEEE80211R
276 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
277 sta->auth_alg, req_ies, req_ies_len);
278
279 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
280#else /* CONFIG_IEEE80211R */
281 /* Keep compiler silent about unused variables */
282 if (status) {
283 }
284#endif /* CONFIG_IEEE80211R */
285
b5b969e9
JM
286 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
287 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
88b32a99
SP
288
289 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
290 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
291 else
292 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
b5b969e9
JM
293
294 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
295
296 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
297
ef796391 298#ifdef CONFIG_P2P
99c01af9
JM
299 if (req_ies) {
300 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
301 req_ies, req_ies_len);
302 }
ef796391
JM
303#endif /* CONFIG_P2P */
304
b5b969e9 305 return 0;
08a74e6a
JM
306
307fail:
88b32a99
SP
308#ifdef CONFIG_IEEE80211R
309 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
310#endif /* CONFIG_IEEE80211R */
08a74e6a
JM
311 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
312 ap_free_sta(hapd, sta);
313 return -1;
b5b969e9
JM
314}
315
316
317void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
318{
319 struct sta_info *sta;
320
83e843e8
JM
321 if (addr == NULL) {
322 /*
323 * This could potentially happen with unexpected event from the
324 * driver wrapper. This was seen at least in one case where the
325 * driver ended up reporting a station mode event while hostapd
326 * was running, so better make sure we stop processing such an
327 * event here.
328 */
329 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
330 "with no address");
1f4c7b6b 331 return;
83e843e8
JM
332 }
333
b5b969e9
JM
334 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
335 HOSTAPD_LEVEL_INFO, "disassociated");
336
337 sta = ap_get_sta(hapd, addr);
338 if (sta == NULL) {
339 wpa_printf(MSG_DEBUG, "Disassociation notification for "
340 "unknown STA " MACSTR, MAC2STR(addr));
341 return;
342 }
343
ae055af4 344 ap_sta_set_authorized(hapd, sta, 0);
b5b969e9
JM
345 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
346 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
347 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
348 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
349 ap_free_sta(hapd, sta);
350}
351
352
0d7e5a3a
JB
353void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
354{
355 struct sta_info *sta = ap_get_sta(hapd, addr);
356
357 if (!sta || !hapd->conf->disassoc_low_ack)
358 return;
359
360 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
361 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
362 "missing ACKs");
363 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
364 if (sta)
365 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
366}
367
368
1b487b8b
TP
369void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
370 int offset)
371{
c7803a02 372#ifdef NEED_AP_MLME
1b487b8b
TP
373 int channel;
374
375 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
376 HOSTAPD_LEVEL_INFO, "driver had channel switch: "
377 "freq=%d, ht=%d, offset=%d", freq, ht, offset);
378
379 hapd->iface->freq = freq;
380
381 channel = hostapd_hw_get_channel(hapd, freq);
382 if (!channel) {
383 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
384 HOSTAPD_LEVEL_WARNING, "driver switched to "
385 "bad channel!");
386 return;
387 }
388
389 hapd->iconf->channel = channel;
390 hapd->iconf->ieee80211n = ht;
391 hapd->iconf->secondary_channel = offset;
c7803a02 392#endif /* NEED_AP_MLME */
1b487b8b
TP
393}
394
395
04a85e44 396int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
baf513d6
JB
397 const u8 *bssid, const u8 *ie, size_t ie_len,
398 int ssi_signal)
e67b55fb
JM
399{
400 size_t i;
401 int ret = 0;
402
b211f3eb
JM
403 if (sa == NULL || ie == NULL)
404 return -1;
405
406 random_add_randomness(sa, ETH_ALEN);
e67b55fb
JM
407 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
408 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
baf513d6
JB
409 sa, da, bssid, ie, ie_len,
410 ssi_signal) > 0) {
e67b55fb
JM
411 ret = 1;
412 break;
413 }
414 }
415 return ret;
416}
417
418
5c61a282
JM
419#ifdef HOSTAPD
420
88b32a99
SP
421#ifdef CONFIG_IEEE80211R
422static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
423 const u8 *bssid,
424 u16 auth_transaction, u16 status,
425 const u8 *ies, size_t ies_len)
426{
427 struct hostapd_data *hapd = ctx;
428 struct sta_info *sta;
429
430 sta = ap_get_sta(hapd, dst);
431 if (sta == NULL)
432 return;
433
434 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
435 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
436 sta->flags |= WLAN_STA_AUTH;
437
438 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
439}
440#endif /* CONFIG_IEEE80211R */
441
442
88b32a99
SP
443static void hostapd_notif_auth(struct hostapd_data *hapd,
444 struct auth_info *rx_auth)
445{
446 struct sta_info *sta;
447 u16 status = WLAN_STATUS_SUCCESS;
448 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
449 size_t resp_ies_len = 0;
450
451 sta = ap_get_sta(hapd, rx_auth->peer);
452 if (!sta) {
453 sta = ap_sta_add(hapd, rx_auth->peer);
454 if (sta == NULL) {
455 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
456 goto fail;
457 }
458 }
459 sta->flags &= ~WLAN_STA_PREAUTH;
460 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
461#ifdef CONFIG_IEEE80211R
462 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
463 sta->auth_alg = WLAN_AUTH_FT;
464 if (sta->wpa_sm == NULL)
465 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
466 sta->addr);
467 if (sta->wpa_sm == NULL) {
468 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
469 "state machine");
470 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
471 goto fail;
472 }
473 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
474 rx_auth->auth_transaction, rx_auth->ies,
475 rx_auth->ies_len,
476 hostapd_notify_auth_ft_finish, hapd);
477 return;
478 }
479#endif /* CONFIG_IEEE80211R */
480fail:
481 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
482 status, resp_ies, resp_ies_len);
483}
484
485
486static void hostapd_action_rx(struct hostapd_data *hapd,
487 struct rx_action *action)
488{
489 struct sta_info *sta;
490
7d9c0cd3
MP
491 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
492 action->category, (int) action->len);
493
88b32a99
SP
494 sta = ap_get_sta(hapd, action->sa);
495 if (sta == NULL) {
496 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
497 return;
498 }
499#ifdef CONFIG_IEEE80211R
500 if (action->category == WLAN_ACTION_FT) {
501 wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
502 __func__, (int) action->len);
503 wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
504 }
505#endif /* CONFIG_IEEE80211R */
7d9c0cd3
MP
506#ifdef CONFIG_IEEE80211W
507 if (action->category == WLAN_ACTION_SA_QUERY && action->len >= 4) {
508 wpa_printf(MSG_DEBUG, "%s: SA_QUERY_ACTION length %d",
509 __func__, (int) action->len);
510 ieee802_11_sa_query_action(hapd, action->sa,
511 *(action->data + 1),
512 action->data + 2);
513 }
514#endif /* CONFIG_IEEE80211W */
ad3872a3 515#ifdef CONFIG_WNM
d32d94db
XC
516 if (action->category == WLAN_ACTION_WNM) {
517 wpa_printf(MSG_DEBUG, "%s: WNM_ACTION length %d",
518 __func__, (int) action->len);
519 ieee802_11_rx_wnm_action_ap(hapd, action);
520 }
ad3872a3 521#endif /* CONFIG_WNM */
88b32a99
SP
522}
523
524
fe6bdb77 525#ifdef NEED_AP_MLME
f8b1f695 526
f8b1f695
JM
527#define HAPD_BROADCAST ((struct hostapd_data *) -1)
528
529static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
530 const u8 *bssid)
531{
532 size_t i;
533
534 if (bssid == NULL)
535 return NULL;
536 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
537 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
538 return HAPD_BROADCAST;
539
540 for (i = 0; i < iface->num_bss; i++) {
541 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
542 return iface->bss[i];
543 }
544
545 return NULL;
546}
547
548
549static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
9b90955e
JB
550 const u8 *bssid, const u8 *addr,
551 int wds)
f8b1f695 552{
9b90955e 553 hapd = get_hapd_bssid(hapd->iface, bssid);
f8b1f695
JM
554 if (hapd == NULL || hapd == HAPD_BROADCAST)
555 return;
556
9b90955e 557 ieee802_11_rx_from_unknown(hapd, addr, wds);
f8b1f695
JM
558}
559
560
2a8b7416 561static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 562{
4b9841d3 563 struct hostapd_iface *iface = hapd->iface;
b57e086c 564 const struct ieee80211_hdr *hdr;
4b9841d3 565 const u8 *bssid;
2a8b7416 566 struct hostapd_frame_info fi;
4b9841d3 567
2a8b7416
JM
568 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
569 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3
JM
570 if (bssid == NULL)
571 return;
572
573 hapd = get_hapd_bssid(iface, bssid);
574 if (hapd == NULL) {
575 u16 fc;
576 fc = le_to_host16(hdr->frame_control);
577
578 /*
579 * Drop frames to unknown BSSIDs except for Beacon frames which
580 * could be used to update neighbor information.
581 */
582 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
583 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
584 hapd = iface->bss[0];
585 else
586 return;
587 }
588
2a8b7416
JM
589 os_memset(&fi, 0, sizeof(fi));
590 fi.datarate = rx_mgmt->datarate;
591 fi.ssi_signal = rx_mgmt->ssi_signal;
592
4b9841d3
JM
593 if (hapd == HAPD_BROADCAST) {
594 size_t i;
595 for (i = 0; i < iface->num_bss; i++)
2a8b7416
JM
596 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
597 rx_mgmt->frame_len, &fi);
4b9841d3 598 } else
2a8b7416 599 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
bbb921da
JM
600
601 random_add_randomness(&fi, sizeof(fi));
b5b969e9
JM
602}
603
604
9e2704c3
JM
605static void hostapd_rx_action(struct hostapd_data *hapd,
606 struct rx_action *rx_action)
607{
608 struct rx_mgmt rx_mgmt;
609 u8 *buf;
610 struct ieee80211_hdr *hdr;
611
612 wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
613 " BSSID=" MACSTR " category=%u",
614 MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
615 MAC2STR(rx_action->bssid), rx_action->category);
616 wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
617 rx_action->data, rx_action->len);
618
619 buf = os_zalloc(24 + 1 + rx_action->len);
620 if (buf == NULL)
621 return;
622 hdr = (struct ieee80211_hdr *) buf;
623 hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
624 WLAN_FC_STYPE_ACTION);
625 if (rx_action->category == WLAN_ACTION_SA_QUERY) {
626 /*
627 * Assume frame was protected; it would have been dropped if
628 * not.
629 */
630 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
631 }
632 os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
633 os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
634 os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
635 buf[24] = rx_action->category;
636 os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
637 os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
638 rx_mgmt.frame = buf;
639 rx_mgmt.frame_len = 24 + 1 + rx_action->len;
640 hostapd_mgmt_rx(hapd, &rx_mgmt);
641 os_free(buf);
642}
643
644
f8b1f695
JM
645static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
646 size_t len, u16 stype, int ok)
b5b969e9 647{
4b9841d3
JM
648 struct ieee80211_hdr *hdr;
649 hdr = (struct ieee80211_hdr *) buf;
650 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
651 if (hapd == NULL || hapd == HAPD_BROADCAST)
652 return;
b5b969e9
JM
653 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
654}
f82ef4d8 655
f8b1f695 656#endif /* NEED_AP_MLME */
ad1e68e6
JM
657
658
a8e0505b
JM
659static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
660{
661 struct sta_info *sta = ap_get_sta(hapd, addr);
662 if (sta)
663 return 0;
664
665 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
666 " - adding a new STA", MAC2STR(addr));
667 sta = ap_sta_add(hapd, addr);
668 if (sta) {
669 hostapd_new_assoc_sta(hapd, sta, 0);
670 } else {
671 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
672 MAC2STR(addr));
673 return -1;
674 }
675
676 return 0;
677}
678
679
680static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
681 const u8 *data, size_t data_len)
682{
683 struct hostapd_iface *iface = hapd->iface;
f826635c 684 struct sta_info *sta;
a8e0505b
JM
685 size_t j;
686
687 for (j = 0; j < iface->num_bss; j++) {
f826635c
DB
688 if ((sta = ap_get_sta(iface->bss[j], src))) {
689 if (sta->flags & WLAN_STA_ASSOC) {
690 hapd = iface->bss[j];
691 break;
692 }
a8e0505b
JM
693 }
694 }
695
696 ieee802_1x_receive(hapd, src, data, data_len);
697}
698
699
9646a8ab 700void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
701 union wpa_event_data *data)
702{
703 struct hostapd_data *hapd = ctx;
74781dfc
JM
704#ifndef CONFIG_NO_STDOUT_DEBUG
705 int level = MSG_DEBUG;
ad1e68e6 706
34caf71a 707 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
74781dfc
JM
708 data->rx_mgmt.frame_len >= 24) {
709 const struct ieee80211_hdr *hdr;
710 u16 fc;
711 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
712 fc = le_to_host16(hdr->frame_control);
713 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
714 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
715 level = MSG_EXCESSIVE;
716 }
717
718 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
e26cd1a1 719 event_to_string(event), event);
74781dfc 720#endif /* CONFIG_NO_STDOUT_DEBUG */
e26cd1a1 721
ad1e68e6
JM
722 switch (event) {
723 case EVENT_MICHAEL_MIC_FAILURE:
724 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
725 break;
726 case EVENT_SCAN_RESULTS:
727 if (hapd->iface->scan_cb)
728 hapd->iface->scan_cb(hapd->iface);
729 break;
08fd8c15 730#ifdef CONFIG_IEEE80211R
f2dab64e 731 case EVENT_FT_RRB_RX:
08fd8c15
JM
732 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
733 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
734 break;
735#endif /* CONFIG_IEEE80211R */
fcf0f87d 736 case EVENT_WPS_BUTTON_PUSHED:
d601247c 737 hostapd_wps_button_pushed(hapd, NULL);
fcf0f87d 738 break;
f8b1f695
JM
739#ifdef NEED_AP_MLME
740 case EVENT_TX_STATUS:
741 switch (data->tx_status.type) {
742 case WLAN_FC_TYPE_MGMT:
743 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
744 data->tx_status.data_len,
745 data->tx_status.stype,
746 data->tx_status.ack);
747 break;
748 case WLAN_FC_TYPE_DATA:
749 hostapd_tx_status(hapd, data->tx_status.dst,
750 data->tx_status.data,
751 data->tx_status.data_len,
752 data->tx_status.ack);
753 break;
754 }
755 break;
dd840f79
JB
756 case EVENT_EAPOL_TX_STATUS:
757 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
758 data->eapol_tx_status.data,
759 data->eapol_tx_status.data_len,
760 data->eapol_tx_status.ack);
761 break;
bcf24348
JB
762 case EVENT_DRIVER_CLIENT_POLL_OK:
763 hostapd_client_poll_ok(hapd, data->client_poll.addr);
764 break;
f8b1f695 765 case EVENT_RX_FROM_UNKNOWN:
9b90955e
JB
766 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
767 data->rx_from_unknown.addr,
768 data->rx_from_unknown.wds);
f8b1f695
JM
769 break;
770 case EVENT_RX_MGMT:
2a8b7416 771 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
f8b1f695
JM
772 break;
773#endif /* NEED_AP_MLME */
a0e0d3bb 774 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
775 if (data->rx_probe_req.sa == NULL ||
776 data->rx_probe_req.ie == NULL)
777 break;
a0e0d3bb 778 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
04a85e44
JM
779 data->rx_probe_req.da,
780 data->rx_probe_req.bssid,
a0e0d3bb 781 data->rx_probe_req.ie,
baf513d6
JB
782 data->rx_probe_req.ie_len,
783 data->rx_probe_req.ssi_signal);
a0e0d3bb 784 break;
a70a5d6d 785 case EVENT_NEW_STA:
a8e0505b
JM
786 hostapd_event_new_sta(hapd, data->new_sta.addr);
787 break;
788 case EVENT_EAPOL_RX:
789 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
790 data->eapol_rx.data,
791 data->eapol_rx.data_len);
792 break;
1d041bec
JM
793 case EVENT_ASSOC:
794 hostapd_notif_assoc(hapd, data->assoc_info.addr,
795 data->assoc_info.req_ies,
39b08b5f
SP
796 data->assoc_info.req_ies_len,
797 data->assoc_info.reassoc);
1d041bec
JM
798 break;
799 case EVENT_DISASSOC:
800 if (data)
801 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
802 break;
803 case EVENT_DEAUTH:
804 if (data)
805 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
806 break;
0d7e5a3a
JB
807 case EVENT_STATION_LOW_ACK:
808 if (!data)
809 break;
810 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
811 break;
9e2704c3
JM
812 case EVENT_RX_ACTION:
813 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
814 data->rx_action.bssid == NULL)
815 break;
88b32a99 816#ifdef NEED_AP_MLME
9e2704c3 817 hostapd_rx_action(hapd, &data->rx_action);
7cc7307d 818#endif /* NEED_AP_MLME */
88b32a99
SP
819 hostapd_action_rx(hapd, &data->rx_action);
820 break;
821 case EVENT_AUTH:
822 hostapd_notif_auth(hapd, &data->auth);
823 break;
1b487b8b
TP
824 case EVENT_CH_SWITCH:
825 if (!data)
826 break;
827 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
828 data->ch_switch.ht_enabled,
829 data->ch_switch.ch_offset);
830 break;
ad1e68e6
JM
831 default:
832 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
833 break;
834 }
835}
f8b1f695
JM
836
837#endif /* HOSTAPD */