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