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