Move wpa_supplicant specific scan code away from src/drivers
[libeap.git] / wpa_supplicant / sme.c
1 /*
2  * wpa_supplicant - SME
3  * Copyright (c) 2009, 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 "common/ieee802_11_defs.h"
19 #include "eapol_supp/eapol_supp_sm.h"
20 #include "common/wpa_common.h"
21 #include "rsn_supp/wpa.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "config.h"
24 #include "wpa_supplicant_i.h"
25 #include "driver_i.h"
26 #include "wpas_glue.h"
27 #include "wps_supplicant.h"
28 #include "notify.h"
29 #include "blacklist.h"
30 #include "bss.h"
31 #include "scan.h"
32 #include "sme.h"
33
34 void sme_authenticate(struct wpa_supplicant *wpa_s,
35                       struct wpa_bss *bss, struct wpa_ssid *ssid)
36 {
37         struct wpa_driver_auth_params params;
38         struct wpa_ssid *old_ssid;
39 #ifdef CONFIG_IEEE80211R
40         const u8 *ie;
41 #endif /* CONFIG_IEEE80211R */
42 #ifdef CONFIG_IEEE80211R
43         const u8 *md = NULL;
44 #endif /* CONFIG_IEEE80211R */
45         int i, bssid_changed;
46
47         if (bss == NULL) {
48                 wpa_printf(MSG_ERROR, "SME: No scan result available for the "
49                            "network");
50                 return;
51         }
52
53         wpa_s->current_bss = bss;
54
55         os_memset(&params, 0, sizeof(params));
56         wpa_s->reassociate = 0;
57
58         params.freq = bss->freq;
59         params.bssid = bss->bssid;
60         params.ssid = bss->ssid;
61         params.ssid_len = bss->ssid_len;
62
63         if (wpa_s->sme.ssid_len != params.ssid_len ||
64             os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
65                 wpa_s->sme.prev_bssid_set = 0;
66
67         wpa_s->sme.freq = params.freq;
68         os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
69         wpa_s->sme.ssid_len = params.ssid_len;
70
71         params.auth_alg = AUTH_ALG_OPEN_SYSTEM;
72 #ifdef IEEE8021X_EAPOL
73         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
74                 if (ssid->leap) {
75                         if (ssid->non_leap == 0)
76                                 params.auth_alg = AUTH_ALG_LEAP;
77                         else
78                                 params.auth_alg |= AUTH_ALG_LEAP;
79                 }
80         }
81 #endif /* IEEE8021X_EAPOL */
82         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
83                    params.auth_alg);
84         if (ssid->auth_alg) {
85                 params.auth_alg = 0;
86                 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
87                         params.auth_alg |= AUTH_ALG_OPEN_SYSTEM;
88                 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
89                         params.auth_alg |= AUTH_ALG_SHARED_KEY;
90                 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
91                         params.auth_alg |= AUTH_ALG_LEAP;
92                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
93                            params.auth_alg);
94         }
95
96         for (i = 0; i < NUM_WEP_KEYS; i++) {
97                 if (ssid->wep_key_len[i])
98                         params.wep_key[i] = ssid->wep_key[i];
99                 params.wep_key_len[i] = ssid->wep_key_len[i];
100         }
101         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
102
103         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
104         os_memset(wpa_s->bssid, 0, ETH_ALEN);
105         os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
106         if (bssid_changed)
107                 wpas_notify_bssid_changed(wpa_s);
108
109         if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
110                     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
111             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
112                                WPA_KEY_MGMT_FT_IEEE8021X |
113                                WPA_KEY_MGMT_FT_PSK |
114                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
115                                WPA_KEY_MGMT_PSK_SHA256))) {
116                 int try_opportunistic;
117                 try_opportunistic = ssid->proactive_key_caching &&
118                         (ssid->proto & WPA_PROTO_RSN);
119                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
120                                             wpa_s->current_ssid,
121                                             try_opportunistic) == 0)
122                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
123                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
124                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
125                                               wpa_s->sme.assoc_req_ie,
126                                               &wpa_s->sme.assoc_req_ie_len)) {
127                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
128                                    "management and encryption suites");
129                         return;
130                 }
131         } else if (ssid->key_mgmt &
132                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
133                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
134                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
135                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
136                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
137                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
138                                               wpa_s->sme.assoc_req_ie,
139                                               &wpa_s->sme.assoc_req_ie_len)) {
140                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
141                                    "management and encryption suites (no scan "
142                                    "results)");
143                         return;
144                 }
145 #ifdef CONFIG_WPS
146         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
147                 struct wpabuf *wps_ie;
148                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
149                 if (wps_ie && wpabuf_len(wps_ie) <=
150                     sizeof(wpa_s->sme.assoc_req_ie)) {
151                         wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
152                         os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
153                                   wpa_s->sme.assoc_req_ie_len);
154                 } else
155                         wpa_s->sme.assoc_req_ie_len = 0;
156                 wpabuf_free(wps_ie);
157                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
158 #endif /* CONFIG_WPS */
159         } else {
160                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
161                 wpa_s->sme.assoc_req_ie_len = 0;
162         }
163
164 #ifdef CONFIG_IEEE80211R
165         ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
166         if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
167                 md = ie + 2;
168         wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
169         if (md) {
170                 /* Prepare for the next transition */
171                 wpa_ft_prepare_auth_request(wpa_s->wpa);
172         }
173
174         if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
175                                     WPA_KEY_MGMT_FT_IEEE8021X)) {
176                 if (wpa_s->sme.assoc_req_ie_len + 5 <
177                     sizeof(wpa_s->sme.assoc_req_ie)) {
178                         struct rsn_mdie *mdie;
179                         u8 *pos = wpa_s->sme.assoc_req_ie +
180                                 wpa_s->sme.assoc_req_ie_len;
181                         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
182                         *pos++ = sizeof(*mdie);
183                         mdie = (struct rsn_mdie *) pos;
184                         os_memcpy(mdie->mobility_domain, md,
185                                   MOBILITY_DOMAIN_ID_LEN);
186                         mdie->ft_capab = 0;
187                         wpa_s->sme.assoc_req_ie_len += 5;
188                 }
189
190                 if (wpa_s->sme.ft_used &&
191                     os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0) {
192                         wpa_printf(MSG_DEBUG, "SME: Trying to use FT "
193                                    "over-the-air");
194                         params.auth_alg = AUTH_ALG_FT;
195                         params.ie = wpa_s->sme.ft_ies;
196                         params.ie_len = wpa_s->sme.ft_ies_len;
197                 }
198         }
199 #endif /* CONFIG_IEEE80211R */
200
201 #ifdef CONFIG_IEEE80211W
202         switch (ssid->ieee80211w) {
203         case NO_IEEE80211W:
204                 wpa_s->sme.mfp = NO_MGMT_FRAME_PROTECTION;
205                 break;
206         case IEEE80211W_OPTIONAL:
207                 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_OPTIONAL;
208                 break;
209         case IEEE80211W_REQUIRED:
210                 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
211                 break;
212         }
213         if (ssid->ieee80211w != NO_IEEE80211W && bss) {
214                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
215                 struct wpa_ie_data _ie;
216                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
217                     _ie.capabilities &
218                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
219                         wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
220                                    "require MFP");
221                         wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
222                 }
223         }
224 #endif /* CONFIG_IEEE80211W */
225
226         wpa_supplicant_cancel_scan(wpa_s);
227
228         wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
229                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
230                 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
231
232         wpa_clear_keys(wpa_s, bss->bssid);
233         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
234         old_ssid = wpa_s->current_ssid;
235         wpa_s->current_ssid = ssid;
236         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
237         wpa_supplicant_initiate_eapol(wpa_s);
238         if (old_ssid != wpa_s->current_ssid)
239                 wpas_notify_network_changed(wpa_s);
240
241         if (wpa_drv_authenticate(wpa_s, &params) < 0) {
242                 wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
243                         "driver failed");
244                 return;
245         }
246
247         /* TODO: add timeout on authentication */
248
249         /*
250          * Association will be started based on the authentication event from
251          * the driver.
252          */
253 }
254
255
256 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
257 {
258         struct wpa_driver_associate_params params;
259         struct wpa_ssid *ssid = wpa_s->current_ssid;
260
261         if (ssid == NULL) {
262                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
263                            "network is not selected");
264                 return;
265         }
266
267         if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
268                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
269                            "not in authenticating state");
270                 return;
271         }
272
273         if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
274                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication with "
275                            "unexpected peer " MACSTR,
276                            MAC2STR(data->auth.peer));
277                 return;
278         }
279
280         wpa_printf(MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
281                    " auth_type=%d status_code=%d",
282                    MAC2STR(data->auth.peer), data->auth.auth_type,
283                    data->auth.status_code);
284         wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
285                     data->auth.ies, data->auth.ies_len);
286
287         if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
288                 wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
289                            "code %d)", data->auth.status_code);
290                 return;
291         }
292
293 #ifdef CONFIG_IEEE80211R
294         if (data->auth.auth_type == WLAN_AUTH_FT) {
295                 union wpa_event_data edata;
296                 os_memset(&edata, 0, sizeof(edata));
297                 edata.ft_ies.ies = data->auth.ies;
298                 edata.ft_ies.ies_len = data->auth.ies_len;
299                 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
300                 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
301         }
302 #endif /* CONFIG_IEEE80211R */
303
304         os_memset(&params, 0, sizeof(params));
305         params.bssid = data->auth.peer;
306         params.ssid = wpa_s->sme.ssid;
307         params.ssid_len = wpa_s->sme.ssid_len;
308         params.freq = wpa_s->sme.freq;
309         params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
310                 wpa_s->sme.assoc_req_ie : NULL;
311         params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
312 #ifdef CONFIG_IEEE80211R
313         if (data->auth.auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
314                 params.wpa_ie = wpa_s->sme.ft_ies;
315                 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
316         }
317 #endif /* CONFIG_IEEE80211R */
318         params.mode = ssid->mode;
319         params.mgmt_frame_protection = wpa_s->sme.mfp;
320         if (wpa_s->sme.prev_bssid_set)
321                 params.prev_bssid = wpa_s->sme.prev_bssid;
322
323         wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
324                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
325                 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
326                 params.freq);
327
328         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
329
330         if (wpa_drv_associate(wpa_s, &params) < 0) {
331                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
332                         "failed");
333                 return;
334         }
335
336         /* TODO: add timeout on association */
337 }
338
339
340 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
341                       const u8 *ies, size_t ies_len)
342 {
343         if (md == NULL || ies == NULL) {
344                 wpa_printf(MSG_DEBUG, "SME: Remove mobility domain");
345                 os_free(wpa_s->sme.ft_ies);
346                 wpa_s->sme.ft_ies = NULL;
347                 wpa_s->sme.ft_ies_len = 0;
348                 wpa_s->sme.ft_used = 0;
349                 return 0;
350         }
351
352         os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
353         wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
354         os_free(wpa_s->sme.ft_ies);
355         wpa_s->sme.ft_ies = os_malloc(ies_len);
356         if (wpa_s->sme.ft_ies == NULL)
357                 return -1;
358         os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
359         wpa_s->sme.ft_ies_len = ies_len;
360         return 0;
361 }
362
363
364 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
365                             union wpa_event_data *data)
366 {
367         int bssid_changed;
368         int timeout = 5000;
369
370         wpa_printf(MSG_DEBUG, "SME: Association with " MACSTR " failed: "
371                    "status code %d", MAC2STR(wpa_s->pending_bssid),
372                    data->assoc_reject.status_code);
373
374         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
375
376         /*
377          * For now, unconditionally terminate the previous authentication. In
378          * theory, this should not be needed, but mac80211 gets quite confused
379          * if the authentication is left pending.. Some roaming cases might
380          * benefit from using the previous authentication, so this could be
381          * optimized in the future.
382          */
383         if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
384                                    WLAN_REASON_DEAUTH_LEAVING) < 0) {
385                 wpa_msg(wpa_s, MSG_INFO,
386                         "Deauth request to the driver failed");
387         }
388         wpa_s->sme.prev_bssid_set = 0;
389
390         if (wpa_blacklist_add(wpa_s, wpa_s->pending_bssid) == 0) {
391                 struct wpa_blacklist *b;
392                 b = wpa_blacklist_get(wpa_s, wpa_s->pending_bssid);
393                 if (b && b->count < 3) {
394                         /*
395                          * Speed up next attempt if there could be other APs
396                          * that could accept association.
397                          */
398                         timeout = 100;
399                 }
400         }
401         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
402         os_memset(wpa_s->bssid, 0, ETH_ALEN);
403         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
404         if (bssid_changed)
405                 wpas_notify_bssid_changed(wpa_s);
406
407         /*
408          * TODO: if more than one possible AP is available in scan results,
409          * could try the other ones before requesting a new scan.
410          */
411         wpa_supplicant_req_scan(wpa_s, timeout / 1000,
412                                 1000 * (timeout % 1000));
413 }
414
415
416 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
417                               union wpa_event_data *data)
418 {
419         wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
420         wpa_supplicant_req_scan(wpa_s, 5, 0);
421 }
422
423
424 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
425                                union wpa_event_data *data)
426 {
427         wpa_printf(MSG_DEBUG, "SME: Association timed out");
428         wpa_supplicant_mark_disassoc(wpa_s);
429         wpa_supplicant_req_scan(wpa_s, 5, 0);
430 }
431
432
433 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
434                         union wpa_event_data *data)
435 {
436         wpa_printf(MSG_DEBUG, "SME: Disassociation event received");
437         if (!is_zero_ether_addr(wpa_s->bssid) &&
438             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
439                 /*
440                  * cfg80211/mac80211 can get into somewhat confused state if
441                  * the AP only disassociates us and leaves us in authenticated
442                  * state. For now, force the state to be cleared to avoid
443                  * confusing errors if we try to associate with the AP again.
444                  */
445                 wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
446                            "state");
447                 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
448                                        WLAN_REASON_DEAUTH_LEAVING);
449         }
450 }