Document the recently added WITHOUT_SRCS variable.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / preauth.c
1 /*
2  * WPA Supplicant - RSN pre-authentication and PMKSA caching
3  * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <sys/time.h>
18 #ifndef CONFIG_NATIVE_WINDOWS
19 #include <netinet/in.h>
20 #endif /* CONFIG_NATIVE_WINDOWS */
21 #include <string.h>
22 #include <time.h>
23
24 #include "common.h"
25 #include "sha1.h"
26 #include "wpa.h"
27 #include "driver.h"
28 #include "eloop.h"
29 #include "wpa_supplicant.h"
30 #include "config.h"
31 #include "l2_packet.h"
32 #include "eapol_sm.h"
33 #include "preauth.h"
34 #include "wpa_i.h"
35
36
37 #define PMKID_CANDIDATE_PRIO_SCAN 1000
38 static const int pmksa_cache_max_entries = 32;
39
40
41 struct rsn_pmksa_candidate {
42         struct rsn_pmksa_candidate *next;
43         u8 bssid[ETH_ALEN];
44         int priority;
45 };
46
47
48 /**
49  * rsn_pmkid - Calculate PMK identifier
50  * @pmk: Pairwise master key
51  * @aa: Authenticator address
52  * @spa: Supplicant address
53  *
54  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
55  * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
56  */
57 static void rsn_pmkid(const u8 *pmk, const u8 *aa, const u8 *spa, u8 *pmkid)
58 {
59         char *title = "PMK Name";
60         const unsigned char *addr[3];
61         const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
62         unsigned char hash[SHA1_MAC_LEN];
63
64         addr[0] = (unsigned char *) title;
65         addr[1] = aa;
66         addr[2] = spa;
67
68         hmac_sha1_vector(pmk, PMK_LEN, 3, addr, len, hash);
69         memcpy(pmkid, hash, PMKID_LEN);
70 }
71
72
73 static void pmksa_cache_set_expiration(struct wpa_sm *sm);
74
75
76 static void pmksa_cache_free_entry(struct wpa_sm *sm,
77                                    struct rsn_pmksa_cache *entry, int replace)
78 {
79         int current;
80
81         current = sm->cur_pmksa == entry ||
82                 (sm->pmk_len == entry->pmk_len &&
83                  memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0);
84
85         free(entry);
86         sm->pmksa_count--;
87
88         if (current) {
89                 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
90                 sm->cur_pmksa = NULL;
91
92                 if (replace) {
93                         /* A new entry is being added, so no need to
94                          * deauthenticate in this case. This happens when EAP
95                          * authentication is completed again (reauth or failed
96                          * PMKSA caching attempt). */
97                         return;
98                 }
99
100                 memset(sm->pmk, 0, sizeof(sm->pmk));
101                 wpa_sm_deauthenticate(sm, REASON_UNSPECIFIED);
102                 wpa_sm_req_scan(sm, 0, 0);
103         }
104 }
105
106
107 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
108 {
109         struct wpa_sm *sm = eloop_ctx;
110         time_t now;
111
112         time(&now);
113         while (sm->pmksa && sm->pmksa->expiration <= now) {
114                 struct rsn_pmksa_cache *entry = sm->pmksa;
115                 sm->pmksa = entry->next;
116                 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
117                            MACSTR, MAC2STR(entry->aa));
118                 pmksa_cache_free_entry(sm, entry, 0);
119         }
120
121         pmksa_cache_set_expiration(sm);
122 }
123
124
125 static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
126 {
127         struct wpa_sm *sm = eloop_ctx;
128         sm->cur_pmksa = NULL;
129         eapol_sm_request_reauth(sm->eapol);
130 }
131
132
133 static void pmksa_cache_set_expiration(struct wpa_sm *sm)
134 {
135         int sec;
136         struct rsn_pmksa_cache *entry;
137
138         eloop_cancel_timeout(pmksa_cache_expire, sm, NULL);
139         eloop_cancel_timeout(pmksa_cache_reauth, sm, NULL);
140         if (sm->pmksa == NULL)
141                 return;
142         sec = sm->pmksa->expiration - time(NULL);
143         if (sec < 0)
144                 sec = 0;
145         eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, sm, NULL);
146
147         entry = sm->cur_pmksa ? sm->cur_pmksa :
148                 pmksa_cache_get(sm, sm->bssid, NULL);
149         if (entry) {
150                 sec = sm->pmksa->reauth_time - time(NULL);
151                 if (sec < 0)
152                         sec = 0;
153                 eloop_register_timeout(sec, 0, pmksa_cache_reauth, sm, NULL);
154         }
155 }
156
157
158 /**
159  * pmksa_cache_add - Add a PMKSA cache entry
160  * @sm: Pointer to WPA state machine data from wpa_sm_init()
161  * @pmk: The new pairwise master key
162  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
163  * @aa: Authenticator address
164  * @spa: Supplicant address
165  * @ssid: The network configuration for which this PMK is being added
166  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
167  *
168  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
169  * cache. If an old entry is already in the cache for the same Authenticator,
170  * this entry will be replaced with the new entry. PMKID will be calculated
171  * based on the PMK and the driver interface is notified of the new PMKID.
172  */
173 struct rsn_pmksa_cache *
174 pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk,
175                 size_t pmk_len, const u8 *aa, const u8 *spa,
176                 struct wpa_ssid *ssid)
177 {
178         struct rsn_pmksa_cache *entry, *pos, *prev;
179         time_t now;
180
181         if (sm->proto != WPA_PROTO_RSN || pmk_len > PMK_LEN)
182                 return NULL;
183
184         entry = malloc(sizeof(*entry));
185         if (entry == NULL)
186                 return NULL;
187         memset(entry, 0, sizeof(*entry));
188         memcpy(entry->pmk, pmk, pmk_len);
189         entry->pmk_len = pmk_len;
190         rsn_pmkid(pmk, aa, spa, entry->pmkid);
191         now = time(NULL);
192         entry->expiration = now + sm->dot11RSNAConfigPMKLifetime;
193         entry->reauth_time = now + sm->dot11RSNAConfigPMKLifetime *
194                 sm->dot11RSNAConfigPMKReauthThreshold / 100;
195         entry->akmp = WPA_KEY_MGMT_IEEE8021X;
196         memcpy(entry->aa, aa, ETH_ALEN);
197         entry->ssid = ssid;
198
199         /* Replace an old entry for the same Authenticator (if found) with the
200          * new entry */
201         pos = sm->pmksa;
202         prev = NULL;
203         while (pos) {
204                 if (memcmp(aa, pos->aa, ETH_ALEN) == 0) {
205                         if (pos->pmk_len == pmk_len &&
206                             memcmp(pos->pmk, pmk, pmk_len) == 0 &&
207                             memcmp(pos->pmkid, entry->pmkid, PMKID_LEN) == 0) {
208                                 wpa_printf(MSG_DEBUG, "WPA: reusing previous "
209                                            "PMKSA entry");
210                                 free(entry);
211                                 return pos;
212                         }
213                         if (prev == NULL)
214                                 sm->pmksa = pos->next;
215                         else
216                                 prev->next = pos->next;
217                         wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
218                                    "the current AP");
219                         pmksa_cache_free_entry(sm, pos, 1);
220                         break;
221                 }
222                 prev = pos;
223                 pos = pos->next;
224         }
225
226         if (sm->pmksa_count >= pmksa_cache_max_entries && sm->pmksa) {
227                 /* Remove the oldest entry to make room for the new entry */
228                 pos = sm->pmksa;
229                 sm->pmksa = pos->next;
230                 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
231                            "entry (for " MACSTR ") to make room for new one",
232                            MAC2STR(pos->aa));
233                 wpa_sm_remove_pmkid(sm, pos->aa, pos->pmkid);
234                 pmksa_cache_free_entry(sm, pos, 0);
235         }
236
237         /* Add the new entry; order by expiration time */
238         pos = sm->pmksa;
239         prev = NULL;
240         while (pos) {
241                 if (pos->expiration > entry->expiration)
242                         break;
243                 prev = pos;
244                 pos = pos->next;
245         }
246         if (prev == NULL) {
247                 entry->next = sm->pmksa;
248                 sm->pmksa = entry;
249                 pmksa_cache_set_expiration(sm);
250         } else {
251                 entry->next = prev->next;
252                 prev->next = entry;
253         }
254         sm->pmksa_count++;
255         wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
256                    MAC2STR(entry->aa));
257         wpa_sm_add_pmkid(sm, entry->aa, entry->pmkid);
258
259         return entry;
260 }
261
262
263 /**
264  * pmksa_cache_free - Free all entries in PMKSA cache
265  * @sm: Pointer to WPA state machine data from wpa_sm_init()
266  */
267 void pmksa_cache_free(struct wpa_sm *sm)
268 {
269         struct rsn_pmksa_cache *entry, *prev;
270
271         if (sm == NULL)
272                 return;
273
274         entry = sm->pmksa;
275         sm->pmksa = NULL;
276         while (entry) {
277                 prev = entry;
278                 entry = entry->next;
279                 free(prev);
280         }
281         pmksa_cache_set_expiration(sm);
282         sm->cur_pmksa = NULL;
283 }
284
285
286 /**
287  * pmksa_cache_get - Fetch a PMKSA cache entry
288  * @sm: Pointer to WPA state machine data from wpa_sm_init()
289  * @aa: Authenticator address or %NULL to match any
290  * @pmkid: PMKID or %NULL to match any
291  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
292  */
293 struct rsn_pmksa_cache * pmksa_cache_get(struct wpa_sm *sm,
294                                          const u8 *aa, const u8 *pmkid)
295 {
296         struct rsn_pmksa_cache *entry = sm->pmksa;
297         while (entry) {
298                 if ((aa == NULL || memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
299                     (pmkid == NULL ||
300                      memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
301                         return entry;
302                 entry = entry->next;
303         }
304         return NULL;
305 }
306
307
308 /**
309  * pmksa_cache_notify_reconfig - Reconfiguration notification for PMKSA cache
310  * @sm: Pointer to WPA state machine data from wpa_sm_init()
311  *
312  * Clear references to old data structures when wpa_supplicant is reconfigured.
313  */
314 void pmksa_cache_notify_reconfig(struct wpa_sm *sm)
315 {
316         struct rsn_pmksa_cache *entry = sm->pmksa;
317         while (entry) {
318                 entry->ssid = NULL;
319                 entry = entry->next;
320         }
321 }
322
323
324 static struct rsn_pmksa_cache *
325 pmksa_cache_clone_entry(struct wpa_sm *sm,
326                         const struct rsn_pmksa_cache *old_entry, const u8 *aa)
327 {
328         struct rsn_pmksa_cache *new_entry;
329
330         new_entry = pmksa_cache_add(sm, old_entry->pmk, old_entry->pmk_len,
331                                     aa, sm->own_addr, old_entry->ssid);
332         if (new_entry == NULL)
333                 return NULL;
334
335         /* TODO: reorder entries based on expiration time? */
336         new_entry->expiration = old_entry->expiration;
337         new_entry->opportunistic = 1;
338
339         return new_entry;
340 }
341
342
343 /**
344  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
345  * @sm: Pointer to WPA state machine data from wpa_sm_init()
346  * @ssid: Pointer to the current network configuration
347  * @aa: Authenticator address for the new AP
348  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
349  *
350  * Try to create a new PMKSA cache entry opportunistically by guessing that the
351  * new AP is sharing the same PMK as another AP that has the same SSID and has
352  * already an entry in PMKSA cache.
353  */
354 static struct rsn_pmksa_cache *
355 pmksa_cache_get_opportunistic(struct wpa_sm *sm,
356                               struct wpa_ssid *ssid, const u8 *aa)
357 {
358         struct rsn_pmksa_cache *entry = sm->pmksa;
359
360         if (ssid == NULL)
361                 return NULL;
362         while (entry) {
363                 if (entry->ssid == ssid) {
364                         entry = pmksa_cache_clone_entry(sm, entry, aa);
365                         if (entry) {
366                                 wpa_printf(MSG_DEBUG, "RSN: added "
367                                            "opportunistic PMKSA cache entry "
368                                            "for " MACSTR, MAC2STR(aa));
369                         }
370                         return entry;
371                 }
372                 entry = entry->next;
373         }
374         return NULL;
375 }
376
377
378 /**
379  * pmksa_cache_get_current - Get the current used PMKSA entry
380  * @sm: Pointer to WPA state machine data from wpa_sm_init()
381  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
382  */
383 struct rsn_pmksa_cache * pmksa_cache_get_current(struct wpa_sm *sm)
384 {
385         if (sm == NULL)
386                 return NULL;
387         return sm->cur_pmksa;
388 }
389
390
391 /**
392  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
393  * @sm: Pointer to WPA state machine data from wpa_sm_init()
394  */
395 void pmksa_cache_clear_current(struct wpa_sm *sm)
396 {
397         if (sm == NULL)
398                 return;
399         sm->cur_pmksa = NULL;
400 }
401
402
403 /**
404  * pmksa_cache_set_current - Set the current PMKSA entry selection
405  * @sm: Pointer to WPA state machine data from wpa_sm_init()
406  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
407  * @bssid: BSSID for PMKSA or %NULL if not used
408  * @ssid: The network configuration for the current network
409  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
410  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
411  */
412 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
413                             const u8 *bssid, struct wpa_ssid *ssid,
414                             int try_opportunistic)
415 {
416         sm->cur_pmksa = NULL;
417         if (pmkid)
418                 sm->cur_pmksa = pmksa_cache_get(sm, NULL, pmkid);
419         if (sm->cur_pmksa == NULL && bssid)
420                 sm->cur_pmksa = pmksa_cache_get(sm, bssid, NULL);
421         if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
422                 sm->cur_pmksa = pmksa_cache_get_opportunistic(sm, ssid, bssid);
423         if (sm->cur_pmksa) {
424                 wpa_hexdump(MSG_DEBUG, "RSN: PMKID",
425                             sm->cur_pmksa->pmkid, PMKID_LEN);
426                 return 0;
427         }
428         return -1;
429 }
430
431
432 /**
433  * pmksa_cache_list - Dump text list of entries in PMKSA cache
434  * @sm: Pointer to WPA state machine data from wpa_sm_init()
435  * @buf: Buffer for the list
436  * @len: Length of the buffer
437  * Returns: number of bytes written to buffer
438  *
439  * This function is used to generate a text format representation of the
440  * current PMKSA cache contents for the ctrl_iface PMKSA command.
441  */
442 int pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
443 {
444         int i, j;
445         char *pos = buf;
446         struct rsn_pmksa_cache *entry;
447         time_t now;
448
449         time(&now);
450         pos += snprintf(pos, buf + len - pos,
451                         "Index / AA / PMKID / expiration (in seconds) / "
452                         "opportunistic\n");
453         i = 0;
454         entry = sm->pmksa;
455         while (entry) {
456                 i++;
457                 pos += snprintf(pos, buf + len - pos, "%d " MACSTR " ",
458                                 i, MAC2STR(entry->aa));
459                 for (j = 0; j < PMKID_LEN; j++)
460                         pos += snprintf(pos, buf + len - pos, "%02x",
461                                         entry->pmkid[j]);
462                 pos += snprintf(pos, buf + len - pos, " %d %d\n",
463                                 (int) (entry->expiration - now),
464                                 entry->opportunistic);
465                 entry = entry->next;
466         }
467         return pos - buf;
468 }
469
470
471 /**
472  * pmksa_candidate_free - Free all entries in PMKSA candidate list
473  * @sm: Pointer to WPA state machine data from wpa_sm_init()
474  */
475 void pmksa_candidate_free(struct wpa_sm *sm)
476 {
477         struct rsn_pmksa_candidate *entry, *prev;
478
479         if (sm == NULL)
480                 return;
481
482         entry = sm->pmksa_candidates;
483         sm->pmksa_candidates = NULL;
484         while (entry) {
485                 prev = entry;
486                 entry = entry->next;
487                 free(prev);
488         }
489 }
490
491
492 #ifdef IEEE8021X_EAPOL
493
494 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
495                                 const u8 *buf, size_t len)
496 {
497         struct wpa_sm *sm = ctx;
498
499         wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr));
500         wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len);
501
502         if (sm->preauth_eapol == NULL ||
503             memcmp(sm->preauth_bssid, "\x00\x00\x00\x00\x00\x00",
504                    ETH_ALEN) == 0 ||
505             memcmp(sm->preauth_bssid, src_addr, ETH_ALEN) != 0) {
506                 wpa_printf(MSG_WARNING, "RSN pre-auth frame received from "
507                            "unexpected source " MACSTR " - dropped",
508                            MAC2STR(src_addr));
509                 return;
510         }
511
512         eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len);
513 }
514
515
516 static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, int success,
517                                  void *ctx)
518 {
519         struct wpa_sm *sm = ctx;
520         u8 pmk[PMK_LEN];
521
522         if (success) {
523                 int res, pmk_len;
524                 pmk_len = PMK_LEN;
525                 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
526 #ifdef EAP_LEAP
527                 if (res) {
528                         res = eapol_sm_get_key(eapol, pmk, 16);
529                         pmk_len = 16;
530                 }
531 #endif /* EAP_LEAP */
532                 if (res == 0) {
533                         wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth",
534                                         pmk, pmk_len);
535                         sm->pmk_len = pmk_len;
536                         pmksa_cache_add(sm, pmk, pmk_len,
537                                         sm->preauth_bssid, sm->own_addr,
538                                         sm->cur_ssid);
539                 } else {
540                         wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: failed to get "
541                                 "master session key from pre-auth EAPOL state "
542                                 "machines");
543                         success = 0;
544                 }
545         }
546
547         wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: pre-authentication with " MACSTR
548                 " %s", MAC2STR(sm->preauth_bssid),
549                 success ? "completed successfully" : "failed");
550
551         rsn_preauth_deinit(sm);
552         rsn_preauth_candidate_process(sm);
553 }
554
555
556 static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx)
557 {
558         struct wpa_sm *sm = eloop_ctx;
559
560         wpa_msg(sm->ctx->ctx, MSG_INFO, "RSN: pre-authentication with " MACSTR
561                 " timed out", MAC2STR(sm->preauth_bssid));
562         rsn_preauth_deinit(sm);
563         rsn_preauth_candidate_process(sm);
564 }
565
566
567 static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf,
568                                   size_t len)
569 {
570         struct wpa_sm *sm = ctx;
571         u8 *msg;
572         size_t msglen;
573         int res;
574
575         /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
576          * extra copy here */
577
578         if (sm->l2_preauth == NULL)
579                 return -1;
580
581         msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL);
582         if (msg == NULL)
583                 return -1;
584
585         wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen);
586         res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid,
587                              ETH_P_RSN_PREAUTH, msg, msglen);
588         free(msg);
589         return res;
590 }
591
592
593 /**
594  * rsn_preauth_init - Start new RSN pre-authentication
595  * @sm: Pointer to WPA state machine data from wpa_sm_init()
596  * @dst: Authenticator address (BSSID) with which to preauthenticate
597  * @config: Current network configuration
598  * Returns: 0 on success, -1 on another pre-authentication is in progress,
599  * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine
600  * initialization failure, -4 on memory allocation failure
601  *
602  * This function request an RSN pre-authentication with a given destination
603  * address. This is usually called for PMKSA candidates found from scan results
604  * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger
605  * pre-authentication.
606  */
607 int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst, struct wpa_ssid *config)
608 {
609         struct eapol_config eapol_conf;
610         struct eapol_ctx *ctx;
611
612         if (sm->preauth_eapol)
613                 return -1;
614
615         wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: starting pre-authentication "
616                 "with " MACSTR, MAC2STR(dst));
617
618         sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr,
619                                         ETH_P_RSN_PREAUTH,
620                                         rsn_preauth_receive, sm, 0);
621         if (sm->l2_preauth == NULL) {
622                 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet "
623                            "processing for pre-authentication");
624                 return -2;
625         }
626
627         ctx = malloc(sizeof(*ctx));
628         if (ctx == NULL) {
629                 wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context.");
630                 return -4;
631         }
632         memset(ctx, 0, sizeof(*ctx));
633         ctx->ctx = sm->ctx->ctx;
634         ctx->msg_ctx = sm->ctx->ctx;
635         ctx->preauth = 1;
636         ctx->cb = rsn_preauth_eapol_cb;
637         ctx->cb_ctx = sm;
638         ctx->scard_ctx = sm->scard_ctx;
639         ctx->eapol_send = rsn_preauth_eapol_send;
640         ctx->eapol_send_ctx = sm;
641         ctx->set_config_blob = sm->ctx->set_config_blob;
642         ctx->get_config_blob = sm->ctx->get_config_blob;
643
644         sm->preauth_eapol = eapol_sm_init(ctx);
645         if (sm->preauth_eapol == NULL) {
646                 free(ctx);
647                 wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL "
648                            "state machines for pre-authentication");
649                 return -3;
650         }
651         memset(&eapol_conf, 0, sizeof(eapol_conf));
652         eapol_conf.accept_802_1x_keys = 0;
653         eapol_conf.required_keys = 0;
654         eapol_conf.fast_reauth = sm->fast_reauth;
655         if (config)
656                 eapol_conf.workaround = config->eap_workaround;
657         eapol_sm_notify_config(sm->preauth_eapol, config, &eapol_conf);
658         /*
659          * Use a shorter startPeriod with preauthentication since the first
660          * preauth EAPOL-Start frame may end up being dropped due to race
661          * condition in the AP between the data receive and key configuration
662          * after the 4-Way Handshake.
663          */
664         eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6);
665         memcpy(sm->preauth_bssid, dst, ETH_ALEN);
666
667         eapol_sm_notify_portValid(sm->preauth_eapol, TRUE);
668         /* 802.1X::portControl = Auto */
669         eapol_sm_notify_portEnabled(sm->preauth_eapol, TRUE);
670
671         eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0,
672                                rsn_preauth_timeout, sm, NULL);
673
674         return 0;
675 }
676
677
678 /**
679  * rsn_preauth_deinit - Abort RSN pre-authentication
680  * @sm: Pointer to WPA state machine data from wpa_sm_init()
681  *
682  * This function aborts the current RSN pre-authentication (if one is started)
683  * and frees resources allocated for it.
684  */
685 void rsn_preauth_deinit(struct wpa_sm *sm)
686 {
687         if (sm == NULL || !sm->preauth_eapol)
688                 return;
689
690         eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL);
691         eapol_sm_deinit(sm->preauth_eapol);
692         sm->preauth_eapol = NULL;
693         memset(sm->preauth_bssid, 0, ETH_ALEN);
694
695         l2_packet_deinit(sm->l2_preauth);
696         sm->l2_preauth = NULL;
697 }
698
699
700 /**
701  * rsn_preauth_candidate_process - Process PMKSA candidates
702  * @sm: Pointer to WPA state machine data from wpa_sm_init()
703  *
704  * Go through the PMKSA candidates and start pre-authentication if a candidate
705  * without an existing PMKSA cache entry is found. Processed candidates will be
706  * removed from the list.
707  */
708 void rsn_preauth_candidate_process(struct wpa_sm *sm)
709 {
710         struct rsn_pmksa_candidate *candidate;
711
712         if (sm->pmksa_candidates == NULL)
713                 return;
714
715         /* TODO: drop priority for old candidate entries */
716
717         wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: processing PMKSA candidate "
718                 "list");
719         if (sm->preauth_eapol ||
720             sm->proto != WPA_PROTO_RSN ||
721             wpa_sm_get_state(sm) != WPA_COMPLETED ||
722             sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X) {
723                 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: not in suitable state "
724                         "for new pre-authentication");
725                 return; /* invalid state for new pre-auth */
726         }
727
728         while (sm->pmksa_candidates) {
729                 struct rsn_pmksa_cache *p = NULL;
730                 candidate = sm->pmksa_candidates;
731                 p = pmksa_cache_get(sm, candidate->bssid, NULL);
732                 if (memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 &&
733                     (p == NULL || p->opportunistic)) {
734                         wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: PMKSA "
735                                 "candidate " MACSTR
736                                 " selected for pre-authentication",
737                                 MAC2STR(candidate->bssid));
738                         sm->pmksa_candidates = candidate->next;
739                         rsn_preauth_init(sm, candidate->bssid, sm->cur_ssid);
740                         free(candidate);
741                         return;
742                 }
743                 wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: PMKSA candidate "
744                         MACSTR " does not need pre-authentication anymore",
745                         MAC2STR(candidate->bssid));
746                 /* Some drivers (e.g., NDIS) expect to get notified about the
747                  * PMKIDs again, so report the existing data now. */
748                 if (p) {
749                         wpa_sm_add_pmkid(sm, candidate->bssid, p->pmkid);
750                 }
751
752                 sm->pmksa_candidates = candidate->next;
753                 free(candidate);
754         }
755         wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: no more pending PMKSA "
756                 "candidates");
757 }
758
759
760 /**
761  * pmksa_candidate_add - Add a new PMKSA candidate
762  * @sm: Pointer to WPA state machine data from wpa_sm_init()
763  * @bssid: BSSID (authenticator address) of the candidate
764  * @prio: Priority (the smaller number, the higher priority)
765  * @preauth: Whether the candidate AP advertises support for pre-authentication
766  *
767  * This function is used to add PMKSA candidates for RSN pre-authentication. It
768  * is called from scan result processing and from driver events for PMKSA
769  * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event().
770  */
771 void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid,
772                          int prio, int preauth)
773 {
774         struct rsn_pmksa_candidate *cand, *prev, *pos;
775
776         if (sm->cur_ssid && sm->cur_ssid->proactive_key_caching)
777                 pmksa_cache_get_opportunistic(sm, sm->cur_ssid, bssid);
778
779         if (!preauth) {
780                 wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without "
781                            "preauth flag");
782                 return;
783         }
784
785         /* If BSSID already on candidate list, update the priority of the old
786          * entry. Do not override priority based on normal scan results. */
787         prev = NULL;
788         cand = sm->pmksa_candidates;
789         while (cand) {
790                 if (memcmp(cand->bssid, bssid, ETH_ALEN) == 0) {
791                         if (prev)
792                                 prev->next = cand->next;
793                         else
794                                 sm->pmksa_candidates = cand->next;
795                         break;
796                 }
797                 prev = cand;
798                 cand = cand->next;
799         }
800
801         if (cand) {
802                 if (prio < PMKID_CANDIDATE_PRIO_SCAN)
803                         cand->priority = prio;
804         } else {
805                 cand = malloc(sizeof(*cand));
806                 if (cand == NULL)
807                         return;
808                 memset(cand, 0, sizeof(*cand));
809                 memcpy(cand->bssid, bssid, ETH_ALEN);
810                 cand->priority = prio;
811         }
812
813         /* Add candidate to the list; order by increasing priority value. i.e.,
814          * highest priority (smallest value) first. */
815         prev = NULL;
816         pos = sm->pmksa_candidates;
817         while (pos) {
818                 if (cand->priority <= pos->priority)
819                         break;
820                 prev = pos;
821                 pos = pos->next;
822         }
823         cand->next = pos;
824         if (prev)
825                 prev->next = cand;
826         else
827                 sm->pmksa_candidates = cand;
828
829         wpa_msg(sm->ctx->ctx, MSG_DEBUG, "RSN: added PMKSA cache "
830                 "candidate " MACSTR " prio %d", MAC2STR(bssid), prio);
831         rsn_preauth_candidate_process(sm);
832 }
833
834
835 /* TODO: schedule periodic scans if current AP supports preauth */
836
837 /**
838  * rsn_preauth_scan_results - Process scan results to find PMKSA candidates
839  * @sm: Pointer to WPA state machine data from wpa_sm_init()
840  * @results: Scan results
841  * @count: Number of BSSes in scan results
842  *
843  * This functions goes through the scan results and adds all suitable APs
844  * (Authenticators) into PMKSA candidate list.
845  */
846 void rsn_preauth_scan_results(struct wpa_sm *sm,
847                               struct wpa_scan_result *results, int count)
848 {
849         struct wpa_scan_result *r;
850         struct wpa_ie_data ie;
851         int i;
852         struct rsn_pmksa_cache *pmksa;
853
854         if (sm->cur_ssid == NULL)
855                 return;
856
857         /*
858          * TODO: is it ok to free all candidates? What about the entries
859          * received from EVENT_PMKID_CANDIDATE?
860          */
861         pmksa_candidate_free(sm);
862
863         for (i = count - 1; i >= 0; i--) {
864                 r = &results[i];
865                 if (r->ssid_len != sm->cur_ssid->ssid_len ||
866                     memcmp(r->ssid, sm->cur_ssid->ssid,
867                            r->ssid_len) != 0)
868                         continue;
869
870                 if (memcmp(r->bssid, sm->bssid, ETH_ALEN) == 0)
871                         continue;
872
873                 if (r->rsn_ie_len == 0 ||
874                     wpa_parse_wpa_ie(r->rsn_ie, r->rsn_ie_len, &ie))
875                         continue;
876
877                 pmksa = pmksa_cache_get(sm, r->bssid, NULL);
878                 if (pmksa &&
879                     (!pmksa->opportunistic ||
880                      !(ie.capabilities & WPA_CAPABILITY_PREAUTH)))
881                         continue;
882
883                 /*
884                  * Give less priority to candidates found from normal
885                  * scan results.
886                  */
887                 pmksa_candidate_add(sm, r->bssid,
888                                     PMKID_CANDIDATE_PRIO_SCAN,
889                                     ie.capabilities & WPA_CAPABILITY_PREAUTH);
890         }
891 }
892
893
894 /**
895  * rsn_preauth_get_status - Get pre-authentication status
896  * @sm: Pointer to WPA state machine data from wpa_sm_init()
897  * @buf: Buffer for status information
898  * @buflen: Maximum buffer length
899  * @verbose: Whether to include verbose status information
900  * Returns: Number of bytes written to buf.
901  *
902  * Query WPA2 pre-authentication for status information. This function fills in
903  * a text area with current status information. If the buffer (buf) is not
904  * large enough, status information will be truncated to fit the buffer.
905  */
906 int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
907                            int verbose)
908 {
909         char *pos = buf, *end = buf + buflen;
910         int res;
911
912         if (sm->preauth_eapol) {
913                 pos += snprintf(pos, end - pos, "Pre-authentication "
914                                 "EAPOL state machines:\n");
915                 res = eapol_sm_get_status(sm->preauth_eapol,
916                                           pos, end - pos, verbose);
917                 if (res >= 0)
918                         pos += res;
919         }
920
921         return pos - buf;
922 }
923
924
925 /**
926  * rsn_preauth_in_progress - Verify whether pre-authentication is in progress
927  * @sm: Pointer to WPA state machine data from wpa_sm_init()
928  */
929 int rsn_preauth_in_progress(struct wpa_sm *sm)
930 {
931         return sm->preauth_eapol != NULL;
932 }
933
934 #endif /* IEEE8021X_EAPOL */