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