904b533d03a5be31e04361b7b1be0d6219c12a70
[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, int own_request);
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                 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
870                         struct wpabuf *p2p_ie;
871                         u8 dev_addr[ETH_ALEN];
872
873                         ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
874                         if (ie == NULL) {
875                                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no P2P element");
876                                 continue;
877                         }
878                         p2p_ie = wpa_bss_get_vendor_ie_multi(
879                                 bss, P2P_IE_VENDOR_TYPE);
880                         if (p2p_ie == NULL) {
881                                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - could not fetch P2P element");
882                                 continue;
883                         }
884
885                         if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
886                             || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
887                                          ETH_ALEN) != 0) {
888                                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no matching GO P2P Device Address in P2P element");
889                                 wpabuf_free(p2p_ie);
890                                 continue;
891                         }
892                         wpabuf_free(p2p_ie);
893                 }
894
895                 /*
896                  * TODO: skip the AP if its P2P IE has Group Formation
897                  * bit set in the P2P Group Capability Bitmap and we
898                  * are not in Group Formation with that device.
899                  */
900 #endif /* CONFIG_P2P */
901
902                 /* Matching configuration found */
903                 return ssid;
904         }
905
906         /* No matching configuration found */
907         return NULL;
908 }
909
910
911 static struct wpa_bss *
912 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
913                           struct wpa_ssid *group,
914                           struct wpa_ssid **selected_ssid)
915 {
916         unsigned int i;
917
918         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
919                 group->priority);
920
921         for (i = 0; i < wpa_s->last_scan_res_used; i++) {
922                 struct wpa_bss *bss = wpa_s->last_scan_res[i];
923                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
924                 if (!*selected_ssid)
925                         continue;
926                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
927                         " ssid='%s'",
928                         MAC2STR(bss->bssid),
929                         wpa_ssid_txt(bss->ssid, bss->ssid_len));
930                 return bss;
931         }
932
933         return NULL;
934 }
935
936
937 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
938                                              struct wpa_ssid **selected_ssid)
939 {
940         struct wpa_bss *selected = NULL;
941         int prio;
942
943         if (wpa_s->last_scan_res == NULL ||
944             wpa_s->last_scan_res_used == 0)
945                 return NULL; /* no scan results from last update */
946
947         while (selected == NULL) {
948                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
949                         selected = wpa_supplicant_select_bss(
950                                 wpa_s, wpa_s->conf->pssid[prio],
951                                 selected_ssid);
952                         if (selected)
953                                 break;
954                 }
955
956                 if (selected == NULL && wpa_s->blacklist &&
957                     !wpa_s->countermeasures) {
958                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
959                                 "blacklist and try again");
960                         wpa_blacklist_clear(wpa_s);
961                         wpa_s->blacklist_cleared++;
962                 } else if (selected == NULL)
963                         break;
964         }
965
966         return selected;
967 }
968
969
970 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
971                                         int timeout_sec, int timeout_usec)
972 {
973         if (!wpa_supplicant_enabled_networks(wpa_s)) {
974                 /*
975                  * No networks are enabled; short-circuit request so
976                  * we don't wait timeout seconds before transitioning
977                  * to INACTIVE state.
978                  */
979                 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
980                         "since there are no enabled networks");
981                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
982 #ifdef CONFIG_P2P
983                 wpa_s->sta_scan_pending = 0;
984 #endif /* CONFIG_P2P */
985                 return;
986         }
987
988         wpa_s->scan_for_connection = 1;
989         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
990 }
991
992
993 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
994                            struct wpa_bss *selected,
995                            struct wpa_ssid *ssid)
996 {
997         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
998                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
999                         "PBC session overlap");
1000 #ifdef CONFIG_P2P
1001                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
1002                         return -1;
1003 #endif /* CONFIG_P2P */
1004
1005 #ifdef CONFIG_WPS
1006                 wpas_wps_cancel(wpa_s);
1007 #endif /* CONFIG_WPS */
1008                 return -1;
1009         }
1010
1011         wpa_msg(wpa_s, MSG_DEBUG,
1012                 "Considering connect request: reassociate: %d  selected: "
1013                 MACSTR "  bssid: " MACSTR "  pending: " MACSTR
1014                 "  wpa_state: %s  ssid=%p  current_ssid=%p",
1015                 wpa_s->reassociate, MAC2STR(selected->bssid),
1016                 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1017                 wpa_supplicant_state_txt(wpa_s->wpa_state),
1018                 ssid, wpa_s->current_ssid);
1019
1020         /*
1021          * Do not trigger new association unless the BSSID has changed or if
1022          * reassociation is requested. If we are in process of associating with
1023          * the selected BSSID, do not trigger new attempt.
1024          */
1025         if (wpa_s->reassociate ||
1026             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1027              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1028                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
1029               (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1030                os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1031                0) ||
1032               (is_zero_ether_addr(wpa_s->pending_bssid) &&
1033                ssid != wpa_s->current_ssid)))) {
1034                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1035                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
1036                         return 0;
1037                 }
1038                 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1039                         MAC2STR(selected->bssid));
1040                 wpa_supplicant_associate(wpa_s, selected, ssid);
1041         } else {
1042                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1043                         "connect with the selected AP");
1044         }
1045
1046         return 0;
1047 }
1048
1049
1050 static struct wpa_ssid *
1051 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1052 {
1053         int prio;
1054         struct wpa_ssid *ssid;
1055
1056         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1057                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1058                 {
1059                         if (wpas_network_disabled(wpa_s, ssid))
1060                                 continue;
1061                         if (ssid->mode == IEEE80211_MODE_IBSS ||
1062                             ssid->mode == IEEE80211_MODE_AP)
1063                                 return ssid;
1064                 }
1065         }
1066         return NULL;
1067 }
1068
1069
1070 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1071  * on BSS added and BSS changed events */
1072 static void wpa_supplicant_rsn_preauth_scan_results(
1073         struct wpa_supplicant *wpa_s)
1074 {
1075         struct wpa_bss *bss;
1076
1077         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1078                 return;
1079
1080         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1081                 const u8 *ssid, *rsn;
1082
1083                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1084                 if (ssid == NULL)
1085                         continue;
1086
1087                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1088                 if (rsn == NULL)
1089                         continue;
1090
1091                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1092         }
1093
1094 }
1095
1096
1097 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1098                                        struct wpa_bss *selected,
1099                                        struct wpa_ssid *ssid)
1100 {
1101         struct wpa_bss *current_bss = NULL;
1102         int min_diff;
1103
1104         if (wpa_s->reassociate)
1105                 return 1; /* explicit request to reassociate */
1106         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1107                 return 1; /* we are not associated; continue */
1108         if (wpa_s->current_ssid == NULL)
1109                 return 1; /* unknown current SSID */
1110         if (wpa_s->current_ssid != ssid)
1111                 return 1; /* different network block */
1112
1113         if (wpas_driver_bss_selection(wpa_s))
1114                 return 0; /* Driver-based roaming */
1115
1116         if (wpa_s->current_ssid->ssid)
1117                 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1118                                           wpa_s->current_ssid->ssid,
1119                                           wpa_s->current_ssid->ssid_len);
1120         if (!current_bss)
1121                 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1122
1123         if (!current_bss)
1124                 return 1; /* current BSS not seen in scan results */
1125
1126         if (current_bss == selected)
1127                 return 0;
1128
1129         if (selected->last_update_idx > current_bss->last_update_idx)
1130                 return 1; /* current BSS not seen in the last scan */
1131
1132 #ifndef CONFIG_NO_ROAMING
1133         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1134         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1135                 MAC2STR(current_bss->bssid), current_bss->level);
1136         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1137                 MAC2STR(selected->bssid), selected->level);
1138
1139         if (wpa_s->current_ssid->bssid_set &&
1140             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1141             0) {
1142                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1143                         "has preferred BSSID");
1144                 return 1;
1145         }
1146
1147         if (current_bss->level < 0 && current_bss->level > selected->level) {
1148                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1149                         "signal level");
1150                 return 0;
1151         }
1152
1153         min_diff = 2;
1154         if (current_bss->level < 0) {
1155                 if (current_bss->level < -85)
1156                         min_diff = 1;
1157                 else if (current_bss->level < -80)
1158                         min_diff = 2;
1159                 else if (current_bss->level < -75)
1160                         min_diff = 3;
1161                 else if (current_bss->level < -70)
1162                         min_diff = 4;
1163                 else
1164                         min_diff = 5;
1165         }
1166         if (abs(current_bss->level - selected->level) < min_diff) {
1167                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1168                         "in signal level");
1169                 return 0;
1170         }
1171
1172         return 1;
1173 #else /* CONFIG_NO_ROAMING */
1174         return 0;
1175 #endif /* CONFIG_NO_ROAMING */
1176 }
1177
1178
1179 /* Return != 0 if no scan results could be fetched or if scan results should not
1180  * be shared with other virtual interfaces. */
1181 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1182                                               union wpa_event_data *data,
1183                                               int own_request)
1184 {
1185         struct wpa_scan_results *scan_res;
1186         int ap = 0;
1187 #ifndef CONFIG_NO_RANDOM_POOL
1188         size_t i, num;
1189 #endif /* CONFIG_NO_RANDOM_POOL */
1190
1191 #ifdef CONFIG_AP
1192         if (wpa_s->ap_iface)
1193                 ap = 1;
1194 #endif /* CONFIG_AP */
1195
1196         wpa_supplicant_notify_scanning(wpa_s, 0);
1197
1198 #ifdef CONFIG_P2P
1199         if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
1200             !wpa_s->global->p2p_disabled &&
1201             wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1202             !wpa_s->scan_res_handler) {
1203                 wpa_s->global->p2p_cb_on_scan_complete = 0;
1204                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1205                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1206                                 "stopped scan processing");
1207                         wpa_s->sta_scan_pending = 1;
1208                         wpa_supplicant_req_scan(wpa_s, 5, 0);
1209                         return -1;
1210                 }
1211         }
1212         wpa_s->sta_scan_pending = 0;
1213 #endif /* CONFIG_P2P */
1214
1215         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1216                                                    data ? &data->scan_info :
1217                                                    NULL, 1);
1218         if (scan_res == NULL) {
1219                 if (wpa_s->conf->ap_scan == 2 || ap ||
1220                     wpa_s->scan_res_handler == scan_only_handler)
1221                         return -1;
1222                 if (!own_request)
1223                         return -1;
1224                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1225                         "scanning again");
1226                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1227                 return -1;
1228         }
1229
1230 #ifndef CONFIG_NO_RANDOM_POOL
1231         num = scan_res->num;
1232         if (num > 10)
1233                 num = 10;
1234         for (i = 0; i < num; i++) {
1235                 u8 buf[5];
1236                 struct wpa_scan_res *res = scan_res->res[i];
1237                 buf[0] = res->bssid[5];
1238                 buf[1] = res->qual & 0xff;
1239                 buf[2] = res->noise & 0xff;
1240                 buf[3] = res->level & 0xff;
1241                 buf[4] = res->tsf & 0xff;
1242                 random_add_randomness(buf, sizeof(buf));
1243         }
1244 #endif /* CONFIG_NO_RANDOM_POOL */
1245
1246         if (own_request && wpa_s->scan_res_handler) {
1247                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1248                                          struct wpa_scan_results *scan_res);
1249
1250                 scan_res_handler = wpa_s->scan_res_handler;
1251                 wpa_s->scan_res_handler = NULL;
1252                 scan_res_handler(wpa_s, scan_res);
1253
1254                 wpa_scan_results_free(scan_res);
1255                 return -2;
1256         }
1257
1258         if (ap) {
1259                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1260 #ifdef CONFIG_AP
1261                 if (wpa_s->ap_iface->scan_cb)
1262                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1263 #endif /* CONFIG_AP */
1264                 wpa_scan_results_free(scan_res);
1265                 return 0;
1266         }
1267
1268         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1269         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1270         wpas_notify_scan_results(wpa_s);
1271
1272         wpas_notify_scan_done(wpa_s, 1);
1273
1274         if (sme_proc_obss_scan(wpa_s) > 0) {
1275                 wpa_scan_results_free(scan_res);
1276                 return 0;
1277         }
1278
1279         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1280                 wpa_scan_results_free(scan_res);
1281                 return 0;
1282         }
1283
1284         if (autoscan_notify_scan(wpa_s, scan_res)) {
1285                 wpa_scan_results_free(scan_res);
1286                 return 0;
1287         }
1288
1289         if (wpa_s->disconnected) {
1290                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1291                 wpa_scan_results_free(scan_res);
1292                 return 0;
1293         }
1294
1295         if (!wpas_driver_bss_selection(wpa_s) &&
1296             bgscan_notify_scan(wpa_s, scan_res) == 1) {
1297                 wpa_scan_results_free(scan_res);
1298                 return 0;
1299         }
1300
1301         wpas_wps_update_ap_info(wpa_s, scan_res);
1302
1303         wpa_scan_results_free(scan_res);
1304
1305         return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1306 }
1307
1308
1309 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1310                                               int new_scan, int own_request)
1311 {
1312         struct wpa_bss *selected;
1313         struct wpa_ssid *ssid = NULL;
1314
1315         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1316
1317         if (selected) {
1318                 int skip;
1319                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1320                 if (skip) {
1321                         if (new_scan)
1322                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1323                         return 0;
1324                 }
1325
1326                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1327                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1328                         return -1;
1329                 }
1330                 if (new_scan)
1331                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1332                 /*
1333                  * Do not notify other virtual radios of scan results since we do not
1334                  * want them to start other associations at the same time.
1335                  */
1336                 return 1;
1337         } else {
1338                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1339                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1340                 if (ssid) {
1341                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1342                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1343                         if (new_scan)
1344                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1345                 } else if (own_request) {
1346                         /*
1347                          * No SSID found. If SCAN results are as a result of
1348                          * own scan request and not due to a scan request on
1349                          * another shared interface, try another scan.
1350                          */
1351                         int timeout_sec = wpa_s->scan_interval;
1352                         int timeout_usec = 0;
1353 #ifdef CONFIG_P2P
1354                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1355                                 return 0;
1356
1357                         if (wpa_s->p2p_in_provisioning) {
1358                                 /*
1359                                  * Use shorter wait during P2P Provisioning
1360                                  * state to speed up group formation.
1361                                  */
1362                                 timeout_sec = 0;
1363                                 timeout_usec = 250000;
1364                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1365                                                             timeout_usec);
1366                                 return 0;
1367                         }
1368 #endif /* CONFIG_P2P */
1369 #ifdef CONFIG_INTERWORKING
1370                         if (wpa_s->conf->auto_interworking &&
1371                             wpa_s->conf->interworking &&
1372                             wpa_s->conf->cred) {
1373                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1374                                         "start ANQP fetch since no matching "
1375                                         "networks found");
1376                                 wpa_s->network_select = 1;
1377                                 wpa_s->auto_network_select = 1;
1378                                 interworking_start_fetch_anqp(wpa_s);
1379                                 return 1;
1380                         }
1381 #endif /* CONFIG_INTERWORKING */
1382                         if (wpa_supplicant_req_sched_scan(wpa_s))
1383                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1384                                                             timeout_usec);
1385                 }
1386         }
1387         return 0;
1388 }
1389
1390
1391 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1392                                               union wpa_event_data *data)
1393 {
1394         const char *rn, *rn2;
1395         struct wpa_supplicant *ifs;
1396
1397         if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
1398                 /*
1399                  * If no scan results could be fetched, then no need to
1400                  * notify those interfaces that did not actually request
1401                  * this scan. Similarly, if scan results started a new operation on this
1402                  * interface, do not notify other interfaces to avoid concurrent
1403                  * operations during a connection attempt.
1404                  */
1405                 return;
1406         }
1407
1408         /*
1409          * Check other interfaces to see if they have the same radio-name. If
1410          * so, they get updated with this same scan info.
1411          */
1412         if (!wpa_s->driver->get_radio_name)
1413                 return;
1414
1415         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1416         if (rn == NULL || rn[0] == '\0')
1417                 return;
1418
1419         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1420                 "sharing same radio (%s) in event_scan_results", rn);
1421
1422         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1423                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1424                         continue;
1425
1426                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1427                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1428                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1429                                    "sibling", ifs->ifname);
1430                         _wpa_supplicant_event_scan_results(ifs, data, 0);
1431                 }
1432         }
1433 }
1434
1435 #endif /* CONFIG_NO_SCAN_PROCESSING */
1436
1437
1438 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1439 {
1440 #ifdef CONFIG_NO_SCAN_PROCESSING
1441         return -1;
1442 #else /* CONFIG_NO_SCAN_PROCESSING */
1443         struct os_time now;
1444
1445         if (wpa_s->last_scan_res_used <= 0)
1446                 return -1;
1447
1448         os_get_time(&now);
1449         if (now.sec - wpa_s->last_scan.sec > 5) {
1450                 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1451                 return -1;
1452         }
1453
1454         return wpas_select_network_from_last_scan(wpa_s, 0, 1);
1455 #endif /* CONFIG_NO_SCAN_PROCESSING */
1456 }
1457
1458 #ifdef CONFIG_WNM
1459
1460 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1461 {
1462         struct wpa_supplicant *wpa_s = eloop_ctx;
1463
1464         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1465                 return;
1466
1467         if (!wpa_s->no_keep_alive) {
1468                 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1469                            MAC2STR(wpa_s->bssid));
1470                 /* TODO: could skip this if normal data traffic has been sent */
1471                 /* TODO: Consider using some more appropriate data frame for
1472                  * this */
1473                 if (wpa_s->l2)
1474                         l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1475                                        (u8 *) "", 0);
1476         }
1477
1478 #ifdef CONFIG_SME
1479         if (wpa_s->sme.bss_max_idle_period) {
1480                 unsigned int msec;
1481                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1482                 if (msec > 100)
1483                         msec -= 100;
1484                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1485                                        wnm_bss_keep_alive, wpa_s, NULL);
1486         }
1487 #endif /* CONFIG_SME */
1488 }
1489
1490
1491 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1492                                    const u8 *ies, size_t ies_len)
1493 {
1494         struct ieee802_11_elems elems;
1495
1496         if (ies == NULL)
1497                 return;
1498
1499         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1500                 return;
1501
1502 #ifdef CONFIG_SME
1503         if (elems.bss_max_idle_period) {
1504                 unsigned int msec;
1505                 wpa_s->sme.bss_max_idle_period =
1506                         WPA_GET_LE16(elems.bss_max_idle_period);
1507                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1508                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1509                            (elems.bss_max_idle_period[2] & 0x01) ?
1510                            " (protected keep-live required)" : "");
1511                 if (wpa_s->sme.bss_max_idle_period == 0)
1512                         wpa_s->sme.bss_max_idle_period = 1;
1513                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1514                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1515                          /* msec times 1000 */
1516                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1517                         if (msec > 100)
1518                                 msec -= 100;
1519                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1520                                                wnm_bss_keep_alive, wpa_s,
1521                                                NULL);
1522                 }
1523         }
1524 #endif /* CONFIG_SME */
1525 }
1526
1527 #endif /* CONFIG_WNM */
1528
1529
1530 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1531 {
1532 #ifdef CONFIG_WNM
1533         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1534 #endif /* CONFIG_WNM */
1535 }
1536
1537
1538 #ifdef CONFIG_INTERWORKING
1539
1540 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1541                             size_t len)
1542 {
1543         int res;
1544
1545         wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1546         res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1547         if (res) {
1548                 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1549         }
1550
1551         return res;
1552 }
1553
1554
1555 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1556                                             const u8 *ies, size_t ies_len)
1557 {
1558         struct ieee802_11_elems elems;
1559
1560         if (ies == NULL)
1561                 return;
1562
1563         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1564                 return;
1565
1566         if (elems.qos_map_set) {
1567                 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1568                                  elems.qos_map_set_len);
1569         }
1570 }
1571
1572 #endif /* CONFIG_INTERWORKING */
1573
1574
1575 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1576                                           union wpa_event_data *data)
1577 {
1578         int l, len, found = 0, wpa_found, rsn_found;
1579         const u8 *p;
1580 #ifdef CONFIG_IEEE80211R
1581         u8 bssid[ETH_ALEN];
1582 #endif /* CONFIG_IEEE80211R */
1583
1584         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1585         if (data->assoc_info.req_ies)
1586                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1587                             data->assoc_info.req_ies_len);
1588         if (data->assoc_info.resp_ies) {
1589                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1590                             data->assoc_info.resp_ies_len);
1591 #ifdef CONFIG_TDLS
1592                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1593                                         data->assoc_info.resp_ies_len);
1594 #endif /* CONFIG_TDLS */
1595 #ifdef CONFIG_WNM
1596                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1597                                        data->assoc_info.resp_ies_len);
1598 #endif /* CONFIG_WNM */
1599 #ifdef CONFIG_INTERWORKING
1600                 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1601                                                 data->assoc_info.resp_ies_len);
1602 #endif /* CONFIG_INTERWORKING */
1603         }
1604         if (data->assoc_info.beacon_ies)
1605                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1606                             data->assoc_info.beacon_ies,
1607                             data->assoc_info.beacon_ies_len);
1608         if (data->assoc_info.freq)
1609                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1610                         data->assoc_info.freq);
1611
1612         p = data->assoc_info.req_ies;
1613         l = data->assoc_info.req_ies_len;
1614
1615         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1616         while (p && l >= 2) {
1617                 len = p[1] + 2;
1618                 if (len > l) {
1619                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1620                                     p, l);
1621                         break;
1622                 }
1623                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1624                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1625                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1626                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1627                                 break;
1628                         found = 1;
1629                         wpa_find_assoc_pmkid(wpa_s);
1630                         break;
1631                 }
1632                 l -= len;
1633                 p += len;
1634         }
1635         if (!found && data->assoc_info.req_ies)
1636                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1637
1638 #ifdef CONFIG_IEEE80211R
1639 #ifdef CONFIG_SME
1640         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1641                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1642                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1643                                                  data->assoc_info.resp_ies,
1644                                                  data->assoc_info.resp_ies_len,
1645                                                  bssid) < 0) {
1646                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1647                                 "Reassociation Response failed");
1648                         wpa_supplicant_deauthenticate(
1649                                 wpa_s, WLAN_REASON_INVALID_IE);
1650                         return -1;
1651                 }
1652         }
1653
1654         p = data->assoc_info.resp_ies;
1655         l = data->assoc_info.resp_ies_len;
1656
1657 #ifdef CONFIG_WPS_STRICT
1658         if (p && wpa_s->current_ssid &&
1659             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1660                 struct wpabuf *wps;
1661                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1662                 if (wps == NULL) {
1663                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1664                                 "include WPS IE in (Re)Association Response");
1665                         return -1;
1666                 }
1667
1668                 if (wps_validate_assoc_resp(wps) < 0) {
1669                         wpabuf_free(wps);
1670                         wpa_supplicant_deauthenticate(
1671                                 wpa_s, WLAN_REASON_INVALID_IE);
1672                         return -1;
1673                 }
1674                 wpabuf_free(wps);
1675         }
1676 #endif /* CONFIG_WPS_STRICT */
1677
1678         /* Go through the IEs and make a copy of the MDIE, if present. */
1679         while (p && l >= 2) {
1680                 len = p[1] + 2;
1681                 if (len > l) {
1682                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1683                                     p, l);
1684                         break;
1685                 }
1686                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1687                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1688                         wpa_s->sme.ft_used = 1;
1689                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1690                                   MOBILITY_DOMAIN_ID_LEN);
1691                         break;
1692                 }
1693                 l -= len;
1694                 p += len;
1695         }
1696 #endif /* CONFIG_SME */
1697
1698         /* Process FT when SME is in the driver */
1699         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1700             wpa_ft_is_completed(wpa_s->wpa)) {
1701                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1702                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1703                                                  data->assoc_info.resp_ies,
1704                                                  data->assoc_info.resp_ies_len,
1705                                                  bssid) < 0) {
1706                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1707                                 "Reassociation Response failed");
1708                         wpa_supplicant_deauthenticate(
1709                                 wpa_s, WLAN_REASON_INVALID_IE);
1710                         return -1;
1711                 }
1712                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1713         }
1714
1715         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1716                              data->assoc_info.resp_ies_len);
1717 #endif /* CONFIG_IEEE80211R */
1718
1719         /* WPA/RSN IE from Beacon/ProbeResp */
1720         p = data->assoc_info.beacon_ies;
1721         l = data->assoc_info.beacon_ies_len;
1722
1723         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1724          */
1725         wpa_found = rsn_found = 0;
1726         while (p && l >= 2) {
1727                 len = p[1] + 2;
1728                 if (len > l) {
1729                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1730                                     p, l);
1731                         break;
1732                 }
1733                 if (!wpa_found &&
1734                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1735                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1736                         wpa_found = 1;
1737                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1738                 }
1739
1740                 if (!rsn_found &&
1741                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1742                         rsn_found = 1;
1743                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1744                 }
1745
1746                 l -= len;
1747                 p += len;
1748         }
1749
1750         if (!wpa_found && data->assoc_info.beacon_ies)
1751                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1752         if (!rsn_found && data->assoc_info.beacon_ies)
1753                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1754         if (wpa_found || rsn_found)
1755                 wpa_s->ap_ies_from_associnfo = 1;
1756
1757         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1758             wpa_s->assoc_freq != data->assoc_info.freq) {
1759                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1760                            "%u to %u MHz",
1761                            wpa_s->assoc_freq, data->assoc_info.freq);
1762                 wpa_supplicant_update_scan_results(wpa_s);
1763         }
1764
1765         wpa_s->assoc_freq = data->assoc_info.freq;
1766
1767         return 0;
1768 }
1769
1770
1771 static struct wpa_bss * wpa_supplicant_get_new_bss(
1772         struct wpa_supplicant *wpa_s, const u8 *bssid)
1773 {
1774         struct wpa_bss *bss = NULL;
1775         struct wpa_ssid *ssid = wpa_s->current_ssid;
1776
1777         if (ssid->ssid_len > 0)
1778                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1779         if (!bss)
1780                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1781
1782         return bss;
1783 }
1784
1785
1786 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1787 {
1788         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1789
1790         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1791                 return -1;
1792
1793         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1794                 return 0;
1795
1796         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1797                                         WPA_IE_VENDOR_TYPE);
1798         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1799
1800         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1801                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1802             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1803                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1804                 return -1;
1805
1806         return 0;
1807 }
1808
1809
1810 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1811                                        union wpa_event_data *data)
1812 {
1813         u8 bssid[ETH_ALEN];
1814         int ft_completed;
1815
1816 #ifdef CONFIG_AP
1817         if (wpa_s->ap_iface) {
1818                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1819                                     data->assoc_info.addr,
1820                                     data->assoc_info.req_ies,
1821                                     data->assoc_info.req_ies_len,
1822                                     data->assoc_info.reassoc);
1823                 return;
1824         }
1825 #endif /* CONFIG_AP */
1826
1827         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1828         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1829                 return;
1830
1831         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1832                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1833                 wpa_supplicant_deauthenticate(
1834                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1835                 return;
1836         }
1837
1838         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1839         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1840                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1841                         MACSTR, MAC2STR(bssid));
1842                 random_add_randomness(bssid, ETH_ALEN);
1843                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1844                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1845                 wpas_notify_bssid_changed(wpa_s);
1846
1847                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1848                         wpa_clear_keys(wpa_s, bssid);
1849                 }
1850                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1851                         wpa_supplicant_deauthenticate(
1852                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1853                         return;
1854                 }
1855                 if (wpa_s->current_ssid) {
1856                         struct wpa_bss *bss = NULL;
1857
1858                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1859                         if (!bss) {
1860                                 wpa_supplicant_update_scan_results(wpa_s);
1861
1862                                 /* Get the BSS from the new scan results */
1863                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1864                         }
1865
1866                         if (bss)
1867                                 wpa_s->current_bss = bss;
1868                 }
1869
1870                 if (wpa_s->conf->ap_scan == 1 &&
1871                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1872                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1873                                 wpa_msg(wpa_s, MSG_WARNING,
1874                                         "WPA/RSN IEs not updated");
1875                 }
1876         }
1877
1878 #ifdef CONFIG_SME
1879         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1880         wpa_s->sme.prev_bssid_set = 1;
1881 #endif /* CONFIG_SME */
1882
1883         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1884         if (wpa_s->current_ssid) {
1885                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1886                  * initialized before association, but for other modes,
1887                  * initialize PC/SC here, if the current configuration needs
1888                  * smartcard or SIM/USIM. */
1889                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1890         }
1891         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1892         if (wpa_s->l2)
1893                 l2_packet_notify_auth_start(wpa_s->l2);
1894
1895         /*
1896          * Set portEnabled first to FALSE in order to get EAP state machine out
1897          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1898          * state machine may transit to AUTHENTICATING state based on obsolete
1899          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1900          * AUTHENTICATED without ever giving chance to EAP state machine to
1901          * reset the state.
1902          */
1903         if (!ft_completed) {
1904                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1905                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1906         }
1907         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1908                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1909         /* 802.1X::portControl = Auto */
1910         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1911         wpa_s->eapol_received = 0;
1912         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1913             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1914             (wpa_s->current_ssid &&
1915              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1916                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1917                     (wpa_s->drv_flags &
1918                      WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1919                         /*
1920                          * Set the key after having received joined-IBSS event
1921                          * from the driver.
1922                          */
1923                         wpa_supplicant_set_wpa_none_key(wpa_s,
1924                                                         wpa_s->current_ssid);
1925                 }
1926                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1927                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1928         } else if (!ft_completed) {
1929                 /* Timeout for receiving the first EAPOL packet */
1930                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1931         }
1932         wpa_supplicant_cancel_scan(wpa_s);
1933
1934         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1935             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1936                 /*
1937                  * We are done; the driver will take care of RSN 4-way
1938                  * handshake.
1939                  */
1940                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1941                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1942                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1943                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1944         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1945                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1946                 /*
1947                  * The driver will take care of RSN 4-way handshake, so we need
1948                  * to allow EAPOL supplicant to complete its work without
1949                  * waiting for WPA supplicant.
1950                  */
1951                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1952         } else if (ft_completed) {
1953                 /*
1954                  * FT protocol completed - make sure EAPOL state machine ends
1955                  * up in authenticated.
1956                  */
1957                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1958                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1959                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1960                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1961         }
1962
1963         wpa_s->last_eapol_matches_bssid = 0;
1964
1965         if (wpa_s->pending_eapol_rx) {
1966                 struct os_time now, age;
1967                 os_get_time(&now);
1968                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1969                 if (age.sec == 0 && age.usec < 100000 &&
1970                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1971                     0) {
1972                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1973                                 "frame that was received just before "
1974                                 "association notification");
1975                         wpa_supplicant_rx_eapol(
1976                                 wpa_s, wpa_s->pending_eapol_rx_src,
1977                                 wpabuf_head(wpa_s->pending_eapol_rx),
1978                                 wpabuf_len(wpa_s->pending_eapol_rx));
1979                 }
1980                 wpabuf_free(wpa_s->pending_eapol_rx);
1981                 wpa_s->pending_eapol_rx = NULL;
1982         }
1983
1984         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1985              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1986             wpa_s->current_ssid &&
1987             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1988                 /* Set static WEP keys again */
1989                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1990         }
1991
1992 #ifdef CONFIG_IBSS_RSN
1993         if (wpa_s->current_ssid &&
1994             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1995             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1996             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1997             wpa_s->ibss_rsn == NULL) {
1998                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1999                 if (!wpa_s->ibss_rsn) {
2000                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2001                         wpa_supplicant_deauthenticate(
2002                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2003                         return;
2004                 }
2005
2006                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2007         }
2008 #endif /* CONFIG_IBSS_RSN */
2009
2010         wpas_wps_notify_assoc(wpa_s, bssid);
2011 }
2012
2013
2014 static int disconnect_reason_recoverable(u16 reason_code)
2015 {
2016         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2017                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2018                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2019 }
2020
2021
2022 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2023                                           u16 reason_code,
2024                                           int locally_generated)
2025 {
2026         const u8 *bssid;
2027
2028         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2029                 /*
2030                  * At least Host AP driver and a Prism3 card seemed to be
2031                  * generating streams of disconnected events when configuring
2032                  * IBSS for WPA-None. Ignore them for now.
2033                  */
2034                 return;
2035         }
2036
2037         bssid = wpa_s->bssid;
2038         if (is_zero_ether_addr(bssid))
2039                 bssid = wpa_s->pending_bssid;
2040
2041         if (!is_zero_ether_addr(bssid) ||
2042             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2043                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2044                         " reason=%d%s",
2045                         MAC2STR(bssid), reason_code,
2046                         locally_generated ? " locally_generated=1" : "");
2047         }
2048 }
2049
2050
2051 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2052                                  int locally_generated)
2053 {
2054         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2055             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2056                 return 0; /* Not in 4-way handshake with PSK */
2057
2058         /*
2059          * It looks like connection was lost while trying to go through PSK
2060          * 4-way handshake. Filter out known disconnection cases that are caused
2061          * by something else than PSK mismatch to avoid confusing reports.
2062          */
2063
2064         if (locally_generated) {
2065                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2066                         return 0;
2067         }
2068
2069         return 1;
2070 }
2071
2072
2073 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2074                                                  u16 reason_code,
2075                                                  int locally_generated)
2076 {
2077         const u8 *bssid;
2078         int authenticating;
2079         u8 prev_pending_bssid[ETH_ALEN];
2080         struct wpa_bss *fast_reconnect = NULL;
2081 #ifndef CONFIG_NO_SCAN_PROCESSING
2082         struct wpa_ssid *fast_reconnect_ssid = NULL;
2083 #endif /* CONFIG_NO_SCAN_PROCESSING */
2084         struct wpa_ssid *last_ssid;
2085
2086         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2087         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2088
2089         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2090                 /*
2091                  * At least Host AP driver and a Prism3 card seemed to be
2092                  * generating streams of disconnected events when configuring
2093                  * IBSS for WPA-None. Ignore them for now.
2094                  */
2095                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2096                         "IBSS/WPA-None mode");
2097                 return;
2098         }
2099
2100         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2101                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2102                         "pre-shared key may be incorrect");
2103                 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2104                         return; /* P2P group removed */
2105                 wpas_auth_failed(wpa_s);
2106         }
2107         if (!wpa_s->disconnected &&
2108             (!wpa_s->auto_reconnect_disabled ||
2109              wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
2110                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2111                         "reconnect (wps=%d wpa_state=%d)",
2112                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2113                         wpa_s->wpa_state);
2114                 if (wpa_s->wpa_state == WPA_COMPLETED &&
2115                     wpa_s->current_ssid &&
2116                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2117                     !locally_generated &&
2118                     disconnect_reason_recoverable(reason_code)) {
2119                         /*
2120                          * It looks like the AP has dropped association with
2121                          * us, but could allow us to get back in. Try to
2122                          * reconnect to the same BSS without full scan to save
2123                          * time for some common cases.
2124                          */
2125                         fast_reconnect = wpa_s->current_bss;
2126 #ifndef CONFIG_NO_SCAN_PROCESSING
2127                         fast_reconnect_ssid = wpa_s->current_ssid;
2128 #endif /* CONFIG_NO_SCAN_PROCESSING */
2129                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2130                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2131                 else
2132                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2133                                 "immediate scan");
2134         } else {
2135                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2136                         "try to re-connect");
2137                 wpa_s->reassociate = 0;
2138                 wpa_s->disconnected = 1;
2139                 wpa_supplicant_cancel_sched_scan(wpa_s);
2140         }
2141         bssid = wpa_s->bssid;
2142         if (is_zero_ether_addr(bssid))
2143                 bssid = wpa_s->pending_bssid;
2144         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2145                 wpas_connection_failed(wpa_s, bssid);
2146         wpa_sm_notify_disassoc(wpa_s->wpa);
2147         if (locally_generated)
2148                 wpa_s->disconnect_reason = -reason_code;
2149         else
2150                 wpa_s->disconnect_reason = reason_code;
2151         wpas_notify_disconnect_reason(wpa_s);
2152         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2153                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2154                 wpa_s->keys_cleared = 0;
2155                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2156         }
2157         last_ssid = wpa_s->current_ssid;
2158         wpa_supplicant_mark_disassoc(wpa_s);
2159
2160         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2161                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2162                 wpa_s->current_ssid = last_ssid;
2163         }
2164
2165         if (fast_reconnect) {
2166 #ifndef CONFIG_NO_SCAN_PROCESSING
2167                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2168                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2169                                            fast_reconnect_ssid) < 0) {
2170                         /* Recover through full scan */
2171                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2172                 }
2173 #endif /* CONFIG_NO_SCAN_PROCESSING */
2174         }
2175 }
2176
2177
2178 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2179 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2180 {
2181         struct wpa_supplicant *wpa_s = eloop_ctx;
2182
2183         if (!wpa_s->pending_mic_error_report)
2184                 return;
2185
2186         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2187         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2188         wpa_s->pending_mic_error_report = 0;
2189 }
2190 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2191
2192
2193 static void
2194 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2195                                          union wpa_event_data *data)
2196 {
2197         int pairwise;
2198         struct os_time t;
2199
2200         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2201         pairwise = (data && data->michael_mic_failure.unicast);
2202         os_get_time(&t);
2203         if ((wpa_s->last_michael_mic_error &&
2204              t.sec - wpa_s->last_michael_mic_error <= 60) ||
2205             wpa_s->pending_mic_error_report) {
2206                 if (wpa_s->pending_mic_error_report) {
2207                         /*
2208                          * Send the pending MIC error report immediately since
2209                          * we are going to start countermeasures and AP better
2210                          * do the same.
2211                          */
2212                         wpa_sm_key_request(wpa_s->wpa, 1,
2213                                            wpa_s->pending_mic_error_pairwise);
2214                 }
2215
2216                 /* Send the new MIC error report immediately since we are going
2217                  * to start countermeasures and AP better do the same.
2218                  */
2219                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2220
2221                 /* initialize countermeasures */
2222                 wpa_s->countermeasures = 1;
2223
2224                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2225
2226                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2227
2228                 /*
2229                  * Need to wait for completion of request frame. We do not get
2230                  * any callback for the message completion, so just wait a
2231                  * short while and hope for the best. */
2232                 os_sleep(0, 10000);
2233
2234                 wpa_drv_set_countermeasures(wpa_s, 1);
2235                 wpa_supplicant_deauthenticate(wpa_s,
2236                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2237                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2238                                      wpa_s, NULL);
2239                 eloop_register_timeout(60, 0,
2240                                        wpa_supplicant_stop_countermeasures,
2241                                        wpa_s, NULL);
2242                 /* TODO: mark the AP rejected for 60 second. STA is
2243                  * allowed to associate with another AP.. */
2244         } else {
2245 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2246                 if (wpa_s->mic_errors_seen) {
2247                         /*
2248                          * Reduce the effectiveness of Michael MIC error
2249                          * reports as a means for attacking against TKIP if
2250                          * more than one MIC failure is noticed with the same
2251                          * PTK. We delay the transmission of the reports by a
2252                          * random time between 0 and 60 seconds in order to
2253                          * force the attacker wait 60 seconds before getting
2254                          * the information on whether a frame resulted in a MIC
2255                          * failure.
2256                          */
2257                         u8 rval[4];
2258                         int sec;
2259
2260                         if (os_get_random(rval, sizeof(rval)) < 0)
2261                                 sec = os_random() % 60;
2262                         else
2263                                 sec = WPA_GET_BE32(rval) % 60;
2264                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2265                                 "report %d seconds", sec);
2266                         wpa_s->pending_mic_error_report = 1;
2267                         wpa_s->pending_mic_error_pairwise = pairwise;
2268                         eloop_cancel_timeout(
2269                                 wpa_supplicant_delayed_mic_error_report,
2270                                 wpa_s, NULL);
2271                         eloop_register_timeout(
2272                                 sec, os_random() % 1000000,
2273                                 wpa_supplicant_delayed_mic_error_report,
2274                                 wpa_s, NULL);
2275                 } else {
2276                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2277                 }
2278 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2279                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2280 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2281         }
2282         wpa_s->last_michael_mic_error = t.sec;
2283         wpa_s->mic_errors_seen++;
2284 }
2285
2286
2287 #ifdef CONFIG_TERMINATE_ONLASTIF
2288 static int any_interfaces(struct wpa_supplicant *head)
2289 {
2290         struct wpa_supplicant *wpa_s;
2291
2292         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2293                 if (!wpa_s->interface_removed)
2294                         return 1;
2295         return 0;
2296 }
2297 #endif /* CONFIG_TERMINATE_ONLASTIF */
2298
2299
2300 static void
2301 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2302                                       union wpa_event_data *data)
2303 {
2304         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2305                 return;
2306
2307         switch (data->interface_status.ievent) {
2308         case EVENT_INTERFACE_ADDED:
2309                 if (!wpa_s->interface_removed)
2310                         break;
2311                 wpa_s->interface_removed = 0;
2312                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2313                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2314                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2315                                 "driver after interface was added");
2316                 }
2317                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2318                 break;
2319         case EVENT_INTERFACE_REMOVED:
2320                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2321                 wpa_s->interface_removed = 1;
2322                 wpa_supplicant_mark_disassoc(wpa_s);
2323                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2324                 l2_packet_deinit(wpa_s->l2);
2325                 wpa_s->l2 = NULL;
2326 #ifdef CONFIG_IBSS_RSN
2327                 ibss_rsn_deinit(wpa_s->ibss_rsn);
2328                 wpa_s->ibss_rsn = NULL;
2329 #endif /* CONFIG_IBSS_RSN */
2330 #ifdef CONFIG_TERMINATE_ONLASTIF
2331                 /* check if last interface */
2332                 if (!any_interfaces(wpa_s->global->ifaces))
2333                         eloop_terminate();
2334 #endif /* CONFIG_TERMINATE_ONLASTIF */
2335                 break;
2336         }
2337 }
2338
2339
2340 #ifdef CONFIG_PEERKEY
2341 static void
2342 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2343                               union wpa_event_data *data)
2344 {
2345         if (data == NULL)
2346                 return;
2347         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2348 }
2349 #endif /* CONFIG_PEERKEY */
2350
2351
2352 #ifdef CONFIG_TDLS
2353 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2354                                       union wpa_event_data *data)
2355 {
2356         if (data == NULL)
2357                 return;
2358         switch (data->tdls.oper) {
2359         case TDLS_REQUEST_SETUP:
2360                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2361                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2362                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2363                 else
2364                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2365                 break;
2366         case TDLS_REQUEST_TEARDOWN:
2367                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2368                         wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2369                                                data->tdls.reason_code);
2370                 else
2371                         wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2372                                           data->tdls.peer);
2373                 break;
2374         }
2375 }
2376 #endif /* CONFIG_TDLS */
2377
2378
2379 #ifdef CONFIG_WNM
2380 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2381                                      union wpa_event_data *data)
2382 {
2383         if (data == NULL)
2384                 return;
2385         switch (data->wnm.oper) {
2386         case WNM_OPER_SLEEP:
2387                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2388                            "(action=%d, intval=%d)",
2389                            data->wnm.sleep_action, data->wnm.sleep_intval);
2390                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2391                                              data->wnm.sleep_intval, NULL);
2392                 break;
2393         }
2394 }
2395 #endif /* CONFIG_WNM */
2396
2397
2398 #ifdef CONFIG_IEEE80211R
2399 static void
2400 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2401                                  union wpa_event_data *data)
2402 {
2403         if (data == NULL)
2404                 return;
2405
2406         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2407                                     data->ft_ies.ies_len,
2408                                     data->ft_ies.ft_action,
2409                                     data->ft_ies.target_ap,
2410                                     data->ft_ies.ric_ies,
2411                                     data->ft_ies.ric_ies_len) < 0) {
2412                 /* TODO: prevent MLME/driver from trying to associate? */
2413         }
2414 }
2415 #endif /* CONFIG_IEEE80211R */
2416
2417
2418 #ifdef CONFIG_IBSS_RSN
2419 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2420                                                 union wpa_event_data *data)
2421 {
2422         struct wpa_ssid *ssid;
2423         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2424                 return;
2425         if (data == NULL)
2426                 return;
2427         ssid = wpa_s->current_ssid;
2428         if (ssid == NULL)
2429                 return;
2430         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2431                 return;
2432
2433         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2434 }
2435
2436
2437 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2438                                            union wpa_event_data *data)
2439 {
2440         struct wpa_ssid *ssid = wpa_s->current_ssid;
2441
2442         if (ssid == NULL)
2443                 return;
2444
2445         /* check if the ssid is correctly configured as IBSS/RSN */
2446         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2447                 return;
2448
2449         ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2450                              data->rx_mgmt.frame_len);
2451 }
2452 #endif /* CONFIG_IBSS_RSN */
2453
2454
2455 #ifdef CONFIG_IEEE80211R
2456 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2457                          size_t len)
2458 {
2459         const u8 *sta_addr, *target_ap_addr;
2460         u16 status;
2461
2462         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2463         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2464                 return; /* only SME case supported for now */
2465         if (len < 1 + 2 * ETH_ALEN + 2)
2466                 return;
2467         if (data[0] != 2)
2468                 return; /* Only FT Action Response is supported for now */
2469         sta_addr = data + 1;
2470         target_ap_addr = data + 1 + ETH_ALEN;
2471         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2472         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2473                 MACSTR " TargetAP " MACSTR " status %u",
2474                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2475
2476         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2477                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2478                         " in FT Action Response", MAC2STR(sta_addr));
2479                 return;
2480         }
2481
2482         if (status) {
2483                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2484                         "failure (status code %d)", status);
2485                 /* TODO: report error to FT code(?) */
2486                 return;
2487         }
2488
2489         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2490                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2491                                     target_ap_addr, NULL, 0) < 0)
2492                 return;
2493
2494 #ifdef CONFIG_SME
2495         {
2496                 struct wpa_bss *bss;
2497                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2498                 if (bss)
2499                         wpa_s->sme.freq = bss->freq;
2500                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2501                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2502                               WLAN_AUTH_FT);
2503         }
2504 #endif /* CONFIG_SME */
2505 }
2506 #endif /* CONFIG_IEEE80211R */
2507
2508
2509 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2510                                                struct unprot_deauth *e)
2511 {
2512 #ifdef CONFIG_IEEE80211W
2513         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2514                    "dropped: " MACSTR " -> " MACSTR
2515                    " (reason code %u)",
2516                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2517         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2518 #endif /* CONFIG_IEEE80211W */
2519 }
2520
2521
2522 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2523                                                  struct unprot_disassoc *e)
2524 {
2525 #ifdef CONFIG_IEEE80211W
2526         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2527                    "dropped: " MACSTR " -> " MACSTR
2528                    " (reason code %u)",
2529                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2530         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2531 #endif /* CONFIG_IEEE80211W */
2532 }
2533
2534
2535 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2536                                   u16 reason_code, int locally_generated,
2537                                   const u8 *ie, size_t ie_len, int deauth)
2538 {
2539 #ifdef CONFIG_AP
2540         if (wpa_s->ap_iface && addr) {
2541                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2542                 return;
2543         }
2544
2545         if (wpa_s->ap_iface) {
2546                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2547                 return;
2548         }
2549 #endif /* CONFIG_AP */
2550
2551         wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2552
2553         if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2554             ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2555               (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2556              eapol_sm_failed(wpa_s->eapol)))
2557                 wpas_auth_failed(wpa_s);
2558
2559 #ifdef CONFIG_P2P
2560         if (deauth && reason_code > 0) {
2561                 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2562                                           locally_generated) > 0) {
2563                         /*
2564                          * The interface was removed, so cannot continue
2565                          * processing any additional operations after this.
2566                          */
2567                         return;
2568                 }
2569         }
2570 #endif /* CONFIG_P2P */
2571
2572         wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2573                                              locally_generated);
2574 }
2575
2576
2577 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2578                                 struct disassoc_info *info)
2579 {
2580         u16 reason_code = 0;
2581         int locally_generated = 0;
2582         const u8 *addr = NULL;
2583         const u8 *ie = NULL;
2584         size_t ie_len = 0;
2585
2586         wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2587
2588         if (info) {
2589                 addr = info->addr;
2590                 ie = info->ie;
2591                 ie_len = info->ie_len;
2592                 reason_code = info->reason_code;
2593                 locally_generated = info->locally_generated;
2594                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2595                         locally_generated ? " (locally generated)" : "");
2596                 if (addr)
2597                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2598                                 MAC2STR(addr));
2599                 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2600                             ie, ie_len);
2601         }
2602
2603 #ifdef CONFIG_AP
2604         if (wpa_s->ap_iface && info && info->addr) {
2605                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2606                 return;
2607         }
2608
2609         if (wpa_s->ap_iface) {
2610                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2611                 return;
2612         }
2613 #endif /* CONFIG_AP */
2614
2615 #ifdef CONFIG_P2P
2616         if (info) {
2617                 wpas_p2p_disassoc_notif(
2618                         wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2619                         locally_generated);
2620         }
2621 #endif /* CONFIG_P2P */
2622
2623         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2624                 sme_event_disassoc(wpa_s, info);
2625
2626         wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2627                               ie, ie_len, 0);
2628 }
2629
2630
2631 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2632                               struct deauth_info *info)
2633 {
2634         u16 reason_code = 0;
2635         int locally_generated = 0;
2636         const u8 *addr = NULL;
2637         const u8 *ie = NULL;
2638         size_t ie_len = 0;
2639
2640         wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2641
2642         if (info) {
2643                 addr = info->addr;
2644                 ie = info->ie;
2645                 ie_len = info->ie_len;
2646                 reason_code = info->reason_code;
2647                 locally_generated = info->locally_generated;
2648                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2649                         reason_code,
2650                         locally_generated ? " (locally generated)" : "");
2651                 if (addr) {
2652                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2653                                 MAC2STR(addr));
2654                 }
2655                 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2656                             ie, ie_len);
2657         }
2658
2659         wpa_reset_ft_completed(wpa_s->wpa);
2660
2661         wpas_event_disconnect(wpa_s, addr, reason_code,
2662                               locally_generated, ie, ie_len, 1);
2663 }
2664
2665
2666 static void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s)
2667 {
2668         const char *rn, *rn2;
2669         struct wpa_supplicant *ifs;
2670
2671         if (wpa_s->drv_priv == NULL)
2672                 return; /* Ignore event during drv initialization */
2673
2674         free_hw_features(wpa_s);
2675         wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2676                 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2677
2678 #ifdef CONFIG_P2P
2679         wpas_p2p_update_channel_list(wpa_s);
2680 #endif /* CONFIG_P2P */
2681
2682         /*
2683          * Check other interfaces to see if they have the same radio-name. If
2684          * so, they get updated with this same hw mode info.
2685          */
2686         if (!wpa_s->driver->get_radio_name)
2687                 return;
2688
2689         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
2690         if (rn == NULL || rn[0] == '\0')
2691                 return;
2692
2693         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
2694                 "sharing same radio (%s) in event_channel_list_change", rn);
2695
2696         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2697                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
2698                         continue;
2699
2700                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
2701                 if (rn2 && os_strcmp(rn, rn2) == 0) {
2702                         wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2703                                    ifs->ifname);
2704                         free_hw_features(ifs);
2705                         ifs->hw.modes = wpa_drv_get_hw_feature_data(
2706                                 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2707                 }
2708         }
2709 }
2710
2711
2712 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2713                           union wpa_event_data *data)
2714 {
2715         struct wpa_supplicant *wpa_s = ctx;
2716
2717         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2718             event != EVENT_INTERFACE_ENABLED &&
2719             event != EVENT_INTERFACE_STATUS &&
2720             event != EVENT_SCHED_SCAN_STOPPED) {
2721                 wpa_dbg(wpa_s, MSG_DEBUG,
2722                         "Ignore event %s (%d) while interface is disabled",
2723                         event_to_string(event), event);
2724                 return;
2725         }
2726
2727 #ifndef CONFIG_NO_STDOUT_DEBUG
2728 {
2729         int level = MSG_DEBUG;
2730
2731         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2732                 const struct ieee80211_hdr *hdr;
2733                 u16 fc;
2734                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2735                 fc = le_to_host16(hdr->frame_control);
2736                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2737                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2738                         level = MSG_EXCESSIVE;
2739         }
2740
2741         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2742                 event_to_string(event), event);
2743 }
2744 #endif /* CONFIG_NO_STDOUT_DEBUG */
2745
2746         switch (event) {
2747         case EVENT_AUTH:
2748                 sme_event_auth(wpa_s, data);
2749                 break;
2750         case EVENT_ASSOC:
2751                 wpa_supplicant_event_assoc(wpa_s, data);
2752                 break;
2753         case EVENT_DISASSOC:
2754                 wpas_event_disassoc(wpa_s,
2755                                     data ? &data->disassoc_info : NULL);
2756                 break;
2757         case EVENT_DEAUTH:
2758                 wpas_event_deauth(wpa_s,
2759                                   data ? &data->deauth_info : NULL);
2760                 break;
2761         case EVENT_MICHAEL_MIC_FAILURE:
2762                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2763                 break;
2764 #ifndef CONFIG_NO_SCAN_PROCESSING
2765         case EVENT_SCAN_RESULTS:
2766                 wpa_supplicant_event_scan_results(wpa_s, data);
2767                 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2768                     wpa_s->wpa_state != WPA_ASSOCIATING)
2769                         wpas_p2p_continue_after_scan(wpa_s);
2770                 break;
2771 #endif /* CONFIG_NO_SCAN_PROCESSING */
2772         case EVENT_ASSOCINFO:
2773                 wpa_supplicant_event_associnfo(wpa_s, data);
2774                 break;
2775         case EVENT_INTERFACE_STATUS:
2776                 wpa_supplicant_event_interface_status(wpa_s, data);
2777                 break;
2778         case EVENT_PMKID_CANDIDATE:
2779                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2780                 break;
2781 #ifdef CONFIG_PEERKEY
2782         case EVENT_STKSTART:
2783                 wpa_supplicant_event_stkstart(wpa_s, data);
2784                 break;
2785 #endif /* CONFIG_PEERKEY */
2786 #ifdef CONFIG_TDLS
2787         case EVENT_TDLS:
2788                 wpa_supplicant_event_tdls(wpa_s, data);
2789                 break;
2790 #endif /* CONFIG_TDLS */
2791 #ifdef CONFIG_WNM
2792         case EVENT_WNM:
2793                 wpa_supplicant_event_wnm(wpa_s, data);
2794                 break;
2795 #endif /* CONFIG_WNM */
2796 #ifdef CONFIG_IEEE80211R
2797         case EVENT_FT_RESPONSE:
2798                 wpa_supplicant_event_ft_response(wpa_s, data);
2799                 break;
2800 #endif /* CONFIG_IEEE80211R */
2801 #ifdef CONFIG_IBSS_RSN
2802         case EVENT_IBSS_RSN_START:
2803                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2804                 break;
2805 #endif /* CONFIG_IBSS_RSN */
2806         case EVENT_ASSOC_REJECT:
2807                 if (data->assoc_reject.bssid)
2808                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2809                                 "bssid=" MACSTR " status_code=%u",
2810                                 MAC2STR(data->assoc_reject.bssid),
2811                                 data->assoc_reject.status_code);
2812                 else
2813                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2814                                 "status_code=%u",
2815                                 data->assoc_reject.status_code);
2816                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2817                         sme_event_assoc_reject(wpa_s, data);
2818                 else {
2819                         const u8 *bssid = data->assoc_reject.bssid;
2820                         if (bssid == NULL || is_zero_ether_addr(bssid))
2821                                 bssid = wpa_s->pending_bssid;
2822                         wpas_connection_failed(wpa_s, bssid);
2823                         wpa_supplicant_mark_disassoc(wpa_s);
2824                 }
2825                 break;
2826         case EVENT_AUTH_TIMED_OUT:
2827                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2828                         sme_event_auth_timed_out(wpa_s, data);
2829                 break;
2830         case EVENT_ASSOC_TIMED_OUT:
2831                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2832                         sme_event_assoc_timed_out(wpa_s, data);
2833                 break;
2834         case EVENT_TX_STATUS:
2835                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2836                         " type=%d stype=%d",
2837                         MAC2STR(data->tx_status.dst),
2838                         data->tx_status.type, data->tx_status.stype);
2839 #ifdef CONFIG_AP
2840                 if (wpa_s->ap_iface == NULL) {
2841 #ifdef CONFIG_OFFCHANNEL
2842                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2843                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2844                                 offchannel_send_action_tx_status(
2845                                         wpa_s, data->tx_status.dst,
2846                                         data->tx_status.data,
2847                                         data->tx_status.data_len,
2848                                         data->tx_status.ack ?
2849                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2850                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2851 #endif /* CONFIG_OFFCHANNEL */
2852                         break;
2853                 }
2854 #endif /* CONFIG_AP */
2855 #ifdef CONFIG_OFFCHANNEL
2856                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2857                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2858                 /*
2859                  * Catch TX status events for Action frames we sent via group
2860                  * interface in GO mode.
2861                  */
2862                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2863                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2864                     os_memcmp(wpa_s->parent->pending_action_dst,
2865                               data->tx_status.dst, ETH_ALEN) == 0) {
2866                         offchannel_send_action_tx_status(
2867                                 wpa_s->parent, data->tx_status.dst,
2868                                 data->tx_status.data,
2869                                 data->tx_status.data_len,
2870                                 data->tx_status.ack ?
2871                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2872                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2873                         break;
2874                 }
2875 #endif /* CONFIG_OFFCHANNEL */
2876 #ifdef CONFIG_AP
2877                 switch (data->tx_status.type) {
2878                 case WLAN_FC_TYPE_MGMT:
2879                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2880                                       data->tx_status.data_len,
2881                                       data->tx_status.stype,
2882                                       data->tx_status.ack);
2883                         break;
2884                 case WLAN_FC_TYPE_DATA:
2885                         ap_tx_status(wpa_s, data->tx_status.dst,
2886                                      data->tx_status.data,
2887                                      data->tx_status.data_len,
2888                                      data->tx_status.ack);
2889                         break;
2890                 }
2891 #endif /* CONFIG_AP */
2892                 break;
2893 #ifdef CONFIG_AP
2894         case EVENT_EAPOL_TX_STATUS:
2895                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2896                                    data->eapol_tx_status.data,
2897                                    data->eapol_tx_status.data_len,
2898                                    data->eapol_tx_status.ack);
2899                 break;
2900         case EVENT_DRIVER_CLIENT_POLL_OK:
2901                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2902                 break;
2903         case EVENT_RX_FROM_UNKNOWN:
2904                 if (wpa_s->ap_iface == NULL)
2905                         break;
2906                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2907                                        data->rx_from_unknown.wds);
2908                 break;
2909         case EVENT_CH_SWITCH:
2910                 if (!data)
2911                         break;
2912                 if (!wpa_s->ap_iface) {
2913                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2914                                 "event in non-AP mode");
2915                         break;
2916                 }
2917
2918                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2919                                   data->ch_switch.ht_enabled,
2920                                   data->ch_switch.ch_offset);
2921                 break;
2922 #endif /* CONFIG_AP */
2923 #if defined(CONFIG_AP) || defined(CONFIG_IBSS_RSN)
2924         case EVENT_RX_MGMT: {
2925                 u16 fc, stype;
2926                 const struct ieee80211_mgmt *mgmt;
2927
2928                 mgmt = (const struct ieee80211_mgmt *)
2929                         data->rx_mgmt.frame;
2930                 fc = le_to_host16(mgmt->frame_control);
2931                 stype = WLAN_FC_GET_STYPE(fc);
2932
2933 #ifdef CONFIG_AP
2934                 if (wpa_s->ap_iface == NULL) {
2935 #endif /* CONFIG_AP */
2936 #ifdef CONFIG_P2P
2937                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2938                             data->rx_mgmt.frame_len > 24) {
2939                                 const u8 *src = mgmt->sa;
2940                                 const u8 *ie = mgmt->u.probe_req.variable;
2941                                 size_t ie_len = data->rx_mgmt.frame_len -
2942                                         (mgmt->u.probe_req.variable -
2943                                          data->rx_mgmt.frame);
2944                                 wpas_p2p_probe_req_rx(
2945                                         wpa_s, src, mgmt->da,
2946                                         mgmt->bssid, ie, ie_len,
2947                                         data->rx_mgmt.ssi_signal);
2948                                 break;
2949                         }
2950 #endif /* CONFIG_P2P */
2951 #ifdef CONFIG_IBSS_RSN
2952                         if (stype == WLAN_FC_STYPE_AUTH &&
2953                             data->rx_mgmt.frame_len >= 30) {
2954                                 wpa_supplicant_event_ibss_auth(wpa_s, data);
2955                                 break;
2956                         }
2957 #endif /* CONFIG_IBSS_RSN */
2958                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2959                                 "management frame in non-AP mode");
2960                         break;
2961 #ifdef CONFIG_AP
2962                 }
2963
2964                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2965                     data->rx_mgmt.frame_len > 24) {
2966                         const u8 *ie = mgmt->u.probe_req.variable;
2967                         size_t ie_len = data->rx_mgmt.frame_len -
2968                                 (mgmt->u.probe_req.variable -
2969                                  data->rx_mgmt.frame);
2970
2971                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2972                                          mgmt->bssid, ie, ie_len,
2973                                          data->rx_mgmt.ssi_signal);
2974                 }
2975
2976                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2977 #endif /* CONFIG_AP */
2978                 break;
2979                 }
2980 #endif /* CONFIG_AP || CONFIG_IBSS_RSN */
2981         case EVENT_RX_ACTION:
2982                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2983                         " Category=%u DataLen=%d freq=%d MHz",
2984                         MAC2STR(data->rx_action.sa),
2985                         data->rx_action.category, (int) data->rx_action.len,
2986                         data->rx_action.freq);
2987 #ifdef CONFIG_IEEE80211R
2988                 if (data->rx_action.category == WLAN_ACTION_FT) {
2989                         ft_rx_action(wpa_s, data->rx_action.data,
2990                                      data->rx_action.len);
2991                         break;
2992                 }
2993 #endif /* CONFIG_IEEE80211R */
2994 #ifdef CONFIG_IEEE80211W
2995 #ifdef CONFIG_SME
2996                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2997                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2998                                         data->rx_action.data,
2999                                         data->rx_action.len);
3000                         break;
3001                 }
3002 #endif /* CONFIG_SME */
3003 #endif /* CONFIG_IEEE80211W */
3004 #ifdef CONFIG_WNM
3005                 if (data->rx_action.category == WLAN_ACTION_WNM) {
3006                         ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
3007                         break;
3008                 }
3009 #endif /* CONFIG_WNM */
3010 #ifdef CONFIG_GAS
3011                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
3012                     gas_query_rx(wpa_s->gas, data->rx_action.da,
3013                                  data->rx_action.sa, data->rx_action.bssid,
3014                                  data->rx_action.data, data->rx_action.len,
3015                                  data->rx_action.freq) == 0)
3016                         break;
3017 #endif /* CONFIG_GAS */
3018 #ifdef CONFIG_TDLS
3019                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
3020                     data->rx_action.len >= 4 &&
3021                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3022                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
3023                                 "Response from " MACSTR,
3024                                 MAC2STR(data->rx_action.sa));
3025                         break;
3026                 }
3027 #endif /* CONFIG_TDLS */
3028 #ifdef CONFIG_INTERWORKING
3029                 if (data->rx_action.category == WLAN_ACTION_QOS &&
3030                     data->rx_action.len >= 1 &&
3031                     data->rx_action.data[0] == QOS_QOS_MAP_CONFIG) {
3032                         wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3033                                 MACSTR, MAC2STR(data->rx_action.sa));
3034                         if (os_memcmp(data->rx_action.sa, wpa_s->bssid, ETH_ALEN)
3035                             == 0)
3036                                 wpas_qos_map_set(wpa_s, data->rx_action.data + 1,
3037                                                  data->rx_action.len - 1);
3038                         break;
3039                 }
3040 #endif /* CONFIG_INTERWORKING */
3041 #ifdef CONFIG_P2P
3042                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
3043                                    data->rx_action.sa,
3044                                    data->rx_action.bssid,
3045                                    data->rx_action.category,
3046                                    data->rx_action.data,
3047                                    data->rx_action.len, data->rx_action.freq);
3048 #endif /* CONFIG_P2P */
3049                 break;
3050         case EVENT_RX_PROBE_REQ:
3051                 if (data->rx_probe_req.sa == NULL ||
3052                     data->rx_probe_req.ie == NULL)
3053                         break;
3054 #ifdef CONFIG_AP
3055                 if (wpa_s->ap_iface) {
3056                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3057                                              data->rx_probe_req.sa,
3058                                              data->rx_probe_req.da,
3059                                              data->rx_probe_req.bssid,
3060                                              data->rx_probe_req.ie,
3061                                              data->rx_probe_req.ie_len,
3062                                              data->rx_probe_req.ssi_signal);
3063                         break;
3064                 }
3065 #endif /* CONFIG_AP */
3066 #ifdef CONFIG_P2P
3067                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3068                                       data->rx_probe_req.da,
3069                                       data->rx_probe_req.bssid,
3070                                       data->rx_probe_req.ie,
3071                                       data->rx_probe_req.ie_len,
3072                                       data->rx_probe_req.ssi_signal);
3073 #endif /* CONFIG_P2P */
3074                 break;
3075         case EVENT_REMAIN_ON_CHANNEL:
3076 #ifdef CONFIG_OFFCHANNEL
3077                 offchannel_remain_on_channel_cb(
3078                         wpa_s, data->remain_on_channel.freq,
3079                         data->remain_on_channel.duration);
3080 #endif /* CONFIG_OFFCHANNEL */
3081 #ifdef CONFIG_P2P
3082                 wpas_p2p_remain_on_channel_cb(
3083                         wpa_s, data->remain_on_channel.freq,
3084                         data->remain_on_channel.duration);
3085 #endif /* CONFIG_P2P */
3086                 break;
3087         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3088 #ifdef CONFIG_OFFCHANNEL
3089                 offchannel_cancel_remain_on_channel_cb(
3090                         wpa_s, data->remain_on_channel.freq);
3091 #endif /* CONFIG_OFFCHANNEL */
3092 #ifdef CONFIG_P2P
3093                 wpas_p2p_cancel_remain_on_channel_cb(
3094                         wpa_s, data->remain_on_channel.freq);
3095 #endif /* CONFIG_P2P */
3096                 break;
3097 #ifdef CONFIG_P2P
3098         case EVENT_P2P_DEV_FOUND: {
3099                 struct p2p_peer_info peer_info;
3100
3101                 os_memset(&peer_info, 0, sizeof(peer_info));
3102                 if (data->p2p_dev_found.dev_addr)
3103                         os_memcpy(peer_info.p2p_device_addr,
3104                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
3105                 if (data->p2p_dev_found.pri_dev_type)
3106                         os_memcpy(peer_info.pri_dev_type,
3107                                   data->p2p_dev_found.pri_dev_type,
3108                                   sizeof(peer_info.pri_dev_type));
3109                 if (data->p2p_dev_found.dev_name)
3110                         os_strlcpy(peer_info.device_name,
3111                                    data->p2p_dev_found.dev_name,
3112                                    sizeof(peer_info.device_name));
3113                 peer_info.config_methods = data->p2p_dev_found.config_methods;
3114                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
3115                 peer_info.group_capab = data->p2p_dev_found.group_capab;
3116
3117                 /*
3118                  * FIX: new_device=1 is not necessarily correct. We should
3119                  * maintain a P2P peer database in wpa_supplicant and update
3120                  * this information based on whether the peer is truly new.
3121                  */
3122                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
3123                 break;
3124                 }
3125         case EVENT_P2P_GO_NEG_REQ_RX:
3126                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
3127                                    data->p2p_go_neg_req_rx.dev_passwd_id);
3128                 break;
3129         case EVENT_P2P_GO_NEG_COMPLETED:
3130                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
3131                 break;
3132         case EVENT_P2P_PROV_DISC_REQUEST:
3133                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
3134                                    data->p2p_prov_disc_req.config_methods,
3135                                    data->p2p_prov_disc_req.dev_addr,
3136                                    data->p2p_prov_disc_req.pri_dev_type,
3137                                    data->p2p_prov_disc_req.dev_name,
3138                                    data->p2p_prov_disc_req.supp_config_methods,
3139                                    data->p2p_prov_disc_req.dev_capab,
3140                                    data->p2p_prov_disc_req.group_capab,
3141                                    NULL, 0);
3142                 break;
3143         case EVENT_P2P_PROV_DISC_RESPONSE:
3144                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
3145                                     data->p2p_prov_disc_resp.config_methods);
3146                 break;
3147         case EVENT_P2P_SD_REQUEST:
3148                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
3149                                 data->p2p_sd_req.sa,
3150                                 data->p2p_sd_req.dialog_token,
3151                                 data->p2p_sd_req.update_indic,
3152                                 data->p2p_sd_req.tlvs,
3153                                 data->p2p_sd_req.tlvs_len);
3154                 break;
3155         case EVENT_P2P_SD_RESPONSE:
3156                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
3157                                  data->p2p_sd_resp.update_indic,
3158                                  data->p2p_sd_resp.tlvs,
3159                                  data->p2p_sd_resp.tlvs_len);
3160                 break;
3161 #endif /* CONFIG_P2P */
3162         case EVENT_EAPOL_RX:
3163                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3164                                         data->eapol_rx.data,
3165                                         data->eapol_rx.data_len);
3166                 break;
3167         case EVENT_SIGNAL_CHANGE:
3168                 bgscan_notify_signal_change(
3169                         wpa_s, data->signal_change.above_threshold,
3170                         data->signal_change.current_signal,
3171                         data->signal_change.current_noise,
3172                         data->signal_change.current_txrate);
3173                 break;
3174         case EVENT_INTERFACE_ENABLED:
3175                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3176                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3177                         wpa_supplicant_update_mac_addr(wpa_s);
3178 #ifdef CONFIG_AP
3179                         if (!wpa_s->ap_iface) {
3180                                 wpa_supplicant_set_state(wpa_s,
3181                                                          WPA_DISCONNECTED);
3182                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3183                         } else
3184                                 wpa_supplicant_set_state(wpa_s,
3185                                                          WPA_COMPLETED);
3186 #else /* CONFIG_AP */
3187                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3188                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3189 #endif /* CONFIG_AP */
3190                 }
3191                 break;
3192         case EVENT_INTERFACE_DISABLED:
3193                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3194                 wpa_supplicant_mark_disassoc(wpa_s);
3195                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3196                 break;
3197         case EVENT_CHANNEL_LIST_CHANGED:
3198                 wpa_supplicant_update_channel_list(wpa_s);
3199                 break;
3200         case EVENT_INTERFACE_UNAVAILABLE:
3201 #ifdef CONFIG_P2P
3202                 wpas_p2p_interface_unavailable(wpa_s);
3203 #endif /* CONFIG_P2P */
3204                 break;
3205         case EVENT_BEST_CHANNEL:
3206                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3207                         "(%d %d %d)",
3208                         data->best_chan.freq_24, data->best_chan.freq_5,
3209                         data->best_chan.freq_overall);
3210                 wpa_s->best_24_freq = data->best_chan.freq_24;
3211                 wpa_s->best_5_freq = data->best_chan.freq_5;
3212                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3213 #ifdef CONFIG_P2P
3214                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3215                                               data->best_chan.freq_5,
3216                                               data->best_chan.freq_overall);
3217 #endif /* CONFIG_P2P */
3218                 break;
3219         case EVENT_UNPROT_DEAUTH:
3220                 wpa_supplicant_event_unprot_deauth(wpa_s,
3221                                                    &data->unprot_deauth);
3222                 break;
3223         case EVENT_UNPROT_DISASSOC:
3224                 wpa_supplicant_event_unprot_disassoc(wpa_s,
3225                                                      &data->unprot_disassoc);
3226                 break;
3227         case EVENT_STATION_LOW_ACK:
3228 #ifdef CONFIG_AP
3229                 if (wpa_s->ap_iface && data)
3230                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3231                                                   data->low_ack.addr);
3232 #endif /* CONFIG_AP */
3233 #ifdef CONFIG_TDLS
3234                 if (data)
3235                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3236 #endif /* CONFIG_TDLS */
3237                 break;
3238         case EVENT_IBSS_PEER_LOST:
3239 #ifdef CONFIG_IBSS_RSN
3240                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3241 #endif /* CONFIG_IBSS_RSN */
3242                 break;
3243         case EVENT_DRIVER_GTK_REKEY:
3244                 if (os_memcmp(data->driver_gtk_rekey.bssid,
3245                               wpa_s->bssid, ETH_ALEN))
3246                         break;
3247                 if (!wpa_s->wpa)
3248                         break;
3249                 wpa_sm_update_replay_ctr(wpa_s->wpa,
3250                                          data->driver_gtk_rekey.replay_ctr);
3251                 break;
3252         case EVENT_SCHED_SCAN_STOPPED:
3253                 wpa_s->sched_scanning = 0;
3254                 wpa_supplicant_notify_scanning(wpa_s, 0);
3255
3256                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3257                         break;
3258
3259                 /*
3260                  * If we timed out, start a new sched scan to continue
3261                  * searching for more SSIDs.
3262                  */
3263                 if (wpa_s->sched_scan_timed_out)
3264                         wpa_supplicant_req_sched_scan(wpa_s);
3265                 break;
3266         case EVENT_WPS_BUTTON_PUSHED:
3267 #ifdef CONFIG_WPS
3268                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3269 #endif /* CONFIG_WPS */
3270                 break;
3271         case EVENT_CONNECT_FAILED_REASON:
3272 #ifdef CONFIG_AP
3273                 if (!wpa_s->ap_iface || !data)
3274                         break;
3275                 hostapd_event_connect_failed_reason(
3276                         wpa_s->ap_iface->bss[0],
3277                         data->connect_failed_reason.addr,
3278                         data->connect_failed_reason.code);
3279 #endif /* CONFIG_AP */
3280                 break;
3281         default:
3282                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3283                 break;
3284         }
3285 }