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