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