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