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