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