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