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