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