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