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