OSDN Git Service

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