drv_callbacks.c revision e0e48dc666fb14a7bb60264ca87463ba7bc1fe0b
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	} 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
88	sta = ap_get_sta(hapd, addr);
89	if (sta) {
90		ap_sta_no_session_timeout(hapd, sta);
91		accounting_sta_stop(hapd, sta);
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;
98	} else {
99		sta = ap_sta_add(hapd, addr);
100		if (sta == NULL) {
101			hostapd_drv_sta_disassoc(hapd, addr,
102						 WLAN_REASON_DISASSOC_AP_BUSY);
103			return -1;
104		}
105	}
106	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
107
108#ifdef CONFIG_P2P
109	if (elems.p2p) {
110		wpabuf_free(sta->p2p_ie);
111		sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
112							  P2P_IE_VENDOR_TYPE);
113		if (sta->p2p_ie)
114			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
115	}
116#endif /* CONFIG_P2P */
117
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
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
134	if (hapd->conf->wpa) {
135		if (ie == NULL || ielen == 0) {
136#ifdef CONFIG_WPS
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			}
144#endif /* CONFIG_WPS */
145
146			wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
147			return -1;
148		}
149#ifdef CONFIG_WPS
150		if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
151		    os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
152			struct wpabuf *wps;
153			sta->flags |= WLAN_STA_WPS;
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			}
164			goto skip_wpa_check;
165		}
166#endif /* CONFIG_WPS */
167
168		if (sta->wpa_sm == NULL)
169			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
170							sta->addr,
171							p2p_dev_addr);
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,
178					  ie, ielen,
179					  elems.mdie, elems.mdie_len);
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);
184			if (res == WPA_INVALID_GROUP) {
185				reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
186				status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
187			} else if (res == WPA_INVALID_PAIRWISE) {
188				reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
189				status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
190			} else if (res == WPA_INVALID_AKMP) {
191				reason = WLAN_REASON_AKMP_NOT_VALID;
192				status = WLAN_STATUS_AKMP_NOT_VALID;
193			}
194#ifdef CONFIG_IEEE80211W
195			else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
196				reason = WLAN_REASON_INVALID_IE;
197				status = WLAN_STATUS_INVALID_IE;
198			} else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
199				reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
200				status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
201			}
202#endif /* CONFIG_IEEE80211W */
203			else {
204				reason = WLAN_REASON_INVALID_IE;
205				status = WLAN_STATUS_INVALID_IE;
206			}
207			goto fail;
208		}
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
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 */
257	} else if (hapd->conf->wps_state) {
258#ifdef CONFIG_WPS
259		struct wpabuf *wps;
260		if (req_ies)
261			wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
262							  WPS_IE_VENDOR_TYPE);
263		else
264			wps = NULL;
265#ifdef CONFIG_WPS_STRICT
266		if (wps && wps_validate_assoc_req(wps) < 0) {
267			reason = WLAN_REASON_INVALID_IE;
268			status = WLAN_STATUS_INVALID_IE;
269			wpabuf_free(wps);
270			goto fail;
271		}
272#endif /* CONFIG_WPS_STRICT */
273		if (wps) {
274			sta->flags |= WLAN_STA_WPS;
275			if (wps_is_20(wps)) {
276				wpa_printf(MSG_DEBUG, "WPS: STA supports "
277					   "WPS 2.0");
278				sta->flags |= WLAN_STA_WPS2;
279			}
280		} else
281			sta->flags |= WLAN_STA_MAYBE_WPS;
282		wpabuf_free(wps);
283#endif /* CONFIG_WPS */
284	}
285#ifdef CONFIG_WPS
286skip_wpa_check:
287#endif /* CONFIG_WPS */
288
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
300	new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
301	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
302
303	if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
304		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
305	else
306		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
307
308	hostapd_new_assoc_sta(hapd, sta, !new_assoc);
309
310	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
311
312#ifdef CONFIG_P2P
313	if (req_ies) {
314		p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
315				      req_ies, req_ies_len);
316	}
317#endif /* CONFIG_P2P */
318
319	return 0;
320
321fail:
322#ifdef CONFIG_IEEE80211R
323	hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
324#endif /* CONFIG_IEEE80211R */
325	hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
326	ap_free_sta(hapd, sta);
327	return -1;
328}
329
330
331void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
332{
333	struct sta_info *sta;
334
335	if (addr == NULL) {
336		/*
337		 * This could potentially happen with unexpected event from the
338		 * driver wrapper. This was seen at least in one case where the
339		 * driver ended up reporting a station mode event while hostapd
340		 * was running, so better make sure we stop processing such an
341		 * event here.
342		 */
343		wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
344			   "with no address");
345		return;
346	}
347
348	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
349		       HOSTAPD_LEVEL_INFO, "disassociated");
350
351	sta = ap_get_sta(hapd, addr);
352	if (sta == NULL) {
353		wpa_printf(MSG_DEBUG, "Disassociation notification for "
354			   "unknown STA " MACSTR, MAC2STR(addr));
355		return;
356	}
357
358	ap_sta_set_authorized(hapd, sta, 0);
359	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
360	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
361	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
362	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
363	ap_free_sta(hapd, sta);
364}
365
366
367void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
368{
369	struct sta_info *sta = ap_get_sta(hapd, addr);
370
371	if (!sta || !hapd->conf->disassoc_low_ack)
372		return;
373
374	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
375		       HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
376		       "missing ACKs");
377	hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
378	if (sta)
379		ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
380}
381
382
383void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
384			     int offset)
385{
386#ifdef NEED_AP_MLME
387	int channel;
388
389	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
390		       HOSTAPD_LEVEL_INFO, "driver had channel switch: "
391		       "freq=%d, ht=%d, offset=%d", freq, ht, offset);
392
393	hapd->iface->freq = freq;
394
395	channel = hostapd_hw_get_channel(hapd, freq);
396	if (!channel) {
397		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
398			       HOSTAPD_LEVEL_WARNING, "driver switched to "
399			       "bad channel!");
400		return;
401	}
402
403	hapd->iconf->channel = channel;
404	hapd->iconf->ieee80211n = ht;
405	hapd->iconf->secondary_channel = offset;
406
407	if (hapd->iface->csa_in_progress && freq == hapd->iface->cs_freq) {
408		hostapd_cleanup_cs_params(hapd);
409
410		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED "freq=%d",
411			freq);
412	}
413#endif /* NEED_AP_MLME */
414}
415
416
417void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
418					 const u8 *addr, int reason_code)
419{
420	switch (reason_code) {
421	case MAX_CLIENT_REACHED:
422		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
423			MAC2STR(addr));
424		break;
425	case BLOCKED_CLIENT:
426		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
427			MAC2STR(addr));
428		break;
429	}
430}
431
432
433int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
434			 const u8 *bssid, const u8 *ie, size_t ie_len,
435			 int ssi_signal)
436{
437	size_t i;
438	int ret = 0;
439
440	if (sa == NULL || ie == NULL)
441		return -1;
442
443	random_add_randomness(sa, ETH_ALEN);
444	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
445		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
446					    sa, da, bssid, ie, ie_len,
447					    ssi_signal) > 0) {
448			ret = 1;
449			break;
450		}
451	}
452	return ret;
453}
454
455
456#ifdef HOSTAPD
457
458#ifdef CONFIG_IEEE80211R
459static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
460					  const u8 *bssid,
461					  u16 auth_transaction, u16 status,
462					  const u8 *ies, size_t ies_len)
463{
464	struct hostapd_data *hapd = ctx;
465	struct sta_info *sta;
466
467	sta = ap_get_sta(hapd, dst);
468	if (sta == NULL)
469		return;
470
471	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
472		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
473	sta->flags |= WLAN_STA_AUTH;
474
475	hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
476}
477#endif /* CONFIG_IEEE80211R */
478
479
480static void hostapd_notif_auth(struct hostapd_data *hapd,
481			       struct auth_info *rx_auth)
482{
483	struct sta_info *sta;
484	u16 status = WLAN_STATUS_SUCCESS;
485	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
486	size_t resp_ies_len = 0;
487
488	sta = ap_get_sta(hapd, rx_auth->peer);
489	if (!sta) {
490		sta = ap_sta_add(hapd, rx_auth->peer);
491		if (sta == NULL) {
492			status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
493			goto fail;
494		}
495	}
496	sta->flags &= ~WLAN_STA_PREAUTH;
497	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
498#ifdef CONFIG_IEEE80211R
499	if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
500		sta->auth_alg = WLAN_AUTH_FT;
501		if (sta->wpa_sm == NULL)
502			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
503							sta->addr, NULL);
504		if (sta->wpa_sm == NULL) {
505			wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
506				   "state machine");
507			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
508			goto fail;
509		}
510		wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
511				    rx_auth->auth_transaction, rx_auth->ies,
512				    rx_auth->ies_len,
513				    hostapd_notify_auth_ft_finish, hapd);
514		return;
515	}
516#endif /* CONFIG_IEEE80211R */
517fail:
518	hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
519			 status, resp_ies, resp_ies_len);
520}
521
522
523static void hostapd_action_rx(struct hostapd_data *hapd,
524			      struct rx_action *action)
525{
526	struct sta_info *sta;
527
528        wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
529		   action->category, (int) action->len);
530
531	sta = ap_get_sta(hapd, action->sa);
532	if (sta == NULL) {
533		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
534		return;
535	}
536#ifdef CONFIG_IEEE80211R
537	if (action->category == WLAN_ACTION_FT) {
538		wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
539			   __func__, (int) action->len);
540		wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
541	}
542#endif /* CONFIG_IEEE80211R */
543#ifdef CONFIG_IEEE80211W
544	if (action->category == WLAN_ACTION_SA_QUERY && action->len >= 4) {
545		wpa_printf(MSG_DEBUG, "%s: SA_QUERY_ACTION length %d",
546			   __func__, (int) action->len);
547		ieee802_11_sa_query_action(hapd, action->sa,
548					   *(action->data + 1),
549					   action->data + 2);
550	}
551#endif /* CONFIG_IEEE80211W */
552#ifdef CONFIG_WNM
553	if (action->category == WLAN_ACTION_WNM) {
554		wpa_printf(MSG_DEBUG, "%s: WNM_ACTION length %d",
555			   __func__, (int) action->len);
556		ieee802_11_rx_wnm_action_ap(hapd, action);
557	}
558#endif /* CONFIG_WNM */
559}
560
561
562#ifdef NEED_AP_MLME
563
564#define HAPD_BROADCAST ((struct hostapd_data *) -1)
565
566static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
567					    const u8 *bssid)
568{
569	size_t i;
570
571	if (bssid == NULL)
572		return NULL;
573	if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
574	    bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
575		return HAPD_BROADCAST;
576
577	for (i = 0; i < iface->num_bss; i++) {
578		if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
579			return iface->bss[i];
580	}
581
582	return NULL;
583}
584
585
586static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
587					const u8 *bssid, const u8 *addr,
588					int wds)
589{
590	hapd = get_hapd_bssid(hapd->iface, bssid);
591	if (hapd == NULL || hapd == HAPD_BROADCAST)
592		return;
593
594	ieee802_11_rx_from_unknown(hapd, addr, wds);
595}
596
597
598static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
599{
600	struct hostapd_iface *iface = hapd->iface;
601	const struct ieee80211_hdr *hdr;
602	const u8 *bssid;
603	struct hostapd_frame_info fi;
604
605	hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
606	bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
607	if (bssid == NULL)
608		return;
609
610	hapd = get_hapd_bssid(iface, bssid);
611	if (hapd == NULL) {
612		u16 fc;
613		fc = le_to_host16(hdr->frame_control);
614
615		/*
616		 * Drop frames to unknown BSSIDs except for Beacon frames which
617		 * could be used to update neighbor information.
618		 */
619		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
620		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
621			hapd = iface->bss[0];
622		else
623			return;
624	}
625
626	os_memset(&fi, 0, sizeof(fi));
627	fi.datarate = rx_mgmt->datarate;
628	fi.ssi_signal = rx_mgmt->ssi_signal;
629
630	if (hapd == HAPD_BROADCAST) {
631		size_t i;
632		for (i = 0; i < iface->num_bss; i++)
633			ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
634					rx_mgmt->frame_len, &fi);
635	} else
636		ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
637
638	random_add_randomness(&fi, sizeof(fi));
639}
640
641
642static void hostapd_rx_action(struct hostapd_data *hapd,
643			      struct rx_action *rx_action)
644{
645	struct rx_mgmt rx_mgmt;
646	u8 *buf;
647	struct ieee80211_hdr *hdr;
648
649	wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
650		   " BSSID=" MACSTR " category=%u",
651		   MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
652		   MAC2STR(rx_action->bssid), rx_action->category);
653	wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
654		    rx_action->data, rx_action->len);
655
656	buf = os_zalloc(24 + 1 + rx_action->len);
657	if (buf == NULL)
658		return;
659	hdr = (struct ieee80211_hdr *) buf;
660	hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
661					  WLAN_FC_STYPE_ACTION);
662	if (rx_action->category == WLAN_ACTION_SA_QUERY) {
663		/*
664		 * Assume frame was protected; it would have been dropped if
665		 * not.
666		 */
667		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
668	}
669	os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
670	os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
671	os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
672	buf[24] = rx_action->category;
673	os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
674	os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
675	rx_mgmt.frame = buf;
676	rx_mgmt.frame_len = 24 + 1 + rx_action->len;
677	hostapd_mgmt_rx(hapd, &rx_mgmt);
678	os_free(buf);
679}
680
681
682static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
683			       size_t len, u16 stype, int ok)
684{
685	struct ieee80211_hdr *hdr;
686	hdr = (struct ieee80211_hdr *) buf;
687	hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
688	if (hapd == NULL || hapd == HAPD_BROADCAST)
689		return;
690	ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
691}
692
693#endif /* NEED_AP_MLME */
694
695
696static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
697{
698	struct sta_info *sta = ap_get_sta(hapd, addr);
699	if (sta)
700		return 0;
701
702	wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
703		   " - adding a new STA", MAC2STR(addr));
704	sta = ap_sta_add(hapd, addr);
705	if (sta) {
706		hostapd_new_assoc_sta(hapd, sta, 0);
707	} else {
708		wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
709			   MAC2STR(addr));
710		return -1;
711	}
712
713	return 0;
714}
715
716
717static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
718				   const u8 *data, size_t data_len)
719{
720	struct hostapd_iface *iface = hapd->iface;
721	struct sta_info *sta;
722	size_t j;
723
724	for (j = 0; j < iface->num_bss; j++) {
725		if ((sta = ap_get_sta(iface->bss[j], src))) {
726			if (sta->flags & WLAN_STA_ASSOC) {
727				hapd = iface->bss[j];
728				break;
729			}
730		}
731	}
732
733	ieee802_1x_receive(hapd, src, data, data_len);
734}
735
736
737static struct hostapd_channel_data * hostapd_get_mode_channel(
738	struct hostapd_iface *iface, unsigned int freq)
739{
740	int i;
741	struct hostapd_channel_data *chan;
742
743	for (i = 0; i < iface->current_mode->num_channels; i++) {
744		chan = &iface->current_mode->channels[i];
745		if (!chan)
746			return NULL;
747		if ((unsigned int) chan->freq == freq)
748			return chan;
749	}
750
751	return NULL;
752}
753
754
755static void hostapd_update_nf(struct hostapd_iface *iface,
756			      struct hostapd_channel_data *chan,
757			      struct freq_survey *survey)
758{
759	if (!iface->chans_surveyed) {
760		chan->min_nf = survey->nf;
761		iface->lowest_nf = survey->nf;
762	} else {
763		if (dl_list_empty(&chan->survey_list))
764			chan->min_nf = survey->nf;
765		else if (survey->nf < chan->min_nf)
766			chan->min_nf = survey->nf;
767		if (survey->nf < iface->lowest_nf)
768			iface->lowest_nf = survey->nf;
769	}
770}
771
772
773static void hostapd_event_get_survey(struct hostapd_data *hapd,
774				     struct survey_results *survey_results)
775{
776	struct hostapd_iface *iface = hapd->iface;
777	struct freq_survey *survey, *tmp;
778	struct hostapd_channel_data *chan;
779
780	if (dl_list_empty(&survey_results->survey_list)) {
781		wpa_printf(MSG_DEBUG, "No survey data received");
782		return;
783	}
784
785	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
786			      struct freq_survey, list) {
787		chan = hostapd_get_mode_channel(iface, survey->freq);
788		if (!chan)
789			continue;
790		if (chan->flag & HOSTAPD_CHAN_DISABLED)
791			continue;
792
793		dl_list_del(&survey->list);
794		dl_list_add_tail(&chan->survey_list, &survey->list);
795
796		hostapd_update_nf(iface, chan, survey);
797
798		iface->chans_surveyed++;
799	}
800}
801
802
803#ifdef NEED_AP_MLME
804
805static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
806					     struct dfs_event *radar)
807{
808	wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
809	hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
810				   radar->chan_offset, radar->chan_width,
811				   radar->cf1, radar->cf2);
812}
813
814
815static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
816					   struct dfs_event *radar)
817{
818	wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
819	hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
820				 radar->chan_offset, radar->chan_width,
821				 radar->cf1, radar->cf2);
822}
823
824
825static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
826					  struct dfs_event *radar)
827{
828	wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
829	hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
830				 radar->chan_offset, radar->chan_width,
831				 radar->cf1, radar->cf2);
832}
833
834
835static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
836					   struct dfs_event *radar)
837{
838	wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
839	hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
840				 radar->chan_offset, radar->chan_width,
841				 radar->cf1, radar->cf2);
842}
843
844#endif /* NEED_AP_MLME */
845
846
847void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
848			  union wpa_event_data *data)
849{
850	struct hostapd_data *hapd = ctx;
851#ifndef CONFIG_NO_STDOUT_DEBUG
852	int level = MSG_DEBUG;
853
854	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
855	    data->rx_mgmt.frame_len >= 24) {
856		const struct ieee80211_hdr *hdr;
857		u16 fc;
858		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
859		fc = le_to_host16(hdr->frame_control);
860		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
861		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
862			level = MSG_EXCESSIVE;
863		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
864		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
865			level = MSG_EXCESSIVE;
866	}
867
868	wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
869		event_to_string(event), event);
870#endif /* CONFIG_NO_STDOUT_DEBUG */
871
872	switch (event) {
873	case EVENT_MICHAEL_MIC_FAILURE:
874		michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
875		break;
876	case EVENT_SCAN_RESULTS:
877		if (hapd->iface->scan_cb)
878			hapd->iface->scan_cb(hapd->iface);
879		break;
880#ifdef CONFIG_IEEE80211R
881	case EVENT_FT_RRB_RX:
882		wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
883			      data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
884		break;
885#endif /* CONFIG_IEEE80211R */
886	case EVENT_WPS_BUTTON_PUSHED:
887		hostapd_wps_button_pushed(hapd, NULL);
888		break;
889#ifdef NEED_AP_MLME
890	case EVENT_TX_STATUS:
891		switch (data->tx_status.type) {
892		case WLAN_FC_TYPE_MGMT:
893			hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
894					   data->tx_status.data_len,
895					   data->tx_status.stype,
896					   data->tx_status.ack);
897			break;
898		case WLAN_FC_TYPE_DATA:
899			hostapd_tx_status(hapd, data->tx_status.dst,
900					  data->tx_status.data,
901					  data->tx_status.data_len,
902					  data->tx_status.ack);
903			break;
904		}
905		break;
906	case EVENT_EAPOL_TX_STATUS:
907		hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
908					data->eapol_tx_status.data,
909					data->eapol_tx_status.data_len,
910					data->eapol_tx_status.ack);
911		break;
912	case EVENT_DRIVER_CLIENT_POLL_OK:
913		hostapd_client_poll_ok(hapd, data->client_poll.addr);
914		break;
915	case EVENT_RX_FROM_UNKNOWN:
916		hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
917					    data->rx_from_unknown.addr,
918					    data->rx_from_unknown.wds);
919		break;
920	case EVENT_RX_MGMT:
921		hostapd_mgmt_rx(hapd, &data->rx_mgmt);
922		break;
923#endif /* NEED_AP_MLME */
924	case EVENT_RX_PROBE_REQ:
925		if (data->rx_probe_req.sa == NULL ||
926		    data->rx_probe_req.ie == NULL)
927			break;
928		hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
929				     data->rx_probe_req.da,
930				     data->rx_probe_req.bssid,
931				     data->rx_probe_req.ie,
932				     data->rx_probe_req.ie_len,
933				     data->rx_probe_req.ssi_signal);
934		break;
935	case EVENT_NEW_STA:
936		hostapd_event_new_sta(hapd, data->new_sta.addr);
937		break;
938	case EVENT_EAPOL_RX:
939		hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
940				       data->eapol_rx.data,
941				       data->eapol_rx.data_len);
942		break;
943	case EVENT_ASSOC:
944		hostapd_notif_assoc(hapd, data->assoc_info.addr,
945				    data->assoc_info.req_ies,
946				    data->assoc_info.req_ies_len,
947				    data->assoc_info.reassoc);
948		break;
949	case EVENT_DISASSOC:
950		if (data)
951			hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
952		break;
953	case EVENT_DEAUTH:
954		if (data)
955			hostapd_notif_disassoc(hapd, data->deauth_info.addr);
956		break;
957	case EVENT_STATION_LOW_ACK:
958		if (!data)
959			break;
960		hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
961		break;
962	case EVENT_RX_ACTION:
963		if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
964		    data->rx_action.bssid == NULL)
965			break;
966#ifdef NEED_AP_MLME
967		hostapd_rx_action(hapd, &data->rx_action);
968#endif /* NEED_AP_MLME */
969		hostapd_action_rx(hapd, &data->rx_action);
970		break;
971	case EVENT_AUTH:
972		hostapd_notif_auth(hapd, &data->auth);
973		break;
974	case EVENT_CH_SWITCH:
975		if (!data)
976			break;
977		hostapd_event_ch_switch(hapd, data->ch_switch.freq,
978					data->ch_switch.ht_enabled,
979					data->ch_switch.ch_offset);
980		break;
981	case EVENT_CONNECT_FAILED_REASON:
982		if (!data)
983			break;
984		hostapd_event_connect_failed_reason(
985			hapd, data->connect_failed_reason.addr,
986			data->connect_failed_reason.code);
987		break;
988	case EVENT_SURVEY:
989		hostapd_event_get_survey(hapd, &data->survey_results);
990		break;
991#ifdef NEED_AP_MLME
992	case EVENT_DFS_RADAR_DETECTED:
993		if (!data)
994			break;
995		hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
996		break;
997	case EVENT_DFS_CAC_FINISHED:
998		if (!data)
999			break;
1000		hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1001		break;
1002	case EVENT_DFS_CAC_ABORTED:
1003		if (!data)
1004			break;
1005		hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1006		break;
1007	case EVENT_DFS_NOP_FINISHED:
1008		if (!data)
1009			break;
1010		hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1011		break;
1012	case EVENT_CHANNEL_LIST_CHANGED:
1013		/* channel list changed (regulatory?), update channel list */
1014		/* TODO: check this. hostapd_get_hw_features() initializes
1015		 * too much stuff. */
1016		/* hostapd_get_hw_features(hapd->iface); */
1017		hostapd_channel_list_updated(
1018			hapd->iface, data->channel_list_changed.initiator);
1019		break;
1020#endif /* NEED_AP_MLME */
1021	default:
1022		wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1023		break;
1024	}
1025}
1026
1027#endif /* HOSTAPD */
1028