Copy previous BSSID into STA data only after full validation of the request
[libeap.git] / hostapd / ieee802_11.c
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007-2008, Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #ifndef CONFIG_NATIVE_WINDOWS
19
20 #include <net/if.h>
21
22 #include "eloop.h"
23 #include "hostapd.h"
24 #include "ieee802_11.h"
25 #include "beacon.h"
26 #include "hw_features.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11_auth.h"
30 #include "sta_info.h"
31 #include "rc4.h"
32 #include "ieee802_1x.h"
33 #include "wpa.h"
34 #include "wme.h"
35 #include "ap_list.h"
36 #include "accounting.h"
37 #include "driver.h"
38 #include "ieee802_11h.h"
39 #include "mlme.h"
40
41
42 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
43 {
44         u8 *pos = eid;
45         int i, num, count;
46
47         if (hapd->iface->current_rates == NULL)
48                 return eid;
49
50         *pos++ = WLAN_EID_SUPP_RATES;
51         num = hapd->iface->num_rates;
52         if (num > 8) {
53                 /* rest of the rates are encoded in Extended supported
54                  * rates element */
55                 num = 8;
56         }
57
58         *pos++ = num;
59         count = 0;
60         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
61              i++) {
62                 count++;
63                 *pos = hapd->iface->current_rates[i].rate / 5;
64                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
65                         *pos |= 0x80;
66                 pos++;
67         }
68
69         return pos;
70 }
71
72
73 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
74 {
75         u8 *pos = eid;
76         int i, num, count;
77
78         if (hapd->iface->current_rates == NULL)
79                 return eid;
80
81         num = hapd->iface->num_rates;
82         if (num <= 8)
83                 return eid;
84         num -= 8;
85
86         *pos++ = WLAN_EID_EXT_SUPP_RATES;
87         *pos++ = num;
88         count = 0;
89         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
90              i++) {
91                 count++;
92                 if (count <= 8)
93                         continue; /* already in SuppRates IE */
94                 *pos = hapd->iface->current_rates[i].rate / 5;
95                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
96                         *pos |= 0x80;
97                 pos++;
98         }
99
100         return pos;
101 }
102
103
104 u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
105 {
106 #ifdef CONFIG_IEEE80211N
107         struct ieee80211_ht_capability *cap;
108         u8 *pos = eid;
109
110         if (!hapd->iconf->ieee80211n)
111                 return eid;
112
113         *pos++ = WLAN_EID_HT_CAP;
114         *pos++ = sizeof(*cap);
115
116         cap = (struct ieee80211_ht_capability *) pos;
117         os_memset(cap, 0, sizeof(*cap));
118         SET_2BIT_U8(&cap->mac_ht_params_info,
119                     MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
120                     MAX_RX_AMPDU_FACTOR_64KB);
121
122         cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
123
124         cap->supported_mcs_set[0] = 0xff;
125         cap->supported_mcs_set[1] = 0xff;
126
127         pos += sizeof(*cap);
128
129         return pos;
130 #else /* CONFIG_IEEE80211N */
131         return eid;
132 #endif /* CONFIG_IEEE80211N */
133 }
134
135
136 u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
137 {
138 #ifdef CONFIG_IEEE80211N
139         struct ieee80211_ht_operation *oper;
140         u8 *pos = eid;
141
142         if (!hapd->iconf->ieee80211n)
143                 return eid;
144
145         *pos++ = WLAN_EID_HT_OPERATION;
146         *pos++ = sizeof(*oper);
147
148         oper = (struct ieee80211_ht_operation *) pos;
149         os_memset(oper, 0, sizeof(*oper));
150
151         oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
152
153         pos += sizeof(*oper);
154
155         return pos;
156 #else /* CONFIG_IEEE80211N */
157         return eid;
158 #endif /* CONFIG_IEEE80211N */
159 }
160
161
162 #ifdef CONFIG_IEEE80211N
163
164 /*
165 op_mode
166 Set to 0 (HT pure) under the followign conditions
167         - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
168         - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
169 Set to 1 (HT non-member protection) if there may be non-HT STAs
170         in both the primary and the secondary channel
171 Set to 2 if only HT STAs are associated in BSS,
172         however and at least one 20 MHz HT STA is associated
173 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
174         (currently non-GF HT station is considered as non-HT STA also)
175 */
176 int hostapd_ht_operation_update(struct hostapd_iface *iface)
177 {
178         u16 cur_op_mode, new_op_mode;
179         int op_mode_changes = 0;
180
181         if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
182                 return 0;
183
184         wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
185                    __func__, iface->ht_op_mode);
186
187         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
188             && iface->num_sta_ht_no_gf) {
189                 iface->ht_op_mode |=
190                         HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
191                 op_mode_changes++;
192         } else if ((iface->ht_op_mode &
193                     HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
194                    iface->num_sta_ht_no_gf == 0) {
195                 iface->ht_op_mode &=
196                         ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
197                 op_mode_changes++;
198         }
199
200         if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
201             (iface->num_sta_no_ht || iface->olbc_ht)) {
202                 iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
203                 op_mode_changes++;
204         } else if ((iface->ht_op_mode &
205                     HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
206                    (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
207                 iface->ht_op_mode &=
208                         ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
209                 op_mode_changes++;
210         }
211
212         /* Note: currently we switch to the MIXED op mode if HT non-greenfield
213          * station is associated. Probably it's a theoretical case, since
214          * it looks like all known HT STAs support greenfield.
215          */
216         new_op_mode = 0;
217         if (iface->num_sta_no_ht ||
218             (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
219                 new_op_mode = OP_MODE_MIXED;
220         else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
221                  && iface->num_sta_ht_20mhz)
222                 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
223         else if (iface->olbc_ht)
224                 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
225         else
226                 new_op_mode = OP_MODE_PURE;
227
228         cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
229         if (cur_op_mode != new_op_mode) {
230                 iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
231                 iface->ht_op_mode |= new_op_mode;
232                 op_mode_changes++;
233         }
234
235         wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
236                    __func__, iface->ht_op_mode, op_mode_changes);
237
238         return op_mode_changes;
239 }
240
241 #endif /* CONFIG_IEEE80211N */
242
243
244 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
245                            int probe)
246 {
247         int capab = WLAN_CAPABILITY_ESS;
248         int privacy;
249
250         if (hapd->iface->num_sta_no_short_preamble == 0 &&
251             hapd->iconf->preamble == SHORT_PREAMBLE)
252                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
253
254         privacy = hapd->conf->ssid.wep.keys_set;
255
256         if (hapd->conf->ieee802_1x &&
257             (hapd->conf->default_wep_key_len ||
258              hapd->conf->individual_wep_key_len))
259                 privacy = 1;
260
261         if (hapd->conf->wpa)
262                 privacy = 1;
263
264         if (sta) {
265                 int policy, def_klen;
266                 if (probe && sta->ssid_probe) {
267                         policy = sta->ssid_probe->security_policy;
268                         def_klen = sta->ssid_probe->wep.default_len;
269                 } else {
270                         policy = sta->ssid->security_policy;
271                         def_klen = sta->ssid->wep.default_len;
272                 }
273                 privacy = policy != SECURITY_PLAINTEXT;
274                 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
275                         privacy = 0;
276         }
277
278         if (privacy)
279                 capab |= WLAN_CAPABILITY_PRIVACY;
280
281         if (hapd->iface->current_mode &&
282             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
283             hapd->iface->num_sta_no_short_slot_time == 0)
284                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
285
286         if (hapd->iface->dfs_enable) 
287                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
288
289         return capab;
290 }
291
292
293 #define OUI_MICROSOFT 0x0050f2 /* Microsoft (also used in Wi-Fi specs)
294                                 * 00:50:F2 */
295
296 static int ieee802_11_parse_vendor_specific(struct hostapd_data *hapd,
297                                             u8 *pos, size_t elen,
298                                             struct ieee802_11_elems *elems,
299                                             int show_errors)
300 {
301         unsigned int oui;
302
303         /* first 3 bytes in vendor specific information element are the IEEE
304          * OUI of the vendor. The following byte is used a vendor specific
305          * sub-type. */
306         if (elen < 4) {
307                 if (show_errors) {
308                         wpa_printf(MSG_MSGDUMP, "short vendor specific "
309                                    "information element ignored (len=%lu)",
310                                    (unsigned long) elen);
311                 }
312                 return -1;
313         }
314
315         oui = WPA_GET_BE24(pos);
316         switch (oui) {
317         case OUI_MICROSOFT:
318                 /* Microsoft/Wi-Fi information elements are further typed and
319                  * subtyped */
320                 switch (pos[3]) {
321                 case 1:
322                         /* Microsoft OUI (00:50:F2) with OUI Type 1:
323                          * real WPA information element */
324                         elems->wpa_ie = pos;
325                         elems->wpa_ie_len = elen;
326                         break;
327                 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
328                         if (elen < 5) {
329                                 wpa_printf(MSG_MSGDUMP, "short WME "
330                                            "information element ignored "
331                                            "(len=%lu)",
332                                            (unsigned long) elen);
333                                 return -1;
334                         }
335                         switch (pos[4]) {
336                         case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
337                         case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
338                                 elems->wme = pos;
339                                 elems->wme_len = elen;
340                                 break;
341                         case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
342                                 elems->wme_tspec = pos;
343                                 elems->wme_tspec_len = elen;
344                                 break;
345                         default:
346                                 wpa_printf(MSG_MSGDUMP, "unknown WME "
347                                            "information element ignored "
348                                            "(subtype=%d len=%lu)",
349                                            pos[4], (unsigned long) elen);
350                                 return -1;
351                         }
352                         break;
353                 default:
354                         wpa_printf(MSG_MSGDUMP, "Unknown Microsoft "
355                                    "information element ignored "
356                                    "(type=%d len=%lu)\n",
357                                    pos[3], (unsigned long) elen);
358                         return -1;
359                 }
360                 break;
361
362         default:
363                 wpa_printf(MSG_MSGDUMP, "unknown vendor specific information "
364                            "element ignored (vendor OUI %02x:%02x:%02x "
365                            "len=%lu)",
366                            pos[0], pos[1], pos[2], (unsigned long) elen);
367                 return -1;
368         }
369
370         return 0;
371 }
372
373
374 ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
375                                 size_t len,
376                                 struct ieee802_11_elems *elems,
377                                 int show_errors)
378 {
379         size_t left = len;
380         u8 *pos = start;
381         int unknown = 0;
382
383         os_memset(elems, 0, sizeof(*elems));
384
385         while (left >= 2) {
386                 u8 id, elen;
387
388                 id = *pos++;
389                 elen = *pos++;
390                 left -= 2;
391
392                 if (elen > left) {
393                         if (show_errors) {
394                                 wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
395                                            "parse failed (id=%d elen=%d "
396                                            "left=%lu)",
397                                            id, elen, (unsigned long) left);
398                                 wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
399                         }
400                         return ParseFailed;
401                 }
402
403                 switch (id) {
404                 case WLAN_EID_SSID:
405                         elems->ssid = pos;
406                         elems->ssid_len = elen;
407                         break;
408                 case WLAN_EID_SUPP_RATES:
409                         elems->supp_rates = pos;
410                         elems->supp_rates_len = elen;
411                         break;
412                 case WLAN_EID_FH_PARAMS:
413                         elems->fh_params = pos;
414                         elems->fh_params_len = elen;
415                         break;
416                 case WLAN_EID_DS_PARAMS:
417                         elems->ds_params = pos;
418                         elems->ds_params_len = elen;
419                         break;
420                 case WLAN_EID_CF_PARAMS:
421                         elems->cf_params = pos;
422                         elems->cf_params_len = elen;
423                         break;
424                 case WLAN_EID_TIM:
425                         elems->tim = pos;
426                         elems->tim_len = elen;
427                         break;
428                 case WLAN_EID_IBSS_PARAMS:
429                         elems->ibss_params = pos;
430                         elems->ibss_params_len = elen;
431                         break;
432                 case WLAN_EID_CHALLENGE:
433                         elems->challenge = pos;
434                         elems->challenge_len = elen;
435                         break;
436                 case WLAN_EID_ERP_INFO:
437                         elems->erp_info = pos;
438                         elems->erp_info_len = elen;
439                         break;
440                 case WLAN_EID_EXT_SUPP_RATES:
441                         elems->ext_supp_rates = pos;
442                         elems->ext_supp_rates_len = elen;
443                         break;
444                 case WLAN_EID_VENDOR_SPECIFIC:
445                         if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
446                                                              elems,
447                                                              show_errors))
448                                 unknown++;
449                         break;
450                 case WLAN_EID_RSN:
451                         elems->rsn_ie = pos;
452                         elems->rsn_ie_len = elen;
453                         break;
454                 case WLAN_EID_PWR_CAPABILITY:
455                         elems->power_cap = pos;
456                         elems->power_cap_len = elen;
457                         break;
458                 case WLAN_EID_SUPPORTED_CHANNELS:
459                         elems->supp_channels = pos;
460                         elems->supp_channels_len = elen;
461                         break;
462                 case WLAN_EID_MOBILITY_DOMAIN:
463                         elems->mdie = pos;
464                         elems->mdie_len = elen;
465                         break;
466                 case WLAN_EID_FAST_BSS_TRANSITION:
467                         elems->ftie = pos;
468                         elems->ftie_len = elen;
469                         break;
470                 case WLAN_EID_HT_CAP:
471                         elems->ht_capabilities = pos;
472                         elems->ht_capabilities_len = elen;
473                         break;
474                 case WLAN_EID_HT_OPERATION:
475                         elems->ht_operation = pos;
476                         elems->ht_operation_len = elen;
477                         break;
478                 default:
479                         unknown++;
480                         if (!show_errors)
481                                 break;
482                         wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
483                                    "ignored unknown element (id=%d elen=%d)",
484                                    id, elen);
485                         break;
486                 }
487
488                 left -= elen;
489                 pos += elen;
490         }
491
492         if (left)
493                 return ParseFailed;
494
495         return unknown ? ParseUnknown : ParseOK;
496 }
497
498
499 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
500 {
501         int i;
502         if (len > HOSTAPD_MAX_SSID_LEN)
503                 len = HOSTAPD_MAX_SSID_LEN;
504         for (i = 0; i < len; i++) {
505                 if (ssid[i] >= 32 && ssid[i] < 127)
506                         buf[i] = ssid[i];
507                 else
508                         buf[i] = '.';
509         }
510         buf[len] = '\0';
511 }
512
513
514 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
515 {
516         struct ieee80211_mgmt mgmt;
517         char buf[30];
518
519         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
520                        HOSTAPD_LEVEL_DEBUG,
521                        "deauthenticate - reason %d", reason);
522         os_snprintf(buf, sizeof(buf), "SEND-DEAUTHENTICATE %d", reason);
523         os_memset(&mgmt, 0, sizeof(mgmt));
524         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
525                                           WLAN_FC_STYPE_DEAUTH);
526         os_memcpy(mgmt.da, addr, ETH_ALEN);
527         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
528         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
529         mgmt.u.deauth.reason_code = host_to_le16(reason);
530         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
531                                     sizeof(mgmt.u.deauth), 0) < 0)
532                 perror("ieee802_11_send_deauth: send");
533 }
534
535
536 static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
537 {
538         struct hostapd_data *hapd = eloop_ctx;
539         struct ieee80211_mgmt mgmt;
540         char ssid_txt[33];
541
542         if (hapd->assoc_ap_state == WAIT_BEACON)
543                 hapd->assoc_ap_state = AUTHENTICATE;
544         if (hapd->assoc_ap_state != AUTHENTICATE)
545                 return;
546
547         ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
548                               hapd->assoc_ap_ssid_len);
549         printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
550                MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
551
552         os_memset(&mgmt, 0, sizeof(mgmt));
553         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
554                                           WLAN_FC_STYPE_AUTH);
555         /* Request TX callback */
556         mgmt.frame_control |= host_to_le16(BIT(1));
557         os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
558         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
559         os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
560         mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
561         mgmt.u.auth.auth_transaction = host_to_le16(1);
562         mgmt.u.auth.status_code = host_to_le16(0);
563         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
564                                     sizeof(mgmt.u.auth), 0) < 0)
565                 perror("ieee802_11_sta_authenticate: send");
566
567         /* Try to authenticate again, if this attempt fails or times out. */
568         eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
569 }
570
571
572 static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
573 {
574         struct hostapd_data *hapd = eloop_ctx;
575         u8 buf[256];
576         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
577         u8 *p;
578         char ssid_txt[33];
579
580         if (hapd->assoc_ap_state == AUTHENTICATE)
581                 hapd->assoc_ap_state = ASSOCIATE;
582         if (hapd->assoc_ap_state != ASSOCIATE)
583                 return;
584
585         ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
586                               hapd->assoc_ap_ssid_len);
587         printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
588                MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
589
590         os_memset(mgmt, 0, sizeof(*mgmt));
591         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
592                                           WLAN_FC_STYPE_ASSOC_REQ);
593         /* Request TX callback */
594         mgmt->frame_control |= host_to_le16(BIT(1));
595         os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
596         os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
597         os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
598         mgmt->u.assoc_req.capab_info = host_to_le16(0);
599         mgmt->u.assoc_req.listen_interval = host_to_le16(1);
600         p = &mgmt->u.assoc_req.variable[0];
601
602         *p++ = WLAN_EID_SSID;
603         *p++ = hapd->assoc_ap_ssid_len;
604         os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
605         p += hapd->assoc_ap_ssid_len;
606
607         p = hostapd_eid_supp_rates(hapd, p);
608         p = hostapd_eid_ext_supp_rates(hapd, p);
609
610         if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
611                 perror("ieee802_11_sta_associate: send");
612
613         /* Try to authenticate again, if this attempt fails or times out. */
614         eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
615 }
616
617
618 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
619                            u16 auth_transaction, u8 *challenge, int iswep)
620 {
621         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
622                        HOSTAPD_LEVEL_DEBUG,
623                        "authentication (shared key, transaction %d)",
624                        auth_transaction);
625
626         if (auth_transaction == 1) {
627                 if (!sta->challenge) {
628                         /* Generate a pseudo-random challenge */
629                         u8 key[8];
630                         time_t now;
631                         int r;
632                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
633                         if (sta->challenge == NULL)
634                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
635
636                         now = time(NULL);
637                         r = random();
638                         os_memcpy(key, &now, 4);
639                         os_memcpy(key + 4, &r, 4);
640                         rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
641                             key, sizeof(key));
642                 }
643                 return 0;
644         }
645
646         if (auth_transaction != 3)
647                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
648
649         /* Transaction 3 */
650         if (!iswep || !sta->challenge || !challenge ||
651             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
652                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
653                                HOSTAPD_LEVEL_INFO,
654                                "shared key authentication - invalid "
655                                "challenge-response");
656                 return WLAN_STATUS_CHALLENGE_FAIL;
657         }
658
659         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
660                        HOSTAPD_LEVEL_DEBUG,
661                        "authentication OK (shared key)");
662 #ifdef IEEE80211_REQUIRE_AUTH_ACK
663         /* Station will be marked authenticated if it ACKs the
664          * authentication reply. */
665 #else
666         sta->flags |= WLAN_STA_AUTH;
667         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
668 #endif
669         os_free(sta->challenge);
670         sta->challenge = NULL;
671
672         return 0;
673 }
674
675
676 static void send_auth_reply(struct hostapd_data *hapd,
677                             const u8 *dst, const u8 *bssid,
678                             u16 auth_alg, u16 auth_transaction, u16 resp,
679                             const u8 *ies, size_t ies_len)
680 {
681         struct ieee80211_mgmt *reply;
682         u8 *buf;
683         size_t rlen;
684
685         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
686         buf = os_zalloc(rlen);
687         if (buf == NULL)
688                 return;
689
690         reply = (struct ieee80211_mgmt *) buf;
691         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
692                                             WLAN_FC_STYPE_AUTH);
693         /* Request TX callback */
694         reply->frame_control |= host_to_le16(BIT(1));
695         os_memcpy(reply->da, dst, ETH_ALEN);
696         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
697         os_memcpy(reply->bssid, bssid, ETH_ALEN);
698
699         reply->u.auth.auth_alg = host_to_le16(auth_alg);
700         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
701         reply->u.auth.status_code = host_to_le16(resp);
702
703         if (ies && ies_len)
704                 os_memcpy(reply->u.auth.variable, ies, ies_len);
705
706         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
707                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
708                    MAC2STR(dst), auth_alg, auth_transaction,
709                    resp, (unsigned long) ies_len);
710         if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
711                 perror("send_auth_reply: send");
712
713         os_free(buf);
714 }
715
716
717 #ifdef CONFIG_IEEE80211R
718 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
719                                   u16 auth_transaction, u16 status,
720                                   const u8 *ies, size_t ies_len)
721 {
722         struct hostapd_data *hapd = ctx;
723         struct sta_info *sta;
724
725         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
726                         status, ies, ies_len);
727
728         if (status != WLAN_STATUS_SUCCESS)
729                 return;
730
731         sta = ap_get_sta(hapd, dst);
732         if (sta == NULL)
733                 return;
734
735         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
736                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
737         sta->flags |= WLAN_STA_AUTH;
738         mlme_authenticate_indication(hapd, sta);
739 }
740 #endif /* CONFIG_IEEE80211R */
741
742
743 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
744                         size_t len)
745 {
746         u16 auth_alg, auth_transaction, status_code;
747         u16 resp = WLAN_STATUS_SUCCESS;
748         struct sta_info *sta = NULL;
749         int res;
750         u16 fc;
751         u8 *challenge = NULL;
752         u32 session_timeout, acct_interim_interval;
753         int vlan_id = 0;
754         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
755         size_t resp_ies_len = 0;
756
757         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
758                 printf("handle_auth - too short payload (len=%lu)\n",
759                        (unsigned long) len);
760                 return;
761         }
762
763         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
764         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
765         status_code = le_to_host16(mgmt->u.auth.status_code);
766         fc = le_to_host16(mgmt->frame_control);
767
768         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
769             2 + WLAN_AUTH_CHALLENGE_LEN &&
770             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
771             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
772                 challenge = &mgmt->u.auth.variable[2];
773
774         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
775                    "auth_transaction=%d status_code=%d wep=%d%s",
776                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
777                    status_code, !!(fc & WLAN_FC_ISWEP),
778                    challenge ? " challenge" : "");
779
780         if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
781             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
782             os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
783                 if (status_code != 0) {
784                         printf("Authentication (as station) with AP "
785                                MACSTR " failed (status_code=%d)\n",
786                                MAC2STR(hapd->conf->assoc_ap_addr),
787                                status_code);
788                         return;
789                 }
790                 printf("Authenticated (as station) with AP " MACSTR "\n",
791                        MAC2STR(hapd->conf->assoc_ap_addr));
792                 ieee802_11_sta_associate(hapd, NULL);
793                 return;
794         }
795
796         if (hapd->tkip_countermeasures) {
797                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
798                 goto fail;
799         }
800
801         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
802                auth_alg == WLAN_AUTH_OPEN) ||
803 #ifdef CONFIG_IEEE80211R
804               (hapd->conf->wpa &&
805                (hapd->conf->wpa_key_mgmt &
806                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
807                auth_alg == WLAN_AUTH_FT) ||
808 #endif /* CONFIG_IEEE80211R */
809               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
810                auth_alg == WLAN_AUTH_SHARED_KEY))) {
811                 printf("Unsupported authentication algorithm (%d)\n",
812                        auth_alg);
813                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
814                 goto fail;
815         }
816
817         if (!(auth_transaction == 1 ||
818               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
819                 printf("Unknown authentication transaction number (%d)\n",
820                        auth_transaction);
821                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
822                 goto fail;
823         }
824
825         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
826                 printf("Station " MACSTR " not allowed to authenticate.\n",
827                        MAC2STR(mgmt->sa));
828                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
829                 goto fail;
830         }
831
832         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
833                                       &session_timeout,
834                                       &acct_interim_interval, &vlan_id);
835         if (res == HOSTAPD_ACL_REJECT) {
836                 printf("Station " MACSTR " not allowed to authenticate.\n",
837                        MAC2STR(mgmt->sa));
838                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
839                 goto fail;
840         }
841         if (res == HOSTAPD_ACL_PENDING) {
842                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
843                            " waiting for an external authentication",
844                            MAC2STR(mgmt->sa));
845                 /* Authentication code will re-send the authentication frame
846                  * after it has received (and cached) information from the
847                  * external source. */
848                 return;
849         }
850
851         sta = ap_sta_add(hapd, mgmt->sa);
852         if (!sta) {
853                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
854                 goto fail;
855         }
856
857         if (vlan_id > 0) {
858                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
859                                                sta->vlan_id) == NULL) {
860                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
861                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
862                                        "%d received from RADIUS server",
863                                        vlan_id);
864                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
865                         goto fail;
866                 }
867                 sta->vlan_id = vlan_id;
868                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
869                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
870         }
871
872         sta->flags &= ~WLAN_STA_PREAUTH;
873         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
874
875         if (hapd->conf->radius->acct_interim_interval == 0 &&
876             acct_interim_interval)
877                 sta->acct_interim_interval = acct_interim_interval;
878         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
879                 ap_sta_session_timeout(hapd, sta, session_timeout);
880         else
881                 ap_sta_no_session_timeout(hapd, sta);
882
883         switch (auth_alg) {
884         case WLAN_AUTH_OPEN:
885                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
886                                HOSTAPD_LEVEL_DEBUG,
887                                "authentication OK (open system)");
888 #ifdef IEEE80211_REQUIRE_AUTH_ACK
889                 /* Station will be marked authenticated if it ACKs the
890                  * authentication reply. */
891 #else
892                 sta->flags |= WLAN_STA_AUTH;
893                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
894                 sta->auth_alg = WLAN_AUTH_OPEN;
895                 mlme_authenticate_indication(hapd, sta);
896 #endif
897                 break;
898         case WLAN_AUTH_SHARED_KEY:
899                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
900                                        fc & WLAN_FC_ISWEP);
901                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
902                 mlme_authenticate_indication(hapd, sta);
903                 if (sta->challenge && auth_transaction == 1) {
904                         resp_ies[0] = WLAN_EID_CHALLENGE;
905                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
906                         os_memcpy(resp_ies + 2, sta->challenge,
907                                   WLAN_AUTH_CHALLENGE_LEN);
908                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
909                 }
910                 break;
911 #ifdef CONFIG_IEEE80211R
912         case WLAN_AUTH_FT:
913                 sta->auth_alg = WLAN_AUTH_FT;
914                 if (sta->wpa_sm == NULL)
915                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
916                                                         sta->addr);
917                 if (sta->wpa_sm == NULL) {
918                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
919                                    "state machine");
920                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
921                         goto fail;
922                 }
923                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
924                                     auth_transaction, mgmt->u.auth.variable,
925                                     len - IEEE80211_HDRLEN -
926                                     sizeof(mgmt->u.auth),
927                                     handle_auth_ft_finish, hapd);
928                 /* handle_auth_ft_finish() callback will complete auth. */
929                 return;
930 #endif /* CONFIG_IEEE80211R */
931         }
932
933  fail:
934         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
935                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
936 }
937
938
939 static void handle_assoc(struct hostapd_data *hapd,
940                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
941 {
942         u16 capab_info, listen_interval;
943         u16 resp = WLAN_STATUS_SUCCESS;
944         u8 *pos, *wpa_ie;
945         size_t wpa_ie_len;
946         int send_deauth = 0, send_len, left, i;
947         struct sta_info *sta;
948         struct ieee802_11_elems elems;
949         u8 buf[sizeof(struct ieee80211_mgmt) + 512];
950         struct ieee80211_mgmt *reply;
951
952         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
953                                       sizeof(mgmt->u.assoc_req))) {
954                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
955                        "\n", reassoc, (unsigned long) len);
956                 return;
957         }
958
959         if (reassoc) {
960                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
961                 listen_interval = le_to_host16(
962                         mgmt->u.reassoc_req.listen_interval);
963                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
964                            " capab_info=0x%02x listen_interval=%d current_ap="
965                            MACSTR,
966                            MAC2STR(mgmt->sa), capab_info, listen_interval,
967                            MAC2STR(mgmt->u.reassoc_req.current_ap));
968                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
969                 pos = mgmt->u.reassoc_req.variable;
970         } else {
971                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
972                 listen_interval = le_to_host16(
973                         mgmt->u.assoc_req.listen_interval);
974                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
975                            " capab_info=0x%02x listen_interval=%d",
976                            MAC2STR(mgmt->sa), capab_info, listen_interval);
977                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
978                 pos = mgmt->u.assoc_req.variable;
979         }
980
981         sta = ap_get_sta(hapd, mgmt->sa);
982 #ifdef CONFIG_IEEE80211R
983         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
984             (sta->flags & WLAN_STA_AUTH) == 0) {
985                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
986                            "prior to authentication since it is using "
987                            "over-the-DS FT", MAC2STR(mgmt->sa));
988         } else
989 #endif /* CONFIG_IEEE80211R */
990         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
991                 printf("STA " MACSTR " trying to associate before "
992                        "authentication\n", MAC2STR(mgmt->sa));
993                 if (sta) {
994                         printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
995                                MAC2STR(sta->addr), sta->aid, sta->flags);
996                 }
997                 send_deauth = 1;
998                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
999                 goto fail;
1000         }
1001
1002         if (hapd->tkip_countermeasures) {
1003                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1004                 goto fail;
1005         }
1006
1007         if (listen_interval > hapd->conf->max_listen_interval) {
1008                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1009                                HOSTAPD_LEVEL_DEBUG,
1010                                "Too large Listen Interval (%d)",
1011                                listen_interval);
1012                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1013                 goto fail;
1014         }
1015
1016         sta->capability = capab_info;
1017
1018         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1019          * is used */
1020         if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
1021             || !elems.ssid) {
1022                 printf("STA " MACSTR " sent invalid association request\n",
1023                        MAC2STR(sta->addr));
1024                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1025                 goto fail;
1026         }
1027
1028         if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1029             os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
1030         {
1031                 char ssid_txt[33];
1032                 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
1033                 printf("Station " MACSTR " tried to associate with "
1034                        "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
1035                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1036                 goto fail;
1037         }
1038
1039         sta->flags &= ~WLAN_STA_WME;
1040         if (elems.wme && hapd->conf->wme_enabled) {
1041                 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
1042                         hostapd_logger(hapd, sta->addr,
1043                                        HOSTAPD_MODULE_WPA,
1044                                        HOSTAPD_LEVEL_DEBUG,
1045                                        "invalid WME element in association "
1046                                        "request");
1047                 else
1048                         sta->flags |= WLAN_STA_WME;
1049         }
1050
1051         if (!elems.supp_rates) {
1052                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1053                                HOSTAPD_LEVEL_DEBUG,
1054                                "No supported rates element in AssocReq");
1055                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1056                 goto fail;
1057         }
1058
1059         if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
1060                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1061                                HOSTAPD_LEVEL_DEBUG,
1062                                "Invalid supported rates element length %d",
1063                                elems.supp_rates_len);
1064                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1065                 goto fail;
1066         }
1067
1068         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
1069         os_memcpy(sta->supported_rates, elems.supp_rates,
1070                   elems.supp_rates_len);
1071         sta->supported_rates_len = elems.supp_rates_len;
1072
1073         if (elems.ext_supp_rates) {
1074                 if (elems.supp_rates_len + elems.ext_supp_rates_len >
1075                     sizeof(sta->supported_rates)) {
1076                         hostapd_logger(hapd, mgmt->sa,
1077                                        HOSTAPD_MODULE_IEEE80211,
1078                                        HOSTAPD_LEVEL_DEBUG,
1079                                        "Invalid supported rates element length"
1080                                        " %d+%d", elems.supp_rates_len,
1081                                        elems.ext_supp_rates_len);
1082                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1083                         goto fail;
1084                 }
1085
1086                 os_memcpy(sta->supported_rates + elems.supp_rates_len,
1087                           elems.ext_supp_rates, elems.ext_supp_rates_len);
1088                 sta->supported_rates_len += elems.ext_supp_rates_len;
1089         }
1090
1091 #ifdef CONFIG_IEEE80211N
1092         /* save HT capabilities in the sta object */
1093         os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
1094         if (elems.ht_capabilities &&
1095             elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
1096             && (sta->flags & WLAN_STA_WME)) {
1097                 /* note: without WMM capability, treat the sta as non-HT */
1098                 sta->flags |= WLAN_STA_HT;
1099                 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
1100                 sta->ht_capabilities.length =
1101                         sizeof(struct ieee80211_ht_capability);
1102                 os_memcpy(&sta->ht_capabilities.data,
1103                           elems.ht_capabilities,
1104                           sizeof(struct ieee80211_ht_capability));
1105         } else
1106                 sta->flags &= ~WLAN_STA_HT;
1107 #endif /* CONFIG_IEEE80211N */
1108
1109         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1110                 wpa_ie = elems.rsn_ie;
1111                 wpa_ie_len = elems.rsn_ie_len;
1112         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1113                    elems.wpa_ie) {
1114                 wpa_ie = elems.wpa_ie;
1115                 wpa_ie_len = elems.wpa_ie_len;
1116         } else {
1117                 wpa_ie = NULL;
1118                 wpa_ie_len = 0;
1119         }
1120         if (hapd->conf->wpa && wpa_ie == NULL) {
1121                 printf("STA " MACSTR ": No WPA/RSN IE in association "
1122                        "request\n", MAC2STR(sta->addr));
1123                 resp = WLAN_STATUS_INVALID_IE;
1124                 goto fail;
1125         }
1126
1127         if (hapd->conf->wpa && wpa_ie) {
1128                 int res;
1129                 wpa_ie -= 2;
1130                 wpa_ie_len += 2;
1131                 if (sta->wpa_sm == NULL)
1132                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1133                                                         sta->addr);
1134                 if (sta->wpa_sm == NULL) {
1135                         printf("Failed to initialize WPA state machine\n");
1136                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1137                         goto fail;
1138                 }
1139                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1140                                           wpa_ie, wpa_ie_len,
1141                                           elems.mdie, elems.mdie_len);
1142                 if (res == WPA_INVALID_GROUP)
1143                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1144                 else if (res == WPA_INVALID_PAIRWISE)
1145                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1146                 else if (res == WPA_INVALID_AKMP)
1147                         resp = WLAN_STATUS_AKMP_NOT_VALID;
1148                 else if (res == WPA_ALLOC_FAIL)
1149                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1150 #ifdef CONFIG_IEEE80211W
1151                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1152                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1153                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1154                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1155 #endif /* CONFIG_IEEE80211W */
1156                 else if (res == WPA_INVALID_MDIE)
1157                         resp = WLAN_STATUS_INVALID_MDIE;
1158                 else if (res != WPA_IE_OK)
1159                         resp = WLAN_STATUS_INVALID_IE;
1160                 if (resp != WLAN_STATUS_SUCCESS)
1161                         goto fail;
1162 #ifdef CONFIG_IEEE80211W
1163                 if (wpa_auth_uses_mfp(sta->wpa_sm))
1164                         sta->flags |= WLAN_STA_MFP;
1165                 else
1166                         sta->flags &= ~WLAN_STA_MFP;
1167 #endif /* CONFIG_IEEE80211W */
1168
1169 #ifdef CONFIG_IEEE80211R
1170                 if (sta->auth_alg == WLAN_AUTH_FT) {
1171                         if (!reassoc) {
1172                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1173                                            "to use association (not "
1174                                            "re-association) with FT auth_alg",
1175                                            MAC2STR(sta->addr));
1176                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1177                                 goto fail;
1178                         }
1179
1180                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
1181                         if (resp != WLAN_STATUS_SUCCESS)
1182                                 goto fail;
1183                 }
1184 #endif /* CONFIG_IEEE80211R */
1185         }
1186
1187         if (hapd->iface->dfs_enable &&
1188             hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
1189                 if (hostapd_check_power_cap(hapd, elems.power_cap,
1190                                             elems.power_cap_len)) {
1191                         resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
1192                         hostapd_logger(hapd, sta->addr,
1193                                        HOSTAPD_MODULE_IEEE80211,
1194                                        HOSTAPD_LEVEL_DEBUG,
1195                                        "Power capabilities of the station not "
1196                                        "acceptable");
1197                         goto fail;
1198                 }
1199         }
1200
1201         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1202                 sta->flags |= WLAN_STA_NONERP;
1203         for (i = 0; i < sta->supported_rates_len; i++) {
1204                 if ((sta->supported_rates[i] & 0x7f) > 22) {
1205                         sta->flags &= ~WLAN_STA_NONERP;
1206                         break;
1207                 }
1208         }
1209         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1210                 sta->nonerp_set = 1;
1211                 hapd->iface->num_sta_non_erp++;
1212                 if (hapd->iface->num_sta_non_erp == 1)
1213                         ieee802_11_set_beacons(hapd->iface);
1214         }
1215
1216         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1217             !sta->no_short_slot_time_set) {
1218                 sta->no_short_slot_time_set = 1;
1219                 hapd->iface->num_sta_no_short_slot_time++;
1220                 if (hapd->iface->current_mode->mode ==
1221                     HOSTAPD_MODE_IEEE80211G &&
1222                     hapd->iface->num_sta_no_short_slot_time == 1)
1223                         ieee802_11_set_beacons(hapd->iface);
1224         }
1225
1226         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1227                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1228         else
1229                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1230
1231         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1232             !sta->no_short_preamble_set) {
1233                 sta->no_short_preamble_set = 1;
1234                 hapd->iface->num_sta_no_short_preamble++;
1235                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1236                     && hapd->iface->num_sta_no_short_preamble == 1)
1237                         ieee802_11_set_beacons(hapd->iface);
1238         }
1239
1240 #ifdef CONFIG_IEEE80211N
1241         if (sta->flags & WLAN_STA_HT) {
1242                 if ((sta->ht_capabilities.data.capabilities_info &
1243                      HT_CAP_INFO_GREEN_FIELD) == 0) {
1244                         hapd->iface->num_sta_ht_no_gf++;
1245                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1246                                    "greenfield, num of non-gf stations %d",
1247                                    __func__, MAC2STR(sta->addr),
1248                                    hapd->iface->num_sta_ht_no_gf);
1249                 }
1250                 if ((sta->ht_capabilities.data.capabilities_info &
1251                      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1252                         hapd->iface->num_sta_ht_20mhz++;
1253                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1254                                    "num of 20MHz HT STAs %d",
1255                                    __func__, MAC2STR(sta->addr),
1256                                    hapd->iface->num_sta_ht_20mhz);
1257                 }
1258         } else {
1259                 hapd->iface->num_sta_no_ht++;
1260                 if (hapd->iconf->ieee80211n) {
1261                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1262                                    " - no HT, num of non-HT stations %d",
1263                                    __func__, MAC2STR(sta->addr),
1264                                    hapd->iface->num_sta_no_ht);
1265                 }
1266         }
1267
1268         if (hostapd_ht_operation_update(hapd->iface) > 0)
1269                 ieee802_11_set_beacons(hapd->iface);
1270 #endif /* CONFIG_IEEE80211N */
1271
1272         /* get a unique AID */
1273         if (sta->aid > 0) {
1274                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1275         } else {
1276                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1277                         if (hapd->sta_aid[sta->aid - 1] == NULL)
1278                                 break;
1279                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1280                         sta->aid = 0;
1281                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1282                         wpa_printf(MSG_ERROR, "  no room for more AIDs");
1283                         goto fail;
1284                 } else {
1285                         hapd->sta_aid[sta->aid - 1] = sta;
1286                         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1287                 }
1288         }
1289
1290         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1291                        HOSTAPD_LEVEL_DEBUG,
1292                        "association OK (aid %d)", sta->aid);
1293         /* Station will be marked associated, after it acknowledges AssocResp
1294          */
1295
1296         if (reassoc) {
1297                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1298                           ETH_ALEN);
1299         }
1300
1301         if (sta->last_assoc_req)
1302                 os_free(sta->last_assoc_req);
1303         sta->last_assoc_req = os_malloc(len);
1304         if (sta->last_assoc_req)
1305                 os_memcpy(sta->last_assoc_req, mgmt, len);
1306
1307         /* Make sure that the previously registered inactivity timer will not
1308          * remove the STA immediately. */
1309         sta->timeout_next = STA_NULLFUNC;
1310
1311  fail:
1312         os_memset(buf, 0, sizeof(buf));
1313         reply = (struct ieee80211_mgmt *) buf;
1314         reply->frame_control =
1315                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1316                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1317                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1318                                WLAN_FC_STYPE_ASSOC_RESP)));
1319         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1320         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1321         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1322
1323         send_len = IEEE80211_HDRLEN;
1324         if (send_deauth) {
1325                 send_len += sizeof(reply->u.deauth);
1326                 reply->u.deauth.reason_code = host_to_le16(resp);
1327         } else {
1328                 u8 *p;
1329                 send_len += sizeof(reply->u.assoc_resp);
1330                 reply->u.assoc_resp.capab_info =
1331                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1332                 reply->u.assoc_resp.status_code = host_to_le16(resp);
1333                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1334                                                        | BIT(14) | BIT(15));
1335                 /* Supported rates */
1336                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1337                 /* Extended supported rates */
1338                 p = hostapd_eid_ext_supp_rates(hapd, p);
1339                 if (sta->flags & WLAN_STA_WME)
1340                         p = hostapd_eid_wme(hapd, p);
1341
1342                 p = hostapd_eid_ht_capabilities_info(hapd, p);
1343                 p = hostapd_eid_ht_operation(hapd, p);
1344
1345 #ifdef CONFIG_IEEE80211R
1346                 if (resp == WLAN_STATUS_SUCCESS) {
1347                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1348                          * Transition Information, RSN */
1349                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1350                                                         buf + sizeof(buf) - p,
1351                                                         sta->auth_alg);
1352                 }
1353 #endif /* CONFIG_IEEE80211R */
1354
1355                 send_len += p - reply->u.assoc_resp.variable;
1356
1357                 /* Request TX callback */
1358                 reply->frame_control |= host_to_le16(BIT(1));
1359         }
1360
1361         if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1362                 perror("handle_assoc: send");
1363 }
1364
1365
1366 static void handle_assoc_resp(struct hostapd_data *hapd,
1367                               struct ieee80211_mgmt *mgmt, size_t len)
1368 {
1369         u16 status_code, aid;
1370
1371         if (hapd->assoc_ap_state != ASSOCIATE) {
1372                 printf("Unexpected association response received from " MACSTR
1373                        "\n", MAC2STR(mgmt->sa));
1374                 return;
1375         }
1376
1377         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
1378                 printf("handle_assoc_resp - too short payload (len=%lu)\n",
1379                        (unsigned long) len);
1380                 return;
1381         }
1382
1383         if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
1384             os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
1385                 printf("Received association response from unexpected address "
1386                        "(SA=" MACSTR " BSSID=" MACSTR "\n",
1387                        MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1388                 return;
1389         }
1390
1391         status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1392         aid = le_to_host16(mgmt->u.assoc_resp.aid);
1393         aid &= ~(BIT(14) | BIT(15));
1394
1395         if (status_code != 0) {
1396                 printf("Association (as station) with AP " MACSTR " failed "
1397                        "(status_code=%d)\n",
1398                        MAC2STR(hapd->conf->assoc_ap_addr), status_code);
1399                 /* Try to authenticate again */
1400                 hapd->assoc_ap_state = AUTHENTICATE;
1401                 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
1402                                        hapd, NULL);
1403         }
1404
1405         printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
1406                MAC2STR(hapd->conf->assoc_ap_addr), aid);
1407         hapd->assoc_ap_aid = aid;
1408         hapd->assoc_ap_state = ASSOCIATED;
1409
1410         if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
1411                 printf("Could not set associated AP address to kernel "
1412                        "driver.\n");
1413         }
1414 }
1415
1416
1417 static void handle_disassoc(struct hostapd_data *hapd,
1418                             struct ieee80211_mgmt *mgmt, size_t len)
1419 {
1420         struct sta_info *sta;
1421
1422         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1423                 printf("handle_disassoc - too short payload (len=%lu)\n",
1424                        (unsigned long) len);
1425                 return;
1426         }
1427
1428         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1429                    MAC2STR(mgmt->sa),
1430                    le_to_host16(mgmt->u.disassoc.reason_code));
1431
1432         if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1433             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1434                 printf("Assoc AP " MACSTR " sent disassociation "
1435                        "(reason_code=%d) - try to authenticate\n",
1436                        MAC2STR(hapd->conf->assoc_ap_addr),
1437                        le_to_host16(mgmt->u.disassoc.reason_code));
1438                 hapd->assoc_ap_state = AUTHENTICATE;
1439                 ieee802_11_sta_authenticate(hapd, NULL);
1440                 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1441                                        hapd, NULL);
1442                 return;
1443         }
1444
1445         sta = ap_get_sta(hapd, mgmt->sa);
1446         if (sta == NULL) {
1447                 printf("Station " MACSTR " trying to disassociate, but it "
1448                        "is not associated.\n", MAC2STR(mgmt->sa));
1449                 return;
1450         }
1451
1452         sta->flags &= ~WLAN_STA_ASSOC;
1453         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1454         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1455                        HOSTAPD_LEVEL_INFO, "disassociated");
1456         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1457         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1458         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1459          * authenticated. */
1460         accounting_sta_stop(hapd, sta);
1461         ieee802_1x_free_station(sta);
1462         hostapd_sta_remove(hapd, sta->addr);
1463
1464         if (sta->timeout_next == STA_NULLFUNC ||
1465             sta->timeout_next == STA_DISASSOC) {
1466                 sta->timeout_next = STA_DEAUTH;
1467                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1468                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1469                                        hapd, sta);
1470         }
1471
1472         mlme_disassociate_indication(
1473                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1474 }
1475
1476
1477 static void handle_deauth(struct hostapd_data *hapd,
1478                           struct ieee80211_mgmt *mgmt, size_t len)
1479 {
1480         struct sta_info *sta;
1481
1482         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1483                 printf("handle_deauth - too short payload (len=%lu)\n",
1484                        (unsigned long) len);
1485                 return;
1486         }
1487
1488         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1489                    " reason_code=%d",
1490                    MAC2STR(mgmt->sa),
1491                    le_to_host16(mgmt->u.deauth.reason_code));
1492
1493         if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1494             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1495                 printf("Assoc AP " MACSTR " sent deauthentication "
1496                        "(reason_code=%d) - try to authenticate\n",
1497                        MAC2STR(hapd->conf->assoc_ap_addr),
1498                        le_to_host16(mgmt->u.deauth.reason_code));
1499                 hapd->assoc_ap_state = AUTHENTICATE;
1500                 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1501                                        hapd, NULL);
1502                 return;
1503         }
1504
1505         sta = ap_get_sta(hapd, mgmt->sa);
1506         if (sta == NULL) {
1507                 printf("Station " MACSTR " trying to deauthenticate, but it "
1508                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1509                 return;
1510         }
1511
1512         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1513         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1514         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1515                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1516         mlme_deauthenticate_indication(
1517                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1518         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1519         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1520         ap_free_sta(hapd, sta);
1521 }
1522
1523
1524 static void handle_beacon(struct hostapd_data *hapd,
1525                           struct ieee80211_mgmt *mgmt, size_t len,
1526                           struct hostapd_frame_info *fi)
1527 {
1528         struct ieee802_11_elems elems;
1529
1530         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1531                 printf("handle_beacon - too short payload (len=%lu)\n",
1532                        (unsigned long) len);
1533                 return;
1534         }
1535
1536         (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
1537                                       len - (IEEE80211_HDRLEN +
1538                                              sizeof(mgmt->u.beacon)), &elems,
1539                                       0);
1540
1541         if (hapd->assoc_ap_state == WAIT_BEACON &&
1542             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1543                 if (elems.ssid && elems.ssid_len <= 32) {
1544                         os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
1545                                   elems.ssid_len);
1546                         hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
1547                         hapd->assoc_ap_ssid_len = elems.ssid_len;
1548                 }
1549                 ieee802_11_sta_authenticate(hapd, NULL);
1550         }
1551
1552         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1553 }
1554
1555
1556 static void handle_action(struct hostapd_data *hapd,
1557                           struct ieee80211_mgmt *mgmt, size_t len)
1558 {
1559         if (len < IEEE80211_HDRLEN + 1) {
1560                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1561                                HOSTAPD_LEVEL_DEBUG,
1562                                "handle_action - too short payload (len=%lu)",
1563                                (unsigned long) len);
1564                 return;
1565         }
1566
1567         switch (mgmt->u.action.category) {
1568 #ifdef CONFIG_IEEE80211R
1569         case WLAN_ACTION_FT:
1570         {
1571                 struct sta_info *sta;
1572
1573                 sta = ap_get_sta(hapd, mgmt->sa);
1574                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1575                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1576                                    "frame from unassociated STA " MACSTR,
1577                                    MAC2STR(mgmt->sa));
1578                         return;
1579                 }
1580
1581                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1582                                      len - IEEE80211_HDRLEN))
1583                         break;
1584
1585                 return;
1586         }
1587 #endif /* CONFIG_IEEE80211R */
1588         case WME_ACTION_CATEGORY:
1589                 hostapd_wme_action(hapd, mgmt, len);
1590                 return;
1591         }
1592
1593         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1594                        HOSTAPD_LEVEL_DEBUG,
1595                        "handle_action - unknown action category %d or invalid "
1596                        "frame",
1597                        mgmt->u.action.category);
1598         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1599             !(mgmt->sa[0] & 0x01)) {
1600                 /*
1601                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1602                  * Return the Action frame to the source without change
1603                  * except that MSB of the Category set to 1.
1604                  */
1605                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1606                            "frame back to sender");
1607                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1608                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1609                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1610                 mgmt->u.action.category |= 0x80;
1611
1612                 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1613         }
1614 }
1615
1616
1617 /**
1618  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1619  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1620  * sent to)
1621  * @buf: management frame data (starting from IEEE 802.11 header)
1622  * @len: length of frame data in octets
1623  * @stype: management frame subtype from frame control field
1624  *
1625  * Process all incoming IEEE 802.11 management frames. This will be called for
1626  * each frame received from the kernel driver through wlan#ap interface. In
1627  * addition, it can be called to re-inserted pending frames (e.g., when using
1628  * external RADIUS server as an MAC ACL).
1629  */
1630 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1631                      struct hostapd_frame_info *fi)
1632 {
1633         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1634         int broadcast;
1635
1636         if (stype == WLAN_FC_STYPE_BEACON) {
1637                 handle_beacon(hapd, mgmt, len, fi);
1638                 return;
1639         }
1640
1641         if (fi && fi->passive_scan)
1642                 return;
1643
1644         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1645                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1646                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1647
1648         if (!broadcast &&
1649             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
1650             (hapd->assoc_ap_state == DO_NOT_ASSOC ||
1651              os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
1652         {
1653                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1654                        MAC2STR(mgmt->bssid));
1655                 return;
1656         }
1657
1658
1659         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1660                 handle_probe_req(hapd, mgmt, len);
1661                 return;
1662         }
1663
1664         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1665                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1666                                HOSTAPD_LEVEL_DEBUG,
1667                                "MGMT: DA=" MACSTR " not our address",
1668                                MAC2STR(mgmt->da));
1669                 return;
1670         }
1671
1672         switch (stype) {
1673         case WLAN_FC_STYPE_AUTH:
1674                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1675                 handle_auth(hapd, mgmt, len);
1676                 break;
1677         case WLAN_FC_STYPE_ASSOC_REQ:
1678                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1679                 handle_assoc(hapd, mgmt, len, 0);
1680                 break;
1681         case WLAN_FC_STYPE_ASSOC_RESP:
1682                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
1683                 handle_assoc_resp(hapd, mgmt, len);
1684                 break;
1685         case WLAN_FC_STYPE_REASSOC_REQ:
1686                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1687                 handle_assoc(hapd, mgmt, len, 1);
1688                 break;
1689         case WLAN_FC_STYPE_DISASSOC:
1690                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1691                 handle_disassoc(hapd, mgmt, len);
1692                 break;
1693         case WLAN_FC_STYPE_DEAUTH:
1694                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1695                 handle_deauth(hapd, mgmt, len);
1696                 break;
1697         case WLAN_FC_STYPE_ACTION:
1698                 wpa_printf(MSG_DEBUG, "mgmt::action");
1699                 handle_action(hapd, mgmt, len);
1700                 break;
1701         default:
1702                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1703                                HOSTAPD_LEVEL_DEBUG,
1704                                "unknown mgmt frame subtype %d", stype);
1705                 break;
1706         }
1707 }
1708
1709
1710 static void handle_auth_cb(struct hostapd_data *hapd,
1711                            struct ieee80211_mgmt *mgmt,
1712                            size_t len, int ok)
1713 {
1714         u16 auth_alg, auth_transaction, status_code;
1715         struct sta_info *sta;
1716
1717         if (!ok) {
1718                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1719                                HOSTAPD_LEVEL_NOTICE,
1720                                "did not acknowledge authentication response");
1721                 return;
1722         }
1723
1724         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1725                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1726                        (unsigned long) len);
1727                 return;
1728         }
1729
1730         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1731         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1732         status_code = le_to_host16(mgmt->u.auth.status_code);
1733
1734         sta = ap_get_sta(hapd, mgmt->da);
1735         if (!sta) {
1736                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1737                        MAC2STR(mgmt->da));
1738                 return;
1739         }
1740
1741         if (status_code == WLAN_STATUS_SUCCESS &&
1742             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1743              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1744                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1745                                HOSTAPD_LEVEL_INFO, "authenticated");
1746                 sta->flags |= WLAN_STA_AUTH;
1747         }
1748 }
1749
1750
1751 static void handle_assoc_cb(struct hostapd_data *hapd,
1752                             struct ieee80211_mgmt *mgmt,
1753                             size_t len, int reassoc, int ok)
1754 {
1755         u16 status;
1756         struct sta_info *sta;
1757         int new_assoc = 1;
1758         struct ht_cap_ie *ht_cap = NULL;
1759
1760         if (!ok) {
1761                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1762                                HOSTAPD_LEVEL_DEBUG,
1763                                "did not acknowledge association response");
1764                 return;
1765         }
1766
1767         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1768                                       sizeof(mgmt->u.assoc_resp))) {
1769                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1770                        "(len=%lu)\n", reassoc, (unsigned long) len);
1771                 return;
1772         }
1773
1774         if (reassoc)
1775                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1776         else
1777                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1778
1779         sta = ap_get_sta(hapd, mgmt->da);
1780         if (!sta) {
1781                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1782                        MAC2STR(mgmt->da));
1783                 return;
1784         }
1785
1786         if (status != WLAN_STATUS_SUCCESS)
1787                 goto fail;
1788
1789         /* Stop previous accounting session, if one is started, and allocate
1790          * new session id for the new session. */
1791         accounting_sta_stop(hapd, sta);
1792         accounting_sta_get_id(hapd, sta);
1793
1794         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1795                        HOSTAPD_LEVEL_INFO,
1796                        "associated (aid %d, accounting session %08X-%08X)",
1797                        sta->aid, sta->acct_session_id_hi,
1798                        sta->acct_session_id_lo);
1799
1800         if (sta->flags & WLAN_STA_ASSOC)
1801                 new_assoc = 0;
1802         sta->flags |= WLAN_STA_ASSOC;
1803
1804         if (reassoc)
1805                 mlme_reassociate_indication(hapd, sta);
1806         else
1807                 mlme_associate_indication(hapd, sta);
1808
1809 #ifdef CONFIG_IEEE80211N
1810         if (sta->flags & WLAN_STA_HT)
1811                 ht_cap = &sta->ht_capabilities;
1812 #endif /* CONFIG_IEEE80211N */
1813
1814         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1815                             sta->capability, sta->supported_rates,
1816                             sta->supported_rates_len, 0, sta->listen_interval,
1817                             ht_cap))
1818         {
1819                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1820                                HOSTAPD_LEVEL_NOTICE,
1821                                "Could not add STA to kernel driver");
1822         }
1823
1824         if (sta->eapol_sm == NULL) {
1825                 /*
1826                  * This STA does not use RADIUS server for EAP authentication,
1827                  * so bind it to the selected VLAN interface now, since the
1828                  * interface selection is not going to change anymore.
1829                  */
1830                 ap_sta_bind_vlan(hapd, sta, 0);
1831         } else if (sta->vlan_id) {
1832                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1833                 ap_sta_bind_vlan(hapd, sta, 0);
1834         }
1835         if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1836                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1837                                       WLAN_STA_SHORT_PREAMBLE, ~0);
1838         } else {
1839                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1840                                       0, ~WLAN_STA_SHORT_PREAMBLE);
1841         }
1842
1843         if (sta->auth_alg == WLAN_AUTH_FT)
1844                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1845         else
1846                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1847         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1848
1849         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1850
1851  fail:
1852         /* Copy of the association request is not needed anymore */
1853         if (sta->last_assoc_req) {
1854                 os_free(sta->last_assoc_req);
1855                 sta->last_assoc_req = NULL;
1856         }
1857 }
1858
1859
1860 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1861                         u16 stype, int ok)
1862 {
1863         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1864
1865         switch (stype) {
1866         case WLAN_FC_STYPE_AUTH:
1867                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1868                 handle_auth_cb(hapd, mgmt, len, ok);
1869                 break;
1870         case WLAN_FC_STYPE_ASSOC_RESP:
1871                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1872                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1873                 break;
1874         case WLAN_FC_STYPE_REASSOC_RESP:
1875                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1876                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1877                 break;
1878         case WLAN_FC_STYPE_PROBE_RESP:
1879                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1880                 break;
1881         case WLAN_FC_STYPE_DEAUTH:
1882                 /* ignore */
1883                 break;
1884         default:
1885                 printf("unknown mgmt cb frame subtype %d\n", stype);
1886                 break;
1887         }
1888 }
1889
1890
1891 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1892                                                 void *timeout_ctx)
1893 {
1894         struct hostapd_data *hapd = eloop_ctx;
1895         hapd->tkip_countermeasures = 0;
1896         hostapd_set_countermeasures(hapd, 0);
1897         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1898                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1899 }
1900
1901
1902 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1903 {
1904         struct sta_info *sta;
1905
1906         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1907                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1908
1909         wpa_auth_countermeasures_start(hapd->wpa_auth);
1910         hapd->tkip_countermeasures = 1;
1911         hostapd_set_countermeasures(hapd, 1);
1912         wpa_gtk_rekey(hapd->wpa_auth);
1913         eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
1914         eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
1915                                hapd, NULL);
1916         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
1917                 hostapd_sta_deauth(hapd, sta->addr,
1918                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
1919                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1920                                 WLAN_STA_AUTHORIZED);
1921                 hostapd_sta_remove(hapd, sta->addr);
1922         }
1923 }
1924
1925
1926 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
1927                                    int local)
1928 {
1929         time_t now;
1930
1931         if (addr && local) {
1932                 struct sta_info *sta = ap_get_sta(hapd, addr);
1933                 if (sta != NULL) {
1934                         wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
1935                         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1936                                        HOSTAPD_LEVEL_INFO,
1937                                        "Michael MIC failure detected in "
1938                                        "received frame");
1939                         mlme_michaelmicfailure_indication(hapd, addr);
1940                 } else {
1941                         wpa_printf(MSG_DEBUG,
1942                                    "MLME-MICHAELMICFAILURE.indication "
1943                                    "for not associated STA (" MACSTR
1944                                    ") ignored", MAC2STR(addr));
1945                         return;
1946                 }
1947         }
1948
1949         time(&now);
1950         if (now > hapd->michael_mic_failure + 60) {
1951                 hapd->michael_mic_failures = 1;
1952         } else {
1953                 hapd->michael_mic_failures++;
1954                 if (hapd->michael_mic_failures > 1)
1955                         ieee80211_tkip_countermeasures_start(hapd);
1956         }
1957         hapd->michael_mic_failure = now;
1958 }
1959
1960
1961 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1962 {
1963         /* TODO */
1964         return 0;
1965 }
1966
1967
1968 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1969                            char *buf, size_t buflen)
1970 {
1971         /* TODO */
1972         return 0;
1973 }
1974
1975 #endif /* CONFIG_NATIVE_WINDOWS */