1 /*
2 * WPA Supplicant
3 * Copyright (c) 2003-2010, 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 * This file implements functions for registering and unregistering
15 * %wpa_supplicant interfaces. In addition, this file contains number of
16 * functions for managing network connections.
17 */
19 #include "includes.h"
21 #include "common.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "eap_peer/eap.h"
24 #include "eap_server/eap_methods.h"
25 #include "rsn_supp/wpa.h"
26 #include "eloop.h"
27 #include "config.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
33 #include "common/version.h"
34 #include "rsn_supp/preauth.h"
35 #include "rsn_supp/pmksa_cache.h"
36 #include "common/wpa_ctrl.h"
37 #include "mlme.h"
38 #include "common/ieee802_11_defs.h"
39 #include "p2p/p2p.h"
40 #include "blacklist.h"
41 #include "wpas_glue.h"
42 #include "wps_supplicant.h"
43 #include "ibss_rsn.h"
44 #include "sme.h"
45 #include "ap.h"
46 #include "p2p_supplicant.h"
47 #include "notify.h"
48 #include "bgscan.h"
49 #include "bss.h"
50 #include "scan.h"
52 const char *wpa_supplicant_version =
53 "wpa_supplicant v" VERSION_STR "\n"
54 "Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> and contributors";
56 const char *wpa_supplicant_license =
57 "This program is free software. You can distribute it and/or modify it\n"
58 "under the terms of the GNU General Public License version 2.\n"
59 "\n"
60 "Alternatively, this software may be distributed under the terms of the\n"
61 "BSD license. See README and COPYING for more details.\n"
62 #ifdef EAP_TLS_OPENSSL
63 "\nThis product includes software developed by the OpenSSL Project\n"
64 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
65 #endif /* EAP_TLS_OPENSSL */
66 ;
68 #ifndef CONFIG_NO_STDOUT_DEBUG
69 /* Long text divided into parts in order to fit in C89 strings size limits. */
70 const char *wpa_supplicant_full_license1 =
71 "This program is free software; you can redistribute it and/or modify\n"
72 "it under the terms of the GNU General Public License version 2 as\n"
73 "published by the Free Software Foundation.\n"
74 "\n"
75 "This program is distributed in the hope that it will be useful,\n"
76 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
77 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
78 "GNU General Public License for more details.\n"
79 "\n";
80 const char *wpa_supplicant_full_license2 =
81 "You should have received a copy of the GNU General Public License\n"
82 "along with this program; if not, write to the Free Software\n"
83 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
84 "\n"
85 "Alternatively, this software may be distributed under the terms of the\n"
86 "BSD license.\n"
87 "\n"
88 "Redistribution and use in source and binary forms, with or without\n"
89 "modification, are permitted provided that the following conditions are\n"
90 "met:\n"
91 "\n";
92 const char *wpa_supplicant_full_license3 =
93 "1. Redistributions of source code must retain the above copyright\n"
94 " notice, this list of conditions and the following disclaimer.\n"
95 "\n"
96 "2. Redistributions in binary form must reproduce the above copyright\n"
97 " notice, this list of conditions and the following disclaimer in the\n"
98 " documentation and/or other materials provided with the distribution.\n"
99 "\n";
100 const char *wpa_supplicant_full_license4 =
101 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
102 " names of its contributors may be used to endorse or promote products\n"
103 " derived from this software without specific prior written permission.\n"
104 "\n"
105 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
106 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
107 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
108 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
109 const char *wpa_supplicant_full_license5 =
110 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
111 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
112 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
113 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
114 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
115 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
116 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
117 "\n";
118 #endif /* CONFIG_NO_STDOUT_DEBUG */
120 extern int wpa_debug_level;
121 extern int wpa_debug_show_keys;
122 extern int wpa_debug_timestamp;
123 extern struct wpa_driver_ops *wpa_drivers[];
125 /* Configure default/group WEP keys for static WEP */
126 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
127 {
128 int i, set = 0;
130 for (i = 0; i < NUM_WEP_KEYS; i++) {
131 if (ssid->wep_key_len[i] == 0)
132 continue;
134 set = 1;
135 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
136 i, i == ssid->wep_tx_keyidx, NULL, 0,
137 ssid->wep_key[i], ssid->wep_key_len[i]);
138 }
140 return set;
141 }
144 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146 {
147 u8 key[32];
148 size_t keylen;
149 enum wpa_alg alg;
150 u8 seq[6] = { 0 };
152 /* IBSS/WPA-None uses only one key (Group) for both receiving and
153 * sending unicast and multicast packets. */
155 if (ssid->mode != WPAS_MODE_IBSS) {
156 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
157 "IBSS/ad-hoc) for WPA-None", ssid->mode);
158 return -1;
159 }
161 if (!ssid->psk_set) {
162 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
163 "WPA-None");
164 return -1;
165 }
167 switch (wpa_s->group_cipher) {
168 case WPA_CIPHER_CCMP:
169 os_memcpy(key, ssid->psk, 16);
170 keylen = 16;
171 alg = WPA_ALG_CCMP;
172 break;
173 case WPA_CIPHER_TKIP:
174 /* WPA-None uses the same Michael MIC key for both TX and RX */
175 os_memcpy(key, ssid->psk, 16 + 8);
176 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
177 keylen = 32;
178 alg = WPA_ALG_TKIP;
179 break;
180 default:
181 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
182 "WPA-None", wpa_s->group_cipher);
183 return -1;
184 }
186 /* TODO: should actually remember the previously used seq#, both for TX
187 * and RX from each STA.. */
189 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
190 }
193 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
194 {
195 struct wpa_supplicant *wpa_s = eloop_ctx;
196 const u8 *bssid = wpa_s->bssid;
197 if (is_zero_ether_addr(bssid))
198 bssid = wpa_s->pending_bssid;
199 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
200 MAC2STR(bssid));
201 wpa_blacklist_add(wpa_s, bssid);
202 wpa_sm_notify_disassoc(wpa_s->wpa);
203 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
204 wpa_s->reassociate = 1;
205 wpa_supplicant_req_scan(wpa_s, 0, 0);
206 }
209 /**
210 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
211 * @wpa_s: Pointer to wpa_supplicant data
212 * @sec: Number of seconds after which to time out authentication
213 * @usec: Number of microseconds after which to time out authentication
214 *
215 * This function is used to schedule a timeout for the current authentication
216 * attempt.
217 */
218 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
219 int sec, int usec)
220 {
221 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
222 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
223 return;
225 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
226 "%d usec", sec, usec);
227 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
228 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
229 }
232 /**
233 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
234 * @wpa_s: Pointer to wpa_supplicant data
235 *
236 * This function is used to cancel authentication timeout scheduled with
237 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
238 * been completed.
239 */
240 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
241 {
242 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
243 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
244 wpa_blacklist_del(wpa_s, wpa_s->bssid);
245 }
248 /**
249 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
250 * @wpa_s: Pointer to wpa_supplicant data
251 *
252 * This function is used to configure EAPOL state machine based on the selected
253 * authentication mode.
254 */
255 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
256 {
257 #ifdef IEEE8021X_EAPOL
258 struct eapol_config eapol_conf;
259 struct wpa_ssid *ssid = wpa_s->current_ssid;
261 #ifdef CONFIG_IBSS_RSN
262 if (ssid->mode == WPAS_MODE_IBSS &&
263 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
264 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
265 /*
266 * RSN IBSS authentication is per-STA and we can disable the
267 * per-BSSID EAPOL authentication.
268 */
269 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
270 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
271 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
272 return;
273 }
274 #endif /* CONFIG_IBSS_RSN */
276 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
277 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
279 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
280 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
281 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
282 else
283 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
285 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
286 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
287 eapol_conf.accept_802_1x_keys = 1;
288 eapol_conf.required_keys = 0;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
290 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
291 }
292 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
293 eapol_conf.required_keys |=
294 EAPOL_REQUIRE_KEY_BROADCAST;
295 }
297 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
298 eapol_conf.required_keys = 0;
299 }
300 if (wpa_s->conf)
301 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
302 eapol_conf.workaround = ssid->eap_workaround;
303 eapol_conf.eap_disabled =
304 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
305 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
306 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
307 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
308 #endif /* IEEE8021X_EAPOL */
309 }
312 /**
313 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
314 * @wpa_s: Pointer to wpa_supplicant data
315 * @ssid: Configuration data for the network
316 *
317 * This function is used to configure WPA state machine and related parameters
318 * to a mode where WPA is not enabled. This is called as part of the
319 * authentication configuration when the selected network does not use WPA.
320 */
321 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
322 struct wpa_ssid *ssid)
323 {
324 int i;
326 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
327 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
328 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
329 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
330 else
331 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
332 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
333 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
334 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
335 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
336 wpa_s->group_cipher = WPA_CIPHER_NONE;
337 wpa_s->mgmt_group_cipher = 0;
339 for (i = 0; i < NUM_WEP_KEYS; i++) {
340 if (ssid->wep_key_len[i] > 5) {
341 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
342 wpa_s->group_cipher = WPA_CIPHER_WEP104;
343 break;
344 } else if (ssid->wep_key_len[i] > 0) {
345 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
346 wpa_s->group_cipher = WPA_CIPHER_WEP40;
347 break;
348 }
349 }
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
354 wpa_s->pairwise_cipher);
355 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
356 #ifdef CONFIG_IEEE80211W
357 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
358 wpa_s->mgmt_group_cipher);
359 #endif /* CONFIG_IEEE80211W */
361 pmksa_cache_clear_current(wpa_s->wpa);
362 }
365 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
366 {
367 bgscan_deinit(wpa_s);
368 scard_deinit(wpa_s->scard);
369 wpa_s->scard = NULL;
370 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
371 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
372 l2_packet_deinit(wpa_s->l2);
373 wpa_s->l2 = NULL;
374 if (wpa_s->l2_br) {
375 l2_packet_deinit(wpa_s->l2_br);
376 wpa_s->l2_br = NULL;
377 }
379 if (wpa_s->ctrl_iface) {
380 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
381 wpa_s->ctrl_iface = NULL;
382 }
383 if (wpa_s->conf != NULL) {
384 struct wpa_ssid *ssid;
385 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
386 wpas_notify_network_removed(wpa_s, ssid);
387 wpa_config_free(wpa_s->conf);
388 wpa_s->conf = NULL;
389 }
391 os_free(wpa_s->confname);
392 wpa_s->confname = NULL;
394 wpa_sm_set_eapol(wpa_s->wpa, NULL);
395 eapol_sm_deinit(wpa_s->eapol);
396 wpa_s->eapol = NULL;
398 rsn_preauth_deinit(wpa_s->wpa);
400 pmksa_candidate_free(wpa_s->wpa);
401 wpa_sm_deinit(wpa_s->wpa);
402 wpa_s->wpa = NULL;
403 wpa_blacklist_clear(wpa_s);
405 wpa_bss_deinit(wpa_s);
407 wpa_supplicant_cancel_scan(wpa_s);
408 wpa_supplicant_cancel_auth_timeout(wpa_s);
410 ieee80211_sta_deinit(wpa_s);
412 wpas_wps_deinit(wpa_s);
414 wpabuf_free(wpa_s->pending_eapol_rx);
415 wpa_s->pending_eapol_rx = NULL;
417 #ifdef CONFIG_IBSS_RSN
418 ibss_rsn_deinit(wpa_s->ibss_rsn);
419 wpa_s->ibss_rsn = NULL;
420 #endif /* CONFIG_IBSS_RSN */
422 #ifdef CONFIG_SME
423 os_free(wpa_s->sme.ft_ies);
424 wpa_s->sme.ft_ies = NULL;
425 wpa_s->sme.ft_ies_len = 0;
426 #ifdef CONFIG_IEEE80211W
427 sme_stop_sa_query(wpa_s);
428 #endif /* CONFIG_IEEE80211W */
429 #endif /* CONFIG_SME */
431 #ifdef CONFIG_AP
432 wpa_supplicant_ap_deinit(wpa_s);
433 #endif /* CONFIG_AP */
435 #ifdef CONFIG_P2P
436 wpas_p2p_deinit(wpa_s);
437 #endif /* CONFIG_P2P */
439 os_free(wpa_s->next_scan_freqs);
440 wpa_s->next_scan_freqs = NULL;
441 }
444 /**
445 * wpa_clear_keys - Clear keys configured for the driver
446 * @wpa_s: Pointer to wpa_supplicant data
447 * @addr: Previously used BSSID or %NULL if not available
448 *
449 * This function clears the encryption keys that has been previously configured
450 * for the driver.
451 */
452 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
453 {
454 if (wpa_s->keys_cleared) {
455 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
456 * timing issues with keys being cleared just before new keys
457 * are set or just after association or something similar. This
458 * shows up in group key handshake failing often because of the
459 * client not receiving the first encrypted packets correctly.
460 * Skipping some of the extra key clearing steps seems to help
461 * in completing group key handshake more reliably. */
462 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
463 "skip key clearing");
464 return;
465 }
467 /* MLME-DELETEKEYS.request */
468 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
469 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
470 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
471 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
472 #ifdef CONFIG_IEEE80211W
473 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
474 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
475 #endif /* CONFIG_IEEE80211W */
476 if (addr) {
477 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
478 0);
479 /* MLME-SETPROTECTION.request(None) */
480 wpa_drv_mlme_setprotection(
481 wpa_s, addr,
482 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
483 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
484 }
485 wpa_s->keys_cleared = 1;
486 }
489 /**
490 * wpa_supplicant_state_txt - Get the connection state name as a text string
491 * @state: State (wpa_state; WPA_*)
492 * Returns: The state name as a printable text string
493 */
494 const char * wpa_supplicant_state_txt(enum wpa_states state)
495 {
496 switch (state) {
497 case WPA_DISCONNECTED:
498 return "DISCONNECTED";
499 case WPA_INACTIVE:
500 return "INACTIVE";
501 case WPA_INTERFACE_DISABLED:
502 return "INTERFACE_DISABLED";
503 case WPA_SCANNING:
504 return "SCANNING";
505 case WPA_AUTHENTICATING:
506 return "AUTHENTICATING";
507 case WPA_ASSOCIATING:
508 return "ASSOCIATING";
509 case WPA_ASSOCIATED:
510 return "ASSOCIATED";
511 case WPA_4WAY_HANDSHAKE:
512 return "4WAY_HANDSHAKE";
513 case WPA_GROUP_HANDSHAKE:
514 return "GROUP_HANDSHAKE";
515 case WPA_COMPLETED:
516 return "COMPLETED";
517 default:
518 return "UNKNOWN";
519 }
520 }
523 /**
524 * wpa_supplicant_set_state - Set current connection state
525 * @wpa_s: Pointer to wpa_supplicant data
526 * @state: The new connection state
527 *
528 * This function is called whenever the connection state changes, e.g.,
529 * association is completed for WPA/WPA2 4-Way Handshake is started.
530 */
531 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
532 enum wpa_states state)
533 {
534 enum wpa_states old_state = wpa_s->wpa_state;
536 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
537 wpa_supplicant_state_txt(wpa_s->wpa_state),
538 wpa_supplicant_state_txt(state));
540 if (state != WPA_SCANNING)
541 wpa_supplicant_notify_scanning(wpa_s, 0);
543 if (state == WPA_COMPLETED && wpa_s->new_connection) {
544 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
545 struct wpa_ssid *ssid = wpa_s->current_ssid;
546 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
547 MACSTR " completed %s [id=%d id_str=%s]",
548 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
549 "(reauth)" : "(auth)",
550 ssid ? ssid->id : -1,
551 ssid && ssid->id_str ? ssid->id_str : "");
552 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
553 wpa_s->new_connection = 0;
554 wpa_s->reassociated_connection = 1;
555 wpa_drv_set_operstate(wpa_s, 1);
556 #ifndef IEEE8021X_EAPOL
557 wpa_drv_set_supp_port(wpa_s, 1);
558 #endif /* IEEE8021X_EAPOL */
559 wpa_s->after_wps = 0;
560 #ifdef CONFIG_P2P
561 wpas_p2p_completed(wpa_s);
562 #endif /* CONFIG_P2P */
563 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
564 state == WPA_ASSOCIATED) {
565 wpa_s->new_connection = 1;
566 wpa_drv_set_operstate(wpa_s, 0);
567 #ifndef IEEE8021X_EAPOL
568 wpa_drv_set_supp_port(wpa_s, 0);
569 #endif /* IEEE8021X_EAPOL */
570 }
571 wpa_s->wpa_state = state;
573 if (wpa_s->wpa_state != old_state)
574 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
575 }
578 void wpa_supplicant_terminate_proc(struct wpa_global *global)
579 {
580 int pending = 0;
581 #ifdef CONFIG_WPS
582 struct wpa_supplicant *wpa_s = global->ifaces;
583 while (wpa_s) {
584 if (wpas_wps_terminate_pending(wpa_s) == 1)
585 pending = 1;
586 wpa_s = wpa_s->next;
587 }
588 #endif /* CONFIG_WPS */
589 if (pending)
590 return;
591 eloop_terminate();
592 }
595 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
596 {
597 struct wpa_global *global = signal_ctx;
598 struct wpa_supplicant *wpa_s;
599 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
600 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
601 "received", sig);
602 }
603 wpa_supplicant_terminate_proc(global);
604 }
607 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
608 {
609 enum wpa_states old_state = wpa_s->wpa_state;
611 wpa_s->pairwise_cipher = 0;
612 wpa_s->group_cipher = 0;
613 wpa_s->mgmt_group_cipher = 0;
614 wpa_s->key_mgmt = 0;
615 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
616 wpa_s->wpa_state = WPA_DISCONNECTED;
618 if (wpa_s->wpa_state != old_state)
619 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
620 }
623 /**
624 * wpa_supplicant_reload_configuration - Reload configuration data
625 * @wpa_s: Pointer to wpa_supplicant data
626 * Returns: 0 on success or -1 if configuration parsing failed
627 *
628 * This function can be used to request that the configuration data is reloaded
629 * (e.g., after configuration file change). This function is reloading
630 * configuration only for one interface, so this may need to be called multiple
631 * times if %wpa_supplicant is controlling multiple interfaces and all
632 * interfaces need reconfiguration.
633 */
634 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
635 {
636 struct wpa_config *conf;
637 struct wpa_ssid *old_ssid;
638 int reconf_ctrl;
639 int old_ap_scan;
641 if (wpa_s->confname == NULL)
642 return -1;
643 conf = wpa_config_read(wpa_s->confname);
644 if (conf == NULL) {
645 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
646 "file '%s' - exiting", wpa_s->confname);
647 return -1;
648 }
649 conf->changed_parameters = (unsigned int) -1;
651 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
652 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
653 os_strcmp(conf->ctrl_interface,
654 wpa_s->conf->ctrl_interface) != 0);
656 if (reconf_ctrl && wpa_s->ctrl_iface) {
657 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
658 wpa_s->ctrl_iface = NULL;
659 }
661 eapol_sm_invalidate_cached_session(wpa_s->eapol);
662 old_ssid = wpa_s->current_ssid;
663 wpa_s->current_ssid = NULL;
664 if (old_ssid != wpa_s->current_ssid)
665 wpas_notify_network_changed(wpa_s);
667 /*
668 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
669 * pkcs11_engine_path, pkcs11_module_path.
670 */
671 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
672 /*
673 * Clear forced success to clear EAP state for next
674 * authentication.
675 */
676 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
677 }
678 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
679 wpa_sm_set_config(wpa_s->wpa, NULL);
680 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
681 rsn_preauth_deinit(wpa_s->wpa);
683 old_ap_scan = wpa_s->conf->ap_scan;
684 wpa_config_free(wpa_s->conf);
685 wpa_s->conf = conf;
686 if (old_ap_scan != wpa_s->conf->ap_scan)
687 wpas_notify_ap_scan_changed(wpa_s);
689 if (reconf_ctrl)
690 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
692 wpa_supplicant_update_config(wpa_s);
694 wpa_supplicant_clear_status(wpa_s);
695 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
696 wpa_s->reassociate = 1;
697 wpa_supplicant_req_scan(wpa_s, 0, 0);
698 }
699 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
700 return 0;
701 }
704 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
705 {
706 struct wpa_global *global = signal_ctx;
707 struct wpa_supplicant *wpa_s;
708 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
709 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
710 sig);
711 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
712 wpa_supplicant_terminate_proc(global);
713 }
714 }
715 }
718 enum wpa_cipher cipher_suite2driver(int cipher)
719 {
720 switch (cipher) {
721 case WPA_CIPHER_NONE:
722 return CIPHER_NONE;
723 case WPA_CIPHER_WEP40:
724 return CIPHER_WEP40;
725 case WPA_CIPHER_WEP104:
726 return CIPHER_WEP104;
727 case WPA_CIPHER_CCMP:
728 return CIPHER_CCMP;
729 case WPA_CIPHER_TKIP:
730 default:
731 return CIPHER_TKIP;
732 }
733 }
736 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
737 {
738 switch (key_mgmt) {
739 case WPA_KEY_MGMT_NONE:
740 return KEY_MGMT_NONE;
741 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
742 return KEY_MGMT_802_1X_NO_WPA;
743 case WPA_KEY_MGMT_IEEE8021X:
744 return KEY_MGMT_802_1X;
745 case WPA_KEY_MGMT_WPA_NONE:
746 return KEY_MGMT_WPA_NONE;
747 case WPA_KEY_MGMT_FT_IEEE8021X:
748 return KEY_MGMT_FT_802_1X;
749 case WPA_KEY_MGMT_FT_PSK:
750 return KEY_MGMT_FT_PSK;
751 case WPA_KEY_MGMT_IEEE8021X_SHA256:
752 return KEY_MGMT_802_1X_SHA256;
753 case WPA_KEY_MGMT_PSK_SHA256:
754 return KEY_MGMT_PSK_SHA256;
755 case WPA_KEY_MGMT_WPS:
756 return KEY_MGMT_WPS;
757 case WPA_KEY_MGMT_PSK:
758 default:
759 return KEY_MGMT_PSK;
760 }
761 }
764 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
765 struct wpa_ssid *ssid,
766 struct wpa_ie_data *ie)
767 {
768 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
769 if (ret) {
770 if (ret == -2) {
771 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
772 "from association info");
773 }
774 return -1;
775 }
777 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
778 "cipher suites");
779 if (!(ie->group_cipher & ssid->group_cipher)) {
780 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
781 "cipher 0x%x (mask 0x%x) - reject",
782 ie->group_cipher, ssid->group_cipher);
783 return -1;
784 }
785 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
786 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
787 "cipher 0x%x (mask 0x%x) - reject",
788 ie->pairwise_cipher, ssid->pairwise_cipher);
789 return -1;
790 }
791 if (!(ie->key_mgmt & ssid->key_mgmt)) {
792 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
793 "management 0x%x (mask 0x%x) - reject",
794 ie->key_mgmt, ssid->key_mgmt);
795 return -1;
796 }
798 #ifdef CONFIG_IEEE80211W
799 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
800 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
801 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
802 "that does not support management frame protection - "
803 "reject");
804 return -1;
805 }
806 #endif /* CONFIG_IEEE80211W */
808 return 0;
809 }
812 /**
813 * wpa_supplicant_set_suites - Set authentication and encryption parameters
814 * @wpa_s: Pointer to wpa_supplicant data
815 * @bss: Scan results for the selected BSS, or %NULL if not available
816 * @ssid: Configuration data for the selected network
817 * @wpa_ie: Buffer for the WPA/RSN IE
818 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
819 * used buffer length in case the functions returns success.
820 * Returns: 0 on success or -1 on failure
821 *
822 * This function is used to configure authentication and encryption parameters
823 * based on the network configuration and scan result for the selected BSS (if
824 * available).
825 */
826 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
827 struct wpa_bss *bss, struct wpa_ssid *ssid,
828 u8 *wpa_ie, size_t *wpa_ie_len)
829 {
830 struct wpa_ie_data ie;
831 int sel, proto;
832 const u8 *bss_wpa, *bss_rsn;
834 if (bss) {
835 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
836 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
837 } else
838 bss_wpa = bss_rsn = NULL;
840 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
841 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
842 (ie.group_cipher & ssid->group_cipher) &&
843 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
844 (ie.key_mgmt & ssid->key_mgmt)) {
845 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
846 proto = WPA_PROTO_RSN;
847 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
848 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
849 (ie.group_cipher & ssid->group_cipher) &&
850 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
851 (ie.key_mgmt & ssid->key_mgmt)) {
852 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
853 proto = WPA_PROTO_WPA;
854 } else if (bss) {
855 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
856 return -1;
857 } else {
858 if (ssid->proto & WPA_PROTO_RSN)
859 proto = WPA_PROTO_RSN;
860 else
861 proto = WPA_PROTO_WPA;
862 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
863 os_memset(&ie, 0, sizeof(ie));
864 ie.group_cipher = ssid->group_cipher;
865 ie.pairwise_cipher = ssid->pairwise_cipher;
866 ie.key_mgmt = ssid->key_mgmt;
867 #ifdef CONFIG_IEEE80211W
868 ie.mgmt_group_cipher =
869 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
870 WPA_CIPHER_AES_128_CMAC : 0;
871 #endif /* CONFIG_IEEE80211W */
872 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
873 "based on configuration");
874 } else
875 proto = ie.proto;
876 }
878 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
879 "pairwise %d key_mgmt %d proto %d",
880 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
881 #ifdef CONFIG_IEEE80211W
882 if (ssid->ieee80211w) {
883 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
884 ie.mgmt_group_cipher);
885 }
886 #endif /* CONFIG_IEEE80211W */
888 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
889 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
890 !!(ssid->proto & WPA_PROTO_RSN));
892 if (bss || !wpa_s->ap_ies_from_associnfo) {
893 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
894 bss_wpa ? 2 + bss_wpa[1] : 0) ||
895 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
896 bss_rsn ? 2 + bss_rsn[1] : 0))
897 return -1;
898 }
900 sel = ie.group_cipher & ssid->group_cipher;
901 if (sel & WPA_CIPHER_CCMP) {
902 wpa_s->group_cipher = WPA_CIPHER_CCMP;
903 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
904 } else if (sel & WPA_CIPHER_TKIP) {
905 wpa_s->group_cipher = WPA_CIPHER_TKIP;
906 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
907 } else if (sel & WPA_CIPHER_WEP104) {
908 wpa_s->group_cipher = WPA_CIPHER_WEP104;
909 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
910 } else if (sel & WPA_CIPHER_WEP40) {
911 wpa_s->group_cipher = WPA_CIPHER_WEP40;
912 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
913 } else {
914 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
915 "cipher");
916 return -1;
917 }
919 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
920 if (sel & WPA_CIPHER_CCMP) {
921 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
922 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
923 } else if (sel & WPA_CIPHER_TKIP) {
924 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
925 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
926 } else if (sel & WPA_CIPHER_NONE) {
927 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
928 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
929 } else {
930 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
931 "cipher");
932 return -1;
933 }
935 sel = ie.key_mgmt & ssid->key_mgmt;
936 if (0) {
937 #ifdef CONFIG_IEEE80211R
938 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
939 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
940 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
941 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
942 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
943 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
944 #endif /* CONFIG_IEEE80211R */
945 #ifdef CONFIG_IEEE80211W
946 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
947 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
948 wpa_dbg(wpa_s, MSG_DEBUG,
949 "WPA: using KEY_MGMT 802.1X with SHA256");
950 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
951 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
952 wpa_dbg(wpa_s, MSG_DEBUG,
953 "WPA: using KEY_MGMT PSK with SHA256");
954 #endif /* CONFIG_IEEE80211W */
955 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
956 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
957 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
958 } else if (sel & WPA_KEY_MGMT_PSK) {
959 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
960 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
961 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
962 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
963 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
964 } else {
965 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
966 "authenticated key management type");
967 return -1;
968 }
970 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
971 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
972 wpa_s->pairwise_cipher);
973 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
975 #ifdef CONFIG_IEEE80211W
976 sel = ie.mgmt_group_cipher;
977 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
978 !(ie.capabilities & WPA_CAPABILITY_MFPC))
979 sel = 0;
980 if (sel & WPA_CIPHER_AES_128_CMAC) {
981 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
982 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
983 "AES-128-CMAC");
984 } else {
985 wpa_s->mgmt_group_cipher = 0;
986 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
987 }
988 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
989 wpa_s->mgmt_group_cipher);
990 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
991 #endif /* CONFIG_IEEE80211W */
993 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
994 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
995 return -1;
996 }
998 if (ssid->key_mgmt &
999 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1000 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1001 else
1002 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1004 return 0;
1005 }
1008 /**
1009 * wpa_supplicant_associate - Request association
1010 * @wpa_s: Pointer to wpa_supplicant data
1011 * @bss: Scan results for the selected BSS, or %NULL if not available
1012 * @ssid: Configuration data for the selected network
1013 *
1014 * This function is used to request %wpa_supplicant to associate with a BSS.
1015 */
1016 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1017 struct wpa_bss *bss, struct wpa_ssid *ssid)
1018 {
1019 u8 wpa_ie[200];
1020 size_t wpa_ie_len;
1021 int use_crypt, ret, i, bssid_changed;
1022 int algs = WPA_AUTH_ALG_OPEN;
1023 enum wpa_cipher cipher_pairwise, cipher_group;
1024 struct wpa_driver_associate_params params;
1025 int wep_keys_set = 0;
1026 struct wpa_driver_capa capa;
1027 int assoc_failed = 0;
1028 struct wpa_ssid *old_ssid;
1030 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1031 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1032 #ifdef CONFIG_AP
1033 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1034 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1035 "mode");
1036 return;
1037 }
1038 wpa_supplicant_create_ap(wpa_s, ssid);
1039 wpa_s->current_bss = bss;
1040 #else /* CONFIG_AP */
1041 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1042 "the build");
1043 #endif /* CONFIG_AP */
1044 return;
1045 }
1047 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1048 ssid->mode == IEEE80211_MODE_INFRA) {
1049 sme_authenticate(wpa_s, bss, ssid);
1050 return;
1051 }
1053 os_memset(¶ms, 0, sizeof(params));
1054 wpa_s->reassociate = 0;
1055 if (bss) {
1056 #ifdef CONFIG_IEEE80211R
1057 const u8 *ie, *md = NULL;
1058 #endif /* CONFIG_IEEE80211R */
1059 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1060 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1061 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1062 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1063 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1064 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1065 if (bssid_changed)
1066 wpas_notify_bssid_changed(wpa_s);
1067 #ifdef CONFIG_IEEE80211R
1068 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1069 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1070 md = ie + 2;
1071 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1072 if (md) {
1073 /* Prepare for the next transition */
1074 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1075 }
1076 #endif /* CONFIG_IEEE80211R */
1077 #ifdef CONFIG_WPS
1078 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1079 wpa_s->conf->ap_scan == 2 &&
1080 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1081 /* Use ap_scan==1 style network selection to find the network
1082 */
1083 wpa_s->scan_req = 2;
1084 wpa_s->reassociate = 1;
1085 wpa_supplicant_req_scan(wpa_s, 0, 0);
1086 return;
1087 #endif /* CONFIG_WPS */
1088 } else {
1089 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1090 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1091 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1092 }
1093 wpa_supplicant_cancel_scan(wpa_s);
1095 /* Starting new association, so clear the possibly used WPA IE from the
1096 * previous association. */
1097 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1099 #ifdef IEEE8021X_EAPOL
1100 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1101 if (ssid->leap) {
1102 if (ssid->non_leap == 0)
1103 algs = WPA_AUTH_ALG_LEAP;
1104 else
1105 algs |= WPA_AUTH_ALG_LEAP;
1106 }
1107 }
1108 #endif /* IEEE8021X_EAPOL */
1109 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1110 if (ssid->auth_alg) {
1111 algs = ssid->auth_alg;
1112 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1113 "0x%x", algs);
1114 }
1116 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1117 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1118 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1119 WPA_KEY_MGMT_FT_IEEE8021X |
1120 WPA_KEY_MGMT_FT_PSK |
1121 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1122 WPA_KEY_MGMT_PSK_SHA256))) {
1123 int try_opportunistic;
1124 try_opportunistic = ssid->proactive_key_caching &&
1125 (ssid->proto & WPA_PROTO_RSN);
1126 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1127 wpa_s->current_ssid,
1128 try_opportunistic) == 0)
1129 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1130 wpa_ie_len = sizeof(wpa_ie);
1131 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1132 wpa_ie, &wpa_ie_len)) {
1133 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1134 "key management and encryption suites");
1135 return;
1136 }
1137 } else if (ssid->key_mgmt &
1138 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1139 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1140 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1141 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1142 wpa_ie_len = sizeof(wpa_ie);
1143 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1144 wpa_ie, &wpa_ie_len)) {
1145 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1146 "key management and encryption suites (no "
1147 "scan results)");
1148 return;
1149 }
1150 #ifdef CONFIG_WPS
1151 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1152 struct wpabuf *wps_ie;
1153 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1154 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1155 wpa_ie_len = wpabuf_len(wps_ie);
1156 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1157 } else
1158 wpa_ie_len = 0;
1159 wpabuf_free(wps_ie);
1160 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1161 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1162 params.wps = WPS_MODE_PRIVACY;
1163 else
1164 params.wps = WPS_MODE_OPEN;
1165 #endif /* CONFIG_WPS */
1166 } else {
1167 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1168 wpa_ie_len = 0;
1169 }
1171 #ifdef CONFIG_P2P
1172 if (wpa_s->global->p2p) {
1173 u8 *pos;
1174 size_t len;
1175 int res;
1176 int p2p_group;
1177 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1178 pos = wpa_ie + wpa_ie_len;
1179 len = sizeof(wpa_ie) - wpa_ie_len;
1180 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1181 if (res >= 0)
1182 wpa_ie_len += res;
1183 }
1185 wpa_s->cross_connect_disallowed = 0;
1186 if (bss) {
1187 struct wpabuf *p2p;
1188 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1189 if (p2p) {
1190 wpa_s->cross_connect_disallowed =
1191 p2p_get_cross_connect_disallowed(p2p);
1192 wpabuf_free(p2p);
1193 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1194 "connection",
1195 wpa_s->cross_connect_disallowed ?
1196 "disallows" : "allows");
1197 }
1198 }
1199 #endif /* CONFIG_P2P */
1201 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1202 use_crypt = 1;
1203 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1204 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1205 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1206 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1207 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1208 use_crypt = 0;
1209 if (wpa_set_wep_keys(wpa_s, ssid)) {
1210 use_crypt = 1;
1211 wep_keys_set = 1;
1212 }
1213 }
1214 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1215 use_crypt = 0;
1217 #ifdef IEEE8021X_EAPOL
1218 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1219 if ((ssid->eapol_flags &
1220 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1221 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1222 !wep_keys_set) {
1223 use_crypt = 0;
1224 } else {
1225 /* Assume that dynamic WEP-104 keys will be used and
1226 * set cipher suites in order for drivers to expect
1227 * encryption. */
1228 cipher_pairwise = cipher_group = CIPHER_WEP104;
1229 }
1230 }
1231 #endif /* IEEE8021X_EAPOL */
1233 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1234 /* Set the key before (and later after) association */
1235 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1236 }
1238 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1239 if (bss) {
1240 params.bssid = bss->bssid;
1241 params.ssid = bss->ssid;
1242 params.ssid_len = bss->ssid_len;
1243 params.freq = bss->freq;
1244 } else {
1245 params.ssid = ssid->ssid;
1246 params.ssid_len = ssid->ssid_len;
1247 }
1248 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1249 params.freq == 0)
1250 params.freq = ssid->frequency; /* Initial channel for IBSS */
1251 params.wpa_ie = wpa_ie;
1252 params.wpa_ie_len = wpa_ie_len;
1253 params.pairwise_suite = cipher_pairwise;
1254 params.group_suite = cipher_group;
1255 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1256 params.auth_alg = algs;
1257 params.mode = ssid->mode;
1258 for (i = 0; i < NUM_WEP_KEYS; i++) {
1259 if (ssid->wep_key_len[i])
1260 params.wep_key[i] = ssid->wep_key[i];
1261 params.wep_key_len[i] = ssid->wep_key_len[i];
1262 }
1263 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1265 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1266 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1267 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1268 params.passphrase = ssid->passphrase;
1269 if (ssid->psk_set)
1270 params.psk = ssid->psk;
1271 }
1273 params.drop_unencrypted = use_crypt;
1275 #ifdef CONFIG_IEEE80211W
1276 params.mgmt_frame_protection = ssid->ieee80211w;
1277 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1278 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1279 struct wpa_ie_data ie;
1280 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1281 ie.capabilities &
1282 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1283 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1284 "MFP: require MFP");
1285 params.mgmt_frame_protection =
1286 MGMT_FRAME_PROTECTION_REQUIRED;
1287 }
1288 }
1289 #endif /* CONFIG_IEEE80211W */
1291 #ifdef CONFIG_P2P
1292 if (wpa_s->global->p2p &&
1293 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1294 params.p2p = 1;
1295 #endif /* CONFIG_P2P */
1297 if (wpa_s->parent->set_sta_uapsd)
1298 params.uapsd = wpa_s->parent->sta_uapsd;
1299 else
1300 params.uapsd = -1;
1302 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1303 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1304 else
1305 ret = wpa_drv_associate(wpa_s, ¶ms);
1306 if (ret < 0) {
1307 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1308 "failed");
1309 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1310 /*
1311 * The driver is known to mean what is saying, so we
1312 * can stop right here; the association will not
1313 * succeed.
1314 */
1315 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1316 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1317 return;
1318 }
1319 /* try to continue anyway; new association will be tried again
1320 * after timeout */
1321 assoc_failed = 1;
1322 }
1324 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1325 /* Set the key after the association just in case association
1326 * cleared the previously configured key. */
1327 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1328 /* No need to timeout authentication since there is no key
1329 * management. */
1330 wpa_supplicant_cancel_auth_timeout(wpa_s);
1331 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1332 #ifdef CONFIG_IBSS_RSN
1333 } else if (ssid->mode == WPAS_MODE_IBSS &&
1334 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1335 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1336 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1337 /*
1338 * RSN IBSS authentication is per-STA and we can disable the
1339 * per-BSSID authentication.
1340 */
1341 wpa_supplicant_cancel_auth_timeout(wpa_s);
1342 #endif /* CONFIG_IBSS_RSN */
1343 } else {
1344 /* Timeout for IEEE 802.11 authentication and association */
1345 int timeout = 60;
1347 if (assoc_failed) {
1348 /* give IBSS a bit more time */
1349 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1350 } else if (wpa_s->conf->ap_scan == 1) {
1351 /* give IBSS a bit more time */
1352 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1353 }
1354 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1355 }
1357 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1358 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1359 /* Set static WEP keys again */
1360 wpa_set_wep_keys(wpa_s, ssid);
1361 }
1363 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1364 /*
1365 * Do not allow EAP session resumption between different
1366 * network configurations.
1367 */
1368 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1369 }
1370 old_ssid = wpa_s->current_ssid;
1371 wpa_s->current_ssid = ssid;
1372 wpa_s->current_bss = bss;
1373 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1374 wpa_supplicant_initiate_eapol(wpa_s);
1375 if (old_ssid != wpa_s->current_ssid)
1376 wpas_notify_network_changed(wpa_s);
1377 }
1380 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1381 const u8 *addr)
1382 {
1383 struct wpa_ssid *old_ssid;
1385 wpa_clear_keys(wpa_s, addr);
1386 wpa_supplicant_mark_disassoc(wpa_s);
1387 old_ssid = wpa_s->current_ssid;
1388 wpa_s->current_ssid = NULL;
1389 wpa_s->current_bss = NULL;
1390 wpa_sm_set_config(wpa_s->wpa, NULL);
1391 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1392 if (old_ssid != wpa_s->current_ssid)
1393 wpas_notify_network_changed(wpa_s);
1394 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1395 }
1398 /**
1399 * wpa_supplicant_disassociate - Disassociate the current connection
1400 * @wpa_s: Pointer to wpa_supplicant data
1401 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1402 *
1403 * This function is used to request %wpa_supplicant to disassociate with the
1404 * current AP.
1405 */
1406 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1407 int reason_code)
1408 {
1409 u8 *addr = NULL;
1411 if (!is_zero_ether_addr(wpa_s->bssid)) {
1412 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1413 ieee80211_sta_disassociate(wpa_s, reason_code);
1414 else
1415 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1416 addr = wpa_s->bssid;
1417 }
1419 wpa_supplicant_clear_connection(wpa_s, addr);
1420 }
1423 /**
1424 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1425 * @wpa_s: Pointer to wpa_supplicant data
1426 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1427 *
1428 * This function is used to request %wpa_supplicant to deauthenticate from the
1429 * current AP.
1430 */
1431 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1432 int reason_code)
1433 {
1434 u8 *addr = NULL;
1436 if (!is_zero_ether_addr(wpa_s->bssid)) {
1437 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1438 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1439 else
1440 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1441 reason_code);
1442 addr = wpa_s->bssid;
1443 }
1445 wpa_supplicant_clear_connection(wpa_s, addr);
1446 }
1449 /**
1450 * wpa_supplicant_enable_network - Mark a configured network as enabled
1451 * @wpa_s: wpa_supplicant structure for a network interface
1452 * @ssid: wpa_ssid structure for a configured network or %NULL
1453 *
1454 * Enables the specified network or all networks if no network specified.
1455 */
1456 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1457 struct wpa_ssid *ssid)
1458 {
1459 struct wpa_ssid *other_ssid;
1460 int was_disabled;
1462 if (ssid == NULL) {
1463 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1464 other_ssid = other_ssid->next) {
1465 if (other_ssid->disabled == 2)
1466 continue; /* do not change persistent P2P group
1467 * data */
1468 if (other_ssid == wpa_s->current_ssid &&
1469 other_ssid->disabled)
1470 wpa_s->reassociate = 1;
1472 was_disabled = other_ssid->disabled;
1474 other_ssid->disabled = 0;
1476 if (was_disabled != other_ssid->disabled)
1477 wpas_notify_network_enabled_changed(
1478 wpa_s, other_ssid);
1479 }
1480 if (wpa_s->reassociate)
1481 wpa_supplicant_req_scan(wpa_s, 0, 0);
1482 } else if (ssid->disabled && ssid->disabled != 2) {
1483 if (wpa_s->current_ssid == NULL) {
1484 /*
1485 * Try to reassociate since there is no current
1486 * configuration and a new network was made available.
1487 */
1488 wpa_s->reassociate = 1;
1489 wpa_supplicant_req_scan(wpa_s, 0, 0);
1490 }
1492 was_disabled = ssid->disabled;
1494 ssid->disabled = 0;
1496 if (was_disabled != ssid->disabled)
1497 wpas_notify_network_enabled_changed(wpa_s, ssid);
1498 }
1499 }
1502 /**
1503 * wpa_supplicant_disable_network - Mark a configured network as disabled
1504 * @wpa_s: wpa_supplicant structure for a network interface
1505 * @ssid: wpa_ssid structure for a configured network or %NULL
1506 *
1507 * Disables the specified network or all networks if no network specified.
1508 */
1509 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1510 struct wpa_ssid *ssid)
1511 {
1512 struct wpa_ssid *other_ssid;
1513 int was_disabled;
1515 if (ssid == NULL) {
1516 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1517 other_ssid = other_ssid->next) {
1518 was_disabled = other_ssid->disabled;
1519 if (was_disabled == 2)
1520 continue; /* do not change persistent P2P group
1521 * data */
1523 other_ssid->disabled = 1;
1525 if (was_disabled != other_ssid->disabled)
1526 wpas_notify_network_enabled_changed(
1527 wpa_s, other_ssid);
1528 }
1529 if (wpa_s->current_ssid)
1530 wpa_supplicant_disassociate(
1531 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1532 } else if (ssid->disabled != 2) {
1533 if (ssid == wpa_s->current_ssid)
1534 wpa_supplicant_disassociate(
1535 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1537 was_disabled = ssid->disabled;
1539 ssid->disabled = 1;
1541 if (was_disabled != ssid->disabled)
1542 wpas_notify_network_enabled_changed(wpa_s, ssid);
1543 }
1544 }
1547 /**
1548 * wpa_supplicant_select_network - Attempt association with a network
1549 * @wpa_s: wpa_supplicant structure for a network interface
1550 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1551 */
1552 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1553 struct wpa_ssid *ssid)
1554 {
1556 struct wpa_ssid *other_ssid;
1558 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1559 wpa_supplicant_disassociate(
1560 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1562 /*
1563 * Mark all other networks disabled or mark all networks enabled if no
1564 * network specified.
1565 */
1566 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1567 other_ssid = other_ssid->next) {
1568 int was_disabled = other_ssid->disabled;
1569 if (was_disabled == 2)
1570 continue; /* do not change persistent P2P group data */
1572 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1574 if (was_disabled != other_ssid->disabled)
1575 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1576 }
1577 wpa_s->disconnected = 0;
1578 wpa_s->reassociate = 1;
1579 wpa_supplicant_req_scan(wpa_s, 0, 0);
1581 if (ssid)
1582 wpas_notify_network_selected(wpa_s, ssid);
1583 }
1586 /**
1587 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1588 * @wpa_s: wpa_supplicant structure for a network interface
1589 * @ap_scan: AP scan mode
1590 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1591 *
1592 */
1593 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1594 {
1596 int old_ap_scan;
1598 if (ap_scan < 0 || ap_scan > 2)
1599 return -1;
1601 old_ap_scan = wpa_s->conf->ap_scan;
1602 wpa_s->conf->ap_scan = ap_scan;
1604 if (old_ap_scan != wpa_s->conf->ap_scan)
1605 wpas_notify_ap_scan_changed(wpa_s);
1607 return 0;
1608 }
1611 /**
1612 * wpa_supplicant_set_debug_params - Set global debug params
1613 * @global: wpa_global structure
1614 * @debug_level: debug level
1615 * @debug_timestamp: determines if show timestamp in debug data
1616 * @debug_show_keys: determines if show keys in debug data
1617 * Returns: 0 if succeed or -1 if debug_level has wrong value
1618 */
1619 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1620 int debug_timestamp, int debug_show_keys)
1621 {
1623 int old_level, old_timestamp, old_show_keys;
1625 /* check for allowed debuglevels */
1626 if (debug_level != MSG_MSGDUMP &&
1627 debug_level != MSG_DEBUG &&
1628 debug_level != MSG_INFO &&
1629 debug_level != MSG_WARNING &&
1630 debug_level != MSG_ERROR)
1631 return -1;
1633 old_level = wpa_debug_level;
1634 old_timestamp = wpa_debug_timestamp;
1635 old_show_keys = wpa_debug_show_keys;
1637 wpa_debug_level = debug_level;
1638 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1639 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1641 if (wpa_debug_level != old_level)
1642 wpas_notify_debug_level_changed(global);
1643 if (wpa_debug_timestamp != old_timestamp)
1644 wpas_notify_debug_timestamp_changed(global);
1645 if (wpa_debug_show_keys != old_show_keys)
1646 wpas_notify_debug_show_keys_changed(global);
1648 return 0;
1649 }
1652 /**
1653 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1654 * @wpa_s: Pointer to wpa_supplicant data
1655 * Returns: A pointer to the current network structure or %NULL on failure
1656 */
1657 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1658 {
1659 struct wpa_ssid *entry;
1660 u8 ssid[MAX_SSID_LEN];
1661 int res;
1662 size_t ssid_len;
1663 u8 bssid[ETH_ALEN];
1664 int wired;
1666 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1667 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1668 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1669 "MLME");
1670 return NULL;
1671 }
1672 } else {
1673 res = wpa_drv_get_ssid(wpa_s, ssid);
1674 if (res < 0) {
1675 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1676 "driver");
1677 return NULL;
1678 }
1679 ssid_len = res;
1680 }
1682 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1683 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1684 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1685 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1686 "driver");
1687 return NULL;
1688 }
1690 wired = wpa_s->conf->ap_scan == 0 &&
1691 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1693 entry = wpa_s->conf->ssid;
1694 while (entry) {
1695 if (!entry->disabled &&
1696 ((ssid_len == entry->ssid_len &&
1697 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1698 (!entry->bssid_set ||
1699 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1700 return entry;
1701 #ifdef CONFIG_WPS
1702 if (!entry->disabled &&
1703 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1704 (entry->ssid == NULL || entry->ssid_len == 0) &&
1705 (!entry->bssid_set ||
1706 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1707 return entry;
1708 #endif /* CONFIG_WPS */
1709 entry = entry->next;
1710 }
1712 return NULL;
1713 }
1716 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1717 const char *name)
1718 {
1719 int i;
1720 size_t len;
1721 const char *pos, *driver = name;
1723 if (wpa_s == NULL)
1724 return -1;
1726 if (wpa_drivers[0] == NULL) {
1727 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1728 "wpa_supplicant");
1729 return -1;
1730 }
1732 if (name == NULL) {
1733 /* default to first driver in the list */
1734 wpa_s->driver = wpa_drivers[0];
1735 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1736 return 0;
1737 }
1739 do {
1740 pos = os_strchr(driver, ',');
1741 if (pos)
1742 len = pos - driver;
1743 else
1744 len = os_strlen(driver);
1746 for (i = 0; wpa_drivers[i]; i++) {
1747 if (os_strlen(wpa_drivers[i]->name) == len &&
1748 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1749 0) {
1750 wpa_s->driver = wpa_drivers[i];
1751 wpa_s->global_drv_priv =
1752 wpa_s->global->drv_priv[i];
1753 return 0;
1754 }
1755 }
1757 driver = pos + 1;
1758 } while (pos);
1760 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1761 return -1;
1762 }
1765 /**
1766 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1767 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1768 * with struct wpa_driver_ops::init()
1769 * @src_addr: Source address of the EAPOL frame
1770 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1771 * @len: Length of the EAPOL data
1772 *
1773 * This function is called for each received EAPOL frame. Most driver
1774 * interfaces rely on more generic OS mechanism for receiving frames through
1775 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1776 * take care of received EAPOL frames and deliver them to the core supplicant
1777 * code by calling this function.
1778 */
1779 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1780 const u8 *buf, size_t len)
1781 {
1782 struct wpa_supplicant *wpa_s = ctx;
1784 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1785 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1787 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1788 /*
1789 * There is possible race condition between receiving the
1790 * association event and the EAPOL frame since they are coming
1791 * through different paths from the driver. In order to avoid
1792 * issues in trying to process the EAPOL frame before receiving
1793 * association information, lets queue it for processing until
1794 * the association event is received.
1795 */
1796 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1797 "of received EAPOL frame");
1798 wpabuf_free(wpa_s->pending_eapol_rx);
1799 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1800 if (wpa_s->pending_eapol_rx) {
1801 os_get_time(&wpa_s->pending_eapol_rx_time);
1802 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1803 ETH_ALEN);
1804 }
1805 return;
1806 }
1808 #ifdef CONFIG_AP
1809 if (wpa_s->ap_iface) {
1810 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1811 return;
1812 }
1813 #endif /* CONFIG_AP */
1815 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1816 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
1817 "no key management is configured");
1818 return;
1819 }
1821 if (wpa_s->eapol_received == 0 &&
1822 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1823 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1824 wpa_s->wpa_state != WPA_COMPLETED) &&
1825 (wpa_s->current_ssid == NULL ||
1826 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1827 /* Timeout for completing IEEE 802.1X and WPA authentication */
1828 wpa_supplicant_req_auth_timeout(
1829 wpa_s,
1830 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1831 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1832 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1833 70 : 10, 0);
1834 }
1835 wpa_s->eapol_received++;
1837 if (wpa_s->countermeasures) {
1838 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
1839 "EAPOL packet");
1840 return;
1841 }
1843 #ifdef CONFIG_IBSS_RSN
1844 if (wpa_s->current_ssid &&
1845 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1846 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1847 return;
1848 }
1849 #endif /* CONFIG_IBSS_RSN */
1851 /* Source address of the incoming EAPOL frame could be compared to the
1852 * current BSSID. However, it is possible that a centralized
1853 * Authenticator could be using another MAC address than the BSSID of
1854 * an AP, so just allow any address to be used for now. The replies are
1855 * still sent to the current BSSID (if available), though. */
1857 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1858 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1859 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1860 return;
1861 wpa_drv_poll(wpa_s);
1862 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1863 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1864 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1865 /*
1866 * Set portValid = TRUE here since we are going to skip 4-way
1867 * handshake processing which would normally set portValid. We
1868 * need this to allow the EAPOL state machines to be completed
1869 * without going through EAPOL-Key handshake.
1870 */
1871 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1872 }
1873 }
1876 /**
1877 * wpa_supplicant_driver_init - Initialize driver interface parameters
1878 * @wpa_s: Pointer to wpa_supplicant data
1879 * Returns: 0 on success, -1 on failure
1880 *
1881 * This function is called to initialize driver interface parameters.
1882 * wpa_drv_init() must have been called before this function to initialize the
1883 * driver interface.
1884 */
1885 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1886 {
1887 static int interface_count = 0;
1889 if (wpa_s->driver->send_eapol) {
1890 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1891 if (addr)
1892 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1893 } else if (!(wpa_s->drv_flags &
1894 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
1895 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1896 wpa_drv_get_mac_addr(wpa_s),
1897 ETH_P_EAPOL,
1898 wpa_supplicant_rx_eapol, wpa_s, 0);
1899 if (wpa_s->l2 == NULL)
1900 return -1;
1901 } else {
1902 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1903 if (addr)
1904 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1905 }
1907 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1908 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
1909 return -1;
1910 }
1912 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
1913 MAC2STR(wpa_s->own_addr));
1915 if (wpa_s->bridge_ifname[0]) {
1916 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
1917 "interface '%s'", wpa_s->bridge_ifname);
1918 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1919 wpa_s->own_addr,
1920 ETH_P_EAPOL,
1921 wpa_supplicant_rx_eapol, wpa_s,
1922 0);
1923 if (wpa_s->l2_br == NULL) {
1924 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
1925 "connection for the bridge interface '%s'",
1926 wpa_s->bridge_ifname);
1927 return -1;
1928 }
1929 }
1931 wpa_clear_keys(wpa_s, NULL);
1933 /* Make sure that TKIP countermeasures are not left enabled (could
1934 * happen if wpa_supplicant is killed during countermeasures. */
1935 wpa_drv_set_countermeasures(wpa_s, 0);
1937 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1938 wpa_drv_flush_pmkid(wpa_s);
1940 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1941 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
1942 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1943 interface_count++;
1944 } else
1945 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1947 return 0;
1948 }
1951 static int wpa_supplicant_daemon(const char *pid_file)
1952 {
1953 wpa_printf(MSG_DEBUG, "Daemonize..");
1954 return os_daemonize(pid_file);
1955 }
1958 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1959 {
1960 struct wpa_supplicant *wpa_s;
1962 wpa_s = os_zalloc(sizeof(*wpa_s));
1963 if (wpa_s == NULL)
1964 return NULL;
1965 wpa_s->scan_req = 1;
1966 wpa_s->new_connection = 1;
1967 wpa_s->parent = wpa_s;
1969 return wpa_s;
1970 }
1973 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1974 struct wpa_interface *iface)
1975 {
1976 const char *ifname, *driver;
1977 struct wpa_driver_capa capa;
1979 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1980 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1981 iface->confname ? iface->confname : "N/A",
1982 iface->driver ? iface->driver : "default",
1983 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1984 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1986 if (iface->confname) {
1987 #ifdef CONFIG_BACKEND_FILE
1988 wpa_s->confname = os_rel2abs_path(iface->confname);
1989 if (wpa_s->confname == NULL) {
1990 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1991 "for configuration file '%s'.",
1992 iface->confname);
1993 return -1;
1994 }
1995 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1996 iface->confname, wpa_s->confname);
1997 #else /* CONFIG_BACKEND_FILE */
1998 wpa_s->confname = os_strdup(iface->confname);
1999 #endif /* CONFIG_BACKEND_FILE */
2000 wpa_s->conf = wpa_config_read(wpa_s->confname);
2001 if (wpa_s->conf == NULL) {
2002 wpa_printf(MSG_ERROR, "Failed to read or parse "
2003 "configuration '%s'.", wpa_s->confname);
2004 return -1;
2005 }
2007 /*
2008 * Override ctrl_interface and driver_param if set on command
2009 * line.
2010 */
2011 if (iface->ctrl_interface) {
2012 os_free(wpa_s->conf->ctrl_interface);
2013 wpa_s->conf->ctrl_interface =
2014 os_strdup(iface->ctrl_interface);
2015 }
2017 if (iface->driver_param) {
2018 os_free(wpa_s->conf->driver_param);
2019 wpa_s->conf->driver_param =
2020 os_strdup(iface->driver_param);
2021 }
2022 } else
2023 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2024 iface->driver_param);
2026 if (wpa_s->conf == NULL) {
2027 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2028 return -1;
2029 }
2031 if (iface->ifname == NULL) {
2032 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2033 return -1;
2034 }
2035 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2036 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2037 iface->ifname);
2038 return -1;
2039 }
2040 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2042 if (iface->bridge_ifname) {
2043 if (os_strlen(iface->bridge_ifname) >=
2044 sizeof(wpa_s->bridge_ifname)) {
2045 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2046 "name '%s'.", iface->bridge_ifname);
2047 return -1;
2048 }
2049 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2050 sizeof(wpa_s->bridge_ifname));
2051 }
2053 /* RSNA Supplicant Key Management - INITIALIZE */
2054 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2055 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2057 /* Initialize driver interface and register driver event handler before
2058 * L2 receive handler so that association events are processed before
2059 * EAPOL-Key packets if both become available for the same select()
2060 * call. */
2061 driver = iface->driver;
2062 next_driver:
2063 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2064 return -1;
2066 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2067 if (wpa_s->drv_priv == NULL) {
2068 const char *pos;
2069 pos = driver ? os_strchr(driver, ',') : NULL;
2070 if (pos) {
2071 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2072 "driver interface - try next driver wrapper");
2073 driver = pos + 1;
2074 goto next_driver;
2075 }
2076 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2077 "interface");
2078 return -1;
2079 }
2080 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2081 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2082 "driver_param '%s'", wpa_s->conf->driver_param);
2083 return -1;
2084 }
2086 ifname = wpa_drv_get_ifname(wpa_s);
2087 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2088 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2089 "interface name with '%s'", ifname);
2090 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2091 }
2093 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2094 return -1;
2096 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2097 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2098 NULL);
2099 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2101 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2102 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2103 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2104 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2105 "dot11RSNAConfigPMKLifetime");
2106 return -1;
2107 }
2109 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2110 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2111 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2112 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2113 "dot11RSNAConfigPMKReauthThreshold");
2114 return -1;
2115 }
2117 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2118 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2119 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2120 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2121 "dot11RSNAConfigSATimeout");
2122 return -1;
2123 }
2125 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2126 wpa_s->drv_flags = capa.flags;
2127 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2128 if (ieee80211_sta_init(wpa_s))
2129 return -1;
2130 }
2131 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2132 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2133 wpa_s->max_stations = capa.max_stations;
2134 }
2135 if (wpa_s->max_remain_on_chan == 0)
2136 wpa_s->max_remain_on_chan = 1000;
2138 if (wpa_supplicant_driver_init(wpa_s) < 0)
2139 return -1;
2141 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2142 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2143 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2144 return -1;
2145 }
2147 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2149 if (wpas_wps_init(wpa_s))
2150 return -1;
2152 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2153 return -1;
2154 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2156 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2157 if (wpa_s->ctrl_iface == NULL) {
2158 wpa_printf(MSG_ERROR,
2159 "Failed to initialize control interface '%s'.\n"
2160 "You may have another wpa_supplicant process "
2161 "already running or the file was\n"
2162 "left by an unclean termination of wpa_supplicant "
2163 "in which case you will need\n"
2164 "to manually remove this file before starting "
2165 "wpa_supplicant again.\n",
2166 wpa_s->conf->ctrl_interface);
2167 return -1;
2168 }
2170 #ifdef CONFIG_IBSS_RSN
2171 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2172 if (!wpa_s->ibss_rsn) {
2173 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to init IBSS RSN");
2174 return -1;
2175 }
2176 #endif /* CONFIG_IBSS_RSN */
2178 #ifdef CONFIG_P2P
2179 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2180 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2181 return -1;
2182 }
2183 #endif /* CONFIG_P2P */
2185 if (wpa_bss_init(wpa_s) < 0)
2186 return -1;
2188 return 0;
2189 }
2192 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2193 int notify)
2194 {
2195 if (wpa_s->drv_priv) {
2196 wpa_supplicant_deauthenticate(wpa_s,
2197 WLAN_REASON_DEAUTH_LEAVING);
2199 wpa_drv_set_countermeasures(wpa_s, 0);
2200 wpa_clear_keys(wpa_s, NULL);
2201 }
2203 wpa_supplicant_cleanup(wpa_s);
2205 if (notify)
2206 wpas_notify_iface_removed(wpa_s);
2208 if (wpa_s->drv_priv)
2209 wpa_drv_deinit(wpa_s);
2210 }
2213 /**
2214 * wpa_supplicant_add_iface - Add a new network interface
2215 * @global: Pointer to global data from wpa_supplicant_init()
2216 * @iface: Interface configuration options
2217 * Returns: Pointer to the created interface or %NULL on failure
2218 *
2219 * This function is used to add new network interfaces for %wpa_supplicant.
2220 * This can be called before wpa_supplicant_run() to add interfaces before the
2221 * main event loop has been started. In addition, new interfaces can be added
2222 * dynamically while %wpa_supplicant is already running. This could happen,
2223 * e.g., when a hotplug network adapter is inserted.
2224 */
2225 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2226 struct wpa_interface *iface)
2227 {
2228 struct wpa_supplicant *wpa_s;
2229 struct wpa_interface t_iface;
2230 struct wpa_ssid *ssid;
2232 if (global == NULL || iface == NULL)
2233 return NULL;
2235 wpa_s = wpa_supplicant_alloc();
2236 if (wpa_s == NULL)
2237 return NULL;
2239 wpa_s->global = global;
2241 t_iface = *iface;
2242 if (global->params.override_driver) {
2243 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2244 "('%s' -> '%s')",
2245 iface->driver, global->params.override_driver);
2246 t_iface.driver = global->params.override_driver;
2247 }
2248 if (global->params.override_ctrl_interface) {
2249 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2250 "ctrl_interface ('%s' -> '%s')",
2251 iface->ctrl_interface,
2252 global->params.override_ctrl_interface);
2253 t_iface.ctrl_interface =
2254 global->params.override_ctrl_interface;
2255 }
2256 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2257 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2258 iface->ifname);
2259 wpa_supplicant_deinit_iface(wpa_s, 0);
2260 os_free(wpa_s);
2261 return NULL;
2262 }
2264 /* Notify the control interfaces about new iface */
2265 if (wpas_notify_iface_added(wpa_s)) {
2266 wpa_supplicant_deinit_iface(wpa_s, 1);
2267 os_free(wpa_s);
2268 return NULL;
2269 }
2271 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2272 wpas_notify_network_added(wpa_s, ssid);
2274 wpa_s->next = global->ifaces;
2275 global->ifaces = wpa_s;
2277 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2279 return wpa_s;
2280 }
2283 /**
2284 * wpa_supplicant_remove_iface - Remove a network interface
2285 * @global: Pointer to global data from wpa_supplicant_init()
2286 * @wpa_s: Pointer to the network interface to be removed
2287 * Returns: 0 if interface was removed, -1 if interface was not found
2288 *
2289 * This function can be used to dynamically remove network interfaces from
2290 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2291 * addition, this function is used to remove all remaining interfaces when
2292 * %wpa_supplicant is terminated.
2293 */
2294 int wpa_supplicant_remove_iface(struct wpa_global *global,
2295 struct wpa_supplicant *wpa_s)
2296 {
2297 struct wpa_supplicant *prev;
2299 /* Remove interface from the global list of interfaces */
2300 prev = global->ifaces;
2301 if (prev == wpa_s) {
2302 global->ifaces = wpa_s->next;
2303 } else {
2304 while (prev && prev->next != wpa_s)
2305 prev = prev->next;
2306 if (prev == NULL)
2307 return -1;
2308 prev->next = wpa_s->next;
2309 }
2311 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2313 if (global->p2p_group_formation == wpa_s)
2314 global->p2p_group_formation = NULL;
2315 wpa_supplicant_deinit_iface(wpa_s, 1);
2316 os_free(wpa_s);
2318 return 0;
2319 }
2322 /**
2323 * wpa_supplicant_get_iface - Get a new network interface
2324 * @global: Pointer to global data from wpa_supplicant_init()
2325 * @ifname: Interface name
2326 * Returns: Pointer to the interface or %NULL if not found
2327 */
2328 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2329 const char *ifname)
2330 {
2331 struct wpa_supplicant *wpa_s;
2333 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2334 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2335 return wpa_s;
2336 }
2337 return NULL;
2338 }
2341 #ifndef CONFIG_NO_WPA_MSG
2342 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2343 {
2344 struct wpa_supplicant *wpa_s = ctx;
2345 if (wpa_s == NULL)
2346 return NULL;
2347 return wpa_s->ifname;
2348 }
2349 #endif /* CONFIG_NO_WPA_MSG */
2352 /**
2353 * wpa_supplicant_init - Initialize %wpa_supplicant
2354 * @params: Parameters for %wpa_supplicant
2355 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2356 *
2357 * This function is used to initialize %wpa_supplicant. After successful
2358 * initialization, the returned data pointer can be used to add and remove
2359 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2360 */
2361 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2362 {
2363 struct wpa_global *global;
2364 int ret, i;
2366 if (params == NULL)
2367 return NULL;
2369 #ifndef CONFIG_NO_WPA_MSG
2370 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2371 #endif /* CONFIG_NO_WPA_MSG */
2373 wpa_debug_open_file(params->wpa_debug_file_path);
2374 if (params->wpa_debug_syslog)
2375 wpa_debug_open_syslog();
2377 ret = eap_register_methods();
2378 if (ret) {
2379 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2380 if (ret == -2)
2381 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2382 "the same EAP type.");
2383 return NULL;
2384 }
2386 global = os_zalloc(sizeof(*global));
2387 if (global == NULL)
2388 return NULL;
2389 dl_list_init(&global->p2p_srv_bonjour);
2390 dl_list_init(&global->p2p_srv_upnp);
2391 global->params.daemonize = params->daemonize;
2392 global->params.wait_for_monitor = params->wait_for_monitor;
2393 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2394 if (params->pid_file)
2395 global->params.pid_file = os_strdup(params->pid_file);
2396 if (params->ctrl_interface)
2397 global->params.ctrl_interface =
2398 os_strdup(params->ctrl_interface);
2399 if (params->override_driver)
2400 global->params.override_driver =
2401 os_strdup(params->override_driver);
2402 if (params->override_ctrl_interface)
2403 global->params.override_ctrl_interface =
2404 os_strdup(params->override_ctrl_interface);
2405 wpa_debug_level = global->params.wpa_debug_level =
2406 params->wpa_debug_level;
2407 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2408 params->wpa_debug_show_keys;
2409 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2410 params->wpa_debug_timestamp;
2412 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2414 if (eloop_init()) {
2415 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2416 wpa_supplicant_deinit(global);
2417 return NULL;
2418 }
2420 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2421 if (global->ctrl_iface == NULL) {
2422 wpa_supplicant_deinit(global);
2423 return NULL;
2424 }
2426 if (wpas_notify_supplicant_initialized(global)) {
2427 wpa_supplicant_deinit(global);
2428 return NULL;
2429 }
2431 for (i = 0; wpa_drivers[i]; i++)
2432 global->drv_count++;
2433 if (global->drv_count == 0) {
2434 wpa_printf(MSG_ERROR, "No drivers enabled");
2435 wpa_supplicant_deinit(global);
2436 return NULL;
2437 }
2438 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2439 if (global->drv_priv == NULL) {
2440 wpa_supplicant_deinit(global);
2441 return NULL;
2442 }
2443 for (i = 0; wpa_drivers[i]; i++) {
2444 if (!wpa_drivers[i]->global_init)
2445 continue;
2446 global->drv_priv[i] = wpa_drivers[i]->global_init();
2447 if (global->drv_priv[i] == NULL) {
2448 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2449 "'%s'", wpa_drivers[i]->name);
2450 wpa_supplicant_deinit(global);
2451 return NULL;
2452 }
2453 }
2455 return global;
2456 }
2459 /**
2460 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2461 * @global: Pointer to global data from wpa_supplicant_init()
2462 * Returns: 0 after successful event loop run, -1 on failure
2463 *
2464 * This function starts the main event loop and continues running as long as
2465 * there are any remaining events. In most cases, this function is running as
2466 * long as the %wpa_supplicant process in still in use.
2467 */
2468 int wpa_supplicant_run(struct wpa_global *global)
2469 {
2470 struct wpa_supplicant *wpa_s;
2472 if (global->params.daemonize &&
2473 wpa_supplicant_daemon(global->params.pid_file))
2474 return -1;
2476 if (global->params.wait_for_monitor) {
2477 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2478 if (wpa_s->ctrl_iface)
2479 wpa_supplicant_ctrl_iface_wait(
2480 wpa_s->ctrl_iface);
2481 }
2483 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2484 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2486 eloop_run();
2488 return 0;
2489 }
2492 /**
2493 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2494 * @global: Pointer to global data from wpa_supplicant_init()
2495 *
2496 * This function is called to deinitialize %wpa_supplicant and to free all
2497 * allocated resources. Remaining network interfaces will also be removed.
2498 */
2499 void wpa_supplicant_deinit(struct wpa_global *global)
2500 {
2501 int i;
2503 if (global == NULL)
2504 return;
2506 #ifdef CONFIG_P2P
2507 wpas_p2p_deinit_global(global);
2508 #endif /* CONFIG_P2P */
2510 while (global->ifaces)
2511 wpa_supplicant_remove_iface(global, global->ifaces);
2513 if (global->ctrl_iface)
2514 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2516 wpas_notify_supplicant_deinitialized(global);
2518 eap_peer_unregister_methods();
2519 #ifdef CONFIG_AP
2520 eap_server_unregister_methods();
2521 #endif /* CONFIG_AP */
2523 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2524 if (!global->drv_priv[i])
2525 continue;
2526 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2527 }
2528 os_free(global->drv_priv);
2530 eloop_destroy();
2532 if (global->params.pid_file) {
2533 os_daemonize_terminate(global->params.pid_file);
2534 os_free(global->params.pid_file);
2535 }
2536 os_free(global->params.ctrl_interface);
2537 os_free(global->params.override_driver);
2538 os_free(global->params.override_ctrl_interface);
2540 os_free(global);
2541 wpa_debug_close_syslog();
2542 wpa_debug_close_file();
2543 }
2546 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2547 {
2548 #ifdef CONFIG_WPS
2549 wpas_wps_update_config(wpa_s);
2550 #endif /* CONFIG_WPS */
2552 #ifdef CONFIG_P2P
2553 wpas_p2p_update_config(wpa_s);
2554 #endif /* CONFIG_P2P */
2556 wpa_s->conf->changed_parameters = 0;
2557 }
2560 void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
2561 size_t num_hw_features)
2562 {
2563 size_t i;
2565 if (hw_features == NULL)
2566 return;
2568 for (i = 0; i < num_hw_features; i++) {
2569 os_free(hw_features[i].channels);
2570 os_free(hw_features[i].rates);
2571 }
2573 os_free(hw_features);
2574 }
2577 static void add_freq(int *freqs, int *num_freqs, int freq)
2578 {
2579 int i;
2581 for (i = 0; i < *num_freqs; i++) {
2582 if (freqs[i] == freq)
2583 return;
2584 }
2586 freqs[*num_freqs] = freq;
2587 (*num_freqs)++;
2588 }
2591 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2592 {
2593 struct wpa_bss *bss, *cbss;
2594 const int max_freqs = 10;
2595 int *freqs;
2596 int num_freqs = 0;
2598 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2599 if (freqs == NULL)
2600 return NULL;
2602 cbss = wpa_s->current_bss;
2604 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2605 if (bss == cbss)
2606 continue;
2607 if (bss->ssid_len == cbss->ssid_len &&
2608 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2609 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2610 add_freq(freqs, &num_freqs, bss->freq);
2611 if (num_freqs == max_freqs)
2612 break;
2613 }
2614 }
2616 if (num_freqs == 0) {
2617 os_free(freqs);
2618 freqs = NULL;
2619 }
2621 return freqs;
2622 }
2625 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2626 {
2627 int timeout;
2628 int count;
2629 int *freqs = NULL;
2631 /*
2632 * Add the failed BSSID into the blacklist and speed up next scan
2633 * attempt if there could be other APs that could accept association.
2634 * The current blacklist count indicates how many times we have tried
2635 * connecting to this AP and multiple attempts mean that other APs are
2636 * either not available or has already been tried, so that we can start
2637 * increasing the delay here to avoid constant scanning.
2638 */
2639 count = wpa_blacklist_add(wpa_s, bssid);
2640 if (count == 1 && wpa_s->current_bss) {
2641 /*
2642 * This BSS was not in the blacklist before. If there is
2643 * another BSS available for the same ESS, we should try that
2644 * next. Otherwise, we may as well try this one once more
2645 * before allowing other, likely worse, ESSes to be considered.
2646 */
2647 freqs = get_bss_freqs_in_ess(wpa_s);
2648 if (freqs) {
2649 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2650 "has been seen; try it next");
2651 wpa_blacklist_add(wpa_s, bssid);
2652 /*
2653 * On the next scan, go through only the known channels
2654 * used in this ESS based on previous scans to speed up
2655 * common load balancing use case.
2656 */
2657 os_free(wpa_s->next_scan_freqs);
2658 wpa_s->next_scan_freqs = freqs;
2659 }
2660 }
2662 switch (count) {
2663 case 1:
2664 timeout = 100;
2665 break;
2666 case 2:
2667 timeout = 500;
2668 break;
2669 case 3:
2670 timeout = 1000;
2671 break;
2672 default:
2673 timeout = 5000;
2674 }
2676 /*
2677 * TODO: if more than one possible AP is available in scan results,
2678 * could try the other ones before requesting a new scan.
2679 */
2680 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2681 1000 * (timeout % 1000));
2682 }