2 * RSN pre-authentication (supplicant)
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "l2_packet/l2_packet.h"
15 #include "eapol_supp/eapol_supp_sm.h"
17 #include "pmksa_cache.h"
21 #ifdef IEEE8021X_EAPOL
23 #define PMKID_CANDIDATE_PRIO_SCAN 1000
26 struct rsn_pmksa_candidate {
34 * pmksa_candidate_free - Free all entries in PMKSA candidate list
35 * @sm: Pointer to WPA state machine data from wpa_sm_init()
37 void pmksa_candidate_free(struct wpa_sm *sm)
39 struct rsn_pmksa_candidate *entry, *n;
44 dl_list_for_each_safe(entry, n, &sm->pmksa_candidates,
45 struct rsn_pmksa_candidate, list) {
46 dl_list_del(&entry->list);
52 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
53 const u8 *buf, size_t len)
55 struct wpa_sm *sm = ctx;
57 wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr));
58 wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len);
60 if (sm->preauth_eapol == NULL ||
61 is_zero_ether_addr(sm->preauth_bssid) ||
62 os_memcmp(sm->preauth_bssid, src_addr, ETH_ALEN) != 0) {
63 wpa_printf(MSG_WARNING, "RSN pre-auth frame received from "
64 "unexpected source " MACSTR " - dropped",
69 eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len);
73 static void rsn_preauth_eapol_cb(struct eapol_sm *eapol,
74 enum eapol_supp_result result,
77 struct wpa_sm *sm = ctx;
80 if (result == EAPOL_SUPP_RESULT_SUCCESS) {
83 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
86 * EAP-LEAP is an exception from other EAP methods: it
87 * uses only 16-byte PMK.
89 res = eapol_sm_get_key(eapol, pmk, 16);
93 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth",
95 sm->pmk_len = pmk_len;
96 pmksa_cache_add(sm->pmksa, pmk, pmk_len,
97 sm->preauth_bssid, sm->own_addr,
99 WPA_KEY_MGMT_IEEE8021X);
101 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
102 "RSN: failed to get master session key from "
103 "pre-auth EAPOL state machines");
104 result = EAPOL_SUPP_RESULT_FAILURE;
108 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with "
109 MACSTR " %s", MAC2STR(sm->preauth_bssid),
110 result == EAPOL_SUPP_RESULT_SUCCESS ? "completed successfully" :
113 rsn_preauth_deinit(sm);
114 rsn_preauth_candidate_process(sm);
118 static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx)
120 struct wpa_sm *sm = eloop_ctx;
122 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with "
123 MACSTR " timed out", MAC2STR(sm->preauth_bssid));
124 rsn_preauth_deinit(sm);
125 rsn_preauth_candidate_process(sm);
129 static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf,
132 struct wpa_sm *sm = ctx;
137 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
140 if (sm->l2_preauth == NULL)
143 msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL);
147 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen);
148 res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid,
149 ETH_P_RSN_PREAUTH, msg, msglen);
156 * rsn_preauth_init - Start new RSN pre-authentication
157 * @sm: Pointer to WPA state machine data from wpa_sm_init()
158 * @dst: Authenticator address (BSSID) with which to preauthenticate
159 * @eap_conf: Current EAP configuration
160 * Returns: 0 on success, -1 on another pre-authentication is in progress,
161 * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine
162 * initialization failure, -4 on memory allocation failure
164 * This function request an RSN pre-authentication with a given destination
165 * address. This is usually called for PMKSA candidates found from scan results
166 * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger
167 * pre-authentication.
169 int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
170 struct eap_peer_config *eap_conf)
172 struct eapol_config eapol_conf;
173 struct eapol_ctx *ctx;
175 if (sm->preauth_eapol)
178 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
179 "RSN: starting pre-authentication with " MACSTR, MAC2STR(dst));
181 sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr,
183 rsn_preauth_receive, sm, 0);
184 if (sm->l2_preauth == NULL) {
185 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet "
186 "processing for pre-authentication");
190 if (sm->bridge_ifname) {
191 sm->l2_preauth_br = l2_packet_init(sm->bridge_ifname,
194 rsn_preauth_receive, sm, 0);
195 if (sm->l2_preauth_br == NULL) {
196 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 "
197 "packet processing (bridge) for "
198 "pre-authentication");
203 ctx = os_zalloc(sizeof(*ctx));
205 wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context.");
208 ctx->ctx = sm->ctx->ctx;
209 ctx->msg_ctx = sm->ctx->ctx;
211 ctx->cb = rsn_preauth_eapol_cb;
213 ctx->scard_ctx = sm->scard_ctx;
214 ctx->eapol_send = rsn_preauth_eapol_send;
215 ctx->eapol_send_ctx = sm;
216 ctx->set_config_blob = sm->ctx->set_config_blob;
217 ctx->get_config_blob = sm->ctx->get_config_blob;
219 sm->preauth_eapol = eapol_sm_init(ctx);
220 if (sm->preauth_eapol == NULL) {
222 wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL "
223 "state machines for pre-authentication");
226 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
227 eapol_conf.accept_802_1x_keys = 0;
228 eapol_conf.required_keys = 0;
229 eapol_conf.fast_reauth = sm->fast_reauth;
230 eapol_conf.workaround = sm->eap_workaround;
231 eapol_sm_notify_config(sm->preauth_eapol, eap_conf, &eapol_conf);
233 * Use a shorter startPeriod with preauthentication since the first
234 * preauth EAPOL-Start frame may end up being dropped due to race
235 * condition in the AP between the data receive and key configuration
236 * after the 4-Way Handshake.
238 eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6);
239 os_memcpy(sm->preauth_bssid, dst, ETH_ALEN);
241 eapol_sm_notify_portValid(sm->preauth_eapol, TRUE);
242 /* 802.1X::portControl = Auto */
243 eapol_sm_notify_portEnabled(sm->preauth_eapol, TRUE);
245 eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0,
246 rsn_preauth_timeout, sm, NULL);
253 * rsn_preauth_deinit - Abort RSN pre-authentication
254 * @sm: Pointer to WPA state machine data from wpa_sm_init()
256 * This function aborts the current RSN pre-authentication (if one is started)
257 * and frees resources allocated for it.
259 void rsn_preauth_deinit(struct wpa_sm *sm)
261 if (sm == NULL || !sm->preauth_eapol)
264 eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL);
265 eapol_sm_deinit(sm->preauth_eapol);
266 sm->preauth_eapol = NULL;
267 os_memset(sm->preauth_bssid, 0, ETH_ALEN);
269 l2_packet_deinit(sm->l2_preauth);
270 sm->l2_preauth = NULL;
271 if (sm->l2_preauth_br) {
272 l2_packet_deinit(sm->l2_preauth_br);
273 sm->l2_preauth_br = NULL;
279 * rsn_preauth_candidate_process - Process PMKSA candidates
280 * @sm: Pointer to WPA state machine data from wpa_sm_init()
282 * Go through the PMKSA candidates and start pre-authentication if a candidate
283 * without an existing PMKSA cache entry is found. Processed candidates will be
284 * removed from the list.
286 void rsn_preauth_candidate_process(struct wpa_sm *sm)
288 struct rsn_pmksa_candidate *candidate, *n;
290 if (dl_list_empty(&sm->pmksa_candidates))
293 /* TODO: drop priority for old candidate entries */
295 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: processing PMKSA candidate "
297 if (sm->preauth_eapol ||
298 sm->proto != WPA_PROTO_RSN ||
299 wpa_sm_get_state(sm) != WPA_COMPLETED ||
300 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
301 sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SHA256)) {
302 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: not in suitable "
303 "state for new pre-authentication");
304 return; /* invalid state for new pre-auth */
307 dl_list_for_each_safe(candidate, n, &sm->pmksa_candidates,
308 struct rsn_pmksa_candidate, list) {
309 struct rsn_pmksa_cache_entry *p = NULL;
310 p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL, NULL);
311 if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 &&
312 (p == NULL || p->opportunistic)) {
313 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA "
315 " selected for pre-authentication",
316 MAC2STR(candidate->bssid));
317 dl_list_del(&candidate->list);
318 rsn_preauth_init(sm, candidate->bssid,
323 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA candidate "
324 MACSTR " does not need pre-authentication anymore",
325 MAC2STR(candidate->bssid));
326 /* Some drivers (e.g., NDIS) expect to get notified about the
327 * PMKIDs again, so report the existing data now. */
329 wpa_sm_add_pmkid(sm, candidate->bssid, p->pmkid);
332 dl_list_del(&candidate->list);
335 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: no more pending PMKSA "
341 * pmksa_candidate_add - Add a new PMKSA candidate
342 * @sm: Pointer to WPA state machine data from wpa_sm_init()
343 * @bssid: BSSID (authenticator address) of the candidate
344 * @prio: Priority (the smaller number, the higher priority)
345 * @preauth: Whether the candidate AP advertises support for pre-authentication
347 * This function is used to add PMKSA candidates for RSN pre-authentication. It
348 * is called from scan result processing and from driver events for PMKSA
349 * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event().
351 void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid,
352 int prio, int preauth)
354 struct rsn_pmksa_candidate *cand, *pos;
356 if (sm->network_ctx && sm->proactive_key_caching)
357 pmksa_cache_get_opportunistic(sm->pmksa, sm->network_ctx,
361 wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without "
366 /* If BSSID already on candidate list, update the priority of the old
367 * entry. Do not override priority based on normal scan results. */
369 dl_list_for_each(pos, &sm->pmksa_candidates,
370 struct rsn_pmksa_candidate, list) {
371 if (os_memcmp(pos->bssid, bssid, ETH_ALEN) == 0) {
378 dl_list_del(&cand->list);
379 if (prio < PMKID_CANDIDATE_PRIO_SCAN)
380 cand->priority = prio;
382 cand = os_zalloc(sizeof(*cand));
385 os_memcpy(cand->bssid, bssid, ETH_ALEN);
386 cand->priority = prio;
389 /* Add candidate to the list; order by increasing priority value. i.e.,
390 * highest priority (smallest value) first. */
391 dl_list_for_each(pos, &sm->pmksa_candidates,
392 struct rsn_pmksa_candidate, list) {
393 if (cand->priority <= pos->priority) {
394 dl_list_add(pos->list.prev, &cand->list);
400 dl_list_add_tail(&sm->pmksa_candidates, &cand->list);
402 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: added PMKSA cache "
403 "candidate " MACSTR " prio %d", MAC2STR(bssid), prio);
404 rsn_preauth_candidate_process(sm);
408 /* TODO: schedule periodic scans if current AP supports preauth */
411 * rsn_preauth_scan_results - Start processing scan results for canditates
412 * @sm: Pointer to WPA state machine data from wpa_sm_init()
413 * Returns: 0 if ready to process results or -1 to skip processing
415 * This functions is used to notify RSN code about start of new scan results
416 * processing. The actual scan results will be provided by calling
417 * rsn_preauth_scan_result() for each BSS if this function returned 0.
419 int rsn_preauth_scan_results(struct wpa_sm *sm)
421 if (sm->ssid_len == 0)
425 * TODO: is it ok to free all candidates? What about the entries
426 * received from EVENT_PMKID_CANDIDATE?
428 pmksa_candidate_free(sm);
435 * rsn_preauth_scan_result - Processing scan result for PMKSA canditates
436 * @sm: Pointer to WPA state machine data from wpa_sm_init()
438 * Add all suitable APs (Authenticators) from scan results into PMKSA
441 void rsn_preauth_scan_result(struct wpa_sm *sm, const u8 *bssid,
442 const u8 *ssid, const u8 *rsn)
444 struct wpa_ie_data ie;
445 struct rsn_pmksa_cache_entry *pmksa;
447 if (ssid[1] != sm->ssid_len ||
448 os_memcmp(ssid + 2, sm->ssid, sm->ssid_len) != 0)
449 return; /* Not for the current SSID */
451 if (os_memcmp(bssid, sm->bssid, ETH_ALEN) == 0)
452 return; /* Ignore current AP */
454 if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie))
457 pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL, NULL);
458 if (pmksa && (!pmksa->opportunistic ||
459 !(ie.capabilities & WPA_CAPABILITY_PREAUTH)))
462 /* Give less priority to candidates found from normal scan results. */
463 pmksa_candidate_add(sm, bssid, PMKID_CANDIDATE_PRIO_SCAN,
464 ie.capabilities & WPA_CAPABILITY_PREAUTH);
468 #ifdef CONFIG_CTRL_IFACE
470 * rsn_preauth_get_status - Get pre-authentication status
471 * @sm: Pointer to WPA state machine data from wpa_sm_init()
472 * @buf: Buffer for status information
473 * @buflen: Maximum buffer length
474 * @verbose: Whether to include verbose status information
475 * Returns: Number of bytes written to buf.
477 * Query WPA2 pre-authentication for status information. This function fills in
478 * a text area with current status information. If the buffer (buf) is not
479 * large enough, status information will be truncated to fit the buffer.
481 int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
484 char *pos = buf, *end = buf + buflen;
487 if (sm->preauth_eapol) {
488 ret = os_snprintf(pos, end - pos, "Pre-authentication "
489 "EAPOL state machines:\n");
490 if (ret < 0 || ret >= end - pos)
493 res = eapol_sm_get_status(sm->preauth_eapol,
494 pos, end - pos, verbose);
501 #endif /* CONFIG_CTRL_IFACE */
505 * rsn_preauth_in_progress - Verify whether pre-authentication is in progress
506 * @sm: Pointer to WPA state machine data from wpa_sm_init()
508 int rsn_preauth_in_progress(struct wpa_sm *sm)
510 return sm->preauth_eapol != NULL;
513 #endif /* IEEE8021X_EAPOL */