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