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