OSDN Git Service

Initial wpa_supplicant 0.6.9 code
[android-x86/external-wpa_supplicant_6.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2008, 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 "wpa.h"
20 #include "eloop.h"
21 #include "drivers/driver.h"
22 #include "config.h"
23 #include "l2_packet/l2_packet.h"
24 #include "wpa_supplicant_i.h"
25 #include "pcsc_funcs.h"
26 #include "preauth.h"
27 #include "pmksa_cache.h"
28 #include "wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ctrl_iface_dbus.h"
31 #include "ieee802_11_defs.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34 #include "wps_supplicant.h"
35
36
37 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
38 {
39         struct wpa_ssid *ssid;
40
41         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
42                 return 0;
43
44         wpa_printf(MSG_DEBUG, "Select network based on association "
45                    "information");
46         ssid = wpa_supplicant_get_ssid(wpa_s);
47         if (ssid == NULL) {
48                 wpa_printf(MSG_INFO, "No network configuration found for the "
49                            "current AP");
50                 return -1;
51         }
52
53         if (ssid->disabled) {
54                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
55                 return -1;
56         }
57
58         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
59                    "AP");
60         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
61                               WPA_KEY_MGMT_WPA_NONE |
62                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
63                               WPA_KEY_MGMT_PSK_SHA256 |
64                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
65                 u8 wpa_ie[80];
66                 size_t wpa_ie_len = sizeof(wpa_ie);
67                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
68                                           wpa_ie, &wpa_ie_len);
69         } else {
70                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
71         }
72
73         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
74                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
75         wpa_s->current_ssid = ssid;
76         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
77         wpa_supplicant_initiate_eapol(wpa_s);
78
79         return 0;
80 }
81
82
83 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
84                                                 void *sock_ctx)
85 {
86         struct wpa_supplicant *wpa_s = eloop_ctx;
87
88         if (wpa_s->countermeasures) {
89                 wpa_s->countermeasures = 0;
90                 wpa_drv_set_countermeasures(wpa_s, 0);
91                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
92                 wpa_supplicant_req_scan(wpa_s, 0, 0);
93         }
94 }
95
96
97 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
98 {
99         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
100         os_memset(wpa_s->bssid, 0, ETH_ALEN);
101         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
102         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
103         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
104         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
105                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
106         wpa_s->ap_ies_from_associnfo = 0;
107 }
108
109
110 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
111 {
112         struct wpa_ie_data ie;
113         int pmksa_set = -1;
114         size_t i;
115
116         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
117             ie.pmkid == NULL)
118                 return;
119
120         for (i = 0; i < ie.num_pmkid; i++) {
121                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
122                                                     ie.pmkid + i * PMKID_LEN,
123                                                     NULL, NULL, 0);
124                 if (pmksa_set == 0) {
125                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
126                         break;
127                 }
128         }
129
130         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
131                    "cache", pmksa_set == 0 ? "" : "not ");
132 }
133
134
135 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
136                                                  union wpa_event_data *data)
137 {
138         if (data == NULL) {
139                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
140                 return;
141         }
142         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
143                    " index=%d preauth=%d",
144                    MAC2STR(data->pmkid_candidate.bssid),
145                    data->pmkid_candidate.index,
146                    data->pmkid_candidate.preauth);
147
148         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
149                             data->pmkid_candidate.index,
150                             data->pmkid_candidate.preauth);
151 }
152
153
154 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
155 {
156         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
157             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
158                 return 0;
159
160 #ifdef IEEE8021X_EAPOL
161         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
162             wpa_s->current_ssid &&
163             !(wpa_s->current_ssid->eapol_flags &
164               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
165                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
166                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
167                  * plaintext or static WEP keys). */
168                 return 0;
169         }
170 #endif /* IEEE8021X_EAPOL */
171
172         return 1;
173 }
174
175
176 /**
177  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
178  * @wpa_s: pointer to wpa_supplicant data
179  * @ssid: Configuration data for the network
180  * Returns: 0 on success, -1 on failure
181  *
182  * This function is called when starting authentication with a network that is
183  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
184  */
185 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
186                               struct wpa_ssid *ssid)
187 {
188 #ifdef IEEE8021X_EAPOL
189         int aka = 0, sim = 0, type;
190
191         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
192                 return 0;
193
194         if (ssid->eap.eap_methods == NULL) {
195                 sim = 1;
196                 aka = 1;
197         } else {
198                 struct eap_method_type *eap = ssid->eap.eap_methods;
199                 while (eap->vendor != EAP_VENDOR_IETF ||
200                        eap->method != EAP_TYPE_NONE) {
201                         if (eap->vendor == EAP_VENDOR_IETF) {
202                                 if (eap->method == EAP_TYPE_SIM)
203                                         sim = 1;
204                                 else if (eap->method == EAP_TYPE_AKA)
205                                         aka = 1;
206                         }
207                         eap++;
208                 }
209         }
210
211         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
212                 sim = 0;
213         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
214                 aka = 0;
215
216         if (!sim && !aka) {
217                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
218                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
219                 return 0;
220         }
221
222         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
223                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
224         if (sim && aka)
225                 type = SCARD_TRY_BOTH;
226         else if (aka)
227                 type = SCARD_USIM_ONLY;
228         else
229                 type = SCARD_GSM_SIM_ONLY;
230
231         wpa_s->scard = scard_init(type);
232         if (wpa_s->scard == NULL) {
233                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
234                            "(pcsc-lite)");
235                 return -1;
236         }
237         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
238         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
239 #endif /* IEEE8021X_EAPOL */
240
241         return 0;
242 }
243
244
245 #ifndef CONFIG_NO_SCAN_PROCESSING
246 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
247                                         struct wpa_ssid *ssid)
248 {
249         int i, privacy = 0;
250
251         if (ssid->mixed_cell)
252                 return 1;
253
254 #ifdef CONFIG_WPS
255         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
256                 return 1;
257 #endif /* CONFIG_WPS */
258
259         for (i = 0; i < NUM_WEP_KEYS; i++) {
260                 if (ssid->wep_key_len[i]) {
261                         privacy = 1;
262                         break;
263                 }
264         }
265 #ifdef IEEE8021X_EAPOL
266         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
267             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
268                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
269                 privacy = 1;
270 #endif /* IEEE8021X_EAPOL */
271
272         if (bss->caps & IEEE80211_CAP_PRIVACY)
273                 return privacy;
274         return !privacy;
275 }
276
277
278 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
279                                          struct wpa_ssid *ssid,
280                                          struct wpa_scan_res *bss)
281 {
282         struct wpa_ie_data ie;
283         int proto_match = 0;
284         const u8 *rsn_ie, *wpa_ie;
285         int ret;
286
287         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
288         if (ret >= 0)
289                 return ret;
290
291         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
292         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
293                 proto_match++;
294
295                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
296                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
297                         break;
298                 }
299                 if (!(ie.proto & ssid->proto)) {
300                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
301                                    "mismatch");
302                         break;
303                 }
304
305                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
306                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
307                                    "mismatch");
308                         break;
309                 }
310
311                 if (!(ie.group_cipher & ssid->group_cipher)) {
312                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
313                                    "mismatch");
314                         break;
315                 }
316
317                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
318                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
319                                    "mismatch");
320                         break;
321                 }
322
323 #ifdef CONFIG_IEEE80211W
324                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
325                     ssid->ieee80211w == IEEE80211W_REQUIRED) {
326                         wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
327                                    "protection");
328                         break;
329                 }
330 #endif /* CONFIG_IEEE80211W */
331
332                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
333                 return 1;
334         }
335
336         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
337         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
338                 proto_match++;
339
340                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
341                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
342                         break;
343                 }
344                 if (!(ie.proto & ssid->proto)) {
345                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
346                                    "mismatch");
347                         break;
348                 }
349
350                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
351                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
352                                    "mismatch");
353                         break;
354                 }
355
356                 if (!(ie.group_cipher & ssid->group_cipher)) {
357                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
358                                    "mismatch");
359                         break;
360                 }
361
362                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
363                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
364                                    "mismatch");
365                         break;
366                 }
367
368                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
369                 return 1;
370         }
371
372         if (proto_match == 0)
373                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
374
375         return 0;
376 }
377
378
379 static struct wpa_scan_res *
380 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
381                               struct wpa_ssid *group,
382                               struct wpa_ssid **selected_ssid)
383 {
384         struct wpa_ssid *ssid;
385         struct wpa_scan_res *bss;
386         size_t i;
387         struct wpa_blacklist *e;
388         const u8 *ie;
389
390         wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
391         for (i = 0; i < wpa_s->scan_res->num; i++) {
392                 const u8 *ssid_;
393                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
394                 bss = wpa_s->scan_res->res[i];
395
396                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
397                 ssid_ = ie ? ie + 2 : (u8 *) "";
398                 ssid_len = ie ? ie[1] : 0;
399
400                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
401                 wpa_ie_len = ie ? ie[1] : 0;
402
403                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
404                 rsn_ie_len = ie ? ie[1] : 0;
405
406                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
407                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
408                            (int) i, MAC2STR(bss->bssid),
409                            wpa_ssid_txt(ssid_, ssid_len),
410                            wpa_ie_len, rsn_ie_len, bss->caps);
411
412                 e = wpa_blacklist_get(wpa_s, bss->bssid);
413                 if (e && e->count > 1) {
414                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
415                         continue;
416                 }
417
418                 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
419                         wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
420                         continue;
421                 }
422
423                 for (ssid = group; ssid; ssid = ssid->pnext) {
424                         int check_ssid = 1;
425
426                         if (ssid->disabled) {
427                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
428                                 continue;
429                         }
430
431 #ifdef CONFIG_WPS
432                         if (ssid->ssid_len == 0 &&
433                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
434                                 check_ssid = 0;
435 #endif /* CONFIG_WPS */
436
437                         if (check_ssid &&
438                             (ssid_len != ssid->ssid_len ||
439                              os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
440                                 wpa_printf(MSG_DEBUG, "   skip - "
441                                            "SSID mismatch");
442                                 continue;
443                         }
444
445                         if (ssid->bssid_set &&
446                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
447                         {
448                                 wpa_printf(MSG_DEBUG, "   skip - "
449                                            "BSSID mismatch");
450                                 continue;
451                         }
452
453                         if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
454                                 continue;
455
456                         wpa_printf(MSG_DEBUG, "   selected WPA AP "
457                                    MACSTR " ssid='%s'",
458                                    MAC2STR(bss->bssid),
459                                    wpa_ssid_txt(ssid_, ssid_len));
460                         *selected_ssid = ssid;
461                         return bss;
462                 }
463         }
464
465         return NULL;
466 }
467
468
469 static struct wpa_scan_res *
470 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
471                                   struct wpa_ssid *group,
472                                   struct wpa_ssid **selected_ssid)
473 {
474         struct wpa_ssid *ssid;
475         struct wpa_scan_res *bss;
476         size_t i;
477         struct wpa_blacklist *e;
478         const u8 *ie;
479
480         wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
481         for (i = 0; i < wpa_s->scan_res->num; i++) {
482                 const u8 *ssid_;
483                 u8 wpa_ie_len, rsn_ie_len, ssid_len;
484                 bss = wpa_s->scan_res->res[i];
485
486                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
487                 ssid_ = ie ? ie + 2 : (u8 *) "";
488                 ssid_len = ie ? ie[1] : 0;
489
490                 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
491                 wpa_ie_len = ie ? ie[1] : 0;
492
493                 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
494                 rsn_ie_len = ie ? ie[1] : 0;
495
496                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
497                            "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
498                            (int) i, MAC2STR(bss->bssid),
499                            wpa_ssid_txt(ssid_, ssid_len),
500                            wpa_ie_len, rsn_ie_len, bss->caps);
501
502                 e = wpa_blacklist_get(wpa_s, bss->bssid);
503                 if (e && e->count > 1) {
504                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
505                         continue;
506                 }
507
508                 for (ssid = group; ssid; ssid = ssid->pnext) {
509                         int check_ssid = ssid->ssid_len != 0;
510
511                         if (ssid->disabled) {
512                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
513                                 continue;
514                         }
515
516 #ifdef CONFIG_WPS
517                         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
518                                 /* Only allow wildcard SSID match if an AP
519                                  * advertises active WPS operation that matches
520                                  * with our mode. */
521                                 check_ssid = 1;
522                                 if (ssid->ssid_len == 0 &&
523                                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
524                                                               bss))
525                                         check_ssid = 0;
526                         }
527 #endif /* CONFIG_WPS */
528
529                         if (check_ssid &&
530                             (ssid_len != ssid->ssid_len ||
531                              os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
532                                 wpa_printf(MSG_DEBUG, "   skip - "
533                                            "SSID mismatch");
534                                 continue;
535                         }
536
537                         if (ssid->bssid_set &&
538                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
539                         {
540                                 wpa_printf(MSG_DEBUG, "   skip - "
541                                            "BSSID mismatch");
542                                 continue;
543                         }
544                         
545                         if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
546                             !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
547                             !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
548                         {
549                                 wpa_printf(MSG_DEBUG, "   skip - "
550                                            "non-WPA network not allowed");
551                                 continue;
552                         }
553
554                         if ((ssid->key_mgmt & 
555                              (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
556                               WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
557                               WPA_KEY_MGMT_IEEE8021X_SHA256 |
558                               WPA_KEY_MGMT_PSK_SHA256)) &&
559                             (wpa_ie_len != 0 || rsn_ie_len != 0)) {
560                                 wpa_printf(MSG_DEBUG, "   skip - "
561                                            "WPA network");
562                                 continue;
563                         }
564
565                         if (!wpa_supplicant_match_privacy(bss, ssid)) {
566                                 wpa_printf(MSG_DEBUG, "   skip - "
567                                            "privacy mismatch");
568                                 continue;
569                         }
570
571                         if (bss->caps & IEEE80211_CAP_IBSS) {
572                                 wpa_printf(MSG_DEBUG, "   skip - "
573                                            "IBSS (adhoc) network");
574                                 continue;
575                         }
576
577                         wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
578                                    MACSTR " ssid='%s'",
579                                    MAC2STR(bss->bssid),
580                                    wpa_ssid_txt(ssid_, ssid_len));
581                         *selected_ssid = ssid;
582                         return bss;
583                 }
584         }
585
586         return NULL;
587 }
588
589
590 static struct wpa_scan_res *
591 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
592                           struct wpa_ssid **selected_ssid)
593 {
594         struct wpa_scan_res *selected;
595
596         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
597                    group->priority);
598
599         /* First, try to find WPA-enabled AP */
600         selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
601         if (selected)
602                 return selected;
603
604         /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
605          * allows this. */
606         return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
607 }
608
609
610 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
611 {
612         int prio, timeout;
613         struct wpa_scan_res *selected = NULL;
614         struct wpa_ssid *ssid = NULL;
615
616         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
617                 if (wpa_s->conf->ap_scan == 2)
618                         return;
619                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
620                            "scanning again");
621                 timeout = 1;
622                 goto req_scan;
623         }
624
625         /*
626          * Don't post the results if this was the initial cached
627          * and there were no results.
628          */
629         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
630             wpa_s->scan_res->num == 0) {
631                 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
632                         "empty - not posting");
633         } else {
634                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
635                 wpa_supplicant_dbus_notify_scan_results(wpa_s);
636                 wpas_wps_notify_scan_results(wpa_s);
637         }
638
639         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)) ||
640             wpa_s->disconnected)
641                 return;
642
643         while (selected == NULL) {
644                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
645                         selected = wpa_supplicant_select_bss(
646                                 wpa_s, wpa_s->conf->pssid[prio], &ssid);
647                         if (selected)
648                                 break;
649                 }
650
651                 if (selected == NULL && wpa_s->blacklist) {
652                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
653                                    "and try again");
654                         wpa_blacklist_clear(wpa_s);
655                         wpa_s->blacklist_cleared++;
656                 } else if (selected == NULL) {
657                         break;
658                 }
659         }
660
661         if (selected) {
662                 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
663                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
664                                 "PBC session overlap");
665                         timeout = 10;
666                         goto req_scan;
667                 }
668
669                 /* Do not trigger new association unless the BSSID has changed
670                  * or if reassociation is requested. If we are in process of
671                  * associating with the selected BSSID, do not trigger new
672                  * attempt. */
673                 if (wpa_s->reassociate ||
674                     (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
675                      (wpa_s->wpa_state != WPA_ASSOCIATING ||
676                       os_memcmp(selected->bssid, wpa_s->pending_bssid,
677                                 ETH_ALEN) != 0))) {
678                         if (wpa_supplicant_scard_init(wpa_s, ssid)) {
679                                 wpa_supplicant_req_scan(wpa_s, 10, 0);
680                                 return;
681                         }
682                         wpa_supplicant_associate(wpa_s, selected, ssid);
683                 } else {
684                         wpa_printf(MSG_DEBUG, "Already associated with the "
685                                    "selected AP.");
686                 }
687                 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
688         } else {
689                 wpa_printf(MSG_DEBUG, "No suitable AP found.");
690                 timeout = 5;
691                 goto req_scan;
692         }
693
694         return;
695
696 req_scan:
697         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
698                 /*
699                  * Quick recovery if the initial scan results were not
700                  * complete when fetched before the first scan request.
701                  */
702                 wpa_s->scan_res_tried++;
703                 timeout = 0;
704         }
705         wpa_supplicant_req_scan(wpa_s, timeout, 0);
706 }
707 #endif /* CONFIG_NO_SCAN_PROCESSING */
708
709
710 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
711                                            union wpa_event_data *data)
712 {
713         int l, len, found = 0, wpa_found, rsn_found;
714         u8 *p;
715
716         wpa_printf(MSG_DEBUG, "Association info event");
717         if (data->assoc_info.req_ies)
718                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
719                             data->assoc_info.req_ies_len);
720         if (data->assoc_info.resp_ies)
721                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
722                             data->assoc_info.resp_ies_len);
723         if (data->assoc_info.beacon_ies)
724                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
725                             data->assoc_info.beacon_ies,
726                             data->assoc_info.beacon_ies_len);
727
728         p = data->assoc_info.req_ies;
729         l = data->assoc_info.req_ies_len;
730
731         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
732         while (p && l >= 2) {
733                 len = p[1] + 2;
734                 if (len > l) {
735                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
736                                     p, l);
737                         break;
738                 }
739                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
740                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
741                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
742                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
743                                 break;
744                         found = 1;
745                         wpa_find_assoc_pmkid(wpa_s);
746                         break;
747                 }
748                 l -= len;
749                 p += len;
750         }
751         if (!found && data->assoc_info.req_ies)
752                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
753
754         /* WPA/RSN IE from Beacon/ProbeResp */
755         p = data->assoc_info.beacon_ies;
756         l = data->assoc_info.beacon_ies_len;
757
758         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
759          */
760         wpa_found = rsn_found = 0;
761         while (p && l >= 2) {
762                 len = p[1] + 2;
763                 if (len > l) {
764                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
765                                     p, l);
766                         break;
767                 }
768                 if (!wpa_found &&
769                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
770                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
771                         wpa_found = 1;
772                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
773                 }
774
775                 if (!rsn_found &&
776                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
777                         rsn_found = 1;
778                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
779                 }
780
781                 l -= len;
782                 p += len;
783         }
784
785         if (!wpa_found && data->assoc_info.beacon_ies)
786                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
787         if (!rsn_found && data->assoc_info.beacon_ies)
788                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
789         if (wpa_found || rsn_found)
790                 wpa_s->ap_ies_from_associnfo = 1;
791 }
792
793
794 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
795                                        union wpa_event_data *data)
796 {
797         u8 bssid[ETH_ALEN];
798         int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
799
800         if (data)
801                 wpa_supplicant_event_associnfo(wpa_s, data);
802
803         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
804         if (wpa_s->use_client_mlme)
805                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
806         if (wpa_s->use_client_mlme ||
807             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
808              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
809                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
810                         MACSTR, MAC2STR(bssid));
811                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
812                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
813                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
814                         wpa_clear_keys(wpa_s, bssid);
815                 }
816                 if (wpa_supplicant_select_config(wpa_s) < 0) {
817                         wpa_supplicant_disassociate(
818                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
819                         return;
820                 }
821         }
822
823         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
824         if (wpa_s->current_ssid) {
825                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
826                  * initialized before association, but for other modes,
827                  * initialize PC/SC here, if the current configuration needs
828                  * smartcard or SIM/USIM. */
829                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
830         }
831         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
832         l2_packet_notify_auth_start(wpa_s->l2);
833
834         /*
835          * Set portEnabled first to FALSE in order to get EAP state machine out
836          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
837          * state machine may transit to AUTHENTICATING state based on obsolete
838          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
839          * AUTHENTICATED without ever giving chance to EAP state machine to
840          * reset the state.
841          */
842         if (!ft_completed) {
843                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
844                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
845         }
846         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
847                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
848         /* 802.1X::portControl = Auto */
849         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
850         wpa_s->eapol_received = 0;
851         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
852             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
853                 wpa_supplicant_cancel_auth_timeout(wpa_s);
854                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
855         } else if (!ft_completed) {
856                 /* Timeout for receiving the first EAPOL packet */
857                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
858         }
859         wpa_supplicant_cancel_scan(wpa_s);
860
861         if (wpa_s->driver_4way_handshake &&
862             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
863                 /*
864                  * We are done; the driver will take care of RSN 4-way
865                  * handshake.
866                  */
867                 wpa_supplicant_cancel_auth_timeout(wpa_s);
868                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
869                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
870                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
871         }
872 }
873
874
875 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
876 {
877         const u8 *bssid;
878
879         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
880                 /*
881                  * At least Host AP driver and a Prism3 card seemed to be
882                  * generating streams of disconnected events when configuring
883                  * IBSS for WPA-None. Ignore them for now.
884                  */
885                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
886                            "IBSS/WPA-None mode");
887                 return;
888         }
889
890         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
891             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
892                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
893                         "pre-shared key may be incorrect");
894         }
895         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
896                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
897         bssid = wpa_s->bssid;
898         if (is_zero_ether_addr(bssid))
899                 bssid = wpa_s->pending_bssid;
900         wpa_blacklist_add(wpa_s, bssid);
901         wpa_sm_notify_disassoc(wpa_s->wpa);
902         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
903                 "remove keys");
904         if (wpa_supplicant_dynamic_keys(wpa_s)) {
905                 wpa_s->keys_cleared = 0;
906                 wpa_clear_keys(wpa_s, wpa_s->bssid);
907         }
908         wpa_supplicant_mark_disassoc(wpa_s);
909 }
910
911
912 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
913 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
914                                                     void *sock_ctx)
915 {
916         struct wpa_supplicant *wpa_s = eloop_ctx;
917
918         if (!wpa_s->pending_mic_error_report)
919                 return;
920
921         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
922         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
923         wpa_s->pending_mic_error_report = 0;
924 }
925 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
926
927
928 static void
929 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
930                                          union wpa_event_data *data)
931 {
932         int pairwise;
933         struct os_time t;
934
935         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
936         pairwise = (data && data->michael_mic_failure.unicast);
937         os_get_time(&t);
938         if ((wpa_s->last_michael_mic_error &&
939              t.sec - wpa_s->last_michael_mic_error <= 60) ||
940             wpa_s->pending_mic_error_report) {
941                 if (wpa_s->pending_mic_error_report) {
942                         /*
943                          * Send the pending MIC error report immediately since
944                          * we are going to start countermeasures and AP better
945                          * do the same.
946                          */
947                         wpa_sm_key_request(wpa_s->wpa, 1,
948                                            wpa_s->pending_mic_error_pairwise);
949                 }
950
951                 /* Send the new MIC error report immediately since we are going
952                  * to start countermeasures and AP better do the same.
953                  */
954                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
955
956                 /* initialize countermeasures */
957                 wpa_s->countermeasures = 1;
958                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
959
960                 /*
961                  * Need to wait for completion of request frame. We do not get
962                  * any callback for the message completion, so just wait a
963                  * short while and hope for the best. */
964                 os_sleep(0, 10000);
965
966                 wpa_drv_set_countermeasures(wpa_s, 1);
967                 wpa_supplicant_deauthenticate(wpa_s,
968                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
969                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
970                                      wpa_s, NULL);
971                 eloop_register_timeout(60, 0,
972                                        wpa_supplicant_stop_countermeasures,
973                                        wpa_s, NULL);
974                 /* TODO: mark the AP rejected for 60 second. STA is
975                  * allowed to associate with another AP.. */
976         } else {
977 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
978                 if (wpa_s->mic_errors_seen) {
979                         /*
980                          * Reduce the effectiveness of Michael MIC error
981                          * reports as a means for attacking against TKIP if
982                          * more than one MIC failure is noticed with the same
983                          * PTK. We delay the transmission of the reports by a
984                          * random time between 0 and 60 seconds in order to
985                          * force the attacker wait 60 seconds before getting
986                          * the information on whether a frame resulted in a MIC
987                          * failure.
988                          */
989                         u8 rval[4];
990                         int sec;
991
992                         if (os_get_random(rval, sizeof(rval)) < 0)
993                                 sec = os_random() % 60;
994                         else
995                                 sec = WPA_GET_BE32(rval) % 60;
996                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
997                                    "seconds", sec);
998                         wpa_s->pending_mic_error_report = 1;
999                         wpa_s->pending_mic_error_pairwise = pairwise;
1000                         eloop_cancel_timeout(
1001                                 wpa_supplicant_delayed_mic_error_report,
1002                                 wpa_s, NULL);
1003                         eloop_register_timeout(
1004                                 sec, os_random() % 1000000,
1005                                 wpa_supplicant_delayed_mic_error_report,
1006                                 wpa_s, NULL);
1007                 } else {
1008                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1009                 }
1010 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1011                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1012 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1013         }
1014         wpa_s->last_michael_mic_error = t.sec;
1015         wpa_s->mic_errors_seen++;
1016 }
1017
1018
1019 static void
1020 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1021                                       union wpa_event_data *data)
1022 {
1023         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1024                 return;
1025
1026         switch (data->interface_status.ievent) {
1027         case EVENT_INTERFACE_ADDED:
1028                 if (!wpa_s->interface_removed)
1029                         break;
1030                 wpa_s->interface_removed = 0;
1031                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1032                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1033                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1034                                    "after interface was added.");
1035                 }
1036                 break;
1037         case EVENT_INTERFACE_REMOVED:
1038                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1039                 wpa_s->interface_removed = 1;
1040                 wpa_supplicant_mark_disassoc(wpa_s);
1041                 l2_packet_deinit(wpa_s->l2);
1042                 wpa_s->l2 = NULL;
1043                 break;
1044         }
1045 }
1046
1047
1048 #ifdef CONFIG_PEERKEY
1049 static void
1050 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1051                               union wpa_event_data *data)
1052 {
1053         if (data == NULL)
1054                 return;
1055         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1056 }
1057 #endif /* CONFIG_PEERKEY */
1058
1059
1060 #ifdef CONFIG_IEEE80211R
1061 static void
1062 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1063                                  union wpa_event_data *data)
1064 {
1065         if (data == NULL)
1066                 return;
1067
1068         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1069                                     data->ft_ies.ies_len,
1070                                     data->ft_ies.ft_action,
1071                                     data->ft_ies.target_ap) < 0) {
1072                 /* TODO: prevent MLME/driver from trying to associate? */
1073         }
1074 }
1075 #endif /* CONFIG_IEEE80211R */
1076
1077
1078 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1079                           union wpa_event_data *data)
1080 {
1081         struct wpa_supplicant *wpa_s = ctx;
1082
1083         switch (event) {
1084         case EVENT_ASSOC:
1085                 wpa_supplicant_event_assoc(wpa_s, data);
1086                 break;
1087         case EVENT_DISASSOC:
1088                 wpa_supplicant_event_disassoc(wpa_s);
1089                 break;
1090         case EVENT_MICHAEL_MIC_FAILURE:
1091                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1092                 break;
1093 #ifndef CONFIG_NO_SCAN_PROCESSING
1094         case EVENT_SCAN_RESULTS:
1095                 wpa_supplicant_event_scan_results(wpa_s);
1096                 break;
1097 #endif /* CONFIG_NO_SCAN_PROCESSING */
1098         case EVENT_ASSOCINFO:
1099                 wpa_supplicant_event_associnfo(wpa_s, data);
1100                 break;
1101         case EVENT_INTERFACE_STATUS:
1102                 wpa_supplicant_event_interface_status(wpa_s, data);
1103                 break;
1104         case EVENT_PMKID_CANDIDATE:
1105                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1106                 break;
1107 #ifdef CONFIG_PEERKEY
1108         case EVENT_STKSTART:
1109                 wpa_supplicant_event_stkstart(wpa_s, data);
1110                 break;
1111 #endif /* CONFIG_PEERKEY */
1112 #ifdef CONFIG_IEEE80211R
1113         case EVENT_FT_RESPONSE:
1114                 wpa_supplicant_event_ft_response(wpa_s, data);
1115                 break;
1116 #endif /* CONFIG_IEEE80211R */
1117         default:
1118                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1119                 break;
1120         }
1121 }