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