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