]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/sme.c
HS 2.0: Add Hotspot 2.0 ANQP elements to Interworking queries
[thirdparty/hostap.git] / wpa_supplicant / sme.c
1 /*
2 * wpa_supplicant - SME
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "eapol_supp/eapol_supp_sm.h"
16 #include "common/wpa_common.h"
17 #include "rsn_supp/wpa.h"
18 #include "rsn_supp/pmksa_cache.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "driver_i.h"
22 #include "wpas_glue.h"
23 #include "wps_supplicant.h"
24 #include "p2p_supplicant.h"
25 #include "notify.h"
26 #include "bss.h"
27 #include "scan.h"
28 #include "sme.h"
29 #include "hs20_supplicant.h"
30
31 #define SME_AUTH_TIMEOUT 5
32 #define SME_ASSOC_TIMEOUT 5
33
34 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
35 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
36 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
37 #ifdef CONFIG_IEEE80211W
38 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
39 #endif /* CONFIG_IEEE80211W */
40
41
42 void sme_authenticate(struct wpa_supplicant *wpa_s,
43 struct wpa_bss *bss, struct wpa_ssid *ssid)
44 {
45 struct wpa_driver_auth_params params;
46 struct wpa_ssid *old_ssid;
47 #ifdef CONFIG_IEEE80211R
48 const u8 *ie;
49 #endif /* CONFIG_IEEE80211R */
50 #ifdef CONFIG_IEEE80211R
51 const u8 *md = NULL;
52 #endif /* CONFIG_IEEE80211R */
53 int i, bssid_changed;
54
55 if (bss == NULL) {
56 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
57 "the network");
58 return;
59 }
60
61 wpa_s->current_bss = bss;
62
63 os_memset(&params, 0, sizeof(params));
64 wpa_s->reassociate = 0;
65
66 params.freq = bss->freq;
67 params.bssid = bss->bssid;
68 params.ssid = bss->ssid;
69 params.ssid_len = bss->ssid_len;
70 params.p2p = ssid->p2p_group;
71
72 if (wpa_s->sme.ssid_len != params.ssid_len ||
73 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
74 wpa_s->sme.prev_bssid_set = 0;
75
76 wpa_s->sme.freq = params.freq;
77 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
78 wpa_s->sme.ssid_len = params.ssid_len;
79
80 params.auth_alg = WPA_AUTH_ALG_OPEN;
81 #ifdef IEEE8021X_EAPOL
82 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
83 if (ssid->leap) {
84 if (ssid->non_leap == 0)
85 params.auth_alg = WPA_AUTH_ALG_LEAP;
86 else
87 params.auth_alg |= WPA_AUTH_ALG_LEAP;
88 }
89 }
90 #endif /* IEEE8021X_EAPOL */
91 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
92 params.auth_alg);
93 if (ssid->auth_alg) {
94 params.auth_alg = ssid->auth_alg;
95 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
96 "0x%x", params.auth_alg);
97 }
98
99 for (i = 0; i < NUM_WEP_KEYS; i++) {
100 if (ssid->wep_key_len[i])
101 params.wep_key[i] = ssid->wep_key[i];
102 params.wep_key_len[i] = ssid->wep_key_len[i];
103 }
104 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
105
106 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
107 os_memset(wpa_s->bssid, 0, ETH_ALEN);
108 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
109 if (bssid_changed)
110 wpas_notify_bssid_changed(wpa_s);
111
112 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
113 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
114 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
115 int try_opportunistic;
116 try_opportunistic = ssid->proactive_key_caching &&
117 (ssid->proto & WPA_PROTO_RSN);
118 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
119 wpa_s->current_ssid,
120 try_opportunistic) == 0)
121 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
122 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
123 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
124 wpa_s->sme.assoc_req_ie,
125 &wpa_s->sme.assoc_req_ie_len)) {
126 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
127 "key management and encryption suites");
128 return;
129 }
130 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
131 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
132 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
133 wpa_s->sme.assoc_req_ie,
134 &wpa_s->sme.assoc_req_ie_len)) {
135 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
136 "key management and encryption suites (no "
137 "scan results)");
138 return;
139 }
140 #ifdef CONFIG_WPS
141 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
142 struct wpabuf *wps_ie;
143 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
144 if (wps_ie && wpabuf_len(wps_ie) <=
145 sizeof(wpa_s->sme.assoc_req_ie)) {
146 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
147 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
148 wpa_s->sme.assoc_req_ie_len);
149 } else
150 wpa_s->sme.assoc_req_ie_len = 0;
151 wpabuf_free(wps_ie);
152 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
153 #endif /* CONFIG_WPS */
154 } else {
155 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
156 wpa_s->sme.assoc_req_ie_len = 0;
157 }
158
159 #ifdef CONFIG_IEEE80211R
160 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
161 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
162 md = ie + 2;
163 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
164 if (md) {
165 /* Prepare for the next transition */
166 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
167 }
168
169 if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
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 = md[MOBILITY_DOMAIN_ID_LEN];
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_sm_has_ptk(wpa_s->wpa)) {
187 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
188 "over-the-air");
189 params.auth_alg = WPA_AUTH_ALG_FT;
190 params.ie = wpa_s->sme.ft_ies;
191 params.ie_len = wpa_s->sme.ft_ies_len;
192 }
193 }
194 #endif /* CONFIG_IEEE80211R */
195
196 #ifdef CONFIG_IEEE80211W
197 wpa_s->sme.mfp = ssid->ieee80211w;
198 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
199 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
200 struct wpa_ie_data _ie;
201 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
202 _ie.capabilities &
203 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
204 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
205 "MFP: require MFP");
206 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
207 }
208 }
209 #endif /* CONFIG_IEEE80211W */
210
211 #ifdef CONFIG_P2P
212 if (wpa_s->global->p2p) {
213 u8 *pos;
214 size_t len;
215 int res;
216 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
217 len = sizeof(wpa_s->sme.assoc_req_ie) -
218 wpa_s->sme.assoc_req_ie_len;
219 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
220 ssid->p2p_group);
221 if (res >= 0)
222 wpa_s->sme.assoc_req_ie_len += res;
223 }
224 #endif /* CONFIG_P2P */
225
226 #ifdef CONFIG_HS20
227 if (wpa_s->conf->hs20) {
228 struct wpabuf *hs20;
229 hs20 = wpabuf_alloc(20);
230 if (hs20) {
231 wpas_hs20_add_indication(hs20);
232 os_memcpy(wpa_s->sme.assoc_req_ie +
233 wpa_s->sme.assoc_req_ie_len,
234 wpabuf_head(hs20), wpabuf_len(hs20));
235 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
236 wpabuf_free(hs20);
237 }
238 }
239 #endif /* CONFIG_HS20 */
240
241 #ifdef CONFIG_INTERWORKING
242 if (wpa_s->conf->interworking) {
243 u8 *pos = wpa_s->sme.assoc_req_ie;
244 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
245 pos += 2 + pos[1];
246 os_memmove(pos + 6, pos,
247 wpa_s->sme.assoc_req_ie_len -
248 (pos - wpa_s->sme.assoc_req_ie));
249 wpa_s->sme.assoc_req_ie_len += 6;
250 *pos++ = WLAN_EID_EXT_CAPAB;
251 *pos++ = 4;
252 *pos++ = 0x00;
253 *pos++ = 0x00;
254 *pos++ = 0x00;
255 *pos++ = 0x80; /* Bit 31 - Interworking */
256 }
257 #endif /* CONFIG_INTERWORKING */
258
259 wpa_supplicant_cancel_sched_scan(wpa_s);
260 wpa_supplicant_cancel_scan(wpa_s);
261
262 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
263 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
264 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
265
266 wpa_clear_keys(wpa_s, bss->bssid);
267 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
268 old_ssid = wpa_s->current_ssid;
269 wpa_s->current_ssid = ssid;
270 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
271 wpa_supplicant_initiate_eapol(wpa_s);
272 if (old_ssid != wpa_s->current_ssid)
273 wpas_notify_network_changed(wpa_s);
274
275 wpa_s->sme.auth_alg = params.auth_alg;
276 if (wpa_drv_authenticate(wpa_s, &params) < 0) {
277 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
278 "driver failed");
279 wpas_connection_failed(wpa_s, bss->bssid);
280 wpa_supplicant_mark_disassoc(wpa_s);
281 return;
282 }
283
284 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
285 NULL);
286
287 /*
288 * Association will be started based on the authentication event from
289 * the driver.
290 */
291 }
292
293
294 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
295 {
296 struct wpa_ssid *ssid = wpa_s->current_ssid;
297
298 if (ssid == NULL) {
299 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
300 "when network is not selected");
301 return;
302 }
303
304 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
305 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
306 "when not in authenticating state");
307 return;
308 }
309
310 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
311 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
312 "unexpected peer " MACSTR,
313 MAC2STR(data->auth.peer));
314 return;
315 }
316
317 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
318 " auth_type=%d status_code=%d",
319 MAC2STR(data->auth.peer), data->auth.auth_type,
320 data->auth.status_code);
321 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
322 data->auth.ies, data->auth.ies_len);
323
324 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
325
326 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
327 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
328 "code %d)", data->auth.status_code);
329
330 if (data->auth.status_code !=
331 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
332 wpa_s->sme.auth_alg == data->auth.auth_type ||
333 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
334 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
335 return;
336 }
337
338 switch (data->auth.auth_type) {
339 case WLAN_AUTH_OPEN:
340 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
341
342 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
343 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
344 wpa_s->current_ssid);
345 return;
346
347 case WLAN_AUTH_SHARED_KEY:
348 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
349
350 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
351 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
352 wpa_s->current_ssid);
353 return;
354
355 default:
356 return;
357 }
358 }
359
360 #ifdef CONFIG_IEEE80211R
361 if (data->auth.auth_type == WLAN_AUTH_FT) {
362 union wpa_event_data edata;
363 os_memset(&edata, 0, sizeof(edata));
364 edata.ft_ies.ies = data->auth.ies;
365 edata.ft_ies.ies_len = data->auth.ies_len;
366 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
367 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
368 }
369 #endif /* CONFIG_IEEE80211R */
370
371 sme_associate(wpa_s, ssid->mode, data->auth.peer,
372 data->auth.auth_type);
373 }
374
375
376 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
377 const u8 *bssid, u16 auth_type)
378 {
379 struct wpa_driver_associate_params params;
380 struct ieee802_11_elems elems;
381 #ifdef CONFIG_HT_OVERRIDES
382 struct ieee80211_ht_capabilities htcaps;
383 struct ieee80211_ht_capabilities htcaps_mask;
384 #endif /* CONFIG_HT_OVERRIDES */
385
386 os_memset(&params, 0, sizeof(params));
387 params.bssid = bssid;
388 params.ssid = wpa_s->sme.ssid;
389 params.ssid_len = wpa_s->sme.ssid_len;
390 params.freq = wpa_s->sme.freq;
391 params.bg_scan_period = wpa_s->current_ssid ?
392 wpa_s->current_ssid->bg_scan_period : -1;
393 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
394 wpa_s->sme.assoc_req_ie : NULL;
395 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
396 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
397 params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
398 #ifdef CONFIG_HT_OVERRIDES
399 os_memset(&htcaps, 0, sizeof(htcaps));
400 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
401 params.htcaps = (u8 *) &htcaps;
402 params.htcaps_mask = (u8 *) &htcaps_mask;
403 wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
404 #endif /* CONFIG_HT_OVERRIDES */
405 #ifdef CONFIG_IEEE80211R
406 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
407 params.wpa_ie = wpa_s->sme.ft_ies;
408 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
409 }
410 #endif /* CONFIG_IEEE80211R */
411 params.mode = mode;
412 params.mgmt_frame_protection = wpa_s->sme.mfp;
413 if (wpa_s->sme.prev_bssid_set)
414 params.prev_bssid = wpa_s->sme.prev_bssid;
415
416 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
417 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
418 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
419 params.freq);
420
421 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
422
423 if (params.wpa_ie == NULL ||
424 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
425 < 0) {
426 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
427 os_memset(&elems, 0, sizeof(elems));
428 }
429 if (elems.rsn_ie) {
430 params.wpa_proto = WPA_PROTO_RSN;
431 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
432 elems.rsn_ie_len + 2);
433 } else if (elems.wpa_ie) {
434 params.wpa_proto = WPA_PROTO_WPA;
435 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
436 elems.wpa_ie_len + 2);
437 } else
438 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
439 if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
440 params.p2p = 1;
441
442 if (wpa_s->parent->set_sta_uapsd)
443 params.uapsd = wpa_s->parent->sta_uapsd;
444 else
445 params.uapsd = -1;
446
447 if (wpa_drv_associate(wpa_s, &params) < 0) {
448 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
449 "driver failed");
450 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
451 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
452 return;
453 }
454
455 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
456 NULL);
457 }
458
459
460 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
461 const u8 *ies, size_t ies_len)
462 {
463 if (md == NULL || ies == NULL) {
464 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
465 os_free(wpa_s->sme.ft_ies);
466 wpa_s->sme.ft_ies = NULL;
467 wpa_s->sme.ft_ies_len = 0;
468 wpa_s->sme.ft_used = 0;
469 return 0;
470 }
471
472 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
473 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
474 os_free(wpa_s->sme.ft_ies);
475 wpa_s->sme.ft_ies = os_malloc(ies_len);
476 if (wpa_s->sme.ft_ies == NULL)
477 return -1;
478 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
479 wpa_s->sme.ft_ies_len = ies_len;
480 return 0;
481 }
482
483
484 static void sme_deauth(struct wpa_supplicant *wpa_s)
485 {
486 int bssid_changed;
487
488 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
489
490 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
491 WLAN_REASON_DEAUTH_LEAVING) < 0) {
492 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
493 "failed");
494 }
495 wpa_s->sme.prev_bssid_set = 0;
496
497 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
498 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
499 os_memset(wpa_s->bssid, 0, ETH_ALEN);
500 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
501 if (bssid_changed)
502 wpas_notify_bssid_changed(wpa_s);
503 }
504
505
506 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
507 union wpa_event_data *data)
508 {
509 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
510 "status code %d", MAC2STR(wpa_s->pending_bssid),
511 data->assoc_reject.status_code);
512
513 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
514
515 /*
516 * For now, unconditionally terminate the previous authentication. In
517 * theory, this should not be needed, but mac80211 gets quite confused
518 * if the authentication is left pending.. Some roaming cases might
519 * benefit from using the previous authentication, so this could be
520 * optimized in the future.
521 */
522 sme_deauth(wpa_s);
523 }
524
525
526 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
527 union wpa_event_data *data)
528 {
529 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
530 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
531 wpa_supplicant_mark_disassoc(wpa_s);
532 }
533
534
535 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
536 union wpa_event_data *data)
537 {
538 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
539 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
540 wpa_supplicant_mark_disassoc(wpa_s);
541 }
542
543
544 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
545 union wpa_event_data *data)
546 {
547 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
548 if (wpa_s->sme.prev_bssid_set) {
549 /*
550 * cfg80211/mac80211 can get into somewhat confused state if
551 * the AP only disassociates us and leaves us in authenticated
552 * state. For now, force the state to be cleared to avoid
553 * confusing errors if we try to associate with the AP again.
554 */
555 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
556 "driver state");
557 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
558 WLAN_REASON_DEAUTH_LEAVING);
559 }
560 }
561
562
563 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
564 {
565 struct wpa_supplicant *wpa_s = eloop_ctx;
566 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
567 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
568 sme_deauth(wpa_s);
569 }
570 }
571
572
573 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
574 {
575 struct wpa_supplicant *wpa_s = eloop_ctx;
576 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
577 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
578 sme_deauth(wpa_s);
579 }
580 }
581
582
583 void sme_state_changed(struct wpa_supplicant *wpa_s)
584 {
585 /* Make sure timers are cleaned up appropriately. */
586 if (wpa_s->wpa_state != WPA_ASSOCIATING)
587 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
588 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
589 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
590 }
591
592
593 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
594 const u8 *prev_pending_bssid)
595 {
596 /*
597 * mac80211-workaround to force deauth on failed auth cmd,
598 * requires us to remain in authenticating state to allow the
599 * second authentication attempt to be continued properly.
600 */
601 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
602 "to proceed after disconnection event");
603 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
604 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
605
606 /*
607 * Re-arm authentication timer in case auth fails for whatever reason.
608 */
609 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
610 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
611 NULL);
612 }
613
614
615 void sme_deinit(struct wpa_supplicant *wpa_s)
616 {
617 os_free(wpa_s->sme.ft_ies);
618 wpa_s->sme.ft_ies = NULL;
619 wpa_s->sme.ft_ies_len = 0;
620 #ifdef CONFIG_IEEE80211W
621 sme_stop_sa_query(wpa_s);
622 #endif /* CONFIG_IEEE80211W */
623
624 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
625 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
626 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
627 }
628
629
630 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
631 const u8 *chan_list, u8 num_channels,
632 u8 num_intol)
633 {
634 struct ieee80211_2040_bss_coex_ie *bc_ie;
635 struct ieee80211_2040_intol_chan_report *ic_report;
636 struct wpabuf *buf;
637
638 wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
639 MAC2STR(wpa_s->bssid));
640
641 buf = wpabuf_alloc(2 + /* action.category + action_code */
642 sizeof(struct ieee80211_2040_bss_coex_ie) +
643 sizeof(struct ieee80211_2040_intol_chan_report) +
644 num_channels);
645 if (buf == NULL)
646 return;
647
648 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
649 wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
650
651 bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
652 bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
653 bc_ie->length = 1;
654 if (num_intol)
655 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
656
657 if (num_channels > 0) {
658 ic_report = wpabuf_put(buf, sizeof(*ic_report));
659 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
660 ic_report->length = num_channels + 1;
661 ic_report->op_class = 0;
662 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
663 num_channels);
664 }
665
666 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
667 wpa_s->own_addr, wpa_s->bssid,
668 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
669 wpa_msg(wpa_s, MSG_INFO,
670 "SME: Failed to send 20/40 BSS Coexistence frame");
671 }
672
673 wpabuf_free(buf);
674 }
675
676
677 /**
678 * enum wpas_band - Frequency band
679 * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
680 * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
681 */
682 enum wpas_band {
683 WPAS_BAND_2GHZ,
684 WPAS_BAND_5GHZ,
685 WPAS_BAND_INVALID
686 };
687
688 /**
689 * freq_to_channel - Convert frequency into channel info
690 * @channel: Buffer for returning channel number
691 * Returns: Band (2 or 5 GHz)
692 */
693 static enum wpas_band freq_to_channel(int freq, u8 *channel)
694 {
695 enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
696 u8 chan = 0;
697
698 if (freq >= 2412 && freq <= 2472)
699 chan = (freq - 2407) / 5;
700 else if (freq == 2484)
701 chan = 14;
702 else if (freq >= 5180 && freq <= 5805)
703 chan = (freq - 5000) / 5;
704
705 *channel = chan;
706 return band;
707 }
708
709
710 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
711 {
712 struct wpa_bss *bss;
713 const u8 *ie;
714 u16 ht_cap;
715 u8 chan_list[P2P_MAX_CHANNELS], channel;
716 u8 num_channels = 0, num_intol = 0, i;
717
718 if (!wpa_s->sme.sched_obss_scan)
719 return 0;
720
721 wpa_s->sme.sched_obss_scan = 0;
722 if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
723 return 1;
724
725 /*
726 * Check whether AP uses regulatory triplet or channel triplet in
727 * country info. Right now the operating class of the BSS channel
728 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
729 * based on the assumption that operating class triplet is not used in
730 * beacon frame. If the First Channel Number/Operating Extension
731 * Identifier octet has a positive integer value of 201 or greater,
732 * then its operating class triplet.
733 *
734 * TODO: If Supported Operating Classes element is present in beacon
735 * frame, have to lookup operating class in Annex E and fill them in
736 * 2040 coex frame.
737 */
738 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
739 if (ie && (ie[1] >= 6) && (ie[5] >= 201))
740 return 1;
741
742 os_memset(chan_list, 0, sizeof(chan_list));
743
744 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
745 /* Skip other band bss */
746 if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
747 continue;
748
749 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
750 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
751
752 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
753 /* Check whether the channel is already considered */
754 for (i = 0; i < num_channels; i++) {
755 if (channel == chan_list[i])
756 break;
757 }
758 if (i != num_channels)
759 continue;
760
761 if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
762 num_intol++;
763
764 chan_list[num_channels++] = channel;
765 }
766 }
767
768 sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
769 return 1;
770 }
771
772
773 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
774 u16 num_modes,
775 enum hostapd_hw_mode mode)
776 {
777 u16 i;
778
779 for (i = 0; i < num_modes; i++) {
780 if (modes[i].mode == mode)
781 return &modes[i];
782 }
783
784 return NULL;
785 }
786
787
788 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
789 enum hostapd_hw_mode band,
790 struct wpa_driver_scan_params *params)
791 {
792 /* Include only supported channels for the specified band */
793 struct hostapd_hw_modes *mode;
794 int count, i;
795
796 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
797 if (mode == NULL) {
798 /* No channels supported in this band - use empty list */
799 params->freqs = os_zalloc(sizeof(int));
800 return;
801 }
802
803 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
804 if (params->freqs == NULL)
805 return;
806 for (count = 0, i = 0; i < mode->num_channels; i++) {
807 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
808 continue;
809 params->freqs[count++] = mode->channels[i].freq;
810 }
811 }
812
813
814 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
815 {
816 struct wpa_supplicant *wpa_s = eloop_ctx;
817 struct wpa_driver_scan_params params;
818
819 if (!wpa_s->current_bss) {
820 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
821 return;
822 }
823
824 os_memset(&params, 0, sizeof(params));
825 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params);
826 wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
827
828 if (wpa_supplicant_trigger_scan(wpa_s, &params))
829 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
830 else
831 wpa_s->sme.sched_obss_scan = 1;
832 os_free(params.freqs);
833
834 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
835 sme_obss_scan_timeout, wpa_s, NULL);
836 }
837
838
839 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
840 {
841 const u8 *ie;
842 struct wpa_bss *bss = wpa_s->current_bss;
843 struct wpa_ssid *ssid = wpa_s->current_ssid;
844
845 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
846 wpa_s->sme.sched_obss_scan = 0;
847 if (!enable)
848 return;
849
850 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) || ssid == NULL ||
851 ssid->mode != IEEE80211_MODE_INFRA)
852 return; /* Not using station SME in wpa_supplicant */
853
854 if (!wpa_s->hw.modes ||
855 !(wpa_s->hw.modes->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
856 return; /* Driver does not support HT40 */
857
858 if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
859 return; /* Not associated on 2.4 GHz band */
860
861 /* Check whether AP supports HT40 */
862 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
863 if (!ie || ie[1] < 2 ||
864 !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
865 return; /* AP does not support HT40 */
866
867 ie = wpa_bss_get_ie(wpa_s->current_bss,
868 WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
869 if (!ie || ie[1] < 14)
870 return; /* AP does not request OBSS scans */
871
872 wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
873 if (wpa_s->sme.obss_scan_int < 10) {
874 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
875 "replaced with the minimum 10 sec",
876 wpa_s->sme.obss_scan_int);
877 wpa_s->sme.obss_scan_int = 10;
878 }
879 wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
880 wpa_s->sme.obss_scan_int);
881 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
882 sme_obss_scan_timeout, wpa_s, NULL);
883 }
884
885
886 #ifdef CONFIG_IEEE80211W
887
888 static const unsigned int sa_query_max_timeout = 1000;
889 static const unsigned int sa_query_retry_timeout = 201;
890
891 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
892 {
893 u32 tu;
894 struct os_time now, passed;
895 os_get_time(&now);
896 os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
897 tu = (passed.sec * 1000000 + passed.usec) / 1024;
898 if (sa_query_max_timeout < tu) {
899 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
900 sme_stop_sa_query(wpa_s);
901 wpa_supplicant_deauthenticate(
902 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
903 return 1;
904 }
905
906 return 0;
907 }
908
909
910 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
911 const u8 *trans_id)
912 {
913 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
914 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
915 MACSTR, MAC2STR(wpa_s->bssid));
916 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
917 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
918 req[0] = WLAN_ACTION_SA_QUERY;
919 req[1] = WLAN_SA_QUERY_REQUEST;
920 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
921 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
922 wpa_s->own_addr, wpa_s->bssid,
923 req, sizeof(req), 0) < 0)
924 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
925 "Request");
926 }
927
928
929 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
930 {
931 struct wpa_supplicant *wpa_s = eloop_ctx;
932 unsigned int timeout, sec, usec;
933 u8 *trans_id, *nbuf;
934
935 if (wpa_s->sme.sa_query_count > 0 &&
936 sme_check_sa_query_timeout(wpa_s))
937 return;
938
939 nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
940 (wpa_s->sme.sa_query_count + 1) *
941 WLAN_SA_QUERY_TR_ID_LEN);
942 if (nbuf == NULL)
943 return;
944 if (wpa_s->sme.sa_query_count == 0) {
945 /* Starting a new SA Query procedure */
946 os_get_time(&wpa_s->sme.sa_query_start);
947 }
948 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
949 wpa_s->sme.sa_query_trans_id = nbuf;
950 wpa_s->sme.sa_query_count++;
951
952 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
953
954 timeout = sa_query_retry_timeout;
955 sec = ((timeout / 1000) * 1024) / 1000;
956 usec = (timeout % 1000) * 1024;
957 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
958
959 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
960 wpa_s->sme.sa_query_count);
961
962 sme_send_sa_query_req(wpa_s, trans_id);
963 }
964
965
966 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
967 {
968 sme_sa_query_timer(wpa_s, NULL);
969 }
970
971
972 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
973 {
974 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
975 os_free(wpa_s->sme.sa_query_trans_id);
976 wpa_s->sme.sa_query_trans_id = NULL;
977 wpa_s->sme.sa_query_count = 0;
978 }
979
980
981 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
982 const u8 *da, u16 reason_code)
983 {
984 struct wpa_ssid *ssid;
985
986 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
987 return;
988 if (wpa_s->wpa_state != WPA_COMPLETED)
989 return;
990 ssid = wpa_s->current_ssid;
991 if (ssid == NULL || ssid->ieee80211w == 0)
992 return;
993 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
994 return;
995 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
996 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
997 return;
998 if (wpa_s->sme.sa_query_count > 0)
999 return;
1000
1001 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1002 "possible AP/STA state mismatch - trigger SA Query");
1003 sme_start_sa_query(wpa_s);
1004 }
1005
1006
1007 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1008 const u8 *data, size_t len)
1009 {
1010 int i;
1011
1012 if (wpa_s->sme.sa_query_trans_id == NULL ||
1013 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1014 data[0] != WLAN_SA_QUERY_RESPONSE)
1015 return;
1016 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1017 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
1018
1019 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1020 return;
1021
1022 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1023 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1024 i * WLAN_SA_QUERY_TR_ID_LEN,
1025 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1026 break;
1027 }
1028
1029 if (i >= wpa_s->sme.sa_query_count) {
1030 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1031 "transaction identifier found");
1032 return;
1033 }
1034
1035 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1036 "from " MACSTR, MAC2STR(sa));
1037 sme_stop_sa_query(wpa_s);
1038 }
1039
1040 #endif /* CONFIG_IEEE80211W */