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