]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wpa_supplicant.c
Fix CONFIG_SME build without CONFIG_IEEE80211W
[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 #ifdef CONFIG_IEEE80211W
428 sme_stop_sa_query(wpa_s);
429 #endif /* CONFIG_IEEE80211W */
430 #endif /* CONFIG_SME */
431
432 #ifdef CONFIG_AP
433 wpa_supplicant_ap_deinit(wpa_s);
434 #endif /* CONFIG_AP */
435
436 #ifdef CONFIG_P2P
437 wpas_p2p_deinit(wpa_s);
438 #endif /* CONFIG_P2P */
439
440 os_free(wpa_s->next_scan_freqs);
441 wpa_s->next_scan_freqs = NULL;
442 }
443
444
445 /**
446 * wpa_clear_keys - Clear keys configured for the driver
447 * @wpa_s: Pointer to wpa_supplicant data
448 * @addr: Previously used BSSID or %NULL if not available
449 *
450 * This function clears the encryption keys that has been previously configured
451 * for the driver.
452 */
453 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
454 {
455 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
456
457 if (wpa_s->keys_cleared) {
458 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
459 * timing issues with keys being cleared just before new keys
460 * are set or just after association or something similar. This
461 * shows up in group key handshake failing often because of the
462 * client not receiving the first encrypted packets correctly.
463 * Skipping some of the extra key clearing steps seems to help
464 * in completing group key handshake more reliably. */
465 wpa_printf(MSG_DEBUG, "No keys have been configured - "
466 "skip key clearing");
467 return;
468 }
469
470 /* MLME-DELETEKEYS.request */
471 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
472 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
473 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
474 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
475 #ifdef CONFIG_IEEE80211W
476 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
477 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
478 #endif /* CONFIG_IEEE80211W */
479 if (addr) {
480 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
481 0);
482 /* MLME-SETPROTECTION.request(None) */
483 wpa_drv_mlme_setprotection(
484 wpa_s, addr,
485 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
486 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
487 }
488 wpa_s->keys_cleared = 1;
489 }
490
491
492 /**
493 * wpa_supplicant_state_txt - Get the connection state name as a text string
494 * @state: State (wpa_state; WPA_*)
495 * Returns: The state name as a printable text string
496 */
497 const char * wpa_supplicant_state_txt(enum wpa_states state)
498 {
499 switch (state) {
500 case WPA_DISCONNECTED:
501 return "DISCONNECTED";
502 case WPA_INACTIVE:
503 return "INACTIVE";
504 case WPA_INTERFACE_DISABLED:
505 return "INTERFACE_DISABLED";
506 case WPA_SCANNING:
507 return "SCANNING";
508 case WPA_AUTHENTICATING:
509 return "AUTHENTICATING";
510 case WPA_ASSOCIATING:
511 return "ASSOCIATING";
512 case WPA_ASSOCIATED:
513 return "ASSOCIATED";
514 case WPA_4WAY_HANDSHAKE:
515 return "4WAY_HANDSHAKE";
516 case WPA_GROUP_HANDSHAKE:
517 return "GROUP_HANDSHAKE";
518 case WPA_COMPLETED:
519 return "COMPLETED";
520 default:
521 return "UNKNOWN";
522 }
523 }
524
525
526 /**
527 * wpa_supplicant_set_state - Set current connection state
528 * @wpa_s: Pointer to wpa_supplicant data
529 * @state: The new connection state
530 *
531 * This function is called whenever the connection state changes, e.g.,
532 * association is completed for WPA/WPA2 4-Way Handshake is started.
533 */
534 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
535 enum wpa_states state)
536 {
537 enum wpa_states old_state = wpa_s->wpa_state;
538
539 wpa_printf(MSG_DEBUG, "%s: State: %s -> %s",
540 wpa_s->ifname, wpa_supplicant_state_txt(wpa_s->wpa_state),
541 wpa_supplicant_state_txt(state));
542
543 if (state != WPA_SCANNING)
544 wpa_supplicant_notify_scanning(wpa_s, 0);
545
546 if (state == WPA_COMPLETED && wpa_s->new_connection) {
547 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
548 struct wpa_ssid *ssid = wpa_s->current_ssid;
549 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
550 MACSTR " completed %s [id=%d id_str=%s]",
551 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
552 "(reauth)" : "(auth)",
553 ssid ? ssid->id : -1,
554 ssid && ssid->id_str ? ssid->id_str : "");
555 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
556 wpa_s->new_connection = 0;
557 wpa_s->reassociated_connection = 1;
558 wpa_drv_set_operstate(wpa_s, 1);
559 #ifndef IEEE8021X_EAPOL
560 wpa_drv_set_supp_port(wpa_s, 1);
561 #endif /* IEEE8021X_EAPOL */
562 wpa_s->after_wps = 0;
563 #ifdef CONFIG_P2P
564 wpas_p2p_completed(wpa_s);
565 #endif /* CONFIG_P2P */
566 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
567 state == WPA_ASSOCIATED) {
568 wpa_s->new_connection = 1;
569 wpa_drv_set_operstate(wpa_s, 0);
570 #ifndef IEEE8021X_EAPOL
571 wpa_drv_set_supp_port(wpa_s, 0);
572 #endif /* IEEE8021X_EAPOL */
573 }
574 wpa_s->wpa_state = state;
575
576 if (wpa_s->wpa_state != old_state)
577 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
578 }
579
580
581 void wpa_supplicant_terminate_proc(struct wpa_global *global)
582 {
583 int pending = 0;
584 #ifdef CONFIG_WPS
585 struct wpa_supplicant *wpa_s = global->ifaces;
586 while (wpa_s) {
587 if (wpas_wps_terminate_pending(wpa_s) == 1)
588 pending = 1;
589 wpa_s = wpa_s->next;
590 }
591 #endif /* CONFIG_WPS */
592 if (pending)
593 return;
594 eloop_terminate();
595 }
596
597
598 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
599 {
600 struct wpa_global *global = signal_ctx;
601 struct wpa_supplicant *wpa_s;
602 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
603 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
604 "received", sig);
605 }
606 wpa_supplicant_terminate_proc(global);
607 }
608
609
610 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
611 {
612 enum wpa_states old_state = wpa_s->wpa_state;
613
614 wpa_s->pairwise_cipher = 0;
615 wpa_s->group_cipher = 0;
616 wpa_s->mgmt_group_cipher = 0;
617 wpa_s->key_mgmt = 0;
618 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
619 wpa_s->wpa_state = WPA_DISCONNECTED;
620
621 if (wpa_s->wpa_state != old_state)
622 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
623 }
624
625
626 /**
627 * wpa_supplicant_reload_configuration - Reload configuration data
628 * @wpa_s: Pointer to wpa_supplicant data
629 * Returns: 0 on success or -1 if configuration parsing failed
630 *
631 * This function can be used to request that the configuration data is reloaded
632 * (e.g., after configuration file change). This function is reloading
633 * configuration only for one interface, so this may need to be called multiple
634 * times if %wpa_supplicant is controlling multiple interfaces and all
635 * interfaces need reconfiguration.
636 */
637 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
638 {
639 struct wpa_config *conf;
640 struct wpa_ssid *old_ssid;
641 int reconf_ctrl;
642 int old_ap_scan;
643
644 if (wpa_s->confname == NULL)
645 return -1;
646 conf = wpa_config_read(wpa_s->confname);
647 if (conf == NULL) {
648 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
649 "file '%s' - exiting", wpa_s->confname);
650 return -1;
651 }
652 conf->changed_parameters = (unsigned int) -1;
653
654 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
655 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
656 os_strcmp(conf->ctrl_interface,
657 wpa_s->conf->ctrl_interface) != 0);
658
659 if (reconf_ctrl && wpa_s->ctrl_iface) {
660 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
661 wpa_s->ctrl_iface = NULL;
662 }
663
664 eapol_sm_invalidate_cached_session(wpa_s->eapol);
665 old_ssid = wpa_s->current_ssid;
666 wpa_s->current_ssid = NULL;
667 if (old_ssid != wpa_s->current_ssid)
668 wpas_notify_network_changed(wpa_s);
669
670 /*
671 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
672 * pkcs11_engine_path, pkcs11_module_path.
673 */
674 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
675 /*
676 * Clear forced success to clear EAP state for next
677 * authentication.
678 */
679 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
680 }
681 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
682 wpa_sm_set_config(wpa_s->wpa, NULL);
683 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
684 rsn_preauth_deinit(wpa_s->wpa);
685
686 old_ap_scan = wpa_s->conf->ap_scan;
687 wpa_config_free(wpa_s->conf);
688 wpa_s->conf = conf;
689 if (old_ap_scan != wpa_s->conf->ap_scan)
690 wpas_notify_ap_scan_changed(wpa_s);
691
692 if (reconf_ctrl)
693 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
694
695 wpa_supplicant_update_config(wpa_s);
696
697 wpa_supplicant_clear_status(wpa_s);
698 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
699 wpa_s->reassociate = 1;
700 wpa_supplicant_req_scan(wpa_s, 0, 0);
701 }
702 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
703 return 0;
704 }
705
706
707 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
708 {
709 struct wpa_global *global = signal_ctx;
710 struct wpa_supplicant *wpa_s;
711 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
712 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
713 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
714 wpa_supplicant_terminate_proc(global);
715 }
716 }
717 }
718
719
720 enum wpa_cipher cipher_suite2driver(int cipher)
721 {
722 switch (cipher) {
723 case WPA_CIPHER_NONE:
724 return CIPHER_NONE;
725 case WPA_CIPHER_WEP40:
726 return CIPHER_WEP40;
727 case WPA_CIPHER_WEP104:
728 return CIPHER_WEP104;
729 case WPA_CIPHER_CCMP:
730 return CIPHER_CCMP;
731 case WPA_CIPHER_TKIP:
732 default:
733 return CIPHER_TKIP;
734 }
735 }
736
737
738 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
739 {
740 switch (key_mgmt) {
741 case WPA_KEY_MGMT_NONE:
742 return KEY_MGMT_NONE;
743 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
744 return KEY_MGMT_802_1X_NO_WPA;
745 case WPA_KEY_MGMT_IEEE8021X:
746 return KEY_MGMT_802_1X;
747 case WPA_KEY_MGMT_WPA_NONE:
748 return KEY_MGMT_WPA_NONE;
749 case WPA_KEY_MGMT_FT_IEEE8021X:
750 return KEY_MGMT_FT_802_1X;
751 case WPA_KEY_MGMT_FT_PSK:
752 return KEY_MGMT_FT_PSK;
753 case WPA_KEY_MGMT_IEEE8021X_SHA256:
754 return KEY_MGMT_802_1X_SHA256;
755 case WPA_KEY_MGMT_PSK_SHA256:
756 return KEY_MGMT_PSK_SHA256;
757 case WPA_KEY_MGMT_WPS:
758 return KEY_MGMT_WPS;
759 case WPA_KEY_MGMT_PSK:
760 default:
761 return KEY_MGMT_PSK;
762 }
763 }
764
765
766 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
767 struct wpa_ssid *ssid,
768 struct wpa_ie_data *ie)
769 {
770 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
771 if (ret) {
772 if (ret == -2) {
773 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
774 "from association info");
775 }
776 return -1;
777 }
778
779 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
780 "suites");
781 if (!(ie->group_cipher & ssid->group_cipher)) {
782 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
783 "cipher 0x%x (mask 0x%x) - reject",
784 ie->group_cipher, ssid->group_cipher);
785 return -1;
786 }
787 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
788 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
789 "cipher 0x%x (mask 0x%x) - reject",
790 ie->pairwise_cipher, ssid->pairwise_cipher);
791 return -1;
792 }
793 if (!(ie->key_mgmt & ssid->key_mgmt)) {
794 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
795 "management 0x%x (mask 0x%x) - reject",
796 ie->key_mgmt, ssid->key_mgmt);
797 return -1;
798 }
799
800 #ifdef CONFIG_IEEE80211W
801 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
802 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
803 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
804 "that does not support management frame protection - "
805 "reject");
806 return -1;
807 }
808 #endif /* CONFIG_IEEE80211W */
809
810 return 0;
811 }
812
813
814 /**
815 * wpa_supplicant_set_suites - Set authentication and encryption parameters
816 * @wpa_s: Pointer to wpa_supplicant data
817 * @bss: Scan results for the selected BSS, or %NULL if not available
818 * @ssid: Configuration data for the selected network
819 * @wpa_ie: Buffer for the WPA/RSN IE
820 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
821 * used buffer length in case the functions returns success.
822 * Returns: 0 on success or -1 on failure
823 *
824 * This function is used to configure authentication and encryption parameters
825 * based on the network configuration and scan result for the selected BSS (if
826 * available).
827 */
828 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
829 struct wpa_bss *bss, struct wpa_ssid *ssid,
830 u8 *wpa_ie, size_t *wpa_ie_len)
831 {
832 struct wpa_ie_data ie;
833 int sel, proto;
834 const u8 *bss_wpa, *bss_rsn;
835
836 if (bss) {
837 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
838 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
839 } else
840 bss_wpa = bss_rsn = NULL;
841
842 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
843 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
844 (ie.group_cipher & ssid->group_cipher) &&
845 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
846 (ie.key_mgmt & ssid->key_mgmt)) {
847 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
848 proto = WPA_PROTO_RSN;
849 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
850 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
851 (ie.group_cipher & ssid->group_cipher) &&
852 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
853 (ie.key_mgmt & ssid->key_mgmt)) {
854 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
855 proto = WPA_PROTO_WPA;
856 } else if (bss) {
857 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
858 return -1;
859 } else {
860 if (ssid->proto & WPA_PROTO_RSN)
861 proto = WPA_PROTO_RSN;
862 else
863 proto = WPA_PROTO_WPA;
864 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
865 os_memset(&ie, 0, sizeof(ie));
866 ie.group_cipher = ssid->group_cipher;
867 ie.pairwise_cipher = ssid->pairwise_cipher;
868 ie.key_mgmt = ssid->key_mgmt;
869 #ifdef CONFIG_IEEE80211W
870 ie.mgmt_group_cipher =
871 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
872 WPA_CIPHER_AES_128_CMAC : 0;
873 #endif /* CONFIG_IEEE80211W */
874 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
875 "on configuration");
876 } else
877 proto = ie.proto;
878 }
879
880 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
881 "pairwise %d key_mgmt %d proto %d",
882 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
883 #ifdef CONFIG_IEEE80211W
884 if (ssid->ieee80211w) {
885 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
886 ie.mgmt_group_cipher);
887 }
888 #endif /* CONFIG_IEEE80211W */
889
890 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
891 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
892 !!(ssid->proto & WPA_PROTO_RSN));
893
894 if (bss || !wpa_s->ap_ies_from_associnfo) {
895 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
896 bss_wpa ? 2 + bss_wpa[1] : 0) ||
897 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
898 bss_rsn ? 2 + bss_rsn[1] : 0))
899 return -1;
900 }
901
902 sel = ie.group_cipher & ssid->group_cipher;
903 if (sel & WPA_CIPHER_CCMP) {
904 wpa_s->group_cipher = WPA_CIPHER_CCMP;
905 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
906 } else if (sel & WPA_CIPHER_TKIP) {
907 wpa_s->group_cipher = WPA_CIPHER_TKIP;
908 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
909 } else if (sel & WPA_CIPHER_WEP104) {
910 wpa_s->group_cipher = WPA_CIPHER_WEP104;
911 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
912 } else if (sel & WPA_CIPHER_WEP40) {
913 wpa_s->group_cipher = WPA_CIPHER_WEP40;
914 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
915 } else {
916 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
917 return -1;
918 }
919
920 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
921 if (sel & WPA_CIPHER_CCMP) {
922 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
923 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
924 } else if (sel & WPA_CIPHER_TKIP) {
925 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
926 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
927 } else if (sel & WPA_CIPHER_NONE) {
928 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
929 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
930 } else {
931 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
932 "cipher.");
933 return -1;
934 }
935
936 sel = ie.key_mgmt & ssid->key_mgmt;
937 if (0) {
938 #ifdef CONFIG_IEEE80211R
939 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
940 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
941 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
942 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
943 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
944 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
945 #endif /* CONFIG_IEEE80211R */
946 #ifdef CONFIG_IEEE80211W
947 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
948 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
949 wpa_msg(wpa_s, MSG_DEBUG,
950 "WPA: using KEY_MGMT 802.1X with SHA256");
951 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
952 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
953 wpa_msg(wpa_s, MSG_DEBUG,
954 "WPA: using KEY_MGMT PSK with SHA256");
955 #endif /* CONFIG_IEEE80211W */
956 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
957 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
958 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
959 } else if (sel & WPA_KEY_MGMT_PSK) {
960 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
961 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
962 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
963 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
964 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
965 } else {
966 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
967 "key management type.");
968 return -1;
969 }
970
971 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
972 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
973 wpa_s->pairwise_cipher);
974 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
975
976 #ifdef CONFIG_IEEE80211W
977 sel = ie.mgmt_group_cipher;
978 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
979 !(ie.capabilities & WPA_CAPABILITY_MFPC))
980 sel = 0;
981 if (sel & WPA_CIPHER_AES_128_CMAC) {
982 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
983 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
984 "AES-128-CMAC");
985 } else {
986 wpa_s->mgmt_group_cipher = 0;
987 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
988 }
989 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
990 wpa_s->mgmt_group_cipher);
991 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
992 #endif /* CONFIG_IEEE80211W */
993
994 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
995 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
996 return -1;
997 }
998
999 if (ssid->key_mgmt &
1000 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1001 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1002 else
1003 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1004
1005 return 0;
1006 }
1007
1008
1009 /**
1010 * wpa_supplicant_associate - Request association
1011 * @wpa_s: Pointer to wpa_supplicant data
1012 * @bss: Scan results for the selected BSS, or %NULL if not available
1013 * @ssid: Configuration data for the selected network
1014 *
1015 * This function is used to request %wpa_supplicant to associate with a BSS.
1016 */
1017 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1018 struct wpa_bss *bss, struct wpa_ssid *ssid)
1019 {
1020 u8 wpa_ie[200];
1021 size_t wpa_ie_len;
1022 int use_crypt, ret, i, bssid_changed;
1023 int algs = WPA_AUTH_ALG_OPEN;
1024 enum wpa_cipher cipher_pairwise, cipher_group;
1025 struct wpa_driver_associate_params params;
1026 int wep_keys_set = 0;
1027 struct wpa_driver_capa capa;
1028 int assoc_failed = 0;
1029 struct wpa_ssid *old_ssid;
1030
1031 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1032 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1033 #ifdef CONFIG_AP
1034 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1035 wpa_printf(MSG_INFO, "Driver does not support AP "
1036 "mode");
1037 return;
1038 }
1039 wpa_supplicant_create_ap(wpa_s, ssid);
1040 wpa_s->current_bss = bss;
1041 #else /* CONFIG_AP */
1042 wpa_printf(MSG_ERROR, "AP mode support not included in the "
1043 "build");
1044 #endif /* CONFIG_AP */
1045 return;
1046 }
1047
1048 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1049 ssid->mode == IEEE80211_MODE_INFRA) {
1050 sme_authenticate(wpa_s, bss, ssid);
1051 return;
1052 }
1053
1054 os_memset(&params, 0, sizeof(params));
1055 wpa_s->reassociate = 0;
1056 if (bss) {
1057 #ifdef CONFIG_IEEE80211R
1058 const u8 *ie, *md = NULL;
1059 #endif /* CONFIG_IEEE80211R */
1060 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1061 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1062 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1063 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1064 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1065 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1066 if (bssid_changed)
1067 wpas_notify_bssid_changed(wpa_s);
1068 #ifdef CONFIG_IEEE80211R
1069 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1070 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1071 md = ie + 2;
1072 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1073 if (md) {
1074 /* Prepare for the next transition */
1075 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1076 }
1077 #endif /* CONFIG_IEEE80211R */
1078 #ifdef CONFIG_WPS
1079 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1080 wpa_s->conf->ap_scan == 2 &&
1081 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1082 /* Use ap_scan==1 style network selection to find the network
1083 */
1084 wpa_s->scan_req = 2;
1085 wpa_s->reassociate = 1;
1086 wpa_supplicant_req_scan(wpa_s, 0, 0);
1087 return;
1088 #endif /* CONFIG_WPS */
1089 } else {
1090 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1091 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1092 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1093 }
1094 wpa_supplicant_cancel_scan(wpa_s);
1095
1096 /* Starting new association, so clear the possibly used WPA IE from the
1097 * previous association. */
1098 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1099
1100 #ifdef IEEE8021X_EAPOL
1101 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1102 if (ssid->leap) {
1103 if (ssid->non_leap == 0)
1104 algs = WPA_AUTH_ALG_LEAP;
1105 else
1106 algs |= WPA_AUTH_ALG_LEAP;
1107 }
1108 }
1109 #endif /* IEEE8021X_EAPOL */
1110 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1111 if (ssid->auth_alg) {
1112 algs = ssid->auth_alg;
1113 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1114 algs);
1115 }
1116
1117 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1118 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1119 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1120 WPA_KEY_MGMT_FT_IEEE8021X |
1121 WPA_KEY_MGMT_FT_PSK |
1122 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1123 WPA_KEY_MGMT_PSK_SHA256))) {
1124 int try_opportunistic;
1125 try_opportunistic = ssid->proactive_key_caching &&
1126 (ssid->proto & WPA_PROTO_RSN);
1127 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1128 wpa_s->current_ssid,
1129 try_opportunistic) == 0)
1130 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1131 wpa_ie_len = sizeof(wpa_ie);
1132 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1133 wpa_ie, &wpa_ie_len)) {
1134 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1135 "management and encryption suites");
1136 return;
1137 }
1138 } else if (ssid->key_mgmt &
1139 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1140 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1141 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1142 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1143 wpa_ie_len = sizeof(wpa_ie);
1144 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1145 wpa_ie, &wpa_ie_len)) {
1146 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1147 "management and encryption suites (no scan "
1148 "results)");
1149 return;
1150 }
1151 #ifdef CONFIG_WPS
1152 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1153 struct wpabuf *wps_ie;
1154 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1155 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1156 wpa_ie_len = wpabuf_len(wps_ie);
1157 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1158 } else
1159 wpa_ie_len = 0;
1160 wpabuf_free(wps_ie);
1161 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1162 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1163 params.wps = WPS_MODE_PRIVACY;
1164 else
1165 params.wps = WPS_MODE_OPEN;
1166 #endif /* CONFIG_WPS */
1167 } else {
1168 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1169 wpa_ie_len = 0;
1170 }
1171
1172 #ifdef CONFIG_P2P
1173 if (wpa_s->global->p2p) {
1174 u8 *pos;
1175 size_t len;
1176 int res;
1177 int p2p_group;
1178 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1179 pos = wpa_ie + wpa_ie_len;
1180 len = sizeof(wpa_ie) - wpa_ie_len;
1181 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1182 if (res >= 0)
1183 wpa_ie_len += res;
1184 }
1185
1186 wpa_s->cross_connect_disallowed = 0;
1187 if (bss) {
1188 struct wpabuf *p2p;
1189 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1190 if (p2p) {
1191 wpa_s->cross_connect_disallowed =
1192 p2p_get_cross_connect_disallowed(p2p);
1193 wpabuf_free(p2p);
1194 wpa_printf(MSG_DEBUG, "P2P: WLAN AP %s cross "
1195 "connection",
1196 wpa_s->cross_connect_disallowed ?
1197 "disallows" : "allows");
1198 }
1199 }
1200 #endif /* CONFIG_P2P */
1201
1202 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1203 use_crypt = 1;
1204 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1205 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1206 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1207 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1208 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1209 use_crypt = 0;
1210 if (wpa_set_wep_keys(wpa_s, ssid)) {
1211 use_crypt = 1;
1212 wep_keys_set = 1;
1213 }
1214 }
1215 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1216 use_crypt = 0;
1217
1218 #ifdef IEEE8021X_EAPOL
1219 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1220 if ((ssid->eapol_flags &
1221 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1222 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1223 !wep_keys_set) {
1224 use_crypt = 0;
1225 } else {
1226 /* Assume that dynamic WEP-104 keys will be used and
1227 * set cipher suites in order for drivers to expect
1228 * encryption. */
1229 cipher_pairwise = cipher_group = CIPHER_WEP104;
1230 }
1231 }
1232 #endif /* IEEE8021X_EAPOL */
1233
1234 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1235 /* Set the key before (and later after) association */
1236 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1237 }
1238
1239 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1240 if (bss) {
1241 params.bssid = bss->bssid;
1242 params.ssid = bss->ssid;
1243 params.ssid_len = bss->ssid_len;
1244 params.freq = bss->freq;
1245 } else {
1246 params.ssid = ssid->ssid;
1247 params.ssid_len = ssid->ssid_len;
1248 }
1249 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1250 params.freq == 0)
1251 params.freq = ssid->frequency; /* Initial channel for IBSS */
1252 params.wpa_ie = wpa_ie;
1253 params.wpa_ie_len = wpa_ie_len;
1254 params.pairwise_suite = cipher_pairwise;
1255 params.group_suite = cipher_group;
1256 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1257 params.auth_alg = algs;
1258 params.mode = ssid->mode;
1259 for (i = 0; i < NUM_WEP_KEYS; i++) {
1260 if (ssid->wep_key_len[i])
1261 params.wep_key[i] = ssid->wep_key[i];
1262 params.wep_key_len[i] = ssid->wep_key_len[i];
1263 }
1264 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1265
1266 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1267 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1268 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1269 params.passphrase = ssid->passphrase;
1270 if (ssid->psk_set)
1271 params.psk = ssid->psk;
1272 }
1273
1274 params.drop_unencrypted = use_crypt;
1275
1276 #ifdef CONFIG_IEEE80211W
1277 params.mgmt_frame_protection = ssid->ieee80211w;
1278 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1279 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1280 struct wpa_ie_data ie;
1281 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1282 ie.capabilities &
1283 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1284 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1285 "require MFP");
1286 params.mgmt_frame_protection =
1287 MGMT_FRAME_PROTECTION_REQUIRED;
1288 }
1289 }
1290 #endif /* CONFIG_IEEE80211W */
1291
1292 #ifdef CONFIG_P2P
1293 if (wpa_s->global->p2p &&
1294 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1295 params.p2p = 1;
1296 #endif /* CONFIG_P2P */
1297
1298 if (wpa_s->parent->set_sta_uapsd)
1299 params.uapsd = wpa_s->parent->sta_uapsd;
1300 else
1301 params.uapsd = -1;
1302
1303 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1304 ret = ieee80211_sta_associate(wpa_s, &params);
1305 else
1306 ret = wpa_drv_associate(wpa_s, &params);
1307 if (ret < 0) {
1308 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1309 "failed");
1310 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1311 /*
1312 * The driver is known to mean what is saying, so we
1313 * can stop right here; the association will not
1314 * succeed.
1315 */
1316 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1317 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1318 return;
1319 }
1320 /* try to continue anyway; new association will be tried again
1321 * after timeout */
1322 assoc_failed = 1;
1323 }
1324
1325 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1326 /* Set the key after the association just in case association
1327 * cleared the previously configured key. */
1328 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1329 /* No need to timeout authentication since there is no key
1330 * management. */
1331 wpa_supplicant_cancel_auth_timeout(wpa_s);
1332 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1333 #ifdef CONFIG_IBSS_RSN
1334 } else if (ssid->mode == WPAS_MODE_IBSS &&
1335 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1336 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1337 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1338 /*
1339 * RSN IBSS authentication is per-STA and we can disable the
1340 * per-BSSID authentication.
1341 */
1342 wpa_supplicant_cancel_auth_timeout(wpa_s);
1343 #endif /* CONFIG_IBSS_RSN */
1344 } else {
1345 /* Timeout for IEEE 802.11 authentication and association */
1346 int timeout = 60;
1347
1348 if (assoc_failed) {
1349 /* give IBSS a bit more time */
1350 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1351 } else if (wpa_s->conf->ap_scan == 1) {
1352 /* give IBSS a bit more time */
1353 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1354 }
1355 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1356 }
1357
1358 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1359 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1360 /* Set static WEP keys again */
1361 wpa_set_wep_keys(wpa_s, ssid);
1362 }
1363
1364 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1365 /*
1366 * Do not allow EAP session resumption between different
1367 * network configurations.
1368 */
1369 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1370 }
1371 old_ssid = wpa_s->current_ssid;
1372 wpa_s->current_ssid = ssid;
1373 wpa_s->current_bss = bss;
1374 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1375 wpa_supplicant_initiate_eapol(wpa_s);
1376 if (old_ssid != wpa_s->current_ssid)
1377 wpas_notify_network_changed(wpa_s);
1378 }
1379
1380
1381 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1382 const u8 *addr)
1383 {
1384 struct wpa_ssid *old_ssid;
1385
1386 wpa_clear_keys(wpa_s, addr);
1387 wpa_supplicant_mark_disassoc(wpa_s);
1388 old_ssid = wpa_s->current_ssid;
1389 wpa_s->current_ssid = NULL;
1390 wpa_s->current_bss = NULL;
1391 wpa_sm_set_config(wpa_s->wpa, NULL);
1392 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1393 if (old_ssid != wpa_s->current_ssid)
1394 wpas_notify_network_changed(wpa_s);
1395 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1396 }
1397
1398
1399 /**
1400 * wpa_supplicant_disassociate - Disassociate the current connection
1401 * @wpa_s: Pointer to wpa_supplicant data
1402 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1403 *
1404 * This function is used to request %wpa_supplicant to disassociate with the
1405 * current AP.
1406 */
1407 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1408 int reason_code)
1409 {
1410 u8 *addr = NULL;
1411
1412 if (!is_zero_ether_addr(wpa_s->bssid)) {
1413 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1414 ieee80211_sta_disassociate(wpa_s, reason_code);
1415 else
1416 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1417 addr = wpa_s->bssid;
1418 }
1419
1420 wpa_supplicant_clear_connection(wpa_s, addr);
1421 }
1422
1423
1424 /**
1425 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1426 * @wpa_s: Pointer to wpa_supplicant data
1427 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1428 *
1429 * This function is used to request %wpa_supplicant to deauthenticate from the
1430 * current AP.
1431 */
1432 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1433 int reason_code)
1434 {
1435 u8 *addr = NULL;
1436
1437 if (!is_zero_ether_addr(wpa_s->bssid)) {
1438 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1439 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1440 else
1441 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1442 reason_code);
1443 addr = wpa_s->bssid;
1444 }
1445
1446 wpa_supplicant_clear_connection(wpa_s, addr);
1447 }
1448
1449
1450 /**
1451 * wpa_supplicant_enable_network - Mark a configured network as enabled
1452 * @wpa_s: wpa_supplicant structure for a network interface
1453 * @ssid: wpa_ssid structure for a configured network or %NULL
1454 *
1455 * Enables the specified network or all networks if no network specified.
1456 */
1457 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1458 struct wpa_ssid *ssid)
1459 {
1460 struct wpa_ssid *other_ssid;
1461 int was_disabled;
1462
1463 if (ssid == NULL) {
1464 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1465 other_ssid = other_ssid->next) {
1466 if (other_ssid->disabled == 2)
1467 continue; /* do not change persistent P2P group
1468 * data */
1469 if (other_ssid == wpa_s->current_ssid &&
1470 other_ssid->disabled)
1471 wpa_s->reassociate = 1;
1472
1473 was_disabled = other_ssid->disabled;
1474
1475 other_ssid->disabled = 0;
1476
1477 if (was_disabled != other_ssid->disabled)
1478 wpas_notify_network_enabled_changed(
1479 wpa_s, other_ssid);
1480 }
1481 if (wpa_s->reassociate)
1482 wpa_supplicant_req_scan(wpa_s, 0, 0);
1483 } else if (ssid->disabled && ssid->disabled != 2) {
1484 if (wpa_s->current_ssid == NULL) {
1485 /*
1486 * Try to reassociate since there is no current
1487 * configuration and a new network was made available.
1488 */
1489 wpa_s->reassociate = 1;
1490 wpa_supplicant_req_scan(wpa_s, 0, 0);
1491 }
1492
1493 was_disabled = ssid->disabled;
1494
1495 ssid->disabled = 0;
1496
1497 if (was_disabled != ssid->disabled)
1498 wpas_notify_network_enabled_changed(wpa_s, ssid);
1499 }
1500 }
1501
1502
1503 /**
1504 * wpa_supplicant_disable_network - Mark a configured network as disabled
1505 * @wpa_s: wpa_supplicant structure for a network interface
1506 * @ssid: wpa_ssid structure for a configured network or %NULL
1507 *
1508 * Disables the specified network or all networks if no network specified.
1509 */
1510 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1511 struct wpa_ssid *ssid)
1512 {
1513 struct wpa_ssid *other_ssid;
1514 int was_disabled;
1515
1516 if (ssid == NULL) {
1517 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1518 other_ssid = other_ssid->next) {
1519 was_disabled = other_ssid->disabled;
1520 if (was_disabled == 2)
1521 continue; /* do not change persistent P2P group
1522 * data */
1523
1524 other_ssid->disabled = 1;
1525
1526 if (was_disabled != other_ssid->disabled)
1527 wpas_notify_network_enabled_changed(
1528 wpa_s, other_ssid);
1529 }
1530 if (wpa_s->current_ssid)
1531 wpa_supplicant_disassociate(
1532 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1533 } else if (ssid->disabled != 2) {
1534 if (ssid == wpa_s->current_ssid)
1535 wpa_supplicant_disassociate(
1536 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1537
1538 was_disabled = ssid->disabled;
1539
1540 ssid->disabled = 1;
1541
1542 if (was_disabled != ssid->disabled)
1543 wpas_notify_network_enabled_changed(wpa_s, ssid);
1544 }
1545 }
1546
1547
1548 /**
1549 * wpa_supplicant_select_network - Attempt association with a network
1550 * @wpa_s: wpa_supplicant structure for a network interface
1551 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1552 */
1553 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1554 struct wpa_ssid *ssid)
1555 {
1556
1557 struct wpa_ssid *other_ssid;
1558
1559 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1560 wpa_supplicant_disassociate(
1561 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1562
1563 /*
1564 * Mark all other networks disabled or mark all networks enabled if no
1565 * network specified.
1566 */
1567 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1568 other_ssid = other_ssid->next) {
1569 int was_disabled = other_ssid->disabled;
1570 if (was_disabled == 2)
1571 continue; /* do not change persistent P2P group data */
1572
1573 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1574
1575 if (was_disabled != other_ssid->disabled)
1576 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1577 }
1578 wpa_s->disconnected = 0;
1579 wpa_s->reassociate = 1;
1580 wpa_supplicant_req_scan(wpa_s, 0, 0);
1581
1582 if (ssid)
1583 wpas_notify_network_selected(wpa_s, ssid);
1584 }
1585
1586
1587 /**
1588 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1589 * @wpa_s: wpa_supplicant structure for a network interface
1590 * @ap_scan: AP scan mode
1591 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1592 *
1593 */
1594 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1595 {
1596
1597 int old_ap_scan;
1598
1599 if (ap_scan < 0 || ap_scan > 2)
1600 return -1;
1601
1602 old_ap_scan = wpa_s->conf->ap_scan;
1603 wpa_s->conf->ap_scan = ap_scan;
1604
1605 if (old_ap_scan != wpa_s->conf->ap_scan)
1606 wpas_notify_ap_scan_changed(wpa_s);
1607
1608 return 0;
1609 }
1610
1611
1612 /**
1613 * wpa_supplicant_set_debug_params - Set global debug params
1614 * @global: wpa_global structure
1615 * @debug_level: debug level
1616 * @debug_timestamp: determines if show timestamp in debug data
1617 * @debug_show_keys: determines if show keys in debug data
1618 * Returns: 0 if succeed or -1 if debug_level has wrong value
1619 */
1620 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1621 int debug_timestamp, int debug_show_keys)
1622 {
1623
1624 int old_level, old_timestamp, old_show_keys;
1625
1626 /* check for allowed debuglevels */
1627 if (debug_level != MSG_MSGDUMP &&
1628 debug_level != MSG_DEBUG &&
1629 debug_level != MSG_INFO &&
1630 debug_level != MSG_WARNING &&
1631 debug_level != MSG_ERROR)
1632 return -1;
1633
1634 old_level = wpa_debug_level;
1635 old_timestamp = wpa_debug_timestamp;
1636 old_show_keys = wpa_debug_show_keys;
1637
1638 wpa_debug_level = debug_level;
1639 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1640 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1641
1642 if (wpa_debug_level != old_level)
1643 wpas_notify_debug_level_changed(global);
1644 if (wpa_debug_timestamp != old_timestamp)
1645 wpas_notify_debug_timestamp_changed(global);
1646 if (wpa_debug_show_keys != old_show_keys)
1647 wpas_notify_debug_show_keys_changed(global);
1648
1649 return 0;
1650 }
1651
1652
1653 /**
1654 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1655 * @wpa_s: Pointer to wpa_supplicant data
1656 * Returns: A pointer to the current network structure or %NULL on failure
1657 */
1658 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1659 {
1660 struct wpa_ssid *entry;
1661 u8 ssid[MAX_SSID_LEN];
1662 int res;
1663 size_t ssid_len;
1664 u8 bssid[ETH_ALEN];
1665 int wired;
1666
1667 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1668 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1669 wpa_printf(MSG_WARNING, "Could not read SSID from "
1670 "MLME.");
1671 return NULL;
1672 }
1673 } else {
1674 res = wpa_drv_get_ssid(wpa_s, ssid);
1675 if (res < 0) {
1676 wpa_printf(MSG_WARNING, "Could not read SSID from "
1677 "driver.");
1678 return NULL;
1679 }
1680 ssid_len = res;
1681 }
1682
1683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1684 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1685 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1686 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1687 return NULL;
1688 }
1689
1690 wired = wpa_s->conf->ap_scan == 0 &&
1691 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1692
1693 entry = wpa_s->conf->ssid;
1694 while (entry) {
1695 if (!entry->disabled &&
1696 ((ssid_len == entry->ssid_len &&
1697 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1698 (!entry->bssid_set ||
1699 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1700 return entry;
1701 #ifdef CONFIG_WPS
1702 if (!entry->disabled &&
1703 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1704 (entry->ssid == NULL || entry->ssid_len == 0) &&
1705 (!entry->bssid_set ||
1706 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1707 return entry;
1708 #endif /* CONFIG_WPS */
1709 entry = entry->next;
1710 }
1711
1712 return NULL;
1713 }
1714
1715
1716 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1717 const char *name)
1718 {
1719 int i;
1720 size_t len;
1721 const char *pos, *driver = name;
1722
1723 if (wpa_s == NULL)
1724 return -1;
1725
1726 if (wpa_drivers[0] == NULL) {
1727 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1728 "wpa_supplicant.");
1729 return -1;
1730 }
1731
1732 if (name == NULL) {
1733 /* default to first driver in the list */
1734 wpa_s->driver = wpa_drivers[0];
1735 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1736 return 0;
1737 }
1738
1739 do {
1740 pos = os_strchr(driver, ',');
1741 if (pos)
1742 len = pos - driver;
1743 else
1744 len = os_strlen(driver);
1745
1746 for (i = 0; wpa_drivers[i]; i++) {
1747 if (os_strlen(wpa_drivers[i]->name) == len &&
1748 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1749 0) {
1750 wpa_s->driver = wpa_drivers[i];
1751 wpa_s->global_drv_priv =
1752 wpa_s->global->drv_priv[i];
1753 return 0;
1754 }
1755 }
1756
1757 driver = pos + 1;
1758 } while (pos);
1759
1760 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1761 return -1;
1762 }
1763
1764
1765 /**
1766 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1767 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1768 * with struct wpa_driver_ops::init()
1769 * @src_addr: Source address of the EAPOL frame
1770 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1771 * @len: Length of the EAPOL data
1772 *
1773 * This function is called for each received EAPOL frame. Most driver
1774 * interfaces rely on more generic OS mechanism for receiving frames through
1775 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1776 * take care of received EAPOL frames and deliver them to the core supplicant
1777 * code by calling this function.
1778 */
1779 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1780 const u8 *buf, size_t len)
1781 {
1782 struct wpa_supplicant *wpa_s = ctx;
1783
1784 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1785 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1786
1787 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1788 /*
1789 * There is possible race condition between receiving the
1790 * association event and the EAPOL frame since they are coming
1791 * through different paths from the driver. In order to avoid
1792 * issues in trying to process the EAPOL frame before receiving
1793 * association information, lets queue it for processing until
1794 * the association event is received.
1795 */
1796 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1797 "received EAPOL frame");
1798 wpabuf_free(wpa_s->pending_eapol_rx);
1799 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1800 if (wpa_s->pending_eapol_rx) {
1801 os_get_time(&wpa_s->pending_eapol_rx_time);
1802 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1803 ETH_ALEN);
1804 }
1805 return;
1806 }
1807
1808 #ifdef CONFIG_AP
1809 if (wpa_s->ap_iface) {
1810 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1811 return;
1812 }
1813 #endif /* CONFIG_AP */
1814
1815 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1816 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1817 "no key management is configured");
1818 return;
1819 }
1820
1821 if (wpa_s->eapol_received == 0 &&
1822 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1823 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1824 wpa_s->wpa_state != WPA_COMPLETED) &&
1825 (wpa_s->current_ssid == NULL ||
1826 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1827 /* Timeout for completing IEEE 802.1X and WPA authentication */
1828 wpa_supplicant_req_auth_timeout(
1829 wpa_s,
1830 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1831 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1832 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1833 70 : 10, 0);
1834 }
1835 wpa_s->eapol_received++;
1836
1837 if (wpa_s->countermeasures) {
1838 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1839 "packet");
1840 return;
1841 }
1842
1843 #ifdef CONFIG_IBSS_RSN
1844 if (wpa_s->current_ssid &&
1845 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1846 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1847 return;
1848 }
1849 #endif /* CONFIG_IBSS_RSN */
1850
1851 /* Source address of the incoming EAPOL frame could be compared to the
1852 * current BSSID. However, it is possible that a centralized
1853 * Authenticator could be using another MAC address than the BSSID of
1854 * an AP, so just allow any address to be used for now. The replies are
1855 * still sent to the current BSSID (if available), though. */
1856
1857 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1858 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1859 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1860 return;
1861 wpa_drv_poll(wpa_s);
1862 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1863 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1864 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1865 /*
1866 * Set portValid = TRUE here since we are going to skip 4-way
1867 * handshake processing which would normally set portValid. We
1868 * need this to allow the EAPOL state machines to be completed
1869 * without going through EAPOL-Key handshake.
1870 */
1871 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1872 }
1873 }
1874
1875
1876 /**
1877 * wpa_supplicant_driver_init - Initialize driver interface parameters
1878 * @wpa_s: Pointer to wpa_supplicant data
1879 * Returns: 0 on success, -1 on failure
1880 *
1881 * This function is called to initialize driver interface parameters.
1882 * wpa_drv_init() must have been called before this function to initialize the
1883 * driver interface.
1884 */
1885 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1886 {
1887 static int interface_count = 0;
1888
1889 if (wpa_s->driver->send_eapol) {
1890 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1891 if (addr)
1892 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1893 } else if (!(wpa_s->drv_flags &
1894 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
1895 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1896 wpa_drv_get_mac_addr(wpa_s),
1897 ETH_P_EAPOL,
1898 wpa_supplicant_rx_eapol, wpa_s, 0);
1899 if (wpa_s->l2 == NULL)
1900 return -1;
1901 } else {
1902 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1903 if (addr)
1904 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1905 }
1906
1907 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1908 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1909 return -1;
1910 }
1911
1912 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1913 MAC2STR(wpa_s->own_addr));
1914
1915 if (wpa_s->bridge_ifname[0]) {
1916 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1917 " '%s'", wpa_s->bridge_ifname);
1918 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1919 wpa_s->own_addr,
1920 ETH_P_EAPOL,
1921 wpa_supplicant_rx_eapol, wpa_s,
1922 0);
1923 if (wpa_s->l2_br == NULL) {
1924 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1925 "connection for the bridge interface '%s'",
1926 wpa_s->bridge_ifname);
1927 return -1;
1928 }
1929 }
1930
1931 wpa_clear_keys(wpa_s, NULL);
1932
1933 /* Make sure that TKIP countermeasures are not left enabled (could
1934 * happen if wpa_supplicant is killed during countermeasures. */
1935 wpa_drv_set_countermeasures(wpa_s, 0);
1936
1937 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1938 wpa_drv_flush_pmkid(wpa_s);
1939
1940 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1941 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
1942 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1943 interface_count++;
1944 } else
1945 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1946
1947 return 0;
1948 }
1949
1950
1951 static int wpa_supplicant_daemon(const char *pid_file)
1952 {
1953 wpa_printf(MSG_DEBUG, "Daemonize..");
1954 return os_daemonize(pid_file);
1955 }
1956
1957
1958 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1959 {
1960 struct wpa_supplicant *wpa_s;
1961
1962 wpa_s = os_zalloc(sizeof(*wpa_s));
1963 if (wpa_s == NULL)
1964 return NULL;
1965 wpa_s->scan_req = 1;
1966 wpa_s->new_connection = 1;
1967 wpa_s->parent = wpa_s;
1968
1969 return wpa_s;
1970 }
1971
1972
1973 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1974 struct wpa_interface *iface)
1975 {
1976 const char *ifname, *driver;
1977 struct wpa_driver_capa capa;
1978
1979 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1980 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1981 iface->confname ? iface->confname : "N/A",
1982 iface->driver ? iface->driver : "default",
1983 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1984 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1985
1986 if (iface->confname) {
1987 #ifdef CONFIG_BACKEND_FILE
1988 wpa_s->confname = os_rel2abs_path(iface->confname);
1989 if (wpa_s->confname == NULL) {
1990 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1991 "for configuration file '%s'.",
1992 iface->confname);
1993 return -1;
1994 }
1995 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1996 iface->confname, wpa_s->confname);
1997 #else /* CONFIG_BACKEND_FILE */
1998 wpa_s->confname = os_strdup(iface->confname);
1999 #endif /* CONFIG_BACKEND_FILE */
2000 wpa_s->conf = wpa_config_read(wpa_s->confname);
2001 if (wpa_s->conf == NULL) {
2002 wpa_printf(MSG_ERROR, "Failed to read or parse "
2003 "configuration '%s'.", wpa_s->confname);
2004 return -1;
2005 }
2006
2007 /*
2008 * Override ctrl_interface and driver_param if set on command
2009 * line.
2010 */
2011 if (iface->ctrl_interface) {
2012 os_free(wpa_s->conf->ctrl_interface);
2013 wpa_s->conf->ctrl_interface =
2014 os_strdup(iface->ctrl_interface);
2015 }
2016
2017 if (iface->driver_param) {
2018 os_free(wpa_s->conf->driver_param);
2019 wpa_s->conf->driver_param =
2020 os_strdup(iface->driver_param);
2021 }
2022 } else
2023 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2024 iface->driver_param);
2025
2026 if (wpa_s->conf == NULL) {
2027 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2028 return -1;
2029 }
2030
2031 if (iface->ifname == NULL) {
2032 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2033 return -1;
2034 }
2035 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2036 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2037 iface->ifname);
2038 return -1;
2039 }
2040 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2041
2042 if (iface->bridge_ifname) {
2043 if (os_strlen(iface->bridge_ifname) >=
2044 sizeof(wpa_s->bridge_ifname)) {
2045 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2046 "name '%s'.", iface->bridge_ifname);
2047 return -1;
2048 }
2049 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2050 sizeof(wpa_s->bridge_ifname));
2051 }
2052
2053 /* RSNA Supplicant Key Management - INITIALIZE */
2054 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2055 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2056
2057 /* Initialize driver interface and register driver event handler before
2058 * L2 receive handler so that association events are processed before
2059 * EAPOL-Key packets if both become available for the same select()
2060 * call. */
2061 driver = iface->driver;
2062 next_driver:
2063 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2064 return -1;
2065
2066 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2067 if (wpa_s->drv_priv == NULL) {
2068 const char *pos;
2069 pos = driver ? os_strchr(driver, ',') : NULL;
2070 if (pos) {
2071 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2072 "interface - try next driver wrapper");
2073 driver = pos + 1;
2074 goto next_driver;
2075 }
2076 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2077 return -1;
2078 }
2079 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2080 wpa_printf(MSG_ERROR, "Driver interface rejected "
2081 "driver_param '%s'", wpa_s->conf->driver_param);
2082 return -1;
2083 }
2084
2085 ifname = wpa_drv_get_ifname(wpa_s);
2086 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2087 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2088 "name with '%s'", ifname);
2089 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2090 }
2091
2092 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2093 return -1;
2094
2095 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2096 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2097 NULL);
2098 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2099
2100 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2101 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2102 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2103 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2104 "dot11RSNAConfigPMKLifetime");
2105 return -1;
2106 }
2107
2108 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2109 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2110 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2111 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2112 "dot11RSNAConfigPMKReauthThreshold");
2113 return -1;
2114 }
2115
2116 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2117 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2118 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2119 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2120 "dot11RSNAConfigSATimeout");
2121 return -1;
2122 }
2123
2124 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2125 wpa_s->drv_flags = capa.flags;
2126 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2127 if (ieee80211_sta_init(wpa_s))
2128 return -1;
2129 }
2130 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2131 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2132 wpa_s->max_stations = capa.max_stations;
2133 }
2134 if (wpa_s->max_remain_on_chan == 0)
2135 wpa_s->max_remain_on_chan = 1000;
2136
2137 if (wpa_supplicant_driver_init(wpa_s) < 0)
2138 return -1;
2139
2140 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2141 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2142 wpa_printf(MSG_DEBUG, "Failed to set country");
2143 return -1;
2144 }
2145
2146 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2147
2148 if (wpas_wps_init(wpa_s))
2149 return -1;
2150
2151 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2152 return -1;
2153 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2154
2155 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2156 if (wpa_s->ctrl_iface == NULL) {
2157 wpa_printf(MSG_ERROR,
2158 "Failed to initialize control interface '%s'.\n"
2159 "You may have another wpa_supplicant process "
2160 "already running or the file was\n"
2161 "left by an unclean termination of wpa_supplicant "
2162 "in which case you will need\n"
2163 "to manually remove this file before starting "
2164 "wpa_supplicant again.\n",
2165 wpa_s->conf->ctrl_interface);
2166 return -1;
2167 }
2168
2169 #ifdef CONFIG_IBSS_RSN
2170 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2171 if (!wpa_s->ibss_rsn) {
2172 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2173 return -1;
2174 }
2175 #endif /* CONFIG_IBSS_RSN */
2176
2177 #ifdef CONFIG_P2P
2178 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2179 wpa_printf(MSG_ERROR, "Failed to init P2P");
2180 return -1;
2181 }
2182 #endif /* CONFIG_P2P */
2183
2184 if (wpa_bss_init(wpa_s) < 0)
2185 return -1;
2186
2187 return 0;
2188 }
2189
2190
2191 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2192 int notify)
2193 {
2194 if (wpa_s->drv_priv) {
2195 wpa_supplicant_deauthenticate(wpa_s,
2196 WLAN_REASON_DEAUTH_LEAVING);
2197
2198 wpa_drv_set_countermeasures(wpa_s, 0);
2199 wpa_clear_keys(wpa_s, NULL);
2200 }
2201
2202 wpa_supplicant_cleanup(wpa_s);
2203
2204 if (notify)
2205 wpas_notify_iface_removed(wpa_s);
2206
2207 if (wpa_s->drv_priv)
2208 wpa_drv_deinit(wpa_s);
2209 }
2210
2211
2212 /**
2213 * wpa_supplicant_add_iface - Add a new network interface
2214 * @global: Pointer to global data from wpa_supplicant_init()
2215 * @iface: Interface configuration options
2216 * Returns: Pointer to the created interface or %NULL on failure
2217 *
2218 * This function is used to add new network interfaces for %wpa_supplicant.
2219 * This can be called before wpa_supplicant_run() to add interfaces before the
2220 * main event loop has been started. In addition, new interfaces can be added
2221 * dynamically while %wpa_supplicant is already running. This could happen,
2222 * e.g., when a hotplug network adapter is inserted.
2223 */
2224 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2225 struct wpa_interface *iface)
2226 {
2227 struct wpa_supplicant *wpa_s;
2228 struct wpa_interface t_iface;
2229 struct wpa_ssid *ssid;
2230
2231 if (global == NULL || iface == NULL)
2232 return NULL;
2233
2234 wpa_s = wpa_supplicant_alloc();
2235 if (wpa_s == NULL)
2236 return NULL;
2237
2238 wpa_s->global = global;
2239
2240 t_iface = *iface;
2241 if (global->params.override_driver) {
2242 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2243 "('%s' -> '%s')",
2244 iface->driver, global->params.override_driver);
2245 t_iface.driver = global->params.override_driver;
2246 }
2247 if (global->params.override_ctrl_interface) {
2248 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2249 "ctrl_interface ('%s' -> '%s')",
2250 iface->ctrl_interface,
2251 global->params.override_ctrl_interface);
2252 t_iface.ctrl_interface =
2253 global->params.override_ctrl_interface;
2254 }
2255 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2256 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2257 iface->ifname);
2258 wpa_supplicant_deinit_iface(wpa_s, 0);
2259 os_free(wpa_s);
2260 return NULL;
2261 }
2262
2263 /* Notify the control interfaces about new iface */
2264 if (wpas_notify_iface_added(wpa_s)) {
2265 wpa_supplicant_deinit_iface(wpa_s, 1);
2266 os_free(wpa_s);
2267 return NULL;
2268 }
2269
2270 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2271 wpas_notify_network_added(wpa_s, ssid);
2272
2273 wpa_s->next = global->ifaces;
2274 global->ifaces = wpa_s;
2275
2276 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2277
2278 return wpa_s;
2279 }
2280
2281
2282 /**
2283 * wpa_supplicant_remove_iface - Remove a network interface
2284 * @global: Pointer to global data from wpa_supplicant_init()
2285 * @wpa_s: Pointer to the network interface to be removed
2286 * Returns: 0 if interface was removed, -1 if interface was not found
2287 *
2288 * This function can be used to dynamically remove network interfaces from
2289 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2290 * addition, this function is used to remove all remaining interfaces when
2291 * %wpa_supplicant is terminated.
2292 */
2293 int wpa_supplicant_remove_iface(struct wpa_global *global,
2294 struct wpa_supplicant *wpa_s)
2295 {
2296 struct wpa_supplicant *prev;
2297
2298 /* Remove interface from the global list of interfaces */
2299 prev = global->ifaces;
2300 if (prev == wpa_s) {
2301 global->ifaces = wpa_s->next;
2302 } else {
2303 while (prev && prev->next != wpa_s)
2304 prev = prev->next;
2305 if (prev == NULL)
2306 return -1;
2307 prev->next = wpa_s->next;
2308 }
2309
2310 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2311
2312 if (global->p2p_group_formation == wpa_s)
2313 global->p2p_group_formation = NULL;
2314 wpa_supplicant_deinit_iface(wpa_s, 1);
2315 os_free(wpa_s);
2316
2317 return 0;
2318 }
2319
2320
2321 /**
2322 * wpa_supplicant_get_iface - Get a new network interface
2323 * @global: Pointer to global data from wpa_supplicant_init()
2324 * @ifname: Interface name
2325 * Returns: Pointer to the interface or %NULL if not found
2326 */
2327 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2328 const char *ifname)
2329 {
2330 struct wpa_supplicant *wpa_s;
2331
2332 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2333 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2334 return wpa_s;
2335 }
2336 return NULL;
2337 }
2338
2339
2340 /**
2341 * wpa_supplicant_init - Initialize %wpa_supplicant
2342 * @params: Parameters for %wpa_supplicant
2343 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2344 *
2345 * This function is used to initialize %wpa_supplicant. After successful
2346 * initialization, the returned data pointer can be used to add and remove
2347 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2348 */
2349 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2350 {
2351 struct wpa_global *global;
2352 int ret, i;
2353
2354 if (params == NULL)
2355 return NULL;
2356
2357 wpa_debug_open_file(params->wpa_debug_file_path);
2358 if (params->wpa_debug_syslog)
2359 wpa_debug_open_syslog();
2360
2361 ret = eap_register_methods();
2362 if (ret) {
2363 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2364 if (ret == -2)
2365 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2366 "the same EAP type.");
2367 return NULL;
2368 }
2369
2370 global = os_zalloc(sizeof(*global));
2371 if (global == NULL)
2372 return NULL;
2373 dl_list_init(&global->p2p_srv_bonjour);
2374 dl_list_init(&global->p2p_srv_upnp);
2375 global->params.daemonize = params->daemonize;
2376 global->params.wait_for_monitor = params->wait_for_monitor;
2377 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2378 if (params->pid_file)
2379 global->params.pid_file = os_strdup(params->pid_file);
2380 if (params->ctrl_interface)
2381 global->params.ctrl_interface =
2382 os_strdup(params->ctrl_interface);
2383 if (params->override_driver)
2384 global->params.override_driver =
2385 os_strdup(params->override_driver);
2386 if (params->override_ctrl_interface)
2387 global->params.override_ctrl_interface =
2388 os_strdup(params->override_ctrl_interface);
2389 wpa_debug_level = global->params.wpa_debug_level =
2390 params->wpa_debug_level;
2391 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2392 params->wpa_debug_show_keys;
2393 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2394 params->wpa_debug_timestamp;
2395
2396 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2397
2398 if (eloop_init()) {
2399 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2400 wpa_supplicant_deinit(global);
2401 return NULL;
2402 }
2403
2404 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2405 if (global->ctrl_iface == NULL) {
2406 wpa_supplicant_deinit(global);
2407 return NULL;
2408 }
2409
2410 if (wpas_notify_supplicant_initialized(global)) {
2411 wpa_supplicant_deinit(global);
2412 return NULL;
2413 }
2414
2415 for (i = 0; wpa_drivers[i]; i++)
2416 global->drv_count++;
2417 if (global->drv_count == 0) {
2418 wpa_printf(MSG_ERROR, "No drivers enabled");
2419 wpa_supplicant_deinit(global);
2420 return NULL;
2421 }
2422 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2423 if (global->drv_priv == NULL) {
2424 wpa_supplicant_deinit(global);
2425 return NULL;
2426 }
2427 for (i = 0; wpa_drivers[i]; i++) {
2428 if (!wpa_drivers[i]->global_init)
2429 continue;
2430 global->drv_priv[i] = wpa_drivers[i]->global_init();
2431 if (global->drv_priv[i] == NULL) {
2432 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2433 "'%s'", wpa_drivers[i]->name);
2434 wpa_supplicant_deinit(global);
2435 return NULL;
2436 }
2437 }
2438
2439 return global;
2440 }
2441
2442
2443 /**
2444 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2445 * @global: Pointer to global data from wpa_supplicant_init()
2446 * Returns: 0 after successful event loop run, -1 on failure
2447 *
2448 * This function starts the main event loop and continues running as long as
2449 * there are any remaining events. In most cases, this function is running as
2450 * long as the %wpa_supplicant process in still in use.
2451 */
2452 int wpa_supplicant_run(struct wpa_global *global)
2453 {
2454 struct wpa_supplicant *wpa_s;
2455
2456 if (global->params.daemonize &&
2457 wpa_supplicant_daemon(global->params.pid_file))
2458 return -1;
2459
2460 if (global->params.wait_for_monitor) {
2461 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2462 if (wpa_s->ctrl_iface)
2463 wpa_supplicant_ctrl_iface_wait(
2464 wpa_s->ctrl_iface);
2465 }
2466
2467 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2468 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2469
2470 eloop_run();
2471
2472 return 0;
2473 }
2474
2475
2476 /**
2477 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2478 * @global: Pointer to global data from wpa_supplicant_init()
2479 *
2480 * This function is called to deinitialize %wpa_supplicant and to free all
2481 * allocated resources. Remaining network interfaces will also be removed.
2482 */
2483 void wpa_supplicant_deinit(struct wpa_global *global)
2484 {
2485 int i;
2486
2487 if (global == NULL)
2488 return;
2489
2490 #ifdef CONFIG_P2P
2491 wpas_p2p_deinit_global(global);
2492 #endif /* CONFIG_P2P */
2493
2494 while (global->ifaces)
2495 wpa_supplicant_remove_iface(global, global->ifaces);
2496
2497 if (global->ctrl_iface)
2498 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2499
2500 wpas_notify_supplicant_deinitialized(global);
2501
2502 eap_peer_unregister_methods();
2503 #ifdef CONFIG_AP
2504 eap_server_unregister_methods();
2505 #endif /* CONFIG_AP */
2506
2507 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2508 if (!global->drv_priv[i])
2509 continue;
2510 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2511 }
2512 os_free(global->drv_priv);
2513
2514 eloop_destroy();
2515
2516 if (global->params.pid_file) {
2517 os_daemonize_terminate(global->params.pid_file);
2518 os_free(global->params.pid_file);
2519 }
2520 os_free(global->params.ctrl_interface);
2521 os_free(global->params.override_driver);
2522 os_free(global->params.override_ctrl_interface);
2523
2524 os_free(global);
2525 wpa_debug_close_syslog();
2526 wpa_debug_close_file();
2527 }
2528
2529
2530 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2531 {
2532 #ifdef CONFIG_WPS
2533 wpas_wps_update_config(wpa_s);
2534 #endif /* CONFIG_WPS */
2535
2536 #ifdef CONFIG_P2P
2537 wpas_p2p_update_config(wpa_s);
2538 #endif /* CONFIG_P2P */
2539
2540 wpa_s->conf->changed_parameters = 0;
2541 }
2542
2543
2544 void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
2545 size_t num_hw_features)
2546 {
2547 size_t i;
2548
2549 if (hw_features == NULL)
2550 return;
2551
2552 for (i = 0; i < num_hw_features; i++) {
2553 os_free(hw_features[i].channels);
2554 os_free(hw_features[i].rates);
2555 }
2556
2557 os_free(hw_features);
2558 }
2559
2560
2561 static void add_freq(int *freqs, int *num_freqs, int freq)
2562 {
2563 int i;
2564
2565 for (i = 0; i < *num_freqs; i++) {
2566 if (freqs[i] == freq)
2567 return;
2568 }
2569
2570 freqs[*num_freqs] = freq;
2571 (*num_freqs)++;
2572 }
2573
2574
2575 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2576 {
2577 struct wpa_bss *bss, *cbss;
2578 const int max_freqs = 10;
2579 int *freqs;
2580 int num_freqs = 0;
2581
2582 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2583 if (freqs == NULL)
2584 return NULL;
2585
2586 cbss = wpa_s->current_bss;
2587
2588 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2589 if (bss == cbss)
2590 continue;
2591 if (bss->ssid_len == cbss->ssid_len &&
2592 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2593 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2594 add_freq(freqs, &num_freqs, bss->freq);
2595 if (num_freqs == max_freqs)
2596 break;
2597 }
2598 }
2599
2600 if (num_freqs == 0) {
2601 os_free(freqs);
2602 freqs = NULL;
2603 }
2604
2605 return freqs;
2606 }
2607
2608
2609 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2610 {
2611 int timeout;
2612 int count;
2613 int *freqs = NULL;
2614
2615 /*
2616 * Add the failed BSSID into the blacklist and speed up next scan
2617 * attempt if there could be other APs that could accept association.
2618 * The current blacklist count indicates how many times we have tried
2619 * connecting to this AP and multiple attempts mean that other APs are
2620 * either not available or has already been tried, so that we can start
2621 * increasing the delay here to avoid constant scanning.
2622 */
2623 count = wpa_blacklist_add(wpa_s, bssid);
2624 if (count == 1 && wpa_s->current_bss) {
2625 /*
2626 * This BSS was not in the blacklist before. If there is
2627 * another BSS available for the same ESS, we should try that
2628 * next. Otherwise, we may as well try this one once more
2629 * before allowing other, likely worse, ESSes to be considered.
2630 */
2631 freqs = get_bss_freqs_in_ess(wpa_s);
2632 if (freqs) {
2633 wpa_printf(MSG_DEBUG, "Another BSS in this ESS has "
2634 "been seen; try it next");
2635 wpa_blacklist_add(wpa_s, bssid);
2636 /*
2637 * On the next scan, go through only the known channels
2638 * used in this ESS based on previous scans to speed up
2639 * common load balancing use case.
2640 */
2641 os_free(wpa_s->next_scan_freqs);
2642 wpa_s->next_scan_freqs = freqs;
2643 }
2644 }
2645
2646 switch (count) {
2647 case 1:
2648 timeout = 100;
2649 break;
2650 case 2:
2651 timeout = 500;
2652 break;
2653 case 3:
2654 timeout = 1000;
2655 break;
2656 default:
2657 timeout = 5000;
2658 }
2659
2660 /*
2661 * TODO: if more than one possible AP is available in scan results,
2662 * could try the other ones before requesting a new scan.
2663 */
2664 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2665 1000 * (timeout % 1000));
2666 }