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