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