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