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