/src/hostap/src/rsn_supp/pmksa_cache.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * WPA Supplicant - RSN PMKSA cache |
3 | | * Copyright (c) 2004-2009, 2011-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 "eloop.h" |
13 | | #include "eapol_supp/eapol_supp_sm.h" |
14 | | #include "wpa.h" |
15 | | #include "wpa_i.h" |
16 | | #include "pmksa_cache.h" |
17 | | |
18 | | #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA) |
19 | | |
20 | | static const int pmksa_cache_max_entries = 32; |
21 | | |
22 | | struct rsn_pmksa_cache { |
23 | | struct rsn_pmksa_cache_entry *pmksa; /* PMKSA cache */ |
24 | | int pmksa_count; /* number of entries in PMKSA cache */ |
25 | | struct wpa_sm *sm; /* TODO: get rid of this reference(?) */ |
26 | | |
27 | | void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx, |
28 | | enum pmksa_free_reason reason); |
29 | | bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry, |
30 | | void *ctx); |
31 | | void (*notify_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx); |
32 | | void *ctx; |
33 | | }; |
34 | | |
35 | | |
36 | | static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa); |
37 | | |
38 | | |
39 | | static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry) |
40 | 0 | { |
41 | 0 | bin_clear_free(entry, sizeof(*entry)); |
42 | 0 | } |
43 | | |
44 | | |
45 | | static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa, |
46 | | struct rsn_pmksa_cache_entry *entry, |
47 | | enum pmksa_free_reason reason) |
48 | 0 | { |
49 | 0 | if (pmksa->sm) |
50 | 0 | wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, entry->aa, |
51 | 0 | entry->pmkid, |
52 | 0 | entry->fils_cache_id_set ? |
53 | 0 | entry->fils_cache_id : NULL); |
54 | 0 | pmksa->pmksa_count--; |
55 | 0 | if (pmksa->free_cb) |
56 | 0 | pmksa->free_cb(entry, pmksa->ctx, reason); |
57 | 0 | _pmksa_cache_free_entry(entry); |
58 | 0 | } |
59 | | |
60 | | |
61 | | void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa, |
62 | | struct rsn_pmksa_cache_entry *entry) |
63 | 0 | { |
64 | 0 | struct rsn_pmksa_cache_entry *e; |
65 | |
|
66 | 0 | e = pmksa->pmksa; |
67 | 0 | while (e) { |
68 | 0 | if (e == entry) { |
69 | 0 | pmksa->pmksa = entry->next; |
70 | 0 | break; |
71 | 0 | } |
72 | 0 | if (e->next == entry) { |
73 | 0 | e->next = entry->next; |
74 | 0 | break; |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | 0 | if (!e) { |
79 | 0 | wpa_printf(MSG_DEBUG, |
80 | 0 | "RSN: Could not remove PMKSA cache entry %p since it is not in the list", |
81 | 0 | entry); |
82 | 0 | return; |
83 | 0 | } |
84 | | |
85 | 0 | pmksa_cache_free_entry(pmksa, entry, PMKSA_FREE); |
86 | 0 | } |
87 | | |
88 | | |
89 | | static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx) |
90 | 0 | { |
91 | 0 | struct rsn_pmksa_cache *pmksa = eloop_ctx; |
92 | 0 | struct os_reltime now; |
93 | 0 | struct rsn_pmksa_cache_entry *prev = NULL, *tmp; |
94 | 0 | struct rsn_pmksa_cache_entry *entry = pmksa->pmksa; |
95 | |
|
96 | 0 | os_get_reltime(&now); |
97 | 0 | while (entry && entry->expiration <= now.sec) { |
98 | 0 | if (wpa_key_mgmt_sae(entry->akmp) && pmksa->is_current_cb && |
99 | 0 | pmksa->is_current_cb(entry, pmksa->ctx)) { |
100 | | /* Do not expire the currently used PMKSA entry for SAE |
101 | | * since there is no convenient mechanism for |
102 | | * reauthenticating during an association with SAE. The |
103 | | * expired entry will be removed after this association |
104 | | * has been lost. */ |
105 | 0 | wpa_printf(MSG_DEBUG, |
106 | 0 | "RSN: postpone PMKSA cache entry expiration for SAE with " |
107 | 0 | MACSTR, MAC2STR(entry->aa)); |
108 | 0 | prev = entry; |
109 | 0 | entry = entry->next; |
110 | 0 | continue; |
111 | 0 | } |
112 | | |
113 | 0 | wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for " |
114 | 0 | MACSTR, MAC2STR(entry->aa)); |
115 | 0 | if (prev) |
116 | 0 | prev->next = entry->next; |
117 | 0 | else |
118 | 0 | pmksa->pmksa = entry->next; |
119 | 0 | tmp = entry; |
120 | 0 | entry = entry->next; |
121 | 0 | pmksa_cache_free_entry(pmksa, tmp, PMKSA_EXPIRE); |
122 | 0 | } |
123 | |
|
124 | 0 | pmksa_cache_set_expiration(pmksa); |
125 | 0 | } |
126 | | |
127 | | |
128 | | static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx) |
129 | 0 | { |
130 | 0 | struct rsn_pmksa_cache *pmksa = eloop_ctx; |
131 | |
|
132 | 0 | if (!pmksa->sm) |
133 | 0 | return; |
134 | | |
135 | 0 | if (pmksa->sm->driver_bss_selection) { |
136 | 0 | struct rsn_pmksa_cache_entry *entry; |
137 | |
|
138 | 0 | entry = pmksa->sm->cur_pmksa ? |
139 | 0 | pmksa->sm->cur_pmksa : |
140 | 0 | pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, |
141 | 0 | NULL, 0); |
142 | 0 | if (entry && wpa_key_mgmt_sae(entry->akmp)) { |
143 | 0 | wpa_printf(MSG_DEBUG, |
144 | 0 | "RSN: remove reauth threshold passed PMKSA from the driver for SAE"); |
145 | 0 | entry->sae_reauth_scheduled = true; |
146 | 0 | wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, |
147 | 0 | entry->aa, entry->pmkid, NULL); |
148 | 0 | return; |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | 0 | pmksa->sm->cur_pmksa = NULL; |
153 | 0 | eapol_sm_request_reauth(pmksa->sm->eapol); |
154 | 0 | } |
155 | | |
156 | | |
157 | | static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa) |
158 | 1.96k | { |
159 | 1.96k | int sec; |
160 | 1.96k | struct rsn_pmksa_cache_entry *entry; |
161 | 1.96k | struct os_reltime now; |
162 | | |
163 | 1.96k | eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL); |
164 | 1.96k | eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL); |
165 | 1.96k | if (pmksa->pmksa == NULL) |
166 | 1.96k | return; |
167 | 0 | os_get_reltime(&now); |
168 | 0 | sec = pmksa->pmksa->expiration - now.sec; |
169 | 0 | if (sec < 0) { |
170 | 0 | sec = 0; |
171 | 0 | if (wpa_key_mgmt_sae(pmksa->pmksa->akmp) && |
172 | 0 | pmksa->is_current_cb && |
173 | 0 | pmksa->is_current_cb(pmksa->pmksa, pmksa->ctx)) { |
174 | | /* Do not continue polling for the current PMKSA entry |
175 | | * from SAE to expire every second. Use the expiration |
176 | | * time to the following entry, if any, and wait at |
177 | | * maximum 10 minutes to check again. |
178 | | */ |
179 | 0 | entry = pmksa->pmksa->next; |
180 | 0 | if (entry) { |
181 | 0 | sec = entry->expiration - now.sec; |
182 | 0 | if (sec < 0) |
183 | 0 | sec = 0; |
184 | 0 | else if (sec > 600) |
185 | 0 | sec = 600; |
186 | 0 | } else { |
187 | 0 | sec = 600; |
188 | 0 | } |
189 | 0 | } |
190 | 0 | } |
191 | 0 | eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL); |
192 | |
|
193 | 0 | if (!pmksa->sm) |
194 | 0 | return; |
195 | | |
196 | 0 | entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa : |
197 | 0 | pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, NULL, 0); |
198 | 0 | if (entry && |
199 | 0 | (!wpa_key_mgmt_sae(entry->akmp) || |
200 | 0 | (pmksa->sm->driver_bss_selection && |
201 | 0 | !entry->sae_reauth_scheduled))) { |
202 | 0 | sec = pmksa->pmksa->reauth_time - now.sec; |
203 | 0 | if (sec < 0) |
204 | 0 | sec = 0; |
205 | 0 | eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa, |
206 | 0 | NULL); |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | |
211 | | /** |
212 | | * pmksa_cache_add - Add a PMKSA cache entry |
213 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
214 | | * @pmk: The new pairwise master key |
215 | | * @pmk_len: PMK length in bytes, usually PMK_LEN (32) |
216 | | * @pmkid: Calculated PMKID |
217 | | * @kck: Key confirmation key or %NULL if not yet derived |
218 | | * @kck_len: KCK length in bytes |
219 | | * @aa: Authenticator address |
220 | | * @spa: Supplicant address |
221 | | * @network_ctx: Network configuration context for this PMK |
222 | | * @akmp: WPA_KEY_MGMT_* used in key derivation |
223 | | * @cache_id: Pointer to FILS Cache Identifier or %NULL if not advertised |
224 | | * Returns: Pointer to the added PMKSA cache entry or %NULL on error |
225 | | * |
226 | | * This function create a PMKSA entry for a new PMK and adds it to the PMKSA |
227 | | * cache. If an old entry is already in the cache for the same Authenticator, |
228 | | * this entry will be replaced with the new entry. PMKID will be calculated |
229 | | * based on the PMK and the driver interface is notified of the new PMKID. |
230 | | */ |
231 | | struct rsn_pmksa_cache_entry * |
232 | | pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len, |
233 | | const u8 *pmkid, const u8 *kck, size_t kck_len, |
234 | | const u8 *aa, const u8 *spa, void *network_ctx, int akmp, |
235 | | const u8 *cache_id) |
236 | 0 | { |
237 | 0 | struct rsn_pmksa_cache_entry *entry; |
238 | 0 | struct os_reltime now; |
239 | 0 | unsigned int pmk_lifetime = 43200; |
240 | 0 | unsigned int pmk_reauth_threshold = 70; |
241 | |
|
242 | 0 | if (pmk_len > PMK_LEN_MAX) |
243 | 0 | return NULL; |
244 | | |
245 | 0 | if (kck_len > WPA_KCK_MAX_LEN) |
246 | 0 | return NULL; |
247 | | |
248 | 0 | if (wpa_key_mgmt_suite_b(akmp) && !kck) |
249 | 0 | return NULL; |
250 | | |
251 | 0 | entry = os_zalloc(sizeof(*entry)); |
252 | 0 | if (entry == NULL) |
253 | 0 | return NULL; |
254 | 0 | os_memcpy(entry->pmk, pmk, pmk_len); |
255 | 0 | entry->pmk_len = pmk_len; |
256 | 0 | if (kck_len > 0) |
257 | 0 | os_memcpy(entry->kck, kck, kck_len); |
258 | 0 | entry->kck_len = kck_len; |
259 | 0 | if (pmkid) |
260 | 0 | os_memcpy(entry->pmkid, pmkid, PMKID_LEN); |
261 | 0 | else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) |
262 | 0 | rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid); |
263 | 0 | else if (wpa_key_mgmt_suite_b(akmp)) |
264 | 0 | rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid); |
265 | 0 | else |
266 | 0 | rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp); |
267 | 0 | os_get_reltime(&now); |
268 | 0 | if (pmksa->sm) { |
269 | 0 | pmk_lifetime = pmksa->sm->dot11RSNAConfigPMKLifetime; |
270 | 0 | pmk_reauth_threshold = |
271 | 0 | pmksa->sm->dot11RSNAConfigPMKReauthThreshold; |
272 | 0 | } |
273 | 0 | entry->expiration = now.sec + pmk_lifetime; |
274 | 0 | entry->reauth_time = now.sec + |
275 | 0 | pmk_lifetime * pmk_reauth_threshold / 100; |
276 | 0 | entry->akmp = akmp; |
277 | 0 | if (cache_id) { |
278 | 0 | entry->fils_cache_id_set = 1; |
279 | 0 | os_memcpy(entry->fils_cache_id, cache_id, FILS_CACHE_ID_LEN); |
280 | 0 | } |
281 | 0 | os_memcpy(entry->aa, aa, ETH_ALEN); |
282 | 0 | os_memcpy(entry->spa, spa, ETH_ALEN); |
283 | 0 | entry->network_ctx = network_ctx; |
284 | |
|
285 | 0 | return pmksa_cache_add_entry(pmksa, entry); |
286 | 0 | } |
287 | | |
288 | | |
289 | | struct rsn_pmksa_cache_entry * |
290 | | pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa, |
291 | | struct rsn_pmksa_cache_entry *entry) |
292 | 0 | { |
293 | 0 | struct rsn_pmksa_cache_entry *pos, *prev; |
294 | | |
295 | | /* Replace an old entry for the same Authenticator (if found) with the |
296 | | * new entry */ |
297 | 0 | pos = pmksa->pmksa; |
298 | 0 | prev = NULL; |
299 | 0 | while (pos) { |
300 | 0 | if (ether_addr_equal(entry->aa, pos->aa) && |
301 | 0 | ether_addr_equal(entry->spa, pos->spa)) { |
302 | 0 | if (pos->pmk_len == entry->pmk_len && |
303 | 0 | os_memcmp_const(pos->pmk, entry->pmk, |
304 | 0 | entry->pmk_len) == 0 && |
305 | 0 | os_memcmp_const(pos->pmkid, entry->pmkid, |
306 | 0 | PMKID_LEN) == 0) { |
307 | 0 | wpa_printf(MSG_DEBUG, "WPA: reusing previous " |
308 | 0 | "PMKSA entry"); |
309 | 0 | os_free(entry); |
310 | 0 | return pos; |
311 | 0 | } |
312 | 0 | if (prev == NULL) |
313 | 0 | pmksa->pmksa = pos->next; |
314 | 0 | else |
315 | 0 | prev->next = pos->next; |
316 | | |
317 | | /* |
318 | | * If OKC is used, there may be other PMKSA cache |
319 | | * entries based on the same PMK. These needs to be |
320 | | * flushed so that a new entry can be created based on |
321 | | * the new PMK. Only clear other entries if they have a |
322 | | * matching PMK and this PMK has been used successfully |
323 | | * with the current AP, i.e., if opportunistic flag has |
324 | | * been cleared in wpa_supplicant_key_neg_complete(). |
325 | | */ |
326 | 0 | wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for " |
327 | 0 | "the current AP and any PMKSA cache entry " |
328 | 0 | "that was based on the old PMK"); |
329 | 0 | if (!pos->opportunistic) |
330 | 0 | pmksa_cache_flush(pmksa, entry->network_ctx, |
331 | 0 | pos->pmk, pos->pmk_len, |
332 | 0 | false); |
333 | 0 | pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE); |
334 | 0 | break; |
335 | 0 | } |
336 | 0 | prev = pos; |
337 | 0 | pos = pos->next; |
338 | 0 | } |
339 | | |
340 | 0 | if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) { |
341 | | /* Remove the oldest entry to make room for the new entry */ |
342 | 0 | pos = pmksa->pmksa; |
343 | |
|
344 | 0 | if (pmksa->sm && pos == pmksa->sm->cur_pmksa) { |
345 | | /* |
346 | | * Never remove the current PMKSA cache entry, since |
347 | | * it's in use, and removing it triggers a needless |
348 | | * deauthentication. |
349 | | */ |
350 | 0 | pos = pos->next; |
351 | 0 | pmksa->pmksa->next = pos ? pos->next : NULL; |
352 | 0 | } else |
353 | 0 | pmksa->pmksa = pos->next; |
354 | |
|
355 | 0 | if (pos) { |
356 | 0 | wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle " |
357 | 0 | "PMKSA cache entry (for " MACSTR ") to " |
358 | 0 | "make room for new one", |
359 | 0 | MAC2STR(pos->aa)); |
360 | 0 | pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE); |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | | /* Add the new entry; order by expiration time */ |
365 | 0 | pos = pmksa->pmksa; |
366 | 0 | prev = NULL; |
367 | 0 | while (pos) { |
368 | 0 | if (pos->expiration > entry->expiration) |
369 | 0 | break; |
370 | 0 | prev = pos; |
371 | 0 | pos = pos->next; |
372 | 0 | } |
373 | 0 | if (prev == NULL) { |
374 | 0 | entry->next = pmksa->pmksa; |
375 | 0 | pmksa->pmksa = entry; |
376 | 0 | pmksa_cache_set_expiration(pmksa); |
377 | 0 | } else { |
378 | 0 | entry->next = prev->next; |
379 | 0 | prev->next = entry; |
380 | 0 | } |
381 | 0 | pmksa->pmksa_count++; |
382 | 0 | wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR |
383 | 0 | " spa=" MACSTR " network_ctx=%p akmp=0x%x", |
384 | 0 | MAC2STR(entry->aa), MAC2STR(entry->spa), |
385 | 0 | entry->network_ctx, entry->akmp); |
386 | |
|
387 | 0 | if (!pmksa->sm) |
388 | 0 | return entry; |
389 | | |
390 | 0 | if (pmksa->notify_cb) |
391 | 0 | pmksa->notify_cb(entry, pmksa->ctx); |
392 | |
|
393 | 0 | wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, entry->pmkid, |
394 | 0 | entry->fils_cache_id_set ? entry->fils_cache_id : NULL, |
395 | 0 | entry->pmk, entry->pmk_len, |
396 | 0 | pmksa->sm->dot11RSNAConfigPMKLifetime, |
397 | 0 | pmksa->sm->dot11RSNAConfigPMKReauthThreshold, |
398 | 0 | entry->akmp); |
399 | |
|
400 | 0 | return entry; |
401 | 0 | } |
402 | | |
403 | | |
404 | | /** |
405 | | * pmksa_cache_flush - Flush PMKSA cache entries for a specific network |
406 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
407 | | * @network_ctx: Network configuration context or %NULL to flush all entries |
408 | | * @pmk: PMK to match for or %NULL to match all PMKs |
409 | | * @pmk_len: PMK length |
410 | | * @external_only: Flush only PMKSA cache entries configured by external |
411 | | * applications |
412 | | */ |
413 | | void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx, |
414 | | const u8 *pmk, size_t pmk_len, bool external_only) |
415 | 0 | { |
416 | 0 | struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp; |
417 | 0 | int removed = 0; |
418 | |
|
419 | 0 | entry = pmksa->pmksa; |
420 | 0 | while (entry) { |
421 | 0 | if ((entry->network_ctx == network_ctx || |
422 | 0 | network_ctx == NULL) && |
423 | 0 | (pmk == NULL || |
424 | 0 | (pmk_len == entry->pmk_len && |
425 | 0 | os_memcmp(pmk, entry->pmk, pmk_len) == 0)) && |
426 | 0 | (!external_only || entry->external)) { |
427 | 0 | wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry " |
428 | 0 | "for " MACSTR, MAC2STR(entry->aa)); |
429 | 0 | if (prev) |
430 | 0 | prev->next = entry->next; |
431 | 0 | else |
432 | 0 | pmksa->pmksa = entry->next; |
433 | 0 | tmp = entry; |
434 | 0 | entry = entry->next; |
435 | 0 | pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE); |
436 | 0 | removed++; |
437 | 0 | } else { |
438 | 0 | prev = entry; |
439 | 0 | entry = entry->next; |
440 | 0 | } |
441 | 0 | } |
442 | 0 | if (removed) |
443 | 0 | pmksa_cache_set_expiration(pmksa); |
444 | 0 | } |
445 | | |
446 | | |
447 | | /** |
448 | | * pmksa_cache_deinit - Free all entries in PMKSA cache |
449 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
450 | | */ |
451 | | void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa) |
452 | 1.96k | { |
453 | 1.96k | struct rsn_pmksa_cache_entry *entry, *prev; |
454 | | |
455 | 1.96k | if (pmksa == NULL) |
456 | 0 | return; |
457 | | |
458 | 1.96k | entry = pmksa->pmksa; |
459 | 1.96k | pmksa->pmksa = NULL; |
460 | 1.96k | while (entry) { |
461 | 0 | prev = entry; |
462 | 0 | entry = entry->next; |
463 | 0 | os_free(prev); |
464 | 0 | } |
465 | 1.96k | pmksa_cache_set_expiration(pmksa); |
466 | 1.96k | os_free(pmksa); |
467 | 1.96k | } |
468 | | |
469 | | |
470 | | /** |
471 | | * pmksa_cache_get - Fetch a PMKSA cache entry |
472 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
473 | | * @aa: Authenticator address or %NULL to match any |
474 | | * @pmkid: PMKID or %NULL to match any |
475 | | * @network_ctx: Network context or %NULL to match any |
476 | | * @akmp: Specific AKMP to search for or 0 for any |
477 | | * Returns: Pointer to PMKSA cache entry or %NULL if no match was found |
478 | | */ |
479 | | struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa, |
480 | | const u8 *aa, const u8 *spa, |
481 | | const u8 *pmkid, |
482 | | const void *network_ctx, |
483 | | int akmp) |
484 | 8 | { |
485 | 8 | struct rsn_pmksa_cache_entry *entry = pmksa->pmksa; |
486 | 8 | while (entry) { |
487 | 0 | if ((aa == NULL || ether_addr_equal(entry->aa, aa)) && |
488 | 0 | (!spa || ether_addr_equal(entry->spa, spa)) && |
489 | 0 | (pmkid == NULL || |
490 | 0 | os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) && |
491 | 0 | (!akmp || akmp == entry->akmp) && |
492 | 0 | (network_ctx == NULL || network_ctx == entry->network_ctx)) |
493 | 0 | return entry; |
494 | 0 | entry = entry->next; |
495 | 0 | } |
496 | 8 | return NULL; |
497 | 8 | } |
498 | | |
499 | | |
500 | | static struct rsn_pmksa_cache_entry * |
501 | | pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa, |
502 | | const struct rsn_pmksa_cache_entry *old_entry, |
503 | | const u8 *aa) |
504 | 0 | { |
505 | 0 | struct rsn_pmksa_cache_entry *new_entry; |
506 | 0 | os_time_t old_expiration = old_entry->expiration; |
507 | 0 | os_time_t old_reauth_time = old_entry->reauth_time; |
508 | 0 | const u8 *pmkid = NULL; |
509 | |
|
510 | 0 | if (!pmksa->sm) |
511 | 0 | return NULL; |
512 | | |
513 | 0 | if (wpa_key_mgmt_sae(old_entry->akmp) || |
514 | 0 | wpa_key_mgmt_fils(old_entry->akmp)) |
515 | 0 | pmkid = old_entry->pmkid; |
516 | 0 | new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len, |
517 | 0 | pmkid, old_entry->kck, old_entry->kck_len, |
518 | 0 | aa, pmksa->sm->own_addr, |
519 | 0 | old_entry->network_ctx, old_entry->akmp, |
520 | 0 | old_entry->fils_cache_id_set ? |
521 | 0 | old_entry->fils_cache_id : NULL); |
522 | 0 | if (new_entry == NULL) |
523 | 0 | return NULL; |
524 | | |
525 | | /* TODO: reorder entries based on expiration time? */ |
526 | 0 | new_entry->expiration = old_expiration; |
527 | 0 | new_entry->reauth_time = old_reauth_time; |
528 | 0 | new_entry->opportunistic = 1; |
529 | |
|
530 | 0 | return new_entry; |
531 | 0 | } |
532 | | |
533 | | |
534 | | /** |
535 | | * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry |
536 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
537 | | * @network_ctx: Network configuration context |
538 | | * @aa: Authenticator address for the new AP |
539 | | * @akmp: Specific AKMP to search for or 0 for any |
540 | | * Returns: Pointer to a new PMKSA cache entry or %NULL if not available |
541 | | * |
542 | | * Try to create a new PMKSA cache entry opportunistically by guessing that the |
543 | | * new AP is sharing the same PMK as another AP that has the same SSID and has |
544 | | * already an entry in PMKSA cache. |
545 | | */ |
546 | | struct rsn_pmksa_cache_entry * |
547 | | pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx, |
548 | | const u8 *aa, int akmp) |
549 | 0 | { |
550 | 0 | struct rsn_pmksa_cache_entry *entry = pmksa->pmksa; |
551 | |
|
552 | 0 | wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa)); |
553 | 0 | if (network_ctx == NULL) |
554 | 0 | return NULL; |
555 | 0 | while (entry) { |
556 | 0 | if (entry->network_ctx == network_ctx && |
557 | 0 | (!akmp || entry->akmp == akmp)) { |
558 | 0 | struct os_reltime now; |
559 | |
|
560 | 0 | if (wpa_key_mgmt_sae(entry->akmp) && |
561 | 0 | os_get_reltime(&now) == 0 && |
562 | 0 | entry->reauth_time < now.sec) { |
563 | 0 | wpa_printf(MSG_DEBUG, |
564 | 0 | "RSN: Do not clone PMKSA cache entry for " |
565 | 0 | MACSTR |
566 | 0 | " since its reauth threshold has passed", |
567 | 0 | MAC2STR(entry->aa)); |
568 | 0 | entry = entry->next; |
569 | 0 | continue; |
570 | 0 | } |
571 | | |
572 | 0 | entry = pmksa_cache_clone_entry(pmksa, entry, aa); |
573 | 0 | if (entry) { |
574 | 0 | wpa_printf(MSG_DEBUG, "RSN: added " |
575 | 0 | "opportunistic PMKSA cache entry " |
576 | 0 | "for " MACSTR, MAC2STR(aa)); |
577 | 0 | } |
578 | 0 | return entry; |
579 | 0 | } |
580 | 0 | entry = entry->next; |
581 | 0 | } |
582 | 0 | return NULL; |
583 | 0 | } |
584 | | |
585 | | |
586 | | static struct rsn_pmksa_cache_entry * |
587 | | pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa, |
588 | | const void *network_ctx, const u8 *cache_id) |
589 | 0 | { |
590 | 0 | struct rsn_pmksa_cache_entry *entry; |
591 | |
|
592 | 0 | for (entry = pmksa->pmksa; entry; entry = entry->next) { |
593 | 0 | if (network_ctx == entry->network_ctx && |
594 | 0 | entry->fils_cache_id_set && |
595 | 0 | os_memcmp(cache_id, entry->fils_cache_id, |
596 | 0 | FILS_CACHE_ID_LEN) == 0) |
597 | 0 | return entry; |
598 | 0 | } |
599 | | |
600 | 0 | return NULL; |
601 | 0 | } |
602 | | |
603 | | |
604 | | /** |
605 | | * pmksa_cache_get_current - Get the current used PMKSA entry |
606 | | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
607 | | * Returns: Pointer to the current PMKSA cache entry or %NULL if not available |
608 | | */ |
609 | | struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm) |
610 | 0 | { |
611 | 0 | if (sm == NULL) |
612 | 0 | return NULL; |
613 | 0 | return sm->cur_pmksa; |
614 | 0 | } |
615 | | |
616 | | |
617 | | /** |
618 | | * pmksa_cache_clear_current - Clear the current PMKSA entry selection |
619 | | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
620 | | */ |
621 | | void pmksa_cache_clear_current(struct wpa_sm *sm) |
622 | 0 | { |
623 | 0 | if (sm == NULL) |
624 | 0 | return; |
625 | 0 | if (sm->cur_pmksa) |
626 | 0 | wpa_printf(MSG_DEBUG, |
627 | 0 | "RSN: Clear current PMKSA entry selection"); |
628 | 0 | sm->cur_pmksa = NULL; |
629 | 0 | } |
630 | | |
631 | | |
632 | | /** |
633 | | * pmksa_cache_set_current - Set the current PMKSA entry selection |
634 | | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
635 | | * @pmkid: PMKID for selecting PMKSA or %NULL if not used |
636 | | * @bssid: BSSID for PMKSA or %NULL if not used |
637 | | * @network_ctx: Network configuration context |
638 | | * @try_opportunistic: Whether to allow opportunistic PMKSA caching |
639 | | * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used |
640 | | * @associated: Whether the device is associated |
641 | | * Returns: 0 if PMKSA was found or -1 if no matching entry was found |
642 | | */ |
643 | | int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, |
644 | | const u8 *bssid, void *network_ctx, |
645 | | int try_opportunistic, const u8 *fils_cache_id, |
646 | | int akmp, bool associated) |
647 | 0 | { |
648 | 0 | struct rsn_pmksa_cache *pmksa = sm->pmksa; |
649 | 0 | wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p " |
650 | 0 | "try_opportunistic=%d akmp=0x%x", |
651 | 0 | network_ctx, try_opportunistic, akmp); |
652 | 0 | if (pmkid) |
653 | 0 | wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID", |
654 | 0 | pmkid, PMKID_LEN); |
655 | 0 | if (bssid) |
656 | 0 | wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR, |
657 | 0 | MAC2STR(bssid)); |
658 | 0 | if (fils_cache_id) |
659 | 0 | wpa_printf(MSG_DEBUG, |
660 | 0 | "RSN: Search for FILS Cache Identifier %02x%02x", |
661 | 0 | fils_cache_id[0], fils_cache_id[1]); |
662 | |
|
663 | 0 | sm->cur_pmksa = NULL; |
664 | 0 | if (pmkid) |
665 | 0 | sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, sm->own_addr, |
666 | 0 | pmkid, network_ctx, akmp); |
667 | 0 | if (sm->cur_pmksa == NULL && bssid) |
668 | 0 | sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, sm->own_addr, |
669 | 0 | NULL, network_ctx, akmp); |
670 | 0 | if (sm->cur_pmksa == NULL && try_opportunistic && bssid) |
671 | 0 | sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa, |
672 | 0 | network_ctx, |
673 | 0 | bssid, akmp); |
674 | 0 | if (sm->cur_pmksa == NULL && fils_cache_id) |
675 | 0 | sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa, |
676 | 0 | network_ctx, |
677 | 0 | fils_cache_id); |
678 | 0 | if (sm->cur_pmksa) { |
679 | 0 | struct os_reltime now; |
680 | |
|
681 | 0 | if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) && |
682 | 0 | os_get_reltime(&now) == 0 && |
683 | 0 | sm->cur_pmksa->reauth_time < now.sec) { |
684 | | /* Driver-based roaming might have used a PMKSA entry |
685 | | * that is already past the reauthentication threshold. |
686 | | * Remove the related PMKID from the driver to avoid |
687 | | * further uses for this PMKSA, but allow the |
688 | | * association to continue since the PMKSA has not yet |
689 | | * expired. */ |
690 | 0 | wpa_sm_remove_pmkid(sm, sm->cur_pmksa->network_ctx, |
691 | 0 | sm->cur_pmksa->aa, |
692 | 0 | sm->cur_pmksa->pmkid, NULL); |
693 | 0 | if (associated) { |
694 | 0 | wpa_printf(MSG_DEBUG, |
695 | 0 | "RSN: Associated with " MACSTR |
696 | 0 | " using reauth threshold passed PMKSA cache entry", |
697 | 0 | MAC2STR(sm->cur_pmksa->aa)); |
698 | 0 | } else { |
699 | 0 | wpa_printf(MSG_DEBUG, |
700 | 0 | "RSN: Do not allow PMKSA cache entry for " |
701 | 0 | MACSTR |
702 | 0 | " to be used for SAE since its reauth threshold has passed", |
703 | 0 | MAC2STR(sm->cur_pmksa->aa)); |
704 | 0 | sm->cur_pmksa = NULL; |
705 | 0 | return -1; |
706 | 0 | } |
707 | 0 | } |
708 | | |
709 | 0 | wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID", |
710 | 0 | sm->cur_pmksa->pmkid, PMKID_LEN); |
711 | 0 | return 0; |
712 | 0 | } |
713 | 0 | wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found"); |
714 | 0 | return -1; |
715 | 0 | } |
716 | | |
717 | | |
718 | | /** |
719 | | * pmksa_cache_list - Dump text list of entries in PMKSA cache |
720 | | * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() |
721 | | * @buf: Buffer for the list |
722 | | * @len: Length of the buffer |
723 | | * Returns: number of bytes written to buffer |
724 | | * |
725 | | * This function is used to generate a text format representation of the |
726 | | * current PMKSA cache contents for the ctrl_iface PMKSA command. |
727 | | */ |
728 | | int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len) |
729 | 0 | { |
730 | 0 | int i, ret; |
731 | 0 | char *pos = buf; |
732 | 0 | struct rsn_pmksa_cache_entry *entry; |
733 | 0 | struct os_reltime now; |
734 | 0 | int cache_id_used = 0; |
735 | |
|
736 | 0 | for (entry = pmksa->pmksa; entry; entry = entry->next) { |
737 | 0 | if (entry->fils_cache_id_set) { |
738 | 0 | cache_id_used = 1; |
739 | 0 | break; |
740 | 0 | } |
741 | 0 | } |
742 | |
|
743 | 0 | os_get_reltime(&now); |
744 | 0 | ret = os_snprintf(pos, buf + len - pos, |
745 | 0 | "Index / AA / PMKID / expiration (in seconds) / " |
746 | 0 | "opportunistic%s\n", |
747 | 0 | cache_id_used ? " / FILS Cache Identifier" : ""); |
748 | 0 | if (os_snprintf_error(buf + len - pos, ret)) |
749 | 0 | return pos - buf; |
750 | 0 | pos += ret; |
751 | 0 | i = 0; |
752 | 0 | entry = pmksa->pmksa; |
753 | 0 | while (entry) { |
754 | 0 | i++; |
755 | 0 | ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ", |
756 | 0 | i, MAC2STR(entry->aa)); |
757 | 0 | if (os_snprintf_error(buf + len - pos, ret)) |
758 | 0 | return pos - buf; |
759 | 0 | pos += ret; |
760 | 0 | pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid, |
761 | 0 | PMKID_LEN); |
762 | 0 | ret = os_snprintf(pos, buf + len - pos, " %d %d", |
763 | 0 | (int) (entry->expiration - now.sec), |
764 | 0 | entry->opportunistic); |
765 | 0 | if (os_snprintf_error(buf + len - pos, ret)) |
766 | 0 | return pos - buf; |
767 | 0 | pos += ret; |
768 | 0 | if (entry->fils_cache_id_set) { |
769 | 0 | ret = os_snprintf(pos, buf + len - pos, " %02x%02x", |
770 | 0 | entry->fils_cache_id[0], |
771 | 0 | entry->fils_cache_id[1]); |
772 | 0 | if (os_snprintf_error(buf + len - pos, ret)) |
773 | 0 | return pos - buf; |
774 | 0 | pos += ret; |
775 | 0 | } |
776 | 0 | ret = os_snprintf(pos, buf + len - pos, "\n"); |
777 | 0 | if (os_snprintf_error(buf + len - pos, ret)) |
778 | 0 | return pos - buf; |
779 | 0 | pos += ret; |
780 | 0 | entry = entry->next; |
781 | 0 | } |
782 | 0 | return pos - buf; |
783 | 0 | } |
784 | | |
785 | | |
786 | | struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa) |
787 | 0 | { |
788 | 0 | return pmksa->pmksa; |
789 | 0 | } |
790 | | |
791 | | |
792 | | /** |
793 | | * pmksa_cache_init - Initialize PMKSA cache |
794 | | * @free_cb: Callback function to be called when a PMKSA cache entry is freed |
795 | | * @ctx: Context pointer for free_cb function |
796 | | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
797 | | * Returns: Pointer to PMKSA cache data or %NULL on failure |
798 | | */ |
799 | | struct rsn_pmksa_cache * |
800 | | pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry, |
801 | | void *ctx, enum pmksa_free_reason reason), |
802 | | bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry, |
803 | | void *ctx), |
804 | | void (*notify_cb)(struct rsn_pmksa_cache_entry *entry, |
805 | | void *ctx), |
806 | | void *ctx, struct wpa_sm *sm) |
807 | 1.96k | { |
808 | 1.96k | struct rsn_pmksa_cache *pmksa; |
809 | | |
810 | 1.96k | pmksa = os_zalloc(sizeof(*pmksa)); |
811 | 1.96k | if (pmksa) { |
812 | 1.96k | pmksa->free_cb = free_cb; |
813 | 1.96k | pmksa->is_current_cb = is_current_cb; |
814 | 1.96k | pmksa->notify_cb = notify_cb; |
815 | 1.96k | pmksa->ctx = ctx; |
816 | 1.96k | pmksa->sm = sm; |
817 | 1.96k | } |
818 | | |
819 | 1.96k | return pmksa; |
820 | 1.96k | } |
821 | | |
822 | | |
823 | | void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa) |
824 | 0 | { |
825 | 0 | struct rsn_pmksa_cache_entry *entry; |
826 | 0 | struct os_reltime now; |
827 | |
|
828 | 0 | if (!pmksa || !pmksa->pmksa) |
829 | 0 | return; |
830 | | |
831 | 0 | os_get_reltime(&now); |
832 | 0 | for (entry = pmksa->pmksa; entry; entry = entry->next) { |
833 | 0 | u32 life_time; |
834 | 0 | u8 reauth_threshold; |
835 | |
|
836 | 0 | if (entry->expiration - now.sec < 1 || |
837 | 0 | entry->reauth_time - now.sec < 1) |
838 | 0 | continue; |
839 | | |
840 | 0 | life_time = entry->expiration - now.sec; |
841 | 0 | reauth_threshold = (entry->reauth_time - now.sec) * 100 / |
842 | 0 | life_time; |
843 | 0 | if (!reauth_threshold) |
844 | 0 | continue; |
845 | | |
846 | 0 | wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, |
847 | 0 | entry->pmkid, |
848 | 0 | entry->fils_cache_id_set ? |
849 | 0 | entry->fils_cache_id : NULL, |
850 | 0 | entry->pmk, entry->pmk_len, life_time, |
851 | 0 | reauth_threshold, entry->akmp); |
852 | 0 | } |
853 | 0 | } |
854 | | |
855 | | #else /* IEEE8021X_EAPOL */ |
856 | | |
857 | | struct rsn_pmksa_cache * |
858 | | pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry, |
859 | | void *ctx, enum pmksa_free_reason reason), |
860 | | bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry, |
861 | | void *ctx), |
862 | | void (*notify_cb)(struct rsn_pmksa_cache_entry *entry, |
863 | | void *ctx), |
864 | | void *ctx, struct wpa_sm *sm) |
865 | | { |
866 | | return (void *) -1; |
867 | | } |
868 | | |
869 | | |
870 | | void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa) |
871 | | { |
872 | | } |
873 | | |
874 | | |
875 | | struct rsn_pmksa_cache_entry * |
876 | | pmksa_cache_get(struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa, |
877 | | const u8 *pmkid, const void *network_ctx, int akmp) |
878 | | { |
879 | | return NULL; |
880 | | } |
881 | | |
882 | | |
883 | | struct rsn_pmksa_cache_entry * |
884 | | pmksa_cache_get_current(struct wpa_sm *sm) |
885 | | { |
886 | | return NULL; |
887 | | } |
888 | | |
889 | | |
890 | | int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len) |
891 | | { |
892 | | return -1; |
893 | | } |
894 | | |
895 | | |
896 | | struct rsn_pmksa_cache_entry * |
897 | | pmksa_cache_head(struct rsn_pmksa_cache *pmksa) |
898 | | { |
899 | | return NULL; |
900 | | } |
901 | | |
902 | | |
903 | | struct rsn_pmksa_cache_entry * |
904 | | pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa, |
905 | | struct rsn_pmksa_cache_entry *entry) |
906 | | { |
907 | | return NULL; |
908 | | } |
909 | | |
910 | | |
911 | | struct rsn_pmksa_cache_entry * |
912 | | pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len, |
913 | | const u8 *pmkid, const u8 *kck, size_t kck_len, |
914 | | const u8 *aa, const u8 *spa, void *network_ctx, int akmp, |
915 | | const u8 *cache_id) |
916 | | { |
917 | | return NULL; |
918 | | } |
919 | | |
920 | | |
921 | | void pmksa_cache_clear_current(struct wpa_sm *sm) |
922 | | { |
923 | | } |
924 | | |
925 | | |
926 | | int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, const u8 *bssid, |
927 | | void *network_ctx, int try_opportunistic, |
928 | | const u8 *fils_cache_id, int akmp, bool associated) |
929 | | { |
930 | | return -1; |
931 | | } |
932 | | |
933 | | |
934 | | void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx, |
935 | | const u8 *pmk, size_t pmk_len, bool external_only) |
936 | | { |
937 | | } |
938 | | |
939 | | |
940 | | void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa, |
941 | | struct rsn_pmksa_cache_entry *entry) |
942 | | { |
943 | | } |
944 | | |
945 | | |
946 | | void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa) |
947 | | { |
948 | | } |
949 | | |
950 | | #endif /* IEEE8021X_EAPOL */ |