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