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