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