Merge commit 'garage/master'
[wpasupplicant] / 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 "ieee802_11_defs.h"
19 #include "eapol_supp/eapol_supp_sm.h"
20 #include "wpa_common.h"
21 #include "wpa.h"
22 #include "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 "sme.h"
29
30 void sme_authenticate(struct wpa_supplicant *wpa_s,
31                       struct wpa_scan_res *bss, struct wpa_ssid *ssid)
32 {
33         struct wpa_driver_auth_params params;
34         const u8 *ie;
35 #ifdef CONFIG_IEEE80211R
36         const u8 *md = NULL;
37 #endif /* CONFIG_IEEE80211R */
38         int i;
39
40         if (bss == NULL) {
41                 wpa_printf(MSG_ERROR, "SME: No scan result available for the "
42                            "network");
43                 return;
44         }
45
46         os_memset(&params, 0, sizeof(params));
47         wpa_s->reassociate = 0;
48
49         params.freq = bss->freq;
50         params.bssid = bss->bssid;
51         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
52         if (ie == NULL) {
53                 wpa_printf(MSG_ERROR, "SME: SSID not available for the BSS");
54                 return;
55         }
56         params.ssid = ie + 2;
57         params.ssid_len = ie[1];
58
59         wpa_s->sme.freq = params.freq;
60         os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
61         wpa_s->sme.ssid_len = params.ssid_len;
62
63         params.auth_alg = AUTH_ALG_OPEN_SYSTEM;
64 #ifdef IEEE8021X_EAPOL
65         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
66                 if (ssid->leap) {
67                         if (ssid->non_leap == 0)
68                                 params.auth_alg = AUTH_ALG_LEAP;
69                         else
70                                 params.auth_alg |= AUTH_ALG_LEAP;
71                 }
72         }
73 #endif /* IEEE8021X_EAPOL */
74         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
75                    params.auth_alg);
76         if (ssid->auth_alg) {
77                 params.auth_alg = 0;
78                 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
79                         params.auth_alg |= AUTH_ALG_OPEN_SYSTEM;
80                 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
81                         params.auth_alg |= AUTH_ALG_SHARED_KEY;
82                 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
83                         params.auth_alg |= AUTH_ALG_LEAP;
84                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
85                            params.auth_alg);
86         }
87
88         for (i = 0; i < NUM_WEP_KEYS; i++) {
89                 if (ssid->wep_key_len[i])
90                         params.wep_key[i] = ssid->wep_key[i];
91                 params.wep_key_len[i] = ssid->wep_key_len[i];
92         }
93         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
94
95         os_memset(wpa_s->bssid, 0, ETH_ALEN);
96         os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
97
98         if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
99                     wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
100             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
101                                WPA_KEY_MGMT_FT_IEEE8021X |
102                                WPA_KEY_MGMT_FT_PSK |
103                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
104                                WPA_KEY_MGMT_PSK_SHA256))) {
105                 int try_opportunistic;
106                 try_opportunistic = ssid->proactive_key_caching &&
107                         (ssid->proto & WPA_PROTO_RSN);
108                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
109                                             wpa_s->current_ssid,
110                                             try_opportunistic) == 0)
111                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
112                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
113                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
114                                               wpa_s->sme.assoc_req_ie,
115                                               &wpa_s->sme.assoc_req_ie_len)) {
116                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
117                                    "management and encryption suites");
118                         return;
119                 }
120         } else if (ssid->key_mgmt &
121                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
122                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
123                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
124                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
125                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
126                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
127                                               wpa_s->sme.assoc_req_ie,
128                                               &wpa_s->sme.assoc_req_ie_len)) {
129                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
130                                    "management and encryption suites (no scan "
131                                    "results)");
132                         return;
133                 }
134 #ifdef CONFIG_WPS
135         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
136                 struct wpabuf *wps_ie;
137                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
138                 if (wps_ie && wpabuf_len(wps_ie) <=
139                     sizeof(wpa_s->sme.assoc_req_ie)) {
140                         wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
141                         os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
142                                   wpa_s->sme.assoc_req_ie_len);
143                 } else
144                         wpa_s->sme.assoc_req_ie_len = 0;
145                 wpabuf_free(wps_ie);
146                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
147 #endif /* CONFIG_WPS */
148         } else {
149                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
150                 wpa_s->sme.assoc_req_ie_len = 0;
151         }
152
153 #ifdef CONFIG_IEEE80211R
154         ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
155         if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
156                 md = ie + 2;
157         wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
158         if (md) {
159                 /* Prepare for the next transition */
160                 wpa_ft_prepare_auth_request(wpa_s->wpa);
161         }
162
163         if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
164                                     WPA_KEY_MGMT_FT_IEEE8021X)) {
165                 if (wpa_s->sme.assoc_req_ie_len + 5 <
166                     sizeof(wpa_s->sme.assoc_req_ie)) {
167                         struct rsn_mdie *mdie;
168                         u8 *pos = wpa_s->sme.assoc_req_ie +
169                                 wpa_s->sme.assoc_req_ie_len;
170                         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
171                         *pos++ = sizeof(*mdie);
172                         mdie = (struct rsn_mdie *) pos;
173                         os_memcpy(mdie->mobility_domain, md,
174                                   MOBILITY_DOMAIN_ID_LEN);
175                         mdie->ft_capab = 0;
176                         wpa_s->sme.assoc_req_ie_len += 5;
177                 }
178
179                 if (wpa_s->sme.ft_used &&
180                     os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0) {
181                         wpa_printf(MSG_DEBUG, "SME: Trying to use FT "
182                                    "over-the-air");
183                         params.auth_alg = AUTH_ALG_FT;
184                         params.ie = wpa_s->sme.ft_ies;
185                         params.ie_len = wpa_s->sme.ft_ies_len;
186                 }
187         }
188 #endif /* CONFIG_IEEE80211R */
189
190 #ifdef CONFIG_IEEE80211W
191         switch (ssid->ieee80211w) {
192         case NO_IEEE80211W:
193                 wpa_s->sme.mfp = NO_MGMT_FRAME_PROTECTION;
194                 break;
195         case IEEE80211W_OPTIONAL:
196                 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_OPTIONAL;
197                 break;
198         case IEEE80211W_REQUIRED:
199                 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
200                 break;
201         }
202         if (ssid->ieee80211w != NO_IEEE80211W && bss) {
203                 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
204                 struct wpa_ie_data _ie;
205                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
206                     _ie.capabilities &
207                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
208                         wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
209                                    "require MFP");
210                         wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
211                 }
212         }
213 #endif /* CONFIG_IEEE80211W */
214
215         wpa_supplicant_cancel_scan(wpa_s);
216
217         wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
218                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
219                 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
220
221         wpa_clear_keys(wpa_s, bss->bssid);
222         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
223         wpa_s->current_ssid = ssid;
224         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
225         wpa_supplicant_initiate_eapol(wpa_s);
226
227         if (wpa_drv_authenticate(wpa_s, &params) < 0) {
228                 wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
229                         "driver failed");
230                 return;
231         }
232
233         /* TODO: add timeout on authentication */
234
235         /*
236          * Association will be started based on the authentication event from
237          * the driver.
238          */
239 }
240
241
242 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
243 {
244         struct wpa_driver_associate_params params;
245         struct wpa_ssid *ssid = wpa_s->current_ssid;
246
247         if (ssid == NULL) {
248                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
249                            "network is not selected");
250                 return;
251         }
252
253         if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
254                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
255                            "not in authenticating state");
256                 return;
257         }
258
259         if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
260                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication with "
261                            "unexpected peer " MACSTR,
262                            MAC2STR(data->auth.peer));
263                 return;
264         }
265
266         wpa_printf(MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
267                    " auth_type=%d status_code=%d",
268                    MAC2STR(data->auth.peer), data->auth.auth_type,
269                    data->auth.status_code);
270         wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
271                     data->auth.ies, data->auth.ies_len);
272
273         if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
274                 wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
275                            "code %d)", data->auth.status_code);
276                 return;
277         }
278
279 #ifdef CONFIG_IEEE80211R
280         if (data->auth.auth_type == WLAN_AUTH_FT) {
281                 union wpa_event_data edata;
282                 os_memset(&edata, 0, sizeof(edata));
283                 edata.ft_ies.ies = data->auth.ies;
284                 edata.ft_ies.ies_len = data->auth.ies_len;
285                 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
286                 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
287         }
288 #endif /* CONFIG_IEEE80211R */
289
290         os_memset(&params, 0, sizeof(params));
291         params.bssid = data->auth.peer;
292         params.ssid = wpa_s->sme.ssid;
293         params.ssid_len = wpa_s->sme.ssid_len;
294         params.freq = wpa_s->sme.freq;
295         params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
296                 wpa_s->sme.assoc_req_ie : NULL;
297         params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
298 #ifdef CONFIG_IEEE80211R
299         if (data->auth.auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
300                 params.wpa_ie = wpa_s->sme.ft_ies;
301                 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
302         }
303 #endif /* CONFIG_IEEE80211R */
304         params.mode = ssid->mode;
305         params.mgmt_frame_protection = wpa_s->sme.mfp;
306
307         wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
308                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
309                 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
310                 params.freq);
311
312         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
313
314         if (wpa_drv_associate(wpa_s, &params) < 0) {
315                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
316                         "failed");
317                 return;
318         }
319
320         /* TODO: add timeout on association */
321 }
322
323
324 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
325                       const u8 *ies, size_t ies_len)
326 {
327         if (md == NULL || ies == NULL) {
328                 wpa_printf(MSG_DEBUG, "SME: Remove mobility domain");
329                 os_free(wpa_s->sme.ft_ies);
330                 wpa_s->sme.ft_ies = NULL;
331                 wpa_s->sme.ft_ies_len = 0;
332                 wpa_s->sme.ft_used = 0;
333                 return 0;
334         }
335
336         os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
337         wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
338         os_free(wpa_s->sme.ft_ies);
339         wpa_s->sme.ft_ies = os_malloc(ies_len);
340         if (wpa_s->sme.ft_ies == NULL)
341                 return -1;
342         os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
343         wpa_s->sme.ft_ies_len = ies_len;
344         return 0;
345 }
346
347
348 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
349                             union wpa_event_data *data)
350 {
351         wpa_printf(MSG_DEBUG, "SME: Association failed: status code %d",
352                    data->assoc_reject.status_code);
353
354         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
355         os_memset(wpa_s->bssid, 0, ETH_ALEN);
356         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
357
358         /*
359          * TODO: if more than one possible AP is available in scan results,
360          * could try the other ones before requesting a new scan.
361          */
362         wpa_supplicant_req_scan(wpa_s, 5, 0);
363 }
364
365
366 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
367                               union wpa_event_data *data)
368 {
369         wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
370         wpa_supplicant_req_scan(wpa_s, 5, 0);
371 }
372
373
374 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
375                                union wpa_event_data *data)
376 {
377         wpa_printf(MSG_DEBUG, "SME: Association timed out");
378         wpa_supplicant_mark_disassoc(wpa_s);
379         wpa_supplicant_req_scan(wpa_s, 5, 0);
380 }