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