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