P2P: Include P2P IE in (Re)AssocReq to infra AP if it uses P2P IE
[libeap.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         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
686                 wpa_s->reassociate = 1;
687                 wpa_supplicant_req_scan(wpa_s, 0, 0);
688         }
689         wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
690         return 0;
691 }
692
693
694 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
695 {
696         struct wpa_global *global = signal_ctx;
697         struct wpa_supplicant *wpa_s;
698         wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
699         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
700                 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
701                         wpa_supplicant_terminate_proc(global);
702                 }
703         }
704 }
705
706
707 enum wpa_cipher cipher_suite2driver(int cipher)
708 {
709         switch (cipher) {
710         case WPA_CIPHER_NONE:
711                 return CIPHER_NONE;
712         case WPA_CIPHER_WEP40:
713                 return CIPHER_WEP40;
714         case WPA_CIPHER_WEP104:
715                 return CIPHER_WEP104;
716         case WPA_CIPHER_CCMP:
717                 return CIPHER_CCMP;
718         case WPA_CIPHER_TKIP:
719         default:
720                 return CIPHER_TKIP;
721         }
722 }
723
724
725 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
726 {
727         switch (key_mgmt) {
728         case WPA_KEY_MGMT_NONE:
729                 return KEY_MGMT_NONE;
730         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
731                 return KEY_MGMT_802_1X_NO_WPA;
732         case WPA_KEY_MGMT_IEEE8021X:
733                 return KEY_MGMT_802_1X;
734         case WPA_KEY_MGMT_WPA_NONE:
735                 return KEY_MGMT_WPA_NONE;
736         case WPA_KEY_MGMT_FT_IEEE8021X:
737                 return KEY_MGMT_FT_802_1X;
738         case WPA_KEY_MGMT_FT_PSK:
739                 return KEY_MGMT_FT_PSK;
740         case WPA_KEY_MGMT_IEEE8021X_SHA256:
741                 return KEY_MGMT_802_1X_SHA256;
742         case WPA_KEY_MGMT_PSK_SHA256:
743                 return KEY_MGMT_PSK_SHA256;
744         case WPA_KEY_MGMT_WPS:
745                 return KEY_MGMT_WPS;
746         case WPA_KEY_MGMT_PSK:
747         default:
748                 return KEY_MGMT_PSK;
749         }
750 }
751
752
753 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
754                                          struct wpa_ssid *ssid,
755                                          struct wpa_ie_data *ie)
756 {
757         int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
758         if (ret) {
759                 if (ret == -2) {
760                         wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
761                                 "from association info");
762                 }
763                 return -1;
764         }
765
766         wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
767                    "suites");
768         if (!(ie->group_cipher & ssid->group_cipher)) {
769                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
770                         "cipher 0x%x (mask 0x%x) - reject",
771                         ie->group_cipher, ssid->group_cipher);
772                 return -1;
773         }
774         if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
775                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
776                         "cipher 0x%x (mask 0x%x) - reject",
777                         ie->pairwise_cipher, ssid->pairwise_cipher);
778                 return -1;
779         }
780         if (!(ie->key_mgmt & ssid->key_mgmt)) {
781                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
782                         "management 0x%x (mask 0x%x) - reject",
783                         ie->key_mgmt, ssid->key_mgmt);
784                 return -1;
785         }
786
787 #ifdef CONFIG_IEEE80211W
788         if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
789             ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
790                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
791                         "that does not support management frame protection - "
792                         "reject");
793                 return -1;
794         }
795 #endif /* CONFIG_IEEE80211W */
796
797         return 0;
798 }
799
800
801 /**
802  * wpa_supplicant_set_suites - Set authentication and encryption parameters
803  * @wpa_s: Pointer to wpa_supplicant data
804  * @bss: Scan results for the selected BSS, or %NULL if not available
805  * @ssid: Configuration data for the selected network
806  * @wpa_ie: Buffer for the WPA/RSN IE
807  * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
808  * used buffer length in case the functions returns success.
809  * Returns: 0 on success or -1 on failure
810  *
811  * This function is used to configure authentication and encryption parameters
812  * based on the network configuration and scan result for the selected BSS (if
813  * available).
814  */
815 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
816                               struct wpa_bss *bss, struct wpa_ssid *ssid,
817                               u8 *wpa_ie, size_t *wpa_ie_len)
818 {
819         struct wpa_ie_data ie;
820         int sel, proto;
821         const u8 *bss_wpa, *bss_rsn;
822
823         if (bss) {
824                 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
825                 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
826         } else
827                 bss_wpa = bss_rsn = NULL;
828
829         if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
830             wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
831             (ie.group_cipher & ssid->group_cipher) &&
832             (ie.pairwise_cipher & ssid->pairwise_cipher) &&
833             (ie.key_mgmt & ssid->key_mgmt)) {
834                 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
835                 proto = WPA_PROTO_RSN;
836         } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
837                    wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
838                    (ie.group_cipher & ssid->group_cipher) &&
839                    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
840                    (ie.key_mgmt & ssid->key_mgmt)) {
841                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
842                 proto = WPA_PROTO_WPA;
843         } else if (bss) {
844                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
845                 return -1;
846         } else {
847                 if (ssid->proto & WPA_PROTO_RSN)
848                         proto = WPA_PROTO_RSN;
849                 else
850                         proto = WPA_PROTO_WPA;
851                 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
852                         os_memset(&ie, 0, sizeof(ie));
853                         ie.group_cipher = ssid->group_cipher;
854                         ie.pairwise_cipher = ssid->pairwise_cipher;
855                         ie.key_mgmt = ssid->key_mgmt;
856 #ifdef CONFIG_IEEE80211W
857                         ie.mgmt_group_cipher =
858                                 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
859                                 WPA_CIPHER_AES_128_CMAC : 0;
860 #endif /* CONFIG_IEEE80211W */
861                         wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
862                                    "on configuration");
863                 } else
864                         proto = ie.proto;
865         }
866
867         wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
868                    "pairwise %d key_mgmt %d proto %d",
869                    ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
870 #ifdef CONFIG_IEEE80211W
871         if (ssid->ieee80211w) {
872                 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
873                            ie.mgmt_group_cipher);
874         }
875 #endif /* CONFIG_IEEE80211W */
876
877         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
878         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
879                          !!(ssid->proto & WPA_PROTO_RSN));
880
881         if (bss || !wpa_s->ap_ies_from_associnfo) {
882                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
883                                          bss_wpa ? 2 + bss_wpa[1] : 0) ||
884                     wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
885                                          bss_rsn ? 2 + bss_rsn[1] : 0))
886                         return -1;
887         }
888
889         sel = ie.group_cipher & ssid->group_cipher;
890         if (sel & WPA_CIPHER_CCMP) {
891                 wpa_s->group_cipher = WPA_CIPHER_CCMP;
892                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
893         } else if (sel & WPA_CIPHER_TKIP) {
894                 wpa_s->group_cipher = WPA_CIPHER_TKIP;
895                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
896         } else if (sel & WPA_CIPHER_WEP104) {
897                 wpa_s->group_cipher = WPA_CIPHER_WEP104;
898                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
899         } else if (sel & WPA_CIPHER_WEP40) {
900                 wpa_s->group_cipher = WPA_CIPHER_WEP40;
901                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
902         } else {
903                 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
904                 return -1;
905         }
906
907         sel = ie.pairwise_cipher & ssid->pairwise_cipher;
908         if (sel & WPA_CIPHER_CCMP) {
909                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
910                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
911         } else if (sel & WPA_CIPHER_TKIP) {
912                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
913                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
914         } else if (sel & WPA_CIPHER_NONE) {
915                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
916                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
917         } else {
918                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
919                            "cipher.");
920                 return -1;
921         }
922
923         sel = ie.key_mgmt & ssid->key_mgmt;
924         if (0) {
925 #ifdef CONFIG_IEEE80211R
926         } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
927                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
928                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
929         } else if (sel & WPA_KEY_MGMT_FT_PSK) {
930                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
931                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
932 #endif /* CONFIG_IEEE80211R */
933 #ifdef CONFIG_IEEE80211W
934         } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
935                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
936                 wpa_msg(wpa_s, MSG_DEBUG,
937                         "WPA: using KEY_MGMT 802.1X with SHA256");
938         } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
939                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
940                 wpa_msg(wpa_s, MSG_DEBUG,
941                         "WPA: using KEY_MGMT PSK with SHA256");
942 #endif /* CONFIG_IEEE80211W */
943         } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
944                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
945                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
946         } else if (sel & WPA_KEY_MGMT_PSK) {
947                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
948                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
949         } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
950                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
951                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
952         } else {
953                 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
954                            "key management type.");
955                 return -1;
956         }
957
958         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
959         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
960                          wpa_s->pairwise_cipher);
961         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
962
963 #ifdef CONFIG_IEEE80211W
964         sel = ie.mgmt_group_cipher;
965         if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
966             !(ie.capabilities & WPA_CAPABILITY_MFPC))
967                 sel = 0;
968         if (sel & WPA_CIPHER_AES_128_CMAC) {
969                 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
970                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
971                         "AES-128-CMAC");
972         } else {
973                 wpa_s->mgmt_group_cipher = 0;
974                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
975         }
976         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
977                          wpa_s->mgmt_group_cipher);
978         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
979 #endif /* CONFIG_IEEE80211W */
980
981         if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
982                 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
983                 return -1;
984         }
985
986         if (ssid->key_mgmt &
987             (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
988                 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
989         else
990                 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
991
992         return 0;
993 }
994
995
996 /**
997  * wpa_supplicant_associate - Request association
998  * @wpa_s: Pointer to wpa_supplicant data
999  * @bss: Scan results for the selected BSS, or %NULL if not available
1000  * @ssid: Configuration data for the selected network
1001  *
1002  * This function is used to request %wpa_supplicant to associate with a BSS.
1003  */
1004 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1005                               struct wpa_bss *bss, struct wpa_ssid *ssid)
1006 {
1007         u8 wpa_ie[200];
1008         size_t wpa_ie_len;
1009         int use_crypt, ret, i, bssid_changed;
1010         int algs = WPA_AUTH_ALG_OPEN;
1011         enum wpa_cipher cipher_pairwise, cipher_group;
1012         struct wpa_driver_associate_params params;
1013         int wep_keys_set = 0;
1014         struct wpa_driver_capa capa;
1015         int assoc_failed = 0;
1016         struct wpa_ssid *old_ssid;
1017
1018         if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1019             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1020 #ifdef CONFIG_AP
1021                 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1022                         wpa_printf(MSG_INFO, "Driver does not support AP "
1023                                    "mode");
1024                         return;
1025                 }
1026                 wpa_supplicant_create_ap(wpa_s, ssid);
1027                 wpa_s->current_bss = bss;
1028 #else /* CONFIG_AP */
1029                 wpa_printf(MSG_ERROR, "AP mode support not included in the "
1030                            "build");
1031 #endif /* CONFIG_AP */
1032                 return;
1033         }
1034
1035         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1036             ssid->mode == IEEE80211_MODE_INFRA) {
1037                 sme_authenticate(wpa_s, bss, ssid);
1038                 return;
1039         }
1040
1041         os_memset(&params, 0, sizeof(params));
1042         wpa_s->reassociate = 0;
1043         if (bss) {
1044 #ifdef CONFIG_IEEE80211R
1045                 const u8 *ie, *md = NULL;
1046 #endif /* CONFIG_IEEE80211R */
1047                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1048                         " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1049                         wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1050                 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1051                 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1052                 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1053                 if (bssid_changed)
1054                         wpas_notify_bssid_changed(wpa_s);
1055 #ifdef CONFIG_IEEE80211R
1056                 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1057                 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1058                         md = ie + 2;
1059                 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1060                 if (md) {
1061                         /* Prepare for the next transition */
1062                         wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1063                 }
1064 #endif /* CONFIG_IEEE80211R */
1065 #ifdef CONFIG_WPS
1066         } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1067                    wpa_s->conf->ap_scan == 2 &&
1068                    (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1069                 /* Use ap_scan==1 style network selection to find the network
1070                  */
1071                 wpa_s->scan_req = 2;
1072                 wpa_s->reassociate = 1;
1073                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1074                 return;
1075 #endif /* CONFIG_WPS */
1076         } else {
1077                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1078                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1079                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1080         }
1081         wpa_supplicant_cancel_scan(wpa_s);
1082
1083         /* Starting new association, so clear the possibly used WPA IE from the
1084          * previous association. */
1085         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1086
1087 #ifdef IEEE8021X_EAPOL
1088         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1089                 if (ssid->leap) {
1090                         if (ssid->non_leap == 0)
1091                                 algs = WPA_AUTH_ALG_LEAP;
1092                         else
1093                                 algs |= WPA_AUTH_ALG_LEAP;
1094                 }
1095         }
1096 #endif /* IEEE8021X_EAPOL */
1097         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1098         if (ssid->auth_alg) {
1099                 algs = ssid->auth_alg;
1100                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1101                            algs);
1102         }
1103
1104         if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1105                     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1106             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1107                                WPA_KEY_MGMT_FT_IEEE8021X |
1108                                WPA_KEY_MGMT_FT_PSK |
1109                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
1110                                WPA_KEY_MGMT_PSK_SHA256))) {
1111                 int try_opportunistic;
1112                 try_opportunistic = ssid->proactive_key_caching &&
1113                         (ssid->proto & WPA_PROTO_RSN);
1114                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1115                                             wpa_s->current_ssid,
1116                                             try_opportunistic) == 0)
1117                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1118                 wpa_ie_len = sizeof(wpa_ie);
1119                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1120                                               wpa_ie, &wpa_ie_len)) {
1121                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1122                                    "management and encryption suites");
1123                         return;
1124                 }
1125         } else if (ssid->key_mgmt &
1126                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1127                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1128                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1129                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1130                 wpa_ie_len = sizeof(wpa_ie);
1131                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1132                                               wpa_ie, &wpa_ie_len)) {
1133                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1134                                    "management and encryption suites (no scan "
1135                                    "results)");
1136                         return;
1137                 }
1138 #ifdef CONFIG_WPS
1139         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1140                 struct wpabuf *wps_ie;
1141                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1142                 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1143                         wpa_ie_len = wpabuf_len(wps_ie);
1144                         os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1145                 } else
1146                         wpa_ie_len = 0;
1147                 wpabuf_free(wps_ie);
1148                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1149                 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1150                         params.wps = WPS_MODE_PRIVACY;
1151                 else
1152                         params.wps = WPS_MODE_OPEN;
1153 #endif /* CONFIG_WPS */
1154         } else {
1155                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1156                 wpa_ie_len = 0;
1157         }
1158
1159 #ifdef CONFIG_P2P
1160         if (wpa_s->global->p2p) {
1161                 u8 *pos;
1162                 size_t len;
1163                 int res;
1164                 int p2p_group;
1165                 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1166                 pos = wpa_ie + wpa_ie_len;
1167                 len = sizeof(wpa_ie) - wpa_ie_len;
1168                 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1169                 if (res >= 0)
1170                         wpa_ie_len += res;
1171         }
1172 #endif /* CONFIG_P2P */
1173
1174         wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1175         use_crypt = 1;
1176         cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1177         cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1178         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1179             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1180                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1181                         use_crypt = 0;
1182                 if (wpa_set_wep_keys(wpa_s, ssid)) {
1183                         use_crypt = 1;
1184                         wep_keys_set = 1;
1185                 }
1186         }
1187         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1188                 use_crypt = 0;
1189
1190 #ifdef IEEE8021X_EAPOL
1191         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1192                 if ((ssid->eapol_flags &
1193                      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1194                       EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1195                     !wep_keys_set) {
1196                         use_crypt = 0;
1197                 } else {
1198                         /* Assume that dynamic WEP-104 keys will be used and
1199                          * set cipher suites in order for drivers to expect
1200                          * encryption. */
1201                         cipher_pairwise = cipher_group = CIPHER_WEP104;
1202                 }
1203         }
1204 #endif /* IEEE8021X_EAPOL */
1205
1206         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1207                 /* Set the key before (and later after) association */
1208                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1209         }
1210
1211         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1212         if (bss) {
1213                 params.bssid = bss->bssid;
1214                 params.ssid = bss->ssid;
1215                 params.ssid_len = bss->ssid_len;
1216                 params.freq = bss->freq;
1217         } else {
1218                 params.ssid = ssid->ssid;
1219                 params.ssid_len = ssid->ssid_len;
1220         }
1221         if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1222             params.freq == 0)
1223                 params.freq = ssid->frequency; /* Initial channel for IBSS */
1224         params.wpa_ie = wpa_ie;
1225         params.wpa_ie_len = wpa_ie_len;
1226         params.pairwise_suite = cipher_pairwise;
1227         params.group_suite = cipher_group;
1228         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1229         params.auth_alg = algs;
1230         params.mode = ssid->mode;
1231         for (i = 0; i < NUM_WEP_KEYS; i++) {
1232                 if (ssid->wep_key_len[i])
1233                         params.wep_key[i] = ssid->wep_key[i];
1234                 params.wep_key_len[i] = ssid->wep_key_len[i];
1235         }
1236         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1237
1238         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1239             (params.key_mgmt_suite == KEY_MGMT_PSK ||
1240              params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1241                 params.passphrase = ssid->passphrase;
1242                 if (ssid->psk_set)
1243                         params.psk = ssid->psk;
1244         }
1245
1246         params.drop_unencrypted = use_crypt;
1247
1248 #ifdef CONFIG_IEEE80211W
1249         params.mgmt_frame_protection = ssid->ieee80211w;
1250         if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1251                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1252                 struct wpa_ie_data ie;
1253                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1254                     ie.capabilities &
1255                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1256                         wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1257                                    "require MFP");
1258                         params.mgmt_frame_protection =
1259                                 MGMT_FRAME_PROTECTION_REQUIRED;
1260                 }
1261         }
1262 #endif /* CONFIG_IEEE80211W */
1263
1264 #ifdef CONFIG_P2P
1265         if (wpa_s->global->p2p &&
1266             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1267                 params.p2p = 1;
1268 #endif /* CONFIG_P2P */
1269
1270         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1271                 ret = ieee80211_sta_associate(wpa_s, &params);
1272         else
1273                 ret = wpa_drv_associate(wpa_s, &params);
1274         if (ret < 0) {
1275                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1276                         "failed");
1277                 /* try to continue anyway; new association will be tried again
1278                  * after timeout */
1279                 assoc_failed = 1;
1280         }
1281
1282         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1283                 /* Set the key after the association just in case association
1284                  * cleared the previously configured key. */
1285                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1286                 /* No need to timeout authentication since there is no key
1287                  * management. */
1288                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1289                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1290 #ifdef CONFIG_IBSS_RSN
1291         } else if (ssid->mode == WPAS_MODE_IBSS &&
1292                    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1293                    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1294                 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1295                 /*
1296                  * RSN IBSS authentication is per-STA and we can disable the
1297                  * per-BSSID authentication.
1298                  */
1299                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1300 #endif /* CONFIG_IBSS_RSN */
1301         } else {
1302                 /* Timeout for IEEE 802.11 authentication and association */
1303                 int timeout = 60;
1304
1305                 if (assoc_failed) {
1306                         /* give IBSS a bit more time */
1307                         timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1308                 } else if (wpa_s->conf->ap_scan == 1) {
1309                         /* give IBSS a bit more time */
1310                         timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1311                 }
1312                 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1313         }
1314
1315         if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1316             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1317                 /* Set static WEP keys again */
1318                 wpa_set_wep_keys(wpa_s, ssid);
1319         }
1320
1321         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1322                 /*
1323                  * Do not allow EAP session resumption between different
1324                  * network configurations.
1325                  */
1326                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1327         }
1328         old_ssid = wpa_s->current_ssid;
1329         wpa_s->current_ssid = ssid;
1330         wpa_s->current_bss = bss;
1331         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1332         wpa_supplicant_initiate_eapol(wpa_s);
1333         if (old_ssid != wpa_s->current_ssid)
1334                 wpas_notify_network_changed(wpa_s);
1335 }
1336
1337
1338 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1339                                             const u8 *addr)
1340 {
1341         struct wpa_ssid *old_ssid;
1342
1343         wpa_clear_keys(wpa_s, addr);
1344         wpa_supplicant_mark_disassoc(wpa_s);
1345         old_ssid = wpa_s->current_ssid;
1346         wpa_s->current_ssid = NULL;
1347         wpa_s->current_bss = NULL;
1348         wpa_sm_set_config(wpa_s->wpa, NULL);
1349         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1350         if (old_ssid != wpa_s->current_ssid)
1351                 wpas_notify_network_changed(wpa_s);
1352         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1353 }
1354
1355
1356 /**
1357  * wpa_supplicant_disassociate - Disassociate the current connection
1358  * @wpa_s: Pointer to wpa_supplicant data
1359  * @reason_code: IEEE 802.11 reason code for the disassociate frame
1360  *
1361  * This function is used to request %wpa_supplicant to disassociate with the
1362  * current AP.
1363  */
1364 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1365                                  int reason_code)
1366 {
1367         u8 *addr = NULL;
1368
1369         if (!is_zero_ether_addr(wpa_s->bssid)) {
1370                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1371                         ieee80211_sta_disassociate(wpa_s, reason_code);
1372                 else
1373                         wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1374                 addr = wpa_s->bssid;
1375         }
1376
1377         wpa_supplicant_clear_connection(wpa_s, addr);
1378 }
1379
1380
1381 /**
1382  * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1383  * @wpa_s: Pointer to wpa_supplicant data
1384  * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1385  *
1386  * This function is used to request %wpa_supplicant to deauthenticate from the
1387  * current AP.
1388  */
1389 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1390                                    int reason_code)
1391 {
1392         u8 *addr = NULL;
1393
1394         if (!is_zero_ether_addr(wpa_s->bssid)) {
1395                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1396                         ieee80211_sta_deauthenticate(wpa_s, reason_code);
1397                 else
1398                         wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1399                                                reason_code);
1400                 addr = wpa_s->bssid;
1401         }
1402
1403         wpa_supplicant_clear_connection(wpa_s, addr);
1404 }
1405
1406
1407 /**
1408  * wpa_supplicant_enable_network - Mark a configured network as enabled
1409  * @wpa_s: wpa_supplicant structure for a network interface
1410  * @ssid: wpa_ssid structure for a configured network or %NULL
1411  *
1412  * Enables the specified network or all networks if no network specified.
1413  */
1414 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1415                                    struct wpa_ssid *ssid)
1416 {
1417         struct wpa_ssid *other_ssid;
1418         int was_disabled;
1419
1420         if (ssid == NULL) {
1421                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1422                      other_ssid = other_ssid->next) {
1423                         if (other_ssid->disabled == 2)
1424                                 continue; /* do not change persistent P2P group
1425                                            * data */
1426                         if (other_ssid == wpa_s->current_ssid &&
1427                             other_ssid->disabled)
1428                                 wpa_s->reassociate = 1;
1429
1430                         was_disabled = other_ssid->disabled;
1431
1432                         other_ssid->disabled = 0;
1433
1434                         if (was_disabled != other_ssid->disabled)
1435                                 wpas_notify_network_enabled_changed(
1436                                         wpa_s, other_ssid);
1437                 }
1438                 if (wpa_s->reassociate)
1439                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1440         } else if (ssid->disabled && ssid->disabled != 2) {
1441                 if (wpa_s->current_ssid == NULL) {
1442                         /*
1443                          * Try to reassociate since there is no current
1444                          * configuration and a new network was made available.
1445                          */
1446                         wpa_s->reassociate = 1;
1447                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1448                 }
1449
1450                 was_disabled = ssid->disabled;
1451
1452                 ssid->disabled = 0;
1453
1454                 if (was_disabled != ssid->disabled)
1455                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1456         }
1457 }
1458
1459
1460 /**
1461  * wpa_supplicant_disable_network - Mark a configured network as disabled
1462  * @wpa_s: wpa_supplicant structure for a network interface
1463  * @ssid: wpa_ssid structure for a configured network or %NULL
1464  *
1465  * Disables the specified network or all networks if no network specified.
1466  */
1467 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1468                                     struct wpa_ssid *ssid)
1469 {
1470         struct wpa_ssid *other_ssid;
1471         int was_disabled;
1472
1473         if (ssid == NULL) {
1474                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1475                      other_ssid = other_ssid->next) {
1476                         was_disabled = other_ssid->disabled;
1477                         if (was_disabled == 2)
1478                                 continue; /* do not change persistent P2P group
1479                                            * data */
1480
1481                         other_ssid->disabled = 1;
1482
1483                         if (was_disabled != other_ssid->disabled)
1484                                 wpas_notify_network_enabled_changed(
1485                                         wpa_s, other_ssid);
1486                 }
1487                 if (wpa_s->current_ssid)
1488                         wpa_supplicant_disassociate(
1489                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1490         } else if (ssid->disabled != 2) {
1491                 if (ssid == wpa_s->current_ssid)
1492                         wpa_supplicant_disassociate(
1493                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1494
1495                 was_disabled = ssid->disabled;
1496
1497                 ssid->disabled = 1;
1498
1499                 if (was_disabled != ssid->disabled)
1500                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1501         }
1502 }
1503
1504
1505 /**
1506  * wpa_supplicant_select_network - Attempt association with a network
1507  * @wpa_s: wpa_supplicant structure for a network interface
1508  * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1509  */
1510 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1511                                    struct wpa_ssid *ssid)
1512 {
1513
1514         struct wpa_ssid *other_ssid;
1515
1516         if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1517                 wpa_supplicant_disassociate(
1518                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1519
1520         /*
1521          * Mark all other networks disabled or mark all networks enabled if no
1522          * network specified.
1523          */
1524         for (other_ssid = wpa_s->conf->ssid; other_ssid;
1525              other_ssid = other_ssid->next) {
1526                 int was_disabled = other_ssid->disabled;
1527                 if (was_disabled == 2)
1528                         continue; /* do not change persistent P2P group data */
1529
1530                 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1531
1532                 if (was_disabled != other_ssid->disabled)
1533                         wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1534         }
1535         wpa_s->disconnected = 0;
1536         wpa_s->reassociate = 1;
1537         wpa_supplicant_req_scan(wpa_s, 0, 0);
1538
1539         if (ssid)
1540                 wpas_notify_network_selected(wpa_s, ssid);
1541 }
1542
1543
1544 /**
1545  * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1546  * @wpa_s: wpa_supplicant structure for a network interface
1547  * @ap_scan: AP scan mode
1548  * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1549  *
1550  */
1551 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1552 {
1553
1554         int old_ap_scan;
1555
1556         if (ap_scan < 0 || ap_scan > 2)
1557                 return -1;
1558
1559         old_ap_scan = wpa_s->conf->ap_scan;
1560         wpa_s->conf->ap_scan = ap_scan;
1561
1562         if (old_ap_scan != wpa_s->conf->ap_scan)
1563                 wpas_notify_ap_scan_changed(wpa_s);
1564
1565         return 0;
1566 }
1567
1568
1569 /**
1570  * wpa_supplicant_set_debug_params - Set global debug params
1571  * @global: wpa_global structure
1572  * @debug_level: debug level
1573  * @debug_timestamp: determines if show timestamp in debug data
1574  * @debug_show_keys: determines if show keys in debug data
1575  * Returns: 0 if succeed or -1 if debug_level has wrong value
1576  */
1577 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1578                                     int debug_timestamp, int debug_show_keys)
1579 {
1580
1581         int old_level, old_timestamp, old_show_keys;
1582
1583         /* check for allowed debuglevels */
1584         if (debug_level != MSG_MSGDUMP &&
1585             debug_level != MSG_DEBUG &&
1586             debug_level != MSG_INFO &&
1587             debug_level != MSG_WARNING &&
1588             debug_level != MSG_ERROR)
1589                 return -1;
1590
1591         old_level = wpa_debug_level;
1592         old_timestamp = wpa_debug_timestamp;
1593         old_show_keys = wpa_debug_show_keys;
1594
1595         wpa_debug_level = debug_level;
1596         wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1597         wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1598
1599         if (wpa_debug_level != old_level)
1600                 wpas_notify_debug_level_changed(global);
1601         if (wpa_debug_timestamp != old_timestamp)
1602                 wpas_notify_debug_timestamp_changed(global);
1603         if (wpa_debug_show_keys != old_show_keys)
1604                 wpas_notify_debug_show_keys_changed(global);
1605
1606         return 0;
1607 }
1608
1609
1610 /**
1611  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1612  * @wpa_s: Pointer to wpa_supplicant data
1613  * Returns: A pointer to the current network structure or %NULL on failure
1614  */
1615 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1616 {
1617         struct wpa_ssid *entry;
1618         u8 ssid[MAX_SSID_LEN];
1619         int res;
1620         size_t ssid_len;
1621         u8 bssid[ETH_ALEN];
1622         int wired;
1623
1624         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1625                 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1626                         wpa_printf(MSG_WARNING, "Could not read SSID from "
1627                                    "MLME.");
1628                         return NULL;
1629                 }
1630         } else {
1631                 res = wpa_drv_get_ssid(wpa_s, ssid);
1632                 if (res < 0) {
1633                         wpa_printf(MSG_WARNING, "Could not read SSID from "
1634                                    "driver.");
1635                         return NULL;
1636                 }
1637                 ssid_len = res;
1638         }
1639
1640         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1641                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1642         else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1643                 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1644                 return NULL;
1645         }
1646
1647         wired = wpa_s->conf->ap_scan == 0 &&
1648                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1649
1650         entry = wpa_s->conf->ssid;
1651         while (entry) {
1652                 if (!entry->disabled &&
1653                     ((ssid_len == entry->ssid_len &&
1654                       os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1655                     (!entry->bssid_set ||
1656                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1657                         return entry;
1658 #ifdef CONFIG_WPS
1659                 if (!entry->disabled &&
1660                     (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1661                     (entry->ssid == NULL || entry->ssid_len == 0) &&
1662                     (!entry->bssid_set ||
1663                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1664                         return entry;
1665 #endif /* CONFIG_WPS */
1666                 entry = entry->next;
1667         }
1668
1669         return NULL;
1670 }
1671
1672
1673 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1674                                      const char *name)
1675 {
1676         int i;
1677         size_t len;
1678         const char *pos;
1679
1680         if (wpa_s == NULL)
1681                 return -1;
1682
1683         if (wpa_drivers[0] == NULL) {
1684                 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1685                            "wpa_supplicant.");
1686                 return -1;
1687         }
1688
1689         if (name == NULL) {
1690                 /* default to first driver in the list */
1691                 wpa_s->driver = wpa_drivers[0];
1692                 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1693                 return 0;
1694         }
1695
1696         pos = os_strchr(name, ',');
1697         if (pos)
1698                 len = pos - name;
1699         else
1700                 len = os_strlen(name);
1701         for (i = 0; wpa_drivers[i]; i++) {
1702                 if (os_strlen(wpa_drivers[i]->name) == len &&
1703                     os_strncmp(name, wpa_drivers[i]->name, len) ==
1704                     0) {
1705                         wpa_s->driver = wpa_drivers[i];
1706                         wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
1707                         return 0;
1708                 }
1709         }
1710
1711         wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1712         return -1;
1713 }
1714
1715
1716 /**
1717  * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1718  * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1719  *      with struct wpa_driver_ops::init()
1720  * @src_addr: Source address of the EAPOL frame
1721  * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1722  * @len: Length of the EAPOL data
1723  *
1724  * This function is called for each received EAPOL frame. Most driver
1725  * interfaces rely on more generic OS mechanism for receiving frames through
1726  * l2_packet, but if such a mechanism is not available, the driver wrapper may
1727  * take care of received EAPOL frames and deliver them to the core supplicant
1728  * code by calling this function.
1729  */
1730 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1731                              const u8 *buf, size_t len)
1732 {
1733         struct wpa_supplicant *wpa_s = ctx;
1734
1735         wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1736         wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1737
1738         if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1739                 /*
1740                  * There is possible race condition between receiving the
1741                  * association event and the EAPOL frame since they are coming
1742                  * through different paths from the driver. In order to avoid
1743                  * issues in trying to process the EAPOL frame before receiving
1744                  * association information, lets queue it for processing until
1745                  * the association event is received.
1746                  */
1747                 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1748                            "received EAPOL frame");
1749                 wpabuf_free(wpa_s->pending_eapol_rx);
1750                 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1751                 if (wpa_s->pending_eapol_rx) {
1752                         os_get_time(&wpa_s->pending_eapol_rx_time);
1753                         os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1754                                   ETH_ALEN);
1755                 }
1756                 return;
1757         }
1758
1759 #ifdef CONFIG_AP
1760         if (wpa_s->ap_iface) {
1761                 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1762                 return;
1763         }
1764 #endif /* CONFIG_AP */
1765
1766         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1767                 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1768                            "no key management is configured");
1769                 return;
1770         }
1771
1772         if (wpa_s->eapol_received == 0 &&
1773             (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1774              !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1775              wpa_s->wpa_state != WPA_COMPLETED) &&
1776             (wpa_s->current_ssid == NULL ||
1777              wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1778                 /* Timeout for completing IEEE 802.1X and WPA authentication */
1779                 wpa_supplicant_req_auth_timeout(
1780                         wpa_s,
1781                         (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1782                          wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1783                          wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1784                         70 : 10, 0);
1785         }
1786         wpa_s->eapol_received++;
1787
1788         if (wpa_s->countermeasures) {
1789                 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1790                            "packet");
1791                 return;
1792         }
1793
1794 #ifdef CONFIG_IBSS_RSN
1795         if (wpa_s->current_ssid &&
1796             wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1797                 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1798                 return;
1799         }
1800 #endif /* CONFIG_IBSS_RSN */
1801
1802         /* Source address of the incoming EAPOL frame could be compared to the
1803          * current BSSID. However, it is possible that a centralized
1804          * Authenticator could be using another MAC address than the BSSID of
1805          * an AP, so just allow any address to be used for now. The replies are
1806          * still sent to the current BSSID (if available), though. */
1807
1808         os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1809         if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1810             eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1811                 return;
1812         wpa_drv_poll(wpa_s);
1813         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1814                 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1815         else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1816                 /*
1817                  * Set portValid = TRUE here since we are going to skip 4-way
1818                  * handshake processing which would normally set portValid. We
1819                  * need this to allow the EAPOL state machines to be completed
1820                  * without going through EAPOL-Key handshake.
1821                  */
1822                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1823         }
1824 }
1825
1826
1827 /**
1828  * wpa_supplicant_driver_init - Initialize driver interface parameters
1829  * @wpa_s: Pointer to wpa_supplicant data
1830  * Returns: 0 on success, -1 on failure
1831  *
1832  * This function is called to initialize driver interface parameters.
1833  * wpa_drv_init() must have been called before this function to initialize the
1834  * driver interface.
1835  */
1836 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1837 {
1838         static int interface_count = 0;
1839
1840         if (wpa_s->driver->send_eapol) {
1841                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1842                 if (addr)
1843                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1844         } else if (!(wpa_s->drv_flags &
1845                      WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
1846                 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1847                                            wpa_drv_get_mac_addr(wpa_s),
1848                                            ETH_P_EAPOL,
1849                                            wpa_supplicant_rx_eapol, wpa_s, 0);
1850                 if (wpa_s->l2 == NULL)
1851                         return -1;
1852         } else {
1853                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1854                 if (addr)
1855                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1856         }
1857
1858         if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1859                 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1860                 return -1;
1861         }
1862
1863         wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1864                    MAC2STR(wpa_s->own_addr));
1865
1866         if (wpa_s->bridge_ifname[0]) {
1867                 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1868                            " '%s'", wpa_s->bridge_ifname);
1869                 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1870                                               wpa_s->own_addr,
1871                                               ETH_P_EAPOL,
1872                                               wpa_supplicant_rx_eapol, wpa_s,
1873                                               0);
1874                 if (wpa_s->l2_br == NULL) {
1875                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1876                                    "connection for the bridge interface '%s'",
1877                                    wpa_s->bridge_ifname);
1878                         return -1;
1879                 }
1880         }
1881
1882         wpa_clear_keys(wpa_s, NULL);
1883
1884         /* Make sure that TKIP countermeasures are not left enabled (could
1885          * happen if wpa_supplicant is killed during countermeasures. */
1886         wpa_drv_set_countermeasures(wpa_s, 0);
1887
1888         wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1889         wpa_drv_flush_pmkid(wpa_s);
1890
1891         wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1892         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
1893                 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1894                 interface_count++;
1895         } else
1896                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1897
1898         return 0;
1899 }
1900
1901
1902 static int wpa_supplicant_daemon(const char *pid_file)
1903 {
1904         wpa_printf(MSG_DEBUG, "Daemonize..");
1905         return os_daemonize(pid_file);
1906 }
1907
1908
1909 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1910 {
1911         struct wpa_supplicant *wpa_s;
1912
1913         wpa_s = os_zalloc(sizeof(*wpa_s));
1914         if (wpa_s == NULL)
1915                 return NULL;
1916         wpa_s->scan_req = 1;
1917         wpa_s->new_connection = 1;
1918         wpa_s->parent = wpa_s;
1919
1920         return wpa_s;
1921 }
1922
1923
1924 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1925                                      struct wpa_interface *iface)
1926 {
1927         const char *ifname, *driver;
1928         struct wpa_driver_capa capa;
1929
1930         wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1931                    "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1932                    iface->confname ? iface->confname : "N/A",
1933                    iface->driver ? iface->driver : "default",
1934                    iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1935                    iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1936
1937         if (iface->confname) {
1938 #ifdef CONFIG_BACKEND_FILE
1939                 wpa_s->confname = os_rel2abs_path(iface->confname);
1940                 if (wpa_s->confname == NULL) {
1941                         wpa_printf(MSG_ERROR, "Failed to get absolute path "
1942                                    "for configuration file '%s'.",
1943                                    iface->confname);
1944                         return -1;
1945                 }
1946                 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1947                            iface->confname, wpa_s->confname);
1948 #else /* CONFIG_BACKEND_FILE */
1949                 wpa_s->confname = os_strdup(iface->confname);
1950 #endif /* CONFIG_BACKEND_FILE */
1951                 wpa_s->conf = wpa_config_read(wpa_s->confname);
1952                 if (wpa_s->conf == NULL) {
1953                         wpa_printf(MSG_ERROR, "Failed to read or parse "
1954                                    "configuration '%s'.", wpa_s->confname);
1955                         return -1;
1956                 }
1957
1958                 /*
1959                  * Override ctrl_interface and driver_param if set on command
1960                  * line.
1961                  */
1962                 if (iface->ctrl_interface) {
1963                         os_free(wpa_s->conf->ctrl_interface);
1964                         wpa_s->conf->ctrl_interface =
1965                                 os_strdup(iface->ctrl_interface);
1966                 }
1967
1968                 if (iface->driver_param) {
1969                         os_free(wpa_s->conf->driver_param);
1970                         wpa_s->conf->driver_param =
1971                                 os_strdup(iface->driver_param);
1972                 }
1973         } else
1974                 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1975                                                      iface->driver_param);
1976
1977         if (wpa_s->conf == NULL) {
1978                 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1979                 return -1;
1980         }
1981
1982         if (iface->ifname == NULL) {
1983                 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1984                 return -1;
1985         }
1986         if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1987                 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1988                            iface->ifname);
1989                 return -1;
1990         }
1991         os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1992
1993         if (iface->bridge_ifname) {
1994                 if (os_strlen(iface->bridge_ifname) >=
1995                     sizeof(wpa_s->bridge_ifname)) {
1996                         wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1997                                    "name '%s'.", iface->bridge_ifname);
1998                         return -1;
1999                 }
2000                 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2001                            sizeof(wpa_s->bridge_ifname));
2002         }
2003
2004         /* RSNA Supplicant Key Management - INITIALIZE */
2005         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2006         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2007
2008         /* Initialize driver interface and register driver event handler before
2009          * L2 receive handler so that association events are processed before
2010          * EAPOL-Key packets if both become available for the same select()
2011          * call. */
2012         driver = iface->driver;
2013 next_driver:
2014         if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2015                 return -1;
2016
2017         wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2018         if (wpa_s->drv_priv == NULL) {
2019                 const char *pos;
2020                 pos = driver ? os_strchr(driver, ',') : NULL;
2021                 if (pos) {
2022                         wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2023                                    "interface - try next driver wrapper");
2024                         driver = pos + 1;
2025                         goto next_driver;
2026                 }
2027                 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2028                 return -1;
2029         }
2030         if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2031                 wpa_printf(MSG_ERROR, "Driver interface rejected "
2032                            "driver_param '%s'", wpa_s->conf->driver_param);
2033                 return -1;
2034         }
2035
2036         ifname = wpa_drv_get_ifname(wpa_s);
2037         if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2038                 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2039                            "name with '%s'", ifname);
2040                 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2041         }
2042
2043         if (wpa_supplicant_init_wpa(wpa_s) < 0)
2044                 return -1;
2045
2046         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2047                           wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2048                           NULL);
2049         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2050
2051         if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2052             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2053                              wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2054                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2055                            "dot11RSNAConfigPMKLifetime");
2056                 return -1;
2057         }
2058
2059         if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2060             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2061                              wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2062                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2063                         "dot11RSNAConfigPMKReauthThreshold");
2064                 return -1;
2065         }
2066
2067         if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2068             wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2069                              wpa_s->conf->dot11RSNAConfigSATimeout)) {
2070                 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2071                            "dot11RSNAConfigSATimeout");
2072                 return -1;
2073         }
2074
2075         if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2076                 wpa_s->drv_flags = capa.flags;
2077                 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2078                         if (ieee80211_sta_init(wpa_s))
2079                                 return -1;
2080                 }
2081                 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2082                 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2083         }
2084         if (wpa_s->max_remain_on_chan == 0)
2085                 wpa_s->max_remain_on_chan = 1000;
2086
2087         if (wpa_supplicant_driver_init(wpa_s) < 0)
2088                 return -1;
2089
2090         if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2091             wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2092                 wpa_printf(MSG_DEBUG, "Failed to set country");
2093                 return -1;
2094         }
2095
2096         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2097
2098         if (wpas_wps_init(wpa_s))
2099                 return -1;
2100
2101         if (wpa_supplicant_init_eapol(wpa_s) < 0)
2102                 return -1;
2103         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2104
2105         wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2106         if (wpa_s->ctrl_iface == NULL) {
2107                 wpa_printf(MSG_ERROR,
2108                            "Failed to initialize control interface '%s'.\n"
2109                            "You may have another wpa_supplicant process "
2110                            "already running or the file was\n"
2111                            "left by an unclean termination of wpa_supplicant "
2112                            "in which case you will need\n"
2113                            "to manually remove this file before starting "
2114                            "wpa_supplicant again.\n",
2115                            wpa_s->conf->ctrl_interface);
2116                 return -1;
2117         }
2118
2119 #ifdef CONFIG_IBSS_RSN
2120         wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2121         if (!wpa_s->ibss_rsn) {
2122                 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2123                 return -1;
2124         }
2125 #endif /* CONFIG_IBSS_RSN */
2126
2127 #ifdef CONFIG_P2P
2128         if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2129                 wpa_printf(MSG_ERROR, "Failed to init P2P");
2130                 return -1;
2131         }
2132 #endif /* CONFIG_P2P */
2133
2134         if (wpa_bss_init(wpa_s) < 0)
2135                 return -1;
2136
2137         return 0;
2138 }
2139
2140
2141 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2142                                         int notify)
2143 {
2144         if (wpa_s->drv_priv) {
2145                 wpa_supplicant_deauthenticate(wpa_s,
2146                                               WLAN_REASON_DEAUTH_LEAVING);
2147
2148                 wpa_drv_set_countermeasures(wpa_s, 0);
2149                 wpa_clear_keys(wpa_s, NULL);
2150         }
2151
2152         wpa_supplicant_cleanup(wpa_s);
2153
2154         if (notify)
2155                 wpas_notify_iface_removed(wpa_s);
2156
2157         if (wpa_s->drv_priv)
2158                 wpa_drv_deinit(wpa_s);
2159 }
2160
2161
2162 /**
2163  * wpa_supplicant_add_iface - Add a new network interface
2164  * @global: Pointer to global data from wpa_supplicant_init()
2165  * @iface: Interface configuration options
2166  * Returns: Pointer to the created interface or %NULL on failure
2167  *
2168  * This function is used to add new network interfaces for %wpa_supplicant.
2169  * This can be called before wpa_supplicant_run() to add interfaces before the
2170  * main event loop has been started. In addition, new interfaces can be added
2171  * dynamically while %wpa_supplicant is already running. This could happen,
2172  * e.g., when a hotplug network adapter is inserted.
2173  */
2174 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2175                                                  struct wpa_interface *iface)
2176 {
2177         struct wpa_supplicant *wpa_s;
2178         struct wpa_interface t_iface;
2179         struct wpa_ssid *ssid;
2180
2181         if (global == NULL || iface == NULL)
2182                 return NULL;
2183
2184         wpa_s = wpa_supplicant_alloc();
2185         if (wpa_s == NULL)
2186                 return NULL;
2187
2188         wpa_s->global = global;
2189
2190         t_iface = *iface;
2191         if (global->params.override_driver) {
2192                 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2193                            "('%s' -> '%s')",
2194                            iface->driver, global->params.override_driver);
2195                 t_iface.driver = global->params.override_driver;
2196         }
2197         if (global->params.override_ctrl_interface) {
2198                 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2199                            "ctrl_interface ('%s' -> '%s')",
2200                            iface->ctrl_interface,
2201                            global->params.override_ctrl_interface);
2202                 t_iface.ctrl_interface =
2203                         global->params.override_ctrl_interface;
2204         }
2205         if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2206                 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2207                            iface->ifname);
2208                 wpa_supplicant_deinit_iface(wpa_s, 0);
2209                 os_free(wpa_s);
2210                 return NULL;
2211         }
2212
2213         /* Notify the control interfaces about new iface */
2214         if (wpas_notify_iface_added(wpa_s)) {
2215                 wpa_supplicant_deinit_iface(wpa_s, 1);
2216                 os_free(wpa_s);
2217                 return NULL;
2218         }
2219
2220         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2221                 wpas_notify_network_added(wpa_s, ssid);
2222
2223         wpa_s->next = global->ifaces;
2224         global->ifaces = wpa_s;
2225
2226         wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2227
2228         return wpa_s;
2229 }
2230
2231
2232 /**
2233  * wpa_supplicant_remove_iface - Remove a network interface
2234  * @global: Pointer to global data from wpa_supplicant_init()
2235  * @wpa_s: Pointer to the network interface to be removed
2236  * Returns: 0 if interface was removed, -1 if interface was not found
2237  *
2238  * This function can be used to dynamically remove network interfaces from
2239  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2240  * addition, this function is used to remove all remaining interfaces when
2241  * %wpa_supplicant is terminated.
2242  */
2243 int wpa_supplicant_remove_iface(struct wpa_global *global,
2244                                 struct wpa_supplicant *wpa_s)
2245 {
2246         struct wpa_supplicant *prev;
2247
2248         /* Remove interface from the global list of interfaces */
2249         prev = global->ifaces;
2250         if (prev == wpa_s) {
2251                 global->ifaces = wpa_s->next;
2252         } else {
2253                 while (prev && prev->next != wpa_s)
2254                         prev = prev->next;
2255                 if (prev == NULL)
2256                         return -1;
2257                 prev->next = wpa_s->next;
2258         }
2259
2260         wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2261
2262         if (global->p2p_group_formation == wpa_s)
2263                 global->p2p_group_formation = NULL;
2264         wpa_supplicant_deinit_iface(wpa_s, 1);
2265         os_free(wpa_s);
2266
2267         return 0;
2268 }
2269
2270
2271 /**
2272  * wpa_supplicant_get_iface - Get a new network interface
2273  * @global: Pointer to global data from wpa_supplicant_init()
2274  * @ifname: Interface name
2275  * Returns: Pointer to the interface or %NULL if not found
2276  */
2277 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2278                                                  const char *ifname)
2279 {
2280         struct wpa_supplicant *wpa_s;
2281
2282         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2283                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2284                         return wpa_s;
2285         }
2286         return NULL;
2287 }
2288
2289
2290 /**
2291  * wpa_supplicant_init - Initialize %wpa_supplicant
2292  * @params: Parameters for %wpa_supplicant
2293  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2294  *
2295  * This function is used to initialize %wpa_supplicant. After successful
2296  * initialization, the returned data pointer can be used to add and remove
2297  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2298  */
2299 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2300 {
2301         struct wpa_global *global;
2302         int ret, i;
2303
2304         if (params == NULL)
2305                 return NULL;
2306
2307         wpa_debug_open_file(params->wpa_debug_file_path);
2308         if (params->wpa_debug_syslog)
2309                 wpa_debug_open_syslog();
2310
2311         ret = eap_register_methods();
2312         if (ret) {
2313                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2314                 if (ret == -2)
2315                         wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2316                                    "the same EAP type.");
2317                 return NULL;
2318         }
2319
2320         global = os_zalloc(sizeof(*global));
2321         if (global == NULL)
2322                 return NULL;
2323         dl_list_init(&global->p2p_srv_bonjour);
2324         dl_list_init(&global->p2p_srv_upnp);
2325         global->params.daemonize = params->daemonize;
2326         global->params.wait_for_monitor = params->wait_for_monitor;
2327         global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2328         if (params->pid_file)
2329                 global->params.pid_file = os_strdup(params->pid_file);
2330         if (params->ctrl_interface)
2331                 global->params.ctrl_interface =
2332                         os_strdup(params->ctrl_interface);
2333         if (params->override_driver)
2334                 global->params.override_driver =
2335                         os_strdup(params->override_driver);
2336         if (params->override_ctrl_interface)
2337                 global->params.override_ctrl_interface =
2338                         os_strdup(params->override_ctrl_interface);
2339         wpa_debug_level = global->params.wpa_debug_level =
2340                 params->wpa_debug_level;
2341         wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2342                 params->wpa_debug_show_keys;
2343         wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2344                 params->wpa_debug_timestamp;
2345
2346         if (eloop_init()) {
2347                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2348                 wpa_supplicant_deinit(global);
2349                 return NULL;
2350         }
2351
2352         global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2353         if (global->ctrl_iface == NULL) {
2354                 wpa_supplicant_deinit(global);
2355                 return NULL;
2356         }
2357
2358         if (wpas_notify_supplicant_initialized(global)) {
2359                 wpa_supplicant_deinit(global);
2360                 return NULL;
2361         }
2362
2363         for (i = 0; wpa_drivers[i]; i++)
2364                 global->drv_count++;
2365         if (global->drv_count == 0) {
2366                 wpa_printf(MSG_ERROR, "No drivers enabled");
2367                 wpa_supplicant_deinit(global);
2368                 return NULL;
2369         }
2370         global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2371         if (global->drv_priv == NULL) {
2372                 wpa_supplicant_deinit(global);
2373                 return NULL;
2374         }
2375         for (i = 0; wpa_drivers[i]; i++) {
2376                 if (!wpa_drivers[i]->global_init)
2377                         continue;
2378                 global->drv_priv[i] = wpa_drivers[i]->global_init();
2379                 if (global->drv_priv[i] == NULL) {
2380                         wpa_printf(MSG_ERROR, "Failed to initialize driver "
2381                                    "'%s'", wpa_drivers[i]->name);
2382                         wpa_supplicant_deinit(global);
2383                         return NULL;
2384                 }
2385         }
2386
2387         return global;
2388 }
2389
2390
2391 /**
2392  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2393  * @global: Pointer to global data from wpa_supplicant_init()
2394  * Returns: 0 after successful event loop run, -1 on failure
2395  *
2396  * This function starts the main event loop and continues running as long as
2397  * there are any remaining events. In most cases, this function is running as
2398  * long as the %wpa_supplicant process in still in use.
2399  */
2400 int wpa_supplicant_run(struct wpa_global *global)
2401 {
2402         struct wpa_supplicant *wpa_s;
2403
2404         if (global->params.daemonize &&
2405             wpa_supplicant_daemon(global->params.pid_file))
2406                 return -1;
2407
2408         if (global->params.wait_for_monitor) {
2409                 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2410                         if (wpa_s->ctrl_iface)
2411                                 wpa_supplicant_ctrl_iface_wait(
2412                                         wpa_s->ctrl_iface);
2413         }
2414
2415         eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2416         eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2417
2418         eloop_run();
2419
2420         return 0;
2421 }
2422
2423
2424 /**
2425  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2426  * @global: Pointer to global data from wpa_supplicant_init()
2427  *
2428  * This function is called to deinitialize %wpa_supplicant and to free all
2429  * allocated resources. Remaining network interfaces will also be removed.
2430  */
2431 void wpa_supplicant_deinit(struct wpa_global *global)
2432 {
2433         int i;
2434
2435         if (global == NULL)
2436                 return;
2437
2438 #ifdef CONFIG_P2P
2439         wpas_p2p_deinit_global(global);
2440 #endif /* CONFIG_P2P */
2441
2442         while (global->ifaces)
2443                 wpa_supplicant_remove_iface(global, global->ifaces);
2444
2445         if (global->ctrl_iface)
2446                 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2447
2448         wpas_notify_supplicant_deinitialized(global);
2449
2450         eap_peer_unregister_methods();
2451 #ifdef CONFIG_AP
2452         eap_server_unregister_methods();
2453 #endif /* CONFIG_AP */
2454
2455         for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2456                 if (!global->drv_priv[i])
2457                         continue;
2458                 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2459         }
2460         os_free(global->drv_priv);
2461
2462         eloop_destroy();
2463
2464         if (global->params.pid_file) {
2465                 os_daemonize_terminate(global->params.pid_file);
2466                 os_free(global->params.pid_file);
2467         }
2468         os_free(global->params.ctrl_interface);
2469         os_free(global->params.override_driver);
2470         os_free(global->params.override_ctrl_interface);
2471
2472         os_free(global);
2473         wpa_debug_close_syslog();
2474         wpa_debug_close_file();
2475 }
2476
2477
2478 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2479 {
2480 #ifdef CONFIG_WPS
2481         wpas_wps_update_config(wpa_s);
2482 #endif /* CONFIG_WPS */
2483
2484 #ifdef CONFIG_P2P
2485         wpas_p2p_update_config(wpa_s);
2486 #endif /* CONFIG_P2P */
2487
2488         wpa_s->conf->changed_parameters = 0;
2489 }