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