5792eb59189f5f8f1daca63a09e1066f6ba3479b
[mech_eap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "pcsc_funcs.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "common/wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ap/hostapd.h"
31 #include "p2p/p2p.h"
32 #include "notify.h"
33 #include "common/ieee802_11_defs.h"
34 #include "common/ieee802_11_common.h"
35 #include "crypto/random.h"
36 #include "blacklist.h"
37 #include "wpas_glue.h"
38 #include "wps_supplicant.h"
39 #include "ibss_rsn.h"
40 #include "sme.h"
41 #include "p2p_supplicant.h"
42 #include "bgscan.h"
43 #include "ap.h"
44 #include "bss.h"
45 #include "mlme.h"
46 #include "scan.h"
47
48
49 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
50 {
51         struct wpa_ssid *ssid, *old_ssid;
52
53         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54                 return 0;
55
56         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
57                 "information");
58         ssid = wpa_supplicant_get_ssid(wpa_s);
59         if (ssid == NULL) {
60                 wpa_msg(wpa_s, MSG_INFO,
61                         "No network configuration found for the current AP");
62                 return -1;
63         }
64
65         if (ssid->disabled) {
66                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
67                 return -1;
68         }
69
70         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
71                 "current AP");
72         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
73                               WPA_KEY_MGMT_WPA_NONE |
74                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
75                               WPA_KEY_MGMT_PSK_SHA256 |
76                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
77                 u8 wpa_ie[80];
78                 size_t wpa_ie_len = sizeof(wpa_ie);
79                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
80                                           wpa_ie, &wpa_ie_len);
81         } else {
82                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
83         }
84
85         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
86                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
87         old_ssid = wpa_s->current_ssid;
88         wpa_s->current_ssid = ssid;
89         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
90         wpa_supplicant_initiate_eapol(wpa_s);
91         if (old_ssid != wpa_s->current_ssid)
92                 wpas_notify_network_changed(wpa_s);
93
94         return 0;
95 }
96
97
98 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99                                                 void *sock_ctx)
100 {
101         struct wpa_supplicant *wpa_s = eloop_ctx;
102
103         if (wpa_s->countermeasures) {
104                 wpa_s->countermeasures = 0;
105                 wpa_drv_set_countermeasures(wpa_s, 0);
106                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
107                 wpa_supplicant_req_scan(wpa_s, 0, 0);
108         }
109 }
110
111
112 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
113 {
114         int bssid_changed;
115
116         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
117                 return;
118
119         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
120         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
121         os_memset(wpa_s->bssid, 0, ETH_ALEN);
122         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
123         wpa_s->current_bss = NULL;
124         wpa_s->assoc_freq = 0;
125         if (bssid_changed)
126                 wpas_notify_bssid_changed(wpa_s);
127
128         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
129         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
130         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
131                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
132         wpa_s->ap_ies_from_associnfo = 0;
133 }
134
135
136 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
137 {
138         struct wpa_ie_data ie;
139         int pmksa_set = -1;
140         size_t i;
141
142         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
143             ie.pmkid == NULL)
144                 return;
145
146         for (i = 0; i < ie.num_pmkid; i++) {
147                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
148                                                     ie.pmkid + i * PMKID_LEN,
149                                                     NULL, NULL, 0);
150                 if (pmksa_set == 0) {
151                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
152                         break;
153                 }
154         }
155
156         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
157                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
158 }
159
160
161 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
162                                                  union wpa_event_data *data)
163 {
164         if (data == NULL) {
165                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
166                         "event");
167                 return;
168         }
169         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
170                 " index=%d preauth=%d",
171                 MAC2STR(data->pmkid_candidate.bssid),
172                 data->pmkid_candidate.index,
173                 data->pmkid_candidate.preauth);
174
175         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
176                             data->pmkid_candidate.index,
177                             data->pmkid_candidate.preauth);
178 }
179
180
181 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
182 {
183         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
184             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
185                 return 0;
186
187 #ifdef IEEE8021X_EAPOL
188         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
189             wpa_s->current_ssid &&
190             !(wpa_s->current_ssid->eapol_flags &
191               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
192                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
193                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
194                  * plaintext or static WEP keys). */
195                 return 0;
196         }
197 #endif /* IEEE8021X_EAPOL */
198
199         return 1;
200 }
201
202
203 /**
204  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
205  * @wpa_s: pointer to wpa_supplicant data
206  * @ssid: Configuration data for the network
207  * Returns: 0 on success, -1 on failure
208  *
209  * This function is called when starting authentication with a network that is
210  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
211  */
212 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
213                               struct wpa_ssid *ssid)
214 {
215 #ifdef IEEE8021X_EAPOL
216         int aka = 0, sim = 0, type;
217
218         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
219                 return 0;
220
221         if (ssid->eap.eap_methods == NULL) {
222                 sim = 1;
223                 aka = 1;
224         } else {
225                 struct eap_method_type *eap = ssid->eap.eap_methods;
226                 while (eap->vendor != EAP_VENDOR_IETF ||
227                        eap->method != EAP_TYPE_NONE) {
228                         if (eap->vendor == EAP_VENDOR_IETF) {
229                                 if (eap->method == EAP_TYPE_SIM)
230                                         sim = 1;
231                                 else if (eap->method == EAP_TYPE_AKA)
232                                         aka = 1;
233                         }
234                         eap++;
235                 }
236         }
237
238         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
239                 sim = 0;
240         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
241                 aka = 0;
242
243         if (!sim && !aka) {
244                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
245                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
246                         "enabled");
247                 return 0;
248         }
249
250         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
251                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
252         if (sim && aka)
253                 type = SCARD_TRY_BOTH;
254         else if (aka)
255                 type = SCARD_USIM_ONLY;
256         else
257                 type = SCARD_GSM_SIM_ONLY;
258
259         wpa_s->scard = scard_init(type);
260         if (wpa_s->scard == NULL) {
261                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
262                         "(pcsc-lite)");
263                 return -1;
264         }
265         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
266         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
267 #endif /* IEEE8021X_EAPOL */
268
269         return 0;
270 }
271
272
273 #ifndef CONFIG_NO_SCAN_PROCESSING
274 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
275                                         struct wpa_ssid *ssid)
276 {
277         int i, privacy = 0;
278
279         if (ssid->mixed_cell)
280                 return 1;
281
282 #ifdef CONFIG_WPS
283         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
284                 return 1;
285 #endif /* CONFIG_WPS */
286
287         for (i = 0; i < NUM_WEP_KEYS; i++) {
288                 if (ssid->wep_key_len[i]) {
289                         privacy = 1;
290                         break;
291                 }
292         }
293 #ifdef IEEE8021X_EAPOL
294         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
295             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
296                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
297                 privacy = 1;
298 #endif /* IEEE8021X_EAPOL */
299
300         if (bss->caps & IEEE80211_CAP_PRIVACY)
301                 return privacy;
302         return !privacy;
303 }
304
305
306 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
307                                          struct wpa_ssid *ssid,
308                                          struct wpa_scan_res *bss)
309 {
310         struct wpa_ie_data ie;
311         int proto_match = 0;
312         const u8 *rsn_ie, *wpa_ie;
313         int ret;
314         int wep_ok;
315
316         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
317         if (ret >= 0)
318                 return ret;
319
320         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
321         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
322                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
323                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
324                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
325
326         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
327         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
328                 proto_match++;
329
330                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
331                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
332                                 "failed");
333                         break;
334                 }
335
336                 if (wep_ok &&
337                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
338                 {
339                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
340                                 "in RSN IE");
341                         return 1;
342                 }
343
344                 if (!(ie.proto & ssid->proto)) {
345                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
346                                 "mismatch");
347                         break;
348                 }
349
350                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
351                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
352                                 "cipher mismatch");
353                         break;
354                 }
355
356                 if (!(ie.group_cipher & ssid->group_cipher)) {
357                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
358                                 "cipher mismatch");
359                         break;
360                 }
361
362                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
363                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
364                                 "mismatch");
365                         break;
366                 }
367
368 #ifdef CONFIG_IEEE80211W
369                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
370                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
371                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
372                                 "frame protection");
373                         break;
374                 }
375 #endif /* CONFIG_IEEE80211W */
376
377                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
378                 return 1;
379         }
380
381         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
382         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
383                 proto_match++;
384
385                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
386                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
387                                 "failed");
388                         break;
389                 }
390
391                 if (wep_ok &&
392                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
393                 {
394                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
395                                 "in WPA IE");
396                         return 1;
397                 }
398
399                 if (!(ie.proto & ssid->proto)) {
400                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
401                                 "mismatch");
402                         break;
403                 }
404
405                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
406                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
407                                 "cipher mismatch");
408                         break;
409                 }
410
411                 if (!(ie.group_cipher & ssid->group_cipher)) {
412                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
413                                 "cipher mismatch");
414                         break;
415                 }
416
417                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
418                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
419                                 "mismatch");
420                         break;
421                 }
422
423                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
424                 return 1;
425         }
426
427         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
428             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
429                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
430                 return 0;
431         }
432
433         /* Allow in non-WPA configuration */
434         return 1;
435 }
436
437
438 static int freq_allowed(int *freqs, int freq)
439 {
440         int i;
441
442         if (freqs == NULL)
443                 return 1;
444
445         for (i = 0; freqs[i]; i++)
446                 if (freqs[i] == freq)
447                         return 1;
448         return 0;
449 }
450
451
452 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
453                                             int i, struct wpa_scan_res *bss,
454                                             struct wpa_ssid *group)
455 {
456         const u8 *ssid_;
457         u8 wpa_ie_len, rsn_ie_len, ssid_len;
458         int wpa;
459         struct wpa_blacklist *e;
460         const u8 *ie;
461         struct wpa_ssid *ssid;
462
463         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
464         ssid_ = ie ? ie + 2 : (u8 *) "";
465         ssid_len = ie ? ie[1] : 0;
466
467         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
468         wpa_ie_len = ie ? ie[1] : 0;
469
470         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
471         rsn_ie_len = ie ? ie[1] : 0;
472
473         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
474                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
475                 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
476                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
477                 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
478
479         e = wpa_blacklist_get(wpa_s, bss->bssid);
480         if (e) {
481                 int limit = 1;
482                 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
483                         /*
484                          * When only a single network is enabled, we can
485                          * trigger blacklisting on the first failure. This
486                          * should not be done with multiple enabled networks to
487                          * avoid getting forced to move into a worse ESS on
488                          * single error if there are no other BSSes of the
489                          * current ESS.
490                          */
491                         limit = 0;
492                 }
493                 if (e->count > limit) {
494                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
495                                 "(count=%d limit=%d)", e->count, limit);
496                         return 0;
497                 }
498         }
499
500         if (ssid_len == 0) {
501                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
502                 return 0;
503         }
504
505         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
506
507         for (ssid = group; ssid; ssid = ssid->pnext) {
508                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
509
510                 if (ssid->disabled) {
511                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
512                         continue;
513                 }
514
515 #ifdef CONFIG_WPS
516                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
517                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
518                                 "(WPS)");
519                         continue;
520                 }
521
522                 if (wpa && ssid->ssid_len == 0 &&
523                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
524                         check_ssid = 0;
525
526                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
527                         /* Only allow wildcard SSID match if an AP
528                          * advertises active WPS operation that matches
529                          * with our mode. */
530                         check_ssid = 1;
531                         if (ssid->ssid_len == 0 &&
532                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
533                                 check_ssid = 0;
534                 }
535 #endif /* CONFIG_WPS */
536
537                 if (check_ssid &&
538                     (ssid_len != ssid->ssid_len ||
539                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
540                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
541                         continue;
542                 }
543
544                 if (ssid->bssid_set &&
545                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
546                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
547                         continue;
548                 }
549
550                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
551                         continue;
552
553                 if (!wpa &&
554                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
555                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
556                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
557                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
558                                 "not allowed");
559                         continue;
560                 }
561
562                 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
563                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
564                                 "mismatch");
565                         continue;
566                 }
567
568                 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
569                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
570                                 "network");
571                         continue;
572                 }
573
574                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
575                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
576                                 "allowed");
577                         continue;
578                 }
579
580 #ifdef CONFIG_P2P
581                 /*
582                  * TODO: skip the AP if its P2P IE has Group Formation
583                  * bit set in the P2P Group Capability Bitmap and we
584                  * are not in Group Formation with that device.
585                  */
586 #endif /* CONFIG_P2P */
587
588                 /* Matching configuration found */
589                 return ssid;
590         }
591
592         /* No matching configuration found */
593         return 0;
594 }
595
596
597 static struct wpa_bss *
598 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
599                           struct wpa_scan_results *scan_res,
600                           struct wpa_ssid *group,
601                           struct wpa_ssid **selected_ssid)
602 {
603         size_t i;
604
605         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
606                 group->priority);
607
608         for (i = 0; i < scan_res->num; i++) {
609                 struct wpa_scan_res *bss = scan_res->res[i];
610                 const u8 *ie, *ssid;
611                 u8 ssid_len;
612
613                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
614                 if (!*selected_ssid)
615                         continue;
616
617                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
618                 ssid = ie ? ie + 2 : (u8 *) "";
619                 ssid_len = ie ? ie[1] : 0;
620
621                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
622                         " ssid='%s'",
623                         MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
624                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
625         }
626
627         return NULL;
628 }
629
630
631 static struct wpa_bss *
632 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
633                             struct wpa_scan_results *scan_res,
634                             struct wpa_ssid **selected_ssid)
635 {
636         struct wpa_bss *selected = NULL;
637         int prio;
638
639         while (selected == NULL) {
640                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
641                         selected = wpa_supplicant_select_bss(
642                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
643                                 selected_ssid);
644                         if (selected)
645                                 break;
646                 }
647
648                 if (selected == NULL && wpa_s->blacklist) {
649                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
650                                 "blacklist and try again");
651                         wpa_blacklist_clear(wpa_s);
652                         wpa_s->blacklist_cleared++;
653                 } else if (selected == NULL)
654                         break;
655         }
656
657         return selected;
658 }
659
660
661 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
662                                         int timeout_sec, int timeout_usec)
663 {
664         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
665                 /*
666                  * No networks are enabled; short-circuit request so
667                  * we don't wait timeout seconds before transitioning
668                  * to INACTIVE state.
669                  */
670                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
671                 return;
672         }
673         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
674 }
675
676
677 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
678                             struct wpa_bss *selected,
679                             struct wpa_ssid *ssid)
680 {
681         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
682                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
683                         "PBC session overlap");
684 #ifdef CONFIG_P2P
685                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
686                         return;
687 #endif /* CONFIG_P2P */
688
689 #ifdef CONFIG_WPS
690                 wpas_wps_cancel(wpa_s);
691 #endif /* CONFIG_WPS */
692                 return;
693         }
694
695         /*
696          * Do not trigger new association unless the BSSID has changed or if
697          * reassociation is requested. If we are in process of associating with
698          * the selected BSSID, do not trigger new attempt.
699          */
700         if (wpa_s->reassociate ||
701             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
702              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
703                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
704               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
705               0))) {
706                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
707                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
708                         return;
709                 }
710                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
711                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
712                         "  pending: " MACSTR "  wpa_state: %s",
713                         wpa_s->reassociate, MAC2STR(selected->bssid),
714                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
715                         wpa_supplicant_state_txt(wpa_s->wpa_state));
716                 wpa_supplicant_associate(wpa_s, selected, ssid);
717         } else {
718                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
719                         "selected AP");
720         }
721 }
722
723
724 static struct wpa_ssid *
725 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
726 {
727         int prio;
728         struct wpa_ssid *ssid;
729
730         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
731                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
732                 {
733                         if (ssid->disabled)
734                                 continue;
735                         if (ssid->mode == IEEE80211_MODE_IBSS ||
736                             ssid->mode == IEEE80211_MODE_AP)
737                                 return ssid;
738                 }
739         }
740         return NULL;
741 }
742
743
744 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
745  * on BSS added and BSS changed events */
746 static void wpa_supplicant_rsn_preauth_scan_results(
747         struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
748 {
749         int i;
750
751         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
752                 return;
753
754         for (i = scan_res->num - 1; i >= 0; i--) {
755                 const u8 *ssid, *rsn;
756                 struct wpa_scan_res *r;
757
758                 r = scan_res->res[i];
759
760                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
761                 if (ssid == NULL)
762                         continue;
763
764                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
765                 if (rsn == NULL)
766                         continue;
767
768                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
769         }
770
771 }
772
773
774 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
775                                        struct wpa_bss *selected,
776                                        struct wpa_ssid *ssid,
777                                        struct wpa_scan_results *scan_res)
778 {
779         size_t i;
780         struct wpa_scan_res *current_bss = NULL;
781         int min_diff;
782
783         if (wpa_s->reassociate)
784                 return 1; /* explicit request to reassociate */
785         if (wpa_s->wpa_state < WPA_ASSOCIATED)
786                 return 1; /* we are not associated; continue */
787         if (wpa_s->current_ssid == NULL)
788                 return 1; /* unknown current SSID */
789         if (wpa_s->current_ssid != ssid)
790                 return 1; /* different network block */
791
792         for (i = 0; i < scan_res->num; i++) {
793                 struct wpa_scan_res *res = scan_res->res[i];
794                 const u8 *ie;
795                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
796                         continue;
797
798                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
799                 if (ie == NULL)
800                         continue;
801                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
802                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
803                         continue;
804                 current_bss = res;
805                 break;
806         }
807
808         if (!current_bss)
809                 return 1; /* current BSS not seen in scan results */
810
811         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
812         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
813                 MAC2STR(current_bss->bssid), current_bss->level);
814         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
815                 MAC2STR(selected->bssid), selected->level);
816
817         if (wpa_s->current_ssid->bssid_set &&
818             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
819             0) {
820                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
821                         "has preferred BSSID");
822                 return 1;
823         }
824
825         min_diff = 2;
826         if (current_bss->level < 0) {
827                 if (current_bss->level < -85)
828                         min_diff = 1;
829                 else if (current_bss->level < -80)
830                         min_diff = 2;
831                 else if (current_bss->level < -75)
832                         min_diff = 3;
833                 else if (current_bss->level < -70)
834                         min_diff = 4;
835                 else
836                         min_diff = 5;
837         }
838         if (abs(current_bss->level - selected->level) < min_diff) {
839                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
840                         "in signal level");
841                 return 0;
842         }
843
844         return 1;
845 }
846
847 /* Return < 0 if no scan results could be fetched. */
848 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
849                                               union wpa_event_data *data)
850 {
851         struct wpa_bss *selected;
852         struct wpa_ssid *ssid = NULL;
853         struct wpa_scan_results *scan_res;
854         int ap = 0;
855
856 #ifdef CONFIG_AP
857         if (wpa_s->ap_iface)
858                 ap = 1;
859 #endif /* CONFIG_AP */
860
861         wpa_supplicant_notify_scanning(wpa_s, 0);
862
863         scan_res = wpa_supplicant_get_scan_results(wpa_s,
864                                                    data ? &data->scan_info :
865                                                    NULL, 1);
866         if (scan_res == NULL) {
867                 if (wpa_s->conf->ap_scan == 2 || ap)
868                         return -1;
869                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
870                         "scanning again");
871                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
872                 return -1;
873         }
874
875 #ifndef CONFIG_NO_RANDOM_POOL
876         size_t i, num;
877         num = scan_res->num;
878         if (num > 10)
879                 num = 10;
880         for (i = 0; i < num; i++) {
881                 u8 buf[5];
882                 struct wpa_scan_res *res = scan_res->res[i];
883                 buf[0] = res->bssid[5];
884                 buf[1] = res->qual & 0xff;
885                 buf[2] = res->noise & 0xff;
886                 buf[3] = res->level & 0xff;
887                 buf[4] = res->tsf & 0xff;
888                 random_add_randomness(buf, sizeof(buf));
889         }
890 #endif /* CONFIG_NO_RANDOM_POOL */
891
892         if (wpa_s->scan_res_handler) {
893                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
894                                          struct wpa_scan_results *scan_res);
895
896                 scan_res_handler = wpa_s->scan_res_handler;
897                 wpa_s->scan_res_handler = NULL;
898                 scan_res_handler(wpa_s, scan_res);
899
900                 wpa_scan_results_free(scan_res);
901                 return 0;
902         }
903
904         if (ap) {
905                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
906                 wpa_scan_results_free(scan_res);
907                 return 0;
908         }
909
910         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
911         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
912         wpas_notify_scan_results(wpa_s);
913
914         wpas_notify_scan_done(wpa_s, 1);
915
916         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
917                 wpa_scan_results_free(scan_res);
918                 return 0;
919         }
920
921         if (wpa_s->disconnected) {
922                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
923                 wpa_scan_results_free(scan_res);
924                 return 0;
925         }
926
927         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
928                 wpa_scan_results_free(scan_res);
929                 return 0;
930         }
931
932         wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
933
934         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
935
936         if (selected) {
937                 int skip;
938                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
939                                                     scan_res);
940                 wpa_scan_results_free(scan_res);
941                 if (skip)
942                         return 0;
943                 wpa_supplicant_connect(wpa_s, selected, ssid);
944         } else {
945                 wpa_scan_results_free(scan_res);
946                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
947                 ssid = wpa_supplicant_pick_new_network(wpa_s);
948                 if (ssid) {
949                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
950                         wpa_supplicant_associate(wpa_s, NULL, ssid);
951                 } else {
952                         int timeout_sec = 5;
953                         int timeout_usec = 0;
954 #ifdef CONFIG_P2P
955                         if (wpa_s->p2p_in_provisioning) {
956                                 /*
957                                  * Use shorter wait during P2P Provisioning
958                                  * state to speed up group formation.
959                                  */
960                                 timeout_sec = 0;
961                                 timeout_usec = 250000;
962                         }
963 #endif /* CONFIG_P2P */
964                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
965                                                     timeout_usec);
966                 }
967         }
968         return 0;
969 }
970
971
972 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
973                                               union wpa_event_data *data)
974 {
975         const char *rn, *rn2;
976         struct wpa_supplicant *ifs;
977
978         if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
979                 /*
980                  * If no scan results could be fetched, then no need to
981                  * notify those interfaces that did not actually request
982                  * this scan.
983                  */
984                 return;
985         }
986
987         /*
988          * Check other interfaces to see if they have the same radio-name. If
989          * so, they get updated with this same scan info.
990          */
991         if (!wpa_s->driver->get_radio_name)
992                 return;
993
994         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
995         if (rn == NULL || rn[0] == '\0')
996                 return;
997
998         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
999                 "sharing same radio (%s) in event_scan_results", rn);
1000
1001         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1002                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1003                         continue;
1004
1005                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1006                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1007                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1008                                    "sibling", ifs->ifname);
1009                         _wpa_supplicant_event_scan_results(ifs, data);
1010                 }
1011         }
1012 }
1013
1014 #endif /* CONFIG_NO_SCAN_PROCESSING */
1015
1016
1017 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1018                                           union wpa_event_data *data)
1019 {
1020         int l, len, found = 0, wpa_found, rsn_found;
1021         const u8 *p;
1022
1023         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1024         if (data->assoc_info.req_ies)
1025                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1026                             data->assoc_info.req_ies_len);
1027         if (data->assoc_info.resp_ies) {
1028                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1029                             data->assoc_info.resp_ies_len);
1030 #ifdef CONFIG_TDLS
1031                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1032                                         data->assoc_info.resp_ies_len);
1033 #endif /* CONFIG_TDLS */
1034         }
1035         if (data->assoc_info.beacon_ies)
1036                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1037                             data->assoc_info.beacon_ies,
1038                             data->assoc_info.beacon_ies_len);
1039         if (data->assoc_info.freq)
1040                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1041                         data->assoc_info.freq);
1042
1043         p = data->assoc_info.req_ies;
1044         l = data->assoc_info.req_ies_len;
1045
1046         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1047         while (p && l >= 2) {
1048                 len = p[1] + 2;
1049                 if (len > l) {
1050                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1051                                     p, l);
1052                         break;
1053                 }
1054                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1055                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1056                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1057                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1058                                 break;
1059                         found = 1;
1060                         wpa_find_assoc_pmkid(wpa_s);
1061                         break;
1062                 }
1063                 l -= len;
1064                 p += len;
1065         }
1066         if (!found && data->assoc_info.req_ies)
1067                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1068
1069 #ifdef CONFIG_IEEE80211R
1070 #ifdef CONFIG_SME
1071         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1072                 u8 bssid[ETH_ALEN];
1073                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1074                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1075                                                  data->assoc_info.resp_ies,
1076                                                  data->assoc_info.resp_ies_len,
1077                                                  bssid) < 0) {
1078                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1079                                 "Reassociation Response failed");
1080                         wpa_supplicant_deauthenticate(
1081                                 wpa_s, WLAN_REASON_INVALID_IE);
1082                         return -1;
1083                 }
1084         }
1085
1086         p = data->assoc_info.resp_ies;
1087         l = data->assoc_info.resp_ies_len;
1088
1089 #ifdef CONFIG_WPS_STRICT
1090         if (wpa_s->current_ssid &&
1091             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1092                 struct wpabuf *wps;
1093                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1094                 if (wps == NULL) {
1095                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1096                                 "include WPS IE in (Re)Association Response");
1097                         return -1;
1098                 }
1099
1100                 if (wps_validate_assoc_resp(wps) < 0) {
1101                         wpabuf_free(wps);
1102                         wpa_supplicant_deauthenticate(
1103                                 wpa_s, WLAN_REASON_INVALID_IE);
1104                         return -1;
1105                 }
1106                 wpabuf_free(wps);
1107         }
1108 #endif /* CONFIG_WPS_STRICT */
1109
1110         /* Go through the IEs and make a copy of the MDIE, if present. */
1111         while (p && l >= 2) {
1112                 len = p[1] + 2;
1113                 if (len > l) {
1114                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1115                                     p, l);
1116                         break;
1117                 }
1118                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1119                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1120                         wpa_s->sme.ft_used = 1;
1121                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1122                                   MOBILITY_DOMAIN_ID_LEN);
1123                         break;
1124                 }
1125                 l -= len;
1126                 p += len;
1127         }
1128 #endif /* CONFIG_SME */
1129
1130         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1131                              data->assoc_info.resp_ies_len);
1132 #endif /* CONFIG_IEEE80211R */
1133
1134         /* WPA/RSN IE from Beacon/ProbeResp */
1135         p = data->assoc_info.beacon_ies;
1136         l = data->assoc_info.beacon_ies_len;
1137
1138         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1139          */
1140         wpa_found = rsn_found = 0;
1141         while (p && l >= 2) {
1142                 len = p[1] + 2;
1143                 if (len > l) {
1144                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1145                                     p, l);
1146                         break;
1147                 }
1148                 if (!wpa_found &&
1149                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1150                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1151                         wpa_found = 1;
1152                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1153                 }
1154
1155                 if (!rsn_found &&
1156                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1157                         rsn_found = 1;
1158                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1159                 }
1160
1161                 l -= len;
1162                 p += len;
1163         }
1164
1165         if (!wpa_found && data->assoc_info.beacon_ies)
1166                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1167         if (!rsn_found && data->assoc_info.beacon_ies)
1168                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1169         if (wpa_found || rsn_found)
1170                 wpa_s->ap_ies_from_associnfo = 1;
1171
1172         wpa_s->assoc_freq = data->assoc_info.freq;
1173
1174         return 0;
1175 }
1176
1177
1178 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1179                                        union wpa_event_data *data)
1180 {
1181         u8 bssid[ETH_ALEN];
1182         int ft_completed;
1183         int bssid_changed;
1184         struct wpa_driver_capa capa;
1185
1186 #ifdef CONFIG_AP
1187         if (wpa_s->ap_iface) {
1188                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1189                                     data->assoc_info.addr,
1190                                     data->assoc_info.req_ies,
1191                                     data->assoc_info.req_ies_len,
1192                                     data->assoc_info.reassoc);
1193                 return;
1194         }
1195 #endif /* CONFIG_AP */
1196
1197         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1198         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1199                 return;
1200
1201         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1202         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1203                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1204         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1205             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1206              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1207                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1208                         MACSTR, MAC2STR(bssid));
1209                 random_add_randomness(bssid, ETH_ALEN);
1210                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1211                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1212                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1213                 if (bssid_changed)
1214                         wpas_notify_bssid_changed(wpa_s);
1215
1216                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1217                         wpa_clear_keys(wpa_s, bssid);
1218                 }
1219                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1220                         wpa_supplicant_disassociate(
1221                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1222                         return;
1223                 }
1224                 if (wpa_s->current_ssid) {
1225                         struct wpa_bss *bss = NULL;
1226                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1227                         if (ssid->ssid_len > 0)
1228                                 bss = wpa_bss_get(wpa_s, bssid,
1229                                                   ssid->ssid, ssid->ssid_len);
1230                         if (!bss)
1231                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1232                         if (bss)
1233                                 wpa_s->current_bss = bss;
1234                 }
1235         }
1236
1237 #ifdef CONFIG_SME
1238         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1239         wpa_s->sme.prev_bssid_set = 1;
1240 #endif /* CONFIG_SME */
1241
1242         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1243         if (wpa_s->current_ssid) {
1244                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1245                  * initialized before association, but for other modes,
1246                  * initialize PC/SC here, if the current configuration needs
1247                  * smartcard or SIM/USIM. */
1248                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1249         }
1250         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1251         if (wpa_s->l2)
1252                 l2_packet_notify_auth_start(wpa_s->l2);
1253
1254         /*
1255          * Set portEnabled first to FALSE in order to get EAP state machine out
1256          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1257          * state machine may transit to AUTHENTICATING state based on obsolete
1258          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1259          * AUTHENTICATED without ever giving chance to EAP state machine to
1260          * reset the state.
1261          */
1262         if (!ft_completed) {
1263                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1264                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1265         }
1266         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1267                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1268         /* 802.1X::portControl = Auto */
1269         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1270         wpa_s->eapol_received = 0;
1271         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1272             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1273             (wpa_s->current_ssid &&
1274              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1275                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1276                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1277         } else if (!ft_completed) {
1278                 /* Timeout for receiving the first EAPOL packet */
1279                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1280         }
1281         wpa_supplicant_cancel_scan(wpa_s);
1282
1283         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1284             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1285                 /*
1286                  * We are done; the driver will take care of RSN 4-way
1287                  * handshake.
1288                  */
1289                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1290                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1291                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1292                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1293         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1294                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1295                 /*
1296                  * The driver will take care of RSN 4-way handshake, so we need
1297                  * to allow EAPOL supplicant to complete its work without
1298                  * waiting for WPA supplicant.
1299                  */
1300                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1301         } else if (ft_completed) {
1302                 /*
1303                  * FT protocol completed - make sure EAPOL state machine ends
1304                  * up in authenticated.
1305                  */
1306                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1307                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1308                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1309                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1310         }
1311
1312         if (wpa_s->pending_eapol_rx) {
1313                 struct os_time now, age;
1314                 os_get_time(&now);
1315                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1316                 if (age.sec == 0 && age.usec < 100000 &&
1317                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1318                     0) {
1319                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1320                                 "frame that was received just before "
1321                                 "association notification");
1322                         wpa_supplicant_rx_eapol(
1323                                 wpa_s, wpa_s->pending_eapol_rx_src,
1324                                 wpabuf_head(wpa_s->pending_eapol_rx),
1325                                 wpabuf_len(wpa_s->pending_eapol_rx));
1326                 }
1327                 wpabuf_free(wpa_s->pending_eapol_rx);
1328                 wpa_s->pending_eapol_rx = NULL;
1329         }
1330
1331 #ifdef CONFIG_BGSCAN
1332         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1333                 bgscan_deinit(wpa_s);
1334                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1335                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1336                                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to "
1337                                         "initialize bgscan");
1338                                 /*
1339                                  * Live without bgscan; it is only used as a
1340                                  * roaming optimization, so the initial
1341                                  * connection is not affected.
1342                                  */
1343                         } else
1344                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1345                 } else
1346                         wpa_s->bgscan_ssid = NULL;
1347         }
1348 #endif /* CONFIG_BGSCAN */
1349
1350         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1351              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1352             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1353             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1354                 /* Set static WEP keys again */
1355                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1356         }
1357
1358 #ifdef CONFIG_IBSS_RSN
1359         if (wpa_s->current_ssid &&
1360             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1361             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1362             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE)
1363                 ibss_rsn_connected(wpa_s->ibss_rsn);
1364 #endif /* CONFIG_IBSS_RSN */
1365 }
1366
1367
1368 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1369                                           u16 reason_code)
1370 {
1371         const u8 *bssid;
1372         int authenticating;
1373         u8 prev_pending_bssid[ETH_ALEN];
1374
1375         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1376         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1377
1378         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1379                 /*
1380                  * At least Host AP driver and a Prism3 card seemed to be
1381                  * generating streams of disconnected events when configuring
1382                  * IBSS for WPA-None. Ignore them for now.
1383                  */
1384                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1385                         "IBSS/WPA-None mode");
1386                 return;
1387         }
1388
1389         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1390             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1391                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1392                         "pre-shared key may be incorrect");
1393         }
1394         if (!wpa_s->auto_reconnect_disabled ||
1395             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1396                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
1397                         "reconnect (wps=%d)",
1398                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1399                 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1400                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1401         } else {
1402                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
1403                         "try to re-connect");
1404                 wpa_s->reassociate = 0;
1405                 wpa_s->disconnected = 1;
1406         }
1407         bssid = wpa_s->bssid;
1408         if (is_zero_ether_addr(bssid))
1409                 bssid = wpa_s->pending_bssid;
1410         wpas_connection_failed(wpa_s, bssid);
1411         wpa_sm_notify_disassoc(wpa_s->wpa);
1412         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1413                 " reason=%d",
1414                 MAC2STR(bssid), reason_code);
1415         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1416                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1417                 wpa_s->keys_cleared = 0;
1418                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1419         }
1420         wpa_supplicant_mark_disassoc(wpa_s);
1421         bgscan_deinit(wpa_s);
1422         wpa_s->bgscan_ssid = NULL;
1423
1424         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1425                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1426 }
1427
1428
1429 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1430 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1431                                                     void *sock_ctx)
1432 {
1433         struct wpa_supplicant *wpa_s = eloop_ctx;
1434
1435         if (!wpa_s->pending_mic_error_report)
1436                 return;
1437
1438         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1439         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1440         wpa_s->pending_mic_error_report = 0;
1441 }
1442 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1443
1444
1445 static void
1446 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1447                                          union wpa_event_data *data)
1448 {
1449         int pairwise;
1450         struct os_time t;
1451
1452         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1453         pairwise = (data && data->michael_mic_failure.unicast);
1454         os_get_time(&t);
1455         if ((wpa_s->last_michael_mic_error &&
1456              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1457             wpa_s->pending_mic_error_report) {
1458                 if (wpa_s->pending_mic_error_report) {
1459                         /*
1460                          * Send the pending MIC error report immediately since
1461                          * we are going to start countermeasures and AP better
1462                          * do the same.
1463                          */
1464                         wpa_sm_key_request(wpa_s->wpa, 1,
1465                                            wpa_s->pending_mic_error_pairwise);
1466                 }
1467
1468                 /* Send the new MIC error report immediately since we are going
1469                  * to start countermeasures and AP better do the same.
1470                  */
1471                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1472
1473                 /* initialize countermeasures */
1474                 wpa_s->countermeasures = 1;
1475                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1476
1477                 /*
1478                  * Need to wait for completion of request frame. We do not get
1479                  * any callback for the message completion, so just wait a
1480                  * short while and hope for the best. */
1481                 os_sleep(0, 10000);
1482
1483                 wpa_drv_set_countermeasures(wpa_s, 1);
1484                 wpa_supplicant_deauthenticate(wpa_s,
1485                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1486                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1487                                      wpa_s, NULL);
1488                 eloop_register_timeout(60, 0,
1489                                        wpa_supplicant_stop_countermeasures,
1490                                        wpa_s, NULL);
1491                 /* TODO: mark the AP rejected for 60 second. STA is
1492                  * allowed to associate with another AP.. */
1493         } else {
1494 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1495                 if (wpa_s->mic_errors_seen) {
1496                         /*
1497                          * Reduce the effectiveness of Michael MIC error
1498                          * reports as a means for attacking against TKIP if
1499                          * more than one MIC failure is noticed with the same
1500                          * PTK. We delay the transmission of the reports by a
1501                          * random time between 0 and 60 seconds in order to
1502                          * force the attacker wait 60 seconds before getting
1503                          * the information on whether a frame resulted in a MIC
1504                          * failure.
1505                          */
1506                         u8 rval[4];
1507                         int sec;
1508
1509                         if (os_get_random(rval, sizeof(rval)) < 0)
1510                                 sec = os_random() % 60;
1511                         else
1512                                 sec = WPA_GET_BE32(rval) % 60;
1513                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1514                                 "report %d seconds", sec);
1515                         wpa_s->pending_mic_error_report = 1;
1516                         wpa_s->pending_mic_error_pairwise = pairwise;
1517                         eloop_cancel_timeout(
1518                                 wpa_supplicant_delayed_mic_error_report,
1519                                 wpa_s, NULL);
1520                         eloop_register_timeout(
1521                                 sec, os_random() % 1000000,
1522                                 wpa_supplicant_delayed_mic_error_report,
1523                                 wpa_s, NULL);
1524                 } else {
1525                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1526                 }
1527 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1528                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1529 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1530         }
1531         wpa_s->last_michael_mic_error = t.sec;
1532         wpa_s->mic_errors_seen++;
1533 }
1534
1535
1536 #ifdef CONFIG_TERMINATE_ONLASTIF
1537 static int any_interfaces(struct wpa_supplicant *head)
1538 {
1539         struct wpa_supplicant *wpa_s;
1540
1541         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1542                 if (!wpa_s->interface_removed)
1543                         return 1;
1544         return 0;
1545 }
1546 #endif /* CONFIG_TERMINATE_ONLASTIF */
1547
1548
1549 static void
1550 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1551                                       union wpa_event_data *data)
1552 {
1553         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1554                 return;
1555
1556         switch (data->interface_status.ievent) {
1557         case EVENT_INTERFACE_ADDED:
1558                 if (!wpa_s->interface_removed)
1559                         break;
1560                 wpa_s->interface_removed = 0;
1561                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1562                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1563                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1564                                 "driver after interface was added");
1565                 }
1566                 break;
1567         case EVENT_INTERFACE_REMOVED:
1568                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1569                 wpa_s->interface_removed = 1;
1570                 wpa_supplicant_mark_disassoc(wpa_s);
1571                 l2_packet_deinit(wpa_s->l2);
1572                 wpa_s->l2 = NULL;
1573 #ifdef CONFIG_TERMINATE_ONLASTIF
1574                 /* check if last interface */
1575                 if (!any_interfaces(wpa_s->global->ifaces))
1576                         eloop_terminate();
1577 #endif /* CONFIG_TERMINATE_ONLASTIF */
1578                 break;
1579         }
1580 }
1581
1582
1583 #ifdef CONFIG_PEERKEY
1584 static void
1585 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1586                               union wpa_event_data *data)
1587 {
1588         if (data == NULL)
1589                 return;
1590         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1591 }
1592 #endif /* CONFIG_PEERKEY */
1593
1594
1595 #ifdef CONFIG_TDLS
1596 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1597                                       union wpa_event_data *data)
1598 {
1599         if (data == NULL)
1600                 return;
1601         switch (data->tdls.oper) {
1602         case TDLS_REQUEST_SETUP:
1603                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1604                 break;
1605         case TDLS_REQUEST_TEARDOWN:
1606                 /* request from driver to add FTIE */
1607                 wpa_tdls_recv_teardown_notify(wpa_s->wpa, data->tdls.peer,
1608                                               data->tdls.reason_code);
1609                 break;
1610         }
1611 }
1612 #endif /* CONFIG_TDLS */
1613
1614
1615 #ifdef CONFIG_IEEE80211R
1616 static void
1617 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1618                                  union wpa_event_data *data)
1619 {
1620         if (data == NULL)
1621                 return;
1622
1623         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1624                                     data->ft_ies.ies_len,
1625                                     data->ft_ies.ft_action,
1626                                     data->ft_ies.target_ap,
1627                                     data->ft_ies.ric_ies,
1628                                     data->ft_ies.ric_ies_len) < 0) {
1629                 /* TODO: prevent MLME/driver from trying to associate? */
1630         }
1631 }
1632 #endif /* CONFIG_IEEE80211R */
1633
1634
1635 #ifdef CONFIG_IBSS_RSN
1636 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1637                                                 union wpa_event_data *data)
1638 {
1639         struct wpa_ssid *ssid;
1640         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1641                 return;
1642         if (data == NULL)
1643                 return;
1644         ssid = wpa_s->current_ssid;
1645         if (ssid == NULL)
1646                 return;
1647         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1648                 return;
1649
1650         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1651 }
1652 #endif /* CONFIG_IBSS_RSN */
1653
1654
1655 #ifdef CONFIG_IEEE80211R
1656 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1657                          size_t len)
1658 {
1659         const u8 *sta_addr, *target_ap_addr;
1660         u16 status;
1661
1662         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1663         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1664                 return; /* only SME case supported for now */
1665         if (len < 1 + 2 * ETH_ALEN + 2)
1666                 return;
1667         if (data[0] != 2)
1668                 return; /* Only FT Action Response is supported for now */
1669         sta_addr = data + 1;
1670         target_ap_addr = data + 1 + ETH_ALEN;
1671         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1672         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1673                 MACSTR " TargetAP " MACSTR " status %u",
1674                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1675
1676         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1677                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1678                         " in FT Action Response", MAC2STR(sta_addr));
1679                 return;
1680         }
1681
1682         if (status) {
1683                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1684                         "failure (status code %d)", status);
1685                 /* TODO: report error to FT code(?) */
1686                 return;
1687         }
1688
1689         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1690                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1691                                     target_ap_addr, NULL, 0) < 0)
1692                 return;
1693
1694 #ifdef CONFIG_SME
1695         {
1696                 struct wpa_bss *bss;
1697                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1698                 if (bss)
1699                         wpa_s->sme.freq = bss->freq;
1700                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1701                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1702                               WLAN_AUTH_FT);
1703         }
1704 #endif /* CONFIG_SME */
1705 }
1706 #endif /* CONFIG_IEEE80211R */
1707
1708
1709 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1710                                                struct unprot_deauth *e)
1711 {
1712 #ifdef CONFIG_IEEE80211W
1713         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1714                    "dropped: " MACSTR " -> " MACSTR
1715                    " (reason code %u)",
1716                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1717         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1718 #endif /* CONFIG_IEEE80211W */
1719 }
1720
1721
1722 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1723                                                  struct unprot_disassoc *e)
1724 {
1725 #ifdef CONFIG_IEEE80211W
1726         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1727                    "dropped: " MACSTR " -> " MACSTR
1728                    " (reason code %u)",
1729                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1730         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1731 #endif /* CONFIG_IEEE80211W */
1732 }
1733
1734
1735 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1736                           union wpa_event_data *data)
1737 {
1738         struct wpa_supplicant *wpa_s = ctx;
1739         u16 reason_code = 0;
1740
1741         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1742             event != EVENT_INTERFACE_ENABLED &&
1743             event != EVENT_INTERFACE_STATUS) {
1744                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
1745                         "disabled", event);
1746                 return;
1747         }
1748
1749         wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
1750                 event, wpa_s->ifname);
1751
1752         switch (event) {
1753         case EVENT_AUTH:
1754                 sme_event_auth(wpa_s, data);
1755                 break;
1756         case EVENT_ASSOC:
1757                 wpa_supplicant_event_assoc(wpa_s, data);
1758                 break;
1759         case EVENT_DISASSOC:
1760                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
1761                 if (data) {
1762                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1763                                 data->disassoc_info.reason_code);
1764                         if (data->disassoc_info.addr)
1765                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
1766                                         MAC2STR(data->disassoc_info.addr));
1767                 }
1768 #ifdef CONFIG_AP
1769                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1770                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1771                                                data->disassoc_info.addr);
1772                         break;
1773                 }
1774 #endif /* CONFIG_AP */
1775                 if (data) {
1776                         reason_code = data->disassoc_info.reason_code;
1777                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1778                                     data->disassoc_info.ie,
1779                                     data->disassoc_info.ie_len);
1780 #ifdef CONFIG_P2P
1781                         wpas_p2p_disassoc_notif(
1782                                 wpa_s, data->disassoc_info.addr, reason_code,
1783                                 data->disassoc_info.ie,
1784                                 data->disassoc_info.ie_len);
1785 #endif /* CONFIG_P2P */
1786                 }
1787                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1788                         sme_event_disassoc(wpa_s, data);
1789                 /* fall through */
1790         case EVENT_DEAUTH:
1791                 if (event == EVENT_DEAUTH) {
1792                         wpa_dbg(wpa_s, MSG_DEBUG,
1793                                 "Deauthentication notification");
1794                         if (data) {
1795                                 reason_code = data->deauth_info.reason_code;
1796                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1797                                         data->deauth_info.reason_code);
1798                                 if (data->deauth_info.addr) {
1799                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
1800                                                 MACSTR,
1801                                                 MAC2STR(data->deauth_info.
1802                                                         addr));
1803                                 }
1804                                 wpa_hexdump(MSG_DEBUG,
1805                                             "Deauthentication frame IE(s)",
1806                                             data->deauth_info.ie,
1807                                             data->deauth_info.ie_len);
1808 #ifdef CONFIG_P2P
1809                                 wpas_p2p_deauth_notif(
1810                                         wpa_s, data->deauth_info.addr,
1811                                         reason_code,
1812                                         data->deauth_info.ie,
1813                                         data->deauth_info.ie_len);
1814 #endif /* CONFIG_P2P */
1815                         }
1816                 }
1817 #ifdef CONFIG_AP
1818                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1819                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1820                                                data->deauth_info.addr);
1821                         break;
1822                 }
1823 #endif /* CONFIG_AP */
1824                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1825                 break;
1826         case EVENT_MICHAEL_MIC_FAILURE:
1827                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1828                 break;
1829 #ifndef CONFIG_NO_SCAN_PROCESSING
1830         case EVENT_SCAN_RESULTS:
1831                 wpa_supplicant_event_scan_results(wpa_s, data);
1832                 break;
1833 #endif /* CONFIG_NO_SCAN_PROCESSING */
1834         case EVENT_ASSOCINFO:
1835                 wpa_supplicant_event_associnfo(wpa_s, data);
1836                 break;
1837         case EVENT_INTERFACE_STATUS:
1838                 wpa_supplicant_event_interface_status(wpa_s, data);
1839                 break;
1840         case EVENT_PMKID_CANDIDATE:
1841                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1842                 break;
1843 #ifdef CONFIG_PEERKEY
1844         case EVENT_STKSTART:
1845                 wpa_supplicant_event_stkstart(wpa_s, data);
1846                 break;
1847 #endif /* CONFIG_PEERKEY */
1848 #ifdef CONFIG_TDLS
1849         case EVENT_TDLS:
1850                 wpa_supplicant_event_tdls(wpa_s, data);
1851                 break;
1852 #endif /* CONFIG_TDLS */
1853 #ifdef CONFIG_IEEE80211R
1854         case EVENT_FT_RESPONSE:
1855                 wpa_supplicant_event_ft_response(wpa_s, data);
1856                 break;
1857 #endif /* CONFIG_IEEE80211R */
1858 #ifdef CONFIG_IBSS_RSN
1859         case EVENT_IBSS_RSN_START:
1860                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1861                 break;
1862 #endif /* CONFIG_IBSS_RSN */
1863         case EVENT_ASSOC_REJECT:
1864                 if (data->assoc_reject.bssid)
1865                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1866                                 "bssid=" MACSTR " status_code=%u",
1867                                 MAC2STR(data->assoc_reject.bssid),
1868                                 data->assoc_reject.status_code);
1869                 else
1870                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1871                                 "status_code=%u",
1872                                 data->assoc_reject.status_code);
1873                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1874                         sme_event_assoc_reject(wpa_s, data);
1875                 break;
1876         case EVENT_AUTH_TIMED_OUT:
1877                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1878                         sme_event_auth_timed_out(wpa_s, data);
1879                 break;
1880         case EVENT_ASSOC_TIMED_OUT:
1881                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1882                         sme_event_assoc_timed_out(wpa_s, data);
1883                 break;
1884 #ifdef CONFIG_AP
1885         case EVENT_TX_STATUS:
1886                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
1887                         " type=%d stype=%d",
1888                         MAC2STR(data->tx_status.dst),
1889                         data->tx_status.type, data->tx_status.stype);
1890                 if (wpa_s->ap_iface == NULL) {
1891 #ifdef CONFIG_P2P
1892                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1893                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1894                                 wpas_send_action_tx_status(
1895                                         wpa_s, data->tx_status.dst,
1896                                         data->tx_status.data,
1897                                         data->tx_status.data_len,
1898                                         data->tx_status.ack ?
1899                                         P2P_SEND_ACTION_SUCCESS :
1900                                         P2P_SEND_ACTION_NO_ACK);
1901 #endif /* CONFIG_P2P */
1902                         break;
1903                 }
1904 #ifdef CONFIG_P2P
1905                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
1906                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
1907                 /*
1908                  * Catch TX status events for Action frames we sent via group
1909                  * interface in GO mode.
1910                  */
1911                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1912                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1913                     os_memcmp(wpa_s->parent->pending_action_dst,
1914                               data->tx_status.dst, ETH_ALEN) == 0) {
1915                         wpas_send_action_tx_status(
1916                                 wpa_s->parent, data->tx_status.dst,
1917                                 data->tx_status.data,
1918                                 data->tx_status.data_len,
1919                                 data->tx_status.ack ?
1920                                 P2P_SEND_ACTION_SUCCESS :
1921                                 P2P_SEND_ACTION_NO_ACK);
1922                         break;
1923                 }
1924 #endif /* CONFIG_P2P */
1925                 switch (data->tx_status.type) {
1926                 case WLAN_FC_TYPE_MGMT:
1927                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1928                                       data->tx_status.data_len,
1929                                       data->tx_status.stype,
1930                                       data->tx_status.ack);
1931                         break;
1932                 case WLAN_FC_TYPE_DATA:
1933                         ap_tx_status(wpa_s, data->tx_status.dst,
1934                                      data->tx_status.data,
1935                                      data->tx_status.data_len,
1936                                      data->tx_status.ack);
1937                         break;
1938                 }
1939                 break;
1940         case EVENT_RX_FROM_UNKNOWN:
1941                 if (wpa_s->ap_iface == NULL)
1942                         break;
1943                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1944                                        data->rx_from_unknown.len);
1945                 break;
1946         case EVENT_RX_MGMT:
1947                 if (wpa_s->ap_iface == NULL) {
1948 #ifdef CONFIG_P2P
1949                         u16 fc, stype;
1950                         const struct ieee80211_mgmt *mgmt;
1951                         mgmt = (const struct ieee80211_mgmt *)
1952                                 data->rx_mgmt.frame;
1953                         fc = le_to_host16(mgmt->frame_control);
1954                         stype = WLAN_FC_GET_STYPE(fc);
1955                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1956                             data->rx_mgmt.frame_len > 24) {
1957                                 const u8 *src = mgmt->sa;
1958                                 const u8 *ie = mgmt->u.probe_req.variable;
1959                                 size_t ie_len = data->rx_mgmt.frame_len -
1960                                         (mgmt->u.probe_req.variable -
1961                                          data->rx_mgmt.frame);
1962                                 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1963                                 break;
1964                         }
1965 #endif /* CONFIG_P2P */
1966                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
1967                                 "management frame in non-AP mode");
1968                         break;
1969                 }
1970                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1971                 break;
1972 #endif /* CONFIG_AP */
1973         case EVENT_RX_ACTION:
1974                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
1975                         " Category=%u DataLen=%d freq=%d MHz",
1976                         MAC2STR(data->rx_action.sa),
1977                         data->rx_action.category, (int) data->rx_action.len,
1978                         data->rx_action.freq);
1979 #ifdef CONFIG_IEEE80211R
1980                 if (data->rx_action.category == WLAN_ACTION_FT) {
1981                         ft_rx_action(wpa_s, data->rx_action.data,
1982                                      data->rx_action.len);
1983                         break;
1984                 }
1985 #endif /* CONFIG_IEEE80211R */
1986 #ifdef CONFIG_IEEE80211W
1987 #ifdef CONFIG_SME
1988                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
1989                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
1990                                         data->rx_action.data,
1991                                         data->rx_action.len);
1992                         break;
1993                 }
1994 #endif /* CONFIG_SME */
1995 #endif /* CONFIG_IEEE80211W */
1996 #ifdef CONFIG_P2P
1997                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
1998                                    data->rx_action.sa,
1999                                    data->rx_action.bssid,
2000                                    data->rx_action.category,
2001                                    data->rx_action.data,
2002                                    data->rx_action.len, data->rx_action.freq);
2003 #endif /* CONFIG_P2P */
2004                 break;
2005         case EVENT_RX_PROBE_REQ:
2006 #ifdef CONFIG_AP
2007                 if (wpa_s->ap_iface) {
2008                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2009                                              data->rx_probe_req.sa,
2010                                              data->rx_probe_req.ie,
2011                                              data->rx_probe_req.ie_len);
2012                         break;
2013                 }
2014 #endif /* CONFIG_AP */
2015 #ifdef CONFIG_P2P
2016                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2017                                       data->rx_probe_req.ie,
2018                                       data->rx_probe_req.ie_len);
2019 #endif /* CONFIG_P2P */
2020                 break;
2021 #ifdef CONFIG_P2P
2022         case EVENT_REMAIN_ON_CHANNEL:
2023                 wpas_p2p_remain_on_channel_cb(
2024                         wpa_s, data->remain_on_channel.freq,
2025                         data->remain_on_channel.duration);
2026                 break;
2027         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2028                 wpas_p2p_cancel_remain_on_channel_cb(
2029                         wpa_s, data->remain_on_channel.freq);
2030                 break;
2031         case EVENT_P2P_DEV_FOUND: {
2032                 struct p2p_peer_info peer_info;
2033
2034                 os_memset(&peer_info, 0, sizeof(peer_info));
2035                 if (data->p2p_dev_found.dev_addr)
2036                         os_memcpy(peer_info.p2p_device_addr,
2037                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2038                 if (data->p2p_dev_found.pri_dev_type)
2039                         os_memcpy(peer_info.pri_dev_type,
2040                                   data->p2p_dev_found.pri_dev_type,
2041                                   sizeof(peer_info.pri_dev_type));
2042                 if (data->p2p_dev_found.dev_name)
2043                         os_strlcpy(peer_info.device_name,
2044                                    data->p2p_dev_found.dev_name,
2045                                    sizeof(peer_info.device_name));
2046                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2047                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2048                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2049
2050                 /*
2051                  * FIX: new_device=1 is not necessarily correct. We should
2052                  * maintain a P2P peer database in wpa_supplicant and update
2053                  * this information based on whether the peer is truly new.
2054                  */
2055                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2056                 break;
2057                 }
2058         case EVENT_P2P_GO_NEG_REQ_RX:
2059                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2060                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2061                 break;
2062         case EVENT_P2P_GO_NEG_COMPLETED:
2063                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2064                 break;
2065         case EVENT_P2P_PROV_DISC_REQUEST:
2066                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2067                                    data->p2p_prov_disc_req.config_methods,
2068                                    data->p2p_prov_disc_req.dev_addr,
2069                                    data->p2p_prov_disc_req.pri_dev_type,
2070                                    data->p2p_prov_disc_req.dev_name,
2071                                    data->p2p_prov_disc_req.supp_config_methods,
2072                                    data->p2p_prov_disc_req.dev_capab,
2073                                    data->p2p_prov_disc_req.group_capab);
2074                 break;
2075         case EVENT_P2P_PROV_DISC_RESPONSE:
2076                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2077                                     data->p2p_prov_disc_resp.config_methods);
2078                 break;
2079         case EVENT_P2P_SD_REQUEST:
2080                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2081                                 data->p2p_sd_req.sa,
2082                                 data->p2p_sd_req.dialog_token,
2083                                 data->p2p_sd_req.update_indic,
2084                                 data->p2p_sd_req.tlvs,
2085                                 data->p2p_sd_req.tlvs_len);
2086                 break;
2087         case EVENT_P2P_SD_RESPONSE:
2088                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2089                                  data->p2p_sd_resp.update_indic,
2090                                  data->p2p_sd_resp.tlvs,
2091                                  data->p2p_sd_resp.tlvs_len);
2092                 break;
2093 #endif /* CONFIG_P2P */
2094 #ifdef CONFIG_CLIENT_MLME
2095         case EVENT_MLME_RX: {
2096                 struct ieee80211_rx_status rx_status;
2097                 os_memset(&rx_status, 0, sizeof(rx_status));
2098                 rx_status.freq = data->mlme_rx.freq;
2099                 rx_status.channel = data->mlme_rx.channel;
2100                 rx_status.ssi = data->mlme_rx.ssi;
2101                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
2102                                  &rx_status);
2103                 break;
2104         }
2105 #endif /* CONFIG_CLIENT_MLME */
2106         case EVENT_EAPOL_RX:
2107                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2108                                         data->eapol_rx.data,
2109                                         data->eapol_rx.data_len);
2110                 break;
2111         case EVENT_SIGNAL_CHANGE:
2112                 bgscan_notify_signal_change(
2113                         wpa_s, data->signal_change.above_threshold,
2114                         data->signal_change.current_signal,
2115                         data->signal_change.current_noise,
2116                         data->signal_change.current_txrate);
2117                 break;
2118         case EVENT_INTERFACE_ENABLED:
2119                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2120                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2121 #ifdef CONFIG_AP
2122                         if (!wpa_s->ap_iface) {
2123                                 wpa_supplicant_set_state(wpa_s,
2124                                                          WPA_DISCONNECTED);
2125                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2126                         } else
2127                                 wpa_supplicant_set_state(wpa_s,
2128                                                          WPA_COMPLETED);
2129 #else /* CONFIG_AP */
2130                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2131                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2132 #endif /* CONFIG_AP */
2133                 }
2134                 break;
2135         case EVENT_INTERFACE_DISABLED:
2136                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2137                 wpa_supplicant_mark_disassoc(wpa_s);
2138                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2139                 break;
2140         case EVENT_CHANNEL_LIST_CHANGED:
2141                 if (wpa_s->drv_priv == NULL)
2142                         break; /* Ignore event during drv initialization */
2143 #ifdef CONFIG_P2P
2144                 wpas_p2p_update_channel_list(wpa_s);
2145 #endif /* CONFIG_P2P */
2146                 break;
2147         case EVENT_INTERFACE_UNAVAILABLE:
2148 #ifdef CONFIG_P2P
2149                 wpas_p2p_interface_unavailable(wpa_s);
2150 #endif /* CONFIG_P2P */
2151                 break;
2152         case EVENT_BEST_CHANNEL:
2153                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2154                         "(%d %d %d)",
2155                         data->best_chan.freq_24, data->best_chan.freq_5,
2156                         data->best_chan.freq_overall);
2157                 wpa_s->best_24_freq = data->best_chan.freq_24;
2158                 wpa_s->best_5_freq = data->best_chan.freq_5;
2159                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2160 #ifdef CONFIG_P2P
2161                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2162                                               data->best_chan.freq_5,
2163                                               data->best_chan.freq_overall);
2164 #endif /* CONFIG_P2P */
2165                 break;
2166         case EVENT_UNPROT_DEAUTH:
2167                 wpa_supplicant_event_unprot_deauth(wpa_s,
2168                                                    &data->unprot_deauth);
2169                 break;
2170         case EVENT_UNPROT_DISASSOC:
2171                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2172                                                      &data->unprot_disassoc);
2173                 break;
2174         case EVENT_STATION_LOW_ACK:
2175 #ifdef CONFIG_AP
2176                 if (wpa_s->ap_iface && data)
2177                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2178                                                   data->low_ack.addr);
2179 #endif /* CONFIG_AP */
2180                 break;
2181         default:
2182                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2183                 break;
2184         }
2185 }