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