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