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