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