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