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