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