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