]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
Fix whitespace style
[thirdparty/hostap.git] / src / ap / drv_callbacks.c
CommitLineData
b5b969e9
JM
1/*
2 * hostapd / Callback functions for driver wrappers
94ddef3e 3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
b5b969e9 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"
3140803b 16#include "common/wpa_ctrl.h"
bbb921da 17#include "crypto/random.h"
ef796391 18#include "p2p/p2p.h"
54f489be 19#include "wps/wps.h"
d32d94db 20#include "wnm_ap.h"
6226e38d
JM
21#include "hostapd.h"
22#include "ieee802_11.h"
23#include "sta_info.h"
24#include "accounting.h"
25#include "tkip_countermeasures.h"
6226e38d
JM
26#include "ieee802_1x.h"
27#include "wpa_auth.h"
6226e38d 28#include "wps_hostapd.h"
51e2a27a 29#include "ap_drv_ops.h"
8b06c1ed 30#include "ap_config.h"
1b487b8b 31#include "hw_features.h"
e76da505 32#include "dfs.h"
b5b969e9
JM
33
34
b5b969e9 35int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
2bb20281 36 const u8 *req_ies, size_t req_ies_len, int reassoc)
b5b969e9
JM
37{
38 struct sta_info *sta;
39 int new_assoc, res;
c41a1095 40 struct ieee802_11_elems elems;
2bb20281
JM
41 const u8 *ie;
42 size_t ielen;
88b32a99
SP
43#ifdef CONFIG_IEEE80211R
44 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
45 u8 *p = buf;
46#endif /* CONFIG_IEEE80211R */
08a74e6a 47 u16 reason = WLAN_REASON_UNSPECIFIED;
88b32a99 48 u16 status = WLAN_STATUS_SUCCESS;
94ddef3e 49 const u8 *p2p_dev_addr = NULL;
b5b969e9 50
68532a9c
JM
51 if (addr == NULL) {
52 /*
53 * This could potentially happen with unexpected event from the
54 * driver wrapper. This was seen at least in one case where the
55 * driver ended up being set to station mode while hostapd was
56 * running, so better make sure we stop processing such an
57 * event here.
58 */
59 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
60 "no address");
61 return -1;
62 }
bbb921da 63 random_add_randomness(addr, ETH_ALEN);
68532a9c 64
b5b969e9
JM
65 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
66 HOSTAPD_LEVEL_INFO, "associated");
67
2bb20281 68 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
c41a1095
JM
69 if (elems.wps_ie) {
70 ie = elems.wps_ie - 2;
71 ielen = elems.wps_ie_len + 2;
72 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
73 } else if (elems.rsn_ie) {
74 ie = elems.rsn_ie - 2;
75 ielen = elems.rsn_ie_len + 2;
76 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
77 } else if (elems.wpa_ie) {
78 ie = elems.wpa_ie - 2;
79 ielen = elems.wpa_ie_len + 2;
80 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
81 } else {
82 ie = NULL;
83 ielen = 0;
84 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
85 "(Re)AssocReq");
86 }
87
b5b969e9
JM
88 sta = ap_get_sta(hapd, addr);
89 if (sta) {
4331263b 90 ap_sta_no_session_timeout(hapd, sta);
b5b969e9 91 accounting_sta_stop(hapd, sta);
c72bd6d4
JM
92
93 /*
94 * Make sure that the previously registered inactivity timer
95 * will not remove the STA immediately.
96 */
97 sta->timeout_next = STA_NULLFUNC;
b5b969e9
JM
98 } else {
99 sta = ap_sta_add(hapd, addr);
8bd0fc0e
JM
100 if (sta == NULL) {
101 hostapd_drv_sta_disassoc(hapd, addr,
102 WLAN_REASON_DISASSOC_AP_BUSY);
b5b969e9 103 return -1;
8bd0fc0e 104 }
b5b969e9 105 }
17f6b900 106 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
b5b969e9 107
b305c684
JM
108#ifdef CONFIG_P2P
109 if (elems.p2p) {
110 wpabuf_free(sta->p2p_ie);
2bb20281 111 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
b305c684 112 P2P_IE_VENDOR_TYPE);
94ddef3e
JM
113 if (sta->p2p_ie)
114 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
b305c684
JM
115 }
116#endif /* CONFIG_P2P */
117
c551700f
KP
118#ifdef CONFIG_INTERWORKING
119 if (elems.ext_capab && elems.ext_capab_len > 4) {
120 if (elems.ext_capab[4] & 0x01)
121 sta->qos_map_enabled = 1;
122 }
123#endif /* CONFIG_INTERWORKING */
124
f403dcd6
JM
125#ifdef CONFIG_HS20
126 wpabuf_free(sta->hs20_ie);
127 if (elems.hs20 && elems.hs20_len > 4) {
128 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
129 elems.hs20_len - 4);
130 } else
131 sta->hs20_ie = NULL;
132#endif /* CONFIG_HS20 */
133
b5b969e9
JM
134 if (hapd->conf->wpa) {
135 if (ie == NULL || ielen == 0) {
633d4469 136#ifdef CONFIG_WPS
b5b969e9
JM
137 if (hapd->conf->wps_state) {
138 wpa_printf(MSG_DEBUG, "STA did not include "
139 "WPA/RSN IE in (Re)Association "
140 "Request - possible WPS use");
141 sta->flags |= WLAN_STA_MAYBE_WPS;
142 goto skip_wpa_check;
143 }
633d4469 144#endif /* CONFIG_WPS */
b5b969e9
JM
145
146 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
147 return -1;
148 }
633d4469 149#ifdef CONFIG_WPS
b5b969e9
JM
150 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
151 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
17f6b900 152 struct wpabuf *wps;
b5b969e9 153 sta->flags |= WLAN_STA_WPS;
17f6b900
JM
154 wps = ieee802_11_vendor_ie_concat(ie, ielen,
155 WPS_IE_VENDOR_TYPE);
156 if (wps) {
157 if (wps_is_20(wps)) {
158 wpa_printf(MSG_DEBUG, "WPS: STA "
159 "supports WPS 2.0");
160 sta->flags |= WLAN_STA_WPS2;
161 }
162 wpabuf_free(wps);
163 }
b5b969e9
JM
164 goto skip_wpa_check;
165 }
633d4469 166#endif /* CONFIG_WPS */
b5b969e9
JM
167
168 if (sta->wpa_sm == NULL)
169 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e
JM
170 sta->addr,
171 p2p_dev_addr);
b5b969e9
JM
172 if (sta->wpa_sm == NULL) {
173 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
174 "machine");
175 return -1;
176 }
177 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
88b32a99
SP
178 ie, ielen,
179 elems.mdie, elems.mdie_len);
b5b969e9
JM
180 if (res != WPA_IE_OK) {
181 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
182 "rejected? (res %u)", res);
183 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
88b32a99 184 if (res == WPA_INVALID_GROUP) {
08a74e6a 185 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
186 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
187 } else if (res == WPA_INVALID_PAIRWISE) {
08a74e6a 188 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
88b32a99
SP
189 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
190 } else if (res == WPA_INVALID_AKMP) {
08a74e6a 191 reason = WLAN_REASON_AKMP_NOT_VALID;
88b32a99
SP
192 status = WLAN_STATUS_AKMP_NOT_VALID;
193 }
355d36a7 194#ifdef CONFIG_IEEE80211W
88b32a99 195 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
08a74e6a 196 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
197 status = WLAN_STATUS_INVALID_IE;
198 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
08a74e6a 199 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
200 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
201 }
355d36a7 202#endif /* CONFIG_IEEE80211W */
88b32a99 203 else {
08a74e6a 204 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
205 status = WLAN_STATUS_INVALID_IE;
206 }
08a74e6a 207 goto fail;
b5b969e9 208 }
7d9c0cd3
MP
209#ifdef CONFIG_IEEE80211W
210 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
211 sta->sa_query_count > 0)
212 ap_check_sa_query_timeout(hapd, sta);
213 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
214 (sta->auth_alg != WLAN_AUTH_FT)) {
215 /*
216 * STA has already been associated with MFP and SA
217 * Query timeout has not been reached. Reject the
218 * association attempt temporarily and start SA Query,
219 * if one is not pending.
220 */
221
222 if (sta->sa_query_count == 0)
223 ap_sta_start_sa_query(hapd, sta);
224
225#ifdef CONFIG_IEEE80211R
226 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
227
228 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
229
230 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
231 p - buf);
232#endif /* CONFIG_IEEE80211R */
233 return 0;
234 }
235
236 if (wpa_auth_uses_mfp(sta->wpa_sm))
237 sta->flags |= WLAN_STA_MFP;
238 else
239 sta->flags &= ~WLAN_STA_MFP;
240#endif /* CONFIG_IEEE80211W */
241
88b32a99
SP
242#ifdef CONFIG_IEEE80211R
243 if (sta->auth_alg == WLAN_AUTH_FT) {
244 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
245 req_ies_len);
246 if (status != WLAN_STATUS_SUCCESS) {
247 if (status == WLAN_STATUS_INVALID_PMKID)
248 reason = WLAN_REASON_INVALID_IE;
249 if (status == WLAN_STATUS_INVALID_MDIE)
250 reason = WLAN_REASON_INVALID_IE;
251 if (status == WLAN_STATUS_INVALID_FTIE)
252 reason = WLAN_REASON_INVALID_IE;
253 goto fail;
254 }
255 }
256#endif /* CONFIG_IEEE80211R */
a9aca28b 257 } else if (hapd->conf->wps_state) {
633d4469 258#ifdef CONFIG_WPS
17f6b900 259 struct wpabuf *wps;
2bb20281
JM
260 if (req_ies)
261 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
fa15d405
JM
262 WPS_IE_VENDOR_TYPE);
263 else
264 wps = NULL;
54f489be 265#ifdef CONFIG_WPS_STRICT
fa15d405 266 if (wps && wps_validate_assoc_req(wps) < 0) {
08a74e6a 267 reason = WLAN_REASON_INVALID_IE;
88b32a99 268 status = WLAN_STATUS_INVALID_IE;
fa15d405 269 wpabuf_free(wps);
08a74e6a 270 goto fail;
54f489be 271 }
54f489be 272#endif /* CONFIG_WPS_STRICT */
fa15d405 273 if (wps) {
a9aca28b 274 sta->flags |= WLAN_STA_WPS;
fa15d405 275 if (wps_is_20(wps)) {
17f6b900
JM
276 wpa_printf(MSG_DEBUG, "WPS: STA supports "
277 "WPS 2.0");
278 sta->flags |= WLAN_STA_WPS2;
279 }
a9aca28b
JM
280 } else
281 sta->flags |= WLAN_STA_MAYBE_WPS;
17f6b900 282 wpabuf_free(wps);
633d4469 283#endif /* CONFIG_WPS */
b5b969e9 284 }
633d4469 285#ifdef CONFIG_WPS
b5b969e9 286skip_wpa_check:
633d4469 287#endif /* CONFIG_WPS */
b5b969e9 288
88b32a99
SP
289#ifdef CONFIG_IEEE80211R
290 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
291 sta->auth_alg, req_ies, req_ies_len);
292
293 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
294#else /* CONFIG_IEEE80211R */
295 /* Keep compiler silent about unused variables */
296 if (status) {
297 }
298#endif /* CONFIG_IEEE80211R */
299
b5b969e9
JM
300 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
301 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
3578e665 302 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
88b32a99
SP
303
304 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
305 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
306 else
307 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
b5b969e9
JM
308
309 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
310
311 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
312
ef796391 313#ifdef CONFIG_P2P
99c01af9
JM
314 if (req_ies) {
315 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
316 req_ies, req_ies_len);
317 }
ef796391
JM
318#endif /* CONFIG_P2P */
319
b5b969e9 320 return 0;
08a74e6a
JM
321
322fail:
88b32a99
SP
323#ifdef CONFIG_IEEE80211R
324 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
325#endif /* CONFIG_IEEE80211R */
08a74e6a
JM
326 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
327 ap_free_sta(hapd, sta);
328 return -1;
b5b969e9
JM
329}
330
331
332void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
333{
334 struct sta_info *sta;
335
83e843e8
JM
336 if (addr == NULL) {
337 /*
338 * This could potentially happen with unexpected event from the
339 * driver wrapper. This was seen at least in one case where the
340 * driver ended up reporting a station mode event while hostapd
341 * was running, so better make sure we stop processing such an
342 * event here.
343 */
344 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
345 "with no address");
1f4c7b6b 346 return;
83e843e8
JM
347 }
348
b5b969e9
JM
349 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
350 HOSTAPD_LEVEL_INFO, "disassociated");
351
352 sta = ap_get_sta(hapd, addr);
353 if (sta == NULL) {
354 wpa_printf(MSG_DEBUG, "Disassociation notification for "
355 "unknown STA " MACSTR, MAC2STR(addr));
356 return;
357 }
358
ae055af4 359 ap_sta_set_authorized(hapd, sta, 0);
b5b969e9
JM
360 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
361 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
362 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
363 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
364 ap_free_sta(hapd, sta);
365}
366
367
0d7e5a3a
JB
368void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
369{
370 struct sta_info *sta = ap_get_sta(hapd, addr);
371
372 if (!sta || !hapd->conf->disassoc_low_ack)
373 return;
374
375 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
376 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
377 "missing ACKs");
378 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
379 if (sta)
380 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
381}
382
383
1b487b8b 384void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
8d1fdde7 385 int offset, int width, int cf1, int cf2)
1b487b8b 386{
c7803a02 387#ifdef NEED_AP_MLME
8d1fdde7 388 int channel, chwidth, seg0_idx = 0, seg1_idx = 0;
1b487b8b
TP
389
390 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
391 HOSTAPD_LEVEL_INFO, "driver had channel switch: "
8d1fdde7
JD
392 "freq=%d, ht=%d, offset=%d, width=%d, cf1=%d, cf2=%d",
393 freq, ht, offset, width, cf1, cf2);
1b487b8b
TP
394
395 hapd->iface->freq = freq;
396
397 channel = hostapd_hw_get_channel(hapd, freq);
398 if (!channel) {
399 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
400 HOSTAPD_LEVEL_WARNING, "driver switched to "
401 "bad channel!");
402 return;
403 }
404
8d1fdde7
JD
405 switch (width) {
406 case CHAN_WIDTH_80:
407 chwidth = VHT_CHANWIDTH_80MHZ;
408 break;
409 case CHAN_WIDTH_80P80:
410 chwidth = VHT_CHANWIDTH_80P80MHZ;
411 break;
412 case CHAN_WIDTH_160:
413 chwidth = VHT_CHANWIDTH_160MHZ;
414 break;
415 case CHAN_WIDTH_20_NOHT:
416 case CHAN_WIDTH_20:
417 case CHAN_WIDTH_40:
418 default:
419 chwidth = VHT_CHANWIDTH_USE_HT;
420 break;
421 }
422
423 switch (hapd->iface->current_mode->mode) {
424 case HOSTAPD_MODE_IEEE80211A:
425 if (cf1 > 5000)
426 seg0_idx = (cf1 - 5000) / 5;
427 if (cf2 > 5000)
428 seg1_idx = (cf2 - 5000) / 5;
429 break;
430 default:
431 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
432 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
433 break;
434 }
435
1b487b8b
TP
436 hapd->iconf->channel = channel;
437 hapd->iconf->ieee80211n = ht;
438 hapd->iconf->secondary_channel = offset;
8d1fdde7
JD
439 hapd->iconf->vht_oper_chwidth = chwidth;
440 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
441 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
bf281c12 442
8f4713c5
AO
443 if (hapd->iface->csa_in_progress &&
444 freq == hapd->iface->cs_freq_params.freq) {
bf281c12
AO
445 hostapd_cleanup_cs_params(hapd);
446
447 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED "freq=%d",
448 freq);
449 }
c7803a02 450#endif /* NEED_AP_MLME */
1b487b8b
TP
451}
452
453
3140803b
RM
454void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
455 const u8 *addr, int reason_code)
456{
457 switch (reason_code) {
458 case MAX_CLIENT_REACHED:
459 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
460 MAC2STR(addr));
461 break;
462 case BLOCKED_CLIENT:
463 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
464 MAC2STR(addr));
465 break;
466 }
467}
468
469
04a85e44 470int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
baf513d6
JB
471 const u8 *bssid, const u8 *ie, size_t ie_len,
472 int ssi_signal)
e67b55fb
JM
473{
474 size_t i;
475 int ret = 0;
476
b211f3eb
JM
477 if (sa == NULL || ie == NULL)
478 return -1;
479
480 random_add_randomness(sa, ETH_ALEN);
e67b55fb
JM
481 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
482 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
baf513d6
JB
483 sa, da, bssid, ie, ie_len,
484 ssi_signal) > 0) {
e67b55fb
JM
485 ret = 1;
486 break;
487 }
488 }
489 return ret;
490}
491
492
5c61a282
JM
493#ifdef HOSTAPD
494
88b32a99
SP
495#ifdef CONFIG_IEEE80211R
496static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
497 const u8 *bssid,
498 u16 auth_transaction, u16 status,
499 const u8 *ies, size_t ies_len)
500{
501 struct hostapd_data *hapd = ctx;
502 struct sta_info *sta;
503
504 sta = ap_get_sta(hapd, dst);
505 if (sta == NULL)
506 return;
507
508 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
509 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
510 sta->flags |= WLAN_STA_AUTH;
511
512 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
513}
514#endif /* CONFIG_IEEE80211R */
515
516
88b32a99
SP
517static void hostapd_notif_auth(struct hostapd_data *hapd,
518 struct auth_info *rx_auth)
519{
520 struct sta_info *sta;
521 u16 status = WLAN_STATUS_SUCCESS;
522 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
523 size_t resp_ies_len = 0;
524
525 sta = ap_get_sta(hapd, rx_auth->peer);
526 if (!sta) {
527 sta = ap_sta_add(hapd, rx_auth->peer);
528 if (sta == NULL) {
728d9717 529 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
88b32a99
SP
530 goto fail;
531 }
532 }
533 sta->flags &= ~WLAN_STA_PREAUTH;
534 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
535#ifdef CONFIG_IEEE80211R
536 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
537 sta->auth_alg = WLAN_AUTH_FT;
538 if (sta->wpa_sm == NULL)
539 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 540 sta->addr, NULL);
88b32a99
SP
541 if (sta->wpa_sm == NULL) {
542 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
543 "state machine");
544 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
545 goto fail;
546 }
547 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
548 rx_auth->auth_transaction, rx_auth->ies,
549 rx_auth->ies_len,
550 hostapd_notify_auth_ft_finish, hapd);
551 return;
552 }
553#endif /* CONFIG_IEEE80211R */
554fail:
555 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
556 status, resp_ies, resp_ies_len);
557}
558
559
560static void hostapd_action_rx(struct hostapd_data *hapd,
561 struct rx_action *action)
562{
563 struct sta_info *sta;
564
7d9c0cd3
MP
565 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
566 action->category, (int) action->len);
567
88b32a99
SP
568 sta = ap_get_sta(hapd, action->sa);
569 if (sta == NULL) {
570 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
571 return;
572 }
573#ifdef CONFIG_IEEE80211R
574 if (action->category == WLAN_ACTION_FT) {
575 wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
576 __func__, (int) action->len);
577 wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
578 }
579#endif /* CONFIG_IEEE80211R */
7d9c0cd3
MP
580#ifdef CONFIG_IEEE80211W
581 if (action->category == WLAN_ACTION_SA_QUERY && action->len >= 4) {
582 wpa_printf(MSG_DEBUG, "%s: SA_QUERY_ACTION length %d",
583 __func__, (int) action->len);
584 ieee802_11_sa_query_action(hapd, action->sa,
585 *(action->data + 1),
586 action->data + 2);
587 }
588#endif /* CONFIG_IEEE80211W */
ad3872a3 589#ifdef CONFIG_WNM
d32d94db
XC
590 if (action->category == WLAN_ACTION_WNM) {
591 wpa_printf(MSG_DEBUG, "%s: WNM_ACTION length %d",
592 __func__, (int) action->len);
593 ieee802_11_rx_wnm_action_ap(hapd, action);
594 }
ad3872a3 595#endif /* CONFIG_WNM */
88b32a99
SP
596}
597
598
fe6bdb77 599#ifdef NEED_AP_MLME
f8b1f695 600
f8b1f695
JM
601#define HAPD_BROADCAST ((struct hostapd_data *) -1)
602
603static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
604 const u8 *bssid)
605{
606 size_t i;
607
608 if (bssid == NULL)
609 return NULL;
610 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
611 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
612 return HAPD_BROADCAST;
613
614 for (i = 0; i < iface->num_bss; i++) {
615 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
616 return iface->bss[i];
617 }
618
619 return NULL;
620}
621
622
623static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
9b90955e
JB
624 const u8 *bssid, const u8 *addr,
625 int wds)
f8b1f695 626{
9b90955e 627 hapd = get_hapd_bssid(hapd->iface, bssid);
f8b1f695
JM
628 if (hapd == NULL || hapd == HAPD_BROADCAST)
629 return;
630
9b90955e 631 ieee802_11_rx_from_unknown(hapd, addr, wds);
f8b1f695
JM
632}
633
634
2a8b7416 635static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 636{
4b9841d3 637 struct hostapd_iface *iface = hapd->iface;
b57e086c 638 const struct ieee80211_hdr *hdr;
4b9841d3 639 const u8 *bssid;
2a8b7416 640 struct hostapd_frame_info fi;
4b9841d3 641
2a8b7416
JM
642 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
643 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3
JM
644 if (bssid == NULL)
645 return;
646
647 hapd = get_hapd_bssid(iface, bssid);
648 if (hapd == NULL) {
649 u16 fc;
650 fc = le_to_host16(hdr->frame_control);
651
652 /*
653 * Drop frames to unknown BSSIDs except for Beacon frames which
654 * could be used to update neighbor information.
655 */
656 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
657 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
658 hapd = iface->bss[0];
659 else
660 return;
661 }
662
2a8b7416
JM
663 os_memset(&fi, 0, sizeof(fi));
664 fi.datarate = rx_mgmt->datarate;
665 fi.ssi_signal = rx_mgmt->ssi_signal;
666
4b9841d3
JM
667 if (hapd == HAPD_BROADCAST) {
668 size_t i;
669 for (i = 0; i < iface->num_bss; i++)
2a8b7416
JM
670 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
671 rx_mgmt->frame_len, &fi);
4b9841d3 672 } else
2a8b7416 673 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
bbb921da
JM
674
675 random_add_randomness(&fi, sizeof(fi));
b5b969e9
JM
676}
677
678
9e2704c3
JM
679static void hostapd_rx_action(struct hostapd_data *hapd,
680 struct rx_action *rx_action)
681{
682 struct rx_mgmt rx_mgmt;
683 u8 *buf;
684 struct ieee80211_hdr *hdr;
685
686 wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
687 " BSSID=" MACSTR " category=%u",
688 MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
689 MAC2STR(rx_action->bssid), rx_action->category);
690 wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
691 rx_action->data, rx_action->len);
692
693 buf = os_zalloc(24 + 1 + rx_action->len);
694 if (buf == NULL)
695 return;
696 hdr = (struct ieee80211_hdr *) buf;
697 hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
698 WLAN_FC_STYPE_ACTION);
2d2ecf51
JM
699 if (rx_action->protected == 1)
700 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
701 else if (rx_action->category == WLAN_ACTION_SA_QUERY) {
9e2704c3
JM
702 /*
703 * Assume frame was protected; it would have been dropped if
704 * not.
705 */
706 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
707 }
708 os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
709 os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
710 os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
711 buf[24] = rx_action->category;
712 os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
713 os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
714 rx_mgmt.frame = buf;
715 rx_mgmt.frame_len = 24 + 1 + rx_action->len;
716 hostapd_mgmt_rx(hapd, &rx_mgmt);
717 os_free(buf);
718}
719
720
f8b1f695
JM
721static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
722 size_t len, u16 stype, int ok)
b5b969e9 723{
4b9841d3
JM
724 struct ieee80211_hdr *hdr;
725 hdr = (struct ieee80211_hdr *) buf;
726 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
727 if (hapd == NULL || hapd == HAPD_BROADCAST)
728 return;
b5b969e9
JM
729 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
730}
f82ef4d8 731
f8b1f695 732#endif /* NEED_AP_MLME */
ad1e68e6
JM
733
734
a8e0505b
JM
735static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
736{
737 struct sta_info *sta = ap_get_sta(hapd, addr);
738 if (sta)
739 return 0;
740
741 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
742 " - adding a new STA", MAC2STR(addr));
743 sta = ap_sta_add(hapd, addr);
744 if (sta) {
745 hostapd_new_assoc_sta(hapd, sta, 0);
746 } else {
747 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
748 MAC2STR(addr));
749 return -1;
750 }
751
752 return 0;
753}
754
755
756static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
757 const u8 *data, size_t data_len)
758{
759 struct hostapd_iface *iface = hapd->iface;
f826635c 760 struct sta_info *sta;
a8e0505b
JM
761 size_t j;
762
763 for (j = 0; j < iface->num_bss; j++) {
f826635c
DB
764 if ((sta = ap_get_sta(iface->bss[j], src))) {
765 if (sta->flags & WLAN_STA_ASSOC) {
766 hapd = iface->bss[j];
767 break;
768 }
a8e0505b
JM
769 }
770 }
771
772 ieee802_1x_receive(hapd, src, data, data_len);
773}
774
775
0185007c
MK
776static struct hostapd_channel_data * hostapd_get_mode_channel(
777 struct hostapd_iface *iface, unsigned int freq)
778{
779 int i;
780 struct hostapd_channel_data *chan;
781
782 for (i = 0; i < iface->current_mode->num_channels; i++) {
783 chan = &iface->current_mode->channels[i];
784 if (!chan)
785 return NULL;
786 if ((unsigned int) chan->freq == freq)
787 return chan;
788 }
789
790 return NULL;
791}
792
793
794static void hostapd_update_nf(struct hostapd_iface *iface,
795 struct hostapd_channel_data *chan,
796 struct freq_survey *survey)
797{
798 if (!iface->chans_surveyed) {
799 chan->min_nf = survey->nf;
800 iface->lowest_nf = survey->nf;
801 } else {
802 if (dl_list_empty(&chan->survey_list))
803 chan->min_nf = survey->nf;
804 else if (survey->nf < chan->min_nf)
805 chan->min_nf = survey->nf;
806 if (survey->nf < iface->lowest_nf)
807 iface->lowest_nf = survey->nf;
808 }
809}
810
811
812static void hostapd_event_get_survey(struct hostapd_data *hapd,
813 struct survey_results *survey_results)
814{
815 struct hostapd_iface *iface = hapd->iface;
816 struct freq_survey *survey, *tmp;
817 struct hostapd_channel_data *chan;
818
819 if (dl_list_empty(&survey_results->survey_list)) {
820 wpa_printf(MSG_DEBUG, "No survey data received");
821 return;
822 }
823
824 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
825 struct freq_survey, list) {
826 chan = hostapd_get_mode_channel(iface, survey->freq);
827 if (!chan)
828 continue;
829 if (chan->flag & HOSTAPD_CHAN_DISABLED)
830 continue;
831
832 dl_list_del(&survey->list);
833 dl_list_add_tail(&chan->survey_list, &survey->list);
834
835 hostapd_update_nf(iface, chan, survey);
836
837 iface->chans_surveyed++;
838 }
839}
840
841
e76da505
JD
842#ifdef NEED_AP_MLME
843
844static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
845 struct dfs_event *radar)
846{
e76da505 847 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
dc036d9e 848 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
849 radar->chan_offset, radar->chan_width,
850 radar->cf1, radar->cf2);
e76da505
JD
851}
852
853
854static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
855 struct dfs_event *radar)
856{
857 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
dc036d9e 858 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
58b73e3d
JD
859 radar->chan_offset, radar->chan_width,
860 radar->cf1, radar->cf2);
e76da505
JD
861}
862
863
864static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
865 struct dfs_event *radar)
866{
867 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
dc036d9e 868 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
58b73e3d
JD
869 radar->chan_offset, radar->chan_width,
870 radar->cf1, radar->cf2);
e76da505
JD
871}
872
873
874static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
875 struct dfs_event *radar)
876{
877 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
dc036d9e 878 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
879 radar->chan_offset, radar->chan_width,
880 radar->cf1, radar->cf2);
e76da505
JD
881}
882
883#endif /* NEED_AP_MLME */
884
885
9646a8ab 886void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
887 union wpa_event_data *data)
888{
889 struct hostapd_data *hapd = ctx;
74781dfc
JM
890#ifndef CONFIG_NO_STDOUT_DEBUG
891 int level = MSG_DEBUG;
ad1e68e6 892
34caf71a 893 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
74781dfc
JM
894 data->rx_mgmt.frame_len >= 24) {
895 const struct ieee80211_hdr *hdr;
896 u16 fc;
897 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
898 fc = le_to_host16(hdr->frame_control);
899 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
900 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
901 level = MSG_EXCESSIVE;
cc2ada86
JM
902 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
903 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
904 level = MSG_EXCESSIVE;
74781dfc
JM
905 }
906
907 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
e26cd1a1 908 event_to_string(event), event);
74781dfc 909#endif /* CONFIG_NO_STDOUT_DEBUG */
e26cd1a1 910
ad1e68e6
JM
911 switch (event) {
912 case EVENT_MICHAEL_MIC_FAILURE:
913 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
914 break;
915 case EVENT_SCAN_RESULTS:
916 if (hapd->iface->scan_cb)
917 hapd->iface->scan_cb(hapd->iface);
918 break;
08fd8c15 919#ifdef CONFIG_IEEE80211R
f2dab64e 920 case EVENT_FT_RRB_RX:
08fd8c15
JM
921 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
922 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
923 break;
924#endif /* CONFIG_IEEE80211R */
fcf0f87d 925 case EVENT_WPS_BUTTON_PUSHED:
d601247c 926 hostapd_wps_button_pushed(hapd, NULL);
fcf0f87d 927 break;
f8b1f695
JM
928#ifdef NEED_AP_MLME
929 case EVENT_TX_STATUS:
930 switch (data->tx_status.type) {
931 case WLAN_FC_TYPE_MGMT:
932 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
933 data->tx_status.data_len,
934 data->tx_status.stype,
935 data->tx_status.ack);
936 break;
937 case WLAN_FC_TYPE_DATA:
938 hostapd_tx_status(hapd, data->tx_status.dst,
939 data->tx_status.data,
940 data->tx_status.data_len,
941 data->tx_status.ack);
942 break;
943 }
944 break;
dd840f79
JB
945 case EVENT_EAPOL_TX_STATUS:
946 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
947 data->eapol_tx_status.data,
948 data->eapol_tx_status.data_len,
949 data->eapol_tx_status.ack);
950 break;
bcf24348
JB
951 case EVENT_DRIVER_CLIENT_POLL_OK:
952 hostapd_client_poll_ok(hapd, data->client_poll.addr);
953 break;
f8b1f695 954 case EVENT_RX_FROM_UNKNOWN:
9b90955e
JB
955 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
956 data->rx_from_unknown.addr,
957 data->rx_from_unknown.wds);
f8b1f695
JM
958 break;
959 case EVENT_RX_MGMT:
2a8b7416 960 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
f8b1f695
JM
961 break;
962#endif /* NEED_AP_MLME */
a0e0d3bb 963 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
964 if (data->rx_probe_req.sa == NULL ||
965 data->rx_probe_req.ie == NULL)
966 break;
a0e0d3bb 967 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
04a85e44
JM
968 data->rx_probe_req.da,
969 data->rx_probe_req.bssid,
a0e0d3bb 970 data->rx_probe_req.ie,
baf513d6
JB
971 data->rx_probe_req.ie_len,
972 data->rx_probe_req.ssi_signal);
a0e0d3bb 973 break;
a70a5d6d 974 case EVENT_NEW_STA:
a8e0505b
JM
975 hostapd_event_new_sta(hapd, data->new_sta.addr);
976 break;
977 case EVENT_EAPOL_RX:
978 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
979 data->eapol_rx.data,
980 data->eapol_rx.data_len);
981 break;
1d041bec
JM
982 case EVENT_ASSOC:
983 hostapd_notif_assoc(hapd, data->assoc_info.addr,
984 data->assoc_info.req_ies,
39b08b5f
SP
985 data->assoc_info.req_ies_len,
986 data->assoc_info.reassoc);
1d041bec
JM
987 break;
988 case EVENT_DISASSOC:
989 if (data)
990 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
991 break;
992 case EVENT_DEAUTH:
993 if (data)
994 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
995 break;
0d7e5a3a
JB
996 case EVENT_STATION_LOW_ACK:
997 if (!data)
998 break;
999 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1000 break;
9e2704c3
JM
1001 case EVENT_RX_ACTION:
1002 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
1003 data->rx_action.bssid == NULL)
1004 break;
88b32a99 1005#ifdef NEED_AP_MLME
9e2704c3 1006 hostapd_rx_action(hapd, &data->rx_action);
7cc7307d 1007#endif /* NEED_AP_MLME */
88b32a99
SP
1008 hostapd_action_rx(hapd, &data->rx_action);
1009 break;
1010 case EVENT_AUTH:
1011 hostapd_notif_auth(hapd, &data->auth);
1012 break;
1b487b8b
TP
1013 case EVENT_CH_SWITCH:
1014 if (!data)
1015 break;
1016 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1017 data->ch_switch.ht_enabled,
8d1fdde7
JD
1018 data->ch_switch.ch_offset,
1019 data->ch_switch.ch_width,
1020 data->ch_switch.cf1,
1021 data->ch_switch.cf2);
1b487b8b 1022 break;
3140803b
RM
1023 case EVENT_CONNECT_FAILED_REASON:
1024 if (!data)
1025 break;
1026 hostapd_event_connect_failed_reason(
1027 hapd, data->connect_failed_reason.addr,
1028 data->connect_failed_reason.code);
1029 break;
0185007c
MK
1030 case EVENT_SURVEY:
1031 hostapd_event_get_survey(hapd, &data->survey_results);
1032 break;
e76da505
JD
1033#ifdef NEED_AP_MLME
1034 case EVENT_DFS_RADAR_DETECTED:
1035 if (!data)
1036 break;
1037 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1038 break;
1039 case EVENT_DFS_CAC_FINISHED:
1040 if (!data)
1041 break;
1042 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1043 break;
1044 case EVENT_DFS_CAC_ABORTED:
1045 if (!data)
1046 break;
1047 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1048 break;
1049 case EVENT_DFS_NOP_FINISHED:
1050 if (!data)
1051 break;
1052 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1053 break;
1054 case EVENT_CHANNEL_LIST_CHANGED:
1055 /* channel list changed (regulatory?), update channel list */
1056 /* TODO: check this. hostapd_get_hw_features() initializes
1057 * too much stuff. */
1058 /* hostapd_get_hw_features(hapd->iface); */
795baf77
AS
1059 hostapd_channel_list_updated(
1060 hapd->iface, data->channel_list_changed.initiator);
e76da505
JD
1061 break;
1062#endif /* NEED_AP_MLME */
ad1e68e6
JM
1063 default:
1064 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1065 break;
1066 }
1067}
f8b1f695
JM
1068
1069#endif /* HOSTAPD */