WPS: Reconnect for a failed data connection when STA_AUTOCONNECT is 0
[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              wpas_wps_reenable_networks_pending(wpa_s))) {
2381                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2382                         "reconnect (wps=%d/%d wpa_state=%d)",
2383                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2384                         wpas_wps_searching(wpa_s),
2385                         wpa_s->wpa_state);
2386                 if (wpa_s->wpa_state == WPA_COMPLETED &&
2387                     wpa_s->current_ssid &&
2388                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2389                     !locally_generated &&
2390                     disconnect_reason_recoverable(reason_code)) {
2391                         /*
2392                          * It looks like the AP has dropped association with
2393                          * us, but could allow us to get back in. Try to
2394                          * reconnect to the same BSS without full scan to save
2395                          * time for some common cases.
2396                          */
2397                         fast_reconnect = wpa_s->current_bss;
2398                         fast_reconnect_ssid = wpa_s->current_ssid;
2399                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2400                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2401                 else
2402                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2403                                 "immediate scan");
2404         } else {
2405                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2406                         "try to re-connect");
2407                 wpa_s->reassociate = 0;
2408                 wpa_s->disconnected = 1;
2409                 if (!wpa_s->pno)
2410                         wpa_supplicant_cancel_sched_scan(wpa_s);
2411         }
2412         bssid = wpa_s->bssid;
2413         if (is_zero_ether_addr(bssid))
2414                 bssid = wpa_s->pending_bssid;
2415         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2416                 wpas_connection_failed(wpa_s, bssid);
2417         wpa_sm_notify_disassoc(wpa_s->wpa);
2418         if (locally_generated)
2419                 wpa_s->disconnect_reason = -reason_code;
2420         else
2421                 wpa_s->disconnect_reason = reason_code;
2422         wpas_notify_disconnect_reason(wpa_s);
2423         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2424                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2425                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2426         }
2427         last_ssid = wpa_s->current_ssid;
2428         wpa_supplicant_mark_disassoc(wpa_s);
2429
2430         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2431                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2432                 wpa_s->current_ssid = last_ssid;
2433         }
2434
2435         if (fast_reconnect &&
2436             !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2437             !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2438             !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2439                              fast_reconnect->ssid_len) &&
2440             !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
2441 #ifndef CONFIG_NO_SCAN_PROCESSING
2442                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2443                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2444                                            fast_reconnect_ssid) < 0) {
2445                         /* Recover through full scan */
2446                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2447                 }
2448 #endif /* CONFIG_NO_SCAN_PROCESSING */
2449         } else if (fast_reconnect) {
2450                 /*
2451                  * Could not reconnect to the same BSS due to network being
2452                  * disabled. Use a new scan to match the alternative behavior
2453                  * above, i.e., to continue automatic reconnection attempt in a
2454                  * way that enforces disabled network rules.
2455                  */
2456                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2457         }
2458 }
2459
2460
2461 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2462 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2463 {
2464         struct wpa_supplicant *wpa_s = eloop_ctx;
2465
2466         if (!wpa_s->pending_mic_error_report)
2467                 return;
2468
2469         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2470         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2471         wpa_s->pending_mic_error_report = 0;
2472 }
2473 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2474
2475
2476 static void
2477 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2478                                          union wpa_event_data *data)
2479 {
2480         int pairwise;
2481         struct os_reltime t;
2482
2483         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2484         pairwise = (data && data->michael_mic_failure.unicast);
2485         os_get_reltime(&t);
2486         if ((wpa_s->last_michael_mic_error.sec &&
2487              !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
2488             wpa_s->pending_mic_error_report) {
2489                 if (wpa_s->pending_mic_error_report) {
2490                         /*
2491                          * Send the pending MIC error report immediately since
2492                          * we are going to start countermeasures and AP better
2493                          * do the same.
2494                          */
2495                         wpa_sm_key_request(wpa_s->wpa, 1,
2496                                            wpa_s->pending_mic_error_pairwise);
2497                 }
2498
2499                 /* Send the new MIC error report immediately since we are going
2500                  * to start countermeasures and AP better do the same.
2501                  */
2502                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2503
2504                 /* initialize countermeasures */
2505                 wpa_s->countermeasures = 1;
2506
2507                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2508
2509                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2510
2511                 /*
2512                  * Need to wait for completion of request frame. We do not get
2513                  * any callback for the message completion, so just wait a
2514                  * short while and hope for the best. */
2515                 os_sleep(0, 10000);
2516
2517                 wpa_drv_set_countermeasures(wpa_s, 1);
2518                 wpa_supplicant_deauthenticate(wpa_s,
2519                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2520                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2521                                      wpa_s, NULL);
2522                 eloop_register_timeout(60, 0,
2523                                        wpa_supplicant_stop_countermeasures,
2524                                        wpa_s, NULL);
2525                 /* TODO: mark the AP rejected for 60 second. STA is
2526                  * allowed to associate with another AP.. */
2527         } else {
2528 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2529                 if (wpa_s->mic_errors_seen) {
2530                         /*
2531                          * Reduce the effectiveness of Michael MIC error
2532                          * reports as a means for attacking against TKIP if
2533                          * more than one MIC failure is noticed with the same
2534                          * PTK. We delay the transmission of the reports by a
2535                          * random time between 0 and 60 seconds in order to
2536                          * force the attacker wait 60 seconds before getting
2537                          * the information on whether a frame resulted in a MIC
2538                          * failure.
2539                          */
2540                         u8 rval[4];
2541                         int sec;
2542
2543                         if (os_get_random(rval, sizeof(rval)) < 0)
2544                                 sec = os_random() % 60;
2545                         else
2546                                 sec = WPA_GET_BE32(rval) % 60;
2547                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2548                                 "report %d seconds", sec);
2549                         wpa_s->pending_mic_error_report = 1;
2550                         wpa_s->pending_mic_error_pairwise = pairwise;
2551                         eloop_cancel_timeout(
2552                                 wpa_supplicant_delayed_mic_error_report,
2553                                 wpa_s, NULL);
2554                         eloop_register_timeout(
2555                                 sec, os_random() % 1000000,
2556                                 wpa_supplicant_delayed_mic_error_report,
2557                                 wpa_s, NULL);
2558                 } else {
2559                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2560                 }
2561 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2562                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2563 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2564         }
2565         wpa_s->last_michael_mic_error = t;
2566         wpa_s->mic_errors_seen++;
2567 }
2568
2569
2570 #ifdef CONFIG_TERMINATE_ONLASTIF
2571 static int any_interfaces(struct wpa_supplicant *head)
2572 {
2573         struct wpa_supplicant *wpa_s;
2574
2575         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2576                 if (!wpa_s->interface_removed)
2577                         return 1;
2578         return 0;
2579 }
2580 #endif /* CONFIG_TERMINATE_ONLASTIF */
2581
2582
2583 static void
2584 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2585                                       union wpa_event_data *data)
2586 {
2587         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2588                 return;
2589
2590         switch (data->interface_status.ievent) {
2591         case EVENT_INTERFACE_ADDED:
2592                 if (!wpa_s->interface_removed)
2593                         break;
2594                 wpa_s->interface_removed = 0;
2595                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2596                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2597                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2598                                 "driver after interface was added");
2599                 }
2600
2601 #ifdef CONFIG_P2P
2602                 if (!wpa_s->global->p2p &&
2603                     !wpa_s->global->p2p_disabled &&
2604                     !wpa_s->conf->p2p_disabled &&
2605                     (wpa_s->drv_flags &
2606                      WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2607                     wpas_p2p_add_p2pdev_interface(
2608                             wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2609                         wpa_printf(MSG_INFO,
2610                                    "P2P: Failed to enable P2P Device interface");
2611                         /* Try to continue without. P2P will be disabled. */
2612                 }
2613 #endif /* CONFIG_P2P */
2614
2615                 break;
2616         case EVENT_INTERFACE_REMOVED:
2617                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2618                 wpa_s->interface_removed = 1;
2619                 wpa_supplicant_mark_disassoc(wpa_s);
2620                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2621                 l2_packet_deinit(wpa_s->l2);
2622                 wpa_s->l2 = NULL;
2623
2624 #ifdef CONFIG_P2P
2625                 if (wpa_s->global->p2p &&
2626                     wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2627                     (wpa_s->drv_flags &
2628                      WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2629                         wpa_dbg(wpa_s, MSG_DEBUG,
2630                                 "Removing P2P Device interface");
2631                         wpa_supplicant_remove_iface(
2632                                 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2633                                 0);
2634                         wpa_s->global->p2p_init_wpa_s = NULL;
2635                 }
2636 #endif /* CONFIG_P2P */
2637
2638 #ifdef CONFIG_TERMINATE_ONLASTIF
2639                 /* check if last interface */
2640                 if (!any_interfaces(wpa_s->global->ifaces))
2641                         eloop_terminate();
2642 #endif /* CONFIG_TERMINATE_ONLASTIF */
2643                 break;
2644         }
2645 }
2646
2647
2648 #ifdef CONFIG_PEERKEY
2649 static void
2650 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2651                               union wpa_event_data *data)
2652 {
2653         if (data == NULL)
2654                 return;
2655         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2656 }
2657 #endif /* CONFIG_PEERKEY */
2658
2659
2660 #ifdef CONFIG_TDLS
2661 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2662                                       union wpa_event_data *data)
2663 {
2664         if (data == NULL)
2665                 return;
2666         switch (data->tdls.oper) {
2667         case TDLS_REQUEST_SETUP:
2668                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2669                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2670                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2671                 else
2672                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2673                 break;
2674         case TDLS_REQUEST_TEARDOWN:
2675                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2676                         wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2677                                                data->tdls.reason_code);
2678                 else
2679                         wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2680                                           data->tdls.peer);
2681                 break;
2682         case TDLS_REQUEST_DISCOVER:
2683                         wpa_tdls_send_discovery_request(wpa_s->wpa,
2684                                                         data->tdls.peer);
2685                 break;
2686         }
2687 }
2688 #endif /* CONFIG_TDLS */
2689
2690
2691 #ifdef CONFIG_WNM
2692 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2693                                      union wpa_event_data *data)
2694 {
2695         if (data == NULL)
2696                 return;
2697         switch (data->wnm.oper) {
2698         case WNM_OPER_SLEEP:
2699                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2700                            "(action=%d, intval=%d)",
2701                            data->wnm.sleep_action, data->wnm.sleep_intval);
2702                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2703                                              data->wnm.sleep_intval, NULL);
2704                 break;
2705         }
2706 }
2707 #endif /* CONFIG_WNM */
2708
2709
2710 #ifdef CONFIG_IEEE80211R
2711 static void
2712 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2713                                  union wpa_event_data *data)
2714 {
2715         if (data == NULL)
2716                 return;
2717
2718         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2719                                     data->ft_ies.ies_len,
2720                                     data->ft_ies.ft_action,
2721                                     data->ft_ies.target_ap,
2722                                     data->ft_ies.ric_ies,
2723                                     data->ft_ies.ric_ies_len) < 0) {
2724                 /* TODO: prevent MLME/driver from trying to associate? */
2725         }
2726 }
2727 #endif /* CONFIG_IEEE80211R */
2728
2729
2730 #ifdef CONFIG_IBSS_RSN
2731 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2732                                                 union wpa_event_data *data)
2733 {
2734         struct wpa_ssid *ssid;
2735         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2736                 return;
2737         if (data == NULL)
2738                 return;
2739         ssid = wpa_s->current_ssid;
2740         if (ssid == NULL)
2741                 return;
2742         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2743                 return;
2744
2745         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2746 }
2747
2748
2749 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2750                                            union wpa_event_data *data)
2751 {
2752         struct wpa_ssid *ssid = wpa_s->current_ssid;
2753
2754         if (ssid == NULL)
2755                 return;
2756
2757         /* check if the ssid is correctly configured as IBSS/RSN */
2758         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2759                 return;
2760
2761         ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2762                              data->rx_mgmt.frame_len);
2763 }
2764 #endif /* CONFIG_IBSS_RSN */
2765
2766
2767 #ifdef CONFIG_IEEE80211R
2768 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2769                          size_t len)
2770 {
2771         const u8 *sta_addr, *target_ap_addr;
2772         u16 status;
2773
2774         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2775         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2776                 return; /* only SME case supported for now */
2777         if (len < 1 + 2 * ETH_ALEN + 2)
2778                 return;
2779         if (data[0] != 2)
2780                 return; /* Only FT Action Response is supported for now */
2781         sta_addr = data + 1;
2782         target_ap_addr = data + 1 + ETH_ALEN;
2783         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2784         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2785                 MACSTR " TargetAP " MACSTR " status %u",
2786                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2787
2788         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2789                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2790                         " in FT Action Response", MAC2STR(sta_addr));
2791                 return;
2792         }
2793
2794         if (status) {
2795                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2796                         "failure (status code %d)", status);
2797                 /* TODO: report error to FT code(?) */
2798                 return;
2799         }
2800
2801         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2802                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2803                                     target_ap_addr, NULL, 0) < 0)
2804                 return;
2805
2806 #ifdef CONFIG_SME
2807         {
2808                 struct wpa_bss *bss;
2809                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2810                 if (bss)
2811                         wpa_s->sme.freq = bss->freq;
2812                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2813                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2814                               WLAN_AUTH_FT);
2815         }
2816 #endif /* CONFIG_SME */
2817 }
2818 #endif /* CONFIG_IEEE80211R */
2819
2820
2821 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2822                                                struct unprot_deauth *e)
2823 {
2824 #ifdef CONFIG_IEEE80211W
2825         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2826                    "dropped: " MACSTR " -> " MACSTR
2827                    " (reason code %u)",
2828                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2829         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2830 #endif /* CONFIG_IEEE80211W */
2831 }
2832
2833
2834 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2835                                                  struct unprot_disassoc *e)
2836 {
2837 #ifdef CONFIG_IEEE80211W
2838         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2839                    "dropped: " MACSTR " -> " MACSTR
2840                    " (reason code %u)",
2841                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2842         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2843 #endif /* CONFIG_IEEE80211W */
2844 }
2845
2846
2847 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2848                                   u16 reason_code, int locally_generated,
2849                                   const u8 *ie, size_t ie_len, int deauth)
2850 {
2851 #ifdef CONFIG_AP
2852         if (wpa_s->ap_iface && addr) {
2853                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2854                 return;
2855         }
2856
2857         if (wpa_s->ap_iface) {
2858                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2859                 return;
2860         }
2861 #endif /* CONFIG_AP */
2862
2863         if (!locally_generated)
2864                 wpa_s->own_disconnect_req = 0;
2865
2866         wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2867
2868         if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2869               ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2870                 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2871                eapol_sm_failed(wpa_s->eapol))) &&
2872              !wpa_s->eap_expected_failure))
2873                 wpas_auth_failed(wpa_s, "AUTH_FAILED");
2874
2875 #ifdef CONFIG_P2P
2876         if (deauth && reason_code > 0) {
2877                 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2878                                           locally_generated) > 0) {
2879                         /*
2880                          * The interface was removed, so cannot continue
2881                          * processing any additional operations after this.
2882                          */
2883                         return;
2884                 }
2885         }
2886 #endif /* CONFIG_P2P */
2887
2888         wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2889                                              locally_generated);
2890 }
2891
2892
2893 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2894                                 struct disassoc_info *info)
2895 {
2896         u16 reason_code = 0;
2897         int locally_generated = 0;
2898         const u8 *addr = NULL;
2899         const u8 *ie = NULL;
2900         size_t ie_len = 0;
2901
2902         wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2903
2904         if (info) {
2905                 addr = info->addr;
2906                 ie = info->ie;
2907                 ie_len = info->ie_len;
2908                 reason_code = info->reason_code;
2909                 locally_generated = info->locally_generated;
2910                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2911                         locally_generated ? " (locally generated)" : "");
2912                 if (addr)
2913                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2914                                 MAC2STR(addr));
2915                 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2916                             ie, ie_len);
2917         }
2918
2919 #ifdef CONFIG_AP
2920         if (wpa_s->ap_iface && info && info->addr) {
2921                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2922                 return;
2923         }
2924
2925         if (wpa_s->ap_iface) {
2926                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2927                 return;
2928         }
2929 #endif /* CONFIG_AP */
2930
2931 #ifdef CONFIG_P2P
2932         if (info) {
2933                 wpas_p2p_disassoc_notif(
2934                         wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2935                         locally_generated);
2936         }
2937 #endif /* CONFIG_P2P */
2938
2939         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2940                 sme_event_disassoc(wpa_s, info);
2941
2942         wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2943                               ie, ie_len, 0);
2944 }
2945
2946
2947 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2948                               struct deauth_info *info)
2949 {
2950         u16 reason_code = 0;
2951         int locally_generated = 0;
2952         const u8 *addr = NULL;
2953         const u8 *ie = NULL;
2954         size_t ie_len = 0;
2955
2956         wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2957
2958         if (info) {
2959                 addr = info->addr;
2960                 ie = info->ie;
2961                 ie_len = info->ie_len;
2962                 reason_code = info->reason_code;
2963                 locally_generated = info->locally_generated;
2964                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2965                         reason_code,
2966                         locally_generated ? " (locally generated)" : "");
2967                 if (addr) {
2968                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2969                                 MAC2STR(addr));
2970                 }
2971                 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2972                             ie, ie_len);
2973         }
2974
2975         wpa_reset_ft_completed(wpa_s->wpa);
2976
2977         wpas_event_disconnect(wpa_s, addr, reason_code,
2978                               locally_generated, ie, ie_len, 1);
2979 }
2980
2981
2982 static const char * reg_init_str(enum reg_change_initiator init)
2983 {
2984         switch (init) {
2985         case REGDOM_SET_BY_CORE:
2986                 return "CORE";
2987         case REGDOM_SET_BY_USER:
2988                 return "USER";
2989         case REGDOM_SET_BY_DRIVER:
2990                 return "DRIVER";
2991         case REGDOM_SET_BY_COUNTRY_IE:
2992                 return "COUNTRY_IE";
2993         case REGDOM_BEACON_HINT:
2994                 return "BEACON_HINT";
2995         }
2996         return "?";
2997 }
2998
2999
3000 static const char * reg_type_str(enum reg_type type)
3001 {
3002         switch (type) {
3003         case REGDOM_TYPE_UNKNOWN:
3004                 return "UNKNOWN";
3005         case REGDOM_TYPE_COUNTRY:
3006                 return "COUNTRY";
3007         case REGDOM_TYPE_WORLD:
3008                 return "WORLD";
3009         case REGDOM_TYPE_CUSTOM_WORLD:
3010                 return "CUSTOM_WORLD";
3011         case REGDOM_TYPE_INTERSECTION:
3012                 return "INTERSECTION";
3013         }
3014         return "?";
3015 }
3016
3017
3018 static void wpa_supplicant_update_channel_list(
3019         struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
3020 {
3021         struct wpa_supplicant *ifs;
3022
3023         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
3024                 reg_init_str(info->initiator), reg_type_str(info->type),
3025                 info->alpha2[0] ? " alpha2=" : "",
3026                 info->alpha2[0] ? info->alpha2 : "");
3027
3028         if (wpa_s->drv_priv == NULL)
3029                 return; /* Ignore event during drv initialization */
3030
3031         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3032                          radio_list) {
3033                 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3034                            ifs->ifname);
3035                 free_hw_features(ifs);
3036                 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3037                         ifs, &ifs->hw.num_modes, &ifs->hw.flags);
3038         }
3039
3040         /* Restart sched_scan with updated channel list */
3041         if (wpa_s->sched_scanning) {
3042                 wpa_dbg(wpa_s, MSG_DEBUG,
3043                         "Channel list changed restart sched scan.");
3044                 wpa_supplicant_cancel_sched_scan(wpa_s);
3045                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3046         }
3047
3048         wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
3049 }
3050
3051
3052 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
3053                                       const u8 *frame, size_t len, int freq,
3054                                       int rssi)
3055 {
3056         const struct ieee80211_mgmt *mgmt;
3057         const u8 *payload;
3058         size_t plen;
3059         u8 category;
3060
3061         if (len < IEEE80211_HDRLEN + 2)
3062                 return;
3063
3064         mgmt = (const struct ieee80211_mgmt *) frame;
3065         payload = frame + IEEE80211_HDRLEN;
3066         category = *payload++;
3067         plen = len - IEEE80211_HDRLEN - 1;
3068
3069         wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3070                 " Category=%u DataLen=%d freq=%d MHz",
3071                 MAC2STR(mgmt->sa), category, (int) plen, freq);
3072
3073         if (category == WLAN_ACTION_WMM) {
3074                 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3075                 return;
3076         }
3077
3078 #ifdef CONFIG_IEEE80211R
3079         if (category == WLAN_ACTION_FT) {
3080                 ft_rx_action(wpa_s, payload, plen);
3081                 return;
3082         }
3083 #endif /* CONFIG_IEEE80211R */
3084
3085 #ifdef CONFIG_IEEE80211W
3086 #ifdef CONFIG_SME
3087         if (category == WLAN_ACTION_SA_QUERY) {
3088                 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3089                 return;
3090         }
3091 #endif /* CONFIG_SME */
3092 #endif /* CONFIG_IEEE80211W */
3093
3094 #ifdef CONFIG_WNM
3095         if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3096                 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3097                 return;
3098         }
3099 #endif /* CONFIG_WNM */
3100
3101 #ifdef CONFIG_GAS
3102         if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3103              mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3104             gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
3105                          mgmt->u.action.category,
3106                          payload, plen, freq) == 0)
3107                 return;
3108 #endif /* CONFIG_GAS */
3109
3110 #ifdef CONFIG_TDLS
3111         if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3112             payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3113                 wpa_dbg(wpa_s, MSG_DEBUG,
3114                         "TDLS: Received Discovery Response from " MACSTR,
3115                         MAC2STR(mgmt->sa));
3116                 return;
3117         }
3118 #endif /* CONFIG_TDLS */
3119
3120 #ifdef CONFIG_INTERWORKING
3121         if (category == WLAN_ACTION_QOS && plen >= 1 &&
3122             payload[0] == QOS_QOS_MAP_CONFIG) {
3123                 const u8 *pos = payload + 1;
3124                 size_t qlen = plen - 1;
3125                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3126                         MACSTR, MAC2STR(mgmt->sa));
3127                 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3128                     qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3129                     pos[1] <= qlen - 2 && pos[1] >= 16)
3130                         wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3131                 return;
3132         }
3133 #endif /* CONFIG_INTERWORKING */
3134
3135         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3136             payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3137                 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3138                 return;
3139         }
3140
3141         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3142             payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3143                 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3144                                                          payload + 1, plen - 1,
3145                                                          rssi);
3146                 return;
3147         }
3148
3149 #ifdef CONFIG_FST
3150         if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3151                 fst_rx_action(wpa_s->fst, mgmt, len);
3152                 return;
3153         }
3154 #endif /* CONFIG_FST */
3155
3156         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3157                            category, payload, plen, freq);
3158         if (wpa_s->ifmsh)
3159                 mesh_mpm_action_rx(wpa_s, mgmt, len);
3160 }
3161
3162
3163 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3164                                              union wpa_event_data *event)
3165 {
3166         struct wpa_freq_range_list *list;
3167         char *str = NULL;
3168
3169         list = &event->freq_range;
3170
3171         if (list->num)
3172                 str = freq_range_list_str(list);
3173         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3174                 str ? str : "");
3175
3176 #ifdef CONFIG_P2P
3177         if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3178                 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3179                         __func__);
3180         } else {
3181                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
3182
3183                 /*
3184                  * The update channel flow will also take care of moving a GO
3185                  * from the unsafe frequency if needed.
3186                  */
3187                 wpas_p2p_update_channel_list(wpa_s,
3188                                              WPAS_P2P_CHANNEL_UPDATE_AVOID);
3189         }
3190 #endif /* CONFIG_P2P */
3191
3192         os_free(str);
3193 }
3194
3195
3196 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3197                                             union wpa_event_data *data)
3198 {
3199         wpa_dbg(wpa_s, MSG_DEBUG,
3200                 "Connection authorized by device, previous state %d",
3201                 wpa_s->wpa_state);
3202         if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3203                 wpa_supplicant_cancel_auth_timeout(wpa_s);
3204                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3205                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3206                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3207         }
3208         wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3209         wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
3210                                data->assoc_info.ptk_kck_len,
3211                                data->assoc_info.ptk_kek,
3212                                data->assoc_info.ptk_kek_len);
3213 }
3214
3215
3216 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3217                           union wpa_event_data *data)
3218 {
3219         struct wpa_supplicant *wpa_s = ctx;
3220         int resched;
3221
3222         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3223             event != EVENT_INTERFACE_ENABLED &&
3224             event != EVENT_INTERFACE_STATUS &&
3225             event != EVENT_SCHED_SCAN_STOPPED) {
3226                 wpa_dbg(wpa_s, MSG_DEBUG,
3227                         "Ignore event %s (%d) while interface is disabled",
3228                         event_to_string(event), event);
3229                 return;
3230         }
3231
3232 #ifndef CONFIG_NO_STDOUT_DEBUG
3233 {
3234         int level = MSG_DEBUG;
3235
3236         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
3237                 const struct ieee80211_hdr *hdr;
3238                 u16 fc;
3239                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3240                 fc = le_to_host16(hdr->frame_control);
3241                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3242                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3243                         level = MSG_EXCESSIVE;
3244         }
3245
3246         wpa_dbg(wpa_s, level, "Event %s (%d) received",
3247                 event_to_string(event), event);
3248 }
3249 #endif /* CONFIG_NO_STDOUT_DEBUG */
3250
3251         switch (event) {
3252         case EVENT_AUTH:
3253                 sme_event_auth(wpa_s, data);
3254                 break;
3255         case EVENT_ASSOC:
3256                 wpa_supplicant_event_assoc(wpa_s, data);
3257                 if (data && data->assoc_info.authorized)
3258                         wpa_supplicant_event_assoc_auth(wpa_s, data);
3259                 break;
3260         case EVENT_DISASSOC:
3261                 wpas_event_disassoc(wpa_s,
3262                                     data ? &data->disassoc_info : NULL);
3263                 break;
3264         case EVENT_DEAUTH:
3265                 wpas_event_deauth(wpa_s,
3266                                   data ? &data->deauth_info : NULL);
3267                 break;
3268         case EVENT_MICHAEL_MIC_FAILURE:
3269                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3270                 break;
3271 #ifndef CONFIG_NO_SCAN_PROCESSING
3272         case EVENT_SCAN_STARTED:
3273                 if (wpa_s->own_scan_requested ||
3274                     (data && !data->scan_info.external_scan)) {
3275                         struct os_reltime diff;
3276
3277                         os_get_reltime(&wpa_s->scan_start_time);
3278                         os_reltime_sub(&wpa_s->scan_start_time,
3279                                        &wpa_s->scan_trigger_time, &diff);
3280                         wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3281                                 diff.sec, diff.usec);
3282                         wpa_s->own_scan_requested = 0;
3283                         wpa_s->own_scan_running = 1;
3284                         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3285                             wpa_s->manual_scan_use_id) {
3286                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
3287                                              WPA_EVENT_SCAN_STARTED "id=%u",
3288                                              wpa_s->manual_scan_id);
3289                         } else {
3290                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
3291                                              WPA_EVENT_SCAN_STARTED);
3292                         }
3293                 } else {
3294                         wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3295                         wpa_s->radio->external_scan_running = 1;
3296                         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
3297                 }
3298                 break;
3299         case EVENT_SCAN_RESULTS:
3300                 if (!(data && data->scan_info.external_scan) &&
3301                     os_reltime_initialized(&wpa_s->scan_start_time)) {
3302                         struct os_reltime now, diff;
3303                         os_get_reltime(&now);
3304                         os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3305                         wpa_s->scan_start_time.sec = 0;
3306                         wpa_s->scan_start_time.usec = 0;
3307                         wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3308                                 diff.sec, diff.usec);
3309                 }
3310                 if (wpa_supplicant_event_scan_results(wpa_s, data))
3311                         break; /* interface may have been removed */
3312                 if (!(data && data->scan_info.external_scan))
3313                         wpa_s->own_scan_running = 0;
3314                 if (data && data->scan_info.nl_scan_event)
3315                         wpa_s->radio->external_scan_running = 0;
3316                 radio_work_check_next(wpa_s);
3317                 break;
3318 #endif /* CONFIG_NO_SCAN_PROCESSING */
3319         case EVENT_ASSOCINFO:
3320                 wpa_supplicant_event_associnfo(wpa_s, data);
3321                 break;
3322         case EVENT_INTERFACE_STATUS:
3323                 wpa_supplicant_event_interface_status(wpa_s, data);
3324                 break;
3325         case EVENT_PMKID_CANDIDATE:
3326                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3327                 break;
3328 #ifdef CONFIG_PEERKEY
3329         case EVENT_STKSTART:
3330                 wpa_supplicant_event_stkstart(wpa_s, data);
3331                 break;
3332 #endif /* CONFIG_PEERKEY */
3333 #ifdef CONFIG_TDLS
3334         case EVENT_TDLS:
3335                 wpa_supplicant_event_tdls(wpa_s, data);
3336                 break;
3337 #endif /* CONFIG_TDLS */
3338 #ifdef CONFIG_WNM
3339         case EVENT_WNM:
3340                 wpa_supplicant_event_wnm(wpa_s, data);
3341                 break;
3342 #endif /* CONFIG_WNM */
3343 #ifdef CONFIG_IEEE80211R
3344         case EVENT_FT_RESPONSE:
3345                 wpa_supplicant_event_ft_response(wpa_s, data);
3346                 break;
3347 #endif /* CONFIG_IEEE80211R */
3348 #ifdef CONFIG_IBSS_RSN
3349         case EVENT_IBSS_RSN_START:
3350                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3351                 break;
3352 #endif /* CONFIG_IBSS_RSN */
3353         case EVENT_ASSOC_REJECT:
3354                 if (data->assoc_reject.bssid)
3355                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3356                                 "bssid=" MACSTR " status_code=%u",
3357                                 MAC2STR(data->assoc_reject.bssid),
3358                                 data->assoc_reject.status_code);
3359                 else
3360                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3361                                 "status_code=%u",
3362                                 data->assoc_reject.status_code);
3363                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3364                         sme_event_assoc_reject(wpa_s, data);
3365                 else {
3366                         const u8 *bssid = data->assoc_reject.bssid;
3367                         if (bssid == NULL || is_zero_ether_addr(bssid))
3368                                 bssid = wpa_s->pending_bssid;
3369                         wpas_connection_failed(wpa_s, bssid);
3370                         wpa_supplicant_mark_disassoc(wpa_s);
3371                 }
3372                 break;
3373         case EVENT_AUTH_TIMED_OUT:
3374                 /* It is possible to get this event from earlier connection */
3375                 if (wpa_s->current_ssid &&
3376                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3377                         wpa_dbg(wpa_s, MSG_DEBUG,
3378                                 "Ignore AUTH_TIMED_OUT in mesh configuration");
3379                         break;
3380                 }
3381                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3382                         sme_event_auth_timed_out(wpa_s, data);
3383                 break;
3384         case EVENT_ASSOC_TIMED_OUT:
3385                 /* It is possible to get this event from earlier connection */
3386                 if (wpa_s->current_ssid &&
3387                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3388                         wpa_dbg(wpa_s, MSG_DEBUG,
3389                                 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3390                         break;
3391                 }
3392                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3393                         sme_event_assoc_timed_out(wpa_s, data);
3394                 break;
3395         case EVENT_TX_STATUS:
3396                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3397                         " type=%d stype=%d",
3398                         MAC2STR(data->tx_status.dst),
3399                         data->tx_status.type, data->tx_status.stype);
3400 #ifdef CONFIG_AP
3401                 if (wpa_s->ap_iface == NULL) {
3402 #ifdef CONFIG_OFFCHANNEL
3403                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3404                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
3405                                 offchannel_send_action_tx_status(
3406                                         wpa_s, data->tx_status.dst,
3407                                         data->tx_status.data,
3408                                         data->tx_status.data_len,
3409                                         data->tx_status.ack ?
3410                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
3411                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
3412 #endif /* CONFIG_OFFCHANNEL */
3413                         break;
3414                 }
3415 #endif /* CONFIG_AP */
3416 #ifdef CONFIG_OFFCHANNEL
3417                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3418                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3419                 /*
3420                  * Catch TX status events for Action frames we sent via group
3421                  * interface in GO mode.
3422                  */
3423                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3424                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3425                     os_memcmp(wpa_s->parent->pending_action_dst,
3426                               data->tx_status.dst, ETH_ALEN) == 0) {
3427                         offchannel_send_action_tx_status(
3428                                 wpa_s->parent, data->tx_status.dst,
3429                                 data->tx_status.data,
3430                                 data->tx_status.data_len,
3431                                 data->tx_status.ack ?
3432                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
3433                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
3434                         break;
3435                 }
3436 #endif /* CONFIG_OFFCHANNEL */
3437 #ifdef CONFIG_AP
3438                 switch (data->tx_status.type) {
3439                 case WLAN_FC_TYPE_MGMT:
3440                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3441                                       data->tx_status.data_len,
3442                                       data->tx_status.stype,
3443                                       data->tx_status.ack);
3444                         break;
3445                 case WLAN_FC_TYPE_DATA:
3446                         ap_tx_status(wpa_s, data->tx_status.dst,
3447                                      data->tx_status.data,
3448                                      data->tx_status.data_len,
3449                                      data->tx_status.ack);
3450                         break;
3451                 }
3452 #endif /* CONFIG_AP */
3453                 break;
3454 #ifdef CONFIG_AP
3455         case EVENT_EAPOL_TX_STATUS:
3456                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3457                                    data->eapol_tx_status.data,
3458                                    data->eapol_tx_status.data_len,
3459                                    data->eapol_tx_status.ack);
3460                 break;
3461         case EVENT_DRIVER_CLIENT_POLL_OK:
3462                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
3463                 break;
3464         case EVENT_RX_FROM_UNKNOWN:
3465                 if (wpa_s->ap_iface == NULL)
3466                         break;
3467                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3468                                        data->rx_from_unknown.wds);
3469                 break;
3470         case EVENT_CH_SWITCH:
3471                 if (!data || !wpa_s->current_ssid)
3472                         break;
3473
3474                 wpa_s->assoc_freq = data->ch_switch.freq;
3475                 wpa_s->current_ssid->frequency = data->ch_switch.freq;
3476
3477                 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
3478                     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
3479                     wpa_s->current_ssid->mode ==
3480                     WPAS_MODE_P2P_GROUP_FORMATION) {
3481                         wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3482                                           data->ch_switch.ht_enabled,
3483                                           data->ch_switch.ch_offset,
3484                                           data->ch_switch.ch_width,
3485                                           data->ch_switch.cf1,
3486                                           data->ch_switch.cf2);
3487                 }
3488
3489                 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
3490                 break;
3491 #ifdef NEED_AP_MLME
3492         case EVENT_DFS_RADAR_DETECTED:
3493                 if (data)
3494                         wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3495                 break;
3496         case EVENT_DFS_CAC_STARTED:
3497                 if (data)
3498                         wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3499                 break;
3500         case EVENT_DFS_CAC_FINISHED:
3501                 if (data)
3502                         wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3503                 break;
3504         case EVENT_DFS_CAC_ABORTED:
3505                 if (data)
3506                         wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3507                 break;
3508         case EVENT_DFS_NOP_FINISHED:
3509                 if (data)
3510                         wpas_event_dfs_cac_nop_finished(wpa_s,
3511                                                         &data->dfs_event);
3512                 break;
3513 #endif /* NEED_AP_MLME */
3514 #endif /* CONFIG_AP */
3515         case EVENT_RX_MGMT: {
3516                 u16 fc, stype;
3517                 const struct ieee80211_mgmt *mgmt;
3518
3519 #ifdef CONFIG_TESTING_OPTIONS
3520                 if (wpa_s->ext_mgmt_frame_handling) {
3521                         struct rx_mgmt *rx = &data->rx_mgmt;
3522                         size_t hex_len = 2 * rx->frame_len + 1;
3523                         char *hex = os_malloc(hex_len);
3524                         if (hex) {
3525                                 wpa_snprintf_hex(hex, hex_len,
3526                                                  rx->frame, rx->frame_len);
3527                                 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3528                                         rx->freq, rx->datarate, rx->ssi_signal,
3529                                         hex);
3530                                 os_free(hex);
3531                         }
3532                         break;
3533                 }
3534 #endif /* CONFIG_TESTING_OPTIONS */
3535
3536                 mgmt = (const struct ieee80211_mgmt *)
3537                         data->rx_mgmt.frame;
3538                 fc = le_to_host16(mgmt->frame_control);
3539                 stype = WLAN_FC_GET_STYPE(fc);
3540
3541 #ifdef CONFIG_AP
3542                 if (wpa_s->ap_iface == NULL) {
3543 #endif /* CONFIG_AP */
3544 #ifdef CONFIG_P2P
3545                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3546                             data->rx_mgmt.frame_len > 24) {
3547                                 const u8 *src = mgmt->sa;
3548                                 const u8 *ie = mgmt->u.probe_req.variable;
3549                                 size_t ie_len = data->rx_mgmt.frame_len -
3550                                         (mgmt->u.probe_req.variable -
3551                                          data->rx_mgmt.frame);
3552                                 wpas_p2p_probe_req_rx(
3553                                         wpa_s, src, mgmt->da,
3554                                         mgmt->bssid, ie, ie_len,
3555                                         data->rx_mgmt.freq,
3556                                         data->rx_mgmt.ssi_signal);
3557                                 break;
3558                         }
3559 #endif /* CONFIG_P2P */
3560 #ifdef CONFIG_IBSS_RSN
3561                         if (wpa_s->current_ssid &&
3562                             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3563                             stype == WLAN_FC_STYPE_AUTH &&
3564                             data->rx_mgmt.frame_len >= 30) {
3565                                 wpa_supplicant_event_ibss_auth(wpa_s, data);
3566                                 break;
3567                         }
3568 #endif /* CONFIG_IBSS_RSN */
3569
3570                         if (stype == WLAN_FC_STYPE_ACTION) {
3571                                 wpas_event_rx_mgmt_action(
3572                                         wpa_s, data->rx_mgmt.frame,
3573                                         data->rx_mgmt.frame_len,
3574                                         data->rx_mgmt.freq,
3575                                         data->rx_mgmt.ssi_signal);
3576                                 break;
3577                         }
3578
3579                         if (wpa_s->ifmsh) {
3580                                 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
3581                                 break;
3582                         }
3583
3584                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3585                                 "management frame in non-AP mode");
3586                         break;
3587 #ifdef CONFIG_AP
3588                 }
3589
3590                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3591                     data->rx_mgmt.frame_len > 24) {
3592                         const u8 *ie = mgmt->u.probe_req.variable;
3593                         size_t ie_len = data->rx_mgmt.frame_len -
3594                                 (mgmt->u.probe_req.variable -
3595                                  data->rx_mgmt.frame);
3596
3597                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3598                                          mgmt->bssid, ie, ie_len,
3599                                          data->rx_mgmt.ssi_signal);
3600                 }
3601
3602                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
3603 #endif /* CONFIG_AP */
3604                 break;
3605                 }
3606         case EVENT_RX_PROBE_REQ:
3607                 if (data->rx_probe_req.sa == NULL ||
3608                     data->rx_probe_req.ie == NULL)
3609                         break;
3610 #ifdef CONFIG_AP
3611                 if (wpa_s->ap_iface) {
3612                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3613                                              data->rx_probe_req.sa,
3614                                              data->rx_probe_req.da,
3615                                              data->rx_probe_req.bssid,
3616                                              data->rx_probe_req.ie,
3617                                              data->rx_probe_req.ie_len,
3618                                              data->rx_probe_req.ssi_signal);
3619                         break;
3620                 }
3621 #endif /* CONFIG_AP */
3622                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3623                                       data->rx_probe_req.da,
3624                                       data->rx_probe_req.bssid,
3625                                       data->rx_probe_req.ie,
3626                                       data->rx_probe_req.ie_len,
3627                                       0,
3628                                       data->rx_probe_req.ssi_signal);
3629                 break;
3630         case EVENT_REMAIN_ON_CHANNEL:
3631 #ifdef CONFIG_OFFCHANNEL
3632                 offchannel_remain_on_channel_cb(
3633                         wpa_s, data->remain_on_channel.freq,
3634                         data->remain_on_channel.duration);
3635 #endif /* CONFIG_OFFCHANNEL */
3636                 wpas_p2p_remain_on_channel_cb(
3637                         wpa_s, data->remain_on_channel.freq,
3638                         data->remain_on_channel.duration);
3639                 break;
3640         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3641 #ifdef CONFIG_OFFCHANNEL
3642                 offchannel_cancel_remain_on_channel_cb(
3643                         wpa_s, data->remain_on_channel.freq);
3644 #endif /* CONFIG_OFFCHANNEL */
3645                 wpas_p2p_cancel_remain_on_channel_cb(
3646                         wpa_s, data->remain_on_channel.freq);
3647                 break;
3648         case EVENT_EAPOL_RX:
3649                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3650                                         data->eapol_rx.data,
3651                                         data->eapol_rx.data_len);
3652                 break;
3653         case EVENT_SIGNAL_CHANGE:
3654                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3655                         "above=%d signal=%d noise=%d txrate=%d",
3656                         data->signal_change.above_threshold,
3657                         data->signal_change.current_signal,
3658                         data->signal_change.current_noise,
3659                         data->signal_change.current_txrate);
3660                 wpa_bss_update_level(wpa_s->current_bss,
3661                                      data->signal_change.current_signal);
3662                 bgscan_notify_signal_change(
3663                         wpa_s, data->signal_change.above_threshold,
3664                         data->signal_change.current_signal,
3665                         data->signal_change.current_noise,
3666                         data->signal_change.current_txrate);
3667                 break;
3668         case EVENT_INTERFACE_ENABLED:
3669                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3670                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3671                         wpa_supplicant_update_mac_addr(wpa_s);
3672                         if (wpa_s->p2p_mgmt) {
3673                                 wpa_supplicant_set_state(wpa_s,
3674                                                          WPA_DISCONNECTED);
3675                                 break;
3676                         }
3677
3678 #ifdef CONFIG_AP
3679                         if (!wpa_s->ap_iface) {
3680                                 wpa_supplicant_set_state(wpa_s,
3681                                                          WPA_DISCONNECTED);
3682                                 wpa_s->scan_req = NORMAL_SCAN_REQ;
3683                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3684                         } else
3685                                 wpa_supplicant_set_state(wpa_s,
3686                                                          WPA_COMPLETED);
3687 #else /* CONFIG_AP */
3688                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3689                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3690 #endif /* CONFIG_AP */
3691                 }
3692                 break;
3693         case EVENT_INTERFACE_DISABLED:
3694                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3695 #ifdef CONFIG_P2P
3696                 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3697                     (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3698                      wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3699                         /*
3700                          * Mark interface disabled if this happens to end up not
3701                          * being removed as a separate P2P group interface.
3702                          */
3703                         wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3704                         /*
3705                          * The interface was externally disabled. Remove
3706                          * it assuming an external entity will start a
3707                          * new session if needed.
3708                          */
3709                         if (wpa_s->current_ssid &&
3710                             wpa_s->current_ssid->p2p_group)
3711                                 wpas_p2p_interface_unavailable(wpa_s);
3712                         else
3713                                 wpas_p2p_disconnect(wpa_s);
3714                         /*
3715                          * wpa_s instance may have been freed, so must not use
3716                          * it here anymore.
3717                          */
3718                         break;
3719                 }
3720                 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3721                     p2p_in_progress(wpa_s->global->p2p) > 1) {
3722                         /* This radio work will be cancelled, so clear P2P
3723                          * state as well.
3724                          */
3725                         p2p_stop_find(wpa_s->global->p2p);
3726                 }
3727 #endif /* CONFIG_P2P */
3728
3729                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3730                         /*
3731                          * Indicate disconnection to keep ctrl_iface events
3732                          * consistent.
3733                          */
3734                         wpa_supplicant_event_disassoc(
3735                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3736                 }
3737                 wpa_supplicant_mark_disassoc(wpa_s);
3738                 radio_remove_works(wpa_s, NULL, 0);
3739
3740                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3741                 break;
3742         case EVENT_CHANNEL_LIST_CHANGED:
3743                 wpa_supplicant_update_channel_list(
3744                         wpa_s, &data->channel_list_changed);
3745                 break;
3746         case EVENT_INTERFACE_UNAVAILABLE:
3747                 wpas_p2p_interface_unavailable(wpa_s);
3748                 break;
3749         case EVENT_BEST_CHANNEL:
3750                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3751                         "(%d %d %d)",
3752                         data->best_chan.freq_24, data->best_chan.freq_5,
3753                         data->best_chan.freq_overall);
3754                 wpa_s->best_24_freq = data->best_chan.freq_24;
3755                 wpa_s->best_5_freq = data->best_chan.freq_5;
3756                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3757                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3758                                               data->best_chan.freq_5,
3759                                               data->best_chan.freq_overall);
3760                 break;
3761         case EVENT_UNPROT_DEAUTH:
3762                 wpa_supplicant_event_unprot_deauth(wpa_s,
3763                                                    &data->unprot_deauth);
3764                 break;
3765         case EVENT_UNPROT_DISASSOC:
3766                 wpa_supplicant_event_unprot_disassoc(wpa_s,
3767                                                      &data->unprot_disassoc);
3768                 break;
3769         case EVENT_STATION_LOW_ACK:
3770 #ifdef CONFIG_AP
3771                 if (wpa_s->ap_iface && data)
3772                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3773                                                   data->low_ack.addr);
3774 #endif /* CONFIG_AP */
3775 #ifdef CONFIG_TDLS
3776                 if (data)
3777                         wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3778                                                           data->low_ack.addr);
3779 #endif /* CONFIG_TDLS */
3780                 break;
3781         case EVENT_IBSS_PEER_LOST:
3782 #ifdef CONFIG_IBSS_RSN
3783                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3784 #endif /* CONFIG_IBSS_RSN */
3785                 break;
3786         case EVENT_DRIVER_GTK_REKEY:
3787                 if (os_memcmp(data->driver_gtk_rekey.bssid,
3788                               wpa_s->bssid, ETH_ALEN))
3789                         break;
3790                 if (!wpa_s->wpa)
3791                         break;
3792                 wpa_sm_update_replay_ctr(wpa_s->wpa,
3793                                          data->driver_gtk_rekey.replay_ctr);
3794                 break;
3795         case EVENT_SCHED_SCAN_STOPPED:
3796                 wpa_s->pno = 0;
3797                 wpa_s->sched_scanning = 0;
3798                 resched = wpa_s->scanning;
3799                 wpa_supplicant_notify_scanning(wpa_s, 0);
3800
3801                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3802                         break;
3803
3804                 /*
3805                  * Start a new sched scan to continue searching for more SSIDs
3806                  * either if timed out or PNO schedule scan is pending.
3807                  */
3808                 if (wpa_s->sched_scan_timed_out) {
3809                         wpa_supplicant_req_sched_scan(wpa_s);
3810                 } else if (wpa_s->pno_sched_pending) {
3811                         wpa_s->pno_sched_pending = 0;
3812                         wpas_start_pno(wpa_s);
3813                 } else if (resched) {
3814                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3815                 }
3816
3817                 break;
3818         case EVENT_WPS_BUTTON_PUSHED:
3819 #ifdef CONFIG_WPS
3820                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3821 #endif /* CONFIG_WPS */
3822                 break;
3823         case EVENT_AVOID_FREQUENCIES:
3824                 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3825                 break;
3826         case EVENT_CONNECT_FAILED_REASON:
3827 #ifdef CONFIG_AP
3828                 if (!wpa_s->ap_iface || !data)
3829                         break;
3830                 hostapd_event_connect_failed_reason(
3831                         wpa_s->ap_iface->bss[0],
3832                         data->connect_failed_reason.addr,
3833                         data->connect_failed_reason.code);
3834 #endif /* CONFIG_AP */
3835                 break;
3836         case EVENT_NEW_PEER_CANDIDATE:
3837 #ifdef CONFIG_MESH
3838                 if (!wpa_s->ifmsh || !data)
3839                         break;
3840                 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3841                                      data->mesh_peer.ies,
3842                                      data->mesh_peer.ie_len);
3843 #endif /* CONFIG_MESH */
3844                 break;
3845         default:
3846                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3847                 break;
3848         }
3849 }