]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/wpa_supplicant/events.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 (ssid->ssid_len != 0 &&
445                             (bss->ssid_len != ssid->ssid_len ||
446                              os_memcmp(bss->ssid, ssid->ssid,
447                                        bss->ssid_len) != 0)) {
448                                 wpa_printf(MSG_DEBUG, "   skip - "
449                                            "SSID mismatch");
450                                 continue;
451                         }
452
453                         if (ssid->bssid_set &&
454                             os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
455                         {
456                                 wpa_printf(MSG_DEBUG, "   skip - "
457                                            "BSSID mismatch");
458                                 continue;
459                         }
460                         
461                         if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
462                             !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
463                         {
464                                 wpa_printf(MSG_DEBUG, "   skip - "
465                                            "non-WPA network not allowed");
466                                 continue;
467                         }
468
469                         if ((ssid->key_mgmt & 
470                              (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK)) &&
471                             (bss->wpa_ie_len != 0 || bss->rsn_ie_len != 0)) {
472                                 wpa_printf(MSG_DEBUG, "   skip - "
473                                            "WPA network");
474                                 continue;
475                         }
476
477                         if (!wpa_supplicant_match_privacy(bss, ssid)) {
478                                 wpa_printf(MSG_DEBUG, "   skip - "
479                                            "privacy mismatch");
480                                 continue;
481                         }
482
483                         if (bss->caps & IEEE80211_CAP_IBSS) {
484                                 wpa_printf(MSG_DEBUG, "   skip - "
485                                            "IBSS (adhoc) network");
486                                 continue;
487                         }
488
489                         selected = bss;
490                         *selected_ssid = ssid;
491                         wpa_printf(MSG_DEBUG, "   selected non-WPA AP "
492                                    MACSTR " ssid='%s'",
493                                    MAC2STR(bss->bssid),
494                                    wpa_ssid_txt(bss->ssid, bss->ssid_len));
495                         break;
496                 }
497         }
498
499         return selected;
500 }
501
502
503 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
504 {
505         int num, prio, timeout;
506         struct wpa_scan_result *selected = NULL;
507         struct wpa_ssid *ssid = NULL;
508         struct wpa_scan_result *results;
509
510         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
511                 if (wpa_s->conf->ap_scan == 2)
512                         return;
513                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
514                            "scanning again");
515                 timeout = 1;
516                 goto req_scan;
517         }
518
519         wpa_supplicant_dbus_notify_scan_results(wpa_s);
520
521         if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
522                 return;
523         results = wpa_s->scan_results;
524         num = wpa_s->num_scan_results;
525
526         while (selected == NULL) {
527                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
528                         selected = wpa_supplicant_select_bss(
529                                 wpa_s, wpa_s->conf->pssid[prio], results, num,
530                                 &ssid);
531                         if (selected)
532                                 break;
533                 }
534
535                 if (selected == NULL && wpa_s->blacklist) {
536                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
537                                    "and try again");
538                         wpa_blacklist_clear(wpa_s);
539                 } else if (selected == NULL) {
540                         break;
541                 }
542         }
543
544         if (selected) {
545                 /* Do not trigger new association unless the BSSID has changed
546                  * or if reassociation is requested. If we are in process of
547                  * associating with the selected BSSID, do not trigger new
548                  * attempt. */
549                 if (wpa_s->reassociate ||
550                     (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
551                      (wpa_s->wpa_state != WPA_ASSOCIATING ||
552                       os_memcmp(selected->bssid, wpa_s->pending_bssid,
553                                 ETH_ALEN) != 0))) {
554                         if (wpa_supplicant_scard_init(wpa_s, ssid)) {
555                                 wpa_supplicant_req_scan(wpa_s, 10, 0);
556                                 return;
557                         }
558                         wpa_supplicant_associate(wpa_s, selected, ssid);
559                 } else {
560                         wpa_printf(MSG_DEBUG, "Already associated with the "
561                                    "selected AP.");
562                 }
563                 rsn_preauth_scan_results(wpa_s->wpa, results, num);
564         } else {
565                 wpa_printf(MSG_DEBUG, "No suitable AP found.");
566                 timeout = 5;
567                 goto req_scan;
568         }
569
570         return;
571
572 req_scan:
573         if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
574                 /*
575                  * Quick recovery if the initial scan results were not
576                  * complete when fetched before the first scan request.
577                  */
578                 wpa_s->scan_res_tried++;
579                 timeout = 0;
580         }
581         wpa_supplicant_req_scan(wpa_s, timeout, 0);
582 }
583
584
585 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
586                                            union wpa_event_data *data)
587 {
588         int l, len, found = 0, wpa_found, rsn_found;
589         u8 *p;
590
591         wpa_printf(MSG_DEBUG, "Association info event");
592         if (data->assoc_info.req_ies)
593                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
594                             data->assoc_info.req_ies_len);
595         if (data->assoc_info.resp_ies)
596                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
597                             data->assoc_info.resp_ies_len);
598         if (data->assoc_info.beacon_ies)
599                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
600                             data->assoc_info.beacon_ies,
601                             data->assoc_info.beacon_ies_len);
602
603         p = data->assoc_info.req_ies;
604         l = data->assoc_info.req_ies_len;
605
606         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
607         while (p && l >= 2) {
608                 len = p[1] + 2;
609                 if (len > l) {
610                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
611                                     p, l);
612                         break;
613                 }
614                 if ((p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
615                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
616                     (p[0] == RSN_INFO_ELEM && p[1] >= 2)) {
617                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
618                                 break;
619                         found = 1;
620                         wpa_find_assoc_pmkid(wpa_s);
621                         break;
622                 }
623                 l -= len;
624                 p += len;
625         }
626         if (!found && data->assoc_info.req_ies)
627                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
628
629         /* WPA/RSN IE from Beacon/ProbeResp */
630         p = data->assoc_info.beacon_ies;
631         l = data->assoc_info.beacon_ies_len;
632
633         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
634          */
635         wpa_found = rsn_found = 0;
636         while (p && l >= 2) {
637                 len = p[1] + 2;
638                 if (len > l) {
639                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
640                                     p, l);
641                         break;
642                 }
643                 if (!wpa_found &&
644                     p[0] == GENERIC_INFO_ELEM && p[1] >= 6 &&
645                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
646                         wpa_found = 1;
647                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
648                 }
649
650                 if (!rsn_found &&
651                     p[0] == RSN_INFO_ELEM && p[1] >= 2) {
652                         rsn_found = 1;
653                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
654                 }
655
656                 l -= len;
657                 p += len;
658         }
659
660         if (!wpa_found && data->assoc_info.beacon_ies)
661                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
662         if (!rsn_found && data->assoc_info.beacon_ies)
663                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
664 }
665
666
667 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
668                                        union wpa_event_data *data)
669 {
670         u8 bssid[ETH_ALEN];
671
672         if (data)
673                 wpa_supplicant_event_associnfo(wpa_s, data);
674
675         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
676         if (wpa_s->use_client_mlme)
677                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
678         if (wpa_s->use_client_mlme ||
679             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
680              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
681                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
682                         MACSTR, MAC2STR(bssid));
683                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
684                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
685                 if (wpa_supplicant_dynamic_keys(wpa_s)) {
686                         wpa_clear_keys(wpa_s, bssid);
687                 }
688                 if (wpa_supplicant_select_config(wpa_s) < 0) {
689                         wpa_supplicant_disassociate(wpa_s,
690                                                     REASON_DEAUTH_LEAVING);
691                         return;
692                 }
693         }
694
695         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
696         if (wpa_s->current_ssid) {
697                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
698                  * initialized before association, but for other modes,
699                  * initialize PC/SC here, if the current configuration needs
700                  * smartcard or SIM/USIM. */
701                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
702         }
703         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
704         l2_packet_notify_auth_start(wpa_s->l2);
705
706         /*
707          * Set portEnabled first to FALSE in order to get EAP state machine out
708          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
709          * state machine may transit to AUTHENTICATING state based on obsolete
710          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
711          * AUTHENTICATED without ever giving chance to EAP state machine to
712          * reset the state.
713          */
714         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
715         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
716         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK)
717                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
718         /* 802.1X::portControl = Auto */
719         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
720         wpa_s->eapol_received = 0;
721         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
722             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
723                 wpa_supplicant_cancel_auth_timeout(wpa_s);
724                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
725         } else {
726                 /* Timeout for receiving the first EAPOL packet */
727                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
728         }
729         wpa_supplicant_cancel_scan(wpa_s);
730 }
731
732
733 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
734 {
735         const u8 *bssid;
736
737         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
738                 /*
739                  * At least Host AP driver and a Prism3 card seemed to be
740                  * generating streams of disconnected events when configuring
741                  * IBSS for WPA-None. Ignore them for now.
742                  */
743                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
744                            "IBSS/WPA-None mode");
745                 return;
746         }
747
748         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
749             wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
750                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
751                         "pre-shared key may be incorrect");
752         }
753         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
754                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
755         bssid = wpa_s->bssid;
756         if (os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
757                 bssid = wpa_s->pending_bssid;
758         wpa_blacklist_add(wpa_s, bssid);
759         wpa_sm_notify_disassoc(wpa_s->wpa);
760         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
761                 "remove keys");
762         if (wpa_supplicant_dynamic_keys(wpa_s)) {
763                 wpa_s->keys_cleared = 0;
764                 wpa_clear_keys(wpa_s, wpa_s->bssid);
765         }
766         wpa_supplicant_mark_disassoc(wpa_s);
767 }
768
769
770 static void
771 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
772                                          union wpa_event_data *data)
773 {
774         int pairwise;
775         struct os_time t;
776
777         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
778         pairwise = (data && data->michael_mic_failure.unicast);
779         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
780         os_get_time(&t);
781         if (wpa_s->last_michael_mic_error &&
782             t.sec - wpa_s->last_michael_mic_error <= 60) {
783                 /* initialize countermeasures */
784                 wpa_s->countermeasures = 1;
785                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
786
787                 /*
788                  * Need to wait for completion of request frame. We do not get
789                  * any callback for the message completion, so just wait a
790                  * short while and hope for the best. */
791                 os_sleep(0, 10000);
792
793                 wpa_drv_set_countermeasures(wpa_s, 1);
794                 wpa_supplicant_deauthenticate(wpa_s,
795                                               REASON_MICHAEL_MIC_FAILURE);
796                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
797                                      wpa_s, NULL);
798                 eloop_register_timeout(60, 0,
799                                        wpa_supplicant_stop_countermeasures,
800                                        wpa_s, NULL);
801                 /* TODO: mark the AP rejected for 60 second. STA is
802                  * allowed to associate with another AP.. */
803         }
804         wpa_s->last_michael_mic_error = t.sec;
805 }
806
807
808 #ifdef CONFIG_TERMINATE_ONLASTIF
809 static int any_interfaces(struct wpa_supplicant *head)
810 {
811         struct wpa_supplicant *wpa_s;
812
813         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
814                 if (!wpa_s->interface_removed)
815                         return 1;
816         return 0;
817 }
818 #endif /* CONFIG_TERMINATE_ONLASTIF */
819
820 static void
821 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
822                                       union wpa_event_data *data)
823 {
824         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
825                 return;
826
827         switch (data->interface_status.ievent) {
828         case EVENT_INTERFACE_ADDED:
829                 if (!wpa_s->interface_removed)
830                         break;
831                 wpa_s->interface_removed = 0;
832                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
833                 if (wpa_supplicant_driver_init(wpa_s, 1) < 0) {
834                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
835                                    "after interface was added.");
836                 }
837                 break;
838         case EVENT_INTERFACE_REMOVED:
839                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
840                 wpa_s->interface_removed = 1;
841                 wpa_supplicant_mark_disassoc(wpa_s);
842                 l2_packet_deinit(wpa_s->l2);
843                 wpa_s->l2 = NULL;
844 #ifdef CONFIG_TERMINATE_ONLASTIF
845                 /* check if last interface */
846                 if (!any_interfaces(wpa_s->global->ifaces))
847                         eloop_terminate();
848 #endif /* CONFIG_TERMINATE_ONLASTIF */
849                 break;
850         }
851 }
852
853
854 #ifdef CONFIG_PEERKEY
855 static void
856 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
857                               union wpa_event_data *data)
858 {
859         if (data == NULL)
860                 return;
861         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
862 }
863 #endif /* CONFIG_PEERKEY */
864
865
866 void wpa_supplicant_event(struct wpa_supplicant *wpa_s, wpa_event_type event,
867                           union wpa_event_data *data)
868 {
869         switch (event) {
870         case EVENT_ASSOC:
871                 wpa_supplicant_event_assoc(wpa_s, data);
872                 break;
873         case EVENT_DISASSOC:
874                 wpa_supplicant_event_disassoc(wpa_s);
875                 break;
876         case EVENT_MICHAEL_MIC_FAILURE:
877                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
878                 break;
879         case EVENT_SCAN_RESULTS:
880                 wpa_supplicant_event_scan_results(wpa_s);
881                 break;
882         case EVENT_ASSOCINFO:
883                 wpa_supplicant_event_associnfo(wpa_s, data);
884                 break;
885         case EVENT_INTERFACE_STATUS:
886                 wpa_supplicant_event_interface_status(wpa_s, data);
887                 break;
888         case EVENT_PMKID_CANDIDATE:
889                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
890                 break;
891 #ifdef CONFIG_PEERKEY
892         case EVENT_STKSTART:
893                 wpa_supplicant_event_stkstart(wpa_s, data);
894                 break;
895 #endif /* CONFIG_PEERKEY */
896         default:
897                 wpa_printf(MSG_INFO, "Unknown event %d", event);
898                 break;
899         }
900 }