Cleaned up TX callback request processing
[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 #ifdef CONFIG_IEEE80211W
375 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
376                                             struct sta_info *sta, u8 *eid)
377 {
378         u8 *pos = eid;
379         u32 timeout;
380
381         *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
382         *pos++ = 4;
383         timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
384                 hapd->conf->assoc_ping_timeout;
385         WPA_PUT_LE32(pos, timeout);
386         pos += 4;
387
388         return pos;
389 }
390 #endif /* CONFIG_IEEE80211W */
391
392
393 ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
394                                 size_t len,
395                                 struct ieee802_11_elems *elems,
396                                 int show_errors)
397 {
398         size_t left = len;
399         u8 *pos = start;
400         int unknown = 0;
401
402         os_memset(elems, 0, sizeof(*elems));
403
404         while (left >= 2) {
405                 u8 id, elen;
406
407                 id = *pos++;
408                 elen = *pos++;
409                 left -= 2;
410
411                 if (elen > left) {
412                         if (show_errors) {
413                                 wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
414                                            "parse failed (id=%d elen=%d "
415                                            "left=%lu)",
416                                            id, elen, (unsigned long) left);
417                                 wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
418                         }
419                         return ParseFailed;
420                 }
421
422                 switch (id) {
423                 case WLAN_EID_SSID:
424                         elems->ssid = pos;
425                         elems->ssid_len = elen;
426                         break;
427                 case WLAN_EID_SUPP_RATES:
428                         elems->supp_rates = pos;
429                         elems->supp_rates_len = elen;
430                         break;
431                 case WLAN_EID_FH_PARAMS:
432                         elems->fh_params = pos;
433                         elems->fh_params_len = elen;
434                         break;
435                 case WLAN_EID_DS_PARAMS:
436                         elems->ds_params = pos;
437                         elems->ds_params_len = elen;
438                         break;
439                 case WLAN_EID_CF_PARAMS:
440                         elems->cf_params = pos;
441                         elems->cf_params_len = elen;
442                         break;
443                 case WLAN_EID_TIM:
444                         elems->tim = pos;
445                         elems->tim_len = elen;
446                         break;
447                 case WLAN_EID_IBSS_PARAMS:
448                         elems->ibss_params = pos;
449                         elems->ibss_params_len = elen;
450                         break;
451                 case WLAN_EID_CHALLENGE:
452                         elems->challenge = pos;
453                         elems->challenge_len = elen;
454                         break;
455                 case WLAN_EID_ERP_INFO:
456                         elems->erp_info = pos;
457                         elems->erp_info_len = elen;
458                         break;
459                 case WLAN_EID_EXT_SUPP_RATES:
460                         elems->ext_supp_rates = pos;
461                         elems->ext_supp_rates_len = elen;
462                         break;
463                 case WLAN_EID_VENDOR_SPECIFIC:
464                         if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
465                                                              elems,
466                                                              show_errors))
467                                 unknown++;
468                         break;
469                 case WLAN_EID_RSN:
470                         elems->rsn_ie = pos;
471                         elems->rsn_ie_len = elen;
472                         break;
473                 case WLAN_EID_PWR_CAPABILITY:
474                         elems->power_cap = pos;
475                         elems->power_cap_len = elen;
476                         break;
477                 case WLAN_EID_SUPPORTED_CHANNELS:
478                         elems->supp_channels = pos;
479                         elems->supp_channels_len = elen;
480                         break;
481                 case WLAN_EID_MOBILITY_DOMAIN:
482                         elems->mdie = pos;
483                         elems->mdie_len = elen;
484                         break;
485                 case WLAN_EID_FAST_BSS_TRANSITION:
486                         elems->ftie = pos;
487                         elems->ftie_len = elen;
488                         break;
489                 case WLAN_EID_HT_CAP:
490                         elems->ht_capabilities = pos;
491                         elems->ht_capabilities_len = elen;
492                         break;
493                 case WLAN_EID_HT_OPERATION:
494                         elems->ht_operation = pos;
495                         elems->ht_operation_len = elen;
496                         break;
497                 default:
498                         unknown++;
499                         if (!show_errors)
500                                 break;
501                         wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
502                                    "ignored unknown element (id=%d elen=%d)",
503                                    id, elen);
504                         break;
505                 }
506
507                 left -= elen;
508                 pos += elen;
509         }
510
511         if (left)
512                 return ParseFailed;
513
514         return unknown ? ParseUnknown : ParseOK;
515 }
516
517
518 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
519 {
520         int i;
521         if (len > HOSTAPD_MAX_SSID_LEN)
522                 len = HOSTAPD_MAX_SSID_LEN;
523         for (i = 0; i < len; i++) {
524                 if (ssid[i] >= 32 && ssid[i] < 127)
525                         buf[i] = ssid[i];
526                 else
527                         buf[i] = '.';
528         }
529         buf[len] = '\0';
530 }
531
532
533 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
534 {
535         struct ieee80211_mgmt mgmt;
536
537         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
538                        HOSTAPD_LEVEL_DEBUG,
539                        "deauthenticate - reason %d", reason);
540         os_memset(&mgmt, 0, sizeof(mgmt));
541         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
542                                           WLAN_FC_STYPE_DEAUTH);
543         os_memcpy(mgmt.da, addr, ETH_ALEN);
544         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
545         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
546         mgmt.u.deauth.reason_code = host_to_le16(reason);
547         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
548                                     sizeof(mgmt.u.deauth), 0) < 0)
549                 perror("ieee802_11_send_deauth: send");
550 }
551
552
553 static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
554 {
555         struct hostapd_data *hapd = eloop_ctx;
556         struct ieee80211_mgmt mgmt;
557         char ssid_txt[33];
558
559         if (hapd->assoc_ap_state == WAIT_BEACON)
560                 hapd->assoc_ap_state = AUTHENTICATE;
561         if (hapd->assoc_ap_state != AUTHENTICATE)
562                 return;
563
564         ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
565                               hapd->assoc_ap_ssid_len);
566         printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
567                MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
568
569         os_memset(&mgmt, 0, sizeof(mgmt));
570         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
571                                           WLAN_FC_STYPE_AUTH);
572         os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
573         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
574         os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
575         mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
576         mgmt.u.auth.auth_transaction = host_to_le16(1);
577         mgmt.u.auth.status_code = host_to_le16(0);
578         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
579                                     sizeof(mgmt.u.auth), 0) < 0)
580                 perror("ieee802_11_sta_authenticate: send");
581
582         /* Try to authenticate again, if this attempt fails or times out. */
583         eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
584 }
585
586
587 static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
588 {
589         struct hostapd_data *hapd = eloop_ctx;
590         u8 buf[256];
591         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
592         u8 *p;
593         char ssid_txt[33];
594
595         if (hapd->assoc_ap_state == AUTHENTICATE)
596                 hapd->assoc_ap_state = ASSOCIATE;
597         if (hapd->assoc_ap_state != ASSOCIATE)
598                 return;
599
600         ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
601                               hapd->assoc_ap_ssid_len);
602         printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
603                MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
604
605         os_memset(mgmt, 0, sizeof(*mgmt));
606         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
607                                           WLAN_FC_STYPE_ASSOC_REQ);
608         os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
609         os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
610         os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
611         mgmt->u.assoc_req.capab_info = host_to_le16(0);
612         mgmt->u.assoc_req.listen_interval = host_to_le16(1);
613         p = &mgmt->u.assoc_req.variable[0];
614
615         *p++ = WLAN_EID_SSID;
616         *p++ = hapd->assoc_ap_ssid_len;
617         os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
618         p += hapd->assoc_ap_ssid_len;
619
620         p = hostapd_eid_supp_rates(hapd, p);
621         p = hostapd_eid_ext_supp_rates(hapd, p);
622
623         if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
624                 perror("ieee802_11_sta_associate: send");
625
626         /* Try to authenticate again, if this attempt fails or times out. */
627         eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
628 }
629
630
631 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
632                            u16 auth_transaction, u8 *challenge, int iswep)
633 {
634         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
635                        HOSTAPD_LEVEL_DEBUG,
636                        "authentication (shared key, transaction %d)",
637                        auth_transaction);
638
639         if (auth_transaction == 1) {
640                 if (!sta->challenge) {
641                         /* Generate a pseudo-random challenge */
642                         u8 key[8];
643                         time_t now;
644                         int r;
645                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
646                         if (sta->challenge == NULL)
647                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
648
649                         now = time(NULL);
650                         r = random();
651                         os_memcpy(key, &now, 4);
652                         os_memcpy(key + 4, &r, 4);
653                         rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
654                             key, sizeof(key));
655                 }
656                 return 0;
657         }
658
659         if (auth_transaction != 3)
660                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
661
662         /* Transaction 3 */
663         if (!iswep || !sta->challenge || !challenge ||
664             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
665                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
666                                HOSTAPD_LEVEL_INFO,
667                                "shared key authentication - invalid "
668                                "challenge-response");
669                 return WLAN_STATUS_CHALLENGE_FAIL;
670         }
671
672         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
673                        HOSTAPD_LEVEL_DEBUG,
674                        "authentication OK (shared key)");
675 #ifdef IEEE80211_REQUIRE_AUTH_ACK
676         /* Station will be marked authenticated if it ACKs the
677          * authentication reply. */
678 #else
679         sta->flags |= WLAN_STA_AUTH;
680         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
681 #endif
682         os_free(sta->challenge);
683         sta->challenge = NULL;
684
685         return 0;
686 }
687
688
689 static void send_auth_reply(struct hostapd_data *hapd,
690                             const u8 *dst, const u8 *bssid,
691                             u16 auth_alg, u16 auth_transaction, u16 resp,
692                             const u8 *ies, size_t ies_len)
693 {
694         struct ieee80211_mgmt *reply;
695         u8 *buf;
696         size_t rlen;
697
698         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
699         buf = os_zalloc(rlen);
700         if (buf == NULL)
701                 return;
702
703         reply = (struct ieee80211_mgmt *) buf;
704         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
705                                             WLAN_FC_STYPE_AUTH);
706         os_memcpy(reply->da, dst, ETH_ALEN);
707         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
708         os_memcpy(reply->bssid, bssid, ETH_ALEN);
709
710         reply->u.auth.auth_alg = host_to_le16(auth_alg);
711         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
712         reply->u.auth.status_code = host_to_le16(resp);
713
714         if (ies && ies_len)
715                 os_memcpy(reply->u.auth.variable, ies, ies_len);
716
717         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
718                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
719                    MAC2STR(dst), auth_alg, auth_transaction,
720                    resp, (unsigned long) ies_len);
721         if (hostapd_send_mgmt_frame(hapd, reply, rlen, 0) < 0)
722                 perror("send_auth_reply: send");
723
724         os_free(buf);
725 }
726
727
728 #ifdef CONFIG_IEEE80211R
729 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
730                                   u16 auth_transaction, u16 status,
731                                   const u8 *ies, size_t ies_len)
732 {
733         struct hostapd_data *hapd = ctx;
734         struct sta_info *sta;
735
736         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
737                         status, ies, ies_len);
738
739         if (status != WLAN_STATUS_SUCCESS)
740                 return;
741
742         sta = ap_get_sta(hapd, dst);
743         if (sta == NULL)
744                 return;
745
746         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
747                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
748         sta->flags |= WLAN_STA_AUTH;
749         mlme_authenticate_indication(hapd, sta);
750 }
751 #endif /* CONFIG_IEEE80211R */
752
753
754 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
755                         size_t len)
756 {
757         u16 auth_alg, auth_transaction, status_code;
758         u16 resp = WLAN_STATUS_SUCCESS;
759         struct sta_info *sta = NULL;
760         int res;
761         u16 fc;
762         u8 *challenge = NULL;
763         u32 session_timeout, acct_interim_interval;
764         int vlan_id = 0;
765         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
766         size_t resp_ies_len = 0;
767
768         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
769                 printf("handle_auth - too short payload (len=%lu)\n",
770                        (unsigned long) len);
771                 return;
772         }
773
774         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
775         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
776         status_code = le_to_host16(mgmt->u.auth.status_code);
777         fc = le_to_host16(mgmt->frame_control);
778
779         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
780             2 + WLAN_AUTH_CHALLENGE_LEN &&
781             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
782             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
783                 challenge = &mgmt->u.auth.variable[2];
784
785         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
786                    "auth_transaction=%d status_code=%d wep=%d%s",
787                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
788                    status_code, !!(fc & WLAN_FC_ISWEP),
789                    challenge ? " challenge" : "");
790
791         if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
792             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
793             os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
794                 if (status_code != 0) {
795                         printf("Authentication (as station) with AP "
796                                MACSTR " failed (status_code=%d)\n",
797                                MAC2STR(hapd->conf->assoc_ap_addr),
798                                status_code);
799                         return;
800                 }
801                 printf("Authenticated (as station) with AP " MACSTR "\n",
802                        MAC2STR(hapd->conf->assoc_ap_addr));
803                 ieee802_11_sta_associate(hapd, NULL);
804                 return;
805         }
806
807         if (hapd->tkip_countermeasures) {
808                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
809                 goto fail;
810         }
811
812         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
813                auth_alg == WLAN_AUTH_OPEN) ||
814 #ifdef CONFIG_IEEE80211R
815               (hapd->conf->wpa &&
816                (hapd->conf->wpa_key_mgmt &
817                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
818                auth_alg == WLAN_AUTH_FT) ||
819 #endif /* CONFIG_IEEE80211R */
820               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
821                auth_alg == WLAN_AUTH_SHARED_KEY))) {
822                 printf("Unsupported authentication algorithm (%d)\n",
823                        auth_alg);
824                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
825                 goto fail;
826         }
827
828         if (!(auth_transaction == 1 ||
829               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
830                 printf("Unknown authentication transaction number (%d)\n",
831                        auth_transaction);
832                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
833                 goto fail;
834         }
835
836         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
837                 printf("Station " MACSTR " not allowed to authenticate.\n",
838                        MAC2STR(mgmt->sa));
839                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
840                 goto fail;
841         }
842
843         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
844                                       &session_timeout,
845                                       &acct_interim_interval, &vlan_id);
846         if (res == HOSTAPD_ACL_REJECT) {
847                 printf("Station " MACSTR " not allowed to authenticate.\n",
848                        MAC2STR(mgmt->sa));
849                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
850                 goto fail;
851         }
852         if (res == HOSTAPD_ACL_PENDING) {
853                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
854                            " waiting for an external authentication",
855                            MAC2STR(mgmt->sa));
856                 /* Authentication code will re-send the authentication frame
857                  * after it has received (and cached) information from the
858                  * external source. */
859                 return;
860         }
861
862         sta = ap_sta_add(hapd, mgmt->sa);
863         if (!sta) {
864                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
865                 goto fail;
866         }
867
868         if (vlan_id > 0) {
869                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
870                                                sta->vlan_id) == NULL) {
871                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
872                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
873                                        "%d received from RADIUS server",
874                                        vlan_id);
875                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
876                         goto fail;
877                 }
878                 sta->vlan_id = vlan_id;
879                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
880                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
881         }
882
883         sta->flags &= ~WLAN_STA_PREAUTH;
884         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
885
886         if (hapd->conf->radius->acct_interim_interval == 0 &&
887             acct_interim_interval)
888                 sta->acct_interim_interval = acct_interim_interval;
889         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
890                 ap_sta_session_timeout(hapd, sta, session_timeout);
891         else
892                 ap_sta_no_session_timeout(hapd, sta);
893
894         switch (auth_alg) {
895         case WLAN_AUTH_OPEN:
896                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
897                                HOSTAPD_LEVEL_DEBUG,
898                                "authentication OK (open system)");
899 #ifdef IEEE80211_REQUIRE_AUTH_ACK
900                 /* Station will be marked authenticated if it ACKs the
901                  * authentication reply. */
902 #else
903                 sta->flags |= WLAN_STA_AUTH;
904                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
905                 sta->auth_alg = WLAN_AUTH_OPEN;
906                 mlme_authenticate_indication(hapd, sta);
907 #endif
908                 break;
909         case WLAN_AUTH_SHARED_KEY:
910                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
911                                        fc & WLAN_FC_ISWEP);
912                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
913                 mlme_authenticate_indication(hapd, sta);
914                 if (sta->challenge && auth_transaction == 1) {
915                         resp_ies[0] = WLAN_EID_CHALLENGE;
916                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
917                         os_memcpy(resp_ies + 2, sta->challenge,
918                                   WLAN_AUTH_CHALLENGE_LEN);
919                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
920                 }
921                 break;
922 #ifdef CONFIG_IEEE80211R
923         case WLAN_AUTH_FT:
924                 sta->auth_alg = WLAN_AUTH_FT;
925                 if (sta->wpa_sm == NULL)
926                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
927                                                         sta->addr);
928                 if (sta->wpa_sm == NULL) {
929                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
930                                    "state machine");
931                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
932                         goto fail;
933                 }
934                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
935                                     auth_transaction, mgmt->u.auth.variable,
936                                     len - IEEE80211_HDRLEN -
937                                     sizeof(mgmt->u.auth),
938                                     handle_auth_ft_finish, hapd);
939                 /* handle_auth_ft_finish() callback will complete auth. */
940                 return;
941 #endif /* CONFIG_IEEE80211R */
942         }
943
944  fail:
945         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
946                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
947 }
948
949
950 static void handle_assoc(struct hostapd_data *hapd,
951                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
952 {
953         u16 capab_info, listen_interval;
954         u16 resp = WLAN_STATUS_SUCCESS;
955         u8 *pos, *wpa_ie;
956         size_t wpa_ie_len;
957         int send_deauth = 0, send_len, left, i;
958         struct sta_info *sta;
959         struct ieee802_11_elems elems;
960         u8 buf[sizeof(struct ieee80211_mgmt) + 512];
961         struct ieee80211_mgmt *reply;
962
963         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
964                                       sizeof(mgmt->u.assoc_req))) {
965                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
966                        "\n", reassoc, (unsigned long) len);
967                 return;
968         }
969
970         if (reassoc) {
971                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
972                 listen_interval = le_to_host16(
973                         mgmt->u.reassoc_req.listen_interval);
974                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
975                            " capab_info=0x%02x listen_interval=%d current_ap="
976                            MACSTR,
977                            MAC2STR(mgmt->sa), capab_info, listen_interval,
978                            MAC2STR(mgmt->u.reassoc_req.current_ap));
979                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
980                 pos = mgmt->u.reassoc_req.variable;
981         } else {
982                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
983                 listen_interval = le_to_host16(
984                         mgmt->u.assoc_req.listen_interval);
985                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
986                            " capab_info=0x%02x listen_interval=%d",
987                            MAC2STR(mgmt->sa), capab_info, listen_interval);
988                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
989                 pos = mgmt->u.assoc_req.variable;
990         }
991
992         sta = ap_get_sta(hapd, mgmt->sa);
993 #ifdef CONFIG_IEEE80211R
994         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
995             (sta->flags & WLAN_STA_AUTH) == 0) {
996                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
997                            "prior to authentication since it is using "
998                            "over-the-DS FT", MAC2STR(mgmt->sa));
999         } else
1000 #endif /* CONFIG_IEEE80211R */
1001         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1002                 printf("STA " MACSTR " trying to associate before "
1003                        "authentication\n", MAC2STR(mgmt->sa));
1004                 if (sta) {
1005                         printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
1006                                MAC2STR(sta->addr), sta->aid, sta->flags);
1007                 }
1008                 send_deauth = 1;
1009                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1010                 goto fail;
1011         }
1012
1013         if (hapd->tkip_countermeasures) {
1014                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1015                 goto fail;
1016         }
1017
1018         if (listen_interval > hapd->conf->max_listen_interval) {
1019                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1020                                HOSTAPD_LEVEL_DEBUG,
1021                                "Too large Listen Interval (%d)",
1022                                listen_interval);
1023                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1024                 goto fail;
1025         }
1026
1027         sta->capability = capab_info;
1028
1029         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1030          * is used */
1031         if (ieee802_11_parse_elems(hapd, pos, left, &elems, 1) == ParseFailed
1032             || !elems.ssid) {
1033                 printf("STA " MACSTR " sent invalid association request\n",
1034                        MAC2STR(sta->addr));
1035                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1036                 goto fail;
1037         }
1038
1039         if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1040             os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
1041         {
1042                 char ssid_txt[33];
1043                 ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
1044                 printf("Station " MACSTR " tried to associate with "
1045                        "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
1046                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1047                 goto fail;
1048         }
1049
1050         sta->flags &= ~WLAN_STA_WME;
1051         if (elems.wme && hapd->conf->wme_enabled) {
1052                 if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
1053                         hostapd_logger(hapd, sta->addr,
1054                                        HOSTAPD_MODULE_WPA,
1055                                        HOSTAPD_LEVEL_DEBUG,
1056                                        "invalid WME element in association "
1057                                        "request");
1058                 else
1059                         sta->flags |= WLAN_STA_WME;
1060         }
1061
1062         if (!elems.supp_rates) {
1063                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1064                                HOSTAPD_LEVEL_DEBUG,
1065                                "No supported rates element in AssocReq");
1066                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1067                 goto fail;
1068         }
1069
1070         if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
1071                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1072                                HOSTAPD_LEVEL_DEBUG,
1073                                "Invalid supported rates element length %d",
1074                                elems.supp_rates_len);
1075                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1076                 goto fail;
1077         }
1078
1079         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
1080         os_memcpy(sta->supported_rates, elems.supp_rates,
1081                   elems.supp_rates_len);
1082         sta->supported_rates_len = elems.supp_rates_len;
1083
1084         if (elems.ext_supp_rates) {
1085                 if (elems.supp_rates_len + elems.ext_supp_rates_len >
1086                     sizeof(sta->supported_rates)) {
1087                         hostapd_logger(hapd, mgmt->sa,
1088                                        HOSTAPD_MODULE_IEEE80211,
1089                                        HOSTAPD_LEVEL_DEBUG,
1090                                        "Invalid supported rates element length"
1091                                        " %d+%d", elems.supp_rates_len,
1092                                        elems.ext_supp_rates_len);
1093                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1094                         goto fail;
1095                 }
1096
1097                 os_memcpy(sta->supported_rates + elems.supp_rates_len,
1098                           elems.ext_supp_rates, elems.ext_supp_rates_len);
1099                 sta->supported_rates_len += elems.ext_supp_rates_len;
1100         }
1101
1102 #ifdef CONFIG_IEEE80211N
1103         /* save HT capabilities in the sta object */
1104         os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
1105         if (elems.ht_capabilities &&
1106             elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_capability)
1107             && (sta->flags & WLAN_STA_WME)) {
1108                 /* note: without WMM capability, treat the sta as non-HT */
1109                 sta->flags |= WLAN_STA_HT;
1110                 sta->ht_capabilities.id = WLAN_EID_HT_CAP;
1111                 sta->ht_capabilities.length =
1112                         sizeof(struct ieee80211_ht_capability);
1113                 os_memcpy(&sta->ht_capabilities.data,
1114                           elems.ht_capabilities,
1115                           sizeof(struct ieee80211_ht_capability));
1116         } else
1117                 sta->flags &= ~WLAN_STA_HT;
1118 #endif /* CONFIG_IEEE80211N */
1119
1120         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1121                 wpa_ie = elems.rsn_ie;
1122                 wpa_ie_len = elems.rsn_ie_len;
1123         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1124                    elems.wpa_ie) {
1125                 wpa_ie = elems.wpa_ie;
1126                 wpa_ie_len = elems.wpa_ie_len;
1127         } else {
1128                 wpa_ie = NULL;
1129                 wpa_ie_len = 0;
1130         }
1131         if (hapd->conf->wpa && wpa_ie == NULL) {
1132                 printf("STA " MACSTR ": No WPA/RSN IE in association "
1133                        "request\n", MAC2STR(sta->addr));
1134                 resp = WLAN_STATUS_INVALID_IE;
1135                 goto fail;
1136         }
1137
1138         if (hapd->conf->wpa && wpa_ie) {
1139                 int res;
1140                 wpa_ie -= 2;
1141                 wpa_ie_len += 2;
1142                 if (sta->wpa_sm == NULL)
1143                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1144                                                         sta->addr);
1145                 if (sta->wpa_sm == NULL) {
1146                         printf("Failed to initialize WPA state machine\n");
1147                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1148                         goto fail;
1149                 }
1150                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1151                                           wpa_ie, wpa_ie_len,
1152                                           elems.mdie, elems.mdie_len);
1153                 if (res == WPA_INVALID_GROUP)
1154                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1155                 else if (res == WPA_INVALID_PAIRWISE)
1156                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1157                 else if (res == WPA_INVALID_AKMP)
1158                         resp = WLAN_STATUS_AKMP_NOT_VALID;
1159                 else if (res == WPA_ALLOC_FAIL)
1160                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1161 #ifdef CONFIG_IEEE80211W
1162                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1163                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1164                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1165                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
1166 #endif /* CONFIG_IEEE80211W */
1167                 else if (res == WPA_INVALID_MDIE)
1168                         resp = WLAN_STATUS_INVALID_MDIE;
1169                 else if (res != WPA_IE_OK)
1170                         resp = WLAN_STATUS_INVALID_IE;
1171                 if (resp != WLAN_STATUS_SUCCESS)
1172                         goto fail;
1173 #ifdef CONFIG_IEEE80211W
1174                 if ((sta->flags & WLAN_STA_MFP) && !sta->ping_timed_out) {
1175                         /*
1176                          * STA has already been associated with MFP and ping
1177                          * timeout has not been reached. Reject the
1178                          * association attempt temporarily and start ping, if
1179                          * one is not pending.
1180                          */
1181
1182                         if (sta->ping_count == 0)
1183                                 ap_sta_start_ping(hapd, sta);
1184
1185                         resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1186                         goto fail;
1187                 }
1188
1189                 if (wpa_auth_uses_mfp(sta->wpa_sm))
1190                         sta->flags |= WLAN_STA_MFP;
1191                 else
1192                         sta->flags &= ~WLAN_STA_MFP;
1193 #endif /* CONFIG_IEEE80211W */
1194
1195 #ifdef CONFIG_IEEE80211R
1196                 if (sta->auth_alg == WLAN_AUTH_FT) {
1197                         if (!reassoc) {
1198                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1199                                            "to use association (not "
1200                                            "re-association) with FT auth_alg",
1201                                            MAC2STR(sta->addr));
1202                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1203                                 goto fail;
1204                         }
1205
1206                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
1207                         if (resp != WLAN_STATUS_SUCCESS)
1208                                 goto fail;
1209                 }
1210 #endif /* CONFIG_IEEE80211R */
1211         }
1212
1213         if (hapd->iface->dfs_enable &&
1214             hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
1215                 if (hostapd_check_power_cap(hapd, elems.power_cap,
1216                                             elems.power_cap_len)) {
1217                         resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
1218                         hostapd_logger(hapd, sta->addr,
1219                                        HOSTAPD_MODULE_IEEE80211,
1220                                        HOSTAPD_LEVEL_DEBUG,
1221                                        "Power capabilities of the station not "
1222                                        "acceptable");
1223                         goto fail;
1224                 }
1225         }
1226
1227         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1228                 sta->flags |= WLAN_STA_NONERP;
1229         for (i = 0; i < sta->supported_rates_len; i++) {
1230                 if ((sta->supported_rates[i] & 0x7f) > 22) {
1231                         sta->flags &= ~WLAN_STA_NONERP;
1232                         break;
1233                 }
1234         }
1235         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1236                 sta->nonerp_set = 1;
1237                 hapd->iface->num_sta_non_erp++;
1238                 if (hapd->iface->num_sta_non_erp == 1)
1239                         ieee802_11_set_beacons(hapd->iface);
1240         }
1241
1242         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1243             !sta->no_short_slot_time_set) {
1244                 sta->no_short_slot_time_set = 1;
1245                 hapd->iface->num_sta_no_short_slot_time++;
1246                 if (hapd->iface->current_mode->mode ==
1247                     HOSTAPD_MODE_IEEE80211G &&
1248                     hapd->iface->num_sta_no_short_slot_time == 1)
1249                         ieee802_11_set_beacons(hapd->iface);
1250         }
1251
1252         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1253                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1254         else
1255                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1256
1257         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1258             !sta->no_short_preamble_set) {
1259                 sta->no_short_preamble_set = 1;
1260                 hapd->iface->num_sta_no_short_preamble++;
1261                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1262                     && hapd->iface->num_sta_no_short_preamble == 1)
1263                         ieee802_11_set_beacons(hapd->iface);
1264         }
1265
1266 #ifdef CONFIG_IEEE80211N
1267         if (sta->flags & WLAN_STA_HT) {
1268                 if ((sta->ht_capabilities.data.capabilities_info &
1269                      HT_CAP_INFO_GREEN_FIELD) == 0) {
1270                         hapd->iface->num_sta_ht_no_gf++;
1271                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
1272                                    "greenfield, num of non-gf stations %d",
1273                                    __func__, MAC2STR(sta->addr),
1274                                    hapd->iface->num_sta_ht_no_gf);
1275                 }
1276                 if ((sta->ht_capabilities.data.capabilities_info &
1277                      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
1278                         hapd->iface->num_sta_ht_20mhz++;
1279                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
1280                                    "num of 20MHz HT STAs %d",
1281                                    __func__, MAC2STR(sta->addr),
1282                                    hapd->iface->num_sta_ht_20mhz);
1283                 }
1284         } else {
1285                 hapd->iface->num_sta_no_ht++;
1286                 if (hapd->iconf->ieee80211n) {
1287                         wpa_printf(MSG_DEBUG, "%s STA " MACSTR
1288                                    " - no HT, num of non-HT stations %d",
1289                                    __func__, MAC2STR(sta->addr),
1290                                    hapd->iface->num_sta_no_ht);
1291                 }
1292         }
1293
1294         if (hostapd_ht_operation_update(hapd->iface) > 0)
1295                 ieee802_11_set_beacons(hapd->iface);
1296 #endif /* CONFIG_IEEE80211N */
1297
1298         /* get a unique AID */
1299         if (sta->aid > 0) {
1300                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
1301         } else {
1302                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1303                         if (hapd->sta_aid[sta->aid - 1] == NULL)
1304                                 break;
1305                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1306                         sta->aid = 0;
1307                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1308                         wpa_printf(MSG_ERROR, "  no room for more AIDs");
1309                         goto fail;
1310                 } else {
1311                         hapd->sta_aid[sta->aid - 1] = sta;
1312                         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
1313                 }
1314         }
1315
1316         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1317                        HOSTAPD_LEVEL_DEBUG,
1318                        "association OK (aid %d)", sta->aid);
1319         /* Station will be marked associated, after it acknowledges AssocResp
1320          */
1321
1322         if (reassoc) {
1323                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1324                           ETH_ALEN);
1325         }
1326
1327         if (sta->last_assoc_req)
1328                 os_free(sta->last_assoc_req);
1329         sta->last_assoc_req = os_malloc(len);
1330         if (sta->last_assoc_req)
1331                 os_memcpy(sta->last_assoc_req, mgmt, len);
1332
1333         /* Make sure that the previously registered inactivity timer will not
1334          * remove the STA immediately. */
1335         sta->timeout_next = STA_NULLFUNC;
1336
1337  fail:
1338         os_memset(buf, 0, sizeof(buf));
1339         reply = (struct ieee80211_mgmt *) buf;
1340         reply->frame_control =
1341                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1342                              (send_deauth ? WLAN_FC_STYPE_DEAUTH :
1343                               (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1344                                WLAN_FC_STYPE_ASSOC_RESP)));
1345         os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
1346         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1347         os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
1348
1349         send_len = IEEE80211_HDRLEN;
1350         if (send_deauth) {
1351                 send_len += sizeof(reply->u.deauth);
1352                 reply->u.deauth.reason_code = host_to_le16(resp);
1353         } else {
1354                 u8 *p;
1355                 send_len += sizeof(reply->u.assoc_resp);
1356                 reply->u.assoc_resp.capab_info =
1357                         host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1358                 reply->u.assoc_resp.status_code = host_to_le16(resp);
1359                 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
1360                                                        | BIT(14) | BIT(15));
1361                 /* Supported rates */
1362                 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1363                 /* Extended supported rates */
1364                 p = hostapd_eid_ext_supp_rates(hapd, p);
1365                 if (sta->flags & WLAN_STA_WME)
1366                         p = hostapd_eid_wme(hapd, p);
1367
1368                 p = hostapd_eid_ht_capabilities_info(hapd, p);
1369                 p = hostapd_eid_ht_operation(hapd, p);
1370
1371 #ifdef CONFIG_IEEE80211R
1372                 if (resp == WLAN_STATUS_SUCCESS) {
1373                         /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1374                          * Transition Information, RSN */
1375                         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1376                                                         buf + sizeof(buf) - p,
1377                                                         sta->auth_alg);
1378                 }
1379 #endif /* CONFIG_IEEE80211R */
1380
1381 #ifdef CONFIG_IEEE80211W
1382                 if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1383                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1384 #endif /* CONFIG_IEEE80211W */
1385
1386                 send_len += p - reply->u.assoc_resp.variable;
1387         }
1388
1389         if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
1390                 perror("handle_assoc: send");
1391 }
1392
1393
1394 static void handle_assoc_resp(struct hostapd_data *hapd,
1395                               struct ieee80211_mgmt *mgmt, size_t len)
1396 {
1397         u16 status_code, aid;
1398
1399         if (hapd->assoc_ap_state != ASSOCIATE) {
1400                 printf("Unexpected association response received from " MACSTR
1401                        "\n", MAC2STR(mgmt->sa));
1402                 return;
1403         }
1404
1405         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
1406                 printf("handle_assoc_resp - too short payload (len=%lu)\n",
1407                        (unsigned long) len);
1408                 return;
1409         }
1410
1411         if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
1412             os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
1413                 printf("Received association response from unexpected address "
1414                        "(SA=" MACSTR " BSSID=" MACSTR "\n",
1415                        MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1416                 return;
1417         }
1418
1419         status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1420         aid = le_to_host16(mgmt->u.assoc_resp.aid);
1421         aid &= ~(BIT(14) | BIT(15));
1422
1423         if (status_code != 0) {
1424                 printf("Association (as station) with AP " MACSTR " failed "
1425                        "(status_code=%d)\n",
1426                        MAC2STR(hapd->conf->assoc_ap_addr), status_code);
1427                 /* Try to authenticate again */
1428                 hapd->assoc_ap_state = AUTHENTICATE;
1429                 eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
1430                                        hapd, NULL);
1431         }
1432
1433         printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
1434                MAC2STR(hapd->conf->assoc_ap_addr), aid);
1435         hapd->assoc_ap_aid = aid;
1436         hapd->assoc_ap_state = ASSOCIATED;
1437
1438         if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
1439                 printf("Could not set associated AP address to kernel "
1440                        "driver.\n");
1441         }
1442 }
1443
1444
1445 static void handle_disassoc(struct hostapd_data *hapd,
1446                             struct ieee80211_mgmt *mgmt, size_t len)
1447 {
1448         struct sta_info *sta;
1449
1450         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1451                 printf("handle_disassoc - too short payload (len=%lu)\n",
1452                        (unsigned long) len);
1453                 return;
1454         }
1455
1456         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1457                    MAC2STR(mgmt->sa),
1458                    le_to_host16(mgmt->u.disassoc.reason_code));
1459
1460         if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1461             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1462                 printf("Assoc AP " MACSTR " sent disassociation "
1463                        "(reason_code=%d) - try to authenticate\n",
1464                        MAC2STR(hapd->conf->assoc_ap_addr),
1465                        le_to_host16(mgmt->u.disassoc.reason_code));
1466                 hapd->assoc_ap_state = AUTHENTICATE;
1467                 ieee802_11_sta_authenticate(hapd, NULL);
1468                 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1469                                        hapd, NULL);
1470                 return;
1471         }
1472
1473         sta = ap_get_sta(hapd, mgmt->sa);
1474         if (sta == NULL) {
1475                 printf("Station " MACSTR " trying to disassociate, but it "
1476                        "is not associated.\n", MAC2STR(mgmt->sa));
1477                 return;
1478         }
1479
1480         sta->flags &= ~WLAN_STA_ASSOC;
1481         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1482         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1483                        HOSTAPD_LEVEL_INFO, "disassociated");
1484         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1485         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1486         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1487          * authenticated. */
1488         accounting_sta_stop(hapd, sta);
1489         ieee802_1x_free_station(sta);
1490         hostapd_sta_remove(hapd, sta->addr);
1491
1492         if (sta->timeout_next == STA_NULLFUNC ||
1493             sta->timeout_next == STA_DISASSOC) {
1494                 sta->timeout_next = STA_DEAUTH;
1495                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1496                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1497                                        hapd, sta);
1498         }
1499
1500         mlme_disassociate_indication(
1501                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1502 }
1503
1504
1505 static void handle_deauth(struct hostapd_data *hapd,
1506                           struct ieee80211_mgmt *mgmt, size_t len)
1507 {
1508         struct sta_info *sta;
1509
1510         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1511                 printf("handle_deauth - too short payload (len=%lu)\n",
1512                        (unsigned long) len);
1513                 return;
1514         }
1515
1516         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1517                    " reason_code=%d",
1518                    MAC2STR(mgmt->sa),
1519                    le_to_host16(mgmt->u.deauth.reason_code));
1520
1521         if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
1522             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1523                 printf("Assoc AP " MACSTR " sent deauthentication "
1524                        "(reason_code=%d) - try to authenticate\n",
1525                        MAC2STR(hapd->conf->assoc_ap_addr),
1526                        le_to_host16(mgmt->u.deauth.reason_code));
1527                 hapd->assoc_ap_state = AUTHENTICATE;
1528                 eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
1529                                        hapd, NULL);
1530                 return;
1531         }
1532
1533         sta = ap_get_sta(hapd, mgmt->sa);
1534         if (sta == NULL) {
1535                 printf("Station " MACSTR " trying to deauthenticate, but it "
1536                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1537                 return;
1538         }
1539
1540         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1541         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1542         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1543                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1544         mlme_deauthenticate_indication(
1545                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1546         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1547         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1548         ap_free_sta(hapd, sta);
1549 }
1550
1551
1552 static void handle_beacon(struct hostapd_data *hapd,
1553                           struct ieee80211_mgmt *mgmt, size_t len,
1554                           struct hostapd_frame_info *fi)
1555 {
1556         struct ieee802_11_elems elems;
1557
1558         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1559                 printf("handle_beacon - too short payload (len=%lu)\n",
1560                        (unsigned long) len);
1561                 return;
1562         }
1563
1564         (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
1565                                       len - (IEEE80211_HDRLEN +
1566                                              sizeof(mgmt->u.beacon)), &elems,
1567                                       0);
1568
1569         if (hapd->assoc_ap_state == WAIT_BEACON &&
1570             os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
1571                 if (elems.ssid && elems.ssid_len <= 32) {
1572                         os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
1573                                   elems.ssid_len);
1574                         hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
1575                         hapd->assoc_ap_ssid_len = elems.ssid_len;
1576                 }
1577                 ieee802_11_sta_authenticate(hapd, NULL);
1578         }
1579
1580         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1581 }
1582
1583
1584 #ifdef CONFIG_IEEE80211W
1585 static void hostapd_ping_action(struct hostapd_data *hapd,
1586                                 struct ieee80211_mgmt *mgmt, size_t len)
1587 {
1588         struct sta_info *sta;
1589         u8 *end;
1590         int i;
1591
1592         end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
1593         if (((u8 *) mgmt) + len < end) {
1594                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
1595                            "frame (len=%lu)", (unsigned long) len);
1596                 return;
1597         }
1598
1599         if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
1600                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
1601                            mgmt->u.action.u.ping_resp.action);
1602                 return;
1603         }
1604
1605         /* MLME-PING.confirm */
1606
1607         sta = ap_get_sta(hapd, mgmt->sa);
1608         if (sta == NULL || sta->ping_trans_id == NULL) {
1609                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1610                            "pending ping request found");
1611                 return;
1612         }
1613
1614         for (i = 0; i < sta->ping_count; i++) {
1615                 if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
1616                               mgmt->u.action.u.ping_resp.trans_id,
1617                               WLAN_PING_TRANS_ID_LEN) == 0)
1618                         break;
1619         }
1620
1621         if (i >= sta->ping_count) {
1622                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
1623                            "transaction identifier found");
1624                 return;
1625         }
1626
1627         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1628                        HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
1629         ap_sta_stop_ping(hapd, sta);
1630 }
1631 #endif /* CONFIG_IEEE80211W */
1632
1633
1634 static void handle_action(struct hostapd_data *hapd,
1635                           struct ieee80211_mgmt *mgmt, size_t len)
1636 {
1637         if (len < IEEE80211_HDRLEN + 1) {
1638                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1639                                HOSTAPD_LEVEL_DEBUG,
1640                                "handle_action - too short payload (len=%lu)",
1641                                (unsigned long) len);
1642                 return;
1643         }
1644
1645         switch (mgmt->u.action.category) {
1646 #ifdef CONFIG_IEEE80211R
1647         case WLAN_ACTION_FT:
1648         {
1649                 struct sta_info *sta;
1650
1651                 sta = ap_get_sta(hapd, mgmt->sa);
1652                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1653                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1654                                    "frame from unassociated STA " MACSTR,
1655                                    MAC2STR(mgmt->sa));
1656                         return;
1657                 }
1658
1659                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1660                                      len - IEEE80211_HDRLEN))
1661                         break;
1662
1663                 return;
1664         }
1665 #endif /* CONFIG_IEEE80211R */
1666         case WLAN_ACTION_WMM:
1667                 hostapd_wme_action(hapd, mgmt, len);
1668                 return;
1669 #ifdef CONFIG_IEEE80211W
1670         case WLAN_ACTION_PING:
1671                 hostapd_ping_action(hapd, mgmt, len);
1672                 return;
1673 #endif /* CONFIG_IEEE80211W */
1674         }
1675
1676         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1677                        HOSTAPD_LEVEL_DEBUG,
1678                        "handle_action - unknown action category %d or invalid "
1679                        "frame",
1680                        mgmt->u.action.category);
1681         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1682             !(mgmt->sa[0] & 0x01)) {
1683                 /*
1684                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1685                  * Return the Action frame to the source without change
1686                  * except that MSB of the Category set to 1.
1687                  */
1688                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1689                            "frame back to sender");
1690                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1691                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1692                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1693                 mgmt->u.action.category |= 0x80;
1694
1695                 hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
1696         }
1697 }
1698
1699
1700 /**
1701  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1702  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1703  * sent to)
1704  * @buf: management frame data (starting from IEEE 802.11 header)
1705  * @len: length of frame data in octets
1706  * @stype: management frame subtype from frame control field
1707  *
1708  * Process all incoming IEEE 802.11 management frames. This will be called for
1709  * each frame received from the kernel driver through wlan#ap interface. In
1710  * addition, it can be called to re-inserted pending frames (e.g., when using
1711  * external RADIUS server as an MAC ACL).
1712  */
1713 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1714                      struct hostapd_frame_info *fi)
1715 {
1716         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1717         int broadcast;
1718
1719         if (stype == WLAN_FC_STYPE_BEACON) {
1720                 handle_beacon(hapd, mgmt, len, fi);
1721                 return;
1722         }
1723
1724         if (fi && fi->passive_scan)
1725                 return;
1726
1727         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1728                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1729                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1730
1731         if (!broadcast &&
1732             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
1733             (hapd->assoc_ap_state == DO_NOT_ASSOC ||
1734              os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
1735         {
1736                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1737                        MAC2STR(mgmt->bssid));
1738                 return;
1739         }
1740
1741
1742         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1743                 handle_probe_req(hapd, mgmt, len);
1744                 return;
1745         }
1746
1747         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1748                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1749                                HOSTAPD_LEVEL_DEBUG,
1750                                "MGMT: DA=" MACSTR " not our address",
1751                                MAC2STR(mgmt->da));
1752                 return;
1753         }
1754
1755         switch (stype) {
1756         case WLAN_FC_STYPE_AUTH:
1757                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1758                 handle_auth(hapd, mgmt, len);
1759                 break;
1760         case WLAN_FC_STYPE_ASSOC_REQ:
1761                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1762                 handle_assoc(hapd, mgmt, len, 0);
1763                 break;
1764         case WLAN_FC_STYPE_ASSOC_RESP:
1765                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
1766                 handle_assoc_resp(hapd, mgmt, len);
1767                 break;
1768         case WLAN_FC_STYPE_REASSOC_REQ:
1769                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1770                 handle_assoc(hapd, mgmt, len, 1);
1771                 break;
1772         case WLAN_FC_STYPE_DISASSOC:
1773                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1774                 handle_disassoc(hapd, mgmt, len);
1775                 break;
1776         case WLAN_FC_STYPE_DEAUTH:
1777                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1778                 handle_deauth(hapd, mgmt, len);
1779                 break;
1780         case WLAN_FC_STYPE_ACTION:
1781                 wpa_printf(MSG_DEBUG, "mgmt::action");
1782                 handle_action(hapd, mgmt, len);
1783                 break;
1784         default:
1785                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1786                                HOSTAPD_LEVEL_DEBUG,
1787                                "unknown mgmt frame subtype %d", stype);
1788                 break;
1789         }
1790 }
1791
1792
1793 static void handle_auth_cb(struct hostapd_data *hapd,
1794                            struct ieee80211_mgmt *mgmt,
1795                            size_t len, int ok)
1796 {
1797         u16 auth_alg, auth_transaction, status_code;
1798         struct sta_info *sta;
1799
1800         if (!ok) {
1801                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1802                                HOSTAPD_LEVEL_NOTICE,
1803                                "did not acknowledge authentication response");
1804                 return;
1805         }
1806
1807         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1808                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1809                        (unsigned long) len);
1810                 return;
1811         }
1812
1813         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1814         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1815         status_code = le_to_host16(mgmt->u.auth.status_code);
1816
1817         sta = ap_get_sta(hapd, mgmt->da);
1818         if (!sta) {
1819                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1820                        MAC2STR(mgmt->da));
1821                 return;
1822         }
1823
1824         if (status_code == WLAN_STATUS_SUCCESS &&
1825             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1826              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1827                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1828                                HOSTAPD_LEVEL_INFO, "authenticated");
1829                 sta->flags |= WLAN_STA_AUTH;
1830         }
1831 }
1832
1833
1834 static void handle_assoc_cb(struct hostapd_data *hapd,
1835                             struct ieee80211_mgmt *mgmt,
1836                             size_t len, int reassoc, int ok)
1837 {
1838         u16 status;
1839         struct sta_info *sta;
1840         int new_assoc = 1;
1841         struct ht_cap_ie *ht_cap = NULL;
1842
1843         if (!ok) {
1844                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1845                                HOSTAPD_LEVEL_DEBUG,
1846                                "did not acknowledge association response");
1847                 return;
1848         }
1849
1850         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1851                                       sizeof(mgmt->u.assoc_resp))) {
1852                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1853                        "(len=%lu)\n", reassoc, (unsigned long) len);
1854                 return;
1855         }
1856
1857         if (reassoc)
1858                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1859         else
1860                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1861
1862         sta = ap_get_sta(hapd, mgmt->da);
1863         if (!sta) {
1864                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1865                        MAC2STR(mgmt->da));
1866                 return;
1867         }
1868
1869         if (status != WLAN_STATUS_SUCCESS)
1870                 goto fail;
1871
1872         /* Stop previous accounting session, if one is started, and allocate
1873          * new session id for the new session. */
1874         accounting_sta_stop(hapd, sta);
1875         accounting_sta_get_id(hapd, sta);
1876
1877         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1878                        HOSTAPD_LEVEL_INFO,
1879                        "associated (aid %d, accounting session %08X-%08X)",
1880                        sta->aid, sta->acct_session_id_hi,
1881                        sta->acct_session_id_lo);
1882
1883         if (sta->flags & WLAN_STA_ASSOC)
1884                 new_assoc = 0;
1885         sta->flags |= WLAN_STA_ASSOC;
1886
1887         if (reassoc)
1888                 mlme_reassociate_indication(hapd, sta);
1889         else
1890                 mlme_associate_indication(hapd, sta);
1891
1892 #ifdef CONFIG_IEEE80211N
1893         if (sta->flags & WLAN_STA_HT)
1894                 ht_cap = &sta->ht_capabilities;
1895 #endif /* CONFIG_IEEE80211N */
1896
1897 #ifdef CONFIG_IEEE80211W
1898         sta->ping_timed_out = 0;
1899 #endif /* CONFIG_IEEE80211W */
1900
1901         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1902                             sta->capability, sta->supported_rates,
1903                             sta->supported_rates_len, 0, sta->listen_interval,
1904                             ht_cap))
1905         {
1906                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1907                                HOSTAPD_LEVEL_NOTICE,
1908                                "Could not add STA to kernel driver");
1909         }
1910
1911         if (sta->eapol_sm == NULL) {
1912                 /*
1913                  * This STA does not use RADIUS server for EAP authentication,
1914                  * so bind it to the selected VLAN interface now, since the
1915                  * interface selection is not going to change anymore.
1916                  */
1917                 ap_sta_bind_vlan(hapd, sta, 0);
1918         } else if (sta->vlan_id) {
1919                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1920                 ap_sta_bind_vlan(hapd, sta, 0);
1921         }
1922         if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
1923                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1924                                       WLAN_STA_SHORT_PREAMBLE, ~0);
1925         } else {
1926                 hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
1927                                       0, ~WLAN_STA_SHORT_PREAMBLE);
1928         }
1929
1930         if (sta->auth_alg == WLAN_AUTH_FT)
1931                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1932         else
1933                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1934         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1935
1936         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1937
1938  fail:
1939         /* Copy of the association request is not needed anymore */
1940         if (sta->last_assoc_req) {
1941                 os_free(sta->last_assoc_req);
1942                 sta->last_assoc_req = NULL;
1943         }
1944 }
1945
1946
1947 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1948                         u16 stype, int ok)
1949 {
1950         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1951
1952         switch (stype) {
1953         case WLAN_FC_STYPE_AUTH:
1954                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1955                 handle_auth_cb(hapd, mgmt, len, ok);
1956                 break;
1957         case WLAN_FC_STYPE_ASSOC_RESP:
1958                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1959                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1960                 break;
1961         case WLAN_FC_STYPE_REASSOC_RESP:
1962                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1963                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1964                 break;
1965         case WLAN_FC_STYPE_PROBE_RESP:
1966                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1967                 break;
1968         case WLAN_FC_STYPE_DEAUTH:
1969                 /* ignore */
1970                 break;
1971         case WLAN_FC_STYPE_ACTION:
1972                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1973                 break;
1974         default:
1975                 printf("unknown mgmt cb frame subtype %d\n", stype);
1976                 break;
1977         }
1978 }
1979
1980
1981 static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
1982                                                 void *timeout_ctx)
1983 {
1984         struct hostapd_data *hapd = eloop_ctx;
1985         hapd->tkip_countermeasures = 0;
1986         hostapd_set_countermeasures(hapd, 0);
1987         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1988                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
1989 }
1990
1991
1992 static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
1993 {
1994         struct sta_info *sta;
1995
1996         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1997                        HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
1998
1999         wpa_auth_countermeasures_start(hapd->wpa_auth);
2000         hapd->tkip_countermeasures = 1;
2001         hostapd_set_countermeasures(hapd, 1);
2002         wpa_gtk_rekey(hapd->wpa_auth);
2003         eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
2004         eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
2005                                hapd, NULL);
2006         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
2007                 hostapd_sta_deauth(hapd, sta->addr,
2008                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
2009                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
2010                                 WLAN_STA_AUTHORIZED);
2011                 hostapd_sta_remove(hapd, sta->addr);
2012         }
2013 }
2014
2015
2016 void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
2017                                    int local)
2018 {
2019         time_t now;
2020
2021         if (addr && local) {
2022                 struct sta_info *sta = ap_get_sta(hapd, addr);
2023                 if (sta != NULL) {
2024                         wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
2025                         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
2026                                        HOSTAPD_LEVEL_INFO,
2027                                        "Michael MIC failure detected in "
2028                                        "received frame");
2029                         mlme_michaelmicfailure_indication(hapd, addr);
2030                 } else {
2031                         wpa_printf(MSG_DEBUG,
2032                                    "MLME-MICHAELMICFAILURE.indication "
2033                                    "for not associated STA (" MACSTR
2034                                    ") ignored", MAC2STR(addr));
2035                         return;
2036                 }
2037         }
2038
2039         time(&now);
2040         if (now > hapd->michael_mic_failure + 60) {
2041                 hapd->michael_mic_failures = 1;
2042         } else {
2043                 hapd->michael_mic_failures++;
2044                 if (hapd->michael_mic_failures > 1)
2045                         ieee80211_tkip_countermeasures_start(hapd);
2046         }
2047         hapd->michael_mic_failure = now;
2048 }
2049
2050
2051 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2052 {
2053         /* TODO */
2054         return 0;
2055 }
2056
2057
2058 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2059                            char *buf, size_t buflen)
2060 {
2061         /* TODO */
2062         return 0;
2063 }
2064
2065 #endif /* CONFIG_NATIVE_WINDOWS */