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