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