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