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