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