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