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