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