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