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