blob: 97f98f28e0b86aa7655a926b6b646a5ef3f6e6d6 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / IEEE 802.11 Management
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080016#include "crypto/sha256.h"
17#include "crypto/random.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "common/ieee802_11_defs.h"
19#include "common/ieee802_11_common.h"
20#include "common/wpa_ctrl.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080021#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#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"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080032#include "pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#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"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080040#include "wnm_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "ieee802_11.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080042#include "dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
44
45u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
46{
47 u8 *pos = eid;
48 int i, num, count;
49
50 if (hapd->iface->current_rates == NULL)
51 return eid;
52
53 *pos++ = WLAN_EID_SUPP_RATES;
54 num = hapd->iface->num_rates;
55 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
56 num++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080057 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
58 num++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 if (num > 8) {
60 /* rest of the rates are encoded in Extended supported
61 * rates element */
62 num = 8;
63 }
64
65 *pos++ = num;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066 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
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080075 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
76 count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080078 }
79
80 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
81 count++;
82 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
83 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084
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++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800100 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
101 num++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 if (num <= 8)
103 return eid;
104 num -= 8;
105
106 *pos++ = WLAN_EID_EXT_SUPP_RATES;
107 *pos++ = num;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 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
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800119 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130
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;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800140 int dfs;
141
142 /* Check if any of configured channels require DFS */
143 dfs = hostapd_is_dfs_required(hapd->iface);
144 if (dfs < 0) {
145 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
146 dfs);
147 dfs = 0;
148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149
150 if (hapd->iface->num_sta_no_short_preamble == 0 &&
151 hapd->iconf->preamble == SHORT_PREAMBLE)
152 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
153
154 privacy = hapd->conf->ssid.wep.keys_set;
155
156 if (hapd->conf->ieee802_1x &&
157 (hapd->conf->default_wep_key_len ||
158 hapd->conf->individual_wep_key_len))
159 privacy = 1;
160
161 if (hapd->conf->wpa)
162 privacy = 1;
163
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800164#ifdef CONFIG_HS20
165 if (hapd->conf->osen)
166 privacy = 1;
167#endif /* CONFIG_HS20 */
168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 if (sta) {
170 int policy, def_klen;
171 if (probe && sta->ssid_probe) {
172 policy = sta->ssid_probe->security_policy;
173 def_klen = sta->ssid_probe->wep.default_len;
174 } else {
175 policy = sta->ssid->security_policy;
176 def_klen = sta->ssid->wep.default_len;
177 }
178 privacy = policy != SECURITY_PLAINTEXT;
179 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
180 privacy = 0;
181 }
182
183 if (privacy)
184 capab |= WLAN_CAPABILITY_PRIVACY;
185
186 if (hapd->iface->current_mode &&
187 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
188 hapd->iface->num_sta_no_short_slot_time == 0)
189 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
190
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800191 /*
192 * Currently, Spectrum Management capability bit is set when directly
193 * requested in configuration by spectrum_mgmt_required or when AP is
194 * running on DFS channel.
195 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
196 */
197 if (hapd->iface->current_mode &&
198 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
199 (hapd->iconf->spectrum_mgmt_required || dfs))
200 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202 if (hapd->conf->radio_measurements)
203 capab |= IEEE80211_CAP_RRM;
204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 return capab;
206}
207
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
210 u16 auth_transaction, const u8 *challenge,
211 int iswep)
212{
213 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
214 HOSTAPD_LEVEL_DEBUG,
215 "authentication (shared key, transaction %d)",
216 auth_transaction);
217
218 if (auth_transaction == 1) {
219 if (!sta->challenge) {
220 /* Generate a pseudo-random challenge */
221 u8 key[8];
222 struct os_time now;
223 int r;
224 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
225 if (sta->challenge == NULL)
226 return WLAN_STATUS_UNSPECIFIED_FAILURE;
227
228 os_get_time(&now);
229 r = os_random();
230 os_memcpy(key, &now.sec, 4);
231 os_memcpy(key + 4, &r, 4);
232 rc4_skip(key, sizeof(key), 0,
233 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
234 }
235 return 0;
236 }
237
238 if (auth_transaction != 3)
239 return WLAN_STATUS_UNSPECIFIED_FAILURE;
240
241 /* Transaction 3 */
242 if (!iswep || !sta->challenge || !challenge ||
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700243 os_memcmp_const(sta->challenge, challenge,
244 WLAN_AUTH_CHALLENGE_LEN)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
246 HOSTAPD_LEVEL_INFO,
247 "shared key authentication - invalid "
248 "challenge-response");
249 return WLAN_STATUS_CHALLENGE_FAIL;
250 }
251
252 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
253 HOSTAPD_LEVEL_DEBUG,
254 "authentication OK (shared key)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 sta->flags |= WLAN_STA_AUTH;
256 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 os_free(sta->challenge);
258 sta->challenge = NULL;
259
260 return 0;
261}
262
263
264static void send_auth_reply(struct hostapd_data *hapd,
265 const u8 *dst, const u8 *bssid,
266 u16 auth_alg, u16 auth_transaction, u16 resp,
267 const u8 *ies, size_t ies_len)
268{
269 struct ieee80211_mgmt *reply;
270 u8 *buf;
271 size_t rlen;
272
273 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
274 buf = os_zalloc(rlen);
275 if (buf == NULL)
276 return;
277
278 reply = (struct ieee80211_mgmt *) buf;
279 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
280 WLAN_FC_STYPE_AUTH);
281 os_memcpy(reply->da, dst, ETH_ALEN);
282 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
283 os_memcpy(reply->bssid, bssid, ETH_ALEN);
284
285 reply->u.auth.auth_alg = host_to_le16(auth_alg);
286 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
287 reply->u.auth.status_code = host_to_le16(resp);
288
289 if (ies && ies_len)
290 os_memcpy(reply->u.auth.variable, ies, ies_len);
291
292 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
293 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
294 MAC2STR(dst), auth_alg, auth_transaction,
295 resp, (unsigned long) ies_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800297 wpa_printf(MSG_INFO, "send_auth_reply: send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298
299 os_free(buf);
300}
301
302
303#ifdef CONFIG_IEEE80211R
304static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
305 u16 auth_transaction, u16 status,
306 const u8 *ies, size_t ies_len)
307{
308 struct hostapd_data *hapd = ctx;
309 struct sta_info *sta;
310
311 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
312 status, ies, ies_len);
313
314 if (status != WLAN_STATUS_SUCCESS)
315 return;
316
317 sta = ap_get_sta(hapd, dst);
318 if (sta == NULL)
319 return;
320
321 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
322 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
323 sta->flags |= WLAN_STA_AUTH;
324 mlme_authenticate_indication(hapd, sta);
325}
326#endif /* CONFIG_IEEE80211R */
327
328
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329#ifdef CONFIG_SAE
330
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800331static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
332 struct sta_info *sta, int update)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800333{
334 struct wpabuf *buf;
335
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800336 if (hapd->conf->ssid.wpa_passphrase == NULL) {
337 wpa_printf(MSG_DEBUG, "SAE: No password available");
338 return NULL;
339 }
340
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800341 if (update &&
342 sae_prepare_commit(hapd->own_addr, sta->addr,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800343 (u8 *) hapd->conf->ssid.wpa_passphrase,
344 os_strlen(hapd->conf->ssid.wpa_passphrase),
345 sta->sae) < 0) {
346 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
347 return NULL;
348 }
349
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800350 buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800351 if (buf == NULL)
352 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800353 sae_write_commit(sta->sae, buf, sta->sae->tmp ?
354 sta->sae->tmp->anti_clogging_token : NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800355
356 return buf;
357}
358
359
360static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
361 struct sta_info *sta)
362{
363 struct wpabuf *buf;
364
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800365 buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800366 if (buf == NULL)
367 return NULL;
368
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800369 sae_write_confirm(sta->sae, buf);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800370
371 return buf;
372}
373
374
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800375static int auth_sae_send_commit(struct hostapd_data *hapd,
376 struct sta_info *sta,
377 const u8 *bssid, int update)
378{
379 struct wpabuf *data;
380
381 data = auth_build_sae_commit(hapd, sta, update);
382 if (data == NULL)
383 return WLAN_STATUS_UNSPECIFIED_FAILURE;
384
385 send_auth_reply(hapd, sta->addr, bssid,
386 WLAN_AUTH_SAE, 1, WLAN_STATUS_SUCCESS,
387 wpabuf_head(data), wpabuf_len(data));
388
389 wpabuf_free(data);
390
391 return WLAN_STATUS_SUCCESS;
392}
393
394
395static int auth_sae_send_confirm(struct hostapd_data *hapd,
396 struct sta_info *sta,
397 const u8 *bssid)
398{
399 struct wpabuf *data;
400
401 data = auth_build_sae_confirm(hapd, sta);
402 if (data == NULL)
403 return WLAN_STATUS_UNSPECIFIED_FAILURE;
404
405 send_auth_reply(hapd, sta->addr, bssid,
406 WLAN_AUTH_SAE, 2, WLAN_STATUS_SUCCESS,
407 wpabuf_head(data), wpabuf_len(data));
408
409 wpabuf_free(data);
410
411 return WLAN_STATUS_SUCCESS;
412}
413
414
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800415static int use_sae_anti_clogging(struct hostapd_data *hapd)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800416{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800417 struct sta_info *sta;
418 unsigned int open = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800419
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800420 if (hapd->conf->sae_anti_clogging_threshold == 0)
421 return 1;
422
423 for (sta = hapd->sta_list; sta; sta = sta->next) {
424 if (!sta->sae)
425 continue;
426 if (sta->sae->state != SAE_COMMITTED &&
427 sta->sae->state != SAE_CONFIRMED)
428 continue;
429 open++;
430 if (open >= hapd->conf->sae_anti_clogging_threshold)
431 return 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800432 }
433
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800434 return 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800435}
436
437
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800438static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
439 const u8 *token, size_t token_len)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800440{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800441 u8 mac[SHA256_MAC_LEN];
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800442
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800443 if (token_len != SHA256_MAC_LEN)
444 return -1;
445 if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
446 addr, ETH_ALEN, mac) < 0 ||
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700447 os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800448 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800449
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800450 return 0;
451}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800452
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800453
454static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800455 int group, const u8 *addr)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800456{
457 struct wpabuf *buf;
458 u8 *token;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800459 struct os_reltime now;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800460
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800461 os_get_reltime(&now);
462 if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
463 os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800464 if (random_get_bytes(hapd->sae_token_key,
465 sizeof(hapd->sae_token_key)) < 0)
466 return NULL;
467 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
468 hapd->sae_token_key, sizeof(hapd->sae_token_key));
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800469 hapd->last_sae_token_key_update = now;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800470 }
471
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800472 buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800473 if (buf == NULL)
474 return NULL;
475
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800476 wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
477
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800478 token = wpabuf_put(buf, SHA256_MAC_LEN);
479 hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
480 addr, ETH_ALEN, token);
481
482 return buf;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800483}
484
485
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800486static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
487 const u8 *bssid, u8 auth_transaction)
488{
489 int ret;
490
491 if (auth_transaction != 1 && auth_transaction != 2)
492 return WLAN_STATUS_UNSPECIFIED_FAILURE;
493
494 switch (sta->sae->state) {
495 case SAE_NOTHING:
496 if (auth_transaction == 1) {
497 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
498 if (ret)
499 return ret;
500 sta->sae->state = SAE_COMMITTED;
501
502 if (sae_process_commit(sta->sae) < 0)
503 return WLAN_STATUS_UNSPECIFIED_FAILURE;
504
505 /*
506 * In mesh case, both Commit and Confirm can be sent
507 * immediately. In infrastructure BSS, only a single
508 * Authentication frame (Commit) is expected from the AP
509 * here and the second one (Confirm) will be sent once
510 * the STA has sent its second Authentication frame
511 * (Confirm).
512 */
513 if (hapd->conf->mesh & MESH_ENABLED) {
514 /*
515 * Send both Commit and Confirm immediately
516 * based on SAE finite state machine
517 * Nothing -> Confirm transition.
518 */
519 ret = auth_sae_send_confirm(hapd, sta, bssid);
520 if (ret)
521 return ret;
522 sta->sae->state = SAE_CONFIRMED;
523 } else {
524 /*
525 * For infrastructure BSS, send only the Commit
526 * message now to get alternating sequence of
527 * Authentication frames between the AP and STA.
528 * Confirm will be sent in
529 * Commited -> Confirmed/Accepted transition
530 * when receiving Confirm from STA.
531 */
532 }
533 } else {
534 hostapd_logger(hapd, sta->addr,
535 HOSTAPD_MODULE_IEEE80211,
536 HOSTAPD_LEVEL_DEBUG,
537 "SAE confirm before commit");
538 }
539 break;
540 case SAE_COMMITTED:
541 if (auth_transaction == 1) {
542 if (sae_process_commit(sta->sae) < 0)
543 return WLAN_STATUS_UNSPECIFIED_FAILURE;
544
545 ret = auth_sae_send_confirm(hapd, sta, bssid);
546 if (ret)
547 return ret;
548 sta->sae->state = SAE_CONFIRMED;
549 } else if (hapd->conf->mesh & MESH_ENABLED) {
550 /*
551 * In mesh case, follow SAE finite state machine and
552 * send Commit now.
553 */
554 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
555 if (ret)
556 return ret;
557 } else {
558 /*
559 * For instructure BSS, send the postponed Confirm from
560 * Nothing -> Confirmed transition that was reduced to
561 * Nothing -> Committed above.
562 */
563 ret = auth_sae_send_confirm(hapd, sta, bssid);
564 if (ret)
565 return ret;
566
567 sta->sae->state = SAE_CONFIRMED;
568
569 /*
570 * Since this was triggered on Confirm RX, run another
571 * step to get to Accepted without waiting for
572 * additional events.
573 */
574 return sae_sm_step(hapd, sta, bssid, auth_transaction);
575 }
576 break;
577 case SAE_CONFIRMED:
578 if (auth_transaction == 1) {
579 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
580 if (ret)
581 return ret;
582
583 if (sae_process_commit(sta->sae) < 0)
584 return WLAN_STATUS_UNSPECIFIED_FAILURE;
585
586 ret = auth_sae_send_confirm(hapd, sta, bssid);
587 if (ret)
588 return ret;
589 } else {
590 sta->flags |= WLAN_STA_AUTH;
591 sta->auth_alg = WLAN_AUTH_SAE;
592 mlme_authenticate_indication(hapd, sta);
593 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
594 sta->sae->state = SAE_ACCEPTED;
595 wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
596 sta->sae->pmk);
597 }
598 break;
599 case SAE_ACCEPTED:
600 if (auth_transaction == 1) {
601 wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
602 ") doing reauthentication",
603 MAC2STR(sta->addr));
604 ap_free_sta(hapd, sta);
605 } else {
606 ret = auth_sae_send_confirm(hapd, sta, bssid);
607 sae_clear_temp_data(sta->sae);
608 if (ret)
609 return ret;
610 }
611 break;
612 default:
613 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
614 sta->sae->state);
615 return WLAN_STATUS_UNSPECIFIED_FAILURE;
616 }
617 return WLAN_STATUS_SUCCESS;
618}
619
620
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800621static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
622 const struct ieee80211_mgmt *mgmt, size_t len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800623 u16 auth_transaction, u16 status_code)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800624{
625 u16 resp = WLAN_STATUS_SUCCESS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800626 struct wpabuf *data = NULL;
627
628 if (!sta->sae) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800629 if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800630 return;
631 sta->sae = os_zalloc(sizeof(*sta->sae));
632 if (sta->sae == NULL)
633 return;
634 sta->sae->state = SAE_NOTHING;
635 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800636
637 if (auth_transaction == 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800638 const u8 *token = NULL, *pos, *end;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800639 size_t token_len = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800640 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
641 HOSTAPD_LEVEL_DEBUG,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800642 "start SAE authentication (RX commit, status=%u)",
643 status_code);
644
645 if ((hapd->conf->mesh & MESH_ENABLED) &&
646 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
647 sta->sae->tmp) {
648 pos = mgmt->u.auth.variable;
649 end = ((const u8 *) mgmt) + len;
650 if (pos + sizeof(le16) > end) {
651 wpa_printf(MSG_ERROR,
652 "SAE: Too short anti-clogging token request");
653 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
654 goto reply;
655 }
656 resp = sae_group_allowed(sta->sae,
657 hapd->conf->sae_groups,
658 WPA_GET_LE16(pos));
659 if (resp != WLAN_STATUS_SUCCESS) {
660 wpa_printf(MSG_ERROR,
661 "SAE: Invalid group in anti-clogging token request");
662 goto reply;
663 }
664 pos += sizeof(le16);
665
666 wpabuf_free(sta->sae->tmp->anti_clogging_token);
667 sta->sae->tmp->anti_clogging_token =
668 wpabuf_alloc_copy(pos, end - pos);
669 if (sta->sae->tmp->anti_clogging_token == NULL) {
670 wpa_printf(MSG_ERROR,
671 "SAE: Failed to alloc for anti-clogging token");
672 return;
673 }
674
675 /*
676 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
677 * is 76, a new Commit Message shall be constructed
678 * with the Anti-Clogging Token from the received
679 * Authentication frame, and the commit-scalar and
680 * COMMIT-ELEMENT previously sent.
681 */
682 if (auth_sae_send_commit(hapd, sta, mgmt->bssid, 0)) {
683 wpa_printf(MSG_ERROR,
684 "SAE: Failed to send commit message");
685 return;
686 }
687 sta->sae->state = SAE_COMMITTED;
688 return;
689 }
690
691 if (status_code != WLAN_STATUS_SUCCESS)
692 return;
693
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800694 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
695 ((const u8 *) mgmt) + len -
696 mgmt->u.auth.variable, &token,
697 &token_len, hapd->conf->sae_groups);
698 if (token && check_sae_token(hapd, sta->addr, token, token_len)
699 < 0) {
700 wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
701 "incorrect token from " MACSTR,
702 MAC2STR(sta->addr));
703 return;
704 }
705
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800706 if (resp != WLAN_STATUS_SUCCESS)
707 goto reply;
708
709 if (!token && use_sae_anti_clogging(hapd)) {
710 wpa_printf(MSG_DEBUG,
711 "SAE: Request anti-clogging token from "
712 MACSTR, MAC2STR(sta->addr));
713 data = auth_build_token_req(hapd, sta->sae->group,
714 sta->addr);
715 resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
716 if (hapd->conf->mesh & MESH_ENABLED)
717 sta->sae->state = SAE_NOTHING;
718 goto reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800719 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800720
721 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800722 } else if (auth_transaction == 2) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800723 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
724 HOSTAPD_LEVEL_DEBUG,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800725 "SAE authentication (RX confirm, status=%u)",
726 status_code);
727 if (status_code != WLAN_STATUS_SUCCESS)
728 return;
729 if (sta->sae->state >= SAE_CONFIRMED ||
730 !(hapd->conf->mesh & MESH_ENABLED)) {
731 if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
732 ((u8 *) mgmt) + len -
733 mgmt->u.auth.variable) < 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800734 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800735 goto reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800736 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800737 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800738 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800739 } else {
740 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
741 HOSTAPD_LEVEL_DEBUG,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800742 "unexpected SAE authentication transaction %u (status=%u)",
743 auth_transaction, status_code);
744 if (status_code != WLAN_STATUS_SUCCESS)
745 return;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800746 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
747 }
748
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800749reply:
750 if (resp != WLAN_STATUS_SUCCESS) {
751 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
752 auth_transaction, resp,
753 data ? wpabuf_head(data) : (u8 *) "",
754 data ? wpabuf_len(data) : 0);
755 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800756 wpabuf_free(data);
757}
758#endif /* CONFIG_SAE */
759
760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761static void handle_auth(struct hostapd_data *hapd,
762 const struct ieee80211_mgmt *mgmt, size_t len)
763{
764 u16 auth_alg, auth_transaction, status_code;
765 u16 resp = WLAN_STATUS_SUCCESS;
766 struct sta_info *sta = NULL;
767 int res;
768 u16 fc;
769 const u8 *challenge = NULL;
770 u32 session_timeout, acct_interim_interval;
771 int vlan_id = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800772 struct hostapd_sta_wpa_psk_short *psk = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
774 size_t resp_ies_len = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700775 char *identity = NULL;
776 char *radius_cui = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800777 u16 seq_ctrl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778
779 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800780 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
781 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 return;
783 }
784
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700785#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700786 if (hapd->iconf->ignore_auth_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700787 drand48() < hapd->iconf->ignore_auth_probability) {
788 wpa_printf(MSG_INFO,
789 "TESTING: ignoring auth frame from " MACSTR,
790 MAC2STR(mgmt->sa));
791 return;
792 }
793#endif /* CONFIG_TESTING_OPTIONS */
794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
796 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
797 status_code = le_to_host16(mgmt->u.auth.status_code);
798 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800799 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800
801 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
802 2 + WLAN_AUTH_CHALLENGE_LEN &&
803 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
804 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
805 challenge = &mgmt->u.auth.variable[2];
806
807 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800808 "auth_transaction=%d status_code=%d wep=%d%s "
809 "seq_ctrl=0x%x%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
811 status_code, !!(fc & WLAN_FC_ISWEP),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800812 challenge ? " challenge" : "",
813 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814
815 if (hapd->tkip_countermeasures) {
816 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
817 goto fail;
818 }
819
820 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
821 auth_alg == WLAN_AUTH_OPEN) ||
822#ifdef CONFIG_IEEE80211R
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 auth_alg == WLAN_AUTH_FT) ||
825#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800826#ifdef CONFIG_SAE
827 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
828 auth_alg == WLAN_AUTH_SAE) ||
829#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
831 auth_alg == WLAN_AUTH_SHARED_KEY))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800832 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
833 auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
835 goto fail;
836 }
837
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800838 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800840 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
841 auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
843 goto fail;
844 }
845
846 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800847 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
848 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
850 goto fail;
851 }
852
853 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
854 &session_timeout,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800855 &acct_interim_interval, &vlan_id,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800856 &psk, &identity, &radius_cui);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 if (res == HOSTAPD_ACL_REJECT) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800859 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
860 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
862 goto fail;
863 }
864 if (res == HOSTAPD_ACL_PENDING) {
865 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
866 " waiting for an external authentication",
867 MAC2STR(mgmt->sa));
868 /* Authentication code will re-send the authentication frame
869 * after it has received (and cached) information from the
870 * external source. */
871 return;
872 }
873
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800874 sta = ap_get_sta(hapd, mgmt->sa);
875 if (sta) {
876 if ((fc & WLAN_FC_RETRY) &&
877 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
878 sta->last_seq_ctrl == seq_ctrl &&
879 sta->last_subtype == WLAN_FC_STYPE_AUTH) {
880 hostapd_logger(hapd, sta->addr,
881 HOSTAPD_MODULE_IEEE80211,
882 HOSTAPD_LEVEL_DEBUG,
883 "Drop repeated authentication frame seq_ctrl=0x%x",
884 seq_ctrl);
885 return;
886 }
887 } else {
888#ifdef CONFIG_MESH
889 if (hapd->conf->mesh & MESH_ENABLED) {
890 /* if the mesh peer is not available, we don't do auth.
891 */
892 wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
893 " not yet known - drop Authentiation frame",
894 MAC2STR(mgmt->sa));
895 /*
896 * Save a copy of the frame so that it can be processed
897 * if a new peer entry is added shortly after this.
898 */
899 wpabuf_free(hapd->mesh_pending_auth);
900 hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
901 os_get_reltime(&hapd->mesh_pending_auth_time);
902 return;
903 }
904#endif /* CONFIG_MESH */
905
906 sta = ap_sta_add(hapd, mgmt->sa);
907 if (!sta) {
908 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
909 goto fail;
910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800912 sta->last_seq_ctrl = seq_ctrl;
913 sta->last_subtype = WLAN_FC_STYPE_AUTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914
915 if (vlan_id > 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700916 if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
918 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
919 "%d received from RADIUS server",
920 vlan_id);
921 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
922 goto fail;
923 }
924 sta->vlan_id = vlan_id;
925 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
926 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
927 }
928
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800929 hostapd_free_psk_list(sta->psk);
930 if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
931 sta->psk = psk;
932 psk = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934 sta->psk = NULL;
935 }
936
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700937 sta->identity = identity;
938 identity = NULL;
939 sta->radius_cui = radius_cui;
940 radius_cui = NULL;
941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 sta->flags &= ~WLAN_STA_PREAUTH;
943 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
944
945 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
946 sta->acct_interim_interval = acct_interim_interval;
947 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
948 ap_sta_session_timeout(hapd, sta, session_timeout);
949 else
950 ap_sta_no_session_timeout(hapd, sta);
951
952 switch (auth_alg) {
953 case WLAN_AUTH_OPEN:
954 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
955 HOSTAPD_LEVEL_DEBUG,
956 "authentication OK (open system)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 sta->flags |= WLAN_STA_AUTH;
958 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
959 sta->auth_alg = WLAN_AUTH_OPEN;
960 mlme_authenticate_indication(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 break;
962 case WLAN_AUTH_SHARED_KEY:
963 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
964 fc & WLAN_FC_ISWEP);
965 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
966 mlme_authenticate_indication(hapd, sta);
967 if (sta->challenge && auth_transaction == 1) {
968 resp_ies[0] = WLAN_EID_CHALLENGE;
969 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
970 os_memcpy(resp_ies + 2, sta->challenge,
971 WLAN_AUTH_CHALLENGE_LEN);
972 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
973 }
974 break;
975#ifdef CONFIG_IEEE80211R
976 case WLAN_AUTH_FT:
977 sta->auth_alg = WLAN_AUTH_FT;
978 if (sta->wpa_sm == NULL)
979 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700980 sta->addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 if (sta->wpa_sm == NULL) {
982 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
983 "state machine");
984 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
985 goto fail;
986 }
987 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
988 auth_transaction, mgmt->u.auth.variable,
989 len - IEEE80211_HDRLEN -
990 sizeof(mgmt->u.auth),
991 handle_auth_ft_finish, hapd);
992 /* handle_auth_ft_finish() callback will complete auth. */
993 return;
994#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800995#ifdef CONFIG_SAE
996 case WLAN_AUTH_SAE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800997#ifdef CONFIG_MESH
998 if (status_code == WLAN_STATUS_SUCCESS &&
999 hapd->conf->mesh & MESH_ENABLED) {
1000 if (sta->wpa_sm == NULL)
1001 sta->wpa_sm =
1002 wpa_auth_sta_init(hapd->wpa_auth,
1003 sta->addr, NULL);
1004 if (sta->wpa_sm == NULL) {
1005 wpa_printf(MSG_DEBUG,
1006 "SAE: Failed to initialize WPA state machine");
1007 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1008 goto fail;
1009 }
1010 }
1011#endif /* CONFIG_MESH */
1012 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
1013 status_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001014 return;
1015#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 }
1017
1018 fail:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001019 os_free(identity);
1020 os_free(radius_cui);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001021 hostapd_free_psk_list(psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
1024 auth_transaction + 1, resp, resp_ies, resp_ies_len);
1025}
1026
1027
1028static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
1029{
1030 int i, j = 32, aid;
1031
1032 /* get a unique AID */
1033 if (sta->aid > 0) {
1034 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1035 return 0;
1036 }
1037
1038 for (i = 0; i < AID_WORDS; i++) {
1039 if (hapd->sta_aid[i] == (u32) -1)
1040 continue;
1041 for (j = 0; j < 32; j++) {
1042 if (!(hapd->sta_aid[i] & BIT(j)))
1043 break;
1044 }
1045 if (j < 32)
1046 break;
1047 }
1048 if (j == 32)
1049 return -1;
1050 aid = i * 32 + j + 1;
1051 if (aid > 2007)
1052 return -1;
1053
1054 sta->aid = aid;
1055 hapd->sta_aid[i] |= BIT(j);
1056 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1057 return 0;
1058}
1059
1060
1061static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1062 const u8 *ssid_ie, size_t ssid_ie_len)
1063{
1064 if (ssid_ie == NULL)
1065 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1066
1067 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1068 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1070 HOSTAPD_LEVEL_INFO,
1071 "Station tried to associate with unknown SSID "
Dmitry Shmidt3c479372014-02-04 10:50:36 -08001072 "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1074 }
1075
1076 return WLAN_STATUS_SUCCESS;
1077}
1078
1079
1080static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1081 const u8 *wmm_ie, size_t wmm_ie_len)
1082{
1083 sta->flags &= ~WLAN_STA_WMM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 sta->qosinfo = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 if (wmm_ie && hapd->conf->wmm_enabled) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 struct wmm_information_element *wmm;
1087
1088 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 hostapd_logger(hapd, sta->addr,
1090 HOSTAPD_MODULE_WPA,
1091 HOSTAPD_LEVEL_DEBUG,
1092 "invalid WMM element in association "
1093 "request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1095 }
1096
1097 sta->flags |= WLAN_STA_WMM;
1098 wmm = (struct wmm_information_element *) wmm_ie;
1099 sta->qosinfo = wmm->qos_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 }
1101 return WLAN_STATUS_SUCCESS;
1102}
1103
1104
1105static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1106 struct ieee802_11_elems *elems)
1107{
1108 if (!elems->supp_rates) {
1109 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1110 HOSTAPD_LEVEL_DEBUG,
1111 "No supported rates element in AssocReq");
1112 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1113 }
1114
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001115 if (elems->supp_rates_len + elems->ext_supp_rates_len >
1116 sizeof(sta->supported_rates)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1118 HOSTAPD_LEVEL_DEBUG,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 "Invalid supported rates element length %d+%d",
1120 elems->supp_rates_len,
1121 elems->ext_supp_rates_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1123 }
1124
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001125 sta->supported_rates_len = merge_byte_arrays(
1126 sta->supported_rates, sizeof(sta->supported_rates),
1127 elems->supp_rates, elems->supp_rates_len,
1128 elems->ext_supp_rates, elems->ext_supp_rates_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129
1130 return WLAN_STATUS_SUCCESS;
1131}
1132
1133
Dmitry Shmidt051af732013-10-22 13:52:46 -07001134static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1135 const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1136{
1137#ifdef CONFIG_INTERWORKING
1138 /* check for QoS Map support */
1139 if (ext_capab_ie_len >= 5) {
1140 if (ext_capab_ie[4] & 0x01)
1141 sta->qos_map_enabled = 1;
1142 }
1143#endif /* CONFIG_INTERWORKING */
1144
1145 return WLAN_STATUS_SUCCESS;
1146}
1147
1148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
1150 const u8 *ies, size_t ies_len, int reassoc)
1151{
1152 struct ieee802_11_elems elems;
1153 u16 resp;
1154 const u8 *wpa_ie;
1155 size_t wpa_ie_len;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001156 const u8 *p2p_dev_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157
1158 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1159 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1160 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1161 "association request");
1162 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1163 }
1164
1165 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1166 if (resp != WLAN_STATUS_SUCCESS)
1167 return resp;
1168 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
1169 if (resp != WLAN_STATUS_SUCCESS)
1170 return resp;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001171 resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
1172 if (resp != WLAN_STATUS_SUCCESS)
1173 return resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 resp = copy_supp_rates(hapd, sta, &elems);
1175 if (resp != WLAN_STATUS_SUCCESS)
1176 return resp;
1177#ifdef CONFIG_IEEE80211N
1178 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
1179 elems.ht_capabilities_len);
1180 if (resp != WLAN_STATUS_SUCCESS)
1181 return resp;
1182 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1183 !(sta->flags & WLAN_STA_HT)) {
1184 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1185 HOSTAPD_LEVEL_INFO, "Station does not support "
1186 "mandatory HT PHY - reject association");
1187 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1188 }
1189#endif /* CONFIG_IEEE80211N */
1190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001191#ifdef CONFIG_IEEE80211AC
1192 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
1193 elems.vht_capabilities_len);
1194 if (resp != WLAN_STATUS_SUCCESS)
1195 return resp;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001196
1197 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1198 if (resp != WLAN_STATUS_SUCCESS)
1199 return resp;
1200
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001201 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1202 !(sta->flags & WLAN_STA_VHT)) {
1203 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1204 HOSTAPD_LEVEL_INFO, "Station does not support "
1205 "mandatory VHT PHY - reject association");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001206 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001207 }
1208#endif /* CONFIG_IEEE80211AC */
1209
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001210#ifdef CONFIG_P2P
1211 if (elems.p2p) {
1212 wpabuf_free(sta->p2p_ie);
1213 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1214 P2P_IE_VENDOR_TYPE);
1215 if (sta->p2p_ie)
1216 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1217 } else {
1218 wpabuf_free(sta->p2p_ie);
1219 sta->p2p_ie = NULL;
1220 }
1221#endif /* CONFIG_P2P */
1222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1224 wpa_ie = elems.rsn_ie;
1225 wpa_ie_len = elems.rsn_ie_len;
1226 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1227 elems.wpa_ie) {
1228 wpa_ie = elems.wpa_ie;
1229 wpa_ie_len = elems.wpa_ie_len;
1230 } else {
1231 wpa_ie = NULL;
1232 wpa_ie_len = 0;
1233 }
1234
1235#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001236 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 if (hapd->conf->wps_state && elems.wps_ie) {
1238 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1239 "Request - assume WPS is used");
1240 sta->flags |= WLAN_STA_WPS;
1241 wpabuf_free(sta->wps_ie);
1242 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1243 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001244 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1245 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1246 sta->flags |= WLAN_STA_WPS2;
1247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 wpa_ie = NULL;
1249 wpa_ie_len = 0;
1250 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1251 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1252 "(Re)Association Request - reject");
1253 return WLAN_STATUS_INVALID_IE;
1254 }
1255 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1256 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1257 "(Re)Association Request - possible WPS use");
1258 sta->flags |= WLAN_STA_MAYBE_WPS;
1259 } else
1260#endif /* CONFIG_WPS */
1261 if (hapd->conf->wpa && wpa_ie == NULL) {
1262 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1263 HOSTAPD_LEVEL_INFO,
1264 "No WPA/RSN IE in association request");
1265 return WLAN_STATUS_INVALID_IE;
1266 }
1267
1268 if (hapd->conf->wpa && wpa_ie) {
1269 int res;
1270 wpa_ie -= 2;
1271 wpa_ie_len += 2;
1272 if (sta->wpa_sm == NULL)
1273 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001274 sta->addr,
1275 p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 if (sta->wpa_sm == NULL) {
1277 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1278 "state machine");
1279 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1280 }
1281 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1282 wpa_ie, wpa_ie_len,
1283 elems.mdie, elems.mdie_len);
1284 if (res == WPA_INVALID_GROUP)
1285 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1286 else if (res == WPA_INVALID_PAIRWISE)
1287 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1288 else if (res == WPA_INVALID_AKMP)
1289 resp = WLAN_STATUS_AKMP_NOT_VALID;
1290 else if (res == WPA_ALLOC_FAIL)
1291 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1292#ifdef CONFIG_IEEE80211W
1293 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1294 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1295 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1296 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1297#endif /* CONFIG_IEEE80211W */
1298 else if (res == WPA_INVALID_MDIE)
1299 resp = WLAN_STATUS_INVALID_MDIE;
1300 else if (res != WPA_IE_OK)
1301 resp = WLAN_STATUS_INVALID_IE;
1302 if (resp != WLAN_STATUS_SUCCESS)
1303 return resp;
1304#ifdef CONFIG_IEEE80211W
1305 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1306 sta->sa_query_count > 0)
1307 ap_check_sa_query_timeout(hapd, sta);
1308 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1309 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1310 /*
1311 * STA has already been associated with MFP and SA
1312 * Query timeout has not been reached. Reject the
1313 * association attempt temporarily and start SA Query,
1314 * if one is not pending.
1315 */
1316
1317 if (sta->sa_query_count == 0)
1318 ap_sta_start_sa_query(hapd, sta);
1319
1320 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1321 }
1322
1323 if (wpa_auth_uses_mfp(sta->wpa_sm))
1324 sta->flags |= WLAN_STA_MFP;
1325 else
1326 sta->flags &= ~WLAN_STA_MFP;
1327#endif /* CONFIG_IEEE80211W */
1328
1329#ifdef CONFIG_IEEE80211R
1330 if (sta->auth_alg == WLAN_AUTH_FT) {
1331 if (!reassoc) {
1332 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1333 "to use association (not "
1334 "re-association) with FT auth_alg",
1335 MAC2STR(sta->addr));
1336 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1337 }
1338
1339 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1340 ies_len);
1341 if (resp != WLAN_STATUS_SUCCESS)
1342 return resp;
1343 }
1344#endif /* CONFIG_IEEE80211R */
1345
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001346#ifdef CONFIG_SAE
1347 if (wpa_auth_uses_sae(sta->wpa_sm) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001348 sta->auth_alg == WLAN_AUTH_OPEN) {
1349 struct rsn_pmksa_cache_entry *sa;
1350 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1351 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1352 wpa_printf(MSG_DEBUG,
1353 "SAE: No PMKSA cache entry found for "
1354 MACSTR, MAC2STR(sta->addr));
1355 return WLAN_STATUS_INVALID_PMKID;
1356 }
1357 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1358 " using PMKSA caching", MAC2STR(sta->addr));
1359 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1360 sta->auth_alg != WLAN_AUTH_SAE &&
1361 !(sta->auth_alg == WLAN_AUTH_FT &&
1362 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1364 "SAE AKM after non-SAE auth_alg %u",
1365 MAC2STR(sta->addr), sta->auth_alg);
1366 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1367 }
1368#endif /* CONFIG_SAE */
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370#ifdef CONFIG_IEEE80211N
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001371 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1373 hostapd_logger(hapd, sta->addr,
1374 HOSTAPD_MODULE_IEEE80211,
1375 HOSTAPD_LEVEL_INFO,
1376 "Station tried to use TKIP with HT "
1377 "association");
1378 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1379 }
1380#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001381#ifdef CONFIG_HS20
1382 } else if (hapd->conf->osen) {
1383 if (elems.osen == NULL) {
1384 hostapd_logger(
1385 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1386 HOSTAPD_LEVEL_INFO,
1387 "No HS 2.0 OSEN element in association request");
1388 return WLAN_STATUS_INVALID_IE;
1389 }
1390
1391 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1392 if (sta->wpa_sm == NULL)
1393 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1394 sta->addr, NULL);
1395 if (sta->wpa_sm == NULL) {
1396 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1397 "state machine");
1398 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1399 }
1400 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1401 elems.osen - 2, elems.osen_len + 2) < 0)
1402 return WLAN_STATUS_INVALID_IE;
1403#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 } else
1405 wpa_auth_sta_no_wpa(sta->wpa_sm);
1406
1407#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1409#endif /* CONFIG_P2P */
1410
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001411#ifdef CONFIG_HS20
1412 wpabuf_free(sta->hs20_ie);
1413 if (elems.hs20 && elems.hs20_len > 4) {
1414 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1415 elems.hs20_len - 4);
1416 } else
1417 sta->hs20_ie = NULL;
1418#endif /* CONFIG_HS20 */
1419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 return WLAN_STATUS_SUCCESS;
1421}
1422
1423
1424static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1425 u16 reason_code)
1426{
1427 int send_len;
1428 struct ieee80211_mgmt reply;
1429
1430 os_memset(&reply, 0, sizeof(reply));
1431 reply.frame_control =
1432 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1433 os_memcpy(reply.da, addr, ETH_ALEN);
1434 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1435 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1436
1437 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1438 reply.u.deauth.reason_code = host_to_le16(reason_code);
1439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1442 strerror(errno));
1443}
1444
1445
1446static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1447 u16 status_code, int reassoc, const u8 *ies,
1448 size_t ies_len)
1449{
1450 int send_len;
1451 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1452 struct ieee80211_mgmt *reply;
1453 u8 *p;
1454
1455 os_memset(buf, 0, sizeof(buf));
1456 reply = (struct ieee80211_mgmt *) buf;
1457 reply->frame_control =
1458 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1459 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1460 WLAN_FC_STYPE_ASSOC_RESP));
1461 os_memcpy(reply->da, sta->addr, ETH_ALEN);
1462 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1463 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1464
1465 send_len = IEEE80211_HDRLEN;
1466 send_len += sizeof(reply->u.assoc_resp);
1467 reply->u.assoc_resp.capab_info =
1468 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1469 reply->u.assoc_resp.status_code = host_to_le16(status_code);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001470 reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 /* Supported rates */
1472 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1473 /* Extended supported rates */
1474 p = hostapd_eid_ext_supp_rates(hapd, p);
1475
1476#ifdef CONFIG_IEEE80211R
1477 if (status_code == WLAN_STATUS_SUCCESS) {
1478 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1479 * Transition Information, RSN, [RIC Response] */
1480 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1481 buf + sizeof(buf) - p,
1482 sta->auth_alg, ies, ies_len);
1483 }
1484#endif /* CONFIG_IEEE80211R */
1485
1486#ifdef CONFIG_IEEE80211W
1487 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1488 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1489#endif /* CONFIG_IEEE80211W */
1490
1491#ifdef CONFIG_IEEE80211N
1492 p = hostapd_eid_ht_capabilities(hapd, p);
1493 p = hostapd_eid_ht_operation(hapd, p);
1494#endif /* CONFIG_IEEE80211N */
1495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001496#ifdef CONFIG_IEEE80211AC
1497 p = hostapd_eid_vht_capabilities(hapd, p);
1498 p = hostapd_eid_vht_operation(hapd, p);
1499#endif /* CONFIG_IEEE80211AC */
1500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 p = hostapd_eid_ext_capab(hapd, p);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001502 p = hostapd_eid_bss_max_idle_period(hapd, p);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001503 if (sta->qos_map_enabled)
1504 p = hostapd_eid_qos_map_set(hapd, p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
1506 if (sta->flags & WLAN_STA_WMM)
1507 p = hostapd_eid_wmm(hapd, p);
1508
1509#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001510 if ((sta->flags & WLAN_STA_WPS) ||
1511 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 struct wpabuf *wps = wps_build_assoc_resp_ie();
1513 if (wps) {
1514 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1515 p += wpabuf_len(wps);
1516 wpabuf_free(wps);
1517 }
1518 }
1519#endif /* CONFIG_WPS */
1520
1521#ifdef CONFIG_P2P
1522 if (sta->p2p_ie) {
1523 struct wpabuf *p2p_resp_ie;
1524 enum p2p_status_code status;
1525 switch (status_code) {
1526 case WLAN_STATUS_SUCCESS:
1527 status = P2P_SC_SUCCESS;
1528 break;
1529 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1530 status = P2P_SC_FAIL_LIMIT_REACHED;
1531 break;
1532 default:
1533 status = P2P_SC_FAIL_INVALID_PARAMS;
1534 break;
1535 }
1536 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1537 if (p2p_resp_ie) {
1538 os_memcpy(p, wpabuf_head(p2p_resp_ie),
1539 wpabuf_len(p2p_resp_ie));
1540 p += wpabuf_len(p2p_resp_ie);
1541 wpabuf_free(p2p_resp_ie);
1542 }
1543 }
1544#endif /* CONFIG_P2P */
1545
1546#ifdef CONFIG_P2P_MANAGER
1547 if (hapd->conf->p2p & P2P_MANAGE)
1548 p = hostapd_eid_p2p_manage(hapd, p);
1549#endif /* CONFIG_P2P_MANAGER */
1550
1551 send_len += p - reply->u.assoc_resp.variable;
1552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1555 strerror(errno));
1556}
1557
1558
1559static void handle_assoc(struct hostapd_data *hapd,
1560 const struct ieee80211_mgmt *mgmt, size_t len,
1561 int reassoc)
1562{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001563 u16 capab_info, listen_interval, seq_ctrl, fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 u16 resp = WLAN_STATUS_SUCCESS;
1565 const u8 *pos;
1566 int left, i;
1567 struct sta_info *sta;
1568
1569 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1570 sizeof(mgmt->u.assoc_req))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001571 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
1572 reassoc, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 return;
1574 }
1575
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001576#ifdef CONFIG_TESTING_OPTIONS
1577 if (reassoc) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001578 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001579 drand48() < hapd->iconf->ignore_reassoc_probability) {
1580 wpa_printf(MSG_INFO,
1581 "TESTING: ignoring reassoc request from "
1582 MACSTR, MAC2STR(mgmt->sa));
1583 return;
1584 }
1585 } else {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001586 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001587 drand48() < hapd->iconf->ignore_assoc_probability) {
1588 wpa_printf(MSG_INFO,
1589 "TESTING: ignoring assoc request from "
1590 MACSTR, MAC2STR(mgmt->sa));
1591 return;
1592 }
1593 }
1594#endif /* CONFIG_TESTING_OPTIONS */
1595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001596 fc = le_to_host16(mgmt->frame_control);
1597 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 if (reassoc) {
1600 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1601 listen_interval = le_to_host16(
1602 mgmt->u.reassoc_req.listen_interval);
1603 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1604 " capab_info=0x%02x listen_interval=%d current_ap="
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001605 MACSTR " seq_ctrl=0x%x%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 MAC2STR(mgmt->sa), capab_info, listen_interval,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001607 MAC2STR(mgmt->u.reassoc_req.current_ap),
1608 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1610 pos = mgmt->u.reassoc_req.variable;
1611 } else {
1612 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1613 listen_interval = le_to_host16(
1614 mgmt->u.assoc_req.listen_interval);
1615 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001616 " capab_info=0x%02x listen_interval=%d "
1617 "seq_ctrl=0x%x%s",
1618 MAC2STR(mgmt->sa), capab_info, listen_interval,
1619 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1621 pos = mgmt->u.assoc_req.variable;
1622 }
1623
1624 sta = ap_get_sta(hapd, mgmt->sa);
1625#ifdef CONFIG_IEEE80211R
1626 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1627 (sta->flags & WLAN_STA_AUTH) == 0) {
1628 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1629 "prior to authentication since it is using "
1630 "over-the-DS FT", MAC2STR(mgmt->sa));
1631 } else
1632#endif /* CONFIG_IEEE80211R */
1633 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1634 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1635 HOSTAPD_LEVEL_INFO, "Station tried to "
1636 "associate before authentication "
1637 "(aid=%d flags=0x%x)",
1638 sta ? sta->aid : -1,
1639 sta ? sta->flags : 0);
1640 send_deauth(hapd, mgmt->sa,
1641 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1642 return;
1643 }
1644
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001645 if ((fc & WLAN_FC_RETRY) &&
1646 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1647 sta->last_seq_ctrl == seq_ctrl &&
1648 sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1649 WLAN_FC_STYPE_ASSOC_REQ) {
1650 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1651 HOSTAPD_LEVEL_DEBUG,
1652 "Drop repeated association frame seq_ctrl=0x%x",
1653 seq_ctrl);
1654 return;
1655 }
1656 sta->last_seq_ctrl = seq_ctrl;
1657 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1658 WLAN_FC_STYPE_ASSOC_REQ;
1659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 if (hapd->tkip_countermeasures) {
1661 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1662 goto fail;
1663 }
1664
1665 if (listen_interval > hapd->conf->max_listen_interval) {
1666 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1667 HOSTAPD_LEVEL_DEBUG,
1668 "Too large Listen Interval (%d)",
1669 listen_interval);
1670 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1671 goto fail;
1672 }
1673
1674 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1675 * is used */
1676 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1677 if (resp != WLAN_STATUS_SUCCESS)
1678 goto fail;
1679
1680 if (hostapd_get_aid(hapd, sta) < 0) {
1681 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1682 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1683 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1684 goto fail;
1685 }
1686
1687 sta->capability = capab_info;
1688 sta->listen_interval = listen_interval;
1689
1690 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1691 sta->flags |= WLAN_STA_NONERP;
1692 for (i = 0; i < sta->supported_rates_len; i++) {
1693 if ((sta->supported_rates[i] & 0x7f) > 22) {
1694 sta->flags &= ~WLAN_STA_NONERP;
1695 break;
1696 }
1697 }
1698 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1699 sta->nonerp_set = 1;
1700 hapd->iface->num_sta_non_erp++;
1701 if (hapd->iface->num_sta_non_erp == 1)
1702 ieee802_11_set_beacons(hapd->iface);
1703 }
1704
1705 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1706 !sta->no_short_slot_time_set) {
1707 sta->no_short_slot_time_set = 1;
1708 hapd->iface->num_sta_no_short_slot_time++;
1709 if (hapd->iface->current_mode->mode ==
1710 HOSTAPD_MODE_IEEE80211G &&
1711 hapd->iface->num_sta_no_short_slot_time == 1)
1712 ieee802_11_set_beacons(hapd->iface);
1713 }
1714
1715 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1716 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1717 else
1718 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1719
1720 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1721 !sta->no_short_preamble_set) {
1722 sta->no_short_preamble_set = 1;
1723 hapd->iface->num_sta_no_short_preamble++;
1724 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1725 && hapd->iface->num_sta_no_short_preamble == 1)
1726 ieee802_11_set_beacons(hapd->iface);
1727 }
1728
1729#ifdef CONFIG_IEEE80211N
1730 update_ht_state(hapd, sta);
1731#endif /* CONFIG_IEEE80211N */
1732
1733 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1734 HOSTAPD_LEVEL_DEBUG,
1735 "association OK (aid %d)", sta->aid);
1736 /* Station will be marked associated, after it acknowledges AssocResp
1737 */
1738 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1739
1740#ifdef CONFIG_IEEE80211W
1741 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1742 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1743 "SA Query procedure", reassoc ? "re" : "");
1744 /* TODO: Send a protected Disassociate frame to the STA using
1745 * the old key and Reason Code "Previous Authentication no
1746 * longer valid". Make sure this is only sent protected since
1747 * unprotected frame would be received by the STA that is now
1748 * trying to associate.
1749 */
1750 }
1751#endif /* CONFIG_IEEE80211W */
1752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 /* Make sure that the previously registered inactivity timer will not
1754 * remove the STA immediately. */
1755 sta->timeout_next = STA_NULLFUNC;
1756
1757 fail:
1758 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1759}
1760
1761
1762static void handle_disassoc(struct hostapd_data *hapd,
1763 const struct ieee80211_mgmt *mgmt, size_t len)
1764{
1765 struct sta_info *sta;
1766
1767 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001768 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
1769 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 return;
1771 }
1772
1773 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1774 MAC2STR(mgmt->sa),
1775 le_to_host16(mgmt->u.disassoc.reason_code));
1776
1777 sta = ap_get_sta(hapd, mgmt->sa);
1778 if (sta == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001779 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
1780 MAC2STR(mgmt->sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 return;
1782 }
1783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001784 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001785 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1788 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1789 HOSTAPD_LEVEL_INFO, "disassociated");
1790 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1791 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1792 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1793 * authenticated. */
1794 accounting_sta_stop(hapd, sta);
1795 ieee802_1x_free_station(sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001796 if (sta->ipaddr)
1797 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
1798 ap_sta_ip6addr_del(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 hostapd_drv_sta_remove(hapd, sta->addr);
1800
1801 if (sta->timeout_next == STA_NULLFUNC ||
1802 sta->timeout_next == STA_DISASSOC) {
1803 sta->timeout_next = STA_DEAUTH;
1804 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1805 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1806 hapd, sta);
1807 }
1808
1809 mlme_disassociate_indication(
1810 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1811}
1812
1813
1814static void handle_deauth(struct hostapd_data *hapd,
1815 const struct ieee80211_mgmt *mgmt, size_t len)
1816{
1817 struct sta_info *sta;
1818
1819 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001820 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1821 "payload (len=%lu)", (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 return;
1823 }
1824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 " reason_code=%d",
1827 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1828
1829 sta = ap_get_sta(hapd, mgmt->sa);
1830 if (sta == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001831 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1832 "to deauthenticate, but it is not authenticated",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 MAC2STR(mgmt->sa));
1834 return;
1835 }
1836
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001838 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1840 WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1842 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1843 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1844 mlme_deauthenticate_indication(
1845 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1846 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1847 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1848 ap_free_sta(hapd, sta);
1849}
1850
1851
1852static void handle_beacon(struct hostapd_data *hapd,
1853 const struct ieee80211_mgmt *mgmt, size_t len,
1854 struct hostapd_frame_info *fi)
1855{
1856 struct ieee802_11_elems elems;
1857
1858 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001859 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
1860 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 return;
1862 }
1863
1864 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1865 len - (IEEE80211_HDRLEN +
1866 sizeof(mgmt->u.beacon)), &elems,
1867 0);
1868
1869 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1870}
1871
1872
1873#ifdef CONFIG_IEEE80211W
1874
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001875static int hostapd_sa_query_action(struct hostapd_data *hapd,
1876 const struct ieee80211_mgmt *mgmt,
1877 size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 const u8 *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880
1881 end = mgmt->u.action.u.sa_query_resp.trans_id +
1882 WLAN_SA_QUERY_TR_ID_LEN;
1883 if (((u8 *) mgmt) + len < end) {
1884 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1885 "frame (len=%lu)", (unsigned long) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001886 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 }
1888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 ieee802_11_sa_query_action(hapd, mgmt->sa,
1890 mgmt->u.action.u.sa_query_resp.action,
1891 mgmt->u.action.u.sa_query_resp.trans_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001892 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893}
1894
1895
1896static int robust_action_frame(u8 category)
1897{
1898 return category != WLAN_ACTION_PUBLIC &&
1899 category != WLAN_ACTION_HT;
1900}
1901#endif /* CONFIG_IEEE80211W */
1902
1903
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001904static int handle_action(struct hostapd_data *hapd,
1905 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906{
1907 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001908 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909
1910 if (len < IEEE80211_HDRLEN + 1) {
1911 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1912 HOSTAPD_LEVEL_DEBUG,
1913 "handle_action - too short payload (len=%lu)",
1914 (unsigned long) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001915 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 }
1917
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001918 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1919 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
1920 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
1921 "frame (category=%u) from unassociated STA " MACSTR,
1922 MAC2STR(mgmt->sa), mgmt->u.action.category);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001923 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001924 }
1925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926#ifdef CONFIG_IEEE80211W
1927 if (sta && (sta->flags & WLAN_STA_MFP) &&
Dmitry Shmidt18463232014-01-24 12:29:41 -08001928 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
1929 robust_action_frame(mgmt->u.action.category)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1931 HOSTAPD_LEVEL_DEBUG,
1932 "Dropped unprotected Robust Action frame from "
1933 "an MFP STA");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001934 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 }
1936#endif /* CONFIG_IEEE80211W */
1937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001938 if (sta) {
1939 u16 fc = le_to_host16(mgmt->frame_control);
1940 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1941
1942 if ((fc & WLAN_FC_RETRY) &&
1943 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1944 sta->last_seq_ctrl == seq_ctrl &&
1945 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
1946 hostapd_logger(hapd, sta->addr,
1947 HOSTAPD_MODULE_IEEE80211,
1948 HOSTAPD_LEVEL_DEBUG,
1949 "Drop repeated action frame seq_ctrl=0x%x",
1950 seq_ctrl);
1951 return 1;
1952 }
1953
1954 sta->last_seq_ctrl = seq_ctrl;
1955 sta->last_subtype = WLAN_FC_STYPE_ACTION;
1956 }
1957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958 switch (mgmt->u.action.category) {
1959#ifdef CONFIG_IEEE80211R
1960 case WLAN_ACTION_FT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001961 if (!sta ||
1962 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963 len - IEEE80211_HDRLEN))
1964 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001965 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966#endif /* CONFIG_IEEE80211R */
1967 case WLAN_ACTION_WMM:
1968 hostapd_wmm_action(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001969 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970#ifdef CONFIG_IEEE80211W
1971 case WLAN_ACTION_SA_QUERY:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001972 return hostapd_sa_query_action(hapd, mgmt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001974#ifdef CONFIG_WNM
1975 case WLAN_ACTION_WNM:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001976 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
1977 return 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001978#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 case WLAN_ACTION_PUBLIC:
Dmitry Shmidt18463232014-01-24 12:29:41 -08001980 case WLAN_ACTION_PROTECTED_DUAL:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001981#ifdef CONFIG_IEEE80211N
1982 if (mgmt->u.action.u.public_action.action ==
1983 WLAN_PA_20_40_BSS_COEX) {
1984 wpa_printf(MSG_DEBUG,
1985 "HT20/40 coex mgmt frame received from STA "
1986 MACSTR, MAC2STR(mgmt->sa));
1987 hostapd_2040_coex_action(hapd, mgmt, len);
1988 }
1989#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 if (hapd->public_action_cb) {
1991 hapd->public_action_cb(hapd->public_action_cb_ctx,
1992 (u8 *) mgmt, len,
1993 hapd->iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001995 if (hapd->public_action_cb2) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001996 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001997 (u8 *) mgmt, len,
1998 hapd->iface->freq);
1999 }
2000 if (hapd->public_action_cb || hapd->public_action_cb2)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002001 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 break;
2003 case WLAN_ACTION_VENDOR_SPECIFIC:
2004 if (hapd->vendor_action_cb) {
2005 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
2006 (u8 *) mgmt, len,
2007 hapd->iface->freq) == 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002008 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 }
2010 break;
2011 }
2012
2013 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2014 HOSTAPD_LEVEL_DEBUG,
2015 "handle_action - unknown action category %d or invalid "
2016 "frame",
2017 mgmt->u.action.category);
2018 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
2019 !(mgmt->sa[0] & 0x01)) {
2020 struct ieee80211_mgmt *resp;
2021
2022 /*
2023 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2024 * Return the Action frame to the source without change
2025 * except that MSB of the Category set to 1.
2026 */
2027 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2028 "frame back to sender");
2029 resp = os_malloc(len);
2030 if (resp == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002031 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 os_memcpy(resp, mgmt, len);
2033 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2034 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2035 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2036 resp->u.action.category |= 0x80;
2037
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2039 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2040 "Action frame");
2041 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 os_free(resp);
2043 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002044
2045 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046}
2047
2048
2049/**
2050 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2051 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2052 * sent to)
2053 * @buf: management frame data (starting from IEEE 802.11 header)
2054 * @len: length of frame data in octets
2055 * @fi: meta data about received frame (signal level, etc.)
2056 *
2057 * Process all incoming IEEE 802.11 management frames. This will be called for
2058 * each frame received from the kernel driver through wlan#ap interface. In
2059 * addition, it can be called to re-inserted pending frames (e.g., when using
2060 * external RADIUS server as an MAC ACL).
2061 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002062int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2063 struct hostapd_frame_info *fi)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064{
2065 struct ieee80211_mgmt *mgmt;
2066 int broadcast;
2067 u16 fc, stype;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002068 int ret = 0;
2069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 if (len < 24)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002071 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072
2073 mgmt = (struct ieee80211_mgmt *) buf;
2074 fc = le_to_host16(mgmt->frame_control);
2075 stype = WLAN_FC_GET_STYPE(fc);
2076
2077 if (stype == WLAN_FC_STYPE_BEACON) {
2078 handle_beacon(hapd, mgmt, len, fi);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002079 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 }
2081
2082 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2083 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2084 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2085
2086 if (!broadcast &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002087#ifdef CONFIG_P2P
2088 /* Invitation responses can be sent with the peer MAC as BSSID */
2089 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2090 stype == WLAN_FC_STYPE_ACTION) &&
2091#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002092#ifdef CONFIG_MESH
2093 !(hapd->conf->mesh & MESH_ENABLED) &&
2094#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002096 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2097 MAC2STR(mgmt->bssid));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002098 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
2101
2102 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002103 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002104 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 }
2106
2107 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2108 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2109 HOSTAPD_LEVEL_DEBUG,
2110 "MGMT: DA=" MACSTR " not our address",
2111 MAC2STR(mgmt->da));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002112 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 }
2114
2115 switch (stype) {
2116 case WLAN_FC_STYPE_AUTH:
2117 wpa_printf(MSG_DEBUG, "mgmt::auth");
2118 handle_auth(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002119 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 break;
2121 case WLAN_FC_STYPE_ASSOC_REQ:
2122 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2123 handle_assoc(hapd, mgmt, len, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002124 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 break;
2126 case WLAN_FC_STYPE_REASSOC_REQ:
2127 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2128 handle_assoc(hapd, mgmt, len, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002129 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 break;
2131 case WLAN_FC_STYPE_DISASSOC:
2132 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2133 handle_disassoc(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002134 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 break;
2136 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002137 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 handle_deauth(hapd, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002139 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 break;
2141 case WLAN_FC_STYPE_ACTION:
2142 wpa_printf(MSG_DEBUG, "mgmt::action");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002143 ret = handle_action(hapd, mgmt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 break;
2145 default:
2146 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2147 HOSTAPD_LEVEL_DEBUG,
2148 "unknown mgmt frame subtype %d", stype);
2149 break;
2150 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002151
2152 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153}
2154
2155
2156static void handle_auth_cb(struct hostapd_data *hapd,
2157 const struct ieee80211_mgmt *mgmt,
2158 size_t len, int ok)
2159{
2160 u16 auth_alg, auth_transaction, status_code;
2161 struct sta_info *sta;
2162
2163 if (!ok) {
2164 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2165 HOSTAPD_LEVEL_NOTICE,
2166 "did not acknowledge authentication response");
2167 return;
2168 }
2169
2170 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002171 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2172 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 return;
2174 }
2175
2176 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2177 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2178 status_code = le_to_host16(mgmt->u.auth.status_code);
2179
2180 sta = ap_get_sta(hapd, mgmt->da);
2181 if (!sta) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002182 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2183 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 return;
2185 }
2186
2187 if (status_code == WLAN_STATUS_SUCCESS &&
2188 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2189 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2190 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2191 HOSTAPD_LEVEL_INFO, "authenticated");
2192 sta->flags |= WLAN_STA_AUTH;
2193 }
2194}
2195
2196
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002197static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2198 struct sta_info *sta,
2199 char *ifname_wds)
2200{
2201 int i;
2202 struct hostapd_ssid *ssid = sta->ssid;
2203
2204 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2205 return;
2206
2207 for (i = 0; i < 4; i++) {
2208 if (ssid->wep.key[i] &&
2209 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2210 i == ssid->wep.idx, NULL, 0,
2211 ssid->wep.key[i], ssid->wep.len[i])) {
2212 wpa_printf(MSG_WARNING,
2213 "Could not set WEP keys for WDS interface; %s",
2214 ifname_wds);
2215 break;
2216 }
2217 }
2218}
2219
2220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221static void handle_assoc_cb(struct hostapd_data *hapd,
2222 const struct ieee80211_mgmt *mgmt,
2223 size_t len, int reassoc, int ok)
2224{
2225 u16 status;
2226 struct sta_info *sta;
2227 int new_assoc = 1;
2228 struct ieee80211_ht_capabilities ht_cap;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002229 struct ieee80211_vht_capabilities vht_cap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2232 sizeof(mgmt->u.assoc_resp))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002233 wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2234 reassoc, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 return;
2236 }
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 sta = ap_get_sta(hapd, mgmt->da);
2239 if (!sta) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002240 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2241 MAC2STR(mgmt->da));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 return;
2243 }
2244
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002245 if (!ok) {
2246 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2247 HOSTAPD_LEVEL_DEBUG,
2248 "did not acknowledge association response");
2249 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2250 return;
2251 }
2252
2253 if (reassoc)
2254 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2255 else
2256 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 if (status != WLAN_STATUS_SUCCESS)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002259 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260
2261 /* Stop previous accounting session, if one is started, and allocate
2262 * new session id for the new session. */
2263 accounting_sta_stop(hapd, sta);
2264
2265 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2266 HOSTAPD_LEVEL_INFO,
2267 "associated (aid %d)",
2268 sta->aid);
2269
2270 if (sta->flags & WLAN_STA_ASSOC)
2271 new_assoc = 0;
2272 sta->flags |= WLAN_STA_ASSOC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002273 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002274 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 sta->auth_alg == WLAN_AUTH_FT) {
2276 /*
2277 * Open, static WEP, or FT protocol; no separate authorization
2278 * step.
2279 */
2280 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 }
2282
2283 if (reassoc)
2284 mlme_reassociate_indication(hapd, sta);
2285 else
2286 mlme_associate_indication(hapd, sta);
2287
2288#ifdef CONFIG_IEEE80211W
2289 sta->sa_query_timed_out = 0;
2290#endif /* CONFIG_IEEE80211W */
2291
2292 /*
2293 * Remove the STA entry in order to make sure the STA PS state gets
2294 * cleared and configuration gets updated in case of reassociation back
2295 * to the same AP.
2296 */
2297 hostapd_drv_sta_remove(hapd, sta->addr);
2298
2299#ifdef CONFIG_IEEE80211N
2300 if (sta->flags & WLAN_STA_HT)
2301 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
2302#endif /* CONFIG_IEEE80211N */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002303#ifdef CONFIG_IEEE80211AC
2304 if (sta->flags & WLAN_STA_VHT)
2305 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
2306#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307
2308 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
2309 sta->supported_rates, sta->supported_rates_len,
2310 sta->listen_interval,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002312 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002313 sta->flags, sta->qosinfo, sta->vht_opmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2315 HOSTAPD_LEVEL_NOTICE,
2316 "Could not add STA to kernel driver");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002317
2318 ap_sta_disconnect(hapd, sta, sta->addr,
2319 WLAN_REASON_DISASSOC_AP_BUSY);
2320
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002321 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 }
2323
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002324 if (sta->flags & WLAN_STA_WDS) {
2325 int ret;
2326 char ifname_wds[IFNAMSIZ + 1];
2327
2328 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2329 sta->aid, 1);
2330 if (!ret)
2331 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333
2334 if (sta->eapol_sm == NULL) {
2335 /*
2336 * This STA does not use RADIUS server for EAP authentication,
2337 * so bind it to the selected VLAN interface now, since the
2338 * interface selection is not going to change anymore.
2339 */
2340 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002341 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 } else if (sta->vlan_id) {
2343 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2344 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002345 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 }
2347
2348 hostapd_set_sta_flags(hapd, sta);
2349
2350 if (sta->auth_alg == WLAN_AUTH_FT)
2351 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2352 else
2353 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2354 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2355
2356 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357}
2358
2359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360static void handle_deauth_cb(struct hostapd_data *hapd,
2361 const struct ieee80211_mgmt *mgmt,
2362 size_t len, int ok)
2363{
2364 struct sta_info *sta;
2365 if (mgmt->da[0] & 0x01)
2366 return;
2367 sta = ap_get_sta(hapd, mgmt->da);
2368 if (!sta) {
2369 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2370 " not found", MAC2STR(mgmt->da));
2371 return;
2372 }
2373 if (ok)
2374 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2375 MAC2STR(sta->addr));
2376 else
2377 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2378 "deauth", MAC2STR(sta->addr));
2379
2380 ap_sta_deauth_cb(hapd, sta);
2381}
2382
2383
2384static void handle_disassoc_cb(struct hostapd_data *hapd,
2385 const struct ieee80211_mgmt *mgmt,
2386 size_t len, int ok)
2387{
2388 struct sta_info *sta;
2389 if (mgmt->da[0] & 0x01)
2390 return;
2391 sta = ap_get_sta(hapd, mgmt->da);
2392 if (!sta) {
2393 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2394 " not found", MAC2STR(mgmt->da));
2395 return;
2396 }
2397 if (ok)
2398 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2399 MAC2STR(sta->addr));
2400 else
2401 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2402 "disassoc", MAC2STR(sta->addr));
2403
2404 ap_sta_disassoc_cb(hapd, sta);
2405}
2406
2407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408/**
2409 * ieee802_11_mgmt_cb - Process management frame TX status callback
2410 * @hapd: hostapd BSS data structure (the BSS from which the management frame
2411 * was sent from)
2412 * @buf: management frame data (starting from IEEE 802.11 header)
2413 * @len: length of frame data in octets
2414 * @stype: management frame subtype from frame control field
2415 * @ok: Whether the frame was ACK'ed
2416 */
2417void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2418 u16 stype, int ok)
2419{
2420 const struct ieee80211_mgmt *mgmt;
2421 mgmt = (const struct ieee80211_mgmt *) buf;
2422
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002423#ifdef CONFIG_TESTING_OPTIONS
2424 if (hapd->ext_mgmt_frame_handling) {
2425 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2426 stype, ok);
2427 return;
2428 }
2429#endif /* CONFIG_TESTING_OPTIONS */
2430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 switch (stype) {
2432 case WLAN_FC_STYPE_AUTH:
2433 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2434 handle_auth_cb(hapd, mgmt, len, ok);
2435 break;
2436 case WLAN_FC_STYPE_ASSOC_RESP:
2437 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2438 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2439 break;
2440 case WLAN_FC_STYPE_REASSOC_RESP:
2441 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2442 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2443 break;
2444 case WLAN_FC_STYPE_PROBE_RESP:
2445 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2446 break;
2447 case WLAN_FC_STYPE_DEAUTH:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002448 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2449 handle_deauth_cb(hapd, mgmt, len, ok);
2450 break;
2451 case WLAN_FC_STYPE_DISASSOC:
2452 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2453 handle_disassoc_cb(hapd, mgmt, len, ok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 break;
2455 case WLAN_FC_STYPE_ACTION:
2456 wpa_printf(MSG_DEBUG, "mgmt::action cb");
2457 break;
2458 default:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002459 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 break;
2461 }
2462}
2463
2464
2465int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2466{
2467 /* TODO */
2468 return 0;
2469}
2470
2471
2472int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2473 char *buf, size_t buflen)
2474{
2475 /* TODO */
2476 return 0;
2477}
2478
2479
2480void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2481 const u8 *buf, size_t len, int ack)
2482{
2483 struct sta_info *sta;
2484 struct hostapd_iface *iface = hapd->iface;
2485
2486 sta = ap_get_sta(hapd, addr);
2487 if (sta == NULL && iface->num_bss > 1) {
2488 size_t j;
2489 for (j = 0; j < iface->num_bss; j++) {
2490 hapd = iface->bss[j];
2491 sta = ap_get_sta(hapd, addr);
2492 if (sta)
2493 break;
2494 }
2495 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002496 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 return;
2498 if (sta->flags & WLAN_STA_PENDING_POLL) {
2499 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2500 "activity poll", MAC2STR(sta->addr),
2501 ack ? "ACKed" : "did not ACK");
2502 if (ack)
2503 sta->flags &= ~WLAN_STA_PENDING_POLL;
2504 }
2505
2506 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2507}
2508
2509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002510void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2511 const u8 *data, size_t len, int ack)
2512{
2513 struct sta_info *sta;
2514 struct hostapd_iface *iface = hapd->iface;
2515
2516 sta = ap_get_sta(hapd, dst);
2517 if (sta == NULL && iface->num_bss > 1) {
2518 size_t j;
2519 for (j = 0; j < iface->num_bss; j++) {
2520 hapd = iface->bss[j];
2521 sta = ap_get_sta(hapd, dst);
2522 if (sta)
2523 break;
2524 }
2525 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002526 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2527 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2528 MACSTR " that is not currently associated",
2529 MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 return;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002531 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002532
2533 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2534}
2535
2536
2537void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2538{
2539 struct sta_info *sta;
2540 struct hostapd_iface *iface = hapd->iface;
2541
2542 sta = ap_get_sta(hapd, addr);
2543 if (sta == NULL && iface->num_bss > 1) {
2544 size_t j;
2545 for (j = 0; j < iface->num_bss; j++) {
2546 hapd = iface->bss[j];
2547 sta = ap_get_sta(hapd, addr);
2548 if (sta)
2549 break;
2550 }
2551 }
2552 if (sta == NULL)
2553 return;
2554 if (!(sta->flags & WLAN_STA_PENDING_POLL))
2555 return;
2556
2557 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2558 "activity poll", MAC2STR(sta->addr));
2559 sta->flags &= ~WLAN_STA_PENDING_POLL;
2560}
2561
2562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2564 int wds)
2565{
2566 struct sta_info *sta;
2567
2568 sta = ap_get_sta(hapd, src);
2569 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002570 if (!hapd->conf->wds_sta)
2571 return;
2572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 if (wds && !(sta->flags & WLAN_STA_WDS)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002574 int ret;
2575 char ifname_wds[IFNAMSIZ + 1];
2576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2578 "STA " MACSTR " (aid %u)",
2579 MAC2STR(sta->addr), sta->aid);
2580 sta->flags |= WLAN_STA_WDS;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002581 ret = hostapd_set_wds_sta(hapd, ifname_wds,
2582 sta->addr, sta->aid, 1);
2583 if (!ret)
2584 hostapd_set_wds_encryption(hapd, sta,
2585 ifname_wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 }
2587 return;
2588 }
2589
2590 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2591 MACSTR, MAC2STR(src));
2592 if (src[0] & 0x01) {
2593 /* Broadcast bit set in SA?! Ignore the frame silently. */
2594 return;
2595 }
2596
2597 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2598 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2599 "already been sent, but no TX status yet known - "
2600 "ignore Class 3 frame issue with " MACSTR,
2601 MAC2STR(src));
2602 return;
2603 }
2604
2605 if (sta && (sta->flags & WLAN_STA_AUTH))
2606 hostapd_drv_sta_disassoc(
2607 hapd, src,
2608 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2609 else
2610 hostapd_drv_sta_deauth(
2611 hapd, src,
2612 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2613}
2614
2615
2616#endif /* CONFIG_NATIVE_WINDOWS */