]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wpa_supplicant.c
Add handling of OBSS scan requests and 20/40 BSS coex reports
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
1 /*
2 * WPA Supplicant
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 *
8 * This file implements functions for registering and unregistering
9 * %wpa_supplicant interfaces. In addition, this file contains number of
10 * functions for managing network connections.
11 */
12
13 #include "includes.h"
14
15 #include "common.h"
16 #include "crypto/random.h"
17 #include "crypto/sha1.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "eap_peer/eap.h"
20 #include "eap_server/eap_methods.h"
21 #include "rsn_supp/wpa.h"
22 #include "eloop.h"
23 #include "config.h"
24 #include "l2_packet/l2_packet.h"
25 #include "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "ctrl_iface.h"
28 #include "pcsc_funcs.h"
29 #include "common/version.h"
30 #include "rsn_supp/preauth.h"
31 #include "rsn_supp/pmksa_cache.h"
32 #include "common/wpa_ctrl.h"
33 #include "common/ieee802_11_defs.h"
34 #include "p2p/p2p.h"
35 #include "blacklist.h"
36 #include "wpas_glue.h"
37 #include "wps_supplicant.h"
38 #include "ibss_rsn.h"
39 #include "sme.h"
40 #include "gas_query.h"
41 #include "ap.h"
42 #include "p2p_supplicant.h"
43 #include "notify.h"
44 #include "bgscan.h"
45 #include "bss.h"
46 #include "scan.h"
47 #include "offchannel.h"
48
49 const char *wpa_supplicant_version =
50 "wpa_supplicant v" VERSION_STR "\n"
51 "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
52
53 const char *wpa_supplicant_license =
54 "This software may be distributed under the terms of the BSD license.\n"
55 "See README for more details.\n"
56 #ifdef EAP_TLS_OPENSSL
57 "\nThis product includes software developed by the OpenSSL Project\n"
58 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
59 #endif /* EAP_TLS_OPENSSL */
60 ;
61
62 #ifndef CONFIG_NO_STDOUT_DEBUG
63 /* Long text divided into parts in order to fit in C89 strings size limits. */
64 const char *wpa_supplicant_full_license1 =
65 "";
66 const char *wpa_supplicant_full_license2 =
67 "This software may be distributed under the terms of the BSD license.\n"
68 "\n"
69 "Redistribution and use in source and binary forms, with or without\n"
70 "modification, are permitted provided that the following conditions are\n"
71 "met:\n"
72 "\n";
73 const char *wpa_supplicant_full_license3 =
74 "1. Redistributions of source code must retain the above copyright\n"
75 " notice, this list of conditions and the following disclaimer.\n"
76 "\n"
77 "2. Redistributions in binary form must reproduce the above copyright\n"
78 " notice, this list of conditions and the following disclaimer in the\n"
79 " documentation and/or other materials provided with the distribution.\n"
80 "\n";
81 const char *wpa_supplicant_full_license4 =
82 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
83 " names of its contributors may be used to endorse or promote products\n"
84 " derived from this software without specific prior written permission.\n"
85 "\n"
86 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
87 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
88 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
89 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
90 const char *wpa_supplicant_full_license5 =
91 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
92 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
93 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
94 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
95 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
96 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
97 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
98 "\n";
99 #endif /* CONFIG_NO_STDOUT_DEBUG */
100
101 extern int wpa_debug_level;
102 extern int wpa_debug_show_keys;
103 extern int wpa_debug_timestamp;
104 extern struct wpa_driver_ops *wpa_drivers[];
105
106 /* Configure default/group WEP keys for static WEP */
107 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
108 {
109 int i, set = 0;
110
111 for (i = 0; i < NUM_WEP_KEYS; i++) {
112 if (ssid->wep_key_len[i] == 0)
113 continue;
114
115 set = 1;
116 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
117 i, i == ssid->wep_tx_keyidx, NULL, 0,
118 ssid->wep_key[i], ssid->wep_key_len[i]);
119 }
120
121 return set;
122 }
123
124
125 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
126 struct wpa_ssid *ssid)
127 {
128 u8 key[32];
129 size_t keylen;
130 enum wpa_alg alg;
131 u8 seq[6] = { 0 };
132
133 /* IBSS/WPA-None uses only one key (Group) for both receiving and
134 * sending unicast and multicast packets. */
135
136 if (ssid->mode != WPAS_MODE_IBSS) {
137 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
138 "IBSS/ad-hoc) for WPA-None", ssid->mode);
139 return -1;
140 }
141
142 if (!ssid->psk_set) {
143 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
144 "WPA-None");
145 return -1;
146 }
147
148 switch (wpa_s->group_cipher) {
149 case WPA_CIPHER_CCMP:
150 os_memcpy(key, ssid->psk, 16);
151 keylen = 16;
152 alg = WPA_ALG_CCMP;
153 break;
154 case WPA_CIPHER_TKIP:
155 /* WPA-None uses the same Michael MIC key for both TX and RX */
156 os_memcpy(key, ssid->psk, 16 + 8);
157 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
158 keylen = 32;
159 alg = WPA_ALG_TKIP;
160 break;
161 default:
162 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
163 "WPA-None", wpa_s->group_cipher);
164 return -1;
165 }
166
167 /* TODO: should actually remember the previously used seq#, both for TX
168 * and RX from each STA.. */
169
170 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
171 }
172
173
174 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176 struct wpa_supplicant *wpa_s = eloop_ctx;
177 const u8 *bssid = wpa_s->bssid;
178 if (is_zero_ether_addr(bssid))
179 bssid = wpa_s->pending_bssid;
180 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
181 MAC2STR(bssid));
182 wpa_blacklist_add(wpa_s, bssid);
183 wpa_sm_notify_disassoc(wpa_s->wpa);
184 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
185 wpa_s->reassociate = 1;
186
187 /*
188 * If we timed out, the AP or the local radio may be busy.
189 * So, wait a second until scanning again.
190 */
191 wpa_supplicant_req_scan(wpa_s, 1, 0);
192 }
193
194
195 /**
196 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
197 * @wpa_s: Pointer to wpa_supplicant data
198 * @sec: Number of seconds after which to time out authentication
199 * @usec: Number of microseconds after which to time out authentication
200 *
201 * This function is used to schedule a timeout for the current authentication
202 * attempt.
203 */
204 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
205 int sec, int usec)
206 {
207 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
208 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
209 return;
210
211 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
212 "%d usec", sec, usec);
213 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
214 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
215 }
216
217
218 /**
219 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
220 * @wpa_s: Pointer to wpa_supplicant data
221 *
222 * This function is used to cancel authentication timeout scheduled with
223 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
224 * been completed.
225 */
226 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
227 {
228 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
229 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
230 wpa_blacklist_del(wpa_s, wpa_s->bssid);
231 }
232
233
234 /**
235 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
236 * @wpa_s: Pointer to wpa_supplicant data
237 *
238 * This function is used to configure EAPOL state machine based on the selected
239 * authentication mode.
240 */
241 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
242 {
243 #ifdef IEEE8021X_EAPOL
244 struct eapol_config eapol_conf;
245 struct wpa_ssid *ssid = wpa_s->current_ssid;
246
247 #ifdef CONFIG_IBSS_RSN
248 if (ssid->mode == WPAS_MODE_IBSS &&
249 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
250 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
251 /*
252 * RSN IBSS authentication is per-STA and we can disable the
253 * per-BSSID EAPOL authentication.
254 */
255 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
256 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
257 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
258 return;
259 }
260 #endif /* CONFIG_IBSS_RSN */
261
262 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
263 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
264
265 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
266 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
267 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
268 else
269 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
270
271 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
272 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
273 eapol_conf.accept_802_1x_keys = 1;
274 eapol_conf.required_keys = 0;
275 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
276 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
277 }
278 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
279 eapol_conf.required_keys |=
280 EAPOL_REQUIRE_KEY_BROADCAST;
281 }
282
283 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
284 eapol_conf.required_keys = 0;
285 }
286 if (wpa_s->conf)
287 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
288 eapol_conf.workaround = ssid->eap_workaround;
289 eapol_conf.eap_disabled =
290 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
291 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
292 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
293 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
294 #endif /* IEEE8021X_EAPOL */
295 }
296
297
298 /**
299 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
300 * @wpa_s: Pointer to wpa_supplicant data
301 * @ssid: Configuration data for the network
302 *
303 * This function is used to configure WPA state machine and related parameters
304 * to a mode where WPA is not enabled. This is called as part of the
305 * authentication configuration when the selected network does not use WPA.
306 */
307 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
308 struct wpa_ssid *ssid)
309 {
310 int i;
311
312 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
313 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
314 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
315 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
316 else
317 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
318 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
319 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
320 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
321 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
322 wpa_s->group_cipher = WPA_CIPHER_NONE;
323 wpa_s->mgmt_group_cipher = 0;
324
325 for (i = 0; i < NUM_WEP_KEYS; i++) {
326 if (ssid->wep_key_len[i] > 5) {
327 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
328 wpa_s->group_cipher = WPA_CIPHER_WEP104;
329 break;
330 } else if (ssid->wep_key_len[i] > 0) {
331 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
332 wpa_s->group_cipher = WPA_CIPHER_WEP40;
333 break;
334 }
335 }
336
337 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
338 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
339 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
340 wpa_s->pairwise_cipher);
341 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
342 #ifdef CONFIG_IEEE80211W
343 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
344 wpa_s->mgmt_group_cipher);
345 #endif /* CONFIG_IEEE80211W */
346
347 pmksa_cache_clear_current(wpa_s->wpa);
348 }
349
350
351 void free_hw_features(struct wpa_supplicant *wpa_s)
352 {
353 int i;
354 if (wpa_s->hw.modes == NULL)
355 return;
356
357 for (i = 0; i < wpa_s->hw.num_modes; i++) {
358 os_free(wpa_s->hw.modes[i].channels);
359 os_free(wpa_s->hw.modes[i].rates);
360 }
361
362 os_free(wpa_s->hw.modes);
363 wpa_s->hw.modes = NULL;
364 }
365
366
367 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
368 {
369 bgscan_deinit(wpa_s);
370 scard_deinit(wpa_s->scard);
371 wpa_s->scard = NULL;
372 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
373 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
374 l2_packet_deinit(wpa_s->l2);
375 wpa_s->l2 = NULL;
376 if (wpa_s->l2_br) {
377 l2_packet_deinit(wpa_s->l2_br);
378 wpa_s->l2_br = NULL;
379 }
380
381 if (wpa_s->conf != NULL) {
382 struct wpa_ssid *ssid;
383 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
384 wpas_notify_network_removed(wpa_s, ssid);
385 }
386
387 os_free(wpa_s->confname);
388 wpa_s->confname = NULL;
389
390 wpa_sm_set_eapol(wpa_s->wpa, NULL);
391 eapol_sm_deinit(wpa_s->eapol);
392 wpa_s->eapol = NULL;
393
394 rsn_preauth_deinit(wpa_s->wpa);
395
396 #ifdef CONFIG_TDLS
397 wpa_tdls_deinit(wpa_s->wpa);
398 #endif /* CONFIG_TDLS */
399
400 pmksa_candidate_free(wpa_s->wpa);
401 wpa_sm_deinit(wpa_s->wpa);
402 wpa_s->wpa = NULL;
403 wpa_blacklist_clear(wpa_s);
404
405 wpa_bss_deinit(wpa_s);
406
407 wpa_supplicant_cancel_scan(wpa_s);
408 wpa_supplicant_cancel_auth_timeout(wpa_s);
409 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
410 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
411 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
412 wpa_s, NULL);
413 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
414
415 wpas_wps_deinit(wpa_s);
416
417 wpabuf_free(wpa_s->pending_eapol_rx);
418 wpa_s->pending_eapol_rx = NULL;
419
420 #ifdef CONFIG_IBSS_RSN
421 ibss_rsn_deinit(wpa_s->ibss_rsn);
422 wpa_s->ibss_rsn = NULL;
423 #endif /* CONFIG_IBSS_RSN */
424
425 sme_deinit(wpa_s);
426
427 #ifdef CONFIG_AP
428 wpa_supplicant_ap_deinit(wpa_s);
429 #endif /* CONFIG_AP */
430
431 #ifdef CONFIG_P2P
432 wpas_p2p_deinit(wpa_s);
433 #endif /* CONFIG_P2P */
434
435 #ifdef CONFIG_OFFCHANNEL
436 offchannel_deinit(wpa_s);
437 #endif /* CONFIG_OFFCHANNEL */
438
439 wpa_supplicant_cancel_sched_scan(wpa_s);
440
441 os_free(wpa_s->next_scan_freqs);
442 wpa_s->next_scan_freqs = NULL;
443
444 gas_query_deinit(wpa_s->gas);
445 wpa_s->gas = NULL;
446
447 free_hw_features(wpa_s);
448
449 os_free(wpa_s->bssid_filter);
450 wpa_s->bssid_filter = NULL;
451 }
452
453
454 /**
455 * wpa_clear_keys - Clear keys configured for the driver
456 * @wpa_s: Pointer to wpa_supplicant data
457 * @addr: Previously used BSSID or %NULL if not available
458 *
459 * This function clears the encryption keys that has been previously configured
460 * for the driver.
461 */
462 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
463 {
464 if (wpa_s->keys_cleared) {
465 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
466 * timing issues with keys being cleared just before new keys
467 * are set or just after association or something similar. This
468 * shows up in group key handshake failing often because of the
469 * client not receiving the first encrypted packets correctly.
470 * Skipping some of the extra key clearing steps seems to help
471 * in completing group key handshake more reliably. */
472 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
473 "skip key clearing");
474 return;
475 }
476
477 /* MLME-DELETEKEYS.request */
478 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
479 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
480 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
481 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
482 #ifdef CONFIG_IEEE80211W
483 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
484 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
485 #endif /* CONFIG_IEEE80211W */
486 if (addr) {
487 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
488 0);
489 /* MLME-SETPROTECTION.request(None) */
490 wpa_drv_mlme_setprotection(
491 wpa_s, addr,
492 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
493 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
494 }
495 wpa_s->keys_cleared = 1;
496 }
497
498
499 /**
500 * wpa_supplicant_state_txt - Get the connection state name as a text string
501 * @state: State (wpa_state; WPA_*)
502 * Returns: The state name as a printable text string
503 */
504 const char * wpa_supplicant_state_txt(enum wpa_states state)
505 {
506 switch (state) {
507 case WPA_DISCONNECTED:
508 return "DISCONNECTED";
509 case WPA_INACTIVE:
510 return "INACTIVE";
511 case WPA_INTERFACE_DISABLED:
512 return "INTERFACE_DISABLED";
513 case WPA_SCANNING:
514 return "SCANNING";
515 case WPA_AUTHENTICATING:
516 return "AUTHENTICATING";
517 case WPA_ASSOCIATING:
518 return "ASSOCIATING";
519 case WPA_ASSOCIATED:
520 return "ASSOCIATED";
521 case WPA_4WAY_HANDSHAKE:
522 return "4WAY_HANDSHAKE";
523 case WPA_GROUP_HANDSHAKE:
524 return "GROUP_HANDSHAKE";
525 case WPA_COMPLETED:
526 return "COMPLETED";
527 default:
528 return "UNKNOWN";
529 }
530 }
531
532
533 #ifdef CONFIG_BGSCAN
534
535 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
536 {
537 if (wpas_driver_bss_selection(wpa_s))
538 return;
539 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
540 return;
541
542 bgscan_deinit(wpa_s);
543 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
544 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
545 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
546 "bgscan");
547 /*
548 * Live without bgscan; it is only used as a roaming
549 * optimization, so the initial connection is not
550 * affected.
551 */
552 } else
553 wpa_s->bgscan_ssid = wpa_s->current_ssid;
554 } else
555 wpa_s->bgscan_ssid = NULL;
556 }
557
558
559 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
560 {
561 if (wpa_s->bgscan_ssid != NULL) {
562 bgscan_deinit(wpa_s);
563 wpa_s->bgscan_ssid = NULL;
564 }
565 }
566
567 #endif /* CONFIG_BGSCAN */
568
569
570 /**
571 * wpa_supplicant_set_state - Set current connection state
572 * @wpa_s: Pointer to wpa_supplicant data
573 * @state: The new connection state
574 *
575 * This function is called whenever the connection state changes, e.g.,
576 * association is completed for WPA/WPA2 4-Way Handshake is started.
577 */
578 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
579 enum wpa_states state)
580 {
581 enum wpa_states old_state = wpa_s->wpa_state;
582
583 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
584 wpa_supplicant_state_txt(wpa_s->wpa_state),
585 wpa_supplicant_state_txt(state));
586
587 if (state != WPA_SCANNING)
588 wpa_supplicant_notify_scanning(wpa_s, 0);
589
590 if (state == WPA_COMPLETED && wpa_s->new_connection) {
591 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
592 struct wpa_ssid *ssid = wpa_s->current_ssid;
593 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
594 MACSTR " completed %s [id=%d id_str=%s]",
595 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
596 "(reauth)" : "(auth)",
597 ssid ? ssid->id : -1,
598 ssid && ssid->id_str ? ssid->id_str : "");
599 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
600 wpa_s->new_connection = 0;
601 wpa_s->reassociated_connection = 1;
602 wpa_drv_set_operstate(wpa_s, 1);
603 #ifndef IEEE8021X_EAPOL
604 wpa_drv_set_supp_port(wpa_s, 1);
605 #endif /* IEEE8021X_EAPOL */
606 wpa_s->after_wps = 0;
607 #ifdef CONFIG_P2P
608 wpas_p2p_completed(wpa_s);
609 #endif /* CONFIG_P2P */
610
611 sme_sched_obss_scan(wpa_s, 1);
612 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
613 state == WPA_ASSOCIATED) {
614 wpa_s->new_connection = 1;
615 wpa_drv_set_operstate(wpa_s, 0);
616 #ifndef IEEE8021X_EAPOL
617 wpa_drv_set_supp_port(wpa_s, 0);
618 #endif /* IEEE8021X_EAPOL */
619 sme_sched_obss_scan(wpa_s, 0);
620 }
621 wpa_s->wpa_state = state;
622
623 #ifdef CONFIG_BGSCAN
624 if (state == WPA_COMPLETED)
625 wpa_supplicant_start_bgscan(wpa_s);
626 else
627 wpa_supplicant_stop_bgscan(wpa_s);
628 #endif /* CONFIG_BGSCAN */
629
630 if (wpa_s->wpa_state != old_state) {
631 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
632
633 if (wpa_s->wpa_state == WPA_COMPLETED ||
634 old_state == WPA_COMPLETED)
635 wpas_notify_auth_changed(wpa_s);
636 }
637 }
638
639
640 void wpa_supplicant_terminate_proc(struct wpa_global *global)
641 {
642 int pending = 0;
643 #ifdef CONFIG_WPS
644 struct wpa_supplicant *wpa_s = global->ifaces;
645 while (wpa_s) {
646 if (wpas_wps_terminate_pending(wpa_s) == 1)
647 pending = 1;
648 wpa_s = wpa_s->next;
649 }
650 #endif /* CONFIG_WPS */
651 if (pending)
652 return;
653 eloop_terminate();
654 }
655
656
657 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
658 {
659 struct wpa_global *global = signal_ctx;
660 wpa_supplicant_terminate_proc(global);
661 }
662
663
664 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
665 {
666 enum wpa_states old_state = wpa_s->wpa_state;
667
668 wpa_s->pairwise_cipher = 0;
669 wpa_s->group_cipher = 0;
670 wpa_s->mgmt_group_cipher = 0;
671 wpa_s->key_mgmt = 0;
672 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
673 wpa_s->wpa_state = WPA_DISCONNECTED;
674
675 if (wpa_s->wpa_state != old_state)
676 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
677 }
678
679
680 /**
681 * wpa_supplicant_reload_configuration - Reload configuration data
682 * @wpa_s: Pointer to wpa_supplicant data
683 * Returns: 0 on success or -1 if configuration parsing failed
684 *
685 * This function can be used to request that the configuration data is reloaded
686 * (e.g., after configuration file change). This function is reloading
687 * configuration only for one interface, so this may need to be called multiple
688 * times if %wpa_supplicant is controlling multiple interfaces and all
689 * interfaces need reconfiguration.
690 */
691 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
692 {
693 struct wpa_config *conf;
694 int reconf_ctrl;
695 int old_ap_scan;
696
697 if (wpa_s->confname == NULL)
698 return -1;
699 conf = wpa_config_read(wpa_s->confname);
700 if (conf == NULL) {
701 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
702 "file '%s' - exiting", wpa_s->confname);
703 return -1;
704 }
705 conf->changed_parameters = (unsigned int) -1;
706
707 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
708 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
709 os_strcmp(conf->ctrl_interface,
710 wpa_s->conf->ctrl_interface) != 0);
711
712 if (reconf_ctrl && wpa_s->ctrl_iface) {
713 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
714 wpa_s->ctrl_iface = NULL;
715 }
716
717 eapol_sm_invalidate_cached_session(wpa_s->eapol);
718 if (wpa_s->current_ssid) {
719 wpa_supplicant_deauthenticate(wpa_s,
720 WLAN_REASON_DEAUTH_LEAVING);
721 }
722
723 /*
724 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
725 * pkcs11_engine_path, pkcs11_module_path.
726 */
727 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
728 /*
729 * Clear forced success to clear EAP state for next
730 * authentication.
731 */
732 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
733 }
734 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
735 wpa_sm_set_config(wpa_s->wpa, NULL);
736 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
737 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
738 rsn_preauth_deinit(wpa_s->wpa);
739
740 old_ap_scan = wpa_s->conf->ap_scan;
741 wpa_config_free(wpa_s->conf);
742 wpa_s->conf = conf;
743 if (old_ap_scan != wpa_s->conf->ap_scan)
744 wpas_notify_ap_scan_changed(wpa_s);
745
746 if (reconf_ctrl)
747 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
748
749 wpa_supplicant_update_config(wpa_s);
750
751 wpa_supplicant_clear_status(wpa_s);
752 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
753 wpa_s->reassociate = 1;
754 wpa_supplicant_req_scan(wpa_s, 0, 0);
755 }
756 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
757 return 0;
758 }
759
760
761 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
762 {
763 struct wpa_global *global = signal_ctx;
764 struct wpa_supplicant *wpa_s;
765 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
766 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
767 sig);
768 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
769 wpa_supplicant_terminate_proc(global);
770 }
771 }
772 }
773
774
775 enum wpa_cipher cipher_suite2driver(int cipher)
776 {
777 switch (cipher) {
778 case WPA_CIPHER_NONE:
779 return CIPHER_NONE;
780 case WPA_CIPHER_WEP40:
781 return CIPHER_WEP40;
782 case WPA_CIPHER_WEP104:
783 return CIPHER_WEP104;
784 case WPA_CIPHER_CCMP:
785 return CIPHER_CCMP;
786 case WPA_CIPHER_TKIP:
787 default:
788 return CIPHER_TKIP;
789 }
790 }
791
792
793 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
794 {
795 switch (key_mgmt) {
796 case WPA_KEY_MGMT_NONE:
797 return KEY_MGMT_NONE;
798 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
799 return KEY_MGMT_802_1X_NO_WPA;
800 case WPA_KEY_MGMT_IEEE8021X:
801 return KEY_MGMT_802_1X;
802 case WPA_KEY_MGMT_WPA_NONE:
803 return KEY_MGMT_WPA_NONE;
804 case WPA_KEY_MGMT_FT_IEEE8021X:
805 return KEY_MGMT_FT_802_1X;
806 case WPA_KEY_MGMT_FT_PSK:
807 return KEY_MGMT_FT_PSK;
808 case WPA_KEY_MGMT_IEEE8021X_SHA256:
809 return KEY_MGMT_802_1X_SHA256;
810 case WPA_KEY_MGMT_PSK_SHA256:
811 return KEY_MGMT_PSK_SHA256;
812 case WPA_KEY_MGMT_WPS:
813 return KEY_MGMT_WPS;
814 case WPA_KEY_MGMT_PSK:
815 default:
816 return KEY_MGMT_PSK;
817 }
818 }
819
820
821 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
822 struct wpa_ssid *ssid,
823 struct wpa_ie_data *ie)
824 {
825 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
826 if (ret) {
827 if (ret == -2) {
828 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
829 "from association info");
830 }
831 return -1;
832 }
833
834 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
835 "cipher suites");
836 if (!(ie->group_cipher & ssid->group_cipher)) {
837 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
838 "cipher 0x%x (mask 0x%x) - reject",
839 ie->group_cipher, ssid->group_cipher);
840 return -1;
841 }
842 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
843 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
844 "cipher 0x%x (mask 0x%x) - reject",
845 ie->pairwise_cipher, ssid->pairwise_cipher);
846 return -1;
847 }
848 if (!(ie->key_mgmt & ssid->key_mgmt)) {
849 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
850 "management 0x%x (mask 0x%x) - reject",
851 ie->key_mgmt, ssid->key_mgmt);
852 return -1;
853 }
854
855 #ifdef CONFIG_IEEE80211W
856 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
857 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
858 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
859 "that does not support management frame protection - "
860 "reject");
861 return -1;
862 }
863 #endif /* CONFIG_IEEE80211W */
864
865 return 0;
866 }
867
868
869 /**
870 * wpa_supplicant_set_suites - Set authentication and encryption parameters
871 * @wpa_s: Pointer to wpa_supplicant data
872 * @bss: Scan results for the selected BSS, or %NULL if not available
873 * @ssid: Configuration data for the selected network
874 * @wpa_ie: Buffer for the WPA/RSN IE
875 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
876 * used buffer length in case the functions returns success.
877 * Returns: 0 on success or -1 on failure
878 *
879 * This function is used to configure authentication and encryption parameters
880 * based on the network configuration and scan result for the selected BSS (if
881 * available).
882 */
883 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
884 struct wpa_bss *bss, struct wpa_ssid *ssid,
885 u8 *wpa_ie, size_t *wpa_ie_len)
886 {
887 struct wpa_ie_data ie;
888 int sel, proto;
889 const u8 *bss_wpa, *bss_rsn;
890
891 if (bss) {
892 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
893 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
894 } else
895 bss_wpa = bss_rsn = NULL;
896
897 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
898 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
899 (ie.group_cipher & ssid->group_cipher) &&
900 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
901 (ie.key_mgmt & ssid->key_mgmt)) {
902 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
903 proto = WPA_PROTO_RSN;
904 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
905 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
906 (ie.group_cipher & ssid->group_cipher) &&
907 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
908 (ie.key_mgmt & ssid->key_mgmt)) {
909 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
910 proto = WPA_PROTO_WPA;
911 } else if (bss) {
912 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
913 return -1;
914 } else {
915 if (ssid->proto & WPA_PROTO_RSN)
916 proto = WPA_PROTO_RSN;
917 else
918 proto = WPA_PROTO_WPA;
919 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
920 os_memset(&ie, 0, sizeof(ie));
921 ie.group_cipher = ssid->group_cipher;
922 ie.pairwise_cipher = ssid->pairwise_cipher;
923 ie.key_mgmt = ssid->key_mgmt;
924 #ifdef CONFIG_IEEE80211W
925 ie.mgmt_group_cipher =
926 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
927 WPA_CIPHER_AES_128_CMAC : 0;
928 #endif /* CONFIG_IEEE80211W */
929 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
930 "based on configuration");
931 } else
932 proto = ie.proto;
933 }
934
935 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
936 "pairwise %d key_mgmt %d proto %d",
937 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
938 #ifdef CONFIG_IEEE80211W
939 if (ssid->ieee80211w) {
940 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
941 ie.mgmt_group_cipher);
942 }
943 #endif /* CONFIG_IEEE80211W */
944
945 wpa_s->wpa_proto = proto;
946 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
947 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
948 !!(ssid->proto & WPA_PROTO_RSN));
949
950 if (bss || !wpa_s->ap_ies_from_associnfo) {
951 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
952 bss_wpa ? 2 + bss_wpa[1] : 0) ||
953 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
954 bss_rsn ? 2 + bss_rsn[1] : 0))
955 return -1;
956 }
957
958 sel = ie.group_cipher & ssid->group_cipher;
959 if (sel & WPA_CIPHER_CCMP) {
960 wpa_s->group_cipher = WPA_CIPHER_CCMP;
961 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
962 } else if (sel & WPA_CIPHER_TKIP) {
963 wpa_s->group_cipher = WPA_CIPHER_TKIP;
964 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
965 } else if (sel & WPA_CIPHER_WEP104) {
966 wpa_s->group_cipher = WPA_CIPHER_WEP104;
967 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
968 } else if (sel & WPA_CIPHER_WEP40) {
969 wpa_s->group_cipher = WPA_CIPHER_WEP40;
970 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
971 } else {
972 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
973 "cipher");
974 return -1;
975 }
976
977 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
978 if (sel & WPA_CIPHER_CCMP) {
979 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
980 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
981 } else if (sel & WPA_CIPHER_TKIP) {
982 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
983 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
984 } else if (sel & WPA_CIPHER_NONE) {
985 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
986 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
987 } else {
988 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
989 "cipher");
990 return -1;
991 }
992
993 sel = ie.key_mgmt & ssid->key_mgmt;
994 if (0) {
995 #ifdef CONFIG_IEEE80211R
996 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
997 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
998 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
999 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1000 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1001 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1002 #endif /* CONFIG_IEEE80211R */
1003 #ifdef CONFIG_IEEE80211W
1004 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1005 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1006 wpa_dbg(wpa_s, MSG_DEBUG,
1007 "WPA: using KEY_MGMT 802.1X with SHA256");
1008 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1009 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1010 wpa_dbg(wpa_s, MSG_DEBUG,
1011 "WPA: using KEY_MGMT PSK with SHA256");
1012 #endif /* CONFIG_IEEE80211W */
1013 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1014 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1015 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1016 } else if (sel & WPA_KEY_MGMT_PSK) {
1017 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1018 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1019 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1020 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1021 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1022 } else {
1023 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1024 "authenticated key management type");
1025 return -1;
1026 }
1027
1028 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1029 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1030 wpa_s->pairwise_cipher);
1031 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1032
1033 #ifdef CONFIG_IEEE80211W
1034 sel = ie.mgmt_group_cipher;
1035 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1036 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1037 sel = 0;
1038 if (sel & WPA_CIPHER_AES_128_CMAC) {
1039 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1040 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1041 "AES-128-CMAC");
1042 } else {
1043 wpa_s->mgmt_group_cipher = 0;
1044 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1045 }
1046 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1047 wpa_s->mgmt_group_cipher);
1048 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1049 #endif /* CONFIG_IEEE80211W */
1050
1051 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1052 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1053 return -1;
1054 }
1055
1056 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
1057 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1058 #ifndef CONFIG_NO_PBKDF2
1059 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1060 ssid->passphrase) {
1061 u8 psk[PMK_LEN];
1062 pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1063 bss->ssid_len, 4096, psk, PMK_LEN);
1064 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1065 psk, PMK_LEN);
1066 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1067 }
1068 #endif /* CONFIG_NO_PBKDF2 */
1069 } else
1070 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1071
1072 return 0;
1073 }
1074
1075
1076 /**
1077 * wpa_supplicant_associate - Request association
1078 * @wpa_s: Pointer to wpa_supplicant data
1079 * @bss: Scan results for the selected BSS, or %NULL if not available
1080 * @ssid: Configuration data for the selected network
1081 *
1082 * This function is used to request %wpa_supplicant to associate with a BSS.
1083 */
1084 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1085 struct wpa_bss *bss, struct wpa_ssid *ssid)
1086 {
1087 u8 wpa_ie[200];
1088 size_t wpa_ie_len;
1089 int use_crypt, ret, i, bssid_changed;
1090 int algs = WPA_AUTH_ALG_OPEN;
1091 enum wpa_cipher cipher_pairwise, cipher_group;
1092 struct wpa_driver_associate_params params;
1093 int wep_keys_set = 0;
1094 struct wpa_driver_capa capa;
1095 int assoc_failed = 0;
1096 struct wpa_ssid *old_ssid;
1097 #ifdef CONFIG_HT_OVERRIDES
1098 struct ieee80211_ht_capabilities htcaps;
1099 struct ieee80211_ht_capabilities htcaps_mask;
1100 #endif /* CONFIG_HT_OVERRIDES */
1101
1102 #ifdef CONFIG_IBSS_RSN
1103 ibss_rsn_deinit(wpa_s->ibss_rsn);
1104 wpa_s->ibss_rsn = NULL;
1105 #endif /* CONFIG_IBSS_RSN */
1106
1107 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1108 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1109 #ifdef CONFIG_AP
1110 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1111 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1112 "mode");
1113 return;
1114 }
1115 wpa_supplicant_create_ap(wpa_s, ssid);
1116 wpa_s->current_bss = bss;
1117 #else /* CONFIG_AP */
1118 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1119 "the build");
1120 #endif /* CONFIG_AP */
1121 return;
1122 }
1123
1124 #ifdef CONFIG_TDLS
1125 if (bss)
1126 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1127 bss->ie_len);
1128 #endif /* CONFIG_TDLS */
1129
1130 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1131 ssid->mode == IEEE80211_MODE_INFRA) {
1132 sme_authenticate(wpa_s, bss, ssid);
1133 return;
1134 }
1135
1136 os_memset(&params, 0, sizeof(params));
1137 wpa_s->reassociate = 0;
1138 if (bss && !wpas_driver_bss_selection(wpa_s)) {
1139 #ifdef CONFIG_IEEE80211R
1140 const u8 *ie, *md = NULL;
1141 #endif /* CONFIG_IEEE80211R */
1142 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1143 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1144 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1145 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1146 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1147 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1148 if (bssid_changed)
1149 wpas_notify_bssid_changed(wpa_s);
1150 #ifdef CONFIG_IEEE80211R
1151 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1152 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1153 md = ie + 2;
1154 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1155 if (md) {
1156 /* Prepare for the next transition */
1157 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1158 }
1159 #endif /* CONFIG_IEEE80211R */
1160 #ifdef CONFIG_WPS
1161 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1162 wpa_s->conf->ap_scan == 2 &&
1163 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1164 /* Use ap_scan==1 style network selection to find the network
1165 */
1166 wpa_s->scan_req = 2;
1167 wpa_s->reassociate = 1;
1168 wpa_supplicant_req_scan(wpa_s, 0, 0);
1169 return;
1170 #endif /* CONFIG_WPS */
1171 } else {
1172 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1173 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1174 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1175 }
1176 wpa_supplicant_cancel_sched_scan(wpa_s);
1177 wpa_supplicant_cancel_scan(wpa_s);
1178
1179 /* Starting new association, so clear the possibly used WPA IE from the
1180 * previous association. */
1181 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1182
1183 #ifdef IEEE8021X_EAPOL
1184 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1185 if (ssid->leap) {
1186 if (ssid->non_leap == 0)
1187 algs = WPA_AUTH_ALG_LEAP;
1188 else
1189 algs |= WPA_AUTH_ALG_LEAP;
1190 }
1191 }
1192 #endif /* IEEE8021X_EAPOL */
1193 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1194 if (ssid->auth_alg) {
1195 algs = ssid->auth_alg;
1196 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1197 "0x%x", algs);
1198 }
1199
1200 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1201 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1202 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
1203 int try_opportunistic;
1204 try_opportunistic = ssid->proactive_key_caching &&
1205 (ssid->proto & WPA_PROTO_RSN);
1206 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1207 wpa_s->current_ssid,
1208 try_opportunistic) == 0)
1209 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1210 wpa_ie_len = sizeof(wpa_ie);
1211 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1212 wpa_ie, &wpa_ie_len)) {
1213 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1214 "key management and encryption suites");
1215 return;
1216 }
1217 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
1218 wpa_ie_len = sizeof(wpa_ie);
1219 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1220 wpa_ie, &wpa_ie_len)) {
1221 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1222 "key management and encryption suites (no "
1223 "scan results)");
1224 return;
1225 }
1226 #ifdef CONFIG_WPS
1227 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1228 struct wpabuf *wps_ie;
1229 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1230 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1231 wpa_ie_len = wpabuf_len(wps_ie);
1232 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1233 } else
1234 wpa_ie_len = 0;
1235 wpabuf_free(wps_ie);
1236 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1237 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1238 params.wps = WPS_MODE_PRIVACY;
1239 else
1240 params.wps = WPS_MODE_OPEN;
1241 wpa_s->wpa_proto = 0;
1242 #endif /* CONFIG_WPS */
1243 } else {
1244 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1245 wpa_ie_len = 0;
1246 wpa_s->wpa_proto = 0;
1247 }
1248
1249 #ifdef CONFIG_P2P
1250 if (wpa_s->global->p2p) {
1251 u8 *pos;
1252 size_t len;
1253 int res;
1254 pos = wpa_ie + wpa_ie_len;
1255 len = sizeof(wpa_ie) - wpa_ie_len;
1256 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1257 ssid->p2p_group);
1258 if (res >= 0)
1259 wpa_ie_len += res;
1260 }
1261
1262 wpa_s->cross_connect_disallowed = 0;
1263 if (bss) {
1264 struct wpabuf *p2p;
1265 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1266 if (p2p) {
1267 wpa_s->cross_connect_disallowed =
1268 p2p_get_cross_connect_disallowed(p2p);
1269 wpabuf_free(p2p);
1270 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1271 "connection",
1272 wpa_s->cross_connect_disallowed ?
1273 "disallows" : "allows");
1274 }
1275 }
1276 #endif /* CONFIG_P2P */
1277
1278 #ifdef CONFIG_INTERWORKING
1279 if (wpa_s->conf->interworking) {
1280 u8 *pos = wpa_ie;
1281 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1282 pos += 2 + pos[1];
1283 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1284 wpa_ie_len += 6;
1285 *pos++ = WLAN_EID_EXT_CAPAB;
1286 *pos++ = 4;
1287 *pos++ = 0x00;
1288 *pos++ = 0x00;
1289 *pos++ = 0x00;
1290 *pos++ = 0x80; /* Bit 31 - Interworking */
1291 }
1292 #endif /* CONFIG_INTERWORKING */
1293
1294 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1295 use_crypt = 1;
1296 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1297 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1298 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1299 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1300 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1301 use_crypt = 0;
1302 if (wpa_set_wep_keys(wpa_s, ssid)) {
1303 use_crypt = 1;
1304 wep_keys_set = 1;
1305 }
1306 }
1307 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1308 use_crypt = 0;
1309
1310 #ifdef IEEE8021X_EAPOL
1311 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1312 if ((ssid->eapol_flags &
1313 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1314 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1315 !wep_keys_set) {
1316 use_crypt = 0;
1317 } else {
1318 /* Assume that dynamic WEP-104 keys will be used and
1319 * set cipher suites in order for drivers to expect
1320 * encryption. */
1321 cipher_pairwise = cipher_group = CIPHER_WEP104;
1322 }
1323 }
1324 #endif /* IEEE8021X_EAPOL */
1325
1326 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1327 /* Set the key before (and later after) association */
1328 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1329 }
1330
1331 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1332 if (bss) {
1333 params.ssid = bss->ssid;
1334 params.ssid_len = bss->ssid_len;
1335 if (!wpas_driver_bss_selection(wpa_s)) {
1336 params.bssid = bss->bssid;
1337 params.freq = bss->freq;
1338 }
1339 } else {
1340 params.ssid = ssid->ssid;
1341 params.ssid_len = ssid->ssid_len;
1342 }
1343
1344 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1345 wpa_s->conf->ap_scan == 2) {
1346 params.bssid = ssid->bssid;
1347 params.fixed_bssid = 1;
1348 }
1349
1350 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1351 params.freq == 0)
1352 params.freq = ssid->frequency; /* Initial channel for IBSS */
1353 params.wpa_ie = wpa_ie;
1354 params.wpa_ie_len = wpa_ie_len;
1355 params.pairwise_suite = cipher_pairwise;
1356 params.group_suite = cipher_group;
1357 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1358 params.wpa_proto = wpa_s->wpa_proto;
1359 params.auth_alg = algs;
1360 params.mode = ssid->mode;
1361 params.bg_scan_period = ssid->bg_scan_period;
1362 for (i = 0; i < NUM_WEP_KEYS; i++) {
1363 if (ssid->wep_key_len[i])
1364 params.wep_key[i] = ssid->wep_key[i];
1365 params.wep_key_len[i] = ssid->wep_key_len[i];
1366 }
1367 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1368
1369 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1370 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1371 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1372 params.passphrase = ssid->passphrase;
1373 if (ssid->psk_set)
1374 params.psk = ssid->psk;
1375 }
1376
1377 params.drop_unencrypted = use_crypt;
1378
1379 #ifdef CONFIG_IEEE80211W
1380 params.mgmt_frame_protection = ssid->ieee80211w;
1381 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1382 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1383 struct wpa_ie_data ie;
1384 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1385 ie.capabilities &
1386 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1387 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1388 "MFP: require MFP");
1389 params.mgmt_frame_protection =
1390 MGMT_FRAME_PROTECTION_REQUIRED;
1391 }
1392 }
1393 #endif /* CONFIG_IEEE80211W */
1394
1395 params.p2p = ssid->p2p_group;
1396
1397 if (wpa_s->parent->set_sta_uapsd)
1398 params.uapsd = wpa_s->parent->sta_uapsd;
1399 else
1400 params.uapsd = -1;
1401
1402 #ifdef CONFIG_HT_OVERRIDES
1403 os_memset(&htcaps, 0, sizeof(htcaps));
1404 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1405 params.htcaps = (u8 *) &htcaps;
1406 params.htcaps_mask = (u8 *) &htcaps_mask;
1407 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1408 #endif /* CONFIG_HT_OVERRIDES */
1409
1410 ret = wpa_drv_associate(wpa_s, &params);
1411 if (ret < 0) {
1412 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1413 "failed");
1414 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1415 /*
1416 * The driver is known to mean what is saying, so we
1417 * can stop right here; the association will not
1418 * succeed.
1419 */
1420 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1421 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1422 return;
1423 }
1424 /* try to continue anyway; new association will be tried again
1425 * after timeout */
1426 assoc_failed = 1;
1427 }
1428
1429 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1430 /* Set the key after the association just in case association
1431 * cleared the previously configured key. */
1432 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1433 /* No need to timeout authentication since there is no key
1434 * management. */
1435 wpa_supplicant_cancel_auth_timeout(wpa_s);
1436 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1437 #ifdef CONFIG_IBSS_RSN
1438 } else if (ssid->mode == WPAS_MODE_IBSS &&
1439 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1440 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1441 /*
1442 * RSN IBSS authentication is per-STA and we can disable the
1443 * per-BSSID authentication.
1444 */
1445 wpa_supplicant_cancel_auth_timeout(wpa_s);
1446 #endif /* CONFIG_IBSS_RSN */
1447 } else {
1448 /* Timeout for IEEE 802.11 authentication and association */
1449 int timeout = 60;
1450
1451 if (assoc_failed) {
1452 /* give IBSS a bit more time */
1453 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1454 } else if (wpa_s->conf->ap_scan == 1) {
1455 /* give IBSS a bit more time */
1456 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1457 }
1458 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1459 }
1460
1461 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1462 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1463 /* Set static WEP keys again */
1464 wpa_set_wep_keys(wpa_s, ssid);
1465 }
1466
1467 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1468 /*
1469 * Do not allow EAP session resumption between different
1470 * network configurations.
1471 */
1472 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1473 }
1474 old_ssid = wpa_s->current_ssid;
1475 wpa_s->current_ssid = ssid;
1476 wpa_s->current_bss = bss;
1477 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1478 wpa_supplicant_initiate_eapol(wpa_s);
1479 if (old_ssid != wpa_s->current_ssid)
1480 wpas_notify_network_changed(wpa_s);
1481 }
1482
1483
1484 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1485 const u8 *addr)
1486 {
1487 struct wpa_ssid *old_ssid;
1488
1489 wpa_clear_keys(wpa_s, addr);
1490 wpa_supplicant_mark_disassoc(wpa_s);
1491 old_ssid = wpa_s->current_ssid;
1492 wpa_s->current_ssid = NULL;
1493 wpa_s->current_bss = NULL;
1494 wpa_sm_set_config(wpa_s->wpa, NULL);
1495 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1496 if (old_ssid != wpa_s->current_ssid)
1497 wpas_notify_network_changed(wpa_s);
1498 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1499 }
1500
1501
1502 /**
1503 * wpa_supplicant_disassociate - Disassociate the current connection
1504 * @wpa_s: Pointer to wpa_supplicant data
1505 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1506 *
1507 * This function is used to request %wpa_supplicant to disassociate with the
1508 * current AP.
1509 */
1510 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1511 int reason_code)
1512 {
1513 u8 *addr = NULL;
1514 union wpa_event_data event;
1515
1516 if (!is_zero_ether_addr(wpa_s->bssid)) {
1517 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1518 addr = wpa_s->bssid;
1519 os_memset(&event, 0, sizeof(event));
1520 event.disassoc_info.reason_code = (u16) reason_code;
1521 event.disassoc_info.locally_generated = 1;
1522 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
1523 }
1524
1525 wpa_supplicant_clear_connection(wpa_s, addr);
1526 }
1527
1528
1529 /**
1530 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1531 * @wpa_s: Pointer to wpa_supplicant data
1532 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1533 *
1534 * This function is used to request %wpa_supplicant to deauthenticate from the
1535 * current AP.
1536 */
1537 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1538 int reason_code)
1539 {
1540 u8 *addr = NULL;
1541 union wpa_event_data event;
1542
1543 if (!is_zero_ether_addr(wpa_s->bssid)) {
1544 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1545 addr = wpa_s->bssid;
1546 os_memset(&event, 0, sizeof(event));
1547 event.deauth_info.reason_code = (u16) reason_code;
1548 event.deauth_info.locally_generated = 1;
1549 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
1550 }
1551
1552 wpa_supplicant_clear_connection(wpa_s, addr);
1553 }
1554
1555
1556 /**
1557 * wpa_supplicant_enable_network - Mark a configured network as enabled
1558 * @wpa_s: wpa_supplicant structure for a network interface
1559 * @ssid: wpa_ssid structure for a configured network or %NULL
1560 *
1561 * Enables the specified network or all networks if no network specified.
1562 */
1563 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1564 struct wpa_ssid *ssid)
1565 {
1566 struct wpa_ssid *other_ssid;
1567 int was_disabled;
1568
1569 if (ssid == NULL) {
1570 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1571 other_ssid = other_ssid->next) {
1572 if (other_ssid->disabled == 2)
1573 continue; /* do not change persistent P2P group
1574 * data */
1575 if (other_ssid == wpa_s->current_ssid &&
1576 other_ssid->disabled)
1577 wpa_s->reassociate = 1;
1578
1579 was_disabled = other_ssid->disabled;
1580
1581 other_ssid->disabled = 0;
1582
1583 if (was_disabled != other_ssid->disabled)
1584 wpas_notify_network_enabled_changed(
1585 wpa_s, other_ssid);
1586 }
1587 if (wpa_s->reassociate)
1588 wpa_supplicant_req_scan(wpa_s, 0, 0);
1589 } else if (ssid->disabled && ssid->disabled != 2) {
1590 if (wpa_s->current_ssid == NULL) {
1591 /*
1592 * Try to reassociate since there is no current
1593 * configuration and a new network was made available.
1594 */
1595 wpa_s->reassociate = 1;
1596 wpa_supplicant_req_scan(wpa_s, 0, 0);
1597 }
1598
1599 was_disabled = ssid->disabled;
1600
1601 ssid->disabled = 0;
1602
1603 if (was_disabled != ssid->disabled)
1604 wpas_notify_network_enabled_changed(wpa_s, ssid);
1605 }
1606 }
1607
1608
1609 /**
1610 * wpa_supplicant_disable_network - Mark a configured network as disabled
1611 * @wpa_s: wpa_supplicant structure for a network interface
1612 * @ssid: wpa_ssid structure for a configured network or %NULL
1613 *
1614 * Disables the specified network or all networks if no network specified.
1615 */
1616 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1617 struct wpa_ssid *ssid)
1618 {
1619 struct wpa_ssid *other_ssid;
1620 int was_disabled;
1621
1622 if (ssid == NULL) {
1623 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1624 other_ssid = other_ssid->next) {
1625 was_disabled = other_ssid->disabled;
1626 if (was_disabled == 2)
1627 continue; /* do not change persistent P2P group
1628 * data */
1629
1630 other_ssid->disabled = 1;
1631
1632 if (was_disabled != other_ssid->disabled)
1633 wpas_notify_network_enabled_changed(
1634 wpa_s, other_ssid);
1635 }
1636 if (wpa_s->current_ssid)
1637 wpa_supplicant_disassociate(
1638 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1639 } else if (ssid->disabled != 2) {
1640 if (ssid == wpa_s->current_ssid)
1641 wpa_supplicant_disassociate(
1642 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1643
1644 was_disabled = ssid->disabled;
1645
1646 ssid->disabled = 1;
1647
1648 if (was_disabled != ssid->disabled)
1649 wpas_notify_network_enabled_changed(wpa_s, ssid);
1650 }
1651 }
1652
1653
1654 /**
1655 * wpa_supplicant_select_network - Attempt association with a network
1656 * @wpa_s: wpa_supplicant structure for a network interface
1657 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1658 */
1659 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1660 struct wpa_ssid *ssid)
1661 {
1662
1663 struct wpa_ssid *other_ssid;
1664 int disconnected = 0;
1665
1666 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
1667 wpa_supplicant_disassociate(
1668 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1669 disconnected = 1;
1670 }
1671
1672 /*
1673 * Mark all other networks disabled or mark all networks enabled if no
1674 * network specified.
1675 */
1676 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1677 other_ssid = other_ssid->next) {
1678 int was_disabled = other_ssid->disabled;
1679 if (was_disabled == 2)
1680 continue; /* do not change persistent P2P group data */
1681
1682 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1683
1684 if (was_disabled != other_ssid->disabled)
1685 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1686 }
1687
1688 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1689 /* We are already associated with the selected network */
1690 wpa_printf(MSG_DEBUG, "Already associated with the "
1691 "selected network - do nothing");
1692 return;
1693 }
1694
1695 if (ssid)
1696 wpa_s->current_ssid = ssid;
1697 wpa_s->connect_without_scan = NULL;
1698 wpa_s->disconnected = 0;
1699 wpa_s->reassociate = 1;
1700 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
1701
1702 if (ssid)
1703 wpas_notify_network_selected(wpa_s, ssid);
1704 }
1705
1706
1707 /**
1708 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1709 * @wpa_s: wpa_supplicant structure for a network interface
1710 * @ap_scan: AP scan mode
1711 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1712 *
1713 */
1714 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1715 {
1716
1717 int old_ap_scan;
1718
1719 if (ap_scan < 0 || ap_scan > 2)
1720 return -1;
1721
1722 #ifdef ANDROID
1723 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1724 wpa_s->wpa_state >= WPA_ASSOCIATING &&
1725 wpa_s->wpa_state < WPA_COMPLETED) {
1726 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1727 "associating", wpa_s->conf->ap_scan, ap_scan);
1728 return 0;
1729 }
1730 #endif /* ANDROID */
1731
1732 old_ap_scan = wpa_s->conf->ap_scan;
1733 wpa_s->conf->ap_scan = ap_scan;
1734
1735 if (old_ap_scan != wpa_s->conf->ap_scan)
1736 wpas_notify_ap_scan_changed(wpa_s);
1737
1738 return 0;
1739 }
1740
1741
1742 /**
1743 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1744 * @wpa_s: wpa_supplicant structure for a network interface
1745 * @expire_age: Expiration age in seconds
1746 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1747 *
1748 */
1749 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1750 unsigned int bss_expire_age)
1751 {
1752 if (bss_expire_age < 10) {
1753 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1754 bss_expire_age);
1755 return -1;
1756 }
1757 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1758 bss_expire_age);
1759 wpa_s->conf->bss_expiration_age = bss_expire_age;
1760
1761 return 0;
1762 }
1763
1764
1765 /**
1766 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1767 * @wpa_s: wpa_supplicant structure for a network interface
1768 * @expire_count: number of scans after which an unseen BSS is reclaimed
1769 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1770 *
1771 */
1772 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1773 unsigned int bss_expire_count)
1774 {
1775 if (bss_expire_count < 1) {
1776 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1777 bss_expire_count);
1778 return -1;
1779 }
1780 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1781 bss_expire_count);
1782 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1783
1784 return 0;
1785 }
1786
1787
1788 /**
1789 * wpa_supplicant_set_scan_interval - Set scan interval
1790 * @wpa_s: wpa_supplicant structure for a network interface
1791 * @scan_interval: scan interval in seconds
1792 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
1793 *
1794 */
1795 int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
1796 int scan_interval)
1797 {
1798 if (scan_interval < 0) {
1799 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
1800 scan_interval);
1801 return -1;
1802 }
1803 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
1804 scan_interval);
1805 wpa_s->scan_interval = scan_interval;
1806
1807 return 0;
1808 }
1809
1810
1811 /**
1812 * wpa_supplicant_set_debug_params - Set global debug params
1813 * @global: wpa_global structure
1814 * @debug_level: debug level
1815 * @debug_timestamp: determines if show timestamp in debug data
1816 * @debug_show_keys: determines if show keys in debug data
1817 * Returns: 0 if succeed or -1 if debug_level has wrong value
1818 */
1819 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1820 int debug_timestamp, int debug_show_keys)
1821 {
1822
1823 int old_level, old_timestamp, old_show_keys;
1824
1825 /* check for allowed debuglevels */
1826 if (debug_level != MSG_EXCESSIVE &&
1827 debug_level != MSG_MSGDUMP &&
1828 debug_level != MSG_DEBUG &&
1829 debug_level != MSG_INFO &&
1830 debug_level != MSG_WARNING &&
1831 debug_level != MSG_ERROR)
1832 return -1;
1833
1834 old_level = wpa_debug_level;
1835 old_timestamp = wpa_debug_timestamp;
1836 old_show_keys = wpa_debug_show_keys;
1837
1838 wpa_debug_level = debug_level;
1839 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1840 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1841
1842 if (wpa_debug_level != old_level)
1843 wpas_notify_debug_level_changed(global);
1844 if (wpa_debug_timestamp != old_timestamp)
1845 wpas_notify_debug_timestamp_changed(global);
1846 if (wpa_debug_show_keys != old_show_keys)
1847 wpas_notify_debug_show_keys_changed(global);
1848
1849 return 0;
1850 }
1851
1852
1853 /**
1854 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1855 * @wpa_s: Pointer to wpa_supplicant data
1856 * Returns: A pointer to the current network structure or %NULL on failure
1857 */
1858 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1859 {
1860 struct wpa_ssid *entry;
1861 u8 ssid[MAX_SSID_LEN];
1862 int res;
1863 size_t ssid_len;
1864 u8 bssid[ETH_ALEN];
1865 int wired;
1866
1867 res = wpa_drv_get_ssid(wpa_s, ssid);
1868 if (res < 0) {
1869 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1870 "driver");
1871 return NULL;
1872 }
1873 ssid_len = res;
1874
1875 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1876 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1877 "driver");
1878 return NULL;
1879 }
1880
1881 wired = wpa_s->conf->ap_scan == 0 &&
1882 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1883
1884 entry = wpa_s->conf->ssid;
1885 while (entry) {
1886 if (!entry->disabled &&
1887 ((ssid_len == entry->ssid_len &&
1888 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1889 (!entry->bssid_set ||
1890 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1891 return entry;
1892 #ifdef CONFIG_WPS
1893 if (!entry->disabled &&
1894 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1895 (entry->ssid == NULL || entry->ssid_len == 0) &&
1896 (!entry->bssid_set ||
1897 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1898 return entry;
1899 #endif /* CONFIG_WPS */
1900
1901 if (!entry->disabled && entry->bssid_set &&
1902 entry->ssid_len == 0 &&
1903 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1904 return entry;
1905
1906 entry = entry->next;
1907 }
1908
1909 return NULL;
1910 }
1911
1912
1913 static int select_driver(struct wpa_supplicant *wpa_s, int i)
1914 {
1915 struct wpa_global *global = wpa_s->global;
1916
1917 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
1918 global->drv_priv[i] = wpa_drivers[i]->global_init();
1919 if (global->drv_priv[i] == NULL) {
1920 wpa_printf(MSG_ERROR, "Failed to initialize driver "
1921 "'%s'", wpa_drivers[i]->name);
1922 return -1;
1923 }
1924 }
1925
1926 wpa_s->driver = wpa_drivers[i];
1927 wpa_s->global_drv_priv = global->drv_priv[i];
1928
1929 return 0;
1930 }
1931
1932
1933 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1934 const char *name)
1935 {
1936 int i;
1937 size_t len;
1938 const char *pos, *driver = name;
1939
1940 if (wpa_s == NULL)
1941 return -1;
1942
1943 if (wpa_drivers[0] == NULL) {
1944 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1945 "wpa_supplicant");
1946 return -1;
1947 }
1948
1949 if (name == NULL) {
1950 /* default to first driver in the list */
1951 return select_driver(wpa_s, 0);
1952 }
1953
1954 do {
1955 pos = os_strchr(driver, ',');
1956 if (pos)
1957 len = pos - driver;
1958 else
1959 len = os_strlen(driver);
1960
1961 for (i = 0; wpa_drivers[i]; i++) {
1962 if (os_strlen(wpa_drivers[i]->name) == len &&
1963 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1964 0) {
1965 /* First driver that succeeds wins */
1966 if (select_driver(wpa_s, i) == 0)
1967 return 0;
1968 }
1969 }
1970
1971 driver = pos + 1;
1972 } while (pos);
1973
1974 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1975 return -1;
1976 }
1977
1978
1979 /**
1980 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1981 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1982 * with struct wpa_driver_ops::init()
1983 * @src_addr: Source address of the EAPOL frame
1984 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1985 * @len: Length of the EAPOL data
1986 *
1987 * This function is called for each received EAPOL frame. Most driver
1988 * interfaces rely on more generic OS mechanism for receiving frames through
1989 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1990 * take care of received EAPOL frames and deliver them to the core supplicant
1991 * code by calling this function.
1992 */
1993 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1994 const u8 *buf, size_t len)
1995 {
1996 struct wpa_supplicant *wpa_s = ctx;
1997
1998 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1999 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2000
2001 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2002 /*
2003 * There is possible race condition between receiving the
2004 * association event and the EAPOL frame since they are coming
2005 * through different paths from the driver. In order to avoid
2006 * issues in trying to process the EAPOL frame before receiving
2007 * association information, lets queue it for processing until
2008 * the association event is received.
2009 */
2010 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
2011 "of received EAPOL frame");
2012 wpabuf_free(wpa_s->pending_eapol_rx);
2013 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2014 if (wpa_s->pending_eapol_rx) {
2015 os_get_time(&wpa_s->pending_eapol_rx_time);
2016 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2017 ETH_ALEN);
2018 }
2019 return;
2020 }
2021
2022 #ifdef CONFIG_AP
2023 if (wpa_s->ap_iface) {
2024 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2025 return;
2026 }
2027 #endif /* CONFIG_AP */
2028
2029 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2030 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2031 "no key management is configured");
2032 return;
2033 }
2034
2035 if (wpa_s->eapol_received == 0 &&
2036 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2037 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2038 wpa_s->wpa_state != WPA_COMPLETED) &&
2039 (wpa_s->current_ssid == NULL ||
2040 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2041 /* Timeout for completing IEEE 802.1X and WPA authentication */
2042 wpa_supplicant_req_auth_timeout(
2043 wpa_s,
2044 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2045 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2046 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2047 70 : 10, 0);
2048 }
2049 wpa_s->eapol_received++;
2050
2051 if (wpa_s->countermeasures) {
2052 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2053 "EAPOL packet");
2054 return;
2055 }
2056
2057 #ifdef CONFIG_IBSS_RSN
2058 if (wpa_s->current_ssid &&
2059 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2060 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2061 return;
2062 }
2063 #endif /* CONFIG_IBSS_RSN */
2064
2065 /* Source address of the incoming EAPOL frame could be compared to the
2066 * current BSSID. However, it is possible that a centralized
2067 * Authenticator could be using another MAC address than the BSSID of
2068 * an AP, so just allow any address to be used for now. The replies are
2069 * still sent to the current BSSID (if available), though. */
2070
2071 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2072 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2073 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2074 return;
2075 wpa_drv_poll(wpa_s);
2076 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2077 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2078 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2079 /*
2080 * Set portValid = TRUE here since we are going to skip 4-way
2081 * handshake processing which would normally set portValid. We
2082 * need this to allow the EAPOL state machines to be completed
2083 * without going through EAPOL-Key handshake.
2084 */
2085 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2086 }
2087 }
2088
2089
2090 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
2091 {
2092 if (wpa_s->driver->send_eapol) {
2093 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2094 if (addr)
2095 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2096 } else if (!(wpa_s->drv_flags &
2097 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2098 l2_packet_deinit(wpa_s->l2);
2099 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2100 wpa_drv_get_mac_addr(wpa_s),
2101 ETH_P_EAPOL,
2102 wpa_supplicant_rx_eapol, wpa_s, 0);
2103 if (wpa_s->l2 == NULL)
2104 return -1;
2105 } else {
2106 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2107 if (addr)
2108 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2109 }
2110
2111 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2112 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2113 return -1;
2114 }
2115
2116 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2117 MAC2STR(wpa_s->own_addr));
2118 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2119
2120 return 0;
2121 }
2122
2123
2124 static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2125 const u8 *buf, size_t len)
2126 {
2127 struct wpa_supplicant *wpa_s = ctx;
2128 const struct l2_ethhdr *eth;
2129
2130 if (len < sizeof(*eth))
2131 return;
2132 eth = (const struct l2_ethhdr *) buf;
2133
2134 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2135 !(eth->h_dest[0] & 0x01)) {
2136 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2137 " (bridge - not for this interface - ignore)",
2138 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2139 return;
2140 }
2141
2142 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2143 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2144 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2145 len - sizeof(*eth));
2146 }
2147
2148
2149 /**
2150 * wpa_supplicant_driver_init - Initialize driver interface parameters
2151 * @wpa_s: Pointer to wpa_supplicant data
2152 * Returns: 0 on success, -1 on failure
2153 *
2154 * This function is called to initialize driver interface parameters.
2155 * wpa_drv_init() must have been called before this function to initialize the
2156 * driver interface.
2157 */
2158 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2159 {
2160 static int interface_count = 0;
2161
2162 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2163 return -1;
2164
2165 if (wpa_s->bridge_ifname[0]) {
2166 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2167 "interface '%s'", wpa_s->bridge_ifname);
2168 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2169 wpa_s->own_addr,
2170 ETH_P_EAPOL,
2171 wpa_supplicant_rx_eapol_bridge,
2172 wpa_s, 1);
2173 if (wpa_s->l2_br == NULL) {
2174 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2175 "connection for the bridge interface '%s'",
2176 wpa_s->bridge_ifname);
2177 return -1;
2178 }
2179 }
2180
2181 wpa_clear_keys(wpa_s, NULL);
2182
2183 /* Make sure that TKIP countermeasures are not left enabled (could
2184 * happen if wpa_supplicant is killed during countermeasures. */
2185 wpa_drv_set_countermeasures(wpa_s, 0);
2186
2187 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2188 wpa_drv_flush_pmkid(wpa_s);
2189
2190 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2191 wpa_s->prev_scan_wildcard = 0;
2192
2193 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2194 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2195 100000))
2196 wpa_supplicant_req_scan(wpa_s, interface_count,
2197 100000);
2198 interface_count++;
2199 } else
2200 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2201
2202 return 0;
2203 }
2204
2205
2206 static int wpa_supplicant_daemon(const char *pid_file)
2207 {
2208 wpa_printf(MSG_DEBUG, "Daemonize..");
2209 return os_daemonize(pid_file);
2210 }
2211
2212
2213 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2214 {
2215 struct wpa_supplicant *wpa_s;
2216
2217 wpa_s = os_zalloc(sizeof(*wpa_s));
2218 if (wpa_s == NULL)
2219 return NULL;
2220 wpa_s->scan_req = 1;
2221 wpa_s->scan_interval = 5;
2222 wpa_s->new_connection = 1;
2223 wpa_s->parent = wpa_s;
2224 wpa_s->sched_scanning = 0;
2225
2226 return wpa_s;
2227 }
2228
2229
2230 #ifdef CONFIG_HT_OVERRIDES
2231
2232 static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2233 struct ieee80211_ht_capabilities *htcaps,
2234 struct ieee80211_ht_capabilities *htcaps_mask,
2235 const char *ht_mcs)
2236 {
2237 /* parse ht_mcs into hex array */
2238 int i;
2239 const char *tmp = ht_mcs;
2240 char *end = NULL;
2241
2242 /* If ht_mcs is null, do not set anything */
2243 if (!ht_mcs)
2244 return 0;
2245
2246 /* This is what we are setting in the kernel */
2247 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2248
2249 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2250
2251 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2252 errno = 0;
2253 long v = strtol(tmp, &end, 16);
2254 if (errno == 0) {
2255 wpa_msg(wpa_s, MSG_DEBUG,
2256 "htcap value[%i]: %ld end: %p tmp: %p",
2257 i, v, end, tmp);
2258 if (end == tmp)
2259 break;
2260
2261 htcaps->supported_mcs_set[i] = v;
2262 tmp = end;
2263 } else {
2264 wpa_msg(wpa_s, MSG_ERROR,
2265 "Failed to parse ht-mcs: %s, error: %s\n",
2266 ht_mcs, strerror(errno));
2267 return -1;
2268 }
2269 }
2270
2271 /*
2272 * If we were able to parse any values, then set mask for the MCS set.
2273 */
2274 if (i) {
2275 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2276 IEEE80211_HT_MCS_MASK_LEN - 1);
2277 /* skip the 3 reserved bits */
2278 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2279 0x1f;
2280 }
2281
2282 return 0;
2283 }
2284
2285
2286 static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2287 struct ieee80211_ht_capabilities *htcaps,
2288 struct ieee80211_ht_capabilities *htcaps_mask,
2289 int disabled)
2290 {
2291 u16 msk;
2292
2293 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2294
2295 if (disabled == -1)
2296 return 0;
2297
2298 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2299 htcaps_mask->ht_capabilities_info |= msk;
2300 if (disabled)
2301 htcaps->ht_capabilities_info &= msk;
2302 else
2303 htcaps->ht_capabilities_info |= msk;
2304
2305 return 0;
2306 }
2307
2308
2309 static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2310 struct ieee80211_ht_capabilities *htcaps,
2311 struct ieee80211_ht_capabilities *htcaps_mask,
2312 int factor)
2313 {
2314 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2315
2316 if (factor == -1)
2317 return 0;
2318
2319 if (factor < 0 || factor > 3) {
2320 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2321 "Must be 0-3 or -1", factor);
2322 return -EINVAL;
2323 }
2324
2325 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2326 htcaps->a_mpdu_params &= ~0x3;
2327 htcaps->a_mpdu_params |= factor & 0x3;
2328
2329 return 0;
2330 }
2331
2332
2333 static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2334 struct ieee80211_ht_capabilities *htcaps,
2335 struct ieee80211_ht_capabilities *htcaps_mask,
2336 int density)
2337 {
2338 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2339
2340 if (density == -1)
2341 return 0;
2342
2343 if (density < 0 || density > 7) {
2344 wpa_msg(wpa_s, MSG_ERROR,
2345 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2346 density);
2347 return -EINVAL;
2348 }
2349
2350 htcaps_mask->a_mpdu_params |= 0x1C;
2351 htcaps->a_mpdu_params &= ~(0x1C);
2352 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2353
2354 return 0;
2355 }
2356
2357
2358 static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2359 struct ieee80211_ht_capabilities *htcaps,
2360 struct ieee80211_ht_capabilities *htcaps_mask,
2361 int disabled)
2362 {
2363 /* Masking these out disables HT40 */
2364 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2365 HT_CAP_INFO_SHORT_GI40MHZ);
2366
2367 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2368
2369 if (disabled)
2370 htcaps->ht_capabilities_info &= ~msk;
2371 else
2372 htcaps->ht_capabilities_info |= msk;
2373
2374 htcaps_mask->ht_capabilities_info |= msk;
2375
2376 return 0;
2377 }
2378
2379
2380 void wpa_supplicant_apply_ht_overrides(
2381 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2382 struct wpa_driver_associate_params *params)
2383 {
2384 struct ieee80211_ht_capabilities *htcaps;
2385 struct ieee80211_ht_capabilities *htcaps_mask;
2386
2387 if (!ssid)
2388 return;
2389
2390 params->disable_ht = ssid->disable_ht;
2391 if (!params->htcaps || !params->htcaps_mask)
2392 return;
2393
2394 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2395 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2396 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2397 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2398 ssid->disable_max_amsdu);
2399 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2400 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2401 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
2402 }
2403
2404 #endif /* CONFIG_HT_OVERRIDES */
2405
2406
2407 static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
2408 {
2409 #ifdef PCSC_FUNCS
2410 size_t len;
2411
2412 if (!wpa_s->conf->pcsc_reader)
2413 return 0;
2414
2415 wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
2416 if (!wpa_s->scard)
2417 return 1;
2418
2419 if (wpa_s->conf->pcsc_pin &&
2420 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
2421 scard_deinit(wpa_s->scard);
2422 wpa_s->scard = NULL;
2423 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
2424 return -1;
2425 }
2426
2427 len = sizeof(wpa_s->imsi) - 1;
2428 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
2429 scard_deinit(wpa_s->scard);
2430 wpa_s->scard = NULL;
2431 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
2432 return -1;
2433 }
2434 wpa_s->imsi[len] = '\0';
2435
2436 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
2437
2438 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
2439 wpa_s->imsi, wpa_s->mnc_len);
2440
2441 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
2442 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
2443 #endif /* PCSC_FUNCS */
2444
2445 return 0;
2446 }
2447
2448
2449 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2450 struct wpa_interface *iface)
2451 {
2452 const char *ifname, *driver;
2453 struct wpa_driver_capa capa;
2454
2455 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2456 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2457 iface->confname ? iface->confname : "N/A",
2458 iface->driver ? iface->driver : "default",
2459 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2460 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2461
2462 if (iface->confname) {
2463 #ifdef CONFIG_BACKEND_FILE
2464 wpa_s->confname = os_rel2abs_path(iface->confname);
2465 if (wpa_s->confname == NULL) {
2466 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2467 "for configuration file '%s'.",
2468 iface->confname);
2469 return -1;
2470 }
2471 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2472 iface->confname, wpa_s->confname);
2473 #else /* CONFIG_BACKEND_FILE */
2474 wpa_s->confname = os_strdup(iface->confname);
2475 #endif /* CONFIG_BACKEND_FILE */
2476 wpa_s->conf = wpa_config_read(wpa_s->confname);
2477 if (wpa_s->conf == NULL) {
2478 wpa_printf(MSG_ERROR, "Failed to read or parse "
2479 "configuration '%s'.", wpa_s->confname);
2480 return -1;
2481 }
2482
2483 /*
2484 * Override ctrl_interface and driver_param if set on command
2485 * line.
2486 */
2487 if (iface->ctrl_interface) {
2488 os_free(wpa_s->conf->ctrl_interface);
2489 wpa_s->conf->ctrl_interface =
2490 os_strdup(iface->ctrl_interface);
2491 }
2492
2493 if (iface->driver_param) {
2494 os_free(wpa_s->conf->driver_param);
2495 wpa_s->conf->driver_param =
2496 os_strdup(iface->driver_param);
2497 }
2498 } else
2499 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2500 iface->driver_param);
2501
2502 if (wpa_s->conf == NULL) {
2503 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2504 return -1;
2505 }
2506
2507 if (iface->ifname == NULL) {
2508 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2509 return -1;
2510 }
2511 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2512 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2513 iface->ifname);
2514 return -1;
2515 }
2516 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2517
2518 if (iface->bridge_ifname) {
2519 if (os_strlen(iface->bridge_ifname) >=
2520 sizeof(wpa_s->bridge_ifname)) {
2521 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2522 "name '%s'.", iface->bridge_ifname);
2523 return -1;
2524 }
2525 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2526 sizeof(wpa_s->bridge_ifname));
2527 }
2528
2529 /* RSNA Supplicant Key Management - INITIALIZE */
2530 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2531 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2532
2533 /* Initialize driver interface and register driver event handler before
2534 * L2 receive handler so that association events are processed before
2535 * EAPOL-Key packets if both become available for the same select()
2536 * call. */
2537 driver = iface->driver;
2538 next_driver:
2539 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2540 return -1;
2541
2542 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2543 if (wpa_s->drv_priv == NULL) {
2544 const char *pos;
2545 pos = driver ? os_strchr(driver, ',') : NULL;
2546 if (pos) {
2547 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2548 "driver interface - try next driver wrapper");
2549 driver = pos + 1;
2550 goto next_driver;
2551 }
2552 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2553 "interface");
2554 return -1;
2555 }
2556 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2557 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2558 "driver_param '%s'", wpa_s->conf->driver_param);
2559 return -1;
2560 }
2561
2562 ifname = wpa_drv_get_ifname(wpa_s);
2563 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2564 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2565 "interface name with '%s'", ifname);
2566 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2567 }
2568
2569 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2570 return -1;
2571
2572 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2573 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2574 NULL);
2575 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2576
2577 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2578 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2579 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2580 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2581 "dot11RSNAConfigPMKLifetime");
2582 return -1;
2583 }
2584
2585 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2586 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2587 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2588 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2589 "dot11RSNAConfigPMKReauthThreshold");
2590 return -1;
2591 }
2592
2593 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2594 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2595 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2596 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2597 "dot11RSNAConfigSATimeout");
2598 return -1;
2599 }
2600
2601 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2602 &wpa_s->hw.num_modes,
2603 &wpa_s->hw.flags);
2604
2605 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2606 wpa_s->drv_capa_known = 1;
2607 wpa_s->drv_flags = capa.flags;
2608 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
2609 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2610 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2611 wpa_s->sched_scan_supported = capa.sched_scan_supported;
2612 wpa_s->max_match_sets = capa.max_match_sets;
2613 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2614 wpa_s->max_stations = capa.max_stations;
2615 }
2616 if (wpa_s->max_remain_on_chan == 0)
2617 wpa_s->max_remain_on_chan = 1000;
2618
2619 if (wpa_supplicant_driver_init(wpa_s) < 0)
2620 return -1;
2621
2622 #ifdef CONFIG_TDLS
2623 if (wpa_tdls_init(wpa_s->wpa))
2624 return -1;
2625 #endif /* CONFIG_TDLS */
2626
2627 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2628 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2629 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2630 return -1;
2631 }
2632
2633 if (wpas_wps_init(wpa_s))
2634 return -1;
2635
2636 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2637 return -1;
2638 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2639
2640 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2641 if (wpa_s->ctrl_iface == NULL) {
2642 wpa_printf(MSG_ERROR,
2643 "Failed to initialize control interface '%s'.\n"
2644 "You may have another wpa_supplicant process "
2645 "already running or the file was\n"
2646 "left by an unclean termination of wpa_supplicant "
2647 "in which case you will need\n"
2648 "to manually remove this file before starting "
2649 "wpa_supplicant again.\n",
2650 wpa_s->conf->ctrl_interface);
2651 return -1;
2652 }
2653
2654 wpa_s->gas = gas_query_init(wpa_s);
2655 if (wpa_s->gas == NULL) {
2656 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2657 return -1;
2658 }
2659
2660 #ifdef CONFIG_P2P
2661 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2662 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2663 return -1;
2664 }
2665 #endif /* CONFIG_P2P */
2666
2667 if (wpa_bss_init(wpa_s) < 0)
2668 return -1;
2669
2670 if (pcsc_reader_init(wpa_s) < 0)
2671 return -1;
2672
2673 return 0;
2674 }
2675
2676
2677 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2678 int notify, int terminate)
2679 {
2680 if (wpa_s->drv_priv) {
2681 wpa_supplicant_deauthenticate(wpa_s,
2682 WLAN_REASON_DEAUTH_LEAVING);
2683
2684 wpa_drv_set_countermeasures(wpa_s, 0);
2685 wpa_clear_keys(wpa_s, NULL);
2686 }
2687
2688 wpa_supplicant_cleanup(wpa_s);
2689
2690 if (wpa_s->drv_priv)
2691 wpa_drv_deinit(wpa_s);
2692
2693 if (notify)
2694 wpas_notify_iface_removed(wpa_s);
2695
2696 if (terminate)
2697 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2698
2699 if (wpa_s->ctrl_iface) {
2700 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2701 wpa_s->ctrl_iface = NULL;
2702 }
2703
2704 if (wpa_s->conf != NULL) {
2705 wpa_config_free(wpa_s->conf);
2706 wpa_s->conf = NULL;
2707 }
2708 }
2709
2710
2711 /**
2712 * wpa_supplicant_add_iface - Add a new network interface
2713 * @global: Pointer to global data from wpa_supplicant_init()
2714 * @iface: Interface configuration options
2715 * Returns: Pointer to the created interface or %NULL on failure
2716 *
2717 * This function is used to add new network interfaces for %wpa_supplicant.
2718 * This can be called before wpa_supplicant_run() to add interfaces before the
2719 * main event loop has been started. In addition, new interfaces can be added
2720 * dynamically while %wpa_supplicant is already running. This could happen,
2721 * e.g., when a hotplug network adapter is inserted.
2722 */
2723 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2724 struct wpa_interface *iface)
2725 {
2726 struct wpa_supplicant *wpa_s;
2727 struct wpa_interface t_iface;
2728 struct wpa_ssid *ssid;
2729
2730 if (global == NULL || iface == NULL)
2731 return NULL;
2732
2733 wpa_s = wpa_supplicant_alloc();
2734 if (wpa_s == NULL)
2735 return NULL;
2736
2737 wpa_s->global = global;
2738
2739 t_iface = *iface;
2740 if (global->params.override_driver) {
2741 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2742 "('%s' -> '%s')",
2743 iface->driver, global->params.override_driver);
2744 t_iface.driver = global->params.override_driver;
2745 }
2746 if (global->params.override_ctrl_interface) {
2747 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2748 "ctrl_interface ('%s' -> '%s')",
2749 iface->ctrl_interface,
2750 global->params.override_ctrl_interface);
2751 t_iface.ctrl_interface =
2752 global->params.override_ctrl_interface;
2753 }
2754 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2755 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2756 iface->ifname);
2757 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
2758 os_free(wpa_s);
2759 return NULL;
2760 }
2761
2762 /* Notify the control interfaces about new iface */
2763 if (wpas_notify_iface_added(wpa_s)) {
2764 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
2765 os_free(wpa_s);
2766 return NULL;
2767 }
2768
2769 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2770 wpas_notify_network_added(wpa_s, ssid);
2771
2772 wpa_s->next = global->ifaces;
2773 global->ifaces = wpa_s;
2774
2775 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2776
2777 return wpa_s;
2778 }
2779
2780
2781 /**
2782 * wpa_supplicant_remove_iface - Remove a network interface
2783 * @global: Pointer to global data from wpa_supplicant_init()
2784 * @wpa_s: Pointer to the network interface to be removed
2785 * Returns: 0 if interface was removed, -1 if interface was not found
2786 *
2787 * This function can be used to dynamically remove network interfaces from
2788 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2789 * addition, this function is used to remove all remaining interfaces when
2790 * %wpa_supplicant is terminated.
2791 */
2792 int wpa_supplicant_remove_iface(struct wpa_global *global,
2793 struct wpa_supplicant *wpa_s,
2794 int terminate)
2795 {
2796 struct wpa_supplicant *prev;
2797
2798 /* Remove interface from the global list of interfaces */
2799 prev = global->ifaces;
2800 if (prev == wpa_s) {
2801 global->ifaces = wpa_s->next;
2802 } else {
2803 while (prev && prev->next != wpa_s)
2804 prev = prev->next;
2805 if (prev == NULL)
2806 return -1;
2807 prev->next = wpa_s->next;
2808 }
2809
2810 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2811
2812 if (global->p2p_group_formation == wpa_s)
2813 global->p2p_group_formation = NULL;
2814 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
2815 os_free(wpa_s);
2816
2817 return 0;
2818 }
2819
2820
2821 /**
2822 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2823 * @wpa_s: Pointer to the network interface
2824 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2825 */
2826 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2827 {
2828 const char *eapol_method;
2829
2830 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2831 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2832 return "NO-EAP";
2833 }
2834
2835 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2836 if (eapol_method == NULL)
2837 return "UNKNOWN-EAP";
2838
2839 return eapol_method;
2840 }
2841
2842
2843 /**
2844 * wpa_supplicant_get_iface - Get a new network interface
2845 * @global: Pointer to global data from wpa_supplicant_init()
2846 * @ifname: Interface name
2847 * Returns: Pointer to the interface or %NULL if not found
2848 */
2849 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2850 const char *ifname)
2851 {
2852 struct wpa_supplicant *wpa_s;
2853
2854 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2855 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2856 return wpa_s;
2857 }
2858 return NULL;
2859 }
2860
2861
2862 #ifndef CONFIG_NO_WPA_MSG
2863 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2864 {
2865 struct wpa_supplicant *wpa_s = ctx;
2866 if (wpa_s == NULL)
2867 return NULL;
2868 return wpa_s->ifname;
2869 }
2870 #endif /* CONFIG_NO_WPA_MSG */
2871
2872
2873 /**
2874 * wpa_supplicant_init - Initialize %wpa_supplicant
2875 * @params: Parameters for %wpa_supplicant
2876 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2877 *
2878 * This function is used to initialize %wpa_supplicant. After successful
2879 * initialization, the returned data pointer can be used to add and remove
2880 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2881 */
2882 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2883 {
2884 struct wpa_global *global;
2885 int ret, i;
2886
2887 if (params == NULL)
2888 return NULL;
2889
2890 #ifdef CONFIG_DRIVER_NDIS
2891 {
2892 void driver_ndis_init_ops(void);
2893 driver_ndis_init_ops();
2894 }
2895 #endif /* CONFIG_DRIVER_NDIS */
2896
2897 #ifndef CONFIG_NO_WPA_MSG
2898 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2899 #endif /* CONFIG_NO_WPA_MSG */
2900
2901 wpa_debug_open_file(params->wpa_debug_file_path);
2902 if (params->wpa_debug_syslog)
2903 wpa_debug_open_syslog();
2904 if (params->wpa_debug_tracing) {
2905 ret = wpa_debug_open_linux_tracing();
2906 if (ret) {
2907 wpa_printf(MSG_ERROR,
2908 "Failed to enable trace logging");
2909 return NULL;
2910 }
2911 }
2912
2913 ret = eap_register_methods();
2914 if (ret) {
2915 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2916 if (ret == -2)
2917 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2918 "the same EAP type.");
2919 return NULL;
2920 }
2921
2922 global = os_zalloc(sizeof(*global));
2923 if (global == NULL)
2924 return NULL;
2925 dl_list_init(&global->p2p_srv_bonjour);
2926 dl_list_init(&global->p2p_srv_upnp);
2927 global->params.daemonize = params->daemonize;
2928 global->params.wait_for_monitor = params->wait_for_monitor;
2929 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2930 if (params->pid_file)
2931 global->params.pid_file = os_strdup(params->pid_file);
2932 if (params->ctrl_interface)
2933 global->params.ctrl_interface =
2934 os_strdup(params->ctrl_interface);
2935 if (params->override_driver)
2936 global->params.override_driver =
2937 os_strdup(params->override_driver);
2938 if (params->override_ctrl_interface)
2939 global->params.override_ctrl_interface =
2940 os_strdup(params->override_ctrl_interface);
2941 wpa_debug_level = global->params.wpa_debug_level =
2942 params->wpa_debug_level;
2943 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2944 params->wpa_debug_show_keys;
2945 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2946 params->wpa_debug_timestamp;
2947
2948 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2949
2950 if (eloop_init()) {
2951 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2952 wpa_supplicant_deinit(global);
2953 return NULL;
2954 }
2955
2956 random_init(params->entropy_file);
2957
2958 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2959 if (global->ctrl_iface == NULL) {
2960 wpa_supplicant_deinit(global);
2961 return NULL;
2962 }
2963
2964 if (wpas_notify_supplicant_initialized(global)) {
2965 wpa_supplicant_deinit(global);
2966 return NULL;
2967 }
2968
2969 for (i = 0; wpa_drivers[i]; i++)
2970 global->drv_count++;
2971 if (global->drv_count == 0) {
2972 wpa_printf(MSG_ERROR, "No drivers enabled");
2973 wpa_supplicant_deinit(global);
2974 return NULL;
2975 }
2976 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2977 if (global->drv_priv == NULL) {
2978 wpa_supplicant_deinit(global);
2979 return NULL;
2980 }
2981
2982 return global;
2983 }
2984
2985
2986 /**
2987 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2988 * @global: Pointer to global data from wpa_supplicant_init()
2989 * Returns: 0 after successful event loop run, -1 on failure
2990 *
2991 * This function starts the main event loop and continues running as long as
2992 * there are any remaining events. In most cases, this function is running as
2993 * long as the %wpa_supplicant process in still in use.
2994 */
2995 int wpa_supplicant_run(struct wpa_global *global)
2996 {
2997 struct wpa_supplicant *wpa_s;
2998
2999 if (global->params.daemonize &&
3000 wpa_supplicant_daemon(global->params.pid_file))
3001 return -1;
3002
3003 if (global->params.wait_for_monitor) {
3004 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
3005 if (wpa_s->ctrl_iface)
3006 wpa_supplicant_ctrl_iface_wait(
3007 wpa_s->ctrl_iface);
3008 }
3009
3010 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
3011 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
3012
3013 eloop_run();
3014
3015 return 0;
3016 }
3017
3018
3019 /**
3020 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
3021 * @global: Pointer to global data from wpa_supplicant_init()
3022 *
3023 * This function is called to deinitialize %wpa_supplicant and to free all
3024 * allocated resources. Remaining network interfaces will also be removed.
3025 */
3026 void wpa_supplicant_deinit(struct wpa_global *global)
3027 {
3028 int i;
3029
3030 if (global == NULL)
3031 return;
3032
3033 #ifdef CONFIG_P2P
3034 wpas_p2p_deinit_global(global);
3035 #endif /* CONFIG_P2P */
3036
3037 while (global->ifaces)
3038 wpa_supplicant_remove_iface(global, global->ifaces, 1);
3039
3040 if (global->ctrl_iface)
3041 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
3042
3043 wpas_notify_supplicant_deinitialized(global);
3044
3045 eap_peer_unregister_methods();
3046 #ifdef CONFIG_AP
3047 eap_server_unregister_methods();
3048 #endif /* CONFIG_AP */
3049
3050 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
3051 if (!global->drv_priv[i])
3052 continue;
3053 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
3054 }
3055 os_free(global->drv_priv);
3056
3057 random_deinit();
3058
3059 eloop_destroy();
3060
3061 if (global->params.pid_file) {
3062 os_daemonize_terminate(global->params.pid_file);
3063 os_free(global->params.pid_file);
3064 }
3065 os_free(global->params.ctrl_interface);
3066 os_free(global->params.override_driver);
3067 os_free(global->params.override_ctrl_interface);
3068
3069 os_free(global->p2p_disallow_freq);
3070
3071 os_free(global);
3072 wpa_debug_close_syslog();
3073 wpa_debug_close_file();
3074 wpa_debug_close_linux_tracing();
3075 }
3076
3077
3078 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
3079 {
3080 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3081 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3082 char country[3];
3083 country[0] = wpa_s->conf->country[0];
3084 country[1] = wpa_s->conf->country[1];
3085 country[2] = '\0';
3086 if (wpa_drv_set_country(wpa_s, country) < 0) {
3087 wpa_printf(MSG_ERROR, "Failed to set country code "
3088 "'%s'", country);
3089 }
3090 }
3091
3092 #ifdef CONFIG_WPS
3093 wpas_wps_update_config(wpa_s);
3094 #endif /* CONFIG_WPS */
3095
3096 #ifdef CONFIG_P2P
3097 wpas_p2p_update_config(wpa_s);
3098 #endif /* CONFIG_P2P */
3099
3100 wpa_s->conf->changed_parameters = 0;
3101 }
3102
3103
3104 static void add_freq(int *freqs, int *num_freqs, int freq)
3105 {
3106 int i;
3107
3108 for (i = 0; i < *num_freqs; i++) {
3109 if (freqs[i] == freq)
3110 return;
3111 }
3112
3113 freqs[*num_freqs] = freq;
3114 (*num_freqs)++;
3115 }
3116
3117
3118 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
3119 {
3120 struct wpa_bss *bss, *cbss;
3121 const int max_freqs = 10;
3122 int *freqs;
3123 int num_freqs = 0;
3124
3125 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
3126 if (freqs == NULL)
3127 return NULL;
3128
3129 cbss = wpa_s->current_bss;
3130
3131 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
3132 if (bss == cbss)
3133 continue;
3134 if (bss->ssid_len == cbss->ssid_len &&
3135 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
3136 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
3137 add_freq(freqs, &num_freqs, bss->freq);
3138 if (num_freqs == max_freqs)
3139 break;
3140 }
3141 }
3142
3143 if (num_freqs == 0) {
3144 os_free(freqs);
3145 freqs = NULL;
3146 }
3147
3148 return freqs;
3149 }
3150
3151
3152 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
3153 {
3154 int timeout;
3155 int count;
3156 int *freqs = NULL;
3157
3158 /*
3159 * Remove possible authentication timeout since the connection failed.
3160 */
3161 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
3162
3163 /*
3164 * Add the failed BSSID into the blacklist and speed up next scan
3165 * attempt if there could be other APs that could accept association.
3166 * The current blacklist count indicates how many times we have tried
3167 * connecting to this AP and multiple attempts mean that other APs are
3168 * either not available or has already been tried, so that we can start
3169 * increasing the delay here to avoid constant scanning.
3170 */
3171 count = wpa_blacklist_add(wpa_s, bssid);
3172 if (count == 1 && wpa_s->current_bss) {
3173 /*
3174 * This BSS was not in the blacklist before. If there is
3175 * another BSS available for the same ESS, we should try that
3176 * next. Otherwise, we may as well try this one once more
3177 * before allowing other, likely worse, ESSes to be considered.
3178 */
3179 freqs = get_bss_freqs_in_ess(wpa_s);
3180 if (freqs) {
3181 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
3182 "has been seen; try it next");
3183 wpa_blacklist_add(wpa_s, bssid);
3184 /*
3185 * On the next scan, go through only the known channels
3186 * used in this ESS based on previous scans to speed up
3187 * common load balancing use case.
3188 */
3189 os_free(wpa_s->next_scan_freqs);
3190 wpa_s->next_scan_freqs = freqs;
3191 }
3192 }
3193
3194 switch (count) {
3195 case 1:
3196 timeout = 100;
3197 break;
3198 case 2:
3199 timeout = 500;
3200 break;
3201 case 3:
3202 timeout = 1000;
3203 break;
3204 default:
3205 timeout = 5000;
3206 }
3207
3208 /*
3209 * TODO: if more than one possible AP is available in scan results,
3210 * could try the other ones before requesting a new scan.
3211 */
3212 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
3213 1000 * (timeout % 1000));
3214 }
3215
3216
3217 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
3218 {
3219 return wpa_s->conf->ap_scan == 2 ||
3220 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
3221 }
3222
3223
3224 #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
3225 int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
3226 struct wpa_ssid *ssid,
3227 const char *field,
3228 const char *value)
3229 {
3230 #ifdef IEEE8021X_EAPOL
3231 struct eap_peer_config *eap = &ssid->eap;
3232
3233 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
3234 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
3235 (const u8 *) value, os_strlen(value));
3236
3237 switch (wpa_supplicant_ctrl_req_from_string(field)) {
3238 case WPA_CTRL_REQ_EAP_IDENTITY:
3239 os_free(eap->identity);
3240 eap->identity = (u8 *) os_strdup(value);
3241 eap->identity_len = os_strlen(value);
3242 eap->pending_req_identity = 0;
3243 if (ssid == wpa_s->current_ssid)
3244 wpa_s->reassociate = 1;
3245 break;
3246 case WPA_CTRL_REQ_EAP_PASSWORD:
3247 os_free(eap->password);
3248 eap->password = (u8 *) os_strdup(value);
3249 eap->password_len = os_strlen(value);
3250 eap->pending_req_password = 0;
3251 if (ssid == wpa_s->current_ssid)
3252 wpa_s->reassociate = 1;
3253 break;
3254 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
3255 os_free(eap->new_password);
3256 eap->new_password = (u8 *) os_strdup(value);
3257 eap->new_password_len = os_strlen(value);
3258 eap->pending_req_new_password = 0;
3259 if (ssid == wpa_s->current_ssid)
3260 wpa_s->reassociate = 1;
3261 break;
3262 case WPA_CTRL_REQ_EAP_PIN:
3263 os_free(eap->pin);
3264 eap->pin = os_strdup(value);
3265 eap->pending_req_pin = 0;
3266 if (ssid == wpa_s->current_ssid)
3267 wpa_s->reassociate = 1;
3268 break;
3269 case WPA_CTRL_REQ_EAP_OTP:
3270 os_free(eap->otp);
3271 eap->otp = (u8 *) os_strdup(value);
3272 eap->otp_len = os_strlen(value);
3273 os_free(eap->pending_req_otp);
3274 eap->pending_req_otp = NULL;
3275 eap->pending_req_otp_len = 0;
3276 break;
3277 case WPA_CTRL_REQ_EAP_PASSPHRASE:
3278 os_free(eap->private_key_passwd);
3279 eap->private_key_passwd = (u8 *) os_strdup(value);
3280 eap->pending_req_passphrase = 0;
3281 if (ssid == wpa_s->current_ssid)
3282 wpa_s->reassociate = 1;
3283 break;
3284 default:
3285 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
3286 return -1;
3287 }
3288
3289 return 0;
3290 #else /* IEEE8021X_EAPOL */
3291 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
3292 return -1;
3293 #endif /* IEEE8021X_EAPOL */
3294 }
3295 #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */