3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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"
24 #include "wpa_supplicant_i.h"
26 #include "wpas_glue.h"
27 #include "wps_supplicant.h"
29 #include "blacklist.h"
33 void sme_authenticate(struct wpa_supplicant *wpa_s,
34 struct wpa_bss *bss, struct wpa_ssid *ssid)
36 struct wpa_driver_auth_params params;
37 struct wpa_ssid *old_ssid;
39 #ifdef CONFIG_IEEE80211R
41 #endif /* CONFIG_IEEE80211R */
45 wpa_printf(MSG_ERROR, "SME: No scan result available for the "
50 wpa_s->current_bss = bss;
52 os_memset(¶ms, 0, sizeof(params));
53 wpa_s->reassociate = 0;
55 params.freq = bss->freq;
56 params.bssid = bss->bssid;
57 params.ssid = bss->ssid;
58 params.ssid_len = bss->ssid_len;
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;
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;
68 params.auth_alg = AUTH_ALG_OPEN_SYSTEM;
69 #ifdef IEEE8021X_EAPOL
70 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
72 if (ssid->non_leap == 0)
73 params.auth_alg = AUTH_ALG_LEAP;
75 params.auth_alg |= AUTH_ALG_LEAP;
78 #endif /* IEEE8021X_EAPOL */
79 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
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",
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];
98 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
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);
104 wpas_notify_bssid_changed(wpa_s);
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,
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");
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 "
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);
152 wpa_s->sme.assoc_req_ie_len = 0;
154 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
155 #endif /* CONFIG_WPS */
157 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
158 wpa_s->sme.assoc_req_ie_len = 0;
161 #ifdef CONFIG_IEEE80211R
162 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
163 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
165 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
167 /* Prepare for the next transition */
168 wpa_ft_prepare_auth_request(wpa_s->wpa);
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);
184 wpa_s->sme.assoc_req_ie_len += 5;
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 "
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;
196 #endif /* CONFIG_IEEE80211R */
198 #ifdef CONFIG_IEEE80211W
199 switch (ssid->ieee80211w) {
201 wpa_s->sme.mfp = NO_MGMT_FRAME_PROTECTION;
203 case IEEE80211W_OPTIONAL:
204 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_OPTIONAL;
206 case IEEE80211W_REQUIRED:
207 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
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 &&
215 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
216 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
218 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
221 #endif /* CONFIG_IEEE80211W */
223 wpa_supplicant_cancel_scan(wpa_s);
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);
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);
238 if (wpa_drv_authenticate(wpa_s, ¶ms) < 0) {
239 wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
244 /* TODO: add timeout on authentication */
247 * Association will be started based on the authentication event from
253 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
255 struct wpa_driver_associate_params params;
256 struct wpa_ssid *ssid = wpa_s->current_ssid;
259 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
260 "network is not selected");
264 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
265 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
266 "not in authenticating state");
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));
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);
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);
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);
299 #endif /* CONFIG_IEEE80211R */
301 os_memset(¶ms, 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;
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;
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) : "",
325 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
327 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
328 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
333 /* TODO: add timeout on association */
337 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
338 const u8 *ies, size_t ies_len)
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;
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)
355 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
356 wpa_s->sme.ft_ies_len = ies_len;
361 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
362 union wpa_event_data *data)
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);
371 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
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.
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");
385 wpa_s->sme.prev_bssid_set = 0;
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) {
392 * Speed up next attempt if there could be other APs
393 * that could accept association.
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);
402 wpas_notify_bssid_changed(wpa_s);
405 * TODO: if more than one possible AP is available in scan results,
406 * could try the other ones before requesting a new scan.
408 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
409 1000 * (timeout % 1000));
413 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
414 union wpa_event_data *data)
416 wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
417 wpa_supplicant_req_scan(wpa_s, 5, 0);
421 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
422 union wpa_event_data *data)
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);
430 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
431 union wpa_event_data *data)
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)) {
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.
442 wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
444 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
445 WLAN_REASON_DEAUTH_LEAVING);