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