ieee802_11.c revision fb79edc9df1f20461e90e478363d207348213d35
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
181void 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
196static 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
250static 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
290static 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
317static 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
349static 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
364static 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
387static 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
403static 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
433static 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
531static 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
743static 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
776static 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
797static 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
822static 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
851static 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
866static 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
1101static 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
1123static 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
1237static 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
1430static 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
1478static 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
1515static 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
1538static 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
1559static 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
1567static 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		if (hapd->public_action_cb) {
1623			hapd->public_action_cb(hapd->public_action_cb_ctx,
1624					       (u8 *) mgmt, len,
1625					       hapd->iface->freq);
1626		}
1627		if (hapd->public_action_cb2) {
1628			hapd->public_action_cb2(hapd->public_action_cb2_ctx,
1629						(u8 *) mgmt, len,
1630						hapd->iface->freq);
1631		}
1632		if (hapd->public_action_cb || hapd->public_action_cb2)
1633			return 1;
1634		break;
1635	case WLAN_ACTION_VENDOR_SPECIFIC:
1636		if (hapd->vendor_action_cb) {
1637			if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1638						   (u8 *) mgmt, len,
1639						   hapd->iface->freq) == 0)
1640				return 1;
1641		}
1642		break;
1643	}
1644
1645	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1646		       HOSTAPD_LEVEL_DEBUG,
1647		       "handle_action - unknown action category %d or invalid "
1648		       "frame",
1649		       mgmt->u.action.category);
1650	if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1651	    !(mgmt->sa[0] & 0x01)) {
1652		struct ieee80211_mgmt *resp;
1653
1654		/*
1655		 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1656		 * Return the Action frame to the source without change
1657		 * except that MSB of the Category set to 1.
1658		 */
1659		wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1660			   "frame back to sender");
1661		resp = os_malloc(len);
1662		if (resp == NULL)
1663			return 0;
1664		os_memcpy(resp, mgmt, len);
1665		os_memcpy(resp->da, resp->sa, ETH_ALEN);
1666		os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1667		os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1668		resp->u.action.category |= 0x80;
1669
1670		if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
1671			wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
1672				   "Action frame");
1673		}
1674		os_free(resp);
1675	}
1676
1677	return 1;
1678}
1679
1680
1681/**
1682 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1683 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1684 * sent to)
1685 * @buf: management frame data (starting from IEEE 802.11 header)
1686 * @len: length of frame data in octets
1687 * @fi: meta data about received frame (signal level, etc.)
1688 *
1689 * Process all incoming IEEE 802.11 management frames. This will be called for
1690 * each frame received from the kernel driver through wlan#ap interface. In
1691 * addition, it can be called to re-inserted pending frames (e.g., when using
1692 * external RADIUS server as an MAC ACL).
1693 */
1694int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1695		    struct hostapd_frame_info *fi)
1696{
1697	struct ieee80211_mgmt *mgmt;
1698	int broadcast;
1699	u16 fc, stype;
1700	int ret = 0;
1701
1702#ifdef CONFIG_TESTING_OPTIONS
1703	if (hapd->ext_mgmt_frame_handling) {
1704		size_t hex_len = 2 * len + 1;
1705		char *hex = os_malloc(hex_len);
1706		if (hex) {
1707			wpa_snprintf_hex(hex, hex_len, buf, len);
1708			wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1709			os_free(hex);
1710		}
1711		return 1;
1712	}
1713#endif /* CONFIG_TESTING_OPTIONS */
1714
1715	if (len < 24)
1716		return 0;
1717
1718	mgmt = (struct ieee80211_mgmt *) buf;
1719	fc = le_to_host16(mgmt->frame_control);
1720	stype = WLAN_FC_GET_STYPE(fc);
1721
1722	if (stype == WLAN_FC_STYPE_BEACON) {
1723		handle_beacon(hapd, mgmt, len, fi);
1724		return 1;
1725	}
1726
1727	broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1728		mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1729		mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1730
1731	if (!broadcast &&
1732#ifdef CONFIG_P2P
1733	    /* Invitation responses can be sent with the peer MAC as BSSID */
1734	    !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1735	      stype == WLAN_FC_STYPE_ACTION) &&
1736#endif /* CONFIG_P2P */
1737	    os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1738		wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
1739			   MAC2STR(mgmt->bssid));
1740		return 0;
1741	}
1742
1743
1744	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1745		handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
1746		return 1;
1747	}
1748
1749	if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1750		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1751			       HOSTAPD_LEVEL_DEBUG,
1752			       "MGMT: DA=" MACSTR " not our address",
1753			       MAC2STR(mgmt->da));
1754		return 0;
1755	}
1756
1757	switch (stype) {
1758	case WLAN_FC_STYPE_AUTH:
1759		wpa_printf(MSG_DEBUG, "mgmt::auth");
1760		handle_auth(hapd, mgmt, len);
1761		ret = 1;
1762		break;
1763	case WLAN_FC_STYPE_ASSOC_REQ:
1764		wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1765		handle_assoc(hapd, mgmt, len, 0);
1766		ret = 1;
1767		break;
1768	case WLAN_FC_STYPE_REASSOC_REQ:
1769		wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1770		handle_assoc(hapd, mgmt, len, 1);
1771		ret = 1;
1772		break;
1773	case WLAN_FC_STYPE_DISASSOC:
1774		wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1775		handle_disassoc(hapd, mgmt, len);
1776		ret = 1;
1777		break;
1778	case WLAN_FC_STYPE_DEAUTH:
1779		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
1780		handle_deauth(hapd, mgmt, len);
1781		ret = 1;
1782		break;
1783	case WLAN_FC_STYPE_ACTION:
1784		wpa_printf(MSG_DEBUG, "mgmt::action");
1785		ret = handle_action(hapd, mgmt, len);
1786		break;
1787	default:
1788		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1789			       HOSTAPD_LEVEL_DEBUG,
1790			       "unknown mgmt frame subtype %d", stype);
1791		break;
1792	}
1793
1794	return ret;
1795}
1796
1797
1798static void handle_auth_cb(struct hostapd_data *hapd,
1799			   const struct ieee80211_mgmt *mgmt,
1800			   size_t len, int ok)
1801{
1802	u16 auth_alg, auth_transaction, status_code;
1803	struct sta_info *sta;
1804
1805	if (!ok) {
1806		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1807			       HOSTAPD_LEVEL_NOTICE,
1808			       "did not acknowledge authentication response");
1809		return;
1810	}
1811
1812	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1813		wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
1814			   (unsigned long) len);
1815		return;
1816	}
1817
1818	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1819	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1820	status_code = le_to_host16(mgmt->u.auth.status_code);
1821
1822	sta = ap_get_sta(hapd, mgmt->da);
1823	if (!sta) {
1824		wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
1825			   MAC2STR(mgmt->da));
1826		return;
1827	}
1828
1829	if (status_code == WLAN_STATUS_SUCCESS &&
1830	    ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1831	     (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1832		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1833			       HOSTAPD_LEVEL_INFO, "authenticated");
1834		sta->flags |= WLAN_STA_AUTH;
1835	}
1836}
1837
1838
1839static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
1840				       struct sta_info *sta,
1841				       char *ifname_wds)
1842{
1843	int i;
1844	struct hostapd_ssid *ssid = sta->ssid;
1845
1846	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
1847		return;
1848
1849	for (i = 0; i < 4; i++) {
1850		if (ssid->wep.key[i] &&
1851		    hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
1852					i == ssid->wep.idx, NULL, 0,
1853					ssid->wep.key[i], ssid->wep.len[i])) {
1854			wpa_printf(MSG_WARNING,
1855				   "Could not set WEP keys for WDS interface; %s",
1856				   ifname_wds);
1857			break;
1858		}
1859	}
1860}
1861
1862
1863static void handle_assoc_cb(struct hostapd_data *hapd,
1864			    const struct ieee80211_mgmt *mgmt,
1865			    size_t len, int reassoc, int ok)
1866{
1867	u16 status;
1868	struct sta_info *sta;
1869	int new_assoc = 1;
1870	struct ieee80211_ht_capabilities ht_cap;
1871	struct ieee80211_vht_capabilities vht_cap;
1872
1873	if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1874				      sizeof(mgmt->u.assoc_resp))) {
1875		wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
1876			   reassoc, (unsigned long) len);
1877		return;
1878	}
1879
1880	sta = ap_get_sta(hapd, mgmt->da);
1881	if (!sta) {
1882		wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
1883			   MAC2STR(mgmt->da));
1884		return;
1885	}
1886
1887	if (!ok) {
1888		hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1889			       HOSTAPD_LEVEL_DEBUG,
1890			       "did not acknowledge association response");
1891		sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
1892		return;
1893	}
1894
1895	if (reassoc)
1896		status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1897	else
1898		status = le_to_host16(mgmt->u.assoc_resp.status_code);
1899
1900	if (status != WLAN_STATUS_SUCCESS)
1901		goto fail;
1902
1903	/* Stop previous accounting session, if one is started, and allocate
1904	 * new session id for the new session. */
1905	accounting_sta_stop(hapd, sta);
1906
1907	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1908		       HOSTAPD_LEVEL_INFO,
1909		       "associated (aid %d)",
1910		       sta->aid);
1911
1912	if (sta->flags & WLAN_STA_ASSOC)
1913		new_assoc = 0;
1914	sta->flags |= WLAN_STA_ASSOC;
1915	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
1916	if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1917	    sta->auth_alg == WLAN_AUTH_FT) {
1918		/*
1919		 * Open, static WEP, or FT protocol; no separate authorization
1920		 * step.
1921		 */
1922		ap_sta_set_authorized(hapd, sta, 1);
1923	}
1924
1925	if (reassoc)
1926		mlme_reassociate_indication(hapd, sta);
1927	else
1928		mlme_associate_indication(hapd, sta);
1929
1930#ifdef CONFIG_IEEE80211W
1931	sta->sa_query_timed_out = 0;
1932#endif /* CONFIG_IEEE80211W */
1933
1934	/*
1935	 * Remove the STA entry in order to make sure the STA PS state gets
1936	 * cleared and configuration gets updated in case of reassociation back
1937	 * to the same AP.
1938	 */
1939	hostapd_drv_sta_remove(hapd, sta->addr);
1940
1941#ifdef CONFIG_IEEE80211N
1942	if (sta->flags & WLAN_STA_HT)
1943		hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1944#endif /* CONFIG_IEEE80211N */
1945#ifdef CONFIG_IEEE80211AC
1946	if (sta->flags & WLAN_STA_VHT)
1947		hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
1948#endif /* CONFIG_IEEE80211AC */
1949
1950	if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1951			    sta->supported_rates, sta->supported_rates_len,
1952			    sta->listen_interval,
1953			    sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1954			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
1955			    sta->flags, sta->qosinfo)) {
1956		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1957			       HOSTAPD_LEVEL_NOTICE,
1958			       "Could not add STA to kernel driver");
1959
1960		ap_sta_disconnect(hapd, sta, sta->addr,
1961				  WLAN_REASON_DISASSOC_AP_BUSY);
1962
1963		goto fail;
1964	}
1965
1966	if (sta->flags & WLAN_STA_WDS) {
1967		int ret;
1968		char ifname_wds[IFNAMSIZ + 1];
1969
1970		ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
1971					  sta->aid, 1);
1972		if (!ret)
1973			hostapd_set_wds_encryption(hapd, sta, ifname_wds);
1974	}
1975
1976	if (sta->eapol_sm == NULL) {
1977		/*
1978		 * This STA does not use RADIUS server for EAP authentication,
1979		 * so bind it to the selected VLAN interface now, since the
1980		 * interface selection is not going to change anymore.
1981		 */
1982		if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1983			goto fail;
1984	} else if (sta->vlan_id) {
1985		/* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1986		if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1987			goto fail;
1988	}
1989
1990	hostapd_set_sta_flags(hapd, sta);
1991
1992	if (sta->auth_alg == WLAN_AUTH_FT)
1993		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1994	else
1995		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1996	hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1997
1998	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1999
2000 fail:
2001	/* Copy of the association request is not needed anymore */
2002	if (sta->last_assoc_req) {
2003		os_free(sta->last_assoc_req);
2004		sta->last_assoc_req = NULL;
2005	}
2006}
2007
2008
2009static void handle_deauth_cb(struct hostapd_data *hapd,
2010			     const struct ieee80211_mgmt *mgmt,
2011			     size_t len, int ok)
2012{
2013	struct sta_info *sta;
2014	if (mgmt->da[0] & 0x01)
2015		return;
2016	sta = ap_get_sta(hapd, mgmt->da);
2017	if (!sta) {
2018		wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2019			   " not found", MAC2STR(mgmt->da));
2020		return;
2021	}
2022	if (ok)
2023		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2024			   MAC2STR(sta->addr));
2025	else
2026		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2027			   "deauth", MAC2STR(sta->addr));
2028
2029	ap_sta_deauth_cb(hapd, sta);
2030}
2031
2032
2033static void handle_disassoc_cb(struct hostapd_data *hapd,
2034			       const struct ieee80211_mgmt *mgmt,
2035			       size_t len, int ok)
2036{
2037	struct sta_info *sta;
2038	if (mgmt->da[0] & 0x01)
2039		return;
2040	sta = ap_get_sta(hapd, mgmt->da);
2041	if (!sta) {
2042		wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2043			   " not found", MAC2STR(mgmt->da));
2044		return;
2045	}
2046	if (ok)
2047		wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2048			   MAC2STR(sta->addr));
2049	else
2050		wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2051			   "disassoc", MAC2STR(sta->addr));
2052
2053	ap_sta_disassoc_cb(hapd, sta);
2054}
2055
2056
2057/**
2058 * ieee802_11_mgmt_cb - Process management frame TX status callback
2059 * @hapd: hostapd BSS data structure (the BSS from which the management frame
2060 * was sent from)
2061 * @buf: management frame data (starting from IEEE 802.11 header)
2062 * @len: length of frame data in octets
2063 * @stype: management frame subtype from frame control field
2064 * @ok: Whether the frame was ACK'ed
2065 */
2066void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2067			u16 stype, int ok)
2068{
2069	const struct ieee80211_mgmt *mgmt;
2070	mgmt = (const struct ieee80211_mgmt *) buf;
2071
2072#ifdef CONFIG_TESTING_OPTIONS
2073	if (hapd->ext_mgmt_frame_handling) {
2074		wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2075			stype, ok);
2076		return;
2077	}
2078#endif /* CONFIG_TESTING_OPTIONS */
2079
2080	switch (stype) {
2081	case WLAN_FC_STYPE_AUTH:
2082		wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2083		handle_auth_cb(hapd, mgmt, len, ok);
2084		break;
2085	case WLAN_FC_STYPE_ASSOC_RESP:
2086		wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2087		handle_assoc_cb(hapd, mgmt, len, 0, ok);
2088		break;
2089	case WLAN_FC_STYPE_REASSOC_RESP:
2090		wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2091		handle_assoc_cb(hapd, mgmt, len, 1, ok);
2092		break;
2093	case WLAN_FC_STYPE_PROBE_RESP:
2094		wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2095		break;
2096	case WLAN_FC_STYPE_DEAUTH:
2097		wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2098		handle_deauth_cb(hapd, mgmt, len, ok);
2099		break;
2100	case WLAN_FC_STYPE_DISASSOC:
2101		wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2102		handle_disassoc_cb(hapd, mgmt, len, ok);
2103		break;
2104	case WLAN_FC_STYPE_ACTION:
2105		wpa_printf(MSG_DEBUG, "mgmt::action cb");
2106		break;
2107	default:
2108		wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2109		break;
2110	}
2111}
2112
2113
2114int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2115{
2116	/* TODO */
2117	return 0;
2118}
2119
2120
2121int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2122			   char *buf, size_t buflen)
2123{
2124	/* TODO */
2125	return 0;
2126}
2127
2128
2129void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2130		       const u8 *buf, size_t len, int ack)
2131{
2132	struct sta_info *sta;
2133	struct hostapd_iface *iface = hapd->iface;
2134
2135	sta = ap_get_sta(hapd, addr);
2136	if (sta == NULL && iface->num_bss > 1) {
2137		size_t j;
2138		for (j = 0; j < iface->num_bss; j++) {
2139			hapd = iface->bss[j];
2140			sta = ap_get_sta(hapd, addr);
2141			if (sta)
2142				break;
2143		}
2144	}
2145	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2146		return;
2147	if (sta->flags & WLAN_STA_PENDING_POLL) {
2148		wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2149			   "activity poll", MAC2STR(sta->addr),
2150			   ack ? "ACKed" : "did not ACK");
2151		if (ack)
2152			sta->flags &= ~WLAN_STA_PENDING_POLL;
2153	}
2154
2155	ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2156}
2157
2158
2159void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2160			     const u8 *data, size_t len, int ack)
2161{
2162	struct sta_info *sta;
2163	struct hostapd_iface *iface = hapd->iface;
2164
2165	sta = ap_get_sta(hapd, dst);
2166	if (sta == NULL && iface->num_bss > 1) {
2167		size_t j;
2168		for (j = 0; j < iface->num_bss; j++) {
2169			hapd = iface->bss[j];
2170			sta = ap_get_sta(hapd, dst);
2171			if (sta)
2172				break;
2173		}
2174	}
2175	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2176		wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2177			   MACSTR " that is not currently associated",
2178			   MAC2STR(dst));
2179		return;
2180	}
2181
2182	ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2183}
2184
2185
2186void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2187{
2188	struct sta_info *sta;
2189	struct hostapd_iface *iface = hapd->iface;
2190
2191	sta = ap_get_sta(hapd, addr);
2192	if (sta == NULL && iface->num_bss > 1) {
2193		size_t j;
2194		for (j = 0; j < iface->num_bss; j++) {
2195			hapd = iface->bss[j];
2196			sta = ap_get_sta(hapd, addr);
2197			if (sta)
2198				break;
2199		}
2200	}
2201	if (sta == NULL)
2202		return;
2203	if (!(sta->flags & WLAN_STA_PENDING_POLL))
2204		return;
2205
2206	wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2207		   "activity poll", MAC2STR(sta->addr));
2208	sta->flags &= ~WLAN_STA_PENDING_POLL;
2209}
2210
2211
2212void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2213				int wds)
2214{
2215	struct sta_info *sta;
2216
2217	sta = ap_get_sta(hapd, src);
2218	if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2219		if (!hapd->conf->wds_sta)
2220			return;
2221
2222		if (wds && !(sta->flags & WLAN_STA_WDS)) {
2223			int ret;
2224			char ifname_wds[IFNAMSIZ + 1];
2225
2226			wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2227				   "STA " MACSTR " (aid %u)",
2228				   MAC2STR(sta->addr), sta->aid);
2229			sta->flags |= WLAN_STA_WDS;
2230			ret = hostapd_set_wds_sta(hapd, ifname_wds,
2231						  sta->addr, sta->aid, 1);
2232			if (!ret)
2233				hostapd_set_wds_encryption(hapd, sta,
2234							   ifname_wds);
2235		}
2236		return;
2237	}
2238
2239	wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2240		   MACSTR, MAC2STR(src));
2241	if (src[0] & 0x01) {
2242		/* Broadcast bit set in SA?! Ignore the frame silently. */
2243		return;
2244	}
2245
2246	if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2247		wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2248			   "already been sent, but no TX status yet known - "
2249			   "ignore Class 3 frame issue with " MACSTR,
2250			   MAC2STR(src));
2251		return;
2252	}
2253
2254	if (sta && (sta->flags & WLAN_STA_AUTH))
2255		hostapd_drv_sta_disassoc(
2256			hapd, src,
2257			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2258	else
2259		hostapd_drv_sta_deauth(
2260			hapd, src,
2261			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2262}
2263
2264
2265#endif /* CONFIG_NATIVE_WINDOWS */
2266