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