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