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