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