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