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