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