]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/events.c
Support driver-based BSS selection in ap_scan=1 mode
[thirdparty/hostap.git] / wpa_supplicant / events.c
1 /*
2 * WPA Supplicant - Driver event processing
3 * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "pcsc_funcs.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "common/wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ap/hostapd.h"
31 #include "p2p/p2p.h"
32 #include "notify.h"
33 #include "common/ieee802_11_defs.h"
34 #include "common/ieee802_11_common.h"
35 #include "crypto/random.h"
36 #include "blacklist.h"
37 #include "wpas_glue.h"
38 #include "wps_supplicant.h"
39 #include "ibss_rsn.h"
40 #include "sme.h"
41 #include "p2p_supplicant.h"
42 #include "bgscan.h"
43 #include "ap.h"
44 #include "bss.h"
45 #include "mlme.h"
46 #include "scan.h"
47
48
49 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
50 {
51 struct wpa_ssid *ssid, *old_ssid;
52
53 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54 return 0;
55
56 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
57 "information");
58 ssid = wpa_supplicant_get_ssid(wpa_s);
59 if (ssid == NULL) {
60 wpa_msg(wpa_s, MSG_INFO,
61 "No network configuration found for the current AP");
62 return -1;
63 }
64
65 if (ssid->disabled) {
66 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
67 return -1;
68 }
69
70 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
71 "current AP");
72 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
73 WPA_KEY_MGMT_WPA_NONE |
74 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
75 WPA_KEY_MGMT_PSK_SHA256 |
76 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
77 u8 wpa_ie[80];
78 size_t wpa_ie_len = sizeof(wpa_ie);
79 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
80 wpa_ie, &wpa_ie_len);
81 } else {
82 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
83 }
84
85 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
86 eapol_sm_invalidate_cached_session(wpa_s->eapol);
87 old_ssid = wpa_s->current_ssid;
88 wpa_s->current_ssid = ssid;
89 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
90 wpa_supplicant_initiate_eapol(wpa_s);
91 if (old_ssid != wpa_s->current_ssid)
92 wpas_notify_network_changed(wpa_s);
93
94 return 0;
95 }
96
97
98 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99 void *sock_ctx)
100 {
101 struct wpa_supplicant *wpa_s = eloop_ctx;
102
103 if (wpa_s->countermeasures) {
104 wpa_s->countermeasures = 0;
105 wpa_drv_set_countermeasures(wpa_s, 0);
106 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
107 wpa_supplicant_req_scan(wpa_s, 0, 0);
108 }
109 }
110
111
112 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
113 {
114 int bssid_changed;
115
116 #ifdef CONFIG_IBSS_RSN
117 ibss_rsn_deinit(wpa_s->ibss_rsn);
118 wpa_s->ibss_rsn = NULL;
119 #endif /* CONFIG_IBSS_RSN */
120
121 #ifdef CONFIG_AP
122 wpa_supplicant_ap_deinit(wpa_s);
123 #endif /* CONFIG_AP */
124
125 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
126 return;
127
128 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
129 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
130 os_memset(wpa_s->bssid, 0, ETH_ALEN);
131 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
132 wpa_s->current_bss = NULL;
133 wpa_s->assoc_freq = 0;
134 #ifdef CONFIG_IEEE80211R
135 #ifdef CONFIG_SME
136 if (wpa_s->sme.ft_ies)
137 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
138 #endif /* CONFIG_SME */
139 #endif /* CONFIG_IEEE80211R */
140
141 if (bssid_changed)
142 wpas_notify_bssid_changed(wpa_s);
143
144 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
145 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
146 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
147 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
148 wpa_s->ap_ies_from_associnfo = 0;
149 }
150
151
152 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
153 {
154 struct wpa_ie_data ie;
155 int pmksa_set = -1;
156 size_t i;
157
158 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
159 ie.pmkid == NULL)
160 return;
161
162 for (i = 0; i < ie.num_pmkid; i++) {
163 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
164 ie.pmkid + i * PMKID_LEN,
165 NULL, NULL, 0);
166 if (pmksa_set == 0) {
167 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
168 break;
169 }
170 }
171
172 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
173 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
174 }
175
176
177 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
178 union wpa_event_data *data)
179 {
180 if (data == NULL) {
181 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
182 "event");
183 return;
184 }
185 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
186 " index=%d preauth=%d",
187 MAC2STR(data->pmkid_candidate.bssid),
188 data->pmkid_candidate.index,
189 data->pmkid_candidate.preauth);
190
191 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
192 data->pmkid_candidate.index,
193 data->pmkid_candidate.preauth);
194 }
195
196
197 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
198 {
199 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
200 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
201 return 0;
202
203 #ifdef IEEE8021X_EAPOL
204 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
205 wpa_s->current_ssid &&
206 !(wpa_s->current_ssid->eapol_flags &
207 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
208 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
209 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
210 * plaintext or static WEP keys). */
211 return 0;
212 }
213 #endif /* IEEE8021X_EAPOL */
214
215 return 1;
216 }
217
218
219 /**
220 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
221 * @wpa_s: pointer to wpa_supplicant data
222 * @ssid: Configuration data for the network
223 * Returns: 0 on success, -1 on failure
224 *
225 * This function is called when starting authentication with a network that is
226 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
227 */
228 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
229 struct wpa_ssid *ssid)
230 {
231 #ifdef IEEE8021X_EAPOL
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 aka = 1;
249 }
250 eap++;
251 }
252 }
253
254 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
255 sim = 0;
256 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
257 aka = 0;
258
259 if (!sim && !aka) {
260 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
261 "use SIM, but neither EAP-SIM nor EAP-AKA are "
262 "enabled");
263 return 0;
264 }
265
266 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
267 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
268 if (sim && aka)
269 type = SCARD_TRY_BOTH;
270 else if (aka)
271 type = SCARD_USIM_ONLY;
272 else
273 type = SCARD_GSM_SIM_ONLY;
274
275 wpa_s->scard = scard_init(type);
276 if (wpa_s->scard == NULL) {
277 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
278 "(pcsc-lite)");
279 return -1;
280 }
281 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
282 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
283 #endif /* IEEE8021X_EAPOL */
284
285 return 0;
286 }
287
288
289 #ifndef CONFIG_NO_SCAN_PROCESSING
290 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
291 struct wpa_ssid *ssid)
292 {
293 int i, privacy = 0;
294
295 if (ssid->mixed_cell)
296 return 1;
297
298 #ifdef CONFIG_WPS
299 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
300 return 1;
301 #endif /* CONFIG_WPS */
302
303 for (i = 0; i < NUM_WEP_KEYS; i++) {
304 if (ssid->wep_key_len[i]) {
305 privacy = 1;
306 break;
307 }
308 }
309 #ifdef IEEE8021X_EAPOL
310 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
311 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
312 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
313 privacy = 1;
314 #endif /* IEEE8021X_EAPOL */
315
316 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
317 privacy = 1;
318
319 if (bss->caps & IEEE80211_CAP_PRIVACY)
320 return privacy;
321 return !privacy;
322 }
323
324
325 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
326 struct wpa_ssid *ssid,
327 struct wpa_scan_res *bss)
328 {
329 struct wpa_ie_data ie;
330 int proto_match = 0;
331 const u8 *rsn_ie, *wpa_ie;
332 int ret;
333 int wep_ok;
334
335 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
336 if (ret >= 0)
337 return ret;
338
339 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
340 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
341 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
342 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
343 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
344
345 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
346 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
347 proto_match++;
348
349 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
350 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
351 "failed");
352 break;
353 }
354
355 if (wep_ok &&
356 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
357 {
358 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
359 "in RSN IE");
360 return 1;
361 }
362
363 if (!(ie.proto & ssid->proto)) {
364 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
365 "mismatch");
366 break;
367 }
368
369 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
370 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
371 "cipher mismatch");
372 break;
373 }
374
375 if (!(ie.group_cipher & ssid->group_cipher)) {
376 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
377 "cipher mismatch");
378 break;
379 }
380
381 if (!(ie.key_mgmt & ssid->key_mgmt)) {
382 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
383 "mismatch");
384 break;
385 }
386
387 #ifdef CONFIG_IEEE80211W
388 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
389 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
390 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
391 "frame protection");
392 break;
393 }
394 #endif /* CONFIG_IEEE80211W */
395
396 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
397 return 1;
398 }
399
400 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
401 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
402 proto_match++;
403
404 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
405 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
406 "failed");
407 break;
408 }
409
410 if (wep_ok &&
411 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
412 {
413 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
414 "in WPA IE");
415 return 1;
416 }
417
418 if (!(ie.proto & ssid->proto)) {
419 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
420 "mismatch");
421 break;
422 }
423
424 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
425 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
426 "cipher mismatch");
427 break;
428 }
429
430 if (!(ie.group_cipher & ssid->group_cipher)) {
431 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
432 "cipher mismatch");
433 break;
434 }
435
436 if (!(ie.key_mgmt & ssid->key_mgmt)) {
437 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
438 "mismatch");
439 break;
440 }
441
442 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
443 return 1;
444 }
445
446 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
447 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
448 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
449 return 0;
450 }
451
452 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
453 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
454 return 1;
455 }
456
457 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
458 "WPA/WPA2");
459
460 return 0;
461 }
462
463
464 static int freq_allowed(int *freqs, int freq)
465 {
466 int i;
467
468 if (freqs == NULL)
469 return 1;
470
471 for (i = 0; freqs[i]; i++)
472 if (freqs[i] == freq)
473 return 1;
474 return 0;
475 }
476
477
478 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
479 int i, struct wpa_scan_res *bss,
480 struct wpa_ssid *group)
481 {
482 const u8 *ssid_;
483 u8 wpa_ie_len, rsn_ie_len, ssid_len;
484 int wpa;
485 struct wpa_blacklist *e;
486 const u8 *ie;
487 struct wpa_ssid *ssid;
488
489 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
490 ssid_ = ie ? ie + 2 : (u8 *) "";
491 ssid_len = ie ? ie[1] : 0;
492
493 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
494 wpa_ie_len = ie ? ie[1] : 0;
495
496 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
497 rsn_ie_len = ie ? ie[1] : 0;
498
499 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
500 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
501 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
502 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
503 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
504
505 e = wpa_blacklist_get(wpa_s, bss->bssid);
506 if (e) {
507 int limit = 1;
508 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
509 /*
510 * When only a single network is enabled, we can
511 * trigger blacklisting on the first failure. This
512 * should not be done with multiple enabled networks to
513 * avoid getting forced to move into a worse ESS on
514 * single error if there are no other BSSes of the
515 * current ESS.
516 */
517 limit = 0;
518 }
519 if (e->count > limit) {
520 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
521 "(count=%d limit=%d)", e->count, limit);
522 return NULL;
523 }
524 }
525
526 if (ssid_len == 0) {
527 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
528 return NULL;
529 }
530
531 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
532
533 for (ssid = group; ssid; ssid = ssid->pnext) {
534 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
535
536 if (ssid->disabled) {
537 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
538 continue;
539 }
540
541 #ifdef CONFIG_WPS
542 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
543 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
544 "(WPS)");
545 continue;
546 }
547
548 if (wpa && ssid->ssid_len == 0 &&
549 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
550 check_ssid = 0;
551
552 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
553 /* Only allow wildcard SSID match if an AP
554 * advertises active WPS operation that matches
555 * with our mode. */
556 check_ssid = 1;
557 if (ssid->ssid_len == 0 &&
558 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
559 check_ssid = 0;
560 }
561 #endif /* CONFIG_WPS */
562
563 if (check_ssid &&
564 (ssid_len != ssid->ssid_len ||
565 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
566 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
567 continue;
568 }
569
570 if (ssid->bssid_set &&
571 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
572 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
573 continue;
574 }
575
576 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
577 continue;
578
579 if (!wpa &&
580 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
581 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
582 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
583 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
584 "not allowed");
585 continue;
586 }
587
588 if (!wpa_supplicant_match_privacy(bss, ssid)) {
589 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
590 "mismatch");
591 continue;
592 }
593
594 if (bss->caps & IEEE80211_CAP_IBSS) {
595 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
596 "network");
597 continue;
598 }
599
600 if (!freq_allowed(ssid->freq_list, bss->freq)) {
601 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
602 "allowed");
603 continue;
604 }
605
606 #ifdef CONFIG_P2P
607 /*
608 * TODO: skip the AP if its P2P IE has Group Formation
609 * bit set in the P2P Group Capability Bitmap and we
610 * are not in Group Formation with that device.
611 */
612 #endif /* CONFIG_P2P */
613
614 /* Matching configuration found */
615 return ssid;
616 }
617
618 /* No matching configuration found */
619 return NULL;
620 }
621
622
623 static struct wpa_bss *
624 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
625 struct wpa_scan_results *scan_res,
626 struct wpa_ssid *group,
627 struct wpa_ssid **selected_ssid)
628 {
629 size_t i;
630
631 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
632 group->priority);
633
634 for (i = 0; i < scan_res->num; i++) {
635 struct wpa_scan_res *bss = scan_res->res[i];
636 const u8 *ie, *ssid;
637 u8 ssid_len;
638
639 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
640 if (!*selected_ssid)
641 continue;
642
643 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
644 ssid = ie ? ie + 2 : (u8 *) "";
645 ssid_len = ie ? ie[1] : 0;
646
647 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
648 " ssid='%s'",
649 MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
650 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
651 }
652
653 return NULL;
654 }
655
656
657 static struct wpa_bss *
658 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
659 struct wpa_scan_results *scan_res,
660 struct wpa_ssid **selected_ssid)
661 {
662 struct wpa_bss *selected = NULL;
663 int prio;
664
665 while (selected == NULL) {
666 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
667 selected = wpa_supplicant_select_bss(
668 wpa_s, scan_res, wpa_s->conf->pssid[prio],
669 selected_ssid);
670 if (selected)
671 break;
672 }
673
674 if (selected == NULL && wpa_s->blacklist) {
675 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
676 "blacklist and try again");
677 wpa_blacklist_clear(wpa_s);
678 wpa_s->blacklist_cleared++;
679 } else if (selected == NULL)
680 break;
681 }
682
683 return selected;
684 }
685
686
687 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
688 int timeout_sec, int timeout_usec)
689 {
690 if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
691 /*
692 * No networks are enabled; short-circuit request so
693 * we don't wait timeout seconds before transitioning
694 * to INACTIVE state.
695 */
696 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
697 return;
698 }
699 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
700 }
701
702
703 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
704 struct wpa_bss *selected,
705 struct wpa_ssid *ssid)
706 {
707 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
708 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
709 "PBC session overlap");
710 #ifdef CONFIG_P2P
711 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
712 return;
713 #endif /* CONFIG_P2P */
714
715 #ifdef CONFIG_WPS
716 wpas_wps_cancel(wpa_s);
717 #endif /* CONFIG_WPS */
718 return;
719 }
720
721 /*
722 * Do not trigger new association unless the BSSID has changed or if
723 * reassociation is requested. If we are in process of associating with
724 * the selected BSSID, do not trigger new attempt.
725 */
726 if (wpa_s->reassociate ||
727 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
728 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
729 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
730 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
731 0))) {
732 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
733 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
734 return;
735 }
736 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
737 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
738 " pending: " MACSTR " wpa_state: %s",
739 wpa_s->reassociate, MAC2STR(selected->bssid),
740 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
741 wpa_supplicant_state_txt(wpa_s->wpa_state));
742 wpa_supplicant_associate(wpa_s, selected, ssid);
743 } else {
744 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
745 "selected AP");
746 }
747 }
748
749
750 static struct wpa_ssid *
751 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
752 {
753 int prio;
754 struct wpa_ssid *ssid;
755
756 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
757 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
758 {
759 if (ssid->disabled)
760 continue;
761 if (ssid->mode == IEEE80211_MODE_IBSS ||
762 ssid->mode == IEEE80211_MODE_AP)
763 return ssid;
764 }
765 }
766 return NULL;
767 }
768
769
770 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
771 * on BSS added and BSS changed events */
772 static void wpa_supplicant_rsn_preauth_scan_results(
773 struct wpa_supplicant *wpa_s)
774 {
775 struct wpa_bss *bss;
776
777 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
778 return;
779
780 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
781 const u8 *ssid, *rsn;
782
783 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
784 if (ssid == NULL)
785 continue;
786
787 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
788 if (rsn == NULL)
789 continue;
790
791 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
792 }
793
794 }
795
796
797 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
798 struct wpa_bss *selected,
799 struct wpa_ssid *ssid,
800 struct wpa_scan_results *scan_res)
801 {
802 size_t i;
803 struct wpa_scan_res *current_bss = NULL;
804 int min_diff;
805
806 if (wpa_s->reassociate)
807 return 1; /* explicit request to reassociate */
808 if (wpa_s->wpa_state < WPA_ASSOCIATED)
809 return 1; /* we are not associated; continue */
810 if (wpa_s->current_ssid == NULL)
811 return 1; /* unknown current SSID */
812 if (wpa_s->current_ssid != ssid)
813 return 1; /* different network block */
814
815 if (wpas_driver_bss_selection(wpa_s))
816 return 0; /* Driver-based roaming */
817
818 for (i = 0; i < scan_res->num; i++) {
819 struct wpa_scan_res *res = scan_res->res[i];
820 const u8 *ie;
821 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
822 continue;
823
824 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
825 if (ie == NULL)
826 continue;
827 if (ie[1] != wpa_s->current_ssid->ssid_len ||
828 os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
829 continue;
830 current_bss = res;
831 break;
832 }
833
834 if (!current_bss)
835 return 1; /* current BSS not seen in scan results */
836
837 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
838 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
839 MAC2STR(current_bss->bssid), current_bss->level);
840 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
841 MAC2STR(selected->bssid), selected->level);
842
843 if (wpa_s->current_ssid->bssid_set &&
844 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
845 0) {
846 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
847 "has preferred BSSID");
848 return 1;
849 }
850
851 min_diff = 2;
852 if (current_bss->level < 0) {
853 if (current_bss->level < -85)
854 min_diff = 1;
855 else if (current_bss->level < -80)
856 min_diff = 2;
857 else if (current_bss->level < -75)
858 min_diff = 3;
859 else if (current_bss->level < -70)
860 min_diff = 4;
861 else
862 min_diff = 5;
863 }
864 if (abs(current_bss->level - selected->level) < min_diff) {
865 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
866 "in signal level");
867 return 0;
868 }
869
870 return 1;
871 }
872
873 /* Return < 0 if no scan results could be fetched. */
874 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
875 union wpa_event_data *data)
876 {
877 struct wpa_bss *selected;
878 struct wpa_ssid *ssid = NULL;
879 struct wpa_scan_results *scan_res;
880 int ap = 0;
881
882 #ifdef CONFIG_AP
883 if (wpa_s->ap_iface)
884 ap = 1;
885 #endif /* CONFIG_AP */
886
887 wpa_supplicant_notify_scanning(wpa_s, 0);
888
889 scan_res = wpa_supplicant_get_scan_results(wpa_s,
890 data ? &data->scan_info :
891 NULL, 1);
892 if (scan_res == NULL) {
893 if (wpa_s->conf->ap_scan == 2 || ap)
894 return -1;
895 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
896 "scanning again");
897 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
898 return -1;
899 }
900
901 #ifndef CONFIG_NO_RANDOM_POOL
902 size_t i, num;
903 num = scan_res->num;
904 if (num > 10)
905 num = 10;
906 for (i = 0; i < num; i++) {
907 u8 buf[5];
908 struct wpa_scan_res *res = scan_res->res[i];
909 buf[0] = res->bssid[5];
910 buf[1] = res->qual & 0xff;
911 buf[2] = res->noise & 0xff;
912 buf[3] = res->level & 0xff;
913 buf[4] = res->tsf & 0xff;
914 random_add_randomness(buf, sizeof(buf));
915 }
916 #endif /* CONFIG_NO_RANDOM_POOL */
917
918 if (wpa_s->scan_res_handler) {
919 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
920 struct wpa_scan_results *scan_res);
921
922 scan_res_handler = wpa_s->scan_res_handler;
923 wpa_s->scan_res_handler = NULL;
924 scan_res_handler(wpa_s, scan_res);
925
926 wpa_scan_results_free(scan_res);
927 return 0;
928 }
929
930 if (ap) {
931 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
932 #ifdef CONFIG_AP
933 if (wpa_s->ap_iface->scan_cb)
934 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
935 #endif /* CONFIG_AP */
936 wpa_scan_results_free(scan_res);
937 return 0;
938 }
939
940 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
941 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
942 wpas_notify_scan_results(wpa_s);
943
944 wpas_notify_scan_done(wpa_s, 1);
945
946 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
947 wpa_scan_results_free(scan_res);
948 return 0;
949 }
950
951 if (wpa_s->disconnected) {
952 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
953 wpa_scan_results_free(scan_res);
954 return 0;
955 }
956
957 if (!wpas_driver_bss_selection(wpa_s) &&
958 bgscan_notify_scan(wpa_s, scan_res) == 1) {
959 wpa_scan_results_free(scan_res);
960 return 0;
961 }
962
963 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
964
965 if (selected) {
966 int skip;
967 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
968 scan_res);
969 wpa_scan_results_free(scan_res);
970 if (skip)
971 return 0;
972 wpa_supplicant_connect(wpa_s, selected, ssid);
973 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
974 } else {
975 wpa_scan_results_free(scan_res);
976 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
977 ssid = wpa_supplicant_pick_new_network(wpa_s);
978 if (ssid) {
979 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
980 wpa_supplicant_associate(wpa_s, NULL, ssid);
981 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
982 } else {
983 int timeout_sec = wpa_s->scan_interval;
984 int timeout_usec = 0;
985 #ifdef CONFIG_P2P
986 if (wpa_s->p2p_in_provisioning) {
987 /*
988 * Use shorter wait during P2P Provisioning
989 * state to speed up group formation.
990 */
991 timeout_sec = 0;
992 timeout_usec = 250000;
993 }
994 #endif /* CONFIG_P2P */
995 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
996 timeout_usec);
997 }
998 }
999 return 0;
1000 }
1001
1002
1003 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1004 union wpa_event_data *data)
1005 {
1006 const char *rn, *rn2;
1007 struct wpa_supplicant *ifs;
1008
1009 if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1010 /*
1011 * If no scan results could be fetched, then no need to
1012 * notify those interfaces that did not actually request
1013 * this scan.
1014 */
1015 return;
1016 }
1017
1018 /*
1019 * Check other interfaces to see if they have the same radio-name. If
1020 * so, they get updated with this same scan info.
1021 */
1022 if (!wpa_s->driver->get_radio_name)
1023 return;
1024
1025 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1026 if (rn == NULL || rn[0] == '\0')
1027 return;
1028
1029 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1030 "sharing same radio (%s) in event_scan_results", rn);
1031
1032 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1033 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1034 continue;
1035
1036 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1037 if (rn2 && os_strcmp(rn, rn2) == 0) {
1038 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1039 "sibling", ifs->ifname);
1040 _wpa_supplicant_event_scan_results(ifs, data);
1041 }
1042 }
1043 }
1044
1045 #endif /* CONFIG_NO_SCAN_PROCESSING */
1046
1047
1048 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1049 union wpa_event_data *data)
1050 {
1051 int l, len, found = 0, wpa_found, rsn_found;
1052 const u8 *p;
1053
1054 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1055 if (data->assoc_info.req_ies)
1056 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1057 data->assoc_info.req_ies_len);
1058 if (data->assoc_info.resp_ies) {
1059 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1060 data->assoc_info.resp_ies_len);
1061 #ifdef CONFIG_TDLS
1062 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1063 data->assoc_info.resp_ies_len);
1064 #endif /* CONFIG_TDLS */
1065 }
1066 if (data->assoc_info.beacon_ies)
1067 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1068 data->assoc_info.beacon_ies,
1069 data->assoc_info.beacon_ies_len);
1070 if (data->assoc_info.freq)
1071 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1072 data->assoc_info.freq);
1073
1074 p = data->assoc_info.req_ies;
1075 l = data->assoc_info.req_ies_len;
1076
1077 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1078 while (p && l >= 2) {
1079 len = p[1] + 2;
1080 if (len > l) {
1081 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1082 p, l);
1083 break;
1084 }
1085 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1086 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1087 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1088 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1089 break;
1090 found = 1;
1091 wpa_find_assoc_pmkid(wpa_s);
1092 break;
1093 }
1094 l -= len;
1095 p += len;
1096 }
1097 if (!found && data->assoc_info.req_ies)
1098 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1099
1100 #ifdef CONFIG_IEEE80211R
1101 #ifdef CONFIG_SME
1102 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1103 u8 bssid[ETH_ALEN];
1104 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1105 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1106 data->assoc_info.resp_ies,
1107 data->assoc_info.resp_ies_len,
1108 bssid) < 0) {
1109 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1110 "Reassociation Response failed");
1111 wpa_supplicant_deauthenticate(
1112 wpa_s, WLAN_REASON_INVALID_IE);
1113 return -1;
1114 }
1115 }
1116
1117 p = data->assoc_info.resp_ies;
1118 l = data->assoc_info.resp_ies_len;
1119
1120 #ifdef CONFIG_WPS_STRICT
1121 if (p && wpa_s->current_ssid &&
1122 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1123 struct wpabuf *wps;
1124 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1125 if (wps == NULL) {
1126 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1127 "include WPS IE in (Re)Association Response");
1128 return -1;
1129 }
1130
1131 if (wps_validate_assoc_resp(wps) < 0) {
1132 wpabuf_free(wps);
1133 wpa_supplicant_deauthenticate(
1134 wpa_s, WLAN_REASON_INVALID_IE);
1135 return -1;
1136 }
1137 wpabuf_free(wps);
1138 }
1139 #endif /* CONFIG_WPS_STRICT */
1140
1141 /* Go through the IEs and make a copy of the MDIE, if present. */
1142 while (p && l >= 2) {
1143 len = p[1] + 2;
1144 if (len > l) {
1145 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1146 p, l);
1147 break;
1148 }
1149 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1150 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1151 wpa_s->sme.ft_used = 1;
1152 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1153 MOBILITY_DOMAIN_ID_LEN);
1154 break;
1155 }
1156 l -= len;
1157 p += len;
1158 }
1159 #endif /* CONFIG_SME */
1160
1161 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1162 data->assoc_info.resp_ies_len);
1163 #endif /* CONFIG_IEEE80211R */
1164
1165 /* WPA/RSN IE from Beacon/ProbeResp */
1166 p = data->assoc_info.beacon_ies;
1167 l = data->assoc_info.beacon_ies_len;
1168
1169 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1170 */
1171 wpa_found = rsn_found = 0;
1172 while (p && l >= 2) {
1173 len = p[1] + 2;
1174 if (len > l) {
1175 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1176 p, l);
1177 break;
1178 }
1179 if (!wpa_found &&
1180 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1181 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1182 wpa_found = 1;
1183 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1184 }
1185
1186 if (!rsn_found &&
1187 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1188 rsn_found = 1;
1189 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1190 }
1191
1192 l -= len;
1193 p += len;
1194 }
1195
1196 if (!wpa_found && data->assoc_info.beacon_ies)
1197 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1198 if (!rsn_found && data->assoc_info.beacon_ies)
1199 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1200 if (wpa_found || rsn_found)
1201 wpa_s->ap_ies_from_associnfo = 1;
1202
1203 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1204 wpa_s->assoc_freq != data->assoc_info.freq) {
1205 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1206 "%u to %u MHz",
1207 wpa_s->assoc_freq, data->assoc_info.freq);
1208 wpa_supplicant_update_scan_results(wpa_s);
1209 }
1210
1211 wpa_s->assoc_freq = data->assoc_info.freq;
1212
1213 return 0;
1214 }
1215
1216
1217 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1218 union wpa_event_data *data)
1219 {
1220 u8 bssid[ETH_ALEN];
1221 int ft_completed;
1222 int bssid_changed;
1223 struct wpa_driver_capa capa;
1224
1225 #ifdef CONFIG_AP
1226 if (wpa_s->ap_iface) {
1227 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1228 data->assoc_info.addr,
1229 data->assoc_info.req_ies,
1230 data->assoc_info.req_ies_len,
1231 data->assoc_info.reassoc);
1232 return;
1233 }
1234 #endif /* CONFIG_AP */
1235
1236 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1237 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1238 return;
1239
1240 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1242 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1243 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1244 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1245 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1246 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1247 MACSTR, MAC2STR(bssid));
1248 random_add_randomness(bssid, ETH_ALEN);
1249 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1250 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1251 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1252 if (bssid_changed)
1253 wpas_notify_bssid_changed(wpa_s);
1254
1255 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1256 wpa_clear_keys(wpa_s, bssid);
1257 }
1258 if (wpa_supplicant_select_config(wpa_s) < 0) {
1259 wpa_supplicant_disassociate(
1260 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1261 return;
1262 }
1263 if (wpa_s->current_ssid) {
1264 struct wpa_bss *bss = NULL;
1265 struct wpa_ssid *ssid = wpa_s->current_ssid;
1266 if (ssid->ssid_len > 0)
1267 bss = wpa_bss_get(wpa_s, bssid,
1268 ssid->ssid, ssid->ssid_len);
1269 if (!bss)
1270 bss = wpa_bss_get_bssid(wpa_s, bssid);
1271 if (bss)
1272 wpa_s->current_bss = bss;
1273 }
1274 }
1275
1276 #ifdef CONFIG_SME
1277 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1278 wpa_s->sme.prev_bssid_set = 1;
1279 #endif /* CONFIG_SME */
1280
1281 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1282 if (wpa_s->current_ssid) {
1283 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1284 * initialized before association, but for other modes,
1285 * initialize PC/SC here, if the current configuration needs
1286 * smartcard or SIM/USIM. */
1287 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1288 }
1289 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1290 if (wpa_s->l2)
1291 l2_packet_notify_auth_start(wpa_s->l2);
1292
1293 /*
1294 * Set portEnabled first to FALSE in order to get EAP state machine out
1295 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1296 * state machine may transit to AUTHENTICATING state based on obsolete
1297 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1298 * AUTHENTICATED without ever giving chance to EAP state machine to
1299 * reset the state.
1300 */
1301 if (!ft_completed) {
1302 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1303 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1304 }
1305 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1306 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1307 /* 802.1X::portControl = Auto */
1308 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1309 wpa_s->eapol_received = 0;
1310 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1311 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1312 (wpa_s->current_ssid &&
1313 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1314 wpa_supplicant_cancel_auth_timeout(wpa_s);
1315 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1316 } else if (!ft_completed) {
1317 /* Timeout for receiving the first EAPOL packet */
1318 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1319 }
1320 wpa_supplicant_cancel_scan(wpa_s);
1321
1322 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1323 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1324 /*
1325 * We are done; the driver will take care of RSN 4-way
1326 * handshake.
1327 */
1328 wpa_supplicant_cancel_auth_timeout(wpa_s);
1329 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1330 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1331 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1332 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1333 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1334 /*
1335 * The driver will take care of RSN 4-way handshake, so we need
1336 * to allow EAPOL supplicant to complete its work without
1337 * waiting for WPA supplicant.
1338 */
1339 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1340 } else if (ft_completed) {
1341 /*
1342 * FT protocol completed - make sure EAPOL state machine ends
1343 * up in authenticated.
1344 */
1345 wpa_supplicant_cancel_auth_timeout(wpa_s);
1346 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1347 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1348 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1349 }
1350
1351 if (wpa_s->pending_eapol_rx) {
1352 struct os_time now, age;
1353 os_get_time(&now);
1354 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1355 if (age.sec == 0 && age.usec < 100000 &&
1356 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1357 0) {
1358 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1359 "frame that was received just before "
1360 "association notification");
1361 wpa_supplicant_rx_eapol(
1362 wpa_s, wpa_s->pending_eapol_rx_src,
1363 wpabuf_head(wpa_s->pending_eapol_rx),
1364 wpabuf_len(wpa_s->pending_eapol_rx));
1365 }
1366 wpabuf_free(wpa_s->pending_eapol_rx);
1367 wpa_s->pending_eapol_rx = NULL;
1368 }
1369
1370 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1371 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1372 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1373 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1374 /* Set static WEP keys again */
1375 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1376 }
1377
1378 #ifdef CONFIG_IBSS_RSN
1379 if (wpa_s->current_ssid &&
1380 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1381 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1382 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1383 wpa_s->ibss_rsn == NULL) {
1384 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1385 if (!wpa_s->ibss_rsn) {
1386 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1387 wpa_supplicant_deauthenticate(
1388 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1389 return;
1390 }
1391
1392 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1393 }
1394 #endif /* CONFIG_IBSS_RSN */
1395 }
1396
1397
1398 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1399 u16 reason_code)
1400 {
1401 const u8 *bssid;
1402 int authenticating;
1403 u8 prev_pending_bssid[ETH_ALEN];
1404
1405 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1406 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1407
1408 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1409 /*
1410 * At least Host AP driver and a Prism3 card seemed to be
1411 * generating streams of disconnected events when configuring
1412 * IBSS for WPA-None. Ignore them for now.
1413 */
1414 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1415 "IBSS/WPA-None mode");
1416 return;
1417 }
1418
1419 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1420 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1421 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1422 "pre-shared key may be incorrect");
1423 }
1424 if (!wpa_s->auto_reconnect_disabled ||
1425 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1426 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
1427 "reconnect (wps=%d)",
1428 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1429 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1430 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1431 } else {
1432 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
1433 "try to re-connect");
1434 wpa_s->reassociate = 0;
1435 wpa_s->disconnected = 1;
1436 }
1437 bssid = wpa_s->bssid;
1438 if (is_zero_ether_addr(bssid))
1439 bssid = wpa_s->pending_bssid;
1440 wpas_connection_failed(wpa_s, bssid);
1441 wpa_sm_notify_disassoc(wpa_s->wpa);
1442 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1443 " reason=%d",
1444 MAC2STR(bssid), reason_code);
1445 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1446 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1447 wpa_s->keys_cleared = 0;
1448 wpa_clear_keys(wpa_s, wpa_s->bssid);
1449 }
1450 wpa_supplicant_mark_disassoc(wpa_s);
1451
1452 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1453 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1454 }
1455
1456
1457 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1458 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1459 void *sock_ctx)
1460 {
1461 struct wpa_supplicant *wpa_s = eloop_ctx;
1462
1463 if (!wpa_s->pending_mic_error_report)
1464 return;
1465
1466 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1467 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1468 wpa_s->pending_mic_error_report = 0;
1469 }
1470 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1471
1472
1473 static void
1474 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1475 union wpa_event_data *data)
1476 {
1477 int pairwise;
1478 struct os_time t;
1479
1480 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1481 pairwise = (data && data->michael_mic_failure.unicast);
1482 os_get_time(&t);
1483 if ((wpa_s->last_michael_mic_error &&
1484 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1485 wpa_s->pending_mic_error_report) {
1486 if (wpa_s->pending_mic_error_report) {
1487 /*
1488 * Send the pending MIC error report immediately since
1489 * we are going to start countermeasures and AP better
1490 * do the same.
1491 */
1492 wpa_sm_key_request(wpa_s->wpa, 1,
1493 wpa_s->pending_mic_error_pairwise);
1494 }
1495
1496 /* Send the new MIC error report immediately since we are going
1497 * to start countermeasures and AP better do the same.
1498 */
1499 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1500
1501 /* initialize countermeasures */
1502 wpa_s->countermeasures = 1;
1503 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1504
1505 /*
1506 * Need to wait for completion of request frame. We do not get
1507 * any callback for the message completion, so just wait a
1508 * short while and hope for the best. */
1509 os_sleep(0, 10000);
1510
1511 wpa_drv_set_countermeasures(wpa_s, 1);
1512 wpa_supplicant_deauthenticate(wpa_s,
1513 WLAN_REASON_MICHAEL_MIC_FAILURE);
1514 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1515 wpa_s, NULL);
1516 eloop_register_timeout(60, 0,
1517 wpa_supplicant_stop_countermeasures,
1518 wpa_s, NULL);
1519 /* TODO: mark the AP rejected for 60 second. STA is
1520 * allowed to associate with another AP.. */
1521 } else {
1522 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1523 if (wpa_s->mic_errors_seen) {
1524 /*
1525 * Reduce the effectiveness of Michael MIC error
1526 * reports as a means for attacking against TKIP if
1527 * more than one MIC failure is noticed with the same
1528 * PTK. We delay the transmission of the reports by a
1529 * random time between 0 and 60 seconds in order to
1530 * force the attacker wait 60 seconds before getting
1531 * the information on whether a frame resulted in a MIC
1532 * failure.
1533 */
1534 u8 rval[4];
1535 int sec;
1536
1537 if (os_get_random(rval, sizeof(rval)) < 0)
1538 sec = os_random() % 60;
1539 else
1540 sec = WPA_GET_BE32(rval) % 60;
1541 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1542 "report %d seconds", sec);
1543 wpa_s->pending_mic_error_report = 1;
1544 wpa_s->pending_mic_error_pairwise = pairwise;
1545 eloop_cancel_timeout(
1546 wpa_supplicant_delayed_mic_error_report,
1547 wpa_s, NULL);
1548 eloop_register_timeout(
1549 sec, os_random() % 1000000,
1550 wpa_supplicant_delayed_mic_error_report,
1551 wpa_s, NULL);
1552 } else {
1553 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1554 }
1555 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1556 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1557 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1558 }
1559 wpa_s->last_michael_mic_error = t.sec;
1560 wpa_s->mic_errors_seen++;
1561 }
1562
1563
1564 #ifdef CONFIG_TERMINATE_ONLASTIF
1565 static int any_interfaces(struct wpa_supplicant *head)
1566 {
1567 struct wpa_supplicant *wpa_s;
1568
1569 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1570 if (!wpa_s->interface_removed)
1571 return 1;
1572 return 0;
1573 }
1574 #endif /* CONFIG_TERMINATE_ONLASTIF */
1575
1576
1577 static void
1578 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1579 union wpa_event_data *data)
1580 {
1581 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1582 return;
1583
1584 switch (data->interface_status.ievent) {
1585 case EVENT_INTERFACE_ADDED:
1586 if (!wpa_s->interface_removed)
1587 break;
1588 wpa_s->interface_removed = 0;
1589 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1590 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1591 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1592 "driver after interface was added");
1593 }
1594 break;
1595 case EVENT_INTERFACE_REMOVED:
1596 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1597 wpa_s->interface_removed = 1;
1598 wpa_supplicant_mark_disassoc(wpa_s);
1599 l2_packet_deinit(wpa_s->l2);
1600 wpa_s->l2 = NULL;
1601 #ifdef CONFIG_IBSS_RSN
1602 ibss_rsn_deinit(wpa_s->ibss_rsn);
1603 wpa_s->ibss_rsn = NULL;
1604 #endif /* CONFIG_IBSS_RSN */
1605 #ifdef CONFIG_TERMINATE_ONLASTIF
1606 /* check if last interface */
1607 if (!any_interfaces(wpa_s->global->ifaces))
1608 eloop_terminate();
1609 #endif /* CONFIG_TERMINATE_ONLASTIF */
1610 break;
1611 }
1612 }
1613
1614
1615 #ifdef CONFIG_PEERKEY
1616 static void
1617 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1618 union wpa_event_data *data)
1619 {
1620 if (data == NULL)
1621 return;
1622 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1623 }
1624 #endif /* CONFIG_PEERKEY */
1625
1626
1627 #ifdef CONFIG_TDLS
1628 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1629 union wpa_event_data *data)
1630 {
1631 if (data == NULL)
1632 return;
1633 switch (data->tdls.oper) {
1634 case TDLS_REQUEST_SETUP:
1635 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1636 break;
1637 case TDLS_REQUEST_TEARDOWN:
1638 /* request from driver to add FTIE */
1639 wpa_tdls_recv_teardown_notify(wpa_s->wpa, data->tdls.peer,
1640 data->tdls.reason_code);
1641 break;
1642 }
1643 }
1644 #endif /* CONFIG_TDLS */
1645
1646
1647 #ifdef CONFIG_IEEE80211R
1648 static void
1649 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1650 union wpa_event_data *data)
1651 {
1652 if (data == NULL)
1653 return;
1654
1655 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1656 data->ft_ies.ies_len,
1657 data->ft_ies.ft_action,
1658 data->ft_ies.target_ap,
1659 data->ft_ies.ric_ies,
1660 data->ft_ies.ric_ies_len) < 0) {
1661 /* TODO: prevent MLME/driver from trying to associate? */
1662 }
1663 }
1664 #endif /* CONFIG_IEEE80211R */
1665
1666
1667 #ifdef CONFIG_IBSS_RSN
1668 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1669 union wpa_event_data *data)
1670 {
1671 struct wpa_ssid *ssid;
1672 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1673 return;
1674 if (data == NULL)
1675 return;
1676 ssid = wpa_s->current_ssid;
1677 if (ssid == NULL)
1678 return;
1679 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1680 return;
1681
1682 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1683 }
1684 #endif /* CONFIG_IBSS_RSN */
1685
1686
1687 #ifdef CONFIG_IEEE80211R
1688 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1689 size_t len)
1690 {
1691 const u8 *sta_addr, *target_ap_addr;
1692 u16 status;
1693
1694 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1695 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1696 return; /* only SME case supported for now */
1697 if (len < 1 + 2 * ETH_ALEN + 2)
1698 return;
1699 if (data[0] != 2)
1700 return; /* Only FT Action Response is supported for now */
1701 sta_addr = data + 1;
1702 target_ap_addr = data + 1 + ETH_ALEN;
1703 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1704 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1705 MACSTR " TargetAP " MACSTR " status %u",
1706 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1707
1708 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1709 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1710 " in FT Action Response", MAC2STR(sta_addr));
1711 return;
1712 }
1713
1714 if (status) {
1715 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1716 "failure (status code %d)", status);
1717 /* TODO: report error to FT code(?) */
1718 return;
1719 }
1720
1721 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1722 len - (1 + 2 * ETH_ALEN + 2), 1,
1723 target_ap_addr, NULL, 0) < 0)
1724 return;
1725
1726 #ifdef CONFIG_SME
1727 {
1728 struct wpa_bss *bss;
1729 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1730 if (bss)
1731 wpa_s->sme.freq = bss->freq;
1732 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1733 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1734 WLAN_AUTH_FT);
1735 }
1736 #endif /* CONFIG_SME */
1737 }
1738 #endif /* CONFIG_IEEE80211R */
1739
1740
1741 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1742 struct unprot_deauth *e)
1743 {
1744 #ifdef CONFIG_IEEE80211W
1745 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1746 "dropped: " MACSTR " -> " MACSTR
1747 " (reason code %u)",
1748 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1749 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1750 #endif /* CONFIG_IEEE80211W */
1751 }
1752
1753
1754 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1755 struct unprot_disassoc *e)
1756 {
1757 #ifdef CONFIG_IEEE80211W
1758 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1759 "dropped: " MACSTR " -> " MACSTR
1760 " (reason code %u)",
1761 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1762 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1763 #endif /* CONFIG_IEEE80211W */
1764 }
1765
1766
1767 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1768 union wpa_event_data *data)
1769 {
1770 struct wpa_supplicant *wpa_s = ctx;
1771 u16 reason_code = 0;
1772
1773 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1774 event != EVENT_INTERFACE_ENABLED &&
1775 event != EVENT_INTERFACE_STATUS) {
1776 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
1777 "disabled", event);
1778 return;
1779 }
1780
1781 wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
1782 event, wpa_s->ifname);
1783
1784 switch (event) {
1785 case EVENT_AUTH:
1786 sme_event_auth(wpa_s, data);
1787 break;
1788 case EVENT_ASSOC:
1789 wpa_supplicant_event_assoc(wpa_s, data);
1790 break;
1791 case EVENT_DISASSOC:
1792 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
1793 if (data) {
1794 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1795 data->disassoc_info.reason_code);
1796 if (data->disassoc_info.addr)
1797 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
1798 MAC2STR(data->disassoc_info.addr));
1799 }
1800 #ifdef CONFIG_AP
1801 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1802 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1803 data->disassoc_info.addr);
1804 break;
1805 }
1806 #endif /* CONFIG_AP */
1807 if (data) {
1808 reason_code = data->disassoc_info.reason_code;
1809 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1810 data->disassoc_info.ie,
1811 data->disassoc_info.ie_len);
1812 #ifdef CONFIG_P2P
1813 wpas_p2p_disassoc_notif(
1814 wpa_s, data->disassoc_info.addr, reason_code,
1815 data->disassoc_info.ie,
1816 data->disassoc_info.ie_len);
1817 #endif /* CONFIG_P2P */
1818 }
1819 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1820 sme_event_disassoc(wpa_s, data);
1821 /* fall through */
1822 case EVENT_DEAUTH:
1823 if (event == EVENT_DEAUTH) {
1824 wpa_dbg(wpa_s, MSG_DEBUG,
1825 "Deauthentication notification");
1826 if (data) {
1827 reason_code = data->deauth_info.reason_code;
1828 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1829 data->deauth_info.reason_code);
1830 if (data->deauth_info.addr) {
1831 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
1832 MACSTR,
1833 MAC2STR(data->deauth_info.
1834 addr));
1835 }
1836 wpa_hexdump(MSG_DEBUG,
1837 "Deauthentication frame IE(s)",
1838 data->deauth_info.ie,
1839 data->deauth_info.ie_len);
1840 #ifdef CONFIG_P2P
1841 wpas_p2p_deauth_notif(
1842 wpa_s, data->deauth_info.addr,
1843 reason_code,
1844 data->deauth_info.ie,
1845 data->deauth_info.ie_len);
1846 #endif /* CONFIG_P2P */
1847 }
1848 }
1849 #ifdef CONFIG_AP
1850 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1851 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1852 data->deauth_info.addr);
1853 break;
1854 }
1855 #endif /* CONFIG_AP */
1856 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1857 break;
1858 case EVENT_MICHAEL_MIC_FAILURE:
1859 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1860 break;
1861 #ifndef CONFIG_NO_SCAN_PROCESSING
1862 case EVENT_SCAN_RESULTS:
1863 wpa_supplicant_event_scan_results(wpa_s, data);
1864 break;
1865 #endif /* CONFIG_NO_SCAN_PROCESSING */
1866 case EVENT_ASSOCINFO:
1867 wpa_supplicant_event_associnfo(wpa_s, data);
1868 break;
1869 case EVENT_INTERFACE_STATUS:
1870 wpa_supplicant_event_interface_status(wpa_s, data);
1871 break;
1872 case EVENT_PMKID_CANDIDATE:
1873 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1874 break;
1875 #ifdef CONFIG_PEERKEY
1876 case EVENT_STKSTART:
1877 wpa_supplicant_event_stkstart(wpa_s, data);
1878 break;
1879 #endif /* CONFIG_PEERKEY */
1880 #ifdef CONFIG_TDLS
1881 case EVENT_TDLS:
1882 wpa_supplicant_event_tdls(wpa_s, data);
1883 break;
1884 #endif /* CONFIG_TDLS */
1885 #ifdef CONFIG_IEEE80211R
1886 case EVENT_FT_RESPONSE:
1887 wpa_supplicant_event_ft_response(wpa_s, data);
1888 break;
1889 #endif /* CONFIG_IEEE80211R */
1890 #ifdef CONFIG_IBSS_RSN
1891 case EVENT_IBSS_RSN_START:
1892 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1893 break;
1894 #endif /* CONFIG_IBSS_RSN */
1895 case EVENT_ASSOC_REJECT:
1896 if (data->assoc_reject.bssid)
1897 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1898 "bssid=" MACSTR " status_code=%u",
1899 MAC2STR(data->assoc_reject.bssid),
1900 data->assoc_reject.status_code);
1901 else
1902 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1903 "status_code=%u",
1904 data->assoc_reject.status_code);
1905 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1906 sme_event_assoc_reject(wpa_s, data);
1907 break;
1908 case EVENT_AUTH_TIMED_OUT:
1909 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1910 sme_event_auth_timed_out(wpa_s, data);
1911 break;
1912 case EVENT_ASSOC_TIMED_OUT:
1913 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1914 sme_event_assoc_timed_out(wpa_s, data);
1915 break;
1916 #ifdef CONFIG_AP
1917 case EVENT_TX_STATUS:
1918 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
1919 " type=%d stype=%d",
1920 MAC2STR(data->tx_status.dst),
1921 data->tx_status.type, data->tx_status.stype);
1922 if (wpa_s->ap_iface == NULL) {
1923 #ifdef CONFIG_P2P
1924 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1925 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1926 wpas_send_action_tx_status(
1927 wpa_s, data->tx_status.dst,
1928 data->tx_status.data,
1929 data->tx_status.data_len,
1930 data->tx_status.ack ?
1931 P2P_SEND_ACTION_SUCCESS :
1932 P2P_SEND_ACTION_NO_ACK);
1933 #endif /* CONFIG_P2P */
1934 break;
1935 }
1936 #ifdef CONFIG_P2P
1937 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
1938 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
1939 /*
1940 * Catch TX status events for Action frames we sent via group
1941 * interface in GO mode.
1942 */
1943 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1944 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1945 os_memcmp(wpa_s->parent->pending_action_dst,
1946 data->tx_status.dst, ETH_ALEN) == 0) {
1947 wpas_send_action_tx_status(
1948 wpa_s->parent, data->tx_status.dst,
1949 data->tx_status.data,
1950 data->tx_status.data_len,
1951 data->tx_status.ack ?
1952 P2P_SEND_ACTION_SUCCESS :
1953 P2P_SEND_ACTION_NO_ACK);
1954 break;
1955 }
1956 #endif /* CONFIG_P2P */
1957 switch (data->tx_status.type) {
1958 case WLAN_FC_TYPE_MGMT:
1959 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1960 data->tx_status.data_len,
1961 data->tx_status.stype,
1962 data->tx_status.ack);
1963 break;
1964 case WLAN_FC_TYPE_DATA:
1965 ap_tx_status(wpa_s, data->tx_status.dst,
1966 data->tx_status.data,
1967 data->tx_status.data_len,
1968 data->tx_status.ack);
1969 break;
1970 }
1971 break;
1972 case EVENT_RX_FROM_UNKNOWN:
1973 if (wpa_s->ap_iface == NULL)
1974 break;
1975 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1976 data->rx_from_unknown.len);
1977 break;
1978 case EVENT_RX_MGMT:
1979 if (wpa_s->ap_iface == NULL) {
1980 #ifdef CONFIG_P2P
1981 u16 fc, stype;
1982 const struct ieee80211_mgmt *mgmt;
1983 mgmt = (const struct ieee80211_mgmt *)
1984 data->rx_mgmt.frame;
1985 fc = le_to_host16(mgmt->frame_control);
1986 stype = WLAN_FC_GET_STYPE(fc);
1987 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1988 data->rx_mgmt.frame_len > 24) {
1989 const u8 *src = mgmt->sa;
1990 const u8 *ie = mgmt->u.probe_req.variable;
1991 size_t ie_len = data->rx_mgmt.frame_len -
1992 (mgmt->u.probe_req.variable -
1993 data->rx_mgmt.frame);
1994 wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
1995 mgmt->bssid, ie, ie_len);
1996 break;
1997 }
1998 #endif /* CONFIG_P2P */
1999 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2000 "management frame in non-AP mode");
2001 break;
2002 }
2003 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2004 break;
2005 #endif /* CONFIG_AP */
2006 case EVENT_RX_ACTION:
2007 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2008 " Category=%u DataLen=%d freq=%d MHz",
2009 MAC2STR(data->rx_action.sa),
2010 data->rx_action.category, (int) data->rx_action.len,
2011 data->rx_action.freq);
2012 #ifdef CONFIG_IEEE80211R
2013 if (data->rx_action.category == WLAN_ACTION_FT) {
2014 ft_rx_action(wpa_s, data->rx_action.data,
2015 data->rx_action.len);
2016 break;
2017 }
2018 #endif /* CONFIG_IEEE80211R */
2019 #ifdef CONFIG_IEEE80211W
2020 #ifdef CONFIG_SME
2021 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2022 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2023 data->rx_action.data,
2024 data->rx_action.len);
2025 break;
2026 }
2027 #endif /* CONFIG_SME */
2028 #endif /* CONFIG_IEEE80211W */
2029 #ifdef CONFIG_P2P
2030 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2031 data->rx_action.sa,
2032 data->rx_action.bssid,
2033 data->rx_action.category,
2034 data->rx_action.data,
2035 data->rx_action.len, data->rx_action.freq);
2036 #endif /* CONFIG_P2P */
2037 break;
2038 case EVENT_RX_PROBE_REQ:
2039 if (data->rx_probe_req.sa == NULL ||
2040 data->rx_probe_req.ie == NULL)
2041 break;
2042 #ifdef CONFIG_AP
2043 if (wpa_s->ap_iface) {
2044 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2045 data->rx_probe_req.sa,
2046 data->rx_probe_req.da,
2047 data->rx_probe_req.bssid,
2048 data->rx_probe_req.ie,
2049 data->rx_probe_req.ie_len);
2050 break;
2051 }
2052 #endif /* CONFIG_AP */
2053 #ifdef CONFIG_P2P
2054 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2055 data->rx_probe_req.da,
2056 data->rx_probe_req.bssid,
2057 data->rx_probe_req.ie,
2058 data->rx_probe_req.ie_len);
2059 #endif /* CONFIG_P2P */
2060 break;
2061 #ifdef CONFIG_P2P
2062 case EVENT_REMAIN_ON_CHANNEL:
2063 wpas_p2p_remain_on_channel_cb(
2064 wpa_s, data->remain_on_channel.freq,
2065 data->remain_on_channel.duration);
2066 break;
2067 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2068 wpas_p2p_cancel_remain_on_channel_cb(
2069 wpa_s, data->remain_on_channel.freq);
2070 break;
2071 case EVENT_P2P_DEV_FOUND: {
2072 struct p2p_peer_info peer_info;
2073
2074 os_memset(&peer_info, 0, sizeof(peer_info));
2075 if (data->p2p_dev_found.dev_addr)
2076 os_memcpy(peer_info.p2p_device_addr,
2077 data->p2p_dev_found.dev_addr, ETH_ALEN);
2078 if (data->p2p_dev_found.pri_dev_type)
2079 os_memcpy(peer_info.pri_dev_type,
2080 data->p2p_dev_found.pri_dev_type,
2081 sizeof(peer_info.pri_dev_type));
2082 if (data->p2p_dev_found.dev_name)
2083 os_strlcpy(peer_info.device_name,
2084 data->p2p_dev_found.dev_name,
2085 sizeof(peer_info.device_name));
2086 peer_info.config_methods = data->p2p_dev_found.config_methods;
2087 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2088 peer_info.group_capab = data->p2p_dev_found.group_capab;
2089
2090 /*
2091 * FIX: new_device=1 is not necessarily correct. We should
2092 * maintain a P2P peer database in wpa_supplicant and update
2093 * this information based on whether the peer is truly new.
2094 */
2095 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2096 break;
2097 }
2098 case EVENT_P2P_GO_NEG_REQ_RX:
2099 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2100 data->p2p_go_neg_req_rx.dev_passwd_id);
2101 break;
2102 case EVENT_P2P_GO_NEG_COMPLETED:
2103 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2104 break;
2105 case EVENT_P2P_PROV_DISC_REQUEST:
2106 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2107 data->p2p_prov_disc_req.config_methods,
2108 data->p2p_prov_disc_req.dev_addr,
2109 data->p2p_prov_disc_req.pri_dev_type,
2110 data->p2p_prov_disc_req.dev_name,
2111 data->p2p_prov_disc_req.supp_config_methods,
2112 data->p2p_prov_disc_req.dev_capab,
2113 data->p2p_prov_disc_req.group_capab);
2114 break;
2115 case EVENT_P2P_PROV_DISC_RESPONSE:
2116 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2117 data->p2p_prov_disc_resp.config_methods);
2118 break;
2119 case EVENT_P2P_SD_REQUEST:
2120 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2121 data->p2p_sd_req.sa,
2122 data->p2p_sd_req.dialog_token,
2123 data->p2p_sd_req.update_indic,
2124 data->p2p_sd_req.tlvs,
2125 data->p2p_sd_req.tlvs_len);
2126 break;
2127 case EVENT_P2P_SD_RESPONSE:
2128 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2129 data->p2p_sd_resp.update_indic,
2130 data->p2p_sd_resp.tlvs,
2131 data->p2p_sd_resp.tlvs_len);
2132 break;
2133 #endif /* CONFIG_P2P */
2134 #ifdef CONFIG_CLIENT_MLME
2135 case EVENT_MLME_RX: {
2136 struct ieee80211_rx_status rx_status;
2137 os_memset(&rx_status, 0, sizeof(rx_status));
2138 rx_status.freq = data->mlme_rx.freq;
2139 rx_status.channel = data->mlme_rx.channel;
2140 rx_status.ssi = data->mlme_rx.ssi;
2141 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
2142 &rx_status);
2143 break;
2144 }
2145 #endif /* CONFIG_CLIENT_MLME */
2146 case EVENT_EAPOL_RX:
2147 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2148 data->eapol_rx.data,
2149 data->eapol_rx.data_len);
2150 break;
2151 case EVENT_SIGNAL_CHANGE:
2152 bgscan_notify_signal_change(
2153 wpa_s, data->signal_change.above_threshold,
2154 data->signal_change.current_signal,
2155 data->signal_change.current_noise,
2156 data->signal_change.current_txrate);
2157 break;
2158 case EVENT_INTERFACE_ENABLED:
2159 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2160 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2161 #ifdef CONFIG_AP
2162 if (!wpa_s->ap_iface) {
2163 wpa_supplicant_set_state(wpa_s,
2164 WPA_DISCONNECTED);
2165 wpa_supplicant_req_scan(wpa_s, 0, 0);
2166 } else
2167 wpa_supplicant_set_state(wpa_s,
2168 WPA_COMPLETED);
2169 #else /* CONFIG_AP */
2170 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2171 wpa_supplicant_req_scan(wpa_s, 0, 0);
2172 #endif /* CONFIG_AP */
2173 }
2174 break;
2175 case EVENT_INTERFACE_DISABLED:
2176 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2177 wpa_supplicant_mark_disassoc(wpa_s);
2178 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2179 break;
2180 case EVENT_CHANNEL_LIST_CHANGED:
2181 if (wpa_s->drv_priv == NULL)
2182 break; /* Ignore event during drv initialization */
2183 #ifdef CONFIG_P2P
2184 wpas_p2p_update_channel_list(wpa_s);
2185 #endif /* CONFIG_P2P */
2186 break;
2187 case EVENT_INTERFACE_UNAVAILABLE:
2188 #ifdef CONFIG_P2P
2189 wpas_p2p_interface_unavailable(wpa_s);
2190 #endif /* CONFIG_P2P */
2191 break;
2192 case EVENT_BEST_CHANNEL:
2193 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2194 "(%d %d %d)",
2195 data->best_chan.freq_24, data->best_chan.freq_5,
2196 data->best_chan.freq_overall);
2197 wpa_s->best_24_freq = data->best_chan.freq_24;
2198 wpa_s->best_5_freq = data->best_chan.freq_5;
2199 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2200 #ifdef CONFIG_P2P
2201 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2202 data->best_chan.freq_5,
2203 data->best_chan.freq_overall);
2204 #endif /* CONFIG_P2P */
2205 break;
2206 case EVENT_UNPROT_DEAUTH:
2207 wpa_supplicant_event_unprot_deauth(wpa_s,
2208 &data->unprot_deauth);
2209 break;
2210 case EVENT_UNPROT_DISASSOC:
2211 wpa_supplicant_event_unprot_disassoc(wpa_s,
2212 &data->unprot_disassoc);
2213 break;
2214 case EVENT_STATION_LOW_ACK:
2215 #ifdef CONFIG_AP
2216 if (wpa_s->ap_iface && data)
2217 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2218 data->low_ack.addr);
2219 #endif /* CONFIG_AP */
2220 break;
2221 case EVENT_IBSS_PEER_LOST:
2222 #ifdef CONFIG_IBSS_RSN
2223 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2224 #endif /* CONFIG_IBSS_RSN */
2225 break;
2226 case EVENT_DRIVER_GTK_REKEY:
2227 if (os_memcmp(data->driver_gtk_rekey.bssid,
2228 wpa_s->bssid, ETH_ALEN))
2229 break;
2230 if (!wpa_s->wpa)
2231 break;
2232 wpa_sm_update_replay_ctr(wpa_s->wpa,
2233 data->driver_gtk_rekey.replay_ctr);
2234 break;
2235 default:
2236 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2237 break;
2238 }
2239 }