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