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