Line data Source code
1 : /*
2 : * WPA Supplicant - Driver event processing
3 : * Copyright (c) 2003-2015, 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 "eapol_supp/eapol_supp_sm.h"
13 : #include "rsn_supp/wpa.h"
14 : #include "eloop.h"
15 : #include "config.h"
16 : #include "l2_packet/l2_packet.h"
17 : #include "wpa_supplicant_i.h"
18 : #include "driver_i.h"
19 : #include "pcsc_funcs.h"
20 : #include "rsn_supp/preauth.h"
21 : #include "rsn_supp/pmksa_cache.h"
22 : #include "common/wpa_ctrl.h"
23 : #include "eap_peer/eap.h"
24 : #include "ap/hostapd.h"
25 : #include "p2p/p2p.h"
26 : #include "wnm_sta.h"
27 : #include "notify.h"
28 : #include "common/ieee802_11_defs.h"
29 : #include "common/ieee802_11_common.h"
30 : #include "crypto/random.h"
31 : #include "blacklist.h"
32 : #include "wpas_glue.h"
33 : #include "wps_supplicant.h"
34 : #include "ibss_rsn.h"
35 : #include "sme.h"
36 : #include "gas_query.h"
37 : #include "p2p_supplicant.h"
38 : #include "bgscan.h"
39 : #include "autoscan.h"
40 : #include "ap.h"
41 : #include "bss.h"
42 : #include "scan.h"
43 : #include "offchannel.h"
44 : #include "interworking.h"
45 : #include "mesh.h"
46 : #include "mesh_mpm.h"
47 : #include "wmm_ac.h"
48 :
49 :
50 : #ifndef CONFIG_NO_SCAN_PROCESSING
51 : static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
52 : int new_scan, int own_request);
53 : #endif /* CONFIG_NO_SCAN_PROCESSING */
54 :
55 :
56 3132 : static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
57 : struct wpa_ssid *ssid)
58 : {
59 : struct os_reltime now;
60 :
61 3132 : if (ssid == NULL || ssid->disabled_until.sec == 0)
62 3109 : return 0;
63 :
64 23 : os_get_reltime(&now);
65 23 : if (ssid->disabled_until.sec > now.sec)
66 23 : return ssid->disabled_until.sec - now.sec;
67 :
68 0 : wpas_clear_temp_disabled(wpa_s, ssid, 0);
69 :
70 0 : return 0;
71 : }
72 :
73 :
74 2484 : static struct wpa_bss * wpa_supplicant_get_new_bss(
75 : struct wpa_supplicant *wpa_s, const u8 *bssid)
76 : {
77 2484 : struct wpa_bss *bss = NULL;
78 2484 : struct wpa_ssid *ssid = wpa_s->current_ssid;
79 :
80 2484 : if (ssid->ssid_len > 0)
81 2374 : bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
82 2484 : if (!bss)
83 123 : bss = wpa_bss_get_bssid(wpa_s, bssid);
84 :
85 2484 : return bss;
86 : }
87 :
88 :
89 2471 : static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
90 : {
91 2471 : struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
92 :
93 2471 : if (!bss) {
94 13 : wpa_supplicant_update_scan_results(wpa_s);
95 :
96 : /* Get the BSS from the new scan results */
97 13 : bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
98 : }
99 :
100 2471 : if (bss)
101 2471 : wpa_s->current_bss = bss;
102 2471 : }
103 :
104 :
105 2476 : static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
106 : {
107 : struct wpa_ssid *ssid, *old_ssid;
108 : int res;
109 :
110 2476 : if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
111 2457 : wpa_supplicant_update_current_bss(wpa_s);
112 2457 : return 0;
113 : }
114 :
115 19 : wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
116 : "information");
117 19 : ssid = wpa_supplicant_get_ssid(wpa_s);
118 19 : if (ssid == NULL) {
119 4 : wpa_msg(wpa_s, MSG_INFO,
120 : "No network configuration found for the current AP");
121 4 : return -1;
122 : }
123 :
124 15 : if (wpas_network_disabled(wpa_s, ssid)) {
125 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
126 0 : return -1;
127 : }
128 :
129 29 : if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
130 14 : disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
131 1 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
132 1 : return -1;
133 : }
134 :
135 14 : res = wpas_temp_disabled(wpa_s, ssid);
136 14 : if (res > 0) {
137 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
138 : "disabled for %d second(s)", res);
139 0 : return -1;
140 : }
141 :
142 14 : wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
143 : "current AP");
144 14 : if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
145 : u8 wpa_ie[80];
146 6 : size_t wpa_ie_len = sizeof(wpa_ie);
147 6 : if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
148 : wpa_ie, &wpa_ie_len) < 0)
149 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
150 : } else {
151 8 : wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
152 : }
153 :
154 14 : if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
155 0 : eapol_sm_invalidate_cached_session(wpa_s->eapol);
156 14 : old_ssid = wpa_s->current_ssid;
157 14 : wpa_s->current_ssid = ssid;
158 :
159 14 : wpa_supplicant_update_current_bss(wpa_s);
160 :
161 14 : wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
162 14 : wpa_supplicant_initiate_eapol(wpa_s);
163 14 : if (old_ssid != wpa_s->current_ssid)
164 7 : wpas_notify_network_changed(wpa_s);
165 :
166 14 : return 0;
167 : }
168 :
169 :
170 3171 : void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
171 : {
172 3171 : struct wpa_supplicant *wpa_s = eloop_ctx;
173 :
174 3171 : if (wpa_s->countermeasures) {
175 1 : wpa_s->countermeasures = 0;
176 1 : wpa_drv_set_countermeasures(wpa_s, 0);
177 1 : wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
178 1 : wpa_supplicant_req_scan(wpa_s, 0, 0);
179 : }
180 3171 : }
181 :
182 :
183 4732 : void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
184 : {
185 : int bssid_changed;
186 :
187 4732 : wnm_bss_keep_alive_deinit(wpa_s);
188 :
189 : #ifdef CONFIG_IBSS_RSN
190 4732 : ibss_rsn_deinit(wpa_s->ibss_rsn);
191 4732 : wpa_s->ibss_rsn = NULL;
192 : #endif /* CONFIG_IBSS_RSN */
193 :
194 : #ifdef CONFIG_AP
195 4732 : wpa_supplicant_ap_deinit(wpa_s);
196 : #endif /* CONFIG_AP */
197 :
198 4732 : if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
199 4760 : return;
200 :
201 4704 : wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
202 4704 : bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
203 4704 : os_memset(wpa_s->bssid, 0, ETH_ALEN);
204 4704 : os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
205 4704 : sme_clear_on_disassoc(wpa_s);
206 : #ifdef CONFIG_P2P
207 4704 : os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
208 : #endif /* CONFIG_P2P */
209 4704 : wpa_s->current_bss = NULL;
210 4704 : wpa_s->assoc_freq = 0;
211 :
212 4704 : if (bssid_changed)
213 2222 : wpas_notify_bssid_changed(wpa_s);
214 :
215 4704 : eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
216 4704 : eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
217 4704 : if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
218 683 : eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
219 4704 : wpa_s->ap_ies_from_associnfo = 0;
220 4704 : wpa_s->current_ssid = NULL;
221 4704 : eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
222 4704 : wpa_s->key_mgmt = 0;
223 :
224 4704 : wpas_rrm_reset(wpa_s);
225 : }
226 :
227 :
228 0 : static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
229 : {
230 : struct wpa_ie_data ie;
231 0 : int pmksa_set = -1;
232 : size_t i;
233 :
234 0 : if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
235 0 : ie.pmkid == NULL)
236 0 : return;
237 :
238 0 : for (i = 0; i < ie.num_pmkid; i++) {
239 0 : pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
240 0 : ie.pmkid + i * PMKID_LEN,
241 : NULL, NULL, 0);
242 0 : if (pmksa_set == 0) {
243 0 : eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
244 0 : break;
245 : }
246 : }
247 :
248 0 : wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
249 : "PMKSA cache", pmksa_set == 0 ? "" : "not ");
250 : }
251 :
252 :
253 0 : static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
254 : union wpa_event_data *data)
255 : {
256 0 : if (data == NULL) {
257 0 : wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
258 : "event");
259 0 : return;
260 : }
261 0 : wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
262 : " index=%d preauth=%d",
263 : MAC2STR(data->pmkid_candidate.bssid),
264 : data->pmkid_candidate.index,
265 : data->pmkid_candidate.preauth);
266 :
267 0 : pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
268 : data->pmkid_candidate.index,
269 : data->pmkid_candidate.preauth);
270 : }
271 :
272 :
273 4803 : static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
274 : {
275 8979 : if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
276 4176 : wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
277 632 : return 0;
278 :
279 : #ifdef IEEE8021X_EAPOL
280 4191 : if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
281 40 : wpa_s->current_ssid &&
282 20 : !(wpa_s->current_ssid->eapol_flags &
283 : (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
284 : EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
285 : /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
286 : * plaintext or static WEP keys). */
287 16 : return 0;
288 : }
289 : #endif /* IEEE8021X_EAPOL */
290 :
291 4155 : return 1;
292 : }
293 :
294 :
295 : /**
296 : * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
297 : * @wpa_s: pointer to wpa_supplicant data
298 : * @ssid: Configuration data for the network
299 : * Returns: 0 on success, -1 on failure
300 : *
301 : * This function is called when starting authentication with a network that is
302 : * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
303 : */
304 4913 : int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
305 : struct wpa_ssid *ssid)
306 : {
307 : #ifdef IEEE8021X_EAPOL
308 : #ifdef PCSC_FUNCS
309 : int aka = 0, sim = 0;
310 :
311 : if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
312 : wpa_s->scard != NULL || wpa_s->conf->external_sim)
313 : return 0;
314 :
315 : if (ssid == NULL || ssid->eap.eap_methods == NULL) {
316 : sim = 1;
317 : aka = 1;
318 : } else {
319 : struct eap_method_type *eap = ssid->eap.eap_methods;
320 : while (eap->vendor != EAP_VENDOR_IETF ||
321 : eap->method != EAP_TYPE_NONE) {
322 : if (eap->vendor == EAP_VENDOR_IETF) {
323 : if (eap->method == EAP_TYPE_SIM)
324 : sim = 1;
325 : else if (eap->method == EAP_TYPE_AKA ||
326 : eap->method == EAP_TYPE_AKA_PRIME)
327 : aka = 1;
328 : }
329 : eap++;
330 : }
331 : }
332 :
333 : if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
334 : sim = 0;
335 : if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
336 : eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
337 : NULL)
338 : aka = 0;
339 :
340 : if (!sim && !aka) {
341 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
342 : "use SIM, but neither EAP-SIM nor EAP-AKA are "
343 : "enabled");
344 : return 0;
345 : }
346 :
347 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
348 : "(sim=%d aka=%d) - initialize PCSC", sim, aka);
349 :
350 : wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
351 : if (wpa_s->scard == NULL) {
352 : wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
353 : "(pcsc-lite)");
354 : return -1;
355 : }
356 : wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
357 : eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
358 : #endif /* PCSC_FUNCS */
359 : #endif /* IEEE8021X_EAPOL */
360 :
361 4913 : return 0;
362 : }
363 :
364 :
365 : #ifndef CONFIG_NO_SCAN_PROCESSING
366 :
367 2524 : static int has_wep_key(struct wpa_ssid *ssid)
368 : {
369 : int i;
370 :
371 12512 : for (i = 0; i < NUM_WEP_KEYS; i++) {
372 10015 : if (ssid->wep_key_len[i])
373 27 : return 1;
374 : }
375 :
376 2497 : return 0;
377 : }
378 :
379 :
380 2539 : static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
381 : struct wpa_ssid *ssid)
382 : {
383 2539 : int privacy = 0;
384 :
385 2539 : if (ssid->mixed_cell)
386 0 : return 1;
387 :
388 : #ifdef CONFIG_WPS
389 2539 : if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
390 283 : return 1;
391 : #endif /* CONFIG_WPS */
392 :
393 2256 : if (has_wep_key(ssid))
394 27 : privacy = 1;
395 :
396 : #ifdef IEEE8021X_EAPOL
397 2266 : if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
398 10 : ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
399 : EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
400 2 : privacy = 1;
401 : #endif /* IEEE8021X_EAPOL */
402 :
403 2256 : if (wpa_key_mgmt_wpa(ssid->key_mgmt))
404 1886 : privacy = 1;
405 :
406 2256 : if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
407 2 : privacy = 1;
408 :
409 2256 : if (bss->caps & IEEE80211_CAP_PRIVACY)
410 1916 : return privacy;
411 340 : return !privacy;
412 : }
413 :
414 :
415 2637 : static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
416 : struct wpa_ssid *ssid,
417 : struct wpa_bss *bss)
418 : {
419 : struct wpa_ie_data ie;
420 2637 : int proto_match = 0;
421 : const u8 *rsn_ie, *wpa_ie;
422 : int ret;
423 : int wep_ok;
424 :
425 2637 : ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
426 2637 : if (ret >= 0)
427 305 : return ret;
428 :
429 : /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
430 2738 : wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
431 730 : (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
432 704 : ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
433 344 : (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
434 :
435 2332 : rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
436 2332 : while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
437 1867 : proto_match++;
438 :
439 1867 : if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
440 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
441 : "failed");
442 0 : break;
443 : }
444 :
445 1867 : if (wep_ok &&
446 0 : (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
447 : {
448 0 : wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
449 : "in RSN IE");
450 0 : return 1;
451 : }
452 :
453 1867 : if (!(ie.proto & ssid->proto)) {
454 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
455 : "mismatch");
456 0 : break;
457 : }
458 :
459 1867 : if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
460 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
461 : "cipher mismatch");
462 0 : break;
463 : }
464 :
465 1867 : if (!(ie.group_cipher & ssid->group_cipher)) {
466 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
467 : "cipher mismatch");
468 0 : break;
469 : }
470 :
471 1867 : if (!(ie.key_mgmt & ssid->key_mgmt)) {
472 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
473 : "mismatch");
474 0 : break;
475 : }
476 :
477 : #ifdef CONFIG_IEEE80211W
478 3592 : if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
479 1725 : wpas_get_ssid_pmf(wpa_s, ssid) ==
480 : MGMT_FRAME_PROTECTION_REQUIRED) {
481 2 : wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
482 : "frame protection");
483 2 : break;
484 : }
485 : #endif /* CONFIG_IEEE80211W */
486 :
487 1865 : wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
488 1865 : return 1;
489 : }
490 :
491 467 : wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
492 467 : while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
493 19 : proto_match++;
494 :
495 19 : if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
496 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
497 : "failed");
498 0 : break;
499 : }
500 :
501 19 : if (wep_ok &&
502 0 : (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
503 : {
504 0 : wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
505 : "in WPA IE");
506 0 : return 1;
507 : }
508 :
509 19 : if (!(ie.proto & ssid->proto)) {
510 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
511 : "mismatch");
512 0 : break;
513 : }
514 :
515 19 : if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
516 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
517 : "cipher mismatch");
518 0 : break;
519 : }
520 :
521 19 : if (!(ie.group_cipher & ssid->group_cipher)) {
522 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
523 : "cipher mismatch");
524 0 : break;
525 : }
526 :
527 19 : if (!(ie.key_mgmt & ssid->key_mgmt)) {
528 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
529 : "mismatch");
530 0 : break;
531 : }
532 :
533 19 : wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
534 19 : return 1;
535 : }
536 :
537 448 : if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
538 : !rsn_ie) {
539 10 : wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
540 10 : return 1;
541 : }
542 :
543 873 : if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
544 511 : wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
545 74 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
546 74 : return 0;
547 : }
548 :
549 366 : if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
550 2 : wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
551 2 : wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
552 2 : return 1;
553 : }
554 :
555 362 : if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
556 360 : wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
557 360 : return 1;
558 : }
559 :
560 2 : wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
561 : "WPA/WPA2");
562 :
563 2 : return 0;
564 : }
565 :
566 :
567 2535 : static int freq_allowed(int *freqs, int freq)
568 : {
569 : int i;
570 :
571 2535 : if (freqs == NULL)
572 2535 : return 1;
573 :
574 0 : for (i = 0; freqs[i]; i++)
575 0 : if (freqs[i] == freq)
576 0 : return 1;
577 0 : return 0;
578 : }
579 :
580 :
581 2535 : static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
582 : {
583 2535 : const struct hostapd_hw_modes *mode = NULL, *modes;
584 2535 : const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
585 : const u8 *rate_ie;
586 : int i, j, k;
587 :
588 2535 : if (bss->freq == 0)
589 0 : return 1; /* Cannot do matching without knowing band */
590 :
591 2535 : modes = wpa_s->hw.modes;
592 2535 : if (modes == NULL) {
593 : /*
594 : * The driver does not provide any additional information
595 : * about the utilized hardware, so allow the connection attempt
596 : * to continue.
597 : */
598 15 : return 1;
599 : }
600 :
601 10080 : for (i = 0; i < wpa_s->hw.num_modes; i++) {
602 73469 : for (j = 0; j < modes[i].num_channels; j++) {
603 70903 : int freq = modes[i].channels[j].freq;
604 70903 : if (freq == bss->freq) {
605 7468 : if (mode &&
606 2474 : mode->mode == HOSTAPD_MODE_IEEE80211G)
607 2474 : break; /* do not allow 802.11b replace
608 : * 802.11g */
609 2520 : mode = &modes[i];
610 2520 : break;
611 : }
612 : }
613 : }
614 :
615 2520 : if (mode == NULL)
616 0 : return 0;
617 :
618 7560 : for (i = 0; i < (int) sizeof(scan_ie); i++) {
619 5040 : rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
620 5040 : if (rate_ie == NULL)
621 576 : continue;
622 :
623 32373 : for (j = 2; j < rate_ie[1] + 2; j++) {
624 27909 : int flagged = !!(rate_ie[j] & 0x80);
625 27909 : int r = (rate_ie[j] & 0x7f) * 5;
626 :
627 : /*
628 : * IEEE Std 802.11n-2009 7.3.2.2:
629 : * The new BSS Membership selector value is encoded
630 : * like a legacy basic rate, but it is not a rate and
631 : * only indicates if the BSS members are required to
632 : * support the mandatory features of Clause 20 [HT PHY]
633 : * in order to join the BSS.
634 : */
635 27909 : if (flagged && ((rate_ie[j] & 0x7f) ==
636 : BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
637 8 : if (!ht_supported(mode)) {
638 0 : wpa_dbg(wpa_s, MSG_DEBUG,
639 : " hardware does not support "
640 : "HT PHY");
641 0 : return 0;
642 : }
643 8 : continue;
644 : }
645 :
646 : /* There's also a VHT selector for 802.11ac */
647 27901 : if (flagged && ((rate_ie[j] & 0x7f) ==
648 : BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
649 3 : if (!vht_supported(mode)) {
650 0 : wpa_dbg(wpa_s, MSG_DEBUG,
651 : " hardware does not support "
652 : "VHT PHY");
653 0 : return 0;
654 : }
655 3 : continue;
656 : }
657 :
658 27898 : if (!flagged)
659 18400 : continue;
660 :
661 : /* check for legacy basic rates */
662 30924 : for (k = 0; k < mode->num_rates; k++) {
663 30924 : if (mode->rates[k] == r)
664 9498 : break;
665 : }
666 9498 : if (k == mode->num_rates) {
667 : /*
668 : * IEEE Std 802.11-2007 7.3.2.2 demands that in
669 : * order to join a BSS all required rates
670 : * have to be supported by the hardware.
671 : */
672 0 : wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
673 : "not support required rate %d.%d Mbps",
674 : r / 10, r % 10);
675 0 : return 0;
676 : }
677 : }
678 : }
679 :
680 2520 : return 1;
681 : }
682 :
683 :
684 : /*
685 : * Test whether BSS is in an ESS.
686 : * This is done differently in DMG (60 GHz) and non-DMG bands
687 : */
688 2538 : static int bss_is_ess(struct wpa_bss *bss)
689 : {
690 2538 : if (bss_is_dmg(bss)) {
691 0 : return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
692 : IEEE80211_CAP_DMG_AP;
693 : }
694 :
695 2538 : return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
696 : IEEE80211_CAP_ESS);
697 : }
698 :
699 :
700 19 : static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
701 : {
702 : size_t i;
703 :
704 59 : for (i = 0; i < ETH_ALEN; i++) {
705 55 : if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
706 15 : return 0;
707 : }
708 4 : return 1;
709 : }
710 :
711 :
712 13 : static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
713 : {
714 : size_t i;
715 :
716 28 : for (i = 0; i < num; i++) {
717 19 : const u8 *a = list + i * ETH_ALEN * 2;
718 19 : const u8 *m = a + ETH_ALEN;
719 :
720 19 : if (match_mac_mask(a, addr, m))
721 4 : return 1;
722 : }
723 9 : return 0;
724 : }
725 :
726 :
727 4176 : static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
728 : int i, struct wpa_bss *bss,
729 : struct wpa_ssid *group,
730 : int only_first_ssid)
731 : {
732 : u8 wpa_ie_len, rsn_ie_len;
733 : int wpa;
734 : struct wpa_blacklist *e;
735 : const u8 *ie;
736 : struct wpa_ssid *ssid;
737 : int osen;
738 :
739 4176 : ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
740 4176 : wpa_ie_len = ie ? ie[1] : 0;
741 :
742 4176 : ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
743 4176 : rsn_ie_len = ie ? ie[1] : 0;
744 :
745 4176 : ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
746 4176 : osen = ie != NULL;
747 :
748 4176 : wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
749 : "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
750 : i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
751 : wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
752 : wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
753 : (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
754 : wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
755 : " p2p" : "",
756 : osen ? " osen=1" : "");
757 :
758 4176 : e = wpa_blacklist_get(wpa_s, bss->bssid);
759 4176 : if (e) {
760 1061 : int limit = 1;
761 1061 : if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
762 : /*
763 : * When only a single network is enabled, we can
764 : * trigger blacklisting on the first failure. This
765 : * should not be done with multiple enabled networks to
766 : * avoid getting forced to move into a worse ESS on
767 : * single error if there are no other BSSes of the
768 : * current ESS.
769 : */
770 1054 : limit = 0;
771 : }
772 1061 : if (e->count > limit) {
773 1054 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
774 : "(count=%d limit=%d)", e->count, limit);
775 1054 : return NULL;
776 : }
777 : }
778 :
779 3122 : if (bss->ssid_len == 0) {
780 7 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
781 7 : return NULL;
782 : }
783 :
784 3115 : if (disallowed_bssid(wpa_s, bss->bssid)) {
785 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
786 0 : return NULL;
787 : }
788 :
789 3115 : if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
790 2 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
791 2 : return NULL;
792 : }
793 :
794 3113 : wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
795 :
796 5574 : for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
797 4996 : int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
798 : int res;
799 :
800 4996 : if (wpas_network_disabled(wpa_s, ssid)) {
801 1881 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
802 1881 : continue;
803 : }
804 :
805 3115 : res = wpas_temp_disabled(wpa_s, ssid);
806 3115 : if (res > 0) {
807 22 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
808 : "temporarily for %d second(s)", res);
809 22 : continue;
810 : }
811 :
812 : #ifdef CONFIG_WPS
813 3093 : if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
814 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
815 : "(WPS)");
816 0 : continue;
817 : }
818 :
819 3198 : if (wpa && ssid->ssid_len == 0 &&
820 105 : wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
821 104 : check_ssid = 0;
822 :
823 3093 : if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
824 : /* Only allow wildcard SSID match if an AP
825 : * advertises active WPS operation that matches
826 : * with our mode. */
827 48 : check_ssid = 1;
828 90 : if (ssid->ssid_len == 0 &&
829 42 : wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
830 14 : check_ssid = 0;
831 : }
832 : #endif /* CONFIG_WPS */
833 :
834 3192 : if (ssid->bssid_set && ssid->ssid_len == 0 &&
835 99 : os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
836 99 : check_ssid = 0;
837 :
838 6068 : if (check_ssid &&
839 5768 : (bss->ssid_len != ssid->ssid_len ||
840 2793 : os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
841 443 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
842 443 : continue;
843 : }
844 :
845 3248 : if (ssid->bssid_set &&
846 598 : os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
847 6 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
848 6 : continue;
849 : }
850 :
851 : /* check blacklist */
852 2648 : if (ssid->num_bssid_blacklist &&
853 4 : addr_in_list(bss->bssid, ssid->bssid_blacklist,
854 : ssid->num_bssid_blacklist)) {
855 1 : wpa_dbg(wpa_s, MSG_DEBUG,
856 : " skip - BSSID blacklisted");
857 1 : continue;
858 : }
859 :
860 : /* if there is a whitelist, only accept those APs */
861 2652 : if (ssid->num_bssid_whitelist &&
862 9 : !addr_in_list(bss->bssid, ssid->bssid_whitelist,
863 : ssid->num_bssid_whitelist)) {
864 6 : wpa_dbg(wpa_s, MSG_DEBUG,
865 : " skip - BSSID not in whitelist");
866 6 : continue;
867 : }
868 :
869 2637 : if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
870 98 : continue;
871 :
872 2921 : if (!osen && !wpa &&
873 407 : !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
874 35 : !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
875 10 : !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
876 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
877 : "not allowed");
878 0 : continue;
879 : }
880 :
881 2807 : if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
882 268 : has_wep_key(ssid)) {
883 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
884 0 : continue;
885 : }
886 :
887 2539 : if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
888 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
889 : "not allowed");
890 0 : continue;
891 : }
892 :
893 2539 : if (!wpa_supplicant_match_privacy(bss, ssid)) {
894 1 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
895 : "mismatch");
896 1 : continue;
897 : }
898 :
899 2538 : if (!bss_is_ess(bss)) {
900 3 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
901 3 : continue;
902 : }
903 :
904 2535 : if (!freq_allowed(ssid->freq_list, bss->freq)) {
905 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
906 : "allowed");
907 0 : continue;
908 : }
909 :
910 2535 : if (!rate_match(wpa_s, bss)) {
911 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
912 : "not match");
913 0 : continue;
914 : }
915 :
916 : #ifdef CONFIG_P2P
917 3053 : if (ssid->p2p_group &&
918 518 : !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
919 0 : !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
920 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
921 0 : continue;
922 : }
923 :
924 2535 : if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
925 : struct wpabuf *p2p_ie;
926 : u8 dev_addr[ETH_ALEN];
927 :
928 45 : ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
929 45 : if (ie == NULL) {
930 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
931 0 : continue;
932 : }
933 45 : p2p_ie = wpa_bss_get_vendor_ie_multi(
934 : bss, P2P_IE_VENDOR_TYPE);
935 45 : if (p2p_ie == NULL) {
936 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
937 0 : continue;
938 : }
939 :
940 45 : if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
941 45 : || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
942 : ETH_ALEN) != 0) {
943 0 : wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
944 0 : wpabuf_free(p2p_ie);
945 0 : continue;
946 : }
947 45 : wpabuf_free(p2p_ie);
948 : }
949 :
950 : /*
951 : * TODO: skip the AP if its P2P IE has Group Formation
952 : * bit set in the P2P Group Capability Bitmap and we
953 : * are not in Group Formation with that device.
954 : */
955 : #endif /* CONFIG_P2P */
956 :
957 : /* Matching configuration found */
958 2535 : return ssid;
959 : }
960 :
961 : /* No matching configuration found */
962 578 : return NULL;
963 : }
964 :
965 :
966 : static struct wpa_bss *
967 3753 : wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
968 : struct wpa_ssid *group,
969 : struct wpa_ssid **selected_ssid,
970 : int only_first_ssid)
971 : {
972 : unsigned int i;
973 :
974 3753 : if (only_first_ssid)
975 99 : wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
976 : group->id);
977 : else
978 3654 : wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
979 : group->priority);
980 :
981 10788 : for (i = 0; i < wpa_s->last_scan_res_used; i++) {
982 4176 : struct wpa_bss *bss = wpa_s->last_scan_res[i];
983 4176 : *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
984 : only_first_ssid);
985 4176 : if (!*selected_ssid)
986 1641 : continue;
987 2535 : wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
988 : " ssid='%s'",
989 : MAC2STR(bss->bssid),
990 : wpa_ssid_txt(bss->ssid, bss->ssid_len));
991 2535 : return bss;
992 : }
993 :
994 1218 : return NULL;
995 : }
996 :
997 :
998 2952 : struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
999 : struct wpa_ssid **selected_ssid)
1000 : {
1001 2952 : struct wpa_bss *selected = NULL;
1002 : int prio;
1003 2952 : struct wpa_ssid *next_ssid = NULL;
1004 :
1005 5889 : if (wpa_s->last_scan_res == NULL ||
1006 2937 : wpa_s->last_scan_res_used == 0)
1007 138 : return NULL; /* no scan results from last update */
1008 :
1009 2814 : if (wpa_s->next_ssid) {
1010 : struct wpa_ssid *ssid;
1011 :
1012 : /* check that next_ssid is still valid */
1013 100 : for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1014 100 : if (ssid == wpa_s->next_ssid)
1015 90 : break;
1016 : }
1017 90 : next_ssid = ssid;
1018 90 : wpa_s->next_ssid = NULL;
1019 : }
1020 :
1021 9172 : while (selected == NULL) {
1022 5031 : for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1023 3844 : if (next_ssid && next_ssid->priority ==
1024 101 : wpa_s->conf->pssid[prio]->priority) {
1025 99 : selected = wpa_supplicant_select_bss(
1026 : wpa_s, next_ssid, selected_ssid, 1);
1027 99 : if (selected)
1028 89 : break;
1029 : }
1030 3654 : selected = wpa_supplicant_select_bss(
1031 3654 : wpa_s, wpa_s->conf->pssid[prio],
1032 : selected_ssid, 0);
1033 3654 : if (selected)
1034 2446 : break;
1035 : }
1036 :
1037 4833 : if (selected == NULL && wpa_s->blacklist &&
1038 1010 : !wpa_s->countermeasures) {
1039 1009 : wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1040 : "blacklist and try again");
1041 1009 : wpa_blacklist_clear(wpa_s);
1042 1009 : wpa_s->blacklist_cleared++;
1043 2814 : } else if (selected == NULL)
1044 279 : break;
1045 : }
1046 :
1047 2814 : return selected;
1048 : }
1049 :
1050 :
1051 316 : static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1052 : int timeout_sec, int timeout_usec)
1053 : {
1054 316 : if (!wpa_supplicant_enabled_networks(wpa_s)) {
1055 : /*
1056 : * No networks are enabled; short-circuit request so
1057 : * we don't wait timeout seconds before transitioning
1058 : * to INACTIVE state.
1059 : */
1060 118 : wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1061 : "since there are no enabled networks");
1062 118 : wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1063 434 : return;
1064 : }
1065 :
1066 198 : wpa_s->scan_for_connection = 1;
1067 198 : wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1068 : }
1069 :
1070 :
1071 2416 : int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
1072 : struct wpa_bss *selected,
1073 : struct wpa_ssid *ssid)
1074 : {
1075 2416 : if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1076 3 : wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1077 : "PBC session overlap");
1078 : #ifdef CONFIG_P2P
1079 5 : if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1080 2 : wpa_s->p2p_in_provisioning) {
1081 2 : eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1082 : wpa_s, NULL);
1083 2 : return -1;
1084 : }
1085 : #endif /* CONFIG_P2P */
1086 :
1087 : #ifdef CONFIG_WPS
1088 1 : wpas_wps_cancel(wpa_s);
1089 : #endif /* CONFIG_WPS */
1090 1 : return -1;
1091 : }
1092 :
1093 45847 : wpa_msg(wpa_s, MSG_DEBUG,
1094 : "Considering connect request: reassociate: %d selected: "
1095 : MACSTR " bssid: " MACSTR " pending: " MACSTR
1096 : " wpa_state: %s ssid=%p current_ssid=%p",
1097 14478 : wpa_s->reassociate, MAC2STR(selected->bssid),
1098 28956 : MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1099 : wpa_supplicant_state_txt(wpa_s->wpa_state),
1100 : ssid, wpa_s->current_ssid);
1101 :
1102 : /*
1103 : * Do not trigger new association unless the BSSID has changed or if
1104 : * reassociation is requested. If we are in process of associating with
1105 : * the selected BSSID, do not trigger new attempt.
1106 : */
1107 2514 : if (wpa_s->reassociate ||
1108 202 : (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1109 202 : ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1110 102 : wpa_s->wpa_state != WPA_AUTHENTICATING) ||
1111 2 : (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1112 1 : os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1113 1 : 0) ||
1114 1 : (is_zero_ether_addr(wpa_s->pending_bssid) &&
1115 0 : ssid != wpa_s->current_ssid)))) {
1116 2412 : if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1117 0 : wpa_supplicant_req_new_scan(wpa_s, 10, 0);
1118 0 : return 0;
1119 : }
1120 14472 : wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1121 14472 : MAC2STR(selected->bssid));
1122 2412 : wpa_supplicant_associate(wpa_s, selected, ssid);
1123 : } else {
1124 1 : wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1125 : "connect with the selected AP");
1126 : }
1127 :
1128 2413 : return 0;
1129 : }
1130 :
1131 :
1132 : static struct wpa_ssid *
1133 402 : wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1134 : {
1135 : int prio;
1136 : struct wpa_ssid *ssid;
1137 :
1138 660 : for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1139 573 : for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1140 : {
1141 315 : if (wpas_network_disabled(wpa_s, ssid))
1142 30 : continue;
1143 559 : if (ssid->mode == IEEE80211_MODE_IBSS ||
1144 535 : ssid->mode == IEEE80211_MODE_AP ||
1145 261 : ssid->mode == IEEE80211_MODE_MESH)
1146 24 : return ssid;
1147 : }
1148 : }
1149 378 : return NULL;
1150 : }
1151 :
1152 :
1153 : /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1154 : * on BSS added and BSS changed events */
1155 1122 : static void wpa_supplicant_rsn_preauth_scan_results(
1156 : struct wpa_supplicant *wpa_s)
1157 : {
1158 : struct wpa_bss *bss;
1159 :
1160 1122 : if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1161 2073 : return;
1162 :
1163 388 : dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1164 : const u8 *ssid, *rsn;
1165 :
1166 217 : ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1167 217 : if (ssid == NULL)
1168 0 : continue;
1169 :
1170 217 : rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1171 217 : if (rsn == NULL)
1172 69 : continue;
1173 :
1174 148 : rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1175 : }
1176 :
1177 : }
1178 :
1179 :
1180 2303 : static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1181 : struct wpa_bss *selected,
1182 : struct wpa_ssid *ssid)
1183 : {
1184 2303 : struct wpa_bss *current_bss = NULL;
1185 : int min_diff;
1186 :
1187 2303 : if (wpa_s->reassociate)
1188 2162 : return 1; /* explicit request to reassociate */
1189 141 : if (wpa_s->wpa_state < WPA_ASSOCIATED)
1190 98 : return 1; /* we are not associated; continue */
1191 43 : if (wpa_s->current_ssid == NULL)
1192 0 : return 1; /* unknown current SSID */
1193 43 : if (wpa_s->current_ssid != ssid)
1194 0 : return 1; /* different network block */
1195 :
1196 43 : if (wpas_driver_bss_selection(wpa_s))
1197 0 : return 0; /* Driver-based roaming */
1198 :
1199 43 : if (wpa_s->current_ssid->ssid)
1200 86 : current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1201 43 : wpa_s->current_ssid->ssid,
1202 43 : wpa_s->current_ssid->ssid_len);
1203 43 : if (!current_bss)
1204 0 : current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1205 :
1206 43 : if (!current_bss)
1207 0 : return 1; /* current BSS not seen in scan results */
1208 :
1209 43 : if (current_bss == selected)
1210 32 : return 0;
1211 :
1212 11 : if (selected->last_update_idx > current_bss->last_update_idx)
1213 0 : return 1; /* current BSS not seen in the last scan */
1214 :
1215 : #ifndef CONFIG_NO_ROAMING
1216 11 : wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1217 11 : wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1218 : MAC2STR(current_bss->bssid), current_bss->level);
1219 11 : wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1220 : MAC2STR(selected->bssid), selected->level);
1221 :
1222 11 : if (wpa_s->current_ssid->bssid_set &&
1223 0 : os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1224 : 0) {
1225 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1226 : "has preferred BSSID");
1227 0 : return 1;
1228 : }
1229 :
1230 11 : if (current_bss->level < 0 && current_bss->level > selected->level) {
1231 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1232 : "signal level");
1233 0 : return 0;
1234 : }
1235 :
1236 11 : min_diff = 2;
1237 11 : if (current_bss->level < 0) {
1238 11 : if (current_bss->level < -85)
1239 0 : min_diff = 1;
1240 11 : else if (current_bss->level < -80)
1241 0 : min_diff = 2;
1242 11 : else if (current_bss->level < -75)
1243 0 : min_diff = 3;
1244 11 : else if (current_bss->level < -70)
1245 0 : min_diff = 4;
1246 : else
1247 11 : min_diff = 5;
1248 : }
1249 11 : if (abs(current_bss->level - selected->level) < min_diff) {
1250 11 : wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1251 : "in signal level");
1252 11 : return 0;
1253 : }
1254 :
1255 0 : return 1;
1256 : #else /* CONFIG_NO_ROAMING */
1257 : return 0;
1258 : #endif /* CONFIG_NO_ROAMING */
1259 : }
1260 :
1261 :
1262 : /* Return != 0 if no scan results could be fetched or if scan results should not
1263 : * be shared with other virtual interfaces. */
1264 2763 : static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1265 : union wpa_event_data *data,
1266 : int own_request)
1267 : {
1268 2763 : struct wpa_scan_results *scan_res = NULL;
1269 2763 : int ret = 0;
1270 2763 : int ap = 0;
1271 : #ifndef CONFIG_NO_RANDOM_POOL
1272 : size_t i, num;
1273 : #endif /* CONFIG_NO_RANDOM_POOL */
1274 :
1275 : #ifdef CONFIG_AP
1276 2763 : if (wpa_s->ap_iface)
1277 1 : ap = 1;
1278 : #endif /* CONFIG_AP */
1279 :
1280 2763 : wpa_supplicant_notify_scanning(wpa_s, 0);
1281 :
1282 2763 : scan_res = wpa_supplicant_get_scan_results(wpa_s,
1283 : data ? &data->scan_info :
1284 : NULL, 1);
1285 2763 : if (scan_res == NULL) {
1286 0 : if (wpa_s->conf->ap_scan == 2 || ap ||
1287 0 : wpa_s->scan_res_handler == scan_only_handler)
1288 0 : return -1;
1289 0 : if (!own_request)
1290 0 : return -1;
1291 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1292 : "scanning again");
1293 0 : wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1294 0 : ret = -1;
1295 0 : goto scan_work_done;
1296 : }
1297 :
1298 : #ifndef CONFIG_NO_RANDOM_POOL
1299 : num = scan_res->num;
1300 : if (num > 10)
1301 : num = 10;
1302 : for (i = 0; i < num; i++) {
1303 : u8 buf[5];
1304 : struct wpa_scan_res *res = scan_res->res[i];
1305 : buf[0] = res->bssid[5];
1306 : buf[1] = res->qual & 0xff;
1307 : buf[2] = res->noise & 0xff;
1308 : buf[3] = res->level & 0xff;
1309 : buf[4] = res->tsf & 0xff;
1310 : random_add_randomness(buf, sizeof(buf));
1311 : }
1312 : #endif /* CONFIG_NO_RANDOM_POOL */
1313 :
1314 3970 : if (own_request && wpa_s->scan_res_handler &&
1315 1207 : (wpa_s->own_scan_running || !wpa_s->radio->external_scan_running)) {
1316 : void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1317 : struct wpa_scan_results *scan_res);
1318 :
1319 1207 : scan_res_handler = wpa_s->scan_res_handler;
1320 1207 : wpa_s->scan_res_handler = NULL;
1321 1207 : scan_res_handler(wpa_s, scan_res);
1322 1207 : ret = -2;
1323 1207 : goto scan_work_done;
1324 : }
1325 :
1326 1556 : if (ap) {
1327 1 : wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1328 : #ifdef CONFIG_AP
1329 1 : if (wpa_s->ap_iface->scan_cb)
1330 1 : wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1331 : #endif /* CONFIG_AP */
1332 1 : goto scan_work_done;
1333 : }
1334 :
1335 1555 : wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1336 : wpa_s->own_scan_running, wpa_s->radio->external_scan_running);
1337 1555 : if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1338 13 : wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1339 12 : wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1340 : wpa_s->manual_scan_id);
1341 12 : wpa_s->manual_scan_use_id = 0;
1342 : } else {
1343 1543 : wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1344 : }
1345 1555 : wpas_notify_scan_results(wpa_s);
1346 :
1347 1555 : wpas_notify_scan_done(wpa_s, 1);
1348 :
1349 1555 : if (!wpa_s->own_scan_running && wpa_s->radio->external_scan_running) {
1350 12 : wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
1351 12 : wpa_scan_results_free(scan_res);
1352 12 : return 0;
1353 : }
1354 :
1355 1543 : if (wnm_scan_process(wpa_s, 1) > 0)
1356 0 : goto scan_work_done;
1357 :
1358 1543 : if (sme_proc_obss_scan(wpa_s) > 0)
1359 4 : goto scan_work_done;
1360 :
1361 1539 : if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1362 0 : goto scan_work_done;
1363 :
1364 1539 : if (autoscan_notify_scan(wpa_s, scan_res))
1365 0 : goto scan_work_done;
1366 :
1367 1539 : if (wpa_s->disconnected) {
1368 64 : wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1369 64 : goto scan_work_done;
1370 : }
1371 :
1372 2949 : if (!wpas_driver_bss_selection(wpa_s) &&
1373 1474 : bgscan_notify_scan(wpa_s, scan_res) == 1)
1374 0 : goto scan_work_done;
1375 :
1376 1475 : wpas_wps_update_ap_info(wpa_s, scan_res);
1377 :
1378 1475 : wpa_scan_results_free(scan_res);
1379 :
1380 1475 : if (wpa_s->scan_work) {
1381 1450 : struct wpa_radio_work *work = wpa_s->scan_work;
1382 1450 : wpa_s->scan_work = NULL;
1383 1450 : radio_work_done(work);
1384 : }
1385 :
1386 1475 : return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1387 :
1388 : scan_work_done:
1389 1276 : wpa_scan_results_free(scan_res);
1390 1276 : if (wpa_s->scan_work) {
1391 203 : struct wpa_radio_work *work = wpa_s->scan_work;
1392 203 : wpa_s->scan_work = NULL;
1393 203 : radio_work_done(work);
1394 : }
1395 1276 : return ret;
1396 : }
1397 :
1398 :
1399 2708 : static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1400 : int new_scan, int own_request)
1401 : {
1402 : struct wpa_bss *selected;
1403 2708 : struct wpa_ssid *ssid = NULL;
1404 :
1405 2708 : selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1406 :
1407 2708 : if (selected) {
1408 : int skip;
1409 2303 : skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1410 2303 : if (skip) {
1411 43 : if (new_scan)
1412 43 : wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1413 43 : return 0;
1414 : }
1415 :
1416 2260 : if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1417 3 : wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1418 3 : return -1;
1419 : }
1420 2257 : if (new_scan)
1421 1055 : wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1422 : /*
1423 : * Do not notify other virtual radios of scan results since we do not
1424 : * want them to start other associations at the same time.
1425 : */
1426 2257 : return 1;
1427 : } else {
1428 : #ifdef CONFIG_MESH
1429 405 : if (wpa_s->ifmsh) {
1430 3 : wpa_msg(wpa_s, MSG_INFO,
1431 : "Avoiding join because we already joined a mesh group");
1432 3 : return 0;
1433 : }
1434 : #endif /* CONFIG_MESH */
1435 402 : wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1436 402 : ssid = wpa_supplicant_pick_new_network(wpa_s);
1437 402 : if (ssid) {
1438 24 : wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1439 24 : wpa_supplicant_associate(wpa_s, NULL, ssid);
1440 24 : if (new_scan)
1441 24 : wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1442 378 : } else if (own_request) {
1443 : /*
1444 : * No SSID found. If SCAN results are as a result of
1445 : * own scan request and not due to a scan request on
1446 : * another shared interface, try another scan.
1447 : */
1448 370 : int timeout_sec = wpa_s->scan_interval;
1449 370 : int timeout_usec = 0;
1450 : #ifdef CONFIG_P2P
1451 370 : if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1452 0 : return 0;
1453 :
1454 686 : if (wpa_s->p2p_in_provisioning ||
1455 630 : wpa_s->show_group_started ||
1456 314 : wpa_s->p2p_in_invitation) {
1457 : /*
1458 : * Use shorter wait during P2P Provisioning
1459 : * state and during P2P join-a-group operation
1460 : * to speed up group formation.
1461 : */
1462 56 : timeout_sec = 0;
1463 56 : timeout_usec = 250000;
1464 56 : wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1465 : timeout_usec);
1466 56 : return 0;
1467 : }
1468 : #endif /* CONFIG_P2P */
1469 : #ifdef CONFIG_INTERWORKING
1470 368 : if (wpa_s->conf->auto_interworking &&
1471 108 : wpa_s->conf->interworking &&
1472 54 : wpa_s->conf->cred) {
1473 54 : wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1474 : "start ANQP fetch since no matching "
1475 : "networks found");
1476 54 : wpa_s->network_select = 1;
1477 54 : wpa_s->auto_network_select = 1;
1478 54 : interworking_start_fetch_anqp(wpa_s);
1479 54 : return 1;
1480 : }
1481 : #endif /* CONFIG_INTERWORKING */
1482 : #ifdef CONFIG_WPS
1483 260 : if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1484 49 : wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1485 49 : timeout_sec = 0;
1486 49 : timeout_usec = 500000;
1487 49 : wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1488 : timeout_usec);
1489 49 : return 0;
1490 : }
1491 : #endif /* CONFIG_WPS */
1492 211 : if (wpa_supplicant_req_sched_scan(wpa_s))
1493 211 : wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1494 : timeout_usec);
1495 : }
1496 : }
1497 243 : return 0;
1498 : }
1499 :
1500 :
1501 2755 : static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1502 : union wpa_event_data *data)
1503 : {
1504 : struct wpa_supplicant *ifs;
1505 :
1506 2755 : if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
1507 : /*
1508 : * If no scan results could be fetched, then no need to
1509 : * notify those interfaces that did not actually request
1510 : * this scan. Similarly, if scan results started a new operation on this
1511 : * interface, do not notify other interfaces to avoid concurrent
1512 : * operations during a connection attempt.
1513 : */
1514 5048 : return;
1515 : }
1516 :
1517 : /*
1518 : * Check other interfaces to see if they share the same radio. If
1519 : * so, they get updated with this same scan info.
1520 : */
1521 932 : dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1522 : radio_list) {
1523 470 : if (ifs != wpa_s) {
1524 8 : wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1525 8 : "sibling", ifs->ifname);
1526 8 : _wpa_supplicant_event_scan_results(ifs, data, 0);
1527 : }
1528 : }
1529 : }
1530 :
1531 : #endif /* CONFIG_NO_SCAN_PROCESSING */
1532 :
1533 :
1534 1967 : int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1535 : {
1536 : #ifdef CONFIG_NO_SCAN_PROCESSING
1537 : return -1;
1538 : #else /* CONFIG_NO_SCAN_PROCESSING */
1539 : struct os_reltime now;
1540 :
1541 1967 : if (wpa_s->last_scan_res_used <= 0)
1542 700 : return -1;
1543 :
1544 1267 : os_get_reltime(&now);
1545 1267 : if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
1546 34 : wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1547 34 : return -1;
1548 : }
1549 :
1550 1233 : return wpas_select_network_from_last_scan(wpa_s, 0, 1);
1551 : #endif /* CONFIG_NO_SCAN_PROCESSING */
1552 : }
1553 :
1554 : #ifdef CONFIG_WNM
1555 :
1556 7 : static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1557 : {
1558 7 : struct wpa_supplicant *wpa_s = eloop_ctx;
1559 :
1560 7 : if (wpa_s->wpa_state < WPA_ASSOCIATED)
1561 7 : return;
1562 :
1563 7 : if (!wpa_s->no_keep_alive) {
1564 12 : wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1565 12 : MAC2STR(wpa_s->bssid));
1566 : /* TODO: could skip this if normal data traffic has been sent */
1567 : /* TODO: Consider using some more appropriate data frame for
1568 : * this */
1569 2 : if (wpa_s->l2)
1570 2 : l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1571 : (u8 *) "", 0);
1572 : }
1573 :
1574 : #ifdef CONFIG_SME
1575 7 : if (wpa_s->sme.bss_max_idle_period) {
1576 : unsigned int msec;
1577 7 : msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1578 7 : if (msec > 100)
1579 7 : msec -= 100;
1580 7 : eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1581 : wnm_bss_keep_alive, wpa_s, NULL);
1582 : }
1583 : #endif /* CONFIG_SME */
1584 : }
1585 :
1586 :
1587 2461 : static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1588 : const u8 *ies, size_t ies_len)
1589 : {
1590 : struct ieee802_11_elems elems;
1591 :
1592 2461 : if (ies == NULL)
1593 0 : return;
1594 :
1595 2461 : if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1596 0 : return;
1597 :
1598 : #ifdef CONFIG_SME
1599 2461 : if (elems.bss_max_idle_period) {
1600 : unsigned int msec;
1601 2461 : wpa_s->sme.bss_max_idle_period =
1602 2461 : WPA_GET_LE16(elems.bss_max_idle_period);
1603 4922 : wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1604 2461 : "TU)%s", wpa_s->sme.bss_max_idle_period,
1605 2461 : (elems.bss_max_idle_period[2] & 0x01) ?
1606 : " (protected keep-live required)" : "");
1607 2461 : if (wpa_s->sme.bss_max_idle_period == 0)
1608 0 : wpa_s->sme.bss_max_idle_period = 1;
1609 2461 : if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1610 2424 : eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1611 : /* msec times 1000 */
1612 2424 : msec = wpa_s->sme.bss_max_idle_period * 1024;
1613 2424 : if (msec > 100)
1614 2424 : msec -= 100;
1615 2424 : eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1616 : wnm_bss_keep_alive, wpa_s,
1617 : NULL);
1618 : }
1619 : }
1620 : #endif /* CONFIG_SME */
1621 : }
1622 :
1623 : #endif /* CONFIG_WNM */
1624 :
1625 :
1626 4944 : void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1627 : {
1628 : #ifdef CONFIG_WNM
1629 4944 : eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1630 : #endif /* CONFIG_WNM */
1631 4944 : }
1632 :
1633 :
1634 : #ifdef CONFIG_INTERWORKING
1635 :
1636 2 : static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1637 : size_t len)
1638 : {
1639 : int res;
1640 :
1641 2 : wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1642 2 : res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1643 2 : if (res) {
1644 0 : wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1645 : }
1646 :
1647 2 : return res;
1648 : }
1649 :
1650 :
1651 2461 : static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1652 : const u8 *ies, size_t ies_len)
1653 : {
1654 : struct ieee802_11_elems elems;
1655 :
1656 2461 : if (ies == NULL)
1657 0 : return;
1658 :
1659 2461 : if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1660 0 : return;
1661 :
1662 2461 : if (elems.qos_map_set) {
1663 1 : wpas_qos_map_set(wpa_s, elems.qos_map_set,
1664 1 : elems.qos_map_set_len);
1665 : }
1666 : }
1667 :
1668 : #endif /* CONFIG_INTERWORKING */
1669 :
1670 :
1671 2462 : static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1672 : union wpa_event_data *data)
1673 : {
1674 2462 : int l, len, found = 0, wpa_found, rsn_found;
1675 : const u8 *p;
1676 : #ifdef CONFIG_IEEE80211R
1677 : u8 bssid[ETH_ALEN];
1678 : #endif /* CONFIG_IEEE80211R */
1679 :
1680 2462 : wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1681 2462 : if (data->assoc_info.req_ies)
1682 0 : wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1683 : data->assoc_info.req_ies_len);
1684 2462 : if (data->assoc_info.resp_ies) {
1685 2461 : wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1686 : data->assoc_info.resp_ies_len);
1687 : #ifdef CONFIG_TDLS
1688 2461 : wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1689 : data->assoc_info.resp_ies_len);
1690 : #endif /* CONFIG_TDLS */
1691 : #ifdef CONFIG_WNM
1692 2461 : wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1693 : data->assoc_info.resp_ies_len);
1694 : #endif /* CONFIG_WNM */
1695 : #ifdef CONFIG_INTERWORKING
1696 2461 : interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1697 : data->assoc_info.resp_ies_len);
1698 : #endif /* CONFIG_INTERWORKING */
1699 : }
1700 2462 : if (data->assoc_info.beacon_ies)
1701 0 : wpa_hexdump(MSG_DEBUG, "beacon_ies",
1702 0 : data->assoc_info.beacon_ies,
1703 : data->assoc_info.beacon_ies_len);
1704 2462 : if (data->assoc_info.freq)
1705 2443 : wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1706 : data->assoc_info.freq);
1707 :
1708 2462 : p = data->assoc_info.req_ies;
1709 2462 : l = data->assoc_info.req_ies_len;
1710 :
1711 : /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1712 4924 : while (p && l >= 2) {
1713 0 : len = p[1] + 2;
1714 0 : if (len > l) {
1715 0 : wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1716 : p, l);
1717 0 : break;
1718 : }
1719 0 : if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1720 0 : (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1721 0 : (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1722 0 : if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1723 0 : break;
1724 0 : found = 1;
1725 0 : wpa_find_assoc_pmkid(wpa_s);
1726 0 : break;
1727 : }
1728 0 : l -= len;
1729 0 : p += len;
1730 : }
1731 2462 : if (!found && data->assoc_info.req_ies)
1732 0 : wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1733 :
1734 : #ifdef CONFIG_IEEE80211R
1735 : #ifdef CONFIG_SME
1736 2462 : if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1737 442 : if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1738 221 : wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1739 : data->assoc_info.resp_ies,
1740 : data->assoc_info.resp_ies_len,
1741 : bssid) < 0) {
1742 0 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1743 : "Reassociation Response failed");
1744 0 : wpa_supplicant_deauthenticate(
1745 : wpa_s, WLAN_REASON_INVALID_IE);
1746 0 : return -1;
1747 : }
1748 : }
1749 :
1750 2462 : p = data->assoc_info.resp_ies;
1751 2462 : l = data->assoc_info.resp_ies_len;
1752 :
1753 : #ifdef CONFIG_WPS_STRICT
1754 : if (p && wpa_s->current_ssid &&
1755 : wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1756 : struct wpabuf *wps;
1757 : wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1758 : if (wps == NULL) {
1759 : wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1760 : "include WPS IE in (Re)Association Response");
1761 : return -1;
1762 : }
1763 :
1764 : if (wps_validate_assoc_resp(wps) < 0) {
1765 : wpabuf_free(wps);
1766 : wpa_supplicant_deauthenticate(
1767 : wpa_s, WLAN_REASON_INVALID_IE);
1768 : return -1;
1769 : }
1770 : wpabuf_free(wps);
1771 : }
1772 : #endif /* CONFIG_WPS_STRICT */
1773 :
1774 : /* Go through the IEs and make a copy of the MDIE, if present. */
1775 21355 : while (p && l >= 2) {
1776 16676 : len = p[1] + 2;
1777 16676 : if (len > l) {
1778 0 : wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1779 : p, l);
1780 0 : break;
1781 : }
1782 16921 : if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1783 245 : p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1784 245 : wpa_s->sme.ft_used = 1;
1785 245 : os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1786 : MOBILITY_DOMAIN_ID_LEN);
1787 245 : break;
1788 : }
1789 16431 : l -= len;
1790 16431 : p += len;
1791 : }
1792 : #endif /* CONFIG_SME */
1793 :
1794 : /* Process FT when SME is in the driver */
1795 2499 : if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1796 37 : wpa_ft_is_completed(wpa_s->wpa)) {
1797 0 : if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1798 0 : wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1799 : data->assoc_info.resp_ies,
1800 : data->assoc_info.resp_ies_len,
1801 : bssid) < 0) {
1802 0 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1803 : "Reassociation Response failed");
1804 0 : wpa_supplicant_deauthenticate(
1805 : wpa_s, WLAN_REASON_INVALID_IE);
1806 0 : return -1;
1807 : }
1808 0 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1809 : }
1810 :
1811 2462 : wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1812 : data->assoc_info.resp_ies_len);
1813 : #endif /* CONFIG_IEEE80211R */
1814 :
1815 : /* WPA/RSN IE from Beacon/ProbeResp */
1816 2462 : p = data->assoc_info.beacon_ies;
1817 2462 : l = data->assoc_info.beacon_ies_len;
1818 :
1819 : /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1820 : */
1821 2462 : wpa_found = rsn_found = 0;
1822 4924 : while (p && l >= 2) {
1823 0 : len = p[1] + 2;
1824 0 : if (len > l) {
1825 0 : wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1826 : p, l);
1827 0 : break;
1828 : }
1829 0 : if (!wpa_found &&
1830 0 : p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1831 0 : os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1832 0 : wpa_found = 1;
1833 0 : wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1834 : }
1835 :
1836 0 : if (!rsn_found &&
1837 0 : p[0] == WLAN_EID_RSN && p[1] >= 2) {
1838 0 : rsn_found = 1;
1839 0 : wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1840 : }
1841 :
1842 0 : l -= len;
1843 0 : p += len;
1844 : }
1845 :
1846 2462 : if (!wpa_found && data->assoc_info.beacon_ies)
1847 0 : wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1848 2462 : if (!rsn_found && data->assoc_info.beacon_ies)
1849 0 : wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1850 2462 : if (wpa_found || rsn_found)
1851 0 : wpa_s->ap_ies_from_associnfo = 1;
1852 :
1853 2763 : if (wpa_s->assoc_freq && data->assoc_info.freq &&
1854 301 : wpa_s->assoc_freq != data->assoc_info.freq) {
1855 1 : wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1856 : "%u to %u MHz",
1857 : wpa_s->assoc_freq, data->assoc_info.freq);
1858 1 : wpa_supplicant_update_scan_results(wpa_s);
1859 : }
1860 :
1861 2462 : wpa_s->assoc_freq = data->assoc_info.freq;
1862 :
1863 2462 : return 0;
1864 : }
1865 :
1866 :
1867 0 : static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1868 : {
1869 0 : const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1870 :
1871 0 : if (!wpa_s->current_bss || !wpa_s->current_ssid)
1872 0 : return -1;
1873 :
1874 0 : if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1875 0 : return 0;
1876 :
1877 0 : bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1878 : WPA_IE_VENDOR_TYPE);
1879 0 : bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1880 :
1881 0 : if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1882 0 : bss_wpa ? 2 + bss_wpa[1] : 0) ||
1883 0 : wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1884 0 : bss_rsn ? 2 + bss_rsn[1] : 0))
1885 0 : return -1;
1886 :
1887 0 : return 0;
1888 : }
1889 :
1890 :
1891 2477 : static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1892 : union wpa_event_data *data)
1893 : {
1894 : u8 bssid[ETH_ALEN];
1895 : int ft_completed;
1896 :
1897 : #ifdef CONFIG_AP
1898 2477 : if (wpa_s->ap_iface) {
1899 0 : if (!data)
1900 5 : return;
1901 0 : hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1902 : data->assoc_info.addr,
1903 : data->assoc_info.req_ies,
1904 : data->assoc_info.req_ies_len,
1905 : data->assoc_info.reassoc);
1906 0 : return;
1907 : }
1908 : #endif /* CONFIG_AP */
1909 :
1910 2477 : ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1911 2477 : if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1912 0 : return;
1913 :
1914 2477 : if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1915 0 : wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1916 0 : wpa_supplicant_deauthenticate(
1917 : wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1918 0 : return;
1919 : }
1920 :
1921 2477 : wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1922 2477 : if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1923 2476 : wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1924 : MACSTR, MAC2STR(bssid));
1925 : random_add_randomness(bssid, ETH_ALEN);
1926 2476 : os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1927 2476 : os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1928 2476 : wpas_notify_bssid_changed(wpa_s);
1929 :
1930 2476 : if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1931 1925 : wpa_clear_keys(wpa_s, bssid);
1932 : }
1933 2476 : if (wpa_supplicant_select_config(wpa_s) < 0) {
1934 5 : wpa_supplicant_deauthenticate(
1935 : wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1936 5 : return;
1937 : }
1938 :
1939 4933 : if (wpa_s->conf->ap_scan == 1 &&
1940 2462 : wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1941 0 : if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1942 0 : wpa_msg(wpa_s, MSG_WARNING,
1943 : "WPA/RSN IEs not updated");
1944 : }
1945 : }
1946 :
1947 : #ifdef CONFIG_SME
1948 2472 : os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1949 2472 : wpa_s->sme.prev_bssid_set = 1;
1950 2472 : wpa_s->sme.last_unprot_disconnect.sec = 0;
1951 : #endif /* CONFIG_SME */
1952 :
1953 2472 : wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1954 2472 : if (wpa_s->current_ssid) {
1955 : /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1956 : * initialized before association, but for other modes,
1957 : * initialize PC/SC here, if the current configuration needs
1958 : * smartcard or SIM/USIM. */
1959 2472 : wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1960 : }
1961 2472 : wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1962 2472 : if (wpa_s->l2)
1963 2472 : l2_packet_notify_auth_start(wpa_s->l2);
1964 :
1965 : /*
1966 : * Set portEnabled first to FALSE in order to get EAP state machine out
1967 : * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1968 : * state machine may transit to AUTHENTICATING state based on obsolete
1969 : * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1970 : * AUTHENTICATED without ever giving chance to EAP state machine to
1971 : * reset the state.
1972 : */
1973 2472 : if (!ft_completed) {
1974 2251 : eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1975 2251 : eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1976 : }
1977 2472 : if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1978 884 : eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1979 : /* 802.1X::portControl = Auto */
1980 2472 : eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1981 2472 : wpa_s->eapol_received = 0;
1982 4623 : if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1983 4297 : wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1984 4292 : (wpa_s->current_ssid &&
1985 2146 : wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1986 660 : if (wpa_s->current_ssid &&
1987 335 : wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1988 5 : (wpa_s->drv_flags &
1989 : WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1990 : /*
1991 : * Set the key after having received joined-IBSS event
1992 : * from the driver.
1993 : */
1994 5 : wpa_supplicant_set_wpa_none_key(wpa_s,
1995 : wpa_s->current_ssid);
1996 : }
1997 330 : wpa_supplicant_cancel_auth_timeout(wpa_s);
1998 330 : wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1999 2142 : } else if (!ft_completed) {
2000 : /* Timeout for receiving the first EAPOL packet */
2001 1921 : wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2002 : }
2003 2472 : wpa_supplicant_cancel_scan(wpa_s);
2004 :
2005 2472 : if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2006 0 : wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2007 : /*
2008 : * We are done; the driver will take care of RSN 4-way
2009 : * handshake.
2010 : */
2011 0 : wpa_supplicant_cancel_auth_timeout(wpa_s);
2012 0 : wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2013 0 : eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2014 0 : eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2015 2472 : } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2016 0 : wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2017 : /*
2018 : * The driver will take care of RSN 4-way handshake, so we need
2019 : * to allow EAPOL supplicant to complete its work without
2020 : * waiting for WPA supplicant.
2021 : */
2022 0 : eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2023 2472 : } else if (ft_completed) {
2024 : /*
2025 : * FT protocol completed - make sure EAPOL state machine ends
2026 : * up in authenticated.
2027 : */
2028 221 : wpa_supplicant_cancel_auth_timeout(wpa_s);
2029 221 : wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2030 221 : eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2031 221 : eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2032 : }
2033 :
2034 2472 : wpa_s->last_eapol_matches_bssid = 0;
2035 :
2036 2472 : if (wpa_s->pending_eapol_rx) {
2037 : struct os_reltime now, age;
2038 4 : os_get_reltime(&now);
2039 4 : os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
2040 4 : if (age.sec == 0 && age.usec < 100000 &&
2041 0 : os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2042 : 0) {
2043 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2044 : "frame that was received just before "
2045 : "association notification");
2046 0 : wpa_supplicant_rx_eapol(
2047 0 : wpa_s, wpa_s->pending_eapol_rx_src,
2048 0 : wpabuf_head(wpa_s->pending_eapol_rx),
2049 0 : wpabuf_len(wpa_s->pending_eapol_rx));
2050 : }
2051 4 : wpabuf_free(wpa_s->pending_eapol_rx);
2052 4 : wpa_s->pending_eapol_rx = NULL;
2053 : }
2054 :
2055 4623 : if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2056 2482 : wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
2057 662 : wpa_s->current_ssid &&
2058 331 : (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2059 : /* Set static WEP keys again */
2060 323 : wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2061 : }
2062 :
2063 : #ifdef CONFIG_IBSS_RSN
2064 4944 : if (wpa_s->current_ssid &&
2065 2486 : wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2066 23 : wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2067 13 : wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2068 4 : wpa_s->ibss_rsn == NULL) {
2069 4 : wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2070 4 : if (!wpa_s->ibss_rsn) {
2071 0 : wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2072 0 : wpa_supplicant_deauthenticate(
2073 : wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2074 0 : return;
2075 : }
2076 :
2077 4 : ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2078 : }
2079 : #endif /* CONFIG_IBSS_RSN */
2080 :
2081 2472 : wpas_wps_notify_assoc(wpa_s, bssid);
2082 :
2083 2472 : if (data) {
2084 2443 : wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2085 : data->assoc_info.resp_ies_len,
2086 2443 : &data->assoc_info.wmm_params);
2087 :
2088 2443 : if (wpa_s->reassoc_same_bss)
2089 47 : wmm_ac_restore_tspecs(wpa_s);
2090 : }
2091 : }
2092 :
2093 :
2094 40 : static int disconnect_reason_recoverable(u16 reason_code)
2095 : {
2096 78 : return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2097 78 : reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2098 : reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2099 : }
2100 :
2101 :
2102 2349 : static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2103 : u16 reason_code,
2104 : int locally_generated)
2105 : {
2106 : const u8 *bssid;
2107 :
2108 2349 : if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2109 : /*
2110 : * At least Host AP driver and a Prism3 card seemed to be
2111 : * generating streams of disconnected events when configuring
2112 : * IBSS for WPA-None. Ignore them for now.
2113 : */
2114 2354 : return;
2115 : }
2116 :
2117 2344 : bssid = wpa_s->bssid;
2118 2344 : if (is_zero_ether_addr(bssid))
2119 75 : bssid = wpa_s->pending_bssid;
2120 :
2121 2407 : if (!is_zero_ether_addr(bssid) ||
2122 63 : wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2123 15967 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2124 : " reason=%d%s",
2125 13686 : MAC2STR(bssid), reason_code,
2126 : locally_generated ? " locally_generated=1" : "");
2127 : }
2128 : }
2129 :
2130 :
2131 2329 : static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2132 : int locally_generated)
2133 : {
2134 2338 : if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2135 9 : !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2136 2322 : return 0; /* Not in 4-way handshake with PSK */
2137 :
2138 : /*
2139 : * It looks like connection was lost while trying to go through PSK
2140 : * 4-way handshake. Filter out known disconnection cases that are caused
2141 : * by something else than PSK mismatch to avoid confusing reports.
2142 : */
2143 :
2144 7 : if (locally_generated) {
2145 4 : if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2146 0 : return 0;
2147 : }
2148 :
2149 7 : return 1;
2150 : }
2151 :
2152 :
2153 2334 : static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2154 : u16 reason_code,
2155 : int locally_generated)
2156 : {
2157 : const u8 *bssid;
2158 : int authenticating;
2159 : u8 prev_pending_bssid[ETH_ALEN];
2160 2334 : struct wpa_bss *fast_reconnect = NULL;
2161 2334 : struct wpa_ssid *fast_reconnect_ssid = NULL;
2162 : struct wpa_ssid *last_ssid;
2163 :
2164 2334 : authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2165 2334 : os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2166 :
2167 2334 : if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2168 : /*
2169 : * At least Host AP driver and a Prism3 card seemed to be
2170 : * generating streams of disconnected events when configuring
2171 : * IBSS for WPA-None. Ignore them for now.
2172 : */
2173 5 : wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2174 : "IBSS/WPA-None mode");
2175 5 : return;
2176 : }
2177 :
2178 2329 : if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2179 7 : wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2180 : "pre-shared key may be incorrect");
2181 7 : if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2182 2 : return; /* P2P group removed */
2183 5 : wpas_auth_failed(wpa_s, "WRONG_KEY");
2184 : }
2185 4024 : if (!wpa_s->disconnected &&
2186 1699 : (!wpa_s->auto_reconnect_disabled ||
2187 3 : wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2188 1 : wpas_wps_searching(wpa_s))) {
2189 1697 : wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2190 : "reconnect (wps=%d/%d wpa_state=%d)",
2191 : wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2192 : wpas_wps_searching(wpa_s),
2193 : wpa_s->wpa_state);
2194 4365 : if (wpa_s->wpa_state == WPA_COMPLETED &&
2195 1942 : wpa_s->current_ssid &&
2196 1934 : wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2197 40 : !locally_generated &&
2198 40 : disconnect_reason_recoverable(reason_code)) {
2199 : /*
2200 : * It looks like the AP has dropped association with
2201 : * us, but could allow us to get back in. Try to
2202 : * reconnect to the same BSS without full scan to save
2203 : * time for some common cases.
2204 : */
2205 3 : fast_reconnect = wpa_s->current_bss;
2206 3 : fast_reconnect_ssid = wpa_s->current_ssid;
2207 1694 : } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2208 1652 : wpa_supplicant_req_scan(wpa_s, 0, 100000);
2209 : else
2210 42 : wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2211 : "immediate scan");
2212 : } else {
2213 630 : wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2214 : "try to re-connect");
2215 630 : wpa_s->reassociate = 0;
2216 630 : wpa_s->disconnected = 1;
2217 630 : wpa_supplicant_cancel_sched_scan(wpa_s);
2218 : }
2219 2327 : bssid = wpa_s->bssid;
2220 2327 : if (is_zero_ether_addr(bssid))
2221 162 : bssid = wpa_s->pending_bssid;
2222 2327 : if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2223 2173 : wpas_connection_failed(wpa_s, bssid);
2224 2327 : wpa_sm_notify_disassoc(wpa_s->wpa);
2225 2327 : if (locally_generated)
2226 2051 : wpa_s->disconnect_reason = -reason_code;
2227 : else
2228 276 : wpa_s->disconnect_reason = reason_code;
2229 2327 : wpas_notify_disconnect_reason(wpa_s);
2230 2327 : if (wpa_supplicant_dynamic_keys(wpa_s)) {
2231 2009 : wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2232 2009 : wpa_clear_keys(wpa_s, wpa_s->bssid);
2233 : }
2234 2327 : last_ssid = wpa_s->current_ssid;
2235 2327 : wpa_supplicant_mark_disassoc(wpa_s);
2236 :
2237 2327 : if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2238 7 : sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2239 7 : wpa_s->current_ssid = last_ssid;
2240 : }
2241 :
2242 2330 : if (fast_reconnect &&
2243 6 : !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2244 6 : !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2245 3 : !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2246 3 : fast_reconnect->ssid_len) &&
2247 3 : !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
2248 : #ifndef CONFIG_NO_SCAN_PROCESSING
2249 2 : wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2250 4 : if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2251 : fast_reconnect_ssid) < 0) {
2252 : /* Recover through full scan */
2253 0 : wpa_supplicant_req_scan(wpa_s, 0, 100000);
2254 : }
2255 : #endif /* CONFIG_NO_SCAN_PROCESSING */
2256 2325 : } else if (fast_reconnect) {
2257 : /*
2258 : * Could not reconnect to the same BSS due to network being
2259 : * disabled. Use a new scan to match the alternative behavior
2260 : * above, i.e., to continue automatic reconnection attempt in a
2261 : * way that enforces disabled network rules.
2262 : */
2263 1 : wpa_supplicant_req_scan(wpa_s, 0, 100000);
2264 : }
2265 : }
2266 :
2267 :
2268 : #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2269 : void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2270 : {
2271 : struct wpa_supplicant *wpa_s = eloop_ctx;
2272 :
2273 : if (!wpa_s->pending_mic_error_report)
2274 : return;
2275 :
2276 : wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2277 : wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2278 : wpa_s->pending_mic_error_report = 0;
2279 : }
2280 : #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2281 :
2282 :
2283 : static void
2284 4 : wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2285 : union wpa_event_data *data)
2286 : {
2287 : int pairwise;
2288 : struct os_reltime t;
2289 :
2290 4 : wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2291 4 : pairwise = (data && data->michael_mic_failure.unicast);
2292 4 : os_get_reltime(&t);
2293 6 : if ((wpa_s->last_michael_mic_error.sec &&
2294 4 : !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
2295 2 : wpa_s->pending_mic_error_report) {
2296 2 : if (wpa_s->pending_mic_error_report) {
2297 : /*
2298 : * Send the pending MIC error report immediately since
2299 : * we are going to start countermeasures and AP better
2300 : * do the same.
2301 : */
2302 0 : wpa_sm_key_request(wpa_s->wpa, 1,
2303 : wpa_s->pending_mic_error_pairwise);
2304 : }
2305 :
2306 : /* Send the new MIC error report immediately since we are going
2307 : * to start countermeasures and AP better do the same.
2308 : */
2309 2 : wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2310 :
2311 : /* initialize countermeasures */
2312 2 : wpa_s->countermeasures = 1;
2313 :
2314 2 : wpa_blacklist_add(wpa_s, wpa_s->bssid);
2315 :
2316 2 : wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2317 :
2318 : /*
2319 : * Need to wait for completion of request frame. We do not get
2320 : * any callback for the message completion, so just wait a
2321 : * short while and hope for the best. */
2322 2 : os_sleep(0, 10000);
2323 :
2324 2 : wpa_drv_set_countermeasures(wpa_s, 1);
2325 2 : wpa_supplicant_deauthenticate(wpa_s,
2326 : WLAN_REASON_MICHAEL_MIC_FAILURE);
2327 2 : eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2328 : wpa_s, NULL);
2329 2 : eloop_register_timeout(60, 0,
2330 : wpa_supplicant_stop_countermeasures,
2331 : wpa_s, NULL);
2332 : /* TODO: mark the AP rejected for 60 second. STA is
2333 : * allowed to associate with another AP.. */
2334 : } else {
2335 : #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2336 : if (wpa_s->mic_errors_seen) {
2337 : /*
2338 : * Reduce the effectiveness of Michael MIC error
2339 : * reports as a means for attacking against TKIP if
2340 : * more than one MIC failure is noticed with the same
2341 : * PTK. We delay the transmission of the reports by a
2342 : * random time between 0 and 60 seconds in order to
2343 : * force the attacker wait 60 seconds before getting
2344 : * the information on whether a frame resulted in a MIC
2345 : * failure.
2346 : */
2347 : u8 rval[4];
2348 : int sec;
2349 :
2350 : if (os_get_random(rval, sizeof(rval)) < 0)
2351 : sec = os_random() % 60;
2352 : else
2353 : sec = WPA_GET_BE32(rval) % 60;
2354 : wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2355 : "report %d seconds", sec);
2356 : wpa_s->pending_mic_error_report = 1;
2357 : wpa_s->pending_mic_error_pairwise = pairwise;
2358 : eloop_cancel_timeout(
2359 : wpa_supplicant_delayed_mic_error_report,
2360 : wpa_s, NULL);
2361 : eloop_register_timeout(
2362 : sec, os_random() % 1000000,
2363 : wpa_supplicant_delayed_mic_error_report,
2364 : wpa_s, NULL);
2365 : } else {
2366 : wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2367 : }
2368 : #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2369 2 : wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2370 : #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2371 : }
2372 4 : wpa_s->last_michael_mic_error = t;
2373 4 : wpa_s->mic_errors_seen++;
2374 4 : }
2375 :
2376 :
2377 : #ifdef CONFIG_TERMINATE_ONLASTIF
2378 : static int any_interfaces(struct wpa_supplicant *head)
2379 : {
2380 : struct wpa_supplicant *wpa_s;
2381 :
2382 : for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2383 : if (!wpa_s->interface_removed)
2384 : return 1;
2385 : return 0;
2386 : }
2387 : #endif /* CONFIG_TERMINATE_ONLASTIF */
2388 :
2389 :
2390 : static void
2391 20 : wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2392 : union wpa_event_data *data)
2393 : {
2394 20 : if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2395 28 : return;
2396 :
2397 12 : switch (data->interface_status.ievent) {
2398 : case EVENT_INTERFACE_ADDED:
2399 0 : if (!wpa_s->interface_removed)
2400 0 : break;
2401 0 : wpa_s->interface_removed = 0;
2402 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2403 0 : if (wpa_supplicant_driver_init(wpa_s) < 0) {
2404 0 : wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2405 : "driver after interface was added");
2406 : }
2407 0 : break;
2408 : case EVENT_INTERFACE_REMOVED:
2409 12 : wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2410 12 : wpa_s->interface_removed = 1;
2411 12 : wpa_supplicant_mark_disassoc(wpa_s);
2412 12 : wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2413 12 : l2_packet_deinit(wpa_s->l2);
2414 12 : wpa_s->l2 = NULL;
2415 : #ifdef CONFIG_TERMINATE_ONLASTIF
2416 : /* check if last interface */
2417 : if (!any_interfaces(wpa_s->global->ifaces))
2418 : eloop_terminate();
2419 : #endif /* CONFIG_TERMINATE_ONLASTIF */
2420 12 : break;
2421 : }
2422 : }
2423 :
2424 :
2425 : #ifdef CONFIG_PEERKEY
2426 : static void
2427 0 : wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2428 : union wpa_event_data *data)
2429 : {
2430 0 : if (data == NULL)
2431 0 : return;
2432 0 : wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2433 : }
2434 : #endif /* CONFIG_PEERKEY */
2435 :
2436 :
2437 : #ifdef CONFIG_TDLS
2438 0 : static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2439 : union wpa_event_data *data)
2440 : {
2441 0 : if (data == NULL)
2442 0 : return;
2443 0 : switch (data->tdls.oper) {
2444 : case TDLS_REQUEST_SETUP:
2445 0 : wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2446 0 : if (wpa_tdls_is_external_setup(wpa_s->wpa))
2447 0 : wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2448 : else
2449 0 : wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2450 0 : break;
2451 : case TDLS_REQUEST_TEARDOWN:
2452 0 : if (wpa_tdls_is_external_setup(wpa_s->wpa))
2453 0 : wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2454 0 : data->tdls.reason_code);
2455 : else
2456 0 : wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2457 0 : data->tdls.peer);
2458 0 : break;
2459 : }
2460 : }
2461 : #endif /* CONFIG_TDLS */
2462 :
2463 :
2464 : #ifdef CONFIG_WNM
2465 0 : static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2466 : union wpa_event_data *data)
2467 : {
2468 0 : if (data == NULL)
2469 0 : return;
2470 0 : switch (data->wnm.oper) {
2471 : case WNM_OPER_SLEEP:
2472 0 : wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2473 : "(action=%d, intval=%d)",
2474 0 : data->wnm.sleep_action, data->wnm.sleep_intval);
2475 0 : ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2476 0 : data->wnm.sleep_intval, NULL);
2477 0 : break;
2478 : }
2479 : }
2480 : #endif /* CONFIG_WNM */
2481 :
2482 :
2483 : #ifdef CONFIG_IEEE80211R
2484 : static void
2485 113 : wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2486 : union wpa_event_data *data)
2487 : {
2488 113 : if (data == NULL)
2489 113 : return;
2490 :
2491 226 : if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2492 : data->ft_ies.ies_len,
2493 : data->ft_ies.ft_action,
2494 113 : data->ft_ies.target_ap,
2495 : data->ft_ies.ric_ies,
2496 : data->ft_ies.ric_ies_len) < 0) {
2497 : /* TODO: prevent MLME/driver from trying to associate? */
2498 : }
2499 : }
2500 : #endif /* CONFIG_IEEE80211R */
2501 :
2502 :
2503 : #ifdef CONFIG_IBSS_RSN
2504 21 : static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2505 : union wpa_event_data *data)
2506 : {
2507 : struct wpa_ssid *ssid;
2508 21 : if (wpa_s->wpa_state < WPA_ASSOCIATED)
2509 0 : return;
2510 21 : if (data == NULL)
2511 0 : return;
2512 21 : ssid = wpa_s->current_ssid;
2513 21 : if (ssid == NULL)
2514 0 : return;
2515 21 : if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2516 11 : return;
2517 :
2518 10 : ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2519 : }
2520 :
2521 :
2522 18 : static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2523 : union wpa_event_data *data)
2524 : {
2525 18 : struct wpa_ssid *ssid = wpa_s->current_ssid;
2526 :
2527 18 : if (ssid == NULL)
2528 0 : return;
2529 :
2530 : /* check if the ssid is correctly configured as IBSS/RSN */
2531 18 : if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2532 0 : return;
2533 :
2534 18 : ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2535 : data->rx_mgmt.frame_len);
2536 : }
2537 : #endif /* CONFIG_IBSS_RSN */
2538 :
2539 :
2540 : #ifdef CONFIG_IEEE80211R
2541 109 : static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2542 : size_t len)
2543 : {
2544 : const u8 *sta_addr, *target_ap_addr;
2545 : u16 status;
2546 :
2547 109 : wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2548 109 : if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2549 0 : return; /* only SME case supported for now */
2550 109 : if (len < 1 + 2 * ETH_ALEN + 2)
2551 0 : return;
2552 109 : if (data[0] != 2)
2553 0 : return; /* Only FT Action Response is supported for now */
2554 109 : sta_addr = data + 1;
2555 109 : target_ap_addr = data + 1 + ETH_ALEN;
2556 109 : status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2557 109 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2558 : MACSTR " TargetAP " MACSTR " status %u",
2559 : MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2560 :
2561 109 : if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2562 0 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2563 : " in FT Action Response", MAC2STR(sta_addr));
2564 0 : return;
2565 : }
2566 :
2567 109 : if (status) {
2568 1 : wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2569 : "failure (status code %d)", status);
2570 : /* TODO: report error to FT code(?) */
2571 1 : return;
2572 : }
2573 :
2574 108 : if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2575 : len - (1 + 2 * ETH_ALEN + 2), 1,
2576 : target_ap_addr, NULL, 0) < 0)
2577 0 : return;
2578 :
2579 : #ifdef CONFIG_SME
2580 : {
2581 : struct wpa_bss *bss;
2582 108 : bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2583 108 : if (bss)
2584 108 : wpa_s->sme.freq = bss->freq;
2585 108 : wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2586 108 : sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2587 : WLAN_AUTH_FT);
2588 : }
2589 : #endif /* CONFIG_SME */
2590 : }
2591 : #endif /* CONFIG_IEEE80211R */
2592 :
2593 :
2594 19 : static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2595 : struct unprot_deauth *e)
2596 : {
2597 : #ifdef CONFIG_IEEE80211W
2598 247 : wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2599 : "dropped: " MACSTR " -> " MACSTR
2600 : " (reason code %u)",
2601 247 : MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2602 19 : sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2603 : #endif /* CONFIG_IEEE80211W */
2604 19 : }
2605 :
2606 :
2607 19 : static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2608 : struct unprot_disassoc *e)
2609 : {
2610 : #ifdef CONFIG_IEEE80211W
2611 247 : wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2612 : "dropped: " MACSTR " -> " MACSTR
2613 : " (reason code %u)",
2614 247 : MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2615 19 : sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2616 : #endif /* CONFIG_IEEE80211W */
2617 19 : }
2618 :
2619 :
2620 2389 : static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2621 : u16 reason_code, int locally_generated,
2622 : const u8 *ie, size_t ie_len, int deauth)
2623 : {
2624 : #ifdef CONFIG_AP
2625 2389 : if (wpa_s->ap_iface && addr) {
2626 0 : hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2627 0 : return;
2628 : }
2629 :
2630 2389 : if (wpa_s->ap_iface) {
2631 44 : wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2632 44 : return;
2633 : }
2634 : #endif /* CONFIG_AP */
2635 :
2636 2345 : wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2637 :
2638 4593 : if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2639 3633 : ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2640 2256 : (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2641 1114 : eapol_sm_failed(wpa_s->eapol))) &&
2642 243 : !wpa_s->eap_expected_failure))
2643 239 : wpas_auth_failed(wpa_s, "AUTH_FAILED");
2644 :
2645 : #ifdef CONFIG_P2P
2646 2345 : if (deauth && reason_code > 0) {
2647 2333 : if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2648 : locally_generated) > 0) {
2649 : /*
2650 : * The interface was removed, so cannot continue
2651 : * processing any additional operations after this.
2652 : */
2653 11 : return;
2654 : }
2655 : }
2656 : #endif /* CONFIG_P2P */
2657 :
2658 2334 : wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2659 : locally_generated);
2660 : }
2661 :
2662 :
2663 12 : static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2664 : struct disassoc_info *info)
2665 : {
2666 12 : u16 reason_code = 0;
2667 12 : int locally_generated = 0;
2668 12 : const u8 *addr = NULL;
2669 12 : const u8 *ie = NULL;
2670 12 : size_t ie_len = 0;
2671 :
2672 12 : wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2673 :
2674 12 : if (info) {
2675 4 : addr = info->addr;
2676 4 : ie = info->ie;
2677 4 : ie_len = info->ie_len;
2678 4 : reason_code = info->reason_code;
2679 4 : locally_generated = info->locally_generated;
2680 4 : wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2681 : locally_generated ? " (locally generated)" : "");
2682 4 : if (addr)
2683 4 : wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2684 : MAC2STR(addr));
2685 4 : wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2686 : ie, ie_len);
2687 : }
2688 :
2689 : #ifdef CONFIG_AP
2690 12 : if (wpa_s->ap_iface && info && info->addr) {
2691 0 : hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2692 0 : return;
2693 : }
2694 :
2695 12 : if (wpa_s->ap_iface) {
2696 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2697 0 : return;
2698 : }
2699 : #endif /* CONFIG_AP */
2700 :
2701 : #ifdef CONFIG_P2P
2702 12 : if (info) {
2703 4 : wpas_p2p_disassoc_notif(
2704 : wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2705 : locally_generated);
2706 : }
2707 : #endif /* CONFIG_P2P */
2708 :
2709 12 : if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2710 4 : sme_event_disassoc(wpa_s, info);
2711 :
2712 12 : wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2713 : ie, ie_len, 0);
2714 : }
2715 :
2716 :
2717 2377 : static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2718 : struct deauth_info *info)
2719 : {
2720 2377 : u16 reason_code = 0;
2721 2377 : int locally_generated = 0;
2722 2377 : const u8 *addr = NULL;
2723 2377 : const u8 *ie = NULL;
2724 2377 : size_t ie_len = 0;
2725 :
2726 2377 : wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2727 :
2728 2377 : if (info) {
2729 2377 : addr = info->addr;
2730 2377 : ie = info->ie;
2731 2377 : ie_len = info->ie_len;
2732 2377 : reason_code = info->reason_code;
2733 2377 : locally_generated = info->locally_generated;
2734 2377 : wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2735 : reason_code,
2736 : locally_generated ? " (locally generated)" : "");
2737 2377 : if (addr) {
2738 297 : wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2739 : MAC2STR(addr));
2740 : }
2741 2377 : wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2742 : ie, ie_len);
2743 : }
2744 :
2745 2377 : wpa_reset_ft_completed(wpa_s->wpa);
2746 :
2747 2377 : wpas_event_disconnect(wpa_s, addr, reason_code,
2748 : locally_generated, ie, ie_len, 1);
2749 2377 : }
2750 :
2751 :
2752 6951 : static const char * reg_init_str(enum reg_change_initiator init)
2753 : {
2754 6951 : switch (init) {
2755 : case REGDOM_SET_BY_CORE:
2756 6626 : return "CORE";
2757 : case REGDOM_SET_BY_USER:
2758 271 : return "USER";
2759 : case REGDOM_SET_BY_DRIVER:
2760 0 : return "DRIVER";
2761 : case REGDOM_SET_BY_COUNTRY_IE:
2762 18 : return "COUNTRY_IE";
2763 : case REGDOM_BEACON_HINT:
2764 36 : return "BEACON_HINT";
2765 : }
2766 0 : return "?";
2767 : }
2768 :
2769 :
2770 6951 : static const char * reg_type_str(enum reg_type type)
2771 : {
2772 6951 : switch (type) {
2773 : case REGDOM_TYPE_UNKNOWN:
2774 36 : return "UNKNOWN";
2775 : case REGDOM_TYPE_COUNTRY:
2776 289 : return "COUNTRY";
2777 : case REGDOM_TYPE_WORLD:
2778 6626 : return "WORLD";
2779 : case REGDOM_TYPE_CUSTOM_WORLD:
2780 0 : return "CUSTOM_WORLD";
2781 : case REGDOM_TYPE_INTERSECTION:
2782 0 : return "INTERSECTION";
2783 : }
2784 0 : return "?";
2785 : }
2786 :
2787 :
2788 6951 : static void wpa_supplicant_update_channel_list(
2789 : struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
2790 : {
2791 : struct wpa_supplicant *ifs;
2792 :
2793 13902 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
2794 : reg_init_str(info->initiator), reg_type_str(info->type),
2795 6951 : info->alpha2[0] ? " alpha2=" : "",
2796 6951 : info->alpha2[0] ? info->alpha2 : "");
2797 :
2798 6951 : if (wpa_s->drv_priv == NULL)
2799 6951 : return; /* Ignore event during drv initialization */
2800 :
2801 6951 : free_hw_features(wpa_s);
2802 6951 : wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2803 : wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2804 :
2805 6951 : wpas_p2p_update_channel_list(wpa_s);
2806 :
2807 : /*
2808 : * Check other interfaces to see if they share the same radio. If
2809 : * so, they get updated with this same hw mode info.
2810 : */
2811 13941 : dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2812 : radio_list) {
2813 6990 : if (ifs != wpa_s) {
2814 39 : wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2815 39 : ifs->ifname);
2816 39 : free_hw_features(ifs);
2817 39 : ifs->hw.modes = wpa_drv_get_hw_feature_data(
2818 : ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2819 : }
2820 : }
2821 : }
2822 :
2823 :
2824 1503 : static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2825 : const u8 *frame, size_t len, int freq,
2826 : int rssi)
2827 : {
2828 : const struct ieee80211_mgmt *mgmt;
2829 : const u8 *payload;
2830 : size_t plen;
2831 : u8 category;
2832 :
2833 1503 : if (len < IEEE80211_HDRLEN + 2)
2834 0 : return;
2835 :
2836 1503 : mgmt = (const struct ieee80211_mgmt *) frame;
2837 1503 : payload = frame + IEEE80211_HDRLEN;
2838 1503 : category = *payload++;
2839 1503 : plen = len - IEEE80211_HDRLEN - 1;
2840 :
2841 1503 : wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2842 : " Category=%u DataLen=%d freq=%d MHz",
2843 : MAC2STR(mgmt->sa), category, (int) plen, freq);
2844 :
2845 1503 : if (category == WLAN_ACTION_WMM) {
2846 15 : wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
2847 15 : return;
2848 : }
2849 :
2850 : #ifdef CONFIG_IEEE80211R
2851 1488 : if (category == WLAN_ACTION_FT) {
2852 109 : ft_rx_action(wpa_s, payload, plen);
2853 109 : return;
2854 : }
2855 : #endif /* CONFIG_IEEE80211R */
2856 :
2857 : #ifdef CONFIG_IEEE80211W
2858 : #ifdef CONFIG_SME
2859 1379 : if (category == WLAN_ACTION_SA_QUERY) {
2860 3 : sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2861 3 : return;
2862 : }
2863 : #endif /* CONFIG_SME */
2864 : #endif /* CONFIG_IEEE80211W */
2865 :
2866 : #ifdef CONFIG_WNM
2867 1376 : if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2868 43 : ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2869 43 : return;
2870 : }
2871 : #endif /* CONFIG_WNM */
2872 :
2873 : #ifdef CONFIG_GAS
2874 1457 : if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2875 1334 : mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
2876 1210 : gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
2877 1210 : mgmt->u.action.category,
2878 : payload, plen, freq) == 0)
2879 396 : return;
2880 : #endif /* CONFIG_GAS */
2881 :
2882 : #ifdef CONFIG_TDLS
2883 1733 : if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2884 796 : payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2885 2 : wpa_dbg(wpa_s, MSG_DEBUG,
2886 : "TDLS: Received Discovery Response from " MACSTR,
2887 : MAC2STR(mgmt->sa));
2888 2 : return;
2889 : }
2890 : #endif /* CONFIG_TDLS */
2891 :
2892 : #ifdef CONFIG_INTERWORKING
2893 936 : if (category == WLAN_ACTION_QOS && plen >= 1 &&
2894 1 : payload[0] == QOS_QOS_MAP_CONFIG) {
2895 1 : const u8 *pos = payload + 1;
2896 1 : size_t qlen = plen - 1;
2897 1 : wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2898 : MACSTR, MAC2STR(mgmt->sa));
2899 1 : if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2900 2 : qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2901 2 : pos[1] <= qlen - 2 && pos[1] >= 16)
2902 1 : wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2903 1 : return;
2904 : }
2905 : #endif /* CONFIG_INTERWORKING */
2906 :
2907 934 : if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
2908 0 : payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
2909 0 : wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
2910 0 : return;
2911 : }
2912 :
2913 934 : if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
2914 0 : payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
2915 0 : wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
2916 : payload + 1, plen - 1,
2917 : rssi);
2918 0 : return;
2919 : }
2920 :
2921 934 : wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2922 : category, payload, plen, freq);
2923 934 : if (wpa_s->ifmsh)
2924 117 : mesh_mpm_action_rx(wpa_s, mgmt, len);
2925 : }
2926 :
2927 :
2928 4 : static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2929 : union wpa_event_data *event)
2930 : {
2931 : #ifdef CONFIG_P2P
2932 : struct wpa_supplicant *ifs;
2933 : #endif /* CONFIG_P2P */
2934 : struct wpa_freq_range_list *list;
2935 4 : char *str = NULL;
2936 :
2937 4 : list = &event->freq_range;
2938 :
2939 4 : if (list->num)
2940 2 : str = freq_range_list_str(list);
2941 4 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2942 : str ? str : "");
2943 :
2944 : #ifdef CONFIG_P2P
2945 4 : if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2946 0 : wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2947 : __func__);
2948 : } else {
2949 4 : wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2950 4 : wpas_p2p_update_channel_list(wpa_s);
2951 : }
2952 :
2953 8 : for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2954 : int freq;
2955 7 : if (!ifs->current_ssid ||
2956 6 : !ifs->current_ssid->p2p_group ||
2957 3 : (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2958 0 : ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2959 1 : continue;
2960 :
2961 3 : freq = ifs->current_ssid->frequency;
2962 3 : if (!freq_range_list_includes(list, freq)) {
2963 2 : wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2964 : freq);
2965 2 : continue;
2966 : }
2967 :
2968 1 : wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2969 : freq);
2970 : /* TODO: Consider using CSA or removing the group within
2971 : * wpa_supplicant */
2972 1 : wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2973 : }
2974 : #endif /* CONFIG_P2P */
2975 :
2976 4 : os_free(str);
2977 4 : }
2978 :
2979 :
2980 0 : static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
2981 : union wpa_event_data *data)
2982 : {
2983 0 : wpa_dbg(wpa_s, MSG_DEBUG,
2984 : "Connection authorized by device, previous state %d",
2985 : wpa_s->wpa_state);
2986 0 : if (wpa_s->wpa_state == WPA_ASSOCIATED) {
2987 0 : wpa_supplicant_cancel_auth_timeout(wpa_s);
2988 0 : wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2989 0 : eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2990 0 : eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2991 : }
2992 0 : wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
2993 0 : wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
2994 : data->assoc_info.ptk_kck_len,
2995 : data->assoc_info.ptk_kek,
2996 : data->assoc_info.ptk_kek_len);
2997 0 : }
2998 :
2999 :
3000 31778 : void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3001 : union wpa_event_data *data)
3002 : {
3003 31778 : struct wpa_supplicant *wpa_s = ctx;
3004 :
3005 31778 : if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3006 29 : event != EVENT_INTERFACE_ENABLED &&
3007 17 : event != EVENT_INTERFACE_STATUS &&
3008 : event != EVENT_SCHED_SCAN_STOPPED) {
3009 17 : wpa_dbg(wpa_s, MSG_DEBUG,
3010 : "Ignore event %s (%d) while interface is disabled",
3011 : event_to_string(event), event);
3012 31795 : return;
3013 : }
3014 :
3015 : #ifndef CONFIG_NO_STDOUT_DEBUG
3016 : {
3017 31761 : int level = MSG_DEBUG;
3018 :
3019 31761 : if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
3020 : const struct ieee80211_hdr *hdr;
3021 : u16 fc;
3022 4178 : hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3023 4178 : fc = le_to_host16(hdr->frame_control);
3024 8356 : if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3025 4178 : WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3026 337 : level = MSG_EXCESSIVE;
3027 : }
3028 :
3029 31761 : wpa_dbg(wpa_s, level, "Event %s (%d) received",
3030 : event_to_string(event), event);
3031 : }
3032 : #endif /* CONFIG_NO_STDOUT_DEBUG */
3033 :
3034 31761 : switch (event) {
3035 : case EVENT_AUTH:
3036 2380 : sme_event_auth(wpa_s, data);
3037 2380 : break;
3038 : case EVENT_ASSOC:
3039 2477 : wpa_supplicant_event_assoc(wpa_s, data);
3040 2477 : if (data && data->assoc_info.authorized)
3041 0 : wpa_supplicant_event_assoc_auth(wpa_s, data);
3042 2477 : break;
3043 : case EVENT_DISASSOC:
3044 12 : wpas_event_disassoc(wpa_s,
3045 : data ? &data->disassoc_info : NULL);
3046 12 : break;
3047 : case EVENT_DEAUTH:
3048 2377 : wpas_event_deauth(wpa_s,
3049 : data ? &data->deauth_info : NULL);
3050 2377 : break;
3051 : case EVENT_MICHAEL_MIC_FAILURE:
3052 4 : wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3053 4 : break;
3054 : #ifndef CONFIG_NO_SCAN_PROCESSING
3055 : case EVENT_SCAN_STARTED:
3056 2742 : os_get_reltime(&wpa_s->scan_start_time);
3057 2742 : if (wpa_s->own_scan_requested) {
3058 : struct os_reltime diff;
3059 :
3060 2729 : os_reltime_sub(&wpa_s->scan_start_time,
3061 : &wpa_s->scan_trigger_time, &diff);
3062 2729 : wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3063 : diff.sec, diff.usec);
3064 2729 : wpa_s->own_scan_requested = 0;
3065 2729 : wpa_s->own_scan_running = 1;
3066 2729 : if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3067 : wpa_s->manual_scan_use_id) {
3068 15 : wpa_msg_ctrl(wpa_s, MSG_INFO,
3069 : WPA_EVENT_SCAN_STARTED "id=%u",
3070 : wpa_s->manual_scan_id);
3071 : } else {
3072 2714 : wpa_msg_ctrl(wpa_s, MSG_INFO,
3073 : WPA_EVENT_SCAN_STARTED);
3074 : }
3075 : } else {
3076 13 : wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3077 13 : wpa_s->radio->external_scan_running = 1;
3078 13 : wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
3079 : }
3080 2742 : break;
3081 : case EVENT_SCAN_RESULTS:
3082 2755 : if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3083 : struct os_reltime now, diff;
3084 2739 : os_get_reltime(&now);
3085 2739 : os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3086 2739 : wpa_s->scan_start_time.sec = 0;
3087 2739 : wpa_s->scan_start_time.usec = 0;
3088 2739 : wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3089 : diff.sec, diff.usec);
3090 : }
3091 2755 : wpa_supplicant_event_scan_results(wpa_s, data);
3092 2755 : wpa_s->own_scan_running = 0;
3093 2755 : wpa_s->radio->external_scan_running = 0;
3094 2755 : radio_work_check_next(wpa_s);
3095 2755 : break;
3096 : #endif /* CONFIG_NO_SCAN_PROCESSING */
3097 : case EVENT_ASSOCINFO:
3098 15 : wpa_supplicant_event_associnfo(wpa_s, data);
3099 15 : break;
3100 : case EVENT_INTERFACE_STATUS:
3101 20 : wpa_supplicant_event_interface_status(wpa_s, data);
3102 20 : break;
3103 : case EVENT_PMKID_CANDIDATE:
3104 0 : wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3105 0 : break;
3106 : #ifdef CONFIG_PEERKEY
3107 : case EVENT_STKSTART:
3108 0 : wpa_supplicant_event_stkstart(wpa_s, data);
3109 0 : break;
3110 : #endif /* CONFIG_PEERKEY */
3111 : #ifdef CONFIG_TDLS
3112 : case EVENT_TDLS:
3113 0 : wpa_supplicant_event_tdls(wpa_s, data);
3114 0 : break;
3115 : #endif /* CONFIG_TDLS */
3116 : #ifdef CONFIG_WNM
3117 : case EVENT_WNM:
3118 0 : wpa_supplicant_event_wnm(wpa_s, data);
3119 0 : break;
3120 : #endif /* CONFIG_WNM */
3121 : #ifdef CONFIG_IEEE80211R
3122 : case EVENT_FT_RESPONSE:
3123 113 : wpa_supplicant_event_ft_response(wpa_s, data);
3124 113 : break;
3125 : #endif /* CONFIG_IEEE80211R */
3126 : #ifdef CONFIG_IBSS_RSN
3127 : case EVENT_IBSS_RSN_START:
3128 21 : wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3129 21 : break;
3130 : #endif /* CONFIG_IBSS_RSN */
3131 : case EVENT_ASSOC_REJECT:
3132 15 : if (data->assoc_reject.bssid)
3133 84 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3134 : "bssid=" MACSTR " status_code=%u",
3135 72 : MAC2STR(data->assoc_reject.bssid),
3136 12 : data->assoc_reject.status_code);
3137 : else
3138 3 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3139 : "status_code=%u",
3140 3 : data->assoc_reject.status_code);
3141 15 : if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3142 9 : sme_event_assoc_reject(wpa_s, data);
3143 : else {
3144 6 : const u8 *bssid = data->assoc_reject.bssid;
3145 6 : if (bssid == NULL || is_zero_ether_addr(bssid))
3146 3 : bssid = wpa_s->pending_bssid;
3147 6 : wpas_connection_failed(wpa_s, bssid);
3148 6 : wpa_supplicant_mark_disassoc(wpa_s);
3149 : }
3150 15 : break;
3151 : case EVENT_AUTH_TIMED_OUT:
3152 : /* It is possible to get this event from earlier connection */
3153 42 : if (wpa_s->current_ssid &&
3154 21 : wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3155 0 : wpa_dbg(wpa_s, MSG_DEBUG,
3156 : "Ignore AUTH_TIMED_OUT in mesh configuration");
3157 0 : break;
3158 : }
3159 21 : if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3160 20 : sme_event_auth_timed_out(wpa_s, data);
3161 21 : break;
3162 : case EVENT_ASSOC_TIMED_OUT:
3163 : /* It is possible to get this event from earlier connection */
3164 2 : if (wpa_s->current_ssid &&
3165 1 : wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3166 0 : wpa_dbg(wpa_s, MSG_DEBUG,
3167 : "Ignore ASSOC_TIMED_OUT in mesh configuration");
3168 0 : break;
3169 : }
3170 1 : if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3171 1 : sme_event_assoc_timed_out(wpa_s, data);
3172 1 : break;
3173 : case EVENT_TX_STATUS:
3174 2980 : wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3175 : " type=%d stype=%d",
3176 : MAC2STR(data->tx_status.dst),
3177 : data->tx_status.type, data->tx_status.stype);
3178 : #ifdef CONFIG_AP
3179 2980 : if (wpa_s->ap_iface == NULL) {
3180 : #ifdef CONFIG_OFFCHANNEL
3181 3340 : if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3182 1670 : data->tx_status.stype == WLAN_FC_STYPE_ACTION)
3183 1668 : offchannel_send_action_tx_status(
3184 : wpa_s, data->tx_status.dst,
3185 : data->tx_status.data,
3186 : data->tx_status.data_len,
3187 1668 : data->tx_status.ack ?
3188 : OFFCHANNEL_SEND_ACTION_SUCCESS :
3189 : OFFCHANNEL_SEND_ACTION_NO_ACK);
3190 : #endif /* CONFIG_OFFCHANNEL */
3191 1670 : break;
3192 : }
3193 : #endif /* CONFIG_AP */
3194 : #ifdef CONFIG_OFFCHANNEL
3195 1310 : wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3196 : MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3197 : /*
3198 : * Catch TX status events for Action frames we sent via group
3199 : * interface in GO mode.
3200 : */
3201 2610 : if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3202 1385 : data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3203 85 : os_memcmp(wpa_s->parent->pending_action_dst,
3204 : data->tx_status.dst, ETH_ALEN) == 0) {
3205 77 : offchannel_send_action_tx_status(
3206 : wpa_s->parent, data->tx_status.dst,
3207 : data->tx_status.data,
3208 : data->tx_status.data_len,
3209 77 : data->tx_status.ack ?
3210 : OFFCHANNEL_SEND_ACTION_SUCCESS :
3211 : OFFCHANNEL_SEND_ACTION_NO_ACK);
3212 77 : break;
3213 : }
3214 : #endif /* CONFIG_OFFCHANNEL */
3215 : #ifdef CONFIG_AP
3216 1233 : switch (data->tx_status.type) {
3217 : case WLAN_FC_TYPE_MGMT:
3218 2446 : ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3219 : data->tx_status.data_len,
3220 1223 : data->tx_status.stype,
3221 : data->tx_status.ack);
3222 1223 : break;
3223 : case WLAN_FC_TYPE_DATA:
3224 10 : ap_tx_status(wpa_s, data->tx_status.dst,
3225 : data->tx_status.data,
3226 : data->tx_status.data_len,
3227 : data->tx_status.ack);
3228 10 : break;
3229 : }
3230 : #endif /* CONFIG_AP */
3231 1233 : break;
3232 : #ifdef CONFIG_AP
3233 : case EVENT_EAPOL_TX_STATUS:
3234 3158 : ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3235 : data->eapol_tx_status.data,
3236 1579 : data->eapol_tx_status.data_len,
3237 : data->eapol_tx_status.ack);
3238 1579 : break;
3239 : case EVENT_DRIVER_CLIENT_POLL_OK:
3240 0 : ap_client_poll_ok(wpa_s, data->client_poll.addr);
3241 0 : break;
3242 : case EVENT_RX_FROM_UNKNOWN:
3243 0 : if (wpa_s->ap_iface == NULL)
3244 0 : break;
3245 0 : ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3246 : data->rx_from_unknown.wds);
3247 0 : break;
3248 : case EVENT_CH_SWITCH:
3249 15 : if (!data)
3250 0 : break;
3251 15 : if (!wpa_s->ap_iface) {
3252 14 : wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3253 : "event in non-AP mode");
3254 14 : break;
3255 : }
3256 :
3257 2 : wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3258 : data->ch_switch.ht_enabled,
3259 : data->ch_switch.ch_offset,
3260 1 : data->ch_switch.ch_width,
3261 : data->ch_switch.cf1,
3262 : data->ch_switch.cf2);
3263 1 : break;
3264 : #endif /* CONFIG_AP */
3265 : case EVENT_RX_MGMT: {
3266 : u16 fc, stype;
3267 : const struct ieee80211_mgmt *mgmt;
3268 :
3269 : #ifdef CONFIG_TESTING_OPTIONS
3270 4178 : if (wpa_s->ext_mgmt_frame_handling) {
3271 48 : struct rx_mgmt *rx = &data->rx_mgmt;
3272 48 : size_t hex_len = 2 * rx->frame_len + 1;
3273 48 : char *hex = os_malloc(hex_len);
3274 48 : if (hex) {
3275 48 : wpa_snprintf_hex(hex, hex_len,
3276 : rx->frame, rx->frame_len);
3277 48 : wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3278 : rx->freq, rx->datarate, rx->ssi_signal,
3279 : hex);
3280 48 : os_free(hex);
3281 : }
3282 48 : break;
3283 : }
3284 : #endif /* CONFIG_TESTING_OPTIONS */
3285 :
3286 4130 : mgmt = (const struct ieee80211_mgmt *)
3287 : data->rx_mgmt.frame;
3288 4130 : fc = le_to_host16(mgmt->frame_control);
3289 4130 : stype = WLAN_FC_GET_STYPE(fc);
3290 :
3291 : #ifdef CONFIG_AP
3292 4130 : if (wpa_s->ap_iface == NULL) {
3293 : #endif /* CONFIG_AP */
3294 : #ifdef CONFIG_P2P
3295 2986 : if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3296 668 : data->rx_mgmt.frame_len > 24) {
3297 668 : const u8 *src = mgmt->sa;
3298 668 : const u8 *ie = mgmt->u.probe_req.variable;
3299 1336 : size_t ie_len = data->rx_mgmt.frame_len -
3300 668 : (mgmt->u.probe_req.variable -
3301 668 : data->rx_mgmt.frame);
3302 1336 : wpas_p2p_probe_req_rx(
3303 668 : wpa_s, src, mgmt->da,
3304 668 : mgmt->bssid, ie, ie_len,
3305 : data->rx_mgmt.ssi_signal);
3306 668 : break;
3307 : }
3308 : #endif /* CONFIG_P2P */
3309 : #ifdef CONFIG_IBSS_RSN
3310 2142 : if (wpa_s->current_ssid &&
3311 510 : wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3312 18 : stype == WLAN_FC_STYPE_AUTH &&
3313 18 : data->rx_mgmt.frame_len >= 30) {
3314 18 : wpa_supplicant_event_ibss_auth(wpa_s, data);
3315 18 : break;
3316 : }
3317 : #endif /* CONFIG_IBSS_RSN */
3318 :
3319 1632 : if (stype == WLAN_FC_STYPE_ACTION) {
3320 1503 : wpas_event_rx_mgmt_action(
3321 : wpa_s, data->rx_mgmt.frame,
3322 : data->rx_mgmt.frame_len,
3323 : data->rx_mgmt.freq,
3324 : data->rx_mgmt.ssi_signal);
3325 1503 : break;
3326 : }
3327 :
3328 129 : if (wpa_s->ifmsh) {
3329 119 : mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
3330 119 : break;
3331 : }
3332 :
3333 10 : wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3334 : "management frame in non-AP mode");
3335 10 : break;
3336 : #ifdef CONFIG_AP
3337 : }
3338 :
3339 2190 : if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3340 378 : data->rx_mgmt.frame_len > 24) {
3341 378 : const u8 *ie = mgmt->u.probe_req.variable;
3342 756 : size_t ie_len = data->rx_mgmt.frame_len -
3343 378 : (mgmt->u.probe_req.variable -
3344 378 : data->rx_mgmt.frame);
3345 :
3346 756 : wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3347 378 : mgmt->bssid, ie, ie_len,
3348 378 : data->rx_mgmt.ssi_signal);
3349 : }
3350 :
3351 1812 : ap_mgmt_rx(wpa_s, &data->rx_mgmt);
3352 : #endif /* CONFIG_AP */
3353 1812 : break;
3354 : }
3355 : case EVENT_RX_PROBE_REQ:
3356 0 : if (data->rx_probe_req.sa == NULL ||
3357 0 : data->rx_probe_req.ie == NULL)
3358 : break;
3359 : #ifdef CONFIG_AP
3360 0 : if (wpa_s->ap_iface) {
3361 0 : hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3362 : data->rx_probe_req.sa,
3363 : data->rx_probe_req.da,
3364 : data->rx_probe_req.bssid,
3365 : data->rx_probe_req.ie,
3366 : data->rx_probe_req.ie_len,
3367 : data->rx_probe_req.ssi_signal);
3368 0 : break;
3369 : }
3370 : #endif /* CONFIG_AP */
3371 0 : wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3372 : data->rx_probe_req.da,
3373 : data->rx_probe_req.bssid,
3374 : data->rx_probe_req.ie,
3375 : data->rx_probe_req.ie_len,
3376 : data->rx_probe_req.ssi_signal);
3377 0 : break;
3378 : case EVENT_REMAIN_ON_CHANNEL:
3379 : #ifdef CONFIG_OFFCHANNEL
3380 1516 : offchannel_remain_on_channel_cb(
3381 : wpa_s, data->remain_on_channel.freq,
3382 : data->remain_on_channel.duration);
3383 : #endif /* CONFIG_OFFCHANNEL */
3384 1516 : wpas_p2p_remain_on_channel_cb(
3385 : wpa_s, data->remain_on_channel.freq,
3386 : data->remain_on_channel.duration);
3387 1516 : break;
3388 : case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3389 : #ifdef CONFIG_OFFCHANNEL
3390 1429 : offchannel_cancel_remain_on_channel_cb(
3391 : wpa_s, data->remain_on_channel.freq);
3392 : #endif /* CONFIG_OFFCHANNEL */
3393 1429 : wpas_p2p_cancel_remain_on_channel_cb(
3394 : wpa_s, data->remain_on_channel.freq);
3395 1429 : break;
3396 : case EVENT_EAPOL_RX:
3397 0 : wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3398 : data->eapol_rx.data,
3399 : data->eapol_rx.data_len);
3400 0 : break;
3401 : case EVENT_SIGNAL_CHANGE:
3402 5 : wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3403 : "above=%d signal=%d noise=%d txrate=%d",
3404 : data->signal_change.above_threshold,
3405 : data->signal_change.current_signal,
3406 : data->signal_change.current_noise,
3407 : data->signal_change.current_txrate);
3408 5 : bgscan_notify_signal_change(
3409 : wpa_s, data->signal_change.above_threshold,
3410 : data->signal_change.current_signal,
3411 : data->signal_change.current_noise,
3412 : data->signal_change.current_txrate);
3413 5 : break;
3414 : case EVENT_INTERFACE_ENABLED:
3415 13 : wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3416 13 : if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3417 11 : wpa_supplicant_update_mac_addr(wpa_s);
3418 11 : if (wpa_s->p2p_mgmt) {
3419 0 : wpa_supplicant_set_state(wpa_s,
3420 : WPA_DISCONNECTED);
3421 0 : break;
3422 : }
3423 :
3424 : #ifdef CONFIG_AP
3425 11 : if (!wpa_s->ap_iface) {
3426 11 : wpa_supplicant_set_state(wpa_s,
3427 : WPA_DISCONNECTED);
3428 11 : wpa_s->scan_req = NORMAL_SCAN_REQ;
3429 11 : wpa_supplicant_req_scan(wpa_s, 0, 0);
3430 : } else
3431 0 : wpa_supplicant_set_state(wpa_s,
3432 : WPA_COMPLETED);
3433 : #else /* CONFIG_AP */
3434 : wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3435 : wpa_supplicant_req_scan(wpa_s, 0, 0);
3436 : #endif /* CONFIG_AP */
3437 : }
3438 13 : break;
3439 : case EVENT_INTERFACE_DISABLED:
3440 25 : wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3441 : #ifdef CONFIG_P2P
3442 48 : if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3443 30 : (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3444 2 : wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3445 : /*
3446 : * Mark interface disabled if this happens to end up not
3447 : * being removed as a separate P2P group interface.
3448 : */
3449 3 : wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3450 : /*
3451 : * The interface was externally disabled. Remove
3452 : * it assuming an external entity will start a
3453 : * new session if needed.
3454 : */
3455 6 : if (wpa_s->current_ssid &&
3456 3 : wpa_s->current_ssid->p2p_group)
3457 3 : wpas_p2p_interface_unavailable(wpa_s);
3458 : else
3459 0 : wpas_p2p_disconnect(wpa_s);
3460 : /*
3461 : * wpa_s instance may have been freed, so must not use
3462 : * it here anymore.
3463 : */
3464 3 : break;
3465 : }
3466 23 : if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3467 1 : p2p_in_progress(wpa_s->global->p2p) > 1) {
3468 : /* This radio work will be cancelled, so clear P2P
3469 : * state as well.
3470 : */
3471 1 : p2p_stop_find(wpa_s->global->p2p);
3472 : }
3473 : #endif /* CONFIG_P2P */
3474 :
3475 22 : if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3476 : /*
3477 : * Indicate disconnection to keep ctrl_iface events
3478 : * consistent.
3479 : */
3480 4 : wpa_supplicant_event_disassoc(
3481 : wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3482 : }
3483 22 : wpa_supplicant_mark_disassoc(wpa_s);
3484 22 : radio_remove_works(wpa_s, NULL, 0);
3485 :
3486 22 : wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3487 22 : break;
3488 : case EVENT_CHANNEL_LIST_CHANGED:
3489 6951 : wpa_supplicant_update_channel_list(
3490 : wpa_s, &data->channel_list_changed);
3491 6951 : break;
3492 : case EVENT_INTERFACE_UNAVAILABLE:
3493 2 : wpas_p2p_interface_unavailable(wpa_s);
3494 2 : break;
3495 : case EVENT_BEST_CHANNEL:
3496 0 : wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3497 : "(%d %d %d)",
3498 : data->best_chan.freq_24, data->best_chan.freq_5,
3499 : data->best_chan.freq_overall);
3500 0 : wpa_s->best_24_freq = data->best_chan.freq_24;
3501 0 : wpa_s->best_5_freq = data->best_chan.freq_5;
3502 0 : wpa_s->best_overall_freq = data->best_chan.freq_overall;
3503 0 : wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3504 : data->best_chan.freq_5,
3505 : data->best_chan.freq_overall);
3506 0 : break;
3507 : case EVENT_UNPROT_DEAUTH:
3508 19 : wpa_supplicant_event_unprot_deauth(wpa_s,
3509 : &data->unprot_deauth);
3510 19 : break;
3511 : case EVENT_UNPROT_DISASSOC:
3512 19 : wpa_supplicant_event_unprot_disassoc(wpa_s,
3513 : &data->unprot_disassoc);
3514 19 : break;
3515 : case EVENT_STATION_LOW_ACK:
3516 : #ifdef CONFIG_AP
3517 0 : if (wpa_s->ap_iface && data)
3518 0 : hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3519 0 : data->low_ack.addr);
3520 : #endif /* CONFIG_AP */
3521 : #ifdef CONFIG_TDLS
3522 0 : if (data)
3523 0 : wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3524 0 : data->low_ack.addr);
3525 : #endif /* CONFIG_TDLS */
3526 0 : break;
3527 : case EVENT_IBSS_PEER_LOST:
3528 : #ifdef CONFIG_IBSS_RSN
3529 2 : ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3530 : #endif /* CONFIG_IBSS_RSN */
3531 2 : break;
3532 : case EVENT_DRIVER_GTK_REKEY:
3533 0 : if (os_memcmp(data->driver_gtk_rekey.bssid,
3534 : wpa_s->bssid, ETH_ALEN))
3535 0 : break;
3536 0 : if (!wpa_s->wpa)
3537 0 : break;
3538 0 : wpa_sm_update_replay_ctr(wpa_s->wpa,
3539 : data->driver_gtk_rekey.replay_ctr);
3540 0 : break;
3541 : case EVENT_SCHED_SCAN_STOPPED:
3542 0 : wpa_s->pno = 0;
3543 0 : wpa_s->sched_scanning = 0;
3544 0 : wpa_supplicant_notify_scanning(wpa_s, 0);
3545 :
3546 0 : if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3547 0 : break;
3548 :
3549 : /*
3550 : * Start a new sched scan to continue searching for more SSIDs
3551 : * either if timed out or PNO schedule scan is pending.
3552 : */
3553 0 : if (wpa_s->sched_scan_timed_out) {
3554 0 : wpa_supplicant_req_sched_scan(wpa_s);
3555 0 : } else if (wpa_s->pno_sched_pending) {
3556 0 : wpa_s->pno_sched_pending = 0;
3557 0 : wpas_start_pno(wpa_s);
3558 : }
3559 :
3560 0 : break;
3561 : case EVENT_WPS_BUTTON_PUSHED:
3562 : #ifdef CONFIG_WPS
3563 0 : wpas_wps_start_pbc(wpa_s, NULL, 0);
3564 : #endif /* CONFIG_WPS */
3565 0 : break;
3566 : case EVENT_AVOID_FREQUENCIES:
3567 4 : wpa_supplicant_notify_avoid_freq(wpa_s, data);
3568 4 : break;
3569 : case EVENT_CONNECT_FAILED_REASON:
3570 : #ifdef CONFIG_AP
3571 0 : if (!wpa_s->ap_iface || !data)
3572 : break;
3573 0 : hostapd_event_connect_failed_reason(
3574 0 : wpa_s->ap_iface->bss[0],
3575 0 : data->connect_failed_reason.addr,
3576 0 : data->connect_failed_reason.code);
3577 : #endif /* CONFIG_AP */
3578 0 : break;
3579 : case EVENT_NEW_PEER_CANDIDATE:
3580 : #ifdef CONFIG_MESH
3581 50 : if (!wpa_s->ifmsh || !data)
3582 : break;
3583 50 : wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3584 : data->mesh_peer.ies,
3585 : data->mesh_peer.ie_len);
3586 : #endif /* CONFIG_MESH */
3587 50 : break;
3588 : default:
3589 21 : wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3590 21 : break;
3591 : }
3592 : }
|