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