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