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