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