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