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