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