]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa/src/ap/ieee802_1x.c
MFV r341618:
[FreeBSD/FreeBSD.git] / contrib / wpa / src / ap / ieee802_1x.c
1 /*
2  * hostapd / IEEE 802.1X-2004 Authenticator
3  * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "crypto/md5.h"
14 #include "crypto/crypto.h"
15 #include "crypto/random.h"
16 #include "common/ieee802_11_defs.h"
17 #include "radius/radius.h"
18 #include "radius/radius_client.h"
19 #include "eap_server/eap.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "eapol_auth/eapol_auth_sm_i.h"
23 #include "p2p/p2p.h"
24 #include "hostapd.h"
25 #include "accounting.h"
26 #include "sta_info.h"
27 #include "wpa_auth.h"
28 #include "preauth_auth.h"
29 #include "pmksa_cache_auth.h"
30 #include "ap_config.h"
31 #include "ap_drv_ops.h"
32 #include "wps_hostapd.h"
33 #include "hs20.h"
34 /* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
35 #include "ieee802_11.h"
36 #include "ieee802_1x.h"
37
38
39 #ifdef CONFIG_HS20
40 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
41 #endif /* CONFIG_HS20 */
42 static void ieee802_1x_finished(struct hostapd_data *hapd,
43                                 struct sta_info *sta, int success,
44                                 int remediation);
45
46
47 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
48                             u8 type, const u8 *data, size_t datalen)
49 {
50         u8 *buf;
51         struct ieee802_1x_hdr *xhdr;
52         size_t len;
53         int encrypt = 0;
54
55         len = sizeof(*xhdr) + datalen;
56         buf = os_zalloc(len);
57         if (buf == NULL) {
58                 wpa_printf(MSG_ERROR, "malloc() failed for "
59                            "ieee802_1x_send(len=%lu)",
60                            (unsigned long) len);
61                 return;
62         }
63
64         xhdr = (struct ieee802_1x_hdr *) buf;
65         xhdr->version = hapd->conf->eapol_version;
66         xhdr->type = type;
67         xhdr->length = host_to_be16(datalen);
68
69         if (datalen > 0 && data != NULL)
70                 os_memcpy(xhdr + 1, data, datalen);
71
72         if (wpa_auth_pairwise_set(sta->wpa_sm))
73                 encrypt = 1;
74 #ifdef CONFIG_TESTING_OPTIONS
75         if (hapd->ext_eapol_frame_io) {
76                 size_t hex_len = 2 * len + 1;
77                 char *hex = os_malloc(hex_len);
78
79                 if (hex) {
80                         wpa_snprintf_hex(hex, hex_len, buf, len);
81                         wpa_msg(hapd->msg_ctx, MSG_INFO,
82                                 "EAPOL-TX " MACSTR " %s",
83                                 MAC2STR(sta->addr), hex);
84                         os_free(hex);
85                 }
86         } else
87 #endif /* CONFIG_TESTING_OPTIONS */
88         if (sta->flags & WLAN_STA_PREAUTH) {
89                 rsn_preauth_send(hapd, sta, buf, len);
90         } else {
91                 hostapd_drv_hapd_send_eapol(
92                         hapd, sta->addr, buf, len,
93                         encrypt, hostapd_sta_flags_to_drv(sta->flags));
94         }
95
96         os_free(buf);
97 }
98
99
100 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
101                                    struct sta_info *sta, int authorized)
102 {
103         int res;
104
105         if (sta->flags & WLAN_STA_PREAUTH)
106                 return;
107
108         if (authorized) {
109                 ap_sta_set_authorized(hapd, sta, 1);
110                 res = hostapd_set_authorized(hapd, sta, 1);
111                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
112                                HOSTAPD_LEVEL_DEBUG, "authorizing port");
113         } else {
114                 ap_sta_set_authorized(hapd, sta, 0);
115                 res = hostapd_set_authorized(hapd, sta, 0);
116                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
117                                HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
118         }
119
120         if (res && errno != ENOENT) {
121                 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
122                            " flags for kernel driver (errno=%d).",
123                            MAC2STR(sta->addr), errno);
124         }
125
126         if (authorized) {
127                 os_get_reltime(&sta->connected_time);
128                 accounting_sta_start(hapd, sta);
129         }
130 }
131
132
133 #ifndef CONFIG_FIPS
134 #ifndef CONFIG_NO_RC4
135
136 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
137                                   struct sta_info *sta,
138                                   int idx, int broadcast,
139                                   u8 *key_data, size_t key_len)
140 {
141         u8 *buf, *ekey;
142         struct ieee802_1x_hdr *hdr;
143         struct ieee802_1x_eapol_key *key;
144         size_t len, ekey_len;
145         struct eapol_state_machine *sm = sta->eapol_sm;
146
147         if (sm == NULL)
148                 return;
149
150         len = sizeof(*key) + key_len;
151         buf = os_zalloc(sizeof(*hdr) + len);
152         if (buf == NULL)
153                 return;
154
155         hdr = (struct ieee802_1x_hdr *) buf;
156         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
157         key->type = EAPOL_KEY_TYPE_RC4;
158         WPA_PUT_BE16(key->key_length, key_len);
159         wpa_get_ntp_timestamp(key->replay_counter);
160
161         if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
162                 wpa_printf(MSG_ERROR, "Could not get random numbers");
163                 os_free(buf);
164                 return;
165         }
166
167         key->key_index = idx | (broadcast ? 0 : BIT(7));
168         if (hapd->conf->eapol_key_index_workaround) {
169                 /* According to some information, WinXP Supplicant seems to
170                  * interpret bit7 as an indication whether the key is to be
171                  * activated, so make it possible to enable workaround that
172                  * sets this bit for all keys. */
173                 key->key_index |= BIT(7);
174         }
175
176         /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
177          * MSK[32..63] is used to sign the message. */
178         if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
179                 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
180                            "and signing EAPOL-Key");
181                 os_free(buf);
182                 return;
183         }
184         os_memcpy((u8 *) (key + 1), key_data, key_len);
185         ekey_len = sizeof(key->key_iv) + 32;
186         ekey = os_malloc(ekey_len);
187         if (ekey == NULL) {
188                 wpa_printf(MSG_ERROR, "Could not encrypt key");
189                 os_free(buf);
190                 return;
191         }
192         os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
193         os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
194         rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
195         os_free(ekey);
196
197         /* This header is needed here for HMAC-MD5, but it will be regenerated
198          * in ieee802_1x_send() */
199         hdr->version = hapd->conf->eapol_version;
200         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
201         hdr->length = host_to_be16(len);
202         hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
203                  key->key_signature);
204
205         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
206                    " (%s index=%d)", MAC2STR(sm->addr),
207                    broadcast ? "broadcast" : "unicast", idx);
208         ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
209         if (sta->eapol_sm)
210                 sta->eapol_sm->dot1xAuthEapolFramesTx++;
211         os_free(buf);
212 }
213
214
215 static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
216 {
217         struct eapol_authenticator *eapol = hapd->eapol_auth;
218         struct eapol_state_machine *sm = sta->eapol_sm;
219
220         if (sm == NULL || !sm->eap_if->eapKeyData)
221                 return;
222
223         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
224                    MAC2STR(sta->addr));
225
226 #ifndef CONFIG_NO_VLAN
227         if (sta->vlan_id > 0) {
228                 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
229                 return;
230         }
231 #endif /* CONFIG_NO_VLAN */
232
233         if (eapol->default_wep_key) {
234                 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
235                                       eapol->default_wep_key,
236                                       hapd->conf->default_wep_key_len);
237         }
238
239         if (hapd->conf->individual_wep_key_len > 0) {
240                 u8 *ikey;
241                 ikey = os_malloc(hapd->conf->individual_wep_key_len);
242                 if (ikey == NULL ||
243                     random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
244                 {
245                         wpa_printf(MSG_ERROR, "Could not generate random "
246                                    "individual WEP key.");
247                         os_free(ikey);
248                         return;
249                 }
250
251                 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
252                                 ikey, hapd->conf->individual_wep_key_len);
253
254                 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
255                                       hapd->conf->individual_wep_key_len);
256
257                 /* TODO: set encryption in TX callback, i.e., only after STA
258                  * has ACKed EAPOL-Key frame */
259                 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
260                                         sta->addr, 0, 1, NULL, 0, ikey,
261                                         hapd->conf->individual_wep_key_len)) {
262                         wpa_printf(MSG_ERROR, "Could not set individual WEP "
263                                    "encryption.");
264                 }
265
266                 os_free(ikey);
267         }
268 }
269
270 #endif /* CONFIG_NO_RC4 */
271 #endif /* CONFIG_FIPS */
272
273
274 const char *radius_mode_txt(struct hostapd_data *hapd)
275 {
276         switch (hapd->iface->conf->hw_mode) {
277         case HOSTAPD_MODE_IEEE80211AD:
278                 return "802.11ad";
279         case HOSTAPD_MODE_IEEE80211A:
280                 return "802.11a";
281         case HOSTAPD_MODE_IEEE80211G:
282                 return "802.11g";
283         case HOSTAPD_MODE_IEEE80211B:
284         default:
285                 return "802.11b";
286         }
287 }
288
289
290 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
291 {
292         int i;
293         u8 rate = 0;
294
295         for (i = 0; i < sta->supported_rates_len; i++)
296                 if ((sta->supported_rates[i] & 0x7f) > rate)
297                         rate = sta->supported_rates[i] & 0x7f;
298
299         return rate;
300 }
301
302
303 #ifndef CONFIG_NO_RADIUS
304 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
305                                       struct eapol_state_machine *sm,
306                                       const u8 *eap, size_t len)
307 {
308         const u8 *identity;
309         size_t identity_len;
310         const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
311
312         if (len <= sizeof(struct eap_hdr) ||
313             (hdr->code == EAP_CODE_RESPONSE &&
314              eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
315             (hdr->code == EAP_CODE_INITIATE &&
316              eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
317             (hdr->code != EAP_CODE_RESPONSE &&
318              hdr->code != EAP_CODE_INITIATE))
319                 return;
320
321         eap_erp_update_identity(sm->eap, eap, len);
322         identity = eap_get_identity(sm->eap, &identity_len);
323         if (identity == NULL)
324                 return;
325
326         /* Save station identity for future RADIUS packets */
327         os_free(sm->identity);
328         sm->identity = (u8 *) dup_binstr(identity, identity_len);
329         if (sm->identity == NULL) {
330                 sm->identity_len = 0;
331                 return;
332         }
333
334         sm->identity_len = identity_len;
335         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
336                        HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
337         sm->dot1xAuthEapolRespIdFramesRx++;
338 }
339
340
341 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
342                                           struct hostapd_radius_attr *req_attr,
343                                           struct sta_info *sta,
344                                           struct radius_msg *msg)
345 {
346         u32 suite;
347         int ver, val;
348
349         ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
350         val = wpa_auth_get_pairwise(sta->wpa_sm);
351         suite = wpa_cipher_to_suite(ver, val);
352         if (val != -1 &&
353             !hostapd_config_get_radius_attr(req_attr,
354                                             RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
355             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
356                                        suite)) {
357                 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
358                 return -1;
359         }
360
361         suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
362                                      hapd->conf->osen) ?
363                                     WPA_PROTO_RSN : WPA_PROTO_WPA,
364                                     hapd->conf->wpa_group);
365         if (!hostapd_config_get_radius_attr(req_attr,
366                                             RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
367             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
368                                        suite)) {
369                 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
370                 return -1;
371         }
372
373         val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
374         suite = wpa_akm_to_suite(val);
375         if (val != -1 &&
376             !hostapd_config_get_radius_attr(req_attr,
377                                             RADIUS_ATTR_WLAN_AKM_SUITE) &&
378             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
379                                        suite)) {
380                 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
381                 return -1;
382         }
383
384 #ifdef CONFIG_IEEE80211W
385         if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
386                 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
387                                             hapd->conf->group_mgmt_cipher);
388                 if (!hostapd_config_get_radius_attr(
389                             req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
390                     !radius_msg_add_attr_int32(
391                             msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
392                         wpa_printf(MSG_ERROR,
393                                    "Could not add WLAN-Group-Mgmt-Cipher");
394                         return -1;
395                 }
396         }
397 #endif /* CONFIG_IEEE80211W */
398
399         return 0;
400 }
401
402
403 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
404                                       struct hostapd_radius_attr *req_attr,
405                                       struct sta_info *sta,
406                                       struct radius_msg *msg)
407 {
408         char buf[128];
409
410         if (!hostapd_config_get_radius_attr(req_attr,
411                                             RADIUS_ATTR_SERVICE_TYPE) &&
412             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
413                                        RADIUS_SERVICE_TYPE_FRAMED)) {
414                 wpa_printf(MSG_ERROR, "Could not add Service-Type");
415                 return -1;
416         }
417
418         if (!hostapd_config_get_radius_attr(req_attr,
419                                             RADIUS_ATTR_NAS_PORT) &&
420             sta->aid > 0 &&
421             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
422                 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
423                 return -1;
424         }
425
426         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
427                     MAC2STR(sta->addr));
428         buf[sizeof(buf) - 1] = '\0';
429         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
430                                  (u8 *) buf, os_strlen(buf))) {
431                 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
432                 return -1;
433         }
434
435         if (sta->flags & WLAN_STA_PREAUTH) {
436                 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
437                            sizeof(buf));
438         } else {
439                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
440                             radius_sta_rate(hapd, sta) / 2,
441                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
442                             radius_mode_txt(hapd));
443                 buf[sizeof(buf) - 1] = '\0';
444         }
445         if (!hostapd_config_get_radius_attr(req_attr,
446                                             RADIUS_ATTR_CONNECT_INFO) &&
447             !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
448                                  (u8 *) buf, os_strlen(buf))) {
449                 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
450                 return -1;
451         }
452
453         if (sta->acct_session_id) {
454                 os_snprintf(buf, sizeof(buf), "%016llX",
455                             (unsigned long long) sta->acct_session_id);
456                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
457                                          (u8 *) buf, os_strlen(buf))) {
458                         wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
459                         return -1;
460                 }
461         }
462
463         if ((hapd->conf->wpa & 2) &&
464             !hapd->conf->disable_pmksa_caching &&
465             sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
466                 os_snprintf(buf, sizeof(buf), "%016llX",
467                             (unsigned long long)
468                             sta->eapol_sm->acct_multi_session_id);
469                 if (!radius_msg_add_attr(
470                             msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
471                             (u8 *) buf, os_strlen(buf))) {
472                         wpa_printf(MSG_INFO,
473                                    "Could not add Acct-Multi-Session-Id");
474                         return -1;
475                 }
476         }
477
478 #ifdef CONFIG_IEEE80211R_AP
479         if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
480             sta->wpa_sm &&
481             (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
482              sta->auth_alg == WLAN_AUTH_FT) &&
483             !hostapd_config_get_radius_attr(req_attr,
484                                             RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
485             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
486                                        WPA_GET_BE16(
487                                                hapd->conf->mobility_domain))) {
488                 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
489                 return -1;
490         }
491 #endif /* CONFIG_IEEE80211R_AP */
492
493         if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
494             add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
495                 return -1;
496
497         return 0;
498 }
499
500
501 int add_common_radius_attr(struct hostapd_data *hapd,
502                            struct hostapd_radius_attr *req_attr,
503                            struct sta_info *sta,
504                            struct radius_msg *msg)
505 {
506         char buf[128];
507         struct hostapd_radius_attr *attr;
508         int len;
509
510         if (!hostapd_config_get_radius_attr(req_attr,
511                                             RADIUS_ATTR_NAS_IP_ADDRESS) &&
512             hapd->conf->own_ip_addr.af == AF_INET &&
513             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
514                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
515                 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
516                 return -1;
517         }
518
519 #ifdef CONFIG_IPV6
520         if (!hostapd_config_get_radius_attr(req_attr,
521                                             RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
522             hapd->conf->own_ip_addr.af == AF_INET6 &&
523             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
524                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
525                 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
526                 return -1;
527         }
528 #endif /* CONFIG_IPV6 */
529
530         if (!hostapd_config_get_radius_attr(req_attr,
531                                             RADIUS_ATTR_NAS_IDENTIFIER) &&
532             hapd->conf->nas_identifier &&
533             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
534                                  (u8 *) hapd->conf->nas_identifier,
535                                  os_strlen(hapd->conf->nas_identifier))) {
536                 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
537                 return -1;
538         }
539
540         len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
541                           MAC2STR(hapd->own_addr));
542         os_memcpy(&buf[len], hapd->conf->ssid.ssid,
543                   hapd->conf->ssid.ssid_len);
544         len += hapd->conf->ssid.ssid_len;
545         if (!hostapd_config_get_radius_attr(req_attr,
546                                             RADIUS_ATTR_CALLED_STATION_ID) &&
547             !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
548                                  (u8 *) buf, len)) {
549                 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
550                 return -1;
551         }
552
553         if (!hostapd_config_get_radius_attr(req_attr,
554                                             RADIUS_ATTR_NAS_PORT_TYPE) &&
555             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
556                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
557                 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
558                 return -1;
559         }
560
561 #ifdef CONFIG_INTERWORKING
562         if (hapd->conf->interworking &&
563             !is_zero_ether_addr(hapd->conf->hessid)) {
564                 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
565                             MAC2STR(hapd->conf->hessid));
566                 buf[sizeof(buf) - 1] = '\0';
567                 if (!hostapd_config_get_radius_attr(req_attr,
568                                                     RADIUS_ATTR_WLAN_HESSID) &&
569                     !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
570                                          (u8 *) buf, os_strlen(buf))) {
571                         wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
572                         return -1;
573                 }
574         }
575 #endif /* CONFIG_INTERWORKING */
576
577         if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
578                 return -1;
579
580         for (attr = req_attr; attr; attr = attr->next) {
581                 if (!radius_msg_add_attr(msg, attr->type,
582                                          wpabuf_head(attr->val),
583                                          wpabuf_len(attr->val))) {
584                         wpa_printf(MSG_ERROR, "Could not add RADIUS "
585                                    "attribute");
586                         return -1;
587                 }
588         }
589
590         return 0;
591 }
592
593
594 void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
595                                    struct sta_info *sta,
596                                    const u8 *eap, size_t len)
597 {
598         struct radius_msg *msg;
599         struct eapol_state_machine *sm = sta->eapol_sm;
600
601         if (sm == NULL)
602                 return;
603
604         ieee802_1x_learn_identity(hapd, sm, eap, len);
605
606         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
607                    "packet");
608
609         sm->radius_identifier = radius_client_get_id(hapd->radius);
610         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
611                              sm->radius_identifier);
612         if (msg == NULL) {
613                 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
614                 return;
615         }
616
617         if (radius_msg_make_authenticator(msg) < 0) {
618                 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
619                 goto fail;
620         }
621
622         if (sm->identity &&
623             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
624                                  sm->identity, sm->identity_len)) {
625                 wpa_printf(MSG_INFO, "Could not add User-Name");
626                 goto fail;
627         }
628
629         if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
630                                    msg) < 0)
631                 goto fail;
632
633         /* TODO: should probably check MTU from driver config; 2304 is max for
634          * IEEE 802.11, but use 1400 to avoid problems with too large packets
635          */
636         if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
637                                             RADIUS_ATTR_FRAMED_MTU) &&
638             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
639                 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
640                 goto fail;
641         }
642
643         if (!radius_msg_add_eap(msg, eap, len)) {
644                 wpa_printf(MSG_INFO, "Could not add EAP-Message");
645                 goto fail;
646         }
647
648         /* State attribute must be copied if and only if this packet is
649          * Access-Request reply to the previous Access-Challenge */
650         if (sm->last_recv_radius &&
651             radius_msg_get_hdr(sm->last_recv_radius)->code ==
652             RADIUS_CODE_ACCESS_CHALLENGE) {
653                 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
654                                                RADIUS_ATTR_STATE);
655                 if (res < 0) {
656                         wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
657                         goto fail;
658                 }
659                 if (res > 0) {
660                         wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
661                 }
662         }
663
664         if (hapd->conf->radius_request_cui) {
665                 const u8 *cui;
666                 size_t cui_len;
667                 /* Add previously learned CUI or nul CUI to request CUI */
668                 if (sm->radius_cui) {
669                         cui = wpabuf_head(sm->radius_cui);
670                         cui_len = wpabuf_len(sm->radius_cui);
671                 } else {
672                         cui = (const u8 *) "\0";
673                         cui_len = 1;
674                 }
675                 if (!radius_msg_add_attr(msg,
676                                          RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
677                                          cui, cui_len)) {
678                         wpa_printf(MSG_ERROR, "Could not add CUI");
679                         goto fail;
680                 }
681         }
682
683 #ifdef CONFIG_HS20
684         if (hapd->conf->hs20) {
685                 u8 ver = 1; /* Release 2 */
686                 if (HS20_VERSION > 0x10)
687                         ver = 2; /* Release 3 */
688                 if (!radius_msg_add_wfa(
689                             msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
690                             &ver, 1)) {
691                         wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
692                                    "version");
693                         goto fail;
694                 }
695
696                 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
697                         const u8 *pos;
698                         u8 buf[3];
699                         u16 id;
700                         pos = wpabuf_head_u8(sta->hs20_ie);
701                         buf[0] = (*pos) >> 4;
702                         if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
703                             wpabuf_len(sta->hs20_ie) >= 3)
704                                 id = WPA_GET_LE16(pos + 1);
705                         else
706                                 id = 0;
707                         WPA_PUT_BE16(buf + 1, id);
708                         if (!radius_msg_add_wfa(
709                                     msg,
710                                     RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
711                                     buf, sizeof(buf))) {
712                                 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
713                                            "STA version");
714                                 goto fail;
715                         }
716                 }
717
718                 if (sta->roaming_consortium &&
719                     !radius_msg_add_wfa(
720                             msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
721                             wpabuf_head(sta->roaming_consortium),
722                             wpabuf_len(sta->roaming_consortium))) {
723                         wpa_printf(MSG_ERROR,
724                                    "Could not add HS 2.0 Roaming Consortium");
725                         goto fail;
726                 }
727
728                 if (hapd->conf->t_c_filename) {
729                         be32 timestamp;
730
731                         if (!radius_msg_add_wfa(
732                                     msg,
733                                     RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
734                                     (const u8 *) hapd->conf->t_c_filename,
735                                     os_strlen(hapd->conf->t_c_filename))) {
736                                 wpa_printf(MSG_ERROR,
737                                            "Could not add HS 2.0 T&C Filename");
738                                 goto fail;
739                         }
740
741                         timestamp = host_to_be32(hapd->conf->t_c_timestamp);
742                         if (!radius_msg_add_wfa(
743                                     msg,
744                                     RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
745                                     (const u8 *) &timestamp,
746                                     sizeof(timestamp))) {
747                                 wpa_printf(MSG_ERROR,
748                                            "Could not add HS 2.0 Timestamp");
749                                 goto fail;
750                         }
751                 }
752         }
753 #endif /* CONFIG_HS20 */
754
755         if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
756                 goto fail;
757
758         return;
759
760  fail:
761         radius_msg_free(msg);
762 }
763 #endif /* CONFIG_NO_RADIUS */
764
765
766 static void handle_eap_response(struct hostapd_data *hapd,
767                                 struct sta_info *sta, struct eap_hdr *eap,
768                                 size_t len)
769 {
770         u8 type, *data;
771         struct eapol_state_machine *sm = sta->eapol_sm;
772         if (sm == NULL)
773                 return;
774
775         data = (u8 *) (eap + 1);
776
777         if (len < sizeof(*eap) + 1) {
778                 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
779                 return;
780         }
781
782         sm->eap_type_supp = type = data[0];
783
784         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
785                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
786                        "id=%d len=%d) from STA: EAP Response-%s (%d)",
787                        eap->code, eap->identifier, be_to_host16(eap->length),
788                        eap_server_get_name(0, type), type);
789
790         sm->dot1xAuthEapolRespFramesRx++;
791
792         wpabuf_free(sm->eap_if->eapRespData);
793         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
794         sm->eapolEap = TRUE;
795 }
796
797
798 static void handle_eap_initiate(struct hostapd_data *hapd,
799                                 struct sta_info *sta, struct eap_hdr *eap,
800                                 size_t len)
801 {
802 #ifdef CONFIG_ERP
803         u8 type, *data;
804         struct eapol_state_machine *sm = sta->eapol_sm;
805
806         if (sm == NULL)
807                 return;
808
809         if (len < sizeof(*eap) + 1) {
810                 wpa_printf(MSG_INFO,
811                            "handle_eap_initiate: too short response data");
812                 return;
813         }
814
815         data = (u8 *) (eap + 1);
816         type = data[0];
817
818         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
819                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
820                        "id=%d len=%d) from STA: EAP Initiate type %u",
821                        eap->code, eap->identifier, be_to_host16(eap->length),
822                        type);
823
824         wpabuf_free(sm->eap_if->eapRespData);
825         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
826         sm->eapolEap = TRUE;
827 #endif /* CONFIG_ERP */
828 }
829
830
831 /* Process incoming EAP packet from Supplicant */
832 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
833                        u8 *buf, size_t len)
834 {
835         struct eap_hdr *eap;
836         u16 eap_len;
837
838         if (len < sizeof(*eap)) {
839                 wpa_printf(MSG_INFO, "   too short EAP packet");
840                 return;
841         }
842
843         eap = (struct eap_hdr *) buf;
844
845         eap_len = be_to_host16(eap->length);
846         wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
847                    eap->code, eap->identifier, eap_len);
848         if (eap_len < sizeof(*eap)) {
849                 wpa_printf(MSG_DEBUG, "   Invalid EAP length");
850                 return;
851         } else if (eap_len > len) {
852                 wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
853                            "packet");
854                 return;
855         } else if (eap_len < len) {
856                 wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
857                            "packet", (unsigned long) len - eap_len);
858         }
859
860         switch (eap->code) {
861         case EAP_CODE_REQUEST:
862                 wpa_printf(MSG_DEBUG, " (request)");
863                 return;
864         case EAP_CODE_RESPONSE:
865                 wpa_printf(MSG_DEBUG, " (response)");
866                 handle_eap_response(hapd, sta, eap, eap_len);
867                 break;
868         case EAP_CODE_SUCCESS:
869                 wpa_printf(MSG_DEBUG, " (success)");
870                 return;
871         case EAP_CODE_FAILURE:
872                 wpa_printf(MSG_DEBUG, " (failure)");
873                 return;
874         case EAP_CODE_INITIATE:
875                 wpa_printf(MSG_DEBUG, " (initiate)");
876                 handle_eap_initiate(hapd, sta, eap, eap_len);
877                 break;
878         case EAP_CODE_FINISH:
879                 wpa_printf(MSG_DEBUG, " (finish)");
880                 break;
881         default:
882                 wpa_printf(MSG_DEBUG, " (unknown code)");
883                 return;
884         }
885 }
886
887
888 struct eapol_state_machine *
889 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
890 {
891         int flags = 0;
892         if (sta->flags & WLAN_STA_PREAUTH)
893                 flags |= EAPOL_SM_PREAUTH;
894         if (sta->wpa_sm) {
895                 flags |= EAPOL_SM_USES_WPA;
896                 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
897                         flags |= EAPOL_SM_FROM_PMKSA_CACHE;
898         }
899         return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
900                                 sta->wps_ie, sta->p2p_ie, sta,
901                                 sta->identity, sta->radius_cui);
902 }
903
904
905 static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
906                                   size_t len)
907 {
908         if (sta->pending_eapol_rx) {
909                 wpabuf_free(sta->pending_eapol_rx->buf);
910         } else {
911                 sta->pending_eapol_rx =
912                         os_malloc(sizeof(*sta->pending_eapol_rx));
913                 if (!sta->pending_eapol_rx)
914                         return;
915         }
916
917         sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
918         if (!sta->pending_eapol_rx->buf) {
919                 os_free(sta->pending_eapol_rx);
920                 sta->pending_eapol_rx = NULL;
921                 return;
922         }
923
924         os_get_reltime(&sta->pending_eapol_rx->rx_time);
925 }
926
927
928 /**
929  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
930  * @hapd: hostapd BSS data
931  * @sa: Source address (sender of the EAPOL frame)
932  * @buf: EAPOL frame
933  * @len: Length of buf in octets
934  *
935  * This function is called for each incoming EAPOL frame from the interface
936  */
937 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
938                         size_t len)
939 {
940         struct sta_info *sta;
941         struct ieee802_1x_hdr *hdr;
942         struct ieee802_1x_eapol_key *key;
943         u16 datalen;
944         struct rsn_pmksa_cache_entry *pmksa;
945         int key_mgmt;
946
947         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
948             !hapd->conf->wps_state)
949                 return;
950
951         wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
952                    (unsigned long) len, MAC2STR(sa));
953         sta = ap_get_sta(hapd, sa);
954         if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
955                      !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
956                 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
957                            "associated/Pre-authenticating STA");
958
959                 if (sta && (sta->flags & WLAN_STA_AUTH)) {
960                         wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
961                                    " for later use", MAC2STR(sta->addr));
962                         ieee802_1x_save_eapol(sta, buf, len);
963                 }
964
965                 return;
966         }
967
968         if (len < sizeof(*hdr)) {
969                 wpa_printf(MSG_INFO, "   too short IEEE 802.1X packet");
970                 return;
971         }
972
973         hdr = (struct ieee802_1x_hdr *) buf;
974         datalen = be_to_host16(hdr->length);
975         wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
976                    hdr->version, hdr->type, datalen);
977
978         if (len - sizeof(*hdr) < datalen) {
979                 wpa_printf(MSG_INFO, "   frame too short for this IEEE 802.1X packet");
980                 if (sta->eapol_sm)
981                         sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
982                 return;
983         }
984         if (len - sizeof(*hdr) > datalen) {
985                 wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
986                            "IEEE 802.1X packet",
987                            (unsigned long) len - sizeof(*hdr) - datalen);
988         }
989
990         if (sta->eapol_sm) {
991                 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
992                 sta->eapol_sm->dot1xAuthEapolFramesRx++;
993         }
994
995         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
996         if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
997             hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
998             (key->type == EAPOL_KEY_TYPE_WPA ||
999              key->type == EAPOL_KEY_TYPE_RSN)) {
1000                 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
1001                             sizeof(*hdr) + datalen);
1002                 return;
1003         }
1004
1005         if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
1006             !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
1007                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
1008                            "802.1X not enabled and WPS not used");
1009                 return;
1010         }
1011
1012         key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1013         if (key_mgmt != -1 &&
1014             (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1015              key_mgmt == WPA_KEY_MGMT_DPP)) {
1016                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
1017                            "STA is using PSK");
1018                 return;
1019         }
1020
1021         if (!sta->eapol_sm) {
1022                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1023                 if (!sta->eapol_sm)
1024                         return;
1025
1026 #ifdef CONFIG_WPS
1027                 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
1028                         u32 wflags = sta->flags & (WLAN_STA_WPS |
1029                                                    WLAN_STA_WPS2 |
1030                                                    WLAN_STA_MAYBE_WPS);
1031                         if (wflags == WLAN_STA_MAYBE_WPS ||
1032                             wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
1033                                 /*
1034                                  * Delay EAPOL frame transmission until a
1035                                  * possible WPS STA initiates the handshake
1036                                  * with EAPOL-Start. Only allow the wait to be
1037                                  * skipped if the STA is known to support WPS
1038                                  * 2.0.
1039                                  */
1040                                 wpa_printf(MSG_DEBUG, "WPS: Do not start "
1041                                            "EAPOL until EAPOL-Start is "
1042                                            "received");
1043                                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1044                         }
1045                 }
1046 #endif /* CONFIG_WPS */
1047
1048                 sta->eapol_sm->eap_if->portEnabled = TRUE;
1049         }
1050
1051         /* since we support version 1, we can ignore version field and proceed
1052          * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1053         /* TODO: actually, we are not version 1 anymore.. However, Version 2
1054          * does not change frame contents, so should be ok to process frames
1055          * more or less identically. Some changes might be needed for
1056          * verification of fields. */
1057
1058         switch (hdr->type) {
1059         case IEEE802_1X_TYPE_EAP_PACKET:
1060                 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1061                 break;
1062
1063         case IEEE802_1X_TYPE_EAPOL_START:
1064                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1065                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
1066                                "from STA");
1067                 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1068                 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1069                 if (pmksa) {
1070                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1071                                        HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
1072                                        "available - ignore it since "
1073                                        "STA sent EAPOL-Start");
1074                         wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1075                 }
1076                 sta->eapol_sm->eapolStart = TRUE;
1077                 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1078                 eap_server_clear_identity(sta->eapol_sm->eap);
1079                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1080                 break;
1081
1082         case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1083                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1084                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1085                                "from STA");
1086                 sta->acct_terminate_cause =
1087                         RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1088                 accounting_sta_stop(hapd, sta);
1089                 sta->eapol_sm->eapolLogoff = TRUE;
1090                 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1091                 eap_server_clear_identity(sta->eapol_sm->eap);
1092                 break;
1093
1094         case IEEE802_1X_TYPE_EAPOL_KEY:
1095                 wpa_printf(MSG_DEBUG, "   EAPOL-Key");
1096                 if (!ap_sta_is_authorized(sta)) {
1097                         wpa_printf(MSG_DEBUG, "   Dropped key data from "
1098                                    "unauthorized Supplicant");
1099                         break;
1100                 }
1101                 break;
1102
1103         case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1104                 wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
1105                 /* TODO: implement support for this; show data */
1106                 break;
1107
1108         default:
1109                 wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
1110                 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1111                 break;
1112         }
1113
1114         eapol_auth_step(sta->eapol_sm);
1115 }
1116
1117
1118 /**
1119  * ieee802_1x_new_station - Start IEEE 802.1X authentication
1120  * @hapd: hostapd BSS data
1121  * @sta: The station
1122  *
1123  * This function is called to start IEEE 802.1X authentication when a new
1124  * station completes IEEE 802.11 association.
1125  */
1126 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1127 {
1128         struct rsn_pmksa_cache_entry *pmksa;
1129         int reassoc = 1;
1130         int force_1x = 0;
1131         int key_mgmt;
1132
1133 #ifdef CONFIG_WPS
1134         if (hapd->conf->wps_state &&
1135             ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1136              (sta->flags & WLAN_STA_WPS))) {
1137                 /*
1138                  * Need to enable IEEE 802.1X/EAPOL state machines for possible
1139                  * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1140                  * authentication in this BSS.
1141                  */
1142                 force_1x = 1;
1143         }
1144 #endif /* CONFIG_WPS */
1145
1146         if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1147                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1148                            "802.1X not enabled or forced for WPS");
1149                 /*
1150                  * Clear any possible EAPOL authenticator state to support
1151                  * reassociation change from WPS to PSK.
1152                  */
1153                 ieee802_1x_free_station(hapd, sta);
1154                 return;
1155         }
1156
1157         key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1158         if (key_mgmt != -1 &&
1159             (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1160              key_mgmt == WPA_KEY_MGMT_DPP)) {
1161                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1162                 /*
1163                  * Clear any possible EAPOL authenticator state to support
1164                  * reassociation change from WPA-EAP to PSK.
1165                  */
1166                 ieee802_1x_free_station(hapd, sta);
1167                 return;
1168         }
1169
1170         if (sta->eapol_sm == NULL) {
1171                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1172                                HOSTAPD_LEVEL_DEBUG, "start authentication");
1173                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1174                 if (sta->eapol_sm == NULL) {
1175                         hostapd_logger(hapd, sta->addr,
1176                                        HOSTAPD_MODULE_IEEE8021X,
1177                                        HOSTAPD_LEVEL_INFO,
1178                                        "failed to allocate state machine");
1179                         return;
1180                 }
1181                 reassoc = 0;
1182         }
1183
1184 #ifdef CONFIG_WPS
1185         sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1186         if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1187             !(sta->flags & WLAN_STA_WPS2)) {
1188                 /*
1189                  * Delay EAPOL frame transmission until a possible WPS STA
1190                  * initiates the handshake with EAPOL-Start. Only allow the
1191                  * wait to be skipped if the STA is known to support WPS 2.0.
1192                  */
1193                 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1194                            "EAPOL-Start is received");
1195                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1196         }
1197 #endif /* CONFIG_WPS */
1198
1199         sta->eapol_sm->eap_if->portEnabled = TRUE;
1200
1201 #ifdef CONFIG_IEEE80211R_AP
1202         if (sta->auth_alg == WLAN_AUTH_FT) {
1203                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1204                                HOSTAPD_LEVEL_DEBUG,
1205                                "PMK from FT - skip IEEE 802.1X/EAP");
1206                 /* Setup EAPOL state machines to already authenticated state
1207                  * because of existing FT information from R0KH. */
1208                 sta->eapol_sm->keyRun = TRUE;
1209                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1210                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1211                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1212                 sta->eapol_sm->authSuccess = TRUE;
1213                 sta->eapol_sm->authFail = FALSE;
1214                 sta->eapol_sm->portValid = TRUE;
1215                 if (sta->eapol_sm->eap)
1216                         eap_sm_notify_cached(sta->eapol_sm->eap);
1217                 ap_sta_bind_vlan(hapd, sta);
1218                 return;
1219         }
1220 #endif /* CONFIG_IEEE80211R_AP */
1221
1222 #ifdef CONFIG_FILS
1223         if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1224             sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1225             sta->auth_alg == WLAN_AUTH_FILS_PK) {
1226                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1227                                HOSTAPD_LEVEL_DEBUG,
1228                                "PMK from FILS - skip IEEE 802.1X/EAP");
1229                 /* Setup EAPOL state machines to already authenticated state
1230                  * because of existing FILS information. */
1231                 sta->eapol_sm->keyRun = TRUE;
1232                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1233                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1234                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1235                 sta->eapol_sm->authSuccess = TRUE;
1236                 sta->eapol_sm->authFail = FALSE;
1237                 sta->eapol_sm->portValid = TRUE;
1238                 if (sta->eapol_sm->eap)
1239                         eap_sm_notify_cached(sta->eapol_sm->eap);
1240                 return;
1241         }
1242 #endif /* CONFIG_FILS */
1243
1244         pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1245         if (pmksa) {
1246                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1247                                HOSTAPD_LEVEL_DEBUG,
1248                                "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1249                 /* Setup EAPOL state machines to already authenticated state
1250                  * because of existing PMKSA information in the cache. */
1251                 sta->eapol_sm->keyRun = TRUE;
1252                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1253                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1254                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1255                 sta->eapol_sm->authSuccess = TRUE;
1256                 sta->eapol_sm->authFail = FALSE;
1257                 if (sta->eapol_sm->eap)
1258                         eap_sm_notify_cached(sta->eapol_sm->eap);
1259                 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
1260                 ap_sta_bind_vlan(hapd, sta);
1261         } else {
1262                 if (reassoc) {
1263                         /*
1264                          * Force EAPOL state machines to start
1265                          * re-authentication without having to wait for the
1266                          * Supplicant to send EAPOL-Start.
1267                          */
1268                         sta->eapol_sm->reAuthenticate = TRUE;
1269                 }
1270                 eapol_auth_step(sta->eapol_sm);
1271         }
1272 }
1273
1274
1275 void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
1276 {
1277         struct eapol_state_machine *sm = sta->eapol_sm;
1278
1279 #ifdef CONFIG_HS20
1280         eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1281 #endif /* CONFIG_HS20 */
1282
1283         if (sta->pending_eapol_rx) {
1284                 wpabuf_free(sta->pending_eapol_rx->buf);
1285                 os_free(sta->pending_eapol_rx);
1286                 sta->pending_eapol_rx = NULL;
1287         }
1288
1289         if (sm == NULL)
1290                 return;
1291
1292         sta->eapol_sm = NULL;
1293
1294 #ifndef CONFIG_NO_RADIUS
1295         radius_msg_free(sm->last_recv_radius);
1296         radius_free_class(&sm->radius_class);
1297 #endif /* CONFIG_NO_RADIUS */
1298
1299         eapol_auth_free(sm);
1300 }
1301
1302
1303 #ifndef CONFIG_NO_RADIUS
1304 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1305                                           struct sta_info *sta)
1306 {
1307         struct wpabuf *eap;
1308         const struct eap_hdr *hdr;
1309         int eap_type = -1;
1310         char buf[64];
1311         struct radius_msg *msg;
1312         struct eapol_state_machine *sm = sta->eapol_sm;
1313
1314         if (sm == NULL || sm->last_recv_radius == NULL) {
1315                 if (sm)
1316                         sm->eap_if->aaaEapNoReq = TRUE;
1317                 return;
1318         }
1319
1320         msg = sm->last_recv_radius;
1321
1322         eap = radius_msg_get_eap(msg);
1323         if (eap == NULL) {
1324                 /* RFC 3579, Chap. 2.6.3:
1325                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1326                  * attribute */
1327                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1328                                HOSTAPD_LEVEL_WARNING, "could not extract "
1329                                "EAP-Message from RADIUS message");
1330                 sm->eap_if->aaaEapNoReq = TRUE;
1331                 return;
1332         }
1333
1334         if (wpabuf_len(eap) < sizeof(*hdr)) {
1335                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1336                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1337                                "received from authentication server");
1338                 wpabuf_free(eap);
1339                 sm->eap_if->aaaEapNoReq = TRUE;
1340                 return;
1341         }
1342
1343         if (wpabuf_len(eap) > sizeof(*hdr))
1344                 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1345
1346         hdr = wpabuf_head(eap);
1347         switch (hdr->code) {
1348         case EAP_CODE_REQUEST:
1349                 if (eap_type >= 0)
1350                         sm->eap_type_authsrv = eap_type;
1351                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1352                             eap_server_get_name(0, eap_type), eap_type);
1353                 break;
1354         case EAP_CODE_RESPONSE:
1355                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1356                             eap_server_get_name(0, eap_type), eap_type);
1357                 break;
1358         case EAP_CODE_SUCCESS:
1359                 os_strlcpy(buf, "EAP Success", sizeof(buf));
1360                 break;
1361         case EAP_CODE_FAILURE:
1362                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1363                 break;
1364         default:
1365                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1366                 break;
1367         }
1368         buf[sizeof(buf) - 1] = '\0';
1369         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1370                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1371                        "id=%d len=%d) from RADIUS server: %s",
1372                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
1373                        buf);
1374         sm->eap_if->aaaEapReq = TRUE;
1375
1376         wpabuf_free(sm->eap_if->aaaEapReqData);
1377         sm->eap_if->aaaEapReqData = eap;
1378 }
1379
1380
1381 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1382                                 struct sta_info *sta, struct radius_msg *msg,
1383                                 struct radius_msg *req,
1384                                 const u8 *shared_secret,
1385                                 size_t shared_secret_len)
1386 {
1387         struct radius_ms_mppe_keys *keys;
1388         struct eapol_state_machine *sm = sta->eapol_sm;
1389         if (sm == NULL)
1390                 return;
1391
1392         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1393                                       shared_secret_len);
1394
1395         if (keys && keys->send && keys->recv) {
1396                 size_t len = keys->send_len + keys->recv_len;
1397                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1398                                 keys->send, keys->send_len);
1399                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1400                                 keys->recv, keys->recv_len);
1401
1402                 os_free(sm->eap_if->aaaEapKeyData);
1403                 sm->eap_if->aaaEapKeyData = os_malloc(len);
1404                 if (sm->eap_if->aaaEapKeyData) {
1405                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1406                                   keys->recv_len);
1407                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1408                                   keys->send, keys->send_len);
1409                         sm->eap_if->aaaEapKeyDataLen = len;
1410                         sm->eap_if->aaaEapKeyAvailable = TRUE;
1411                 }
1412         } else {
1413                 wpa_printf(MSG_DEBUG,
1414                            "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
1415                            keys, keys ? keys->send : NULL,
1416                            keys ? keys->recv : NULL);
1417         }
1418
1419         if (keys) {
1420                 os_free(keys->send);
1421                 os_free(keys->recv);
1422                 os_free(keys);
1423         }
1424 }
1425
1426
1427 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1428                                           struct sta_info *sta,
1429                                           struct radius_msg *msg)
1430 {
1431         u8 *attr_class;
1432         size_t class_len;
1433         struct eapol_state_machine *sm = sta->eapol_sm;
1434         int count, i;
1435         struct radius_attr_data *nclass;
1436         size_t nclass_count;
1437
1438         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1439             sm == NULL)
1440                 return;
1441
1442         radius_free_class(&sm->radius_class);
1443         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1444         if (count <= 0)
1445                 return;
1446
1447         nclass = os_calloc(count, sizeof(struct radius_attr_data));
1448         if (nclass == NULL)
1449                 return;
1450
1451         nclass_count = 0;
1452
1453         attr_class = NULL;
1454         for (i = 0; i < count; i++) {
1455                 do {
1456                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1457                                                     &attr_class, &class_len,
1458                                                     attr_class) < 0) {
1459                                 i = count;
1460                                 break;
1461                         }
1462                 } while (class_len < 1);
1463
1464                 nclass[nclass_count].data = os_memdup(attr_class, class_len);
1465                 if (nclass[nclass_count].data == NULL)
1466                         break;
1467
1468                 nclass[nclass_count].len = class_len;
1469                 nclass_count++;
1470         }
1471
1472         sm->radius_class.attr = nclass;
1473         sm->radius_class.count = nclass_count;
1474         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1475                    "attributes for " MACSTR,
1476                    (unsigned long) sm->radius_class.count,
1477                    MAC2STR(sta->addr));
1478 }
1479
1480
1481 /* Update sta->identity based on User-Name attribute in Access-Accept */
1482 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1483                                            struct sta_info *sta,
1484                                            struct radius_msg *msg)
1485 {
1486         u8 *buf, *identity;
1487         size_t len;
1488         struct eapol_state_machine *sm = sta->eapol_sm;
1489
1490         if (sm == NULL)
1491                 return;
1492
1493         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1494                                     NULL) < 0)
1495                 return;
1496
1497         identity = (u8 *) dup_binstr(buf, len);
1498         if (identity == NULL)
1499                 return;
1500
1501         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1502                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1503                        "User-Name from Access-Accept '%s'",
1504                        sm->identity ? (char *) sm->identity : "N/A",
1505                        (char *) identity);
1506
1507         os_free(sm->identity);
1508         sm->identity = identity;
1509         sm->identity_len = len;
1510 }
1511
1512
1513 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1514 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1515                                       struct sta_info *sta,
1516                                       struct radius_msg *msg)
1517 {
1518         struct eapol_state_machine *sm = sta->eapol_sm;
1519         struct wpabuf *cui;
1520         u8 *buf;
1521         size_t len;
1522
1523         if (sm == NULL)
1524                 return;
1525
1526         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1527                                     &buf, &len, NULL) < 0)
1528                 return;
1529
1530         cui = wpabuf_alloc_copy(buf, len);
1531         if (cui == NULL)
1532                 return;
1533
1534         wpabuf_free(sm->radius_cui);
1535         sm->radius_cui = cui;
1536 }
1537
1538
1539 #ifdef CONFIG_HS20
1540
1541 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1542 {
1543         sta->remediation = 1;
1544         os_free(sta->remediation_url);
1545         if (len > 2) {
1546                 sta->remediation_url = os_malloc(len);
1547                 if (!sta->remediation_url)
1548                         return;
1549                 sta->remediation_method = pos[0];
1550                 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1551                 sta->remediation_url[len - 1] = '\0';
1552                 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1553                            "for " MACSTR " - server method %u URL %s",
1554                            MAC2STR(sta->addr), sta->remediation_method,
1555                            sta->remediation_url);
1556         } else {
1557                 sta->remediation_url = NULL;
1558                 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1559                            "for " MACSTR, MAC2STR(sta->addr));
1560         }
1561         /* TODO: assign the STA into remediation VLAN or add filtering */
1562 }
1563
1564
1565 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1566                                        struct sta_info *sta, u8 *pos,
1567                                        size_t len)
1568 {
1569         if (len < 3)
1570                 return; /* Malformed information */
1571         sta->hs20_deauth_requested = 1;
1572         wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u  "
1573                    "Re-auth Delay %u",
1574                    *pos, WPA_GET_LE16(pos + 1));
1575         wpabuf_free(sta->hs20_deauth_req);
1576         sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1577         if (sta->hs20_deauth_req) {
1578                 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1579                 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1580                 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1581         }
1582         ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1583 }
1584
1585
1586 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1587                                          struct sta_info *sta, u8 *pos,
1588                                          size_t len, int session_timeout)
1589 {
1590         unsigned int swt;
1591         int warning_time, beacon_int;
1592
1593         if (len < 1)
1594                 return; /* Malformed information */
1595         os_free(sta->hs20_session_info_url);
1596         sta->hs20_session_info_url = os_malloc(len);
1597         if (sta->hs20_session_info_url == NULL)
1598                 return;
1599         swt = pos[0];
1600         os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1601         sta->hs20_session_info_url[len - 1] = '\0';
1602         wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1603                    "(session_timeout=%d)",
1604                    sta->hs20_session_info_url, swt, session_timeout);
1605         if (session_timeout < 0) {
1606                 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1607                 return;
1608         }
1609         if (swt == 255)
1610                 swt = 1; /* Use one minute as the AP selected value */
1611
1612         if ((unsigned int) session_timeout < swt * 60)
1613                 warning_time = 0;
1614         else
1615                 warning_time = session_timeout - swt * 60;
1616
1617         beacon_int = hapd->iconf->beacon_int;
1618         if (beacon_int < 1)
1619                 beacon_int = 100; /* best guess */
1620         sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1621         if (sta->hs20_disassoc_timer > 65535)
1622                 sta->hs20_disassoc_timer = 65535;
1623
1624         ap_sta_session_warning_timeout(hapd, sta, warning_time);
1625 }
1626
1627
1628 static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
1629                                           struct sta_info *sta, u8 *pos,
1630                                           size_t len)
1631 {
1632         if (len < 4)
1633                 return; /* Malformed information */
1634         wpa_printf(MSG_DEBUG,
1635                    "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
1636                    pos[0], pos[1], pos[2], pos[3]);
1637         hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
1638 }
1639
1640
1641 static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
1642                                     struct sta_info *sta, u8 *pos, size_t len)
1643 {
1644         os_free(sta->t_c_url);
1645         sta->t_c_url = os_malloc(len + 1);
1646         if (!sta->t_c_url)
1647                 return;
1648         os_memcpy(sta->t_c_url, pos, len);
1649         sta->t_c_url[len] = '\0';
1650         wpa_printf(MSG_DEBUG,
1651                    "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
1652 }
1653
1654 #endif /* CONFIG_HS20 */
1655
1656
1657 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1658                                   struct sta_info *sta,
1659                                   struct radius_msg *msg,
1660                                   int session_timeout)
1661 {
1662 #ifdef CONFIG_HS20
1663         u8 *buf, *pos, *end, type, sublen;
1664         size_t len;
1665
1666         buf = NULL;
1667         sta->remediation = 0;
1668         sta->hs20_deauth_requested = 0;
1669
1670         for (;;) {
1671                 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1672                                             &buf, &len, buf) < 0)
1673                         break;
1674                 if (len < 6)
1675                         continue;
1676                 pos = buf;
1677                 end = buf + len;
1678                 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1679                         continue;
1680                 pos += 4;
1681
1682                 type = *pos++;
1683                 sublen = *pos++;
1684                 if (sublen < 2)
1685                         continue; /* invalid length */
1686                 sublen -= 2; /* skip header */
1687                 if (pos + sublen > end)
1688                         continue; /* invalid WFA VSA */
1689
1690                 switch (type) {
1691                 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1692                         ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1693                         break;
1694                 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1695                         ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1696                         break;
1697                 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1698                         ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1699                                                      session_timeout);
1700                         break;
1701                 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
1702                         ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
1703                         break;
1704                 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
1705                         ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
1706                         break;
1707                 }
1708         }
1709 #endif /* CONFIG_HS20 */
1710 }
1711
1712
1713 struct sta_id_search {
1714         u8 identifier;
1715         struct eapol_state_machine *sm;
1716 };
1717
1718
1719 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1720                                                struct sta_info *sta,
1721                                                void *ctx)
1722 {
1723         struct sta_id_search *id_search = ctx;
1724         struct eapol_state_machine *sm = sta->eapol_sm;
1725
1726         if (sm && sm->radius_identifier >= 0 &&
1727             sm->radius_identifier == id_search->identifier) {
1728                 id_search->sm = sm;
1729                 return 1;
1730         }
1731         return 0;
1732 }
1733
1734
1735 static struct eapol_state_machine *
1736 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1737 {
1738         struct sta_id_search id_search;
1739         id_search.identifier = identifier;
1740         id_search.sm = NULL;
1741         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1742         return id_search.sm;
1743 }
1744
1745
1746 /**
1747  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1748  * @msg: RADIUS response message
1749  * @req: RADIUS request message
1750  * @shared_secret: RADIUS shared secret
1751  * @shared_secret_len: Length of shared_secret in octets
1752  * @data: Context data (struct hostapd_data *)
1753  * Returns: Processing status
1754  */
1755 static RadiusRxResult
1756 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1757                         const u8 *shared_secret, size_t shared_secret_len,
1758                         void *data)
1759 {
1760         struct hostapd_data *hapd = data;
1761         struct sta_info *sta;
1762         u32 session_timeout = 0, termination_action, acct_interim_interval;
1763         int session_timeout_set;
1764         u32 reason_code;
1765         struct eapol_state_machine *sm;
1766         int override_eapReq = 0;
1767         struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1768         struct vlan_description vlan_desc;
1769 #ifndef CONFIG_NO_VLAN
1770         int *untagged, *tagged, *notempty;
1771 #endif /* CONFIG_NO_VLAN */
1772
1773         os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1774
1775         sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1776         if (sm == NULL) {
1777                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1778                            "station for this RADIUS message");
1779                 return RADIUS_RX_UNKNOWN;
1780         }
1781         sta = sm->sta;
1782
1783         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1784          * present when packet contains an EAP-Message attribute */
1785         if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1786             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1787                                 0) < 0 &&
1788             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1789                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1790                            "Message-Authenticator since it does not include "
1791                            "EAP-Message");
1792         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1793                                      req, 1)) {
1794                 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1795                 return RADIUS_RX_INVALID_AUTHENTICATOR;
1796         }
1797
1798         if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1799             hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1800             hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1801                 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1802                 return RADIUS_RX_UNKNOWN;
1803         }
1804
1805         sm->radius_identifier = -1;
1806         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1807                    MAC2STR(sta->addr));
1808
1809         radius_msg_free(sm->last_recv_radius);
1810         sm->last_recv_radius = msg;
1811
1812         session_timeout_set =
1813                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1814                                            &session_timeout);
1815         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1816                                       &termination_action))
1817                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1818
1819         if (hapd->conf->acct_interim_interval == 0 &&
1820             hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1821             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1822                                       &acct_interim_interval) == 0) {
1823                 if (acct_interim_interval < 60) {
1824                         hostapd_logger(hapd, sta->addr,
1825                                        HOSTAPD_MODULE_IEEE8021X,
1826                                        HOSTAPD_LEVEL_INFO,
1827                                        "ignored too small "
1828                                        "Acct-Interim-Interval %d",
1829                                        acct_interim_interval);
1830                 } else
1831                         sta->acct_interim_interval = acct_interim_interval;
1832         }
1833
1834
1835         switch (hdr->code) {
1836         case RADIUS_CODE_ACCESS_ACCEPT:
1837 #ifndef CONFIG_NO_VLAN
1838                 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1839                         notempty = &vlan_desc.notempty;
1840                         untagged = &vlan_desc.untagged;
1841                         tagged = vlan_desc.tagged;
1842                         *notempty = !!radius_msg_get_vlanid(msg, untagged,
1843                                                             MAX_NUM_TAGGED_VLAN,
1844                                                             tagged);
1845                 }
1846
1847                 if (vlan_desc.notempty &&
1848                     !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1849                         sta->eapol_sm->authFail = TRUE;
1850                         hostapd_logger(hapd, sta->addr,
1851                                        HOSTAPD_MODULE_RADIUS,
1852                                        HOSTAPD_LEVEL_INFO,
1853                                        "Invalid VLAN %d%s received from RADIUS server",
1854                                        vlan_desc.untagged,
1855                                        vlan_desc.tagged[0] ? "+" : "");
1856                         os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1857                         ap_sta_set_vlan(hapd, sta, &vlan_desc);
1858                         break;
1859                 }
1860
1861                 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1862                     !vlan_desc.notempty) {
1863                         sta->eapol_sm->authFail = TRUE;
1864                         hostapd_logger(hapd, sta->addr,
1865                                        HOSTAPD_MODULE_IEEE8021X,
1866                                        HOSTAPD_LEVEL_INFO, "authentication "
1867                                        "server did not include required VLAN "
1868                                        "ID in Access-Accept");
1869                         break;
1870                 }
1871 #endif /* CONFIG_NO_VLAN */
1872
1873                 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1874                         break;
1875
1876 #ifndef CONFIG_NO_VLAN
1877                 if (sta->vlan_id > 0) {
1878                         hostapd_logger(hapd, sta->addr,
1879                                        HOSTAPD_MODULE_RADIUS,
1880                                        HOSTAPD_LEVEL_INFO,
1881                                        "VLAN ID %d", sta->vlan_id);
1882                 }
1883 #endif /* CONFIG_NO_VLAN */
1884
1885                 if ((sta->flags & WLAN_STA_ASSOC) &&
1886                     ap_sta_bind_vlan(hapd, sta) < 0)
1887                         break;
1888
1889                 sta->session_timeout_set = !!session_timeout_set;
1890                 os_get_reltime(&sta->session_timeout);
1891                 sta->session_timeout.sec += session_timeout;
1892
1893                 /* RFC 3580, Ch. 3.17 */
1894                 if (session_timeout_set && termination_action ==
1895                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
1896                         sm->reAuthPeriod = session_timeout;
1897                 else if (session_timeout_set)
1898                         ap_sta_session_timeout(hapd, sta, session_timeout);
1899                 else
1900                         ap_sta_no_session_timeout(hapd, sta);
1901
1902                 sm->eap_if->aaaSuccess = TRUE;
1903                 override_eapReq = 1;
1904                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1905                                     shared_secret_len);
1906                 ieee802_1x_store_radius_class(hapd, sta, msg);
1907                 ieee802_1x_update_sta_identity(hapd, sta, msg);
1908                 ieee802_1x_update_sta_cui(hapd, sta, msg);
1909                 ieee802_1x_check_hs20(hapd, sta, msg,
1910                                       session_timeout_set ?
1911                                       (int) session_timeout : -1);
1912                 break;
1913         case RADIUS_CODE_ACCESS_REJECT:
1914                 sm->eap_if->aaaFail = TRUE;
1915                 override_eapReq = 1;
1916                 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
1917                                               &reason_code) == 0) {
1918                         wpa_printf(MSG_DEBUG,
1919                                    "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
1920                                    MACSTR, reason_code, MAC2STR(sta->addr));
1921                         sta->disconnect_reason_code = reason_code;
1922                 }
1923                 break;
1924         case RADIUS_CODE_ACCESS_CHALLENGE:
1925                 sm->eap_if->aaaEapReq = TRUE;
1926                 if (session_timeout_set) {
1927                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1928                         sm->eap_if->aaaMethodTimeout = session_timeout;
1929                         hostapd_logger(hapd, sm->addr,
1930                                        HOSTAPD_MODULE_IEEE8021X,
1931                                        HOSTAPD_LEVEL_DEBUG,
1932                                        "using EAP timeout of %d seconds (from "
1933                                        "RADIUS)",
1934                                        sm->eap_if->aaaMethodTimeout);
1935                 } else {
1936                         /*
1937                          * Use dynamic retransmission behavior per EAP
1938                          * specification.
1939                          */
1940                         sm->eap_if->aaaMethodTimeout = 0;
1941                 }
1942                 break;
1943         }
1944
1945         ieee802_1x_decapsulate_radius(hapd, sta);
1946         if (override_eapReq)
1947                 sm->eap_if->aaaEapReq = FALSE;
1948
1949 #ifdef CONFIG_FILS
1950 #ifdef NEED_AP_MLME
1951         if (sta->flags & WLAN_STA_PENDING_FILS_ERP) {
1952                 /* TODO: Add a PMKSA entry on success? */
1953                 ieee802_11_finish_fils_auth(
1954                         hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
1955                         sm->eap_if->aaaEapReqData,
1956                         sm->eap_if->aaaEapKeyData,
1957                         sm->eap_if->aaaEapKeyDataLen);
1958         }
1959 #endif /* NEED_AP_MLME */
1960 #endif /* CONFIG_FILS */
1961
1962         eapol_auth_step(sm);
1963
1964         return RADIUS_RX_QUEUED;
1965 }
1966 #endif /* CONFIG_NO_RADIUS */
1967
1968
1969 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1970 {
1971         struct eapol_state_machine *sm = sta->eapol_sm;
1972         if (sm == NULL)
1973                 return;
1974
1975         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1976                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1977
1978 #ifndef CONFIG_NO_RADIUS
1979         radius_msg_free(sm->last_recv_radius);
1980         sm->last_recv_radius = NULL;
1981 #endif /* CONFIG_NO_RADIUS */
1982
1983         if (sm->eap_if->eapTimeout) {
1984                 /*
1985                  * Disconnect the STA since it did not reply to the last EAP
1986                  * request and we cannot continue EAP processing (EAP-Failure
1987                  * could only be sent if the EAP peer actually replied).
1988                  */
1989                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1990                         MAC2STR(sta->addr));
1991
1992                 sm->eap_if->portEnabled = FALSE;
1993                 ap_sta_disconnect(hapd, sta, sta->addr,
1994                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
1995         }
1996 }
1997
1998
1999 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
2000 {
2001         struct eapol_authenticator *eapol = hapd->eapol_auth;
2002
2003         if (hapd->conf->default_wep_key_len < 1)
2004                 return 0;
2005
2006         os_free(eapol->default_wep_key);
2007         eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
2008         if (eapol->default_wep_key == NULL ||
2009             random_get_bytes(eapol->default_wep_key,
2010                              hapd->conf->default_wep_key_len)) {
2011                 wpa_printf(MSG_INFO, "Could not generate random WEP key");
2012                 os_free(eapol->default_wep_key);
2013                 eapol->default_wep_key = NULL;
2014                 return -1;
2015         }
2016
2017         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
2018                         eapol->default_wep_key,
2019                         hapd->conf->default_wep_key_len);
2020
2021         return 0;
2022 }
2023
2024
2025 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
2026                                         struct sta_info *sta, void *ctx)
2027 {
2028         if (sta->eapol_sm) {
2029                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
2030                 eapol_auth_step(sta->eapol_sm);
2031         }
2032         return 0;
2033 }
2034
2035
2036 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
2037 {
2038         struct hostapd_data *hapd = eloop_ctx;
2039         struct eapol_authenticator *eapol = hapd->eapol_auth;
2040
2041         if (eapol->default_wep_key_idx >= 3)
2042                 eapol->default_wep_key_idx =
2043                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
2044         else
2045                 eapol->default_wep_key_idx++;
2046
2047         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
2048                    eapol->default_wep_key_idx);
2049
2050         if (ieee802_1x_rekey_broadcast(hapd)) {
2051                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
2052                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
2053                                "new broadcast key");
2054                 os_free(eapol->default_wep_key);
2055                 eapol->default_wep_key = NULL;
2056                 return;
2057         }
2058
2059         /* TODO: Could setup key for RX here, but change default TX keyid only
2060          * after new broadcast key has been sent to all stations. */
2061         if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
2062                                 broadcast_ether_addr,
2063                                 eapol->default_wep_key_idx, 1, NULL, 0,
2064                                 eapol->default_wep_key,
2065                                 hapd->conf->default_wep_key_len)) {
2066                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
2067                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
2068                                "new broadcast key");
2069                 os_free(eapol->default_wep_key);
2070                 eapol->default_wep_key = NULL;
2071                 return;
2072         }
2073
2074         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
2075
2076         if (hapd->conf->wep_rekeying_period > 0) {
2077                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
2078                                        ieee802_1x_rekey, hapd, NULL);
2079         }
2080 }
2081
2082
2083 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2084                                   const u8 *data, size_t datalen)
2085 {
2086 #ifdef CONFIG_WPS
2087         struct sta_info *sta = sta_ctx;
2088
2089         if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2090             WLAN_STA_MAYBE_WPS) {
2091                 const u8 *identity;
2092                 size_t identity_len;
2093                 struct eapol_state_machine *sm = sta->eapol_sm;
2094
2095                 identity = eap_get_identity(sm->eap, &identity_len);
2096                 if (identity &&
2097                     ((identity_len == WSC_ID_ENROLLEE_LEN &&
2098                       os_memcmp(identity, WSC_ID_ENROLLEE,
2099                                 WSC_ID_ENROLLEE_LEN) == 0) ||
2100                      (identity_len == WSC_ID_REGISTRAR_LEN &&
2101                       os_memcmp(identity, WSC_ID_REGISTRAR,
2102                                 WSC_ID_REGISTRAR_LEN) == 0))) {
2103                         wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
2104                                    "WLAN_STA_WPS");
2105                         sta->flags |= WLAN_STA_WPS;
2106                 }
2107         }
2108 #endif /* CONFIG_WPS */
2109
2110         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2111 }
2112
2113
2114 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2115                                 const u8 *data, size_t datalen)
2116 {
2117 #ifndef CONFIG_NO_RADIUS
2118         struct hostapd_data *hapd = ctx;
2119         struct sta_info *sta = sta_ctx;
2120
2121         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2122 #endif /* CONFIG_NO_RADIUS */
2123 }
2124
2125
2126 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2127                                  int preauth, int remediation)
2128 {
2129         struct hostapd_data *hapd = ctx;
2130         struct sta_info *sta = sta_ctx;
2131         if (preauth)
2132                 rsn_preauth_finished(hapd, sta, success);
2133         else
2134                 ieee802_1x_finished(hapd, sta, success, remediation);
2135 }
2136
2137
2138 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2139                                    size_t identity_len, int phase2,
2140                                    struct eap_user *user)
2141 {
2142         struct hostapd_data *hapd = ctx;
2143         const struct hostapd_eap_user *eap_user;
2144         int i;
2145         int rv = -1;
2146
2147         eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
2148         if (eap_user == NULL)
2149                 goto out;
2150
2151         os_memset(user, 0, sizeof(*user));
2152         user->phase2 = phase2;
2153         for (i = 0; i < EAP_MAX_METHODS; i++) {
2154                 user->methods[i].vendor = eap_user->methods[i].vendor;
2155                 user->methods[i].method = eap_user->methods[i].method;
2156         }
2157
2158         if (eap_user->password) {
2159                 user->password = os_memdup(eap_user->password,
2160                                            eap_user->password_len);
2161                 if (user->password == NULL)
2162                         goto out;
2163                 user->password_len = eap_user->password_len;
2164                 user->password_hash = eap_user->password_hash;
2165                 if (eap_user->salt && eap_user->salt_len) {
2166                         user->salt = os_memdup(eap_user->salt,
2167                                                eap_user->salt_len);
2168                         if (!user->salt)
2169                                 goto out;
2170                         user->salt_len = eap_user->salt_len;
2171                 }
2172         }
2173         user->force_version = eap_user->force_version;
2174         user->macacl = eap_user->macacl;
2175         user->ttls_auth = eap_user->ttls_auth;
2176         user->remediation = eap_user->remediation;
2177         rv = 0;
2178
2179 out:
2180         if (rv)
2181                 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2182
2183         return rv;
2184 }
2185
2186
2187 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2188 {
2189         struct hostapd_data *hapd = ctx;
2190         struct sta_info *sta;
2191         sta = ap_get_sta(hapd, addr);
2192         if (sta == NULL || sta->eapol_sm == NULL)
2193                 return 0;
2194         return 1;
2195 }
2196
2197
2198 static void ieee802_1x_logger(void *ctx, const u8 *addr,
2199                               eapol_logger_level level, const char *txt)
2200 {
2201 #ifndef CONFIG_NO_HOSTAPD_LOGGER
2202         struct hostapd_data *hapd = ctx;
2203         int hlevel;
2204
2205         switch (level) {
2206         case EAPOL_LOGGER_WARNING:
2207                 hlevel = HOSTAPD_LEVEL_WARNING;
2208                 break;
2209         case EAPOL_LOGGER_INFO:
2210                 hlevel = HOSTAPD_LEVEL_INFO;
2211                 break;
2212         case EAPOL_LOGGER_DEBUG:
2213         default:
2214                 hlevel = HOSTAPD_LEVEL_DEBUG;
2215                 break;
2216         }
2217
2218         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2219                        txt);
2220 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2221 }
2222
2223
2224 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2225                                            int authorized)
2226 {
2227         struct hostapd_data *hapd = ctx;
2228         struct sta_info *sta = sta_ctx;
2229         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2230 }
2231
2232
2233 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2234 {
2235         struct hostapd_data *hapd = ctx;
2236         struct sta_info *sta = sta_ctx;
2237         ieee802_1x_abort_auth(hapd, sta);
2238 }
2239
2240
2241 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2242 {
2243 #ifndef CONFIG_FIPS
2244 #ifndef CONFIG_NO_RC4
2245         struct hostapd_data *hapd = ctx;
2246         struct sta_info *sta = sta_ctx;
2247         ieee802_1x_tx_key(hapd, sta);
2248 #endif /* CONFIG_NO_RC4 */
2249 #endif /* CONFIG_FIPS */
2250 }
2251
2252
2253 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2254                                    enum eapol_event type)
2255 {
2256         /* struct hostapd_data *hapd = ctx; */
2257         struct sta_info *sta = sta_ctx;
2258         switch (type) {
2259         case EAPOL_AUTH_SM_CHANGE:
2260                 wpa_auth_sm_notify(sta->wpa_sm);
2261                 break;
2262         case EAPOL_AUTH_REAUTHENTICATE:
2263                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2264                 break;
2265         }
2266 }
2267
2268
2269 #ifdef CONFIG_ERP
2270
2271 static struct eap_server_erp_key *
2272 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2273 {
2274         struct hostapd_data *hapd = ctx;
2275         struct eap_server_erp_key *erp;
2276
2277         dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2278                          list) {
2279                 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2280                         return erp;
2281         }
2282
2283         return NULL;
2284 }
2285
2286
2287 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2288 {
2289         struct hostapd_data *hapd = ctx;
2290
2291         dl_list_add(&hapd->erp_keys, &erp->list);
2292         return 0;
2293 }
2294
2295 #endif /* CONFIG_ERP */
2296
2297
2298 int ieee802_1x_init(struct hostapd_data *hapd)
2299 {
2300         int i;
2301         struct eapol_auth_config conf;
2302         struct eapol_auth_cb cb;
2303
2304         dl_list_init(&hapd->erp_keys);
2305
2306         os_memset(&conf, 0, sizeof(conf));
2307         conf.ctx = hapd;
2308         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2309         conf.wpa = hapd->conf->wpa;
2310         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2311         conf.eap_server = hapd->conf->eap_server;
2312         conf.ssl_ctx = hapd->ssl_ctx;
2313         conf.msg_ctx = hapd->msg_ctx;
2314         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2315         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2316         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2317         conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2318         conf.erp_domain = hapd->conf->erp_domain;
2319         conf.erp = hapd->conf->eap_server_erp;
2320         conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
2321         conf.tls_flags = hapd->conf->tls_flags;
2322         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2323         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2324         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2325         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2326         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2327         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2328         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2329         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2330         conf.tnc = hapd->conf->tnc;
2331         conf.wps = hapd->wps;
2332         conf.fragment_size = hapd->conf->fragment_size;
2333         conf.pwd_group = hapd->conf->pwd_group;
2334         conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2335         if (hapd->conf->server_id) {
2336                 conf.server_id = (const u8 *) hapd->conf->server_id;
2337                 conf.server_id_len = os_strlen(hapd->conf->server_id);
2338         } else {
2339                 conf.server_id = (const u8 *) "hostapd";
2340                 conf.server_id_len = 7;
2341         }
2342
2343         os_memset(&cb, 0, sizeof(cb));
2344         cb.eapol_send = ieee802_1x_eapol_send;
2345         cb.aaa_send = ieee802_1x_aaa_send;
2346         cb.finished = _ieee802_1x_finished;
2347         cb.get_eap_user = ieee802_1x_get_eap_user;
2348         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2349         cb.logger = ieee802_1x_logger;
2350         cb.set_port_authorized = ieee802_1x_set_port_authorized;
2351         cb.abort_auth = _ieee802_1x_abort_auth;
2352         cb.tx_key = _ieee802_1x_tx_key;
2353         cb.eapol_event = ieee802_1x_eapol_event;
2354 #ifdef CONFIG_ERP
2355         cb.erp_get_key = ieee802_1x_erp_get_key;
2356         cb.erp_add_key = ieee802_1x_erp_add_key;
2357 #endif /* CONFIG_ERP */
2358
2359         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2360         if (hapd->eapol_auth == NULL)
2361                 return -1;
2362
2363         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2364             hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2365                 return -1;
2366
2367 #ifndef CONFIG_NO_RADIUS
2368         if (radius_client_register(hapd->radius, RADIUS_AUTH,
2369                                    ieee802_1x_receive_auth, hapd))
2370                 return -1;
2371 #endif /* CONFIG_NO_RADIUS */
2372
2373         if (hapd->conf->default_wep_key_len) {
2374                 for (i = 0; i < 4; i++)
2375                         hostapd_drv_set_key(hapd->conf->iface, hapd,
2376                                             WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2377                                             NULL, 0);
2378
2379                 ieee802_1x_rekey(hapd, NULL);
2380
2381                 if (hapd->eapol_auth->default_wep_key == NULL)
2382                         return -1;
2383         }
2384
2385         return 0;
2386 }
2387
2388
2389 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2390 {
2391         struct eap_server_erp_key *erp;
2392
2393         while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2394                                     list)) != NULL) {
2395                 dl_list_del(&erp->list);
2396                 bin_clear_free(erp, sizeof(*erp));
2397         }
2398 }
2399
2400
2401 void ieee802_1x_deinit(struct hostapd_data *hapd)
2402 {
2403         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2404
2405         if (hapd->driver && hapd->drv_priv &&
2406             (hapd->conf->ieee802_1x || hapd->conf->wpa))
2407                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2408
2409         eapol_auth_deinit(hapd->eapol_auth);
2410         hapd->eapol_auth = NULL;
2411
2412         ieee802_1x_erp_flush(hapd);
2413 }
2414
2415
2416 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2417                          const u8 *buf, size_t len, int ack)
2418 {
2419         struct ieee80211_hdr *hdr;
2420         u8 *pos;
2421         const unsigned char rfc1042_hdr[ETH_ALEN] =
2422                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2423
2424         if (sta == NULL)
2425                 return -1;
2426         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2427                 return 0;
2428
2429         hdr = (struct ieee80211_hdr *) buf;
2430         pos = (u8 *) (hdr + 1);
2431         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2432                 return 0;
2433         pos += sizeof(rfc1042_hdr);
2434         if (WPA_GET_BE16(pos) != ETH_P_PAE)
2435                 return 0;
2436         pos += 2;
2437
2438         return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2439                                           ack);
2440 }
2441
2442
2443 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2444                                const u8 *buf, int len, int ack)
2445 {
2446         const struct ieee802_1x_hdr *xhdr =
2447                 (const struct ieee802_1x_hdr *) buf;
2448         const u8 *pos = buf + sizeof(*xhdr);
2449         struct ieee802_1x_eapol_key *key;
2450
2451         if (len < (int) sizeof(*xhdr))
2452                 return 0;
2453         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2454                    "type=%d length=%d - ack=%d",
2455                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
2456                    be_to_host16(xhdr->length), ack);
2457
2458 #ifdef CONFIG_WPS
2459         if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2460             (sta->flags & WLAN_STA_WPS) &&
2461             ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2462                 wpa_printf(MSG_DEBUG,
2463                            "WPS: Indicate EAP completion on ACK for EAP-Failure");
2464                 hostapd_wps_eap_completed(hapd);
2465         }
2466 #endif /* CONFIG_WPS */
2467
2468         if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2469                 return 0;
2470
2471         if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2472                 const struct wpa_eapol_key *wpa;
2473                 wpa = (const struct wpa_eapol_key *) pos;
2474                 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2475                     wpa->type == EAPOL_KEY_TYPE_WPA)
2476                         wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2477                                                      sta->wpa_sm, ack);
2478         }
2479
2480         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2481          * or Authenticator state machines, but EAPOL-Key packets are not
2482          * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2483          * packets couple of times because otherwise STA keys become
2484          * unsynchronized with AP. */
2485         if (!ack && pos + sizeof(*key) <= buf + len) {
2486                 key = (struct ieee802_1x_eapol_key *) pos;
2487                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2488                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2489                                "frame (%scast index=%d)",
2490                                key->key_index & BIT(7) ? "uni" : "broad",
2491                                key->key_index & ~BIT(7));
2492                 /* TODO: re-send EAPOL-Key couple of times (with short delay
2493                  * between them?). If all attempt fail, report error and
2494                  * deauthenticate STA so that it will get new keys when
2495                  * authenticating again (e.g., after returning in range).
2496                  * Separate limit/transmit state needed both for unicast and
2497                  * broadcast keys(?) */
2498         }
2499         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2500          * to here and change the key only if the EAPOL-Key packet was Acked.
2501          */
2502
2503         return 1;
2504 }
2505
2506
2507 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2508 {
2509         if (sm == NULL || sm->identity == NULL)
2510                 return NULL;
2511
2512         *len = sm->identity_len;
2513         return sm->identity;
2514 }
2515
2516
2517 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2518                                  int idx)
2519 {
2520         if (sm == NULL || sm->radius_class.attr == NULL ||
2521             idx >= (int) sm->radius_class.count)
2522                 return NULL;
2523
2524         *len = sm->radius_class.attr[idx].len;
2525         return sm->radius_class.attr[idx].data;
2526 }
2527
2528
2529 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2530 {
2531         if (sm == NULL)
2532                 return NULL;
2533         return sm->radius_cui;
2534 }
2535
2536
2537 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2538 {
2539         *len = 0;
2540         if (sm == NULL)
2541                 return NULL;
2542
2543         *len = sm->eap_if->eapKeyDataLen;
2544         return sm->eap_if->eapKeyData;
2545 }
2546
2547
2548 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2549                                     int enabled)
2550 {
2551         if (sm == NULL)
2552                 return;
2553         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2554         eapol_auth_step(sm);
2555 }
2556
2557
2558 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2559                                   int valid)
2560 {
2561         if (sm == NULL)
2562                 return;
2563         sm->portValid = valid ? TRUE : FALSE;
2564         eapol_auth_step(sm);
2565 }
2566
2567
2568 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2569 {
2570         if (sm == NULL)
2571                 return;
2572         if (pre_auth)
2573                 sm->flags |= EAPOL_SM_PREAUTH;
2574         else
2575                 sm->flags &= ~EAPOL_SM_PREAUTH;
2576 }
2577
2578
2579 static const char * bool_txt(Boolean val)
2580 {
2581         return val ? "TRUE" : "FALSE";
2582 }
2583
2584
2585 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2586 {
2587         /* TODO */
2588         return 0;
2589 }
2590
2591
2592 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2593                            char *buf, size_t buflen)
2594 {
2595         int len = 0, ret;
2596         struct eapol_state_machine *sm = sta->eapol_sm;
2597         struct os_reltime diff;
2598         const char *name1;
2599         const char *name2;
2600
2601         if (sm == NULL)
2602                 return 0;
2603
2604         ret = os_snprintf(buf + len, buflen - len,
2605                           "dot1xPaePortNumber=%d\n"
2606                           "dot1xPaePortProtocolVersion=%d\n"
2607                           "dot1xPaePortCapabilities=1\n"
2608                           "dot1xPaePortInitialize=%d\n"
2609                           "dot1xPaePortReauthenticate=FALSE\n",
2610                           sta->aid,
2611                           EAPOL_VERSION,
2612                           sm->initialize);
2613         if (os_snprintf_error(buflen - len, ret))
2614                 return len;
2615         len += ret;
2616
2617         /* dot1xAuthConfigTable */
2618         ret = os_snprintf(buf + len, buflen - len,
2619                           "dot1xAuthPaeState=%d\n"
2620                           "dot1xAuthBackendAuthState=%d\n"
2621                           "dot1xAuthAdminControlledDirections=%d\n"
2622                           "dot1xAuthOperControlledDirections=%d\n"
2623                           "dot1xAuthAuthControlledPortStatus=%d\n"
2624                           "dot1xAuthAuthControlledPortControl=%d\n"
2625                           "dot1xAuthQuietPeriod=%u\n"
2626                           "dot1xAuthServerTimeout=%u\n"
2627                           "dot1xAuthReAuthPeriod=%u\n"
2628                           "dot1xAuthReAuthEnabled=%s\n"
2629                           "dot1xAuthKeyTxEnabled=%s\n",
2630                           sm->auth_pae_state + 1,
2631                           sm->be_auth_state + 1,
2632                           sm->adminControlledDirections,
2633                           sm->operControlledDirections,
2634                           sm->authPortStatus,
2635                           sm->portControl,
2636                           sm->quietPeriod,
2637                           sm->serverTimeout,
2638                           sm->reAuthPeriod,
2639                           bool_txt(sm->reAuthEnabled),
2640                           bool_txt(sm->keyTxEnabled));
2641         if (os_snprintf_error(buflen - len, ret))
2642                 return len;
2643         len += ret;
2644
2645         /* dot1xAuthStatsTable */
2646         ret = os_snprintf(buf + len, buflen - len,
2647                           "dot1xAuthEapolFramesRx=%u\n"
2648                           "dot1xAuthEapolFramesTx=%u\n"
2649                           "dot1xAuthEapolStartFramesRx=%u\n"
2650                           "dot1xAuthEapolLogoffFramesRx=%u\n"
2651                           "dot1xAuthEapolRespIdFramesRx=%u\n"
2652                           "dot1xAuthEapolRespFramesRx=%u\n"
2653                           "dot1xAuthEapolReqIdFramesTx=%u\n"
2654                           "dot1xAuthEapolReqFramesTx=%u\n"
2655                           "dot1xAuthInvalidEapolFramesRx=%u\n"
2656                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
2657                           "dot1xAuthLastEapolFrameVersion=%u\n"
2658                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2659                           sm->dot1xAuthEapolFramesRx,
2660                           sm->dot1xAuthEapolFramesTx,
2661                           sm->dot1xAuthEapolStartFramesRx,
2662                           sm->dot1xAuthEapolLogoffFramesRx,
2663                           sm->dot1xAuthEapolRespIdFramesRx,
2664                           sm->dot1xAuthEapolRespFramesRx,
2665                           sm->dot1xAuthEapolReqIdFramesTx,
2666                           sm->dot1xAuthEapolReqFramesTx,
2667                           sm->dot1xAuthInvalidEapolFramesRx,
2668                           sm->dot1xAuthEapLengthErrorFramesRx,
2669                           sm->dot1xAuthLastEapolFrameVersion,
2670                           MAC2STR(sm->addr));
2671         if (os_snprintf_error(buflen - len, ret))
2672                 return len;
2673         len += ret;
2674
2675         /* dot1xAuthDiagTable */
2676         ret = os_snprintf(buf + len, buflen - len,
2677                           "dot1xAuthEntersConnecting=%u\n"
2678                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2679                           "dot1xAuthEntersAuthenticating=%u\n"
2680                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2681                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2682                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2683                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2684                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2685                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2686                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2687                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2688                           "dot1xAuthBackendResponses=%u\n"
2689                           "dot1xAuthBackendAccessChallenges=%u\n"
2690                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2691                           "dot1xAuthBackendAuthSuccesses=%u\n"
2692                           "dot1xAuthBackendAuthFails=%u\n",
2693                           sm->authEntersConnecting,
2694                           sm->authEapLogoffsWhileConnecting,
2695                           sm->authEntersAuthenticating,
2696                           sm->authAuthSuccessesWhileAuthenticating,
2697                           sm->authAuthTimeoutsWhileAuthenticating,
2698                           sm->authAuthFailWhileAuthenticating,
2699                           sm->authAuthEapStartsWhileAuthenticating,
2700                           sm->authAuthEapLogoffWhileAuthenticating,
2701                           sm->authAuthReauthsWhileAuthenticated,
2702                           sm->authAuthEapStartsWhileAuthenticated,
2703                           sm->authAuthEapLogoffWhileAuthenticated,
2704                           sm->backendResponses,
2705                           sm->backendAccessChallenges,
2706                           sm->backendOtherRequestsToSupplicant,
2707                           sm->backendAuthSuccesses,
2708                           sm->backendAuthFails);
2709         if (os_snprintf_error(buflen - len, ret))
2710                 return len;
2711         len += ret;
2712
2713         /* dot1xAuthSessionStatsTable */
2714         os_reltime_age(&sta->acct_session_start, &diff);
2715         ret = os_snprintf(buf + len, buflen - len,
2716                           /* TODO: dot1xAuthSessionOctetsRx */
2717                           /* TODO: dot1xAuthSessionOctetsTx */
2718                           /* TODO: dot1xAuthSessionFramesRx */
2719                           /* TODO: dot1xAuthSessionFramesTx */
2720                           "dot1xAuthSessionId=%016llX\n"
2721                           "dot1xAuthSessionAuthenticMethod=%d\n"
2722                           "dot1xAuthSessionTime=%u\n"
2723                           "dot1xAuthSessionTerminateCause=999\n"
2724                           "dot1xAuthSessionUserName=%s\n",
2725                           (unsigned long long) sta->acct_session_id,
2726                           (wpa_key_mgmt_wpa_ieee8021x(
2727                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2728                           1 : 2,
2729                           (unsigned int) diff.sec,
2730                           sm->identity);
2731         if (os_snprintf_error(buflen - len, ret))
2732                 return len;
2733         len += ret;
2734
2735         if (sm->acct_multi_session_id) {
2736                 ret = os_snprintf(buf + len, buflen - len,
2737                                   "authMultiSessionId=%016llX\n",
2738                                   (unsigned long long)
2739                                   sm->acct_multi_session_id);
2740                 if (os_snprintf_error(buflen - len, ret))
2741                         return len;
2742                 len += ret;
2743         }
2744
2745         name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2746         name2 = eap_server_get_name(0, sm->eap_type_supp);
2747         ret = os_snprintf(buf + len, buflen - len,
2748                           "last_eap_type_as=%d (%s)\n"
2749                           "last_eap_type_sta=%d (%s)\n",
2750                           sm->eap_type_authsrv, name1,
2751                           sm->eap_type_supp, name2);
2752         if (os_snprintf_error(buflen - len, ret))
2753                 return len;
2754         len += ret;
2755
2756         return len;
2757 }
2758
2759
2760 #ifdef CONFIG_HS20
2761 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2762 {
2763         struct hostapd_data *hapd = eloop_ctx;
2764         struct sta_info *sta = timeout_ctx;
2765
2766         if (sta->remediation) {
2767                 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2768                            MACSTR " to indicate Subscription Remediation",
2769                            MAC2STR(sta->addr));
2770                 hs20_send_wnm_notification(hapd, sta->addr,
2771                                            sta->remediation_method,
2772                                            sta->remediation_url);
2773                 os_free(sta->remediation_url);
2774                 sta->remediation_url = NULL;
2775         }
2776
2777         if (sta->hs20_deauth_req) {
2778                 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2779                            MACSTR " to indicate imminent deauthentication",
2780                            MAC2STR(sta->addr));
2781                 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2782                                                       sta->hs20_deauth_req);
2783         }
2784
2785         if (sta->hs20_t_c_filtering) {
2786                 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2787                            MACSTR " to indicate Terms and Conditions filtering",
2788                            MAC2STR(sta->addr));
2789                 hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
2790                 os_free(sta->t_c_url);
2791                 sta->t_c_url = NULL;
2792         }
2793 }
2794 #endif /* CONFIG_HS20 */
2795
2796
2797 static void ieee802_1x_finished(struct hostapd_data *hapd,
2798                                 struct sta_info *sta, int success,
2799                                 int remediation)
2800 {
2801         const u8 *key;
2802         size_t len;
2803         /* TODO: get PMKLifetime from WPA parameters */
2804         static const int dot11RSNAConfigPMKLifetime = 43200;
2805         unsigned int session_timeout;
2806         struct os_reltime now, remaining;
2807
2808 #ifdef CONFIG_HS20
2809         if (remediation && !sta->remediation) {
2810                 sta->remediation = 1;
2811                 os_free(sta->remediation_url);
2812                 sta->remediation_url =
2813                         os_strdup(hapd->conf->subscr_remediation_url);
2814                 sta->remediation_method = 1; /* SOAP-XML SPP */
2815         }
2816
2817         if (success && (sta->remediation || sta->hs20_deauth_req ||
2818                         sta->hs20_t_c_filtering)) {
2819                 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2820                            MACSTR " in 100 ms", MAC2STR(sta->addr));
2821                 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2822                 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2823                                        hapd, sta);
2824         }
2825 #endif /* CONFIG_HS20 */
2826
2827         key = ieee802_1x_get_key(sta->eapol_sm, &len);
2828         if (sta->session_timeout_set) {
2829                 os_get_reltime(&now);
2830                 os_reltime_sub(&sta->session_timeout, &now, &remaining);
2831                 session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
2832         } else {
2833                 session_timeout = dot11RSNAConfigPMKLifetime;
2834         }
2835         if (success && key && len >= PMK_LEN && !sta->remediation &&
2836             !sta->hs20_deauth_requested &&
2837             wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
2838                                sta->eapol_sm) == 0) {
2839                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2840                                HOSTAPD_LEVEL_DEBUG,
2841                                "Added PMKSA cache entry (IEEE 802.1X)");
2842         }
2843
2844         if (!success) {
2845                 /*
2846                  * Many devices require deauthentication after WPS provisioning
2847                  * and some may not be be able to do that themselves, so
2848                  * disconnect the client here. In addition, this may also
2849                  * benefit IEEE 802.1X/EAPOL authentication cases, too since
2850                  * the EAPOL PAE state machine would remain in HELD state for
2851                  * considerable amount of time and some EAP methods, like
2852                  * EAP-FAST with anonymous provisioning, may require another
2853                  * EAPOL authentication to be started to complete connection.
2854                  */
2855                 ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta);
2856         }
2857 }