Reschedule scan from wpas_stop_pno if it was postponed
[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 #ifndef CONFIG_NO_ROAMING
1290         int min_diff;
1291 #endif /* CONFIG_NO_ROAMING */
1292
1293         if (wpa_s->reassociate)
1294                 return 1; /* explicit request to reassociate */
1295         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1296                 return 1; /* we are not associated; continue */
1297         if (wpa_s->current_ssid == NULL)
1298                 return 1; /* unknown current SSID */
1299         if (wpa_s->current_ssid != ssid)
1300                 return 1; /* different network block */
1301
1302         if (wpas_driver_bss_selection(wpa_s))
1303                 return 0; /* Driver-based roaming */
1304
1305         if (wpa_s->current_ssid->ssid)
1306                 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1307                                           wpa_s->current_ssid->ssid,
1308                                           wpa_s->current_ssid->ssid_len);
1309         if (!current_bss)
1310                 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1311
1312         if (!current_bss)
1313                 return 1; /* current BSS not seen in scan results */
1314
1315         if (current_bss == selected)
1316                 return 0;
1317
1318         if (selected->last_update_idx > current_bss->last_update_idx)
1319                 return 1; /* current BSS not seen in the last scan */
1320
1321 #ifndef CONFIG_NO_ROAMING
1322         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1323         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1324                 " level=%d snr=%d est_throughput=%u",
1325                 MAC2STR(current_bss->bssid), current_bss->level,
1326                 current_bss->snr, current_bss->est_throughput);
1327         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1328                 " level=%d snr=%d est_throughput=%u",
1329                 MAC2STR(selected->bssid), selected->level,
1330                 selected->snr, selected->est_throughput);
1331
1332         if (wpa_s->current_ssid->bssid_set &&
1333             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1334             0) {
1335                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1336                         "has preferred BSSID");
1337                 return 1;
1338         }
1339
1340         if (selected->est_throughput > current_bss->est_throughput + 5000) {
1341                 wpa_dbg(wpa_s, MSG_DEBUG,
1342                         "Allow reassociation - selected BSS has better estimated throughput");
1343                 return 1;
1344         }
1345
1346         if (current_bss->level < 0 && current_bss->level > selected->level) {
1347                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1348                         "signal level");
1349                 return 0;
1350         }
1351
1352         min_diff = 2;
1353         if (current_bss->level < 0) {
1354                 if (current_bss->level < -85)
1355                         min_diff = 1;
1356                 else if (current_bss->level < -80)
1357                         min_diff = 2;
1358                 else if (current_bss->level < -75)
1359                         min_diff = 3;
1360                 else if (current_bss->level < -70)
1361                         min_diff = 4;
1362                 else
1363                         min_diff = 5;
1364         }
1365         if (abs(current_bss->level - selected->level) < min_diff) {
1366                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1367                         "in signal level");
1368                 return 0;
1369         }
1370
1371         return 1;
1372 #else /* CONFIG_NO_ROAMING */
1373         return 0;
1374 #endif /* CONFIG_NO_ROAMING */
1375 }
1376
1377
1378 /* Return != 0 if no scan results could be fetched or if scan results should not
1379  * be shared with other virtual interfaces. */
1380 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1381                                               union wpa_event_data *data,
1382                                               int own_request)
1383 {
1384         struct wpa_scan_results *scan_res = NULL;
1385         int ret = 0;
1386         int ap = 0;
1387 #ifndef CONFIG_NO_RANDOM_POOL
1388         size_t i, num;
1389 #endif /* CONFIG_NO_RANDOM_POOL */
1390
1391 #ifdef CONFIG_AP
1392         if (wpa_s->ap_iface)
1393                 ap = 1;
1394 #endif /* CONFIG_AP */
1395
1396         wpa_supplicant_notify_scanning(wpa_s, 0);
1397
1398         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1399                                                    data ? &data->scan_info :
1400                                                    NULL, 1);
1401         if (scan_res == NULL) {
1402                 if (wpa_s->conf->ap_scan == 2 || ap ||
1403                     wpa_s->scan_res_handler == scan_only_handler)
1404                         return -1;
1405                 if (!own_request)
1406                         return -1;
1407                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1408                         "scanning again");
1409                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1410                 ret = -1;
1411                 goto scan_work_done;
1412         }
1413
1414 #ifndef CONFIG_NO_RANDOM_POOL
1415         num = scan_res->num;
1416         if (num > 10)
1417                 num = 10;
1418         for (i = 0; i < num; i++) {
1419                 u8 buf[5];
1420                 struct wpa_scan_res *res = scan_res->res[i];
1421                 buf[0] = res->bssid[5];
1422                 buf[1] = res->qual & 0xff;
1423                 buf[2] = res->noise & 0xff;
1424                 buf[3] = res->level & 0xff;
1425                 buf[4] = res->tsf & 0xff;
1426                 random_add_randomness(buf, sizeof(buf));
1427         }
1428 #endif /* CONFIG_NO_RANDOM_POOL */
1429
1430         if (own_request && wpa_s->scan_res_handler &&
1431             (wpa_s->own_scan_running || !wpa_s->radio->external_scan_running)) {
1432                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1433                                          struct wpa_scan_results *scan_res);
1434
1435                 scan_res_handler = wpa_s->scan_res_handler;
1436                 wpa_s->scan_res_handler = NULL;
1437                 scan_res_handler(wpa_s, scan_res);
1438                 ret = -2;
1439                 goto scan_work_done;
1440         }
1441
1442         if (ap) {
1443                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1444 #ifdef CONFIG_AP
1445                 if (wpa_s->ap_iface->scan_cb)
1446                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1447 #endif /* CONFIG_AP */
1448                 goto scan_work_done;
1449         }
1450
1451         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1452                 wpa_s->own_scan_running, wpa_s->radio->external_scan_running);
1453         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1454             wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1455                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1456                              wpa_s->manual_scan_id);
1457                 wpa_s->manual_scan_use_id = 0;
1458         } else {
1459                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1460         }
1461         wpas_notify_scan_results(wpa_s);
1462
1463         wpas_notify_scan_done(wpa_s, 1);
1464
1465         if (!wpa_s->own_scan_running && wpa_s->radio->external_scan_running) {
1466                 wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
1467                 wpa_scan_results_free(scan_res);
1468                 return 0;
1469         }
1470
1471         if (wnm_scan_process(wpa_s, 1) > 0)
1472                 goto scan_work_done;
1473
1474         if (sme_proc_obss_scan(wpa_s) > 0)
1475                 goto scan_work_done;
1476
1477         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1478                 goto scan_work_done;
1479
1480         if (autoscan_notify_scan(wpa_s, scan_res))
1481                 goto scan_work_done;
1482
1483         if (wpa_s->disconnected) {
1484                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1485                 goto scan_work_done;
1486         }
1487
1488         if (!wpas_driver_bss_selection(wpa_s) &&
1489             bgscan_notify_scan(wpa_s, scan_res) == 1)
1490                 goto scan_work_done;
1491
1492         wpas_wps_update_ap_info(wpa_s, scan_res);
1493
1494         wpa_scan_results_free(scan_res);
1495
1496         if (wpa_s->scan_work) {
1497                 struct wpa_radio_work *work = wpa_s->scan_work;
1498                 wpa_s->scan_work = NULL;
1499                 radio_work_done(work);
1500         }
1501
1502         return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1503
1504 scan_work_done:
1505         wpa_scan_results_free(scan_res);
1506         if (wpa_s->scan_work) {
1507                 struct wpa_radio_work *work = wpa_s->scan_work;
1508                 wpa_s->scan_work = NULL;
1509                 radio_work_done(work);
1510         }
1511         return ret;
1512 }
1513
1514
1515 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1516                                               int new_scan, int own_request)
1517 {
1518         struct wpa_bss *selected;
1519         struct wpa_ssid *ssid = NULL;
1520         int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1521
1522         if (time_to_reenable > 0) {
1523                 wpa_dbg(wpa_s, MSG_DEBUG,
1524                         "Postpone network selection by %d seconds since all networks are disabled",
1525                         time_to_reenable);
1526                 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1527                 eloop_register_timeout(time_to_reenable, 0,
1528                                        wpas_network_reenabled, wpa_s, NULL);
1529                 return 0;
1530         }
1531
1532         if (wpa_s->p2p_mgmt)
1533                 return 0; /* no normal connection on p2p_mgmt interface */
1534
1535         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1536
1537         if (selected) {
1538                 int skip;
1539                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1540                 if (skip) {
1541                         if (new_scan)
1542                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1543                         return 0;
1544                 }
1545
1546                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1547                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1548                         return -1;
1549                 }
1550                 if (new_scan)
1551                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1552                 /*
1553                  * Do not notify other virtual radios of scan results since we do not
1554                  * want them to start other associations at the same time.
1555                  */
1556                 return 1;
1557         } else {
1558 #ifdef CONFIG_MESH
1559                 if (wpa_s->ifmsh) {
1560                         wpa_msg(wpa_s, MSG_INFO,
1561                                 "Avoiding join because we already joined a mesh group");
1562                         return 0;
1563                 }
1564 #endif /* CONFIG_MESH */
1565                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1566                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1567                 if (ssid) {
1568                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1569                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1570                         if (new_scan)
1571                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1572                 } else if (own_request) {
1573                         /*
1574                          * No SSID found. If SCAN results are as a result of
1575                          * own scan request and not due to a scan request on
1576                          * another shared interface, try another scan.
1577                          */
1578                         int timeout_sec = wpa_s->scan_interval;
1579                         int timeout_usec = 0;
1580 #ifdef CONFIG_P2P
1581                         int res;
1582
1583                         res = wpas_p2p_scan_no_go_seen(wpa_s);
1584                         if (res == 2)
1585                                 return 2;
1586                         if (res == 1)
1587                                 return 0;
1588
1589                         if (wpa_s->p2p_in_provisioning ||
1590                             wpa_s->show_group_started ||
1591                             wpa_s->p2p_in_invitation) {
1592                                 /*
1593                                  * Use shorter wait during P2P Provisioning
1594                                  * state and during P2P join-a-group operation
1595                                  * to speed up group formation.
1596                                  */
1597                                 timeout_sec = 0;
1598                                 timeout_usec = 250000;
1599                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1600                                                             timeout_usec);
1601                                 return 0;
1602                         }
1603 #endif /* CONFIG_P2P */
1604 #ifdef CONFIG_INTERWORKING
1605                         if (wpa_s->conf->auto_interworking &&
1606                             wpa_s->conf->interworking &&
1607                             wpa_s->conf->cred) {
1608                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1609                                         "start ANQP fetch since no matching "
1610                                         "networks found");
1611                                 wpa_s->network_select = 1;
1612                                 wpa_s->auto_network_select = 1;
1613                                 interworking_start_fetch_anqp(wpa_s);
1614                                 return 1;
1615                         }
1616 #endif /* CONFIG_INTERWORKING */
1617 #ifdef CONFIG_WPS
1618                         if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1619                                 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1620                                 timeout_sec = 0;
1621                                 timeout_usec = 500000;
1622                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1623                                                             timeout_usec);
1624                                 return 0;
1625                         }
1626 #endif /* CONFIG_WPS */
1627                         if (wpa_supplicant_req_sched_scan(wpa_s))
1628                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1629                                                             timeout_usec);
1630                 }
1631         }
1632         return 0;
1633 }
1634
1635
1636 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1637                                              union wpa_event_data *data)
1638 {
1639         struct wpa_supplicant *ifs;
1640         int res;
1641
1642         res = _wpa_supplicant_event_scan_results(wpa_s, data, 1);
1643         if (res == 2) {
1644                 /*
1645                  * Interface may have been removed, so must not dereference
1646                  * wpa_s after this.
1647                  */
1648                 return 1;
1649         }
1650         if (res != 0) {
1651                 /*
1652                  * If no scan results could be fetched, then no need to
1653                  * notify those interfaces that did not actually request
1654                  * this scan. Similarly, if scan results started a new operation on this
1655                  * interface, do not notify other interfaces to avoid concurrent
1656                  * operations during a connection attempt.
1657                  */
1658                 return 0;
1659         }
1660
1661         /*
1662          * Check other interfaces to see if they share the same radio. If
1663          * so, they get updated with this same scan info.
1664          */
1665         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1666                          radio_list) {
1667                 if (ifs != wpa_s) {
1668                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1669                                    "sibling", ifs->ifname);
1670                         _wpa_supplicant_event_scan_results(ifs, data, 0);
1671                 }
1672         }
1673
1674         return 0;
1675 }
1676
1677 #endif /* CONFIG_NO_SCAN_PROCESSING */
1678
1679
1680 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1681 {
1682 #ifdef CONFIG_NO_SCAN_PROCESSING
1683         return -1;
1684 #else /* CONFIG_NO_SCAN_PROCESSING */
1685         struct os_reltime now;
1686
1687         if (wpa_s->last_scan_res_used <= 0)
1688                 return -1;
1689
1690         os_get_reltime(&now);
1691         if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
1692                 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1693                 return -1;
1694         }
1695
1696         return wpas_select_network_from_last_scan(wpa_s, 0, 1);
1697 #endif /* CONFIG_NO_SCAN_PROCESSING */
1698 }
1699
1700 #ifdef CONFIG_WNM
1701
1702 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1703 {
1704         struct wpa_supplicant *wpa_s = eloop_ctx;
1705
1706         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1707                 return;
1708
1709         if (!wpa_s->no_keep_alive) {
1710                 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1711                            MAC2STR(wpa_s->bssid));
1712                 /* TODO: could skip this if normal data traffic has been sent */
1713                 /* TODO: Consider using some more appropriate data frame for
1714                  * this */
1715                 if (wpa_s->l2)
1716                         l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1717                                        (u8 *) "", 0);
1718         }
1719
1720 #ifdef CONFIG_SME
1721         if (wpa_s->sme.bss_max_idle_period) {
1722                 unsigned int msec;
1723                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1724                 if (msec > 100)
1725                         msec -= 100;
1726                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1727                                        wnm_bss_keep_alive, wpa_s, NULL);
1728         }
1729 #endif /* CONFIG_SME */
1730 }
1731
1732
1733 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1734                                    const u8 *ies, size_t ies_len)
1735 {
1736         struct ieee802_11_elems elems;
1737
1738         if (ies == NULL)
1739                 return;
1740
1741         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1742                 return;
1743
1744 #ifdef CONFIG_SME
1745         if (elems.bss_max_idle_period) {
1746                 unsigned int msec;
1747                 wpa_s->sme.bss_max_idle_period =
1748                         WPA_GET_LE16(elems.bss_max_idle_period);
1749                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1750                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1751                            (elems.bss_max_idle_period[2] & 0x01) ?
1752                            " (protected keep-live required)" : "");
1753                 if (wpa_s->sme.bss_max_idle_period == 0)
1754                         wpa_s->sme.bss_max_idle_period = 1;
1755                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1756                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1757                          /* msec times 1000 */
1758                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1759                         if (msec > 100)
1760                                 msec -= 100;
1761                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1762                                                wnm_bss_keep_alive, wpa_s,
1763                                                NULL);
1764                 }
1765         }
1766 #endif /* CONFIG_SME */
1767 }
1768
1769 #endif /* CONFIG_WNM */
1770
1771
1772 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1773 {
1774 #ifdef CONFIG_WNM
1775         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1776 #endif /* CONFIG_WNM */
1777 }
1778
1779
1780 #ifdef CONFIG_INTERWORKING
1781
1782 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1783                             size_t len)
1784 {
1785         int res;
1786
1787         wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1788         res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1789         if (res) {
1790                 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1791         }
1792
1793         return res;
1794 }
1795
1796
1797 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1798                                             const u8 *ies, size_t ies_len)
1799 {
1800         struct ieee802_11_elems elems;
1801
1802         if (ies == NULL)
1803                 return;
1804
1805         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1806                 return;
1807
1808         if (elems.qos_map_set) {
1809                 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1810                                  elems.qos_map_set_len);
1811         }
1812 }
1813
1814 #endif /* CONFIG_INTERWORKING */
1815
1816
1817 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1818                                           union wpa_event_data *data)
1819 {
1820         int l, len, found = 0, wpa_found, rsn_found;
1821         const u8 *p;
1822 #ifdef CONFIG_IEEE80211R
1823         u8 bssid[ETH_ALEN];
1824 #endif /* CONFIG_IEEE80211R */
1825
1826         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1827         if (data->assoc_info.req_ies)
1828                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1829                             data->assoc_info.req_ies_len);
1830         if (data->assoc_info.resp_ies) {
1831                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1832                             data->assoc_info.resp_ies_len);
1833 #ifdef CONFIG_TDLS
1834                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1835                                         data->assoc_info.resp_ies_len);
1836 #endif /* CONFIG_TDLS */
1837 #ifdef CONFIG_WNM
1838                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1839                                        data->assoc_info.resp_ies_len);
1840 #endif /* CONFIG_WNM */
1841 #ifdef CONFIG_INTERWORKING
1842                 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1843                                                 data->assoc_info.resp_ies_len);
1844 #endif /* CONFIG_INTERWORKING */
1845         }
1846         if (data->assoc_info.beacon_ies)
1847                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1848                             data->assoc_info.beacon_ies,
1849                             data->assoc_info.beacon_ies_len);
1850         if (data->assoc_info.freq)
1851                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1852                         data->assoc_info.freq);
1853
1854         p = data->assoc_info.req_ies;
1855         l = data->assoc_info.req_ies_len;
1856
1857         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1858         while (p && l >= 2) {
1859                 len = p[1] + 2;
1860                 if (len > l) {
1861                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1862                                     p, l);
1863                         break;
1864                 }
1865                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1866                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1867                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1868                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1869                                 break;
1870                         found = 1;
1871                         wpa_find_assoc_pmkid(wpa_s);
1872                         break;
1873                 }
1874                 l -= len;
1875                 p += len;
1876         }
1877         if (!found && data->assoc_info.req_ies)
1878                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1879
1880 #ifdef CONFIG_IEEE80211R
1881 #ifdef CONFIG_SME
1882         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1883                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1884                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1885                                                  data->assoc_info.resp_ies,
1886                                                  data->assoc_info.resp_ies_len,
1887                                                  bssid) < 0) {
1888                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1889                                 "Reassociation Response failed");
1890                         wpa_supplicant_deauthenticate(
1891                                 wpa_s, WLAN_REASON_INVALID_IE);
1892                         return -1;
1893                 }
1894         }
1895
1896         p = data->assoc_info.resp_ies;
1897         l = data->assoc_info.resp_ies_len;
1898
1899 #ifdef CONFIG_WPS_STRICT
1900         if (p && wpa_s->current_ssid &&
1901             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1902                 struct wpabuf *wps;
1903                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1904                 if (wps == NULL) {
1905                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1906                                 "include WPS IE in (Re)Association Response");
1907                         return -1;
1908                 }
1909
1910                 if (wps_validate_assoc_resp(wps) < 0) {
1911                         wpabuf_free(wps);
1912                         wpa_supplicant_deauthenticate(
1913                                 wpa_s, WLAN_REASON_INVALID_IE);
1914                         return -1;
1915                 }
1916                 wpabuf_free(wps);
1917         }
1918 #endif /* CONFIG_WPS_STRICT */
1919
1920         /* Go through the IEs and make a copy of the MDIE, if present. */
1921         while (p && l >= 2) {
1922                 len = p[1] + 2;
1923                 if (len > l) {
1924                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1925                                     p, l);
1926                         break;
1927                 }
1928                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1929                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1930                         wpa_s->sme.ft_used = 1;
1931                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1932                                   MOBILITY_DOMAIN_ID_LEN);
1933                         break;
1934                 }
1935                 l -= len;
1936                 p += len;
1937         }
1938 #endif /* CONFIG_SME */
1939
1940         /* Process FT when SME is in the driver */
1941         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1942             wpa_ft_is_completed(wpa_s->wpa)) {
1943                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1944                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1945                                                  data->assoc_info.resp_ies,
1946                                                  data->assoc_info.resp_ies_len,
1947                                                  bssid) < 0) {
1948                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1949                                 "Reassociation Response failed");
1950                         wpa_supplicant_deauthenticate(
1951                                 wpa_s, WLAN_REASON_INVALID_IE);
1952                         return -1;
1953                 }
1954                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1955         }
1956
1957         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1958                              data->assoc_info.resp_ies_len);
1959 #endif /* CONFIG_IEEE80211R */
1960
1961         /* WPA/RSN IE from Beacon/ProbeResp */
1962         p = data->assoc_info.beacon_ies;
1963         l = data->assoc_info.beacon_ies_len;
1964
1965         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1966          */
1967         wpa_found = rsn_found = 0;
1968         while (p && l >= 2) {
1969                 len = p[1] + 2;
1970                 if (len > l) {
1971                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1972                                     p, l);
1973                         break;
1974                 }
1975                 if (!wpa_found &&
1976                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1977                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1978                         wpa_found = 1;
1979                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1980                 }
1981
1982                 if (!rsn_found &&
1983                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1984                         rsn_found = 1;
1985                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1986                 }
1987
1988                 l -= len;
1989                 p += len;
1990         }
1991
1992         if (!wpa_found && data->assoc_info.beacon_ies)
1993                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1994         if (!rsn_found && data->assoc_info.beacon_ies)
1995                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1996         if (wpa_found || rsn_found)
1997                 wpa_s->ap_ies_from_associnfo = 1;
1998
1999         if (wpa_s->assoc_freq && data->assoc_info.freq &&
2000             wpa_s->assoc_freq != data->assoc_info.freq) {
2001                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2002                            "%u to %u MHz",
2003                            wpa_s->assoc_freq, data->assoc_info.freq);
2004                 wpa_supplicant_update_scan_results(wpa_s);
2005         }
2006
2007         wpa_s->assoc_freq = data->assoc_info.freq;
2008
2009         return 0;
2010 }
2011
2012
2013 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2014 {
2015         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2016
2017         if (!wpa_s->current_bss || !wpa_s->current_ssid)
2018                 return -1;
2019
2020         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2021                 return 0;
2022
2023         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2024                                         WPA_IE_VENDOR_TYPE);
2025         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2026
2027         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2028                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
2029             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2030                                  bss_rsn ? 2 + bss_rsn[1] : 0))
2031                 return -1;
2032
2033         return 0;
2034 }
2035
2036
2037 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2038                                        union wpa_event_data *data)
2039 {
2040         u8 bssid[ETH_ALEN];
2041         int ft_completed;
2042
2043 #ifdef CONFIG_AP
2044         if (wpa_s->ap_iface) {
2045                 if (!data)
2046                         return;
2047                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2048                                     data->assoc_info.addr,
2049                                     data->assoc_info.req_ies,
2050                                     data->assoc_info.req_ies_len,
2051                                     data->assoc_info.reassoc);
2052                 return;
2053         }
2054 #endif /* CONFIG_AP */
2055
2056         eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2057
2058         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2059         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2060                 return;
2061
2062         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2063                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
2064                 wpa_supplicant_deauthenticate(
2065                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2066                 return;
2067         }
2068
2069         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
2070         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
2071                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2072                         MACSTR, MAC2STR(bssid));
2073                 random_add_randomness(bssid, ETH_ALEN);
2074                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2075                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2076                 wpas_notify_bssid_changed(wpa_s);
2077
2078                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2079                         wpa_clear_keys(wpa_s, bssid);
2080                 }
2081                 if (wpa_supplicant_select_config(wpa_s) < 0) {
2082                         wpa_supplicant_deauthenticate(
2083                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2084                         return;
2085                 }
2086
2087                 if (wpa_s->conf->ap_scan == 1 &&
2088                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
2089                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
2090                                 wpa_msg(wpa_s, MSG_WARNING,
2091                                         "WPA/RSN IEs not updated");
2092                 }
2093         }
2094
2095 #ifdef CONFIG_SME
2096         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2097         wpa_s->sme.prev_bssid_set = 1;
2098         wpa_s->sme.last_unprot_disconnect.sec = 0;
2099 #endif /* CONFIG_SME */
2100
2101         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2102         if (wpa_s->current_ssid) {
2103                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2104                  * initialized before association, but for other modes,
2105                  * initialize PC/SC here, if the current configuration needs
2106                  * smartcard or SIM/USIM. */
2107                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2108         }
2109         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2110         if (wpa_s->l2)
2111                 l2_packet_notify_auth_start(wpa_s->l2);
2112
2113         /*
2114          * Set portEnabled first to FALSE in order to get EAP state machine out
2115          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2116          * state machine may transit to AUTHENTICATING state based on obsolete
2117          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2118          * AUTHENTICATED without ever giving chance to EAP state machine to
2119          * reset the state.
2120          */
2121         if (!ft_completed) {
2122                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2123                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2124         }
2125         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
2126                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2127         /* 802.1X::portControl = Auto */
2128         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2129         wpa_s->eapol_received = 0;
2130         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2131             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2132             (wpa_s->current_ssid &&
2133              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
2134                 if (wpa_s->current_ssid &&
2135                     wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
2136                     (wpa_s->drv_flags &
2137                      WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2138                         /*
2139                          * Set the key after having received joined-IBSS event
2140                          * from the driver.
2141                          */
2142                         wpa_supplicant_set_wpa_none_key(wpa_s,
2143                                                         wpa_s->current_ssid);
2144                 }
2145                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2146                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2147         } else if (!ft_completed) {
2148                 /* Timeout for receiving the first EAPOL packet */
2149                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2150         }
2151         wpa_supplicant_cancel_scan(wpa_s);
2152
2153         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2154             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2155                 /*
2156                  * We are done; the driver will take care of RSN 4-way
2157                  * handshake.
2158                  */
2159                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2160                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2161                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2162                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2163         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2164                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2165                 /*
2166                  * The driver will take care of RSN 4-way handshake, so we need
2167                  * to allow EAPOL supplicant to complete its work without
2168                  * waiting for WPA supplicant.
2169                  */
2170                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2171         } else if (ft_completed) {
2172                 /*
2173                  * FT protocol completed - make sure EAPOL state machine ends
2174                  * up in authenticated.
2175                  */
2176                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2177                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2178                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2179                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2180         }
2181
2182         wpa_s->last_eapol_matches_bssid = 0;
2183
2184         if (wpa_s->pending_eapol_rx) {
2185                 struct os_reltime now, age;
2186                 os_get_reltime(&now);
2187                 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
2188                 if (age.sec == 0 && age.usec < 100000 &&
2189                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2190                     0) {
2191                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2192                                 "frame that was received just before "
2193                                 "association notification");
2194                         wpa_supplicant_rx_eapol(
2195                                 wpa_s, wpa_s->pending_eapol_rx_src,
2196                                 wpabuf_head(wpa_s->pending_eapol_rx),
2197                                 wpabuf_len(wpa_s->pending_eapol_rx));
2198                 }
2199                 wpabuf_free(wpa_s->pending_eapol_rx);
2200                 wpa_s->pending_eapol_rx = NULL;
2201         }
2202
2203         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2204              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
2205             wpa_s->current_ssid &&
2206             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2207                 /* Set static WEP keys again */
2208                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2209         }
2210
2211 #ifdef CONFIG_IBSS_RSN
2212         if (wpa_s->current_ssid &&
2213             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2214             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2215             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2216             wpa_s->ibss_rsn == NULL) {
2217                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2218                 if (!wpa_s->ibss_rsn) {
2219                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2220                         wpa_supplicant_deauthenticate(
2221                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2222                         return;
2223                 }
2224
2225                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2226         }
2227 #endif /* CONFIG_IBSS_RSN */
2228
2229         wpas_wps_notify_assoc(wpa_s, bssid);
2230
2231         if (data) {
2232                 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2233                                     data->assoc_info.resp_ies_len,
2234                                     &data->assoc_info.wmm_params);
2235
2236                 if (wpa_s->reassoc_same_bss)
2237                         wmm_ac_restore_tspecs(wpa_s);
2238         }
2239 }
2240
2241
2242 static int disconnect_reason_recoverable(u16 reason_code)
2243 {
2244         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2245                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2246                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2247 }
2248
2249
2250 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2251                                           u16 reason_code,
2252                                           int locally_generated)
2253 {
2254         const u8 *bssid;
2255
2256         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2257                 /*
2258                  * At least Host AP driver and a Prism3 card seemed to be
2259                  * generating streams of disconnected events when configuring
2260                  * IBSS for WPA-None. Ignore them for now.
2261                  */
2262                 return;
2263         }
2264
2265         bssid = wpa_s->bssid;
2266         if (is_zero_ether_addr(bssid))
2267                 bssid = wpa_s->pending_bssid;
2268
2269         if (!is_zero_ether_addr(bssid) ||
2270             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2271                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2272                         " reason=%d%s",
2273                         MAC2STR(bssid), reason_code,
2274                         locally_generated ? " locally_generated=1" : "");
2275         }
2276 }
2277
2278
2279 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2280                                  int locally_generated)
2281 {
2282         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2283             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2284                 return 0; /* Not in 4-way handshake with PSK */
2285
2286         /*
2287          * It looks like connection was lost while trying to go through PSK
2288          * 4-way handshake. Filter out known disconnection cases that are caused
2289          * by something else than PSK mismatch to avoid confusing reports.
2290          */
2291
2292         if (locally_generated) {
2293                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2294                         return 0;
2295         }
2296
2297         return 1;
2298 }
2299
2300
2301 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2302                                                  u16 reason_code,
2303                                                  int locally_generated)
2304 {
2305         const u8 *bssid;
2306         int authenticating;
2307         u8 prev_pending_bssid[ETH_ALEN];
2308         struct wpa_bss *fast_reconnect = NULL;
2309         struct wpa_ssid *fast_reconnect_ssid = NULL;
2310         struct wpa_ssid *last_ssid;
2311
2312         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2313         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2314
2315         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2316                 /*
2317                  * At least Host AP driver and a Prism3 card seemed to be
2318                  * generating streams of disconnected events when configuring
2319                  * IBSS for WPA-None. Ignore them for now.
2320                  */
2321                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2322                         "IBSS/WPA-None mode");
2323                 return;
2324         }
2325
2326         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2327                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2328                         "pre-shared key may be incorrect");
2329                 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2330                         return; /* P2P group removed */
2331                 wpas_auth_failed(wpa_s, "WRONG_KEY");
2332         }
2333         if (!wpa_s->disconnected &&
2334             (!wpa_s->auto_reconnect_disabled ||
2335              wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2336              wpas_wps_searching(wpa_s))) {
2337                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2338                         "reconnect (wps=%d/%d wpa_state=%d)",
2339                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2340                         wpas_wps_searching(wpa_s),
2341                         wpa_s->wpa_state);
2342                 if (wpa_s->wpa_state == WPA_COMPLETED &&
2343                     wpa_s->current_ssid &&
2344                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2345                     !locally_generated &&
2346                     disconnect_reason_recoverable(reason_code)) {
2347                         /*
2348                          * It looks like the AP has dropped association with
2349                          * us, but could allow us to get back in. Try to
2350                          * reconnect to the same BSS without full scan to save
2351                          * time for some common cases.
2352                          */
2353                         fast_reconnect = wpa_s->current_bss;
2354                         fast_reconnect_ssid = wpa_s->current_ssid;
2355                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2356                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2357                 else
2358                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2359                                 "immediate scan");
2360         } else {
2361                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2362                         "try to re-connect");
2363                 wpa_s->reassociate = 0;
2364                 wpa_s->disconnected = 1;
2365                 wpa_supplicant_cancel_sched_scan(wpa_s);
2366         }
2367         bssid = wpa_s->bssid;
2368         if (is_zero_ether_addr(bssid))
2369                 bssid = wpa_s->pending_bssid;
2370         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2371                 wpas_connection_failed(wpa_s, bssid);
2372         wpa_sm_notify_disassoc(wpa_s->wpa);
2373         if (locally_generated)
2374                 wpa_s->disconnect_reason = -reason_code;
2375         else
2376                 wpa_s->disconnect_reason = reason_code;
2377         wpas_notify_disconnect_reason(wpa_s);
2378         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2379                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2380                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2381         }
2382         last_ssid = wpa_s->current_ssid;
2383         wpa_supplicant_mark_disassoc(wpa_s);
2384
2385         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2386                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2387                 wpa_s->current_ssid = last_ssid;
2388         }
2389
2390         if (fast_reconnect &&
2391             !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2392             !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2393             !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2394                              fast_reconnect->ssid_len) &&
2395             !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
2396 #ifndef CONFIG_NO_SCAN_PROCESSING
2397                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2398                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2399                                            fast_reconnect_ssid) < 0) {
2400                         /* Recover through full scan */
2401                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2402                 }
2403 #endif /* CONFIG_NO_SCAN_PROCESSING */
2404         } else if (fast_reconnect) {
2405                 /*
2406                  * Could not reconnect to the same BSS due to network being
2407                  * disabled. Use a new scan to match the alternative behavior
2408                  * above, i.e., to continue automatic reconnection attempt in a
2409                  * way that enforces disabled network rules.
2410                  */
2411                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2412         }
2413 }
2414
2415
2416 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2417 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2418 {
2419         struct wpa_supplicant *wpa_s = eloop_ctx;
2420
2421         if (!wpa_s->pending_mic_error_report)
2422                 return;
2423
2424         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2425         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2426         wpa_s->pending_mic_error_report = 0;
2427 }
2428 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2429
2430
2431 static void
2432 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2433                                          union wpa_event_data *data)
2434 {
2435         int pairwise;
2436         struct os_reltime t;
2437
2438         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2439         pairwise = (data && data->michael_mic_failure.unicast);
2440         os_get_reltime(&t);
2441         if ((wpa_s->last_michael_mic_error.sec &&
2442              !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
2443             wpa_s->pending_mic_error_report) {
2444                 if (wpa_s->pending_mic_error_report) {
2445                         /*
2446                          * Send the pending MIC error report immediately since
2447                          * we are going to start countermeasures and AP better
2448                          * do the same.
2449                          */
2450                         wpa_sm_key_request(wpa_s->wpa, 1,
2451                                            wpa_s->pending_mic_error_pairwise);
2452                 }
2453
2454                 /* Send the new MIC error report immediately since we are going
2455                  * to start countermeasures and AP better do the same.
2456                  */
2457                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2458
2459                 /* initialize countermeasures */
2460                 wpa_s->countermeasures = 1;
2461
2462                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2463
2464                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2465
2466                 /*
2467                  * Need to wait for completion of request frame. We do not get
2468                  * any callback for the message completion, so just wait a
2469                  * short while and hope for the best. */
2470                 os_sleep(0, 10000);
2471
2472                 wpa_drv_set_countermeasures(wpa_s, 1);
2473                 wpa_supplicant_deauthenticate(wpa_s,
2474                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2475                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2476                                      wpa_s, NULL);
2477                 eloop_register_timeout(60, 0,
2478                                        wpa_supplicant_stop_countermeasures,
2479                                        wpa_s, NULL);
2480                 /* TODO: mark the AP rejected for 60 second. STA is
2481                  * allowed to associate with another AP.. */
2482         } else {
2483 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2484                 if (wpa_s->mic_errors_seen) {
2485                         /*
2486                          * Reduce the effectiveness of Michael MIC error
2487                          * reports as a means for attacking against TKIP if
2488                          * more than one MIC failure is noticed with the same
2489                          * PTK. We delay the transmission of the reports by a
2490                          * random time between 0 and 60 seconds in order to
2491                          * force the attacker wait 60 seconds before getting
2492                          * the information on whether a frame resulted in a MIC
2493                          * failure.
2494                          */
2495                         u8 rval[4];
2496                         int sec;
2497
2498                         if (os_get_random(rval, sizeof(rval)) < 0)
2499                                 sec = os_random() % 60;
2500                         else
2501                                 sec = WPA_GET_BE32(rval) % 60;
2502                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2503                                 "report %d seconds", sec);
2504                         wpa_s->pending_mic_error_report = 1;
2505                         wpa_s->pending_mic_error_pairwise = pairwise;
2506                         eloop_cancel_timeout(
2507                                 wpa_supplicant_delayed_mic_error_report,
2508                                 wpa_s, NULL);
2509                         eloop_register_timeout(
2510                                 sec, os_random() % 1000000,
2511                                 wpa_supplicant_delayed_mic_error_report,
2512                                 wpa_s, NULL);
2513                 } else {
2514                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2515                 }
2516 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2517                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2518 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2519         }
2520         wpa_s->last_michael_mic_error = t;
2521         wpa_s->mic_errors_seen++;
2522 }
2523
2524
2525 #ifdef CONFIG_TERMINATE_ONLASTIF
2526 static int any_interfaces(struct wpa_supplicant *head)
2527 {
2528         struct wpa_supplicant *wpa_s;
2529
2530         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2531                 if (!wpa_s->interface_removed)
2532                         return 1;
2533         return 0;
2534 }
2535 #endif /* CONFIG_TERMINATE_ONLASTIF */
2536
2537
2538 static void
2539 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2540                                       union wpa_event_data *data)
2541 {
2542         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2543                 return;
2544
2545         switch (data->interface_status.ievent) {
2546         case EVENT_INTERFACE_ADDED:
2547                 if (!wpa_s->interface_removed)
2548                         break;
2549                 wpa_s->interface_removed = 0;
2550                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2551                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2552                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2553                                 "driver after interface was added");
2554                 }
2555                 break;
2556         case EVENT_INTERFACE_REMOVED:
2557                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2558                 wpa_s->interface_removed = 1;
2559                 wpa_supplicant_mark_disassoc(wpa_s);
2560                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2561                 l2_packet_deinit(wpa_s->l2);
2562                 wpa_s->l2 = NULL;
2563 #ifdef CONFIG_TERMINATE_ONLASTIF
2564                 /* check if last interface */
2565                 if (!any_interfaces(wpa_s->global->ifaces))
2566                         eloop_terminate();
2567 #endif /* CONFIG_TERMINATE_ONLASTIF */
2568                 break;
2569         }
2570 }
2571
2572
2573 #ifdef CONFIG_PEERKEY
2574 static void
2575 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2576                               union wpa_event_data *data)
2577 {
2578         if (data == NULL)
2579                 return;
2580         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2581 }
2582 #endif /* CONFIG_PEERKEY */
2583
2584
2585 #ifdef CONFIG_TDLS
2586 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2587                                       union wpa_event_data *data)
2588 {
2589         if (data == NULL)
2590                 return;
2591         switch (data->tdls.oper) {
2592         case TDLS_REQUEST_SETUP:
2593                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2594                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2595                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2596                 else
2597                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2598                 break;
2599         case TDLS_REQUEST_TEARDOWN:
2600                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2601                         wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2602                                                data->tdls.reason_code);
2603                 else
2604                         wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2605                                           data->tdls.peer);
2606                 break;
2607         case TDLS_REQUEST_DISCOVER:
2608                         wpa_tdls_send_discovery_request(wpa_s->wpa,
2609                                                         data->tdls.peer);
2610                 break;
2611         }
2612 }
2613 #endif /* CONFIG_TDLS */
2614
2615
2616 #ifdef CONFIG_WNM
2617 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2618                                      union wpa_event_data *data)
2619 {
2620         if (data == NULL)
2621                 return;
2622         switch (data->wnm.oper) {
2623         case WNM_OPER_SLEEP:
2624                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2625                            "(action=%d, intval=%d)",
2626                            data->wnm.sleep_action, data->wnm.sleep_intval);
2627                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2628                                              data->wnm.sleep_intval, NULL);
2629                 break;
2630         }
2631 }
2632 #endif /* CONFIG_WNM */
2633
2634
2635 #ifdef CONFIG_IEEE80211R
2636 static void
2637 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2638                                  union wpa_event_data *data)
2639 {
2640         if (data == NULL)
2641                 return;
2642
2643         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2644                                     data->ft_ies.ies_len,
2645                                     data->ft_ies.ft_action,
2646                                     data->ft_ies.target_ap,
2647                                     data->ft_ies.ric_ies,
2648                                     data->ft_ies.ric_ies_len) < 0) {
2649                 /* TODO: prevent MLME/driver from trying to associate? */
2650         }
2651 }
2652 #endif /* CONFIG_IEEE80211R */
2653
2654
2655 #ifdef CONFIG_IBSS_RSN
2656 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2657                                                 union wpa_event_data *data)
2658 {
2659         struct wpa_ssid *ssid;
2660         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2661                 return;
2662         if (data == NULL)
2663                 return;
2664         ssid = wpa_s->current_ssid;
2665         if (ssid == NULL)
2666                 return;
2667         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2668                 return;
2669
2670         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2671 }
2672
2673
2674 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2675                                            union wpa_event_data *data)
2676 {
2677         struct wpa_ssid *ssid = wpa_s->current_ssid;
2678
2679         if (ssid == NULL)
2680                 return;
2681
2682         /* check if the ssid is correctly configured as IBSS/RSN */
2683         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2684                 return;
2685
2686         ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2687                              data->rx_mgmt.frame_len);
2688 }
2689 #endif /* CONFIG_IBSS_RSN */
2690
2691
2692 #ifdef CONFIG_IEEE80211R
2693 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2694                          size_t len)
2695 {
2696         const u8 *sta_addr, *target_ap_addr;
2697         u16 status;
2698
2699         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2700         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2701                 return; /* only SME case supported for now */
2702         if (len < 1 + 2 * ETH_ALEN + 2)
2703                 return;
2704         if (data[0] != 2)
2705                 return; /* Only FT Action Response is supported for now */
2706         sta_addr = data + 1;
2707         target_ap_addr = data + 1 + ETH_ALEN;
2708         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2709         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2710                 MACSTR " TargetAP " MACSTR " status %u",
2711                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2712
2713         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2714                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2715                         " in FT Action Response", MAC2STR(sta_addr));
2716                 return;
2717         }
2718
2719         if (status) {
2720                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2721                         "failure (status code %d)", status);
2722                 /* TODO: report error to FT code(?) */
2723                 return;
2724         }
2725
2726         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2727                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2728                                     target_ap_addr, NULL, 0) < 0)
2729                 return;
2730
2731 #ifdef CONFIG_SME
2732         {
2733                 struct wpa_bss *bss;
2734                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2735                 if (bss)
2736                         wpa_s->sme.freq = bss->freq;
2737                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2738                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2739                               WLAN_AUTH_FT);
2740         }
2741 #endif /* CONFIG_SME */
2742 }
2743 #endif /* CONFIG_IEEE80211R */
2744
2745
2746 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2747                                                struct unprot_deauth *e)
2748 {
2749 #ifdef CONFIG_IEEE80211W
2750         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2751                    "dropped: " MACSTR " -> " MACSTR
2752                    " (reason code %u)",
2753                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2754         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2755 #endif /* CONFIG_IEEE80211W */
2756 }
2757
2758
2759 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2760                                                  struct unprot_disassoc *e)
2761 {
2762 #ifdef CONFIG_IEEE80211W
2763         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2764                    "dropped: " MACSTR " -> " MACSTR
2765                    " (reason code %u)",
2766                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2767         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2768 #endif /* CONFIG_IEEE80211W */
2769 }
2770
2771
2772 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2773                                   u16 reason_code, int locally_generated,
2774                                   const u8 *ie, size_t ie_len, int deauth)
2775 {
2776 #ifdef CONFIG_AP
2777         if (wpa_s->ap_iface && addr) {
2778                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2779                 return;
2780         }
2781
2782         if (wpa_s->ap_iface) {
2783                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2784                 return;
2785         }
2786 #endif /* CONFIG_AP */
2787
2788         if (!locally_generated)
2789                 wpa_s->own_disconnect_req = 0;
2790
2791         wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2792
2793         if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2794               ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2795                 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2796                eapol_sm_failed(wpa_s->eapol))) &&
2797              !wpa_s->eap_expected_failure))
2798                 wpas_auth_failed(wpa_s, "AUTH_FAILED");
2799
2800 #ifdef CONFIG_P2P
2801         if (deauth && reason_code > 0) {
2802                 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2803                                           locally_generated) > 0) {
2804                         /*
2805                          * The interface was removed, so cannot continue
2806                          * processing any additional operations after this.
2807                          */
2808                         return;
2809                 }
2810         }
2811 #endif /* CONFIG_P2P */
2812
2813         wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2814                                              locally_generated);
2815 }
2816
2817
2818 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2819                                 struct disassoc_info *info)
2820 {
2821         u16 reason_code = 0;
2822         int locally_generated = 0;
2823         const u8 *addr = NULL;
2824         const u8 *ie = NULL;
2825         size_t ie_len = 0;
2826
2827         wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2828
2829         if (info) {
2830                 addr = info->addr;
2831                 ie = info->ie;
2832                 ie_len = info->ie_len;
2833                 reason_code = info->reason_code;
2834                 locally_generated = info->locally_generated;
2835                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2836                         locally_generated ? " (locally generated)" : "");
2837                 if (addr)
2838                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2839                                 MAC2STR(addr));
2840                 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2841                             ie, ie_len);
2842         }
2843
2844 #ifdef CONFIG_AP
2845         if (wpa_s->ap_iface && info && info->addr) {
2846                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2847                 return;
2848         }
2849
2850         if (wpa_s->ap_iface) {
2851                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2852                 return;
2853         }
2854 #endif /* CONFIG_AP */
2855
2856 #ifdef CONFIG_P2P
2857         if (info) {
2858                 wpas_p2p_disassoc_notif(
2859                         wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2860                         locally_generated);
2861         }
2862 #endif /* CONFIG_P2P */
2863
2864         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2865                 sme_event_disassoc(wpa_s, info);
2866
2867         wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2868                               ie, ie_len, 0);
2869 }
2870
2871
2872 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2873                               struct deauth_info *info)
2874 {
2875         u16 reason_code = 0;
2876         int locally_generated = 0;
2877         const u8 *addr = NULL;
2878         const u8 *ie = NULL;
2879         size_t ie_len = 0;
2880
2881         wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2882
2883         if (info) {
2884                 addr = info->addr;
2885                 ie = info->ie;
2886                 ie_len = info->ie_len;
2887                 reason_code = info->reason_code;
2888                 locally_generated = info->locally_generated;
2889                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2890                         reason_code,
2891                         locally_generated ? " (locally generated)" : "");
2892                 if (addr) {
2893                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2894                                 MAC2STR(addr));
2895                 }
2896                 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2897                             ie, ie_len);
2898         }
2899
2900         wpa_reset_ft_completed(wpa_s->wpa);
2901
2902         wpas_event_disconnect(wpa_s, addr, reason_code,
2903                               locally_generated, ie, ie_len, 1);
2904 }
2905
2906
2907 static const char * reg_init_str(enum reg_change_initiator init)
2908 {
2909         switch (init) {
2910         case REGDOM_SET_BY_CORE:
2911                 return "CORE";
2912         case REGDOM_SET_BY_USER:
2913                 return "USER";
2914         case REGDOM_SET_BY_DRIVER:
2915                 return "DRIVER";
2916         case REGDOM_SET_BY_COUNTRY_IE:
2917                 return "COUNTRY_IE";
2918         case REGDOM_BEACON_HINT:
2919                 return "BEACON_HINT";
2920         }
2921         return "?";
2922 }
2923
2924
2925 static const char * reg_type_str(enum reg_type type)
2926 {
2927         switch (type) {
2928         case REGDOM_TYPE_UNKNOWN:
2929                 return "UNKNOWN";
2930         case REGDOM_TYPE_COUNTRY:
2931                 return "COUNTRY";
2932         case REGDOM_TYPE_WORLD:
2933                 return "WORLD";
2934         case REGDOM_TYPE_CUSTOM_WORLD:
2935                 return "CUSTOM_WORLD";
2936         case REGDOM_TYPE_INTERSECTION:
2937                 return "INTERSECTION";
2938         }
2939         return "?";
2940 }
2941
2942
2943 static void wpa_supplicant_update_channel_list(
2944         struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
2945 {
2946         struct wpa_supplicant *ifs;
2947
2948         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
2949                 reg_init_str(info->initiator), reg_type_str(info->type),
2950                 info->alpha2[0] ? " alpha2=" : "",
2951                 info->alpha2[0] ? info->alpha2 : "");
2952
2953         if (wpa_s->drv_priv == NULL)
2954                 return; /* Ignore event during drv initialization */
2955
2956         free_hw_features(wpa_s);
2957         wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2958                 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2959
2960         wpas_p2p_update_channel_list(wpa_s);
2961
2962         /*
2963          * Check other interfaces to see if they share the same radio. If
2964          * so, they get updated with this same hw mode info.
2965          */
2966         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2967                          radio_list) {
2968                 if (ifs != wpa_s) {
2969                         wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2970                                    ifs->ifname);
2971                         free_hw_features(ifs);
2972                         ifs->hw.modes = wpa_drv_get_hw_feature_data(
2973                                 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2974                 }
2975         }
2976
2977         /* Restart sched_scan with updated channel list */
2978         if (wpa_s->sched_scanning) {
2979                 wpa_dbg(wpa_s, MSG_DEBUG,
2980                         "Channel list changed restart sched scan.");
2981                 wpa_supplicant_cancel_sched_scan(wpa_s);
2982                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2983         }
2984 }
2985
2986
2987 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2988                                       const u8 *frame, size_t len, int freq,
2989                                       int rssi)
2990 {
2991         const struct ieee80211_mgmt *mgmt;
2992         const u8 *payload;
2993         size_t plen;
2994         u8 category;
2995
2996         if (len < IEEE80211_HDRLEN + 2)
2997                 return;
2998
2999         mgmt = (const struct ieee80211_mgmt *) frame;
3000         payload = frame + IEEE80211_HDRLEN;
3001         category = *payload++;
3002         plen = len - IEEE80211_HDRLEN - 1;
3003
3004         wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3005                 " Category=%u DataLen=%d freq=%d MHz",
3006                 MAC2STR(mgmt->sa), category, (int) plen, freq);
3007
3008         if (category == WLAN_ACTION_WMM) {
3009                 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3010                 return;
3011         }
3012
3013 #ifdef CONFIG_IEEE80211R
3014         if (category == WLAN_ACTION_FT) {
3015                 ft_rx_action(wpa_s, payload, plen);
3016                 return;
3017         }
3018 #endif /* CONFIG_IEEE80211R */
3019
3020 #ifdef CONFIG_IEEE80211W
3021 #ifdef CONFIG_SME
3022         if (category == WLAN_ACTION_SA_QUERY) {
3023                 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3024                 return;
3025         }
3026 #endif /* CONFIG_SME */
3027 #endif /* CONFIG_IEEE80211W */
3028
3029 #ifdef CONFIG_WNM
3030         if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3031                 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3032                 return;
3033         }
3034 #endif /* CONFIG_WNM */
3035
3036 #ifdef CONFIG_GAS
3037         if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3038              mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3039             gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
3040                          mgmt->u.action.category,
3041                          payload, plen, freq) == 0)
3042                 return;
3043 #endif /* CONFIG_GAS */
3044
3045 #ifdef CONFIG_TDLS
3046         if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3047             payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3048                 wpa_dbg(wpa_s, MSG_DEBUG,
3049                         "TDLS: Received Discovery Response from " MACSTR,
3050                         MAC2STR(mgmt->sa));
3051                 return;
3052         }
3053 #endif /* CONFIG_TDLS */
3054
3055 #ifdef CONFIG_INTERWORKING
3056         if (category == WLAN_ACTION_QOS && plen >= 1 &&
3057             payload[0] == QOS_QOS_MAP_CONFIG) {
3058                 const u8 *pos = payload + 1;
3059                 size_t qlen = plen - 1;
3060                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3061                         MACSTR, MAC2STR(mgmt->sa));
3062                 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3063                     qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3064                     pos[1] <= qlen - 2 && pos[1] >= 16)
3065                         wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3066                 return;
3067         }
3068 #endif /* CONFIG_INTERWORKING */
3069
3070         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3071             payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3072                 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3073                 return;
3074         }
3075
3076         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3077             payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3078                 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3079                                                          payload + 1, plen - 1,
3080                                                          rssi);
3081                 return;
3082         }
3083
3084         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3085                            category, payload, plen, freq);
3086         if (wpa_s->ifmsh)
3087                 mesh_mpm_action_rx(wpa_s, mgmt, len);
3088 }
3089
3090
3091 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3092                                              union wpa_event_data *event)
3093 {
3094 #ifdef CONFIG_P2P
3095         struct wpa_supplicant *ifs;
3096 #endif /* CONFIG_P2P */
3097         struct wpa_freq_range_list *list;
3098         char *str = NULL;
3099
3100         list = &event->freq_range;
3101
3102         if (list->num)
3103                 str = freq_range_list_str(list);
3104         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3105                 str ? str : "");
3106
3107 #ifdef CONFIG_P2P
3108         if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3109                 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3110                         __func__);
3111         } else {
3112                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
3113                 wpas_p2p_update_channel_list(wpa_s);
3114         }
3115
3116         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3117                 int freq;
3118                 if (!ifs->current_ssid ||
3119                     !ifs->current_ssid->p2p_group ||
3120                     (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
3121                      ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
3122                         continue;
3123
3124                 freq = ifs->current_ssid->frequency;
3125                 if (!freq_range_list_includes(list, freq)) {
3126                         wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
3127                                 freq);
3128                         continue;
3129                 }
3130
3131                 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
3132                         freq);
3133                 /* TODO: Consider using CSA or removing the group within
3134                  * wpa_supplicant */
3135                 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
3136         }
3137 #endif /* CONFIG_P2P */
3138
3139         os_free(str);
3140 }
3141
3142
3143 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3144                                             union wpa_event_data *data)
3145 {
3146         wpa_dbg(wpa_s, MSG_DEBUG,
3147                 "Connection authorized by device, previous state %d",
3148                 wpa_s->wpa_state);
3149         if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3150                 wpa_supplicant_cancel_auth_timeout(wpa_s);
3151                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3152                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3153                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3154         }
3155         wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3156         wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
3157                                data->assoc_info.ptk_kck_len,
3158                                data->assoc_info.ptk_kek,
3159                                data->assoc_info.ptk_kek_len);
3160 }
3161
3162
3163 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3164                           union wpa_event_data *data)
3165 {
3166         struct wpa_supplicant *wpa_s = ctx;
3167         int resched;
3168
3169         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3170             event != EVENT_INTERFACE_ENABLED &&
3171             event != EVENT_INTERFACE_STATUS &&
3172             event != EVENT_SCHED_SCAN_STOPPED) {
3173                 wpa_dbg(wpa_s, MSG_DEBUG,
3174                         "Ignore event %s (%d) while interface is disabled",
3175                         event_to_string(event), event);
3176                 return;
3177         }
3178
3179 #ifndef CONFIG_NO_STDOUT_DEBUG
3180 {
3181         int level = MSG_DEBUG;
3182
3183         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
3184                 const struct ieee80211_hdr *hdr;
3185                 u16 fc;
3186                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3187                 fc = le_to_host16(hdr->frame_control);
3188                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3189                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3190                         level = MSG_EXCESSIVE;
3191         }
3192
3193         wpa_dbg(wpa_s, level, "Event %s (%d) received",
3194                 event_to_string(event), event);
3195 }
3196 #endif /* CONFIG_NO_STDOUT_DEBUG */
3197
3198         switch (event) {
3199         case EVENT_AUTH:
3200                 sme_event_auth(wpa_s, data);
3201                 break;
3202         case EVENT_ASSOC:
3203                 wpa_supplicant_event_assoc(wpa_s, data);
3204                 if (data && data->assoc_info.authorized)
3205                         wpa_supplicant_event_assoc_auth(wpa_s, data);
3206                 break;
3207         case EVENT_DISASSOC:
3208                 wpas_event_disassoc(wpa_s,
3209                                     data ? &data->disassoc_info : NULL);
3210                 break;
3211         case EVENT_DEAUTH:
3212                 wpas_event_deauth(wpa_s,
3213                                   data ? &data->deauth_info : NULL);
3214                 break;
3215         case EVENT_MICHAEL_MIC_FAILURE:
3216                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3217                 break;
3218 #ifndef CONFIG_NO_SCAN_PROCESSING
3219         case EVENT_SCAN_STARTED:
3220                 os_get_reltime(&wpa_s->scan_start_time);
3221                 if (wpa_s->own_scan_requested) {
3222                         struct os_reltime diff;
3223
3224                         os_reltime_sub(&wpa_s->scan_start_time,
3225                                        &wpa_s->scan_trigger_time, &diff);
3226                         wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3227                                 diff.sec, diff.usec);
3228                         wpa_s->own_scan_requested = 0;
3229                         wpa_s->own_scan_running = 1;
3230                         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3231                             wpa_s->manual_scan_use_id) {
3232                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
3233                                              WPA_EVENT_SCAN_STARTED "id=%u",
3234                                              wpa_s->manual_scan_id);
3235                         } else {
3236                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
3237                                              WPA_EVENT_SCAN_STARTED);
3238                         }
3239                 } else {
3240                         wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3241                         wpa_s->radio->external_scan_running = 1;
3242                         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
3243                 }
3244                 break;
3245         case EVENT_SCAN_RESULTS:
3246                 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3247                         struct os_reltime now, diff;
3248                         os_get_reltime(&now);
3249                         os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3250                         wpa_s->scan_start_time.sec = 0;
3251                         wpa_s->scan_start_time.usec = 0;
3252                         wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3253                                 diff.sec, diff.usec);
3254                 }
3255                 if (wpa_supplicant_event_scan_results(wpa_s, data))
3256                         break; /* interface may have been removed */
3257                 wpa_s->own_scan_running = 0;
3258                 wpa_s->radio->external_scan_running = 0;
3259                 radio_work_check_next(wpa_s);
3260                 break;
3261 #endif /* CONFIG_NO_SCAN_PROCESSING */
3262         case EVENT_ASSOCINFO:
3263                 wpa_supplicant_event_associnfo(wpa_s, data);
3264                 break;
3265         case EVENT_INTERFACE_STATUS:
3266                 wpa_supplicant_event_interface_status(wpa_s, data);
3267                 break;
3268         case EVENT_PMKID_CANDIDATE:
3269                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3270                 break;
3271 #ifdef CONFIG_PEERKEY
3272         case EVENT_STKSTART:
3273                 wpa_supplicant_event_stkstart(wpa_s, data);
3274                 break;
3275 #endif /* CONFIG_PEERKEY */
3276 #ifdef CONFIG_TDLS
3277         case EVENT_TDLS:
3278                 wpa_supplicant_event_tdls(wpa_s, data);
3279                 break;
3280 #endif /* CONFIG_TDLS */
3281 #ifdef CONFIG_WNM
3282         case EVENT_WNM:
3283                 wpa_supplicant_event_wnm(wpa_s, data);
3284                 break;
3285 #endif /* CONFIG_WNM */
3286 #ifdef CONFIG_IEEE80211R
3287         case EVENT_FT_RESPONSE:
3288                 wpa_supplicant_event_ft_response(wpa_s, data);
3289                 break;
3290 #endif /* CONFIG_IEEE80211R */
3291 #ifdef CONFIG_IBSS_RSN
3292         case EVENT_IBSS_RSN_START:
3293                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3294                 break;
3295 #endif /* CONFIG_IBSS_RSN */
3296         case EVENT_ASSOC_REJECT:
3297                 if (data->assoc_reject.bssid)
3298                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3299                                 "bssid=" MACSTR " status_code=%u",
3300                                 MAC2STR(data->assoc_reject.bssid),
3301                                 data->assoc_reject.status_code);
3302                 else
3303                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3304                                 "status_code=%u",
3305                                 data->assoc_reject.status_code);
3306                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3307                         sme_event_assoc_reject(wpa_s, data);
3308                 else {
3309                         const u8 *bssid = data->assoc_reject.bssid;
3310                         if (bssid == NULL || is_zero_ether_addr(bssid))
3311                                 bssid = wpa_s->pending_bssid;
3312                         wpas_connection_failed(wpa_s, bssid);
3313                         wpa_supplicant_mark_disassoc(wpa_s);
3314                 }
3315                 break;
3316         case EVENT_AUTH_TIMED_OUT:
3317                 /* It is possible to get this event from earlier connection */
3318                 if (wpa_s->current_ssid &&
3319                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3320                         wpa_dbg(wpa_s, MSG_DEBUG,
3321                                 "Ignore AUTH_TIMED_OUT in mesh configuration");
3322                         break;
3323                 }
3324                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3325                         sme_event_auth_timed_out(wpa_s, data);
3326                 break;
3327         case EVENT_ASSOC_TIMED_OUT:
3328                 /* It is possible to get this event from earlier connection */
3329                 if (wpa_s->current_ssid &&
3330                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3331                         wpa_dbg(wpa_s, MSG_DEBUG,
3332                                 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3333                         break;
3334                 }
3335                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3336                         sme_event_assoc_timed_out(wpa_s, data);
3337                 break;
3338         case EVENT_TX_STATUS:
3339                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3340                         " type=%d stype=%d",
3341                         MAC2STR(data->tx_status.dst),
3342                         data->tx_status.type, data->tx_status.stype);
3343 #ifdef CONFIG_AP
3344                 if (wpa_s->ap_iface == NULL) {
3345 #ifdef CONFIG_OFFCHANNEL
3346                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3347                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
3348                                 offchannel_send_action_tx_status(
3349                                         wpa_s, data->tx_status.dst,
3350                                         data->tx_status.data,
3351                                         data->tx_status.data_len,
3352                                         data->tx_status.ack ?
3353                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
3354                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
3355 #endif /* CONFIG_OFFCHANNEL */
3356                         break;
3357                 }
3358 #endif /* CONFIG_AP */
3359 #ifdef CONFIG_OFFCHANNEL
3360                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3361                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3362                 /*
3363                  * Catch TX status events for Action frames we sent via group
3364                  * interface in GO mode.
3365                  */
3366                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3367                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3368                     os_memcmp(wpa_s->parent->pending_action_dst,
3369                               data->tx_status.dst, ETH_ALEN) == 0) {
3370                         offchannel_send_action_tx_status(
3371                                 wpa_s->parent, data->tx_status.dst,
3372                                 data->tx_status.data,
3373                                 data->tx_status.data_len,
3374                                 data->tx_status.ack ?
3375                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
3376                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
3377                         break;
3378                 }
3379 #endif /* CONFIG_OFFCHANNEL */
3380 #ifdef CONFIG_AP
3381                 switch (data->tx_status.type) {
3382                 case WLAN_FC_TYPE_MGMT:
3383                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3384                                       data->tx_status.data_len,
3385                                       data->tx_status.stype,
3386                                       data->tx_status.ack);
3387                         break;
3388                 case WLAN_FC_TYPE_DATA:
3389                         ap_tx_status(wpa_s, data->tx_status.dst,
3390                                      data->tx_status.data,
3391                                      data->tx_status.data_len,
3392                                      data->tx_status.ack);
3393                         break;
3394                 }
3395 #endif /* CONFIG_AP */
3396                 break;
3397 #ifdef CONFIG_AP
3398         case EVENT_EAPOL_TX_STATUS:
3399                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3400                                    data->eapol_tx_status.data,
3401                                    data->eapol_tx_status.data_len,
3402                                    data->eapol_tx_status.ack);
3403                 break;
3404         case EVENT_DRIVER_CLIENT_POLL_OK:
3405                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
3406                 break;
3407         case EVENT_RX_FROM_UNKNOWN:
3408                 if (wpa_s->ap_iface == NULL)
3409                         break;
3410                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3411                                        data->rx_from_unknown.wds);
3412                 break;
3413         case EVENT_CH_SWITCH:
3414                 if (!data)
3415                         break;
3416                 if (!wpa_s->ap_iface) {
3417                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3418                                 "event in non-AP mode");
3419                         break;
3420                 }
3421
3422                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3423                                   data->ch_switch.ht_enabled,
3424                                   data->ch_switch.ch_offset,
3425                                   data->ch_switch.ch_width,
3426                                   data->ch_switch.cf1,
3427                                   data->ch_switch.cf2);
3428                 break;
3429 #ifdef NEED_AP_MLME
3430         case EVENT_DFS_RADAR_DETECTED:
3431                 if (data)
3432                         wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3433                 break;
3434         case EVENT_DFS_CAC_STARTED:
3435                 if (data)
3436                         wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3437                 break;
3438         case EVENT_DFS_CAC_FINISHED:
3439                 if (data)
3440                         wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3441                 break;
3442         case EVENT_DFS_CAC_ABORTED:
3443                 if (data)
3444                         wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3445                 break;
3446         case EVENT_DFS_NOP_FINISHED:
3447                 if (data)
3448                         wpas_event_dfs_cac_nop_finished(wpa_s,
3449                                                         &data->dfs_event);
3450                 break;
3451 #endif /* NEED_AP_MLME */
3452 #endif /* CONFIG_AP */
3453         case EVENT_RX_MGMT: {
3454                 u16 fc, stype;
3455                 const struct ieee80211_mgmt *mgmt;
3456
3457 #ifdef CONFIG_TESTING_OPTIONS
3458                 if (wpa_s->ext_mgmt_frame_handling) {
3459                         struct rx_mgmt *rx = &data->rx_mgmt;
3460                         size_t hex_len = 2 * rx->frame_len + 1;
3461                         char *hex = os_malloc(hex_len);
3462                         if (hex) {
3463                                 wpa_snprintf_hex(hex, hex_len,
3464                                                  rx->frame, rx->frame_len);
3465                                 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3466                                         rx->freq, rx->datarate, rx->ssi_signal,
3467                                         hex);
3468                                 os_free(hex);
3469                         }
3470                         break;
3471                 }
3472 #endif /* CONFIG_TESTING_OPTIONS */
3473
3474                 mgmt = (const struct ieee80211_mgmt *)
3475                         data->rx_mgmt.frame;
3476                 fc = le_to_host16(mgmt->frame_control);
3477                 stype = WLAN_FC_GET_STYPE(fc);
3478
3479 #ifdef CONFIG_AP
3480                 if (wpa_s->ap_iface == NULL) {
3481 #endif /* CONFIG_AP */
3482 #ifdef CONFIG_P2P
3483                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3484                             data->rx_mgmt.frame_len > 24) {
3485                                 const u8 *src = mgmt->sa;
3486                                 const u8 *ie = mgmt->u.probe_req.variable;
3487                                 size_t ie_len = data->rx_mgmt.frame_len -
3488                                         (mgmt->u.probe_req.variable -
3489                                          data->rx_mgmt.frame);
3490                                 wpas_p2p_probe_req_rx(
3491                                         wpa_s, src, mgmt->da,
3492                                         mgmt->bssid, ie, ie_len,
3493                                         data->rx_mgmt.ssi_signal);
3494                                 break;
3495                         }
3496 #endif /* CONFIG_P2P */
3497 #ifdef CONFIG_IBSS_RSN
3498                         if (wpa_s->current_ssid &&
3499                             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3500                             stype == WLAN_FC_STYPE_AUTH &&
3501                             data->rx_mgmt.frame_len >= 30) {
3502                                 wpa_supplicant_event_ibss_auth(wpa_s, data);
3503                                 break;
3504                         }
3505 #endif /* CONFIG_IBSS_RSN */
3506
3507                         if (stype == WLAN_FC_STYPE_ACTION) {
3508                                 wpas_event_rx_mgmt_action(
3509                                         wpa_s, data->rx_mgmt.frame,
3510                                         data->rx_mgmt.frame_len,
3511                                         data->rx_mgmt.freq,
3512                                         data->rx_mgmt.ssi_signal);
3513                                 break;
3514                         }
3515
3516                         if (wpa_s->ifmsh) {
3517                                 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
3518                                 break;
3519                         }
3520
3521                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3522                                 "management frame in non-AP mode");
3523                         break;
3524 #ifdef CONFIG_AP
3525                 }
3526
3527                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3528                     data->rx_mgmt.frame_len > 24) {
3529                         const u8 *ie = mgmt->u.probe_req.variable;
3530                         size_t ie_len = data->rx_mgmt.frame_len -
3531                                 (mgmt->u.probe_req.variable -
3532                                  data->rx_mgmt.frame);
3533
3534                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3535                                          mgmt->bssid, ie, ie_len,
3536                                          data->rx_mgmt.ssi_signal);
3537                 }
3538
3539                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
3540 #endif /* CONFIG_AP */
3541                 break;
3542                 }
3543         case EVENT_RX_PROBE_REQ:
3544                 if (data->rx_probe_req.sa == NULL ||
3545                     data->rx_probe_req.ie == NULL)
3546                         break;
3547 #ifdef CONFIG_AP
3548                 if (wpa_s->ap_iface) {
3549                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3550                                              data->rx_probe_req.sa,
3551                                              data->rx_probe_req.da,
3552                                              data->rx_probe_req.bssid,
3553                                              data->rx_probe_req.ie,
3554                                              data->rx_probe_req.ie_len,
3555                                              data->rx_probe_req.ssi_signal);
3556                         break;
3557                 }
3558 #endif /* CONFIG_AP */
3559                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3560                                       data->rx_probe_req.da,
3561                                       data->rx_probe_req.bssid,
3562                                       data->rx_probe_req.ie,
3563                                       data->rx_probe_req.ie_len,
3564                                       data->rx_probe_req.ssi_signal);
3565                 break;
3566         case EVENT_REMAIN_ON_CHANNEL:
3567 #ifdef CONFIG_OFFCHANNEL
3568                 offchannel_remain_on_channel_cb(
3569                         wpa_s, data->remain_on_channel.freq,
3570                         data->remain_on_channel.duration);
3571 #endif /* CONFIG_OFFCHANNEL */
3572                 wpas_p2p_remain_on_channel_cb(
3573                         wpa_s, data->remain_on_channel.freq,
3574                         data->remain_on_channel.duration);
3575                 break;
3576         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3577 #ifdef CONFIG_OFFCHANNEL
3578                 offchannel_cancel_remain_on_channel_cb(
3579                         wpa_s, data->remain_on_channel.freq);
3580 #endif /* CONFIG_OFFCHANNEL */
3581                 wpas_p2p_cancel_remain_on_channel_cb(
3582                         wpa_s, data->remain_on_channel.freq);
3583                 break;
3584         case EVENT_EAPOL_RX:
3585                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3586                                         data->eapol_rx.data,
3587                                         data->eapol_rx.data_len);
3588                 break;
3589         case EVENT_SIGNAL_CHANGE:
3590                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3591                         "above=%d signal=%d noise=%d txrate=%d",
3592                         data->signal_change.above_threshold,
3593                         data->signal_change.current_signal,
3594                         data->signal_change.current_noise,
3595                         data->signal_change.current_txrate);
3596                 wpa_bss_update_level(wpa_s->current_bss,
3597                                      data->signal_change.current_signal);
3598                 bgscan_notify_signal_change(
3599                         wpa_s, data->signal_change.above_threshold,
3600                         data->signal_change.current_signal,
3601                         data->signal_change.current_noise,
3602                         data->signal_change.current_txrate);
3603                 break;
3604         case EVENT_INTERFACE_ENABLED:
3605                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3606                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3607                         wpa_supplicant_update_mac_addr(wpa_s);
3608                         if (wpa_s->p2p_mgmt) {
3609                                 wpa_supplicant_set_state(wpa_s,
3610                                                          WPA_DISCONNECTED);
3611                                 break;
3612                         }
3613
3614 #ifdef CONFIG_AP
3615                         if (!wpa_s->ap_iface) {
3616                                 wpa_supplicant_set_state(wpa_s,
3617                                                          WPA_DISCONNECTED);
3618                                 wpa_s->scan_req = NORMAL_SCAN_REQ;
3619                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3620                         } else
3621                                 wpa_supplicant_set_state(wpa_s,
3622                                                          WPA_COMPLETED);
3623 #else /* CONFIG_AP */
3624                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3625                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3626 #endif /* CONFIG_AP */
3627                 }
3628                 break;
3629         case EVENT_INTERFACE_DISABLED:
3630                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3631 #ifdef CONFIG_P2P
3632                 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3633                     (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3634                      wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3635                         /*
3636                          * Mark interface disabled if this happens to end up not
3637                          * being removed as a separate P2P group interface.
3638                          */
3639                         wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3640                         /*
3641                          * The interface was externally disabled. Remove
3642                          * it assuming an external entity will start a
3643                          * new session if needed.
3644                          */
3645                         if (wpa_s->current_ssid &&
3646                             wpa_s->current_ssid->p2p_group)
3647                                 wpas_p2p_interface_unavailable(wpa_s);
3648                         else
3649                                 wpas_p2p_disconnect(wpa_s);
3650                         /*
3651                          * wpa_s instance may have been freed, so must not use
3652                          * it here anymore.
3653                          */
3654                         break;
3655                 }
3656                 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3657                     p2p_in_progress(wpa_s->global->p2p) > 1) {
3658                         /* This radio work will be cancelled, so clear P2P
3659                          * state as well.
3660                          */
3661                         p2p_stop_find(wpa_s->global->p2p);
3662                 }
3663 #endif /* CONFIG_P2P */
3664
3665                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3666                         /*
3667                          * Indicate disconnection to keep ctrl_iface events
3668                          * consistent.
3669                          */
3670                         wpa_supplicant_event_disassoc(
3671                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3672                 }
3673                 wpa_supplicant_mark_disassoc(wpa_s);
3674                 radio_remove_works(wpa_s, NULL, 0);
3675
3676                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3677                 break;
3678         case EVENT_CHANNEL_LIST_CHANGED:
3679                 wpa_supplicant_update_channel_list(
3680                         wpa_s, &data->channel_list_changed);
3681                 break;
3682         case EVENT_INTERFACE_UNAVAILABLE:
3683                 wpas_p2p_interface_unavailable(wpa_s);
3684                 break;
3685         case EVENT_BEST_CHANNEL:
3686                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3687                         "(%d %d %d)",
3688                         data->best_chan.freq_24, data->best_chan.freq_5,
3689                         data->best_chan.freq_overall);
3690                 wpa_s->best_24_freq = data->best_chan.freq_24;
3691                 wpa_s->best_5_freq = data->best_chan.freq_5;
3692                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3693                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3694                                               data->best_chan.freq_5,
3695                                               data->best_chan.freq_overall);
3696                 break;
3697         case EVENT_UNPROT_DEAUTH:
3698                 wpa_supplicant_event_unprot_deauth(wpa_s,
3699                                                    &data->unprot_deauth);
3700                 break;
3701         case EVENT_UNPROT_DISASSOC:
3702                 wpa_supplicant_event_unprot_disassoc(wpa_s,
3703                                                      &data->unprot_disassoc);
3704                 break;
3705         case EVENT_STATION_LOW_ACK:
3706 #ifdef CONFIG_AP
3707                 if (wpa_s->ap_iface && data)
3708                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3709                                                   data->low_ack.addr);
3710 #endif /* CONFIG_AP */
3711 #ifdef CONFIG_TDLS
3712                 if (data)
3713                         wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3714                                                           data->low_ack.addr);
3715 #endif /* CONFIG_TDLS */
3716                 break;
3717         case EVENT_IBSS_PEER_LOST:
3718 #ifdef CONFIG_IBSS_RSN
3719                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3720 #endif /* CONFIG_IBSS_RSN */
3721                 break;
3722         case EVENT_DRIVER_GTK_REKEY:
3723                 if (os_memcmp(data->driver_gtk_rekey.bssid,
3724                               wpa_s->bssid, ETH_ALEN))
3725                         break;
3726                 if (!wpa_s->wpa)
3727                         break;
3728                 wpa_sm_update_replay_ctr(wpa_s->wpa,
3729                                          data->driver_gtk_rekey.replay_ctr);
3730                 break;
3731         case EVENT_SCHED_SCAN_STOPPED:
3732                 wpa_s->pno = 0;
3733                 wpa_s->sched_scanning = 0;
3734                 resched = wpa_s->scanning;
3735                 wpa_supplicant_notify_scanning(wpa_s, 0);
3736
3737                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3738                         break;
3739
3740                 /*
3741                  * Start a new sched scan to continue searching for more SSIDs
3742                  * either if timed out or PNO schedule scan is pending.
3743                  */
3744                 if (wpa_s->sched_scan_timed_out) {
3745                         wpa_supplicant_req_sched_scan(wpa_s);
3746                 } else if (wpa_s->pno_sched_pending) {
3747                         wpa_s->pno_sched_pending = 0;
3748                         wpas_start_pno(wpa_s);
3749                 } else if (resched) {
3750                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3751                 }
3752
3753                 break;
3754         case EVENT_WPS_BUTTON_PUSHED:
3755 #ifdef CONFIG_WPS
3756                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3757 #endif /* CONFIG_WPS */
3758                 break;
3759         case EVENT_AVOID_FREQUENCIES:
3760                 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3761                 break;
3762         case EVENT_CONNECT_FAILED_REASON:
3763 #ifdef CONFIG_AP
3764                 if (!wpa_s->ap_iface || !data)
3765                         break;
3766                 hostapd_event_connect_failed_reason(
3767                         wpa_s->ap_iface->bss[0],
3768                         data->connect_failed_reason.addr,
3769                         data->connect_failed_reason.code);
3770 #endif /* CONFIG_AP */
3771                 break;
3772         case EVENT_NEW_PEER_CANDIDATE:
3773 #ifdef CONFIG_MESH
3774                 if (!wpa_s->ifmsh || !data)
3775                         break;
3776                 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3777                                      data->mesh_peer.ies,
3778                                      data->mesh_peer.ie_len);
3779 #endif /* CONFIG_MESH */
3780                 break;
3781         default:
3782                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3783                 break;
3784         }
3785 }