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