2 * WPA Supplicant - RSN pre-authentication
3 * Copyright (c) 2003-2009, 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.
20 #include "l2_packet/l2_packet.h"
21 #include "eapol_supp/eapol_supp_sm.h"
23 #include "pmksa_cache.h"
25 #include "common/ieee802_11_defs.h"
28 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA2)
30 #define PMKID_CANDIDATE_PRIO_SCAN 1000
33 struct rsn_pmksa_candidate
{
34 struct rsn_pmksa_candidate
*next
;
41 * pmksa_candidate_free - Free all entries in PMKSA candidate list
42 * @sm: Pointer to WPA state machine data from wpa_sm_init()
44 void pmksa_candidate_free(struct wpa_sm
*sm
)
46 struct rsn_pmksa_candidate
*entry
, *prev
;
51 entry
= sm
->pmksa_candidates
;
52 sm
->pmksa_candidates
= NULL
;
61 static void rsn_preauth_receive(void *ctx
, const u8
*src_addr
,
62 const u8
*buf
, size_t len
)
64 struct wpa_sm
*sm
= ctx
;
66 wpa_printf(MSG_DEBUG
, "RX pre-auth from " MACSTR
, MAC2STR(src_addr
));
67 wpa_hexdump(MSG_MSGDUMP
, "RX pre-auth", buf
, len
);
69 if (sm
->preauth_eapol
== NULL
||
70 is_zero_ether_addr(sm
->preauth_bssid
) ||
71 os_memcmp(sm
->preauth_bssid
, src_addr
, ETH_ALEN
) != 0) {
72 wpa_printf(MSG_WARNING
, "RSN pre-auth frame received from "
73 "unexpected source " MACSTR
" - dropped",
78 eapol_sm_rx_eapol(sm
->preauth_eapol
, src_addr
, buf
, len
);
82 static void rsn_preauth_eapol_cb(struct eapol_sm
*eapol
, int success
,
85 struct wpa_sm
*sm
= ctx
;
91 res
= eapol_sm_get_key(eapol
, pmk
, PMK_LEN
);
94 * EAP-LEAP is an exception from other EAP methods: it
95 * uses only 16-byte PMK.
97 res
= eapol_sm_get_key(eapol
, pmk
, 16);
101 wpa_hexdump_key(MSG_DEBUG
, "RSN: PMK from pre-auth",
103 sm
->pmk_len
= pmk_len
;
104 pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
,
105 sm
->preauth_bssid
, sm
->own_addr
,
107 WPA_KEY_MGMT_IEEE8021X
);
109 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
110 "RSN: failed to get master session key from "
111 "pre-auth EAPOL state machines");
116 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "RSN: pre-authentication with "
117 MACSTR
" %s", MAC2STR(sm
->preauth_bssid
),
118 success
? "completed successfully" : "failed");
120 rsn_preauth_deinit(sm
);
121 rsn_preauth_candidate_process(sm
);
125 static void rsn_preauth_timeout(void *eloop_ctx
, void *timeout_ctx
)
127 struct wpa_sm
*sm
= eloop_ctx
;
129 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "RSN: pre-authentication with "
130 MACSTR
" timed out", MAC2STR(sm
->preauth_bssid
));
131 rsn_preauth_deinit(sm
);
132 rsn_preauth_candidate_process(sm
);
136 static int rsn_preauth_eapol_send(void *ctx
, int type
, const u8
*buf
,
139 struct wpa_sm
*sm
= ctx
;
144 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
147 if (sm
->l2_preauth
== NULL
)
150 msg
= wpa_sm_alloc_eapol(sm
, type
, buf
, len
, &msglen
, NULL
);
154 wpa_hexdump(MSG_MSGDUMP
, "TX EAPOL (preauth)", msg
, msglen
);
155 res
= l2_packet_send(sm
->l2_preauth
, sm
->preauth_bssid
,
156 ETH_P_RSN_PREAUTH
, msg
, msglen
);
163 * rsn_preauth_init - Start new RSN pre-authentication
164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
165 * @dst: Authenticator address (BSSID) with which to preauthenticate
166 * @eap_conf: Current EAP configuration
167 * Returns: 0 on success, -1 on another pre-authentication is in progress,
168 * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine
169 * initialization failure, -4 on memory allocation failure
171 * This function request an RSN pre-authentication with a given destination
172 * address. This is usually called for PMKSA candidates found from scan results
173 * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger
174 * pre-authentication.
176 int rsn_preauth_init(struct wpa_sm
*sm
, const u8
*dst
,
177 struct eap_peer_config
*eap_conf
)
179 struct eapol_config eapol_conf
;
180 struct eapol_ctx
*ctx
;
182 if (sm
->preauth_eapol
)
185 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
186 "RSN: starting pre-authentication with " MACSTR
, MAC2STR(dst
));
188 sm
->l2_preauth
= l2_packet_init(sm
->ifname
, sm
->own_addr
,
190 rsn_preauth_receive
, sm
, 0);
191 if (sm
->l2_preauth
== NULL
) {
192 wpa_printf(MSG_WARNING
, "RSN: Failed to initialize L2 packet "
193 "processing for pre-authentication");
197 if (sm
->bridge_ifname
) {
198 sm
->l2_preauth_br
= l2_packet_init(sm
->bridge_ifname
,
201 rsn_preauth_receive
, sm
, 0);
202 if (sm
->l2_preauth_br
== NULL
) {
203 wpa_printf(MSG_WARNING
, "RSN: Failed to initialize L2 "
204 "packet processing (bridge) for "
205 "pre-authentication");
210 ctx
= os_zalloc(sizeof(*ctx
));
212 wpa_printf(MSG_WARNING
, "Failed to allocate EAPOL context.");
215 ctx
->ctx
= sm
->ctx
->ctx
;
216 ctx
->msg_ctx
= sm
->ctx
->ctx
;
218 ctx
->cb
= rsn_preauth_eapol_cb
;
220 ctx
->scard_ctx
= sm
->scard_ctx
;
221 ctx
->eapol_send
= rsn_preauth_eapol_send
;
222 ctx
->eapol_send_ctx
= sm
;
223 ctx
->set_config_blob
= sm
->ctx
->set_config_blob
;
224 ctx
->get_config_blob
= sm
->ctx
->get_config_blob
;
226 sm
->preauth_eapol
= eapol_sm_init(ctx
);
227 if (sm
->preauth_eapol
== NULL
) {
229 wpa_printf(MSG_WARNING
, "RSN: Failed to initialize EAPOL "
230 "state machines for pre-authentication");
233 os_memset(&eapol_conf
, 0, sizeof(eapol_conf
));
234 eapol_conf
.accept_802_1x_keys
= 0;
235 eapol_conf
.required_keys
= 0;
236 eapol_conf
.fast_reauth
= sm
->fast_reauth
;
237 eapol_conf
.workaround
= sm
->eap_workaround
;
238 eapol_sm_notify_config(sm
->preauth_eapol
, eap_conf
, &eapol_conf
);
240 * Use a shorter startPeriod with preauthentication since the first
241 * preauth EAPOL-Start frame may end up being dropped due to race
242 * condition in the AP between the data receive and key configuration
243 * after the 4-Way Handshake.
245 eapol_sm_configure(sm
->preauth_eapol
, -1, -1, 5, 6);
246 os_memcpy(sm
->preauth_bssid
, dst
, ETH_ALEN
);
248 eapol_sm_notify_portValid(sm
->preauth_eapol
, TRUE
);
249 /* 802.1X::portControl = Auto */
250 eapol_sm_notify_portEnabled(sm
->preauth_eapol
, TRUE
);
252 eloop_register_timeout(sm
->dot11RSNAConfigSATimeout
, 0,
253 rsn_preauth_timeout
, sm
, NULL
);
260 * rsn_preauth_deinit - Abort RSN pre-authentication
261 * @sm: Pointer to WPA state machine data from wpa_sm_init()
263 * This function aborts the current RSN pre-authentication (if one is started)
264 * and frees resources allocated for it.
266 void rsn_preauth_deinit(struct wpa_sm
*sm
)
268 if (sm
== NULL
|| !sm
->preauth_eapol
)
271 eloop_cancel_timeout(rsn_preauth_timeout
, sm
, NULL
);
272 eapol_sm_deinit(sm
->preauth_eapol
);
273 sm
->preauth_eapol
= NULL
;
274 os_memset(sm
->preauth_bssid
, 0, ETH_ALEN
);
276 l2_packet_deinit(sm
->l2_preauth
);
277 sm
->l2_preauth
= NULL
;
278 if (sm
->l2_preauth_br
) {
279 l2_packet_deinit(sm
->l2_preauth_br
);
280 sm
->l2_preauth_br
= NULL
;
286 * rsn_preauth_candidate_process - Process PMKSA candidates
287 * @sm: Pointer to WPA state machine data from wpa_sm_init()
289 * Go through the PMKSA candidates and start pre-authentication if a candidate
290 * without an existing PMKSA cache entry is found. Processed candidates will be
291 * removed from the list.
293 void rsn_preauth_candidate_process(struct wpa_sm
*sm
)
295 struct rsn_pmksa_candidate
*candidate
;
297 if (sm
->pmksa_candidates
== NULL
)
300 /* TODO: drop priority for old candidate entries */
302 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: processing PMKSA candidate "
304 if (sm
->preauth_eapol
||
305 sm
->proto
!= WPA_PROTO_RSN
||
306 wpa_sm_get_state(sm
) != WPA_COMPLETED
||
307 (sm
->key_mgmt
!= WPA_KEY_MGMT_IEEE8021X
&&
308 sm
->key_mgmt
!= WPA_KEY_MGMT_IEEE8021X_SHA256
)) {
309 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: not in suitable "
310 "state for new pre-authentication");
311 return; /* invalid state for new pre-auth */
314 while (sm
->pmksa_candidates
) {
315 struct rsn_pmksa_cache_entry
*p
= NULL
;
316 candidate
= sm
->pmksa_candidates
;
317 p
= pmksa_cache_get(sm
->pmksa
, candidate
->bssid
, NULL
);
318 if (os_memcmp(sm
->bssid
, candidate
->bssid
, ETH_ALEN
) != 0 &&
319 (p
== NULL
|| p
->opportunistic
)) {
320 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA "
322 " selected for pre-authentication",
323 MAC2STR(candidate
->bssid
));
324 sm
->pmksa_candidates
= candidate
->next
;
325 rsn_preauth_init(sm
, candidate
->bssid
,
330 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA candidate "
331 MACSTR
" does not need pre-authentication anymore",
332 MAC2STR(candidate
->bssid
));
333 /* Some drivers (e.g., NDIS) expect to get notified about the
334 * PMKIDs again, so report the existing data now. */
336 wpa_sm_add_pmkid(sm
, candidate
->bssid
, p
->pmkid
);
339 sm
->pmksa_candidates
= candidate
->next
;
342 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: no more pending PMKSA "
348 * pmksa_candidate_add - Add a new PMKSA candidate
349 * @sm: Pointer to WPA state machine data from wpa_sm_init()
350 * @bssid: BSSID (authenticator address) of the candidate
351 * @prio: Priority (the smaller number, the higher priority)
352 * @preauth: Whether the candidate AP advertises support for pre-authentication
354 * This function is used to add PMKSA candidates for RSN pre-authentication. It
355 * is called from scan result processing and from driver events for PMKSA
356 * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event().
358 void pmksa_candidate_add(struct wpa_sm
*sm
, const u8
*bssid
,
359 int prio
, int preauth
)
361 struct rsn_pmksa_candidate
*cand
, *prev
, *pos
;
363 if (sm
->network_ctx
&& sm
->proactive_key_caching
)
364 pmksa_cache_get_opportunistic(sm
->pmksa
, sm
->network_ctx
,
368 wpa_printf(MSG_DEBUG
, "RSN: Ignored PMKID candidate without "
373 /* If BSSID already on candidate list, update the priority of the old
374 * entry. Do not override priority based on normal scan results. */
376 cand
= sm
->pmksa_candidates
;
378 if (os_memcmp(cand
->bssid
, bssid
, ETH_ALEN
) == 0) {
380 prev
->next
= cand
->next
;
382 sm
->pmksa_candidates
= cand
->next
;
390 if (prio
< PMKID_CANDIDATE_PRIO_SCAN
)
391 cand
->priority
= prio
;
393 cand
= os_zalloc(sizeof(*cand
));
396 os_memcpy(cand
->bssid
, bssid
, ETH_ALEN
);
397 cand
->priority
= prio
;
400 /* Add candidate to the list; order by increasing priority value. i.e.,
401 * highest priority (smallest value) first. */
403 pos
= sm
->pmksa_candidates
;
405 if (cand
->priority
<= pos
->priority
)
414 sm
->pmksa_candidates
= cand
;
416 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: added PMKSA cache "
417 "candidate " MACSTR
" prio %d", MAC2STR(bssid
), prio
);
418 rsn_preauth_candidate_process(sm
);
422 /* TODO: schedule periodic scans if current AP supports preauth */
425 * rsn_preauth_scan_results - Start processing scan results for canditates
426 * @sm: Pointer to WPA state machine data from wpa_sm_init()
427 * Returns: 0 if ready to process results or -1 to skip processing
429 * This functions is used to notify RSN code about start of new scan results
430 * processing. The actual scan results will be provided by calling
431 * rsn_preauth_scan_result() for each BSS if this function returned 0.
433 int rsn_preauth_scan_results(struct wpa_sm
*sm
)
435 if (sm
->ssid_len
== 0)
439 * TODO: is it ok to free all candidates? What about the entries
440 * received from EVENT_PMKID_CANDIDATE?
442 pmksa_candidate_free(sm
);
449 * rsn_preauth_scan_result - Processing scan result for PMKSA canditates
450 * @sm: Pointer to WPA state machine data from wpa_sm_init()
452 * Add all suitable APs (Authenticators) from scan results into PMKSA
455 void rsn_preauth_scan_result(struct wpa_sm
*sm
, const u8
*bssid
,
456 const u8
*ssid
, const u8
*rsn
)
458 struct wpa_ie_data ie
;
459 struct rsn_pmksa_cache_entry
*pmksa
;
461 if (ssid
[1] != sm
->ssid_len
||
462 os_memcmp(ssid
+ 2, sm
->ssid
, sm
->ssid_len
) != 0)
463 return; /* Not for the current SSID */
465 if (os_memcmp(bssid
, sm
->bssid
, ETH_ALEN
) == 0)
466 return; /* Ignore current AP */
468 if (wpa_parse_wpa_ie(rsn
, 2 + rsn
[1], &ie
))
471 pmksa
= pmksa_cache_get(sm
->pmksa
, bssid
, NULL
);
472 if (pmksa
&& (!pmksa
->opportunistic
||
473 !(ie
.capabilities
& WPA_CAPABILITY_PREAUTH
)))
476 /* Give less priority to candidates found from normal scan results. */
477 pmksa_candidate_add(sm
, bssid
, PMKID_CANDIDATE_PRIO_SCAN
,
478 ie
.capabilities
& WPA_CAPABILITY_PREAUTH
);
482 #ifdef CONFIG_CTRL_IFACE
484 * rsn_preauth_get_status - Get pre-authentication status
485 * @sm: Pointer to WPA state machine data from wpa_sm_init()
486 * @buf: Buffer for status information
487 * @buflen: Maximum buffer length
488 * @verbose: Whether to include verbose status information
489 * Returns: Number of bytes written to buf.
491 * Query WPA2 pre-authentication for status information. This function fills in
492 * a text area with current status information. If the buffer (buf) is not
493 * large enough, status information will be truncated to fit the buffer.
495 int rsn_preauth_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
498 char *pos
= buf
, *end
= buf
+ buflen
;
501 if (sm
->preauth_eapol
) {
502 ret
= os_snprintf(pos
, end
- pos
, "Pre-authentication "
503 "EAPOL state machines:\n");
504 if (ret
< 0 || ret
>= end
- pos
)
507 res
= eapol_sm_get_status(sm
->preauth_eapol
,
508 pos
, end
- pos
, verbose
);
515 #endif /* CONFIG_CTRL_IFACE */
519 * rsn_preauth_in_progress - Verify whether pre-authentication is in progress
520 * @sm: Pointer to WPA state machine data from wpa_sm_init()
522 int rsn_preauth_in_progress(struct wpa_sm
*sm
)
524 return sm
->preauth_eapol
!= NULL
;
527 #endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */