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