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