ieee802_11.c revision 3c4793790bf06f1fe08a2ddf604c9caa855151f1
1/*
2 * hostapd / IEEE 802.11 Management
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#ifndef CONFIG_NATIVE_WINDOWS
12
13#include "utils/common.h"
14#include "utils/eloop.h"
15#include "crypto/crypto.h"
16#include "crypto/sha256.h"
17#include "crypto/random.h"
18#include "common/ieee802_11_defs.h"
19#include "common/ieee802_11_common.h"
20#include "common/wpa_ctrl.h"
21#include "common/sae.h"
22#include "radius/radius.h"
23#include "radius/radius_client.h"
24#include "p2p/p2p.h"
25#include "wps/wps.h"
26#include "hostapd.h"
27#include "beacon.h"
28#include "ieee802_11_auth.h"
29#include "sta_info.h"
30#include "ieee802_1x.h"
31#include "wpa_auth.h"
32#include "wmm.h"
33#include "ap_list.h"
34#include "accounting.h"
35#include "ap_config.h"
36#include "ap_mlme.h"
37#include "p2p_hostapd.h"
38#include "ap_drv_ops.h"
39#include "wnm_ap.h"
40#include "ieee802_11.h"
41
42
43u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
44{
45	u8 *pos = eid;
46	int i, num, count;
47
48	if (hapd->iface->current_rates == NULL)
49		return eid;
50
51	*pos++ = WLAN_EID_SUPP_RATES;
52	num = hapd->iface->num_rates;
53	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
54		num++;
55	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
56		num++;
57	if (num > 8) {
58		/* rest of the rates are encoded in Extended supported
59		 * rates element */
60		num = 8;
61	}
62
63	*pos++ = num;
64	count = 0;
65	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
66	     i++) {
67		count++;
68		*pos = hapd->iface->current_rates[i].rate / 5;
69		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
70			*pos |= 0x80;
71		pos++;
72	}
73
74	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
75		count++;
76		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
77	}
78
79	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
80		count++;
81		*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
82	}
83
84	return pos;
85}
86
87
88u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
89{
90	u8 *pos = eid;
91	int i, num, count;
92
93	if (hapd->iface->current_rates == NULL)
94		return eid;
95
96	num = hapd->iface->num_rates;
97	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
98		num++;
99	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
100		num++;
101	if (num <= 8)
102		return eid;
103	num -= 8;
104
105	*pos++ = WLAN_EID_EXT_SUPP_RATES;
106	*pos++ = num;
107	count = 0;
108	for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
109	     i++) {
110		count++;
111		if (count <= 8)
112			continue; /* already in SuppRates IE */
113		*pos = hapd->iface->current_rates[i].rate / 5;
114		if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
115			*pos |= 0x80;
116		pos++;
117	}
118
119	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
120		count++;
121		if (count > 8)
122			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
123	}
124
125	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
126		count++;
127		if (count > 8)
128			*pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
129	}
130
131	return pos;
132}
133
134
135u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
136			   int probe)
137{
138	int capab = WLAN_CAPABILITY_ESS;
139	int privacy;
140
141	if (hapd->iface->num_sta_no_short_preamble == 0 &&
142	    hapd->iconf->preamble == SHORT_PREAMBLE)
143		capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
144
145	privacy = hapd->conf->ssid.wep.keys_set;
146
147	if (hapd->conf->ieee802_1x &&
148	    (hapd->conf->default_wep_key_len ||
149	     hapd->conf->individual_wep_key_len))
150		privacy = 1;
151
152	if (hapd->conf->wpa)
153		privacy = 1;
154
155	if (sta) {
156		int policy, def_klen;
157		if (probe && sta->ssid_probe) {
158			policy = sta->ssid_probe->security_policy;
159			def_klen = sta->ssid_probe->wep.default_len;
160		} else {
161			policy = sta->ssid->security_policy;
162			def_klen = sta->ssid->wep.default_len;
163		}
164		privacy = policy != SECURITY_PLAINTEXT;
165		if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
166			privacy = 0;
167	}
168
169	if (privacy)
170		capab |= WLAN_CAPABILITY_PRIVACY;
171
172	if (hapd->iface->current_mode &&
173	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
174	    hapd->iface->num_sta_no_short_slot_time == 0)
175		capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
176
177	return capab;
178}
179
180
181static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
182			   u16 auth_transaction, const u8 *challenge,
183			   int iswep)
184{
185	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
186		       HOSTAPD_LEVEL_DEBUG,
187		       "authentication (shared key, transaction %d)",
188		       auth_transaction);
189
190	if (auth_transaction == 1) {
191		if (!sta->challenge) {
192			/* Generate a pseudo-random challenge */
193			u8 key[8];
194			struct os_time now;
195			int r;
196			sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
197			if (sta->challenge == NULL)
198				return WLAN_STATUS_UNSPECIFIED_FAILURE;
199
200			os_get_time(&now);
201			r = os_random();
202			os_memcpy(key, &now.sec, 4);
203			os_memcpy(key + 4, &r, 4);
204			rc4_skip(key, sizeof(key), 0,
205				 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
206		}
207		return 0;
208	}
209
210	if (auth_transaction != 3)
211		return WLAN_STATUS_UNSPECIFIED_FAILURE;
212
213	/* Transaction 3 */
214	if (!iswep || !sta->challenge || !challenge ||
215	    os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
216		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
217			       HOSTAPD_LEVEL_INFO,
218			       "shared key authentication - invalid "
219			       "challenge-response");
220		return WLAN_STATUS_CHALLENGE_FAIL;
221	}
222
223	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
224		       HOSTAPD_LEVEL_DEBUG,
225		       "authentication OK (shared key)");
226	sta->flags |= WLAN_STA_AUTH;
227	wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
228	os_free(sta->challenge);
229	sta->challenge = NULL;
230
231	return 0;
232}
233
234
235static void send_auth_reply(struct hostapd_data *hapd,
236			    const u8 *dst, const u8 *bssid,
237			    u16 auth_alg, u16 auth_transaction, u16 resp,
238			    const u8 *ies, size_t ies_len)
239{
240	struct ieee80211_mgmt *reply;
241	u8 *buf;
242	size_t rlen;
243
244	rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
245	buf = os_zalloc(rlen);
246	if (buf == NULL)
247		return;
248
249	reply = (struct ieee80211_mgmt *) buf;
250	reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
251					    WLAN_FC_STYPE_AUTH);
252	os_memcpy(reply->da, dst, ETH_ALEN);
253	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
254	os_memcpy(reply->bssid, bssid, ETH_ALEN);
255
256	reply->u.auth.auth_alg = host_to_le16(auth_alg);
257	reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
258	reply->u.auth.status_code = host_to_le16(resp);
259
260	if (ies && ies_len)
261		os_memcpy(reply->u.auth.variable, ies, ies_len);
262
263	wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
264		   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
265		   MAC2STR(dst), auth_alg, auth_transaction,
266		   resp, (unsigned long) ies_len);
267	if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
268		wpa_printf(MSG_INFO, "send_auth_reply: send");
269
270	os_free(buf);
271}
272
273
274#ifdef CONFIG_IEEE80211R
275static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
276				  u16 auth_transaction, u16 status,
277				  const u8 *ies, size_t ies_len)
278{
279	struct hostapd_data *hapd = ctx;
280	struct sta_info *sta;
281
282	send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
283			status, ies, ies_len);
284
285	if (status != WLAN_STATUS_SUCCESS)
286		return;
287
288	sta = ap_get_sta(hapd, dst);
289	if (sta == NULL)
290		return;
291
292	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
293		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
294	sta->flags |= WLAN_STA_AUTH;
295	mlme_authenticate_indication(hapd, sta);
296}
297#endif /* CONFIG_IEEE80211R */
298
299
300#ifdef CONFIG_SAE
301
302static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
303					       struct sta_info *sta)
304{
305	struct wpabuf *buf;
306
307	if (hapd->conf->ssid.wpa_passphrase == NULL) {
308		wpa_printf(MSG_DEBUG, "SAE: No password available");
309		return NULL;
310	}
311
312	if (sae_prepare_commit(hapd->own_addr, sta->addr,
313			       (u8 *) hapd->conf->ssid.wpa_passphrase,
314			       os_strlen(hapd->conf->ssid.wpa_passphrase),
315			       sta->sae) < 0) {
316		wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
317		return NULL;
318	}
319
320	if (sae_process_commit(sta->sae) < 0) {
321		wpa_printf(MSG_DEBUG, "SAE: Failed to process peer commit");
322		return NULL;
323	}
324
325	buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
326	if (buf == NULL)
327		return NULL;
328	sae_write_commit(sta->sae, buf, NULL);
329
330	return buf;
331}
332
333
334static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
335					      struct sta_info *sta)
336{
337	struct wpabuf *buf;
338
339	buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
340	if (buf == NULL)
341		return NULL;
342
343	sae_write_confirm(sta->sae, buf);
344
345	return buf;
346}
347
348
349static int use_sae_anti_clogging(struct hostapd_data *hapd)
350{
351	struct sta_info *sta;
352	unsigned int open = 0;
353
354	if (hapd->conf->sae_anti_clogging_threshold == 0)
355		return 1;
356
357	for (sta = hapd->sta_list; sta; sta = sta->next) {
358		if (!sta->sae)
359			continue;
360		if (sta->sae->state != SAE_COMMITTED &&
361		    sta->sae->state != SAE_CONFIRMED)
362			continue;
363		open++;
364		if (open >= hapd->conf->sae_anti_clogging_threshold)
365			return 1;
366	}
367
368	return 0;
369}
370
371
372static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
373			   const u8 *token, size_t token_len)
374{
375	u8 mac[SHA256_MAC_LEN];
376
377	if (token_len != SHA256_MAC_LEN)
378		return -1;
379	if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
380			addr, ETH_ALEN, mac) < 0 ||
381	    os_memcmp(token, mac, SHA256_MAC_LEN) != 0)
382		return -1;
383
384	return 0;
385}
386
387
388static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
389					    const u8 *addr)
390{
391	struct wpabuf *buf;
392	u8 *token;
393	struct os_reltime now;
394
395	os_get_reltime(&now);
396	if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
397	    os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
398		if (random_get_bytes(hapd->sae_token_key,
399				     sizeof(hapd->sae_token_key)) < 0)
400			return NULL;
401		wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
402			    hapd->sae_token_key, sizeof(hapd->sae_token_key));
403		hapd->last_sae_token_key_update = now;
404	}
405
406	buf = wpabuf_alloc(SHA256_MAC_LEN);
407	if (buf == NULL)
408		return NULL;
409
410	token = wpabuf_put(buf, SHA256_MAC_LEN);
411	hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
412		    addr, ETH_ALEN, token);
413
414	return buf;
415}
416
417
418static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
419			    const struct ieee80211_mgmt *mgmt, size_t len,
420			    u8 auth_transaction)
421{
422	u16 resp = WLAN_STATUS_SUCCESS;
423	struct wpabuf *data = NULL;
424
425	if (!sta->sae) {
426		if (auth_transaction != 1)
427			return;
428		sta->sae = os_zalloc(sizeof(*sta->sae));
429		if (sta->sae == NULL)
430			return;
431		sta->sae->state = SAE_NOTHING;
432	}
433
434	if (auth_transaction == 1) {
435		const u8 *token = NULL;
436		size_t token_len = 0;
437		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
438			       HOSTAPD_LEVEL_DEBUG,
439			       "start SAE authentication (RX commit)");
440		resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
441					((const u8 *) mgmt) + len -
442					mgmt->u.auth.variable, &token,
443					&token_len, hapd->conf->sae_groups);
444		if (token && check_sae_token(hapd, sta->addr, token, token_len)
445		    < 0) {
446			wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
447				   "incorrect token from " MACSTR,
448				   MAC2STR(sta->addr));
449			return;
450		}
451
452		if (resp == WLAN_STATUS_SUCCESS) {
453			if (!token && use_sae_anti_clogging(hapd)) {
454				wpa_printf(MSG_DEBUG, "SAE: Request anti-"
455					   "clogging token from " MACSTR,
456					   MAC2STR(sta->addr));
457				data = auth_build_token_req(hapd, sta->addr);
458				resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
459			} else {
460				data = auth_process_sae_commit(hapd, sta);
461				if (data == NULL)
462					resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
463				else
464					sta->sae->state = SAE_COMMITTED;
465			}
466		}
467	} else if (auth_transaction == 2) {
468		if (sta->sae->state != SAE_COMMITTED) {
469			hostapd_logger(hapd, sta->addr,
470				       HOSTAPD_MODULE_IEEE80211,
471				       HOSTAPD_LEVEL_DEBUG,
472				       "SAE confirm before commit");
473			resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
474		}
475		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
476			       HOSTAPD_LEVEL_DEBUG,
477			       "SAE authentication (RX confirm)");
478		if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
479				       ((u8 *) mgmt) + len -
480				       mgmt->u.auth.variable) < 0) {
481			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
482		} else {
483			resp = WLAN_STATUS_SUCCESS;
484			sta->flags |= WLAN_STA_AUTH;
485			wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
486			sta->auth_alg = WLAN_AUTH_SAE;
487			mlme_authenticate_indication(hapd, sta);
488
489			data = auth_build_sae_confirm(hapd, sta);
490			if (data == NULL)
491				resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
492			else {
493				sta->sae->state = SAE_ACCEPTED;
494				sae_clear_temp_data(sta->sae);
495			}
496		}
497	} else {
498		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
499			       HOSTAPD_LEVEL_DEBUG,
500			       "unexpected SAE authentication transaction %u",
501			       auth_transaction);
502		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
503	}
504
505	sta->auth_alg = WLAN_AUTH_SAE;
506
507	send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
508			auth_transaction, resp,
509			data ? wpabuf_head(data) : (u8 *) "",
510			data ? wpabuf_len(data) : 0);
511	wpabuf_free(data);
512}
513#endif /* CONFIG_SAE */
514
515
516static void handle_auth(struct hostapd_data *hapd,
517			const struct ieee80211_mgmt *mgmt, size_t len)
518{
519	u16 auth_alg, auth_transaction, status_code;
520	u16 resp = WLAN_STATUS_SUCCESS;
521	struct sta_info *sta = NULL;
522	int res;
523	u16 fc;
524	const u8 *challenge = NULL;
525	u32 session_timeout, acct_interim_interval;
526	int vlan_id = 0;
527	struct hostapd_sta_wpa_psk_short *psk = NULL;
528	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
529	size_t resp_ies_len = 0;
530	char *identity = NULL;
531	char *radius_cui = NULL;
532
533	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
534		wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
535			   (unsigned long) len);
536		return;
537	}
538
539#ifdef CONFIG_TESTING_OPTIONS
540	if (hapd->iconf->ignore_auth_probability > 0.0d &&
541	    drand48() < hapd->iconf->ignore_auth_probability) {
542		wpa_printf(MSG_INFO,
543			   "TESTING: ignoring auth frame from " MACSTR,
544			   MAC2STR(mgmt->sa));
545		return;
546	}
547#endif /* CONFIG_TESTING_OPTIONS */
548
549	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
550	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
551	status_code = le_to_host16(mgmt->u.auth.status_code);
552	fc = le_to_host16(mgmt->frame_control);
553
554	if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
555	    2 + WLAN_AUTH_CHALLENGE_LEN &&
556	    mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
557	    mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
558		challenge = &mgmt->u.auth.variable[2];
559
560	wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
561		   "auth_transaction=%d status_code=%d wep=%d%s",
562		   MAC2STR(mgmt->sa), auth_alg, auth_transaction,
563		   status_code, !!(fc & WLAN_FC_ISWEP),
564		   challenge ? " challenge" : "");
565
566	if (hapd->tkip_countermeasures) {
567		resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
568		goto fail;
569	}
570
571	if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
572	       auth_alg == WLAN_AUTH_OPEN) ||
573#ifdef CONFIG_IEEE80211R
574	      (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
575	       auth_alg == WLAN_AUTH_FT) ||
576#endif /* CONFIG_IEEE80211R */
577#ifdef CONFIG_SAE
578	      (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
579	       auth_alg == WLAN_AUTH_SAE) ||
580#endif /* CONFIG_SAE */
581	      ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
582	       auth_alg == WLAN_AUTH_SHARED_KEY))) {
583		wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
584			   auth_alg);
585		resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
586		goto fail;
587	}
588
589	if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
590	      (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
591		wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
592			   auth_transaction);
593		resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
594		goto fail;
595	}
596
597	if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
598		wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
599			   MAC2STR(mgmt->sa));
600		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
601		goto fail;
602	}
603
604	res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
605				      &session_timeout,
606				      &acct_interim_interval, &vlan_id,
607				      &psk, &identity, &radius_cui);
608
609	if (res == HOSTAPD_ACL_REJECT) {
610		wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
611			   MAC2STR(mgmt->sa));
612		resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
613		goto fail;
614	}
615	if (res == HOSTAPD_ACL_PENDING) {
616		wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
617			   " waiting for an external authentication",
618			   MAC2STR(mgmt->sa));
619		/* Authentication code will re-send the authentication frame
620		 * after it has received (and cached) information from the
621		 * external source. */
622		return;
623	}
624
625	sta = ap_sta_add(hapd, mgmt->sa);
626	if (!sta) {
627		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
628		goto fail;
629	}
630
631	if (vlan_id > 0) {
632		if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
633			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
634				       HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
635				       "%d received from RADIUS server",
636				       vlan_id);
637			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
638			goto fail;
639		}
640		sta->vlan_id = vlan_id;
641		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
642			       HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
643	}
644
645	hostapd_free_psk_list(sta->psk);
646	if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
647		sta->psk = psk;
648		psk = NULL;
649	} else {
650		sta->psk = NULL;
651	}
652
653	sta->identity = identity;
654	identity = NULL;
655	sta->radius_cui = radius_cui;
656	radius_cui = NULL;
657
658	sta->flags &= ~WLAN_STA_PREAUTH;
659	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
660
661	if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
662		sta->acct_interim_interval = acct_interim_interval;
663	if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
664		ap_sta_session_timeout(hapd, sta, session_timeout);
665	else
666		ap_sta_no_session_timeout(hapd, sta);
667
668	switch (auth_alg) {
669	case WLAN_AUTH_OPEN:
670		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
671			       HOSTAPD_LEVEL_DEBUG,
672			       "authentication OK (open system)");
673		sta->flags |= WLAN_STA_AUTH;
674		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
675		sta->auth_alg = WLAN_AUTH_OPEN;
676		mlme_authenticate_indication(hapd, sta);
677		break;
678	case WLAN_AUTH_SHARED_KEY:
679		resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
680				       fc & WLAN_FC_ISWEP);
681		sta->auth_alg = WLAN_AUTH_SHARED_KEY;
682		mlme_authenticate_indication(hapd, sta);
683		if (sta->challenge && auth_transaction == 1) {
684			resp_ies[0] = WLAN_EID_CHALLENGE;
685			resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
686			os_memcpy(resp_ies + 2, sta->challenge,
687				  WLAN_AUTH_CHALLENGE_LEN);
688			resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
689		}
690		break;
691#ifdef CONFIG_IEEE80211R
692	case WLAN_AUTH_FT:
693		sta->auth_alg = WLAN_AUTH_FT;
694		if (sta->wpa_sm == NULL)
695			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
696							sta->addr, NULL);
697		if (sta->wpa_sm == NULL) {
698			wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
699				   "state machine");
700			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
701			goto fail;
702		}
703		wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
704				    auth_transaction, mgmt->u.auth.variable,
705				    len - IEEE80211_HDRLEN -
706				    sizeof(mgmt->u.auth),
707				    handle_auth_ft_finish, hapd);
708		/* handle_auth_ft_finish() callback will complete auth. */
709		return;
710#endif /* CONFIG_IEEE80211R */
711#ifdef CONFIG_SAE
712	case WLAN_AUTH_SAE:
713		handle_auth_sae(hapd, sta, mgmt, len, auth_transaction);
714		return;
715#endif /* CONFIG_SAE */
716	}
717
718 fail:
719	os_free(identity);
720	os_free(radius_cui);
721	hostapd_free_psk_list(psk);
722
723	send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
724			auth_transaction + 1, resp, resp_ies, resp_ies_len);
725}
726
727
728static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
729{
730	int i, j = 32, aid;
731
732	/* get a unique AID */
733	if (sta->aid > 0) {
734		wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
735		return 0;
736	}
737
738	for (i = 0; i < AID_WORDS; i++) {
739		if (hapd->sta_aid[i] == (u32) -1)
740			continue;
741		for (j = 0; j < 32; j++) {
742			if (!(hapd->sta_aid[i] & BIT(j)))
743				break;
744		}
745		if (j < 32)
746			break;
747	}
748	if (j == 32)
749		return -1;
750	aid = i * 32 + j + 1;
751	if (aid > 2007)
752		return -1;
753
754	sta->aid = aid;
755	hapd->sta_aid[i] |= BIT(j);
756	wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
757	return 0;
758}
759
760
761static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
762		      const u8 *ssid_ie, size_t ssid_ie_len)
763{
764	if (ssid_ie == NULL)
765		return WLAN_STATUS_UNSPECIFIED_FAILURE;
766
767	if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
768	    os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
769		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
770			       HOSTAPD_LEVEL_INFO,
771			       "Station tried to associate with unknown SSID "
772			       "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
773		return WLAN_STATUS_UNSPECIFIED_FAILURE;
774	}
775
776	return WLAN_STATUS_SUCCESS;
777}
778
779
780static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
781		     const u8 *wmm_ie, size_t wmm_ie_len)
782{
783	sta->flags &= ~WLAN_STA_WMM;
784	sta->qosinfo = 0;
785	if (wmm_ie && hapd->conf->wmm_enabled) {
786		struct wmm_information_element *wmm;
787
788		if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
789			hostapd_logger(hapd, sta->addr,
790				       HOSTAPD_MODULE_WPA,
791				       HOSTAPD_LEVEL_DEBUG,
792				       "invalid WMM element in association "
793				       "request");
794			return WLAN_STATUS_UNSPECIFIED_FAILURE;
795		}
796
797		sta->flags |= WLAN_STA_WMM;
798		wmm = (struct wmm_information_element *) wmm_ie;
799		sta->qosinfo = wmm->qos_info;
800	}
801	return WLAN_STATUS_SUCCESS;
802}
803
804
805static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
806			   struct ieee802_11_elems *elems)
807{
808	if (!elems->supp_rates) {
809		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
810			       HOSTAPD_LEVEL_DEBUG,
811			       "No supported rates element in AssocReq");
812		return WLAN_STATUS_UNSPECIFIED_FAILURE;
813	}
814
815	if (elems->supp_rates_len + elems->ext_supp_rates_len >
816	    sizeof(sta->supported_rates)) {
817		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
818			       HOSTAPD_LEVEL_DEBUG,
819			       "Invalid supported rates element length %d+%d",
820			       elems->supp_rates_len,
821			       elems->ext_supp_rates_len);
822		return WLAN_STATUS_UNSPECIFIED_FAILURE;
823	}
824
825	sta->supported_rates_len = merge_byte_arrays(
826		sta->supported_rates, sizeof(sta->supported_rates),
827		elems->supp_rates, elems->supp_rates_len,
828		elems->ext_supp_rates, elems->ext_supp_rates_len);
829
830	return WLAN_STATUS_SUCCESS;
831}
832
833
834static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
835			   const u8 *ext_capab_ie, size_t ext_capab_ie_len)
836{
837#ifdef CONFIG_INTERWORKING
838	/* check for QoS Map support */
839	if (ext_capab_ie_len >= 5) {
840		if (ext_capab_ie[4] & 0x01)
841			sta->qos_map_enabled = 1;
842	}
843#endif /* CONFIG_INTERWORKING */
844
845	return WLAN_STATUS_SUCCESS;
846}
847
848
849static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
850			   const u8 *ies, size_t ies_len, int reassoc)
851{
852	struct ieee802_11_elems elems;
853	u16 resp;
854	const u8 *wpa_ie;
855	size_t wpa_ie_len;
856	const u8 *p2p_dev_addr = NULL;
857
858	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
859		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
860			       HOSTAPD_LEVEL_INFO, "Station sent an invalid "
861			       "association request");
862		return WLAN_STATUS_UNSPECIFIED_FAILURE;
863	}
864
865	resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
866	if (resp != WLAN_STATUS_SUCCESS)
867		return resp;
868	resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
869	if (resp != WLAN_STATUS_SUCCESS)
870		return resp;
871	resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
872	if (resp != WLAN_STATUS_SUCCESS)
873		return resp;
874	resp = copy_supp_rates(hapd, sta, &elems);
875	if (resp != WLAN_STATUS_SUCCESS)
876		return resp;
877#ifdef CONFIG_IEEE80211N
878	resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
879				 elems.ht_capabilities_len);
880	if (resp != WLAN_STATUS_SUCCESS)
881		return resp;
882	if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
883	    !(sta->flags & WLAN_STA_HT)) {
884		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
885			       HOSTAPD_LEVEL_INFO, "Station does not support "
886			       "mandatory HT PHY - reject association");
887		return WLAN_STATUS_ASSOC_DENIED_NO_HT;
888	}
889#endif /* CONFIG_IEEE80211N */
890
891#ifdef CONFIG_IEEE80211AC
892	resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
893				  elems.vht_capabilities_len);
894	if (resp != WLAN_STATUS_SUCCESS)
895		return resp;
896	if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
897	    !(sta->flags & WLAN_STA_VHT)) {
898		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
899			       HOSTAPD_LEVEL_INFO, "Station does not support "
900			       "mandatory VHT PHY - reject association");
901		return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
902	}
903#endif /* CONFIG_IEEE80211AC */
904
905#ifdef CONFIG_P2P
906	if (elems.p2p) {
907		wpabuf_free(sta->p2p_ie);
908		sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
909							  P2P_IE_VENDOR_TYPE);
910		if (sta->p2p_ie)
911			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
912	} else {
913		wpabuf_free(sta->p2p_ie);
914		sta->p2p_ie = NULL;
915	}
916#endif /* CONFIG_P2P */
917
918	if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
919		wpa_ie = elems.rsn_ie;
920		wpa_ie_len = elems.rsn_ie_len;
921	} else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
922		   elems.wpa_ie) {
923		wpa_ie = elems.wpa_ie;
924		wpa_ie_len = elems.wpa_ie_len;
925	} else {
926		wpa_ie = NULL;
927		wpa_ie_len = 0;
928	}
929
930#ifdef CONFIG_WPS
931	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
932	if (hapd->conf->wps_state && elems.wps_ie) {
933		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
934			   "Request - assume WPS is used");
935		sta->flags |= WLAN_STA_WPS;
936		wpabuf_free(sta->wps_ie);
937		sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
938							  WPS_IE_VENDOR_TYPE);
939		if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
940			wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
941			sta->flags |= WLAN_STA_WPS2;
942		}
943		wpa_ie = NULL;
944		wpa_ie_len = 0;
945		if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
946			wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
947				   "(Re)Association Request - reject");
948			return WLAN_STATUS_INVALID_IE;
949		}
950	} else if (hapd->conf->wps_state && wpa_ie == NULL) {
951		wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
952			   "(Re)Association Request - possible WPS use");
953		sta->flags |= WLAN_STA_MAYBE_WPS;
954	} else
955#endif /* CONFIG_WPS */
956	if (hapd->conf->wpa && wpa_ie == NULL) {
957		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
958			       HOSTAPD_LEVEL_INFO,
959			       "No WPA/RSN IE in association request");
960		return WLAN_STATUS_INVALID_IE;
961	}
962
963	if (hapd->conf->wpa && wpa_ie) {
964		int res;
965		wpa_ie -= 2;
966		wpa_ie_len += 2;
967		if (sta->wpa_sm == NULL)
968			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
969							sta->addr,
970							p2p_dev_addr);
971		if (sta->wpa_sm == NULL) {
972			wpa_printf(MSG_WARNING, "Failed to initialize WPA "
973				   "state machine");
974			return WLAN_STATUS_UNSPECIFIED_FAILURE;
975		}
976		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
977					  wpa_ie, wpa_ie_len,
978					  elems.mdie, elems.mdie_len);
979		if (res == WPA_INVALID_GROUP)
980			resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
981		else if (res == WPA_INVALID_PAIRWISE)
982			resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
983		else if (res == WPA_INVALID_AKMP)
984			resp = WLAN_STATUS_AKMP_NOT_VALID;
985		else if (res == WPA_ALLOC_FAIL)
986			resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
987#ifdef CONFIG_IEEE80211W
988		else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
989			resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
990		else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
991			resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
992#endif /* CONFIG_IEEE80211W */
993		else if (res == WPA_INVALID_MDIE)
994			resp = WLAN_STATUS_INVALID_MDIE;
995		else if (res != WPA_IE_OK)
996			resp = WLAN_STATUS_INVALID_IE;
997		if (resp != WLAN_STATUS_SUCCESS)
998			return resp;
999#ifdef CONFIG_IEEE80211W
1000		if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1001		    sta->sa_query_count > 0)
1002			ap_check_sa_query_timeout(hapd, sta);
1003		if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1004		    (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1005			/*
1006			 * STA has already been associated with MFP and SA
1007			 * Query timeout has not been reached. Reject the
1008			 * association attempt temporarily and start SA Query,
1009			 * if one is not pending.
1010			 */
1011
1012			if (sta->sa_query_count == 0)
1013				ap_sta_start_sa_query(hapd, sta);
1014
1015			return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1016		}
1017
1018		if (wpa_auth_uses_mfp(sta->wpa_sm))
1019			sta->flags |= WLAN_STA_MFP;
1020		else
1021			sta->flags &= ~WLAN_STA_MFP;
1022#endif /* CONFIG_IEEE80211W */
1023
1024#ifdef CONFIG_IEEE80211R
1025		if (sta->auth_alg == WLAN_AUTH_FT) {
1026			if (!reassoc) {
1027				wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1028					   "to use association (not "
1029					   "re-association) with FT auth_alg",
1030					   MAC2STR(sta->addr));
1031				return WLAN_STATUS_UNSPECIFIED_FAILURE;
1032			}
1033
1034			resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1035						       ies_len);
1036			if (resp != WLAN_STATUS_SUCCESS)
1037				return resp;
1038		}
1039#endif /* CONFIG_IEEE80211R */
1040
1041#ifdef CONFIG_SAE
1042		if (wpa_auth_uses_sae(sta->wpa_sm) &&
1043		    sta->auth_alg != WLAN_AUTH_SAE &&
1044		    !(sta->auth_alg == WLAN_AUTH_FT &&
1045		      wpa_auth_uses_ft_sae(sta->wpa_sm))) {
1046			wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1047				   "SAE AKM after non-SAE auth_alg %u",
1048				   MAC2STR(sta->addr), sta->auth_alg);
1049			return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1050		}
1051#endif /* CONFIG_SAE */
1052
1053#ifdef CONFIG_IEEE80211N
1054		if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
1055		    wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1056			hostapd_logger(hapd, sta->addr,
1057				       HOSTAPD_MODULE_IEEE80211,
1058				       HOSTAPD_LEVEL_INFO,
1059				       "Station tried to use TKIP with HT "
1060				       "association");
1061			return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1062		}
1063#endif /* CONFIG_IEEE80211N */
1064	} else
1065		wpa_auth_sta_no_wpa(sta->wpa_sm);
1066
1067#ifdef CONFIG_P2P
1068	p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1069#endif /* CONFIG_P2P */
1070
1071#ifdef CONFIG_HS20
1072	wpabuf_free(sta->hs20_ie);
1073	if (elems.hs20 && elems.hs20_len > 4) {
1074		sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1075						 elems.hs20_len - 4);
1076	} else
1077		sta->hs20_ie = NULL;
1078#endif /* CONFIG_HS20 */
1079
1080	return WLAN_STATUS_SUCCESS;
1081}
1082
1083
1084static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1085			u16 reason_code)
1086{
1087	int send_len;
1088	struct ieee80211_mgmt reply;
1089
1090	os_memset(&reply, 0, sizeof(reply));
1091	reply.frame_control =
1092		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1093	os_memcpy(reply.da, addr, ETH_ALEN);
1094	os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1095	os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1096
1097	send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1098	reply.u.deauth.reason_code = host_to_le16(reason_code);
1099
1100	if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
1101		wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1102			   strerror(errno));
1103}
1104
1105
1106static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1107			    u16 status_code, int reassoc, const u8 *ies,
1108			    size_t ies_len)
1109{
1110	int send_len;
1111	u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1112	struct ieee80211_mgmt *reply;
1113	u8 *p;
1114
1115	os_memset(buf, 0, sizeof(buf));
1116	reply = (struct ieee80211_mgmt *) buf;
1117	reply->frame_control =
1118		IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1119			     (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1120			      WLAN_FC_STYPE_ASSOC_RESP));
1121	os_memcpy(reply->da, sta->addr, ETH_ALEN);
1122	os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1123	os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1124
1125	send_len = IEEE80211_HDRLEN;
1126	send_len += sizeof(reply->u.assoc_resp);
1127	reply->u.assoc_resp.capab_info =
1128		host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1129	reply->u.assoc_resp.status_code = host_to_le16(status_code);
1130	reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1131					       | BIT(14) | BIT(15));
1132	/* Supported rates */
1133	p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1134	/* Extended supported rates */
1135	p = hostapd_eid_ext_supp_rates(hapd, p);
1136
1137#ifdef CONFIG_IEEE80211R
1138	if (status_code == WLAN_STATUS_SUCCESS) {
1139		/* IEEE 802.11r: Mobility Domain Information, Fast BSS
1140		 * Transition Information, RSN, [RIC Response] */
1141		p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1142						buf + sizeof(buf) - p,
1143						sta->auth_alg, ies, ies_len);
1144	}
1145#endif /* CONFIG_IEEE80211R */
1146
1147#ifdef CONFIG_IEEE80211W
1148	if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1149		p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1150#endif /* CONFIG_IEEE80211W */
1151
1152#ifdef CONFIG_IEEE80211N
1153	p = hostapd_eid_ht_capabilities(hapd, p);
1154	p = hostapd_eid_ht_operation(hapd, p);
1155#endif /* CONFIG_IEEE80211N */
1156
1157#ifdef CONFIG_IEEE80211AC
1158	p = hostapd_eid_vht_capabilities(hapd, p);
1159	p = hostapd_eid_vht_operation(hapd, p);
1160#endif /* CONFIG_IEEE80211AC */
1161
1162	p = hostapd_eid_ext_capab(hapd, p);
1163	p = hostapd_eid_bss_max_idle_period(hapd, p);
1164	if (sta->qos_map_enabled)
1165		p = hostapd_eid_qos_map_set(hapd, p);
1166
1167	if (sta->flags & WLAN_STA_WMM)
1168		p = hostapd_eid_wmm(hapd, p);
1169
1170#ifdef CONFIG_WPS
1171	if ((sta->flags & WLAN_STA_WPS) ||
1172	    ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
1173		struct wpabuf *wps = wps_build_assoc_resp_ie();
1174		if (wps) {
1175			os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1176			p += wpabuf_len(wps);
1177			wpabuf_free(wps);
1178		}
1179	}
1180#endif /* CONFIG_WPS */
1181
1182#ifdef CONFIG_P2P
1183	if (sta->p2p_ie) {
1184		struct wpabuf *p2p_resp_ie;
1185		enum p2p_status_code status;
1186		switch (status_code) {
1187		case WLAN_STATUS_SUCCESS:
1188			status = P2P_SC_SUCCESS;
1189			break;
1190		case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1191			status = P2P_SC_FAIL_LIMIT_REACHED;
1192			break;
1193		default:
1194			status = P2P_SC_FAIL_INVALID_PARAMS;
1195			break;
1196		}
1197		p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1198		if (p2p_resp_ie) {
1199			os_memcpy(p, wpabuf_head(p2p_resp_ie),
1200				  wpabuf_len(p2p_resp_ie));
1201			p += wpabuf_len(p2p_resp_ie);
1202			wpabuf_free(p2p_resp_ie);
1203		}
1204	}
1205#endif /* CONFIG_P2P */
1206
1207#ifdef CONFIG_P2P_MANAGER
1208	if (hapd->conf->p2p & P2P_MANAGE)
1209		p = hostapd_eid_p2p_manage(hapd, p);
1210#endif /* CONFIG_P2P_MANAGER */
1211
1212	send_len += p - reply->u.assoc_resp.variable;
1213
1214	if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
1215		wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1216			   strerror(errno));
1217}
1218
1219
1220static void handle_assoc(struct hostapd_data *hapd,
1221			 const struct ieee80211_mgmt *mgmt, size_t len,
1222			 int reassoc)
1223{
1224	u16 capab_info, listen_interval;
1225	u16 resp = WLAN_STATUS_SUCCESS;
1226	const u8 *pos;
1227	int left, i;
1228	struct sta_info *sta;
1229
1230	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1231				      sizeof(mgmt->u.assoc_req))) {
1232		wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
1233			   reassoc, (unsigned long) len);
1234		return;
1235	}
1236
1237#ifdef CONFIG_TESTING_OPTIONS
1238	if (reassoc) {
1239		if (hapd->iconf->ignore_reassoc_probability > 0.0d &&
1240		    drand48() < hapd->iconf->ignore_reassoc_probability) {
1241			wpa_printf(MSG_INFO,
1242				   "TESTING: ignoring reassoc request from "
1243				   MACSTR, MAC2STR(mgmt->sa));
1244			return;
1245		}
1246	} else {
1247		if (hapd->iconf->ignore_assoc_probability > 0.0d &&
1248		    drand48() < hapd->iconf->ignore_assoc_probability) {
1249			wpa_printf(MSG_INFO,
1250				   "TESTING: ignoring assoc request from "
1251				   MACSTR, MAC2STR(mgmt->sa));
1252			return;
1253		}
1254	}
1255#endif /* CONFIG_TESTING_OPTIONS */
1256
1257	if (reassoc) {
1258		capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1259		listen_interval = le_to_host16(
1260			mgmt->u.reassoc_req.listen_interval);
1261		wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1262			   " capab_info=0x%02x listen_interval=%d current_ap="
1263			   MACSTR,
1264			   MAC2STR(mgmt->sa), capab_info, listen_interval,
1265			   MAC2STR(mgmt->u.reassoc_req.current_ap));
1266		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1267		pos = mgmt->u.reassoc_req.variable;
1268	} else {
1269		capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1270		listen_interval = le_to_host16(
1271			mgmt->u.assoc_req.listen_interval);
1272		wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1273			   " capab_info=0x%02x listen_interval=%d",
1274			   MAC2STR(mgmt->sa), capab_info, listen_interval);
1275		left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1276		pos = mgmt->u.assoc_req.variable;
1277	}
1278
1279	sta = ap_get_sta(hapd, mgmt->sa);
1280#ifdef CONFIG_IEEE80211R
1281	if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1282	    (sta->flags & WLAN_STA_AUTH) == 0) {
1283		wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1284			   "prior to authentication since it is using "
1285			   "over-the-DS FT", MAC2STR(mgmt->sa));
1286	} else
1287#endif /* CONFIG_IEEE80211R */
1288	if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1289		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1290			       HOSTAPD_LEVEL_INFO, "Station tried to "
1291			       "associate before authentication "
1292			       "(aid=%d flags=0x%x)",
1293			       sta ? sta->aid : -1,
1294			       sta ? sta->flags : 0);
1295		send_deauth(hapd, mgmt->sa,
1296			    WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1297		return;
1298	}
1299
1300	if (hapd->tkip_countermeasures) {
1301		resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1302		goto fail;
1303	}
1304
1305	if (listen_interval > hapd->conf->max_listen_interval) {
1306		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1307			       HOSTAPD_LEVEL_DEBUG,
1308			       "Too large Listen Interval (%d)",
1309			       listen_interval);
1310		resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1311		goto fail;
1312	}
1313
1314	/* followed by SSID and Supported rates; and HT capabilities if 802.11n
1315	 * is used */
1316	resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1317	if (resp != WLAN_STATUS_SUCCESS)
1318		goto fail;
1319
1320	if (hostapd_get_aid(hapd, sta) < 0) {
1321		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1322			       HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1323		resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1324		goto fail;
1325	}
1326
1327	sta->capability = capab_info;
1328	sta->listen_interval = listen_interval;
1329
1330	if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1331		sta->flags |= WLAN_STA_NONERP;
1332	for (i = 0; i < sta->supported_rates_len; i++) {
1333		if ((sta->supported_rates[i] & 0x7f) > 22) {
1334			sta->flags &= ~WLAN_STA_NONERP;
1335			break;
1336		}
1337	}
1338	if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1339		sta->nonerp_set = 1;
1340		hapd->iface->num_sta_non_erp++;
1341		if (hapd->iface->num_sta_non_erp == 1)
1342			ieee802_11_set_beacons(hapd->iface);
1343	}
1344
1345	if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1346	    !sta->no_short_slot_time_set) {
1347		sta->no_short_slot_time_set = 1;
1348		hapd->iface->num_sta_no_short_slot_time++;
1349		if (hapd->iface->current_mode->mode ==
1350		    HOSTAPD_MODE_IEEE80211G &&
1351		    hapd->iface->num_sta_no_short_slot_time == 1)
1352			ieee802_11_set_beacons(hapd->iface);
1353	}
1354
1355	if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1356		sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1357	else
1358		sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1359
1360	if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1361	    !sta->no_short_preamble_set) {
1362		sta->no_short_preamble_set = 1;
1363		hapd->iface->num_sta_no_short_preamble++;
1364		if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1365		    && hapd->iface->num_sta_no_short_preamble == 1)
1366			ieee802_11_set_beacons(hapd->iface);
1367	}
1368
1369#ifdef CONFIG_IEEE80211N
1370	update_ht_state(hapd, sta);
1371#endif /* CONFIG_IEEE80211N */
1372
1373	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1374		       HOSTAPD_LEVEL_DEBUG,
1375		       "association OK (aid %d)", sta->aid);
1376	/* Station will be marked associated, after it acknowledges AssocResp
1377	 */
1378	sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1379
1380#ifdef CONFIG_IEEE80211W
1381	if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1382		wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1383			   "SA Query procedure", reassoc ? "re" : "");
1384		/* TODO: Send a protected Disassociate frame to the STA using
1385		 * the old key and Reason Code "Previous Authentication no
1386		 * longer valid". Make sure this is only sent protected since
1387		 * unprotected frame would be received by the STA that is now
1388		 * trying to associate.
1389		 */
1390	}
1391#endif /* CONFIG_IEEE80211W */
1392
1393	if (reassoc) {
1394		os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1395			  ETH_ALEN);
1396	}
1397
1398	if (sta->last_assoc_req)
1399		os_free(sta->last_assoc_req);
1400	sta->last_assoc_req = os_malloc(len);
1401	if (sta->last_assoc_req)
1402		os_memcpy(sta->last_assoc_req, mgmt, len);
1403
1404	/* Make sure that the previously registered inactivity timer will not
1405	 * remove the STA immediately. */
1406	sta->timeout_next = STA_NULLFUNC;
1407
1408 fail:
1409	send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1410}
1411
1412
1413static void handle_disassoc(struct hostapd_data *hapd,
1414			    const struct ieee80211_mgmt *mgmt, size_t len)
1415{
1416	struct sta_info *sta;
1417
1418	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1419		wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
1420			   (unsigned long) len);
1421		return;
1422	}
1423
1424	wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1425		   MAC2STR(mgmt->sa),
1426		   le_to_host16(mgmt->u.disassoc.reason_code));
1427
1428	sta = ap_get_sta(hapd, mgmt->sa);
1429	if (sta == NULL) {
1430		wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
1431			   MAC2STR(mgmt->sa));
1432		return;
1433	}
1434
1435	ap_sta_set_authorized(hapd, sta, 0);
1436	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1437	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1438	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1439		       HOSTAPD_LEVEL_INFO, "disassociated");
1440	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1441	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1442	/* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1443	 * authenticated. */
1444	accounting_sta_stop(hapd, sta);
1445	ieee802_1x_free_station(sta);
1446	hostapd_drv_sta_remove(hapd, sta->addr);
1447
1448	if (sta->timeout_next == STA_NULLFUNC ||
1449	    sta->timeout_next == STA_DISASSOC) {
1450		sta->timeout_next = STA_DEAUTH;
1451		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1452		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1453				       hapd, sta);
1454	}
1455
1456	mlme_disassociate_indication(
1457		hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1458}
1459
1460
1461static void handle_deauth(struct hostapd_data *hapd,
1462			  const struct ieee80211_mgmt *mgmt, size_t len)
1463{
1464	struct sta_info *sta;
1465
1466	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1467		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1468			"payload (len=%lu)", (unsigned long) len);
1469		return;
1470	}
1471
1472	wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
1473		" reason_code=%d",
1474		MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1475
1476	sta = ap_get_sta(hapd, mgmt->sa);
1477	if (sta == NULL) {
1478		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1479			"to deauthenticate, but it is not authenticated",
1480			MAC2STR(mgmt->sa));
1481		return;
1482	}
1483
1484	ap_sta_set_authorized(hapd, sta, 0);
1485	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1486			WLAN_STA_ASSOC_REQ_OK);
1487	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1488	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1489		       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1490	mlme_deauthenticate_indication(
1491		hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1492	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1493	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1494	ap_free_sta(hapd, sta);
1495}
1496
1497
1498static void handle_beacon(struct hostapd_data *hapd,
1499			  const struct ieee80211_mgmt *mgmt, size_t len,
1500			  struct hostapd_frame_info *fi)
1501{
1502	struct ieee802_11_elems elems;
1503
1504	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1505		wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
1506			   (unsigned long) len);
1507		return;
1508	}
1509
1510	(void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1511				      len - (IEEE80211_HDRLEN +
1512					     sizeof(mgmt->u.beacon)), &elems,
1513				      0);
1514
1515	ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1516}
1517
1518
1519#ifdef CONFIG_IEEE80211W
1520
1521static int hostapd_sa_query_action(struct hostapd_data *hapd,
1522				   const struct ieee80211_mgmt *mgmt,
1523				   size_t len)
1524{
1525	const u8 *end;
1526
1527	end = mgmt->u.action.u.sa_query_resp.trans_id +
1528		WLAN_SA_QUERY_TR_ID_LEN;
1529	if (((u8 *) mgmt) + len < end) {
1530		wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1531			   "frame (len=%lu)", (unsigned long) len);
1532		return 0;
1533	}
1534
1535	ieee802_11_sa_query_action(hapd, mgmt->sa,
1536				   mgmt->u.action.u.sa_query_resp.action,
1537				   mgmt->u.action.u.sa_query_resp.trans_id);
1538	return 1;
1539}
1540
1541
1542static int robust_action_frame(u8 category)
1543{
1544	return category != WLAN_ACTION_PUBLIC &&
1545		category != WLAN_ACTION_HT;
1546}
1547#endif /* CONFIG_IEEE80211W */
1548
1549
1550static int handle_action(struct hostapd_data *hapd,
1551			 const struct ieee80211_mgmt *mgmt, size_t len)
1552{
1553	struct sta_info *sta;
1554	sta = ap_get_sta(hapd, mgmt->sa);
1555
1556	if (len < IEEE80211_HDRLEN + 1) {
1557		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1558			       HOSTAPD_LEVEL_DEBUG,
1559			       "handle_action - too short payload (len=%lu)",
1560			       (unsigned long) len);
1561		return 0;
1562	}
1563
1564	if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1565	    (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
1566		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
1567			   "frame (category=%u) from unassociated STA " MACSTR,
1568			   MAC2STR(mgmt->sa), mgmt->u.action.category);
1569		return 0;
1570	}
1571
1572#ifdef CONFIG_IEEE80211W
1573	if (sta && (sta->flags & WLAN_STA_MFP) &&
1574	    !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
1575	    robust_action_frame(mgmt->u.action.category)) {
1576		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1577			       HOSTAPD_LEVEL_DEBUG,
1578			       "Dropped unprotected Robust Action frame from "
1579			       "an MFP STA");
1580		return 0;
1581	}
1582#endif /* CONFIG_IEEE80211W */
1583
1584	switch (mgmt->u.action.category) {
1585#ifdef CONFIG_IEEE80211R
1586	case WLAN_ACTION_FT:
1587		if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1588				     len - IEEE80211_HDRLEN))
1589			break;
1590		return 1;
1591#endif /* CONFIG_IEEE80211R */
1592	case WLAN_ACTION_WMM:
1593		hostapd_wmm_action(hapd, mgmt, len);
1594		return 1;
1595#ifdef CONFIG_IEEE80211W
1596	case WLAN_ACTION_SA_QUERY:
1597		return hostapd_sa_query_action(hapd, mgmt, len);
1598#endif /* CONFIG_IEEE80211W */
1599#ifdef CONFIG_WNM
1600	case WLAN_ACTION_WNM:
1601		ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
1602		return 1;
1603#endif /* CONFIG_WNM */
1604	case WLAN_ACTION_PUBLIC:
1605	case WLAN_ACTION_PROTECTED_DUAL:
1606		if (hapd->public_action_cb) {
1607			hapd->public_action_cb(hapd->public_action_cb_ctx,
1608					       (u8 *) mgmt, len,
1609					       hapd->iface->freq);
1610		}
1611		if (hapd->public_action_cb2) {
1612			hapd->public_action_cb2(hapd->public_action_cb2_ctx,
1613						(u8 *) mgmt, len,
1614						hapd->iface->freq);
1615		}
1616		if (hapd->public_action_cb || hapd->public_action_cb2)
1617			return 1;
1618		break;
1619	case WLAN_ACTION_VENDOR_SPECIFIC:
1620		if (hapd->vendor_action_cb) {
1621			if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1622						   (u8 *) mgmt, len,
1623						   hapd->iface->freq) == 0)
1624				return 1;
1625		}
1626		break;
1627	}
1628
1629	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1630		       HOSTAPD_LEVEL_DEBUG,
1631		       "handle_action - unknown action category %d or invalid "
1632		       "frame",
1633		       mgmt->u.action.category);
1634	if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1635	    !(mgmt->sa[0] & 0x01)) {
1636		struct ieee80211_mgmt *resp;
1637
1638		/*
1639		 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1640		 * Return the Action frame to the source without change
1641		 * except that MSB of the Category set to 1.
1642		 */
1643		wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1644			   "frame back to sender");
1645		resp = os_malloc(len);
1646		if (resp == NULL)
1647			return 0;
1648		os_memcpy(resp, mgmt, len);
1649		os_memcpy(resp->da, resp->sa, ETH_ALEN);
1650		os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1651		os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1652		resp->u.action.category |= 0x80;
1653
1654		if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
1655			wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
1656				   "Action frame");
1657		}
1658		os_free(resp);
1659	}
1660
1661	return 1;
1662}
1663
1664
1665/**
1666 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1667 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1668 * sent to)
1669 * @buf: management frame data (starting from IEEE 802.11 header)
1670 * @len: length of frame data in octets
1671 * @fi: meta data about received frame (signal level, etc.)
1672 *
1673 * Process all incoming IEEE 802.11 management frames. This will be called for
1674 * each frame received from the kernel driver through wlan#ap interface. In
1675 * addition, it can be called to re-inserted pending frames (e.g., when using
1676 * external RADIUS server as an MAC ACL).
1677 */
1678int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1679		    struct hostapd_frame_info *fi)
1680{
1681	struct ieee80211_mgmt *mgmt;
1682	int broadcast;
1683	u16 fc, stype;
1684	int ret = 0;
1685
1686#ifdef CONFIG_TESTING_OPTIONS
1687	if (hapd->ext_mgmt_frame_handling) {
1688		size_t hex_len = 2 * len + 1;
1689		char *hex = os_malloc(hex_len);
1690		if (hex) {
1691			wpa_snprintf_hex(hex, hex_len, buf, len);
1692			wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1693			os_free(hex);
1694		}
1695		return 1;
1696	}
1697#endif /* CONFIG_TESTING_OPTIONS */
1698
1699	if (len < 24)
1700		return 0;
1701
1702	mgmt = (struct ieee80211_mgmt *) buf;
1703	fc = le_to_host16(mgmt->frame_control);
1704	stype = WLAN_FC_GET_STYPE(fc);
1705
1706	if (stype == WLAN_FC_STYPE_BEACON) {
1707		handle_beacon(hapd, mgmt, len, fi);
1708		return 1;
1709	}
1710
1711	broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1712		mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1713		mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1714
1715	if (!broadcast &&
1716#ifdef CONFIG_P2P
1717	    /* Invitation responses can be sent with the peer MAC as BSSID */
1718	    !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1719	      stype == WLAN_FC_STYPE_ACTION) &&
1720#endif /* CONFIG_P2P */
1721	    os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1722		wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
1723			   MAC2STR(mgmt->bssid));
1724		return 0;
1725	}
1726
1727
1728	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1729		handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
1730		return 1;
1731	}
1732
1733	if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1734		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1735			       HOSTAPD_LEVEL_DEBUG,
1736			       "MGMT: DA=" MACSTR " not our address",
1737			       MAC2STR(mgmt->da));
1738		return 0;
1739	}
1740
1741	switch (stype) {
1742	case WLAN_FC_STYPE_AUTH:
1743		wpa_printf(MSG_DEBUG, "mgmt::auth");
1744		handle_auth(hapd, mgmt, len);
1745		ret = 1;
1746		break;
1747	case WLAN_FC_STYPE_ASSOC_REQ:
1748		wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1749		handle_assoc(hapd, mgmt, len, 0);
1750		ret = 1;
1751		break;
1752	case WLAN_FC_STYPE_REASSOC_REQ:
1753		wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1754		handle_assoc(hapd, mgmt, len, 1);
1755		ret = 1;
1756		break;
1757	case WLAN_FC_STYPE_DISASSOC:
1758		wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1759		handle_disassoc(hapd, mgmt, len);
1760		ret = 1;
1761		break;
1762	case WLAN_FC_STYPE_DEAUTH:
1763		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
1764		handle_deauth(hapd, mgmt, len);
1765		ret = 1;
1766		break;
1767	case WLAN_FC_STYPE_ACTION:
1768		wpa_printf(MSG_DEBUG, "mgmt::action");
1769		ret = handle_action(hapd, mgmt, len);
1770		break;
1771	default:
1772		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1773			       HOSTAPD_LEVEL_DEBUG,
1774			       "unknown mgmt frame subtype %d", stype);
1775		break;
1776	}
1777
1778	return ret;
1779}
1780
1781
1782static void handle_auth_cb(struct hostapd_data *hapd,
1783			   const struct ieee80211_mgmt *mgmt,
1784			   size_t len, int ok)
1785{
1786	u16 auth_alg, auth_transaction, status_code;
1787	struct sta_info *sta;
1788
1789	if (!ok) {
1790		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1791			       HOSTAPD_LEVEL_NOTICE,
1792			       "did not acknowledge authentication response");
1793		return;
1794	}
1795
1796	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1797		wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
1798			   (unsigned long) len);
1799		return;
1800	}
1801
1802	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1803	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1804	status_code = le_to_host16(mgmt->u.auth.status_code);
1805
1806	sta = ap_get_sta(hapd, mgmt->da);
1807	if (!sta) {
1808		wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
1809			   MAC2STR(mgmt->da));
1810		return;
1811	}
1812
1813	if (status_code == WLAN_STATUS_SUCCESS &&
1814	    ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1815	     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1816		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1817			       HOSTAPD_LEVEL_INFO, "authenticated");
1818		sta->flags |= WLAN_STA_AUTH;
1819	}
1820}
1821
1822
1823static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
1824				       struct sta_info *sta,
1825				       char *ifname_wds)
1826{
1827	int i;
1828	struct hostapd_ssid *ssid = sta->ssid;
1829
1830	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
1831		return;
1832
1833	for (i = 0; i < 4; i++) {
1834		if (ssid->wep.key[i] &&
1835		    hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
1836					i == ssid->wep.idx, NULL, 0,
1837					ssid->wep.key[i], ssid->wep.len[i])) {
1838			wpa_printf(MSG_WARNING,
1839				   "Could not set WEP keys for WDS interface; %s",
1840				   ifname_wds);
1841			break;
1842		}
1843	}
1844}
1845
1846
1847static void handle_assoc_cb(struct hostapd_data *hapd,
1848			    const struct ieee80211_mgmt *mgmt,
1849			    size_t len, int reassoc, int ok)
1850{
1851	u16 status;
1852	struct sta_info *sta;
1853	int new_assoc = 1;
1854	struct ieee80211_ht_capabilities ht_cap;
1855	struct ieee80211_vht_capabilities vht_cap;
1856
1857	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1858				      sizeof(mgmt->u.assoc_resp))) {
1859		wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
1860			   reassoc, (unsigned long) len);
1861		return;
1862	}
1863
1864	sta = ap_get_sta(hapd, mgmt->da);
1865	if (!sta) {
1866		wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
1867			   MAC2STR(mgmt->da));
1868		return;
1869	}
1870
1871	if (!ok) {
1872		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1873			       HOSTAPD_LEVEL_DEBUG,
1874			       "did not acknowledge association response");
1875		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
1876		return;
1877	}
1878
1879	if (reassoc)
1880		status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1881	else
1882		status = le_to_host16(mgmt->u.assoc_resp.status_code);
1883
1884	if (status != WLAN_STATUS_SUCCESS)
1885		goto fail;
1886
1887	/* Stop previous accounting session, if one is started, and allocate
1888	 * new session id for the new session. */
1889	accounting_sta_stop(hapd, sta);
1890
1891	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1892		       HOSTAPD_LEVEL_INFO,
1893		       "associated (aid %d)",
1894		       sta->aid);
1895
1896	if (sta->flags & WLAN_STA_ASSOC)
1897		new_assoc = 0;
1898	sta->flags |= WLAN_STA_ASSOC;
1899	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
1900	if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1901	    sta->auth_alg == WLAN_AUTH_FT) {
1902		/*
1903		 * Open, static WEP, or FT protocol; no separate authorization
1904		 * step.
1905		 */
1906		ap_sta_set_authorized(hapd, sta, 1);
1907	}
1908
1909	if (reassoc)
1910		mlme_reassociate_indication(hapd, sta);
1911	else
1912		mlme_associate_indication(hapd, sta);
1913
1914#ifdef CONFIG_IEEE80211W
1915	sta->sa_query_timed_out = 0;
1916#endif /* CONFIG_IEEE80211W */
1917
1918	/*
1919	 * Remove the STA entry in order to make sure the STA PS state gets
1920	 * cleared and configuration gets updated in case of reassociation back
1921	 * to the same AP.
1922	 */
1923	hostapd_drv_sta_remove(hapd, sta->addr);
1924
1925#ifdef CONFIG_IEEE80211N
1926	if (sta->flags & WLAN_STA_HT)
1927		hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1928#endif /* CONFIG_IEEE80211N */
1929#ifdef CONFIG_IEEE80211AC
1930	if (sta->flags & WLAN_STA_VHT)
1931		hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
1932#endif /* CONFIG_IEEE80211AC */
1933
1934	if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1935			    sta->supported_rates, sta->supported_rates_len,
1936			    sta->listen_interval,
1937			    sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1938			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
1939			    sta->flags, sta->qosinfo)) {
1940		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1941			       HOSTAPD_LEVEL_NOTICE,
1942			       "Could not add STA to kernel driver");
1943
1944		ap_sta_disconnect(hapd, sta, sta->addr,
1945				  WLAN_REASON_DISASSOC_AP_BUSY);
1946
1947		goto fail;
1948	}
1949
1950	if (sta->flags & WLAN_STA_WDS) {
1951		int ret;
1952		char ifname_wds[IFNAMSIZ + 1];
1953
1954		ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
1955					  sta->aid, 1);
1956		if (!ret)
1957			hostapd_set_wds_encryption(hapd, sta, ifname_wds);
1958	}
1959
1960	if (sta->eapol_sm == NULL) {
1961		/*
1962		 * This STA does not use RADIUS server for EAP authentication,
1963		 * so bind it to the selected VLAN interface now, since the
1964		 * interface selection is not going to change anymore.
1965		 */
1966		if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1967			goto fail;
1968	} else if (sta->vlan_id) {
1969		/* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1970		if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1971			goto fail;
1972	}
1973
1974	hostapd_set_sta_flags(hapd, sta);
1975
1976	if (sta->auth_alg == WLAN_AUTH_FT)
1977		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1978	else
1979		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1980	hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1981
1982	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1983
1984 fail:
1985	/* Copy of the association request is not needed anymore */
1986	if (sta->last_assoc_req) {
1987		os_free(sta->last_assoc_req);
1988		sta->last_assoc_req = NULL;
1989	}
1990}
1991
1992
1993static void handle_deauth_cb(struct hostapd_data *hapd,
1994			     const struct ieee80211_mgmt *mgmt,
1995			     size_t len, int ok)
1996{
1997	struct sta_info *sta;
1998	if (mgmt->da[0] & 0x01)
1999		return;
2000	sta = ap_get_sta(hapd, mgmt->da);
2001	if (!sta) {
2002		wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2003			   " not found", MAC2STR(mgmt->da));
2004		return;
2005	}
2006	if (ok)
2007		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2008			   MAC2STR(sta->addr));
2009	else
2010		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2011			   "deauth", MAC2STR(sta->addr));
2012
2013	ap_sta_deauth_cb(hapd, sta);
2014}
2015
2016
2017static void handle_disassoc_cb(struct hostapd_data *hapd,
2018			       const struct ieee80211_mgmt *mgmt,
2019			       size_t len, int ok)
2020{
2021	struct sta_info *sta;
2022	if (mgmt->da[0] & 0x01)
2023		return;
2024	sta = ap_get_sta(hapd, mgmt->da);
2025	if (!sta) {
2026		wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2027			   " not found", MAC2STR(mgmt->da));
2028		return;
2029	}
2030	if (ok)
2031		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2032			   MAC2STR(sta->addr));
2033	else
2034		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2035			   "disassoc", MAC2STR(sta->addr));
2036
2037	ap_sta_disassoc_cb(hapd, sta);
2038}
2039
2040
2041/**
2042 * ieee802_11_mgmt_cb - Process management frame TX status callback
2043 * @hapd: hostapd BSS data structure (the BSS from which the management frame
2044 * was sent from)
2045 * @buf: management frame data (starting from IEEE 802.11 header)
2046 * @len: length of frame data in octets
2047 * @stype: management frame subtype from frame control field
2048 * @ok: Whether the frame was ACK'ed
2049 */
2050void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2051			u16 stype, int ok)
2052{
2053	const struct ieee80211_mgmt *mgmt;
2054	mgmt = (const struct ieee80211_mgmt *) buf;
2055
2056#ifdef CONFIG_TESTING_OPTIONS
2057	if (hapd->ext_mgmt_frame_handling) {
2058		wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2059			stype, ok);
2060		return;
2061	}
2062#endif /* CONFIG_TESTING_OPTIONS */
2063
2064	switch (stype) {
2065	case WLAN_FC_STYPE_AUTH:
2066		wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2067		handle_auth_cb(hapd, mgmt, len, ok);
2068		break;
2069	case WLAN_FC_STYPE_ASSOC_RESP:
2070		wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2071		handle_assoc_cb(hapd, mgmt, len, 0, ok);
2072		break;
2073	case WLAN_FC_STYPE_REASSOC_RESP:
2074		wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2075		handle_assoc_cb(hapd, mgmt, len, 1, ok);
2076		break;
2077	case WLAN_FC_STYPE_PROBE_RESP:
2078		wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2079		break;
2080	case WLAN_FC_STYPE_DEAUTH:
2081		wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2082		handle_deauth_cb(hapd, mgmt, len, ok);
2083		break;
2084	case WLAN_FC_STYPE_DISASSOC:
2085		wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2086		handle_disassoc_cb(hapd, mgmt, len, ok);
2087		break;
2088	case WLAN_FC_STYPE_ACTION:
2089		wpa_printf(MSG_DEBUG, "mgmt::action cb");
2090		break;
2091	default:
2092		wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2093		break;
2094	}
2095}
2096
2097
2098int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2099{
2100	/* TODO */
2101	return 0;
2102}
2103
2104
2105int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2106			   char *buf, size_t buflen)
2107{
2108	/* TODO */
2109	return 0;
2110}
2111
2112
2113void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2114		       const u8 *buf, size_t len, int ack)
2115{
2116	struct sta_info *sta;
2117	struct hostapd_iface *iface = hapd->iface;
2118
2119	sta = ap_get_sta(hapd, addr);
2120	if (sta == NULL && iface->num_bss > 1) {
2121		size_t j;
2122		for (j = 0; j < iface->num_bss; j++) {
2123			hapd = iface->bss[j];
2124			sta = ap_get_sta(hapd, addr);
2125			if (sta)
2126				break;
2127		}
2128	}
2129	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2130		return;
2131	if (sta->flags & WLAN_STA_PENDING_POLL) {
2132		wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2133			   "activity poll", MAC2STR(sta->addr),
2134			   ack ? "ACKed" : "did not ACK");
2135		if (ack)
2136			sta->flags &= ~WLAN_STA_PENDING_POLL;
2137	}
2138
2139	ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2140}
2141
2142
2143void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2144			     const u8 *data, size_t len, int ack)
2145{
2146	struct sta_info *sta;
2147	struct hostapd_iface *iface = hapd->iface;
2148
2149	sta = ap_get_sta(hapd, dst);
2150	if (sta == NULL && iface->num_bss > 1) {
2151		size_t j;
2152		for (j = 0; j < iface->num_bss; j++) {
2153			hapd = iface->bss[j];
2154			sta = ap_get_sta(hapd, dst);
2155			if (sta)
2156				break;
2157		}
2158	}
2159	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2160		wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2161			   MACSTR " that is not currently associated",
2162			   MAC2STR(dst));
2163		return;
2164	}
2165
2166	ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2167}
2168
2169
2170void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2171{
2172	struct sta_info *sta;
2173	struct hostapd_iface *iface = hapd->iface;
2174
2175	sta = ap_get_sta(hapd, addr);
2176	if (sta == NULL && iface->num_bss > 1) {
2177		size_t j;
2178		for (j = 0; j < iface->num_bss; j++) {
2179			hapd = iface->bss[j];
2180			sta = ap_get_sta(hapd, addr);
2181			if (sta)
2182				break;
2183		}
2184	}
2185	if (sta == NULL)
2186		return;
2187	if (!(sta->flags & WLAN_STA_PENDING_POLL))
2188		return;
2189
2190	wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2191		   "activity poll", MAC2STR(sta->addr));
2192	sta->flags &= ~WLAN_STA_PENDING_POLL;
2193}
2194
2195
2196void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2197				int wds)
2198{
2199	struct sta_info *sta;
2200
2201	sta = ap_get_sta(hapd, src);
2202	if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2203		if (!hapd->conf->wds_sta)
2204			return;
2205
2206		if (wds && !(sta->flags & WLAN_STA_WDS)) {
2207			int ret;
2208			char ifname_wds[IFNAMSIZ + 1];
2209
2210			wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2211				   "STA " MACSTR " (aid %u)",
2212				   MAC2STR(sta->addr), sta->aid);
2213			sta->flags |= WLAN_STA_WDS;
2214			ret = hostapd_set_wds_sta(hapd, ifname_wds,
2215						  sta->addr, sta->aid, 1);
2216			if (!ret)
2217				hostapd_set_wds_encryption(hapd, sta,
2218							   ifname_wds);
2219		}
2220		return;
2221	}
2222
2223	wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2224		   MACSTR, MAC2STR(src));
2225	if (src[0] & 0x01) {
2226		/* Broadcast bit set in SA?! Ignore the frame silently. */
2227		return;
2228	}
2229
2230	if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2231		wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2232			   "already been sent, but no TX status yet known - "
2233			   "ignore Class 3 frame issue with " MACSTR,
2234			   MAC2STR(src));
2235		return;
2236	}
2237
2238	if (sta && (sta->flags & WLAN_STA_AUTH))
2239		hostapd_drv_sta_disassoc(
2240			hapd, src,
2241			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2242	else
2243		hostapd_drv_sta_deauth(
2244			hapd, src,
2245			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2246}
2247
2248
2249#endif /* CONFIG_NATIVE_WINDOWS */
2250