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