]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/events.c
This commit was generated by cvs2svn to compensate for changes in r176187,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2006, 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  * $FreeBSD$
15  */
16
17 #include "includes.h"
18
19 #include "common.h"
20 #include "eapol_sm.h"
21 #include "wpa.h"
22 #include "eloop.h"
23 #include "wpa_supplicant.h"
24 #include "config.h"
25 #include "l2_packet.h"
26 #include "wpa_supplicant_i.h"
27 #include "pcsc_funcs.h"
28 #include "preauth.h"
29 #include "pmksa_cache.h"
30 #include "wpa_ctrl.h"
31 #include "eap.h"
32 #include "ctrl_iface_dbus.h"
33
34
35 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
36 {
37         struct wpa_ssid *ssid;
38
39         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
40                 return 0;
41
42         ssid = wpa_supplicant_get_ssid(wpa_s);
43         if (ssid == NULL) {
44                 wpa_printf(MSG_INFO, "No network configuration found for the "
45                            "current AP");
46                 return -1;
47         }
48
49         if (ssid->disabled) {
50                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
51                 return -1;
52         }
53
54         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
55                    "AP");
56         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
57                               WPA_KEY_MGMT_WPA_NONE)) {
58                 u8 wpa_ie[80];
59                 size_t wpa_ie_len = sizeof(wpa_ie);
60                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
61                                           wpa_ie, &wpa_ie_len);
62         } else {
63                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
64         }
65
66         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
67                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
68         wpa_s->current_ssid = ssid;
69         wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
70         wpa_supplicant_initiate_eapol(wpa_s);
71
72         return 0;
73 }
74
75
76 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
77                                                 void *sock_ctx)
78 {
79         struct wpa_supplicant *wpa_s = eloop_ctx;
80
81         if (wpa_s->countermeasures) {
82                 wpa_s->countermeasures = 0;
83                 wpa_drv_set_countermeasures(wpa_s, 0);
84                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
85                 wpa_supplicant_req_scan(wpa_s, 0, 0);
86         }
87 }
88
89
90 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
91 {
92         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
93         os_memset(wpa_s->bssid, 0, ETH_ALEN);
94         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
95         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
96         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
97         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK)
98                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
99 }
100
101
102 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
103 {
104         struct wpa_ie_data ie;
105         int i, pmksa_set = -1;
106
107         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
108             ie.pmkid == NULL)
109                 return;
110
111         for (i = 0; i < ie.num_pmkid; i++) {
112                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
113                                                     ie.pmkid + i * PMKID_LEN,
114                                                     NULL, NULL, 0);
115                 if (pmksa_set == 0) {
116                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
117                         break;
118                 }
119         }
120
121         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
122                    "cache", pmksa_set == 0 ? "" : "not ");
123 }
124
125
126 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
127                                                  union wpa_event_data *data)
128 {
129         if (data == NULL) {
130                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
131                 return;
132         }
133         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
134                    " index=%d preauth=%d",
135                    MAC2STR(data->pmkid_candidate.bssid),
136                    data->pmkid_candidate.index,
137                    data->pmkid_candidate.preauth);
138
139         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
140                             data->pmkid_candidate.index,
141                             data->pmkid_candidate.preauth);
142 }
143
144
145 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
146 {
147         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
148             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
149                 return 0;
150
151 #ifdef IEEE8021X_EAPOL
152         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
153             wpa_s->current_ssid &&
154             !(wpa_s->current_ssid->eapol_flags &
155               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
156                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
157                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
158                  * plaintext or static WEP keys). */
159                 return 0;
160         }
161 #endif /* IEEE8021X_EAPOL */
162
163         return 1;
164 }
165
166
167 /**
168  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
169  * @wpa_s: pointer to wpa_supplicant data
170  * @ssid: Configuration data for the network
171  * Returns: 0 on success, -1 on failure
172  *
173  * This function is called when starting authentication with a network that is
174  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
175  */
176 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
177                               struct wpa_ssid *ssid)
178 {
179 #ifdef IEEE8021X_EAPOL
180         int aka = 0, sim = 0, type;
181
182         if (ssid->pcsc == NULL || wpa_s->scard != NULL)
183                 return 0;
184
185         if (ssid->eap_methods == NULL) {
186                 sim = 1;
187                 aka = 1;
188         } else {
189                 struct eap_method_type *eap = ssid->eap_methods;
190                 while (eap->vendor != EAP_VENDOR_IETF ||
191                        eap->method != EAP_TYPE_NONE) {
192                         if (eap->vendor == EAP_VENDOR_IETF) {
193                                 if (eap->method == EAP_TYPE_SIM)
194                                         sim = 1;
195                                 else if (eap->method == EAP_TYPE_AKA)
196                                         aka = 1;
197                         }
198                         eap++;
199                 }
200         }
201
202         if (eap_sm_get_eap_methods(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
203                 sim = 0;
204         if (eap_sm_get_eap_methods(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
205                 aka = 0;
206
207         if (!sim && !aka) {
208                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
209                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
210                 return 0;
211         }
212
213         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
214                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
215         if (sim && aka)
216                 type = SCARD_TRY_BOTH;
217         else if (aka)
218                 type = SCARD_USIM_ONLY;
219         else
220                 type = SCARD_GSM_SIM_ONLY;
221
222         wpa_s->scard = scard_init(type);
223         if (wpa_s->scard == NULL) {
224                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
225                            "(pcsc-lite)");
226                 return -1;
227         }
228         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
229         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
230 #endif /* IEEE8021X_EAPOL */
231
232         return 0;
233 }
234
235
236 static int wpa_supplicant_match_privacy(struct wpa_scan_result *bss,
237                                         struct wpa_ssid *ssid)
238 {
239         int i, privacy = 0;
240
241         if (ssid->mixed_cell)
242                 return 1;
243
244         for (i = 0; i < NUM_WEP_KEYS; i++) {
245                 if (ssid->wep_key_len[i]) {
246                         privacy = 1;
247                         break;
248                 }
249         }
250 #ifdef IEEE8021X_EAPOL
251         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
252             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
253                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
254                 privacy = 1;
255 #endif /* IEEE8021X_EAPOL */
256
257         if (bss->caps & IEEE80211_CAP_PRIVACY)
258                 return privacy;
259         return !privacy;
260 }
261
262
263 static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
264                                          struct wpa_scan_result *bss)
265 {
266         struct wpa_ie_data ie;
267         int proto_match = 0;
268
269         while ((ssid->proto & WPA_PROTO_RSN) && bss->rsn_ie_len > 0) {
270                 proto_match++;
271
272                 if (wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie)) {
273                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
274                         break;
275                 }
276                 if (!(ie.proto & ssid->proto)) {
277                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
278                                    "mismatch");
279                         break;
280                 }
281
282                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
283                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
284                                    "mismatch");
285                         break;
286                 }
287
288                 if (!(ie.group_cipher & ssid->group_cipher)) {
289                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
290                                    "mismatch");
291                         break;
292                 }
293
294                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
295                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
296                                    "mismatch");
297                         break;
298                 }
299
300 #ifdef CONFIG_IEEE80211W
301                 if (!(ie.capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION)
302                     && ssid->ieee80211w == IEEE80211W_REQUIRED) {
303                         wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
304                                    "protection");
305                         break;
306                 }
307 #endif /* CONFIG_IEEE80211W */
308
309                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
310                 return 1;
311         }
312
313         while ((ssid->proto & WPA_PROTO_WPA) && bss->wpa_ie_len > 0) {
314                 proto_match++;
315
316                 if (wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie)) {
317                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
318                         break;
319                 }
320                 if (!(ie.proto & ssid->proto)) {
321                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
322                                    "mismatch");
323                         break;
324                 }
325
326                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
327                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
328                                    "mismatch");
329                         break;
330                 }
331
332                 if (!(ie.group_cipher & ssid->group_cipher)) {
333                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
334                                    "mismatch");
335                         break;
336                 }
337
338                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
339                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
340                                    "mismatch");
341                         break;
342                 }
343
344                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
345                 return 1;
346         }
347
348         if (proto_match == 0)
349                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
350
351         return 0;
352 }
353
354
355 static struct wpa_scan_result *
356 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
357                           struct wpa_scan_result *results, int num,
358                           struct wpa_ssid **selected_ssid)
359 {
360         struct wpa_ssid *ssid;
361         struct wpa_scan_result *bss, *selected = NULL;
362         int i;
363         struct wpa_blacklist *e;
364
365         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
366                    group->priority);
367
368         bss = NULL;
369         ssid = NULL;
370         /* First, try to find WPA-enabled AP */
371         wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
372         for (i = 0; i < num && !selected; i++) {
373                 bss = &results[i];
374                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
375                            "wpa_ie_len=%lu rsn_ie_len=%lu caps=0x%x",
376                            i, MAC2STR(bss->bssid),
377                            wpa_ssid_txt(bss->ssid, bss->ssid_len),
378                            (unsigned long) bss->wpa_ie_len,
379                            (unsigned long) bss->rsn_ie_len, bss->caps);
380                 e = wpa_blacklist_get(wpa_s, bss->bssid);
381                 if (e && e->count > 1) {
382                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
383                         continue;
384                 }
385
386                 if (bss->wpa_ie_len == 0 && bss->rsn_ie_len == 0) {
387                         wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN IE");
388                         continue;
389                 }
390
391                 for (ssid = group; ssid; ssid = ssid->pnext) {
392                         if (ssid->disabled) {
393                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
394                                 continue;
395                         }
396                         if (bss->ssid_len != ssid->ssid_len ||
397                             os_memcmp(bss->ssid, ssid->ssid,
398                                       bss->ssid_len) != 0) {
399                                 wpa_printf(MSG_DEBUG, "   skip - "
400                                            "SSID mismatch");
401                                 continue;
402                         }
403                         if (ssid->bssid_set &&
404                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
405                         {
406                                 wpa_printf(MSG_DEBUG, "   skip - "
407                                            "BSSID mismatch");
408                                 continue;
409                         }
410                         if (wpa_supplicant_ssid_bss_match(ssid, bss)) {
411                                 selected = bss;
412                                 *selected_ssid = ssid;
413                                 wpa_printf(MSG_DEBUG, "   selected WPA AP "
414                                            MACSTR " ssid='%s'",
415                                            MAC2STR(bss->bssid),
416                                            wpa_ssid_txt(bss->ssid,
417                                                         bss->ssid_len));
418                                 break;
419                         }
420                 }
421         }
422
423         /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
424          * allows this. */
425         wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
426         for (i = 0; i < num && !selected; i++) {
427                 bss = &results[i];
428                 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
429                            "wpa_ie_len=%lu rsn_ie_len=%lu caps=0x%x",
430                            i, MAC2STR(bss->bssid),
431                            wpa_ssid_txt(bss->ssid, bss->ssid_len),
432                            (unsigned long) bss->wpa_ie_len,
433                            (unsigned long) bss->rsn_ie_len, bss->caps);
434                 e = wpa_blacklist_get(wpa_s, bss->bssid);
435                 if (e && e->count > 1) {
436                         wpa_printf(MSG_DEBUG, "   skip - blacklisted");
437                         continue;
438                 }
439                 for (ssid = group; ssid; ssid = ssid->pnext) {
440                         if (ssid->disabled) {
441                                 wpa_printf(MSG_DEBUG, "   skip - disabled");
442                                 continue;
443                         }
444                         if (bss->ssid_len != ssid->ssid_len ||
445                             os_memcmp(bss->ssid, ssid->ssid,
446                                       bss->ssid_len) != 0) {
447                                 wpa_printf(MSG_DEBUG, "   skip - "
448                                            "SSID mismatch");
449                                 continue;
450                         }
451
452                         if (ssid->bssid_set &&
453                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
454                         {
455                                 wpa_printf(MSG_DEBUG, "   skip - "
456                                            "BSSID mismatch");
457                                 continue;
458                         }
459                         
460                         if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
461                             !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
462                         {
463                                 wpa_printf(MSG_DEBUG, "   skip - "
464                                            "non-WPA network not allowed");
465                                 continue;
466                         }
467
468                         if ((ssid->key_mgmt & 
469                              (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK)) ||
470                             bss->wpa_ie_len != 0 || bss->rsn_ie_len != 0) {
471                                 wpa_printf(MSG_DEBUG, "   skip - "
472                                            "WPA network");
473                                 continue;
474                         }
475
476                         if (!wpa_supplicant_match_privacy(bss, ssid)) {
477                                 wpa_printf(MSG_DEBUG, "   skip - "
478                                            "privacy mismatch");
479                                 continue;
480                         }
481
482                         if (bss->caps & IEEE80211_CAP_IBSS) {
483                                 wpa_printf(MSG_DEBUG, "   skip - "
484                                            "IBSS (adhoc) network");
485                                 continue;
486                         }
487
488                         selected = bss;
489                         *selected_ssid = ssid;
490                         wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
491                                    MACSTR " ssid='%s'",
492                                    MAC2STR(bss->bssid),
493                                    wpa_ssid_txt(bss->ssid, bss->ssid_len));
494                         break;
495                 }
496         }
497
498         return selected;
499 }
500
501
502 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
503 {
504         int num, prio, timeout;
505         struct wpa_scan_result *selected = NULL;
506         struct wpa_ssid *ssid = NULL;
507         struct wpa_scan_result *results;
508
509         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
510                 if (wpa_s->conf->ap_scan == 2)
511                         return;
512                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
513                            "scanning again");
514                 timeout = 1;
515                 goto req_scan;
516         }
517
518         wpa_supplicant_dbus_notify_scan_results(wpa_s);
519
520         if (wpa_s->conf->ap_scan == 2)
521                 return;
522         results = wpa_s->scan_results;
523         num = wpa_s->num_scan_results;
524
525         while (selected == NULL) {
526                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
527                         selected = wpa_supplicant_select_bss(
528                                 wpa_s, wpa_s->conf->pssid[prio], results, num,
529                                 &ssid);
530                         if (selected)
531                                 break;
532                 }
533
534                 if (selected == NULL && wpa_s->blacklist) {
535                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
536                                    "and try again");
537                         wpa_blacklist_clear(wpa_s);
538                 } else if (selected == NULL) {
539                         break;
540                 }
541         }
542
543         if (selected) {
544                 /* Do not trigger new association unless the BSSID has changed
545                  * or if reassociation is requested. If we are in process of
546                  * associating with the selected BSSID, do not trigger new
547                  * attempt. */
548                 if (wpa_s->reassociate ||
549                     (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
550                      (wpa_s->wpa_state != WPA_ASSOCIATING ||
551                       os_memcmp(selected->bssid, wpa_s->pending_bssid,
552                                 ETH_ALEN) != 0))) {
553                         if (wpa_supplicant_scard_init(wpa_s, ssid)) {
554                                 wpa_supplicant_req_scan(wpa_s, 10, 0);
555                                 return;
556                         }
557                         wpa_supplicant_associate(wpa_s, selected, ssid);
558                 } else {
559                         wpa_printf(MSG_DEBUG, "Already associated with the "
560                                    "selected AP.");
561                 }
562                 rsn_preauth_scan_results(wpa_s->wpa, results, num);
563         } else {
564                 wpa_printf(MSG_DEBUG, "No suitable AP found.");
565                 timeout = 5;
566                 goto req_scan;
567         }
568
569         return;
570
571 req_scan:
572         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
573                 /*
574                  * Quick recovery if the initial scan results were not
575                  * complete when fetched before the first scan request.
576                  */
577                 wpa_s->scan_res_tried++;
578                 timeout = 0;
579         }
580         wpa_supplicant_req_scan(wpa_s, timeout, 0);
581 }
582
583
584 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
585                                            union wpa_event_data *data)
586 {
587         int l, len, found = 0, wpa_found, rsn_found;
588         u8 *p;
589
590         wpa_printf(MSG_DEBUG, "Association info event");
591         if (data->assoc_info.req_ies)
592                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
593                             data->assoc_info.req_ies_len);
594         if (data->assoc_info.resp_ies)
595                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
596                             data->assoc_info.resp_ies_len);
597         if (data->assoc_info.beacon_ies)
598                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
599                             data->assoc_info.beacon_ies,
600                             data->assoc_info.beacon_ies_len);
601
602         p = data->assoc_info.req_ies;
603         l = data->assoc_info.req_ies_len;
604
605         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
606         while (p && l >= 2) {
607                 len = p[1] + 2;
608                 if (len > l) {
609                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
610                                     p, l);
611                         break;
612                 }
613                 if ((p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
614                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
615                     (p[0] == RSN_INFO_ELEM && p[1] >= 2)) {
616                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
617                                 break;
618                         found = 1;
619                         wpa_find_assoc_pmkid(wpa_s);
620                         break;
621                 }
622                 l -= len;
623                 p += len;
624         }
625         if (!found && data->assoc_info.req_ies)
626                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
627
628         /* WPA/RSN IE from Beacon/ProbeResp */
629         p = data->assoc_info.beacon_ies;
630         l = data->assoc_info.beacon_ies_len;
631
632         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
633          */
634         wpa_found = rsn_found = 0;
635         while (p && l >= 2) {
636                 len = p[1] + 2;
637                 if (len > l) {
638                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
639                                     p, l);
640                         break;
641                 }
642                 if (!wpa_found &&
643                     p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
644                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
645                         wpa_found = 1;
646                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
647                 }
648
649                 if (!rsn_found &&
650                     p[0] == RSN_INFO_ELEM && p[1] >= 2) {
651                         rsn_found = 1;
652                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
653                 }
654
655                 l -= len;
656                 p += len;
657         }
658
659         if (!wpa_found && data->assoc_info.beacon_ies)
660                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
661         if (!rsn_found && data->assoc_info.beacon_ies)
662                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
663 }
664
665
666 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
667                                        union wpa_event_data *data)
668 {
669         u8 bssid[ETH_ALEN];
670
671         if (data)
672                 wpa_supplicant_event_associnfo(wpa_s, data);
673
674         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
675         if (wpa_s->use_client_mlme)
676                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
677         if (wpa_s->use_client_mlme ||
678             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
679              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
680                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
681                         MACSTR, MAC2STR(bssid));
682                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
683                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
684                 if (wpa_supplicant_dynamic_keys(wpa_s)) {
685                         wpa_clear_keys(wpa_s, bssid);
686                 }
687                 if (wpa_supplicant_select_config(wpa_s) < 0) {
688                         wpa_supplicant_disassociate(wpa_s,
689                                                     REASON_DEAUTH_LEAVING);
690                         return;
691                 }
692         }
693
694         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
695         if (wpa_s->current_ssid) {
696                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
697                  * initialized before association, but for other modes,
698                  * initialize PC/SC here, if the current configuration needs
699                  * smartcard or SIM/USIM. */
700                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
701         }
702         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
703         l2_packet_notify_auth_start(wpa_s->l2);
704
705         /*
706          * Set portEnabled first to FALSE in order to get EAP state machine out
707          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
708          * state machine may transit to AUTHENTICATING state based on obsolete
709          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
710          * AUTHENTICATED without ever giving chance to EAP state machine to
711          * reset the state.
712          */
713         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
714         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
715         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK)
716                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
717         /* 802.1X::portControl = Auto */
718         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
719         wpa_s->eapol_received = 0;
720         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
721             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
722                 wpa_supplicant_cancel_auth_timeout(wpa_s);
723                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
724         } else {
725                 /* Timeout for receiving the first EAPOL packet */
726                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
727         }
728         wpa_supplicant_cancel_scan(wpa_s);
729 }
730
731
732 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
733 {
734         const u8 *bssid;
735
736         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
737                 /*
738                  * At least Host AP driver and a Prism3 card seemed to be
739                  * generating streams of disconnected events when configuring
740                  * IBSS for WPA-None. Ignore them for now.
741                  */
742                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
743                            "IBSS/WPA-None mode");
744                 return;
745         }
746
747         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
748             wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
749                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
750                         "pre-shared key may be incorrect");
751         }
752         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
753                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
754         bssid = wpa_s->bssid;
755         if (os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
756                 bssid = wpa_s->pending_bssid;
757         wpa_blacklist_add(wpa_s, bssid);
758         wpa_sm_notify_disassoc(wpa_s->wpa);
759         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
760                 "remove keys");
761         if (wpa_supplicant_dynamic_keys(wpa_s)) {
762                 wpa_s->keys_cleared = 0;
763                 wpa_clear_keys(wpa_s, wpa_s->bssid);
764         }
765         wpa_supplicant_mark_disassoc(wpa_s);
766 }
767
768
769 static void
770 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
771                                          union wpa_event_data *data)
772 {
773         int pairwise;
774         struct os_time t;
775
776         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
777         pairwise = (data && data->michael_mic_failure.unicast);
778         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
779         os_get_time(&t);
780         if (wpa_s->last_michael_mic_error &&
781             t.sec - wpa_s->last_michael_mic_error <= 60) {
782                 /* initialize countermeasures */
783                 wpa_s->countermeasures = 1;
784                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
785
786                 /*
787                  * Need to wait for completion of request frame. We do not get
788                  * any callback for the message completion, so just wait a
789                  * short while and hope for the best. */
790                 os_sleep(0, 10000);
791
792                 wpa_drv_set_countermeasures(wpa_s, 1);
793                 wpa_supplicant_deauthenticate(wpa_s,
794                                               REASON_MICHAEL_MIC_FAILURE);
795                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
796                                      wpa_s, NULL);
797                 eloop_register_timeout(60, 0,
798                                        wpa_supplicant_stop_countermeasures,
799                                        wpa_s, NULL);
800                 /* TODO: mark the AP rejected for 60 second. STA is
801                  * allowed to associate with another AP.. */
802         }
803         wpa_s->last_michael_mic_error = t.sec;
804 }
805
806
807 #ifdef CONFIG_TERMINATE_ONLASTIF
808 static int any_interfaces(struct wpa_supplicant *head)
809 {
810         struct wpa_supplicant *wpa_s;
811
812         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
813                 if (!wpa_s->interface_removed)
814                         return 1;
815         return 0;
816 }
817 #endif /* CONFIG_TERMINATE_ONLASTIF */
818
819 static void
820 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
821                                       union wpa_event_data *data)
822 {
823         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
824                 return;
825
826         switch (data->interface_status.ievent) {
827         case EVENT_INTERFACE_ADDED:
828                 if (!wpa_s->interface_removed)
829                         break;
830                 wpa_s->interface_removed = 0;
831                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
832                 if (wpa_supplicant_driver_init(wpa_s, 1) < 0) {
833                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
834                                    "after interface was added.");
835                 }
836                 break;
837         case EVENT_INTERFACE_REMOVED:
838                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
839                 wpa_s->interface_removed = 1;
840                 wpa_supplicant_mark_disassoc(wpa_s);
841                 l2_packet_deinit(wpa_s->l2);
842                 wpa_s->l2 = NULL;
843 #ifdef CONFIG_TERMINATE_ONLASTIF
844                 /* check if last interface */
845                 if (!any_interfaces(wpa_s->global->ifaces))
846                         eloop_terminate();
847 #endif /* CONFIG_TERMINATE_ONLASTIF */
848                 break;
849         }
850 }
851
852
853 #ifdef CONFIG_PEERKEY
854 static void
855 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
856                               union wpa_event_data *data)
857 {
858         if (data == NULL)
859                 return;
860         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
861 }
862 #endif /* CONFIG_PEERKEY */
863
864
865 void wpa_supplicant_event(struct wpa_supplicant *wpa_s, wpa_event_type event,
866                           union wpa_event_data *data)
867 {
868         switch (event) {
869         case EVENT_ASSOC:
870                 wpa_supplicant_event_assoc(wpa_s, data);
871                 break;
872         case EVENT_DISASSOC:
873                 wpa_supplicant_event_disassoc(wpa_s);
874                 break;
875         case EVENT_MICHAEL_MIC_FAILURE:
876                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
877                 break;
878         case EVENT_SCAN_RESULTS:
879                 wpa_supplicant_event_scan_results(wpa_s);
880                 break;
881         case EVENT_ASSOCINFO:
882                 wpa_supplicant_event_associnfo(wpa_s, data);
883                 break;
884         case EVENT_INTERFACE_STATUS:
885                 wpa_supplicant_event_interface_status(wpa_s, data);
886                 break;
887         case EVENT_PMKID_CANDIDATE:
888                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
889                 break;
890 #ifdef CONFIG_PEERKEY
891         case EVENT_STKSTART:
892                 wpa_supplicant_event_stkstart(wpa_s, data);
893                 break;
894 #endif /* CONFIG_PEERKEY */
895         default:
896                 wpa_printf(MSG_INFO, "Unknown event %d", event);
897                 break;
898         }
899 }