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