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