blob: 635691200c93c4adae922ca4e012b75c0e0d7835 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RSN pre-authentication (supplicant)
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "wpa.h"
13#include "eloop.h"
14#include "l2_packet/l2_packet.h"
15#include "eapol_supp/eapol_supp_sm.h"
16#include "preauth.h"
17#include "pmksa_cache.h"
18#include "wpa_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
20
Dmitry Shmidt34af3062013-07-11 10:46:32 -070021#ifdef IEEE8021X_EAPOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022
23#define PMKID_CANDIDATE_PRIO_SCAN 1000
24
25
26struct rsn_pmksa_candidate {
27 struct dl_list list;
28 u8 bssid[ETH_ALEN];
29 int priority;
30};
31
32
33/**
34 * pmksa_candidate_free - Free all entries in PMKSA candidate list
35 * @sm: Pointer to WPA state machine data from wpa_sm_init()
36 */
37void pmksa_candidate_free(struct wpa_sm *sm)
38{
39 struct rsn_pmksa_candidate *entry, *n;
40
41 if (sm == NULL)
42 return;
43
44 dl_list_for_each_safe(entry, n, &sm->pmksa_candidates,
45 struct rsn_pmksa_candidate, list) {
46 dl_list_del(&entry->list);
47 os_free(entry);
48 }
49}
50
51
52static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
53 const u8 *buf, size_t len)
54{
55 struct wpa_sm *sm = ctx;
56
57 wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr));
58 wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len);
59
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",
65 MAC2STR(src_addr));
66 return;
67 }
68
69 eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len);
70}
71
72
Dmitry Shmidt344abd32014-01-14 13:17:00 -080073static void rsn_preauth_eapol_cb(struct eapol_sm *eapol,
74 enum eapol_supp_result result,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 void *ctx)
76{
77 struct wpa_sm *sm = ctx;
78 u8 pmk[PMK_LEN];
79
Dmitry Shmidt344abd32014-01-14 13:17:00 -080080 if (result == EAPOL_SUPP_RESULT_SUCCESS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 int res, pmk_len;
82 pmk_len = PMK_LEN;
83 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
84 if (res) {
85 /*
86 * EAP-LEAP is an exception from other EAP methods: it
87 * uses only 16-byte PMK.
88 */
89 res = eapol_sm_get_key(eapol, pmk, 16);
90 pmk_len = 16;
91 }
92 if (res == 0) {
93 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth",
94 pmk, pmk_len);
95 sm->pmk_len = pmk_len;
96 pmksa_cache_add(sm->pmksa, pmk, pmk_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080097 NULL, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 sm->preauth_bssid, sm->own_addr,
99 sm->network_ctx,
100 WPA_KEY_MGMT_IEEE8021X);
101 } else {
102 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
103 "RSN: failed to get master session key from "
104 "pre-auth EAPOL state machines");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800105 result = EAPOL_SUPP_RESULT_FAILURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 }
107 }
108
109 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with "
110 MACSTR " %s", MAC2STR(sm->preauth_bssid),
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800111 result == EAPOL_SUPP_RESULT_SUCCESS ? "completed successfully" :
112 "failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113
114 rsn_preauth_deinit(sm);
115 rsn_preauth_candidate_process(sm);
116}
117
118
119static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx)
120{
121 struct wpa_sm *sm = eloop_ctx;
122
123 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with "
124 MACSTR " timed out", MAC2STR(sm->preauth_bssid));
125 rsn_preauth_deinit(sm);
126 rsn_preauth_candidate_process(sm);
127}
128
129
130static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf,
131 size_t len)
132{
133 struct wpa_sm *sm = ctx;
134 u8 *msg;
135 size_t msglen;
136 int res;
137
138 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
139 * extra copy here */
140
141 if (sm->l2_preauth == NULL)
142 return -1;
143
144 msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL);
145 if (msg == NULL)
146 return -1;
147
148 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen);
149 res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid,
150 ETH_P_RSN_PREAUTH, msg, msglen);
151 os_free(msg);
152 return res;
153}
154
155
156/**
157 * rsn_preauth_init - Start new RSN pre-authentication
158 * @sm: Pointer to WPA state machine data from wpa_sm_init()
159 * @dst: Authenticator address (BSSID) with which to preauthenticate
160 * @eap_conf: Current EAP configuration
161 * Returns: 0 on success, -1 on another pre-authentication is in progress,
162 * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine
163 * initialization failure, -4 on memory allocation failure
164 *
165 * This function request an RSN pre-authentication with a given destination
166 * address. This is usually called for PMKSA candidates found from scan results
167 * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger
168 * pre-authentication.
169 */
170int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst,
171 struct eap_peer_config *eap_conf)
172{
173 struct eapol_config eapol_conf;
174 struct eapol_ctx *ctx;
175
176 if (sm->preauth_eapol)
177 return -1;
178
179 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
180 "RSN: starting pre-authentication with " MACSTR, MAC2STR(dst));
181
182 sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr,
183 ETH_P_RSN_PREAUTH,
184 rsn_preauth_receive, sm, 0);
185 if (sm->l2_preauth == NULL) {
186 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet "
187 "processing for pre-authentication");
188 return -2;
189 }
190
191 if (sm->bridge_ifname) {
192 sm->l2_preauth_br = l2_packet_init(sm->bridge_ifname,
193 sm->own_addr,
194 ETH_P_RSN_PREAUTH,
195 rsn_preauth_receive, sm, 0);
196 if (sm->l2_preauth_br == NULL) {
197 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 "
198 "packet processing (bridge) for "
199 "pre-authentication");
200 return -2;
201 }
202 }
203
204 ctx = os_zalloc(sizeof(*ctx));
205 if (ctx == NULL) {
206 wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context.");
207 return -4;
208 }
209 ctx->ctx = sm->ctx->ctx;
210 ctx->msg_ctx = sm->ctx->ctx;
211 ctx->preauth = 1;
212 ctx->cb = rsn_preauth_eapol_cb;
213 ctx->cb_ctx = sm;
214 ctx->scard_ctx = sm->scard_ctx;
215 ctx->eapol_send = rsn_preauth_eapol_send;
216 ctx->eapol_send_ctx = sm;
217 ctx->set_config_blob = sm->ctx->set_config_blob;
218 ctx->get_config_blob = sm->ctx->get_config_blob;
219
220 sm->preauth_eapol = eapol_sm_init(ctx);
221 if (sm->preauth_eapol == NULL) {
222 os_free(ctx);
223 wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL "
224 "state machines for pre-authentication");
225 return -3;
226 }
227 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
228 eapol_conf.accept_802_1x_keys = 0;
229 eapol_conf.required_keys = 0;
230 eapol_conf.fast_reauth = sm->fast_reauth;
231 eapol_conf.workaround = sm->eap_workaround;
232 eapol_sm_notify_config(sm->preauth_eapol, eap_conf, &eapol_conf);
233 /*
234 * Use a shorter startPeriod with preauthentication since the first
235 * preauth EAPOL-Start frame may end up being dropped due to race
236 * condition in the AP between the data receive and key configuration
237 * after the 4-Way Handshake.
238 */
239 eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6);
240 os_memcpy(sm->preauth_bssid, dst, ETH_ALEN);
241
242 eapol_sm_notify_portValid(sm->preauth_eapol, TRUE);
243 /* 802.1X::portControl = Auto */
244 eapol_sm_notify_portEnabled(sm->preauth_eapol, TRUE);
245
246 eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0,
247 rsn_preauth_timeout, sm, NULL);
248
249 return 0;
250}
251
252
253/**
254 * rsn_preauth_deinit - Abort RSN pre-authentication
255 * @sm: Pointer to WPA state machine data from wpa_sm_init()
256 *
257 * This function aborts the current RSN pre-authentication (if one is started)
258 * and frees resources allocated for it.
259 */
260void rsn_preauth_deinit(struct wpa_sm *sm)
261{
262 if (sm == NULL || !sm->preauth_eapol)
263 return;
264
265 eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL);
266 eapol_sm_deinit(sm->preauth_eapol);
267 sm->preauth_eapol = NULL;
268 os_memset(sm->preauth_bssid, 0, ETH_ALEN);
269
270 l2_packet_deinit(sm->l2_preauth);
271 sm->l2_preauth = NULL;
272 if (sm->l2_preauth_br) {
273 l2_packet_deinit(sm->l2_preauth_br);
274 sm->l2_preauth_br = NULL;
275 }
276}
277
278
279/**
280 * rsn_preauth_candidate_process - Process PMKSA candidates
281 * @sm: Pointer to WPA state machine data from wpa_sm_init()
282 *
283 * Go through the PMKSA candidates and start pre-authentication if a candidate
284 * without an existing PMKSA cache entry is found. Processed candidates will be
285 * removed from the list.
286 */
287void rsn_preauth_candidate_process(struct wpa_sm *sm)
288{
289 struct rsn_pmksa_candidate *candidate, *n;
290
291 if (dl_list_empty(&sm->pmksa_candidates))
292 return;
293
294 /* TODO: drop priority for old candidate entries */
295
296 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: processing PMKSA candidate "
297 "list");
298 if (sm->preauth_eapol ||
299 sm->proto != WPA_PROTO_RSN ||
300 wpa_sm_get_state(sm) != WPA_COMPLETED ||
301 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800302 sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SHA256 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800303 sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SUITE_B &&
304 sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: not in suitable "
306 "state for new pre-authentication");
307 return; /* invalid state for new pre-auth */
308 }
309
310 dl_list_for_each_safe(candidate, n, &sm->pmksa_candidates,
311 struct rsn_pmksa_candidate, list) {
312 struct rsn_pmksa_cache_entry *p = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800313 p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 &&
315 (p == NULL || p->opportunistic)) {
316 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA "
317 "candidate " MACSTR
318 " selected for pre-authentication",
319 MAC2STR(candidate->bssid));
320 dl_list_del(&candidate->list);
321 rsn_preauth_init(sm, candidate->bssid,
322 sm->eap_conf_ctx);
323 os_free(candidate);
324 return;
325 }
326 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA candidate "
327 MACSTR " does not need pre-authentication anymore",
328 MAC2STR(candidate->bssid));
329 /* Some drivers (e.g., NDIS) expect to get notified about the
330 * PMKIDs again, so report the existing data now. */
331 if (p) {
332 wpa_sm_add_pmkid(sm, candidate->bssid, p->pmkid);
333 }
334
335 dl_list_del(&candidate->list);
336 os_free(candidate);
337 }
338 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: no more pending PMKSA "
339 "candidates");
340}
341
342
343/**
344 * pmksa_candidate_add - Add a new PMKSA candidate
345 * @sm: Pointer to WPA state machine data from wpa_sm_init()
346 * @bssid: BSSID (authenticator address) of the candidate
347 * @prio: Priority (the smaller number, the higher priority)
348 * @preauth: Whether the candidate AP advertises support for pre-authentication
349 *
350 * This function is used to add PMKSA candidates for RSN pre-authentication. It
351 * is called from scan result processing and from driver events for PMKSA
352 * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event().
353 */
354void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid,
355 int prio, int preauth)
356{
357 struct rsn_pmksa_candidate *cand, *pos;
358
359 if (sm->network_ctx && sm->proactive_key_caching)
360 pmksa_cache_get_opportunistic(sm->pmksa, sm->network_ctx,
361 bssid);
362
363 if (!preauth) {
364 wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without "
365 "preauth flag");
366 return;
367 }
368
369 /* If BSSID already on candidate list, update the priority of the old
370 * entry. Do not override priority based on normal scan results. */
371 cand = NULL;
372 dl_list_for_each(pos, &sm->pmksa_candidates,
373 struct rsn_pmksa_candidate, list) {
374 if (os_memcmp(pos->bssid, bssid, ETH_ALEN) == 0) {
375 cand = pos;
376 break;
377 }
378 }
379
380 if (cand) {
381 dl_list_del(&cand->list);
382 if (prio < PMKID_CANDIDATE_PRIO_SCAN)
383 cand->priority = prio;
384 } else {
385 cand = os_zalloc(sizeof(*cand));
386 if (cand == NULL)
387 return;
388 os_memcpy(cand->bssid, bssid, ETH_ALEN);
389 cand->priority = prio;
390 }
391
392 /* Add candidate to the list; order by increasing priority value. i.e.,
393 * highest priority (smallest value) first. */
394 dl_list_for_each(pos, &sm->pmksa_candidates,
395 struct rsn_pmksa_candidate, list) {
396 if (cand->priority <= pos->priority) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800397 if (!pos->list.prev) {
398 /*
399 * This cannot really happen in pracrice since
400 * pos was fetched from the list and the prev
401 * pointer must be set. It looks like clang
402 * static analyzer gets confused with the
403 * dl_list_del(&cand->list) call above and ends
404 * up assuming pos->list.prev could be NULL.
405 */
406 os_free(cand);
407 return;
408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 dl_list_add(pos->list.prev, &cand->list);
410 cand = NULL;
411 break;
412 }
413 }
414 if (cand)
415 dl_list_add_tail(&sm->pmksa_candidates, &cand->list);
416
417 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: added PMKSA cache "
418 "candidate " MACSTR " prio %d", MAC2STR(bssid), prio);
419 rsn_preauth_candidate_process(sm);
420}
421
422
423/* TODO: schedule periodic scans if current AP supports preauth */
424
425/**
426 * rsn_preauth_scan_results - Start processing scan results for canditates
427 * @sm: Pointer to WPA state machine data from wpa_sm_init()
428 * Returns: 0 if ready to process results or -1 to skip processing
429 *
430 * This functions is used to notify RSN code about start of new scan results
431 * processing. The actual scan results will be provided by calling
432 * rsn_preauth_scan_result() for each BSS if this function returned 0.
433 */
434int rsn_preauth_scan_results(struct wpa_sm *sm)
435{
436 if (sm->ssid_len == 0)
437 return -1;
438
439 /*
440 * TODO: is it ok to free all candidates? What about the entries
441 * received from EVENT_PMKID_CANDIDATE?
442 */
443 pmksa_candidate_free(sm);
444
445 return 0;
446}
447
448
449/**
450 * rsn_preauth_scan_result - Processing scan result for PMKSA canditates
451 * @sm: Pointer to WPA state machine data from wpa_sm_init()
452 *
453 * Add all suitable APs (Authenticators) from scan results into PMKSA
454 * candidate list.
455 */
456void rsn_preauth_scan_result(struct wpa_sm *sm, const u8 *bssid,
457 const u8 *ssid, const u8 *rsn)
458{
459 struct wpa_ie_data ie;
460 struct rsn_pmksa_cache_entry *pmksa;
461
462 if (ssid[1] != sm->ssid_len ||
463 os_memcmp(ssid + 2, sm->ssid, sm->ssid_len) != 0)
464 return; /* Not for the current SSID */
465
466 if (os_memcmp(bssid, sm->bssid, ETH_ALEN) == 0)
467 return; /* Ignore current AP */
468
469 if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie))
470 return;
471
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800472 pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 if (pmksa && (!pmksa->opportunistic ||
474 !(ie.capabilities & WPA_CAPABILITY_PREAUTH)))
475 return;
476
477 /* Give less priority to candidates found from normal scan results. */
478 pmksa_candidate_add(sm, bssid, PMKID_CANDIDATE_PRIO_SCAN,
479 ie.capabilities & WPA_CAPABILITY_PREAUTH);
480}
481
482
483#ifdef CONFIG_CTRL_IFACE
484/**
485 * rsn_preauth_get_status - Get pre-authentication status
486 * @sm: Pointer to WPA state machine data from wpa_sm_init()
487 * @buf: Buffer for status information
488 * @buflen: Maximum buffer length
489 * @verbose: Whether to include verbose status information
490 * Returns: Number of bytes written to buf.
491 *
492 * Query WPA2 pre-authentication for status information. This function fills in
493 * a text area with current status information. If the buffer (buf) is not
494 * large enough, status information will be truncated to fit the buffer.
495 */
496int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
497 int verbose)
498{
499 char *pos = buf, *end = buf + buflen;
500 int res, ret;
501
502 if (sm->preauth_eapol) {
503 ret = os_snprintf(pos, end - pos, "Pre-authentication "
504 "EAPOL state machines:\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800505 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 return pos - buf;
507 pos += ret;
508 res = eapol_sm_get_status(sm->preauth_eapol,
509 pos, end - pos, verbose);
510 if (res >= 0)
511 pos += res;
512 }
513
514 return pos - buf;
515}
516#endif /* CONFIG_CTRL_IFACE */
517
518
519/**
520 * rsn_preauth_in_progress - Verify whether pre-authentication is in progress
521 * @sm: Pointer to WPA state machine data from wpa_sm_init()
522 */
523int rsn_preauth_in_progress(struct wpa_sm *sm)
524{
525 return sm->preauth_eapol != NULL;
526}
527
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700528#endif /* IEEE8021X_EAPOL */