P2P: Optimize scan timeouts for group formation
[libeap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2010, 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 "notify.h"
32 #include "common/ieee802_11_defs.h"
33 #include "common/ieee802_11_common.h"
34 #include "blacklist.h"
35 #include "wpas_glue.h"
36 #include "wps_supplicant.h"
37 #include "ibss_rsn.h"
38 #include "sme.h"
39 #include "bgscan.h"
40 #include "ap.h"
41 #include "bss.h"
42 #include "mlme.h"
43 #include "scan.h"
44
45
46 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
47 {
48         struct wpa_ssid *ssid, *old_ssid;
49
50         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
51                 return 0;
52
53         wpa_printf(MSG_DEBUG, "Select network based on association "
54                    "information");
55         ssid = wpa_supplicant_get_ssid(wpa_s);
56         if (ssid == NULL) {
57                 wpa_printf(MSG_INFO, "No network configuration found for the "
58                            "current AP");
59                 return -1;
60         }
61
62         if (ssid->disabled) {
63                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
64                 return -1;
65         }
66
67         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
68                    "AP");
69         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
70                               WPA_KEY_MGMT_WPA_NONE |
71                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
72                               WPA_KEY_MGMT_PSK_SHA256 |
73                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
74                 u8 wpa_ie[80];
75                 size_t wpa_ie_len = sizeof(wpa_ie);
76                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
77                                           wpa_ie, &wpa_ie_len);
78         } else {
79                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
80         }
81
82         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
83                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
84         old_ssid = wpa_s->current_ssid;
85         wpa_s->current_ssid = ssid;
86         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
87         wpa_supplicant_initiate_eapol(wpa_s);
88         if (old_ssid != wpa_s->current_ssid)
89                 wpas_notify_network_changed(wpa_s);
90
91         return 0;
92 }
93
94
95 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
96                                                 void *sock_ctx)
97 {
98         struct wpa_supplicant *wpa_s = eloop_ctx;
99
100         if (wpa_s->countermeasures) {
101                 wpa_s->countermeasures = 0;
102                 wpa_drv_set_countermeasures(wpa_s, 0);
103                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
104                 wpa_supplicant_req_scan(wpa_s, 0, 0);
105         }
106 }
107
108
109 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
110 {
111         int bssid_changed;
112
113         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
114                 return;
115
116         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
117         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
118         os_memset(wpa_s->bssid, 0, ETH_ALEN);
119         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
120         wpa_s->current_bss = NULL;
121         if (bssid_changed)
122                 wpas_notify_bssid_changed(wpa_s);
123
124         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
125         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
126         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
127                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
128         wpa_s->ap_ies_from_associnfo = 0;
129 }
130
131
132 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
133 {
134         struct wpa_ie_data ie;
135         int pmksa_set = -1;
136         size_t i;
137
138         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
139             ie.pmkid == NULL)
140                 return;
141
142         for (i = 0; i < ie.num_pmkid; i++) {
143                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
144                                                     ie.pmkid + i * PMKID_LEN,
145                                                     NULL, NULL, 0);
146                 if (pmksa_set == 0) {
147                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
148                         break;
149                 }
150         }
151
152         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
153                    "cache", pmksa_set == 0 ? "" : "not ");
154 }
155
156
157 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
158                                                  union wpa_event_data *data)
159 {
160         if (data == NULL) {
161                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
162                 return;
163         }
164         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
165                    " index=%d preauth=%d",
166                    MAC2STR(data->pmkid_candidate.bssid),
167                    data->pmkid_candidate.index,
168                    data->pmkid_candidate.preauth);
169
170         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
171                             data->pmkid_candidate.index,
172                             data->pmkid_candidate.preauth);
173 }
174
175
176 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
177 {
178         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
179             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
180                 return 0;
181
182 #ifdef IEEE8021X_EAPOL
183         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
184             wpa_s->current_ssid &&
185             !(wpa_s->current_ssid->eapol_flags &
186               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
187                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
188                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
189                  * plaintext or static WEP keys). */
190                 return 0;
191         }
192 #endif /* IEEE8021X_EAPOL */
193
194         return 1;
195 }
196
197
198 /**
199  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
200  * @wpa_s: pointer to wpa_supplicant data
201  * @ssid: Configuration data for the network
202  * Returns: 0 on success, -1 on failure
203  *
204  * This function is called when starting authentication with a network that is
205  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
206  */
207 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
208                               struct wpa_ssid *ssid)
209 {
210 #ifdef IEEE8021X_EAPOL
211         int aka = 0, sim = 0, type;
212
213         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
214                 return 0;
215
216         if (ssid->eap.eap_methods == NULL) {
217                 sim = 1;
218                 aka = 1;
219         } else {
220                 struct eap_method_type *eap = ssid->eap.eap_methods;
221                 while (eap->vendor != EAP_VENDOR_IETF ||
222                        eap->method != EAP_TYPE_NONE) {
223                         if (eap->vendor == EAP_VENDOR_IETF) {
224                                 if (eap->method == EAP_TYPE_SIM)
225                                         sim = 1;
226                                 else if (eap->method == EAP_TYPE_AKA)
227                                         aka = 1;
228                         }
229                         eap++;
230                 }
231         }
232
233         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
234                 sim = 0;
235         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
236                 aka = 0;
237
238         if (!sim && !aka) {
239                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
240                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
241                 return 0;
242         }
243
244         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
245                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
246         if (sim && aka)
247                 type = SCARD_TRY_BOTH;
248         else if (aka)
249                 type = SCARD_USIM_ONLY;
250         else
251                 type = SCARD_GSM_SIM_ONLY;
252
253         wpa_s->scard = scard_init(type);
254         if (wpa_s->scard == NULL) {
255                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
256                            "(pcsc-lite)");
257                 return -1;
258         }
259         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
260         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
261 #endif /* IEEE8021X_EAPOL */
262
263         return 0;
264 }
265
266
267 #ifndef CONFIG_NO_SCAN_PROCESSING
268 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
269                                         struct wpa_ssid *ssid)
270 {
271         int i, privacy = 0;
272
273         if (ssid->mixed_cell)
274                 return 1;
275
276 #ifdef CONFIG_WPS
277         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
278                 return 1;
279 #endif /* CONFIG_WPS */
280
281         for (i = 0; i < NUM_WEP_KEYS; i++) {
282                 if (ssid->wep_key_len[i]) {
283                         privacy = 1;
284                         break;
285                 }
286         }
287 #ifdef IEEE8021X_EAPOL
288         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
289             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
290                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
291                 privacy = 1;
292 #endif /* IEEE8021X_EAPOL */
293
294         if (bss->caps & IEEE80211_CAP_PRIVACY)
295                 return privacy;
296         return !privacy;
297 }
298
299
300 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
301                                          struct wpa_ssid *ssid,
302                                          struct wpa_scan_res *bss)
303 {
304         struct wpa_ie_data ie;
305         int proto_match = 0;
306         const u8 *rsn_ie, *wpa_ie;
307         int ret;
308
309         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
310         if (ret >= 0)
311                 return ret;
312
313         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
314         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
315                 proto_match++;
316
317                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
318                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
319                         break;
320                 }
321                 if (!(ie.proto & ssid->proto)) {
322                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
323                                    "mismatch");
324                         break;
325                 }
326
327                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
328                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
329                                    "mismatch");
330                         break;
331                 }
332
333                 if (!(ie.group_cipher & ssid->group_cipher)) {
334                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
335                                    "mismatch");
336                         break;
337                 }
338
339                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
340                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
341                                    "mismatch");
342                         break;
343                 }
344
345 #ifdef CONFIG_IEEE80211W
346                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
347                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
348                         wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
349                                    "protection");
350                         break;
351                 }
352 #endif /* CONFIG_IEEE80211W */
353
354                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
355                 return 1;
356         }
357
358         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
359         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
360                 proto_match++;
361
362                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
363                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
364                         break;
365                 }
366                 if (!(ie.proto & ssid->proto)) {
367                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
368                                    "mismatch");
369                         break;
370                 }
371
372                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
373                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
374                                    "mismatch");
375                         break;
376                 }
377
378                 if (!(ie.group_cipher & ssid->group_cipher)) {
379                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
380                                    "mismatch");
381                         break;
382                 }
383
384                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
385                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
386                                    "mismatch");
387                         break;
388                 }
389
390                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
391                 return 1;
392         }
393
394         if (proto_match == 0)
395                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
396
397         return 0;
398 }
399
400
401 static int freq_allowed(int *freqs, int freq)
402 {
403         int i;
404
405         if (freqs == NULL)
406                 return 1;
407
408         for (i = 0; freqs[i]; i++)
409                 if (freqs[i] == freq)
410                         return 1;
411         return 0;
412 }
413
414
415 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
416                                             int i, struct wpa_scan_res *bss,
417                                             struct wpa_ssid *group)
418 {
419         const u8 *ssid_;
420         u8 wpa_ie_len, rsn_ie_len, ssid_len;
421         int wpa;
422         struct wpa_blacklist *e;
423         const u8 *ie;
424         struct wpa_ssid *ssid;
425
426         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
427         ssid_ = ie ? ie + 2 : (u8 *) "";
428         ssid_len = ie ? ie[1] : 0;
429
430         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
431         wpa_ie_len = ie ? ie[1] : 0;
432
433         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
434         rsn_ie_len = ie ? ie[1] : 0;
435
436         wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
437                    "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
438                    i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
439                    wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
440                    wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
441                    " wps" : "");
442
443         e = wpa_blacklist_get(wpa_s, bss->bssid);
444         if (e && e->count > 1) {
445                 wpa_printf(MSG_DEBUG, "   skip - blacklisted");
446                 return 0;
447         }
448
449         if (ssid_len == 0) {
450                 wpa_printf(MSG_DEBUG, "   skip - SSID not known");
451                 return 0;
452         }
453
454         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
455
456         for (ssid = group; ssid; ssid = ssid->pnext) {
457                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
458
459                 if (ssid->disabled) {
460                         wpa_printf(MSG_DEBUG, "   skip - disabled");
461                         continue;
462                 }
463
464 #ifdef CONFIG_WPS
465                 if (wpa && ssid->ssid_len == 0 &&
466                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
467                         check_ssid = 0;
468
469                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
470                         /* Only allow wildcard SSID match if an AP
471                          * advertises active WPS operation that matches
472                          * with our mode. */
473                         check_ssid = 1;
474                         if (ssid->ssid_len == 0 &&
475                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
476                                 check_ssid = 0;
477                 }
478 #endif /* CONFIG_WPS */
479
480                 if (check_ssid &&
481                     (ssid_len != ssid->ssid_len ||
482                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
483                         wpa_printf(MSG_DEBUG, "   skip - SSID mismatch");
484                         continue;
485                 }
486
487                 if (ssid->bssid_set &&
488                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
489                         wpa_printf(MSG_DEBUG, "   skip - BSSID mismatch");
490                         continue;
491                 }
492
493                 if (wpa && !wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
494                         continue;
495
496                 if (!wpa &&
497                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
498                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
499                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
500                         wpa_printf(MSG_DEBUG, "   skip - non-WPA network not "
501                                    "allowed");
502                         continue;
503                 }
504
505                 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
506                         wpa_printf(MSG_DEBUG, "   skip - privacy mismatch");
507                         continue;
508                 }
509
510                 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
511                         wpa_printf(MSG_DEBUG, "   skip - IBSS (adhoc) "
512                                    "network");
513                         continue;
514                 }
515
516                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
517                         wpa_printf(MSG_DEBUG, "   skip - frequency not "
518                                    "allowed");
519                         continue;
520                 }
521
522 #ifdef CONFIG_P2P
523                 /*
524                  * TODO: skip the AP if its P2P IE has Group Formation
525                  * bit set in the P2P Group Capability Bitmap and we
526                  * are not in Group Formation with that device.
527                  */
528 #endif /* CONFIG_P2P */
529
530                 /* Matching configuration found */
531                 return ssid;
532         }
533
534         /* No matching configuration found */
535         return 0;
536 }
537
538
539 static struct wpa_bss *
540 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
541                           struct wpa_scan_results *scan_res,
542                           struct wpa_ssid *group,
543                           struct wpa_ssid **selected_ssid)
544 {
545         size_t i;
546
547         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
548                    group->priority);
549
550         for (i = 0; i < scan_res->num; i++) {
551                 struct wpa_scan_res *bss = scan_res->res[i];
552                 const u8 *ie, *ssid;
553                 u8 ssid_len;
554
555                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
556                 if (!*selected_ssid)
557                         continue;
558
559                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
560                 ssid = ie ? ie + 2 : (u8 *) "";
561                 ssid_len = ie ? ie[1] : 0;
562
563                 wpa_printf(MSG_DEBUG, "   selected BSS " MACSTR " ssid='%s'",
564                            MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
565                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
566         }
567
568         return NULL;
569 }
570
571
572 static struct wpa_bss *
573 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
574                             struct wpa_scan_results *scan_res,
575                             struct wpa_ssid **selected_ssid)
576 {
577         struct wpa_bss *selected = NULL;
578         int prio;
579
580         while (selected == NULL) {
581                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
582                         selected = wpa_supplicant_select_bss(
583                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
584                                 selected_ssid);
585                         if (selected)
586                                 break;
587                 }
588
589                 if (selected == NULL && wpa_s->blacklist) {
590                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
591                                    "and try again");
592                         wpa_blacklist_clear(wpa_s);
593                         wpa_s->blacklist_cleared++;
594                 } else if (selected == NULL)
595                         break;
596         }
597
598         return selected;
599 }
600
601
602 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
603                                         int timeout_sec, int timeout_usec)
604 {
605         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
606                 /*
607                  * No networks are enabled; short-circuit request so
608                  * we don't wait timeout seconds before transitioning
609                  * to INACTIVE state.
610                  */
611                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
612                 return;
613         }
614         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
615 }
616
617
618 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
619                             struct wpa_bss *selected,
620                             struct wpa_ssid *ssid)
621 {
622         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
623                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
624                         "PBC session overlap");
625                 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
626                 return;
627         }
628
629         /*
630          * Do not trigger new association unless the BSSID has changed or if
631          * reassociation is requested. If we are in process of associating with
632          * the selected BSSID, do not trigger new attempt.
633          */
634         if (wpa_s->reassociate ||
635             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
636              (wpa_s->wpa_state != WPA_ASSOCIATING ||
637               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
638               0))) {
639                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
640                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
641                         return;
642                 }
643                 wpa_supplicant_associate(wpa_s, selected, ssid);
644         } else {
645                 wpa_printf(MSG_DEBUG, "Already associated with the selected "
646                            "AP");
647         }
648 }
649
650
651 static struct wpa_ssid *
652 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
653 {
654         int prio;
655         struct wpa_ssid *ssid;
656
657         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
658                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
659                 {
660                         if (ssid->disabled)
661                                 continue;
662                         if (ssid->mode == IEEE80211_MODE_IBSS ||
663                             ssid->mode == IEEE80211_MODE_AP)
664                                 return ssid;
665                 }
666         }
667         return NULL;
668 }
669
670
671 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
672  * on BSS added and BSS changed events */
673 static void wpa_supplicant_rsn_preauth_scan_results(
674         struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
675 {
676         int i;
677
678         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
679                 return;
680
681         for (i = scan_res->num - 1; i >= 0; i--) {
682                 const u8 *ssid, *rsn;
683                 struct wpa_scan_res *r;
684
685                 r = scan_res->res[i];
686
687                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
688                 if (ssid == NULL)
689                         continue;
690
691                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
692                 if (rsn == NULL)
693                         continue;
694
695                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
696         }
697
698 }
699
700
701 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
702                                        struct wpa_bss *selected,
703                                        struct wpa_ssid *ssid,
704                                        struct wpa_scan_results *scan_res)
705 {
706         size_t i;
707         struct wpa_scan_res *current_bss = NULL;
708         int min_diff;
709
710         if (wpa_s->reassociate)
711                 return 1; /* explicit request to reassociate */
712         if (wpa_s->wpa_state < WPA_ASSOCIATED)
713                 return 1; /* we are not associated; continue */
714         if (wpa_s->current_ssid == NULL)
715                 return 1; /* unknown current SSID */
716         if (wpa_s->current_ssid != ssid)
717                 return 1; /* different network block */
718
719         for (i = 0; i < scan_res->num; i++) {
720                 struct wpa_scan_res *res = scan_res->res[i];
721                 const u8 *ie;
722                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
723                         continue;
724
725                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
726                 if (ie == NULL)
727                         continue;
728                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
729                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
730                         continue;
731                 current_bss = res;
732                 break;
733         }
734
735         if (!current_bss)
736                 return 1; /* current BSS not seen in scan results */
737
738         wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
739         wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
740                    MAC2STR(current_bss->bssid), current_bss->level);
741         wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
742                    MAC2STR(selected->bssid), selected->level);
743
744         if (wpa_s->current_ssid->bssid_set &&
745             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
746             0) {
747                 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
748                            "preferred BSSID");
749                 return 1;
750         }
751
752         min_diff = 2;
753         if (current_bss->level < 0) {
754                 if (current_bss->level < -85)
755                         min_diff = 1;
756                 else if (current_bss->level < -80)
757                         min_diff = 2;
758                 else if (current_bss->level < -75)
759                         min_diff = 3;
760                 else if (current_bss->level < -70)
761                         min_diff = 4;
762                 else
763                         min_diff = 5;
764         }
765         if (abs(current_bss->level - selected->level) < min_diff) {
766                 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
767                            "signal level");
768                 return 0;
769         }
770
771         return 1;
772 }
773
774
775 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
776                                               union wpa_event_data *data)
777 {
778         struct wpa_bss *selected;
779         struct wpa_ssid *ssid = NULL;
780         struct wpa_scan_results *scan_res;
781         int ap = 0;
782
783 #ifdef CONFIG_AP
784         if (wpa_s->ap_iface)
785                 ap = 1;
786 #endif /* CONFIG_AP */
787
788         wpa_supplicant_notify_scanning(wpa_s, 0);
789
790         scan_res = wpa_supplicant_get_scan_results(wpa_s,
791                                                    data ? &data->scan_info :
792                                                    NULL, 1);
793         if (scan_res == NULL) {
794                 if (wpa_s->conf->ap_scan == 2 || ap)
795                         return;
796                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
797                            "scanning again");
798                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
799                 return;
800         }
801
802         if (wpa_s->scan_res_handler) {
803                 wpa_s->scan_res_handler(wpa_s, scan_res);
804                 wpa_s->scan_res_handler = NULL;
805                 wpa_scan_results_free(scan_res);
806                 return;
807         }
808
809         if (ap) {
810                 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
811                 wpa_scan_results_free(scan_res);
812                 return;
813         }
814
815         wpa_printf(MSG_DEBUG, "New scan results available");
816         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
817         wpas_notify_scan_results(wpa_s);
818
819         wpas_notify_scan_done(wpa_s, 1);
820
821         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
822                 wpa_scan_results_free(scan_res);
823                 return;
824         }
825
826         if (wpa_s->disconnected) {
827                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
828                 wpa_scan_results_free(scan_res);
829                 return;
830         }
831
832         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
833                 wpa_scan_results_free(scan_res);
834                 return;
835         }
836
837         wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
838
839         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
840
841         if (selected) {
842                 int skip;
843                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
844                                                     scan_res);
845                 wpa_scan_results_free(scan_res);
846                 if (skip)
847                         return;
848                 wpa_supplicant_connect(wpa_s, selected, ssid);
849         } else {
850                 wpa_scan_results_free(scan_res);
851                 wpa_printf(MSG_DEBUG, "No suitable network found");
852                 ssid = wpa_supplicant_pick_new_network(wpa_s);
853                 if (ssid) {
854                         wpa_printf(MSG_DEBUG, "Setup a new network");
855                         wpa_supplicant_associate(wpa_s, NULL, ssid);
856                 } else {
857                         int timeout_sec = 5;
858                         int timeout_usec = 0;
859 #ifdef CONFIG_P2P
860                         if (wpa_s->p2p_in_provisioning) {
861                                 /*
862                                  * Use shorter wait during P2P Provisioning
863                                  * state to speed up group formation.
864                                  */
865                                 timeout_sec = 0;
866                                 timeout_usec = 250000;
867                         }
868 #endif /* CONFIG_P2P */
869                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
870                                                     timeout_usec);
871                 }
872         }
873 }
874 #endif /* CONFIG_NO_SCAN_PROCESSING */
875
876
877 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
878                                           union wpa_event_data *data)
879 {
880         int l, len, found = 0, wpa_found, rsn_found;
881         const u8 *p;
882
883         wpa_printf(MSG_DEBUG, "Association info event");
884         if (data->assoc_info.req_ies)
885                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
886                             data->assoc_info.req_ies_len);
887         if (data->assoc_info.resp_ies)
888                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
889                             data->assoc_info.resp_ies_len);
890         if (data->assoc_info.beacon_ies)
891                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
892                             data->assoc_info.beacon_ies,
893                             data->assoc_info.beacon_ies_len);
894         if (data->assoc_info.freq)
895                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
896
897         p = data->assoc_info.req_ies;
898         l = data->assoc_info.req_ies_len;
899
900         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
901         while (p && l >= 2) {
902                 len = p[1] + 2;
903                 if (len > l) {
904                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
905                                     p, l);
906                         break;
907                 }
908                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
909                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
910                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
911                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
912                                 break;
913                         found = 1;
914                         wpa_find_assoc_pmkid(wpa_s);
915                         break;
916                 }
917                 l -= len;
918                 p += len;
919         }
920         if (!found && data->assoc_info.req_ies)
921                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
922
923 #ifdef CONFIG_IEEE80211R
924 #ifdef CONFIG_SME
925         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
926                 u8 bssid[ETH_ALEN];
927                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
928                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
929                                                  data->assoc_info.resp_ies,
930                                                  data->assoc_info.resp_ies_len,
931                                                  bssid) < 0) {
932                         wpa_printf(MSG_DEBUG, "FT: Validation of "
933                                    "Reassociation Response failed");
934                         wpa_supplicant_deauthenticate(
935                                 wpa_s, WLAN_REASON_INVALID_IE);
936                         return -1;
937                 }
938         }
939
940         p = data->assoc_info.resp_ies;
941         l = data->assoc_info.resp_ies_len;
942
943 #ifdef CONFIG_WPS_STRICT
944         if (wpa_s->current_ssid &&
945             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
946                 struct wpabuf *wps;
947                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
948                 if (wps == NULL) {
949                         wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
950                                    "WPS IE in (Re)Association Response");
951                         return -1;
952                 }
953
954                 if (wps_validate_assoc_resp(wps) < 0) {
955                         wpabuf_free(wps);
956                         wpa_supplicant_deauthenticate(
957                                 wpa_s, WLAN_REASON_INVALID_IE);
958                         return -1;
959                 }
960                 wpabuf_free(wps);
961         }
962 #endif /* CONFIG_WPS_STRICT */
963
964         /* Go through the IEs and make a copy of the MDIE, if present. */
965         while (p && l >= 2) {
966                 len = p[1] + 2;
967                 if (len > l) {
968                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
969                                     p, l);
970                         break;
971                 }
972                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
973                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
974                         wpa_s->sme.ft_used = 1;
975                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
976                                   MOBILITY_DOMAIN_ID_LEN);
977                         break;
978                 }
979                 l -= len;
980                 p += len;
981         }
982 #endif /* CONFIG_SME */
983
984         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
985                              data->assoc_info.resp_ies_len);
986 #endif /* CONFIG_IEEE80211R */
987
988         /* WPA/RSN IE from Beacon/ProbeResp */
989         p = data->assoc_info.beacon_ies;
990         l = data->assoc_info.beacon_ies_len;
991
992         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
993          */
994         wpa_found = rsn_found = 0;
995         while (p && l >= 2) {
996                 len = p[1] + 2;
997                 if (len > l) {
998                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
999                                     p, l);
1000                         break;
1001                 }
1002                 if (!wpa_found &&
1003                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1004                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1005                         wpa_found = 1;
1006                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1007                 }
1008
1009                 if (!rsn_found &&
1010                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1011                         rsn_found = 1;
1012                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1013                 }
1014
1015                 l -= len;
1016                 p += len;
1017         }
1018
1019         if (!wpa_found && data->assoc_info.beacon_ies)
1020                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1021         if (!rsn_found && data->assoc_info.beacon_ies)
1022                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1023         if (wpa_found || rsn_found)
1024                 wpa_s->ap_ies_from_associnfo = 1;
1025
1026         wpa_s->assoc_freq = data->assoc_info.freq;
1027
1028         return 0;
1029 }
1030
1031
1032 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1033                                        union wpa_event_data *data)
1034 {
1035         u8 bssid[ETH_ALEN];
1036         int ft_completed;
1037         int bssid_changed;
1038         struct wpa_driver_capa capa;
1039
1040 #ifdef CONFIG_AP
1041         if (wpa_s->ap_iface) {
1042                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1043                                     data->assoc_info.addr,
1044                                     data->assoc_info.req_ies,
1045                                     data->assoc_info.req_ies_len);
1046                 return;
1047         }
1048 #endif /* CONFIG_AP */
1049
1050         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1051         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1052                 return;
1053
1054         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1055         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1056                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1057         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1058             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1059              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1060                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1061                         MACSTR, MAC2STR(bssid));
1062                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1063                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1064                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1065                 if (bssid_changed)
1066                         wpas_notify_bssid_changed(wpa_s);
1067
1068                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1069                         wpa_clear_keys(wpa_s, bssid);
1070                 }
1071                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1072                         wpa_supplicant_disassociate(
1073                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1074                         return;
1075                 }
1076                 if (wpa_s->current_ssid) {
1077                         struct wpa_bss *bss = NULL;
1078                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1079                         if (ssid->ssid_len > 0)
1080                                 bss = wpa_bss_get(wpa_s, bssid,
1081                                                   ssid->ssid, ssid->ssid_len);
1082                         if (!bss)
1083                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1084                         if (bss)
1085                                 wpa_s->current_bss = bss;
1086                 }
1087         }
1088
1089 #ifdef CONFIG_SME
1090         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1091         wpa_s->sme.prev_bssid_set = 1;
1092 #endif /* CONFIG_SME */
1093
1094         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1095         if (wpa_s->current_ssid) {
1096                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1097                  * initialized before association, but for other modes,
1098                  * initialize PC/SC here, if the current configuration needs
1099                  * smartcard or SIM/USIM. */
1100                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1101         }
1102         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1103         if (wpa_s->l2)
1104                 l2_packet_notify_auth_start(wpa_s->l2);
1105
1106         /*
1107          * Set portEnabled first to FALSE in order to get EAP state machine out
1108          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1109          * state machine may transit to AUTHENTICATING state based on obsolete
1110          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1111          * AUTHENTICATED without ever giving chance to EAP state machine to
1112          * reset the state.
1113          */
1114         if (!ft_completed) {
1115                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1116                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1117         }
1118         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1119                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1120         /* 802.1X::portControl = Auto */
1121         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1122         wpa_s->eapol_received = 0;
1123         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1124             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1125             (wpa_s->current_ssid &&
1126              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1127                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1128                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1129         } else if (!ft_completed) {
1130                 /* Timeout for receiving the first EAPOL packet */
1131                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1132         }
1133         wpa_supplicant_cancel_scan(wpa_s);
1134
1135         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1136             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1137                 /*
1138                  * We are done; the driver will take care of RSN 4-way
1139                  * handshake.
1140                  */
1141                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1142                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1143                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1144                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1145         }
1146
1147         if (wpa_s->pending_eapol_rx) {
1148                 struct os_time now, age;
1149                 os_get_time(&now);
1150                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1151                 if (age.sec == 0 && age.usec < 100000 &&
1152                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1153                     0) {
1154                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1155                                    "that was received just before association "
1156                                    "notification");
1157                         wpa_supplicant_rx_eapol(
1158                                 wpa_s, wpa_s->pending_eapol_rx_src,
1159                                 wpabuf_head(wpa_s->pending_eapol_rx),
1160                                 wpabuf_len(wpa_s->pending_eapol_rx));
1161                 }
1162                 wpabuf_free(wpa_s->pending_eapol_rx);
1163                 wpa_s->pending_eapol_rx = NULL;
1164         }
1165
1166 #ifdef CONFIG_BGSCAN
1167         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1168                 bgscan_deinit(wpa_s);
1169                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1170                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1171                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1172                                            "bgscan");
1173                                 /*
1174                                  * Live without bgscan; it is only used as a
1175                                  * roaming optimization, so the initial
1176                                  * connection is not affected.
1177                                  */
1178                         } else
1179                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1180                 } else
1181                         wpa_s->bgscan_ssid = NULL;
1182         }
1183 #endif /* CONFIG_BGSCAN */
1184
1185         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1186              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1187             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1188             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1189                 /* Set static WEP keys again */
1190                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1191         }
1192 }
1193
1194
1195 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1196                                           u16 reason_code)
1197 {
1198         const u8 *bssid;
1199 #ifdef CONFIG_SME
1200         int authenticating;
1201         u8 prev_pending_bssid[ETH_ALEN];
1202
1203         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1204         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1205 #endif /* CONFIG_SME */
1206
1207         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1208                 /*
1209                  * At least Host AP driver and a Prism3 card seemed to be
1210                  * generating streams of disconnected events when configuring
1211                  * IBSS for WPA-None. Ignore them for now.
1212                  */
1213                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1214                            "IBSS/WPA-None mode");
1215                 return;
1216         }
1217
1218         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1219             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1220                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1221                         "pre-shared key may be incorrect");
1222         }
1223         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1224                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1225         bssid = wpa_s->bssid;
1226         if (is_zero_ether_addr(bssid))
1227                 bssid = wpa_s->pending_bssid;
1228         wpa_blacklist_add(wpa_s, bssid);
1229         wpa_sm_notify_disassoc(wpa_s->wpa);
1230         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1231                 " reason=%d",
1232                 MAC2STR(bssid), reason_code);
1233         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1234                 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1235                 wpa_s->keys_cleared = 0;
1236                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1237         }
1238         wpa_supplicant_mark_disassoc(wpa_s);
1239         bgscan_deinit(wpa_s);
1240         wpa_s->bgscan_ssid = NULL;
1241 #ifdef CONFIG_SME
1242         if (authenticating &&
1243             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1244                 /*
1245                  * mac80211-workaround to force deauth on failed auth cmd,
1246                  * requires us to remain in authenticating state to allow the
1247                  * second authentication attempt to be continued properly.
1248                  */
1249                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1250                            "proceed after disconnection event");
1251                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1252                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1253         }
1254 #endif /* CONFIG_SME */
1255 }
1256
1257
1258 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1259 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1260                                                     void *sock_ctx)
1261 {
1262         struct wpa_supplicant *wpa_s = eloop_ctx;
1263
1264         if (!wpa_s->pending_mic_error_report)
1265                 return;
1266
1267         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1268         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1269         wpa_s->pending_mic_error_report = 0;
1270 }
1271 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1272
1273
1274 static void
1275 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1276                                          union wpa_event_data *data)
1277 {
1278         int pairwise;
1279         struct os_time t;
1280
1281         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1282         pairwise = (data && data->michael_mic_failure.unicast);
1283         os_get_time(&t);
1284         if ((wpa_s->last_michael_mic_error &&
1285              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1286             wpa_s->pending_mic_error_report) {
1287                 if (wpa_s->pending_mic_error_report) {
1288                         /*
1289                          * Send the pending MIC error report immediately since
1290                          * we are going to start countermeasures and AP better
1291                          * do the same.
1292                          */
1293                         wpa_sm_key_request(wpa_s->wpa, 1,
1294                                            wpa_s->pending_mic_error_pairwise);
1295                 }
1296
1297                 /* Send the new MIC error report immediately since we are going
1298                  * to start countermeasures and AP better do the same.
1299                  */
1300                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1301
1302                 /* initialize countermeasures */
1303                 wpa_s->countermeasures = 1;
1304                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1305
1306                 /*
1307                  * Need to wait for completion of request frame. We do not get
1308                  * any callback for the message completion, so just wait a
1309                  * short while and hope for the best. */
1310                 os_sleep(0, 10000);
1311
1312                 wpa_drv_set_countermeasures(wpa_s, 1);
1313                 wpa_supplicant_deauthenticate(wpa_s,
1314                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1315                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1316                                      wpa_s, NULL);
1317                 eloop_register_timeout(60, 0,
1318                                        wpa_supplicant_stop_countermeasures,
1319                                        wpa_s, NULL);
1320                 /* TODO: mark the AP rejected for 60 second. STA is
1321                  * allowed to associate with another AP.. */
1322         } else {
1323 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1324                 if (wpa_s->mic_errors_seen) {
1325                         /*
1326                          * Reduce the effectiveness of Michael MIC error
1327                          * reports as a means for attacking against TKIP if
1328                          * more than one MIC failure is noticed with the same
1329                          * PTK. We delay the transmission of the reports by a
1330                          * random time between 0 and 60 seconds in order to
1331                          * force the attacker wait 60 seconds before getting
1332                          * the information on whether a frame resulted in a MIC
1333                          * failure.
1334                          */
1335                         u8 rval[4];
1336                         int sec;
1337
1338                         if (os_get_random(rval, sizeof(rval)) < 0)
1339                                 sec = os_random() % 60;
1340                         else
1341                                 sec = WPA_GET_BE32(rval) % 60;
1342                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1343                                    "seconds", sec);
1344                         wpa_s->pending_mic_error_report = 1;
1345                         wpa_s->pending_mic_error_pairwise = pairwise;
1346                         eloop_cancel_timeout(
1347                                 wpa_supplicant_delayed_mic_error_report,
1348                                 wpa_s, NULL);
1349                         eloop_register_timeout(
1350                                 sec, os_random() % 1000000,
1351                                 wpa_supplicant_delayed_mic_error_report,
1352                                 wpa_s, NULL);
1353                 } else {
1354                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1355                 }
1356 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1357                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1358 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1359         }
1360         wpa_s->last_michael_mic_error = t.sec;
1361         wpa_s->mic_errors_seen++;
1362 }
1363
1364
1365 #ifdef CONFIG_TERMINATE_ONLASTIF
1366 static int any_interfaces(struct wpa_supplicant *head)
1367 {
1368         struct wpa_supplicant *wpa_s;
1369
1370         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1371                 if (!wpa_s->interface_removed)
1372                         return 1;
1373         return 0;
1374 }
1375 #endif /* CONFIG_TERMINATE_ONLASTIF */
1376
1377
1378 static void
1379 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1380                                       union wpa_event_data *data)
1381 {
1382         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1383                 return;
1384
1385         switch (data->interface_status.ievent) {
1386         case EVENT_INTERFACE_ADDED:
1387                 if (!wpa_s->interface_removed)
1388                         break;
1389                 wpa_s->interface_removed = 0;
1390                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1391                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1392                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1393                                    "after interface was added.");
1394                 }
1395                 break;
1396         case EVENT_INTERFACE_REMOVED:
1397                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1398                 wpa_s->interface_removed = 1;
1399                 wpa_supplicant_mark_disassoc(wpa_s);
1400                 l2_packet_deinit(wpa_s->l2);
1401                 wpa_s->l2 = NULL;
1402 #ifdef CONFIG_TERMINATE_ONLASTIF
1403                 /* check if last interface */
1404                 if (!any_interfaces(wpa_s->global->ifaces))
1405                         eloop_terminate();
1406 #endif /* CONFIG_TERMINATE_ONLASTIF */
1407                 break;
1408         }
1409 }
1410
1411
1412 #ifdef CONFIG_PEERKEY
1413 static void
1414 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1415                               union wpa_event_data *data)
1416 {
1417         if (data == NULL)
1418                 return;
1419         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1420 }
1421 #endif /* CONFIG_PEERKEY */
1422
1423
1424 #ifdef CONFIG_IEEE80211R
1425 static void
1426 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1427                                  union wpa_event_data *data)
1428 {
1429         if (data == NULL)
1430                 return;
1431
1432         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1433                                     data->ft_ies.ies_len,
1434                                     data->ft_ies.ft_action,
1435                                     data->ft_ies.target_ap,
1436                                     data->ft_ies.ric_ies,
1437                                     data->ft_ies.ric_ies_len) < 0) {
1438                 /* TODO: prevent MLME/driver from trying to associate? */
1439         }
1440 }
1441 #endif /* CONFIG_IEEE80211R */
1442
1443
1444 #ifdef CONFIG_IBSS_RSN
1445 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1446                                                 union wpa_event_data *data)
1447 {
1448         if (data == NULL)
1449                 return;
1450         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1451 }
1452 #endif /* CONFIG_IBSS_RSN */
1453
1454
1455 #ifdef CONFIG_IEEE80211R
1456 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1457                          size_t len)
1458 {
1459         const u8 *sta_addr, *target_ap_addr;
1460         u16 status;
1461
1462         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1463         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1464                 return; /* only SME case supported for now */
1465         if (len < 1 + 2 * ETH_ALEN + 2)
1466                 return;
1467         if (data[0] != 2)
1468                 return; /* Only FT Action Response is supported for now */
1469         sta_addr = data + 1;
1470         target_ap_addr = data + 1 + ETH_ALEN;
1471         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1472         wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1473                    " TargetAP " MACSTR " status %u",
1474                    MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1475
1476         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1477                 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1478                            " in FT Action Response", MAC2STR(sta_addr));
1479                 return;
1480         }
1481
1482         if (status) {
1483                 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1484                            "failure (status code %d)", status);
1485                 /* TODO: report error to FT code(?) */
1486                 return;
1487         }
1488
1489         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1490                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1491                                     target_ap_addr, NULL, 0) < 0)
1492                 return;
1493
1494 #ifdef CONFIG_SME
1495         {
1496                 struct wpa_bss *bss;
1497                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1498                 if (bss)
1499                         wpa_s->sme.freq = bss->freq;
1500                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1501                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1502                               WLAN_AUTH_FT);
1503         }
1504 #endif /* CONFIG_SME */
1505 }
1506 #endif /* CONFIG_IEEE80211R */
1507
1508
1509 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1510                           union wpa_event_data *data)
1511 {
1512         struct wpa_supplicant *wpa_s = ctx;
1513         u16 reason_code = 0;
1514
1515         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1516             event != EVENT_INTERFACE_ENABLED &&
1517             event != EVENT_INTERFACE_STATUS) {
1518                 wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
1519                            "disabled", event);
1520                 return;
1521         }
1522
1523         wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
1524                    event, wpa_s->ifname);
1525
1526         switch (event) {
1527         case EVENT_AUTH:
1528                 sme_event_auth(wpa_s, data);
1529                 break;
1530         case EVENT_ASSOC:
1531                 wpa_supplicant_event_assoc(wpa_s, data);
1532                 break;
1533         case EVENT_DISASSOC:
1534                 wpa_printf(MSG_DEBUG, "Disassociation notification");
1535                 if (data) {
1536                         wpa_printf(MSG_DEBUG, " * reason %u",
1537                                    data->disassoc_info.reason_code);
1538                         if (data->disassoc_info.addr)
1539                                 wpa_printf(MSG_DEBUG, " * address " MACSTR,
1540                                            MAC2STR(data->disassoc_info.addr));
1541                 }
1542 #ifdef CONFIG_AP
1543                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1544                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1545                                                data->disassoc_info.addr);
1546                         break;
1547                 }
1548 #endif /* CONFIG_AP */
1549                 if (data)
1550                         reason_code = data->deauth_info.reason_code;
1551                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1552                         sme_event_disassoc(wpa_s, data);
1553                 /* fall through */
1554         case EVENT_DEAUTH:
1555                 if (event == EVENT_DEAUTH) {
1556                         wpa_printf(MSG_DEBUG, "Deauthentication notification");
1557                         if (data) {
1558                                 reason_code = data->deauth_info.reason_code;
1559                                 wpa_printf(MSG_DEBUG, " * reason %u",
1560                                            data->deauth_info.reason_code);
1561                                 if (data->deauth_info.addr) {
1562                                         wpa_printf(MSG_DEBUG, " * address "
1563                                                    MACSTR,
1564                                                    MAC2STR(data->deauth_info.
1565                                                            addr));
1566                                 }
1567                         }
1568                 }
1569 #ifdef CONFIG_AP
1570                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1571                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1572                                                data->deauth_info.addr);
1573                         break;
1574                 }
1575 #endif /* CONFIG_AP */
1576                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1577                 break;
1578         case EVENT_MICHAEL_MIC_FAILURE:
1579                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1580                 break;
1581 #ifndef CONFIG_NO_SCAN_PROCESSING
1582         case EVENT_SCAN_RESULTS:
1583                 wpa_supplicant_event_scan_results(wpa_s, data);
1584                 break;
1585 #endif /* CONFIG_NO_SCAN_PROCESSING */
1586         case EVENT_ASSOCINFO:
1587                 wpa_supplicant_event_associnfo(wpa_s, data);
1588                 break;
1589         case EVENT_INTERFACE_STATUS:
1590                 wpa_supplicant_event_interface_status(wpa_s, data);
1591                 break;
1592         case EVENT_PMKID_CANDIDATE:
1593                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1594                 break;
1595 #ifdef CONFIG_PEERKEY
1596         case EVENT_STKSTART:
1597                 wpa_supplicant_event_stkstart(wpa_s, data);
1598                 break;
1599 #endif /* CONFIG_PEERKEY */
1600 #ifdef CONFIG_IEEE80211R
1601         case EVENT_FT_RESPONSE:
1602                 wpa_supplicant_event_ft_response(wpa_s, data);
1603                 break;
1604 #endif /* CONFIG_IEEE80211R */
1605 #ifdef CONFIG_IBSS_RSN
1606         case EVENT_IBSS_RSN_START:
1607                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1608                 break;
1609 #endif /* CONFIG_IBSS_RSN */
1610         case EVENT_ASSOC_REJECT:
1611                 sme_event_assoc_reject(wpa_s, data);
1612                 break;
1613         case EVENT_AUTH_TIMED_OUT:
1614                 sme_event_auth_timed_out(wpa_s, data);
1615                 break;
1616         case EVENT_ASSOC_TIMED_OUT:
1617                 sme_event_assoc_timed_out(wpa_s, data);
1618                 break;
1619 #ifdef CONFIG_AP
1620         case EVENT_TX_STATUS:
1621                 if (wpa_s->ap_iface == NULL)
1622                         break;
1623                 switch (data->tx_status.type) {
1624                 case WLAN_FC_TYPE_MGMT:
1625                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1626                                       data->tx_status.data_len,
1627                                       data->tx_status.stype,
1628                                       data->tx_status.ack);
1629                         break;
1630                 case WLAN_FC_TYPE_DATA:
1631                         ap_tx_status(wpa_s, data->tx_status.dst,
1632                                      data->tx_status.data,
1633                                      data->tx_status.data_len,
1634                                      data->tx_status.ack);
1635                         break;
1636                 }
1637                 break;
1638         case EVENT_RX_FROM_UNKNOWN:
1639                 if (wpa_s->ap_iface == NULL)
1640                         break;
1641                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1642                                        data->rx_from_unknown.len);
1643                 break;
1644         case EVENT_RX_MGMT:
1645                 if (wpa_s->ap_iface == NULL)
1646                         break;
1647                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1648                 break;
1649 #endif /* CONFIG_AP */
1650         case EVENT_RX_ACTION:
1651                 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1652                            " Category=%u DataLen=%d freq=%d MHz",
1653                            MAC2STR(data->rx_action.sa),
1654                            data->rx_action.category, (int) data->rx_action.len,
1655                            data->rx_action.freq);
1656 #ifdef CONFIG_IEEE80211R
1657                 if (data->rx_action.category == WLAN_ACTION_FT) {
1658                         ft_rx_action(wpa_s, data->rx_action.data,
1659                                      data->rx_action.len);
1660                         break;
1661                 }
1662 #endif /* CONFIG_IEEE80211R */
1663                 break;
1664 #ifdef CONFIG_CLIENT_MLME
1665         case EVENT_MLME_RX: {
1666                 struct ieee80211_rx_status rx_status;
1667                 os_memset(&rx_status, 0, sizeof(rx_status));
1668                 rx_status.freq = data->mlme_rx.freq;
1669                 rx_status.channel = data->mlme_rx.channel;
1670                 rx_status.ssi = data->mlme_rx.ssi;
1671                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1672                                  &rx_status);
1673                 break;
1674         }
1675 #endif /* CONFIG_CLIENT_MLME */
1676         case EVENT_EAPOL_RX:
1677                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1678                                         data->eapol_rx.data,
1679                                         data->eapol_rx.data_len);
1680                 break;
1681         case EVENT_SIGNAL_CHANGE:
1682                 bgscan_notify_signal_change(
1683                         wpa_s, data->signal_change.above_threshold,
1684                         data->signal_change.current_signal);
1685                 break;
1686         case EVENT_INTERFACE_ENABLED:
1687                 wpa_printf(MSG_DEBUG, "Interface was enabled");
1688                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1689                         wpa_supplicant_set_state(wpa_s,
1690                                                  WPA_DISCONNECTED);
1691                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1692                 }
1693                 break;
1694         case EVENT_INTERFACE_DISABLED:
1695                 wpa_printf(MSG_DEBUG, "Interface was disabled");
1696                 wpa_supplicant_mark_disassoc(wpa_s);
1697                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1698                 break;
1699         default:
1700                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1701                 break;
1702         }
1703 }