2 * WPA Supplicant - RSN pre-authentication
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "drivers/driver.h"
21 #include "l2_packet/l2_packet.h"
22 #include "eapol_supp/eapol_supp_sm.h"
24 #include "pmksa_cache.h"
26 #include "ieee802_11_defs.h"
29 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA2)
31 #define PMKID_CANDIDATE_PRIO_SCAN 1000
34 struct rsn_pmksa_candidate {
35 struct rsn_pmksa_candidate *next;
42 * pmksa_candidate_free - Free all entries in PMKSA candidate list
43 * @sm: Pointer to WPA state machine data from wpa_sm_init()
45 void pmksa_candidate_free(struct wpa_sm *sm)
47 struct rsn_pmksa_candidate *entry, *prev;
52 entry = sm->pmksa_candidates;
53 sm->pmksa_candidates = NULL;
62 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
63 const u8 *buf, size_t len)
65 struct wpa_sm *sm = ctx;
67 wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr));
68 wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len);
70 if (sm->preauth_eapol == NULL ||
71 is_zero_ether_addr(sm->preauth_bssid) ||
72 os_memcmp(sm->preauth_bssid, src_addr, ETH_ALEN) != 0) {
73 wpa_printf(MSG_WARNING, "RSN pre-auth frame received from "
74 "unexpected source " MACSTR " - dropped",
79 eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len);
83 static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, int success,
86 struct wpa_sm *sm = ctx;
92 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
95 * EAP-LEAP is an exception from other EAP methods: it
96 * uses only 16-byte PMK.
98 res = eapol_sm_get_key(eapol, pmk, 16);
102 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth",
104 sm->pmk_len = pmk_len;
105 pmksa_cache_add(sm->pmksa, pmk, pmk_len,
106 sm->preauth_bssid, sm->own_addr,
108 WPA_KEY_MGMT_IEEE8021X);
110 wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: failed to get "
111 "master session key from pre-auth EAPOL state "
117 wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: pre-authentication with " MACSTR
118 " %s", MAC2STR(sm->preauth_bssid),
119 success ? "completed successfully" : "failed");
121 rsn_preauth_deinit(sm);
122 rsn_preauth_candidate_process(sm);
126 static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx)
128 struct wpa_sm *sm = eloop_ctx;
130 wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: pre-authentication with " MACSTR
131 " timed out", MAC2STR(sm->preauth_bssid));
132 rsn_preauth_deinit(sm);
133 rsn_preauth_candidate_process(sm);
137 static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf,
140 struct wpa_sm *sm = ctx;
145 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
148 if (sm->l2_preauth == NULL)
151 msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL);
155 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen);
156 res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid,
157 ETH_P_RSN_PREAUTH, msg, msglen);
164 * rsn_preauth_init - Start new RSN pre-authentication
165 * @sm: Pointer to WPA state machine data from wpa_sm_init()
166 * @dst: Authenticator address (BSSID) with which to preauthenticate
167 * @eap_conf: Current EAP configuration
168 * Returns: 0 on success, -1 on another pre-authentication is in progress,
169 * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine
170 * initialization failure, -4 on memory allocation failure
172 * This function request an RSN pre-authentication with a given destination
173 * address. This is usually called for PMKSA candidates found from scan results
174 * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger
175 * pre-authentication.
177 int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
178 struct eap_peer_config *eap_conf)
180 struct eapol_config eapol_conf;
181 struct eapol_ctx *ctx;
183 if (sm->preauth_eapol)
186 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: starting pre-authentication "
187 "with " MACSTR, MAC2STR(dst));
189 sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr,
191 rsn_preauth_receive, sm, 0);
192 if (sm->l2_preauth == NULL) {
193 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet "
194 "processing for pre-authentication");
198 if (sm->bridge_ifname) {
199 sm->l2_preauth_br = l2_packet_init(sm->bridge_ifname,
202 rsn_preauth_receive, sm, 0);
203 if (sm->l2_preauth_br == NULL) {
204 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 "
205 "packet processing (bridge) for "
206 "pre-authentication");
211 ctx = os_zalloc(sizeof(*ctx));
213 wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context.");
216 ctx->ctx = sm->ctx->ctx;
217 ctx->msg_ctx = sm->ctx->ctx;
219 ctx->cb = rsn_preauth_eapol_cb;
221 ctx->scard_ctx = sm->scard_ctx;
222 ctx->eapol_send = rsn_preauth_eapol_send;
223 ctx->eapol_send_ctx = sm;
224 ctx->set_config_blob = sm->ctx->set_config_blob;
225 ctx->get_config_blob = sm->ctx->get_config_blob;
226 ctx->mac_addr = sm->own_addr;
228 sm->preauth_eapol = eapol_sm_init(ctx);
229 if (sm->preauth_eapol == NULL) {
231 wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL "
232 "state machines for pre-authentication");
235 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
236 eapol_conf.accept_802_1x_keys = 0;
237 eapol_conf.required_keys = 0;
238 eapol_conf.fast_reauth = sm->fast_reauth;
239 eapol_conf.workaround = sm->eap_workaround;
240 eapol_sm_notify_config(sm->preauth_eapol, eap_conf, &eapol_conf);
242 * Use a shorter startPeriod with preauthentication since the first
243 * preauth EAPOL-Start frame may end up being dropped due to race
244 * condition in the AP between the data receive and key configuration
245 * after the 4-Way Handshake.
247 eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6);
248 os_memcpy(sm->preauth_bssid, dst, ETH_ALEN);
250 eapol_sm_notify_portValid(sm->preauth_eapol, TRUE);
251 /* 802.1X::portControl = Auto */
252 eapol_sm_notify_portEnabled(sm->preauth_eapol, TRUE);
254 eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0,
255 rsn_preauth_timeout, sm, NULL);
262 * rsn_preauth_deinit - Abort RSN pre-authentication
263 * @sm: Pointer to WPA state machine data from wpa_sm_init()
265 * This function aborts the current RSN pre-authentication (if one is started)
266 * and frees resources allocated for it.
268 void rsn_preauth_deinit(struct wpa_sm *sm)
270 if (sm == NULL || !sm->preauth_eapol)
273 eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL);
274 eapol_sm_deinit(sm->preauth_eapol);
275 sm->preauth_eapol = NULL;
276 os_memset(sm->preauth_bssid, 0, ETH_ALEN);
278 l2_packet_deinit(sm->l2_preauth);
279 sm->l2_preauth = NULL;
280 if (sm->l2_preauth_br) {
281 l2_packet_deinit(sm->l2_preauth_br);
282 sm->l2_preauth_br = NULL;
288 * rsn_preauth_candidate_process - Process PMKSA candidates
289 * @sm: Pointer to WPA state machine data from wpa_sm_init()
291 * Go through the PMKSA candidates and start pre-authentication if a candidate
292 * without an existing PMKSA cache entry is found. Processed candidates will be
293 * removed from the list.
295 void rsn_preauth_candidate_process(struct wpa_sm *sm)
297 struct rsn_pmksa_candidate *candidate;
299 if (sm->pmksa_candidates == NULL)
302 /* TODO: drop priority for old candidate entries */
304 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: processing PMKSA candidate "
306 if (sm->preauth_eapol ||
307 sm->proto != WPA_PROTO_RSN ||
308 wpa_sm_get_state(sm) != WPA_COMPLETED ||
309 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
310 sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SHA256)) {
311 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: not in suitable state "
312 "for new pre-authentication");
313 return; /* invalid state for new pre-auth */
316 while (sm->pmksa_candidates) {
317 struct rsn_pmksa_cache_entry *p = NULL;
318 candidate = sm->pmksa_candidates;
319 p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL);
320 if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 &&
321 (p == NULL || p->opportunistic)) {
322 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: PMKSA "
324 " selected for pre-authentication",
325 MAC2STR(candidate->bssid));
326 sm->pmksa_candidates = candidate->next;
327 rsn_preauth_init(sm, candidate->bssid,
332 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: PMKSA candidate "
333 MACSTR " does not need pre-authentication anymore",
334 MAC2STR(candidate->bssid));
335 /* Some drivers (e.g., NDIS) expect to get notified about the
336 * PMKIDs again, so report the existing data now. */
338 wpa_sm_add_pmkid(sm, candidate->bssid, p->pmkid);
341 sm->pmksa_candidates = candidate->next;
344 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: no more pending PMKSA "
350 * pmksa_candidate_add - Add a new PMKSA candidate
351 * @sm: Pointer to WPA state machine data from wpa_sm_init()
352 * @bssid: BSSID (authenticator address) of the candidate
353 * @prio: Priority (the smaller number, the higher priority)
354 * @preauth: Whether the candidate AP advertises support for pre-authentication
356 * This function is used to add PMKSA candidates for RSN pre-authentication. It
357 * is called from scan result processing and from driver events for PMKSA
358 * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event().
360 void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid,
361 int prio, int preauth)
363 struct rsn_pmksa_candidate *cand, *prev, *pos;
365 if (sm->network_ctx && sm->proactive_key_caching)
366 pmksa_cache_get_opportunistic(sm->pmksa, sm->network_ctx,
370 wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without "
375 /* If BSSID already on candidate list, update the priority of the old
376 * entry. Do not override priority based on normal scan results. */
378 cand = sm->pmksa_candidates;
380 if (os_memcmp(cand->bssid, bssid, ETH_ALEN) == 0) {
382 prev->next = cand->next;
384 sm->pmksa_candidates = cand->next;
392 if (prio < PMKID_CANDIDATE_PRIO_SCAN)
393 cand->priority = prio;
395 cand = os_zalloc(sizeof(*cand));
398 os_memcpy(cand->bssid, bssid, ETH_ALEN);
399 cand->priority = prio;
402 /* Add candidate to the list; order by increasing priority value. i.e.,
403 * highest priority (smallest value) first. */
405 pos = sm->pmksa_candidates;
407 if (cand->priority <= pos->priority)
416 sm->pmksa_candidates = cand;
418 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: added PMKSA cache "
419 "candidate " MACSTR " prio %d", MAC2STR(bssid), prio);
420 rsn_preauth_candidate_process(sm);
424 /* TODO: schedule periodic scans if current AP supports preauth */
427 * rsn_preauth_scan_results - Process scan results to find PMKSA candidates
428 * @sm: Pointer to WPA state machine data from wpa_sm_init()
429 * @results: Scan results
431 * This functions goes through the scan results and adds all suitable APs
432 * (Authenticators) into PMKSA candidate list.
434 void rsn_preauth_scan_results(struct wpa_sm *sm,
435 struct wpa_scan_results *results)
437 struct wpa_scan_res *r;
438 struct wpa_ie_data ie;
440 struct rsn_pmksa_cache_entry *pmksa;
442 if (sm->ssid_len == 0)
446 * TODO: is it ok to free all candidates? What about the entries
447 * received from EVENT_PMKID_CANDIDATE?
449 pmksa_candidate_free(sm);
451 for (i = results->num - 1; i >= 0; i--) {
452 const u8 *ssid, *rsn;
456 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
457 if (ssid == NULL || ssid[1] != sm->ssid_len ||
458 os_memcmp(ssid + 2, sm->ssid, ssid[1]) != 0)
461 if (os_memcmp(r->bssid, sm->bssid, ETH_ALEN) == 0)
464 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
465 if (rsn == NULL || wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie))
468 pmksa = pmksa_cache_get(sm->pmksa, r->bssid, NULL);
470 (!pmksa->opportunistic ||
471 !(ie.capabilities & WPA_CAPABILITY_PREAUTH)))
475 * Give less priority to candidates found from normal
478 pmksa_candidate_add(sm, r->bssid,
479 PMKID_CANDIDATE_PRIO_SCAN,
480 ie.capabilities & WPA_CAPABILITY_PREAUTH);
485 #ifdef CONFIG_CTRL_IFACE
487 * rsn_preauth_get_status - Get pre-authentication status
488 * @sm: Pointer to WPA state machine data from wpa_sm_init()
489 * @buf: Buffer for status information
490 * @buflen: Maximum buffer length
491 * @verbose: Whether to include verbose status information
492 * Returns: Number of bytes written to buf.
494 * Query WPA2 pre-authentication for status information. This function fills in
495 * a text area with current status information. If the buffer (buf) is not
496 * large enough, status information will be truncated to fit the buffer.
498 int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
501 char *pos = buf, *end = buf + buflen;
504 if (sm->preauth_eapol) {
505 ret = os_snprintf(pos, end - pos, "Pre-authentication "
506 "EAPOL state machines:\n");
507 if (ret < 0 || ret >= end - pos)
510 res = eapol_sm_get_status(sm->preauth_eapol,
511 pos, end - pos, verbose);
518 #endif /* CONFIG_CTRL_IFACE */
522 * rsn_preauth_in_progress - Verify whether pre-authentication is in progress
523 * @sm: Pointer to WPA state machine data from wpa_sm_init()
525 int rsn_preauth_in_progress(struct wpa_sm *sm)
527 return sm->preauth_eapol != NULL;
530 #endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */