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